Home | History | Annotate | Line # | Download | only in kern
makesyscalls.sh revision 1.125.2.3
      1 #	$NetBSD: makesyscalls.sh,v 1.125.2.3 2013/06/23 06:18:58 tls 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
     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 \"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\n" > rumpcalls
    236 	printf "#include <srcsys/syscall.h>\n" > rumpcalls
    237 	printf "#include <srcsys/syscallargs.h>\n\n" > rumpcalls
    238 	printf "#include <rump/rumpclient.h>\n\n" > rumpcalls
    239 	printf "#define rsys_syscall(num, data, dlen, retval)\t\\\n" > rumpcalls
    240 	printf "    rumpclient_syscall(num, data, dlen, retval)\n" > rumpcalls
    241 	printf "#define rsys_seterrno(error) errno = error\n" > rumpcalls
    242 	printf "#define rsys_alias(a,b)\n#else\n" > rumpcalls
    243 	printf "#include <sys/syscall.h>\n" > rumpcalls
    244 	printf "#include <sys/syscallargs.h>\n\n" > rumpcalls
    245 	printf "#include <sys/syscallvar.h>\n\n" > rumpcalls
    246 	printf "#include <rump/rumpuser.h>\n" > rumpcalls
    247 	printf "#include \"rump_private.h\"\n\n" > rumpcalls
    248 	printf "static int\nrsys_syscall" > rumpcalls
    249 	printf "(int num, void *data, size_t dlen, register_t *retval)" > rumpcalls
    250 	printf "\n{\n\tstruct proc *p;\n" > rumpcalls
    251 	printf "\tstruct emul *e;\n" > rumpcalls
    252 	printf "\tstruct sysent *callp;\n" > rumpcalls
    253 	printf "\tint rv;\n\n" > rumpcalls
    254 	printf "\trump_schedule();\n" > rumpcalls
    255 	printf "\tp = curproc;\n" > rumpcalls
    256 	printf "\te = p->p_emul;\n" > rumpcalls
    257 	printf "#ifndef __HAVE_MINIMAL_EMUL\n" > rumpcalls
    258 	printf "\tKASSERT(num > 0 && num < e->e_nsysent);\n" > rumpcalls
    259 	printf "#endif\n" > rumpcalls
    260 	printf "\tcallp = e->e_sysent + num;\n\n" > rumpcalls
    261 	printf "\trv = sy_call(callp, curlwp, data, retval);\n" > rumpcalls
    262 	printf "\trump_unschedule();\n\n\treturn rv;\n}\n\n" > rumpcalls
    263 	printf "#define rsys_seterrno(error) rumpuser_seterrno(error)\n" > rumpcalls
    264 	printf "#define rsys_alias(a,b) __weak_alias(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 
    276 	printf "\n#ifndef RUMP_CLIENT\n" > rumpsysent
    277 	printf "#define\ts(type)\tsizeof(type)\n" > rumpsysent
    278 	printf "#define\tn(type)\t(sizeof(type)/sizeof (%s))\n", registertype > rumpsysent
    279 	printf "#define\tns(type)\tn(type), s(type)\n\n", registertype > rumpsysent
    280 	printf "struct sysent rump_sysent[] = {\n" > rumpsysent
    281 
    282 	# System call names are included by userland (kdump(1)), so
    283 	# hide the include files from it.
    284 	printf "#if defined(_KERNEL_OPT)\n" > sysnames
    285 
    286 	printf "#endif /* _KERNEL_OPT */\n\n" > sysnamesbottom
    287 	printf "const char *const %s[] = {\n",namesname > sysnamesbottom
    288 
    289 	printf " * created from%s\n */\n\n", $0 > sysnumhdr
    290 	printf "#ifndef _" constprefix "SYSCALL_H_\n" > sysnumhdr
    291 	printf "#define	_" constprefix "SYSCALL_H_\n\n" > sysnumhdr
    292 
    293 	printf " * created from%s\n */\n\n", $0 > sysarghdr
    294 	printf "#ifndef _" constprefix "SYSCALLARGS_H_\n" > sysarghdr
    295 	printf "#define	_" constprefix "SYSCALLARGS_H_\n\n" > sysarghdr
    296 
    297 	printf " * created from%s\n */\n\n", $0 > rumpcallshdr
    298 	printf "#ifndef _RUMP_RUMP_SYSCALLS_H_\n" > rumpcallshdr
    299 	printf "#define _RUMP_RUMP_SYSCALLS_H_\n\n" > rumpcallshdr
    300 	printf "#ifdef _KERNEL\n" > rumpcallshdr
    301 	printf "#error Interface not supported inside kernel\n" > rumpcallshdr
    302 	printf "#endif /* _KERNEL */\n\n" > rumpcallshdr
    303 	printf "#include <sys/types.h> /* typedefs */\n" > rumpcallshdr
    304 	printf "#include <sys/select.h> /* typedefs */\n" > rumpcallshdr
    305 	printf "#include <sys/socket.h> /* typedefs */\n\n" > rumpcallshdr
    306 	printf "#include <signal.h> /* typedefs */\n\n" > rumpcallshdr
    307 	printf "#include <rump/rump_syscalls_compat.h>\n\n" > rumpcallshdr
    308 
    309 	printf "%s", sysarghdrextra > sysarghdr
    310 	# Write max number of system call arguments to both headers
    311 	printf("#define\t%sMAXSYSARGS\t%d\n\n", constprefix, maxsysargs) \
    312 		> sysnumhdr
    313 	printf("#define\t%sMAXSYSARGS\t%d\n\n", constprefix, maxsysargs) \
    314 		> sysarghdr
    315 	printf "#undef\tsyscallarg\n" > sysarghdr
    316 	printf "#define\tsyscallarg(x)\t\t\t\t\t\t\t\\\n" > sysarghdr
    317 	printf "\tunion {\t\t\t\t\t\t\t\t\\\n" > sysarghdr
    318 	printf "\t\t%s pad;\t\t\t\t\t\t\\\n", registertype > sysarghdr
    319 	printf "\t\tstruct { x datum; } le;\t\t\t\t\t\\\n" > sysarghdr
    320 	printf "\t\tstruct { /* LINTED zero array dimension */\t\t\\\n" \
    321 		> sysarghdr
    322 	printf "\t\t\tint8_t pad[  /* CONSTCOND */\t\t\t\\\n" > sysarghdr
    323 	printf "\t\t\t\t(sizeof (%s) < sizeof (x))\t\\\n", \
    324 		registertype > sysarghdr
    325 	printf "\t\t\t\t? 0\t\t\t\t\t\\\n" > sysarghdr
    326 	printf "\t\t\t\t: sizeof (%s) - sizeof (x)];\t\\\n", \
    327 		registertype > sysarghdr
    328 	printf "\t\t\tx datum;\t\t\t\t\t\\\n" > sysarghdr
    329 	printf "\t\t} be;\t\t\t\t\t\t\t\\\n" > sysarghdr
    330 	printf "\t}\n" > sysarghdr
    331 	printf("\n#undef check_syscall_args\n") >sysarghdr
    332 	printf("#define check_syscall_args(call) /*LINTED*/ \\\n" \
    333 		"\ttypedef char call##_check_args" \
    334 		    "[sizeof (struct call##_args) \\\n" \
    335 		"\t\t<= %sMAXSYSARGS * sizeof (%s) ? 1 : -1];\n", \
    336 		constprefix, registertype) >sysarghdr
    337 
    338 	# compat types from syscalls.master.  this is slightly ugly,
    339 	# but given that we have so few compats from over 17 years,
    340 	# a more complicated solution is not currently warranted.
    341 	uncompattypes["struct timeval50"] = "struct timeval";
    342 	uncompattypes["struct timespec50"] = "struct timespec";
    343 	uncompattypes["struct stat30"] = "struct stat";
    344 
    345 	next
    346 }
    347 NF == 0 || $1 ~ /^;/ {
    348 	next
    349 }
    350 $0 ~ /^%%$/ {
    351 	intable = 1
    352 	next
    353 }
    354 $1 ~ /^#[ 	]*include/ {
    355 	print > sysdcl
    356 	print > sysnames
    357 	next
    358 }
    359 $1 ~ /^#/ && !intable {
    360 	print > sysdcl
    361 	print > sysnames
    362 	next
    363 }
    364 $1 ~ /^#/ && intable {
    365 	if ($1 ~ /^#[ 	]*if/) {
    366 		savedepth++
    367 		savesyscall[savedepth] = syscall
    368 	}
    369 	if ($1 ~ /^#[ 	]*else/) {
    370 		if (savedepth <= 0) {
    371 			printf("%s: line %d: unbalanced #else\n", \
    372 			    infile, NR)
    373 			exit 1
    374 		}
    375 		syscall = savesyscall[savedepth]
    376 	}
    377 	if ($1 ~ /^#[       ]*endif/) {
    378 		if (savedepth <= 0) {
    379 			printf("%s: line %d: unbalanced #endif\n", \
    380 			    infile, NR)
    381 			exit 1
    382 		}
    383 		savedepth--
    384 	}
    385 	print > sysent
    386 	print > sysarghdr
    387 	print > sysnumhdr
    388 	print > sysprotos
    389 	print > sysnamesbottom
    390 
    391 	# XXX: technically we do not want to have conditionals in rump,
    392 	# but it is easier to just let the cpp handle them than try to
    393 	# figure out what we want here in this script
    394 	print > rumpsysent
    395 	next
    396 }
    397 syscall != $1 {
    398 	printf "%s: line %d: syscall number out of sync at %d\n", \
    399 	   infile, NR, syscall
    400 	printf "line is:\n"
    401 	print
    402 	exit 1
    403 }
    404 function parserr(was, wanted) {
    405 	printf "%s: line %d: unexpected %s (expected <%s>)\n", \
    406 	    infile, NR, was, wanted
    407 	printf "line is:\n"
    408 	print
    409 	exit 1
    410 }
    411 function parseline() {
    412 	f=3			# toss number and type
    413 	if ($2 == "INDIR")
    414 		sycall_flags="SYCALL_INDIRECT"
    415 	else
    416 		sycall_flags="0"
    417 	if ($NF != "}") {
    418 		funcalias=$NF
    419 		end=NF-1
    420 	} else {
    421 		funcalias=""
    422 		end=NF
    423 	}
    424 	if ($f == "INDIR") {		# allow for "NOARG INDIR"
    425 		sycall_flags = "SYCALL_INDIRECT | " sycall_flags
    426 		f++
    427 	}
    428 	if ($f == "MODULAR") {		# registered at runtime
    429 		modular = 1
    430 		f++
    431 	} else {
    432 		modular =  0;
    433 	}
    434 	if ($f == "RUMP") {
    435 		rumpable = 1
    436 		f++
    437 	} else {
    438 		rumpable = 0
    439 	}
    440 	if ($f ~ /^[a-z0-9_]*$/) {	# allow syscall alias
    441 		funcalias=$f
    442 		f++
    443 	}
    444 	if ($f != "{")
    445 		parserr($f, "{")
    446 	f++
    447 	if ($end != "}")
    448 		parserr($end, "}")
    449 	end--
    450 	if ($end != ";")
    451 		parserr($end, ";")
    452 	end--
    453 	if ($end != ")")
    454 		parserr($end, ")")
    455 	end--
    456 
    457 	returntype = oldf = "";
    458 	do {
    459 		if (returntype != "" && oldf != "*")
    460 			returntype = returntype" ";
    461 		returntype = returntype$f;
    462 		oldf = $f;
    463 		f++
    464 	} while ($f != "|" && f < (end-1))
    465 	if (f == (end - 1)) {
    466 		parserr($f, "function argument definition (maybe \"|\"?)");
    467 	}
    468 	f++
    469 
    470 	fprefix=$f
    471 	f++
    472 	if ($f != "|") {
    473 		parserr($f, "function compat delimiter (maybe \"|\"?)");
    474 	}
    475 	f++
    476 
    477 	fcompat=""
    478 	if ($f != "|") {
    479 		fcompat=$f
    480 		f++
    481 	}
    482 
    483 	if ($f != "|") {
    484 		parserr($f, "function name delimiter (maybe \"|\"?)");
    485 	}
    486 	f++
    487 	fbase=$f
    488 
    489 	# pipe is special in how to returns its values.
    490 	# So just generate it manually if present.
    491 	if (rumpable == 1 && fbase == "pipe") {
    492 		rumpable = 0;
    493 		rumphaspipe = 1;
    494 	}
    495 
    496 	if (fcompat != "") {
    497 		funcname=fprefix "___" fbase "" fcompat
    498 	} else {
    499 		funcname=fprefix "_" fbase
    500 	}
    501 	if (returntype == "quad_t" || returntype == "off_t") {
    502 		if (sycall_flags == "0")
    503 			sycall_flags = "SYCALL_RET_64";
    504 		else
    505 			sycall_flags = "SYCALL_RET_64 | " sycall_flags;
    506 	}
    507 
    508 	if (funcalias == "") {
    509 		funcalias=funcname
    510 		sub(/^([^_]+_)*sys_/, "", funcalias)
    511 		realname=fbase
    512 	} else {
    513 		realname=funcalias
    514 	}
    515 	rumpfname=realname "" fcompat
    516 	f++
    517 
    518 	if ($f != "(")
    519 		parserr($f, "(")
    520 	f++
    521 
    522 	argc=0;
    523 	argalign=0;
    524 	if (f == end) {
    525 		if ($f != "void")
    526 			parserr($f, "argument definition")
    527 		isvarargs = 0;
    528 		varargc = 0;
    529 		argtype[0]="void";
    530 		return
    531 	}
    532 
    533 	# some system calls (open() and fcntl()) can accept a variable
    534 	# number of arguments.  If syscalls accept a variable number of
    535 	# arguments, they must still have arguments specified for
    536 	# the remaining argument "positions," because of the way the
    537 	# kernel system call argument handling works.
    538 	#
    539 	# Indirect system calls, e.g. syscall(), are exceptions to this
    540 	# rule, since they are handled entirely by machine-dependent code
    541 	# and do not need argument structures built.
    542 
    543 	isvarargs = 0;
    544 	args64 = 0;
    545 	ptr = 0;
    546 	while (f <= end) {
    547 		if ($f == "...") {
    548 			f++;
    549 			isvarargs = 1;
    550 			varargc = argc;
    551 			continue;
    552 		}
    553 		argc++
    554 		argtype[argc]=""
    555 		oldf=""
    556 		while (f < end && $(f+1) != ",") {
    557 			if (argtype[argc] != "" && oldf != "*")
    558 				argtype[argc] = argtype[argc]" ";
    559 			argtype[argc] = argtype[argc]$f;
    560 			oldf = $f;
    561 			f++
    562 		}
    563 		if (argtype[argc] == "")
    564 			parserr($f, "argument definition")
    565 		if (argtype[argc] == "off_t"  \
    566 		  || argtype[argc] == "dev_t" \
    567 		  || argtype[argc] == "time_t") {
    568 			if ((argalign % 2) != 0 && sysalign &&
    569 			    funcname != "sys_posix_fadvise") # XXX for now
    570 				parserr($f, "a padding argument")
    571 		} else {
    572 			argalign++;
    573 		}
    574 		if (argtype[argc] == "quad_t" || argtype[argc] == "off_t" \
    575 		  || argtype[argc] == "dev_t" || argtype[argc] == "time_t") {
    576 			if (sycall_flags == "0")
    577 				sycall_flags = "SYCALL_ARG"argc-1"_64";
    578 			else
    579 				sycall_flags = "SYCALL_ARG"argc-1"_64 | " sycall_flags;
    580 			args64++;
    581 		}
    582 		if (index(argtype[argc], "*") != 0 && ptr == 0) {
    583 			if (sycall_flags == "0")
    584 				sycall_flags = "SYCALL_ARG_PTR";
    585 			else
    586 				sycall_flags = "SYCALL_ARG_PTR | " sycall_flags;
    587 			ptr = 1;
    588 		}
    589 		argname[argc]=$f;
    590 		f += 2;			# skip name, and any comma
    591 	}
    592 	if (args64 > 0)
    593 		sycall_flags = "SYCALL_NARGS64_VAL("args64") | " sycall_flags;
    594 	# must see another argument after varargs notice.
    595 	if (isvarargs) {
    596 		if (argc == varargc)
    597 			parserr($f, "argument definition")
    598 	} else
    599 		varargc = argc;
    600 }
    601 
    602 function printproto(wrap) {
    603 	printf("/* syscall: \"%s%s\" ret: \"%s\" args:", wrap, funcalias,
    604 	    returntype) > sysnumhdr
    605 	for (i = 1; i <= varargc; i++)
    606 		printf(" \"%s\"", argtype[i]) > sysnumhdr
    607 	if (isvarargs)
    608 		printf(" \"...\"") > sysnumhdr
    609 	printf(" */\n") > sysnumhdr
    610 	printf("#define\t%s%s%s\t%d\n\n", constprefix, wrap, funcalias,
    611 	    syscall) > sysnumhdr
    612 
    613 	# rumpalooza
    614 	if (!rumpable)
    615 		return
    616 
    617 	# accumulate fbases we have seen.  we want the last
    618 	# occurence for the default __RENAME()
    619 	seen = funcseen[fbase]
    620 	funcseen[fbase] = rumpfname
    621 	if (seen)
    622 		return
    623 
    624 	printf("%s rump_sys_%s(", returntype, realname) > rumpprotos
    625 
    626 	for (i = 1; i < varargc; i++)
    627 		if (argname[i] != "PAD")
    628 			printf("%s, ", uncompattype(argtype[i])) > rumpprotos
    629 
    630 	if (isvarargs)
    631 		printf("%s, ...)", uncompattype(argtype[varargc]))>rumpprotos
    632 	else
    633 		printf("%s)", uncompattype(argtype[argc])) > rumpprotos
    634 
    635 	printf(" __RENAME(RUMP_SYS_RENAME_%s)", toupper(fbase))> rumpprotos
    636 	printf(";\n") > rumpprotos
    637 
    638 	# generate forward-declares for types, apart from the
    639 	# braindead typedef jungle we cannot easily handle here
    640 	for (i = 1; i <= varargc; i++) {
    641 		type=uncompattype(argtype[i])
    642 		sub("const ", "", type)
    643 		if (!typeseen[type] && \
    644 		    match(type, "struct") && match(type, "\\*")) {
    645 			typeseen[type] = 1
    646 			sub(" *\\*", "", type);
    647 			printf("%s;\n", type) > rumptypes
    648 		}
    649 	}
    650 }
    651 
    652 function printrumpsysent(insysent, compatwrap) {
    653 	if (!insysent) {
    654 		eno[0] = "rump_enosys"
    655 		eno[1] = "sys_nomodule"
    656 		flags[0] = "SYCALL_NOSYS"
    657 		flags[1] = "0"
    658 		printf("\t{ 0, 0, %s,\n\t    (sy_call_t *)%s }, \t"	\
    659 		    "/* %d = %s */\n",					\
    660 		    flags[modular], eno[modular], syscall, funcalias)	\
    661 		    > rumpsysent
    662 		return
    663 	}
    664 
    665 	printf("\t{ ") > rumpsysent
    666 	if (argc == 0) {
    667 		printf("0, 0, ") > rumpsysent
    668 	} else {
    669 		printf("ns(struct %ssys_%s_args), ", compatwrap_, funcalias) > rumpsysent
    670 	}
    671 
    672 	if (compatwrap == "") {
    673 		if (modular)
    674 			rfn = "(sy_call_t *)sys_nomodule"
    675 		else
    676 			rfn = "(sy_call_t *)" funcname
    677 	} else {
    678 		rfn = "(sy_call_t *)" compatwrap "_" funcname
    679 	}
    680 
    681 	printf("0,\n\t    %s },", rfn) > rumpsysent
    682 	for (i = 0; i < (33 - length(rfn)) / 8; i++)
    683 		printf("\t") > rumpsysent
    684 	printf("/* %d = %s%s */\n", syscall, compatwrap_, funcalias) > rumpsysent
    685 }
    686 
    687 function iscompattype(type) {
    688 	for (var in uncompattypes) {
    689 		if (match(type, var)) {
    690 			return 1
    691 		}
    692 	}
    693 
    694 	return 0
    695 }
    696 
    697 function uncompattype(type) {
    698 	for (var in uncompattypes) {
    699 		if (match(type, var)) {
    700 			sub(var, uncompattypes[var], type)
    701 			return type
    702 		}
    703 	}
    704 
    705 	return type
    706 }
    707 
    708 function putent(type, compatwrap) {
    709 	# output syscall declaration for switch table.
    710 	if (compatwrap == "")
    711 		compatwrap_ = ""
    712 	else
    713 		compatwrap_ = compatwrap "_"
    714 	if (argc == 0)
    715 		arg_type = "void";
    716 	else {
    717 		arg_type = "struct " compatwrap_ funcname "_args";
    718 	}
    719 	proto = "int\t" compatwrap_ funcname "(struct lwp *, const " \
    720 	    arg_type " *, register_t *);\n"
    721 	if (sysmap[proto] != 1) {
    722 		sysmap[proto] = 1;
    723 		print proto > sysprotos;
    724 	}
    725 
    726 	# output syscall switch entry
    727 	printf("\t{ ") > sysent
    728 	if (argc == 0) {
    729 		printf("0, 0, ") > sysent
    730 	} else {
    731 		printf("ns(struct %s%s_args), ", compatwrap_, funcname) > sysent
    732 	}
    733 	if (modular) 
    734 		wfn = "(sy_call_t *)sys_nomodule";
    735 	else if (compatwrap == "")
    736 		wfn = "(sy_call_t *)" funcname;
    737 	else
    738 		wfn = "(sy_call_t *)" compatwrap "(" funcname ")";
    739 	printf("%s,\n\t    %s },", sycall_flags, wfn) > sysent
    740 	for (i = 0; i < (33 - length(wfn)) / 8; i++)
    741 		printf("\t") > sysent
    742 	printf("/* %d = %s%s */\n", syscall, compatwrap_, funcalias) > sysent
    743 
    744 	# output syscall name for names table
    745 	printf("\t/* %3d */\t\"%s%s\",\n", syscall, compatwrap_, funcalias) \
    746 	    > sysnamesbottom
    747 
    748 	# output syscall number of header, if appropriate
    749 	if (type == "STD" || type == "NOARGS" || type == "INDIR" || \
    750 	    type == "NOERR") {
    751 		# output a prototype, to be used to generate lint stubs in
    752 		# libc.
    753 		printproto("")
    754 	} else if (type == "COMPAT" || type == "EXTERN") {
    755 		# Just define the syscall number with a comment.  These
    756 		# may be used by compatibility stubs in libc.
    757 		printproto(compatwrap_)
    758 	}
    759 
    760 	# output syscall argument structure, if it has arguments
    761 	if (argc != 0) {
    762 		printf("\n") > sysarghdr
    763 		if (haverumpcalls && !rumpable)
    764 			printf("#ifndef RUMP_CLIENT\n") > sysarghdr
    765 		printf("struct %s%s_args", compatwrap_, funcname) > sysarghdr
    766 		if (type != "NOARGS") {
    767 			print " {" >sysarghdr;
    768 			for (i = 1; i <= argc; i++)
    769 				printf("\tsyscallarg(%s) %s;\n", argtype[i],
    770 				    argname[i]) > sysarghdr
    771 			printf "}" >sysarghdr;
    772 		}
    773 		printf(";\n") > sysarghdr
    774 		if (type != "NOARGS" && type != "INDIR") {
    775 			printf("check_syscall_args(%s%s)\n", compatwrap_,
    776 			    funcname) >sysarghdr
    777 		}
    778 		if (haverumpcalls && !rumpable)
    779 			printf("#endif /* !RUMP_CLIENT */\n") > sysarghdr
    780 	}
    781 
    782 	if (!rumpable) {
    783 		if (funcname == "sys_pipe" && rumphaspipe == 1)
    784 			insysent = 1
    785 		else
    786 			insysent = 0
    787 	} else {
    788 		insysent = 1
    789 	}
    790 	printrumpsysent(insysent, compatwrap)
    791 
    792 	# output rump marshalling code if necessary
    793 	if (!rumpable) {
    794 		return
    795 	}
    796 
    797 	# need a local prototype, we export the re-re-named one in .h
    798 	printf("\n%s rump___sysimpl_%s(", returntype, rumpfname) \
    799 	    > rumpcalls
    800 	for (i = 1; i < argc; i++) {
    801 		if (argname[i] != "PAD")
    802 			printf("%s, ", uncompattype(argtype[i])) > rumpcalls
    803 	}
    804 	printf("%s);", uncompattype(argtype[argc])) > rumpcalls
    805 
    806 	printf("\n%s\nrump___sysimpl_%s(", returntype, rumpfname) > rumpcalls
    807 	for (i = 1; i < argc; i++) {
    808 		if (argname[i] != "PAD")
    809 			printf("%s %s, ", uncompattype(argtype[i]), \
    810 			    argname[i]) > rumpcalls
    811 	}
    812 	printf("%s %s)\n", uncompattype(argtype[argc]), argname[argc]) \
    813 	    > rumpcalls
    814 	printf("{\n\tregister_t retval[2] = {0, 0};\n") > rumpcalls
    815 	if (returntype != "void") {
    816 		if (type != "NOERR") {
    817 			printf("\tint error = 0;\n") > rumpcalls
    818 		}
    819 		# assume rumpcalls return only integral types
    820 		printf("\t%s rv = -1;\n", returntype) > rumpcalls
    821 	}
    822 
    823 	argarg = "NULL"
    824 	argsize = 0;
    825 	if (argc) {
    826 		argarg = "&callarg"
    827 		argsize = "sizeof(callarg)"
    828 		printf("\tstruct %s%s_args callarg;\n\n",compatwrap_,funcname) \
    829 		    > rumpcalls
    830 		for (i = 1; i <= argc; i++) {
    831 			if (argname[i] == "PAD") {
    832 				printf("\tSPARG(&callarg, %s) = 0;\n", \
    833 				    argname[i]) > rumpcalls
    834 			} else {
    835 				if (iscompattype(argtype[i])) {
    836 					printf("\tSPARG(&callarg, %s) = "    \
    837 					"(%s)%s;\n", argname[i], argtype[i], \
    838 					argname[i]) > rumpcalls
    839 				} else {
    840 					printf("\tSPARG(&callarg, %s) = %s;\n",\
    841 					    argname[i], argname[i]) > rumpcalls
    842 				}
    843 			}
    844 		}
    845 		printf("\n") > rumpcalls
    846 	} else {
    847 		printf("\n") > rumpcalls
    848 	}
    849 	printf("\t") > rumpcalls
    850 	if (returntype != "void" && type != "NOERR")
    851 		printf("error = ") > rumpcalls
    852 	printf("rsys_syscall(%s%s%s, " \
    853 	    "%s, %s, retval);\n", constprefix, compatwrap_, funcalias, \
    854 	    argarg, argsize) > rumpcalls
    855 	if (type != "NOERR") {
    856 		printf("\trsys_seterrno(error);\n") > rumpcalls
    857 		printf("\tif (error == 0) {\n") > rumpcalls
    858 		indent="\t\t"
    859 		ending="\t}\n"
    860 	} else {
    861 		indent="\t"
    862 		ending=""
    863 	}
    864 	if (returntype != "void") {
    865 		printf("%sif (sizeof(%s) > sizeof(register_t))\n", \
    866 		    indent, returntype) > rumpcalls
    867 		printf("%s\trv = *(%s *)retval;\n", \
    868 		    indent, returntype) > rumpcalls
    869 		printf("%selse\n", indent, indent) > rumpcalls
    870 		printf("%s\trv = *retval;\n", indent, returntype) > rumpcalls
    871 		printf("%s", ending) > rumpcalls
    872 		printf("\treturn rv;\n") > rumpcalls
    873 	}
    874 	printf("}\n") > rumpcalls
    875 	printf("rsys_alias(%s%s,rump_enosys)\n", \
    876 	    compatwrap_, funcname) > rumpcalls
    877 
    878 }
    879 $2 == "STD" || $2 == "NODEF" || $2 == "NOARGS" || $2 == "INDIR" \
    880     || $2 == "NOERR" {
    881 	parseline()
    882 	putent($2, "")
    883 	syscall++
    884 	next
    885 }
    886 $2 == "OBSOL" || $2 == "UNIMPL" || $2 == "EXCL" || $2 == "IGNORED" {
    887 	if ($2 == "OBSOL")
    888 		comment="obsolete"
    889 	else if ($2 == "EXCL")
    890 		comment="excluded"
    891 	else if ($2 == "IGNORED")
    892 		comment="ignored"
    893 	else
    894 		comment="unimplemented"
    895 	for (i = 3; i <= NF; i++)
    896 		comment=comment " " $i
    897 
    898 	if ($2 == "IGNORED")
    899 		sys_stub = "(sy_call_t *)nullop";
    900 	else
    901 		sys_stub = sys_nosys;
    902 
    903 	printf("\t{ 0, 0, 0,\n\t    %s },\t\t\t/* %d = %s */\n", \
    904 	    sys_stub, syscall, comment) > sysent
    905 	printf("\t{ 0, 0, SYCALL_NOSYS,\n\t    %s },\t\t/* %d = %s */\n", \
    906 	    "(sy_call_t *)rump_enosys", syscall, comment) > rumpsysent
    907 	printf("\t/* %3d */\t\"#%d (%s)\",\n", syscall, syscall, comment) \
    908 	    > sysnamesbottom
    909 	if ($2 != "UNIMPL")
    910 		printf("\t\t\t\t/* %d is %s */\n", syscall, comment) > sysnumhdr
    911 	syscall++
    912 	next
    913 }
    914 $2 == "EXTERN" {
    915 	parseline()
    916 	putent("EXTERN", "")
    917 	syscall++
    918 	next
    919 }
    920 {
    921 	for (i = 1; i <= ncompat; i++) {
    922 		if ($2 == compat_upper[i]) {
    923 			parseline();
    924 			putent("COMPAT", compat[i])
    925 			syscall++
    926 			next
    927 		}
    928 	}
    929 	printf("%s: line %d: unrecognized keyword %s\n", infile, NR, $2)
    930 	exit 1
    931 }
    932 END {
    933 	# output pipe() syscall with its special retval[2] handling
    934 	if (rumphaspipe) {
    935 		printf("int rump_sys_pipe(int *);\n") > rumpprotos
    936 		printf("\nint rump_sys_pipe(int *);\n") > rumpcalls
    937 		printf("int\nrump_sys_pipe(int *fd)\n{\n") > rumpcalls
    938 		printf("\tregister_t retval[2] = {0, 0};\n") > rumpcalls
    939 		printf("\tint error = 0;\n") > rumpcalls
    940 		printf("\n\terror = rsys_syscall(SYS_pipe, ") > rumpcalls
    941 		printf("NULL, 0, retval);\n") > rumpcalls
    942 		printf("\tif (error) {\n") > rumpcalls
    943 		printf("\t\trsys_seterrno(error);\n") > rumpcalls
    944 		printf("\t} else {\n\t\tfd[0] = retval[0];\n") > rumpcalls
    945 		printf("\t\tfd[1] = retval[1];\n\t}\n") > rumpcalls
    946 		printf("\treturn error ? -1 : 0;\n}\n") > rumpcalls
    947 	}
    948 
    949 	# print default rump syscall interfaces
    950 	for (var in funcseen) {
    951 		printf("#ifndef RUMP_SYS_RENAME_%s\n", \
    952 		    toupper(var)) > rumpcallshdr
    953 		printf("#define RUMP_SYS_RENAME_%s rump___sysimpl_%s\n", \
    954 		    toupper(var), funcseen[var]) > rumpcallshdr
    955 		printf("#endif\n\n") > rumpcallshdr
    956 	}
    957 
    958 	maxsyscall = syscall
    959 	if (nsysent) {
    960 		if (syscall > nsysent) {
    961 			printf("%s: line %d: too many syscalls [%d > %d]\n", infile, NR, syscall, nsysent)
    962 			exit 1
    963 		}
    964 		while (syscall < nsysent) {
    965 			printf("\t{ 0, 0, 0,\n\t    %s },\t\t\t/* %d = filler */\n", \
    966 			    sys_nosys, syscall) > sysent
    967 			printf("\t{ 0, 0, SYCALL_NOSYS,\n\t    %s },\t\t/* %d = filler */\n", \
    968 			    "(sy_call_t *)rump_enosys", syscall) > rumpsysent
    969 			printf("\t/* %3d */\t\"# filler\",\n", syscall) \
    970 			    > sysnamesbottom
    971 			syscall++
    972 		}
    973 	}
    974 	printf("};\n") > sysent
    975 	printf("};\n") > rumpsysent
    976 	printf("CTASSERT(__arraycount(rump_sysent) == SYS_NSYSENT);\n") > rumpsysent
    977 	printf("__strong_alias(sysent,rump_sysent);\n") > rumpsysent
    978 	printf("#endif /* RUMP_CLIENT */\n") > rumpsysent
    979 	if (haverumpcalls)
    980 		printf("#endif /* !RUMP_CLIENT */\n") > sysprotos
    981 	printf("};\n") > sysnamesbottom
    982 	printf("#define\t%sMAXSYSCALL\t%d\n", constprefix, maxsyscall) > sysnumhdr
    983 	if (nsysent)
    984 		printf("#define\t%sNSYSENT\t%d\n", constprefix, nsysent) > sysnumhdr
    985 } '
    986 
    987 cat $sysprotos >> $sysarghdr
    988 echo "#endif /* _${constprefix}SYSCALL_H_ */" >> $sysnumhdr
    989 echo "#endif /* _${constprefix}SYSCALLARGS_H_ */" >> $sysarghdr
    990 printf "\n#endif /* _RUMP_RUMP_SYSCALLS_H_ */\n" >> $rumpprotos
    991 cat $sysdcl $sysent > $syssw
    992 cat $sysnamesbottom >> $sysnames
    993 cat $rumpsysent >> $rumpcalls
    994 
    995 touch $rumptypes
    996 cat $rumptypes >> $rumpcallshdr
    997 echo >> $rumpcallshdr
    998 cat $rumpprotos >> $rumpcallshdr
    999 
   1000 #chmod 444 $sysnames $sysnumhdr $syssw
   1001