Home | History | Annotate | Line # | Download | only in kern
makesyscalls.sh revision 1.108
      1 #! /bin/sh -
      2 #	$NetBSD: makesyscalls.sh,v 1.108 2010/12/30 20:09:53 pooka Exp $
      3 #
      4 # Copyright (c) 1994, 1996, 2000 Christopher G. Demetriou
      5 # All rights reserved.
      6 #
      7 # Redistribution and use in source and binary forms, with or without
      8 # modification, are permitted provided that the following conditions
      9 # are met:
     10 # 1. Redistributions of source code must retain the above copyright
     11 #    notice, this list of conditions and the following disclaimer.
     12 # 2. Redistributions in binary form must reproduce the above copyright
     13 #    notice, this list of conditions and the following disclaimer in the
     14 #    documentation and/or other materials provided with the distribution.
     15 # 3. All advertising materials mentioning features or use of this software
     16 #    must display the following acknowledgement:
     17 #      This product includes software developed for the NetBSD Project
     18 #      by Christopher G. Demetriou.
     19 # 4. The name of the author may not be used to endorse or promote products
     20 #    derived from this software without specific prior written permission
     21 #
     22 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32 
     33 #	@(#)makesyscalls.sh	8.1 (Berkeley) 6/10/93
     34 
     35 set -e
     36 
     37 case $# in
     38     2)	;;
     39     *)	echo "Usage: $0 config-file input-file" 1>&2
     40 	exit 1
     41 	;;
     42 esac
     43 
     44 # the config file sets the following variables:
     45 #	sysalign	check for alignment of off_t
     46 #	sysnames	the syscall names file
     47 #	sysnumhdr	the syscall numbers file
     48 #	syssw		the syscall switch 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 #	registertype	the type for register_t
     55 #	nsysent		the size of the sysent table
     56 #	sys_nosys	[optional] name of function called for unsupported
     57 #			syscalls, if not sys_nosys()
     58 #       maxsysargs	[optiona] the maximum number or arguments
     59 #
     60 # NOTE THAT THIS makesyscalls.sh DOES NOT SUPPORT 'SYSLIBCOMPAT'.
     61 
     62 # source the config file.
     63 sys_nosys="sys_nosys"	# default is sys_nosys(), if not specified otherwise
     64 maxsysargs=8		# default limit is 8 (32bit) arguments
     65 rumpcalls="/dev/null"
     66 rumpcallshdr="/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 
     77 trap "rm $sysdcl $sysprotos $sysent $sysnamesbottom $rumpsysent" 0
     78 
     79 # Awk program (must support nawk extensions)
     80 # Use "awk" at Berkeley, "nawk" or "gawk" elsewhere.
     81 awk=${AWK:-awk}
     82 
     83 # Does this awk have a "toupper" function?
     84 have_toupper=`$awk 'BEGIN { print toupper("true"); exit; }' 2>/dev/null`
     85 
     86 # If this awk does not define "toupper" then define our own.
     87 if [ "$have_toupper" = TRUE ] ; then
     88 	# Used awk (GNU awk or nawk) provides it
     89 	toupper=
     90 else
     91 	# Provide our own toupper()
     92 	toupper='
     93 function toupper(str) {
     94 	_toupper_cmd = "echo "str" |tr a-z A-Z"
     95 	_toupper_cmd | getline _toupper_str;
     96 	close(_toupper_cmd);
     97 	return _toupper_str;
     98 }'
     99 fi
    100 
    101 # before handing it off to awk, make a few adjustments:
    102 #	(1) insert spaces around {, }, (, ), *, and commas.
    103 #	(2) get rid of any and all dollar signs (so that rcs id use safe)
    104 #
    105 # The awk script will deal with blank lines and lines that
    106 # start with the comment character (';').
    107 
    108 sed -e '
    109 s/\$//g
    110 :join
    111 	/\\$/{a\
    112 
    113 	N
    114 	s/\\\n//
    115 	b join
    116 	}
    117 2,${
    118 	/^#/!s/\([{}()*,|]\)/ \1 /g
    119 }
    120 ' < $2 | $awk "
    121 $toupper
    122 BEGIN {
    123 	# Create a NetBSD tag that does not get expanded when checking
    124 	# this script out of CVS.  (This part of the awk script is in a
    125 	# shell double-quoted string, so the backslashes are eaten by
    126 	# the shell.)
    127 	tag = \"\$\" \"NetBSD\" \"\$\"
    128 
    129 	# to allow nested #if/#else/#endif sets
    130 	savedepth = 0
    131 	# to track already processed syscalls
    132 
    133 	sysnames = \"$sysnames\"
    134 	sysprotos = \"$sysprotos\"
    135 	sysnumhdr = \"$sysnumhdr\"
    136 	sysarghdr = \"$sysarghdr\"
    137 	sysarghdrextra = \"$sysarghdrextra\"
    138 	rumpcalls = \"$rumpcalls\"
    139 	rumpcallshdr = \"$rumpcallshdr\"
    140 	rumpsysent = \"$rumpsysent\"
    141 	switchname = \"$switchname\"
    142 	namesname = \"$namesname\"
    143 	constprefix = \"$constprefix\"
    144 	registertype = \"$registertype\"
    145 	sysalign=\"$sysalign\"
    146 	if (!registertype) {
    147 	    registertype = \"register_t\"
    148 	}
    149 	nsysent = \"$nsysent\"
    150 
    151 	sysdcl = \"$sysdcl\"
    152 	syscompat_pref = \"$syscompat_pref\"
    153 	sysent = \"$sysent\"
    154 	sysnamesbottom = \"$sysnamesbottom\"
    155 	sys_nosys = \"$sys_nosys\"
    156 	maxsysargs = \"$maxsysargs\"
    157 	infile = \"$2\"
    158 
    159 	compatopts = \"$compatopts\"
    160 	"'
    161 
    162 	printf "/* %s */\n\n", tag > sysdcl
    163 	printf "/*\n * System call switch table.\n *\n" > sysdcl
    164 	printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysdcl
    165 
    166 	ncompat = split(compatopts,compat)
    167 	for (i = 1; i <= ncompat; i++) {
    168 		compat_upper[i] = toupper(compat[i])
    169 
    170 		printf "\n#ifdef %s\n", compat_upper[i] > sysent
    171 		printf "#define	%s(func) __CONCAT(%s_,func)\n", compat[i], \
    172 		    compat[i] > sysent
    173 		printf "#else\n" > sysent
    174 		printf "#define	%s(func) %s\n", compat[i], sys_nosys > sysent
    175 		printf "#endif\n" > sysent
    176 	}
    177 
    178 	printf "\n#define\ts(type)\tsizeof(type)\n" > sysent
    179 	printf "#define\tn(type)\t(sizeof(type)/sizeof (%s))\n", registertype > sysent
    180 	printf "#define\tns(type)\tn(type), s(type)\n\n", registertype > sysent
    181 	printf "struct sysent %s[] = {\n",switchname > sysent
    182 
    183 	printf "/* %s */\n\n", tag > sysnames
    184 	printf "/*\n * System call names.\n *\n" > sysnames
    185 	printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysnames
    186 
    187 	printf "\n/*\n * System call prototypes.\n */\n\n" > sysprotos
    188 
    189 	printf "/* %s */\n\n", tag > sysnumhdr
    190 	printf "/*\n * System call numbers.\n *\n" > sysnumhdr
    191 	printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysnumhdr
    192 
    193 	printf "/* %s */\n\n", tag > sysarghdr
    194 	printf "/*\n * System call argument lists.\n *\n" > sysarghdr
    195 	printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysarghdr
    196 
    197 	printf "/* %s */\n\n", tag > rumpcalls
    198 	printf "/*\n * System call vector and marshalling for rump.\n *\n" > rumpcalls
    199 	printf " * DO NOT EDIT-- this file is automatically generated.\n" > rumpcalls
    200 
    201 	printf "/* %s */\n\n", tag > rumpcallshdr
    202 	printf "/*\n * System call protos in rump namespace.\n *\n" > rumpcallshdr
    203 	printf " * DO NOT EDIT-- this file is automatically generated.\n" > rumpcallshdr
    204 }
    205 NR == 1 {
    206 	sub(/ $/, "")
    207 	printf " * created from%s\n */\n\n", $0 > sysdcl
    208 	printf "#include <sys/cdefs.h>\n__KERNEL_RCSID(0, \"%s\");\n\n", tag > sysdcl
    209 
    210 	printf " * created from%s\n */\n\n", $0 > sysnames
    211 	printf "#include <sys/cdefs.h>\n__KERNEL_RCSID(0, \"%s\");\n\n", tag > sysnames
    212 
    213 	printf " * created from%s\n */\n\n", $0 > rumpcalls
    214 	printf "#include <sys/cdefs.h>\n__KERNEL_RCSID(0, \"%s\");\n\n", tag > rumpcalls
    215 
    216 	printf "#include <sys/param.h>\n" > rumpcalls
    217 	printf "#include <sys/fstypes.h>\n" > rumpcalls
    218 	printf "#include <sys/proc.h>\n" > rumpcalls
    219 	printf "#include <sys/syscall.h>\n" > rumpcalls
    220 	printf "#include <sys/syscallargs.h>\n\n" > rumpcalls
    221 	printf "#ifdef RUMP_CLIENT\n" > rumpcalls
    222 	printf "#include <errno.h>\n" > rumpcalls
    223 	printf "#include <rump/rumpclient.h>\n\n" > rumpcalls
    224 	printf "#define rsys_syscall(num, data, dlen, retval)\t\\\n" > rumpcalls
    225 	printf "    rumpclient_syscall(num, data, dlen, retval)\n" > rumpcalls
    226 	printf "#define rsys_seterrno(error) errno = error\n" > rumpcalls
    227 	printf "#define rsys_alias(a,b)\n#else\n" > rumpcalls
    228 	printf "#include <sys/syscallvar.h>\n\n" > rumpcalls
    229 	printf "#include <rump/rumpuser.h>\n" > rumpcalls
    230 	printf "#include \"rump_private.h\"\n\n" > rumpcalls
    231 	printf "static int\nrsys_syscall" > rumpcalls
    232 	printf "(int num, void *data, size_t dlen, register_t *retval)" > rumpcalls
    233 	printf "\n{\n\tstruct sysent *callp = rump_sysent + num;\n" > rumpcalls
    234 	printf "\tint rv;\n" > rumpcalls
    235 	printf "\n\tKASSERT(num > 0 && num < SYS_NSYSENT);\n\n" > rumpcalls
    236 	printf "\trump_schedule();\n" > rumpcalls
    237 	printf "\trv = sy_call(callp, curlwp, data, retval);\n" > rumpcalls
    238 	printf "\trump_unschedule();\n\n\treturn rv;\n}\n\n" > rumpcalls
    239 	printf "#define rsys_seterrno(error) rumpuser_seterrno(error)\n" > rumpcalls
    240 	printf "#define rsys_alias(a,b) __weak_alias(a,b);\n#endif\n\n" > rumpcalls
    241 
    242 	printf "#if\tBYTE_ORDER == BIG_ENDIAN\n" > rumpcalls
    243 	printf "#define SPARG(p,k)\t((p)->k.be.datum)\n" > rumpcalls
    244 	printf "#else /* LITTLE_ENDIAN, I hope dearly */\n" > rumpcalls
    245 	printf "#define SPARG(p,k)\t((p)->k.le.datum)\n" > rumpcalls
    246 	printf "#endif\n\n" > rumpcalls
    247 	printf "#ifndef RUMP_CLIENT\n" > rumpcalls
    248 	printf "int rump_enosys(void);\n" > rumpcalls
    249 	printf "int\nrump_enosys()\n{\n\n\treturn ENOSYS;\n}\n" > rumpcalls
    250 	printf "#endif\n" > rumpcalls
    251 
    252 	printf "\n#ifndef RUMP_CLIENT\n" > rumpsysent
    253 	printf "#define\ts(type)\tsizeof(type)\n" > rumpsysent
    254 	printf "#define\tn(type)\t(sizeof(type)/sizeof (%s))\n", registertype > rumpsysent
    255 	printf "#define\tns(type)\tn(type), s(type)\n\n", registertype > rumpsysent
    256 	printf "struct sysent rump_sysent[] = {\n" > rumpsysent
    257 
    258 	# System call names are included by userland (kdump(1)), so
    259 	# hide the include files from it.
    260 	printf "#if defined(_KERNEL_OPT)\n" > sysnames
    261 
    262 	printf "#endif /* _KERNEL_OPT */\n\n" > sysnamesbottom
    263 	printf "const char *const %s[] = {\n",namesname > sysnamesbottom
    264 
    265 	printf " * created from%s\n */\n\n", $0 > sysnumhdr
    266 	printf "#ifndef _" constprefix "SYSCALL_H_\n" > sysnumhdr
    267 	printf "#define	_" constprefix "SYSCALL_H_\n\n" > sysnumhdr
    268 
    269 	printf " * created from%s\n */\n\n", $0 > sysarghdr
    270 	printf "#ifndef _" constprefix "SYSCALLARGS_H_\n" > sysarghdr
    271 	printf "#define	_" constprefix "SYSCALLARGS_H_\n\n" > sysarghdr
    272 
    273 	printf " * created from%s\n */\n\n", $0 > rumpcallshdr
    274 	printf "#ifndef _RUMP_RUMP_SYSCALLS_H_\n" > rumpcallshdr
    275 	printf "#define _RUMP_RUMP_SYSCALLS_H_\n\n" > rumpcallshdr
    276 	printf "#ifdef _KERNEL\n" > rumpcallshdr
    277 	printf "#error Interface not supported inside kernel\n" > rumpcallshdr
    278 	printf "#endif /* _KERNEL */\n\n" > rumpcallshdr
    279 	printf "#include <sys/types.h>\n" > rumpcallshdr
    280 	printf "#include <sys/select.h>\n\n" > rumpcallshdr
    281 	printf "#include <signal.h>\n\n" > rumpcallshdr
    282 
    283 	printf "%s", sysarghdrextra > sysarghdr
    284 	# Write max number of system call arguments to both headers
    285 	printf("#define\t%sMAXSYSARGS\t%d\n\n", constprefix, maxsysargs) \
    286 		> sysnumhdr
    287 	printf("#define\t%sMAXSYSARGS\t%d\n\n", constprefix, maxsysargs) \
    288 		> sysarghdr
    289 	printf "#undef\tsyscallarg\n" > sysarghdr
    290 	printf "#define\tsyscallarg(x)\t\t\t\t\t\t\t\\\n" > sysarghdr
    291 	printf "\tunion {\t\t\t\t\t\t\t\t\\\n" > sysarghdr
    292 	printf "\t\t%s pad;\t\t\t\t\t\t\\\n", registertype > sysarghdr
    293 	printf "\t\tstruct { x datum; } le;\t\t\t\t\t\\\n" > sysarghdr
    294 	printf "\t\tstruct { /* LINTED zero array dimension */\t\t\\\n" \
    295 		> sysarghdr
    296 	printf "\t\t\tint8_t pad[  /* CONSTCOND */\t\t\t\\\n" > sysarghdr
    297 	printf "\t\t\t\t(sizeof (%s) < sizeof (x))\t\\\n", \
    298 		registertype > sysarghdr
    299 	printf "\t\t\t\t? 0\t\t\t\t\t\\\n" > sysarghdr
    300 	printf "\t\t\t\t: sizeof (%s) - sizeof (x)];\t\\\n", \
    301 		registertype > sysarghdr
    302 	printf "\t\t\tx datum;\t\t\t\t\t\\\n" > sysarghdr
    303 	printf "\t\t} be;\t\t\t\t\t\t\t\\\n" > sysarghdr
    304 	printf "\t}\n" > sysarghdr
    305 	printf("\n#undef check_syscall_args\n") >sysarghdr
    306 	printf("#define check_syscall_args(call) /*LINTED*/ \\\n" \
    307 		"\ttypedef char call##_check_args" \
    308 		    "[sizeof (struct call##_args) \\\n" \
    309 		"\t\t<= %sMAXSYSARGS * sizeof (%s) ? 1 : -1];\n", \
    310 		constprefix, registertype) >sysarghdr
    311 	next
    312 }
    313 NF == 0 || $1 ~ /^;/ {
    314 	next
    315 }
    316 $0 ~ /^%%$/ {
    317 	intable = 1
    318 	next
    319 }
    320 $1 ~ /^#[ 	]*include/ {
    321 	print > sysdcl
    322 	print > sysnames
    323 	next
    324 }
    325 $1 ~ /^#/ && !intable {
    326 	print > sysdcl
    327 	print > sysnames
    328 	next
    329 }
    330 $1 ~ /^#/ && intable {
    331 	if ($1 ~ /^#[ 	]*if/) {
    332 		savedepth++
    333 		savesyscall[savedepth] = syscall
    334 	}
    335 	if ($1 ~ /^#[ 	]*else/) {
    336 		if (savedepth <= 0) {
    337 			printf("%s: line %d: unbalanced #else\n", \
    338 			    infile, NR)
    339 			exit 1
    340 		}
    341 		syscall = savesyscall[savedepth]
    342 	}
    343 	if ($1 ~ /^#[       ]*endif/) {
    344 		if (savedepth <= 0) {
    345 			printf("%s: line %d: unbalanced #endif\n", \
    346 			    infile, NR)
    347 			exit 1
    348 		}
    349 		savedepth--
    350 	}
    351 	print > sysent
    352 	print > sysarghdr
    353 	print > sysnumhdr
    354 	print > sysprotos
    355 	print > sysnamesbottom
    356 
    357 	# XXX: technically we do not want to have conditionals in rump,
    358 	# but it is easier to just let the cpp handle them than try to
    359 	# figure out what we want here in this script
    360 	print > rumpsysent
    361 	next
    362 }
    363 syscall != $1 {
    364 	printf "%s: line %d: syscall number out of sync at %d\n", \
    365 	   infile, NR, syscall
    366 	printf "line is:\n"
    367 	print
    368 	exit 1
    369 }
    370 function parserr(was, wanted) {
    371 	printf "%s: line %d: unexpected %s (expected <%s>)\n", \
    372 	    infile, NR, was, wanted
    373 	printf "line is:\n"
    374 	print
    375 	exit 1
    376 }
    377 function parseline() {
    378 	f=3			# toss number and type
    379 	if ($2 == "INDIR")
    380 		sycall_flags="SYCALL_INDIRECT"
    381 	else
    382 		sycall_flags="0"
    383 	if ($NF != "}") {
    384 		funcalias=$NF
    385 		end=NF-1
    386 	} else {
    387 		funcalias=""
    388 		end=NF
    389 	}
    390 	if ($f == "INDIR") {		# allow for "NOARG INDIR"
    391 		sycall_flags = "SYCALL_INDIRECT | " sycall_flags
    392 		f++
    393 	}
    394 	if ($f == "MODULAR") {		# registered at runtime
    395 		modular = 1
    396 		f++
    397 	} else {
    398 		modular =  0;
    399 	}
    400 	if ($f == "RUMP") {
    401 		rumpable = 1
    402 		f++
    403 	} else {
    404 		rumpable = 0
    405 	}
    406 	if ($f ~ /^[a-z0-9_]*$/) {	# allow syscall alias
    407 		funcalias=$f
    408 		f++
    409 	}
    410 	if ($f != "{")
    411 		parserr($f, "{")
    412 	f++
    413 	if ($end != "}")
    414 		parserr($end, "}")
    415 	end--
    416 	if ($end != ";")
    417 		parserr($end, ";")
    418 	end--
    419 	if ($end != ")")
    420 		parserr($end, ")")
    421 	end--
    422 
    423 	returntype = oldf = "";
    424 	do {
    425 		if (returntype != "" && oldf != "*")
    426 			returntype = returntype" ";
    427 		returntype = returntype$f;
    428 		oldf = $f;
    429 		f++
    430 	} while ($f != "|" && f < (end-1))
    431 	if (f == (end - 1)) {
    432 		parserr($f, "function argument definition (maybe \"|\"?)");
    433 	}
    434 	f++
    435 
    436 	fprefix=$f
    437 	f++
    438 	if ($f != "|") {
    439 		parserr($f, "function compat delimiter (maybe \"|\"?)");
    440 	}
    441 	f++
    442 
    443 	fcompat=""
    444 	if ($f != "|") {
    445 		fcompat=$f
    446 		f++
    447 	}
    448 
    449 	if ($f != "|") {
    450 		parserr($f, "function name delimiter (maybe \"|\"?)");
    451 	}
    452 	f++
    453 	fbase=$f
    454 
    455 	# pipe is special in how to returns its values.
    456 	# So just generate it manually if present.
    457 	if (rumpable == 1 && fbase == "pipe") {
    458 		rumpable = 0;
    459 		rumphaspipe = 1;
    460 	}
    461 
    462 	funcstdname=fprefix "_" fbase
    463 	if (fcompat != "") {
    464 		funcname=fprefix "___" fbase "" fcompat
    465 		wantrename=1
    466 	} else {
    467 		funcname=funcstdname
    468 		wantrename=0
    469 	}
    470 	if (returntype == "quad_t" || returntype == "off_t") {
    471 		if (sycall_flags == "0")
    472 			sycall_flags = "SYCALL_RET_64";
    473 		else
    474 			sycall_flags = "SYCALL_RET_64 | " sycall_flags;
    475 	}
    476 
    477 	if (funcalias == "") {
    478 		funcalias=funcname
    479 		sub(/^([^_]+_)*sys_/, "", funcalias)
    480 	}
    481 	f++
    482 
    483 	if ($f != "(")
    484 		parserr($f, "(")
    485 	f++
    486 
    487 	argc=0;
    488 	argalign=0;
    489 	if (f == end) {
    490 		if ($f != "void")
    491 			parserr($f, "argument definition")
    492 		isvarargs = 0;
    493 		varargc = 0;
    494 		argtype[0]="void";
    495 		return
    496 	}
    497 
    498 	# some system calls (open() and fcntl()) can accept a variable
    499 	# number of arguments.  If syscalls accept a variable number of
    500 	# arguments, they must still have arguments specified for
    501 	# the remaining argument "positions," because of the way the
    502 	# kernel system call argument handling works.
    503 	#
    504 	# Indirect system calls, e.g. syscall(), are exceptions to this
    505 	# rule, since they are handled entirely by machine-dependent code
    506 	# and do not need argument structures built.
    507 
    508 	isvarargs = 0;
    509 	args64 = 0;
    510 	while (f <= end) {
    511 		if ($f == "...") {
    512 			f++;
    513 			isvarargs = 1;
    514 			varargc = argc;
    515 			continue;
    516 		}
    517 		argc++
    518 		argtype[argc]=""
    519 		oldf=""
    520 		while (f < end && $(f+1) != ",") {
    521 			if (argtype[argc] != "" && oldf != "*")
    522 				argtype[argc] = argtype[argc]" ";
    523 			argtype[argc] = argtype[argc]$f;
    524 			oldf = $f;
    525 			f++
    526 		}
    527 		if (argtype[argc] == "")
    528 			parserr($f, "argument definition")
    529 		if (argtype[argc] == "off_t"  \
    530 		  || argtype[argc] == "dev_t" \
    531 		  || argtype[argc] == "time_t") {
    532 			if ((argalign % 2) != 0 && sysalign &&
    533 			    funcname != "sys_posix_fadvise") # XXX for now
    534 				parserr($f, "a padding argument")
    535 		} else {
    536 			argalign++;
    537 		}
    538 		if (argtype[argc] == "quad_t" || argtype[argc] == "off_t" \
    539 		  || argtype[argc] == "dev_t" || argtype[argc] == "time_t") {
    540 			if (sycall_flags == "0")
    541 				sycall_flags = "SYCALL_ARG"argc-1"_64";
    542 			else
    543 				sycall_flags = "SYCALL_ARG"argc-1"_64 | " sycall_flags;
    544 			args64++;
    545 		}
    546 		argname[argc]=$f;
    547 		f += 2;			# skip name, and any comma
    548 	}
    549 	if (args64 > 0)
    550 		sycall_flags = "SYCALL_NARGS64_VAL("args64") | " sycall_flags;
    551 	# must see another argument after varargs notice.
    552 	if (isvarargs) {
    553 		if (argc == varargc)
    554 			parserr($f, "argument definition")
    555 	} else
    556 		varargc = argc;
    557 }
    558 
    559 function printproto(wrap) {
    560 	printf("/* syscall: \"%s%s\" ret: \"%s\" args:", wrap, funcalias,
    561 	    returntype) > sysnumhdr
    562 	for (i = 1; i <= varargc; i++)
    563 		printf(" \"%s\"", argtype[i]) > sysnumhdr
    564 	if (isvarargs)
    565 		printf(" \"...\"") > sysnumhdr
    566 	printf(" */\n") > sysnumhdr
    567 	printf("#define\t%s%s%s\t%d\n\n", constprefix, wrap, funcalias,
    568 	    syscall) > sysnumhdr
    569 
    570 	# rumpalooza
    571 	if (!rumpable)
    572 		return
    573 
    574 	if (wantrename)
    575 		printf("%s rump_%s(", returntype, funcstdname) > rumpcallshdr
    576 	else
    577 		printf("%s rump_sys_%s(", returntype, funcalias) > rumpcallshdr
    578 	for (i = 1; i < varargc; i++)
    579 		if (argname[i] != "PAD")
    580 			printf("%s, ", argtype[i]) > rumpcallshdr
    581 	if (isvarargs)
    582 		printf("%s, ...)", argtype[varargc]) > rumpcallshdr
    583 	else
    584 		printf("%s)", argtype[argc]) > rumpcallshdr
    585 	if (wantrename)
    586 		printf(" __RENAME(rump_%s)", funcname) > rumpcallshdr
    587 	printf(";\n") > rumpcallshdr
    588 }
    589 
    590 function printrumpsysent(insysent, compatwrap) {
    591 	if (!insysent) {
    592 		eno[0] = "rump_enosys"
    593 		eno[1] = "sys_nomodule"
    594 		flags[0] = "SYCALL_NOSYS"
    595 		flags[1] = "0"
    596 		printf("\t{ 0, 0, %s,\n\t    (sy_call_t *)%s }, \t"	\
    597 		    "/* %d = %s */\n",					\
    598 		    flags[modular], eno[modular], syscall, funcalias)	\
    599 		    > rumpsysent
    600 		return
    601 	}
    602 
    603 	printf("\t{ ") > rumpsysent
    604 	if (argc == 0) {
    605 		printf("0, 0, ") > rumpsysent
    606 	} else {
    607 		printf("ns(struct sys_%s%s_args), ", compatwrap_, funcalias) > rumpsysent
    608 	}
    609 	printf("0,\n\t    %s },", wfn) > rumpsysent
    610 	for (i = 0; i < (33 - length(wfn)) / 8; i++)
    611 		printf("\t") > rumpsysent
    612 	printf("/* %d = %s%s */\n", syscall, compatwrap_, funcalias) > rumpsysent
    613 }
    614 
    615 function putent(type, compatwrap) {
    616 	# output syscall declaration for switch table.
    617 	if (compatwrap == "")
    618 		compatwrap_ = ""
    619 	else
    620 		compatwrap_ = compatwrap "_"
    621 	if (argc == 0)
    622 		arg_type = "void";
    623 	else {
    624 		arg_type = "struct " compatwrap_ funcname "_args";
    625 	}
    626 	proto = "int\t" compatwrap_ funcname "(struct lwp *, const " \
    627 	    arg_type " *, register_t *);\n"
    628 	if (sysmap[proto] != 1) {
    629 		sysmap[proto] = 1;
    630 		print proto > sysprotos;
    631 	}
    632 
    633 	# output syscall switch entry
    634 	printf("\t{ ") > sysent
    635 	if (argc == 0) {
    636 		printf("0, 0, ") > sysent
    637 	} else {
    638 		printf("ns(struct %s%s_args), ", compatwrap_, funcname) > sysent
    639 	}
    640 	if (modular) 
    641 		wfn = "(sy_call_t *)sys_nomodule";
    642 	else if (compatwrap == "")
    643 		wfn = "(sy_call_t *)" funcname;
    644 	else
    645 		wfn = "(sy_call_t *)" compatwrap "(" funcname ")";
    646 	printf("%s,\n\t    %s },", sycall_flags, wfn) > sysent
    647 	for (i = 0; i < (33 - length(wfn)) / 8; i++)
    648 		printf("\t") > sysent
    649 	printf("/* %d = %s%s */\n", syscall, compatwrap_, funcalias) > sysent
    650 
    651 	# output syscall name for names table
    652 	printf("\t/* %3d */\t\"%s%s\",\n", syscall, compatwrap_, funcalias) \
    653 	    > sysnamesbottom
    654 
    655 	# output syscall number of header, if appropriate
    656 	if (type == "STD" || type == "NOARGS" || type == "INDIR") {
    657 		# output a prototype, to be used to generate lint stubs in
    658 		# libc.
    659 		printproto("")
    660 	} else if (type == "COMPAT") {
    661 		# Just define the syscall number with a comment.  These
    662 		# may be used by compatibility stubs in libc.
    663 		printproto(compatwrap_)
    664 	}
    665 
    666 	# output syscall argument structure, if it has arguments
    667 	if (argc != 0) {
    668 		printf("\nstruct %s%s_args", compatwrap_, funcname) > sysarghdr
    669 		if (type != "NOARGS") {
    670 			print " {" >sysarghdr;
    671 			for (i = 1; i <= argc; i++)
    672 				printf("\tsyscallarg(%s) %s;\n", argtype[i],
    673 				    argname[i]) > sysarghdr
    674 			printf "}" >sysarghdr;
    675 		}
    676 		printf(";\n") > sysarghdr
    677 		if (type != "NOARGS" && type != "INDIR") {
    678 			printf("check_syscall_args(%s%s)\n", compatwrap_,
    679 			    funcname) >sysarghdr
    680 		}
    681 	}
    682 
    683 	if (!rumpable) {
    684 		if (funcname == "sys_pipe" && rumphaspipe == 1)
    685 			insysent = 1
    686 		else
    687 			insysent = 0
    688 	} else {
    689 		insysent = 1
    690 	}
    691 	printrumpsysent(insysent, compatwrap)
    692 
    693 	# output rump marshalling code if necessary
    694 	if (!rumpable) {
    695 		return
    696 	}
    697 
    698 	# need a local prototype, we export the re-re-named one in .h
    699 	printf("\n%s rump_sys_%s(", returntype, funcalias) > rumpcalls
    700 	for (i = 1; i < argc; i++) {
    701 		if (argname[i] != "PAD")
    702 			printf("%s, ", argtype[i]) > rumpcalls
    703 	}
    704 	printf("%s);", argtype[argc]) > rumpcalls
    705 
    706 	printf("\n%s\nrump_sys_%s(", returntype, funcalias) > rumpcalls
    707 	for (i = 1; i < argc; i++) {
    708 		if (argname[i] != "PAD")
    709 			printf("%s %s, ", argtype[i], argname[i]) > rumpcalls
    710 	}
    711 	printf("%s %s)\n", argtype[argc], argname[argc]) > rumpcalls
    712 	printf("{\n\tregister_t rval[2] = {0, 0};\n\tint error = 0;\n") \
    713 	    > rumpcalls
    714 
    715 	argarg = "NULL"
    716 	argsize = 0;
    717 	if (argc) {
    718 		argarg = "&callarg"
    719 		argsize = "sizeof(callarg)"
    720 		printf("\tstruct %s%s_args callarg;\n\n",compatwrap_,funcname) \
    721 		    > rumpcalls
    722 		for (i = 1; i <= argc; i++) {
    723 			if (argname[i] == "PAD") {
    724 				printf("\tSPARG(&callarg, %s) = 0;\n", \
    725 				    argname[i]) > rumpcalls
    726 			} else {
    727 				printf("\tSPARG(&callarg, %s) = %s;\n", \
    728 				    argname[i], argname[i]) > rumpcalls
    729 			}
    730 		}
    731 		printf("\n") > rumpcalls
    732 	} else {
    733 		printf("\n") > rumpcalls
    734 	}
    735 	printf("\terror = rsys_syscall(%s%s, " \
    736 	    "%s, %s, rval);\n", constprefix, funcalias, \
    737 	    argarg, argsize) > rumpcalls
    738 	printf("\tif (error) {\n\t\trval[0] = -1;\n") > rumpcalls
    739 	if (returntype != "void") {
    740 		printf("\t\trsys_seterrno(error);\n\t}\n") > rumpcalls
    741 		printf("\treturn rval[0];\n") > rumpcalls
    742 	} else {
    743 		printf("\t}\n") > rumpcalls
    744 	}
    745 	printf("}\n") > rumpcalls
    746 	printf("rsys_alias(%s,rump_enosys)\n", funcname) > rumpcalls
    747 
    748 }
    749 $2 == "STD" || $2 == "NODEF" || $2 == "NOARGS" || $2 == "INDIR" {
    750 	parseline()
    751 	putent($2, "")
    752 	syscall++
    753 	next
    754 }
    755 $2 == "OBSOL" || $2 == "UNIMPL" || $2 == "EXCL" || $2 == "IGNORED" {
    756 	if ($2 == "OBSOL")
    757 		comment="obsolete"
    758 	else if ($2 == "EXCL")
    759 		comment="excluded"
    760 	else if ($2 == "IGNORED")
    761 		comment="ignored"
    762 	else
    763 		comment="unimplemented"
    764 	for (i = 3; i <= NF; i++)
    765 		comment=comment " " $i
    766 
    767 	if ($2 == "IGNORED")
    768 		sys_stub = "(sy_call_t *)nullop";
    769 	else
    770 		sys_stub = sys_nosys;
    771 
    772 	printf("\t{ 0, 0, 0,\n\t    %s },\t\t\t/* %d = %s */\n", \
    773 	    sys_stub, syscall, comment) > sysent
    774 	printf("\t{ 0, 0, SYCALL_NOSYS,\n\t    %s },\t\t/* %d = %s */\n", \
    775 	    "(sy_call_t *)rump_enosys", syscall, comment) > rumpsysent
    776 	printf("\t/* %3d */\t\"#%d (%s)\",\n", syscall, syscall, comment) \
    777 	    > sysnamesbottom
    778 	if ($2 != "UNIMPL")
    779 		printf("\t\t\t\t/* %d is %s */\n", syscall, comment) > sysnumhdr
    780 	syscall++
    781 	next
    782 }
    783 {
    784 	for (i = 1; i <= ncompat; i++) {
    785 		if ($2 == compat_upper[i]) {
    786 			parseline();
    787 			putent("COMPAT", compat[i])
    788 			syscall++
    789 			next
    790 		}
    791 	}
    792 	printf("%s: line %d: unrecognized keyword %s\n", infile, NR, $2)
    793 	exit 1
    794 }
    795 END {
    796 	# output pipe() syscall with its special rval[2] handling
    797 	if (rumphaspipe) {
    798 		printf("int rump_sys_pipe(int *);\n") > rumpcallshdr
    799 		printf("\nint rump_sys_pipe(int *);\n") > rumpcalls
    800 		printf("int\nrump_sys_pipe(int *fd)\n{\n") > rumpcalls
    801 		printf("\tregister_t rval[2] = {0, 0};\n") > rumpcalls
    802 		printf("\tint error = 0;\n") > rumpcalls
    803 		printf("\n\terror = rsys_syscall(SYS_pipe, ") > rumpcalls
    804 		printf("NULL, 0, rval);\n") > rumpcalls
    805 		printf("\tif (error) {\n") > rumpcalls
    806 		printf("\t\trsys_seterrno(error);\n") > rumpcalls
    807 		printf("\t} else {\n\t\tfd[0] = rval[0];\n") > rumpcalls
    808 		printf("\t\tfd[1] = rval[1];\n\t}\n") > rumpcalls
    809 		printf("\treturn error ? -1 : 0;\n}\n") > rumpcalls
    810 	}
    811 
    812 	maxsyscall = syscall
    813 	if (nsysent) {
    814 		if (syscall > nsysent) {
    815 			printf("%s: line %d: too many syscalls [%d > %d]\n", infile, NR, syscall, nsysent)
    816 			exit 1
    817 		}
    818 		while (syscall < nsysent) {
    819 			printf("\t{ 0, 0, 0,\n\t    %s },\t\t\t/* %d = filler */\n", \
    820 			    sys_nosys, syscall) > sysent
    821 			printf("\t{ 0, 0, SYCALL_NOSYS,\n\t    %s },\t\t/* %d = filler */\n", \
    822 			    "(sy_call_t *)rump_enosys", syscall) > rumpsysent
    823 			printf("\t/* %3d */\t\"# filler\",\n", syscall) \
    824 			    > sysnamesbottom
    825 			syscall++
    826 		}
    827 	}
    828 	printf("};\n") > sysent
    829 	printf("};\n") > rumpsysent
    830 	printf("CTASSERT(__arraycount(rump_sysent) == SYS_NSYSENT);\n") > rumpsysent
    831 	printf("#endif /* RUMP_CLIENT */\n") > rumpsysent
    832 	printf("};\n") > sysnamesbottom
    833 	printf("#define\t%sMAXSYSCALL\t%d\n", constprefix, maxsyscall) > sysnumhdr
    834 	if (nsysent)
    835 		printf("#define\t%sNSYSENT\t%d\n", constprefix, nsysent) > sysnumhdr
    836 } '
    837 
    838 cat $sysprotos >> $sysarghdr
    839 echo "#endif /* _${constprefix}SYSCALL_H_ */" >> $sysnumhdr
    840 echo "#endif /* _${constprefix}SYSCALLARGS_H_ */" >> $sysarghdr
    841 printf "\n#include <rump/rump_syscalls_compat.h>\n" >> $rumpcallshdr
    842 printf "\n#endif /* _RUMP_RUMP_SYSCALLS_H_ */\n" >> $rumpcallshdr
    843 cat $sysdcl $sysent > $syssw
    844 cat $sysnamesbottom >> $sysnames
    845 cat $rumpsysent >> $rumpcalls
    846 
    847 #chmod 444 $sysnames $sysnumhdr $syssw
    848