Home | History | Annotate | Line # | Download | only in kern
makesyscalls.sh revision 1.44
      1 #! /bin/sh -
      2 #	$NetBSD: makesyscalls.sh,v 1.44 2001/01/01 16:41:43 jdolecek 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 	if ($NF != "}") {
    269 		funcalias=$NF
    270 		end=NF-1
    271 	} else {
    272 		funcalias=""
    273 		end=NF
    274 	}
    275 	if ($f ~ /^[a-z0-9_]*$/) {	# allow syscall alias
    276 		funcalias=$f
    277 		f++
    278 	}
    279 	if ($f != "{")
    280 		parserr($f, "{")
    281 	f++
    282 	if ($end != "}")
    283 		parserr($end, "}")
    284 	end--
    285 	if ($end != ";")
    286 		parserr($end, ";")
    287 	end--
    288 	if ($end != ")")
    289 		parserr($end, ")")
    290 	end--
    291 
    292 	returntype = oldf = "";
    293 	do {
    294 		if (returntype != "" && oldf != "*")
    295 			returntype = returntype" ";
    296 		returntype = returntype$f;
    297 		oldf = $f;
    298 		f++
    299 	} while (f < (end - 1) && $(f+1) != "(");
    300 	if (f == (end - 1)) {
    301 		parserr($f, "function argument definition (maybe \"(\"?)");
    302 	}
    303 
    304 	funcname=$f
    305 	if (funcalias == "") {
    306 		funcalias=funcname
    307 		sub(/^([^_]+_)*sys_/, "", funcalias)
    308 	}
    309 	f++
    310 
    311 	if ($f != "(")
    312 		parserr($f, ")")
    313 	f++
    314 
    315 	argc=0;
    316 	if (f == end) {
    317 		if ($f != "void")
    318 			parserr($f, "argument definition")
    319 		isvarargs = 0;
    320 		varargc = 0;
    321 		return
    322 	}
    323 
    324 	# some system calls (open() and fcntl()) can accept a variable
    325 	# number of arguments.  If syscalls accept a variable number of
    326 	# arguments, they must still have arguments specified for
    327 	# the remaining argument "positions," because of the way the
    328 	# kernel system call argument handling works.
    329 	#
    330 	# Indirect system calls, e.g. syscall(), are exceptions to this
    331 	# rule, since they are handled entirely by machine-dependent code
    332 	# and do not need argument structures built.
    333 
    334 	isvarargs = 0;
    335 	while (f <= end) {
    336 		if ($f == "...") {
    337 			f++;
    338 			isvarargs = 1;
    339 			varargc = argc;
    340 			continue;
    341 		}
    342 		argc++
    343 		argtype[argc]=""
    344 		oldf=""
    345 		while (f < end && $(f+1) != ",") {
    346 			if (argtype[argc] != "" && oldf != "*")
    347 				argtype[argc] = argtype[argc]" ";
    348 			argtype[argc] = argtype[argc]$f;
    349 			oldf = $f;
    350 			f++
    351 		}
    352 		if (argtype[argc] == "")
    353 			parserr($f, "argument definition")
    354 		argname[argc]=$f;
    355 		f += 2;			# skip name, and any comma
    356 	}
    357 	# must see another argument after varargs notice.
    358 	if (isvarargs) {
    359 		if (argc == varargc && $2 != "INDIR")
    360 			parserr($f, "argument definition")
    361 	} else
    362 		varargc = argc;
    363 }
    364 function putent(nodefs, compatwrap) {
    365 	# output syscall declaration for switch table.  INDIR functions
    366 	# get none, since they always have sys_nosys() for their table
    367 	# entries.
    368 	if (nodefs != "INDIR") {
    369 		prototype = "(struct proc *, void *, register_t *)"
    370 		if (compatwrap == "")
    371 			printf("int\t%s%s;\n", funcname,
    372 			    prototype) > sysprotos
    373 		else
    374 			printf("int\t%s_%s%s;\n", compatwrap, funcname,
    375 			    prototype) > sysprotos
    376 	}
    377 
    378 	# output syscall switch entry
    379 	if (nodefs == "INDIR") {
    380 		printf("\t{ 0, 0,\n\t    sys_nosys },\t\t\t/* %d = %s (indir) */\n", \
    381 		    syscall, funcalias) > sysent
    382 	} else {
    383 #		printf("\t{ { %d", argc) > sysent
    384 #		for (i = 1; i <= argc; i++) {
    385 #			if (i == 5) 		# wrap the line
    386 #				printf(",\n\t    ") > sysent
    387 #			else
    388 #				printf(", ") > sysent
    389 #			printf("s(%s)", argtypenospc[i]) > sysent
    390 #		}
    391 		printf("\t{ %d, ", argc) > sysent
    392 		if (argc == 0)
    393 			printf("0") > sysent
    394 		else if (compatwrap == "")
    395 			printf("s(struct %s_args)", funcname) > sysent
    396 		else
    397 			printf("s(struct %s_%s_args)", compatwrap,
    398 			    funcname) > sysent
    399 		if (compatwrap == "")
    400 			wfn = sprintf("%s", funcname);
    401 		else
    402 			wfn = sprintf("%s(%s)", compatwrap, funcname);
    403 		printf(",\n\t    %s },", wfn) > sysent
    404 		for (i = 0; i < (33 - length(wfn)) / 8; i++)
    405 			printf("\t") > sysent
    406 		if (compatwrap == "")
    407 			printf("/* %d = %s */\n", syscall, funcalias) > sysent
    408 		else
    409 			printf("/* %d = %s %s */\n", syscall, compatwrap,
    410 			    funcalias) > sysent
    411 	}
    412 
    413 	# output syscall name for names table
    414 	if (compatwrap == "")
    415 		printf("\t\"%s\",\t\t\t/* %d = %s */\n", funcalias, syscall,
    416 		    funcalias) > sysnamesbottom
    417 	else
    418 		printf("\t\"%s_%s\",\t/* %d = %s %s */\n", compatwrap,
    419 		    funcalias, syscall, compatwrap, funcalias) > sysnamesbottom
    420 
    421 	# output syscall number of header, if appropriate
    422 	if (syscallseen[syscall]) {
    423 		# nop
    424 	} else if (nodefs == "" || nodefs == "NOARGS" || nodefs == "INDIR") {
    425 		# output a prototype, to be used to generate lint stubs in
    426 		# libc.
    427 		printf("/* syscall: \"%s\" ret: \"%s\" args:", funcalias,
    428 		    returntype) > sysnumhdr
    429 		for (i = 1; i <= varargc; i++)
    430 			printf(" \"%s\"", argtype[i]) > sysnumhdr
    431 		if (isvarargs)
    432 			printf(" \"...\"") > sysnumhdr
    433 		printf(" */\n") > sysnumhdr
    434 
    435 		printf("#define\t%s%s\t%d\n\n", constprefix, funcalias,
    436 		    syscall) > sysnumhdr
    437 	} else if (nodefs == "COMPAT") {
    438 		# Just define the syscall number with a comment.  These
    439 		# may be used by compatibility stubs in libc.
    440 		printf("#define\t%s%s_%s\t%d\n\n",
    441 		    constprefix, compatwrap, funcalias, syscall) > sysnumhdr
    442 	} else if (nodefs != "NODEF")
    443 		printf("\t\t\t\t/* %d is %s %s */\n\n", syscall,
    444 		    compatwrap, funcalias) > sysnumhdr
    445 	syscallseen[syscall] = 1
    446 
    447 	# output syscall argument structure, if it has arguments
    448 	if (argc != 0 && nodefs != "NOARGS" && nodefs != "INDIR") {
    449 		if (compatwrap == "")
    450 			printf("\nstruct %s_args {\n", funcname) > sysarghdr
    451 		else
    452 			printf("\nstruct %s_%s_args {\n", compatwrap,
    453 			    funcname) > sysarghdr
    454 		for (i = 1; i <= argc; i++)
    455 			printf("\tsyscallarg(%s) %s;\n", argtype[i],
    456 			    argname[i]) > sysarghdr
    457 		printf("};\n") > sysarghdr
    458 	}
    459 }
    460 $2 == "STD" {
    461 	parseline()
    462 	putent("", "");
    463 	syscall++
    464 	next
    465 }
    466 $2 == "NODEF" || $2 == "NOARGS" || $2 == "INDIR" {
    467 	parseline()
    468 	putent($2, "")
    469 	syscall++
    470 	next
    471 }
    472 $2 == "OBSOL" || $2 == "UNIMPL" || $2 == "EXCL" {
    473 	if ($2 == "OBSOL")
    474 		comment="obsolete"
    475 	else if ($2 == "EXCL")
    476 		comment="excluded"
    477 	else
    478 		comment="unimplemented"
    479 	for (i = 3; i <= NF; i++)
    480 		comment=comment " " $i
    481 
    482 	printf("\t{ 0, 0,\n\t    sys_nosys },\t\t\t/* %d = %s */\n", \
    483 	    syscall, comment) > sysent
    484 	printf("\t\"#%d (%s)\",\t\t/* %d = %s */\n", \
    485 	    syscall, comment, syscall, comment) > sysnamesbottom
    486 	if ($2 != "UNIMPL")
    487 		printf("\t\t\t\t/* %d is %s */\n", syscall, comment) > sysnumhdr
    488 	syscall++
    489 	next
    490 }
    491 {
    492 	for (i = 1; i <= ncompat; i++) {
    493 		if ($2 == compat_upper[i]) {
    494 			parseline();
    495 			putent("COMPAT", compat[i])
    496 			syscall++
    497 			next
    498 		}
    499 	}
    500 	printf("%s: line %d: unrecognized keyword %s\n", infile, NR, $2)
    501 	exit 1
    502 }
    503 END {
    504 	maxsyscall = syscall
    505 	if (nsysent) {
    506 		if (syscall > nsysent) {
    507 			printf("%s: line %d: too many syscalls\n", infile, NR)
    508 			exit 1
    509 		}
    510 		while (syscall < nsysent) {
    511 			printf("\t{ 0, 0,\n\t    sys_nosys },\t\t\t/* %d = filler */\n", \
    512 			    syscall) > sysent
    513 			syscall++
    514 		}
    515 	}
    516 	printf("};\n\n") > sysent
    517 	printf("};\n") > sysnamesbottom
    518 	printf("#define\t%sMAXSYSCALL\t%d\n", constprefix, maxsyscall) > sysnumhdr
    519 	if (nsysent)
    520 		printf("#define\t%sNSYSENT\t%d\n", constprefix, nsysent) > sysnumhdr
    521 } '
    522 
    523 cat $sysprotos >> $sysarghdr
    524 echo "#endif /* _${constprefix}_SYSCALLARGS_H_ */" >> $sysarghdr
    525 cat $sysdcl $sysent > $syssw
    526 cat $sysnamesbottom >> $sysnames
    527 
    528 #chmod 444 $sysnames $sysnumhdr $syssw
    529