1 /* $NetBSD: locore.S,v 1.14 2003/04/20 16:21:40 thorpej Exp $ */
4 * Copyright 2011 Semihalf
5 * Copyright (C) 1994-1997 Mark Brinicombe
6 * Copyright (C) 1994 Brini
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Brini.
20 * 4. The name of Brini may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL BRINI BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <sys/syscall.h>
38 #include <machine/asm.h>
39 #include <machine/armreg.h>
40 #include <machine/pte.h>
42 __FBSDID("$FreeBSD$");
44 /* What size should this really be ? It is only used by initarm() */
45 #define INIT_ARM_STACK_SIZE (2048 * 4)
47 #define CPWAIT_BRANCH \
51 mrc p15, 0, tmp, c2, c0, 0 /* arbitrary read of CP15 */ ;\
52 mov tmp, tmp /* wait for it to complete */ ;\
53 CPWAIT_BRANCH /* branch to next insn */
56 * This is for kvm_mkdb, and should be the address of the beginning
57 * of the kernel text segment (not necessarily the same as kernbase).
62 .set kernbase,KERNBASE
64 .set physaddr,PHYSADDR
67 * On entry for FreeBSD boot ABI:
68 * r0 - metadata pointer or 0 (boothowto on AT91's boot2)
69 * r1 - if (r0 == 0) then metadata pointer
70 * On entry for Linux boot ABI:
72 * r1 - machine type (passed as arg2 to initarm)
73 * r2 - Pointer to a tagged list or dtb image (phys addr) (passed as arg1 initarm)
75 * For both types of boot we gather up the args, put them in a struct arm_boot_params
76 * structure and pass that to initarm.
81 STOP_UNWINDING /* Can't unwind into the bootloader! */
83 mov r9, r0 /* 0 or boot mode from boot2 */
84 mov r8, r1 /* Save Machine type */
85 mov ip, r2 /* Save meta data */
86 mov fp, r3 /* Future expantion */
88 /* Make sure interrupts are disabled. */
90 orr r7, r7, #(I32_bit|F32_bit)
93 #if defined (FLASHADDR) && defined(LOADERRAMADDR)
94 /* Check if we're running from flash. */
97 * If we're running with MMU disabled, test against the
98 * physical address instead.
100 mrc p15, 0, r2, c1, c0, 0
101 ands r2, r2, #CPU_CONTROL_MMU_ENABLE
103 ldrne r6, =LOADERRAMADDR
125 Lram_offset: .word from_ram-_C_LABEL(_start)
130 bic r7, r7, #0xf0000000
131 orr r7, r7, #PHYSADDR
135 /* Disable MMU for a while */
136 mrc p15, 0, r2, c1, c0, 0
137 bic r2, r2, #(CPU_CONTROL_MMU_ENABLE | CPU_CONTROL_DC_ENABLE |\
138 CPU_CONTROL_WBUF_ENABLE)
139 bic r2, r2, #(CPU_CONTROL_IC_ENABLE)
140 bic r2, r2, #(CPU_CONTROL_BPRD_ENABLE)
141 mcr p15, 0, r2, c1, c0, 0
149 * Build page table from scratch.
152 /* Find the delta between VA and PA */
156 /* At this point: r2 = VA - PA */
159 * Find the physical address of the table. After these two
163 * r0 = va(pagetable) - (VA - PA)
164 * = va(pagetable) - VA + PA
173 /* Find the start kernels load address */
175 ldr r2, =(L1_S_OFFSET)
179 /* Map 64MiB, preserved over calls to build_pagetables */
183 /* Create the kernel map to jump to */
185 ldr r2, =(KERNVIRTADDR)
188 #if defined(SOCDEV_PA) && defined(SOCDEV_VA)
189 /* Create the custom map */
196 orr r0, r0, #2 /* Set TTB shared memory flag */
198 mcr p15, 0, r0, c2, c0, 0 /* Set TTB */
199 mcr p15, 0, r0, c8, c7, 0 /* Flush TLB */
201 #if defined(CPU_ARM1136) || defined(CPU_ARM1176) || defined(CPU_CORTEXA) || defined(CPU_MV_PJ4B) || defined(CPU_KRAIT)
203 mcr p15, 0, r0, c13, c0, 1 /* Set ASID to 0 */
206 /* Set the Domain Access register. Very important! */
207 mov r0, #((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT)
208 mcr p15, 0, r0, c3, c0, 0
211 * On armv6 enable extended page tables, and set alignment checking
212 * to modulo-4 (CPU_CONTROL_UNAL_ENABLE) for the ldrd/strd
213 * instructions emitted by clang.
215 mrc p15, 0, r0, c1, c0, 0
217 orr r0, r0, #(CPU_CONTROL_V6_EXTPAGE | CPU_CONTROL_UNAL_ENABLE)
218 orr r0, r0, #(CPU_CONTROL_AFLT_ENABLE)
219 orr r0, r0, #(CPU_CONTROL_AF_ENABLE)
221 orr r0, r0, #(CPU_CONTROL_MMU_ENABLE)
222 mcr p15, 0, r0, c1, c0, 0
231 ldmia r1, {r1, r2, sp} /* Set initial stack and */
232 sub r2, r2, r1 /* get zero init data */
235 str r3, [r1], #0x0004 /* get zero init data */
241 mov r1, #28 /* loader info size is 28 bytes also second arg */
242 subs sp, sp, r1 /* allocate arm_boot_params struct on stack */
243 mov r0, sp /* loader info pointer is first arg */
244 bic sp, sp, #7 /* align stack to 8 bytes */
245 str r1, [r0] /* Store length of loader info */
246 str r9, [r0, #4] /* Store r0 from boot loader */
247 str r8, [r0, #8] /* Store r1 from boot loader */
248 str ip, [r0, #12] /* store r2 from boot loader */
249 str fp, [r0, #16] /* store r3 from boot loader */
250 str r5, [r0, #20] /* store the physical address */
251 adr r4, Lpagetable /* load the pagetable address */
253 str r5, [r0, #24] /* store the pagetable address */
254 mov fp, #0 /* trace back starts here */
255 bl _C_LABEL(initarm) /* Off we go */
257 /* init arm will return the new stack pointer. */
260 bl _C_LABEL(mi_startup) /* call mi_startup()! */
262 adr r0, .Lmainreturned
268 * Builds the page table
269 * r0 - The table base address
270 * r1 - The physical address (trashed)
271 * r2 - The virtual address (trashed)
272 * r3 - The number of 1MiB sections
275 * Addresses must be 1MiB aligned
278 /* Set the required page attributed */
279 ldr r4, =(L1_TYPE_S|L1_S_C|L1_S_AP(AP_KRW))
285 /* Move the virtual address to the correct bit location */
286 lsr r2, #(L1_S_SHIFT - 2)
292 add r1, r1, #(L1_S_SIZE)
314 .word svcstk + INIT_ARM_STACK_SIZE
320 .asciz "main() returned"
325 .space INIT_ARM_STACK_SIZE
328 * Memory for the initial pagetable. We are unable to place this in
329 * the bss as this will be cleared after the table is loaded.
331 .section ".init_pagetable"
332 .align 14 /* 16KiB aligned */
340 .word _C_LABEL(cpufuncs)
346 Lstartup_pagetable_secondary:
351 /* Make sure interrupts are disabled. */
353 orr r7, r7, #(I32_bit|F32_bit)
356 /* Disable MMU. It should be disabled already, but make sure. */
357 mrc p15, 0, r2, c1, c0, 0
358 bic r2, r2, #(CPU_CONTROL_MMU_ENABLE | CPU_CONTROL_DC_ENABLE |\
359 CPU_CONTROL_WBUF_ENABLE)
360 bic r2, r2, #(CPU_CONTROL_IC_ENABLE)
361 bic r2, r2, #(CPU_CONTROL_BPRD_ENABLE)
362 mcr p15, 0, r2, c1, c0, 0
368 #if defined(ARM_MMU_V6)
369 bl armv6_idcache_inv_all /* Modifies r0 only */
370 #elif defined(ARM_MMU_V7)
371 bl armv7_idcache_inv_all /* Modifies r0-r3, ip */
374 ldr r0, Lstartup_pagetable_secondary
375 bic r0, r0, #0xf0000000
376 orr r0, r0, #PHYSADDR
378 orr r0, r0, #2 /* Set TTB shared memory flag */
379 mcr p15, 0, r0, c2, c0, 0 /* Set TTB */
380 mcr p15, 0, r0, c8, c7, 0 /* Flush TLB */
383 mcr p15, 0, r0, c13, c0, 1 /* Set ASID to 0 */
385 /* Set the Domain Access register. Very important! */
386 mov r0, #((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT)
387 mcr p15, 0, r0, c3, c0, 0
389 mrc p15, 0, r0, c1, c0, 0
390 orr r0, r0, #CPU_CONTROL_V6_EXTPAGE
391 orr r0, r0, #CPU_CONTROL_AF_ENABLE
392 orr r0, r0, #(CPU_CONTROL_MMU_ENABLE | CPU_CONTROL_DC_ENABLE |\
393 CPU_CONTROL_WBUF_ENABLE)
394 orr r0, r0, #(CPU_CONTROL_IC_ENABLE)
395 orr r0, r0, #(CPU_CONTROL_BPRD_ENABLE)
396 mcr p15, 0, r0, c1, c0, 0
403 ldmia r1, {r1, r2, sp} /* Set initial stack and */
404 mrc p15, 0, r0, c0, c0, 5
410 ldr pc, .Lmpvirt_done
414 mov fp, #0 /* trace back starts here */
415 bl _C_LABEL(init_secondary) /* Off we go */
422 .asciz "init_secondary() returned"
429 bic r2, r2, #(PSR_MODE)
430 orr r2, r2, #(PSR_SVC32_MODE)
431 orr r2, r2, #(I32_bit | F32_bit)
434 ldr r4, .Lcpu_reset_address
439 ldr pc, [r0, #CF_IDCACHE_WBINV_ALL]
441 ldr pc, [r0, #CF_L2CACHE_WBINV_ALL]
444 * Load the cpu_reset_needs_v4_MMU_disable flag to determine if it's
448 ldr r1, .Lcpu_reset_needs_v4_MMU_disable
454 * MMU & IDC off, 32 bit program & data space
455 * Hurl ourselves into the ROM
457 mov r0, #(CPU_CONTROL_32BP_ENABLE | CPU_CONTROL_32BD_ENABLE)
458 mcr 15, 0, r0, c1, c0, 0
459 mcrne 15, 0, r2, c8, c7, 0 /* nail I+D TLB on ARMv4 and greater */
463 * _cpu_reset_address contains the address to branch to, to complete
464 * the cpu reset after turning the MMU off
465 * This variable is provided by the hardware specific code
468 .word _C_LABEL(cpu_reset_address)
471 * cpu_reset_needs_v4_MMU_disable contains a flag that signals if the
472 * v4 MMU disable instruction needs executing... it is an illegal instruction
473 * on f.e. ARM6/7 that locks up the computer in an endless illegal
474 * instruction / data-abort / reset loop.
476 .Lcpu_reset_needs_v4_MMU_disable:
477 .word _C_LABEL(cpu_reset_needs_v4_MMU_disable)
497 .global _C_LABEL(esym)
498 _C_LABEL(esym): .word _C_LABEL(end)
509 * Call the sigreturn system call.
511 * We have to load r7 manually rather than using
512 * "ldr r7, =SYS_sigreturn" to ensure the value of szsigcode is
513 * correct. Using the alternative places esigcode at the address
514 * of the data rather than the address one past the data.
517 ldr r7, [pc, #12] /* Load SYS_sigreturn */
520 /* Well if that failed we better exit quick ! */
522 ldr r7, [pc, #8] /* Load SYS_exit */
525 /* Branch back to retry SYS_sigreturn */
532 .global _C_LABEL(esigcode)
538 .long esigcode-sigcode
540 /* End of locore.S */