From d4849c98408e8699e35da2eb0f13f747f17147c6 Mon Sep 17 00:00:00 2001 From: jenra-uwu Date: Sun, 25 Jul 2021 15:09:53 -0400 Subject: [PATCH] Initial commit --- .gitignore | 1 + Makefile | 13 +++++++++++++ kernel.ld | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/boot.s | 17 +++++++++++++++++ src/kernel.c | 8 ++++++++ src/opensbi.h | 28 ++++++++++++++++++++++++++++ src/opensbi.s | 26 ++++++++++++++++++++++++++ 7 files changed, 137 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 kernel.ld create mode 100644 src/boot.s create mode 100644 src/kernel.c create mode 100644 src/opensbi.h create mode 100644 src/opensbi.s diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..533e957 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +kernel diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6d5f23a --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +CODE=src/ +CC=riscv64-unknown-elf-gcc +CFLAGS=-march=rv64gc -mabi=lp64d -static -mcmodel=medany -fvisibility=hidden -nostdlib -nostartfiles -Tkernel.ld -g -Wall -Wextra +EMU=qemu-system-riscv64 +EFLAGS=-machine virt -cpu rv64 -bios opensbi-riscv64-generic-fw_dynamic.bin -m 256m -nographic -global virtio-mmio.force-legacy=false -s #-S + +.PHONY: all clean run + +all: $(CODE)*.s $(CODE)*.c + $(CC) $(CFLAGS) $? -o kernel + +run: + $(EMU) $(EFLAGS) -kernel kernel diff --git a/kernel.ld b/kernel.ld new file mode 100644 index 0000000..cb22f19 --- /dev/null +++ b/kernel.ld @@ -0,0 +1,44 @@ +OUTPUT_ARCH( "riscv" ) +OUTPUT_FORMAT( "elf64-littleriscv" ) +ENTRY( _start ) +SECTIONS +{ + /* text: text code section */ + . = 0x80200000; + PROVIDE(text_start = .); + .text : { *(.text) } + + /* data: Initialized data segment */ + .gnu_build_id : { *(.note.gnu.build-id) } + + . = ALIGN(4096); + PROVIDE(data_start = .); + .data : { *(.data) } + + . = ALIGN(4096); + PROVIDE(ro_data_start = .); + .rodata : { *(.rodata) } + + . = ALIGN(4096); + PROVIDE(sdata_start = .); + .sdata : { *(.sdata) } + .debug : { *(.debug) } + + . = ALIGN(4096); + PROVIDE(stack_start = .); + . += 0x4000; + PROVIDE(stack_top = .); + + /* Heap */ + . = ALIGN(4096); + PROVIDE(heap_bottom = .); + . += 0x80000; + + /* Pages */ + . = ALIGN(4096); + PROVIDE(pages_bottom = .); + + /* End of uninitalized data segement */ +} + + diff --git a/src/boot.s b/src/boot.s new file mode 100644 index 0000000..77a4aaf --- /dev/null +++ b/src/boot.s @@ -0,0 +1,17 @@ +.section .text +.global _start + +# a0 - current hart id +# a1 - pointer to flattened device tree + +_start: + # Initialise stack pointer + + la sp, stack_top + mv fp, sp + + j kinit + +finish: + j finish + diff --git a/src/kernel.c b/src/kernel.c new file mode 100644 index 0000000..fd21f8d --- /dev/null +++ b/src/kernel.c @@ -0,0 +1,8 @@ +#include "opensbi.h" + +void kinit(unsigned long long hartid, void* fdt) { + sbi_console_putchar('a'); + + while(1); +} + diff --git a/src/opensbi.h b/src/opensbi.h new file mode 100644 index 0000000..0f44661 --- /dev/null +++ b/src/opensbi.h @@ -0,0 +1,28 @@ +#ifndef OPENSBI_H +#define OPENSBI_H + +enum { + SBIRET_ERROR_CODE_SUCCESS = 0, + SBIRET_ERROR_CODE_FAILED = -1, + SBIRET_ERROR_CODE_UNSUPPORTED = -2, + SBIRET_ERROR_CODE_INVALID_PARAMETER = -3, + SBIRET_ERROR_CODE_DENIED = -4, + SBIRET_ERROR_CODE_INVALID_ADDRESS = -5, + SBIRET_ERROR_CODE_ALREADY_AVAILABLE = -6 +}; + +struct sbiret { + unsigned long error; + unsigned long value; +}; + +// sbi_console_putchar(char) -> void +// Puts a character onto the UART port. +void sbi_console_putchar(char); + +// sbi_console_getchar() -> int +// Puts a character onto the UART port. +int sbi_console_getchar(); + +#endif /* OPENSBI_H */ + diff --git a/src/opensbi.s b/src/opensbi.s new file mode 100644 index 0000000..15d497d --- /dev/null +++ b/src/opensbi.s @@ -0,0 +1,26 @@ +.section .text + +.global sbi_console_putchar +.global sbi_console_getchar + +# EIDs are stored in a7 +# FIDs are stored in a6 + +# sbi_console_putchar(char) -> void +# Puts a character onto the UART port. + +sbi_console_putchar: + li a6, 0 + li a7, 1 + ecall + ret + +# sbi_console_getchar() -> int +# Gets a character from the UART port. + +sbi_console_getchar: + li a6, 0 + li a7, 2 + ecall + ret +