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