-
Notifications
You must be signed in to change notification settings - Fork 76
/
trap.c
91 lines (87 loc) · 1.66 KB
/
trap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "os.h"
extern void trap_vector();
extern void virtio_disk_isr();
void trap_init()
{
// set the machine-mode trap handler.
w_mtvec((reg_t)trap_vector);
}
void external_handler()
{
int irq = plic_claim();
if (irq == UART0_IRQ)
{
lib_isr();
}
else if (irq == VIRTIO_IRQ)
{
lib_puts("Virtio IRQ\n");
virtio_disk_isr();
}
else if (irq)
{
lib_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:
/* software interruption */
break;
case 7:
/* timer interruption */
// disable machine-mode timer interrupts.
w_mie(r_mie() & ~(1 << 7));
timer_handler();
return_pc = (reg_t)&os_kernel;
// enable machine-mode timer interrupts.
w_mie(r_mie() | MIE_MTIE);
break;
case 11:
/* external interruption */
external_handler();
break;
default:
lib_puts("unknown async exception!\n");
break;
}
}
else
{
switch (cause_code)
{
case 2:
lib_puts("Illegal instruction!\n");
break;
case 5:
lib_puts("Fault load!\n");
break;
case 7:
lib_puts("Fault store!\n");
break;
case 11:
lib_puts("Machine mode ecall!\n");
break;
default:
/* Synchronous trap - exception */
lib_printf("Sync exceptions! cause code: %d\n", cause_code);
break;
}
for (;;)
{
/* code */
}
}
return return_pc;
}