You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
riscv-pke/kernel/strap.c

55 lines
1.9 KiB

/*
* Utility functions for trap handling in Supervisor mode.
*/
#include "riscv.h"
#include "process.h"
#include "strap.h"
#include "syscall.h"
#include "spike_interface/spike_utils.h"
//
// handling the syscalls. will call do_syscall() defined in kernel/syscall.c
//
static void handle_syscall(trapframe *tf) {
// tf->epc points to the address that our computer will jump to after the trap handling.
// for a syscall, we should return to the NEXT instruction after its handling.
// in RV64G, each instruction occupies exactly 32 bits (i.e., 4 Bytes)
tf->epc += 4;
// TODO (lab1_1): remove the panic call below, and call do_syscall (defined in
// kernel/syscall.c) to conduct real operations of the kernel side for a syscall.
// IMPORTANT: return value should be returned to user app, or else, you will encounter
// problems in later experiments!
panic( "call do_syscall to accomplish the syscall and lab1_1 here.\n" );
}
//
// kernel/smode_trap.S will pass control to smode_trap_handler, when a trap happens
// in S-mode.
//
void smode_trap_handler(void) {
// make sure we are in User mode before entering the trap handling.
// we will consider other previous case in lab1_3 (interrupt).
if ((read_csr(sstatus) & SSTATUS_SPP) != 0) panic("usertrap: not from user mode");
assert(current);
// save user process counter.
current->trapframe->epc = read_csr(sepc);
// if the cause of trap is syscall from user application.
// read_csr() and CAUSE_USER_ECALL are macros defined in kernel/riscv.h
if (read_csr(scause) == CAUSE_USER_ECALL) {
handle_syscall(current->trapframe);
} else {
sprint("smode_trap_handler(): unexpected scause %p\n", read_csr(scause));
sprint(" sepc=%p stval=%p\n", read_csr(sepc), read_csr(stval));
panic( "unexpected exception happened.\n" );
}
// continue (come back to) the execution of current process.
switch_to(current);
}