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