From f3c8918b13a5226f53e7870d60436f812e5d9015 Mon Sep 17 00:00:00 2001 From: Richard Qin Date: Sun, 28 Dec 2025 01:48:50 +0800 Subject: feat(user): add user-space build system and init process to test fork/wait --- src/user/init.cc | 34 ++++++++++++++++++++++++++++++++++ src/user/user.ld | 27 +++++++++++++++++++++++++++ src/user/usys.S | 31 +++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 src/user/init.cc create mode 100644 src/user/user.ld create mode 100644 src/user/usys.S (limited to 'src/user') diff --git a/src/user/init.cc b/src/user/init.cc new file mode 100644 index 0000000..3c42adf --- /dev/null +++ b/src/user/init.cc @@ -0,0 +1,34 @@ +#include "user/user.h" + +int main(); + +extern "C" void _start() +{ + exit(main()); +} + +int main() +{ + int pid = fork(); + + if (pid < 0) + { + write(1, "Fork failed\n", 12); + exit(1); + } + + if (pid == 0) + { + write(1, "Hello from Child!\n", 18); + exit(0); + } + else + { + write(1, "Parent waiting...\n", 18); + wait(0); + write(1, "Parent: Child exited.\n", 22); + } + + while (1); + return 0; +} \ No newline at end of file diff --git a/src/user/user.ld b/src/user/user.ld new file mode 100644 index 0000000..2111b14 --- /dev/null +++ b/src/user/user.ld @@ -0,0 +1,27 @@ +OUTPUT_ARCH(riscv) +ENTRY(_start) + +SECTIONS +{ + . = 0x0; + + .text : { + *(.text.entry) + *(.text .text.*) + } + + .rodata : { + . = ALIGN(16); + *(.rodata .rodata.*) + } + + .data : { + . = ALIGN(16); + *(.data .data.*) + } + + .bss : { + . = ALIGN(16); + *(.bss .bss.*) + } +} \ No newline at end of file diff --git a/src/user/usys.S b/src/user/usys.S new file mode 100644 index 0000000..e805d82 --- /dev/null +++ b/src/user/usys.S @@ -0,0 +1,31 @@ +#include "kernel/syscall.h" + +#define SYSCALL(name) \ + .global name; \ + name: \ + li a7, SYS_ ## name; \ + ecall; \ + ret + +SYSCALL(fork) +SYSCALL(exit) +SYSCALL(wait) +SYSCALL(pipe) +SYSCALL(read) +SYSCALL(write) +SYSCALL(close) +SYSCALL(kill) +SYSCALL(exec) +SYSCALL(open) +SYSCALL(mknod) +SYSCALL(unlink) +SYSCALL(fstat) +SYSCALL(link) +SYSCALL(mkdir) +SYSCALL(chdir) +SYSCALL(dup) +SYSCALL(getpid) +SYSCALL(sbrk) +SYSCALL(sleep) +SYSCALL(uptime) +SYSCALL(putc) \ No newline at end of file -- cgit v1.2.3