Files
riscv-operating-system-mooc/code/os/06-interrupts/trap.c
Wang Chen 2fcd517c78 Use %p to print pointer
We use %ld to print mcause.code to compatilbe with rv64 and it do
no harm for rv32.

Signed-off-by: Wang Chen <wangchen20@iscas.ac.cn>
2024-03-27 14:50:35 +08:00

78 lines
1.4 KiB
C

#include "os.h"
extern void trap_vector(void);
extern void uart_isr(void);
void trap_init()
{
/*
* set the trap-vector base-address for machine-mode
*/
w_mtvec((reg_t)trap_vector);
}
void external_interrupt_handler()
{
int irq = plic_claim();
if (irq == UART0_IRQ){
uart_isr();
} else if (irq) {
printf("unexpected interrupt irq = %d\n", irq);
}
if (irq) {
plic_complete(irq);
}
}
reg_t trap_handler(reg_t epc, reg_t cause)
{
reg_t return_pc = epc;
reg_t cause_code = cause & MCAUSE_MASK_ECODE;
if (cause & MCAUSE_MASK_INTERRUPT) {
/* Asynchronous trap - interrupt */
switch (cause_code) {
case 3:
uart_puts("software interruption!\n");
break;
case 7:
uart_puts("timer interruption!\n");
break;
case 11:
uart_puts("external interruption!\n");
external_interrupt_handler();
break;
default:
printf("Unknown async exception! Code = %ld\n", cause_code);
break;
}
} else {
/* Synchronous trap - exception */
printf("Sync exceptions! Code = %ld\n", cause_code);
panic("OOPS! What can I do!");
//return_pc += 4;
}
return return_pc;
}
void trap_test()
{
/*
* Synchronous exception code = 7
* Store/AMO access fault
*/
*(int *)0x00000000 = 100;
/*
* Synchronous exception code = 5
* Load access fault
*/
//int a = *(int *)0x00000000;
uart_puts("Yeah! I'm return back from trap!\n");
}