Home | History | Annotate | Line # | Download | only in kern
makesyscalls.sh revision 1.69.2.5
      1 #! /bin/sh -
      2 #	$NetBSD: makesyscalls.sh,v 1.69.2.5 2010/10/09 03:32:31 yamt 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 	printf "#include <sys/types.h>\n" > rumpcalls
    216 	printf "#include <sys/param.h>\n" > rumpcalls
    217 	printf "#include <sys/proc.h>\n" > rumpcalls
    218 	printf "#include <sys/syscall.h>\n" > rumpcalls
    219 	printf "#include <sys/syscallargs.h>\n" > rumpcalls
    220 	printf "#include <rump/rumpuser.h>\n" > rumpcalls
    221 	printf "#include \"rump_private.h\"\n\n" > rumpcalls
    222 	printf "#if\tBYTE_ORDER == BIG_ENDIAN\n" > rumpcalls
    223 	printf "#define SPARG(p,k)\t((p)->k.be.datum)\n" > rumpcalls
    224 	printf "#else /* LITTLE_ENDIAN, I hope dearly */\n" > rumpcalls
    225 	printf "#define SPARG(p,k)\t((p)->k.le.datum)\n" > rumpcalls
    226 	printf "#endif\n\n" > rumpcalls
    227 	printf "int rump_enosys(void);\n" > rumpcalls
    228 	printf "int\nrump_enosys()\n{\n\n\treturn ENOSYS;\n}\n" > rumpcalls
    229 
    230 	printf "\n#define\ts(type)\tsizeof(type)\n" > rumpsysent
    231 	printf "#define\tn(type)\t(sizeof(type)/sizeof (%s))\n", registertype > rumpsysent
    232 	printf "#define\tns(type)\tn(type), s(type)\n\n", registertype > rumpsysent
    233 	printf "struct sysent rump_sysent[] = {\n" > rumpsysent
    234 
    235 	# System call names are included by userland (kdump(1)), so
    236 	# hide the include files from it.
    237 	printf "#if defined(_KERNEL_OPT)\n" > sysnames
    238 
    239 	printf "#endif /* _KERNEL_OPT */\n\n" > sysnamesbottom
    240 	printf "const char *const %s[] = {\n",namesname > sysnamesbottom
    241 
    242 	printf " * created from%s\n */\n\n", $0 > sysnumhdr
    243 	printf "#ifndef _" constprefix "SYSCALL_H_\n" > sysnumhdr
    244 	printf "#define	_" constprefix "SYSCALL_H_\n\n" > sysnumhdr
    245 
    246 	printf " * created from%s\n */\n\n", $0 > sysarghdr
    247 	printf "#ifndef _" constprefix "SYSCALLARGS_H_\n" > sysarghdr
    248 	printf "#define	_" constprefix "SYSCALLARGS_H_\n\n" > sysarghdr
    249 
    250 	printf " * created from%s\n */\n\n", $0 > rumpcallshdr
    251 	printf "#ifndef _RUMP_RUMP_SYSCALLS_H_\n" > rumpcallshdr
    252 	printf "#define _RUMP_RUMP_SYSCALLS_H_\n\n" > rumpcallshdr
    253 	printf "#ifdef _KERNEL\n" > rumpcallshdr
    254 	printf "#error Interface not supported inside kernel\n" > rumpcallshdr
    255 	printf "#endif /* _KERNEL */\n\n" > rumpcallshdr
    256 	printf "#include <sys/types.h>\n" > rumpcallshdr
    257 	printf "#include <sys/select.h>\n\n" > rumpcallshdr
    258 	printf "#include <signal.h>\n\n" > rumpcallshdr
    259 
    260 	printf "%s", sysarghdrextra > sysarghdr
    261 	# Write max number of system call arguments to both headers
    262 	printf("#define\t%sMAXSYSARGS\t%d\n\n", constprefix, maxsysargs) \
    263 		> sysnumhdr
    264 	printf("#define\t%sMAXSYSARGS\t%d\n\n", constprefix, maxsysargs) \
    265 		> sysarghdr
    266 	printf "#undef\tsyscallarg\n" > sysarghdr
    267 	printf "#define\tsyscallarg(x)\t\t\t\t\t\t\t\\\n" > sysarghdr
    268 	printf "\tunion {\t\t\t\t\t\t\t\t\\\n" > sysarghdr
    269 	printf "\t\t%s pad;\t\t\t\t\t\t\\\n", registertype > sysarghdr
    270 	printf "\t\tstruct { x datum; } le;\t\t\t\t\t\\\n" > sysarghdr
    271 	printf "\t\tstruct { /* LINTED zero array dimension */\t\t\\\n" \
    272 		> sysarghdr
    273 	printf "\t\t\tint8_t pad[  /* CONSTCOND */\t\t\t\\\n" > sysarghdr
    274 	printf "\t\t\t\t(sizeof (%s) < sizeof (x))\t\\\n", \
    275 		registertype > sysarghdr
    276 	printf "\t\t\t\t? 0\t\t\t\t\t\\\n" > sysarghdr
    277 	printf "\t\t\t\t: sizeof (%s) - sizeof (x)];\t\\\n", \
    278 		registertype > sysarghdr
    279 	printf "\t\t\tx datum;\t\t\t\t\t\\\n" > sysarghdr
    280 	printf "\t\t} be;\t\t\t\t\t\t\t\\\n" > sysarghdr
    281 	printf "\t}\n" > sysarghdr
    282 	printf("\n#undef check_syscall_args\n") >sysarghdr
    283 	printf("#define check_syscall_args(call) \\\n" \
    284 		"\ttypedef char call##_check_args" \
    285 		    "[sizeof (struct call##_args) \\\n" \
    286 		"\t\t<= %sMAXSYSARGS * sizeof (%s) ? 1 : -1];\n", \
    287 		constprefix, registertype) >sysarghdr
    288 	next
    289 }
    290 NF == 0 || $1 ~ /^;/ {
    291 	next
    292 }
    293 $0 ~ /^%%$/ {
    294 	intable = 1
    295 	next
    296 }
    297 $1 ~ /^#[ 	]*include/ {
    298 	print > sysdcl
    299 	print > sysnames
    300 	next
    301 }
    302 $1 ~ /^#/ && !intable {
    303 	print > sysdcl
    304 	print > sysnames
    305 	next
    306 }
    307 $1 ~ /^#/ && intable {
    308 	if ($1 ~ /^#[ 	]*if/) {
    309 		savedepth++
    310 		savesyscall[savedepth] = syscall
    311 	}
    312 	if ($1 ~ /^#[ 	]*else/) {
    313 		if (savedepth <= 0) {
    314 			printf("%s: line %d: unbalanced #else\n", \
    315 			    infile, NR)
    316 			exit 1
    317 		}
    318 		syscall = savesyscall[savedepth]
    319 	}
    320 	if ($1 ~ /^#[       ]*endif/) {
    321 		if (savedepth <= 0) {
    322 			printf("%s: line %d: unbalanced #endif\n", \
    323 			    infile, NR)
    324 			exit 1
    325 		}
    326 		savedepth--
    327 	}
    328 	print > sysent
    329 	print > sysarghdr
    330 	print > sysnumhdr
    331 	print > sysprotos
    332 	print > sysnamesbottom
    333 
    334 	# XXX: technically we do not want to have conditionals in rump,
    335 	# but it is easier to just let the cpp handle them than try to
    336 	# figure out what we want here in this script
    337 	print > rumpsysent
    338 	next
    339 }
    340 syscall != $1 {
    341 	printf "%s: line %d: syscall number out of sync at %d\n", \
    342 	   infile, NR, syscall
    343 	printf "line is:\n"
    344 	print
    345 	exit 1
    346 }
    347 function parserr(was, wanted) {
    348 	printf "%s: line %d: unexpected %s (expected <%s>)\n", \
    349 	    infile, NR, was, wanted
    350 	printf "line is:\n"
    351 	print
    352 	exit 1
    353 }
    354 function parseline() {
    355 	f=3			# toss number and type
    356 	if ($2 == "INDIR")
    357 		sycall_flags="SYCALL_INDIRECT"
    358 	else
    359 		sycall_flags="0"
    360 	if ($NF != "}") {
    361 		funcalias=$NF
    362 		end=NF-1
    363 	} else {
    364 		funcalias=""
    365 		end=NF
    366 	}
    367 	if ($f == "INDIR") {		# allow for "NOARG INDIR"
    368 		sycall_flags = "SYCALL_INDIRECT | " sycall_flags
    369 		f++
    370 	}
    371 	if ($f == "MODULAR") {		# registered at runtime
    372 		modular = 1
    373 		f++
    374 	} else {
    375 		modular =  0;
    376 	}
    377 	if ($f == "RUMP") {
    378 		rumpable = 1
    379 		f++
    380 	} else {
    381 		rumpable = 0
    382 	}
    383 	if ($f ~ /^[a-z0-9_]*$/) {	# allow syscall alias
    384 		funcalias=$f
    385 		f++
    386 	}
    387 	if ($f != "{")
    388 		parserr($f, "{")
    389 	f++
    390 	if ($end != "}")
    391 		parserr($end, "}")
    392 	end--
    393 	if ($end != ";")
    394 		parserr($end, ";")
    395 	end--
    396 	if ($end != ")")
    397 		parserr($end, ")")
    398 	end--
    399 
    400 	returntype = oldf = "";
    401 	do {
    402 		if (returntype != "" && oldf != "*")
    403 			returntype = returntype" ";
    404 		returntype = returntype$f;
    405 		oldf = $f;
    406 		f++
    407 	} while ($f != "|" && f < (end-1))
    408 	if (f == (end - 1)) {
    409 		parserr($f, "function argument definition (maybe \"|\"?)");
    410 	}
    411 	f++
    412 
    413 	fprefix=$f
    414 	f++
    415 	if ($f != "|") {
    416 		parserr($f, "function compat delimiter (maybe \"|\"?)");
    417 	}
    418 	f++
    419 
    420 	fcompat=""
    421 	if ($f != "|") {
    422 		fcompat=$f
    423 		f++
    424 	}
    425 
    426 	if ($f != "|") {
    427 		parserr($f, "function name delimiter (maybe \"|\"?)");
    428 	}
    429 	f++
    430 	fbase=$f
    431 
    432 	# pipe is special in how to returns its values.
    433 	# So just generate it manually if present.
    434 	if (rumpable == 1 && fbase == "pipe") {
    435 		rumpable = 0;
    436 		rumphaspipe = 1;
    437 	}
    438 
    439 	funcstdname=fprefix "_" fbase
    440 	if (fcompat != "") {
    441 		funcname=fprefix "___" fbase "" fcompat
    442 		wantrename=1
    443 	} else {
    444 		funcname=funcstdname
    445 		wantrename=0
    446 	}
    447 	if (returntype == "quad_t" || returntype == "off_t") {
    448 		if (sycall_flags == "0")
    449 			sycall_flags = "SYCALL_RET_64";
    450 		else
    451 			sycall_flags = "SYCALL_RET_64 | " sycall_flags;
    452 	}
    453 
    454 	if (funcalias == "") {
    455 		funcalias=funcname
    456 		sub(/^([^_]+_)*sys_/, "", funcalias)
    457 	}
    458 	f++
    459 
    460 	if ($f != "(")
    461 		parserr($f, "(")
    462 	f++
    463 
    464 	argc=0;
    465 	argalign=0;
    466 	if (f == end) {
    467 		if ($f != "void")
    468 			parserr($f, "argument definition")
    469 		isvarargs = 0;
    470 		varargc = 0;
    471 		argtype[0]="void";
    472 		return
    473 	}
    474 
    475 	# some system calls (open() and fcntl()) can accept a variable
    476 	# number of arguments.  If syscalls accept a variable number of
    477 	# arguments, they must still have arguments specified for
    478 	# the remaining argument "positions," because of the way the
    479 	# kernel system call argument handling works.
    480 	#
    481 	# Indirect system calls, e.g. syscall(), are exceptions to this
    482 	# rule, since they are handled entirely by machine-dependent code
    483 	# and do not need argument structures built.
    484 
    485 	isvarargs = 0;
    486 	args64 = 0;
    487 	while (f <= end) {
    488 		if ($f == "...") {
    489 			f++;
    490 			isvarargs = 1;
    491 			varargc = argc;
    492 			continue;
    493 		}
    494 		argc++
    495 		argtype[argc]=""
    496 		oldf=""
    497 		while (f < end && $(f+1) != ",") {
    498 			if (argtype[argc] != "" && oldf != "*")
    499 				argtype[argc] = argtype[argc]" ";
    500 			argtype[argc] = argtype[argc]$f;
    501 			oldf = $f;
    502 			f++
    503 		}
    504 		if (argtype[argc] == "")
    505 			parserr($f, "argument definition")
    506 		if (argtype[argc] == "off_t"  \
    507 		  || argtype[argc] == "dev_t" \
    508 		  || argtype[argc] == "time_t") {
    509 			if ((argalign % 2) != 0 && sysalign &&
    510 			    funcname != "sys_posix_fadvise") # XXX for now
    511 				parserr($f, "a padding argument")
    512 		} else {
    513 			argalign++;
    514 		}
    515 		if (argtype[argc] == "quad_t" || argtype[argc] == "off_t" \
    516 		  || argtype[argc] == "dev_t" || argtype[argc] == "time_t") {
    517 			if (sycall_flags == "0")
    518 				sycall_flags = "SYCALL_ARG"argc-1"_64";
    519 			else
    520 				sycall_flags = "SYCALL_ARG"argc-1"_64 | " sycall_flags;
    521 			args64++;
    522 		}
    523 		argname[argc]=$f;
    524 		f += 2;			# skip name, and any comma
    525 	}
    526 	if (args64 > 0)
    527 		sycall_flags = "SYCALL_NARGS64_VAL("args64") | " sycall_flags;
    528 	# must see another argument after varargs notice.
    529 	if (isvarargs) {
    530 		if (argc == varargc)
    531 			parserr($f, "argument definition")
    532 	} else
    533 		varargc = argc;
    534 }
    535 
    536 function printproto(wrap) {
    537 	printf("/* syscall: \"%s%s\" ret: \"%s\" args:", wrap, funcalias,
    538 	    returntype) > sysnumhdr
    539 	for (i = 1; i <= varargc; i++)
    540 		printf(" \"%s\"", argtype[i]) > sysnumhdr
    541 	if (isvarargs)
    542 		printf(" \"...\"") > sysnumhdr
    543 	printf(" */\n") > sysnumhdr
    544 	printf("#define\t%s%s%s\t%d\n\n", constprefix, wrap, funcalias,
    545 	    syscall) > sysnumhdr
    546 
    547 	# rumpalooza
    548 	if (!rumpable)
    549 		return
    550 
    551 	if (wantrename)
    552 		printf("%s rump_%s(", returntype, funcstdname) > rumpcallshdr
    553 	else
    554 		printf("%s rump_sys_%s(", returntype, funcalias) > rumpcallshdr
    555 	for (i = 1; i < varargc; i++)
    556 		if (argname[i] != "PAD")
    557 			printf("%s, ", argtype[i]) > rumpcallshdr
    558 	if (isvarargs)
    559 		printf("%s, ...)", argtype[varargc]) > rumpcallshdr
    560 	else
    561 		printf("%s)", argtype[argc]) > rumpcallshdr
    562 	if (wantrename)
    563 		printf(" __RENAME(rump_%s)", funcname) > rumpcallshdr
    564 	printf(";\n") > rumpcallshdr
    565 }
    566 
    567 function printrumpsysent(insysent, compatwrap) {
    568 	if (!insysent) {
    569 		eno[0] = "rump_enosys"
    570 		eno[1] = "sys_nomodule"
    571 		printf("\t{ 0, 0, 0,\n\t    (sy_call_t *)%s },\t\t\t"	\
    572 		    "/* %d = unrumped */\n",				\
    573 		    eno[modular], syscall) > rumpsysent
    574 		return
    575 	}
    576 
    577 	printf("\t{ ") > rumpsysent
    578 	if (argc == 0) {
    579 		printf("0, 0, ") > rumpsysent
    580 	} else {
    581 		printf("ns(struct sys_%s%s_args), ", compatwrap_, funcalias) > rumpsysent
    582 	}
    583 	printf("0,\n\t    %s },", wfn) > rumpsysent
    584 	for (i = 0; i < (41 - length(wfn)) / 8; i++)
    585 		printf("\t") > rumpsysent
    586 	printf("/* %d = %s%s */\n", syscall, compatwrap_, funcalias) > rumpsysent
    587 }
    588 
    589 function putent(type, compatwrap) {
    590 	# output syscall declaration for switch table.
    591 	if (compatwrap == "")
    592 		compatwrap_ = ""
    593 	else
    594 		compatwrap_ = compatwrap "_"
    595 	if (argc == 0)
    596 		arg_type = "void";
    597 	else {
    598 		arg_type = "struct " compatwrap_ funcname "_args";
    599 	}
    600 	proto = "int\t" compatwrap_ funcname "(struct lwp *, const " \
    601 	    arg_type " *, register_t *);\n"
    602 	if (sysmap[proto] != 1) {
    603 		sysmap[proto] = 1;
    604 		print proto > sysprotos;
    605 	}
    606 
    607 	# output syscall switch entry
    608 	printf("\t{ ") > sysent
    609 	if (argc == 0) {
    610 		printf("0, 0, ") > sysent
    611 	} else {
    612 		printf("ns(struct %s%s_args), ", compatwrap_, funcname) > sysent
    613 	}
    614 	if (modular) 
    615 		wfn = "(sy_call_t *)sys_nomodule";
    616 	else if (compatwrap == "")
    617 		wfn = "(sy_call_t *)" funcname;
    618 	else
    619 		wfn = "(sy_call_t *)" compatwrap "(" funcname ")";
    620 	printf("%s,\n\t    %s },", sycall_flags, wfn) > sysent
    621 	for (i = 0; i < (33 - length(wfn)) / 8; i++)
    622 		printf("\t") > sysent
    623 	printf("/* %d = %s%s */\n", syscall, compatwrap_, funcalias) > sysent
    624 
    625 	# output syscall name for names table
    626 	printf("\t/* %3d */\t\"%s%s\",\n", syscall, compatwrap_, funcalias) \
    627 	    > sysnamesbottom
    628 
    629 	# output syscall number of header, if appropriate
    630 	if (type == "STD" || type == "NOARGS" || type == "INDIR") {
    631 		# output a prototype, to be used to generate lint stubs in
    632 		# libc.
    633 		printproto("")
    634 	} else if (type == "COMPAT") {
    635 		# Just define the syscall number with a comment.  These
    636 		# may be used by compatibility stubs in libc.
    637 		printproto(compatwrap_)
    638 	}
    639 
    640 	# output syscall argument structure, if it has arguments
    641 	if (argc != 0) {
    642 		printf("\nstruct %s%s_args", compatwrap_, funcname) > sysarghdr
    643 		if (type != "NOARGS") {
    644 			print " {" >sysarghdr;
    645 			for (i = 1; i <= argc; i++)
    646 				printf("\tsyscallarg(%s) %s;\n", argtype[i],
    647 				    argname[i]) > sysarghdr
    648 			printf "}" >sysarghdr;
    649 		}
    650 		printf(";\n") > sysarghdr
    651 		if (type != "NOARGS" && type != "INDIR") {
    652 			printf("check_syscall_args(%s%s)\n", compatwrap_,
    653 			    funcname) >sysarghdr
    654 		}
    655 	}
    656 
    657 	if (!rumpable) {
    658 		if (funcname == "sys_pipe" && rumphaspipe == 1)
    659 			insysent = 1
    660 		else
    661 			insysent = 0
    662 	} else {
    663 		insysent = 1
    664 	}
    665 	printrumpsysent(insysent, compatwrap)
    666 
    667 	# output rump marshalling code if necessary
    668 	if (!rumpable) {
    669 		return
    670 	}
    671 
    672 	# need a local prototype, we export the re-re-named one in .h
    673 	printf("\n%s rump_sys_%s(", returntype, funcalias) > rumpcalls
    674 	for (i = 1; i < argc; i++) {
    675 		if (argname[i] != "PAD")
    676 			printf("%s, ", argtype[i]) > rumpcalls
    677 	}
    678 	printf("%s);", argtype[argc]) > rumpcalls
    679 
    680 	printf("\n%s\nrump_sys_%s(", returntype, funcalias) > rumpcalls
    681 	for (i = 1; i < argc; i++) {
    682 		if (argname[i] != "PAD")
    683 			printf("%s %s, ", argtype[i], argname[i]) > rumpcalls
    684 	}
    685 	printf("%s %s)\n", argtype[argc], argname[argc]) > rumpcalls
    686 	printf("{\n\tregister_t rval[2] = {0, 0};\n\tint error = 0;\n") \
    687 	    > rumpcalls
    688 
    689 	argarg = "NULL"
    690 	argsize = 0;
    691 	if (argc) {
    692 		argarg = "&callarg"
    693 		argsize = "sizeof(callarg)"
    694 		printf("\tstruct %s%s_args callarg;\n\n",compatwrap_,funcname) \
    695 		    > rumpcalls
    696 		for (i = 1; i <= argc; i++) {
    697 			if (argname[i] == "PAD") {
    698 				printf("\tSPARG(&callarg, %s) = 0;\n", \
    699 				    argname[i]) > rumpcalls
    700 			} else {
    701 				printf("\tSPARG(&callarg, %s) = %s;\n", \
    702 				    argname[i], argname[i]) > rumpcalls
    703 			}
    704 		}
    705 		printf("\n") > rumpcalls
    706 	} else {
    707 		printf("\n") > rumpcalls
    708 	}
    709 	printf("\terror = rump_sysproxy(%s%s, rump_sysproxy_arg,\n\t" \
    710 	    "    (uint8_t *)%s, %s, rval);\n", constprefix, funcalias, \
    711 	    argarg, argsize) > rumpcalls
    712 	printf("\tif (error) {\n\t\trval[0] = -1;\n") > rumpcalls
    713 	if (returntype != "void") {
    714 		printf("\t\trumpuser_seterrno(error);\n\t}\n") > rumpcalls
    715 		printf("\treturn rval[0];\n") > rumpcalls
    716 	} else {
    717 		printf("\t}\n") > rumpcalls
    718 	}
    719 	printf("}\n") > rumpcalls
    720 	printf("__weak_alias(%s,rump_enosys);\n", funcname) > rumpcalls
    721 
    722 }
    723 $2 == "STD" || $2 == "NODEF" || $2 == "NOARGS" || $2 == "INDIR" {
    724 	parseline()
    725 	putent($2, "")
    726 	syscall++
    727 	next
    728 }
    729 $2 == "OBSOL" || $2 == "UNIMPL" || $2 == "EXCL" || $2 == "IGNORED" {
    730 	if ($2 == "OBSOL")
    731 		comment="obsolete"
    732 	else if ($2 == "EXCL")
    733 		comment="excluded"
    734 	else if ($2 == "IGNORED")
    735 		comment="ignored"
    736 	else
    737 		comment="unimplemented"
    738 	for (i = 3; i <= NF; i++)
    739 		comment=comment " " $i
    740 
    741 	if ($2 == "IGNORED")
    742 		sys_stub = "(sy_call_t *)nullop";
    743 	else
    744 		sys_stub = sys_nosys;
    745 
    746 	printf("\t{ 0, 0, 0,\n\t    %s },\t\t\t/* %d = %s */\n", \
    747 	    sys_stub, syscall, comment) > sysent
    748 	printf("\t{ 0, 0, 0,\n\t    %s },\t\t\t/* %d = %s */\n", \
    749 	    "(sy_call_t *)rump_enosys", syscall, comment) > rumpsysent
    750 	printf("\t/* %3d */\t\"#%d (%s)\",\n", syscall, syscall, comment) \
    751 	    > sysnamesbottom
    752 	if ($2 != "UNIMPL")
    753 		printf("\t\t\t\t/* %d is %s */\n", syscall, comment) > sysnumhdr
    754 	syscall++
    755 	next
    756 }
    757 {
    758 	for (i = 1; i <= ncompat; i++) {
    759 		if ($2 == compat_upper[i]) {
    760 			parseline();
    761 			putent("COMPAT", compat[i])
    762 			syscall++
    763 			next
    764 		}
    765 	}
    766 	printf("%s: line %d: unrecognized keyword %s\n", infile, NR, $2)
    767 	exit 1
    768 }
    769 END {
    770 	# output pipe() syscall with its special rval[2] handling
    771 	if (rumphaspipe) {
    772 		printf("int rump_sys_pipe(int *);\n") > rumpcallshdr
    773 		printf("\nint rump_sys_pipe(int *);\n") > rumpcalls
    774 		printf("int\nrump_sys_pipe(int *fd)\n{\n") > rumpcalls
    775 		printf("\tregister_t rval[2] = {0, 0};\n") > rumpcalls
    776 		printf("\tint error = 0;\n") > rumpcalls
    777 		printf("\n\terror = rump_sysproxy(SYS_pipe, ") > rumpcalls
    778 		printf("rump_sysproxy_arg, NULL, 0, rval);\n") > rumpcalls
    779 		printf("\tif (error) {\n") > rumpcalls
    780 		printf("\t\trumpuser_seterrno(error);\n") > rumpcalls
    781 		printf("\t} else {\n\t\tfd[0] = rval[0];\n") > rumpcalls
    782 		printf("\t\tfd[1] = rval[1];\n\t}\n") > rumpcalls
    783 		printf("\treturn error ? -1 : 0;\n}\n") > rumpcalls
    784 	}
    785 
    786 	maxsyscall = syscall
    787 	if (nsysent) {
    788 		if (syscall > nsysent) {
    789 			printf("%s: line %d: too many syscalls [%d > %d]\n", infile, NR, syscall, nsysent)
    790 			exit 1
    791 		}
    792 		while (syscall < nsysent) {
    793 			printf("\t{ 0, 0, 0,\n\t    %s },\t\t\t/* %d = filler */\n", \
    794 			    sys_nosys, syscall) > sysent
    795 			printf("\t{ 0, 0, 0,\n\t    %s },\t\t\t/* %d = filler */\n", \
    796 			    "(sy_call_t *)rump_enosys", syscall) > rumpsysent
    797 			syscall++
    798 		}
    799 	}
    800 	printf("};\n") > sysent
    801 	printf("};\n") > rumpsysent
    802 	printf("CTASSERT(__arraycount(rump_sysent) == SYS_NSYSENT);\n") > rumpsysent
    803 	printf("};\n") > sysnamesbottom
    804 	printf("#define\t%sMAXSYSCALL\t%d\n", constprefix, maxsyscall) > sysnumhdr
    805 	if (nsysent)
    806 		printf("#define\t%sNSYSENT\t%d\n", constprefix, nsysent) > sysnumhdr
    807 } '
    808 
    809 cat $sysprotos >> $sysarghdr
    810 echo "#endif /* _${constprefix}SYSCALL_H_ */" >> $sysnumhdr
    811 echo "#endif /* _${constprefix}SYSCALLARGS_H_ */" >> $sysarghdr
    812 printf "\n#include <rump/rump_syscalls_compat.h>\n" >> $rumpcallshdr
    813 printf "\n#endif /* _RUMP_RUMP_SYSCALLS_H_ */\n" >> $rumpcallshdr
    814 cat $sysdcl $sysent > $syssw
    815 cat $sysnamesbottom >> $sysnames
    816 cat $rumpsysent >> $rumpcalls
    817 
    818 #chmod 444 $sysnames $sysnumhdr $syssw
    819