xref: /freebsd/sys/tools/syscalls/scripts/syscall_h.lua (revision e7e964cb2ebde11593786a28cea0238d9a6e24c3)
1#!/usr/libexec/flua
2--
3-- SPDX-License-Identifier: BSD-2-Clause
4--
5-- Copyright (c) 2024 Tyler Baxter <agge@FreeBSD.org>
6-- Copyright (c) 2023 Warner Losh <imp@bsdimp.com>
7-- Copyright (c) 2019 Kyle Evans <kevans@FreeBSD.org>
8--
9
10-- Setup to be a module, or ran as its own script.
11local syscall_h = {}
12local script = not pcall(debug.getlocal, 4, 1)	-- TRUE if script.
13if script then
14	-- Add library root to the package path.
15	local path = arg[0]:gsub("/[^/]+.lua$", "")
16	package.path = package.path .. ";" .. path .. "/../?.lua"
17end
18
19local FreeBSDSyscall = require("core.freebsd-syscall")
20local generator = require("tools.generator")
21
22-- File has not been decided yet; config will decide file.  Default defined as
23-- /dev/null.
24syscall_h.file = "/dev/null"
25
26-- Libc has all the STD, NOSTD and SYSMUX system calls in it, as well as
27-- replaced system calls dating back to FreeBSD 7. We are lucky that the
28-- system call filename is just the base symbol name for it.
29function syscall_h.generate(tbl, config, fh)
30	-- Grab the master system calls table, and prepare bookkeeping for
31	-- the max system call number.
32	local s = tbl.syscalls
33	local max = 0
34
35	-- Bind the generator to the parameter file.
36	local gen = generator:new({}, fh)
37
38	-- Write the generated preamble.
39	gen:preamble("System call numbers.")
40
41	if config.syshdr_extra then
42		gen:write(string.format("%s\n\n", config.syshdr_extra))
43	end
44
45	for _, v in pairs(s) do
46		local c = v:compatLevel()
47		if v.num > max then
48			max = v.num
49		end
50		if v.type.UNIMPL then
51			goto skip
52		elseif v.type.RESERVED then
53			goto skip
54		elseif v.type.NODEF then
55			goto skip
56		elseif v.type.STD or v.type.NOSTD or v.type.SYSMUX or
57		    c >= 7 then
58			gen:write(string.format("#define\t%s%s%s\t%d\n",
59			    config.syscallprefix, v:compatPrefix(), v.name,
60			    v.num))
61		elseif c >= 0 then
62			local comment
63			if c == 0 then
64				comment = "obsolete"
65			elseif c == 3 then
66				comment = "old"
67			else
68				comment = "freebsd" .. c
69			end
70			gen:write(string.format("\t\t\t\t/* %d is %s %s */\n",
71			    v.num, comment, v.name))
72		end
73		::skip::
74	end
75	gen:write(string.format("#define\t%sMAXSYSCALL\t%d\n",
76	    config.syscallprefix, max + 1))
77end
78
79-- Entry of script:
80if script then
81	local config = require("config")
82
83	if #arg < 1 or #arg > 2 then
84		error("usage: " .. arg[0] .. " syscall.master")
85	end
86
87	local sysfile, configfile = arg[1], arg[2]
88
89	config.merge(configfile)
90	config.mergeCompat()
91
92	-- The parsed system call table.
93	local tbl = FreeBSDSyscall:new{sysfile = sysfile, config = config}
94
95	syscall_h.file = config.syshdr	-- change file here
96	syscall_h.generate(tbl, config, syscall_h.file)
97end
98
99-- Return the module.
100return syscall_h
101