parent
924dbdb3f6
commit
2859704c47
@ -0,0 +1,50 @@
|
||||
#include "kernel/riscv.h"
|
||||
#include "kernel/process.h"
|
||||
#include "spike_interface/spike_utils.h"
|
||||
|
||||
static void handle_instruction_access_fault() { panic("Instruction access fault!"); }
|
||||
|
||||
static void handle_load_access_fault() { panic("Load access fault!"); }
|
||||
|
||||
static void handle_store_access_fault() { panic("Store/AMO access fault!"); }
|
||||
|
||||
static void handle_illegal_instruction() { panic("Illegal instruction!"); }
|
||||
|
||||
static void handle_misaligned_load() { panic("Misaligned Load!"); }
|
||||
|
||||
static void handle_misaligned_store() { panic("Misaligned AMO!"); }
|
||||
|
||||
//
|
||||
// handle_mtrap calls a handling function according to the type of a machine mode interrupt (trap).
|
||||
//
|
||||
void handle_mtrap() {
|
||||
uint64 mcause = read_csr(mcause);
|
||||
switch (mcause) {
|
||||
case CAUSE_FETCH_ACCESS:
|
||||
handle_instruction_access_fault();
|
||||
break;
|
||||
case CAUSE_LOAD_ACCESS:
|
||||
handle_load_access_fault();
|
||||
case CAUSE_STORE_ACCESS:
|
||||
handle_store_access_fault();
|
||||
break;
|
||||
case CAUSE_ILLEGAL_INSTRUCTION:
|
||||
// TODO (lab1_2): call handle_illegal_instruction to implement illegal instruction
|
||||
// interception, and finish lab1_2.
|
||||
panic( "call handle_illegal_instruction to accomplish illegal instruction interception for lab1_2.\n" );
|
||||
|
||||
break;
|
||||
case CAUSE_MISALIGNED_LOAD:
|
||||
handle_misaligned_load();
|
||||
break;
|
||||
case CAUSE_MISALIGNED_STORE:
|
||||
handle_misaligned_store();
|
||||
break;
|
||||
|
||||
default:
|
||||
sprint("machine trap(): unexpected mscause %p\n", mcause);
|
||||
sprint(" mepc=%p mtval=%p\n", read_csr(mepc), read_csr(mtval));
|
||||
panic( "unexpected exception happened in M-mode.\n" );
|
||||
break;
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
#include "util/load_store.S"
|
||||
|
||||
#
|
||||
# M-mode trap entry point
|
||||
#
|
||||
.globl mtrapvec
|
||||
.align 4
|
||||
mtrapvec:
|
||||
# mscratch -> g_itrframe (cf. kernel/machine/minit.c line 94)
|
||||
# swap a0 and mscratch, so that a0 points to interrupt frame,
|
||||
# i.e., [a0] = &g_itrframe
|
||||
csrrw a0, mscratch, a0
|
||||
|
||||
# save the registers in g_itrframe
|
||||
addi t6, a0, 0
|
||||
store_all_registers
|
||||
# save the original content of a0 in g_itrframe
|
||||
csrr t0, mscratch
|
||||
sd t0, 72(a0)
|
||||
|
||||
# switch stack (to use stack0) for the rest of machine mode
|
||||
# trap handling.
|
||||
la sp, stack0
|
||||
li a3, 4096
|
||||
csrr a4, mhartid
|
||||
addi a4, a4, 1
|
||||
mul a3, a3, a4
|
||||
add sp, sp, a3
|
||||
|
||||
# pointing mscratch back to g_itrframe
|
||||
csrw mscratch, a0
|
||||
|
||||
# call machine mode trap handling function
|
||||
call handle_mtrap
|
||||
|
||||
# restore all registers, come back to the status before entering
|
||||
# machine mode handling.
|
||||
csrr t6, mscratch
|
||||
restore_all_registers
|
||||
|
||||
mret
|
@ -1,17 +0,0 @@
|
||||
/*
|
||||
* Below is the given application for lab1_1.
|
||||
*
|
||||
* You can build this app (as well as our PKE OS kernel) by command:
|
||||
* $ make
|
||||
*
|
||||
* Or run this app (with the support from PKE OS kernel) by command:
|
||||
* $ make run
|
||||
*/
|
||||
|
||||
#include "user_lib.h"
|
||||
|
||||
int main(void) {
|
||||
printu("Hello world!\n");
|
||||
|
||||
exit(0);
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
/*
|
||||
* Below is the given application for lab1_2.
|
||||
* This app attempts to issue M-mode instruction in U-mode, and consequently raises an exception.
|
||||
*/
|
||||
|
||||
#include "user_lib.h"
|
||||
#include "util/types.h"
|
||||
|
||||
int main(void) {
|
||||
printu("Going to hack the system by running privilege instructions.\n");
|
||||
// we are now in U(user)-mode, but the "csrw" instruction requires M-mode privilege.
|
||||
// Attempting to execute such instruction will raise illegal instruction exception.
|
||||
asm volatile("csrw sscratch, 0");
|
||||
exit(0);
|
||||
}
|
Loading…
Reference in new issue