feat: add Rust userspace support; fix TLS layout for p_align < 16; kill whole process on sibling-thread faults

This commit is contained in:
daniel committed 2026-10-14 07:16:42 +02:00
1 parent a81298fb24
commit f8b8f9610f
31 files changed
+1076 -38

No files matched your search

+16 -16
View File
@@ -720,10 +720,8 @@ namespace Sched {
uint64_t threadTlsPages = 0;
Process& primary = processTable[primarySlot_];
if (primary.tlsMemSize > 0) {
uint64_t align = primary.tlsAlign < 16 ? 16 : primary.tlsAlign;
uint64_t S = (primary.tlsMemSize + align - 1) & ~(align - 1);
uint64_t total = S + 16;
uint64_t numPages = (total + 0xFFF) / 0x1000;
TlsLayout layout = ComputeTlsLayout(primary.tlsMemSize, primary.tlsAlign);
uint64_t numPages = (layout.totalSize + 0xFFF) / 0x1000;
uint64_t base = 0;
if (!ReserveUserHeapRange(primarySlot_, numPages * 0x1000, base)) {
@@ -757,9 +755,9 @@ namespace Sched {
return -1;
}
uint64_t tp = base + S;
memcpy((void*)base, (const void*)primary.tlsTemplateVaddr,
primary.tlsFileSize);
uint64_t tp = base + layout.tpOffset;
memcpy((void*)(base + layout.templateOffset),
(const void*)primary.tlsTemplateVaddr, primary.tlsFileSize);
*(uint64_t*)tp = tp; /* ABI self-pointer */
threadFsBase = tp;
threadTlsBase = base;
@@ -945,14 +943,13 @@ namespace Sched {
return;
}
uint64_t align = primary.tlsAlign < 16 ? 16 : primary.tlsAlign;
uint64_t blockSize = (primary.tlsMemSize + align - 1) & ~(align - 1);
if (blockSize > thr.fsBase || blockSize > UINT64_MAX - 16) {
TlsLayout layout = ComputeTlsLayout(primary.tlsMemSize, primary.tlsAlign);
if (layout.tpOffset > thr.fsBase || layout.totalSize < layout.tpOffset) {
Panic("Invalid sibling TLS metadata during thread teardown", nullptr);
}
uint64_t base = thr.fsBase - blockSize;
uint64_t pages = (blockSize + 16 + 0xFFF) / 0x1000;
uint64_t base = thr.fsBase - layout.tpOffset;
uint64_t pages = (layout.totalSize + 0xFFF) / 0x1000;
thr.fsBase = 0;
Memory::UnmapAndFreeUserRange(primary.pml4Phys, base, pages);
ReleaseUserHeapRange(primarySlot_, base, pages * 0x1000ULL);
@@ -1791,10 +1788,13 @@ namespace Sched {
}
}
int KillProcess(int pid) {
// Refuse to kill PID 0 (init) or caller's own process
int KillProcess(int pid, int exitCode) {
// Refuse to kill PID 0 (init). Killing the caller's own process is
// allowed: a faulting/aborting sibling thread shares the pid and has
// no other way to take the process down. The caller's own slot gets
// killPending below and exits at syscall return (ExitIfKilled) or
// via the ExitProcess the fault handler calls next.
if (pid == 0) return -1;
if (pid == GetCurrentPid()) return -1;
schedLock.Acquire();
@@ -1817,7 +1817,7 @@ namespace Sched {
return -1;
}
processTable[primarySlot_].exitCode = 256 + 9; /* killed (SIGKILL) */
processTable[primarySlot_].exitCode = exitCode;
// Flag the main thread so its next tick (or scheduler dispatch)
// routes through ExitProcess, which sweeps every sibling thread