makesyscalls.sh revision 1.145.4.2 1 # $NetBSD: makesyscalls.sh,v 1.145.4.2 2015/06/06 14:40:21 skrll Exp $
2 #
3 # Copyright (c) 1994, 1996, 2000 Christopher G. Demetriou
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 # 3. All advertising materials mentioning features or use of this software
15 # must display the following acknowledgement:
16 # This product includes software developed for the NetBSD Project
17 # by Christopher G. Demetriou.
18 # 4. The name of the author may not be used to endorse or promote products
19 # derived from this software without specific prior written permission
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 # @(#)makesyscalls.sh 8.1 (Berkeley) 6/10/93
33
34 set -e
35
36 case $# in
37 2) ;;
38 *) echo "Usage: $0 config-file input-file" 1>&2
39 exit 1
40 ;;
41 esac
42
43 # the config file sets the following variables:
44 # sysalign check for alignment of off_t/dev_t/time_t
45 # sysnames the syscall names file
46 # sysnumhdr the syscall numbers file
47 # syssw the syscall switch file
48 # sysautoload the syscall autoload definitions file
49 # sysarghdr the syscall argument struct definitions
50 # compatopts those syscall types that are for 'compat' syscalls
51 # switchname the name for the 'struct sysent' we define
52 # namesname the name for the 'const char *[]' we define
53 # constprefix the prefix for the system call constants
54 # registertype the type for register_t
55 # nsysent the size of the sysent table
56 # sys_nosys [optional] name of function called for unsupported
57 # syscalls, if not sys_nosys()
58 # maxsysargs [optiona] the maximum number or arguments
59 #
60 # NOTE THAT THIS makesyscalls.sh DOES NOT SUPPORT 'SYSLIBCOMPAT'.
61
62 # source the config file.
63 sys_nosys="sys_nosys" # default is sys_nosys(), if not specified otherwise
64 maxsysargs=8 # default limit is 8 (32bit) arguments
65 systrace="/dev/null"
66 sysautoload="/dev/null"
67 rumpcalls="/dev/null"
68 rumpcallshdr="/dev/null"
69 rumpsysmap="/dev/null"
70 rumpsysent="rumpsysent.tmp"
71 rumpnoflags="\n\t\t.sy_flags = SYCALL_NOSYS,"
72 rumpnosys="(sy_call_t *)rumpns_enosys"
73 rumpnomodule="(sy_call_t *)rumpns_sys_nomodule"
74
75 case $1 in
76 /*) . $1;;
77 *) . ./$1;;
78 esac
79
80 # tmp files:
81 sysdcl="sysent.dcl"
82 sysprotos="sys.protos"
83 syscompat_pref="sysent."
84 sysent="sysent.switch"
85 sysnamesbottom="sysnames.bottom"
86 rumptypes="rumphdr.types"
87 rumpprotos="rumphdr.protos"
88 systracetmp="systrace.$$"
89 systraceret="systraceret.$$"
90
91 trap "rm $sysdcl $sysprotos $sysent $sysnamesbottom $rumpsysent $rumptypes $rumpprotos $systracetmp $systraceret" 0
92
93 # Awk program (must support nawk extensions)
94 # Use "awk" at Berkeley, "nawk" or "gawk" elsewhere.
95 awk=${AWK:-awk}
96
97 # Does this awk have a "toupper" function?
98 have_toupper="$($awk 'BEGIN { print toupper("true"); exit; }' 2>/dev/null)"
99
100 # If this awk does not define "toupper" then define our own.
101 if [ "$have_toupper" = TRUE ] ; then
102 # Used awk (GNU awk or nawk) provides it
103 toupper=
104 else
105 # Provide our own toupper()
106 toupper='
107 function toupper(str) {
108 _toupper_cmd = "echo "str" |tr a-z A-Z"
109 _toupper_cmd | getline _toupper_str;
110 close(_toupper_cmd);
111 return _toupper_str;
112 }'
113 fi
114
115 # before handing it off to awk, make a few adjustments:
116 # (1) insert spaces around {, }, (, ), *, and commas.
117 # (2) get rid of any and all dollar signs (so that rcs id use safe)
118 #
119 # The awk script will deal with blank lines and lines that
120 # start with the comment character (';').
121
122 sed -e '
123 s/\$//g
124 :join
125 /\\$/{a\
126
127 N
128 s/\\\n//
129 b join
130 }
131 2,${
132 /^#/!s/\([{}()*,|]\)/ \1 /g
133 }
134 ' < $2 | $awk "
135 $toupper
136 BEGIN {
137 # Create a NetBSD tag that does not get expanded when checking
138 # this script out of CVS. (This part of the awk script is in a
139 # shell double-quoted string, so the backslashes are eaten by
140 # the shell.)
141 tag = \"\$\" \"NetBSD\" \"\$\"
142
143 # to allow nested #if/#else/#endif sets
144 savedepth = 0
145 auto_skip = 0
146 # to track already processed syscalls
147
148 sysnames = \"$sysnames\"
149 sysprotos = \"$sysprotos\"
150 sysnumhdr = \"$sysnumhdr\"
151 sysarghdr = \"$sysarghdr\"
152 sysarghdrextra = \"$sysarghdrextra\"
153 systrace = \"$systrace\"
154 systracetmp = \"$systracetmp\"
155 systraceret = \"$systraceret\"
156 sysautoload = \"$sysautoload\"
157 rumpcalls = \"$rumpcalls\"
158 rumpcallshdr = \"$rumpcallshdr\"
159 rumpsysent = \"$rumpsysent\"
160 rumpsysmap = \"$rumpsysmap\"
161 switchname = \"$switchname\"
162 namesname = \"$namesname\"
163 constprefix = \"$constprefix\"
164 registertype = \"$registertype\"
165 sysalign=\"$sysalign\"
166 if (!registertype) {
167 registertype = \"register_t\"
168 }
169 nsysent = \"$nsysent\"
170
171 sysdcl = \"$sysdcl\"
172 syscompat_pref = \"$syscompat_pref\"
173 sysent = \"$sysent\"
174 sysnamesbottom = \"$sysnamesbottom\"
175 rumpprotos = \"$rumpprotos\"
176 rumptypes = \"$rumptypes\"
177 sys_nosys = \"$sys_nosys\"
178 maxsysargs = \"$maxsysargs\"
179 rumpnoflags=\"$rumpnoflags\"
180 rumpnosys=\"$rumpnosys\"
181 rumpnomodule=\"$rumpnomodule\"
182 infile = \"$2\"
183
184 compatopts = \"$compatopts\"
185 "'
186
187 if (rumpcalls != "/dev/null")
188 haverumpcalls = 1
189
190 printf "/* %s */\n\n", tag > sysdcl
191 printf "/*\n * System call switch table.\n *\n" > sysdcl
192 printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysdcl
193
194 ncompat = split(compatopts,compat)
195 for (i = 1; i <= ncompat; i++) {
196 compat_upper[i] = toupper(compat[i])
197
198 printf "\n#ifdef %s\n", compat_upper[i] > sysent
199 printf "#define %s(func) __CONCAT(%s_,func)\n", compat[i], \
200 compat[i] > sysent
201 printf "#else\n" > sysent
202 printf "#define %s(func) %s\n", compat[i], sys_nosys > sysent
203 printf "#endif\n" > sysent
204 }
205
206 printf "\n#define\ts(type)\tsizeof(type)\n" > sysent
207 printf "#define\tn(type)\t(sizeof(type)/sizeof (%s))\n", registertype > sysent
208 printf "#define\tns(type)\t.sy_narg = n(type), .sy_argsize = s(type)\n\n", registertype > sysent
209 printf "struct sysent %s[] = {\n",switchname > sysent
210
211 printf "/* %s */\n\n", tag > sysnames
212 printf "/*\n * System call names.\n *\n" > sysnames
213 printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysnames
214
215 printf "\n/*\n * System call prototypes.\n */\n\n" > sysprotos
216 if (haverumpcalls)
217 printf("#ifndef RUMP_CLIENT\n") > sysprotos
218
219 printf "/* %s */\n\n", tag > sysnumhdr
220 printf "/*\n * System call numbers.\n *\n" > sysnumhdr
221 printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysnumhdr
222
223 printf "/* %s */\n\n", tag > sysarghdr
224 printf "/*\n * System call argument lists.\n *\n" > sysarghdr
225 printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysarghdr
226
227 printf "/* %s */\n\n", tag > sysautoload
228 printf "/*\n * System call autoload table.\n *\n" > sysautoload
229 printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysautoload
230
231 printf "/* %s */\n\n", tag > rumpcalls
232 printf "/*\n * System call vector and marshalling for rump.\n *\n" > rumpcalls
233 printf " * DO NOT EDIT-- this file is automatically generated.\n" > rumpcalls
234
235 printf "/* %s */\n\n", tag > rumpcallshdr
236 printf "/*\n * System call protos in rump namespace.\n *\n" > rumpcallshdr
237 printf " * DO NOT EDIT-- this file is automatically generated.\n" > rumpcallshdr
238
239 printf "/* %s */\n\n", tag > systrace
240 printf "/*\n * System call argument to DTrace register array converstion.\n *\n" > systrace
241 printf " * DO NOT EDIT-- this file is automatically generated.\n" > systrace
242 }
243 NR == 1 {
244 sub(/ $/, "")
245 printf " * created from%s\n */\n\n", $0 > sysdcl
246 printf "#include <sys/cdefs.h>\n__KERNEL_RCSID(0, \"%s\");\n\n", tag > sysdcl
247
248 printf " * created from%s\n */\n\n", $0 > sysnames
249 printf "#include <sys/cdefs.h>\n__KERNEL_RCSID(0, \"%s\");\n\n", tag > sysnames
250
251 printf " * created from%s\n */\n\n", $0 > sysautoload
252 printf "#include <sys/cdefs.h>\n__KERNEL_RCSID(0, \"%s\");\n\n", tag > sysautoload
253 printf("static struct {\n") > sysautoload
254 printf("\tu_int\t\tal_code;\n") > sysautoload
255 printf("\tconst char\t*al_module;\n") > sysautoload
256 printf("} const syscalls_autoload[] = {\n") > sysautoload
257
258 printf " * created from%s\n */\n\n", $0 > rumpcalls
259 printf "#ifdef RUMP_CLIENT\n" > rumpcalls
260 printf "#include <rump/rumpuser_port.h>\n" > rumpcalls
261 printf "#endif /* RUMP_CLIENT */\n\n" > rumpcalls
262 printf "#include <sys/param.h>\n\n" > rumpcalls
263 printf "#ifdef __NetBSD__\n" > rumpcalls
264 printf "#include <sys/cdefs.h>\n__KERNEL_RCSID(0, \"%s\");\n\n", tag > rumpcalls
265
266 printf "#include <sys/fstypes.h>\n" > rumpcalls
267 printf "#include <sys/proc.h>\n" > rumpcalls
268 printf "#endif /* __NetBSD__ */\n\n" > rumpcalls
269 printf "#ifdef RUMP_CLIENT\n" > rumpcalls
270 printf "#include <errno.h>\n" > rumpcalls
271 printf "#include <stdint.h>\n" > rumpcalls
272 printf "#include <stdlib.h>\n" > rumpcalls
273 printf "#include <string.h>\n\n" > rumpcalls
274 printf "#include <srcsys/syscall.h>\n" > rumpcalls
275 printf "#include <srcsys/syscallargs.h>\n\n" > rumpcalls
276 printf "#include <rump/rumpclient.h>\n\n" > rumpcalls
277 printf "#define rsys_syscall(num, data, dlen, retval)\t\\\n" > rumpcalls
278 printf " rumpclient_syscall(num, data, dlen, retval)\n" > rumpcalls
279 printf "#define rsys_seterrno(error) errno = error\n" > rumpcalls
280 printf "#else\n" > rumpcalls
281 printf "#include <sys/syscall.h>\n" > rumpcalls
282 printf "#include <sys/syscallargs.h>\n\n" > rumpcalls
283 printf "#include <sys/syscallvar.h>\n\n" > rumpcalls
284 printf "#include <rump/rumpuser.h>\n" > rumpcalls
285 printf "#include \"rump_private.h\"\n\n" > rumpcalls
286 printf "#define rsys_syscall(num, data, dlen, retval)\t\\\n" > rumpcalls
287 printf " rump_syscall(num, data, dlen, retval)\n\n" > rumpcalls
288 printf "#define rsys_seterrno(error) rumpuser_seterrno(error)\n" \
289 > rumpcalls
290 printf "#endif\n\n" > rumpcalls
291
292 printf "#ifndef RUMP_KERNEL_IS_LIBC\n" > rumpcalls
293 printf "#define RUMP_SYS_COMPAT\n" > rumpcalls
294 printf "#endif\n\n" > rumpcalls
295
296 printf "#if\tBYTE_ORDER == BIG_ENDIAN\n" > rumpcalls
297 printf "#define SPARG(p,k)\t((p)->k.be.datum)\n" > rumpcalls
298 printf "#else /* LITTLE_ENDIAN, I hope dearly */\n" > rumpcalls
299 printf "#define SPARG(p,k)\t((p)->k.le.datum)\n" > rumpcalls
300 printf "#endif\n\n" > rumpcalls
301 printf "\nvoid rumpns_sys_nomodule(void);\n" > rumpcalls
302
303 printf "\n#ifndef RUMP_CLIENT\n" > rumpsysent
304 printf "int rumpns_enosys(void);\n" > rumpsysent
305 printf "#define\ts(type)\tsizeof(type)\n" > rumpsysent
306 printf "#define\tn(type)\t(sizeof(type)/sizeof (%s))\n", registertype > rumpsysent
307 printf "#define\tns(type)\tn(type), s(type)\n\n", registertype > rumpsysent
308 printf "struct sysent rump_sysent[] = {\n" > rumpsysent
309
310 # System call names are included by userland (kdump(1)), so
311 # hide the include files from it.
312 printf "#if defined(_KERNEL_OPT)\n" > sysnames
313
314 printf "#endif /* _KERNEL_OPT */\n\n" > sysnamesbottom
315 printf "const char *const %s[] = {\n",namesname > sysnamesbottom
316
317 printf " * created from%s\n */\n\n", $0 > sysnumhdr
318 printf "#ifndef _" constprefix "SYSCALL_H_\n" > sysnumhdr
319 printf "#define _" constprefix "SYSCALL_H_\n\n" > sysnumhdr
320
321 printf " * created from%s\n */\n\n", $0 > sysarghdr
322 printf "#ifndef _" constprefix "SYSCALLARGS_H_\n" > sysarghdr
323 printf "#define _" constprefix "SYSCALLARGS_H_\n\n" > sysarghdr
324
325 printf " * created from%s\n */\n\n", $0 > rumpcallshdr
326 printf "#ifndef _RUMP_RUMP_SYSCALLS_H_\n" > rumpcallshdr
327 printf "#define _RUMP_RUMP_SYSCALLS_H_\n\n" > rumpcallshdr
328 printf "#ifdef _KERNEL\n" > rumpcallshdr
329 printf "#error Interface not supported inside kernel\n" > rumpcallshdr
330 printf "#endif /* _KERNEL */\n\n" > rumpcallshdr
331 printf "#include <rump/rump_syscalls_compat.h>\n\n" > rumpcallshdr
332
333 printf "%s", sysarghdrextra > sysarghdr
334 # Write max number of system call arguments to both headers
335 printf("#define\t%sMAXSYSARGS\t%d\n\n", constprefix, maxsysargs) \
336 > sysnumhdr
337 printf("#define\t%sMAXSYSARGS\t%d\n\n", constprefix, maxsysargs) \
338 > sysarghdr
339 printf "#undef\tsyscallarg\n" > sysarghdr
340 printf "#define\tsyscallarg(x)\t\t\t\t\t\t\t\\\n" > sysarghdr
341 printf "\tunion {\t\t\t\t\t\t\t\t\\\n" > sysarghdr
342 printf "\t\t%s pad;\t\t\t\t\t\t\\\n", registertype > sysarghdr
343 printf "\t\tstruct { x datum; } le;\t\t\t\t\t\\\n" > sysarghdr
344 printf "\t\tstruct { /* LINTED zero array dimension */\t\t\\\n" \
345 > sysarghdr
346 printf "\t\t\tint8_t pad[ /* CONSTCOND */\t\t\t\\\n" > sysarghdr
347 printf "\t\t\t\t(sizeof (%s) < sizeof (x))\t\\\n", \
348 registertype > sysarghdr
349 printf "\t\t\t\t? 0\t\t\t\t\t\\\n" > sysarghdr
350 printf "\t\t\t\t: sizeof (%s) - sizeof (x)];\t\\\n", \
351 registertype > sysarghdr
352 printf "\t\t\tx datum;\t\t\t\t\t\\\n" > sysarghdr
353 printf "\t\t} be;\t\t\t\t\t\t\t\\\n" > sysarghdr
354 printf "\t}\n" > sysarghdr
355 printf("\n#undef check_syscall_args\n") >sysarghdr
356 printf("#define check_syscall_args(call) /*LINTED*/ \\\n" \
357 "\ttypedef char call##_check_args" \
358 "[sizeof (struct call##_args) \\\n" \
359 "\t\t<= %sMAXSYSARGS * sizeof (%s) ? 1 : -1];\n", \
360 constprefix, registertype) >sysarghdr
361
362 printf " * This file is part of the DTrace syscall provider.\n */\n\n" > systrace
363 printf "static void\nsystrace_args(register_t sysnum, const void *params, uintptr_t *uarg, size_t *n_args)\n{\n" > systrace
364 printf "\tintptr_t *iarg = (intptr_t *)uarg;\n" > systrace
365 printf "\tswitch (sysnum) {\n" > systrace
366
367 printf "static void\nsystrace_entry_setargdesc(int sysnum, int ndx, char *desc, size_t descsz)\n{\n\tconst char *p = NULL;\n" > systracetmp
368 printf "\tswitch (sysnum) {\n" > systracetmp
369
370 printf "static void\nsystrace_return_setargdesc(int sysnum, int ndx, char *desc, size_t descsz)\n{\n\tconst char *p = NULL;\n" > systraceret
371 printf "\tswitch (sysnum) {\n" > systraceret
372
373 # compat types from syscalls.master. this is slightly ugly,
374 # but given that we have so few compats from over 17 years,
375 # a more complicated solution is not currently warranted.
376 uncompattypes["struct timeval50"] = "struct timeval";
377 uncompattypes["struct timespec50"] = "struct timespec";
378 uncompattypes["struct stat30"] = "struct stat";
379
380 next
381 }
382 NF == 0 || $1 ~ /^;/ {
383 next
384 }
385 $0 ~ /^%%$/ {
386 intable = 1
387 next
388 }
389 $1 ~ /^#[ ]*include/ {
390 print > sysdcl
391 print > sysnames
392 next
393 }
394 $1 ~ /^#/ && !intable {
395 print > sysdcl
396 print > sysnames
397 next
398 }
399 $1 ~ /^#/ && intable {
400 if ($1 ~ /^#[ ]*if/) {
401 savedepth++
402 savesyscall[savedepth] = syscall
403 skip_auto[savedepth] = auto_skip
404 auto_skip = 0
405
406 # Special handling for sysautoload conditionals
407 #
408 # We ignore all conditions other than those for
409 # !defined(_LP64) which are used for SYSV* syscalls
410 # only
411
412 if ($0 ~ /!defined\(_LP64\)/) {
413 printf("#if !defined(_LP64)\n") > sysautoload
414 auto_skip = savedepth
415 }
416 }
417 if ($1 ~ /^#[ ]*else/) {
418 if (savedepth <= 0) {
419 printf("%s: line %d: unbalanced #else\n", \
420 infile, NR)
421 exit 1
422 }
423 if (auto_skip == savedepth) {
424 print > sysautoload
425 }
426 syscall = savesyscall[savedepth]
427 }
428 if ($1 ~ /^#[ ]*endif/) {
429 if (savedepth <= 0) {
430 printf("%s: line %d: unbalanced #endif\n", \
431 infile, NR)
432 exit 1
433 }
434 if (auto_skip == savedepth) {
435 print > sysautoload
436 }
437 auto_skip = skip_auto[savedepth];
438 savedepth--
439 }
440 print > sysent
441 print > sysarghdr
442 print > sysnumhdr
443 print > sysprotos
444 print > sysnamesbottom
445 print > systrace
446 print > systracetmp
447 print > systraceret
448
449 # XXX: technically we do not want to have conditionals in rump,
450 # but it is easier to just let the cpp handle them than try to
451 # figure out what we want here in this script
452 print > rumpsysent
453 next
454 }
455 syscall != $1 {
456 printf "%s: line %d: syscall number out of sync at %d\n", \
457 infile, NR, syscall
458 printf "line is:\n"
459 print
460 exit 1
461 }
462 function parserr(was, wanted) {
463 printf "%s: line %d: unexpected %s (expected <%s>)\n", \
464 infile, NR, was, wanted
465 printf "line is:\n"
466 print
467 exit 1
468 }
469 function fillerpsysent(syscall, flags, name, comment) {
470 return sprintf("\t{%s\n\t\t.sy_call = %s,\n\t},\t\t/* %d = filler */",\
471 flags, name, syscall, comment);
472 }
473 function parseline() {
474 f=3 # toss number and type
475 if ($2 == "INDIR")
476 sycall_flags="SYCALL_INDIRECT"
477 else
478 sycall_flags="0"
479 if ($NF != "}") {
480 funcalias=$NF
481 end=NF-1
482 } else {
483 funcalias=""
484 end=NF
485 }
486 if ($f == "INDIR") { # allow for "NOARG INDIR"
487 sycall_flags = "SYCALL_INDIRECT | " sycall_flags
488 f++
489 }
490 if ($f == "MODULAR") { # registered at runtime
491 modular = 1
492 f++
493 modname = $f
494 f++
495 } else {
496 modular = 0;
497 }
498 if ($f == "RUMP") {
499 rumpable = 1
500 f++
501 } else {
502 rumpable = 0
503 }
504 if ($f ~ /^[a-z0-9_]*$/) { # allow syscall alias
505 funcalias=$f
506 f++
507 }
508 if ($f != "{")
509 parserr($f, "{")
510 f++
511 if ($end != "}")
512 parserr($end, "}")
513 end--
514 if ($end != ";")
515 parserr($end, ";")
516 end--
517 if ($end != ")")
518 parserr($end, ")")
519 end--
520
521 returntype = oldf = "";
522 do {
523 if (returntype != "" && oldf != "*")
524 returntype = returntype" ";
525 returntype = returntype$f;
526 oldf = $f;
527 f++
528 } while ($f != "|" && f < (end-1))
529 if (f == (end - 1)) {
530 parserr($f, "function argument definition (maybe \"|\"?)");
531 }
532 f++
533
534 fprefix=$f
535 f++
536 if ($f != "|") {
537 parserr($f, "function compat delimiter (maybe \"|\"?)");
538 }
539 f++
540
541 fcompat=""
542 if ($f != "|") {
543 fcompat=$f
544 f++
545 }
546
547 if ($f != "|") {
548 parserr($f, "function name delimiter (maybe \"|\"?)");
549 }
550 f++
551 fbase=$f
552
553 # pipe is special in how to returns its values.
554 # So just generate it manually if present.
555 if (rumpable == 1 && fbase == "pipe") {
556 rumpable = 0;
557 rumphaspipe = 1;
558 }
559
560 if (fcompat != "") {
561 funcname=fprefix "___" fbase "" fcompat
562 } else {
563 funcname=fprefix "_" fbase
564 }
565 if (returntype == "quad_t" || returntype == "off_t") {
566 if (sycall_flags == "0")
567 sycall_flags = "SYCALL_RET_64";
568 else
569 sycall_flags = "SYCALL_RET_64 | " sycall_flags;
570 }
571
572 if (funcalias == "") {
573 funcalias=funcname
574 sub(/^([^_]+_)*sys_/, "", funcalias)
575 realname=fbase
576 } else {
577 realname=funcalias
578 }
579 rumpfname=realname "" fcompat
580 f++
581
582 if ($f != "(")
583 parserr($f, "(")
584 f++
585
586 argc=0;
587 argalign=0;
588 if (f == end) {
589 if ($f != "void")
590 parserr($f, "argument definition")
591 isvarargs = 0;
592 varargc = 0;
593 argtype[0]="void";
594 return
595 }
596
597 # some system calls (open() and fcntl()) can accept a variable
598 # number of arguments. If syscalls accept a variable number of
599 # arguments, they must still have arguments specified for
600 # the remaining argument "positions," because of the way the
601 # kernel system call argument handling works.
602 #
603 # Indirect system calls, e.g. syscall(), are exceptions to this
604 # rule, since they are handled entirely by machine-dependent code
605 # and do not need argument structures built.
606
607 isvarargs = 0;
608 args64 = 0;
609 ptr = 0;
610 while (f <= end) {
611 if ($f == "...") {
612 f++;
613 isvarargs = 1;
614 varargc = argc;
615 continue;
616 }
617 argc++
618 argtype[argc]=""
619 oldf=""
620 while (f < end && $(f+1) != ",") {
621 if (argtype[argc] != "" && oldf != "*")
622 argtype[argc] = argtype[argc]" ";
623 argtype[argc] = argtype[argc]$f;
624 oldf = $f;
625 f++
626 }
627 if (argtype[argc] == "")
628 parserr($f, "argument definition")
629 if (argtype[argc] == "off_t" \
630 || argtype[argc] == "dev_t" \
631 || argtype[argc] == "time_t") {
632 if ((argalign % 2) != 0 && sysalign &&
633 funcname != "sys_posix_fadvise") # XXX for now
634 parserr($f, "a padding argument")
635 } else {
636 argalign++;
637 }
638 if (argtype[argc] == "quad_t" || argtype[argc] == "off_t" \
639 || argtype[argc] == "dev_t" || argtype[argc] == "time_t") {
640 if (sycall_flags == "0")
641 sycall_flags = "SYCALL_ARG"argc-1"_64";
642 else
643 sycall_flags = "SYCALL_ARG"argc-1"_64 | " sycall_flags;
644 args64++;
645 }
646 if (index(argtype[argc], "*") != 0 && ptr == 0) {
647 if (sycall_flags == "0")
648 sycall_flags = "SYCALL_ARG_PTR";
649 else
650 sycall_flags = "SYCALL_ARG_PTR | " sycall_flags;
651 ptr = 1;
652 }
653 argname[argc]=$f;
654 f += 2; # skip name, and any comma
655 }
656 if (args64 > 0)
657 sycall_flags = "SYCALL_NARGS64_VAL("args64") | " sycall_flags;
658 # must see another argument after varargs notice.
659 if (isvarargs) {
660 if (argc == varargc)
661 parserr($f, "argument definition")
662 } else
663 varargc = argc;
664 }
665
666 function printproto(wrap) {
667 printf("/* syscall: \"%s%s\" ret: \"%s\" args:", wrap, funcalias,
668 returntype) > sysnumhdr
669 for (i = 1; i <= varargc; i++)
670 printf(" \"%s\"", argtype[i]) > sysnumhdr
671 if (isvarargs)
672 printf(" \"...\"") > sysnumhdr
673 printf(" */\n") > sysnumhdr
674 printf("#define\t%s%s%s\t%d\n\n", constprefix, wrap, funcalias,
675 syscall) > sysnumhdr
676
677 # output entry for syscall autoload table, if modular
678 if (modular ) {
679 printf("\t { %s%s%s, \"%s\" },\n", constprefix, wrap,
680 funcalias, modname) > sysautoload
681 }
682
683
684 # rumpalooza
685 if (!rumpable)
686 return
687
688 # accumulate fbases we have seen. we want the last
689 # occurence for the default __RENAME()
690 seen = funcseen[fbase]
691 funcseen[fbase] = rumpfname
692 # special case for mknod as type of last argument changed from
693 # uint32_t to dev_t
694 if ((seen && fbase != "mknod") || (!seen && fbase == "mknod"))
695 return
696
697 printf("%s rump_sys_%s(", returntype, realname) > rumpprotos
698
699 for (i = 1; i < varargc; i++)
700 if (argname[i] != "PAD")
701 printf("%s, ", uncompattype(argtype[i])) > rumpprotos
702
703 if (isvarargs)
704 printf("%s, ...)", uncompattype(argtype[varargc]))>rumpprotos
705 else
706 printf("%s)", uncompattype(argtype[argc])) > rumpprotos
707
708 printf(" __RENAME(RUMP_SYS_RENAME_%s)", toupper(fbase))> rumpprotos
709 printf(";\n") > rumpprotos
710
711 # generate forward-declares for types, apart from the
712 # braindead typedef jungle we cannot easily handle here
713 for (i = 1; i <= varargc; i++) {
714 type=uncompattype(argtype[i])
715 sub("const ", "", type)
716 ntype=type
717 sub(" *\\*.*", "", ntype);
718 if (!typeseen[ntype] && \
719 match(type, "struct") && match(type, "\\*")) {
720 typeseen[ntype] = 1
721 printf("%s;\n", ntype) > rumptypes
722 }
723 }
724 }
725
726 function printrumpsysent(insysent, compatwrap) {
727 if (modular) {
728 fn = rumpnomodule
729 flags = rumpnoflags
730 } else {
731 fn = rumpnosys
732 flags = ""
733 }
734 if (!insysent) {
735 printf("\t{%s\n\t\t.sy_call = %s,\n},\t\t/* %d = %s */\n", \
736 flags, fn, syscall, funcalias) > rumpsysent
737 return
738 }
739
740 printf("\t{") > rumpsysent
741 if (argc != 0) {
742 printf("\n\t\tns(struct %ssys_%s_args),", compatwrap_, funcalias) > rumpsysent
743 }
744
745 printf("\n\t\t.sy_call = %s,\n\t},", fn) > rumpsysent
746 printf("\t\t/* %d = %s%s */\n", syscall, compatwrap_, funcalias) > rumpsysent
747 }
748
749 function iscompattype(type) {
750 for (var in uncompattypes) {
751 if (match(type, var)) {
752 return 1
753 }
754 }
755
756 return 0
757 }
758
759 function uncompattype(type) {
760 for (var in uncompattypes) {
761 if (match(type, var)) {
762 sub(var, uncompattypes[var], type)
763 return type
764 }
765 }
766
767 return type
768 }
769
770 function printrumpsysmap(syscall, wfn, funcalias, rumpentry) {
771 printf("%-4d %-22s %-18s %s\n",
772 syscall, wfn, funcalias, rumpentry) > rumpsysmap
773 }
774
775 function putsystrace(type, compatwrap_) {
776 printf("\t/* %s */\n\tcase %d: {\n", funcname, syscall) > systrace
777 printf("\t/* %s */\n\tcase %d:\n", funcname, syscall) > systracetmp
778 printf("\t/* %s */\n\tcase %d:\n", funcname, syscall) > systraceret
779 if (argc > 0) {
780 printf("\t\tswitch(ndx) {\n") > systracetmp
781 printf("\t\tstruct %s%s_args *p = params;\n", compatwrap_, funcname) > systrace
782 for (i = 1; i <= argc; i++) {
783 arg = argtype[i]
784 sub("__restrict$", "", arg)
785 printf("\t\tcase %d:\n\t\t\tp = \"%s\";\n\t\t\tbreak;\n", i - 1, arg) > systracetmp
786 if (arg ~ /.*p_t$/ || arg ~ /.*p$/ || arg ~ /.*_t_p$/ ||
787 arg ~ /.*_pointer_t$/)
788 printf("\t\tuarg[%d] = (intptr_t) SCARG(p, %s).i32; /* %s */\n", \
789 i - 1, \
790 argname[i], arg) > systrace
791 else if (index(arg, "*") > 0 || arg == "caddr_t")
792 printf("\t\tuarg[%d] = (intptr_t) SCARG(p, %s); /* %s */\n", \
793 i - 1, \
794 argname[i], arg) > systrace
795 else if (substr(arg, 1, 1) == "u" || arg == "size_t")
796 printf("\t\tuarg[%d] = SCARG(p, %s); /* %s */\n", \
797 i - 1, \
798 argname[i], arg) > systrace
799 else
800 printf("\t\tiarg[%d] = SCARG(p, %s); /* %s */\n", \
801 i - 1, \
802 argname[i], arg) > systrace
803 }
804 printf("\t\tdefault:\n\t\t\tbreak;\n\t\t};\n") > systracetmp
805
806 printf("\t\tif (ndx == 0 || ndx == 1)\n") > systraceret
807 printf("\t\t\tp = \"%s\";\n", returntype) > systraceret
808 printf("\t\tbreak;\n") > systraceret
809 }
810 printf("\t\t*n_args = %d;\n\t\tbreak;\n\t}\n", argc) > systrace
811 printf("\t\tbreak;\n") > systracetmp
812 }
813
814 function putent(type, compatwrap) {
815 # output syscall declaration for switch table.
816 if (compatwrap == "")
817 compatwrap_ = ""
818 else
819 compatwrap_ = compatwrap "_"
820 if (argc == 0)
821 arg_type = "void";
822 else {
823 arg_type = "struct " compatwrap_ funcname "_args";
824 }
825 putsystrace(type, compatwrap_)
826 proto = "int\t" compatwrap_ funcname "(struct lwp *, const " \
827 arg_type " *, register_t *);\n"
828 if (sysmap[proto] != 1) {
829 sysmap[proto] = 1;
830 print proto > sysprotos;
831 }
832
833 # output syscall switch entry
834 printf("\t{") > sysent
835 if (argc != 0) {
836 printf("\n\t\tns(struct %s%s_args),", compatwrap_, funcname) > sysent
837 }
838 if (modular)
839 wfn = "sys_nomodule";
840 else if (compatwrap == "")
841 wfn = funcname;
842 else
843 wfn = compatwrap "(" funcname ")";
844 wfn_cast="(sy_call_t *)" wfn
845 if (sycall_flags != "0")
846 flags = "\n\t\t.sy_flags = " sycall_flags ","
847 else
848 flags = ""
849 printf("%s\n\t\t.sy_call = %s\n\t},", flags, wfn_cast) > sysent
850 printf("\t\t/* %d = %s%s */\n", syscall, compatwrap_, funcalias) > sysent
851
852 # output syscall name for names table
853 printf("\t/* %3d */\t\"%s%s\",\n", syscall, compatwrap_, funcalias) \
854 > sysnamesbottom
855
856 # output syscall number of header, if appropriate
857 if (type == "STD" || type == "NOARGS" || type == "INDIR" || \
858 type == "NOERR") {
859 # output a prototype, to be used to generate lint stubs in
860 # libc.
861 printproto("")
862 } else if (type == "COMPAT" || type == "EXTERN") {
863 # Just define the syscall number with a comment. These
864 # may be used by compatibility stubs in libc.
865 printproto(compatwrap_)
866 }
867
868 # output syscall argument structure, if it has arguments
869 if (argc != 0) {
870 printf("\n") > sysarghdr
871 if (haverumpcalls && !rumpable)
872 printf("#ifndef RUMP_CLIENT\n") > sysarghdr
873 printf("struct %s%s_args", compatwrap_, funcname) > sysarghdr
874 if (type != "NOARGS") {
875 print " {" >sysarghdr;
876 for (i = 1; i <= argc; i++)
877 printf("\tsyscallarg(%s) %s;\n", argtype[i],
878 argname[i]) > sysarghdr
879 printf "}" >sysarghdr;
880 }
881 printf(";\n") > sysarghdr
882 if (type != "NOARGS" && type != "INDIR") {
883 printf("check_syscall_args(%s%s)\n", compatwrap_,
884 funcname) >sysarghdr
885 }
886 if (haverumpcalls && !rumpable)
887 printf("#endif /* !RUMP_CLIENT */\n") > sysarghdr
888 }
889
890 if (!rumpable) {
891 if (funcname == "sys_pipe" && rumphaspipe == 1) {
892 insysent = 1
893 printrumpsysmap(syscall,
894 funcname, funcalias, "rump_sys_pipe")
895 } else {
896 insysent = 0
897 }
898 } else {
899 insysent = 1
900 }
901 printrumpsysent(insysent, compatwrap)
902
903 # output rump marshalling code if necessary
904 if (!rumpable) {
905 return
906 }
907
908 printrumpsysmap(syscall, wfn, funcalias, "rump___sysimpl_" rumpfname)
909
910 printf("\n") > rumpcalls
911
912 if (compatwrap)
913 printf("#ifdef RUMP_SYS_COMPAT\n") > rumpcalls
914
915 # need a local prototype, we export the re-re-named one in .h
916 printf("%s rump___sysimpl_%s(", returntype, rumpfname) \
917 > rumpcalls
918 for (i = 1; i < argc; i++) {
919 if (argname[i] != "PAD")
920 printf("%s, ", uncompattype(argtype[i])) > rumpcalls
921 }
922 printf("%s);", uncompattype(argtype[argc])) > rumpcalls
923
924 printf("\n%s\nrump___sysimpl_%s(", returntype, rumpfname) > rumpcalls
925 for (i = 1; i < argc; i++) {
926 if (argname[i] != "PAD")
927 printf("%s %s, ", uncompattype(argtype[i]), \
928 argname[i]) > rumpcalls
929 }
930 printf("%s %s)\n", uncompattype(argtype[argc]), argname[argc]) \
931 > rumpcalls
932 printf("{\n\tregister_t retval[2];\n") > rumpcalls
933 if (returntype != "void") {
934 if (type != "NOERR") {
935 printf("\tint error = 0;\n") > rumpcalls
936 }
937 # assume rumpcalls return only integral types
938 printf("\t%s rv = -1;\n", returntype) > rumpcalls
939 }
940
941 argarg = "NULL"
942 argsize = 0;
943 if (argc) {
944 argarg = "&callarg"
945 argsize = "sizeof(callarg)"
946 printf("\tstruct %s%s_args callarg;\n\n",compatwrap_,funcname) \
947 > rumpcalls
948 printf "\tmemset(&callarg, 0, sizeof(callarg));\n" > rumpcalls
949 for (i = 1; i <= argc; i++) {
950 if (argname[i] == "PAD") {
951 printf("\tSPARG(&callarg, %s) = 0;\n", \
952 argname[i]) > rumpcalls
953 } else {
954 if (iscompattype(argtype[i])) {
955 printf("\tSPARG(&callarg, %s) = " \
956 "(%s)%s;\n", argname[i], argtype[i], \
957 argname[i]) > rumpcalls
958 } else {
959 printf("\tSPARG(&callarg, %s) = %s;\n",\
960 argname[i], argname[i]) > rumpcalls
961 }
962 }
963 }
964 printf("\n") > rumpcalls
965 } else {
966 printf("\n") > rumpcalls
967 }
968 printf("\t") > rumpcalls
969 if (returntype != "void" && type != "NOERR")
970 printf("error = ") > rumpcalls
971 printf("rsys_syscall(%s%s%s, " \
972 "%s, %s, retval);\n", constprefix, compatwrap_, funcalias, \
973 argarg, argsize) > rumpcalls
974 if (type != "NOERR") {
975 printf("\trsys_seterrno(error);\n") > rumpcalls
976 printf("\tif (error == 0) {\n") > rumpcalls
977 indent="\t\t"
978 ending="\t}\n"
979 } else {
980 indent="\t"
981 ending=""
982 }
983 if (returntype != "void") {
984 printf("%sif (sizeof(%s) > sizeof(register_t))\n", \
985 indent, returntype) > rumpcalls
986 printf("%s\trv = *(%s *)retval;\n", \
987 indent, returntype) > rumpcalls
988 printf("%selse\n", indent, indent) > rumpcalls
989 printf("%s\trv = *retval;\n", indent, returntype) > rumpcalls
990 printf("%s", ending) > rumpcalls
991 printf("\treturn rv;\n") > rumpcalls
992 }
993 printf("}\n") > rumpcalls
994
995 printf("#ifdef RUMP_KERNEL_IS_LIBC\n") > rumpcalls
996
997 # create the bog-standard, non-renamed public name.
998 # this way we get e.g. select instead of just __select50
999 if (fcompat)
1000 printf("__weak_alias(%s,rump___sysimpl_%s);\n", \
1001 fbase, rumpfname) > rumpcalls
1002
1003 printf("__weak_alias(%s,rump___sysimpl_%s);\n", \
1004 funcalias, rumpfname) > rumpcalls
1005 printf("__weak_alias(_%s,rump___sysimpl_%s);\n", \
1006 funcalias, rumpfname) > rumpcalls
1007 printf("__strong_alias(_sys_%s,rump___sysimpl_%s);\n", \
1008 funcalias, rumpfname) >rumpcalls
1009
1010 printf("#endif /* RUMP_KERNEL_IS_LIBC */\n") > rumpcalls
1011
1012 if (compatwrap)
1013 printf("#endif /* RUMP_SYS_COMPAT */\n") > rumpcalls
1014
1015 }
1016 $2 == "STD" || $2 == "NODEF" || $2 == "NOARGS" || $2 == "INDIR" \
1017 || $2 == "NOERR" {
1018 parseline()
1019 putent($2, "")
1020 syscall++
1021 next
1022 }
1023 $2 == "OBSOL" || $2 == "UNIMPL" || $2 == "EXCL" || $2 == "IGNORED" {
1024 if ($2 == "OBSOL")
1025 comment="obsolete"
1026 else if ($2 == "EXCL")
1027 comment="excluded"
1028 else if ($2 == "IGNORED")
1029 comment="ignored"
1030 else
1031 comment="unimplemented"
1032 for (i = 3; i <= NF; i++)
1033 comment=comment " " $i
1034
1035 if ($2 == "IGNORED")
1036 sys_stub = "(sy_call_t *)nullop";
1037 else
1038 sys_stub = sys_nosys;
1039
1040 print fillerpsysent(syscall, "", sys_stub, comment) > sysent
1041 print fillerpsysent(syscall, rumpnoflags, rumpnosys, comment) > rumpsysent
1042 printf("\t/* %3d */\t\"#%d (%s)\",\n", syscall, syscall, comment) \
1043 > sysnamesbottom
1044 if ($2 != "UNIMPL")
1045 printf("\t\t\t\t/* %d is %s */\n", syscall, comment) > sysnumhdr
1046 syscall++
1047 next
1048 }
1049 $2 == "EXTERN" {
1050 parseline()
1051 putent("EXTERN", "")
1052 syscall++
1053 next
1054 }
1055 {
1056 for (i = 1; i <= ncompat; i++) {
1057 if ($2 == compat_upper[i]) {
1058 parseline();
1059 putent("COMPAT", compat[i])
1060 syscall++
1061 next
1062 }
1063 }
1064 printf("%s: line %d: unrecognized keyword %s\n", infile, NR, $2)
1065 exit 1
1066 }
1067 END {
1068 # output pipe() syscall with its special retval[2] handling
1069 if (rumphaspipe) {
1070 printf("int rump_sys_pipe(int *);\n") > rumpprotos
1071 printf("\nint rump_sys_pipe(int *);\n") > rumpcalls
1072 printf("int\nrump_sys_pipe(int *fd)\n{\n") > rumpcalls
1073 printf("\tregister_t retval[2];\n") > rumpcalls
1074 printf("\tint error = 0;\n") > rumpcalls
1075 printf("\n\terror = rsys_syscall(SYS_pipe, ") > rumpcalls
1076 printf("NULL, 0, retval);\n") > rumpcalls
1077 printf("\tif (error) {\n") > rumpcalls
1078 printf("\t\trsys_seterrno(error);\n") > rumpcalls
1079 printf("\t} else {\n\t\tfd[0] = retval[0];\n") > rumpcalls
1080 printf("\t\tfd[1] = retval[1];\n\t}\n") > rumpcalls
1081 printf("\treturn error ? -1 : 0;\n}\n") > rumpcalls
1082 printf("#ifdef RUMP_KERNEL_IS_LIBC\n") > rumpcalls
1083 printf("__weak_alias(pipe,rump_sys_pipe);\n") > rumpcalls
1084 printf("__weak_alias(_pipe,rump_sys_pipe);\n") > rumpcalls
1085 printf("__strong_alias(_sys_pipe,rump_sys_pipe);\n") > rumpcalls
1086 printf("#endif\n") > rumpcalls
1087 }
1088
1089 # print default rump syscall interfaces
1090 for (var in funcseen) {
1091 printf("#ifndef RUMP_SYS_RENAME_%s\n", \
1092 toupper(var)) > rumpcallshdr
1093 printf("#define RUMP_SYS_RENAME_%s rump___sysimpl_%s\n", \
1094 toupper(var), funcseen[var]) > rumpcallshdr
1095 printf("#endif\n\n") > rumpcallshdr
1096 }
1097
1098 maxsyscall = syscall
1099 if (nsysent) {
1100 if (syscall > nsysent) {
1101 printf("%s: line %d: too many syscalls [%d > %d]\n", infile, NR, syscall, nsysent)
1102 exit 1
1103 }
1104 while (syscall < nsysent) {
1105 print fillerpsysent(syscall, "", sys_nosys, "filler") > sysent
1106 print fillerpsysent(syscall, rumpnoflags, rumpnosys, "filler") > rumpsysent
1107 printf("\t/* %3d */\t\"# filler\",\n", syscall) \
1108 > sysnamesbottom
1109 syscall++
1110 }
1111 }
1112 printf("};\n") > sysent
1113 printf("};\n") > rumpsysent
1114 printf("CTASSERT(__arraycount(rump_sysent) == SYS_NSYSENT);\n") > rumpsysent
1115 printf("__strong_alias(rumpns_sysent,rump_sysent);\n") > rumpsysent
1116 printf("#endif /* RUMP_CLIENT */\n") > rumpsysent
1117 if (haverumpcalls)
1118 printf("#endif /* !RUMP_CLIENT */\n") > sysprotos
1119 printf("};\n") > sysnamesbottom
1120 printf("#define\t%sMAXSYSCALL\t%d\n", constprefix, maxsyscall) > sysnumhdr
1121 if (nsysent)
1122 printf("#define\t%sNSYSENT\t%d\n", constprefix, nsysent) > sysnumhdr
1123 printf "\tdefault:\n\t\t*n_args = 0;\n\t\tbreak;\n\t};\n}\n" > systrace
1124 printf "\tdefault:\n\t\tbreak;\n\t};\n\tif (p != NULL)\n\t\tstrlcpy(desc, p, descsz);\n}\n" > systracetmp
1125 printf "\tdefault:\n\t\tbreak;\n\t};\n\tif (p != NULL)\n\t\tstrlcpy(desc, p, descsz);\n}\n" > systraceret
1126 } '
1127
1128 cat $sysprotos >> $sysarghdr
1129 echo "#endif /* _${constprefix}SYSCALL_H_ */" >> $sysnumhdr
1130 echo "#endif /* _${constprefix}SYSCALLARGS_H_ */" >> $sysarghdr
1131 echo "};" >> $sysautoload
1132 printf "\n#endif /* _RUMP_RUMP_SYSCALLS_H_ */\n" >> $rumpprotos
1133 cat $sysdcl $sysent > $syssw
1134 cat $sysnamesbottom >> $sysnames
1135 cat $rumpsysent >> $rumpcalls
1136
1137 touch $rumptypes
1138 cat $rumptypes >> $rumpcallshdr
1139 echo >> $rumpcallshdr
1140 cat $rumpprotos >> $rumpcallshdr
1141
1142 #chmod 444 $sysnames $sysnumhdr $syssw
1143
1144 cat $systracetmp >> $systrace
1145 cat $systraceret >> $systrace
1146
1147 echo Generated following files:
1148 echo $sysarghdr $sysnumhdr $syssw $sysnames $sysautoload $systrace $rumpcalls $rumpcallshdr $rumpsysmap
1149