From 25108684fe235d4296c3cbc480298df209682586 Mon Sep 17 00:00:00 2001 From: stefan Date: Tue, 4 Apr 2023 06:01:50 +0000 Subject: build system and some other stuff --- sys/kern/entry.S | 39 +++++++++++++++++++++++++++++ sys/kern/init.c | 11 ++++++++ sys/kern/kernel.lds | 58 +++++++++++++++++++++++++++++++++++++++++++ sys/kern/kernel_earlyprintf.c | 11 ++++++++ sys/kern/mm/kalloc.c | 40 +++++++++++++++++++++++++++++ 5 files changed, 159 insertions(+) create mode 100644 sys/kern/entry.S create mode 100644 sys/kern/init.c create mode 100644 sys/kern/kernel.lds create mode 100644 sys/kern/kernel_earlyprintf.c create mode 100644 sys/kern/mm/kalloc.c (limited to 'sys/kern') diff --git a/sys/kern/entry.S b/sys/kern/entry.S new file mode 100644 index 0000000..5ad0490 --- /dev/null +++ b/sys/kern/entry.S @@ -0,0 +1,39 @@ +.section ".text.init" + +.globl _start +_start: + csrw satp, zero + csrw sie, zero + csrw sip, zero + + .option push + .option norelax + la gp, __global_pointer$ + .option pop + + la sp, __stack_start + li t1, PAGE_SIZE + mul t1, t1, a0 + add sp, sp, t1 + + li a2, 1 + lla a3, _boot_hart + amoswap.w a3, a2, (a3) + bnez a3, _spin + + la a2, __bss_start + la a3, __bss_end +1: + sd zero, (a2) + addi a2, a2, __SIZEOF_POINTER__ + blt a2, a3, 1b + + call init + + +_spin: + wfi + j _spin + +.section ".data" +_boot_hart: .word 0 diff --git a/sys/kern/init.c b/sys/kern/init.c new file mode 100644 index 0000000..a49c304 --- /dev/null +++ b/sys/kern/init.c @@ -0,0 +1,11 @@ +#include +#include + +void +init(unsigned long hartid, struct fdt_header *fdt) +{ + kernel_earlyputs("booting...\n"); + if (fdt_uint32(fdt->magic) == FDT_HEADER_MAGIC) + kernel_earlyputs("found flattened device tree!\n"); + +} diff --git a/sys/kern/kernel.lds b/sys/kern/kernel.lds new file mode 100644 index 0000000..2e2fe29 --- /dev/null +++ b/sys/kern/kernel.lds @@ -0,0 +1,58 @@ +OUTPUT_ARCH(riscv) +ENTRY(_start) + +SECTIONS +{ + . = LOAD_ADDR; + . = ALIGN(PAGE_SIZE); + + .text : + { + PROVIDE(__text_start = .); + *(.text.init) + *(.text .text.*) + . = ALIGN(8); + PROVIDE(__text_end = .); + } + . = ALIGN(PAGE_SIZE); + + PROVIDE(__global_pointer$ = .); + + .rodata : + { + PROVIDE(__rodata_start = .); + *(.rodata .rodata.*) + . = ALIGN(8); + PROVIDE(__rodata_end = .); + } + + . = ALIGN(PAGE_SIZE); + + .data : + { + PROVIDE(__data_start = .); + *(.sdata .sdata.*) + *(.data .data.*) + *(.readmostly.data) + *(*.data) + . = ALIGN(8); + PROVIDE(__data_end = .); + } + + . = ALIGN(PAGE_SIZE); + + .bss : + { + PROVIDE(__bss_start = .); + *(.sbss .sbss.*) + *(.bss .bss.*) + . = ALIGN(8); + PROVIDE(__bss_end = .); + } + + . = ALIGN(PAGE_SIZE); + + PROVIDE(__stack_start = .); + PROVIDE(__stack_end = __stack_start + (PAGE_SIZE * NPROC)); + PROVIDE(__heap_start = __stack_end); +} diff --git a/sys/kern/kernel_earlyprintf.c b/sys/kern/kernel_earlyprintf.c new file mode 100644 index 0000000..e6c85c4 --- /dev/null +++ b/sys/kern/kernel_earlyprintf.c @@ -0,0 +1,11 @@ +#include +#include + +int +kernel_earlyputs(const char *str) +{ + do { + sbi_console_putchar(*str); + } while (*(str++) != '\0'); +} + diff --git a/sys/kern/mm/kalloc.c b/sys/kern/mm/kalloc.c new file mode 100644 index 0000000..b17d432 --- /dev/null +++ b/sys/kern/mm/kalloc.c @@ -0,0 +1,40 @@ +#include +#include +#include + +typedef struct freenode { + struct freenode *next; +} freenode_t; + +freenode_t *head = NULL; + +void * +kalloc(void) +{ + freenode_t *p = head; + + if (p == NULL) + return NULL; + + head = p->next; + return p; +} + +void * +kzalloc(void) +{ + freenode_t *p = kalloc(); + memset(p, 0, PAGE_SIZE); + return p; +} + +void +kfree(void *p) +{ + if (p == NULL || (unsigned long)p % PAGE_SIZE) + return; + + freenode_t *tmp = head; + head = p; + head->next = tmp; +} -- cgit v1.2.3