xref: /netbsd-src/sys/arch/arm/arm32/locore.S (revision 975a152cfcdb39ae6e496af647af0c7275ca0b61)
1/*	$NetBSD: locore.S,v 1.33 2013/08/18 06:28:18 matt Exp $	*/
2
3/*
4 * Copyright (C) 1994-1997 Mark Brinicombe
5 * Copyright (C) 1994 Brini
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by Brini.
19 * 4. The name of Brini may not be used to endorse or promote products
20 *    derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL BRINI BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include "assym.h"
35#include <sys/syscall.h>
36#include <sys/errno.h>
37#include <arm/asm.h>
38#include <arm/locore.h>
39
40/* What size should this really be ? It is only used by init_arm() */
41#define INIT_ARM_STACK_SIZE	2048
42
43	RCSID("$NetBSD: locore.S,v 1.33 2013/08/18 06:28:18 matt Exp $")
44
45/*
46 * This is for kvm_mkdb, and should be the address of the beginning
47 * of the kernel text segment (not necessarily the same as kernbase).
48 */
49
50	.text
51	.align	0
52
53ENTRY_NP(kernel_text)
54
55ASENTRY_NP(start)
56	mrs	r1, cpsr		/* fetch CPSR value */
57	msr	spsr_sx, r1		/* set SPSR[23:8] to known value */
58
59	adr	r1, .Lstart
60	ldmia	r1, {r1, r2, r8, sp}	/* Set initial stack and */
61
62#if defined(TPIDRPRW_IS_CURCPU) || defined(TPIDRPRW_IS_CURLWP)
63	mcr	p15, 0, r8, c13, c0, 4
64#endif
65#if defined(TPIDRPRW_IS_CURLWP)
66	ldr	r8, [r8, #L_CPU]	/* r8 needs curcpu in it */
67#endif
68
69	mov	r4, #0
70#ifdef _ARM_ARCH_DWORD_OK
71	mov	r5, #0
72#endif
73.L1:
74#ifdef _ARM_ARCH_DWORD_OK
75	strd	r4, [r1], #0x0008	/* Zero the bss */
76#else
77	str	r4, [r1], #0x0004	/* Zero the bss */
78#endif
79	cmp	r1, r2
80	blt	.L1
81
82	mrc	p15, 0, r3, c0, c0, 0	/* get our cpuid and save it early */
83	str	r3, [r8, #CI_ARM_CPUID]
84
85	mov	fp, #0x00000000		/* trace back starts here */
86	bl	_C_LABEL(initarm)	/* Off we go */
87
88	/* init arm will return the new stack pointer. */
89	mov	sp, r0
90
91	mov	fp, #0x00000000		/* trace back starts here */
92	mov	ip, sp
93	push	{fp, ip, lr, pc}
94	sub	fp, ip, #4
95
96	bl	_C_LABEL(main)		/* call main()! */
97
98	adr	r0, .Lmainreturned
99	b	_C_LABEL(panic)
100	/* NOTREACHED */
101
102.Lstart:
103	.word	_edata
104	.word	_end
105#if defined(TPIDRPRW_IS_CURLWP)
106	.word	_C_LABEL(lwp0)
107#else
108	.word	_C_LABEL(cpu_info_store)
109#endif
110	.word	svcstk + INIT_ARM_STACK_SIZE
111
112.Lmainreturned:
113	.asciz	"main() returned"
114	.align	0
115ASEND(start)
116
117	.bss
118svcstk:
119	.space	INIT_ARM_STACK_SIZE
120
121	.text
122	.align	0
123
124#ifndef OFW
125	/* OFW based systems will used OF_boot() */
126
127.Lcpufuncs:
128	.word	_C_LABEL(cpufuncs)
129
130ENTRY_NP(cpu_reset)
131#ifdef _ARM_ARCH_6
132	cpsid	if, #PSR_SVC32_MODE
133#else
134	mrs     r2, cpsr
135	bic	r2, r2, #(PSR_MODE)
136	orr     r2, r2, #(PSR_SVC32_MODE)
137	orr	r2, r2, #(IF32_bits)
138	msr     cpsr_c, r2
139#endif
140
141	ldr	r0, .Lcpufuncs
142	mov	lr, pc
143	ldr	pc, [r0, #CF_IDCACHE_WBINV_ALL]
144
145	/*
146	 * Load the virutal address of the MD reset function first.
147	 */
148	ldr	r4, .Lcpu_reset_address
149	ldr	r4, [r4]
150	cmp	r4, #0
151
152	/*
153	 * If virtual address is NULL, we must be using the physical address
154	 */
155	ldreq	r4, .Lcpu_reset_address_paddr
156	ldreq	r4, [r4]
157
158	/*
159 	 * MMU & IDC off, 32 bit program & data space
160	 * Hurl ourselves into the ROM
161	 */
162	mrc	p15, 0, r0, c1, c0, 0
163	bic	r0, r0, #(CPU_CONTROL_MMU_ENABLE | CPU_CONTROL_DC_ENABLE)
164	bic	r0, r0, #(CPU_CONTROL_IC_ENABLE)
165	orr	r0, r0, #(CPU_CONTROL_32BP_ENABLE | CPU_CONTROL_32BD_ENABLE)
166	mcr	p15, 0, r0, c1, c0, 0
167	mcreq	p15, 0, r2, c8, c7, 0 	/* nail I+D TLB on ARMv4 and greater */
168	mov     pc, r4
169
170	/*
171	 * cpu_reset_address contains the address to branch to, to complete
172	 * the CPU reset after turning the MMU off
173	 * This variable is provided by the hardware specific code
174	 */
175.Lcpu_reset_address:
176	.word	_C_LABEL(cpu_reset_address)
177.Lcpu_reset_address_paddr:
178	.word	_C_LABEL(cpu_reset_address_paddr)
179END(cpu_reset)
180#endif	/* OFW */
181
182/*
183 * setjump + longjmp
184 */
185ENTRY(setjmp)
186	stmia	r0, {r4-r14}
187	mov	r0, #0x00000000
188	RET
189END(setjmp)
190
191ENTRY(longjmp)
192	ldmia	r0, {r4-r14}
193	mov	r0, #0x00000001
194	RET
195END(longjmp)
196
197	.data
198	.global _C_LABEL(esym)
199_C_LABEL(esym):	.word	_C_LABEL(end)
200
201ENTRY_NP(abort)
202	b	_C_LABEL(abort)
203END(abort)
204
205/*
206 * Part of doing a system dump, we need to save a switchframe onto the
207 * stack, then save the rest of the registers into the dumppcb.
208 */
209ENTRY(dumpsys)
210	/* push registers onto stack */
211	mov	ip, sp
212	push	{r4-r7, ip, lr}
213
214	/* fill in dumppcb */
215	ldr	r0, .Ldumppcb
216
217#ifndef __XSCALE__
218        add     r2, r0, #(PCB_R8)
219        stmia   r2, {r8-r13}
220#else
221        strd    r8, [r0, #(PCB_R8)]
222        strd    r10, [r0, #(PCB_R10)]
223        strd    r12, [r0, #(PCB_R12)]
224#endif
225
226	bl	_C_LABEL(dodumpsys)
227
228	/* unwind the stack */
229	pop	{r4-r7, ip, pc}
230
231.Ldumppcb:
232	.word	_C_LABEL(dumppcb)
233END(dumpsys)
234
235/* End of locore.S */
236