Home | History | Annotate | Line # | Download | only in kern
      1 #	$NetBSD: makesyscalls.sh,v 1.187 2023/07/28 18:19:01 christos 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	[optional] 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 conversion.\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 	uncompattypes["struct kevent100"] = "struct kevent";
    423 
    424 	next
    425 }
    426 NF == 0 || $1 ~ /^;/ {
    427 	next
    428 }
    429 $0 ~ /^%%$/ {
    430 	intable = 1
    431 	next
    432 }
    433 $1 ~ /^#[ 	]*include/ {
    434 	print > sysdcl
    435 	print > sysnames
    436 	print > sysautoload
    437 	next
    438 }
    439 $1 ~ /^#/ && !intable {
    440 	print > sysdcl
    441 	print > sysnames
    442 	print > sysautoload
    443 	next
    444 }
    445 $1 ~ /^#/ && intable {
    446 	if ($1 ~ /^#[ 	]*if/) {
    447 		savedepth++
    448 		savesyscall[savedepth] = syscall
    449 	}
    450 	if ($1 ~ /^#[ 	]*else/) {
    451 		if (savedepth <= 0) {
    452 			printf("%s: line %d: unbalanced #else\n", \
    453 			    infile, NR)
    454 			exit 1
    455 		}
    456 		syscall = savesyscall[savedepth]
    457 	}
    458 	if ($1 ~ /^#[       ]*endif/) {
    459 		if (savedepth <= 0) {
    460 			printf("%s: line %d: unbalanced #endif\n", \
    461 			    infile, NR)
    462 			exit 1
    463 		}
    464 		savedepth--
    465 	}
    466 	print > sysent
    467 	print > sysarghdr
    468 	print > sysnumhdr
    469 	print > sysprotos
    470 	print > sysautoloadbottom
    471 	print > sysnamesbottom
    472 	print > sysnamesfriendly
    473 	print > systrace
    474 	print > systracetmp
    475 	print > systraceret
    476 
    477 	# XXX: technically we do not want to have conditionals in rump,
    478 	# but it is easier to just let the cpp handle them than try to
    479 	# figure out what we want here in this script
    480 	print > rumpsysent
    481 	next
    482 }
    483 syscall != $1 {
    484 	printf "%s: line %d: syscall number out of sync at %d\n", \
    485 	   infile, NR, syscall
    486 	printf "line is:\n"
    487 	print
    488 	exit 1
    489 }
    490 function isarg64(type) {
    491 	gsub("netbsd32_", "", type);
    492 	return type == "quad_t" || type == "off_t" \
    493 	    || type == "dev_t" ||  type == "time_t";
    494 }
    495 function parserr(was, wanted) {
    496 	printf "%s: line %d: unexpected %s (expected <%s>)\n", \
    497 	    infile, NR, was, wanted
    498 	printf "line is:\n"
    499 	print
    500 	exit 1
    501 }
    502 function fillerpsysent(syscall, flags, name, comment) {
    503 	return sprintf("\t{%s\n\t\t.sy_call = %s,\n\t},\t\t/* %d = filler */",\
    504 	    flags, name, syscall, comment);
    505 }
    506 function parseline() {
    507 	f=3			# toss number and type
    508 	if ($2 == "INDIR")
    509 		sycall_flags="SYCALL_INDIRECT"
    510 	else
    511 		sycall_flags="0"
    512 	if ($NF != "}") {
    513 		funcalias=$NF
    514 		end=NF-1
    515 	} else {
    516 		funcalias=""
    517 		end=NF
    518 	}
    519 	if ($f == "INDIR") {		# allow for "NOARG INDIR"
    520 		sycall_flags = "SYCALL_INDIRECT | " sycall_flags
    521 		f++
    522 	}
    523 	if ($f == "MODULAR") {		# registered at runtime
    524 		modular = 1
    525 		f++
    526 		modname = $f
    527 		f++
    528 	} else {
    529 		modular =  0;
    530 	}
    531 	if ($f == "RUMP") {
    532 		rumpable = 1
    533 		f++
    534 	} else {
    535 		rumpable = 0
    536 	}
    537 	if ($f ~ /^[a-z0-9_]*$/) {	# allow syscall alias
    538 		funcalias=$f
    539 		f++
    540 	}
    541 	if ($f != "{")
    542 		parserr($f, "{")
    543 	f++
    544 	if ($end != "}")
    545 		parserr($end, "}")
    546 	end--
    547 	if ($end != ";")
    548 		parserr($end, ";")
    549 	end--
    550 	if ($end != ")")
    551 		parserr($end, ")")
    552 	end--
    553 
    554 	returntype = oldf = "";
    555 	do {
    556 		if (returntype != "" && oldf != "*")
    557 			returntype = returntype" ";
    558 		returntype = returntype$f;
    559 		oldf = $f;
    560 		f++
    561 	} while ($f != "|" && f < (end-1))
    562 	if (f == (end - 1)) {
    563 		parserr($f, "function argument definition (maybe \"|\"?)");
    564 	}
    565 	f++
    566 
    567 	fprefix=$f
    568 	f++
    569 	if ($f != "|") {
    570 		parserr($f, "function compat delimiter (maybe \"|\"?)");
    571 	}
    572 	f++
    573 
    574 	fcompat=""
    575 	if ($f != "|") {
    576 		fcompat=$f
    577 		f++
    578 	}
    579 
    580 	if ($f != "|") {
    581 		parserr($f, "function name delimiter (maybe \"|\"?)");
    582 	}
    583 	f++
    584 	fbase=$f
    585 
    586 	# pipe is special in how to returns its values.
    587 	# So just generate it manually if present.
    588 	if (rumpable == 1 && fbase == "pipe") {
    589 		rumpable = 0;
    590 		rumphaspipe = 1;
    591 	}
    592 
    593 	if (fcompat != "") {
    594 		funcname=fprefix "___" fbase "" fcompat
    595 	} else {
    596 		funcname=fprefix "_" fbase
    597 	}
    598 	if (isarg64(returntype)) {
    599 		if (sycall_flags == "0")
    600 			sycall_flags = "SYCALL_RET_64";
    601 		else
    602 			sycall_flags = "SYCALL_RET_64 | " sycall_flags;
    603 	}
    604 
    605 	if (funcalias == "") {
    606 		funcalias=funcname
    607 		sub(/^([^_]+_)*sys_/, "", funcalias)
    608 		realname=fbase
    609 		rumpfname=realname "" fcompat
    610 	} else {
    611 		realname=funcalias
    612 		rumpfname=realname
    613 	}
    614 
    615 	f++
    616 
    617 	if ($f != "(")
    618 		parserr($f, "(")
    619 	f++
    620 
    621 	argc=0;
    622 	argalign=0;
    623 	if (f == end) {
    624 		if ($f != "void")
    625 			parserr($f, "argument definition")
    626 		isvarargs = 0;
    627 		varargc = 0;
    628 		argtype[0]="void";
    629 		return
    630 	}
    631 
    632 	# some system calls (open() and fcntl()) can accept a variable
    633 	# number of arguments.  If syscalls accept a variable number of
    634 	# arguments, they must still have arguments specified for
    635 	# the remaining argument "positions," because of the way the
    636 	# kernel system call argument handling works.
    637 	#
    638 	# Indirect system calls, e.g. syscall(), are exceptions to this
    639 	# rule, since they are handled entirely by machine-dependent code
    640 	# and do not need argument structures built.
    641 
    642 	isvarargs = 0;
    643 	args64 = 0;
    644 	ptr = 0;
    645 	while (f <= end) {
    646 		if ($f == "...") {
    647 			f++;
    648 			isvarargs = 1;
    649 			varargc = argc;
    650 			continue;
    651 		}
    652 		argc++
    653 		argtype[argc]=""
    654 		oldf=""
    655 		while (f < end && $(f+1) != ",") {
    656 			if (argtype[argc] != "" && oldf != "*")
    657 				argtype[argc] = argtype[argc]" ";
    658 			argtype[argc] = argtype[argc]$f;
    659 			oldf = $f;
    660 			f++
    661 		}
    662 		if (argtype[argc] == "")
    663 			parserr($f, "argument definition")
    664 		if (argtype[argc] == "off_t"  \
    665 		  || argtype[argc] == "dev_t" \
    666 		  || argtype[argc] == "time_t") {
    667 			if ((argalign % 2) != 0 && sysalign &&
    668 			    funcname != "sys_posix_fadvise") # XXX for now
    669 				parserr($f, "a padding argument")
    670 		} else {
    671 			argalign++;
    672 		}
    673 		if (isarg64(argtype[argc])) {
    674 			if (sycall_flags == "0")
    675 				sycall_flags = "SYCALL_ARG"argc-1"_64";
    676 			else
    677 				sycall_flags = "SYCALL_ARG"argc-1"_64 | " sycall_flags;
    678 			args64++;
    679 		}
    680 		if (index(argtype[argc], "*") != 0 && ptr == 0) {
    681 			if (sycall_flags == "0")
    682 				sycall_flags = "SYCALL_ARG_PTR";
    683 			else
    684 				sycall_flags = "SYCALL_ARG_PTR | " sycall_flags;
    685 			ptr = 1;
    686 		}
    687 		argname[argc]=$f;
    688 		f += 2;			# skip name, and any comma
    689 	}
    690 	if (args64 > 0)
    691 		sycall_flags = "SYCALL_NARGS64_VAL("args64") | " sycall_flags;
    692 	# must see another argument after varargs notice.
    693 	if (isvarargs) {
    694 		if (argc == varargc)
    695 			parserr($f, "argument definition")
    696 	} else
    697 		varargc = argc;
    698 }
    699 
    700 function printproto(wrap) {
    701 	printf("/* syscall: \"%s%s\" ret: \"%s\" args:", wrap, funcalias,
    702 	    returntype) > sysnumhdr
    703 	for (i = 1; i <= varargc; i++)
    704 		printf(" \"%s\"", argtype[i]) > sysnumhdr
    705 	if (isvarargs)
    706 		printf(" \"...\"") > sysnumhdr
    707 	printf(" */\n") > sysnumhdr
    708 	printf("#define\t%s%s%s\t%d\n\n", constprefix, wrap, funcalias,
    709 	    syscall) > sysnumhdr
    710 
    711 	# output entry for syscall autoload table, if modular
    712 	if (modular) {
    713 		printf("\t    { %s%s%s, \"%s\" },\n", constprefix, wrap,
    714 		   funcalias, modname) > sysautoloadbottom
    715 	}
    716 
    717 
    718 	# rumpalooza
    719 	if (!rumpable)
    720 		return
    721 
    722 	# accumulate fbases we have seen.  we want the last
    723 	# occurrence for the default __RENAME()
    724 	seen = funcseen[realname]
    725 	funcseen[realname] = rumpfname
    726 	# special case for mknod as type of last argument changed from
    727 	# uint32_t to dev_t
    728 	if ((seen && fbase != "mknod") || (!seen && fbase == "mknod"))
    729 		return
    730 
    731 	printf("%s rump_sys_%s(", returntype, realname) > rumpprotos
    732 
    733 	for (i = 1; i < varargc; i++)
    734 		if (argname[i] != "PAD")
    735 			printf("%s, ", uncompattype(argtype[i])) > rumpprotos
    736 
    737 	if (isvarargs)
    738 		printf("%s, ...)", uncompattype(argtype[varargc]))>rumpprotos
    739 	else
    740 		printf("%s)", uncompattype(argtype[argc])) > rumpprotos
    741 
    742 	printf(" __RENAME(RUMP_SYS_RENAME_%s)", toupper(realname))> rumpprotos
    743 	printf(";\n") > rumpprotos
    744 
    745 	# generate forward-declares for types, apart from the
    746 	# braindead typedef jungle we cannot easily handle here
    747 	for (i = 1; i <= varargc; i++) {
    748 		type=uncompattype(argtype[i])
    749 		sub("const ", "", type)
    750 		ntype=type
    751 		sub(" *\\*.*", "", ntype);
    752 		if (!typeseen[ntype] && \
    753 		    match(type, "struct") && match(type, "\\*")) {
    754 			typeseen[ntype] = 1
    755 			printf("%s;\n", ntype) > rumptypes
    756 		}
    757 	}
    758 }
    759 
    760 function printrumpsysent(insysent, compatwrap_) {
    761 	if (modular) {
    762 		fn = rumpnomodule
    763 		flags = rumpnoflags
    764 	} else {
    765 		fn = rumpnosys
    766 		flags = ""
    767 	}
    768 	if (!insysent) {
    769 		printf("\t{%s\n\t\t.sy_call = %s,\n},\t\t/* %d = %s */\n", \
    770 		    flags, fn, syscall, funcalias) > rumpsysent
    771 		return
    772 	}
    773 
    774 	printf("\t{") > rumpsysent
    775 	if (argc != 0) {
    776 		printf("\n\t\tns(struct %s%s_args),", compatwrap_, funcname) > rumpsysent
    777 	}
    778 
    779 	printf("\n\t\t.sy_call = %s,\n\t},", fn) > rumpsysent
    780 	printf("\t\t/* %d = %s%s */\n", syscall, compatwrap_, funcalias) > rumpsysent
    781 }
    782 
    783 function iscompattype(type) {
    784 	for (var in uncompattypes) {
    785 		if (match(type, var)) {
    786 			return 1
    787 		}
    788 	}
    789 
    790 	return 0
    791 }
    792 
    793 function uncompattype(type) {
    794 	for (var in uncompattypes) {
    795 		if (match(type, var)) {
    796 			sub(var, uncompattypes[var], type)
    797 			return type
    798 		}
    799 	}
    800 
    801 	return type
    802 }
    803 
    804 function printrumpsysmap(syscall, wfn, funcalias, rumpentry) {
    805 	printf("%-4d %-22s %-18s %s\n",
    806 	    syscall, wfn, funcalias, rumpentry) > rumpsysmap
    807 }
    808 
    809 function fixarray(arg) {
    810 	iii = index(arg, "[")
    811 	if (iii == 0) {
    812 		return arg 
    813 	} else {
    814 		return substr(arg, 1, iii - 1) "[0]"
    815 	}
    816 }
    817 
    818 function putsystrace(type, compatwrap_) {
    819 	printf("\t/* %s */\n\tcase %d: {\n", funcname, syscall) > systrace
    820 	printf("\t/* %s */\n\tcase %d:\n", funcname, syscall) > systracetmp
    821 	printf("\t/* %s */\n\tcase %d:\n", funcname, syscall) > systraceret
    822 	if (argc > 0) {
    823 		printf("\t\tswitch(ndx) {\n") > systracetmp
    824 		printf("\t\tconst struct %s%s_args *p = params;\n", compatwrap_, funcname) > systrace
    825 		for (i = 1; i <= argc; i++) {
    826 			arg = argtype[i]
    827 			sub("__restrict$", "", arg)
    828 			printf("\t\tcase %d:\n\t\t\tp = \"%s\";\n\t\t\tbreak;\n", i - 1, arg) > systracetmp
    829 			if (arg ~ /.*p_t$/ || arg ~ /.*p$/ || arg ~ /.*_t_p$/ ||
    830 			    arg ~ /.*_pointer_t$/)
    831 				printf("\t\tuarg[%d] = (intptr_t) SCARG(p, %s).i32; /* %s */\n", \
    832 				     i - 1, \
    833 				     argname[i], arg) > systrace
    834 			else if (index(arg, "*") > 0 || arg == "caddr_t" ||
    835 			    arg ~ /.*_handler_t$/)
    836 				printf("\t\tuarg[%d] = (intptr_t) SCARG(p, %s); /* %s */\n", \
    837 				     i - 1, \
    838 				     fixarray(argname[i]), arg) > systrace
    839 			else if (substr(arg, 1, 1) == "u" || arg == "size_t")
    840 				printf("\t\tuarg[%d] = SCARG(p, %s); /* %s */\n", \
    841 				     i - 1, \
    842 				     argname[i], arg) > systrace
    843 			else
    844 				printf("\t\tiarg[%d] = SCARG(p, %s); /* %s */\n", \
    845 				     i - 1, \
    846 				     fixarray(argname[i]), arg) > systrace
    847 		}
    848 		printf("\t\tdefault:\n\t\t\tbreak;\n\t\t};\n") > systracetmp
    849 
    850 		printf("\t\tif (ndx == 0 || ndx == 1)\n") > systraceret
    851 		printf("\t\t\tp = \"%s\";\n", returntype) > systraceret
    852 		printf("\t\tbreak;\n") > systraceret
    853 	}
    854 	printf("\t\t*n_args = %d;\n\t\tbreak;\n\t}\n", argc) > systrace
    855 	printf("\t\tbreak;\n") > systracetmp
    856 }
    857 
    858 function putent(type, compatwrap) {
    859 	# output syscall declaration for switch table.
    860 	if (compatwrap == "")
    861 		compatwrap_ = ""
    862 	else
    863 		compatwrap_ = compatwrap "_"
    864 	if (argc == 0)
    865 		arg_type = "void";
    866 	else {
    867 		arg_type = "struct " compatwrap_ funcname "_args";
    868 	}
    869 	putsystrace(type, compatwrap_)
    870 	proto = "int\t" compatwrap_ funcname "(struct lwp *, const " \
    871 	    arg_type " *, register_t *);\n"
    872 	if (sysmap[proto] != 1) {
    873 		sysmap[proto] = 1;
    874 		print proto > sysprotos;
    875 	}
    876 
    877 	# output syscall switch entry
    878 	printf("\t{") > sysent
    879 	if (argc != 0) {
    880 		printf("\n\t\tns(struct %s%s_args),", compatwrap_, funcname) > sysent
    881 	}
    882 	if (modular) {
    883 		wfn = "sys_nomodule";
    884 		idx = int(syscall / 32);
    885 		bit = 2 ^ (syscall % 32);
    886 		nomodbits[ idx ] += bit;
    887 	} else if (compatwrap == "")
    888 		wfn = funcname;
    889 	else
    890 		wfn = compatwrap "(" funcname ")";
    891 	wfn_cast="(sy_call_t *)" wfn
    892 	if (sycall_flags != "0")
    893 		flags = "\n\t\t.sy_flags = " sycall_flags ","
    894 	else
    895 		flags = ""
    896 	printf("%s\n\t\t.sy_call = %s\n\t},", flags, wfn_cast) > sysent
    897 	printf("\t\t/* %d = %s%s */\n", syscall, compatwrap_, funcalias) > sysent
    898 
    899 	# output syscall name for names table
    900 	printf("\t/* %3d */\t\"%s%s\",\n", syscall, compatwrap_, funcalias) \
    901 	    > sysnamesbottom
    902 	if (compatwrap_ != "" || fbase == funcalias)
    903 		printf("\t/* %3d */\tNULL, /* %s%s */\n", syscall, \
    904 		    compatwrap_, funcalias) > sysnamesfriendly
    905 	else
    906 		printf("\t/* %3d */\t\"%s%s\",\n", syscall, compatwrap_, \
    907 		    fbase) > sysnamesfriendly
    908 
    909 	# output syscall number of header, if appropriate
    910 	if (type == "STD" || type == "NOARGS" || type == "INDIR" || \
    911 	    type == "NOERR") {
    912 		# output a prototype, to be used to generate lint stubs in
    913 		# libc.
    914 		printproto("")
    915 	} else if (type == "COMPAT" || type == "EXTERN") {
    916 		# Just define the syscall number with a comment.  These
    917 		# may be used by compatibility stubs in libc.
    918 		printproto(compatwrap_)
    919 	}
    920 
    921 	# output syscall argument structure, if it has arguments
    922 	if (argc != 0) {
    923 		printf("\n") > sysarghdr
    924 		if (haverumpcalls && !rumpable)
    925 			printf("#ifndef RUMP_CLIENT\n") > sysarghdr
    926 		printf("struct %s%s_args", compatwrap_, funcname) > sysarghdr
    927 		if (type != "NOARGS") {
    928 			print " {" >sysarghdr;
    929 			for (i = 1; i <= argc; i++)
    930 				printf("\tsyscallarg(%s) %s;\n", argtype[i],
    931 				    argname[i]) > sysarghdr
    932 			printf "}" >sysarghdr;
    933 		}
    934 		printf(";\n") > sysarghdr
    935 		if (type != "NOARGS" && type != "INDIR") {
    936 			printf("check_syscall_args(%s%s)\n", compatwrap_,
    937 			    funcname) >sysarghdr
    938 		}
    939 		if (haverumpcalls && !rumpable)
    940 			printf("#endif /* !RUMP_CLIENT */\n") > sysarghdr
    941 	}
    942 
    943 	if (!rumpable) {
    944 		if (funcname == "sys_pipe" && rumphaspipe == 1) {
    945 			insysent = 1
    946 			printrumpsysmap(syscall,
    947 			    funcname, funcalias, "rump_sys_pipe")
    948 		} else {
    949 			insysent = 0
    950 		}
    951 	} else {
    952 		insysent = 1
    953 	}
    954 	printrumpsysent(insysent, compatwrap_)
    955 
    956 	# output rump marshalling code if necessary
    957 	if (!rumpable) {
    958 		return
    959 	}
    960 
    961 	printrumpsysmap(syscall, wfn, funcalias, "rump___sysimpl_" rumpfname)
    962 
    963 	printf("\n") > rumpcalls
    964 
    965 	if (compatwrap)
    966 		printf("#ifdef RUMP_SYS_COMPAT\n") > rumpcalls
    967 
    968 	# need a local prototype, we export the re-re-named one in .h
    969 	printf("%s rump___sysimpl_%s(", returntype, rumpfname) \
    970 	    > rumpcalls
    971 	for (i = 1; i < argc; i++) {
    972 		if (argname[i] != "PAD")
    973 			printf("%s, ", uncompattype(argtype[i])) > rumpcalls
    974 	}
    975 	printf("%s);", uncompattype(argtype[argc])) > rumpcalls
    976 
    977 	printf("\n%s\nrump___sysimpl_%s(", returntype, rumpfname) > rumpcalls
    978 	for (i = 1; i < argc; i++) {
    979 		if (argname[i] != "PAD")
    980 			printf("%s %s, ", uncompattype(argtype[i]), \
    981 			    argname[i]) > rumpcalls
    982 	}
    983 	printf("%s %s)\n", uncompattype(argtype[argc]), argname[argc]) \
    984 	    > rumpcalls
    985 	printf("{\n\tregister_t retval[2];\n") > rumpcalls
    986 	if (returntype != "void") {
    987 		if (type != "NOERR") {
    988 			printf("\tint error = 0;\n") > rumpcalls
    989 		}
    990 		# assume rumpcalls return only integral types
    991 		printf("\t%s rv = -1;\n", returntype) > rumpcalls
    992 	}
    993 
    994 	argarg = "NULL"
    995 	argsize = 0;
    996 	if (argc) {
    997 		argarg = "&callarg"
    998 		argsize = "sizeof(callarg)"
    999 		printf("\tstruct %s%s_args callarg;\n\n",compatwrap_,funcname) \
   1000 		    > rumpcalls
   1001 		printf "\tmemset(&callarg, 0, sizeof(callarg));\n" > rumpcalls
   1002 		for (i = 1; i <= argc; i++) {
   1003 			if (argname[i] == "PAD") {
   1004 				printf("\tSPARG(&callarg, %s) = 0;\n", \
   1005 				    argname[i]) > rumpcalls
   1006 			} else {
   1007 				if (iscompattype(argtype[i])) {
   1008 					printf("\tSPARG(&callarg, %s) = "    \
   1009 					"(%s)%s;\n", argname[i], argtype[i], \
   1010 					argname[i]) > rumpcalls
   1011 				} else {
   1012 					printf("\tSPARG(&callarg, %s) = %s;\n",\
   1013 					    argname[i], argname[i]) > rumpcalls
   1014 				}
   1015 			}
   1016 		}
   1017 		printf("\n") > rumpcalls
   1018 	} else {
   1019 		printf("\n") > rumpcalls
   1020 	}
   1021 	printf("\t") > rumpcalls
   1022 	if (returntype != "void" && type != "NOERR")
   1023 		printf("error = ") > rumpcalls
   1024 	else if (returntype != "void")
   1025 		printf("(void)") > rumpcalls
   1026 	printf("rsys_syscall(%s%s%s, " \
   1027 	    "%s, %s, retval);\n", constprefix, compatwrap_, funcalias, \
   1028 	    argarg, argsize) > rumpcalls
   1029 	if (type != "NOERR") {
   1030 		printf("\trsys_seterrno(error);\n") > rumpcalls
   1031 		printf("\tif (error == 0) {\n") > rumpcalls
   1032 		indent="\t\t"
   1033 		ending="\t}\n"
   1034 	} else {
   1035 		indent="\t"
   1036 		ending=""
   1037 	}
   1038 	if (returntype != "void") {
   1039 		printf("%sif (sizeof(%s) > sizeof(register_t))\n", \
   1040 		    indent, returntype) > rumpcalls
   1041 		printf("%s\trv = *(%s *)retval;\n", \
   1042 		    indent, returntype) > rumpcalls
   1043 		printf("%selse\n", indent, indent) > rumpcalls
   1044 		printf("%s\trv = *retval;\n", indent, returntype) > rumpcalls
   1045 		printf("%s", ending) > rumpcalls
   1046 		printf("\treturn rv;\n") > rumpcalls
   1047 	}
   1048 	printf("}\n") > rumpcalls
   1049 
   1050 	printf("#ifdef RUMP_KERNEL_IS_LIBC\n") > rumpcalls
   1051 
   1052 	# create the bog-standard, non-renamed public name.
   1053 	# this way we get e.g. select instead of just __select50
   1054 	if (fcompat)
   1055 		printf("__weak_alias(%s,rump___sysimpl_%s);\n", \
   1056 		    fbase, rumpfname) > rumpcalls
   1057 
   1058 	printf("__weak_alias(%s,rump___sysimpl_%s);\n", \
   1059 	    funcalias, rumpfname) > rumpcalls
   1060 	printf("__weak_alias(_%s,rump___sysimpl_%s);\n", \
   1061 	    funcalias, rumpfname) > rumpcalls
   1062 	printf("__strong_alias(_sys_%s,rump___sysimpl_%s);\n", \
   1063 	    funcalias, rumpfname) >rumpcalls
   1064 
   1065 	printf("#endif /* RUMP_KERNEL_IS_LIBC */\n") > rumpcalls
   1066 
   1067 	if (compatwrap)
   1068 		printf("#endif /* RUMP_SYS_COMPAT */\n") > rumpcalls
   1069 
   1070 }
   1071 $2 == "STD" || $2 == "NODEF" || $2 == "NOARGS" || $2 == "INDIR" \
   1072     || $2 == "NOERR" {
   1073 	parseline()
   1074 	putent($2, "")
   1075 	syscall++
   1076 	next
   1077 }
   1078 $2 == "OBSOL" || $2 == "UNIMPL" || $2 == "EXCL" || $2 == "IGNORED" {
   1079 	if ($2 == "OBSOL")
   1080 		comment="obsolete"
   1081 	else if ($2 == "EXCL")
   1082 		comment="excluded"
   1083 	else if ($2 == "IGNORED")
   1084 		comment="ignored"
   1085 	else
   1086 		comment="unimplemented"
   1087 	for (i = 3; i <= NF; i++)
   1088 		comment=comment " " $i
   1089 
   1090 	if ($2 == "IGNORED")
   1091 		sys_stub = "(sy_call_t *)(void *)nullop";
   1092 	else
   1093 		sys_stub = sys_nosys;
   1094 
   1095 	print fillerpsysent(syscall, "", sys_stub, comment) > sysent
   1096 	print fillerpsysent(syscall, rumpnoflags, rumpnosys, comment) > rumpsysent
   1097 	printf("\t/* %3d */\t\"#%d (%s)\",\n", syscall, syscall, comment) \
   1098 	    > sysnamesbottom
   1099 	printf("\t/* %3d */\tNULL, /* %s */\n", syscall, comment) \
   1100 	    > sysnamesfriendly
   1101 	if ($2 != "UNIMPL")
   1102 		printf("\t\t\t\t/* %d is %s */\n", syscall, comment) > sysnumhdr
   1103 	syscall++
   1104 	next
   1105 }
   1106 $2 == "EXTERN" {
   1107 	parseline()
   1108 	putent("EXTERN", "")
   1109 	syscall++
   1110 	next
   1111 }
   1112 {
   1113 	for (i = 1; i <= ncompat; i++) {
   1114 		if ($2 == compat_upper[i]) {
   1115 			parseline();
   1116 			putent("COMPAT", compat[i])
   1117 			syscall++
   1118 			next
   1119 		}
   1120 	}
   1121 	printf("%s: line %d: unrecognized keyword %s\n", infile, NR, $2)
   1122 	exit 1
   1123 }
   1124 
   1125 function sort(arr, n,    i, j, t) {
   1126 	# this is going to be bubble sort because I cannot be bothered to
   1127 	# write a real sort, this whole script is hopefully going to be
   1128 	# deprecated before much longer, and performance of a few hundred
   1129 	# things (even interpreted in awk) should be adequate.
   1130 	for (i = 1; i <= n; i++) {
   1131 		for (j = i + 1; j <= n; j++) {
   1132 			if (arr[j] < arr[i]) {
   1133 				t = arr[i];
   1134 				arr[i] = arr[j];
   1135 				arr[j] = t;
   1136 			}
   1137 		}
   1138 	}
   1139 	return 0;
   1140 }
   1141 
   1142 END {
   1143 	# output pipe() syscall with its special retval[2] handling
   1144 	if (rumphaspipe) {
   1145 		printf("int rump_sys_pipe(int *);\n") > rumpprotos
   1146 		printf("\nint rump_sys_pipe(int *);\n") > rumpcalls
   1147 		printf("int\nrump_sys_pipe(int *fd)\n{\n") > rumpcalls
   1148 		printf("\tregister_t retval[2];\n") > rumpcalls
   1149 		printf("\tint error = 0;\n") > rumpcalls
   1150 		printf("\n\terror = rsys_syscall(SYS_pipe, ") > rumpcalls
   1151 		printf("NULL, 0, retval);\n") > rumpcalls
   1152 		printf("\tif (error) {\n") > rumpcalls
   1153 		printf("\t\trsys_seterrno(error);\n") > rumpcalls
   1154 		printf("\t} else {\n\t\tfd[0] = retval[0];\n") > rumpcalls
   1155 		printf("\t\tfd[1] = retval[1];\n\t}\n") > rumpcalls
   1156 		printf("\treturn error ? -1 : 0;\n}\n") > rumpcalls
   1157 		printf("#ifdef RUMP_KERNEL_IS_LIBC\n") > rumpcalls
   1158 		printf("__weak_alias(pipe,rump_sys_pipe);\n") > rumpcalls
   1159 		printf("__weak_alias(_pipe,rump_sys_pipe);\n") > rumpcalls
   1160 		printf("__strong_alias(_sys_pipe,rump_sys_pipe);\n") > rumpcalls
   1161 		printf("#endif\n") > rumpcalls
   1162 	}
   1163 
   1164 	# print default rump syscall interfaces
   1165 	# be sure to generate them in a deterministic order, not awk
   1166 	# hash order as would happen with a plain "for (var in funcseen)"
   1167 	numfuncseenvars = 0;
   1168 	for (var in funcseen) {
   1169 		funcseenvars[++numfuncseenvars] = var;
   1170 	}
   1171 	sort(funcseenvars, numfuncseenvars)
   1172 	for (i = 1; i <= numfuncseenvars; i++) {
   1173 		var = funcseenvars[i];
   1174 		printf("#ifndef RUMP_SYS_RENAME_%s\n", \
   1175 		    toupper(var)) > rumpcallshdr
   1176 		printf("#define RUMP_SYS_RENAME_%s rump___sysimpl_%s\n", \
   1177 		    toupper(var), funcseen[var]) > rumpcallshdr
   1178 		printf("#endif\n\n") > rumpcallshdr
   1179 	}
   1180 
   1181 	maxsyscall = syscall
   1182 
   1183 	if (nsysent) {
   1184 		if (syscall > nsysent) {
   1185 			printf("%s: line %d: too many syscalls [%d > %d]\n", infile, NR, syscall, nsysent)
   1186 			exit 1
   1187 		}
   1188 		while (syscall < nsysent) {
   1189 			print fillerpsysent(syscall, "", sys_nosys, "filler") > sysent
   1190 			print fillerpsysent(syscall, rumpnoflags, rumpnosys, "filler") > rumpsysent
   1191 			printf("\t/* %3d */\t\"# filler\",\n", syscall) \
   1192 			    > sysnamesbottom
   1193 			printf("\t/* %3d */\tNULL, /* filler */\n", syscall) \
   1194 			    > sysnamesfriendly
   1195 			syscall++
   1196 		}
   1197 	}
   1198 	printf("};\n") > sysent
   1199 	printf("\nconst uint32_t %s_nomodbits[] = {\n", switchname) > sysent
   1200 	printf("};\n") > rumpsysent
   1201 	printf("\nconst uint32_t rump_sysent_nomodbits[] = {\n") > rumpsysent
   1202 	for (i = 0; i < syscall / 32; i++) {
   1203 		printf("\t0x%08x,\t/* syscalls %3d-%3d */\n",
   1204 			nomodbits[i], i * 32, i * 32 + 31) > sysent
   1205 		printf("\t0x%08x,\t/* syscalls %3d-%3d */\n",
   1206 			nomodbits[i], i * 32, i * 32 + 31) > rumpsysent
   1207 	}
   1208 	printf("};\n") > sysent
   1209 	printf("};\n") > rumpsysent
   1210 	printf("CTASSERT(__arraycount(rump_sysent) == SYS_NSYSENT);\n") > rumpsysent
   1211 	printf("__strong_alias(rumpns_sysent,rump_sysent);\n") > rumpsysent
   1212 	printf("#endif /* RUMP_CLIENT */\n") > rumpsysent
   1213 	if (haverumpcalls)
   1214 		printf("#endif /* !RUMP_CLIENT */\n") > sysprotos
   1215 	printf("};\n") > sysnamesbottom
   1216 	printf("};\n") > sysnamesfriendly
   1217 	printf("#define\t%sMAXSYSCALL\t%d\n", constprefix, maxsyscall) > sysnumhdr
   1218 	if (nsysent)
   1219 		printf("#define\t%sNSYSENT\t%d\n", constprefix, nsysent) > sysnumhdr
   1220 	printf "\tdefault:\n\t\t*n_args = 0;\n\t\tbreak;\n\t};\n}\n" > systrace
   1221 	printf "\tdefault:\n\t\tbreak;\n\t};\n\tif (p != NULL)\n\t\tstrlcpy(desc, p, descsz);\n}\n" > systracetmp
   1222 	printf "\tdefault:\n\t\tbreak;\n\t};\n\tif (p != NULL)\n\t\tstrlcpy(desc, p, descsz);\n}\n" > systraceret
   1223 } '
   1224 
   1225 cat $sysprotos >> $sysarghdr
   1226 echo "#endif /* _${constprefix}SYSCALL_H_ */" >> $sysnumhdr
   1227 echo "#endif /* _${constprefix}SYSCALLARGS_H_ */" >> $sysarghdr
   1228 printf "\t    { 0, NULL }\n" >> $sysautoloadbottom
   1229 echo "};" >> $sysautoloadbottom
   1230 printf "\n#endif /* _RUMP_RUMP_SYSCALLS_H_ */\n" >> $rumpprotos
   1231 cat $sysdcl $sysent > $syssw
   1232 cat $sysnamesbottom >> $sysnames
   1233 cat $sysnamesfriendly >> $sysnames
   1234 cat $rumpsysent >> $rumpcalls
   1235 cat $sysautoloadbottom >> $sysautoload
   1236 
   1237 touch $rumptypes
   1238 cat $rumptypes >> $rumpcallshdr
   1239 echo >> $rumpcallshdr
   1240 cat $rumpprotos >> $rumpcallshdr
   1241 
   1242 #chmod 444 $sysnames $sysnumhdr $syssw
   1243 
   1244 cat $systracetmp >> $systrace
   1245 cat $systraceret >> $systrace
   1246 
   1247 echo Generated following files:
   1248 echo $sysarghdr $sysnumhdr $syssw $sysnames $sysautoload $systrace $rumpcalls $rumpcallshdr $rumpsysmap
   1249