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