-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrap.c
More file actions
161 lines (135 loc) · 4.07 KB
/
Copy pathtrap.c
File metadata and controls
161 lines (135 loc) · 4.07 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// The ARM UART, a memory mapped device
#include "types.h"
#include "defs.h"
#include "param.h"
#include "arm.h"
#include "proc.h"
// trap routine
void swi_handler (struct trapframe *r)
{
proc->tf = r;
syscall ();
}
// trap routine
void irq_handler (struct trapframe *r)
{
// proc points to the current process. If the kernel is
// running scheduler, proc is NULL.
if (proc != NULL) {
proc->tf = r;
}
pic_dispatch (r);
if (proc && proc->state == RUNNING) {
yield();
}
}
// trap routine
void reset_handler (struct trapframe *r)
{
cli();
cprintf ("reset at: 0x%x \n", r->pc);
}
// trap routine
void und_handler (struct trapframe *r)
{
cli();
cprintf ("und at: 0x%x \n", r->pc);
}
// trap routine
void dabort_handler (struct trapframe *r)
{
uint dfs, fa;
cli();
// read data fault status register
asm("MRC p15, 0, %[r], c5, c0, 0": [r]"=r" (dfs)::);
// read the fault address register
asm("MRC p15, 0, %[r], c6, c0, 0": [r]"=r" (fa)::);
// Check if this is a translation fault (missing page) for demand paging
// Translation fault status: 0x5 (section), 0x7 (page)
if(proc && fa < proc->sz) {
if(handle_page_fault(proc->pgdir, fa, proc->sz) == 0) {
return;
}
}
cprintf ("data abort: instruction 0x%x, fault addr 0x%x, reason 0x%x \n",
r->pc, fa, dfs);
dump_trapframe (r);
}
// trap routine
void iabort_handler (struct trapframe *r)
{
uint ifs;
// read fault status register
asm("MRC p15, 0, %[r], c5, c0, 0": [r]"=r" (ifs)::);
cli();
cprintf ("prefetch abort at: 0x%x (reason: 0x%x)\n", r->pc, ifs);
dump_trapframe (r);
}
// trap routine
void na_handler (struct trapframe *r)
{
cli();
cprintf ("n/a at: 0x%x \n", r->pc);
}
// trap routine
void fiq_handler (struct trapframe *r)
{
cli();
cprintf ("fiq at: 0x%x \n", r->pc);
}
// low-level init code: in real hardware, lower memory is usually mapped
// to flash during startup, we need to remap it to SDRAM
void trap_init ( )
{
volatile uint32 *ram_start;
char *stk;
int i;
uint modes[] = {FIQ_MODE, IRQ_MODE, ABT_MODE, UND_MODE};
// the opcode of PC relative load (to PC) instruction LDR pc, [pc,...]
static uint32 const LDR_PCPC = 0xE59FF000U;
// create the excpetion vectors
ram_start = (uint32*)VEC_TBL;
ram_start[0] = LDR_PCPC | 0x18; // Reset (SVC)
ram_start[1] = LDR_PCPC | 0x18; // Undefine Instruction (UND)
ram_start[2] = LDR_PCPC | 0x18; // Software interrupt (SVC)
ram_start[3] = LDR_PCPC | 0x18; // Prefetch abort (ABT)
ram_start[4] = LDR_PCPC | 0x18; // Data abort (ABT)
ram_start[5] = LDR_PCPC | 0x18; // Not assigned (-)
ram_start[6] = LDR_PCPC | 0x18; // IRQ (IRQ)
ram_start[7] = LDR_PCPC | 0x18; // FIQ (FIQ)
ram_start[8] = (uint32)trap_reset;
ram_start[9] = (uint32)trap_und;
ram_start[10] = (uint32)trap_swi;
ram_start[11] = (uint32)trap_iabort;
ram_start[12] = (uint32)trap_dabort;
ram_start[13] = (uint32)trap_na;
ram_start[14] = (uint32)trap_irq;
ram_start[15] = (uint32)trap_fiq;
// initialize the stacks for different mode
for (i = 0; i < sizeof(modes)/sizeof(uint); i++) {
stk = alloc_page ();
if (stk == NULL) {
panic("failed to alloc memory for irq stack");
}
set_stk (modes[i], (uint)stk);
}
}
void dump_trapframe (struct trapframe *tf)
{
cprintf ("r14_svc: 0x%x\n", tf->r14_svc);
cprintf (" spsr: 0x%x\n", tf->spsr);
cprintf (" r0: 0x%x\n", tf->r0);
cprintf (" r1: 0x%x\n", tf->r1);
cprintf (" r2: 0x%x\n", tf->r2);
cprintf (" r3: 0x%x\n", tf->r3);
cprintf (" r4: 0x%x\n", tf->r4);
cprintf (" r5: 0x%x\n", tf->r5);
cprintf (" r6: 0x%x\n", tf->r6);
cprintf (" r7: 0x%x\n", tf->r7);
cprintf (" r8: 0x%x\n", tf->r8);
cprintf (" r9: 0x%x\n", tf->r9);
cprintf (" r10: 0x%x\n", tf->r10);
cprintf (" r11: 0x%x\n", tf->r11);
cprintf (" r12: 0x%x\n", tf->r12);
cprintf (" pc: 0x%x\n", tf->pc);
}