locore2.c revision 1.19
1/*	$NetBSD: locore2.c,v 1.19 1998/02/05 04:57:58 gwr Exp $	*/
2
3/*-
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Gordon W. Ross and Jeremy Cooper.
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#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/proc.h>
42#include <sys/reboot.h>
43#include <sys/user.h>
44#include <sys/exec_aout.h>
45
46#include <vm/vm.h>
47
48#include <machine/cpu.h>
49#include <machine/db_machdep.h>
50#include <machine/dvma.h>
51#include <machine/idprom.h>
52#include <machine/leds.h>
53#include <machine/mon.h>
54#include <machine/pmap.h>
55#include <machine/pte.h>
56
57#include <sun3/sun3/interreg.h>
58#include <sun3/sun3/machdep.h>
59#include <sun3/sun3/vector.h>
60
61/* This is defined in locore.s */
62extern char kernel_text[];
63
64/* These are defined by the linker */
65extern char etext[], edata[], end[];
66char *esym;	/* DDB */
67
68/*
69 * XXX: m68k common code needs these...
70 * ... but this port does not need to deal with anything except
71 * an mc68030, so these two variables are always ignored.
72 * XXX: Need to do something about <m68k/include/cpu.h>
73 */
74int cputype =  1;	/* CPU_68030 */
75int mmutype = -1;	/* MMU_68030 */
76
77/*
78 * Now our own stuff.
79 */
80
81struct user *proc0paddr;	/* proc[0] pcb address (u-area VA) */
82extern struct pcb *curpcb;
83
84/* First C code called by locore.s */
85void _bootstrap __P((struct exec));
86
87static void _vm_init __P((struct exec *kehp));
88
89
90#if defined(DDB) && !defined(SYMTAB_SPACE)
91static void _save_symtab __P((struct exec *kehp));
92
93/*
94 * Preserve DDB symbols and strings by setting esym.
95 */
96static void
97_save_symtab(kehp)
98	struct exec *kehp;	/* kernel exec header */
99{
100	int x, *symsz, *strsz;
101	char *endp;
102	char *errdesc = "?";
103
104	/*
105	 * First, sanity-check the exec header.
106	 */
107	if ((kehp->a_midmag & 0xFFF0) != 0x0100) {
108		errdesc = "magic";
109		goto err;
110	}
111	/* Boundary between text and data varries a little. */
112	x = kehp->a_text + kehp->a_data;
113	if (x != (edata - kernel_text)) {
114		errdesc = "a_text+a_data";
115		goto err;
116	}
117	if (kehp->a_bss != (end - edata)) {
118		errdesc = "a_bss";
119		goto err;
120	}
121	if (kehp->a_entry != (int)kernel_text) {
122		errdesc = "a_entry";
123		goto err;
124	}
125	if (kehp->a_trsize || kehp->a_drsize) {
126		errdesc = "a_Xrsize";
127		goto err;
128	}
129	/* The exec header looks OK... */
130
131	/* Check the symtab length word. */
132	endp = end;
133	symsz = (int*)endp;
134	if (kehp->a_syms != *symsz) {
135		errdesc = "a_syms";
136		goto err;
137	}
138	endp += sizeof(int);	/* past length word */
139	endp += *symsz;			/* past nlist array */
140
141	/* Sanity-check the string table length. */
142	strsz = (int*)endp;
143	if ((*strsz < 4) || (*strsz > 0x80000)) {
144		errdesc = "strsize";
145		goto err;
146	}
147
148	/* Success!  We have a valid symbol table! */
149	endp += *strsz;			/* past strings */
150	esym = endp;
151	return;
152
153 err:
154	mon_printf("_save_symtab: bad %s\n", errdesc);
155}
156#endif	/* DDB && !SYMTAB_SPACE */
157
158/*
159 * This function is called from _bootstrap() to initialize
160 * pre-vm-sytem virtual memory.  All this really does is to
161 * set virtual_avail to the first page following preloaded
162 * data (i.e. the kernel and its symbol table) and special
163 * things that may be needed very early (proc0 upages).
164 * Once that is done, pmap_bootstrap() is called to do the
165 * usual preparations for our use of the MMU.
166 */
167static void
168_vm_init(kehp)
169	struct exec *kehp;	/* kernel exec header */
170{
171	vm_offset_t nextva;
172
173	/*
174	 * First preserve our symbol table, which might have been
175	 * loaded after our BSS area by the boot loader.  However,
176	 * if DDB is not part of this kernel, ignore the symbols.
177	 */
178	esym = end;
179#if defined(DDB) && !defined(SYMTAB_SPACE)
180	/* This will advance esym past the symbols. */
181	_save_symtab(kehp);
182#endif
183
184	/*
185	 * Steal some special-purpose, already mapped pages.
186	 * Note: msgbuf is setup in machdep.c:cpu_startup()
187	 */
188	nextva = m68k_round_page(esym);
189
190	/*
191	 * Setup the u-area pages (stack, etc.) for proc0.
192	 * This is done very early (here) to make sure the
193	 * fault handler works in case we hit an early bug.
194	 * (The fault handler may reference proc0 stuff.)
195	 */
196	proc0paddr = (struct user *) nextva;
197	nextva += USPACE;
198	bzero((caddr_t)proc0paddr, USPACE);
199	proc0.p_addr = proc0paddr;
200
201	/*
202	 * Now that proc0 exists, make it the "current" one.
203	 */
204	curproc = &proc0;
205	curpcb = &proc0paddr->u_pcb;
206
207	/*
208	 * Call pmap_bootstrap() so that we may begin using
209	 * pmap_enter_kernel() and pmap_bootstrap_alloc().
210	 */
211	pmap_bootstrap(nextva);
212}
213
214/*
215 * This is called from locore.s just after the kernel is remapped
216 * to its proper address, but before the call to main().  The work
217 * done here corresponds to various things done in locore.s on the
218 * hp300 port (and other m68k) but which we prefer to do in C code.
219 * Also do setup specific to the Sun PROM monitor and IDPROM here.
220 */
221void
222_bootstrap(keh)
223	struct exec keh;	/* kernel exec header */
224{
225
226	/* First, Clear BSS. */
227	bzero(edata, end - edata);
228
229	/* Set v_handler, get boothowto. */
230	sunmon_init();
231
232	/* Handle kernel mapping, pmap_bootstrap(), etc. */
233	_vm_init(&keh);
234
235	/*
236	 * Find and save OBIO mappings needed early,
237	 * and call some init functions.
238	 */
239	obio_init();
240
241	/*
242	 * Point interrupts/exceptions to our vector table.
243	 * (Until now, we use the one setup by the PROM.)
244	 *
245	 * This is done after obio_init() / intreg_init() finds
246	 * the interrupt register and disables the NMI clock so
247	 * it will not cause "spurrious level 7" complaints.
248	 * Done after _vm_init so the PROM can debug that.
249	 */
250	setvbr((void **)vector_table);
251	/* Interrupts are enabled later, after autoconfig. */
252
253	/*
254	 * Find the IDPROM and copy it to memory.
255	 * Needs obio_init and setvbr earlier.
256	 */
257	idprom_init();
258
259	/*
260	 * Turn on the LEDs so we know power is on.
261	 * Needs idprom_init and obio_init earlier.
262	 */
263	leds_init();
264}
265