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