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