-
Notifications
You must be signed in to change notification settings - Fork 0
/
vectors.s
319 lines (258 loc) · 6.95 KB
/
vectors.s
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* File: vectors.s
* -----------------
* RPi interrupt vectors.
*/
.globl _vectors
.globl _vectors_end
_vectors:
ldr pc, _reset_asm
ldr pc, _undefined_instruction_asm
ldr pc, _software_interrupt_asm
ldr pc, _prefetch_abort_asm
ldr pc, _data_abort_asm
ldr pc, _reset_asm
ldr pc, _interrupt_asm
fast_interrupt_asm:
sub lr, lr, #4 @First instr of FIQ handler
push {lr}
push {r0-r12}
mov r0, lr @ Pass old pc
bl fast_interrupt_vector @ C function
pop {r0-r12}
ldm sp!, {pc}^
_reset_asm: .word reset_asm
_undefined_instruction_asm: .word undefined_instruction_asm
_software_interrupt_asm: .word software_interrupt_asm
_prefetch_abort_asm: .word prefetch_abort_asm
_data_abort_asm: .word data_abort_asm
_interrupt_asm: .word interrupt_asm
_vectors_end:
/*
* interrupt_asm
* --------------
* This will run for both timer and GPIO interrupts.
*/
interrupt_asm:
//store basic interrupt stuff
sub lr, lr, #4
push { r0-r12, lr}
bl system_disable_interrupts
pop { r0-r12, lr}
//call the interrupt vector
push { r0-r12 }
mov r0, lr
bl interrupt_vector
pop { r0-r12 }
save_current_thread:
//remember r1 so you can use it for r0
push {r1}
mov r1, r0 //store r0 so it can be restored
push {r2, r3}
bl get_current_thread //r0 now has the address of CURRENT_THREAD
pop {r2, r3}
add r0, r0, #4 // r0 = &CURRENT_THREAD.r0
str r1, [r0] // save what the r0 was
pop {r1} // restore r1
add r0, r0, #4 // r0 = &CURRENT_THREAD.r1
str r1, [r0] // save r1
//r2
add r0, r0, #4
str r2, [r0]
//r3
add r0, r0, #4
str r3, [r0]
//r4
add r0, r0, #4
str r4, [r0]
//r5
add r0, r0, #4
str r5, [r0]
//r6
add r0, r0, #4
str r6, [r0]
//r7
add r0, r0, #4
str r7, [r0]
//r8
add r0, r0, #4
str r8, [r0]
//r9
add r0, r0, #4
str r9, [r0]
//r10
add r0, r0, #4
str r10, [r0]
//r11
add r0, r0, #4
str r11, [r0]
//r12
add r0, r0, #4
str r12, [r0]
//store SVC sp, lr, and pc
mrs r1, cpsr
bic r1, r1, #0x1F
orr r1, r1, #0x13
msr cpsr_c, r1
//sp
add r0, r0, #4
str sp, [r0]
//lr
add r0, r0, #4
str lr, [r0]
//back to IRQ land
mrs r1, cpsr
bic r1, r1, #0x1F
orr r1, r1, #0x12
msr cpsr_c, r1
//pc THIS NEEDS TO BE LR
add r0, r0, #4
str lr, [r0]
//cpsr
add r0, r0, #4
mrs r1, cpsr
str r1, [r0]
//spsr
add r0, r0, #4
mrs r1, spsr
str r1, [r0]
push {r2, r3}
bl increment_thread //r0 now has the address of our next thread
pop {r2, r3}
restore_thread:
// stack is: r0, ... r12, sp, lr, pc, spsr
add r0, r0, #4 // r0 = &CURRENT_THREAD.r0
ldr r1, [r0] // r1 = thread.r0
push {r1}
add r0, r0, #4
ldr r1, [r0] //r1 = thread.r1
push {r1}
add r0, r0, #4
ldr r1, [r0] //r1 = thread.r2
push {r1}
add r0, r0, #4
ldr r1, [r0] //r1 = thread.r3
push {r1}
add r0, r0, #4
ldr r1, [r0] //r1 = thread.r4
push {r1}
add r0, r0, #4
ldr r1, [r0] //r1 = thread.r5
push {r1}
add r0, r0, #4
ldr r1, [r0] //r1 = thread.r6
push {r1}
add r0, r0, #4
ldr r1, [r0] //r1 = thread.r7
push {r1}
add r0, r0, #4
ldr r1, [r0] //r1 = thread.r8
push {r1}
add r0, r0, #4
ldr r1, [r0] //r1 = thread.r9
push {r1}
add r0, r0, #4
ldr r1, [r0] //r1 = thread.r10
push {r1}
add r0, r0, #4
ldr r1, [r0] //r1 = thread.r11
push {r1}
add r0, r0, #4
ldr r1, [r0] //r1 = thread.r12
push {r1}
add r0, r0, #4
ldr r1, [r0] //r1 = thread.sp
push {r1}
add r0, r0, #4
ldr r1, [r0] //r1 = thread.lr
push {r1}
add r0, r0, #4
ldr r1, [r0] //r1 = thread.pc
push {r1}
add r0, r0, #8 //skip cpsr - agreed
ldr r1, [r0] //r1 = thread.spsr
push {r1}
// Our stack now looks like spsr, pc, lr, sp, r12, ... r0 (in order of popping)
pop {r1} //this was the spsr - SPSR
pop {lr} //this was the pc - PC (from thread)
pop {r2} //this was the lr - LR (from thread)
pop {r3} //this was the sp - SP (from thread)
# push {r0-r12, lr}
# mov r0, lr
# bl check_registers // Used to check important registers
# pop {r0-r12, lr}
// switch to SVC
mrs r0, cpsr
bic r0, r0, #0x1F
orr r0, r0, #0x13
msr cpsr_c, r0
msr spsr, r1 // restore spsr
mov lr, r2 // restore lr to be old lr
mov sp, r3 // restore sp
// switch to IRQ
mrs r0, cpsr
bic r0, r0, #0x1F
orr r0, r0, #0x12
msr cpsr_c, r0
// our stack now just has the normal registers in it; restore them
pop {r12}
pop {r11}
pop {r10}
pop {r9}
pop {r8}
pop {r7}
pop {r6}
pop {r5}
pop {r4}
pop {r3}
pop {r2}
pop {r1}
pop {r0}
//enable interrupts
push {r0}
mrs r0,cpsr
bic r0,r0,#0x80 // I=0 enables interrupts
msr cpsr_c,r0
pop {r0}
mov sp, #0x8000
movs pc, lr
reset_asm:
sub lr, lr, #4
push {lr}
push {r0-r12}
mov r0, lr @ Pass old pc
bl reset_vector @ C function
pop {r0-r12}
ldm sp!, {pc}^
undefined_instruction_asm:
sub lr, lr, #4
push {lr}
push {r0-r12}
mov r0, lr @ Pass old pc
bl undefined_instruction_vector @ C function
pop {r0-r12}
ldm sp!, {pc}^
software_interrupt_asm:
sub lr, lr, #4
push {lr}
push {r0-r12}
mov r0, lr @ Pass old pc
bl software_interrupt_vector @ C function
pop {r0-r12}
ldm sp!, {pc}^
prefetch_abort_asm:
sub lr, lr, #4
push {lr}
push {r0-r12}
mov r0, lr @ Pass old pc
bl prefetch_abort_vector @ C function
pop {r0-r12}
ldm sp!, {pc}^
data_abort_asm:
sub lr, lr, #4
push {lr}
push {r0-r12}
mov r0, lr @ Pass old pc
bl data_abort_vector @ C function
pop {r0-r12}
ldm sp!, {pc}^