locore.S revision 1.3
11.3Sthorpej/*	$NetBSD: locore.S,v 1.3 2002/01/25 19:19:25 thorpej 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.1Schris#ifndef netwinder
521.1SchrisENTRY_NP(kernel_text)
531.1Schris#endif
541.1Schris
551.2Sthorpej	.text
561.2Sthorpej	.align	0
571.2Sthorpej
581.1SchrisASENTRY_NP(start)
591.1Schris	add	r1, pc, #(Lstart - . - 8)
601.1Schris	ldmia	r1, {r1, r2, sp}		/* Set initial stack and */
611.1Schris	sub	r2, r2, r1			/* get zero init data */
621.1Schris	mov	r3, #0
631.1Schris
641.1SchrisL1:
651.1Schris	str	r3, [r1], #0x0004		/* Zero the bss */
661.1Schris	subs	r2, r2, #4
671.1Schris	bgt	L1
681.1Schris
691.1Schris	mov	fp, #0x00000000			/* trace back starts here */
701.1Schris	bl	_C_LABEL(initarm)		/* Off we go */
711.1Schris
721.1Schris	/* init arm will return the new stack pointer. */
731.1Schris	mov	sp, r0
741.1Schris
751.1Schris	mov	fp, #0x00000000		/* trace back starts here */
761.1Schris	mov	ip, sp
771.1Schris	stmfd	sp!, {fp, ip, lr, pc}
781.1Schris	sub	fp, ip, #4
791.1Schris
801.1Schris	/* Setup an initial trap frame for start_init to use */
811.1Schris
821.1Schris	PUSHFRAME
831.1Schris
841.1Schris	mov	r0, sp			/* parameter to main is trap frame */
851.1Schris
861.1Schris	bl	_C_LABEL(main)		/* Lets light the flame and start her up */
871.1Schris
881.1Schris	PULLFRAME			/* Pull the trap frame, now valid */
891.1Schris
901.1Schris	movs	pc, lr			/* Exit to user process */
911.1Schris
921.1Schris	/* Never gets here */
931.1Schris
941.1Schris	b	.
951.1Schris
961.1SchrisLstart:
971.1Schris	.word	_edata
981.1Schris	.word	_end
991.1Schris	.word	svcstk + INIT_ARM_STACK_SIZE
1001.1Schris
1011.1Schris	.bss
1021.1Schrissvcstk:
1031.1Schris	.space	INIT_ARM_STACK_SIZE
1041.1Schris
1051.1Schris	.text
1061.1Schris	.align	0
1071.1Schris
1081.1Schris#ifndef OFW
1091.1Schris	/* OFW based systems will used OF_boot() */
1101.1Schris
1111.1SchrisLcpufuncs:
1121.1Schris	.word	_C_LABEL(cpufuncs)
1131.1Schris
1141.1SchrisENTRY_NP(cpu_reset)
1151.1Schris	mrs     r2, cpsr_all
1161.1Schris	bic	r2, r2, #(PSR_MODE)
1171.1Schris	orr     r2, r2, #(PSR_SVC32_MODE)
1181.1Schris	orr	r2, r2, #(I32_bit | F32_bit)
1191.1Schris	msr     cpsr_all, r2
1201.1Schris
1211.1Schris	ldr	r4, Lcpu_reset_address
1221.1Schris	ldr	r4, [r4]
1231.1Schris
1241.1Schris	ldr	r0, Lcpufuncs
1251.1Schris	add	lr, pc, #Lboot_cache_purged - . - 8
1261.3Sthorpej	ldr	pc, [r0, #CF_IDCACHE_WBINV_ALL]
1271.1Schris
1281.1SchrisLboot_cache_purged:
1291.1Schris
1301.1Schris	/*
1311.1Schris	 * Load the cpu_reset_needs_v4_MMU_disable flag to determine if it's
1321.1Schris	 * necessary.
1331.1Schris	 */
1341.1Schris
1351.1Schris	ldr	r1, Lcpu_reset_needs_v4_MMU_disable
1361.1Schris	ldr	r1, [r1]
1371.1Schris	cmp	r1, #0
1381.1Schris
1391.1Schris	/*
1401.1Schris 	 * MMU & IDC off, 32 bit program & data space
1411.1Schris	 * Hurl ourselves into the ROM
1421.1Schris	 */
1431.1Schris	mov	r0, #(CPU_CONTROL_32BP_ENABLE | CPU_CONTROL_32BD_ENABLE)
1441.1Schris	mcr     15, 0, r0, c1, c0, 0
1451.1Schris	mcrne   15, 0, r0, c8, c7, 0 	/* only when v4 MMU disable is asked for */
1461.1Schris	mov     pc, r4
1471.1Schris
1481.1Schris	/*
1491.1Schris	 * _cpu_reset_address contains the address to branch to, to complete
1501.1Schris	 * the cpu reset after turning the MMU off
1511.1Schris	 * This variable is provided by the hardware specific code
1521.1Schris	 */
1531.1SchrisLcpu_reset_address:
1541.1Schris	.word	_C_LABEL(cpu_reset_address)
1551.1Schris
1561.1Schris	/*
1571.1Schris	 * cpu_reset_needs_v4_MMU_disable contains a flag that signals if the
1581.1Schris	 * v4 MMU disable instruction needs executing... it is an illegal instruction
1591.1Schris	 * on f.e. ARM6/7 that locks up the computer in an endless illegal
1601.1Schris	 * instruction / data-abort / reset loop.
1611.1Schris	 */
1621.1SchrisLcpu_reset_needs_v4_MMU_disable:
1631.1Schris	.word	_C_LABEL(cpu_reset_needs_v4_MMU_disable)
1641.1Schris
1651.1Schris#endif	/* OFW */
1661.1Schris
1671.1Schris#ifdef IPKDB
1681.1Schris/*
1691.1Schris * Execute(inst, psr, args, sp)
1701.1Schris *
1711.1Schris * Execute INSTruction with PSR and ARGS[0] - ARGS[3] making
1721.1Schris * available stack at SP for next undefined instruction trap.
1731.1Schris *
1741.1Schris * Move the instruction onto the stack and jump to it.
1751.1Schris */
1761.1SchrisENTRY_NP(Execute)
1771.1Schris	mov	ip, sp
1781.1Schris	stmfd	sp!, {r2, r4-r7, fp, ip, lr, pc}
1791.1Schris	sub	fp, ip, #4
1801.1Schris	mov	ip, r3
1811.1Schris	ldr	r7, return
1821.1Schris	stmfd	sp!, {r0, r7}
1831.1Schris	add	r7, pc, #LExec - . - 8
1841.1Schris	mov	r5, r1
1851.1Schris	mrs	r4, cpsr_all
1861.1Schris	ldmia	r2, {r0-r3}
1871.1Schris	mov	r6, sp
1881.1Schris	mov	sp, ip
1891.1Schris	msr	cpsr_all, r5
1901.1Schris	mov	pc, r6
1911.1SchrisLExec:
1921.1Schris	mrs	r5, cpsr_all
1931.1Schris/* XXX Cannot switch thus easily back from user mode */
1941.1Schris	msr	cpsr_all, r4
1951.1Schris	add	sp, r6, #8
1961.1Schris	ldmfd	sp!, {r6}
1971.1Schris	stmia	r6, {r0-r3}
1981.1Schris	mov	r0, r5
1991.1Schris	ldmdb	fp, {r4-r7, fp, sp, pc}
2001.1Schrisreturn:
2011.1Schris	mov	pc, r7
2021.1Schris#endif
2031.1Schris
2041.1Schris/*
2051.1Schris * setjump + longjmp
2061.1Schris */
2071.1SchrisENTRY(setjmp)
2081.1Schris	stmia	r0, {r4-r14}
2091.1Schris	mov	r0, #0x00000000
2101.1Schris	mov	pc, lr
2111.1Schris
2121.1SchrisENTRY(longjmp)
2131.1Schris	ldmia	r0, {r4-r14}
2141.1Schris	mov	r0, #0x00000001
2151.1Schris	mov	pc, lr
2161.1Schris
2171.1Schris	.data
2181.1Schris	.global _C_LABEL(esym)
2191.1Schris_C_LABEL(esym):	.word	_C_LABEL(end)
2201.1Schris
2211.1SchrisENTRY_NP(abort)
2221.1Schris	b	_C_LABEL(abort)
2231.1Schris
2241.1Schris
2251.1Schris/*
2261.1Schris * Atomic bit set and clear functions
2271.1Schris */
2281.1Schris
2291.1SchrisENTRY(atomic_set_bit)
2301.1Schris	mrs	r2, cpsr_all
2311.1Schris	orr	r3, r2, #(I32_bit)
2321.1Schris	msr	cpsr_all, r3
2331.1Schris
2341.1Schris	ldr	r3, [r0]
2351.1Schris	orr	r3, r3, r1
2361.1Schris	str	r3, [r0]
2371.1Schris
2381.1Schris	msr	cpsr_all, r2
2391.1Schris	mov	pc, lr
2401.1Schris
2411.1Schris
2421.1SchrisENTRY(atomic_clear_bit)
2431.1Schris	mrs	r2, cpsr_all
2441.1Schris	orr	r3, r2, #(I32_bit)
2451.1Schris	msr	cpsr_all, r3
2461.1Schris
2471.1Schris	ldr	r3, [r0]
2481.1Schris	bic	r3, r3, r1
2491.1Schris	str	r3, [r0]
2501.1Schris
2511.1Schris	msr	cpsr_all, r2
2521.1Schris	mov	pc, lr
2531.1Schris
2541.1Schris/* End of locore.S */
255