comparison src/trap.c @ 0:83c23a36980d

Init
author Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
date Fri, 26 May 2017 23:11:05 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:83c23a36980d
1 // The ARM UART, a memory mapped device
2 #include "types.h"
3 #include "defs.h"
4 #include "param.h"
5 #include "arm.h"
6 #include "proc.h"
7
8 // trap routine
9 void swi_handler (struct trapframe *r)
10 {
11 if (proc->killed)
12 exit();
13 proc->tf = r;
14 syscall ();
15 if (proc->killed)
16 exit();
17 }
18
19 // trap routine
20 void irq_handler (struct trapframe *r)
21 {
22 // proc points to the current process. If the kernel is
23 // running scheduler, proc is NULL.
24 if (proc != NULL) {
25 proc->tf = r;
26 }
27
28 pic_dispatch (r);
29 }
30
31 // trap routine
32 void reset_handler (struct trapframe *r)
33 {
34 cli();
35 cprintf ("reset at: 0x%x \n", r->pc);
36 }
37
38 // trap routine
39 void und_handler (struct trapframe *r)
40 {
41 cli();
42 cprintf ("und at: 0x%x \n", r->pc);
43 }
44
45 // trap routine
46 void dabort_handler (struct trapframe *r)
47 {
48 uint dfs, fa;
49
50 cli();
51
52 // read data fault status register
53 asm("MRC p15, 0, %[r], c5, c0, 0": [r]"=r" (dfs)::);
54
55 // read the fault address register
56 asm("MRC p15, 0, %[r], c6, c0, 0": [r]"=r" (fa)::);
57
58 cprintf ("data abort: instruction 0x%x, fault addr 0x%x, reason 0x%x \n",
59 r->pc, fa, dfs);
60
61 dump_trapframe (r);
62 }
63
64 // trap routine
65 void iabort_handler (struct trapframe *r)
66 {
67 uint ifs;
68
69 // read fault status register
70 asm("MRC p15, 0, %[r], c5, c0, 0": [r]"=r" (ifs)::);
71
72 cli();
73 cprintf ("prefetch abort at: 0x%x (reason: 0x%x)\n", r->pc, ifs);
74 dump_trapframe (r);
75 }
76
77 // trap routine
78 void na_handler (struct trapframe *r)
79 {
80 cli();
81 cprintf ("n/a at: 0x%x \n", r->pc);
82 }
83
84 // trap routine
85 void fiq_handler (struct trapframe *r)
86 {
87 cli();
88 cprintf ("fiq at: 0x%x \n", r->pc);
89 }
90
91 // low-level init code: in real hardware, lower memory is usually mapped
92 // to flash during startup, we need to remap it to SDRAM
93 void trap_init ( )
94 {
95 volatile uint32 *ram_start;
96 char *stk;
97 int i;
98 uint modes[] = {FIQ_MODE, IRQ_MODE, ABT_MODE, UND_MODE};
99
100 // the opcode of PC relative load (to PC) instruction LDR pc, [pc,...]
101 static uint32 const LDR_PCPC = 0xE59FF000U;
102
103 // create the excpetion vectors
104 ram_start = (uint32*)VEC_TBL;
105
106 ram_start[0] = LDR_PCPC | 0x18; // Reset (SVC)
107 ram_start[1] = LDR_PCPC | 0x18; // Undefine Instruction (UND)
108 ram_start[2] = LDR_PCPC | 0x18; // Software interrupt (SVC)
109 ram_start[3] = LDR_PCPC | 0x18; // Prefetch abort (ABT)
110 ram_start[4] = LDR_PCPC | 0x18; // Data abort (ABT)
111 ram_start[5] = LDR_PCPC | 0x18; // Not assigned (-)
112 ram_start[6] = LDR_PCPC | 0x18; // IRQ (IRQ)
113 ram_start[7] = LDR_PCPC | 0x18; // FIQ (FIQ)
114
115 ram_start[8] = (uint32)trap_reset;
116 ram_start[9] = (uint32)trap_und;
117 ram_start[10] = (uint32)trap_swi;
118 ram_start[11] = (uint32)trap_iabort;
119 ram_start[12] = (uint32)trap_dabort;
120 ram_start[13] = (uint32)trap_na;
121 ram_start[14] = (uint32)trap_irq;
122 ram_start[15] = (uint32)trap_fiq;
123
124 // initialize the stacks for different mode
125 for (i = 0; i < sizeof(modes)/sizeof(uint); i++) {
126 stk = alloc_page ();
127
128 if (stk == NULL) {
129 panic("failed to alloc memory for irq stack");
130 }
131
132 set_stk (modes[i], (uint)stk);
133 }
134 }
135
136 void dump_trapframe (struct trapframe *tf)
137 {
138 cprintf ("r14_svc: 0x%x\n", tf->r14_svc);
139 cprintf (" spsr: 0x%x\n", tf->spsr);
140 cprintf (" r0: 0x%x\n", tf->r0);
141 cprintf (" r1: 0x%x\n", tf->r1);
142 cprintf (" r2: 0x%x\n", tf->r2);
143 cprintf (" r3: 0x%x\n", tf->r3);
144 cprintf (" r4: 0x%x\n", tf->r4);
145 cprintf (" r5: 0x%x\n", tf->r5);
146 cprintf (" r6: 0x%x\n", tf->r6);
147 cprintf (" r7: 0x%x\n", tf->r7);
148 cprintf (" r8: 0x%x\n", tf->r8);
149 cprintf (" r9: 0x%x\n", tf->r9);
150 cprintf (" r10: 0x%x\n", tf->r10);
151 cprintf (" r11: 0x%x\n", tf->r11);
152 cprintf (" r12: 0x%x\n", tf->r12);
153 cprintf (" pc: 0x%x\n", tf->pc);
154 }