xv6-riscv-kernel/user/usys.py

75 lines
1.6 KiB
Python
Raw Normal View History

#!/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 {} is too short/long!".format(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"
2025-01-15 13:14:02 +01:00
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:
2025-01-15 13:28:20 +01:00
print("#All items match in both sets.")
2025-01-15 13:14:02 +01:00
else:
missing_in_calls = header_calls - calls
missing_in_header_calls = calls - header_calls
if missing_in_calls:
2025-01-21 23:54:00 +01:00
print(
"#These items are in header_calls but not in calls:", missing_in_calls
)
2025-01-15 13:14:02 +01:00
if missing_in_header_calls:
print(
2025-01-15 13:28:20 +01:00
"#These items are in calls but not in header_calls:",
2025-01-15 13:14:02 +01:00
missing_in_header_calls,
)
print("# generated by usys.py - do not edit\n")
print('#include "kernel/syscall.h"\n')
print(assembly)