mirror of
https://github.com/plctlab/riscv-operating-system-mooc.git
synced 2025-12-05 15:15:49 +00:00
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>
90 lines
1.6 KiB
C
90 lines
1.6 KiB
C
#include "os.h"
|
|
|
|
extern void trap_vector(void);
|
|
extern void uart_isr(void);
|
|
extern void timer_handler(void);
|
|
extern void schedule(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");
|
|
/*
|
|
* acknowledge the software interrupt by clearing
|
|
* the MSIP bit in mip.
|
|
*/
|
|
int id = r_mhartid();
|
|
*(uint32_t*)CLINT_MSIP(id) = 0;
|
|
|
|
schedule();
|
|
|
|
break;
|
|
case 7:
|
|
uart_puts("timer interruption!\n");
|
|
timer_handler();
|
|
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");
|
|
}
|
|
|