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