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