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