diff options
-rw-r--r-- | os/.gitignore | 1 | ||||
-rw-r--r-- | os/boot.S | 31 | ||||
-rw-r--r-- | os/build.bat | 7 | ||||
-rw-r--r-- | os/linker.ld | 23 | ||||
-rw-r--r-- | os/notes.txt | 32 | ||||
-rw-r--r-- | os/run.bat | 5 |
6 files changed, 99 insertions, 0 deletions
diff --git a/os/.gitignore b/os/.gitignore new file mode 100644 index 0000000..3722ac6 --- /dev/null +++ b/os/.gitignore @@ -0,0 +1 @@ +build/
diff --git a/os/boot.S b/os/boot.S new file mode 100644 index 0000000..afb966c --- /dev/null +++ b/os/boot.S @@ -0,0 +1,31 @@ +.option norvc
+
+.section .init
+
+.equ UART_BASE, 0x10000000
+
+.global kstart
+kstart:
+ la a0, _str
+ call _println
+
+_halt:
+ wfi
+ j _halt
+
+_println:
+ li t0, UART_BASE
+_println_loop:
+ lb t1, 0(a0)
+ beqz t1, _end_println_loop
+ sb t1, 0(t0)
+ addi a0, a0, 1
+ j _println_loop
+_end_println_loop:
+ li t1, '\n'
+ sb t1, 0(t0)
+ ret
+
+.section .rodata
+
+_str: .string "Hello, world!"
diff --git a/os/build.bat b/os/build.bat new file mode 100644 index 0000000..56f6cf3 --- /dev/null +++ b/os/build.bat @@ -0,0 +1,7 @@ +@echo off
+
+IF NOT EXIST build mkdir build
+
+clang --target=riscv32 -march=rv32im -nostdlib boot.S -c -o build\boot.o
+
+ld.lld -T linker.ld build\boot.o -o build\kernel.elf
diff --git a/os/linker.ld b/os/linker.ld new file mode 100644 index 0000000..d74f640 --- /dev/null +++ b/os/linker.ld @@ -0,0 +1,23 @@ +ENTRY(kstart);
+
+MEMORY {
+ ram (wxa) : ORIGIN = 0x80000000, LENGTH = 128M
+}
+
+PHDRS {
+ text PT_LOAD;
+ rodata PT_LOAD;
+}
+
+SECTIONS {
+ .text : ALIGN(4K) {
+ *(.init);
+ *(.text);
+ } >ram AT>ram :text
+
+ .rodata : ALIGN(4K) {
+ *(.rodata);
+ } >ram AT>ram :rodata
+}
+
+ # @Todo .bss (clear), .data, etc
diff --git a/os/notes.txt b/os/notes.txt new file mode 100644 index 0000000..ccd55a3 --- /dev/null +++ b/os/notes.txt @@ -0,0 +1,32 @@ +qemu-system-riscv32 -machine virt -cpu rv32 -smp 1 -m 128M -nographic -serial mon:stdio -bios none -kernel kernel.elf
+
+qemu-system-riscv32 -machine virt -cpu rv32 -smp 1 -m 128M -bios none -kernel kernel.elf
+
+cmd /c exit
+
+clang --target=riscv32 -march=rv32im -nostdlib boot.S -c -o boot.o
+
+ld.lld -T linker.ld boot.o -o kernel.elf
+
+static const MemMapEntry virt_memmap[] = {
+ [VIRT_DEBUG] = { 0x0, 0x100 },
+ [VIRT_MROM] = { 0x1000, 0xf000 },
+ [VIRT_TEST] = { 0x100000, 0x1000 },
+ [VIRT_RTC] = { 0x101000, 0x1000 },
+ [VIRT_CLINT] = { 0x2000000, 0x10000 },
+ [VIRT_ACLINT_SSWI] = { 0x2F00000, 0x4000 },
+ [VIRT_PCIE_PIO] = { 0x3000000, 0x10000 },
+ [VIRT_PLATFORM_BUS] = { 0x4000000, 0x2000000 },
+ [VIRT_PLIC] = { 0xc000000, VIRT_PLIC_SIZE(VIRT_CPUS_MAX * 2) },
+ [VIRT_APLIC_M] = { 0xc000000, APLIC_SIZE(VIRT_CPUS_MAX) },
+ [VIRT_APLIC_S] = { 0xd000000, APLIC_SIZE(VIRT_CPUS_MAX) },
+ [VIRT_UART0] = { 0x10000000, 0x100 },
+ [VIRT_VIRTIO] = { 0x10001000, 0x1000 },
+ [VIRT_FW_CFG] = { 0x10100000, 0x18 },
+ [VIRT_FLASH] = { 0x20000000, 0x4000000 },
+ [VIRT_IMSIC_M] = { 0x24000000, VIRT_IMSIC_MAX_SIZE },
+ [VIRT_IMSIC_S] = { 0x28000000, VIRT_IMSIC_MAX_SIZE },
+ [VIRT_PCIE_ECAM] = { 0x30000000, 0x10000000 },
+ [VIRT_PCIE_MMIO] = { 0x40000000, 0x40000000 },
+ [VIRT_DRAM] = { 0x80000000, 0x0 },
+};
diff --git a/os/run.bat b/os/run.bat new file mode 100644 index 0000000..6c0b7b1 --- /dev/null +++ b/os/run.bat @@ -0,0 +1,5 @@ +@echo off
+
+qemu-system-riscv32 -machine virt -cpu rv32 -smp 1 -m 128M -nographic -serial mon:stdio -bios none -kernel build\kernel.elf
+
+cmd /c exit
|