blob: 95cd8c1a8de114d1dde0f850178e488b64f48cf9 [file] [log] [blame]
Andrew Boiefa94ee72017-09-28 16:54:35 -07001#!/usr/bin/env python3
2#
3# Copyright (c) 2017 Intel Corporation
4#
5# SPDX-License-Identifier: Apache-2.0
6
Andrew Boiec78c5e62019-03-11 14:45:43 -07007"""
8Script to generate system call invocation macros
9
10This script parses the system call metadata JSON file emitted by
11parse_syscalls.py to create several files:
12
13- A file containing weak aliases of any potentially unimplemented system calls,
14 as well as the system call dispatch table, which maps system call type IDs
15 to their handler functions.
16
Ruslan Mstoi84822e32020-05-26 16:10:21 +030017- A header file defining the system call type IDs, as well as function
Andrew Boiec78c5e62019-03-11 14:45:43 -070018 prototypes for all system call handler functions.
19
20- A directory containing header files. Each header corresponds to a header
21 that was identified as containing system call declarations. These
22 generated headers contain the inline invocation functions for each system
23 call in that header.
24"""
25
Andrew Boiefa94ee72017-09-28 16:54:35 -070026import sys
27import re
28import argparse
29import os
Sebastian Bøe13a68402017-11-20 13:03:55 +010030import json
Andrew Boiefa94ee72017-09-28 16:54:35 -070031
Kumar Galaa1b77fd2020-05-27 11:26:57 -050032types64 = ["int64_t", "uint64_t"]
Andy Ross65649742019-08-06 13:34:31 -070033
34# The kernel linkage is complicated. These functions from
35# userspace_handlers.c are present in the kernel .a library after
36# userspace.c, which contains the weak fallbacks defined here. So the
37# linker finds the weak one first and stops searching, and thus won't
38# see the real implementation which should override. Yet changing the
39# order runs afoul of a comment in CMakeLists.txt that the order is
40# critical. These are core syscalls that won't ever be unconfigured,
41# just disable the fallback mechanism as a simple workaround.
Marc Herbertf9870292019-09-19 10:30:43 -070042noweak = ["z_mrsh_k_object_release",
43 "z_mrsh_k_object_access_grant",
44 "z_mrsh_k_object_alloc"]
Andy Ross65649742019-08-06 13:34:31 -070045
Andrew Boiefa94ee72017-09-28 16:54:35 -070046table_template = """/* auto-generated by gen_syscalls.py, don't edit */
47
Paul Sokolovsky94620bd2017-10-01 23:47:43 +030048/* Weak handler functions that get replaced by the real ones unless a system
49 * call is not implemented due to kernel configuration.
Andrew Boiefa94ee72017-09-28 16:54:35 -070050 */
51%s
52
53const _k_syscall_handler_t _k_syscall_table[K_SYSCALL_LIMIT] = {
54\t%s
55};
56"""
57
58list_template = """
59/* auto-generated by gen_syscalls.py, don't edit */
Flavio Ceolina7fffa92018-09-13 15:06:35 -070060#ifndef ZEPHYR_SYSCALL_LIST_H
61#define ZEPHYR_SYSCALL_LIST_H
Andrew Boiefa94ee72017-09-28 16:54:35 -070062
Sebastian Bøe1a409902018-08-10 15:23:00 +020063%s
64
Andrew Boiefa94ee72017-09-28 16:54:35 -070065#ifndef _ASMLANGUAGE
66
Andrew Boie800b35f2019-11-05 09:27:18 -080067#include <stdint.h>
Andrew Boiec67eb562018-03-26 13:44:44 -070068
Andrew Boiefa94ee72017-09-28 16:54:35 -070069#endif /* _ASMLANGUAGE */
70
Flavio Ceolina7fffa92018-09-13 15:06:35 -070071#endif /* ZEPHYR_SYSCALL_LIST_H */
Andrew Boiefa94ee72017-09-28 16:54:35 -070072"""
73
74syscall_template = """
75/* auto-generated by gen_syscalls.py, don't edit */
Andy Ross65649742019-08-06 13:34:31 -070076%s
Andrew Boiefa94ee72017-09-28 16:54:35 -070077
78#ifndef _ASMLANGUAGE
79
80#include <syscall_list.h>
Flavio Ceolinf4adf102019-12-03 09:09:07 -080081#include <syscall.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -070082
Stephanos Ioannidis2a1c6252019-09-26 13:57:59 +090083#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
Andy Ross65649742019-08-06 13:34:31 -070084#pragma GCC diagnostic push
Stephanos Ioannidis2a1c6252019-09-26 13:57:59 +090085#endif
86
87#ifdef __GNUC__
Andy Ross65649742019-08-06 13:34:31 -070088#pragma GCC diagnostic ignored "-Wstrict-aliasing"
Stephanos Ioannidis2a1c6252019-09-26 13:57:59 +090089#endif
Andy Ross65649742019-08-06 13:34:31 -070090
Andrew Boiefa94ee72017-09-28 16:54:35 -070091#ifdef __cplusplus
92extern "C" {
93#endif
94
95%s
96
97#ifdef __cplusplus
98}
99#endif
100
Stephanos Ioannidis2a1c6252019-09-26 13:57:59 +0900101#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
Andy Ross65649742019-08-06 13:34:31 -0700102#pragma GCC diagnostic pop
Stephanos Ioannidis2a1c6252019-09-26 13:57:59 +0900103#endif
Andy Ross65649742019-08-06 13:34:31 -0700104
Andrew Boiefa94ee72017-09-28 16:54:35 -0700105#endif
Andy Ross65649742019-08-06 13:34:31 -0700106#endif /* include guard */
Andrew Boiefa94ee72017-09-28 16:54:35 -0700107"""
108
109handler_template = """
Andrew Boie800b35f2019-11-05 09:27:18 -0800110extern uintptr_t z_hdlr_%s(uintptr_t arg1, uintptr_t arg2, uintptr_t arg3,
111 uintptr_t arg4, uintptr_t arg5, uintptr_t arg6, void *ssf);
Andrew Boiefa94ee72017-09-28 16:54:35 -0700112"""
113
114weak_template = """
Leandro Pereiraa1ae8452018-03-06 15:08:55 -0800115__weak ALIAS_OF(handler_no_syscall)
Andrew Boie800b35f2019-11-05 09:27:18 -0800116uintptr_t %s(uintptr_t arg1, uintptr_t arg2, uintptr_t arg3,
117 uintptr_t arg4, uintptr_t arg5, uintptr_t arg6, void *ssf);
Andrew Boiefa94ee72017-09-28 16:54:35 -0700118"""
119
120
Andrew Boiea698e842018-07-23 17:27:57 -0700121typename_regex = re.compile(r'(.*?)([A-Za-z0-9_]+)$')
122
123
124class SyscallParseException(Exception):
125 pass
126
127
128def typename_split(item):
129 if "[" in item:
130 raise SyscallParseException(
131 "Please pass arrays to syscalls as pointers, unable to process '%s'" %
132 item)
133
134 if "(" in item:
135 raise SyscallParseException(
136 "Please use typedefs for function pointers")
137
138 mo = typename_regex.match(item)
139 if not mo:
140 raise SyscallParseException("Malformed system call invocation")
141
142 m = mo.groups()
143 return (m[0].strip(), m[1])
144
Andy Ross65649742019-08-06 13:34:31 -0700145def need_split(argtype):
Andrew Boie9ff64bb2019-11-05 09:39:05 -0800146 return (not args.long_registers) and (argtype in types64)
Andy Ross65649742019-08-06 13:34:31 -0700147
148# Note: "lo" and "hi" are named in little endian conventions,
149# but it doesn't matter as long as they are consistently
150# generated.
151def union_decl(type):
Andrew Boie800b35f2019-11-05 09:27:18 -0800152 return "union { struct { uintptr_t lo, hi; } split; %s val; }" % type
Andy Ross65649742019-08-06 13:34:31 -0700153
154def wrapper_defs(func_name, func_type, args):
Andrew Boie9ff64bb2019-11-05 09:39:05 -0800155 ret64 = need_split(func_type)
Andy Ross65649742019-08-06 13:34:31 -0700156 mrsh_args = [] # List of rvalue expressions for the marshalled invocation
157 split_args = []
158 nsplit = 0
Ulf Magnussona0136b92019-09-13 15:47:16 +0200159 for argtype, argname in args:
Andy Ross65649742019-08-06 13:34:31 -0700160 if need_split(argtype):
161 split_args.append((argtype, argname))
162 mrsh_args.append("parm%d.split.lo" % nsplit)
163 mrsh_args.append("parm%d.split.hi" % nsplit)
164 nsplit += 1
165 else:
Andrew Boie800b35f2019-11-05 09:27:18 -0800166 mrsh_args.append("*(uintptr_t *)&" + argname)
Andy Ross65649742019-08-06 13:34:31 -0700167
168 if ret64:
Andrew Boie800b35f2019-11-05 09:27:18 -0800169 mrsh_args.append("(uintptr_t)&ret64")
Andy Ross65649742019-08-06 13:34:31 -0700170
James Harris074dbb92021-03-04 08:21:14 -0800171 decl_arglist = ", ".join([" ".join(argrec) for argrec in args]) or "void"
Andy Ross65649742019-08-06 13:34:31 -0700172
173 wrap = "extern %s z_impl_%s(%s);\n" % (func_type, func_name, decl_arglist)
174 wrap += "static inline %s %s(%s)\n" % (func_type, func_name, decl_arglist)
175 wrap += "{\n"
176 wrap += "#ifdef CONFIG_USERSPACE\n"
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500177 wrap += ("\t" + "uint64_t ret64;\n") if ret64 else ""
Andy Ross65649742019-08-06 13:34:31 -0700178 wrap += "\t" + "if (z_syscall_trap()) {\n"
179
180 for parmnum, rec in enumerate(split_args):
181 (argtype, argname) = rec
182 wrap += "\t\t%s parm%d;\n" % (union_decl(argtype), parmnum)
183 wrap += "\t\t" + "parm%d.val = %s;\n" % (parmnum, argname)
184
185 if len(mrsh_args) > 6:
Andrew Boie800b35f2019-11-05 09:27:18 -0800186 wrap += "\t\t" + "uintptr_t more[] = {\n"
Andy Ross65649742019-08-06 13:34:31 -0700187 wrap += "\t\t\t" + (",\n\t\t\t".join(mrsh_args[5:])) + "\n"
188 wrap += "\t\t" + "};\n"
Andrew Boie800b35f2019-11-05 09:27:18 -0800189 mrsh_args[5:] = ["(uintptr_t) &more"]
Andy Ross65649742019-08-06 13:34:31 -0700190
191 syscall_id = "K_SYSCALL_" + func_name.upper()
Andrew Boie4f77c2a2019-11-07 12:43:29 -0800192 invoke = ("arch_syscall_invoke%d(%s)"
Andy Ross65649742019-08-06 13:34:31 -0700193 % (len(mrsh_args),
194 ", ".join(mrsh_args + [syscall_id])))
195
196 if ret64:
197 wrap += "\t\t" + "(void)%s;\n" % invoke
198 wrap += "\t\t" + "return (%s)ret64;\n" % func_type
199 elif func_type == "void":
200 wrap += "\t\t" + "%s;\n" % invoke
Ulf Magnussona0136b92019-09-13 15:47:16 +0200201 wrap += "\t\t" + "return;\n"
Andy Ross65649742019-08-06 13:34:31 -0700202 else:
203 wrap += "\t\t" + "return (%s) %s;\n" % (func_type, invoke)
204
205 wrap += "\t" + "}\n"
206 wrap += "#endif\n"
207
208 # Otherwise fall through to direct invocation of the impl func.
209 # Note the compiler barrier: that is required to prevent code from
210 # the impl call from being hoisted above the check for user
211 # context.
212 impl_arglist = ", ".join([argrec[1] for argrec in args])
213 impl_call = "z_impl_%s(%s)" % (func_name, impl_arglist)
214 wrap += "\t" + "compiler_barrier();\n"
215 wrap += "\t" + "%s%s;\n" % ("return " if func_type != "void" else "",
216 impl_call)
217
218 wrap += "}\n"
219
220 return wrap
221
222# Returns an expression for the specified (zero-indexed!) marshalled
223# parameter to a syscall, with handling for a final "more" parameter.
224def mrsh_rval(mrsh_num, total):
225 if mrsh_num < 5 or total <= 6:
226 return "arg%d" % mrsh_num
227 else:
Andrew Boie800b35f2019-11-05 09:27:18 -0800228 return "(((uintptr_t *)more)[%d])" % (mrsh_num - 5)
Andy Ross65649742019-08-06 13:34:31 -0700229
230def marshall_defs(func_name, func_type, args):
231 mrsh_name = "z_mrsh_" + func_name
232
Andrew Boie800b35f2019-11-05 09:27:18 -0800233 nmrsh = 0 # number of marshalled uintptr_t parameter
Andy Ross65649742019-08-06 13:34:31 -0700234 vrfy_parms = [] # list of (arg_num, mrsh_or_parm_num, bool_is_split)
235 split_parms = [] # list of a (arg_num, mrsh_num) for each split
Ulf Magnussona0136b92019-09-13 15:47:16 +0200236 for i, (argtype, _) in enumerate(args):
Andy Ross65649742019-08-06 13:34:31 -0700237 if need_split(argtype):
238 vrfy_parms.append((i, len(split_parms), True))
239 split_parms.append((i, nmrsh))
240 nmrsh += 2
241 else:
242 vrfy_parms.append((i, nmrsh, False))
243 nmrsh += 1
244
245 # Final argument for a 64 bit return value?
Andrew Boie9ff64bb2019-11-05 09:39:05 -0800246 if need_split(func_type):
Andy Ross65649742019-08-06 13:34:31 -0700247 nmrsh += 1
248
249 decl_arglist = ", ".join([" ".join(argrec) for argrec in args])
250 mrsh = "extern %s z_vrfy_%s(%s);\n" % (func_type, func_name, decl_arglist)
251
Andrew Boie800b35f2019-11-05 09:27:18 -0800252 mrsh += "uintptr_t %s(uintptr_t arg0, uintptr_t arg1, uintptr_t arg2,\n" % mrsh_name
Andy Ross65649742019-08-06 13:34:31 -0700253 if nmrsh <= 6:
Andrew Boie800b35f2019-11-05 09:27:18 -0800254 mrsh += "\t\t" + "uintptr_t arg3, uintptr_t arg4, uintptr_t arg5, void *ssf)\n"
Andy Ross65649742019-08-06 13:34:31 -0700255 else:
Andrew Boie800b35f2019-11-05 09:27:18 -0800256 mrsh += "\t\t" + "uintptr_t arg3, uintptr_t arg4, void *more, void *ssf)\n"
Andy Ross65649742019-08-06 13:34:31 -0700257 mrsh += "{\n"
Andy Ross7353c7f2020-02-06 13:39:03 -0800258 mrsh += "\t" + "_current->syscall_frame = ssf;\n"
Andy Ross65649742019-08-06 13:34:31 -0700259
260 for unused_arg in range(nmrsh, 6):
261 mrsh += "\t(void) arg%d;\t/* unused */\n" % unused_arg
262
263 if nmrsh > 6:
264 mrsh += ("\tZ_OOPS(Z_SYSCALL_MEMORY_READ(more, "
Andrew Boie800b35f2019-11-05 09:27:18 -0800265 + str(nmrsh - 6) + " * sizeof(uintptr_t)));\n")
Andy Ross65649742019-08-06 13:34:31 -0700266
267 for i, split_rec in enumerate(split_parms):
268 arg_num, mrsh_num = split_rec
Ulf Magnussona0136b92019-09-13 15:47:16 +0200269 arg_type = args[arg_num][0]
270 mrsh += "\t%s parm%d;\n" % (union_decl(arg_type), i)
Andy Ross65649742019-08-06 13:34:31 -0700271 mrsh += "\t" + "parm%d.split.lo = %s;\n" % (i, mrsh_rval(mrsh_num,
272 nmrsh))
273 mrsh += "\t" + "parm%d.split.hi = %s;\n" % (i, mrsh_rval(mrsh_num + 1,
274 nmrsh))
275 # Finally, invoke the verify function
276 out_args = []
277 for i, argn, is_split in vrfy_parms:
278 if is_split:
279 out_args.append("parm%d.val" % argn)
280 else:
281 out_args.append("*(%s*)&%s" % (args[i][0], mrsh_rval(argn, nmrsh)))
282
283 vrfy_call = "z_vrfy_%s(%s)\n" % (func_name, ", ".join(out_args))
284
285 if func_type == "void":
286 mrsh += "\t" + "%s;\n" % vrfy_call
Andrew Boie378024c2020-05-28 11:48:54 -0700287 mrsh += "\t" + "_current->syscall_frame = NULL;\n"
Andy Ross65649742019-08-06 13:34:31 -0700288 mrsh += "\t" + "return 0;\n"
289 else:
290 mrsh += "\t" + "%s ret = %s;\n" % (func_type, vrfy_call)
Andrew Boie378024c2020-05-28 11:48:54 -0700291
Andrew Boie9ff64bb2019-11-05 09:39:05 -0800292 if need_split(func_type):
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500293 ptr = "((uint64_t *)%s)" % mrsh_rval(nmrsh - 1, nmrsh)
Andy Ross65649742019-08-06 13:34:31 -0700294 mrsh += "\t" + "Z_OOPS(Z_SYSCALL_MEMORY_WRITE(%s, 8));\n" % ptr
295 mrsh += "\t" + "*%s = ret;\n" % ptr
Andrew Boie378024c2020-05-28 11:48:54 -0700296 mrsh += "\t" + "_current->syscall_frame = NULL;\n"
Andy Ross65649742019-08-06 13:34:31 -0700297 mrsh += "\t" + "return 0;\n"
298 else:
Andrew Boie378024c2020-05-28 11:48:54 -0700299 mrsh += "\t" + "_current->syscall_frame = NULL;\n"
Andrew Boie800b35f2019-11-05 09:27:18 -0800300 mrsh += "\t" + "return (uintptr_t) ret;\n"
Andy Ross65649742019-08-06 13:34:31 -0700301
302 mrsh += "}\n"
303
304 return mrsh, mrsh_name
Andrew Boiea698e842018-07-23 17:27:57 -0700305
306def analyze_fn(match_group):
307 func, args = match_group
308
309 try:
310 if args == "void":
311 args = []
312 else:
313 args = [typename_split(a.strip()) for a in args.split(",")]
314
315 func_type, func_name = typename_split(func)
316 except SyscallParseException:
317 sys.stderr.write("In declaration of %s\n" % func)
318 raise
319
320 sys_id = "K_SYSCALL_" + func_name.upper()
321
Andy Ross65649742019-08-06 13:34:31 -0700322 marshaller = None
323 marshaller, handler = marshall_defs(func_name, func_type, args)
324 invocation = wrapper_defs(func_name, func_type, args)
Andrew Boiea698e842018-07-23 17:27:57 -0700325
326 # Entry in _k_syscall_table
327 table_entry = "[%s] = %s" % (sys_id, handler)
328
Andy Ross65649742019-08-06 13:34:31 -0700329 return (handler, invocation, marshaller, sys_id, table_entry)
Andrew Boiea698e842018-07-23 17:27:57 -0700330
Andrew Boiefa94ee72017-09-28 16:54:35 -0700331def parse_args():
332 global args
Anas Nashif72565532017-12-12 08:19:25 -0500333 parser = argparse.ArgumentParser(
334 description=__doc__,
335 formatter_class=argparse.RawDescriptionHelpFormatter)
Andrew Boiefa94ee72017-09-28 16:54:35 -0700336
Sebastian Bøe13a68402017-11-20 13:03:55 +0100337 parser.add_argument("-i", "--json-file", required=True,
Anas Nashif72565532017-12-12 08:19:25 -0500338 help="Read syscall information from json file")
Andrew Boiefa94ee72017-09-28 16:54:35 -0700339 parser.add_argument("-d", "--syscall-dispatch", required=True,
Anas Nashif72565532017-12-12 08:19:25 -0500340 help="output C system call dispatch table file")
Andrew Boie353acf42018-07-23 18:10:15 -0700341 parser.add_argument("-l", "--syscall-list", required=True,
342 help="output C system call list header")
Andrew Boiefa94ee72017-09-28 16:54:35 -0700343 parser.add_argument("-o", "--base-output", required=True,
Anas Nashif72565532017-12-12 08:19:25 -0500344 help="Base output directory for syscall macro headers")
Andy Ross65649742019-08-06 13:34:31 -0700345 parser.add_argument("-s", "--split-type", action="append",
Andrew Boie9ff64bb2019-11-05 09:39:05 -0800346 help="A long type that must be split/marshalled on 32-bit systems")
347 parser.add_argument("-x", "--long-registers", action="store_true",
348 help="Indicates we are on system with 64-bit registers")
Andrew Boiefa94ee72017-09-28 16:54:35 -0700349 args = parser.parse_args()
350
351
352def main():
353 parse_args()
354
Ulf Magnussona0136b92019-09-13 15:47:16 +0200355 if args.split_type is not None:
Andy Ross65649742019-08-06 13:34:31 -0700356 for t in args.split_type:
357 types64.append(t)
358
Sebastian Bøe13a68402017-11-20 13:03:55 +0100359 with open(args.json_file, 'r') as fd:
360 syscalls = json.load(fd)
361
Andrew Boiefa94ee72017-09-28 16:54:35 -0700362 invocations = {}
Andy Ross65649742019-08-06 13:34:31 -0700363 mrsh_defs = {}
364 mrsh_includes = {}
Andrew Boiefa94ee72017-09-28 16:54:35 -0700365 ids = []
366 table_entries = []
367 handlers = []
368
Andrew Boiea698e842018-07-23 17:27:57 -0700369 for match_group, fn in syscalls:
Andy Ross65649742019-08-06 13:34:31 -0700370 handler, inv, mrsh, sys_id, entry = analyze_fn(match_group)
Andrew Boiea698e842018-07-23 17:27:57 -0700371
Andrew Boiefa94ee72017-09-28 16:54:35 -0700372 if fn not in invocations:
373 invocations[fn] = []
374
375 invocations[fn].append(inv)
376 ids.append(sys_id)
377 table_entries.append(entry)
378 handlers.append(handler)
379
Andy Ross65649742019-08-06 13:34:31 -0700380 if mrsh:
381 syscall = typename_split(match_group[0])[1]
382 mrsh_defs[syscall] = mrsh
383 mrsh_includes[syscall] = "#include <syscalls/%s>" % fn
384
Andrew Boiefa94ee72017-09-28 16:54:35 -0700385 with open(args.syscall_dispatch, "w") as fp:
Leandro Pereiraa1ae8452018-03-06 15:08:55 -0800386 table_entries.append("[K_SYSCALL_BAD] = handler_bad_syscall")
Andrew Boiefa94ee72017-09-28 16:54:35 -0700387
Andy Ross65649742019-08-06 13:34:31 -0700388 weak_defines = "".join([weak_template % name
389 for name in handlers
390 if not name in noweak])
Andrew Boiefa94ee72017-09-28 16:54:35 -0700391
Andy Ross65649742019-08-06 13:34:31 -0700392 # The "noweak" ones just get a regular declaration
Andrew Boie800b35f2019-11-05 09:27:18 -0800393 weak_defines += "\n".join(["extern uintptr_t %s(uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, uintptr_t arg4, uintptr_t arg5, uintptr_t arg6, void *ssf);"
Andy Ross65649742019-08-06 13:34:31 -0700394 % s for s in noweak])
395
396 fp.write(table_template % (weak_defines,
397 ",\n\t".join(table_entries)))
Andrew Boiefa94ee72017-09-28 16:54:35 -0700398
399 # Listing header emitted to stdout
400 ids.sort()
401 ids.extend(["K_SYSCALL_BAD", "K_SYSCALL_LIMIT"])
Sebastian Bøe1a409902018-08-10 15:23:00 +0200402
403 ids_as_defines = ""
404 for i, item in enumerate(ids):
405 ids_as_defines += "#define {} {}\n".format(item, i)
406
Andrew Boie353acf42018-07-23 18:10:15 -0700407 with open(args.syscall_list, "w") as fp:
Andy Ross65649742019-08-06 13:34:31 -0700408 fp.write(list_template % ids_as_defines)
Andrew Boiefa94ee72017-09-28 16:54:35 -0700409
410 os.makedirs(args.base_output, exist_ok=True)
411 for fn, invo_list in invocations.items():
412 out_fn = os.path.join(args.base_output, fn)
413
Andy Ross65649742019-08-06 13:34:31 -0700414 ig = re.sub("[^a-zA-Z0-9]", "_", "Z_INCLUDE_SYSCALLS_" + fn).upper()
415 include_guard = "#ifndef %s\n#define %s\n" % (ig, ig)
416 header = syscall_template % (include_guard, "\n\n".join(invo_list))
Andrew Boiefa94ee72017-09-28 16:54:35 -0700417
Andrew Boiefa94ee72017-09-28 16:54:35 -0700418 with open(out_fn, "w") as fp:
419 fp.write(header)
420
Andy Ross65649742019-08-06 13:34:31 -0700421 # Likewise emit _mrsh.c files for syscall inclusion
422 for fn in mrsh_defs:
423 mrsh_fn = os.path.join(args.base_output, fn + "_mrsh.c")
424
425 with open(mrsh_fn, "w") as fp:
426 fp.write("/* auto-generated by gen_syscalls.py, don't edit */\n")
Stephanos Ioannidis2a1c6252019-09-26 13:57:59 +0900427 fp.write("#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)\n")
Andy Ross65649742019-08-06 13:34:31 -0700428 fp.write("#pragma GCC diagnostic push\n")
Stephanos Ioannidis2a1c6252019-09-26 13:57:59 +0900429 fp.write("#endif\n")
430 fp.write("#ifdef __GNUC__\n")
Andy Ross65649742019-08-06 13:34:31 -0700431 fp.write("#pragma GCC diagnostic ignored \"-Wstrict-aliasing\"\n")
Stephanos Ioannidis2a1c6252019-09-26 13:57:59 +0900432 fp.write("#endif\n")
Andy Ross65649742019-08-06 13:34:31 -0700433 fp.write(mrsh_includes[fn] + "\n")
434 fp.write("\n")
435 fp.write(mrsh_defs[fn] + "\n")
Stephanos Ioannidis2a1c6252019-09-26 13:57:59 +0900436 fp.write("#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)\n")
Andy Ross65649742019-08-06 13:34:31 -0700437 fp.write("#pragma GCC diagnostic pop\n")
Stephanos Ioannidis2a1c6252019-09-26 13:57:59 +0900438 fp.write("#endif\n")
Anas Nashif72565532017-12-12 08:19:25 -0500439
Andrew Boiefa94ee72017-09-28 16:54:35 -0700440if __name__ == "__main__":
441 main()