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