1 /* $NetBSD: machdep.c,v 1.96 2026/11/15 15:16:23 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1988 University of Utah. 5 * Copyright (c) 1982, 1986, 1990, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * the Systems Programming Group of the University of Utah Computer 10 * Science Department. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * from: Utah $Hdr: machdep.c 1.74 92/12/20$ 37 * 38 * @(#)machdep.c 8.10 (Berkeley) 4/20/94 39 */ 40 41 #include <sys/cdefs.h> 42 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.96 2026/11/15 15:16:23 thorpej Exp $"); 43 44 #include "opt_bufcache.h" 45 #include "opt_ddb.h" 46 #include "opt_kgdb.h" 47 #include "opt_sysv.h" 48 #include "opt_panicbutton.h" 49 #include "opt_modular.h" 50 51 #include <sys/param.h> 52 #include <sys/systm.h> 53 #include <sys/buf.h> 54 #include <sys/callout.h> 55 #include <sys/conf.h> 56 #include <sys/exec.h> 57 #include <sys/file.h> 58 #include <sys/ioctl.h> 59 #include <sys/kernel.h> 60 #include <sys/mbuf.h> 61 #include <sys/mount.h> 62 #include <sys/msgbuf.h> 63 #include <sys/proc.h> 64 #include <sys/reboot.h> 65 #include <sys/signalvar.h> 66 #include <sys/syscallargs.h> 67 #include <sys/tty.h> 68 #include <sys/vnode.h> 69 #include <sys/ksyms.h> 70 #include <sys/module.h> 71 #include <sys/cpu.h> 72 #include <sys/kgdb.h> 73 74 #include <machine/db_machdep.h> 75 #include <ddb/db_sym.h> 76 #include <ddb/db_extern.h> 77 78 #include <machine/autoconf.h> 79 #include <machine/cpu.h> 80 #include <machine/reg.h> 81 #include <machine/pcb.h> 82 #include <machine/psl.h> 83 #include <machine/pte.h> 84 85 #include <m68k/seglist.h> 86 87 #define MAXMEM 64*1024 /* XXX - from cmap.h */ 88 89 #include <uvm/uvm_extern.h> 90 91 #include <sys/sysctl.h> 92 #include <sys/device.h> 93 #include <dev/cons.h> 94 #include <dev/mm.h> 95 #include <dev/ic/z8530reg.h> 96 #include <machine/z8530var.h> 97 #include <cesfic/dev/zsvar.h> 98 99 #include "ksyms.h" 100 101 extern u_int lowram; 102 103 void machine_init(paddr_t); 104 105 /* prototypes for local functions */ 106 char *hexstr(int, int); 107 108 /* functions called from locore.s */ 109 void dumpsys(void); 110 void nmihand(struct frame); 111 112 int delay_divisor; /* delay constant */ 113 114 extern void sicinit(void); 115 extern vaddr_t sicbase; 116 extern vaddr_t zsbase; 117 118 struct pmap_bootmap machine_bootmap[] = { 119 #ifdef __HAVE_NEW_PMAP_68K 120 { .pmbm_vaddr_ptr = &sicbase, 121 .pmbm_paddr = 0x44000000, 122 .pmbm_size = PAGE_SIZE, 123 .pmbm_flags = PMBM_F_CI }, 124 125 { .pmbm_vaddr_ptr = &zsbase, 126 .pmbm_paddr = 0x58000000, 127 .pmbm_size = PAGE_SIZE, 128 .pmbm_flags = PMBM_F_CI }, 129 #endif /* __HAVE_NEW_PMAP_68K */ 130 131 { .pmbm_vaddr = -1 }, 132 }; 133 134 void 135 machine_init(paddr_t nextpa) 136 { 137 boothowto = RB_SINGLE; /* XXX for now */ 138 boothowto |= RB_KDB; /* XXX for now */ 139 140 delay_divisor = delay_divisor_est40(25); /* XXX */ 141 142 phys_seg_list[0].ps_start = lowram; 143 phys_seg_list[0].ps_end = lowram + m68k_ptob(physmem); 144 145 machine_init_common(nextpa); 146 147 /* 148 * map and init interrupt controller 149 */ 150 #ifndef __HAVE_NEW_PMAP_68K 151 physaccess((void*)virtual_avail, (void*)0x44000000, 152 PAGE_SIZE, PG_RW|PG_CI); 153 sicbase = virtual_avail; 154 virtual_avail += PAGE_SIZE; 155 #endif /* ! __HAVE_NEW_PMAP_68K */ 156 sicinit(); 157 } 158 159 int 160 zs_check_kgdb(struct zs_chanstate *cs, int dev) 161 { 162 163 if((boothowto & RB_KDB) && (dev == makedev(10, 0))) 164 return (1); 165 return (0); 166 } 167 168 void zs_kgdb_cnputc(dev_t, int); 169 void zs_kgdb_cnputc(dev_t dev, int c) 170 { 171 zscnputc(dev, c); 172 } 173 int zs_kgdb_cngetc(dev_t); 174 int zs_kgdb_cngetc(dev_t dev) 175 { 176 return (zscngetc(dev)); 177 } 178 179 /* 180 * Console initialization: called early on from main, 181 * before vm init or startup. Do enough configuration 182 * to choose and initialize a console. 183 */ 184 extern void sic_enable_int(int, int, int, int, int); 185 void 186 consinit(void) 187 { 188 189 /* 190 * Initialize the console before we print anything out. 191 */ 192 #ifndef __HAVE_NEW_PMAP_68K 193 physaccess((void*)virtual_avail, 194 (void*)0x58000000, PAGE_SIZE, PG_RW|PG_CI); 195 zsbase = virtual_avail; 196 virtual_avail += PAGE_SIZE; 197 #endif /* ! __HAVE_NEW_PMAP_68K */ 198 zs_cnattach(); 199 200 #ifdef KGDB 201 kgdb_dev = 1; 202 kgdb_attach((void*)zscngetc, (void*)zscnputc, (void *)0); 203 204 if (boothowto & RB_KDB) { 205 kgdb_connect(1); 206 zscons.cn_putc = zs_kgdb_cnputc; 207 zscons.cn_getc = zs_kgdb_cngetc; 208 } 209 #endif 210 #ifdef DDB 211 if (boothowto & RB_KDB) 212 Debugger(); 213 #endif 214 sic_enable_int(39, 2, 1, 7, 0); /* NMI */ 215 } 216 217 /* 218 * cpu_startup: allocate memory for variable-sized tables, 219 * initialize CPU, and do autoconfiguration. 220 */ 221 void 222 cpu_startup(void) 223 { 224 cpu_startup_common(); 225 226 printf("delay constant: %d\n", delay_divisor); 227 } 228 229 void 230 machine_set_model(void) 231 { 232 cpu_setmodel("FIC8234"); 233 } 234 235 /* 236 * machine dependent system variables. 237 */ 238 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup") 239 { 240 241 sysctl_createv(clog, 0, NULL, NULL, 242 CTLFLAG_PERMANENT, 243 CTLTYPE_NODE, "machdep", NULL, 244 NULL, 0, NULL, 0, 245 CTL_MACHDEP, CTL_EOL); 246 247 sysctl_createv(clog, 0, NULL, NULL, 248 CTLFLAG_PERMANENT, 249 CTLTYPE_STRUCT, "console_device", NULL, 250 sysctl_consdev, 0, NULL, sizeof(dev_t), 251 CTL_MACHDEP, CPU_CONSDEV, CTL_EOL); 252 } 253 254 /* 255 * These variables are needed by /sbin/savecore 256 */ 257 u_int32_t dumpmag = 0x8fca0101; /* magic number */ 258 int dumpsize = 0; /* pages */ 259 long dumplo = 0; /* blocks */ 260 261 /* 262 * This is called by main to set dumplo and dumpsize. 263 * Dumps always skip the first CLBYTES of disk space 264 * in case there might be a disk label stored there. 265 * If there is extra space, put dump at the end to 266 * reduce the chance that swapping trashes it. 267 */ 268 void 269 cpu_dumpconf(void) 270 { 271 int nblks; /* size of dump area */ 272 273 if (dumpdev == NODEV) 274 return; 275 nblks = bdev_size(dumpdev); 276 if (nblks <= ctod(1)) 277 return; 278 279 /* 280 * XXX include the final RAM page which is not included in physmem. 281 */ 282 dumpsize = physmem + 1; 283 284 /* Always skip the first CLBYTES, in case there is a label there. */ 285 if (dumplo < ctod(1)) 286 dumplo = ctod(1); 287 288 /* Put dump at end of partition, and make it fit. */ 289 if (dumpsize > dtoc(nblks - dumplo)) 290 dumpsize = dtoc(nblks - dumplo); 291 if (dumplo < nblks - ctod(dumpsize)) 292 dumplo = nblks - ctod(dumpsize); 293 } 294 295 /* 296 * Dump physical memory onto the dump device. Called by doadump() 297 * in locore.s or by cpu_reboot() here in machdep.c 298 */ 299 void 300 dumpsys(void) 301 { 302 const struct bdevsw *bdev; 303 daddr_t blkno; /* current block to write */ 304 /* dump routine */ 305 int (*dump)(dev_t, daddr_t, void *, size_t); 306 int pg; /* page being dumped */ 307 vm_offset_t maddr; /* PA being dumped */ 308 int error; /* error code from (*dump)() */ 309 vaddr_t vmmap = curcpu()->ci_tmpva_vmmap; 310 311 /* Don't put dump messages in msgbuf. */ 312 msgbufmapped = 0; 313 314 /* Make sure dump device is valid. */ 315 if (dumpdev == NODEV) 316 return; 317 bdev = bdevsw_lookup(dumpdev); 318 if (bdev == NULL) 319 return; 320 if (dumpsize == 0) { 321 cpu_dumpconf(); 322 if (dumpsize == 0) 323 return; 324 } 325 if (dumplo < 0) 326 return; 327 dump = bdev->d_dump; 328 blkno = dumplo; 329 330 printf("\ndumping to dev %"PRIx64", offset %ld\n", dumpdev, dumplo); 331 332 printf("dump "); 333 maddr = lowram; 334 for (pg = 0; pg < dumpsize; pg++) { 335 #define NPGMB (1024*1024/PAGE_SIZE) 336 /* print out how many MBs we have dumped */ 337 if (pg && (pg % NPGMB) == 0) 338 printf("%d ", pg / NPGMB); 339 #undef NPGMB 340 pmap_enter(pmap_kernel(), vmmap, maddr, 341 VM_PROT_READ, VM_PROT_READ|PMAP_WIRED); 342 pmap_update(pmap_kernel()); 343 344 error = (*dump)(dumpdev, blkno, (void *)vmmap, PAGE_SIZE); 345 switch (error) { 346 case 0: 347 maddr += PAGE_SIZE; 348 blkno += btodb(PAGE_SIZE); 349 break; 350 351 case ENXIO: 352 printf("device bad\n"); 353 return; 354 355 case EFAULT: 356 printf("device not ready\n"); 357 return; 358 359 case EINVAL: 360 printf("area improper\n"); 361 return; 362 363 case EIO: 364 printf("i/o error\n"); 365 return; 366 367 case EINTR: 368 printf("aborted from console\n"); 369 return; 370 371 default: 372 printf("error %d\n", error); 373 return; 374 } 375 } 376 printf("succeeded\n"); 377 } 378 379 #ifdef PANICBUTTON 380 /* 381 * Declare these so they can be patched. 382 */ 383 int panicbutton = 1; /* non-zero if panic buttons are enabled */ 384 int candbdiv = 2; /* give em half a second (hz / candbdiv) */ 385 386 void candbtimer(void *); 387 388 int crashandburn; 389 390 void 391 candbtimer(void *arg) 392 { 393 394 crashandburn = 0; 395 } 396 #endif /* PANICBUTTON */ 397 398 static int innmihand; /* simple mutex */ 399 400 /* 401 * Level 7 interrupts can be caused by the keyboard or parity errors. 402 */ 403 void 404 nmihand(struct frame frame) 405 { 406 407 /* Prevent unwanted recursion. */ 408 if (innmihand) 409 return; 410 innmihand = 1; 411 412 printf("NMI\n"); 413 #if defined(DDB) || defined(KGDB) 414 Debugger(); 415 #endif 416 417 innmihand = 0; 418 } 419