summaryrefslogtreecommitdiff
path: root/sys/kern
diff options
context:
space:
mode:
authorstefan <stefan@s00.xyz>2023-04-19 20:50:10 -0400
committerstefan <stefan@s00.xyz>2023-04-19 20:50:10 -0400
commit83e17e29456ec9b6d45f4d9f2634eb280c6f414f (patch)
tree004a9b2a7cd3f0c7bb4224b59204680bd5d79681 /sys/kern
parentaf1ce4b2e637ceb418ea72d51c49a3eee276a938 (diff)
downloadsv-83e17e29456ec9b6d45f4d9f2634eb280c6f414f.tar.gz
ticket locks
Diffstat (limited to 'sys/kern')
-rw-r--r--sys/kern/entry.S69
-rw-r--r--sys/kern/init.c51
-rw-r--r--sys/kern/kernel.lds58
-rw-r--r--sys/kern/mm/kalloc.c66
-rw-r--r--sys/kern/printf.c101
-rw-r--r--sys/kern/spinlock.c38
6 files changed, 0 insertions, 383 deletions
diff --git a/sys/kern/entry.S b/sys/kern/entry.S
deleted file mode 100644
index 9bf72ea..0000000
--- a/sys/kern/entry.S
+++ /dev/null
@@ -1,69 +0,0 @@
-.section ".text.init"
-
-// entrypoint for boot hart(s). a0: hartid, a1: *fdt
-.globl _start
-
-
-// entrypoint for other harts. a0: hartid, a1: unused for now
-.global _mpentry
-
-_start:
- /* disable interrupts and paging */
- csrw satp, zero
- csrw sie, zero
- csrw sip, zero
-
- .option push
- .option norelax
- la gp, __global_pointer$
- .option pop
-
- /* setup stack.
- * stack grows toward lower address so sp will mark end of frame.
- */
- addi t0, a0, 1
- li t1, PAGE_SIZE
- la sp, __stack_start
- mul t1, t1, t0
- add sp, sp, t1
-
- /* setup thread pointer */
- mv tp, a0
-
- /* the label _boot_hart is shared between threads. after the first atomic swap, all other harts will branch to mpentry */
- li a2, 1
- lla a3, _boot_hart
- amoswap.w a3, a2, (a3)
- bnez a3, _mpentry
-
- /* clear the bss section */
- la a2, __bss_start
- la a3, __bss_end
-1:
- sd zero, (a2)
- addi a2, a2, __SIZEOF_POINTER__
- blt a2, a3, 1b
-2:
- call init
- j _spin
-
-_mpentry:
- /* satp = 0, sstatus.sie = 0, a0 = hartid, a1 = opaque */
-
- call mpinit
- /* shouldn't reach this point */
- j _spin
-
-_spin:
- wfi
- j _spin
-
-.section ".data"
-_boot_hart:
- .word 0
-
-.section ".rodata"
-/* linker imports */
-.globl HEAP_START
-HEAP_START: .dword __heap_start
-
diff --git a/sys/kern/init.c b/sys/kern/init.c
deleted file mode 100644
index a58d3bc..0000000
--- a/sys/kern/init.c
+++ /dev/null
@@ -1,51 +0,0 @@
-#include <fdt.h>
-#include <mm/kalloc.h>
-#include <printf.h>
-#include <spinlock.h>
-#include <sbi.h>
-#include <stdint.h>
-
-extern uint64_t HEAP_START;
-
-#define HLT()\
- for (;;)\
- asm("wfi")
-
-void
-init(unsigned long hartid, struct fdt_header *fdt)
-{
-
- printf("booting from hart #%d\n", hartid);
- asm volatile ("mv tp, %0" : : "r"(hartid));
- if (fdt_uint32(fdt->magic) == FDT_HEADER_MAGIC)
- printf("found flattened device tree at %p!\n", (uint64_t)fdt);
-
- printf("parsing device tree!\n");
- fdt_walk(fdt);
-
- printf("setting up the heap at %p\n", HEAP_START);
- kalloc_init();
- printf("done!\n");
- // printf("printing free pages:\n");
- ///walkfree();
- printf("bringing up other harts...\n");
- // todo: detect harts from device tree can also be used for more conservative stack allocation
- for (int i = 0; i < NPROC; ++i) {
- if (i == hartid) {
- printf("skipping hart #%d\n", i);
- continue;
- }
- printf("starting hart #%d\n", i);
- struct sbiret r = _start_hart(i, (unsigned long)LOAD_ADDR);
- if (r.err != SBI_SUCCESS)
- printf("ERROR");
- }
-}
-
-void
-mpinit(unsigned long hartid)
-{
- printf("mpinit: %d", hartid);
- HLT();
-}
-
diff --git a/sys/kern/kernel.lds b/sys/kern/kernel.lds
deleted file mode 100644
index 2e2fe29..0000000
--- a/sys/kern/kernel.lds
+++ /dev/null
@@ -1,58 +0,0 @@
-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/mm/kalloc.c b/sys/kern/mm/kalloc.c
deleted file mode 100644
index bfd4f3f..0000000
--- a/sys/kern/mm/kalloc.c
+++ /dev/null
@@ -1,66 +0,0 @@
-#include <stddef.h>
-#include <stdint.h>
-#include <string.h>
-#include <printf.h>
-#include <mm/kalloc.h>
-
-extern uint64_t HEAP_START;
-
-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)
- return;
-
- freenode_t *tmp = head;
- head = p;
- head->next = tmp;
-}
-
-void
-kalloc_init(void)
-{
- freenode_t *p = (freenode_t*)HEAP_START;
- for (;(unsigned long)p + PAGE_SIZE <= (HEAP_START + 0x100000); p += PAGE_SIZE) {
- kfree(p);
- }
-}
-
-void
-walkfree(void)
-{
- freenode_t *node = head;
- int nfree = 0;
- while (node) {
- printf("freenode at %p\n", node);
- node = node->next;
- nfree++;
- }
- printf("found and printed %d free pages!\n", nfree);
-}
diff --git a/sys/kern/printf.c b/sys/kern/printf.c
deleted file mode 100644
index 89add9e..0000000
--- a/sys/kern/printf.c
+++ /dev/null
@@ -1,101 +0,0 @@
-#include <printf.h>
-#include <stdint.h>
-#include <spinlock.h>
-#include <sbi.h>
-#include <stdarg.h>
-
-static char digits[] = "0123456789abcdef";
-
-struct spinlock mutex;
-int locked = 0;
-
-int
-puts(const char *str)
-{
- do {
- sbi_console_putchar(*str);
- } while (*(str++) != '\0');
-}
-
-#define PUTCHAR(c) sbi_console_putchar(c)
-
-static void
-printint(int xx, int base, int sign)
-{
- char buf[16];
- int i;
- unsigned int x;
-
- if (sign && (sign = xx < 0))
- x = -xx;
- else
- x = xx;
-
- i = 0;
- do {
- buf[i++] = digits[x % base];
- } while ((x /= base) != 0);
-
- if (sign)
- buf[i++] = '-';
- while (--i >= 0)
- PUTCHAR(buf[i]);
-
-}
-
-static void
-printptr(uint64_t x)
-{
- int i;
- puts("0x");
- for (i = 0; i < (sizeof(uint64_t) * 2); i++, x <<= 4)
- PUTCHAR(digits[x >> (sizeof(int64_t) * 8 - 4)]);
-}
-
-void
-printf(const char *fmt, ...)
-{
- va_list ap;
- int i, c, _locked;
- char *s;
-
- _locked = locked;
- if (_locked)
- acquire(&mutex);
-
- va_start(ap, fmt);
-
- for (i = 0; (c = fmt[i] & 0xff) != 0; i++) {
- if (c != '%') {
- PUTCHAR(c);
- continue;
- }
- c = fmt[++i] & 0xff;
- if (!c)
- break;
-
- switch (c) {
- case 'd':
- case 'i':
- printint(va_arg(ap, int), 10, 1);
- break;
- case 'x':
- printint(va_arg(ap, int), 16, 1);
- break;
- case 'p':
- printptr(va_arg(ap, uint64_t));
- break;
- case '%':
- PUTCHAR('%');
- break;
- }
- }
- if (_locked)
- release(&mutex);
-}
-
-void printf_init(void)
-{
- initlock(&mutex);
- locked = 1;
-}
diff --git a/sys/kern/spinlock.c b/sys/kern/spinlock.c
deleted file mode 100644
index ade270f..0000000
--- a/sys/kern/spinlock.c
+++ /dev/null
@@ -1,38 +0,0 @@
-#include <asm.h>
-#include <spinlock.h>
-
-void
-initlock(struct spinlock *l)
-{
- l->locked = 0;
-}
-
-void
-acquire(struct spinlock *l)
-{
- sie_disable();
-
- if (l->locked)
- return;
-
- while (__sync_lock_test_and_set(&l->locked, 1))
- ;
-
- __sync_synchronize();
-}
-
-void
-release(struct spinlock *l)
-{
- sie_disable(); // avoid deadlock
-
- if (l->locked)
- return; // interrupts are still disabled, what to do here?
-
- /* fence */
- __sync_synchronize();
-
- __sync_lock_release(&l->locked);
-
- sie_enable();
-}