2017-09-23 04:52:21 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# Copyright (c) 2017 Intel Corporation
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
import sys
|
2017-10-09 03:20:24 +08:00
|
|
|
from enum import Enum
|
|
|
|
|
2017-12-12 21:19:25 +08:00
|
|
|
|
2017-10-09 03:20:24 +08:00
|
|
|
class Retval(Enum):
|
|
|
|
VOID = 0
|
|
|
|
U32 = 1
|
|
|
|
U64 = 2
|
2017-09-23 04:52:21 +08:00
|
|
|
|
2017-12-12 21:19:25 +08:00
|
|
|
|
2017-10-04 03:36:49 +08:00
|
|
|
def gen_macro(ret, argc):
|
2017-10-09 03:20:24 +08:00
|
|
|
if ret == Retval.VOID:
|
|
|
|
suffix = "_VOID"
|
|
|
|
elif ret == Retval.U64:
|
|
|
|
suffix = "_RET64"
|
|
|
|
else:
|
|
|
|
suffix = ""
|
|
|
|
|
|
|
|
sys.stdout.write("K_SYSCALL_DECLARE%d%s(id, name" % (argc, suffix))
|
|
|
|
if (ret != Retval.VOID):
|
2017-09-23 04:52:21 +08:00
|
|
|
sys.stdout.write(", ret")
|
|
|
|
for i in range(argc):
|
|
|
|
sys.stdout.write(", t%d, p%d" % (i, i))
|
|
|
|
sys.stdout.write(")")
|
|
|
|
|
2017-12-12 21:19:25 +08:00
|
|
|
|
2017-09-23 04:52:21 +08:00
|
|
|
def gen_fn(ret, argc, name, extern=False):
|
|
|
|
sys.stdout.write("\t%s %s %s(" %
|
2017-12-12 21:19:25 +08:00
|
|
|
(("extern" if extern else "static inline"),
|
|
|
|
("ret" if ret != Retval.VOID else "void"), name))
|
2017-09-23 04:52:21 +08:00
|
|
|
if argc == 0:
|
2017-12-12 21:19:25 +08:00
|
|
|
sys.stdout.write("void")
|
2017-09-23 04:52:21 +08:00
|
|
|
else:
|
|
|
|
for i in range(argc):
|
|
|
|
sys.stdout.write("t%d p%d" % (i, i))
|
|
|
|
if i != (argc - 1):
|
|
|
|
sys.stdout.write(", ")
|
|
|
|
sys.stdout.write(")")
|
|
|
|
|
2017-12-12 21:19:25 +08:00
|
|
|
|
2017-11-29 05:59:15 +08:00
|
|
|
def tabs(count):
|
2017-12-12 21:19:25 +08:00
|
|
|
sys.stdout.write("\t" * count)
|
|
|
|
|
2017-11-29 05:59:15 +08:00
|
|
|
|
|
|
|
def gen_make_syscall(ret, argc, tabcount):
|
|
|
|
tabs(tabcount)
|
|
|
|
|
|
|
|
# The core kernel is built with the --no-whole-archive linker option.
|
|
|
|
# For all the individual .o files which make up the kernel, if there
|
|
|
|
# are no external references to symbols within these object files,
|
|
|
|
# everything in the object file is dropped.
|
|
|
|
#
|
|
|
|
# This has a subtle interaction with system call handlers. If an object
|
|
|
|
# file has system call handler inside it, and nothing else in the
|
|
|
|
# object file is referenced, then the linker will prefer the weak
|
|
|
|
# version of the handler in the generated syscall_dispatch.c. The
|
|
|
|
# user will get an "unimplemented system call" error if the associated
|
|
|
|
# system call for that handler is made.
|
|
|
|
#
|
|
|
|
# Fix this by making a fake reference to the handler function at the
|
|
|
|
# system call site. The address gets stored inside a special section
|
|
|
|
# "hndlr_ref". This is enough to prevent the handlers from being
|
|
|
|
# dropped, and the hndlr_ref section is itself dropped from the binary
|
|
|
|
# from gc-sections; these references will not consume space.
|
|
|
|
|
2017-12-12 21:19:25 +08:00
|
|
|
sys.stdout.write(
|
|
|
|
"static _GENERIC_SECTION(hndlr_ref) __used void *href = (void *)&_handler_##name; \\\n")
|
2017-11-29 05:59:15 +08:00
|
|
|
tabs(tabcount)
|
2017-10-09 03:20:24 +08:00
|
|
|
if (ret != Retval.VOID):
|
2017-09-23 04:52:21 +08:00
|
|
|
sys.stdout.write("return (ret)")
|
2017-10-09 03:20:24 +08:00
|
|
|
if (argc <= 6 and ret != Retval.U64):
|
2017-09-23 04:52:21 +08:00
|
|
|
sys.stdout.write("_arch")
|
2017-10-09 03:20:24 +08:00
|
|
|
sys.stdout.write("_syscall%s_invoke%d(" %
|
2017-12-12 21:19:25 +08:00
|
|
|
(("_ret64" if ret == Retval.U64 else ""), argc))
|
2017-09-23 04:52:21 +08:00
|
|
|
for i in range(argc):
|
|
|
|
sys.stdout.write("(u32_t)p%d, " % (i))
|
|
|
|
sys.stdout.write("id); \\\n")
|
|
|
|
|
2017-12-12 21:19:25 +08:00
|
|
|
|
2017-09-23 04:52:21 +08:00
|
|
|
def gen_call_impl(ret, argc):
|
2017-10-09 03:20:24 +08:00
|
|
|
if (ret != Retval.VOID):
|
2017-09-23 04:52:21 +08:00
|
|
|
sys.stdout.write("return ")
|
|
|
|
sys.stdout.write("_impl_##name(")
|
|
|
|
for i in range(argc):
|
|
|
|
sys.stdout.write("p%d" % (i))
|
|
|
|
if i != (argc - 1):
|
|
|
|
sys.stdout.write(", ")
|
|
|
|
sys.stdout.write("); \\\n")
|
|
|
|
|
2017-12-12 21:19:25 +08:00
|
|
|
|
2017-09-23 04:52:21 +08:00
|
|
|
def newline():
|
|
|
|
sys.stdout.write(" \\\n")
|
|
|
|
|
2017-12-12 21:19:25 +08:00
|
|
|
|
2017-09-23 04:52:21 +08:00
|
|
|
def gen_defines_inner(ret, argc, kernel_only=False, user_only=False):
|
|
|
|
sys.stdout.write("#define ")
|
2017-10-04 03:36:49 +08:00
|
|
|
gen_macro(ret, argc)
|
2017-09-23 04:52:21 +08:00
|
|
|
newline()
|
|
|
|
|
2017-10-04 03:36:49 +08:00
|
|
|
if not user_only:
|
|
|
|
gen_fn(ret, argc, "_impl_##name", extern=True)
|
|
|
|
sys.stdout.write(";")
|
|
|
|
newline()
|
|
|
|
|
2017-12-12 21:19:25 +08:00
|
|
|
gen_fn(ret, argc, "name")
|
2017-09-23 04:52:21 +08:00
|
|
|
newline()
|
|
|
|
sys.stdout.write("\t{")
|
|
|
|
newline()
|
|
|
|
|
|
|
|
if kernel_only:
|
|
|
|
sys.stdout.write("\t\t")
|
|
|
|
gen_call_impl(ret, argc)
|
|
|
|
elif user_only:
|
2017-11-29 05:59:15 +08:00
|
|
|
gen_make_syscall(ret, argc, 2)
|
2017-09-23 04:52:21 +08:00
|
|
|
else:
|
|
|
|
sys.stdout.write("\t\tif (_is_user_context()) {")
|
|
|
|
newline()
|
|
|
|
|
2017-11-29 05:59:15 +08:00
|
|
|
gen_make_syscall(ret, argc, 3)
|
2017-09-23 04:52:21 +08:00
|
|
|
|
|
|
|
sys.stdout.write("\t\t} else {")
|
|
|
|
newline()
|
2017-10-15 02:00:12 +08:00
|
|
|
|
|
|
|
# Prevent memory access issues if the implementation function gets
|
|
|
|
# inlined
|
2017-12-12 21:19:25 +08:00
|
|
|
sys.stdout.write("\t\t\tcompiler_barrier();")
|
2017-10-15 02:00:12 +08:00
|
|
|
newline()
|
2017-09-23 04:52:21 +08:00
|
|
|
|
|
|
|
sys.stdout.write("\t\t\t")
|
|
|
|
gen_call_impl(ret, argc)
|
|
|
|
sys.stdout.write("\t\t}")
|
|
|
|
newline()
|
|
|
|
|
|
|
|
sys.stdout.write("\t}\n\n")
|
|
|
|
|
2017-10-09 03:20:24 +08:00
|
|
|
|
2017-09-23 04:52:21 +08:00
|
|
|
def gen_defines(argc, kernel_only=False, user_only=False):
|
2017-10-09 03:20:24 +08:00
|
|
|
gen_defines_inner(Retval.VOID, argc, kernel_only, user_only)
|
|
|
|
gen_defines_inner(Retval.U32, argc, kernel_only, user_only)
|
|
|
|
gen_defines_inner(Retval.U64, argc, kernel_only, user_only)
|
2017-09-23 04:52:21 +08:00
|
|
|
|
|
|
|
|
2017-12-12 21:19:25 +08:00
|
|
|
sys.stdout.write(
|
|
|
|
"/* Auto-generated by gen_syscall_header.py, do not edit! */\n\n")
|
2017-09-23 04:52:21 +08:00
|
|
|
sys.stdout.write("#ifndef GEN_SYSCALL_H\n#define GEN_SYSCALL_H\n\n")
|
2018-03-27 04:45:20 +08:00
|
|
|
sys.stdout.write("#include <syscall.h>\n")
|
2017-09-23 04:52:21 +08:00
|
|
|
|
|
|
|
for i in range(11):
|
2017-12-12 21:19:25 +08:00
|
|
|
sys.stdout.write(
|
|
|
|
"#if !defined(CONFIG_USERSPACE) || defined(__ZEPHYR_SUPERVISOR__)\n")
|
2017-09-23 04:52:21 +08:00
|
|
|
gen_defines(i, kernel_only=True)
|
|
|
|
sys.stdout.write("#elif defined(__ZEPHYR_USER__)\n")
|
|
|
|
gen_defines(i, user_only=True)
|
|
|
|
sys.stdout.write("#else /* mixed kernel/user macros */\n")
|
|
|
|
gen_defines(i)
|
|
|
|
sys.stdout.write("#endif /* mixed kernel/user macros */\n\n")
|
|
|
|
|
|
|
|
sys.stdout.write("#endif /* GEN_SYSCALL_H */\n")
|