Path: blob/main/sys/tools/syscalls/scripts/init_sysent.lua
39530 views
#!/usr/libexec/flua1--2-- SPDX-License-Identifier: BSD-2-Clause3--4-- Copyright (c) 2024 Tyler Baxter <[email protected]>5-- Copyright (c) 2023 Warner Losh <[email protected]>6-- Copyright (c) 2019 Kyle Evans <[email protected]>7--89-- Setup to be a module, or ran as its own script.10local init_sysent = {}11local script = not pcall(debug.getlocal, 4, 1) -- TRUE if script.12if script then13-- Add library root to the package path.14local path = arg[0]:gsub("/[^/]+.lua$", "")15package.path = package.path .. ";" .. path .. "/../?.lua"16end1718local FreeBSDSyscall = require("core.freebsd-syscall")19local generator = require("tools.generator")2021-- File has not been decided yet; config will decide file. Default defined as22-- /dev/null.23init_sysent.file = "/dev/null"2425function init_sysent.generate(tbl, config, fh)26-- Grab the master system calls table.27local s = tbl.syscalls2829-- Bind the generator to the parameter file.30local gen = generator:new({}, fh)3132-- Write the generated preamble.33gen:preamble("System call switch table.")3435gen:write(tbl.includes)3637-- Newline before and after this line.38gen:write(39"\n#define AS(name) (sizeof(struct name) / sizeof(syscallarg_t))\n")4041-- Write out all the compat directives from compat_options.42for _, v in pairs(config.compat_options) do43gen:write(string.format([[4445#ifdef %s46#define %s(n, name) .sy_narg = n, .sy_call = (sy_call_t *)__CONCAT(%s, name)47#else48#define %s(n, name) .sy_narg = 0, .sy_call = (sy_call_t *)nosys49#endif50]], v.definition, v.flag:lower(), v.prefix, v.flag:lower()))51end52-- Add a newline only if there were compat_options.53if config.compat_options ~= nil then54gen:write("\n")55end5657gen:write(string.format([[58/* The casts are bogus but will do for now. */59struct sysent %s[] = {60]], config.switchname))6162for _, v in pairs(s) do63local c = v:compatLevel()64-- Comment is the function name by default, but may change65-- based on the type of system call.66local comment = v.name6768gen:write(v.prolog);6970-- Handle non-compat:71if v:native() then72gen:write(string.format(73"\t{ .sy_narg = %s, .sy_call = (sy_call_t *)",74v.args_size))75-- Handle SYSMUX flag:76if v.type.SYSMUX then77gen:write(string.format("nosys, " ..78".sy_auevent = AUE_NULL, " ..79".sy_flags = %s, " ..80".sy_thrcnt = SY_THR_STATIC },",81v.cap))82-- Handle NOSTD flag:83elseif v.type.NOSTD then84gen:write(string.format("lkmressys, " ..85".sy_auevent = AUE_NULL, " ..86".sy_flags = %s, " ..87".sy_thrcnt = SY_THR_ABSENT },",88v.cap))89-- Handle rest of non-compat:90else91if v.name == "nosys" or92v.name == "lkmnosys" or93v.name == "sysarch" or94v.name:find("^freebsd") or95v.name:find("^linux") then96gen:write(string.format("%s, " ..97".sy_auevent = %s, " ..98".sy_flags = %s, " ..99".sy_thrcnt = %s },",100v:symbol(), v.audit, v.cap, v.thr))101else102gen:write(string.format("sys_%s, " ..103".sy_auevent = %s, " ..104".sy_flags = %s, " ..105".sy_thrcnt = %s },",106v:symbol(), v.audit, v.cap, v.thr))107end108end109110-- Handle compat (everything >= FREEBSD3):111elseif c >= 3 then112-- Lookup the info for this specific compat option.113local flag, descr114for _, opt in pairs(config.compat_options) do115if opt.compatlevel == c then116flag = opt.flag117flag = flag:lower()118descr = opt.descr119break120end121end122123if v.type.NOSTD then124gen:write(string.format("\t{ " ..125".sy_narg = %s, " ..126".sy_call = (sy_call_t *)%s, " ..127".sy_auevent = %s, " ..128".sy_flags = 0, " ..129".sy_thrcnt = SY_THR_ABSENT },",130"0", "lkmressys", "AUE_NULL"))131else132gen:write(string.format("\t{ %s(%s,%s), " ..133".sy_auevent = %s, " ..134".sy_flags = %s, " ..135".sy_thrcnt = %s },",136flag, v.args_size, v.name, v.audit, v.cap, v.thr))137end138comment = descr .. " " .. v.name139140-- Handle obsolete:141elseif v.type.OBSOL then142gen:write("\t{ " ..143".sy_narg = 0, .sy_call = (sy_call_t *)nosys, " ..144".sy_auevent = AUE_NULL, .sy_flags = 0, " ..145".sy_thrcnt = SY_THR_ABSENT },")146comment = "obsolete " .. v.name147148-- Handle unimplemented:149elseif v.type.UNIMPL then150gen:write("\t{ " ..151".sy_narg = 0, .sy_call = (sy_call_t *)nosys, " ..152".sy_auevent = AUE_NULL, .sy_flags = 0, " ..153".sy_thrcnt = SY_THR_ABSENT },")154-- UNIMPL comment is not different in sysent.155156-- Handle reserved:157elseif v.type.RESERVED then158gen:write("\t{ " ..159".sy_narg = 0, .sy_call = (sy_call_t *)nosys, " ..160".sy_auevent = AUE_NULL, .sy_flags = 0, " ..161".sy_thrcnt = SY_THR_ABSENT },")162comment = "reserved for local use"163end164165gen:write(string.format("\t/* %d = %s */\n", v.num, comment))166end167gen:write(tbl.epilog)168169-- End170gen:write("};\n")171end172173-- Entry of script:174if script then175local config = require("config")176177if #arg < 1 or #arg > 2 then178error("usage: " .. arg[0] .. " syscall.master")179end180181local sysfile, configfile = arg[1], arg[2]182183config.merge(configfile)184config.mergeCompat()185186-- The parsed syscall table.187local tbl = FreeBSDSyscall:new{sysfile = sysfile, config = config}188189init_sysent.file = config.syssw -- change file here190init_sysent.generate(tbl, config, init_sysent.file)191end192193-- Return the module.194return init_sysent195196197