Home | History | Annotate | Line # | Download | only in kern
makesyscalls.sh revision 1.51
      1 #! /bin/sh -
      2 #	$NetBSD: makesyscalls.sh,v 1.51 2001/10/30 21:52:27 manu 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 #	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 #
     58 # NOTE THAT THIS makesyscalls.sh DOES NOT SUPPORT 'LIBCOMPAT'.
     59 
     60 # source the config file.
     61 sys_nosys="sys_nosys"	# default is sys_nosys(), if not specified otherwise
     62 . ./$1
     63 
     64 # tmp files:
     65 sysdcl="sysent.dcl"
     66 sysprotos="sys.protos"
     67 syscompat_pref="sysent."
     68 sysent="sysent.switch"
     69 sysnamesbottom="sysnames.bottom"
     70 
     71 trap "rm $sysdcl $sysprotos $sysent $sysnamesbottom" 0
     72 
     73 # Awk program (must support nawk extensions)
     74 # Use "awk" at Berkeley, "nawk" or "gawk" elsewhere.
     75 awk=${AWK:-awk}
     76 
     77 # Does this awk have a "toupper" function? (i.e. is it GNU awk)
     78 isgawk=`$awk 'BEGIN { print toupper("true"); exit; }' 2>/dev/null`
     79 
     80 # If this awk does not define "toupper" then define our own.
     81 if [ "$isgawk" = TRUE ] ; then
     82 	# GNU awk provides it.
     83 	toupper=
     84 else
     85 	# Provide our own toupper()
     86 	toupper='
     87 function toupper(str) {
     88 	_toupper_cmd = "echo "str" |tr a-z A-Z"
     89 	_toupper_cmd | getline _toupper_str;
     90 	close(_toupper_cmd);
     91 	return _toupper_str;
     92 }'
     93 fi
     94 
     95 # before handing it off to awk, make a few adjustments:
     96 #	(1) insert spaces around {, }, (, ), *, and commas.
     97 #	(2) get rid of any and all dollar signs (so that rcs id use safe)
     98 #
     99 # The awk script will deal with blank lines and lines that
    100 # start with the comment character (';').
    101 
    102 sed -e '
    103 s/\$//g
    104 :join
    105 	/\\$/{a\
    106 
    107 	N
    108 	s/\\\n//
    109 	b join
    110 	}
    111 2,${
    112 	/^#/!s/\([{}()*,]\)/ \1 /g
    113 }
    114 ' < $2 | $awk "
    115 $toupper
    116 BEGIN {
    117 	# to allow nested #if/#else/#endif sets
    118 	savedepth = 0
    119 	# to track already processed syscalls
    120 	syscallseen[0] = 0
    121 
    122 	sysnames = \"$sysnames\"
    123 	sysprotos = \"$sysprotos\"
    124 	sysnumhdr = \"$sysnumhdr\"
    125 	sysarghdr = \"$sysarghdr\"
    126 	switchname = \"$switchname\"
    127 	namesname = \"$namesname\"
    128 	constprefix = \"$constprefix\"
    129 	registertype = \"$registertype\"
    130 	if (!registertype) {
    131 	    registertype = \"register_t\"
    132 	}
    133 	nsysent = \"$nsysent\"
    134 
    135 	sysdcl = \"$sysdcl\"
    136 	syscompat_pref = \"$syscompat_pref\"
    137 	sysent = \"$sysent\"
    138 	sysnamesbottom = \"$sysnamesbottom\"
    139 	sys_nosys = \"$sys_nosys\"
    140 	infile = \"$2\"
    141 
    142 	compatopts = \"$compatopts\"
    143 	"'
    144 
    145 	printf "/* \$NetBSD\$ */\n\n" > sysdcl
    146 	printf "/*\n * System call switch table.\n *\n" > sysdcl
    147 	printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysdcl
    148 
    149 	ncompat = split(compatopts,compat)
    150 	for (i = 1; i <= ncompat; i++) {
    151 		compat_upper[i] = toupper(compat[i])
    152 
    153 		printf "\n#ifdef %s\n", compat_upper[i] > sysent
    154 		printf "#define	%s(func) __CONCAT(%s_,func)\n", compat[i], \
    155 		    compat[i] > sysent
    156 		printf "#else\n" > sysent
    157 		printf "#define	%s(func) %s\n", compat[i], sys_nosys > sysent
    158 		printf "#endif\n" > sysent
    159 	}
    160 
    161 	printf "\n#define\ts(type)\tsizeof(type)\n\n" > sysent
    162 	printf "struct sysent %s[] = {\n",switchname > sysent
    163 
    164 	printf "/* \$NetBSD\$ */\n\n" > sysnames
    165 	printf "/*\n * System call names.\n *\n" > sysnames
    166 	printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysnames
    167 
    168 	printf "\n/*\n * System call prototypes.\n */\n\n" > sysprotos
    169 
    170 	printf "/* \$NetBSD\$ */\n\n" > sysnumhdr
    171 	printf "/*\n * System call numbers.\n *\n" > sysnumhdr
    172 	printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysnumhdr
    173 
    174 	printf "/* \$NetBSD\$ */\n\n" > sysarghdr
    175 	printf "/*\n * System call argument lists.\n *\n" > sysarghdr
    176 	printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysarghdr
    177 }
    178 NR == 1 {
    179 	printf " * created from%s\n */\n\n", $0 > sysdcl
    180 
    181 	printf " * created from%s\n */\n\n", $0 > sysnames
    182 
    183 	# System call names are included by userland (kdump(1)), so
    184 	# hide the include files from it.
    185 	printf "#if defined(_KERNEL_OPT)\n" > sysnames
    186 
    187 	printf "#endif /* _KERNEL_OPT */\n\n" > sysnamesbottom
    188 	printf "const char *const %s[] = {\n",namesname > sysnamesbottom
    189 
    190 	printf " * created from%s\n */\n\n", $0 > sysnumhdr
    191 
    192 	printf " * created from%s\n */\n\n", $0 > sysarghdr
    193 	printf "#ifndef _" constprefix "_SYSCALLARGS_H_\n" > sysarghdr
    194 	printf "#define	_" constprefix "_SYSCALLARGS_H_\n\n" > sysarghdr
    195 	printf "#ifdef\tsyscallarg\n" > sysarghdr
    196 	printf "#undef\tsyscallarg\n" > sysarghdr
    197 	printf "#endif\n\n" > sysarghdr
    198 	printf "#define\tsyscallarg(x)\t\t\t\t\t\t\t\\\n" > sysarghdr
    199 	printf "\tunion {\t\t\t\t\t\t\t\t\\\n" > sysarghdr
    200 	printf "\t\t%s pad;\t\t\t\t\t\t\\\n", registertype > sysarghdr
    201 	printf "\t\tstruct { x datum; } le;\t\t\t\t\t\\\n" > sysarghdr
    202 	printf "\t\tstruct { /* LINTED zero array dimension */\t\t\\\n" \
    203 		> sysarghdr
    204 	printf "\t\t\tint8_t pad[  /* CONSTCOND */\t\t\t\\\n" > sysarghdr
    205 	printf "\t\t\t\t(sizeof (%s) < sizeof (x))\t\\\n", \
    206 		registertype > sysarghdr
    207 	printf "\t\t\t\t? 0\t\t\t\t\t\\\n" > sysarghdr
    208 	printf "\t\t\t\t: sizeof (%s) - sizeof (x)];\t\\\n", \
    209 		registertype > sysarghdr
    210 	printf "\t\t\tx datum;\t\t\t\t\t\\\n" > sysarghdr
    211 	printf "\t\t} be;\t\t\t\t\t\t\t\\\n" > sysarghdr
    212 	printf "\t}\n" > sysarghdr
    213 	next
    214 }
    215 NF == 0 || $1 ~ /^;/ {
    216 	next
    217 }
    218 $0 ~ /^%%$/ {
    219 	intable = 1
    220 	next
    221 }
    222 $1 ~ /^#[ 	]*include/ {
    223 	print > sysdcl
    224 	print > sysnames
    225 	next
    226 }
    227 $1 ~ /^#/ && !intable {
    228 	print > sysdcl
    229 	print > sysnames
    230 	next
    231 }
    232 $1 ~ /^#/ && intable {
    233 	if ($1 ~ /^#[ 	]*if/) {
    234 		savedepth++
    235 		savesyscall[savedepth] = syscall
    236 	}
    237 	if ($1 ~ /^#[ 	]*else/) {
    238 		if (savedepth <= 0) {
    239 			printf("%s: line %d: unbalanced #else\n", \
    240 			    infile, NR)
    241 			exit 1
    242 		}
    243 		syscall = savesyscall[savedepth]
    244 	}
    245 	if ($1 ~ /^#[       ]*endif/) {
    246 		if (savedepth <= 0) {
    247 			printf("%s: line %d: unbalanced #endif\n", \
    248 			    infile, NR)
    249 			exit 1
    250 		}
    251 		savedepth--
    252 	}
    253 	print > sysent
    254 	print > sysprotos
    255 	print > sysnamesbottom
    256 	next
    257 }
    258 syscall != $1 {
    259 	printf "%s: line %d: syscall number out of sync at %d\n", \
    260 	   infile, NR, syscall
    261 	printf "line is:\n"
    262 	print
    263 	exit 1
    264 }
    265 function parserr(was, wanted) {
    266 	printf "%s: line %d: unexpected %s (expected %s)\n", \
    267 	    infile, NR, was, wanted
    268 	printf "line is:\n"
    269 	print
    270 	exit 1
    271 }
    272 function parseline() {
    273 	f=3			# toss number and type
    274 	sycall_flags="0"
    275 	if ($NF != "}") {
    276 		funcalias=$NF
    277 		end=NF-1
    278 	} else {
    279 		funcalias=""
    280 		end=NF
    281 	}
    282 	if ($f == "MPSAFE") {		# allow for MP-safe syscalls
    283 		sycall_flags = sprintf("SYCALL_MPSAFE | %s", sycall_flags)
    284 		f++
    285 	}
    286 	if ($f ~ /^[a-z0-9_]*$/) {	# allow syscall alias
    287 		funcalias=$f
    288 		f++
    289 	}
    290 	if ($f != "{")
    291 		parserr($f, "{")
    292 	f++
    293 	if ($end != "}")
    294 		parserr($end, "}")
    295 	end--
    296 	if ($end != ";")
    297 		parserr($end, ";")
    298 	end--
    299 	if ($end != ")")
    300 		parserr($end, ")")
    301 	end--
    302 
    303 	returntype = oldf = "";
    304 	do {
    305 		if (returntype != "" && oldf != "*")
    306 			returntype = returntype" ";
    307 		returntype = returntype$f;
    308 		oldf = $f;
    309 		f++
    310 	} while (f < (end - 1) && $(f+1) != "(");
    311 	if (f == (end - 1)) {
    312 		parserr($f, "function argument definition (maybe \"(\"?)");
    313 	}
    314 
    315 	funcname=$f
    316 	if (funcalias == "") {
    317 		funcalias=funcname
    318 		sub(/^([^_]+_)*sys_/, "", funcalias)
    319 	}
    320 	f++
    321 
    322 	if ($f != "(")
    323 		parserr($f, ")")
    324 	f++
    325 
    326 	argc=0;
    327 	if (f == end) {
    328 		if ($f != "void")
    329 			parserr($f, "argument definition")
    330 		isvarargs = 0;
    331 		varargc = 0;
    332 		return
    333 	}
    334 
    335 	# some system calls (open() and fcntl()) can accept a variable
    336 	# number of arguments.  If syscalls accept a variable number of
    337 	# arguments, they must still have arguments specified for
    338 	# the remaining argument "positions," because of the way the
    339 	# kernel system call argument handling works.
    340 	#
    341 	# Indirect system calls, e.g. syscall(), are exceptions to this
    342 	# rule, since they are handled entirely by machine-dependent code
    343 	# and do not need argument structures built.
    344 
    345 	isvarargs = 0;
    346 	while (f <= end) {
    347 		if ($f == "...") {
    348 			f++;
    349 			isvarargs = 1;
    350 			varargc = argc;
    351 			continue;
    352 		}
    353 		argc++
    354 		argtype[argc]=""
    355 		oldf=""
    356 		while (f < end && $(f+1) != ",") {
    357 			if (argtype[argc] != "" && oldf != "*")
    358 				argtype[argc] = argtype[argc]" ";
    359 			argtype[argc] = argtype[argc]$f;
    360 			oldf = $f;
    361 			f++
    362 		}
    363 		if (argtype[argc] == "")
    364 			parserr($f, "argument definition")
    365 		argname[argc]=$f;
    366 		f += 2;			# skip name, and any comma
    367 	}
    368 	# must see another argument after varargs notice.
    369 	if (isvarargs) {
    370 		if (argc == varargc && $2 != "INDIR")
    371 			parserr($f, "argument definition")
    372 	} else
    373 		varargc = argc;
    374 }
    375 function putent(nodefs, compatwrap) {
    376 	# output syscall declaration for switch table.  INDIR functions
    377 	# get none, since they always have sys_nosys() for their table
    378 	# entries.
    379 	if (nodefs != "INDIR") {
    380 		prototype = "(struct proc *, void *, register_t *)"
    381 		if (compatwrap == "")
    382 			printf("int\t%s%s;\n", funcname,
    383 			    prototype) > sysprotos
    384 		else
    385 			printf("int\t%s_%s%s;\n", compatwrap, funcname,
    386 			    prototype) > sysprotos
    387 	}
    388 
    389 	# output syscall switch entry
    390 	if (nodefs == "INDIR") {
    391 		printf("\t{ 0, 0, %s,\n\t    %s },\t\t\t/* %d = %s (indir) */\n", \
    392 		    sycall_flags, sys_nosys, syscall, funcalias) > sysent
    393 	} else {
    394 #		printf("\t{ { %d", argc) > sysent
    395 #		for (i = 1; i <= argc; i++) {
    396 #			if (i == 5) 		# wrap the line
    397 #				printf(",\n\t    ") > sysent
    398 #			else
    399 #				printf(", ") > sysent
    400 #			printf("s(%s)", argtypenospc[i]) > sysent
    401 #		}
    402 		printf("\t{ %d, ", argc) > sysent
    403 		if (argc == 0)
    404 			printf("0") > sysent
    405 		else if (compatwrap == "")
    406 			printf("s(struct %s_args)", funcname) > sysent
    407 		else
    408 			printf("s(struct %s_%s_args)", compatwrap,
    409 			    funcname) > sysent
    410 		if (compatwrap == "")
    411 			wfn = sprintf("%s", funcname);
    412 		else
    413 			wfn = sprintf("%s(%s)", compatwrap, funcname);
    414 		printf(", %s,\n\t    %s },", sycall_flags, wfn) > sysent
    415 		for (i = 0; i < (33 - length(wfn)) / 8; i++)
    416 			printf("\t") > sysent
    417 		if (compatwrap == "")
    418 			printf("/* %d = %s */\n", syscall, funcalias) > sysent
    419 		else
    420 			printf("/* %d = %s %s */\n", syscall, compatwrap,
    421 			    funcalias) > sysent
    422 	}
    423 
    424 	# output syscall name for names table
    425 	if (compatwrap == "")
    426 		printf("\t\"%s\",\t\t\t/* %d = %s */\n", funcalias, syscall,
    427 		    funcalias) > sysnamesbottom
    428 	else
    429 		printf("\t\"%s_%s\",\t/* %d = %s %s */\n", compatwrap,
    430 		    funcalias, syscall, compatwrap, funcalias) > sysnamesbottom
    431 
    432 	# output syscall number of header, if appropriate
    433 	if (syscallseen[syscall]) {
    434 		# nop
    435 	} else if (nodefs == "" || nodefs == "NOARGS" || nodefs == "INDIR") {
    436 		# output a prototype, to be used to generate lint stubs in
    437 		# libc.
    438 		printf("/* syscall: \"%s\" ret: \"%s\" args:", funcalias,
    439 		    returntype) > sysnumhdr
    440 		for (i = 1; i <= varargc; i++)
    441 			printf(" \"%s\"", argtype[i]) > sysnumhdr
    442 		if (isvarargs)
    443 			printf(" \"...\"") > sysnumhdr
    444 		printf(" */\n") > sysnumhdr
    445 
    446 		printf("#define\t%s%s\t%d\n\n", constprefix, funcalias,
    447 		    syscall) > sysnumhdr
    448 	} else if (nodefs == "COMPAT") {
    449 		# Just define the syscall number with a comment.  These
    450 		# may be used by compatibility stubs in libc.
    451 		printf("#define\t%s%s_%s\t%d\n\n",
    452 		    constprefix, compatwrap, funcalias, syscall) > sysnumhdr
    453 	} else if (nodefs != "NODEF")
    454 		printf("\t\t\t\t/* %d is %s %s */\n\n", syscall,
    455 		    compatwrap, funcalias) > sysnumhdr
    456 	syscallseen[syscall] = 1
    457 
    458 	# output syscall argument structure, if it has arguments
    459 	if (argc != 0 && nodefs != "NOARGS" && nodefs != "INDIR") {
    460 		if (compatwrap == "")
    461 			printf("\nstruct %s_args {\n", funcname) > sysarghdr
    462 		else
    463 			printf("\nstruct %s_%s_args {\n", compatwrap,
    464 			    funcname) > sysarghdr
    465 		for (i = 1; i <= argc; i++)
    466 			printf("\tsyscallarg(%s) %s;\n", argtype[i],
    467 			    argname[i]) > sysarghdr
    468 		printf("};\n") > sysarghdr
    469 	}
    470 }
    471 $2 == "STD" {
    472 	parseline()
    473 	putent("", "");
    474 	syscall++
    475 	next
    476 }
    477 $2 == "NODEF" || $2 == "NOARGS" || $2 == "INDIR" {
    478 	parseline()
    479 	putent($2, "")
    480 	syscall++
    481 	next
    482 }
    483 $2 == "OBSOL" || $2 == "UNIMPL" || $2 == "EXCL" {
    484 	if ($2 == "OBSOL")
    485 		comment="obsolete"
    486 	else if ($2 == "EXCL")
    487 		comment="excluded"
    488 	else
    489 		comment="unimplemented"
    490 	for (i = 3; i <= NF; i++)
    491 		comment=comment " " $i
    492 
    493 	printf("\t{ 0, 0, 0,\n\t    %s },\t\t\t/* %d = %s */\n", \
    494 	    sys_nosys, syscall, comment) > sysent
    495 	printf("\t\"#%d (%s)\",\t\t/* %d = %s */\n", \
    496 	    syscall, comment, syscall, comment) > sysnamesbottom
    497 	if ($2 != "UNIMPL")
    498 		printf("\t\t\t\t/* %d is %s */\n", syscall, comment) > sysnumhdr
    499 	syscall++
    500 	next
    501 }
    502 {
    503 	for (i = 1; i <= ncompat; i++) {
    504 		if ($2 == compat_upper[i]) {
    505 			parseline();
    506 			putent("COMPAT", compat[i])
    507 			syscall++
    508 			next
    509 		}
    510 	}
    511 	printf("%s: line %d: unrecognized keyword %s\n", infile, NR, $2)
    512 	exit 1
    513 }
    514 END {
    515 	maxsyscall = syscall
    516 	if (nsysent) {
    517 		if (syscall > nsysent) {
    518 			printf("%s: line %d: too many syscalls [%d > %d]\n", infile, NR, syscall, nsysent)
    519 			exit 1
    520 		}
    521 		while (syscall < nsysent) {
    522 			printf("\t{ 0, 0, 0,\n\t    %s },\t\t\t/* %d = filler */\n", \
    523 			    sys_nosys, syscall) > sysent
    524 			syscall++
    525 		}
    526 	}
    527 	printf("};\n\n") > sysent
    528 	printf("};\n") > sysnamesbottom
    529 	printf("#define\t%sMAXSYSCALL\t%d\n", constprefix, maxsyscall) > sysnumhdr
    530 	if (nsysent)
    531 		printf("#define\t%sNSYSENT\t%d\n", constprefix, nsysent) > sysnumhdr
    532 } '
    533 
    534 cat $sysprotos >> $sysarghdr
    535 echo "#endif /* _${constprefix}_SYSCALLARGS_H_ */" >> $sysarghdr
    536 cat $sysdcl $sysent > $syssw
    537 cat $sysnamesbottom >> $sysnames
    538 
    539 #chmod 444 $sysnames $sysnumhdr $syssw
    540