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