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