Files
ViperOS/vpsdk/stdlib.vp
T
TermiNexus ca72ddbbf7 refactor(syscall): 调用号改用 enum 并全面对齐 Linux x86-64 ABI
内核 intr/syscall.vp 与 vpsdk/syscall.vp 各一份 1..34 的 define 表改为同名 enum Syscall。编号口径:与 Linux 同名同号;Linux 有而我们未实现的留空不占号(12 brk / 39 getpid / 84 rmdir / 217 getdents64 / 257 openat … 需要时按号补)。

15 个语义重合的调用直接用 Linux 号:READ 0 / WRITE 1 / OPEN 2 / CLOSE 3 / STAT 4 / SEEK 8 / MMAP 9 / MUNMAP 11 / YIELD 24 / THREAD_CREATE 56(≈clone) / SPAWN 59(≈execve) / THREAD_EXIT 60(exit) / MKDIR 83 / REMOVE 87 / EXIT 231(exit_group)。

其余 19 个(UI 11 个 + SERIAL_PUTS/TELL/SIZE/OPENDIR/READDIR/CLOSEDIR/LOAD_SO/SO_CALL1)暂放 0x1000 起的**临时私有段**,每条带迁移标注:私有能力最终不占 syscall 号、改走 devfs 设备文件;UI 11 个待用户态 explorer(图形壳)接管 framebuffer 与输入后删除。

内核派发链改 n == int(Syscall.X)(n 是 int/i64、成员是 i32,显式 int() 规避未证实的混合宽度比较);SDK 全部调用点统一为 syscall.Syscall.X;stdlib 的 SYSCALL_MMAP 别名并入 Syscall.MMAP;Terminal 里硬编码的 uint64(18) 改为常量。
2026-10-16 18:32:33 +08:00

60 lines
1.9 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import vpsdk.syscall as _sc
import string
define HEAP_BLOCK_MAGIC = 0x48454150
class _heap_hdr:
magic: uint32
size: uint32
_heap_inited: int = 0
def _heap_init():
global _heap_inited
_heap_inited = 1
export def malloc(size: sizet) -> rawptr:
global _heap_inited
if size == 0: return None
if _heap_inited == 0: _heap_init()
hdr_size: sizet = _heap_hdr.__sizeof__()
total: sizet = hdr_size + size
ptr: uint64 = _sc._syscall1(uint64(_sc.Syscall.MMAP), uint64(total))
if ptr == 0: return None
# 块头必须落在**映射区内部**(映射起点 ptr 处),不能写 ptr - hdr_size:
# 那 8 字节在映射之外 —— 第一次 malloc 时 mmap 返回的正是用户堆起点 0x20000000,
# 写 0x1ffffff8 就是映射区外 ⇒ #PF(err=0x7 用户态写),四个 app 启动即崩的现场。
hdr: _heap_hdr* = _heap_hdr*(uint64(ptr))
hdr.magic = HEAP_BLOCK_MAGIC
hdr.size = uint32(size)
return rawptr(ptr + hdr_size)
export def free(ptr: rawptr):
if ptr is None: return
hdr_size: sizet = _heap_hdr.__sizeof__()
hdr: _heap_hdr* = _heap_hdr*(uint64(ptr) - hdr_size)
if hdr.magic != HEAP_BLOCK_MAGIC: return
export def calloc(nmemb: sizet, size: sizet) -> rawptr:
total: sizet = nmemb * size
p: rawptr = malloc(total)
if p: string.memset(p, 0, total)
return p
export def realloc(ptr: rawptr, size: sizet) -> rawptr:
if ptr is None: return malloc(size)
if size == 0:
free(ptr)
return None
hdr_size: sizet = _heap_hdr.__sizeof__()
hdr: _heap_hdr* = _heap_hdr*(uint64(ptr) - hdr_size)
if hdr.magic != HEAP_BLOCK_MAGIC: return None
old_size: sizet = hdr.size
new_ptr: rawptr = malloc(size)
if new_ptr is None: return None
copy_size: sizet = old_size if old_size < size else size
string.memcpy(new_ptr, ptr, copy_size)
free(ptr)
return new_ptr