-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrap.c
More file actions
145 lines (133 loc) · 3.72 KB
/
trap.c
File metadata and controls
145 lines (133 loc) · 3.72 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "platform.h"
#include "riscv.h"
#include "plic.h"
#include "types.h"
#include "uart.h"
#include "clint.h"
#include "timer.h"
#include "syscall.h"
extern void trap_vector(void);
extern void schedule(void);
void trap_init()
{
/* Set the trap-vector base-address for machine mode */
w_mtvec((reg_t)trap_vector); /* Write trap_vector into mtvec */
w_mie(r_mie() | MIE_MTIE); /* Timer */
w_mie(r_mie() | MIE_MSIE); /* Software */
w_mie(r_mie() | MIE_MEIE); /* External */
w_mstatus(r_mstatus() | MSTATUS_MIE); /* Global Interrupt */
}
/* m-mode external_interrupt, mcause is 0x8000000B */
static void external_interrupt_handler(void)
{
int irq = plic_claim();
if (irq == UART0_IRQ) {
uart_puts("This key raised external interrupt: ");
uart_irq();
} else if (irq) {
printf("Unexpected interrupt irq = %d\n", irq);
}
if (irq) {
plic_complete(irq);
}
}
static void timer_check()
{
uint32_t _tick = get_timer_tick();
timer_t *timer = get_timer_list();
for(int i = 0; i < MAX_NUM_TIMER; i++) {
if (timer->handler != NULL){
if (timer->timeout_tick <= _tick){
timer->handler(timer->args);
// add a new field to identify single time or cycle
timer->handler = NULL;
timer->args = NULL;
timer->timeout_tick = 0;
}
}
timer++;
}
}
/* m-mode timer_interrupt, mcause is 0x80000007 */
static void timer_interrupt_handler(void)
{
/* Notice to load timer first. */
timer_load(TIMER_INTERVAL / 10); /* 100ms interval */
update_tick();
uint32_t _tick = get_timer_tick();
// something to do in timer_interrupt...
printf("Handle the timer interrupt, _tick = %d\n", _tick);
timer_check();
schedule();
}
/* m-mode software_interrupt, mcause is 0x80000003 */
static void software_interrupt_handler(void)
{
int id = r_mhartid();
*(uint32_t *)CLINT_MSIP(id) = 0;
schedule();
}
reg_t trap_handler(reg_t mepc, reg_t cause, struct context *ctx)
{
reg_t return_pc = mepc;
reg_t cause_code = cause & 0xfff;
if (cause & 0x80000000) {
/* Asynchronous trap - interrupt */
printf("Interrupt, cause_code = %d\n", cause_code);
switch (cause_code)
{
case 3:
uart_puts("software interruption!\n");
software_interrupt_handler();
break;
case 7:
uart_puts("timer_interruption!\n");
timer_interrupt_handler();
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("Exception, cause_code = %d\n", cause_code);
switch (cause_code) {
case 8:
uart_puts("Ecall from U-mode\n");
/* Handle the Syscall */
syscall_handler(ctx);
return_pc += 4;
break;
case 9:
uart_puts("Ecall from S-mode\n");
return_pc += 4;
break;
case 11:
uart_puts("Ecall from M-mode\n");
return_pc += 4;
break;
default:
panic("unknown sync exception!\n");
break;
}
}
return return_pc;
}
void trap_test(void)
{
/*
* 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 am back from trap!\n");
}