summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Qin <richard.qin.x@gmail.com>2025-12-28 01:48:50 +0800
committerRichard Qin <richard.qin.x@gmail.com>2025-12-28 01:48:50 +0800
commitf3c8918b13a5226f53e7870d60436f812e5d9015 (patch)
tree5a934b82f23916e67e8cac9d230af93645611f3a
parenta82ab8ad6d277ededc128678aebabf5fbdb01c45 (diff)
downloadlume-f3c8918b13a5226f53e7870d60436f812e5d9015.tar.gz
lume-f3c8918b13a5226f53e7870d60436f812e5d9015.tar.xz
feat(user): add user-space build system and init process to test fork/wait
-rw-r--r--Makefile44
-rwxr-xr-xbuild/kernel.elfbin39464 -> 55040 bytes
-rw-r--r--build/src/kernel/initcode.obin0 -> 1488 bytes
-rw-r--r--build/src/kernel/proc/proc.obin24808 -> 117608 bytes
-rw-r--r--build/user/init.obin0 -> 11264 bytes
-rwxr-xr-xbuild/user/initcodebin0 -> 464 bytes
-rw-r--r--build/user/initcode.asm215
-rwxr-xr-xbuild/user/initcode.outbin0 -> 9256 bytes
-rw-r--r--build/user/usys.obin0 -> 5160 bytes
-rw-r--r--include/kernel/syscall.h4
-rw-r--r--include/user/user.h29
-rw-r--r--src/kernel/initcode.S8
-rw-r--r--src/kernel/proc/proc.cc15
-rw-r--r--src/user/init.cc34
-rw-r--r--src/user/user.ld27
-rw-r--r--src/user/usys.S31
16 files changed, 391 insertions, 16 deletions
diff --git a/Makefile b/Makefile
index 4f89bac..3e06e89 100644
--- a/Makefile
+++ b/Makefile
@@ -3,29 +3,56 @@ CC := $(TOOLCHAIN_PREFIX)gcc
CXX := $(TOOLCHAIN_PREFIX)g++
LD := $(TOOLCHAIN_PREFIX)ld
OBJCOPY := $(TOOLCHAIN_PREFIX)objcopy
+OBJDUMP := $(TOOLCHAIN_PREFIX)objdump
BUILD_DIR := build
SRC_DIR := src
-CFLAGS := -Wall -Wextra -O2 -ffreestanding -nostdlib -mcmodel=medany -Iinclude
+CFLAGS := -Wall -Wextra -O2 -g -ffreestanding -nostdlib -mcmodel=medany -Iinclude
CXXFLAGS := $(CFLAGS) -fno-exceptions -fno-rtti -fno-use-cxa-atexit
-
LINKER_SCRIPT := link.ld
-SRCS_S := $(shell find $(SRC_DIR) -name "*.S")
-SRCS_CXX := $(shell find $(SRC_DIR) -name "*.cc")
+UPROGS_SRC := $(SRC_DIR)/user/init.cc
+UPROGS_OBJ := $(BUILD_DIR)/user/init.o
+ULIB_OBJ := $(BUILD_DIR)/user/usys.o
+USER_LD := $(SRC_DIR)/user/user.ld
+INITCODE_BIN := $(BUILD_DIR)/user/initcode
+
+SRCS_S := $(shell find $(SRC_DIR) -name "*.S" -not -path "$(SRC_DIR)/user/*")
+SRCS_CXX := $(shell find $(SRC_DIR) -name "*.cc" -not -path "$(SRC_DIR)/user/*")
OBJS := $(SRCS_S:%.S=$(BUILD_DIR)/%.o) $(SRCS_CXX:%.cc=$(BUILD_DIR)/%.o)
TARGET := $(BUILD_DIR)/kernel.elf
-.PHONY: all clean run
+.PHONY: all clean run debug
all: $(TARGET)
+$(BUILD_DIR)/user/%.o: $(SRC_DIR)/user/%.cc
+ @mkdir -p $(dir $@)
+ @echo " U_CXX $<"
+ @$(CXX) $(CXXFLAGS) -c $< -o $@
+
+$(BUILD_DIR)/user/%.o: $(SRC_DIR)/user/%.S
+ @mkdir -p $(dir $@)
+ @echo " U_AS $<"
+ @$(CC) $(CFLAGS) -c $< -o $@
+
+$(INITCODE_BIN): $(UPROGS_OBJ) $(ULIB_OBJ) $(USER_LD)
+ @echo " U_LD $@"
+ @$(LD) -T $(USER_LD) -o $@.out $(UPROGS_OBJ) $(ULIB_OBJ)
+ @$(OBJCOPY) -S -O binary $@.out $@
+ @$(OBJDUMP) -S $@.out > $@.asm
+
$(TARGET): $(OBJS) $(LINKER_SCRIPT)
@echo " LD $@"
@$(LD) -T $(LINKER_SCRIPT) -o $@ $(OBJS)
+$(BUILD_DIR)/src/kernel/initcode.o: $(SRC_DIR)/kernel/initcode.S $(INITCODE_BIN)
+ @mkdir -p $(dir $@)
+ @echo " AS_INC $<"
+ @$(CC) $(CFLAGS) -c $< -o $@
+
$(BUILD_DIR)/%.o: %.S
@mkdir -p $(dir $@)
@echo " AS $<"
@@ -41,5 +68,10 @@ run: $(TARGET)
@qemu-system-riscv64 -machine virt -nographic \
-kernel $(TARGET) -m 128M
+debug: $(TARGET)
+ @echo " QEMU (DEBUG) Waiting for GDB..."
+ @qemu-system-riscv64 -machine virt -nographic \
+ -kernel $(TARGET) -m 128M -s -S
+
clean:
- rm -rf $(BUILD_DIR) \ No newline at end of file
+ rm -rf $(BUILD_DIR)
diff --git a/build/kernel.elf b/build/kernel.elf
index 996720e..860775f 100755
--- a/build/kernel.elf
+++ b/build/kernel.elf
Binary files differ
diff --git a/build/src/kernel/initcode.o b/build/src/kernel/initcode.o
new file mode 100644
index 0000000..e53a8a5
--- /dev/null
+++ b/build/src/kernel/initcode.o
Binary files differ
diff --git a/build/src/kernel/proc/proc.o b/build/src/kernel/proc/proc.o
index f4bbe8f..d22780c 100644
--- a/build/src/kernel/proc/proc.o
+++ b/build/src/kernel/proc/proc.o
Binary files differ
diff --git a/build/user/init.o b/build/user/init.o
new file mode 100644
index 0000000..d32ad08
--- /dev/null
+++ b/build/user/init.o
Binary files differ
diff --git a/build/user/initcode b/build/user/initcode
new file mode 100755
index 0000000..511a518
--- /dev/null
+++ b/build/user/initcode
Binary files differ
diff --git a/build/user/initcode.asm b/build/user/initcode.asm
new file mode 100644
index 0000000..e3a1185
--- /dev/null
+++ b/build/user/initcode.asm
@@ -0,0 +1,215 @@
+
+build/user/initcode.out: file format elf64-littleriscv
+
+
+Disassembly of section .text:
+
+0000000000000000 <_start>:
+#include "user/user.h"
+
+int main();
+
+extern "C" void _start()
+{
+ 0: 1141 addi sp,sp,-16
+ 2: e406 sd ra,8(sp)
+ exit(main());
+ 4: 004000ef jal 8 <main>
+
+0000000000000008 <main>:
+}
+
+int main()
+{
+ 8: 1141 addi sp,sp,-16
+ a: e406 sd ra,8(sp)
+ int pid = fork();
+ c: 060000ef jal 6c <fork>
+
+ if (pid < 0)
+ 10: 00054f63 bltz a0,2e <main+0x26>
+ {
+ write(1, "Fork failed\n", 12);
+ exit(1);
+ }
+
+ if (pid == 0)
+ 14: e905 bnez a0,44 <main+0x3c>
+ {
+ write(1, "Hello from Child!\n", 18);
+ 16: 4649 li a2,18
+ 18: 00000597 auipc a1,0x0
+ 1c: 12058593 addi a1,a1,288 # 138 <putc+0x24>
+ 20: 4505 li a0,1
+ 22: 072000ef jal 94 <write>
+ exit(0);
+ 26: 4501 li a0,0
+ 28: 04c000ef jal 74 <exit>
+ write(1, "Parent waiting...\n", 18);
+ wait(0);
+ write(1, "Parent: Child exited.\n", 22);
+ }
+
+ while (1);
+ 2c: a001 j 2c <main+0x24>
+ write(1, "Fork failed\n", 12);
+ 2e: 4631 li a2,12
+ 30: 00000597 auipc a1,0x0
+ 34: 0f058593 addi a1,a1,240 # 120 <putc+0xc>
+ 38: 4505 li a0,1
+ 3a: 05a000ef jal 94 <write>
+ exit(1);
+ 3e: 4505 li a0,1
+ 40: 034000ef jal 74 <exit>
+ write(1, "Parent waiting...\n", 18);
+ 44: 4649 li a2,18
+ 46: 00000597 auipc a1,0x0
+ 4a: 10a58593 addi a1,a1,266 # 150 <putc+0x3c>
+ 4e: 4505 li a0,1
+ 50: 044000ef jal 94 <write>
+ wait(0);
+ 54: 4501 li a0,0
+ 56: 026000ef jal 7c <wait>
+ write(1, "Parent: Child exited.\n", 22);
+ 5a: 4659 li a2,22
+ 5c: 00000597 auipc a1,0x0
+ 60: 10c58593 addi a1,a1,268 # 168 <putc+0x54>
+ 64: 4505 li a0,1
+ 66: 02e000ef jal 94 <write>
+ 6a: b7c9 j 2c <main+0x24>
+
+000000000000006c <fork>:
+ name: \
+ li a7, SYS_ ## name; \
+ ecall; \
+ ret
+
+SYSCALL(fork)
+ 6c: 4889 li a7,2
+ 6e: 00000073 ecall
+ 72: 8082 ret
+
+0000000000000074 <exit>:
+SYSCALL(exit)
+ 74: 488d li a7,3
+ 76: 00000073 ecall
+ 7a: 8082 ret
+
+000000000000007c <wait>:
+SYSCALL(wait)
+ 7c: 4891 li a7,4
+ 7e: 00000073 ecall
+ 82: 8082 ret
+
+0000000000000084 <pipe>:
+SYSCALL(pipe)
+ 84: 4895 li a7,5
+ 86: 00000073 ecall
+ 8a: 8082 ret
+
+000000000000008c <read>:
+SYSCALL(read)
+ 8c: 4899 li a7,6
+ 8e: 00000073 ecall
+ 92: 8082 ret
+
+0000000000000094 <write>:
+SYSCALL(write)
+ 94: 48c5 li a7,17
+ 96: 00000073 ecall
+ 9a: 8082 ret
+
+000000000000009c <close>:
+SYSCALL(close)
+ 9c: 48d9 li a7,22
+ 9e: 00000073 ecall
+ a2: 8082 ret
+
+00000000000000a4 <kill>:
+SYSCALL(kill)
+ a4: 489d li a7,7
+ a6: 00000073 ecall
+ aa: 8082 ret
+
+00000000000000ac <exec>:
+SYSCALL(exec)
+ ac: 48a1 li a7,8
+ ae: 00000073 ecall
+ b2: 8082 ret
+
+00000000000000b4 <open>:
+SYSCALL(open)
+ b4: 48c1 li a7,16
+ b6: 00000073 ecall
+ ba: 8082 ret
+
+00000000000000bc <mknod>:
+SYSCALL(mknod)
+ bc: 48c9 li a7,18
+ be: 00000073 ecall
+ c2: 8082 ret
+
+00000000000000c4 <unlink>:
+SYSCALL(unlink)
+ c4: 48cd li a7,19
+ c6: 00000073 ecall
+ ca: 8082 ret
+
+00000000000000cc <fstat>:
+SYSCALL(fstat)
+ cc: 48a5 li a7,9
+ ce: 00000073 ecall
+ d2: 8082 ret
+
+00000000000000d4 <link>:
+SYSCALL(link)
+ d4: 48d1 li a7,20
+ d6: 00000073 ecall
+ da: 8082 ret
+
+00000000000000dc <mkdir>:
+SYSCALL(mkdir)
+ dc: 48d5 li a7,21
+ de: 00000073 ecall
+ e2: 8082 ret
+
+00000000000000e4 <chdir>:
+SYSCALL(chdir)
+ e4: 48a9 li a7,10
+ e6: 00000073 ecall
+ ea: 8082 ret
+
+00000000000000ec <dup>:
+SYSCALL(dup)
+ ec: 48ad li a7,11
+ ee: 00000073 ecall
+ f2: 8082 ret
+
+00000000000000f4 <getpid>:
+SYSCALL(getpid)
+ f4: 48b1 li a7,12
+ f6: 00000073 ecall
+ fa: 8082 ret
+
+00000000000000fc <sbrk>:
+SYSCALL(sbrk)
+ fc: 48b5 li a7,13
+ fe: 00000073 ecall
+ 102: 8082 ret
+
+0000000000000104 <sleep>:
+SYSCALL(sleep)
+ 104: 48b9 li a7,14
+ 106: 00000073 ecall
+ 10a: 8082 ret
+
+000000000000010c <uptime>:
+SYSCALL(uptime)
+ 10c: 48bd li a7,15
+ 10e: 00000073 ecall
+ 112: 8082 ret
+
+0000000000000114 <putc>:
+ 114: 4885 li a7,1
+ 116: 00000073 ecall
+ 11a: 8082 ret
diff --git a/build/user/initcode.out b/build/user/initcode.out
new file mode 100755
index 0000000..0667728
--- /dev/null
+++ b/build/user/initcode.out
Binary files differ
diff --git a/build/user/usys.o b/build/user/usys.o
new file mode 100644
index 0000000..9ac9e11
--- /dev/null
+++ b/build/user/usys.o
Binary files differ
diff --git a/include/kernel/syscall.h b/include/kernel/syscall.h
index 571741e..222f2f4 100644
--- a/include/kernel/syscall.h
+++ b/include/kernel/syscall.h
@@ -23,4 +23,6 @@
#define SYS_mkdir 21
#define SYS_close 22
-void syscall(); \ No newline at end of file
+#ifndef __ASSEMBLER__
+void syscall();
+#endif \ No newline at end of file
diff --git a/include/user/user.h b/include/user/user.h
new file mode 100644
index 0000000..7ab4923
--- /dev/null
+++ b/include/user/user.h
@@ -0,0 +1,29 @@
+#pragma once
+typedef unsigned long long uint64;
+
+extern "C" {
+ int fork();
+ int exit(int status);
+ int wait(uint64 status_addr);
+ int pipe(int p[]);
+ int write(int fd, const void *buf, int n);
+ int read(int fd, void *buf, int n);
+ int close(int fd);
+ int kill(int pid);
+ int exec(char *, char **);
+ int open(const char *, int);
+ int mknod(const char *, short, short);
+ int unlink(const char *);
+ int fstat(int fd, void *st);
+ int link(const char *, const char *);
+ int mkdir(const char *);
+ int chdir(const char *);
+ int dup(int fd);
+ int getpid();
+ char *sbrk(int n);
+ int sleep(int ticks);
+ int uptime();
+
+
+ void putc(char c);
+} \ No newline at end of file
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