177957Sbenno/* $FreeBSD$ */
277957Sbenno/* $NetBSD: locore.S,v 1.24 2000/05/31 05:09:17 thorpej Exp $ */
377957Sbenno
4139825Simp/*-
577957Sbenno * Copyright (C) 2001 Benno Rice
677957Sbenno * All rights reserved.
777957Sbenno *
877957Sbenno * Redistribution and use in source and binary forms, with or without
977957Sbenno * modification, are permitted provided that the following conditions
1077957Sbenno * are met:
1177957Sbenno * 1. Redistributions of source code must retain the above copyright
1277957Sbenno *    notice, this list of conditions and the following disclaimer.
1377957Sbenno * 2. Redistributions in binary form must reproduce the above copyright
1477957Sbenno *    notice, this list of conditions and the following disclaimer in the
1577957Sbenno *    documentation and/or other materials provided with the distribution.
1677957Sbenno *
1777957Sbenno * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
1877957Sbenno * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1977957Sbenno * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2077957Sbenno * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2177957Sbenno * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2277957Sbenno * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2377957Sbenno * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2477957Sbenno * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2577957Sbenno * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2677957Sbenno * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2777957Sbenno */
28139825Simp/*-
2977957Sbenno * Copyright (C) 1995, 1996 Wolfgang Solfrank.
3077957Sbenno * Copyright (C) 1995, 1996 TooLs GmbH.
3177957Sbenno * All rights reserved.
3277957Sbenno *
3377957Sbenno * Redistribution and use in source and binary forms, with or without
3477957Sbenno * modification, are permitted provided that the following conditions
3577957Sbenno * are met:
3677957Sbenno * 1. Redistributions of source code must retain the above copyright
3777957Sbenno *    notice, this list of conditions and the following disclaimer.
3877957Sbenno * 2. Redistributions in binary form must reproduce the above copyright
3977957Sbenno *    notice, this list of conditions and the following disclaimer in the
4077957Sbenno *    documentation and/or other materials provided with the distribution.
4177957Sbenno * 3. All advertising materials mentioning features or use of this software
4277957Sbenno *    must display the following acknowledgement:
4377957Sbenno *	This product includes software developed by TooLs GmbH.
4477957Sbenno * 4. The name of TooLs GmbH may not be used to endorse or promote products
4577957Sbenno *    derived from this software without specific prior written permission.
4677957Sbenno *
4777957Sbenno * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
4877957Sbenno * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
4977957Sbenno * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
5077957Sbenno * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
5177957Sbenno * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5277957Sbenno * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
5377957Sbenno * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
5477957Sbenno * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
5577957Sbenno * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
5677957Sbenno * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5777957Sbenno */
5877957Sbenno
5977957Sbenno#include "assym.s"
6077957Sbenno
6177957Sbenno#include <sys/syscall.h>
6277957Sbenno
6377957Sbenno#include <machine/trap.h>
6477957Sbenno#include <machine/param.h>
6595719Sbenno#include <machine/spr.h>
6677957Sbenno#include <machine/asm.h>
67243370Sadrian#include "opt_platform.h"
6877957Sbenno
69242723Sjhibbits#include "opt_kdtrace.h"
70242723Sjhibbits
71125438Sgrehan/* Locate the per-CPU data structure */
72125438Sgrehan#define GET_CPUINFO(r)  \
73125438Sgrehan        mfsprg0  r
7477957Sbenno
7577957Sbenno/*
76176617Smarcel * Compiled KERNBASE location and the kernel load address
77176617Smarcel */
78176617Smarcel        .globl  kernbase
79176617Smarcel        .set    kernbase, KERNBASE
80176617Smarcel
81178628Smarcel#define	TMPSTKSZ	8192		/* 8K temporary stack */
82178628Smarcel
83176617Smarcel/*
8477957Sbenno * Globals
8577957Sbenno */
8677957Sbenno	.data
87178621Smarcel	.align	4
8877957SbennoGLOBAL(tmpstk)
89178628Smarcel	.space	TMPSTKSZ
9077957SbennoGLOBAL(esym)
9177957Sbenno	.long	0			/* end of symbol table */
9277957Sbenno
93125708Sgrehan#define	INTRCNT_COUNT	256		/* max(HROWPIC_IRQMAX,OPENPIC_IRQMAX) */
94103599SgrehanGLOBAL(intrnames)
95125708Sgrehan	.space	INTRCNT_COUNT * (MAXCOMLEN + 1) * 2
96224187SattilioGLOBAL(sintrnames)
97224400Sandreast	.long	INTRCNT_COUNT * (MAXCOMLEN + 1) * 2
98224187Sattilio
99103599Sgrehan	.align 4
100103599SgrehanGLOBAL(intrcnt)
101125708Sgrehan	.space	INTRCNT_COUNT * 4 * 2
102224187SattilioGLOBAL(sintrcnt)
103224400Sandreast	.long	INTRCNT_COUNT * 4 * 2
104125438Sgrehan
105184319Smarcel	.text
106184319Smarcel	.globl	btext
107184319Smarcelbtext:
108184319Smarcel
10977957Sbenno/*
11077957Sbenno * This symbol is here for the benefit of kvm_mkdb, and is supposed to
11177957Sbenno * mark the start of kernel text.
11277957Sbenno */
11377957Sbenno	.globl	kernel_text
11477957Sbennokernel_text:
11577957Sbenno
11677957Sbenno/*
11777957Sbenno * Startup entry.  Note, this must be the first thing in the text
11877957Sbenno * segment!
11977957Sbenno */
12077957Sbenno	.text
12178880Sbenno	.globl	__start
12278880Sbenno__start:
12377957Sbenno	li	8,0
12477957Sbenno	li	9,0x100
12577957Sbenno	mtctr	9
12677957Sbenno1:
12777957Sbenno	dcbf	0,8
12877957Sbenno	icbi	0,8
12977957Sbenno	addi	8,8,0x20
13077957Sbenno	bdnz	1b
13177957Sbenno	sync
13277957Sbenno	isync
133243370Sadrian#ifdef WII
134243370Sadrian#include <powerpc/wii/locore.S>
135243370Sadrian#endif
136228605Snwhitehorn	/* Zero bss, in case we were started by something unhelpful */
137228605Snwhitehorn	li	0,0
138228605Snwhitehorn	lis	8,_edata@ha
139228605Snwhitehorn	addi	8,8,_edata@l
140228605Snwhitehorn	lis	9,_end@ha
141228605Snwhitehorn	addi	9,9,_end@l
142228605Snwhitehorn2:	stw     0,0(8)
143228605Snwhitehorn	addi	8,8,4
144228605Snwhitehorn	cmplw	8,9
145228605Snwhitehorn	blt	2b
146228605Snwhitehorn
14799729Sbenno	/* Save the argument pointer and length */
14899729Sbenno	mr	20,6
14999729Sbenno	mr	21,7
15099729Sbenno
15177957Sbenno	lis	8,openfirmware_entry@ha
15277957Sbenno	stw	5,openfirmware_entry@l(8) /* save client interface handler */
15377957Sbenno
154178628Smarcel	lis	1,(tmpstk+TMPSTKSZ-16)@ha
155178628Smarcel	addi	1,1,(tmpstk+TMPSTKSZ-16)@l
15677957Sbenno
15777957Sbenno	mfmsr	0
15877957Sbenno	lis	9,ofmsr@ha
159151891Sgrehan	stwu	0,ofmsr@l(9)
16077957Sbenno
161151891Sgrehan	mfsprg0	0			/* save SPRG0-3 */
162151891Sgrehan	stw	0,4(9)			/* ofmsr[1] = sprg0 */
163151891Sgrehan	mfsprg1 0
164151891Sgrehan	stw	0,8(9)			/* ofmsr[2] = sprg1 */
165151891Sgrehan	mfsprg2 0
166151891Sgrehan	stw	0,12(9)			/* ofmsr[3] = sprg2 */
167151891Sgrehan	mfsprg3 0
168151891Sgrehan	stw	0,16(9)			/* ofmsr[4] = sprg3 */
169151891Sgrehan
170186347Snwhitehorn	bl	OF_initial_setup
17177957Sbenno
172237737Srpaulo	lis	3,kernel_text@ha
173237737Srpaulo	addi	3,3,kernel_text@l
174237737Srpaulo
17577957Sbenno	lis	4,end@ha
17677957Sbenno	addi	4,4,end@l
177237737Srpaulo	add	4,4,3
17877957Sbenno	mr	5,4
17977957Sbenno
18099729Sbenno	/* Restore the argument pointer and length */
18199729Sbenno	mr	6,20
18299729Sbenno	mr	7,21
18399729Sbenno
18477957Sbenno	bl	powerpc_init
185178261Smarcel	mr	%r1, %r3
186178261Smarcel	li	%r3, 0
187178261Smarcel	stw	%r3, 0(%r1)
18877957Sbenno	bl	mi_startup
18977957Sbenno	b	OF_exit
19077957Sbenno
19177957Sbenno/*
19277957Sbenno * int setfault()
19377957Sbenno *
19477957Sbenno * Similar to setjmp to setup for handling faults on accesses to user memory.
19577957Sbenno * Any routine using this may only call bcopy, either the form below,
19677957Sbenno * or the (currently used) C code optimized, so it doesn't use any non-volatile
19777957Sbenno * registers.
19877957Sbenno */
19977957Sbenno	.globl	setfault
20077957Sbennosetfault:
20177957Sbenno	mflr	0
20277957Sbenno	mfcr	12
20377957Sbenno	mfsprg	4,0
204223485Snwhitehorn	lwz	4,TD_PCB(2)	/* curthread = r2 */
20583682Smp	stw	3,PCB_ONFAULT(4)
20677957Sbenno	stw	0,0(3)
20777957Sbenno	stw	1,4(3)
20877957Sbenno	stw	2,8(3)
20977957Sbenno	stmw	12,12(3)
21077957Sbenno	xor	3,3,3
21177957Sbenno	blr
21277957Sbenno
213209975Snwhitehorn#include <powerpc/aim/trap_subr32.S>
214