locore2.c revision 1.30
1/*	$NetBSD: locore2.c,v 1.30 2005/01/22 15:36:11 chs 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/cdefs.h>
40__KERNEL_RCSID(0, "$NetBSD: locore2.c,v 1.30 2005/01/22 15:36:11 chs Exp $");
41
42#include "opt_ddb.h"
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/proc.h>
47#include <sys/reboot.h>
48#include <sys/user.h>
49#define ELFSIZE 32
50#include <sys/exec_elf.h>
51
52#include <uvm/uvm_extern.h>
53
54#include <machine/cpu.h>
55#include <machine/db_machdep.h>
56#include <machine/dvma.h>
57#include <machine/idprom.h>
58#include <machine/leds.h>
59#include <machine/mon.h>
60#include <machine/pmap.h>
61#include <machine/pte.h>
62
63#include <sun3/sun3/interreg.h>
64#include <sun3/sun3/machdep.h>
65#include <sun3/sun3/vector.h>
66
67/* This is defined in locore.s */
68extern char kernel_text[];
69
70/* These are defined by the linker */
71extern char etext[], edata[], end[];
72int nsym;
73char *ssym, *esym;
74
75/*
76 * XXX: m68k common code needs these...
77 * ... but this port does not need to deal with anything except
78 * an mc68030, so these two variables are always ignored.
79 */
80int cputype = CPU_68030;
81int mmutype = MMU_68030;
82
83/*
84 * Now our own stuff.
85 */
86
87struct user *proc0paddr;	/* proc[0] pcb address (u-area VA) */
88extern struct pcb *curpcb;
89
90/* First C code called by locore.s */
91void _bootstrap(void);
92
93static void _vm_init(void);
94
95#if defined(DDB)
96static void _save_symtab(void);
97
98/*
99 * Preserve DDB symbols and strings by setting esym.
100 */
101static void
102_save_symtab(void)
103{
104	int i;
105	Elf_Ehdr *ehdr;
106	Elf_Shdr *shp;
107	vaddr_t minsym, maxsym;
108
109	/*
110	 * Check the ELF headers.
111	 */
112
113	ehdr = (void *)end;
114	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0 ||
115	    ehdr->e_ident[EI_CLASS] != ELFCLASS32) {
116		mon_printf("_save_symtab: bad ELF magic\n");
117		return;
118	}
119
120	/*
121	 * Find the end of the symbols and strings.
122	 */
123
124	maxsym = 0;
125	minsym = ~maxsym;
126	shp = (Elf_Shdr *)(end + ehdr->e_shoff);
127	for (i = 0; i < ehdr->e_shnum; i++) {
128		if (shp[i].sh_type != SHT_SYMTAB &&
129		    shp[i].sh_type != SHT_STRTAB) {
130			continue;
131		}
132		minsym = min(minsym, (vaddr_t)end + shp[i].sh_offset);
133		maxsym = max(maxsym, (vaddr_t)end + shp[i].sh_offset +
134			     shp[i].sh_size);
135	}
136	nsym = 1;
137	ssym = (char *)ehdr;
138	esym = (char *)maxsym;
139}
140#endif	/* DDB */
141
142/*
143 * This function is called from _bootstrap() to initialize
144 * pre-vm-sytem virtual memory.  All this really does is to
145 * set virtual_avail to the first page following preloaded
146 * data (i.e. the kernel and its symbol table) and special
147 * things that may be needed very early (lwp0 upages).
148 * Once that is done, pmap_bootstrap() is called to do the
149 * usual preparations for our use of the MMU.
150 */
151static void
152_vm_init(void)
153{
154	vaddr_t nextva;
155
156	/*
157	 * First preserve our symbol table, which might have been
158	 * loaded after our BSS area by the boot loader.  However,
159	 * if DDB is not part of this kernel, ignore the symbols.
160	 */
161	esym = end + 4;
162#if defined(DDB)
163	/* This will advance esym past the symbols. */
164	_save_symtab();
165#endif
166
167	/*
168	 * Steal some special-purpose, already mapped pages.
169	 * Note: msgbuf is setup in machdep.c:cpu_startup()
170	 */
171	nextva = m68k_round_page(esym);
172
173	/*
174	 * Setup the u-area pages (stack, etc.) for lwp0.
175	 * This is done very early (here) to make sure the
176	 * fault handler works in case we hit an early bug.
177	 * (The fault handler may reference lwp0 stuff.)
178	 */
179	proc0paddr = (struct user *) nextva;
180	nextva += USPACE;
181	memset((caddr_t)proc0paddr, 0, USPACE);
182	lwp0.l_addr = proc0paddr;
183
184	/*
185	 * Now that lwp0 exists, make it the "current" one.
186	 */
187	curlwp = &lwp0;
188	curpcb = &proc0paddr->u_pcb;
189
190	/* This does most of the real work. */
191	pmap_bootstrap(nextva);
192}
193
194/*
195 * This is called from locore.s just after the kernel is remapped
196 * to its proper address, but before the call to main().  The work
197 * done here corresponds to various things done in locore.s on the
198 * hp300 port (and other m68k) but which we prefer to do in C code.
199 * Also do setup specific to the Sun PROM monitor and IDPROM here.
200 */
201void
202_bootstrap(void)
203{
204
205	/* First, Clear BSS. */
206	memset(edata, 0, end - edata);
207
208	/* Set v_handler, get boothowto. */
209	sunmon_init();
210
211	/* Handle kernel mapping, pmap_bootstrap(), etc. */
212	_vm_init();
213
214	/*
215	 * Find and save OBIO mappings needed early,
216	 * and call some init functions.
217	 */
218	obio_init();
219
220	/*
221	 * Point interrupts/exceptions to our vector table.
222	 * (Until now, we use the one setup by the PROM.)
223	 *
224	 * This is done after obio_init() / intreg_init() finds
225	 * the interrupt register and disables the NMI clock so
226	 * it will not cause "spurrious level 7" complaints.
227	 * Done after _vm_init so the PROM can debug that.
228	 */
229	setvbr((void **)vector_table);
230	/* Interrupts are enabled later, after autoconfig. */
231
232	/*
233	 * Find the IDPROM and copy it to memory.
234	 * Needs obio_init and setvbr earlier.
235	 */
236	idprom_init();
237
238	/*
239	 * Turn on the LEDs so we know power is on.
240	 * Needs idprom_init and obio_init earlier.
241	 */
242	leds_init();
243}
244