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/kernel/initcode.S | 8 ++++++++ src/kernel/proc/proc.cc | 15 ++++++--------- src/user/init.cc | 34 ++++++++++++++++++++++++++++++++++ src/user/user.ld | 27 +++++++++++++++++++++++++++ src/user/usys.S | 31 +++++++++++++++++++++++++++++++ 5 files changed, 106 insertions(+), 9 deletions(-) create mode 100644 src/kernel/initcode.S create mode 100644 src/user/init.cc create mode 100644 src/user/user.ld create mode 100644 src/user/usys.S (limited to 'src') diff --git a/src/kernel/initcode.S b/src/kernel/initcode.S new file mode 100644 index 0000000..8b56ce6 --- /dev/null +++ b/src/kernel/initcode.S @@ -0,0 +1,8 @@ +.section .rodata +.global initcode_start +.global initcode_end +.align 4 + +initcode_start: + .incbin "build/user/initcode" +initcode_end: diff --git a/src/kernel/proc/proc.cc b/src/kernel/proc/proc.cc index 15a28b4..008f740 100644 --- a/src/kernel/proc/proc.cc +++ b/src/kernel/proc/proc.cc @@ -23,13 +23,8 @@ struct spinlock proc_lock; int nextpid = 1; - -uchar initcode[] = { - 0x13, 0x05, 0x50, 0x05, // li a0, 85 ('U') - 0x93, 0x08, 0x10, 0x00, // li a7, 1 (SYS_putc) - 0x73, 0x00, 0x00, 0x00, // ecall - 0x6f, 0xf0, 0x5f, 0xff // j loop (-12) -}; +extern char initcode_start[]; +extern char initcode_end[]; struct Proc *myproc() { @@ -277,7 +272,9 @@ namespace ProcManager } p->pagetable = VM::uvmcreate(); - VM::uvminit(p->pagetable, initcode, sizeof(initcode)); + + uint64 initcode_sz = initcode_end - initcode_start; + VM::uvminit(p->pagetable, (uchar *)initcode_start, initcode_sz); p->sz = PGSIZE; if (VM::mappages(p->pagetable, TRAPFRAME, PGSIZE, @@ -402,6 +399,6 @@ namespace ProcManager } } - + } \ No newline at end of file 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