-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrt0.S
189 lines (154 loc) · 4.26 KB
/
crt0.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
/*
* Copyright (c) 2016 Subbaraya Sundeep <sundeep.babi@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <config.h>
.macro save_bl_regs
stmia sp, {r0 - r12} @ Save user registers (now in svc mode) r0-r12
mov r0, sp @ not bootloader stack :(
mov r1, lr
mov r2, pc
mrs r3, cpsr
mrs r4, spsr
add r5, sp, #52 @ placing sp at r13 location
stmia r5, {r0 - r4} @ save sp_SVC, lr_SVC, pc
.endm
/*
sp_svc ----> SVC mode SP points here before interrupt
spsr (cpsr of SVC mode)
SP (of SVC mode)
r14 (lr of SVC mode)
lr (pc of SVC mode)
r12
r11
r10
r9
r8
r7
r6
r5
r4
r3
r2
r1
sp_irq ---> r0
*/
.macro save_context
sub sp, sp, #68
stmia sp, {r0 - r12,lr} @ Save user registers (now in svc mode) r0-r12
cpsid i, #19 /* to svc */
mov r0, lr
cpsid i, #18 /* to interrupt */
/* check alignment hole */
add r1, sp, #68
mrs r2, spsr
add r5, sp, #56
stmia r5, {r0 - r2}
.endm
.macro restore_context
add sp, sp, #56
ldmia sp, {r0 - r2} @ Save user registers (now in svc mode) r0-r12
msr spsr, r2
/* check alignment hole ? */
cpsid i, #19 /* to SVC */
mov sp, r1
mov r14, r0
cpsid i, #18 /* to interrupt mode */
sub sp, sp, #56
ldmia sp, {r0 - r12,lr}
movs pc, lr /* go bck to task running in SVC mode */
.endm
.macro load_init_task
add sp, sp, #56
ldmia sp, {r0 - r2} @ Save user registers (now in svc mode)
msr spsr, r2
/* check alignment hole ? */
sub sp, sp, #56
ldmia sp, {r0 - r12,lr}
.endm
.globl _start
_start:
/* I do not rely on U-boot or Rpi boot code for proper initial values */
cps #19
ldr sp,=bl_regs
save_bl_regs
mrc p15, 0, r1, c1, c0, 0 @ SCTLR (System Control Register)
bic r1, r1, #0x1000 @ clear High vecs - SCTLR.V = 0
mcr p15, 0, r1, c1, c0, 0
/* U-boot modifies VBAR we have to set to 0x00 */
mov r1, #0x0000
mcr p15, 0, r1, c12, c0, 0 @ Set VBAR
ldr sp,=init_stack
mov r1, #512
and r1, r1, #0xFFFFFFF8 @ sp should be 8 byte aligned
add sp, sp, r1
mov r0, sp
bl c_startup
bl get_tos
mov sp, r0 /* current/task1 stack pointer */
load_init_task
cpsid i
mov sp,#0x8000
movs pc, lr /* go bck to task running in SVC mode */
hang: b hang
irq_handler:
ldr sp,=Reg_0 /* sp_IRQ */
str r0, [sp] /* save r0 in Reg_0 */
cps #19 /* to SVC */
mov r0, sp
cps #18 /* to Interrupt */
mov sp, r0 /* Interrupt stack points to top of SVC stack */
/* Add stack adjustment if required ? */
ldr r0,=Reg_0
ldr r0, [r0] /* load r0 from Reg_0 */
save_context
/* Save top of stack in task desc */
mov r0, sp
bl save_tos
bl interrupt_handler
/* Get top of stack from task desc */
bl get_tos
mov sp, r0
restore_context
.section .vectors, "ax"
.org 0x00
ldr pc, _reset
.org 0x04
ldr pc, _undefined_instruction
.org 0x08
ldr pc, _software_interrupt
.org 0x0C
ldr pc, _prefetch_abort
.org 0x10
ldr pc, _data_abort
.org 0x14
ldr pc, _unused_handler
.org 0x18
ldr pc, _interrupt
.org 0x1C
ldr pc, _fast_interrupt
_reset: .word __reset
_undefined_instruction: .word undefined_handler
_software_interrupt: .word swi_handler
_prefetch_abort: .word prefetch_abort_handler
_data_abort: .word data_abort_handler
_unused_handler: .word __reset
_interrupt: .word irq_handler
_fast_interrupt: .word fiq_handler
__reset: b _start