Home | History | Annotate | Line # | Download | only in kern
makesyscalls.sh revision 1.59.52.3
      1 #! /bin/sh -
      2 #	$NetBSD: makesyscalls.sh,v 1.59.52.3 2008/02/18 21:06:46 mjf 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 	argalign=0;
    355 	if (f == end) {
    356 		if ($f != "void")
    357 			parserr($f, "argument definition")
    358 		isvarargs = 0;
    359 		varargc = 0;
    360 		return
    361 	}
    362 
    363 	# some system calls (open() and fcntl()) can accept a variable
    364 	# number of arguments.  If syscalls accept a variable number of
    365 	# arguments, they must still have arguments specified for
    366 	# the remaining argument "positions," because of the way the
    367 	# kernel system call argument handling works.
    368 	#
    369 	# Indirect system calls, e.g. syscall(), are exceptions to this
    370 	# rule, since they are handled entirely by machine-dependent code
    371 	# and do not need argument structures built.
    372 
    373 	isvarargs = 0;
    374 	while (f <= end) {
    375 		if ($f == "...") {
    376 			f++;
    377 			isvarargs = 1;
    378 			varargc = argc;
    379 			continue;
    380 		}
    381 		argc++
    382 		argtype[argc]=""
    383 		oldf=""
    384 		while (f < end && $(f+1) != ",") {
    385 			if (argtype[argc] != "" && oldf != "*")
    386 				argtype[argc] = argtype[argc]" ";
    387 			argtype[argc] = argtype[argc]$f;
    388 			oldf = $f;
    389 			f++
    390 		}
    391 		if (argtype[argc] == "")
    392 			parserr($f, "argument definition")
    393 		if (argtype[argc] == "off_t") {
    394 			if ((argalign % 2) != 0 &&
    395 			    funcname != "sys_posix_fadvise") # XXX for now
    396 				parserr($f, "a padding argument")
    397 		} else {
    398 			argalign++;
    399 		}
    400 		argname[argc]=$f;
    401 		f += 2;			# skip name, and any comma
    402 	}
    403 	# must see another argument after varargs notice.
    404 	if (isvarargs) {
    405 		if (argc == varargc)
    406 			parserr($f, "argument definition")
    407 	} else
    408 		varargc = argc;
    409 }
    410 
    411 function printproto(wrap) {
    412 	printf("/* syscall: \"%s%s\" ret: \"%s\" args:", wrap, funcalias,
    413 	    returntype) > sysnumhdr
    414 	for (i = 1; i <= varargc; i++)
    415 		printf(" \"%s\"", argtype[i]) > sysnumhdr
    416 	if (isvarargs)
    417 		printf(" \"...\"") > sysnumhdr
    418 	printf(" */\n") > sysnumhdr
    419 	printf("#define\t%s%s%s\t%d\n\n", constprefix, wrap, funcalias,
    420 	    syscall) > sysnumhdr
    421 }
    422 
    423 function putent(type, compatwrap) {
    424 	# output syscall declaration for switch table.
    425 	if (compatwrap == "")
    426 		compatwrap_ = ""
    427 	else
    428 		compatwrap_ = compatwrap "_"
    429 	if (argc == 0)
    430 		arg_type = "void";
    431 	else {
    432 		arg_type = "struct " compatwrap_ funcname "_args";
    433 	}
    434 	proto = "int\t" compatwrap_ funcname "(struct lwp *, const " \
    435 	    arg_type " *, register_t *);\n"
    436 	if (sysmap[proto] != 1) {
    437 		sysmap[proto] = 1;
    438 		print proto > sysprotos;
    439 	}
    440 
    441 	# output syscall switch entry
    442 	printf("\t{ ") > sysent
    443 	if (argc == 0) {
    444 		printf("0, 0, ") > sysent
    445 	} else {
    446 		printf("ns(struct %s%s_args), ", compatwrap_, funcname) > sysent
    447 	}
    448 	if (compatwrap == "")
    449 		wfn = "(sy_call_t *)" funcname;
    450 	else
    451 		wfn = "(sy_call_t *)" compatwrap "(" funcname ")";
    452 	printf("%s,\n\t    %s },", sycall_flags, wfn) > sysent
    453 	for (i = 0; i < (33 - length(wfn)) / 8; i++)
    454 		printf("\t") > sysent
    455 	printf("/* %d = %s%s */\n", syscall, compatwrap_, funcalias) > sysent
    456 
    457 	# output syscall name for names table
    458 	printf("\t/* %3d */\t\"%s%s\",\n", syscall, compatwrap_, funcalias) \
    459 	    > sysnamesbottom
    460 
    461 	# output syscall number of header, if appropriate
    462 	if (type == "STD" || type == "NOARGS" || type == "INDIR") {
    463 		# output a prototype, to be used to generate lint stubs in
    464 		# libc.
    465 		printproto("")
    466 	} else if (type == "COMPAT") {
    467 		# Just define the syscall number with a comment.  These
    468 		# may be used by compatibility stubs in libc.
    469 		printproto(compatwrap_)
    470 	}
    471 
    472 	# output syscall argument structure, if it has arguments
    473 	if (argc != 0) {
    474 		printf("\nstruct %s%s_args", compatwrap_, funcname) > sysarghdr
    475 		if (type != "NOARGS") {
    476 			print " {" >sysarghdr;
    477 			for (i = 1; i <= argc; i++)
    478 				printf("\tsyscallarg(%s) %s;\n", argtype[i],
    479 				    argname[i]) > sysarghdr
    480 			printf "}" >sysarghdr;
    481 		}
    482 		printf(";\n") > sysarghdr
    483 		if (type != "NOARGS" && type != "INDIR") {
    484 			printf("check_syscall_args(%s%s)\n", compatwrap_,
    485 			    funcname) >sysarghdr
    486 		}
    487 	}
    488 }
    489 $2 == "STD" || $2 == "NODEF" || $2 == "NOARGS" || $2 == "INDIR" {
    490 	parseline()
    491 	putent($2, "")
    492 	syscall++
    493 	next
    494 }
    495 $2 == "OBSOL" || $2 == "UNIMPL" || $2 == "EXCL" {
    496 	if ($2 == "OBSOL")
    497 		comment="obsolete"
    498 	else if ($2 == "EXCL")
    499 		comment="excluded"
    500 	else
    501 		comment="unimplemented"
    502 	for (i = 3; i <= NF; i++)
    503 		comment=comment " " $i
    504 
    505 	printf("\t{ 0, 0, 0,\n\t    %s },\t\t\t/* %d = %s */\n", \
    506 	    sys_nosys, syscall, comment) > sysent
    507 	printf("\t/* %3d */\t\"#%d (%s)\",\n", syscall, syscall, comment) \
    508 	    > sysnamesbottom
    509 	if ($2 != "UNIMPL")
    510 		printf("\t\t\t\t/* %d is %s */\n", syscall, comment) > sysnumhdr
    511 	syscall++
    512 	next
    513 }
    514 {
    515 	for (i = 1; i <= ncompat; i++) {
    516 		if ($2 == compat_upper[i]) {
    517 			parseline();
    518 			putent("COMPAT", compat[i])
    519 			syscall++
    520 			next
    521 		}
    522 	}
    523 	printf("%s: line %d: unrecognized keyword %s\n", infile, NR, $2)
    524 	exit 1
    525 }
    526 END {
    527 	maxsyscall = syscall
    528 	if (nsysent) {
    529 		if (syscall > nsysent) {
    530 			printf("%s: line %d: too many syscalls [%d > %d]\n", infile, NR, syscall, nsysent)
    531 			exit 1
    532 		}
    533 		while (syscall < nsysent) {
    534 			printf("\t{ 0, 0, 0,\n\t    %s },\t\t\t/* %d = filler */\n", \
    535 			    sys_nosys, syscall) > sysent
    536 			syscall++
    537 		}
    538 	}
    539 	printf("};\n\n") > sysent
    540 	printf("};\n") > sysnamesbottom
    541 	printf("#define\t%sMAXSYSCALL\t%d\n", constprefix, maxsyscall) > sysnumhdr
    542 	if (nsysent)
    543 		printf("#define\t%sNSYSENT\t%d\n", constprefix, nsysent) > sysnumhdr
    544 } '
    545 
    546 cat $sysprotos >> $sysarghdr
    547 echo "#endif /* _${constprefix}SYSCALL_H_ */" >> $sysnumhdr
    548 echo "#endif /* _${constprefix}SYSCALLARGS_H_ */" >> $sysarghdr
    549 cat $sysdcl $sysent > $syssw
    550 cat $sysnamesbottom >> $sysnames
    551 
    552 #chmod 444 $sysnames $sysnumhdr $syssw
    553