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