From 426da8be0db33405b0cfe53108b65040623972f0 Mon Sep 17 00:00:00 2001 From: Kimplul Date: Sat, 11 Jun 2022 19:08:18 +0300 Subject: continue documentation --- arch/riscv64/kernel/cpu.c | 10 +++++++++- arch/riscv64/kernel/entry.S | 11 ++++++----- arch/riscv64/kernel/proc.c | 6 +++--- 3 files changed, 18 insertions(+), 9 deletions(-) (limited to 'arch/riscv64') diff --git a/arch/riscv64/kernel/cpu.c b/arch/riscv64/kernel/cpu.c index 0558040..91b39b6 100644 --- a/arch/riscv64/kernel/cpu.c +++ b/arch/riscv64/kernel/cpu.c @@ -3,9 +3,17 @@ * riscv64 implementation of cpu handling. */ +#include #include id_t cpu_id() { - return 0; /* VERY MUCH TEMP */ + /* yes, slightly weird situation where cur_tcb() call cpu_id() which + * gets the current tcb and returns the cpu id in the tcb, so that that + * id can be used to get the tcb we want. I might try to work out + * something slightly smarter, although this is likely not going to have + * basically any kind of importance for perfomance. */ + struct tcb *t; + __asm__ volatile ("mv %0, tp\n" : "=r" (t) ::); + return t->cpu_id; } diff --git a/arch/riscv64/kernel/entry.S b/arch/riscv64/kernel/entry.S index a5427a7..ee7b10f 100644 --- a/arch/riscv64/kernel/entry.S +++ b/arch/riscv64/kernel/entry.S @@ -25,8 +25,8 @@ _save_context: sr sp, offsetof_sp(tp) mv sp, tp - /* set scratch to 0 and move thread pointer back */ - csrrw tp, CSR_SSCRATCH, x0 + /* move thread pointer back */ + csrrw tp, CSR_SSCRATCH, tp /* save registers */ sr ra, offsetof_ra(sp) @@ -60,6 +60,10 @@ _save_context: sr t5, offsetof_t5(sp) sr t6, offsetof_t6(sp) + /* get current tcb into tp and set scratch to 0 so we can figure out if + * exception occured in kernel or userspace */ + csrrw tp, CSR_SSCRATCH, x0 + /* load supervisor cause */ csrr s4, CSR_SCAUSE /* interrupts fall through, exceptions jump */ @@ -85,9 +89,6 @@ handle_syscall: /* TODO: is a whole function call necessary? */ mv s0, a0 mv s1, a1 - /* get current tcb */ - call cur_tcb - mv tp, a0 /* get associated kernel stack */ mv sp, tp addi sp, sp, -sizeof_registers diff --git a/arch/riscv64/kernel/proc.c b/arch/riscv64/kernel/proc.c index 09d694f..a85ae6f 100644 --- a/arch/riscv64/kernel/proc.c +++ b/arch/riscv64/kernel/proc.c @@ -33,15 +33,15 @@ stat_t set_ipc(struct tcb *t, id_t pid, id_t tid) return OK; } -stat_t set_thread(struct tcb *t, vm_t stack) +stat_t set_thread(struct tcb *t) { /* get location of registers in memory */ /** \todo check alignment, should be fine but just to be sure */ struct riscv_regs *r = (struct riscv_regs *)(--t); /* insert important values into register slots */ - r->sp = (long)stack; - r->tp = (long)t; + r->sp = (long)t->thread_stack_top; + r->tp = (long)t->thread_storage; return OK; } -- cgit v1.3