locore.S revision 1.17
11.17Spooka/*	$NetBSD: locore.S,v 1.17 2005/10/10 09:58:54 pooka 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 "opt_ipkdb.h"
351.1Schris#include "assym.h"
361.1Schris#include <sys/syscall.h>
371.1Schris#include <sys/errno.h>
381.1Schris#include <machine/asm.h>
391.1Schris#include <machine/cpu.h>
401.1Schris#include <machine/frame.h>
411.1Schris#include <machine/param.h>
421.1Schris
431.1Schris/* What size should this really be ? It is only used by init_arm() */
441.1Schris#define INIT_ARM_STACK_SIZE	2048
451.1Schris
461.1Schris/*
471.1Schris * This is for kvm_mkdb, and should be the address of the beginning
481.1Schris * of the kernel text segment (not necessarily the same as kernbase).
491.1Schris */
501.1Schris
511.2Sthorpej	.text
521.2Sthorpej	.align	0
531.5Sthorpej
541.5SthorpejENTRY_NP(kernel_text)
551.2Sthorpej
561.1SchrisASENTRY_NP(start)
571.9Sbjh21	adr	r1, .Lstart
581.4Sthorpej	ldmia	r1, {r1, r2, sp}	/* Set initial stack and */
591.4Sthorpej	sub	r2, r2, r1		/* get zero init data */
601.1Schris	mov	r3, #0
611.1Schris
621.7Sbriggs.L1:
631.4Sthorpej	str	r3, [r1], #0x0004	/* Zero the bss */
641.1Schris	subs	r2, r2, #4
651.7Sbriggs	bgt	.L1
661.1Schris
671.4Sthorpej	mov	fp, #0x00000000		/* trace back starts here */
681.4Sthorpej	bl	_C_LABEL(initarm)	/* Off we go */
691.1Schris
701.1Schris	/* init arm will return the new stack pointer. */
711.1Schris	mov	sp, r0
721.1Schris
731.1Schris	mov	fp, #0x00000000		/* trace back starts here */
741.1Schris	mov	ip, sp
751.1Schris	stmfd	sp!, {fp, ip, lr, pc}
761.1Schris	sub	fp, ip, #4
771.1Schris
781.4Sthorpej	bl	_C_LABEL(main)		/* call main()! */
791.1Schris
801.9Sbjh21	adr	r0, .Lmainreturned
811.4Sthorpej	b	_C_LABEL(panic)
821.17Spooka	/* NOTREACHED */
831.1Schris
841.7Sbriggs.Lstart:
851.1Schris	.word	_edata
861.1Schris	.word	_end
871.1Schris	.word	svcstk + INIT_ARM_STACK_SIZE
881.4Sthorpej
891.8Sthorpej.Lmainreturned:
901.4Sthorpej	.asciz	"main() returned"
911.4Sthorpej	.align	0
921.1Schris
931.1Schris	.bss
941.1Schrissvcstk:
951.1Schris	.space	INIT_ARM_STACK_SIZE
961.1Schris
971.1Schris	.text
981.1Schris	.align	0
991.1Schris
1001.1Schris#ifndef OFW
1011.1Schris	/* OFW based systems will used OF_boot() */
1021.1Schris
1031.8Sthorpej.Lcpufuncs:
1041.1Schris	.word	_C_LABEL(cpufuncs)
1051.1Schris
1061.1SchrisENTRY_NP(cpu_reset)
1071.7Sbriggs	mrs     r2, cpsr
1081.1Schris	bic	r2, r2, #(PSR_MODE)
1091.1Schris	orr     r2, r2, #(PSR_SVC32_MODE)
1101.1Schris	orr	r2, r2, #(I32_bit | F32_bit)
1111.1Schris	msr     cpsr_all, r2
1121.1Schris
1131.8Sthorpej	ldr	r4, .Lcpu_reset_address
1141.1Schris	ldr	r4, [r4]
1151.1Schris
1161.8Sthorpej	ldr	r0, .Lcpufuncs
1171.10Sbsh	mov	lr, pc
1181.3Sthorpej	ldr	pc, [r0, #CF_IDCACHE_WBINV_ALL]
1191.1Schris
1201.1Schris	/*
1211.1Schris	 * Load the cpu_reset_needs_v4_MMU_disable flag to determine if it's
1221.1Schris	 * necessary.
1231.1Schris	 */
1241.1Schris
1251.8Sthorpej	ldr	r1, .Lcpu_reset_needs_v4_MMU_disable
1261.1Schris	ldr	r1, [r1]
1271.1Schris	cmp	r1, #0
1281.14Sthorpej	mov	r2, #0
1291.1Schris
1301.1Schris	/*
1311.1Schris 	 * MMU & IDC off, 32 bit program & data space
1321.1Schris	 * Hurl ourselves into the ROM
1331.1Schris	 */
1341.13Sthorpej	mov	r0, #(CPU_CONTROL_32BP_ENABLE | CPU_CONTROL_32BD_ENABLE)
1351.1Schris	mcr     15, 0, r0, c1, c0, 0
1361.14Sthorpej	mcrne   15, 0, r2, c8, c7, 0 	/* nail I+D TLB on ARMv4 and greater */
1371.1Schris	mov     pc, r4
1381.1Schris
1391.1Schris	/*
1401.1Schris	 * _cpu_reset_address contains the address to branch to, to complete
1411.15Swiz	 * the CPU reset after turning the MMU off
1421.1Schris	 * This variable is provided by the hardware specific code
1431.1Schris	 */
1441.8Sthorpej.Lcpu_reset_address:
1451.1Schris	.word	_C_LABEL(cpu_reset_address)
1461.1Schris
1471.1Schris	/*
1481.1Schris	 * cpu_reset_needs_v4_MMU_disable contains a flag that signals if the
1491.1Schris	 * v4 MMU disable instruction needs executing... it is an illegal instruction
1501.1Schris	 * on f.e. ARM6/7 that locks up the computer in an endless illegal
1511.1Schris	 * instruction / data-abort / reset loop.
1521.1Schris	 */
1531.8Sthorpej.Lcpu_reset_needs_v4_MMU_disable:
1541.1Schris	.word	_C_LABEL(cpu_reset_needs_v4_MMU_disable)
1551.1Schris
1561.1Schris#endif	/* OFW */
1571.1Schris
1581.1Schris#ifdef IPKDB
1591.1Schris/*
1601.1Schris * Execute(inst, psr, args, sp)
1611.1Schris *
1621.1Schris * Execute INSTruction with PSR and ARGS[0] - ARGS[3] making
1631.1Schris * available stack at SP for next undefined instruction trap.
1641.1Schris *
1651.1Schris * Move the instruction onto the stack and jump to it.
1661.1Schris */
1671.1SchrisENTRY_NP(Execute)
1681.1Schris	mov	ip, sp
1691.1Schris	stmfd	sp!, {r2, r4-r7, fp, ip, lr, pc}
1701.1Schris	sub	fp, ip, #4
1711.1Schris	mov	ip, r3
1721.7Sbriggs	ldr	r7, .Lreturn
1731.1Schris	stmfd	sp!, {r0, r7}
1741.16Sskrll	adr	r7, .LExec
1751.1Schris	mov	r5, r1
1761.7Sbriggs	mrs	r4, cpsr
1771.1Schris	ldmia	r2, {r0-r3}
1781.1Schris	mov	r6, sp
1791.1Schris	mov	sp, ip
1801.1Schris	msr	cpsr_all, r5
1811.1Schris	mov	pc, r6
1821.7Sbriggs.LExec:
1831.7Sbriggs	mrs	r5, cpsr
1841.1Schris/* XXX Cannot switch thus easily back from user mode */
1851.1Schris	msr	cpsr_all, r4
1861.1Schris	add	sp, r6, #8
1871.1Schris	ldmfd	sp!, {r6}
1881.1Schris	stmia	r6, {r0-r3}
1891.1Schris	mov	r0, r5
1901.1Schris	ldmdb	fp, {r4-r7, fp, sp, pc}
1911.7Sbriggs.Lreturn:
1921.1Schris	mov	pc, r7
1931.1Schris#endif
1941.1Schris
1951.1Schris/*
1961.1Schris * setjump + longjmp
1971.1Schris */
1981.1SchrisENTRY(setjmp)
1991.1Schris	stmia	r0, {r4-r14}
2001.1Schris	mov	r0, #0x00000000
2011.1Schris	mov	pc, lr
2021.1Schris
2031.1SchrisENTRY(longjmp)
2041.1Schris	ldmia	r0, {r4-r14}
2051.1Schris	mov	r0, #0x00000001
2061.1Schris	mov	pc, lr
2071.1Schris
2081.1Schris	.data
2091.1Schris	.global _C_LABEL(esym)
2101.1Schris_C_LABEL(esym):	.word	_C_LABEL(end)
2111.1Schris
2121.1SchrisENTRY_NP(abort)
2131.1Schris	b	_C_LABEL(abort)
2141.1Schris
2151.1Schris
2161.1Schris/* End of locore.S */
217