diff --git a/examples/kernel-multiboot2-grub/linker.ld b/examples/kernel-multiboot2-grub/linker.ld index 699fefd..3221394 100644 --- a/examples/kernel-multiboot2-grub/linker.ld +++ b/examples/kernel-multiboot2-grub/linker.ld @@ -1,7 +1,7 @@ OUTPUT_ARCH("i386") ENTRY(_start) -_kernel_offset = 0xC0000000; /* 3 GB */ +_kernel_offset = 0xC0000000; /* 3 GiB */ _kernel_phys_base = 4M; _kernel_virt_base = (_kernel_phys_base + _kernel_offset); diff --git a/examples/kernel-multiboot2-limine/linker.ld b/examples/kernel-multiboot2-limine/linker.ld index 699fefd..3221394 100644 --- a/examples/kernel-multiboot2-limine/linker.ld +++ b/examples/kernel-multiboot2-limine/linker.ld @@ -1,7 +1,7 @@ OUTPUT_ARCH("i386") ENTRY(_start) -_kernel_offset = 0xC0000000; /* 3 GB */ +_kernel_offset = 0xC0000000; /* 3 GiB */ _kernel_phys_base = 4M; _kernel_virt_base = (_kernel_phys_base + _kernel_offset); diff --git a/examples/kernel-riscv64/.gitignore b/examples/kernel-riscv64/.gitignore new file mode 100644 index 0000000..96302b7 --- /dev/null +++ b/examples/kernel-riscv64/.gitignore @@ -0,0 +1 @@ +/kernel.elf diff --git a/examples/kernel-riscv64/Makefile b/examples/kernel-riscv64/Makefile index e765faf..7ae55f6 100644 --- a/examples/kernel-riscv64/Makefile +++ b/examples/kernel-riscv64/Makefile @@ -7,5 +7,31 @@ CC = $(CCPREFIX)gcc QEMU = qemu-system-riscv64 -run: - $(QEMU) -machine virt -serial stdio +KERNEL = kernel.elf +LINKERSCR = linker.ld + +CFLAGS = \ + -std=c99 \ + -pedantic \ + -Wall \ + -Wextra \ + -Werror \ + -ffreestanding \ + -mcmodel=medany + +OBJS = main.c.o start.S.o + +run: $(KERNEL) + $(QEMU) -machine virt -bios $< -serial stdio -display none + +clean: + rm -f $(KERNEL) $(OBJS) + +$(KERNEL): $(LINKERSCR) $(OBJS) + $(CC) -T $(LINKERSCR) -o $@ $(OBJS) -nostdlib -lgcc + +%.c.o: %.c + $(CC) -c $< -o $@ $(CFLAGS) + +%.S.o: %.S + $(AS) $< -o $@ diff --git a/examples/kernel-riscv64/linker.ld b/examples/kernel-riscv64/linker.ld new file mode 100644 index 0000000..37c8724 --- /dev/null +++ b/examples/kernel-riscv64/linker.ld @@ -0,0 +1,26 @@ +ENTRY(_start); + +. = 0x80000000; + +SECTIONS { + /* Include entry point at start of binary */ + .text : ALIGN(4K) { + *(.init); + *(.text); + } + .bss : ALIGN(4K) { + PROVIDE(bss_start = .); + *(.bss); + . += 4096; + PROVIDE(stack_top = .); + . += 4096; + PROVIDE(global_pointer = .); + PROVIDE(bss_end = .); + } + .rodata : ALIGN(4K) { + *(.rodata); + } + .data : ALIGN(4K) { + *(.data); + } +} diff --git a/examples/kernel-riscv64/main.c b/examples/kernel-riscv64/main.c new file mode 100644 index 0000000..3dc692b --- /dev/null +++ b/examples/kernel-riscv64/main.c @@ -0,0 +1,19 @@ +#include +#include + +static unsigned char *const uart = (unsigned char*)0x10000000; + +static void putchar(char c) { + *uart = c; +} + +static void print(const char * str) { + while (*str != '\0') { + putchar(*str); + str++; + } +} + +void main() { + print("Hello world!\r\n"); +} diff --git a/examples/kernel-riscv64/start.S b/examples/kernel-riscv64/start.S new file mode 100644 index 0000000..82c64e9 --- /dev/null +++ b/examples/kernel-riscv64/start.S @@ -0,0 +1,37 @@ +.section .init + +.option norvc + +.type _start, @function +.global _start +_start: + .cfi_startproc + +.option push +.option norelax + la gp, global_pointer +.option pop + + /* Reset satp */ + csrw satp, zero + + /* Setup stack */ + la sp, stack_top + + /* Clear the BSS section */ + la t5, bss_start + la t6, bss_end +bss_clear: + sd zero, (t5) + addi t5, t5, 8 + bgeu t5, t6, bss_clear + + la t0, main + csrw mepc, t0 + + /* Jump to kernel! */ + tail main + + .cfi_endproc + +.end