Home | History | Annotate | Line # | Download | only in sun3
      1 /*	$NetBSD: locore2.c,v 1.112 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 and Gordon W. Ross.
      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.112 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/mon.h>
     56 #include <machine/pmap.h>
     57 #include <machine/pte.h>
     58 #include <machine/vectors.h>
     59 
     60 #include <sun3/sun3/control.h>
     61 #include <sun3/sun3/interreg.h>
     62 #include <sun3/sun3/machdep.h>
     63 #include <sun3/sun3/obmem.h>
     64 
     65 #include "ksyms.h"
     66 
     67 /* This is defined in locore.s */
     68 extern char kernel_text[];
     69 
     70 /* These are defined by the linker */
     71 extern char etext[], edata[], end[];
     72 int nsym;
     73 char *ssym, *esym;
     74 
     75 /* Basically a flag: "Do we have a VAC?" */
     76 int cache_size;
     77 
     78 /*
     79  * XXX: m68k common code needs these...
     80  * ... but this port does not need to deal with anything except
     81  * an mc68020, so these two variables are always ignored.
     82  */
     83 int cputype = CPU_68020;
     84 int mmutype = MMU_SUN;
     85 
     86 /*
     87  * Now our own stuff.
     88  */
     89 
     90 u_char cpu_machine_id = 0;
     91 const char *cpu_string;
     92 int cpu_has_vme = 0;
     93 
     94 int delay_divisor = delay_divisor_est(25); /* assume the fastest (3/260) */
     95 
     96 /* First C code called by locore.s */
     97 void *_bootstrap(void);
     98 
     99 #if NKSYMS || defined(DDB) || defined(MODULAR)
    100 /*
    101  * Preserve DDB symbols and strings by setting esym.
    102  */
    103 static void __attribute__((no_instrument_function))
    104 _save_symtab(void)
    105 {
    106 	int i;
    107 	Elf_Ehdr *ehdr;
    108 	Elf_Shdr *shp;
    109 	vaddr_t minsym, maxsym;
    110 
    111 	/*
    112 	 * Check the ELF headers.
    113 	 */
    114 
    115 	ehdr = (void *)end;
    116 	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0 ||
    117 	    ehdr->e_ident[EI_CLASS] != ELFCLASS32) {
    118 		mon_printf("_save_symtab: bad ELF magic\n");
    119 		return;
    120 	}
    121 
    122 	/*
    123 	 * Find the end of the symbols and strings.
    124 	 */
    125 
    126 	maxsym = 0;
    127 	minsym = ~maxsym;
    128 	shp = (Elf_Shdr *)(end + ehdr->e_shoff);
    129 	for (i = 0; i < ehdr->e_shnum; i++) {
    130 		if (shp[i].sh_type != SHT_SYMTAB &&
    131 		    shp[i].sh_type != SHT_STRTAB) {
    132 			continue;
    133 		}
    134 		minsym = uimin(minsym, (vaddr_t)end + shp[i].sh_offset);
    135 		maxsym = uimax(maxsym, (vaddr_t)end + shp[i].sh_offset +
    136 			     shp[i].sh_size);
    137 	}
    138 
    139 	/*
    140 	 * The Sun3/50 has further restrictions on the
    141 	 * size of the kernel boot image.  If preserving
    142 	 * the symbol table would take us over the limit,
    143 	 * then just ignore the symbols.
    144 	 */
    145 
    146 	if ((cpu_machine_id == ID_SUN3_50) &&
    147 	    ((vaddr_t)maxsym > (KERNBASE3 + OBMEM_BW50_ADDR - USPACE))) {
    148 		mon_printf("_save_symtab: too large for 3/50");
    149 		return;
    150 	}
    151 
    152 	nsym = 1;
    153 	ssym = (char *)ehdr;
    154 	esym = (char *)maxsym;
    155 }
    156 #endif	/* DDB */
    157 
    158 /*
    159  * This function is called from _bootstrap() to initialize
    160  * pre-vm-system 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 (lwp0 upages).
    164  * Once that is done, pmap_bootstrap() is called to do the
    165  * usual preparations for our use of the MMU.
    166  *
    167  * Returns the new kernel %sp value for lwp0.
    168  */
    169 static void * __attribute__((no_instrument_function))
    170 _vm_init(void)
    171 {
    172 	vaddr_t nextva;
    173 	paddr_t nextpa;
    174 
    175 	/*
    176 	 * First preserve our symbol table, which might have been
    177 	 * loaded after our BSS area by the boot loader.  However,
    178 	 * if DDB is not part of this kernel, ignore the symbols.
    179 	 */
    180 	esym = end + 4;
    181 #if NKSYMS || defined(DDB) || defined(MODULAR)
    182 	/* This will advance esym past the symbols. */
    183 	_save_symtab();
    184 #endif
    185 
    186 	/*
    187 	 * Steal some special-purpose, already mapped pages.
    188 	 * Note: msgbuf is setup in machdep.c:cpu_startup()
    189 	 */
    190 	nextva = m68k_round_page(esym);
    191 
    192 	/*
    193 	 * Setup the u-area pages (stack, etc.) for lwp0.
    194 	 * This is done very early (here) to make sure the
    195 	 * fault handler works in case we hit an early bug.
    196 	 * (The fault handler may reference lwp0 stuff.)
    197 	 */
    198 	memset((void *)nextva, 0, USPACE);
    199 	uvm_lwp_setuarea(&lwp0, nextva);
    200 
    201 	/* Create a fake exception frame so that cpu_lwp_fork() can copy it. */
    202 	struct trapframe *tf = (struct trapframe *)(nextva + USPACE) - 1;
    203 	tf->tf_sr = PSL_USER;
    204 	lwp0.l_md.md_regs = (int *)tf;
    205 
    206 	nextva += USPACE;
    207 
    208 	/* This does most of the real work. */
    209 	nextpa = pmap_bootstrap(nextva);
    210 
    211 	machine_init_common(nextpa);
    212 
    213 	return tf;
    214 }
    215 
    216 /*
    217  * Determine which Sun3 model we are running on.
    218  * We have to do this very early on the Sun3 because
    219  * pmap_bootstrap() needs to know if it should avoid
    220  * the video memory on the Sun3/50.
    221  *
    222  * XXX: Just save idprom.idp_machtype here, and
    223  * XXX: move the rest of this to identifycpu().
    224  * XXX: Move cache_size stuff to cache.c.
    225  */
    226 static void __attribute__((no_instrument_function))
    227 _verify_hardware(void)
    228 {
    229 	unsigned char machtype;
    230 	int cpu_match = 0;
    231 
    232 	machtype = identity_prom.idp_machtype;
    233 	if ((machtype & IDM_ARCH_MASK) != IDM_ARCH_SUN3) {
    234 		mon_printf("Bad IDPROM arch!\n");
    235 		sunmon_abort();
    236 	}
    237 
    238 	/*
    239 	 * XXX Should really calibrate the delay_divisor against a
    240 	 * XXX timer, but it's been this way for a long time.
    241 	 */
    242 
    243 	cpu_machine_id = machtype;
    244 	switch (cpu_machine_id) {
    245 
    246 	case ID_SUN3_50 :
    247 		cpu_match++;
    248 		cpu_string = "50";
    249 		cpuspeed_khz = 16000;
    250 		delay_divisor = delay_divisor_est(16);
    251 		break;
    252 
    253 	case ID_SUN3_60 :
    254 		cpu_match++;
    255 		cpu_string = "60";
    256 		cpuspeed_khz = 20000;
    257 		delay_divisor = delay_divisor_est(20);
    258 		break;
    259 
    260 	case ID_SUN3_110:
    261 		cpu_match++;
    262 		cpu_string = "110";
    263 		cpuspeed_khz = 16670;
    264 		delay_divisor = delay_divisor_est(17);
    265 		cpu_has_vme = true;
    266 		break;
    267 
    268 	case ID_SUN3_160:
    269 		cpu_match++;
    270 		cpu_string = "160";
    271 		cpuspeed_khz = 16670;
    272 		delay_divisor = delay_divisor_est(17);
    273 		cpu_has_vme = true;
    274 		break;
    275 
    276 	case ID_SUN3_260:
    277 		cpu_match++;
    278 		cpu_string = "260";
    279 		cpuspeed_khz = 25000;
    280 		delay_divisor = delay_divisor_est(25);
    281 		cpu_has_vme = true;
    282 #ifdef	HAVECACHE
    283 		cache_size = 0x10000;	/* 64K */
    284 #endif
    285 		break;
    286 
    287 	case ID_SUN3_E  :
    288 		cpu_match++;
    289 		cpu_string = "E";
    290 		cpuspeed_khz = 20000;
    291 		delay_divisor = delay_divisor_est(20); /* XXX 20MHz correct? */
    292 		cpu_has_vme = true;
    293 		break;
    294 
    295 	default:
    296 		mon_printf("unknown sun3 model\n");
    297 		sunmon_abort();
    298 	}
    299 	if (!cpu_match) {
    300 		mon_printf("kernel not configured for the Sun 3 model\n");
    301 		sunmon_abort();
    302 	}
    303 }
    304 
    305 /*
    306  * This is called from locore.s just after the kernel is remapped
    307  * to its proper address, but before the call to main().  The work
    308  * done here corresponds to various things done in locore.s on the
    309  * hp300 port (and other m68k) but which we prefer to do in C code.
    310  * Also do setup specific to the Sun PROM monitor and IDPROM here.
    311  */
    312 void * __attribute__((no_instrument_function))
    313 _bootstrap(void)
    314 {
    315 	extern struct consdev consdev_prom;	/* XXX */
    316 
    317 	/* First, Clear BSS. */
    318 	memset(edata, 0, end - edata);
    319 
    320 	/* Set v_handler, get boothowto. */
    321 	sunmon_init();
    322 
    323 	/*
    324 	 * Initialize console to point to the PROM (output only) table
    325 	 * for early printf calls.
    326 	 */
    327 	cn_tab = &consdev_prom;
    328 
    329 	/* Copy the IDPROM from control space. */
    330 	idprom_init();
    331 
    332 	/* Validate the Sun3 model (from IDPROM). */
    333 	_verify_hardware();
    334 
    335 	/* Handle kernel mapping, pmap_bootstrap(), etc. */
    336 	void *ksp = _vm_init();
    337 
    338 	/*
    339 	 * Find and save OBIO mappings needed early,
    340 	 * and call some init functions.
    341 	 */
    342 	obio_init();
    343 
    344 	/*
    345 	 * Point interrupts/exceptions to our vector table.
    346 	 * (Until now, we use the one setup by the PROM.)
    347 	 *
    348 	 * This is done after obio_init() / intreg_init() finds
    349 	 * the interrupt register and disables the NMI clock so
    350 	 * it will not cause "spurrious level 7" complaints.
    351 	 * Done after _vm_init so the PROM can debug that.
    352 	 */
    353 	vec_init();
    354 	/* Interrupts are enabled later, after autoconfig. */
    355 
    356 	/*
    357 	 * Turn on the LEDs so we know power is on.
    358 	 * Needs idprom_init and obio_init earlier.
    359 	 */
    360 	leds_init();
    361 
    362 	/*
    363 	 * Initialize the source/destination control registers for
    364 	 * movs.
    365 	 */
    366 	setsfc(FC_USERD);
    367 	setdfc(FC_USERD);
    368 
    369 	return ksp;
    370 }
    371