mirror of
https://github.com/plctlab/riscv-operating-system-mooc.git
synced 2025-12-27 01:28:55 +00:00
initial versioin
This commit is contained in:
77
code/os/06-interrupts/trap.c
Normal file
77
code/os/06-interrupts/trap.c
Normal file
@@ -0,0 +1,77 @@
|
||||
#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 & 0xfff;
|
||||
|
||||
if (cause & 0x80000000) {
|
||||
/* 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:
|
||||
uart_puts("unknown async exception!\n");
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
/* Synchronous trap - exception */
|
||||
printf("Sync exceptions!, code = %d\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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user