1 /* $NetBSD: locore2.c,v 1.37 2026/11/14 02:43:48 thorpej 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 Adam Glass, Gordon W. Ross, and Matthew Fredette. 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.37 2026/11/14 02:43:48 thorpej Exp $"); 34 35 #include "opt_ddb.h" 36 #include "opt_modular.h" 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/proc.h> 41 #include <sys/reboot.h> 42 #define ELFSIZE 32 43 #include <sys/exec_elf.h> 44 45 #include <uvm/uvm_extern.h> 46 47 #include <dev/cons.h> 48 49 #include <machine/cpu.h> 50 #include <machine/db_machdep.h> 51 #include <machine/dvma.h> 52 #include <machine/fcode.h> 53 #include <machine/idprom.h> 54 #include <machine/leds.h> 55 #include <machine/promlib.h> 56 #include <machine/pmap.h> 57 #include <machine/pte.h> 58 #include <machine/vectors.h> 59 60 #include <sun2/sun2/control.h> 61 #include <sun2/sun2/machdep.h> 62 63 #include "ksyms.h" 64 65 /* This is defined in locore.s */ 66 extern char kernel_text[]; 67 68 /* These are defined by the linker */ 69 extern char etext[], edata[], end[]; 70 int nsym; 71 char *ssym, *esym; 72 73 /* 74 * XXX: m68k common code needs these... 75 * ... but this port does not need to deal with anything except 76 * an mc68010, so these two variables are always ignored. 77 */ 78 int cputype = CPU_68010; 79 int mmutype = MMU_SUN; 80 81 /* 82 * Now our own stuff. 83 */ 84 85 u_char cpu_machine_id = 0; 86 const char *cpu_string = NULL; 87 int cpu_has_multibus = 0; 88 int cpu_has_vme = 0; 89 90 /* XXX This is probably overkill. */ 91 int delay_divisor = delay_divisor_est(25); 92 93 /* First C code called by locore.s */ 94 void *_bootstrap(void); 95 96 #if NKSYMS || defined(DDB) || defined(MODULAR) 97 /* 98 * Preserve symbols and strings by setting esym. 99 */ 100 static void __attribute__((no_instrument_function)) 101 _save_symtab(void) 102 { 103 int i; 104 Elf_Ehdr *ehdr; 105 Elf_Shdr *shp; 106 vaddr_t minsym, maxsym; 107 108 /* 109 * Check the ELF headers. 110 */ 111 112 ehdr = (void *)end; 113 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0 || 114 ehdr->e_ident[EI_CLASS] != ELFCLASS32) { 115 prom_printf("_save_symtab: bad ELF magic\n"); 116 return; 117 } 118 119 /* 120 * Find the end of the symbols and strings. 121 */ 122 123 maxsym = 0; 124 minsym = ~maxsym; 125 shp = (Elf_Shdr *)(end + ehdr->e_shoff); 126 for (i = 0; i < ehdr->e_shnum; i++) { 127 if (shp[i].sh_type != SHT_SYMTAB && 128 shp[i].sh_type != SHT_STRTAB) { 129 continue; 130 } 131 minsym = uimin(minsym, (vaddr_t)end + shp[i].sh_offset); 132 maxsym = uimax(maxsym, (vaddr_t)end + shp[i].sh_offset + 133 shp[i].sh_size); 134 } 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-system 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 * 151 * Returns the new kernel %sp value for lwp0. 152 */ 153 static void * __attribute__((no_instrument_function)) 154 _vm_init(void) 155 { 156 vaddr_t nextva; 157 paddr_t nextpa; 158 159 /* 160 * First preserve our symbol table, which might have been 161 * loaded after our BSS area by the boot loader. However, 162 * if DDB is not part of this kernel, ignore the symbols. 163 */ 164 esym = end + 4; 165 #if NKSYMS || defined(DDB) || defined(MODULAR) 166 /* This will advance esym past the symbols. */ 167 _save_symtab(); 168 #endif 169 170 /* 171 * Steal some special-purpose, already mapped pages. 172 * Note: msgbuf is setup in machdep.c:cpu_startup() 173 */ 174 nextva = m68k_round_page(esym); 175 176 /* 177 * Setup the u-area pages (stack, etc.) for lwp0. 178 * This is done very early (here) to make sure the 179 * fault handler works in case we hit an early bug. 180 * (The fault handler may reference lwp0 stuff.) 181 */ 182 uvm_lwp_setuarea(&lwp0, nextva); 183 memset((void *)nextva, 0, USPACE); 184 185 /* Create a fake exception frame so that cpu_lwp_fork() can copy it. */ 186 struct trapframe *tf = (struct trapframe *)(nextva + USPACE) - 1; 187 tf->tf_sr = PSL_USER; 188 lwp0.l_md.md_regs = (int *)tf; 189 190 nextva += USPACE; 191 192 /* This does most of the real work. */ 193 nextpa = pmap_bootstrap(nextva); 194 195 machine_init_common(nextpa); 196 197 return tf; 198 } 199 200 /* 201 * Determine which Sun2 model we are running on. 202 * 203 * XXX: Just save idprom.idp_machtype here, and 204 * XXX: move the rest of this to identifycpu(). 205 * XXX: Move cache_size stuff to cache.c. 206 */ 207 static void __attribute__((no_instrument_function)) 208 _verify_hardware(void) 209 { 210 unsigned char machtype; 211 int cpu_match = 0; 212 213 machtype = identity_prom.idp_machtype; 214 if ((machtype & IDM_ARCH_MASK) != IDM_ARCH_SUN2) { 215 prom_printf("Bad IDPROM arch!\n"); 216 prom_abort(); 217 } 218 219 cpu_machine_id = machtype; 220 switch (cpu_machine_id) { 221 222 case ID_SUN2_120 : 223 cpu_match++; 224 cpu_string = "{120,170}"; 225 cpuspeed_khz = 10000; 226 delay_divisor = delay_divisor_est(10); 227 cpu_has_multibus = true; 228 break; 229 230 case ID_SUN2_50 : 231 cpu_match++; 232 cpu_string = "50"; 233 cpuspeed_khz = 10000; 234 delay_divisor = delay_divisor_est(10); 235 cpu_has_vme = true; 236 break; 237 238 default: 239 prom_printf("unknown sun2 model\n"); 240 prom_abort(); 241 } 242 if (!cpu_match) { 243 prom_printf("kernel not configured for the Sun 2 model\n"); 244 prom_abort(); 245 } 246 } 247 248 /* 249 * This is called from locore.s just after the kernel is remapped 250 * to its proper address, but before the call to main(). The work 251 * done here corresponds to various things done in locore.s on the 252 * hp300 port (and other m68k) but which we prefer to do in C code. 253 * Also do setup specific to the Sun PROM monitor and IDPROM here. 254 */ 255 void * __attribute__((no_instrument_function)) 256 _bootstrap(void) 257 { 258 extern struct consdev consdev_prom; /* XXX */ 259 vaddr_t va; 260 261 /* First, Clear BSS. */ 262 memset(edata, 0, end - edata); 263 264 /* Initialize the PROM. */ 265 prom_init(); 266 267 /* 268 * Initialize console to point to the PROM (output only) table 269 * for early printf calls. 270 */ 271 cn_tab = &consdev_prom; 272 273 /* Copy the IDPROM from control space. */ 274 idprom_init(); 275 276 /* Validate the Sun2 model (from IDPROM). */ 277 _verify_hardware(); 278 279 /* Handle kernel mapping, pmap_bootstrap(), etc. */ 280 void *ksp = _vm_init(); 281 282 /* 283 * Point interrupts/exceptions to our vector table. 284 * (Until now, we use the one setup by the PROM.) 285 */ 286 vec_init(); 287 /* Interrupts are enabled later, after autoconfig. */ 288 289 /* 290 * Now unmap the PROM's physical/virtual pages zero through three. 291 */ 292 for(va = 0; va < PAGE_SIZE * 4; va += PAGE_SIZE) 293 set_pte(va, PG_INVAL); 294 295 /* 296 * Turn on the LEDs so we know power is on. 297 * Needs idprom_init and obio_init earlier. 298 */ 299 leds_init(); 300 301 /* 302 * Initialize the source/destination control registers for 303 * movs. 304 */ 305 setsfc(FC_USERD); 306 setdfc(FC_USERD); 307 308 return ksp; 309 } 310