locore.S revision 1.30
11.30Smatt/*	$NetBSD: locore.S,v 1.30 2012/11/21 19:38:36 matt Exp $	*/
21.1Schris
31.1Schris/*
41.1Schris * Copyright (C) 1994-1997 Mark Brinicombe
51.1Schris * Copyright (C) 1994 Brini
61.1Schris * All rights reserved.
71.1Schris *
81.1Schris * Redistribution and use in source and binary forms, with or without
91.1Schris * modification, are permitted provided that the following conditions
101.1Schris * are met:
111.1Schris * 1. Redistributions of source code must retain the above copyright
121.1Schris *    notice, this list of conditions and the following disclaimer.
131.1Schris * 2. Redistributions in binary form must reproduce the above copyright
141.1Schris *    notice, this list of conditions and the following disclaimer in the
151.1Schris *    documentation and/or other materials provided with the distribution.
161.1Schris * 3. All advertising materials mentioning features or use of this software
171.1Schris *    must display the following acknowledgement:
181.1Schris *	This product includes software developed by Brini.
191.1Schris * 4. The name of Brini may not be used to endorse or promote products
201.1Schris *    derived from this software without specific prior written permission.
211.1Schris *
221.1Schris * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR
231.1Schris * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
241.1Schris * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
251.1Schris * IN NO EVENT SHALL BRINI BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
261.1Schris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
271.1Schris * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
281.1Schris * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
291.1Schris * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
301.1Schris * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
311.1Schris * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
321.1Schris */
331.1Schris
341.1Schris#include "assym.h"
351.1Schris#include <sys/syscall.h>
361.1Schris#include <sys/errno.h>
371.1Schris#include <machine/asm.h>
381.1Schris#include <machine/cpu.h>
391.1Schris#include <machine/frame.h>
401.1Schris#include <machine/param.h>
411.1Schris
421.1Schris/* What size should this really be ? It is only used by init_arm() */
431.1Schris#define INIT_ARM_STACK_SIZE	2048
441.1Schris
451.30Smatt	RCSID("$NetBSD: locore.S,v 1.30 2012/11/21 19:38:36 matt Exp $")
461.23Smatt
471.1Schris/*
481.1Schris * This is for kvm_mkdb, and should be the address of the beginning
491.1Schris * of the kernel text segment (not necessarily the same as kernbase).
501.1Schris */
511.1Schris
521.2Sthorpej	.text
531.2Sthorpej	.align	0
541.5Sthorpej
551.5SthorpejENTRY_NP(kernel_text)
561.2Sthorpej
571.1SchrisASENTRY_NP(start)
581.29Smatt	mrs	r1, cpsr		/* fetch CPSR value */
591.29Smatt	msr	spsr_sx, r1		/* set SPSR[23:8] to known value */
601.29Smatt
611.9Sbjh21	adr	r1, .Lstart
621.27Smatt	ldmia	r1, {r1, r2, r8, sp}	/* Set initial stack and */
631.27Smatt	sub	r2, r2, r1		/* get zero init data and cpu_info_store */
641.25Smatt
651.27Smatt#if defined(TPIDRPRW_IS_CURCPU) || defined(TPIDRPRW_IS_CURLWP)
661.27Smatt	mcr	p15, 0, r8, c13, c0, 4
671.25Smatt#endif
681.28Smatt#if defined(TPIDRPRW_IS_CURLWP)
691.28Smatt	ldr	r8, [r8, #L_CPU]	/* r8 needs curcpu in it */
701.28Smatt#endif
711.25Smatt
721.1Schris	mov	r3, #0
731.7Sbriggs.L1:
741.4Sthorpej	str	r3, [r1], #0x0004	/* Zero the bss */
751.1Schris	subs	r2, r2, #4
761.7Sbriggs	bgt	.L1
771.1Schris
781.27Smatt	mrc	p15, 0, r3, c0, c0, 0	/* get our cpuid and save it early */
791.27Smatt	str	r3, [r8, #CI_ARM_CPUID]
801.27Smatt
811.4Sthorpej	mov	fp, #0x00000000		/* trace back starts here */
821.4Sthorpej	bl	_C_LABEL(initarm)	/* Off we go */
831.1Schris
841.1Schris	/* init arm will return the new stack pointer. */
851.1Schris	mov	sp, r0
861.1Schris
871.1Schris	mov	fp, #0x00000000		/* trace back starts here */
881.1Schris	mov	ip, sp
891.1Schris	stmfd	sp!, {fp, ip, lr, pc}
901.1Schris	sub	fp, ip, #4
911.1Schris
921.4Sthorpej	bl	_C_LABEL(main)		/* call main()! */
931.1Schris
941.9Sbjh21	adr	r0, .Lmainreturned
951.4Sthorpej	b	_C_LABEL(panic)
961.17Spooka	/* NOTREACHED */
971.1Schris
981.7Sbriggs.Lstart:
991.1Schris	.word	_edata
1001.1Schris	.word	_end
1011.28Smatt#if defined(TPIDRPRW_IS_CURLWP)
1021.27Smatt	.word	_C_LABEL(lwp0)
1031.27Smatt#else
1041.28Smatt	.word	_C_LABEL(cpu_info_store)
1051.27Smatt#endif
1061.1Schris	.word	svcstk + INIT_ARM_STACK_SIZE
1071.4Sthorpej
1081.8Sthorpej.Lmainreturned:
1091.4Sthorpej	.asciz	"main() returned"
1101.4Sthorpej	.align	0
1111.1Schris
1121.1Schris	.bss
1131.1Schrissvcstk:
1141.1Schris	.space	INIT_ARM_STACK_SIZE
1151.1Schris
1161.1Schris	.text
1171.1Schris	.align	0
1181.1Schris
1191.1Schris#ifndef OFW
1201.1Schris	/* OFW based systems will used OF_boot() */
1211.1Schris
1221.8Sthorpej.Lcpufuncs:
1231.1Schris	.word	_C_LABEL(cpufuncs)
1241.1Schris
1251.1SchrisENTRY_NP(cpu_reset)
1261.27Smatt#ifdef _ARM_ARCH_6
1271.27Smatt	cpsid	if, #PSR_SVC32_MODE
1281.27Smatt#else
1291.7Sbriggs	mrs     r2, cpsr
1301.1Schris	bic	r2, r2, #(PSR_MODE)
1311.1Schris	orr     r2, r2, #(PSR_SVC32_MODE)
1321.24Smatt	orr	r2, r2, #(IF32_bits)
1331.22Schris	msr     cpsr_c, r2
1341.27Smatt#endif
1351.1Schris
1361.8Sthorpej	ldr	r0, .Lcpufuncs
1371.10Sbsh	mov	lr, pc
1381.3Sthorpej	ldr	pc, [r0, #CF_IDCACHE_WBINV_ALL]
1391.1Schris
1401.1Schris	/*
1411.26Smatt	 * Load the virutal address of the MD reset function first.
1421.1Schris	 */
1431.26Smatt	ldr	r4, .Lcpu_reset_address
1441.26Smatt	ldr	r4, [r4]
1451.26Smatt	cmp	r4, #0
1461.1Schris
1471.26Smatt	/*
1481.26Smatt	 * If virtual address is NULL, we must be using the physical address
1491.26Smatt	 */
1501.26Smatt	ldreq	r4, .Lcpu_reset_address_paddr
1511.26Smatt	ldreq	r4, [r4]
1521.1Schris
1531.1Schris	/*
1541.1Schris 	 * MMU & IDC off, 32 bit program & data space
1551.1Schris	 * Hurl ourselves into the ROM
1561.1Schris	 */
1571.27Smatt	mrc	p15, 0, r0, c1, c0, 0
1581.30Smatt	bic	r0, r0, #(CPU_CONTROL_MMU_ENABLE | CPU_CONTROL_DC_ENABLE)
1591.30Smatt	bic	r0, r0, #(CPU_CONTROL_IC_ENABLE)
1601.30Smatt	orr	r0, r0, #(CPU_CONTROL_32BP_ENABLE | CPU_CONTROL_32BD_ENABLE)
1611.27Smatt	mcr	p15, 0, r0, c1, c0, 0
1621.27Smatt	mcreq	p15, 0, r2, c8, c7, 0 	/* nail I+D TLB on ARMv4 and greater */
1631.1Schris	mov     pc, r4
1641.1Schris
1651.1Schris	/*
1661.26Smatt	 * cpu_reset_address contains the address to branch to, to complete
1671.15Swiz	 * the CPU reset after turning the MMU off
1681.1Schris	 * This variable is provided by the hardware specific code
1691.1Schris	 */
1701.8Sthorpej.Lcpu_reset_address:
1711.1Schris	.word	_C_LABEL(cpu_reset_address)
1721.26Smatt.Lcpu_reset_address_paddr:
1731.26Smatt	.word	_C_LABEL(cpu_reset_address_paddr)
1741.1Schris#endif	/* OFW */
1751.1Schris
1761.1Schris/*
1771.1Schris * setjump + longjmp
1781.1Schris */
1791.1SchrisENTRY(setjmp)
1801.1Schris	stmia	r0, {r4-r14}
1811.1Schris	mov	r0, #0x00000000
1821.1Schris	mov	pc, lr
1831.1Schris
1841.1SchrisENTRY(longjmp)
1851.1Schris	ldmia	r0, {r4-r14}
1861.1Schris	mov	r0, #0x00000001
1871.1Schris	mov	pc, lr
1881.1Schris
1891.1Schris	.data
1901.1Schris	.global _C_LABEL(esym)
1911.1Schris_C_LABEL(esym):	.word	_C_LABEL(end)
1921.1Schris
1931.1SchrisENTRY_NP(abort)
1941.1Schris	b	_C_LABEL(abort)
1951.1Schris
1961.19Schris/*
1971.21Sskrll * Part of doing a system dump, we need to save a switchframe onto the
1981.21Sskrll * stack, then save the rest of the registers into the dumppcb.
1991.19Schris */
2001.19SchrisENTRY(dumpsys)
2011.19Schris	/* push registers onto stack */
2021.20Sskrll	mov	ip, sp
2031.20Sskrll	stmfd	sp!, {r4-r7, ip, lr}
2041.19Schris
2051.19Schris	/* fill in dumppcb */
2061.19Schris	ldr	r0, .Ldumppcb
2071.19Schris
2081.19Schris#ifndef __XSCALE__
2091.19Schris        add     r2, r0, #(PCB_R8)
2101.19Schris        stmia   r2, {r8-r13}
2111.19Schris#else
2121.19Schris        strd    r8, [r0, #(PCB_R8)]
2131.19Schris        strd    r10, [r0, #(PCB_R10)]
2141.19Schris        strd    r12, [r0, #(PCB_R12)]
2151.19Schris#endif
2161.19Schris
2171.19Schris	bl	_C_LABEL(dodumpsys)
2181.19Schris
2191.19Schris	/* unwind the stack */
2201.20Sskrll	ldmfd	sp, {r4-r7, sp, pc}
2211.19Schris
2221.19Schris.Ldumppcb:
2231.19Schris	.word	_C_LABEL(dumppcb)
2241.1Schris
2251.1Schris/* End of locore.S */
226