| 1 |
#!/usr/bin/env python3 |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
import functools |
| 5 |
import sys |
| 6 |
|
| 7 |
# We only generate numbers for a dozen or so syscalls |
| 8 |
SYSCALLS = [ |
| 9 |
'fchmodat2', # defined in glibc header since glibc-2.39 |
| 10 |
'kexec_file_load', |
| 11 |
'open_tree_attr', |
| 12 |
'quotactl_fd', # defined in glibc header since glibc-2.35 |
| 13 |
'removexattrat', |
| 14 |
'setxattrat', |
| 15 |
] # fmt: skip |
| 16 |
|
| 17 |
|
| 18 |
def dictify(f): |
| 19 |
def wrap(*args, **kwargs): |
| 20 |
return dict(f(*args, **kwargs)) |
| 21 |
|
| 22 |
return functools.update_wrapper(wrap, f) |
| 23 |
|
| 24 |
|
| 25 |
@dictify |
| 26 |
def parse_syscall_table(filename): |
| 27 |
print(f'Reading {filename}…') |
| 28 |
with open(filename) as f: |
| 29 |
for line in f: |
| 30 |
items = line.split() |
| 31 |
if len(items) >= 2: |
| 32 |
yield items[0], int(items[1]) |
| 33 |
|
| 34 |
|
| 35 |
def parse_syscall_tables(filenames): |
| 36 |
return {filename.split('-')[-1][:-4]: parse_syscall_table(filename) for filename in filenames} |
| 37 |
|
| 38 |
|
| 39 |
HEADER = '''\ |
| 40 |
/* SPDX-License-Identifier: LGPL-2.1-or-later |
| 41 |
* This file is generated by src/include/override/sys/generate-syscall.py. Do not edit! |
| 42 |
* |
| 43 |
* Use 'ninja -C build update-syscall-tables' to download new syscall tables, |
| 44 |
* and 'ninja -C build update-syscall-header' to regenerate this file. |
| 45 |
* |
| 46 |
* To add a new architecture, extend the arch_list list in meson.build |
| 47 |
* and the template in generate-syscall.py and then run the above commands. |
| 48 |
*/ |
| 49 |
#pragma once |
| 50 |
|
| 51 |
#include_next <sys/syscall.h> /* IWYU pragma: export */ |
| 52 |
|
| 53 |
#ifdef __mips__ |
| 54 |
#include <asm/sgidefs.h> |
| 55 |
#endif |
| 56 |
|
| 57 |
#include <assert.h> |
| 58 |
''' |
| 59 |
|
| 60 |
DEF_TEMPLATE_A = '''\ |
| 61 |
|
| 62 |
#ifndef __IGNORE_{syscall} |
| 63 |
''' |
| 64 |
|
| 65 |
DEF_TEMPLATE_B = '''\ |
| 66 |
# if defined(__aarch64__) |
| 67 |
# define systemd_NR_{syscall} {nr_arm64} |
| 68 |
# elif defined(__alpha__) |
| 69 |
# define systemd_NR_{syscall} {nr_alpha} |
| 70 |
# elif defined(__arc__) || defined(__tilegx__) |
| 71 |
# define systemd_NR_{syscall} {nr_arc} |
| 72 |
# elif defined(__arm__) |
| 73 |
# define systemd_NR_{syscall} {nr_arm} |
| 74 |
# elif defined(__i386__) |
| 75 |
# define systemd_NR_{syscall} {nr_i386} |
| 76 |
# elif defined(__ia64__) |
| 77 |
# define systemd_NR_{syscall} {nr_ia64} |
| 78 |
# elif defined(__loongarch_lp64) |
| 79 |
# define systemd_NR_{syscall} {nr_loongarch64} |
| 80 |
# elif defined(__m68k__) |
| 81 |
# define systemd_NR_{syscall} {nr_m68k} |
| 82 |
# elif defined(_MIPS_SIM) |
| 83 |
# if _MIPS_SIM == _MIPS_SIM_ABI32 |
| 84 |
# define systemd_NR_{syscall} {nr_mipso32} |
| 85 |
# elif _MIPS_SIM == _MIPS_SIM_NABI32 |
| 86 |
# define systemd_NR_{syscall} {nr_mips64n32} |
| 87 |
# elif _MIPS_SIM == _MIPS_SIM_ABI64 |
| 88 |
# define systemd_NR_{syscall} {nr_mips64} |
| 89 |
# else |
| 90 |
# error "Unknown MIPS ABI" |
| 91 |
# endif |
| 92 |
# elif defined(__hppa__) |
| 93 |
# define systemd_NR_{syscall} {nr_parisc} |
| 94 |
# elif defined(__powerpc__) |
| 95 |
# define systemd_NR_{syscall} {nr_powerpc} |
| 96 |
# elif defined(__riscv) |
| 97 |
# if __riscv_xlen == 32 |
| 98 |
# define systemd_NR_{syscall} {nr_riscv32} |
| 99 |
# elif __riscv_xlen == 64 |
| 100 |
# define systemd_NR_{syscall} {nr_riscv64} |
| 101 |
# else |
| 102 |
# error "Unknown RISC-V ABI" |
| 103 |
# endif |
| 104 |
# elif defined(__s390__) |
| 105 |
# define systemd_NR_{syscall} {nr_s390} |
| 106 |
# elif defined(__sh__) |
| 107 |
# define systemd_NR_{syscall} {nr_sh} |
| 108 |
# elif defined(__sparc__) |
| 109 |
# define systemd_NR_{syscall} {nr_sparc} |
| 110 |
# elif defined(__x86_64__) |
| 111 |
# if defined(__ILP32__) |
| 112 |
# define systemd_NR_{syscall} ({nr_x86_64} | /* __X32_SYSCALL_BIT */ 0x40000000) |
| 113 |
# else |
| 114 |
# define systemd_NR_{syscall} {nr_x86_64} |
| 115 |
# endif |
| 116 |
''' |
| 117 |
|
| 118 |
DEF_TEMPLATE_C = '''\ |
| 119 |
# elif !defined(missing_arch_template) |
| 120 |
# warning "{syscall}() syscall number is unknown for your architecture" |
| 121 |
# endif |
| 122 |
''' |
| 123 |
|
| 124 |
DEF_TEMPLATE_D = '''\ |
| 125 |
|
| 126 |
/* may be an (invalid) negative number due to libseccomp, see PR 13319 */ |
| 127 |
# if defined __NR_{syscall} && __NR_{syscall} >= 0 |
| 128 |
# if defined systemd_NR_{syscall} |
| 129 |
static_assert(__NR_{syscall} == systemd_NR_{syscall}, ""); |
| 130 |
# endif |
| 131 |
# else |
| 132 |
# if defined __NR_{syscall} |
| 133 |
# undef __NR_{syscall} |
| 134 |
# endif |
| 135 |
# if defined systemd_NR_{syscall} && systemd_NR_{syscall} >= 0 |
| 136 |
# define __NR_{syscall} systemd_NR_{syscall} |
| 137 |
# endif |
| 138 |
# endif |
| 139 |
#endif''' |
| 140 |
|
| 141 |
DEF_TEMPLATE = DEF_TEMPLATE_A + DEF_TEMPLATE_B + DEF_TEMPLATE_C + DEF_TEMPLATE_D |
| 142 |
|
| 143 |
ARCH_CHECK_A = '''\ |
| 144 |
/* Note: if this code looks strange, this is because it is derived from the same |
| 145 |
* template as the per-syscall blocks below. */ |
| 146 |
''' |
| 147 |
|
| 148 |
ARCH_CHECK_B = '\n'.join(line for line in DEF_TEMPLATE_B.splitlines() if ' define ' not in line) |
| 149 |
|
| 150 |
ARCH_CHECK_C = '''\ |
| 151 |
|
| 152 |
# else |
| 153 |
# warning "Current architecture is missing from the template" |
| 154 |
# define missing_arch_template 1 |
| 155 |
# endif''' |
| 156 |
|
| 157 |
ARCH_CHECK = ARCH_CHECK_A + ARCH_CHECK_B + ARCH_CHECK_C |
| 158 |
|
| 159 |
|
| 160 |
def print_syscall_def(syscall, tables, out): |
| 161 |
mappings = {f'nr_{arch}': t.get(syscall, -1) for arch, t in tables.items()} |
| 162 |
print(DEF_TEMPLATE.format(syscall=syscall, **mappings), file=out) |
| 163 |
|
| 164 |
|
| 165 |
def print_syscall_defs(syscalls, tables, out): |
| 166 |
print(HEADER, file=out) |
| 167 |
print(ARCH_CHECK, file=out) |
| 168 |
for syscall in syscalls: |
| 169 |
print_syscall_def(syscall, tables, out) |
| 170 |
|
| 171 |
|
| 172 |
if __name__ == '__main__': |
| 173 |
output_file = sys.argv[1] |
| 174 |
arch_files = sys.argv[2:] |
| 175 |
|
| 176 |
tables = parse_syscall_tables(arch_files) |
| 177 |
with open(output_file, 'w') as out: |
| 178 |
print_syscall_defs(SYSCALLS, tables, out) |
| 179 |
|
| 180 |
print(f'Wrote {output_file}') |