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