# $NetBSD: makesyscalls.sh,v 1.187 2023/07/28 18:19:01 christos Exp $
#
# Copyright (c) 1994, 1996, 2000 Christopher G. Demetriou
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
# must display the following acknowledgement:
# This product includes software developed for the NetBSD Project
# by Christopher G. Demetriou.
# 4. The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# @(#)makesyscalls.sh 8.1 (Berkeley) 6/10/93
set -e
case $# in
2) ;;
*) echo "Usage: $0 config-file input-file" 1>&2
exit 1
;;
esac
# the config file sets the following variables:
# sysalign check for alignment of off_t/dev_t/time_t
# sysnames the syscall names file
# sysnumhdr the syscall numbers file
# syssw the syscall switch file
# sysautoload the syscall autoload definitions file
# sysarghdr the syscall argument struct definitions
# sysarghdrextra extra stuff dumped into sysarghdr
# systrace the dtrace definitions
# compatopts those syscall types that are for 'compat' syscalls
# switchname the name for the 'struct sysent' we define
# namesname the name for the 'const char *[]' we define
# constprefix the prefix for the system call constants
# emulname the emulation name
# registertype the type for register_t
# nsysent the size of the sysent table
# sys_nosys [optional] name of function called for unsupported
# syscalls, if not sys_nosys()
# maxsysargs [optional] the maximum number or arguments
# rumpcalls ???
# rumpcallshdr ???
# rumpsysmap ???
#
# NOTE THAT THIS makesyscalls.sh DOES NOT SUPPORT 'SYSLIBCOMPAT'.
# source the config file.
sys_nosys="sys_nosys" # default is sys_nosys(), if not specified otherwise
maxsysargs=8 # default limit is 8 (32bit) arguments
systrace="/dev/null"
sysautoload="/dev/null"
rumpcalls="/dev/null"
rumpcallshdr="/dev/null"
rumpsysmap="/dev/null"
rumpsysent="rumpsysent.tmp"
rumpnoflags="\n\t\t.sy_flags = SYCALL_NOSYS,"
rumpnosys="(sy_call_t *)(void *)rumpns_enosys"
rumpnomodule="(sy_call_t *)(void *)rumpns_sys_nomodule"
addsuffix()
{
if [ "$1" = "/dev/null" -o -z "$2" ]; then
echo $1
else
echo $1.$2
fi
}
fail=false
case "${nsysent:-0}" in
*[!0-9]*) errmsg "Non numeric value for nsysent:" "${nsysent}";;
esac
case "${maxsysargs:-0}" in
*[!0-9]*) errmsg "Non numeric value for maxsysargs:" "${maxsysargs}";;
esac
$fail && exit 1
# Awk program (must support nawk extensions)
# Use "awk" at Berkeley, "nawk" or "gawk" elsewhere.
awk=${AWK:-awk}
# Does this awk have a "toupper" function?
have_toupper="$($awk 'BEGIN { print toupper("true"); exit; }' 2>/dev/null)"
# If this awk does not define "toupper" then define our own.
if [ "$have_toupper" = TRUE ] ; then
# Used awk (GNU awk or nawk) provides it
toupper=
else
# Provide our own toupper()
toupper='
function toupper(str) {
_toupper_cmd = "echo "str" |tr a-z A-Z"
_toupper_cmd | getline _toupper_str;
close(_toupper_cmd);
return _toupper_str;
}'
fi
# before handing it off to awk, make a few adjustments:
# (1) insert spaces around {, }, (, ), *, |, and commas.
# (2) get rid of any and all dollar signs (so that rcs id use safe)
#
# The awk script will deal with blank lines and lines that
# start with the comment character (';').
sed -e '
s/\$//g
:join
/\\$/{a\
N
s/\\\n//
b join
}
2,${
/^#/!s/\([{}()*,|]\)/ \1 /g
}
' < $2 | $awk "
$toupper
BEGIN {
# Create a NetBSD tag that does not get expanded when checking
# this script out of CVS. (This part of the awk script is in a
# shell double-quoted string, so the backslashes are eaten by
# the shell.)
tag = \"\$\" \"NetBSD\" \"\$\"
# to allow nested #if/#else/#endif sets
savedepth = 0
# to track already processed syscalls
printf "/* %s */\n\n", tag > sysdcl
printf "/*\n * System call switch table.\n *\n" > sysdcl
printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysdcl
ncompat = split(compatopts,compat)
for (i = 1; i <= ncompat; i++) {
compat_upper[i] = toupper(compat[i])
printf "/* %s */\n\n", tag > sysnames
printf "/*\n * System call names.\n *\n" > sysnames
printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysnames
printf "\n/*\n * System call prototypes.\n */\n\n" > sysprotos
if (haverumpcalls)
printf("#ifndef RUMP_CLIENT\n") > sysprotos
printf "/* %s */\n\n", tag > sysnumhdr
printf "/*\n * System call numbers.\n *\n" > sysnumhdr
printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysnumhdr
printf "/* %s */\n\n", tag > sysarghdr
printf "/*\n * System call argument lists.\n *\n" > sysarghdr
printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysarghdr
printf "/* %s */\n\n", tag > sysautoload
printf "/*\n * System call autoload table.\n *\n" > sysautoload
printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysautoload
printf "/* %s */\n\n", tag > rumpcalls
printf "/*\n * System call vector and marshalling for rump.\n *\n" > rumpcalls
printf " * DO NOT EDIT-- this file is automatically generated.\n" > rumpcalls
printf "/* %s */\n\n", tag > rumpcallshdr
printf "/*\n * System call protos in rump namespace.\n *\n" > rumpcallshdr
printf " * DO NOT EDIT-- this file is automatically generated.\n" > rumpcallshdr
printf "/* %s */\n\n", tag > systrace
printf "/*\n * System call argument to DTrace register array conversion.\n *\n" > systrace
printf " * DO NOT EDIT-- this file is automatically generated.\n" > systrace
}
NR == 1 {
sub(/ $/, "")
printf " * created from%s\n */\n\n", $0 > sysdcl
printf "#include <sys/cdefs.h>\n__KERNEL_RCSID(0, \"%s\");\n\n", tag > sysdcl
printf " * created from%s\n */\n\n", $0 > sysnames
printf "#include <sys/cdefs.h>\n__KERNEL_RCSID(0, \"%s\");\n\n", tag > sysnames
# compat types from syscalls.master. this is slightly ugly,
# but given that we have so few compats from over 17 years,
# a more complicated solution is not currently warranted.
uncompattypes["struct timeval50"] = "struct timeval";
uncompattypes["struct timespec50"] = "struct timespec";
uncompattypes["struct stat30"] = "struct stat";
uncompattypes["struct kevent100"] = "struct kevent";
# XXX: technically we do not want to have conditionals in rump,
# but it is easier to just let the cpp handle them than try to
# figure out what we want here in this script
print > rumpsysent
next
}
syscall != $1 {
printf "%s: line %d: syscall number out of sync at %d\n", \
infile, NR, syscall
printf "line is:\n"
print
exit 1
}
function isarg64(type) {
gsub("netbsd32_", "", type);
return type == "quad_t" || type == "off_t" \
|| type == "dev_t" || type == "time_t";
}
function parserr(was, wanted) {
printf "%s: line %d: unexpected %s (expected <%s>)\n", \
infile, NR, was, wanted
printf "line is:\n"
print
exit 1
}
function fillerpsysent(syscall, flags, name, comment) {
return sprintf("\t{%s\n\t\t.sy_call = %s,\n\t},\t\t/* %d = filler */",\
flags, name, syscall, comment);
}
function parseline() {
f=3 # toss number and type
if ($2 == "INDIR")
sycall_flags="SYCALL_INDIRECT"
else
sycall_flags="0"
if ($NF != "}") {
funcalias=$NF
end=NF-1
} else {
funcalias=""
end=NF
}
if ($f == "INDIR") { # allow for "NOARG INDIR"
sycall_flags = "SYCALL_INDIRECT | " sycall_flags
f++
}
if ($f == "MODULAR") { # registered at runtime
modular = 1
f++
modname = $f
f++
} else {
modular = 0;
}
if ($f == "RUMP") {
rumpable = 1
f++
} else {
rumpable = 0
}
if ($f ~ /^[a-z0-9_]*$/) { # allow syscall alias
funcalias=$f
f++
}
if ($f != "{")
parserr($f, "{")
f++
if ($end != "}")
parserr($end, "}")
end--
if ($end != ";")
parserr($end, ";")
end--
if ($end != ")")
parserr($end, ")")
end--
returntype = oldf = "";
do {
if (returntype != "" && oldf != "*")
returntype = returntype" ";
returntype = returntype$f;
oldf = $f;
f++
} while ($f != "|" && f < (end-1))
if (f == (end - 1)) {
parserr($f, "function argument definition (maybe \"|\"?)");
}
f++
if ($f != "|") {
parserr($f, "function name delimiter (maybe \"|\"?)");
}
f++
fbase=$f
# pipe is special in how to returns its values.
# So just generate it manually if present.
if (rumpable == 1 && fbase == "pipe") {
rumpable = 0;
rumphaspipe = 1;
}
# some system calls (open() and fcntl()) can accept a variable
# number of arguments. If syscalls accept a variable number of
# arguments, they must still have arguments specified for
# the remaining argument "positions," because of the way the
# kernel system call argument handling works.
#
# Indirect system calls, e.g. syscall(), are exceptions to this
# rule, since they are handled entirely by machine-dependent code
# and do not need argument structures built.
isvarargs = 0;
args64 = 0;
ptr = 0;
while (f <= end) {
if ($f == "...") {
f++;
isvarargs = 1;
varargc = argc;
continue;
}
argc++
argtype[argc]=""
oldf=""
while (f < end && $(f+1) != ",") {
if (argtype[argc] != "" && oldf != "*")
argtype[argc] = argtype[argc]" ";
argtype[argc] = argtype[argc]$f;
oldf = $f;
f++
}
if (argtype[argc] == "")
parserr($f, "argument definition")
if (argtype[argc] == "off_t" \
|| argtype[argc] == "dev_t" \
|| argtype[argc] == "time_t") {
if ((argalign % 2) != 0 && sysalign &&
funcname != "sys_posix_fadvise") # XXX for now
parserr($f, "a padding argument")
} else {
argalign++;
}
if (isarg64(argtype[argc])) {
if (sycall_flags == "0")
sycall_flags = "SYCALL_ARG"argc-1"_64";
else
sycall_flags = "SYCALL_ARG"argc-1"_64 | " sycall_flags;
args64++;
}
if (index(argtype[argc], "*") != 0 && ptr == 0) {
if (sycall_flags == "0")
sycall_flags = "SYCALL_ARG_PTR";
else
sycall_flags = "SYCALL_ARG_PTR | " sycall_flags;
ptr = 1;
}
argname[argc]=$f;
f += 2; # skip name, and any comma
}
if (args64 > 0)
sycall_flags = "SYCALL_NARGS64_VAL("args64") | " sycall_flags;
# must see another argument after varargs notice.
if (isvarargs) {
if (argc == varargc)
parserr($f, "argument definition")
} else
varargc = argc;
}
function printproto(wrap) {
printf("/* syscall: \"%s%s\" ret: \"%s\" args:", wrap, funcalias,
returntype) > sysnumhdr
for (i = 1; i <= varargc; i++)
printf(" \"%s\"", argtype[i]) > sysnumhdr
if (isvarargs)
printf(" \"...\"") > sysnumhdr
printf(" */\n") > sysnumhdr
printf("#define\t%s%s%s\t%d\n\n", constprefix, wrap, funcalias,
syscall) > sysnumhdr
# output entry for syscall autoload table, if modular
if (modular) {
printf("\t { %s%s%s, \"%s\" },\n", constprefix, wrap,
funcalias, modname) > sysautoloadbottom
}
# rumpalooza
if (!rumpable)
return
# accumulate fbases we have seen. we want the last
# occurrence for the default __RENAME()
seen = funcseen[realname]
funcseen[realname] = rumpfname
# special case for mknod as type of last argument changed from
# uint32_t to dev_t
if ((seen && fbase != "mknod") || (!seen && fbase == "mknod"))
return
# output syscall number of header, if appropriate
if (type == "STD" || type == "NOARGS" || type == "INDIR" || \
type == "NOERR") {
# output a prototype, to be used to generate lint stubs in
# libc.
printproto("")
} else if (type == "COMPAT" || type == "EXTERN") {
# Just define the syscall number with a comment. These
# may be used by compatibility stubs in libc.
printproto(compatwrap_)
}
# output syscall argument structure, if it has arguments
if (argc != 0) {
printf("\n") > sysarghdr
if (haverumpcalls && !rumpable)
printf("#ifndef RUMP_CLIENT\n") > sysarghdr
printf("struct %s%s_args", compatwrap_, funcname) > sysarghdr
if (type != "NOARGS") {
print " {" >sysarghdr;
for (i = 1; i <= argc; i++)
printf("\tsyscallarg(%s) %s;\n", argtype[i],
argname[i]) > sysarghdr
printf "}" >sysarghdr;
}
printf(";\n") > sysarghdr
if (type != "NOARGS" && type != "INDIR") {
printf("check_syscall_args(%s%s)\n", compatwrap_,
funcname) >sysarghdr
}
if (haverumpcalls && !rumpable)
printf("#endif /* !RUMP_CLIENT */\n") > sysarghdr
}
if (compatwrap)
printf("#ifdef RUMP_SYS_COMPAT\n") > rumpcalls
# need a local prototype, we export the re-re-named one in .h
printf("%s rump___sysimpl_%s(", returntype, rumpfname) \
> rumpcalls
for (i = 1; i < argc; i++) {
if (argname[i] != "PAD")
printf("%s, ", uncompattype(argtype[i])) > rumpcalls
}
printf("%s);", uncompattype(argtype[argc])) > rumpcalls
printf("\n%s\nrump___sysimpl_%s(", returntype, rumpfname) > rumpcalls
for (i = 1; i < argc; i++) {
if (argname[i] != "PAD")
printf("%s %s, ", uncompattype(argtype[i]), \
argname[i]) > rumpcalls
}
printf("%s %s)\n", uncompattype(argtype[argc]), argname[argc]) \
> rumpcalls
printf("{\n\tregister_t retval[2];\n") > rumpcalls
if (returntype != "void") {
if (type != "NOERR") {
printf("\tint error = 0;\n") > rumpcalls
}
# assume rumpcalls return only integral types
printf("\t%s rv = -1;\n", returntype) > rumpcalls
}
# create the bog-standard, non-renamed public name.
# this way we get e.g. select instead of just __select50
if (fcompat)
printf("__weak_alias(%s,rump___sysimpl_%s);\n", \
fbase, rumpfname) > rumpcalls
print fillerpsysent(syscall, "", sys_stub, comment) > sysent
print fillerpsysent(syscall, rumpnoflags, rumpnosys, comment) > rumpsysent
printf("\t/* %3d */\t\"#%d (%s)\",\n", syscall, syscall, comment) \
> sysnamesbottom
printf("\t/* %3d */\tNULL, /* %s */\n", syscall, comment) \
> sysnamesfriendly
if ($2 != "UNIMPL")
printf("\t\t\t\t/* %d is %s */\n", syscall, comment) > sysnumhdr
syscall++
next
}
$2 == "EXTERN" {
parseline()
putent("EXTERN", "")
syscall++
next
}
{
for (i = 1; i <= ncompat; i++) {
if ($2 == compat_upper[i]) {
parseline();
putent("COMPAT", compat[i])
syscall++
next
}
}
printf("%s: line %d: unrecognized keyword %s\n", infile, NR, $2)
exit 1
}
function sort(arr, n, i, j, t) {
# this is going to be bubble sort because I cannot be bothered to
# write a real sort, this whole script is hopefully going to be
# deprecated before much longer, and performance of a few hundred
# things (even interpreted in awk) should be adequate.
for (i = 1; i <= n; i++) {
for (j = i + 1; j <= n; j++) {
if (arr[j] < arr[i]) {
t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
}
return 0;
}
# print default rump syscall interfaces
# be sure to generate them in a deterministic order, not awk
# hash order as would happen with a plain "for (var in funcseen)"
numfuncseenvars = 0;
for (var in funcseen) {
funcseenvars[++numfuncseenvars] = var;
}
sort(funcseenvars, numfuncseenvars)
for (i = 1; i <= numfuncseenvars; i++) {
var = funcseenvars[i];
printf("#ifndef RUMP_SYS_RENAME_%s\n", \
toupper(var)) > rumpcallshdr
printf("#define RUMP_SYS_RENAME_%s rump___sysimpl_%s\n", \
toupper(var), funcseen[var]) > rumpcallshdr
printf("#endif\n\n") > rumpcallshdr
}