2 * Minimal ArmV7 system boot code.
4 * Using semihosting for serial output and exit functions.
8 * Semihosting interface on ARM AArch32
9 * R0 - semihosting call number
10 * R1 - semihosting parameter
12 #define semihosting_call svc 0x123456
13 #define SYS_WRITEC 0x03 /* character to debug channel */
14 #define SYS_WRITE0 0x04 /* string to debug channel */
17 #define ADP_Stopped_ApplicationExit 0x20026
18 #define ADP_Stopped_InternalError 0x20024
21 * Helper macro for annotating functions with elf type and size.
25 .type \name, %function
26 .size \name, . - \name
29 .section .interrupt_vector, "ax"
33 b reset /* reset vector */
34 b undef_instr /* undefined instruction vector */
35 b software_intr /* software interrupt vector */
36 b prefetch_abort /* prefetch abort vector */
37 b data_abort /* data abort vector */
39 b IRQ_handler /* IRQ vector */
40 b FIQ_handler /* FIQ vector */
47 mcr p15, 0, r0, c12, c0, 0 /* Set up VBAR */
49 ldr sp, =stack_end /* Set up the stack */
50 bl mmu_setup /* Set up the MMU */
51 bl main /* Jump to main */
57 ite EQ // if-then-else. "EQ" is for if equal, else otherwise
58 ldreq r1, =ADP_Stopped_ApplicationExit // if r0 == 0
59 ldrne r1, =ADP_Stopped_InternalError // else
71 * The MMU setup for this is very simple using two stage one
72 * translations. The first 1Mb section points to the text
73 * section and the second points to the data and rss.
74 * Currently the fattest test only needs ~50k for that so we
75 * have plenty of space.
77 * The short descriptor Section format is as follows:
79 * PA[31:20] - Section Base Address
80 * NS[19] - Non-secure bit
81 * 0[18] - Section (1 for Super Section)
82 * nG[17] - Not global bit
84 * TEX[14:12] - Memory Region Attributes
85 * AP[15, 11:10] - Access Permission Bits
88 * XN[4] - Execute never bit
89 * C[3] - Memory Region Attributes
90 * B[2] - Memory Region Attributes
92 * PXN[0] - Privileged Execute Never
94 * r0 - point at the table
97 * r3 - common section bits
108 * Outer and Inner WB, no write allocate
117 /* Page table setup (identity mapping). */
120 /* First block: .text/RO/execute enabled */
122 ldr r2, =0xFFF00000 /* 1MB block alignment */
124 orr r2, r2, r3 /* common bits */
125 orr r2, r2, #(1 << 15) /* AP[2] = 1 */
126 orr r2, r2, #(1 << 10) /* AP[0] = 1 => RO @ PL1 */
128 lsr r4, r2, #(20 - 2)
129 str r2, [r0, r4, lsl #0] /* write entry */
131 /* Second block: .data/RW/no execute */
133 ldr r2, =0xFFF00000 /* 1MB block alignment */
135 orr r2, r2, r3 /* common bits */
136 orr r2, r2, #(1 << 10) /* AP[0] = 1 => RW @ PL1 */
137 orr r2, r2, #(1 << 4) /* XN[4] => no execute */
139 lsr r4, r2, #(20 - 2)
140 str r2, [r0, r4, lsl #0] /* write entry */
143 * DACR - Domain Control
145 * Enable client mode for domain 0 (we don't use any others)
148 mcr p15, 0, r0, c3, c0, 0
151 * TTCBR - Translation Table Base Control Register
153 * EAE[31] = 0, 32-bit translation, short descriptor format
154 * N[2:0] = 5 ( TTBRO uses 31:14-5 => 9 bit lookup stage )
157 mcr p15, 0, r0, c1, c0, 2
160 * TTBR0 -Translation Table Base Register 0
162 * [31:9] = Base address of table
164 * QEMU doesn't really care about the cache sharing
165 * attributes so we don't need to either.
168 mcr p15, 0, r0, c2, c0, 0
171 * SCTLR- System Control Register
173 * TE[30] = 0, exceptions to A32 state
174 * AFE[29] = 0, AP[0] is the access permissions bit
175 * EE[25] = 0, Little-endian
176 * WXN[19] = 0 = no effect, Write does not imply XN (execute never)
177 * I[12] = Instruction cachability control
178 * C[2] = Data cachability control
179 * M[0] = 1, enable stage 1 address translation for EL0/1
181 * At this point virtual memory is enabled.
184 mcr p15, 0, r0, c1, c0, 0
188 mov pc, lr /* done, return to caller */
192 /* Output a single character to serial port */
194 STMFD sp!, {r0-r1} // push r0, r1 onto stack
198 LDMFD sp!, {r0-r1} // pop r0, r1 from stack
210 ldr r1, =undef_intr_error
216 ldr r1, =software_intr_error
222 ldr r1, =prefetch_abort_error
228 ldr r1, =data_abort_error
246 * Initiate a exit semihosting call whenever there is any exception
247 * r1 already holds the string.
256 endf exception_handler
259 * We implement a stub raise() function which errors out as tests
260 * shouldn't trigger maths errors.
268 ldr r1, =ADP_Stopped_InternalError
278 .ascii "Reset exception occurred.\n\0"
281 .ascii "Undefined Instruction Exception Occurred.\n\0"
284 .ascii "Software Interrupt Occurred.\n\0"
286 prefetch_abort_error:
287 .ascii "Prefetch Abort Occurred.\n\0"
290 .ascii "Data Abort Occurred.\n\0"
293 .ascii "IRQ exception occurred.\n\0"
296 .ascii "FIQ exception occurred.\n\0"
299 .ascii "Software maths exception.\n\0"
303 * 1st Stage Translation table
304 * 4096 entries, indexed by [31:20]
305 * each entry covers 1Mb of address space
314 /* Space for stack */