diff options
| author | Richard Qin <richard.qin.x@gmail.com> | 2025-12-28 01:48:50 +0800 |
|---|---|---|
| committer | Richard Qin <richard.qin.x@gmail.com> | 2025-12-28 01:48:50 +0800 |
| commit | f3c8918b13a5226f53e7870d60436f812e5d9015 (patch) | |
| tree | 5a934b82f23916e67e8cac9d230af93645611f3a /src/user | |
| parent | a82ab8ad6d277ededc128678aebabf5fbdb01c45 (diff) | |
| download | lume-f3c8918b13a5226f53e7870d60436f812e5d9015.tar.gz lume-f3c8918b13a5226f53e7870d60436f812e5d9015.tar.xz | |
feat(user): add user-space build system and init process to test fork/wait
Diffstat (limited to 'src/user')
| -rw-r--r-- | src/user/init.cc | 34 | ||||
| -rw-r--r-- | src/user/user.ld | 27 | ||||
| -rw-r--r-- | src/user/usys.S | 31 |
3 files changed, 92 insertions, 0 deletions
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 |
