Path: blob/main/sys/tools/syscalls/core/freebsd-syscall.lua
39534 views
--1-- SPDX-License-Identifier: BSD-2-Clause2--3-- Copyright (c) 2024 Tyler Baxter <[email protected]>4-- Copyright (c) 2023 Warner Losh <[email protected]>5-- Copyright (c) 2019 Kyle Evans <[email protected]>6--78local syscall = require("core.syscall")9local util = require("tools.util")1011local FreeBSDSyscall = {}1213FreeBSDSyscall.__index = FreeBSDSyscall1415-- For each compat option in the provided config table, process them and insert16-- them into known_flags for class syscall.17function FreeBSDSyscall:processCompat()18for _, v in pairs(self.config.compat_options) do19if v.stdcompat ~= nil then20local stdcompat = v.stdcompat21v.definition = "COMPAT_" .. stdcompat:upper()22v.compatlevel = tonumber(stdcompat:match("([0-9]+)$"))23v.flag = stdcompat:gsub("FREEBSD", "COMPAT")24v.prefix = stdcompat:lower() .. "_"25v.descr = stdcompat:lower()26end2728-- Add compat option to syscall.known_flags.29table.insert(syscall.known_flags, v.flag)30end31end3233function FreeBSDSyscall:parseSysfile()34local file = self.sysfile35local config = self.config36local commentExpr = "^%s*;.*"3738if file == nil then39return nil, "No file given"40end4142self.syscalls = {}4344local fh, msg = io.open(file)45if fh == nil then46return nil, msg47end4849local incs = ""50local prolog = ""51local first = true52local cpp_warned = false53local s54for line in fh:lines() do55line = line:gsub(commentExpr, "") -- Strip any comments.56-- NOTE: Can't use pure pattern matching here because of57-- the 's' test and this is shorter than a generic pattern58-- matching pattern.59if line == nil or line == "" then60goto skip -- Blank line, skip this line.61elseif s ~= nil then62-- If we have a partial system call object s,63-- then feed it one more line.64if s:add(line) then65-- Append to system call list.66for t in s:iter() do67if t:validate(t.num - 1) then68table.insert(self.syscalls, t)69else70util.abort(1,71"Skipped system call " ..72"at number " .. t.num)73end74end75s = nil76end77elseif line:match("^#%s*include") then78incs = incs .. line .. "\n"79elseif line:match("%%ABI_HEADERS%%") then80local h = self.config.abi_headers81if h ~= nil and h ~= "" then82incs = incs .. h .. "\n"83end84elseif line:match("^#") then85if not cpp_warned then86util.warn("use of non-include cpp " ..87"directives is deprecated")88cpp_warned = true89end90prolog = prolog .. line .. "\n"91else92s = syscall:new()93if first then94self.prolog = prolog95s.prolog = ""96first = false97else98s.prolog = prolog99end100prolog = ""101if s:add(line) then102-- Append to system call list.103for t in s:iter() do104if t:validate(t.num - 1) then105table.insert(self.syscalls, t)106else107util.abort(1,108"Skipped system call " ..109"at number " .. t.num)110end111end112s = nil113end114end115::skip::116end117118-- Special handling for linux nosys.119if config.syscallprefix:find("LINUX") ~= nil then120s = nil121end122123if s ~= nil then124util.abort(1, "Dangling system call at the end")125end126127assert(fh:close())128self.includes = incs129self.epilog = prolog130131if self.prolog ~= "" then132util.warn("non-include pre-processor directives in the " ..133"config prolog will not appear in generated output:\n" ..134self.prolog)135end136end137138function FreeBSDSyscall:findStructs()139self.structs = {}140141for _, s in pairs(self.syscalls) do142if s:native() and not s.type.NODEF then143for _, v in ipairs(s.args) do144local name = util.structName(v.type)145if name ~= nil then146self.structs[name] = name147end148end149end150end151end152153function FreeBSDSyscall:new(obj)154obj = obj or {}155setmetatable(obj, self)156self.__index = self157158obj:processCompat()159obj:parseSysfile()160obj:findStructs()161162return obj163end164165return FreeBSDSyscall166167168