xv6-riscv-kernel/user/usys.py
2025-01-22 00:05:41 +01:00

74 lines
1.6 KiB
Python
Executable file

#!/usr/bin/env python3
template = """
.global {name}
{name}:
li a7, SYS_{name}
ecall
ret
""".strip()
def genstub(syscall: str):
if not 3 <= len(syscall) <= 15:
raise ValueError("Syscall {name} is too short/long!".format(name=syscall[:35]))
return template.format(name=syscall)
syscalls = [
"fork",
"exit",
"wait",
"pipe",
"read",
"write",
"close",
"kill",
"exec",
"open",
"mknod",
"unlink",
"fstat",
"link",
"mkdir",
"chdir",
"dup",
"getpid",
"sbrk",
"sleep",
"uptime",
"trace",
"halt",
]
assembly = "\n\n".join(map(genstub, syscalls))
# TODO: Perhaps do some more specific assertions on the code here
assert len(assembly) > 0, "No assembly code generated. Something is fishy"
with open("kernel/syscall.h", "r") as sfile:
lines = list(filter(lambda x: x.startswith("#define"), sfile.readlines()))
header_calls = set([line.split()[1] for line in lines])
calls = set(["SYS_" + call for call in syscalls])
if header_calls == calls:
print("#All items match in both sets.")
else:
missing_in_calls = header_calls - calls
missing_in_header_calls = calls - header_calls
if missing_in_calls:
print(
"#These items are in header_calls but not in calls:", missing_in_calls
)
if missing_in_header_calls:
print(
"#These items are in calls but not in header_calls:",
missing_in_header_calls,
)
print("# generated by usys.py - do not edit\n")
print('#include "kernel/syscall.h"\n')
print(assembly)