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