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