Groundwork for the GCC driver: a compiler driver must spawn cc1/as/ld and know whether each stage succeeded. Kernel: the scheduler keeps an exit-code ledger (pid -> code; pids are monotonic so entries never alias), published during teardown right before waiters wake. SYS_EXIT records main()'s return value, SYS_KILL records 256+SIGKILL, and the exception handler records 256+signal mapped from the fault vector (#PF/#GP -> SIGSEGV, #DE/FP -> SIGFPE, #UD -> SIGILL). SYS_WAITPID now returns the code: 0..255 for a normal exit, 256+signal for a violent death. Process args grow from 256 bytes to 4 KiB (cc1 invocations do not fit in 256), with crt1 now parsing up to 255 argv entries from a static buffer. libc: new spawn.h with posix_spawn/posix_spawnp over SYS_SPAWN - libiberty's pex layer has a posix_spawn backend, so GCC's driver works without fork. argv is joined into the kernel args string (spaces in arguments rejected; no kernel quoting), envp is not transferred, and non-empty file actions fail loudly with ENOTSUP until the kernel can redirect stdio on spawn. waitpid() now decodes real POSIX status and the sys/wait.h macros distinguish exited from signaled children. Shell: prints [exit code N] after nonzero exits and [terminated by signal N] for killed or crashed children. Verified on the OS: cat on a missing file reports exit code 1; a window-close (clean exit 0) stays silent as it should. Co-Authored-By: Claude Fable 5 <[email protected]>
83 lines
1.9 KiB
C
83 lines
1.9 KiB
C
/*
|
|
* crt1.c
|
|
* Minimal C runtime startup for MontaukOS userspace programs
|
|
* Copyright (c) 2026 Daniel Hammer
|
|
*
|
|
* This startup shim is intentionally small: it fetches the raw
|
|
* command-line buffer from the kernel, tokenizes it into argc/argv,
|
|
* calls main(), then exits with main()'s return code.
|
|
*/
|
|
|
|
static inline long _sys1(long nr, long a1) {
|
|
long ret;
|
|
__asm__ volatile(
|
|
"mov %[a1], %%rdi\n\t"
|
|
"syscall"
|
|
: "=a"(ret)
|
|
: "a"(nr), [a1] "r"(a1)
|
|
: "rcx", "r11", "rdi", "rsi", "rdx", "r8", "r9", "r10", "memory");
|
|
return ret;
|
|
}
|
|
|
|
static inline long _sys2(long nr, long a1, long a2) {
|
|
long ret;
|
|
__asm__ volatile(
|
|
"mov %[a1], %%rdi\n\t"
|
|
"mov %[a2], %%rsi\n\t"
|
|
"syscall"
|
|
: "=a"(ret)
|
|
: "a"(nr), [a1] "r"(a1), [a2] "r"(a2)
|
|
: "rcx", "r11", "rdi", "rsi", "rdx", "r8", "r9", "r10", "memory");
|
|
return ret;
|
|
}
|
|
|
|
#define SYS_EXIT 0
|
|
#define SYS_GETARGS 25
|
|
|
|
extern int main(int argc, char** argv);
|
|
|
|
void _start(void) {
|
|
/* Static: 4 KiB args + 256 argv slots would crowd a 32 KiB stack. */
|
|
static char argbuf[4096];
|
|
static char* argv[256];
|
|
int len = (int)_sys2(SYS_GETARGS, (long)argbuf, (long)sizeof(argbuf));
|
|
|
|
int argc = 0;
|
|
|
|
argv[argc++] = (char*)"prog";
|
|
|
|
if (len > 0) {
|
|
if (len > (int)sizeof(argbuf) - 1) {
|
|
len = (int)sizeof(argbuf) - 1;
|
|
}
|
|
|
|
argbuf[len] = '\0';
|
|
|
|
char* p = argbuf;
|
|
while (*p != '\0' && argc < 255) {
|
|
while (*p == ' ') {
|
|
p++;
|
|
}
|
|
|
|
if (*p == '\0') {
|
|
break;
|
|
}
|
|
|
|
argv[argc++] = p;
|
|
|
|
while (*p != '\0' && *p != ' ') {
|
|
p++;
|
|
}
|
|
|
|
if (*p != '\0') {
|
|
*p++ = '\0';
|
|
}
|
|
}
|
|
}
|
|
|
|
argv[argc] = 0;
|
|
|
|
_sys1(SYS_EXIT, (long)main(argc, argv));
|
|
__builtin_unreachable();
|
|
}
|