1 /* $NetBSD: machdep.c,v 1.210 2026/11/08 08:00:59 tsutsui Exp $ */ 2 3 /* 4 * Copyright (c) 1988 University of Utah. 5 * Copyright (c) 1982, 1986, 1990 The Regents of the University of California. 6 * 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.63 91/04/24$ 37 * 38 * @(#)machdep.c 7.16 (Berkeley) 6/3/91 39 */ 40 41 #include <sys/cdefs.h> 42 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.210 2026/11/08 08:00:59 tsutsui Exp $"); 43 44 #include "opt_ddb.h" 45 #include "opt_mbtype.h" 46 #include "opt_modular.h" 47 #include "opt_panicbutton.h" 48 49 #include <sys/param.h> 50 #include <sys/systm.h> 51 #include <sys/signalvar.h> 52 #include <sys/kernel.h> 53 #include <sys/proc.h> 54 #include <sys/buf.h> 55 #include <sys/reboot.h> 56 #include <sys/conf.h> 57 #include <sys/file.h> 58 #include <sys/device.h> 59 #include <sys/kmem.h> 60 #include <sys/mbuf.h> 61 #include <sys/msgbuf.h> 62 #include <sys/vnode.h> 63 #include <sys/queue.h> 64 #include <sys/mount.h> 65 #include <sys/syscallargs.h> 66 #include <sys/ksyms.h> 67 #include <sys/module.h> 68 #include <sys/intr.h> 69 #include <sys/exec.h> 70 #include <sys/cpu.h> 71 #include <sys/exec_elf.h> 72 #include <sys/core.h> 73 #include <sys/kcore.h> 74 #include <sys/bus.h> 75 76 #include <uvm/uvm_extern.h> 77 78 #include <sys/sysctl.h> 79 80 #include <machine/db_machdep.h> 81 #include <ddb/db_sym.h> 82 #include <ddb/db_extern.h> 83 84 #include <machine/reg.h> 85 #include <machine/psl.h> 86 #include <machine/pte.h> 87 #include <machine/kcore.h> 88 89 #include <m68k/pcr.h> 90 91 #include <dev/cons.h> 92 #include <dev/mm.h> 93 94 #include "ksyms.h" 95 #include "nvr.h" 96 97 #define DEV_NVRAM 11 /* Nvram minor-number */ 98 99 void cpu_init_kcorehdr(paddr_t, paddr_t); 100 static int cpu_dumpsize(void); 101 static int cpu_dump(int (*)(dev_t, daddr_t, void *, size_t), daddr_t *); 102 103 static void call_sicallbacks(void); 104 void straymfpint(int, u_short); 105 106 #ifdef _MILANHW_ 107 void nmihandler(void); 108 #endif 109 110 extern int freebufspace; 111 extern u_int lowram; 112 113 struct pmap_bootmap machine_bootmap[] = { 114 /* XXX */ 115 { .pmbm_vaddr = -1 }, 116 }; 117 118 /* 119 * Console initialization: called early on from main, 120 * before vm init or startup. Do enough configuration 121 * to choose and initialize a console. 122 */ 123 void 124 consinit(void) 125 { 126 int i; 127 128 /* 129 * Initialize error message buffer. pmap_bootstrap() has 130 * positioned this at the end of kernel memory segment - map 131 * and initialize it now. 132 */ 133 extern paddr_t msgbufpa; 134 for (i = 0; i < btoc(MSGBUFSIZE); i++) 135 pmap_kenter_pa((vaddr_t)msgbufaddr + i * PAGE_SIZE, 136 msgbufpa + i * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, 0); 137 pmap_update(pmap_kernel()); 138 initmsgbuf(msgbufaddr, m68k_round_page(MSGBUFSIZE)); 139 140 /* 141 * Initialize hardware that support various console types like 142 * the grf and PCI busses. 143 */ 144 config_console(); 145 146 /* 147 * Now pick the best console candidate. 148 */ 149 cninit(); 150 151 #if NKSYMS || defined(DDB) || defined(MODULAR) 152 { 153 extern int end; 154 extern int *esym; 155 156 ksyms_addsyms_elf((int)esym - (int)&end - sizeof(Elf32_Ehdr), 157 (void *)&end, esym); 158 } 159 #endif 160 #if defined (DDB) 161 if (boothowto & RB_KDB) 162 Debugger(); 163 #endif 164 } 165 166 vsize_t mem_size; 167 168 void 169 machine_set_model(void) 170 { 171 const char *mach; 172 173 switch (machineid & ATARI_ANYMACH) { 174 case ATARI_TT: 175 mach = "Atari TT"; 176 break; 177 case ATARI_FALCON: 178 mach = "Atari Falcon"; 179 break; 180 case ATARI_HADES: 181 mach = "Atari Hades"; 182 break; 183 case ATARI_MILAN: 184 mach = "Atari Milan"; 185 break; 186 default: 187 mach = "Atari UNKNOWN"; 188 break; 189 } 190 191 cpu_setmodel("%s", mach); 192 } 193 194 /* 195 * machine dependent system variables. 196 */ 197 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup") 198 { 199 200 sysctl_createv(clog, 0, NULL, NULL, 201 CTLFLAG_PERMANENT, 202 CTLTYPE_NODE, "machdep", NULL, 203 NULL, 0, NULL, 0, 204 CTL_MACHDEP, CTL_EOL); 205 206 sysctl_createv(clog, 0, NULL, NULL, 207 CTLFLAG_PERMANENT, 208 CTLTYPE_STRUCT, "console_device", NULL, 209 sysctl_consdev, 0, NULL, sizeof(dev_t), 210 CTL_MACHDEP, CPU_CONSDEV, CTL_EOL); 211 } 212 213 static vaddr_t dumpspace; /* Virt. space to map dumppages */ 214 215 /* 216 * Reserve _virtual_ memory to map in the page to be dumped 217 */ 218 vaddr_t 219 reserve_dumppages(vaddr_t p) 220 { 221 222 dumpspace = p; 223 return p + PAGE_SIZE; 224 } 225 226 uint32_t dumpmag = 0x8fca0101; /* magic number for savecore */ 227 int dumpsize = 0; /* also for savecore (pages) */ 228 long dumplo = 0; /* (disk blocks) */ 229 230 void 231 cpu_dumpconf(void) 232 { 233 int nblks, i; 234 235 for (i = dumpsize = 0; i < NMEM_SEGS; i++) { 236 if (boot_segs[i].start == boot_segs[i].end) 237 break; 238 dumpsize += boot_segs[i].end - boot_segs[i].start; 239 } 240 dumpsize = btoc(dumpsize); 241 242 if (dumpdev != NODEV) { 243 nblks = bdev_size(dumpdev); 244 if (nblks > 0) { 245 if (dumpsize > btoc(dbtob(nblks - dumplo))) 246 dumpsize = btoc(dbtob(nblks - dumplo)); 247 else if (dumplo == 0) 248 dumplo = nblks - btodb(ctob(dumpsize)); 249 } 250 } 251 dumplo -= cpu_dumpsize(); 252 253 /* 254 * Don't dump on the first PAGE_SIZE (why PAGE_SIZE?) 255 * in case the dump device includes a disk label. 256 */ 257 if (dumplo < btodb(PAGE_SIZE)) 258 dumplo = btodb(PAGE_SIZE); 259 } 260 261 /* 262 * Doadump comes here after turning off memory management and 263 * getting on the dump stack, either when called above, or by 264 * the auto-restart code. 265 */ 266 void 267 dumpsys(void) 268 { 269 const struct bdevsw *bdev; 270 daddr_t blkno; /* Current block to write */ 271 int (*dump)(dev_t, daddr_t, void *, size_t); 272 /* Dumping function */ 273 u_long maddr; /* PA being dumped */ 274 int segbytes; /* Number of bytes in this seg. */ 275 int segnum; /* Segment we are dumping */ 276 int nbytes; /* Bytes left to dump */ 277 int i, n, error; 278 279 error = segnum = 0; 280 if (dumpdev == NODEV) 281 return; 282 bdev = bdevsw_lookup(dumpdev); 283 if (bdev == NULL) 284 return; 285 /* 286 * For dumps during autoconfiguration, 287 * if dump device has already configured... 288 */ 289 if (dumpsize == 0) 290 cpu_dumpconf(); 291 if (dumplo <= 0) { 292 printf("\ndump to dev %u,%u not possible\n", 293 major(dumpdev), minor(dumpdev)); 294 return; 295 } 296 printf("\ndumping to dev %u,%u offset %ld\n", 297 major(dumpdev), minor(dumpdev), dumplo); 298 299 #if defined(DDB) || defined(PANICWAIT) 300 printf("Do you want to dump memory? [y]"); 301 cnpollc(true); 302 i = cngetc(); 303 cnpollc(false); 304 cnputc(i); 305 switch (i) { 306 case 'n': 307 case 'N': 308 return; 309 case '\n': 310 break; 311 default : 312 cnputc('\n'); 313 } 314 #endif /* defined(DDB) || defined(PANICWAIT) */ 315 316 maddr = 0; 317 segbytes = boot_segs[0].end; 318 blkno = dumplo; 319 dump = bdev->d_dump; 320 nbytes = dumpsize * PAGE_SIZE; 321 322 printf("dump "); 323 324 error = cpu_dump(dump, &blkno); 325 if (!error) { 326 for (i = 0; i < nbytes; i += n, segbytes -= n) { 327 /* 328 * Skip the hole 329 */ 330 if (segbytes == 0) { 331 segnum++; 332 maddr = boot_segs[segnum].start; 333 segbytes = boot_segs[segnum].end - 334 boot_segs[segnum].start; 335 } 336 /* 337 * Print Mb's to go 338 */ 339 n = nbytes - i; 340 if (n && (n % (1024 * 1024)) == 0) 341 printf_nolog("%d ", n / (1024 * 1024)); 342 343 /* 344 * Limit transfer to PAGE_SIZE 345 */ 346 if (n > PAGE_SIZE) 347 n = PAGE_SIZE; 348 349 /* 350 * Map to a VA and write it 351 */ 352 if (maddr != 0) { /* XXX kvtop chokes on this */ 353 pmap_enter(pmap_kernel(), dumpspace, maddr, 354 VM_PROT_READ, VM_PROT_READ|PMAP_WIRED); 355 pmap_update(pmap_kernel()); 356 error = (*dump)(dumpdev, blkno, 357 (void *)dumpspace, n); 358 if (error) 359 break; 360 } 361 362 maddr += n; 363 blkno += btodb(n); 364 } 365 } 366 switch (error) { 367 368 case ENXIO: 369 printf("device bad\n"); 370 break; 371 372 case EFAULT: 373 printf("device not ready\n"); 374 break; 375 376 case EINVAL: 377 printf("area improper\n"); 378 break; 379 380 case EIO: 381 printf("i/o error\n"); 382 break; 383 384 default: 385 printf("succeeded\n"); 386 break; 387 } 388 printf("\n\n"); 389 delay(5000000); /* 5 seconds */ 390 } 391 392 void 393 straymfpint(int pc, u_short evec) 394 { 395 396 printf("unexpected mfp-interrupt (vector offset 0x%x) from 0x%x\n", 397 evec & 0xFFF, pc); 398 } 399 400 /* 401 * this is a handy package to have asynchronously executed 402 * function calls executed at very low interrupt priority. 403 * Example for use is keyboard repeat, where the repeat 404 * handler running at splclock() triggers such a (hardware 405 * aided) software interrupt. 406 * Note: the installed functions are currently called in a 407 * LIFO fashion, might want to change this to FIFO 408 * later. 409 * 410 * XXX: Some of functions which use this callback should be rewritten 411 * XXX: to use MI softintr(9) directly. 412 */ 413 struct si_callback { 414 struct si_callback *next; 415 void (*function)(void *rock1, void *rock2); 416 void *rock1, *rock2; 417 }; 418 static void *si_callback_cookie; 419 static struct si_callback *si_callbacks; 420 static struct si_callback *si_free; 421 #ifdef DIAGNOSTIC 422 static int ncbd; /* number of callback blocks dynamically allocated */ 423 #endif 424 425 void 426 init_sicallback(void) 427 { 428 429 si_callback_cookie = softint_establish(SOFTINT_NET, 430 (void (*)(void *))call_sicallbacks, NULL); 431 } 432 433 void 434 add_sicallback(void (*function)(void *, void *), void *rock1, void *rock2) 435 { 436 struct si_callback *si; 437 int s; 438 439 /* 440 * this function may be called from high-priority interrupt handlers. 441 * We may NOT block for memory-allocation in here!. 442 */ 443 s = splhigh(); 444 if ((si = si_free) != NULL) 445 si_free = si->next; 446 splx(s); 447 448 if (si == NULL) { 449 si = kmem_intr_alloc(sizeof(*si), KM_NOSLEEP); 450 #ifdef DIAGNOSTIC 451 if (si) 452 ++ncbd; /* count # dynamically allocated */ 453 #endif 454 if (si == NULL) 455 return; 456 } 457 458 si->function = function; 459 si->rock1 = rock1; 460 si->rock2 = rock2; 461 462 s = splhigh(); 463 si->next = si_callbacks; 464 si_callbacks = si; 465 splx(s); 466 467 /* 468 * and cause a software interrupt (spl1). This interrupt might 469 * happen immediately, or after returning to a safe enough level. 470 * 471 * XXX: 472 * According to <machine/scu.h> and lev1intr() handler in locore.s, 473 * at least _ATARIHW_ machines (ATARITT and HADES?) seem to have 474 * some hardware support which can initiate real hardware interrupt 475 * at ipl 1 for software interrupt. But as per <machine/mtpr.h>, 476 * this feature was not used at all on setsoft*() calls and 477 * traditional hp300 derived ssir (simulated software interrupt 478 * request) on VAX REI emulation in locore.s is used. 479 */ 480 softint_schedule(si_callback_cookie); 481 } 482 483 void 484 rem_sicallback(void (*function)(void *rock1, void *rock2)) 485 { 486 struct si_callback *si, *psi, *nsi; 487 int s; 488 489 s = splhigh(); 490 for (psi = 0, si = si_callbacks; si; ) { 491 nsi = si->next; 492 493 if (si->function != function) 494 psi = si; 495 else { 496 si->next = si_free; 497 si_free = si; 498 if (psi != NULL) 499 psi->next = nsi; 500 else 501 si_callbacks = nsi; 502 } 503 si = nsi; 504 } 505 splx(s); 506 } 507 508 /* purge the list */ 509 static void 510 call_sicallbacks(void) 511 { 512 struct si_callback *si; 513 int s; 514 void *rock1, *rock2; 515 void (*function)(void *, void *); 516 517 do { 518 s = splhigh(); 519 if ((si = si_callbacks) != NULL) 520 si_callbacks = si->next; 521 splx(s); 522 523 if (si != NULL) { 524 function = si->function; 525 rock1 = si->rock1; 526 rock2 = si->rock2; 527 s = splhigh(); 528 if (si_callbacks) 529 softint_schedule(si_callback_cookie); 530 si->next = si_free; 531 si_free = si; 532 splx(s); 533 534 /* 535 * Raise spl for BASEPRI() checks to see 536 * nested interrupts in some drivers using callbacks 537 * since modern MI softint(9) doesn't seem to do it 538 * in !__HAVE_FAST_SOFTINTS case. 539 * 540 * XXX: This is just a workaround hack. 541 * Each driver should raise spl in its handler 542 * to avoid nested interrupts if necessary. 543 */ 544 s = splsoftnet(); /* XXX */ 545 function(rock1, rock2); 546 splx(s); 547 } 548 } while (si != NULL); 549 #ifdef DIAGNOSTIC 550 if (ncbd) { 551 #ifdef DEBUG 552 printf("call_sicallback: %d more dynamic structures\n", ncbd); 553 #endif 554 ncbd = 0; 555 } 556 #endif 557 } 558 559 #if defined(DEBUG) && !defined(PANICBUTTON) 560 #define PANICBUTTON 561 #endif 562 563 #ifdef PANICBUTTON 564 int panicbutton = 1; /* non-zero if panic buttons are enabled */ 565 int crashandburn = 0; 566 int candbdelay = 50; /* give em half a second */ 567 568 void candbtimer(void); 569 570 void 571 candbtimer(void) 572 { 573 574 crashandburn = 0; 575 } 576 #endif 577 578 #ifdef _MILANHW_ 579 580 /* 581 * Currently the only source of NMI interrupts on the Milan is the PLX9080. 582 * On access errors to the PCI bus, an NMI is generated. This NMI is shorted 583 * in locore in case of a PCI config cycle to a non-existing address to allow 584 * for probes. On other occasions, it ShouldNotHappen(TM). 585 * Note: The handler in locore clears the errors, to make further PCI access 586 * possible. 587 */ 588 void 589 nmihandler(void) 590 { 591 extern unsigned long plx_status; 592 593 printf("nmihandler: plx_status = 0x%08lx\n", plx_status); 594 } 595 #endif 596 597 int 598 mm_md_physacc(paddr_t pa, vm_prot_t prot) 599 { 600 struct memseg *ms; 601 602 for (ms = boot_segs; ms->start != ms->end; ms++) { 603 if ((pa >= ms->start) && (pa < ms->end)) 604 return 0; 605 } 606 return EFAULT; 607 } 608 609 int 610 mm_md_readwrite(dev_t dev, struct uio *uio) 611 { 612 switch (minor(dev)) { 613 case DEV_NVRAM: 614 #if NNVR > 0 615 return nvram_uio(uio); 616 #else 617 return ENXIO; 618 #endif 619 default: 620 return ENXIO; 621 } 622 } 623 624 /* 625 * All info needed to generate a panic dump. All fields are setup by 626 * start_c(). 627 * XXX: Should sheck usage of phys_segs. There is some unwanted overlap 628 * here.... Also, the name is badly chosen. Phys_segs contains the 629 * segment descriptions _after_ reservations are made. 630 * XXX: 'lowram' is obsoleted by the new panicdump format 631 */ 632 static cpu_kcore_hdr_t cpu_kcore_hdr; 633 634 /* 635 * Used by dumpconf() to get the size of the machine-dependent panic-dump 636 * header in disk blocks. 637 */ 638 639 #define CHDRSIZE (ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t))) 640 #define MDHDRSIZE roundup(CHDRSIZE, dbtob(1)) 641 642 int 643 cpu_dumpsize(void) 644 { 645 646 return btodb(MDHDRSIZE); 647 } 648 649 /* 650 * Called by dumpsys() to dump the machine-dependent header. 651 * XXX: Assumes that it will all fit in one diskblock. 652 */ 653 int 654 cpu_dump(int (*dump)(dev_t, daddr_t, void *, size_t), daddr_t *p_blkno) 655 { 656 int buf[MDHDRSIZE/sizeof(int)]; 657 int error; 658 kcore_seg_t *kseg_p; 659 cpu_kcore_hdr_t *chdr_p; 660 661 kseg_p = (kcore_seg_t *)buf; 662 chdr_p = (cpu_kcore_hdr_t *)&buf[ALIGN(sizeof(*kseg_p)) / sizeof(int)]; 663 664 /* 665 * Generate a segment header 666 */ 667 CORE_SETMAGIC(*kseg_p, KCORE_MAGIC, MID_MACHINE, CORE_CPU); 668 kseg_p->c_size = MDHDRSIZE - ALIGN(sizeof(*kseg_p)); 669 670 /* 671 * Add the md header 672 */ 673 *chdr_p = cpu_kcore_hdr; 674 error = dump(dumpdev, *p_blkno, (void *)buf, sizeof(buf)); 675 *p_blkno += btodb(sizeof(buf)); 676 return (error); 677 } 678 679 #if (M68K_NPHYS_RAM_SEGS < NMEM_SEGS) 680 #error "Configuration error: M68K_NPHYS_RAM_SEGS < NMEM_SEGS" 681 #endif 682 /* 683 * Initialize the cpu_kcore_header. 684 */ 685 void __attribute__((no_instrument_function)) 686 cpu_init_kcorehdr(paddr_t kbase, paddr_t sysseg_pa) 687 { 688 cpu_kcore_hdr_t *h = &cpu_kcore_hdr; 689 struct m68k_kcore_hdr *m = &h->un._m68k; 690 extern int end; 691 int i; 692 693 memset(&cpu_kcore_hdr, 0, sizeof(cpu_kcore_hdr)); 694 695 /* 696 * Initialize the `dispatcher' portion of the header. 697 */ 698 strcpy(h->name, machine); 699 h->page_size = PAGE_SIZE; 700 h->kernbase = KERNBASE; 701 702 /* 703 * Fill in information about our MMU configuration. 704 */ 705 m->mmutype = mmutype; 706 m->sg_v = SG_V; 707 m->sg_frame = SG_FRAME; 708 m->sg_ishift = SG_ISHIFT; 709 m->sg_pmask = SG_PMASK; 710 m->sg40_shift1 = SG4_SHIFT1; 711 m->sg40_mask2 = SG4_MASK2; 712 m->sg40_shift2 = SG4_SHIFT2; 713 m->sg40_mask3 = SG4_MASK3; 714 m->sg40_shift3 = SG4_SHIFT3; 715 m->sg40_addr1 = SG4_ADDR1; 716 m->sg40_addr2 = SG4_ADDR2; 717 m->pg_v = PG_V; 718 m->pg_frame = PG_FRAME; 719 720 /* 721 * Initialize pointer to kernel segment table. 722 */ 723 m->sysseg_pa = sysseg_pa; /* PA after relocation */ 724 725 /* 726 * Initialize relocation value such that: 727 * 728 * pa = (va - KERNBASE) + reloc 729 */ 730 m->reloc = kbase; 731 732 /* 733 * Define the end of the relocatable range. 734 */ 735 m->relocend = (vaddr_t)&end; 736 737 for (i = 0; i < NMEM_SEGS; i++) { 738 m->ram_segs[i].start = boot_segs[i].start; 739 m->ram_segs[i].size = boot_segs[i].end - 740 boot_segs[i].start; 741 } 742 } 743