summaryrefslogtreecommitdiff
path: root/sys/kern
diff options
context:
space:
mode:
authorstefan <stefan@s00.xyz>2023-04-04 06:01:50 +0000
committerstefan <stefan@s00.xyz>2023-04-04 06:01:50 +0000
commit25108684fe235d4296c3cbc480298df209682586 (patch)
tree59f051e13154764da2b98dfdc1ce66e363d801da /sys/kern
parent254ffbc869c2376e77ecc298dfd396f077e9d58a (diff)
downloadsv-25108684fe235d4296c3cbc480298df209682586.tar.gz
build system and some other stuff
Diffstat (limited to 'sys/kern')
-rw-r--r--sys/kern/entry.S39
-rw-r--r--sys/kern/init.c11
-rw-r--r--sys/kern/kernel.lds58
-rw-r--r--sys/kern/kernel_earlyprintf.c11
-rw-r--r--sys/kern/mm/kalloc.c40
5 files changed, 159 insertions, 0 deletions
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 <fdt.h>
+#include <kernel_earlyprintf.h>
+
+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 <kernel_earlyprintf.h>
+#include <sbi.h>
+
+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 <stddef.h>
+#include <string.h>
+#include <mm/kalloc.h>
+
+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;
+}