aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKimplul <kimi.h.kuparinen@gmail.com>2022-04-24 13:54:41 +0300
committerKimplul <kimi.h.kuparinen@gmail.com>2022-04-24 13:54:41 +0300
commitbe3696bd5853819212b1240751efd8d85f282c68 (patch)
treefd58279b90ce82c073c6f8d9a929ceb00bec5703
parentd4c420689b1872f6fb22f421dfd27704ee94951a (diff)
downloadkmi-be3696bd5853819212b1240751efd8d85f282c68.tar.gz
kmi-be3696bd5853819212b1240751efd8d85f282c68.zip
start initial irq handling
+ Still lots todo, and I'm not entirely sure how I should handle program space switches (mainly since the kernel uses the process' stack, maybe it shouldn't?)
-rwxr-xr-xarch/riscv64/conf/initbin968 -> 1096 bytes
-rw-r--r--arch/riscv64/conf/initrdbin1536 -> 1536 bytes
-rw-r--r--arch/riscv64/conf/s.S5
-rw-r--r--arch/riscv64/include/csr.h2
-rw-r--r--arch/riscv64/kernel/head.S64
-rw-r--r--arch/riscv64/kernel/irq.c4
-rw-r--r--arch/riscv64/kernel/proc.c2
-rw-r--r--common/mem_regions.c7
-rw-r--r--common/proc.c6
-rw-r--r--include/apos/tcb.h4
10 files changed, 86 insertions, 8 deletions
diff --git a/arch/riscv64/conf/init b/arch/riscv64/conf/init
index f05254f..c415457 100755
--- a/arch/riscv64/conf/init
+++ b/arch/riscv64/conf/init
Binary files differ
diff --git a/arch/riscv64/conf/initrd b/arch/riscv64/conf/initrd
index 90344c1..6d2ac10 100644
--- a/arch/riscv64/conf/initrd
+++ b/arch/riscv64/conf/initrd
Binary files differ
diff --git a/arch/riscv64/conf/s.S b/arch/riscv64/conf/s.S
index 80a3a11..1344f69 100644
--- a/arch/riscv64/conf/s.S
+++ b/arch/riscv64/conf/s.S
@@ -1,4 +1,9 @@
.text
.global _start
_start:
+li a0, 1
+li a1, 2
+li a2, 3
+li a3, 4
+li a4, 5
ecall
diff --git a/arch/riscv64/include/csr.h b/arch/riscv64/include/csr.h
index 7f9fc1f..39e8dc9 100644
--- a/arch/riscv64/include/csr.h
+++ b/arch/riscv64/include/csr.h
@@ -1,8 +1,6 @@
#ifndef APOS_CSR_H
#define APOS_CSR_H
-#include <apos/utils.h>
-
#define SATP_MODE_Sv32 0x80000000
#define SATP_MODE_Sv39 0x8000000000000000
#define SATP_MODE_Sv48 0x9000000000000000
diff --git a/arch/riscv64/kernel/head.S b/arch/riscv64/kernel/head.S
new file mode 100644
index 0000000..b8ad295
--- /dev/null
+++ b/arch/riscv64/kernel/head.S
@@ -0,0 +1,64 @@
+#include <csr.h>
+
+.section .text
+.global handle_irq
+handle_irq:
+ addi sp,sp,-144
+ sd ra,136(sp)
+ sd t0,128(sp)
+ sd t1,120(sp)
+ sd t2,112(sp)
+ sd s0,104(sp)
+ sd a0,96(sp)
+ sd a1,88(sp)
+ sd a2,80(sp)
+ sd a3,72(sp)
+ sd a4,64(sp)
+ sd a5,56(sp)
+ sd a6,48(sp)
+ sd a7,40(sp)
+ sd t3,32(sp)
+ sd t4,24(sp)
+ sd t5,16(sp)
+ sd t6,8(sp)
+ addi s0,sp,144
+ /* load supervisor cause */
+ csrr s4, CSR_SCAUSE
+ /* interrupts fall through, exceptions jump */
+ bge s4, zero, handle_exception
+ /* TODO: handle interrupts */
+ j restore_all
+
+handle_exception:
+ li t0, EXC_SYSCALL
+ /* system exceptions fall through, syscalls jump */
+ beq s4, t0, handle_syscall
+ j restore_all
+
+handle_syscall:
+ jal syscall_dispatch
+ j restore_noreturn
+
+restore_all:
+ ld a0,96(sp)
+
+restore_noreturn:
+ ld ra,136(sp)
+ ld t0,128(sp)
+ ld t1,120(sp)
+ ld t2,112(sp)
+ ld s0,104(sp)
+ ld a1,88(sp)
+ ld a2,80(sp)
+ ld a3,72(sp)
+ ld a4,64(sp)
+ ld a5,56(sp)
+ ld a6,48(sp)
+ ld a7,40(sp)
+ ld t3,32(sp)
+ ld t4,24(sp)
+ ld t5,16(sp)
+ ld t6,8(sp)
+ addi sp,sp,144
+ /* assume supervisor for now */
+ sret
diff --git a/arch/riscv64/kernel/irq.c b/arch/riscv64/kernel/irq.c
index 27e0bd0..d350426 100644
--- a/arch/riscv64/kernel/irq.c
+++ b/arch/riscv64/kernel/irq.c
@@ -13,10 +13,8 @@ void init_irq(void *fdt)
info("CSR_SIE: %lx\n", s);
}
-__aligned(4) void handle_irq()
+void handle_sys_irq()
{
- while (1)
- ;
}
/* very simple for now */
diff --git a/arch/riscv64/kernel/proc.c b/arch/riscv64/kernel/proc.c
index ef8f24e..66325ec 100644
--- a/arch/riscv64/kernel/proc.c
+++ b/arch/riscv64/kernel/proc.c
@@ -5,7 +5,7 @@
stat_t jump_to_userspace(struct tcb *t, int argc, char **argv)
{
csr_write(CSR_SEPC, t->entry);
- __asm__ volatile("mv sp, %0\n" : "=r"(t->proc_stack)::"memory");
+ __asm__ volatile("mv sp, %0\n" ::"r"(t->proc_stack_top) : "memory");
__asm__ volatile("sret\n" ::: "memory");
/* we should never reach this */
return ERR_ADDR;
diff --git a/common/mem_regions.c b/common/mem_regions.c
index e994b0e..bbd9a3b 100644
--- a/common/mem_regions.c
+++ b/common/mem_regions.c
@@ -291,8 +291,11 @@ static vm_t __partition_region(struct mem_region_root *r, struct mem_region *m,
* fs server */
vm_t alloc_region(struct mem_region_root *r, size_t size, size_t *actual_size)
{
- *actual_size = align_up(size, BASE_PAGE_SIZE);
- size_t pages = __page(*actual_size);
+ size_t asize = align_up(size, BASE_PAGE_SIZE);
+ if (actual_size)
+ *actual_size = asize;
+
+ size_t pages = __page(asize);
/* find best fitting, alignment etc. */
size_t align = 0;
diff --git a/common/proc.c b/common/proc.c
index e0b8c70..ea3ab5d 100644
--- a/common/proc.c
+++ b/common/proc.c
@@ -46,12 +46,18 @@ stat_t init_proc(void *fdt, struct vm_branch *b)
if (!t->proc_stack)
return ERR_ADDR;
+ t->proc_stack_top = t->proc_stack + __proc_stack_size;
+
t->call_stack = setup_call_stack(t, __call_stack_size);
if (!t->call_stack)
return ERR_ADDR;
+ t->call_stack_top = t->call_stack + __call_stack_size;
flush_tlb();
+ /* set current tcb */
+ use_tcb(t);
+
/* TODO: move fdt into process space */
return jump_to_userspace(t, 1, 0);
}
diff --git a/include/apos/tcb.h b/include/apos/tcb.h
index 7ed07c5..821d822 100644
--- a/include/apos/tcb.h
+++ b/include/apos/tcb.h
@@ -16,8 +16,12 @@ struct tcb {
id_t tid;
vm_t callback;
+
vm_t proc_stack;
+ vm_t proc_stack_top;
+
vm_t call_stack;
+ vm_t call_stack_top;
/* entry point */
vm_t entry;