From e4d9be3aa746f4bddf67c5e62e1ed374c6d3c046 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Thu, 26 Jun 2025 02:43:43 +0200 Subject: [PATCH] Minimal viable kernel --- .gitignore | 22 +++++++++++++++++++ Makefile | 48 ++++++++++++++++++++++++++++++++++++++++++ entry.S | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ link.ld | 14 ++++++++++++ start.c | 20 ++++++++++++++++++ 5 files changed, 166 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 entry.S create mode 100644 link.ld create mode 100644 start.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b2ad23e --- /dev/null +++ b/.gitignore @@ -0,0 +1,22 @@ +*~ +_* +*.o +*.d +*.asm +*.sym +*.img +*.elf +vectors.S +bootblock +entryother +initcode +initcode.out +kernelmemfs +mkfs +kernel/kernel +user/usys.S +.gdbinit +.vscode +.cache +compile_commands.json +tags diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2a2aade --- /dev/null +++ b/Makefile @@ -0,0 +1,48 @@ +TOOLPREFIX = riscv-none-elf + +CC = $(TOOLPREFIX)-gcc +AS = $(TOOLPREFIX)-as +LD = $(TOOLPREFIX)-ld +OBJCOPY = $(TOOLPREFIX)-objcopy +OBJDUMP = $(TOOLPREFIX)-objdump + +ASFLAGS = -march=rv64gc -mabi=lp64 + +LDFLAGS = -Tlink.ld +LDFLAGS += -m elf64lriscv + +CFLAGS = -Wall -Werror -O +CFLAGS += -mcmodel=medany +CFLAGS += -march=rv64gc -mabi=lp64 +CFLAGS += -ffreestanding -fno-common -nostdlib -mno-relax + +CFLAGS += -I. + +CFLAGS += -fno-stack-protector # Prevents code that needs libc / runtime support +CFLAGS += -MD # Generate header dependency files (.d) +CFLAGS += -fno-pie -no-pie # Fixed address linking +CFLAGS += -ggdb -gdwarf-2 # GDB debug info +CFLAGS += -fno-omit-frame-pointer # More reliable backtraces in GDB + +all: kernel.elf + +kernel.elf: entry.o start.o + @echo LD $@ + @$(LD) $(LDFLAGS) -o $@ $^ + +%.o: %.c + @echo CC $@ + @$(CC) $(CFLAGS) -nostdinc -I. -c $< -o $@ + +%.o: %.S + @echo AS $@ + @$(AS) $(ASFLAGS) -o $@ $< + +qemu: kernel.elf + @echo QEMU $@ + @qemu-system-riscv64 -machine virt -bios none -nographic -kernel kernel.elf + +clean: + rm -f *.o *.elf *.d + +-include *.d diff --git a/entry.S b/entry.S new file mode 100644 index 0000000..377f81f --- /dev/null +++ b/entry.S @@ -0,0 +1,62 @@ +.section .text +.globl _start +_start: + li x1, 0x0 + li x2, 0x0 + li x3, 0x0 + li x4, 0x0 + li x5, 0x0 + li x6, 0x0 + li x7, 0x0 + li x8, 0x0 + li x9, 0x0 + li x10, 0x0 + li x11, 0x0 + li x12, 0x0 + li x13, 0x0 + li x14, 0x0 + li x15, 0x0 + li x16, 0x0 + li x17, 0x0 + li x18, 0x0 + li x19, 0x0 + li x20, 0x0 + li x21, 0x0 + li x22, 0x0 + li x23, 0x0 + li x24, 0x0 + li x25, 0x0 + li x26, 0x0 + li x27, 0x0 + li x28, 0x0 + li x29, 0x0 + li x30, 0x0 + li x31, 0x0 + + li t0, 0x10000000 # UART base address + li t1, 'E' # Character to print + sb t1, 0(t0) + li t1, 'n' + sb t1, 0(t0) + li t1, 't' + sb t1, 0(t0) + li t1, 'r' + sb t1, 0(t0) + li t1, 'y' + sb t1, 0(t0) + li t1, '\n' + sb t1, 0(t0) + + # Set up a stack for C. + la sp, stack0 + li a0, 1024*4 # a0 = 4096 + csrr a1, mhartid # a1 = hart id + addi a1, a1, 1 # hartid + 1 + mul a0, a0, a1 # a0 *= hartid+1 + add sp, sp, a0 # sp += a0 + + # Jump to start() in start.c + call start + +1: + j 1b # Infinite loop diff --git a/link.ld b/link.ld new file mode 100644 index 0000000..baeed47 --- /dev/null +++ b/link.ld @@ -0,0 +1,14 @@ +ENTRY(_start) + +SECTIONS { + . = 0x80000000; + + .text : { + *(.text) + } :text +} + +PHDRS { + text PT_LOAD FLAGS(0x5); /* R + X */ +} + diff --git a/start.c b/start.c new file mode 100644 index 0000000..f14a3c6 --- /dev/null +++ b/start.c @@ -0,0 +1,20 @@ +#define NCPU 3 + +#define UART ((char *)0x10000000) + +void uart_putc(char c) { + *UART = c; +} + +void uart_puts(const char *s) { + while (*s) { + uart_putc(*s++); + } +} + +// Entry.S needs one stack per CPU. +__attribute__((aligned(16))) char stack0[4096 * NCPU]; + +void start() { + uart_puts("Hello Neptune!\n"); +}