srt0.S revision 1.9
1/*	$NetBSD: srt0.S,v 1.9 2004/03/23 15:36:08 pk Exp $	*/
2
3/*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *        This product includes software developed by the NetBSD
21 *        Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 *    contributors may be used to endorse or promote products derived
24 *    from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#define _LOCORE		/* XXX - fix Makefile */
40
41#include <machine/param.h>
42#include <machine/asm.h>
43#include <machine/psl.h>
44
45#define	CCFSZ	96
46
47	.file	"str0.s"
48
49	.comm	_C_LABEL(romp), 4
50	.comm	_C_LABEL(cputyp), 4
51	.comm	_C_LABEL(nbpg), 4
52	.comm	_C_LABEL(pgofset), 4
53	.comm	_C_LABEL(pgshift), 4
54
55	.text
56	.globl	start
57
58start:
59	/*
60	 * Set up a stack.
61	 */
62	set	start, %o1
63	save	%o1, -CCFSZ, %sp
64
65	/*
66	 * Find which address we are at.
67	 */
681:	call	2f
69	 sethi	%hi(1b), %l0
702:	or	%l0, %lo(1b), %l0
71	cmp	%l0, %o7	! %o7 contains actual address of 1b
72	beq	4f		! already there, no need to relocate
73	 nop
74
75	/*
76	 * Relocate.
77	 */
78	add	%o7, (start-1b), %l0
79	set	start, %l1
80	set	_edata, %o0
81	sub	%o0, %l1, %l2		! length
823:	ld	[%l0], %o0
83	add	%l0, 4, %l0
84	st	%o0, [%l1]
85	subcc	%l2, 4, %l2
86	bg	3b
87	 add	%l1, 4, %l1
88
89	/*
90	 * Jump to our relocated self.
91	 */
92	set	4f, %g1
93	jmp	%g1
94	 nop
95
964:
97#ifdef notyet
98	/*
99	 * Enable traps
100	 */
101	wr	%g0, 0, %wim		! make sure we can set psr
102	nop; nop; nop
103	wr	%g0, PSR_S|PSR_PS|PSR_PIL, %psr	! set initial psr
104	nop; nop; nop
105	wr	%g0, 2, %wim		! set initial %wim (w1 invalid)
106
107	rd	%psr, %l0
108	wr	%l0, PSR_ET, %psr
109	nop; nop; nop
110#endif
111
112	/*
113	 * Clear BSS
114	 */
115	set     _edata, %o0             ! bzero(edata, end - edata)
116	set     _end, %o1
117	/* note: bzero() expanded inline for compactness */
118	subcc	%o1, %o0, %o1
119	bz	2f			! in case there is no BSS
120
121	srl	%o1, 2, %o1		! assume _edata & _end are aligned
1221:
123	st	%g0, [%o0]		! while (n--)
124	subcc	%o1, 1, %o1		!	*p = 0; etc..
125	bnz	1b
126	 add	%o0, 4, %o0
1272:
128
129	/*
130	 * Enable interrupts, but only above level 11. This enables "L1-A",
131	 * but avoids spurious interrupt bites from most other devices.
132	 */
133	rd	%psr, %o0
134	andn	%o0, PSR_PIL, %o0
135	wr	%o0, 0xb00, %psr	! (11 << 8)
136	nop; nop; nop
137
138	/*
139	 * Set CPU type that we are running on.
140	 */
141	sethi	%hi(_C_LABEL(cputyp)), %o0
142	set	0x4000, %g7
143	cmp	%i0, %g7
144	beq	is_sun4
145	 nop
146
147	mov	CPU_SUN4C, %g4
148	mov	SUN4CM_PGSHIFT, %g5
149
150	/*
151	 * OpenProm machines pass PROM vector in %o0 (%i0 after save)
152	 * OpenFirm machines pass OF entry in %o3 (%i3 after save)
153	 */
154	cmp	%i0, 0
155	be	is_openfirm
156	 nop
157
158	! save address of PROM vector
159	sethi	%hi(_C_LABEL(romp)), %o1
160	st	%i0, [%o1 + %lo(_C_LABEL(romp))]
161	b,a	is_sun4cm
162
163is_openfirm:
164	! only javastations have OFW, so we know it is a sun4m
165	mov	CPU_SUN4M, %g4
166	! save address of OpenFirmware client interface handler
167	sethi	%hi(_C_LABEL(romp)), %o1
168	st	%i3, [%o1 + %lo(_C_LABEL(romp))]
169	b,a	is_sun4cm
170
171is_sun4:
172	mov	CPU_SUN4, %g4
173	mov	SUN4_PGSHIFT, %g5
174
175is_sun4cm:
176	st	%g4, [%o0 + %lo(_C_LABEL(cputyp))]
177	sethi	%hi(_C_LABEL(pgshift)), %o0	! pgshift = log2(nbpg)
178	st	%g5, [%o0 + %lo(_C_LABEL(pgshift))]
179
180	mov	1, %o0				! nbpg = 1 << pgshift
181	sll	%o0, %g5, %g5
182	sethi	%hi(_C_LABEL(nbpg)), %o0	! nbpg = bytes in a page
183	st	%g5, [%o0 + %lo(_C_LABEL(nbpg))]
184
185	sub	%g5, 1, %g5
186	sethi	%hi(_C_LABEL(pgofset)), %o0	! page offset = nbpg - 1
187	st	%g5, [%o0 + %lo(_C_LABEL(pgofset))]
188
189	call	_C_LABEL(main)
190	 mov	%i0, %o0
191
192	ret
193	 restore
194
195/*
196 * NO-OP place holder function.
197 */
198ENTRY(sparc_noop)
199	retl
200	 nop
201
202/*
203 * Openfirmware entry point: openfirmware(void *args)
204 */
205ENTRY(openfirmware)
206	sethi	%hi(_C_LABEL(romp)), %o1
207	ld	[%o1 + %lo(_C_LABEL(romp))], %o2
208	jmp	%o2
209	 nop
210
211#ifdef TIGHT
212
213/*
214 * XXX - Space saving .div & .rem routines (small & non-negative numbres only)
215 */
216	.align	4
217	.global .div, .udiv
218! int n = 0; while (a >= b) { a -= b; n++; }; return n;
219.div:
220.udiv:
221	cmp	%o0, %o1
222	bl	2f
223	 mov	0, %o5
2241:
225	sub	%o0, %o1, %o0
226	cmp	%o0, %o1
227	bge	1b
228	 add	%o5, 1, %o5
2292:
230	retl
231	 mov	%o5, %o0
232
233	.align	4
234	.global .rem, .urem
235! while (a>=b) a -= b; return a;
236.rem:
237.urem:
238	cmp	%o0, %o1
239	bl	2f
240	 nop
241	sub	%o0, %o1, %o0
2421:
243	cmp	%o0, %o1
244	bge,a	1b
245	 sub	%o0, %o1, %o0
2462:
247	retl
248	 nop
249
250#endif /* TIGHT */
251