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