machdep.c revision 1.139 1 /* $NetBSD: machdep.c,v 1.139 2020/06/11 19:20:45 ad 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 * from: @(#)machdep.c 8.10 (Berkeley) 4/20/94
38 */
39
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.139 2020/06/11 19:20:45 ad Exp $");
42
43 #include "opt_ddb.h"
44 #include "opt_kgdb.h"
45 #include "opt_modular.h"
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/proc.h>
51 #include <sys/buf.h>
52 #include <sys/reboot.h>
53 #include <sys/conf.h>
54 #include <sys/file.h>
55 #include <sys/device.h>
56 #include <sys/malloc.h>
57 #include <sys/mbuf.h>
58 #include <sys/msgbuf.h>
59 #include <sys/ioctl.h>
60 #include <sys/tty.h>
61 #include <sys/mount.h>
62 #include <sys/exec.h>
63 #include <sys/exec_aout.h> /* for MID_* */
64 #include <sys/core.h>
65 #include <sys/kcore.h>
66 #include <sys/vnode.h>
67 #include <sys/syscallargs.h>
68 #include <sys/ksyms.h>
69 #include <sys/module.h>
70 #include <sys/cpu.h>
71 #ifdef KGDB
72 #include <sys/kgdb.h>
73 #endif
74
75 #include <uvm/uvm_extern.h>
76
77 #include <sys/sysctl.h>
78
79 #include <dev/cons.h>
80 #include <dev/mm.h>
81
82 #include <machine/cpu.h>
83 #include <machine/dvma.h>
84 #include <machine/idprom.h>
85 #include <machine/kcore.h>
86 #include <machine/reg.h>
87 #include <machine/pcb.h>
88 #include <machine/psl.h>
89 #include <machine/pte.h>
90 #include <machine/mon.h>
91
92 #if defined(DDB)
93 #include <machine/db_machdep.h>
94 #include <ddb/db_sym.h>
95 #include <ddb/db_extern.h>
96 #endif
97
98 #include <sun3/sun3/machdep.h>
99
100 #include "ksyms.h"
101
102 /* Defined in locore.s */
103 extern char kernel_text[];
104 /* Defined by the linker */
105 extern char etext[];
106
107 /* kernel_arch specific values required by module(9) */
108 const vaddr_t kernbase = KERNBASE3X;
109 const vaddr_t kern_end = KERN_END3X;
110
111 /* Our exported CPU info; we can have only one. */
112 struct cpu_info cpu_info_store;
113
114 struct vm_map *phys_map = NULL;
115
116 int fputype;
117 void * msgbufaddr;
118
119 /* Virtual page frame for /dev/mem (see mem.c) */
120 vaddr_t vmmap;
121
122 u_char cpu_machine_id = 0;
123 const char *cpu_string = NULL;
124 int cpu_has_vme = 0;
125 int has_iocache = 0;
126
127 vaddr_t dumppage;
128
129 static void identifycpu(void);
130 static void initcpu(void);
131
132 /*
133 * Console initialization: called early on from main,
134 * before vm init or cpu_startup. This system is able
135 * to use the console for output immediately (via PROM)
136 * but can not use it for input until after this point.
137 */
138 void
139 consinit(void)
140 {
141
142 /*
143 * Switch from the PROM console (output only)
144 * to our own console driver.
145 */
146 cninit();
147
148 #if NKSYMS || defined(DDB) || defined(MODULAR)
149 {
150 extern int nsym;
151 extern char *ssym, *esym;
152
153 ksyms_addsyms_elf(nsym, ssym, esym);
154 }
155 #endif /* DDB */
156
157 /*
158 * Now that the console can do input as well as
159 * output, consider stopping for a debugger.
160 */
161 if (boothowto & RB_KDB) {
162 #ifdef KGDB
163 /* XXX - Ask on console for kgdb_dev? */
164 /* Note: this will just return if kgdb_dev==NODEV */
165 kgdb_connect(1);
166 #else /* KGDB */
167 /* Either DDB or no debugger (just PROM). */
168 Debugger();
169 #endif /* KGDB */
170 }
171 }
172
173 /*
174 * cpu_startup: allocate memory for variable-sized tables,
175 * initialize CPU, and do autoconfiguration.
176 *
177 * This is called early in init_main.c:main(), after the
178 * kernel memory allocator is ready for use, but before
179 * the creation of processes 1,2, and mountroot, etc.
180 */
181 void
182 cpu_startup(void)
183 {
184 char *v;
185 vaddr_t minaddr, maxaddr;
186 char pbuf[9];
187
188 /*
189 * Initialize message buffer (for kernel printf).
190 * This is put in physical page zero so it will
191 * always be in the same place after a reboot.
192 * Its mapping was prepared in pmap_bootstrap().
193 * Also, offset some to avoid PROM scribbles.
194 */
195 v = (char *)KERNBASE3X;
196 msgbufaddr = v + MSGBUFOFF;
197 initmsgbuf(msgbufaddr, MSGBUFSIZE);
198
199 /*
200 * Good {morning,afternoon,evening,night}.
201 */
202 printf("%s%s", copyright, version);
203 identifycpu();
204 initfpu(); /* also prints FPU type */
205
206 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
207 printf("total memory = %s\n", pbuf);
208
209 /*
210 * Get scratch page for dumpsys().
211 */
212 dumppage = uvm_km_alloc(kernel_map, PAGE_SIZE, 0, UVM_KMF_WIRED);
213 if (dumppage == 0)
214 panic("startup: alloc dumppage");
215
216 minaddr = 0;
217
218 /*
219 * Allocate a submap for physio
220 */
221 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
222 VM_PHYS_SIZE, 0, false, NULL);
223
224 format_bytes(pbuf, sizeof(pbuf), ptoa(uvm_availmem(false)));
225 printf("avail memory = %s\n", pbuf);
226
227 /*
228 * Allocate a virtual page (for use by /dev/mem)
229 * This page is handed to pmap_enter() therefore
230 * it has to be in the normal kernel VA range.
231 */
232 vmmap = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
233 UVM_KMF_VAONLY | UVM_KMF_WAITVA);
234
235 /*
236 * Create the DVMA maps.
237 */
238 dvma_init();
239
240 /*
241 * Set up CPU-specific registers, cache, etc.
242 */
243 initcpu();
244 }
245
246 /*
247 * Info for CTL_HW
248 */
249 char machine[16] = MACHINE; /* from <machine/param.h> */
250 char kernel_arch[16] = "sun3x"; /* XXX needs a sysctl node */
251
252 /*
253 * XXX - Should empirically estimate the divisor...
254 * Note that the value of delay_divisor is roughly
255 * 2048 / cpuclock (where cpuclock is in MHz).
256 */
257 int delay_divisor = 62; /* assume the fastest (33 MHz) */
258
259 void
260 identifycpu(void)
261 {
262 u_char machtype;
263
264 machtype = identity_prom.idp_machtype;
265 if ((machtype & IDM_ARCH_MASK) != IDM_ARCH_SUN3X) {
266 printf("Bad IDPROM arch!\n");
267 sunmon_abort();
268 }
269
270 cpu_machine_id = machtype;
271 switch (cpu_machine_id) {
272
273 case ID_SUN3X_80:
274 cpu_string = "80"; /* Hydra */
275 delay_divisor = 102; /* 20 MHz */
276 cpu_has_vme = false;
277 break;
278
279 case ID_SUN3X_470:
280 cpu_string = "470"; /* Pegasus */
281 delay_divisor = 62; /* 33 MHz */
282 cpu_has_vme = true;
283 break;
284
285 default:
286 printf("unknown sun3x model\n");
287 sunmon_abort();
288 }
289
290 /* Other stuff? (VAC, mc6888x version, etc.) */
291 /* Note: miniroot cares about the kernel_arch part. */
292 cpu_setmodel("%s %s", kernel_arch, cpu_string);
293
294 printf("Model: %s\n", cpu_getmodel());
295 }
296
297 /*
298 * machine dependent system variables.
299 */
300 #if 0 /* XXX - Not yet... */
301 static int
302 sysctl_machdep_root_device(SYSCTLFN_ARGS)
303 {
304 struct sysctlnode node = *rnode;
305
306 node.sysctl_data = some permutation on root_device;
307 node.sysctl_size = strlen(root_device) + 1;
308 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
309 }
310
311 static int
312 sysctl_machdep_booted_kernel(SYSCTLFN_ARGS)
313 {
314 struct sysctlnode node = *rnode;
315
316 node.sysctl_data = some permutation on booted_kernel;
317 node.sysctl_size = strlen(booted_kernel) + 1;
318 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
319 }
320 #endif
321
322 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup")
323 {
324
325 sysctl_createv(clog, 0, NULL, NULL,
326 CTLFLAG_PERMANENT,
327 CTLTYPE_NODE, "machdep", NULL,
328 NULL, 0, NULL, 0,
329 CTL_MACHDEP, CTL_EOL);
330
331 sysctl_createv(clog, 0, NULL, NULL,
332 CTLFLAG_PERMANENT,
333 CTLTYPE_STRUCT, "console_device", NULL,
334 sysctl_consdev, 0, NULL, sizeof(dev_t),
335 CTL_MACHDEP, CPU_CONSDEV, CTL_EOL);
336 #if 0 /* XXX - Not yet... */
337 sysctl_createv(clog, 0, NULL, NULL,
338 CTLFLAG_PERMANENT,
339 CTLTYPE_STRING, "root_device", NULL,
340 sysctl_machdep_root_device, 0, NULL, 0,
341 CTL_MACHDEP, CPU_ROOT_DEVICE, CTL_EOL);
342 sysctl_createv(clog, 0, NULL, NULL,
343 CTLFLAG_PERMANENT,
344 CTLTYPE_STRING, "booted_kernel", NULL,
345 sysctl_machdep_booted_kernel, 0, NULL, 0,
346 CTL_MACHDEP, CPU_BOOTED_KERNEL, CTL_EOL);
347 #endif
348 }
349
350 /* See: sig_machdep.c */
351
352 /*
353 * Do a sync in preparation for a reboot.
354 * XXX - This could probably be common code.
355 * XXX - And now, most of it is in vfs_shutdown()
356 * XXX - Put waittime checks in there too?
357 */
358 int waittime = -1; /* XXX - Who else looks at this? -gwr */
359 static void
360 reboot_sync(void)
361 {
362
363 /* Check waittime here to localize its use to this function. */
364 if (waittime >= 0)
365 return;
366 waittime = 0;
367 vfs_shutdown();
368 }
369
370 /*
371 * Common part of the BSD and SunOS reboot system calls.
372 */
373 __dead void
374 cpu_reboot(int howto, char *user_boot_string)
375 {
376 /* Note: this string MUST be static! */
377 static char bootstr[128];
378 char *p;
379
380 /* If system is cold, just halt. (early panic?) */
381 if (cold)
382 goto haltsys;
383
384 /* Un-blank the screen if appropriate. */
385 cnpollc(1);
386
387 if ((howto & RB_NOSYNC) == 0) {
388 reboot_sync();
389 /*
390 * If we've been adjusting the clock, the todr
391 * will be out of synch; adjust it now.
392 *
393 * XXX - However, if the kernel has been sitting in ddb,
394 * the time will be way off, so don't set the HW clock!
395 * XXX - Should do sanity check against HW clock. -gwr
396 */
397 /* resettodr(); */
398 }
399
400 /* Disable interrupts. */
401 splhigh();
402
403 /* Write out a crash dump if asked. */
404 if (howto & RB_DUMP)
405 dumpsys();
406
407 /* run any shutdown hooks */
408 doshutdownhooks();
409
410 pmf_system_shutdown(boothowto);
411
412 if (howto & RB_HALT) {
413 haltsys:
414 printf("halted.\n");
415 sunmon_halt();
416 }
417
418 /*
419 * Automatic reboot.
420 */
421 if (user_boot_string)
422 strncpy(bootstr, user_boot_string, sizeof(bootstr));
423 else {
424 /*
425 * Build our own boot string with an empty
426 * boot device/file and (maybe) some flags.
427 * The PROM will supply the device/file name.
428 */
429 p = bootstr;
430 *p = '\0';
431 if (howto & (RB_KDB|RB_ASKNAME|RB_SINGLE)) {
432 /* Append the boot flags. */
433 *p++ = ' ';
434 *p++ = '-';
435 if (howto & RB_KDB)
436 *p++ = 'd';
437 if (howto & RB_ASKNAME)
438 *p++ = 'a';
439 if (howto & RB_SINGLE)
440 *p++ = 's';
441 *p = '\0';
442 }
443 }
444 printf("rebooting...\n");
445 sunmon_reboot(bootstr);
446 for (;;) ;
447 /*NOTREACHED*/
448 }
449
450 /*
451 * These variables are needed by /sbin/savecore
452 */
453 uint32_t dumpmag = 0x8fca0101; /* magic number */
454 int dumpsize = 0; /* pages */
455 long dumplo = 0; /* blocks */
456
457 #define DUMP_EXTRA 1 /* CPU-dependent extra pages */
458
459 /*
460 * This is called by main to set dumplo, dumpsize.
461 * Dumps always skip the first PAGE_SIZE of disk space
462 * in case there might be a disk label stored there.
463 * If there is extra space, put dump at the end to
464 * reduce the chance that swapping trashes it.
465 */
466 void
467 cpu_dumpconf(void)
468 {
469 int devblks; /* size of dump device in blocks */
470 int dumpblks; /* size of dump image in blocks */
471
472 if (dumpdev == NODEV)
473 return;
474
475 devblks = bdev_size(dumpdev);
476 if (devblks <= ctod(1))
477 return;
478 devblks &= ~(ctod(1) - 1);
479
480 /*
481 * Note: savecore expects dumpsize to be the
482 * number of pages AFTER the dump header.
483 */
484 dumpsize = physmem; /* pages */
485
486 /* Position dump image near end of space, page aligned. */
487 dumpblks = ctod(physmem + DUMP_EXTRA);
488 dumplo = devblks - dumpblks;
489
490 /* If it does not fit, truncate it by moving dumplo. */
491 /* Note: Must force signed comparison. */
492 if (dumplo < ((long)ctod(1))) {
493 dumplo = ctod(1);
494 dumpsize = dtoc(devblks - dumplo) - DUMP_EXTRA;
495 }
496 }
497
498 /* Note: gdb looks for "dumppcb" in a kernel crash dump. */
499 struct pcb dumppcb;
500
501 /*
502 * Write a crash dump. The format while in swap is:
503 * kcore_seg_t cpu_hdr;
504 * cpu_kcore_hdr_t cpu_data;
505 * padding (PAGE_SIZE-sizeof(kcore_seg_t))
506 * pagemap (2*PAGE_SIZE)
507 * physical memory...
508 */
509 void
510 dumpsys(void)
511 {
512 const struct bdevsw *dsw;
513 kcore_seg_t *kseg_p;
514 cpu_kcore_hdr_t *chdr_p;
515 struct sun3x_kcore_hdr *sh;
516 phys_ram_seg_t *crs_p;
517 char *vaddr;
518 paddr_t paddr;
519 int psize, todo, seg, segsz;
520 daddr_t blkno;
521 int error = 0;
522
523 if (dumpdev == NODEV)
524 return;
525 dsw = bdevsw_lookup(dumpdev);
526 if (dsw == NULL || dsw->d_psize == NULL)
527 return;
528
529 /*
530 * For dumps during autoconfiguration,
531 * if dump device has already configured...
532 */
533 if (dumpsize == 0)
534 cpu_dumpconf();
535 if (dumplo <= 0) {
536 printf("\ndump to dev %u,%u not possible\n",
537 major(dumpdev), minor(dumpdev));
538 return;
539 }
540 savectx(&dumppcb);
541
542 psize = bdev_size(dumpdev);
543 if (psize == -1) {
544 printf("dump area unavailable\n");
545 return;
546 }
547
548 printf("\ndumping to dev %u,%u offset %ld\n",
549 major(dumpdev), minor(dumpdev), dumplo);
550
551 /*
552 * Prepare the dump header
553 */
554 blkno = dumplo;
555 todo = dumpsize; /* pages */
556 vaddr = (char *)dumppage;
557 memset(vaddr, 0, PAGE_SIZE);
558
559 /* Set pointers to all three parts. */
560 kseg_p = (kcore_seg_t *)vaddr;
561 chdr_p = (cpu_kcore_hdr_t *)(kseg_p + 1);
562 sh = &chdr_p->un._sun3x;
563
564 /* Fill in kcore_seg_t part. */
565 CORE_SETMAGIC(*kseg_p, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
566 kseg_p->c_size = (ctob(DUMP_EXTRA) - sizeof(*kseg_p));
567
568 /* Fill in cpu_kcore_hdr_t part. */
569 strncpy(chdr_p->name, kernel_arch, sizeof(chdr_p->name));
570 chdr_p->page_size = PAGE_SIZE;
571 chdr_p->kernbase = KERNBASE3X;
572
573 /* Fill in the sun3x_kcore_hdr part. */
574 pmap_kcore_hdr(sh);
575
576 /* Write out the dump header. */
577 error = (*dsw->d_dump)(dumpdev, blkno, vaddr, PAGE_SIZE);
578 if (error)
579 goto fail;
580 blkno += btodb(PAGE_SIZE);
581
582 /*
583 * Now dump physical memory. Note that physical memory
584 * might NOT be congiguous, so do it by segments.
585 */
586
587 vaddr = (char *)vmmap; /* Borrow /dev/mem VA */
588
589 for (seg = 0; seg < SUN3X_NPHYS_RAM_SEGS; seg++) {
590 crs_p = &sh->ram_segs[seg];
591 paddr = crs_p->start;
592 segsz = crs_p->size;
593
594 while (todo && (segsz > 0)) {
595
596 /* Print pages left after every 16. */
597 if ((todo & 0xf) == 0)
598 printf_nolog("\r%4d", todo);
599
600 /* Make a temporary mapping for the page. */
601 pmap_kenter_pa(vmmap, paddr | PMAP_NC, VM_PROT_READ, 0);
602 pmap_update(pmap_kernel());
603 error = (*dsw->d_dump)(dumpdev, blkno, vaddr,
604 PAGE_SIZE);
605 pmap_kremove(vmmap, PAGE_SIZE);
606 pmap_update(pmap_kernel());
607 if (error)
608 goto fail;
609 paddr += PAGE_SIZE;
610 segsz -= PAGE_SIZE;
611 blkno += btodb(PAGE_SIZE);
612 todo--;
613 }
614 }
615 printf("\rdump succeeded\n");
616 return;
617 fail:
618 printf(" dump error=%d\n", error);
619 }
620
621 static void
622 initcpu(void)
623 {
624 /* XXX: Enable RAM parity/ECC checking? */
625 /* XXX: parityenable(); */
626
627 #ifdef HAVECACHE
628 cache_enable();
629 #endif
630 }
631
632 /* straptrap() in trap.c */
633
634 /* from hp300: badaddr() */
635 /* peek_byte(), peek_word() moved to bus_subr.c */
636
637 /* XXX: parityenable() ? */
638 /* regdump() moved to regdump.c */
639
640 /*
641 * cpu_exec_aout_makecmds():
642 * CPU-dependent a.out format hook for execve().
643 *
644 * Determine if the given exec package refers to something which we
645 * understand and, if so, set up the vmcmds for it.
646 */
647 int
648 cpu_exec_aout_makecmds(struct lwp *l, struct exec_package *epp)
649 {
650 return ENOEXEC;
651 }
652
653 int
654 mm_md_physacc(paddr_t pa, vm_prot_t prot)
655 {
656
657 return pmap_pa_exists(pa) ? 0 : EFAULT;
658 }
659
660 bool
661 mm_md_direct_mapped_phys(paddr_t paddr, vaddr_t *vaddr)
662 {
663 extern paddr_t avail_start;
664
665 if (paddr >= avail_start)
666 return false;
667 *vaddr = KERNBASE3X + paddr;
668 return true;
669 }
670
671 /*
672 * Allow access to the PROM mapping similar to uvm_kernacc().
673 */
674 int
675 mm_md_kernacc(void *ptr, vm_prot_t prot, bool *handled)
676 {
677
678 if ((vaddr_t)ptr < SUN3X_PROM_BASE || (vaddr_t)ptr > SUN3X_MONEND) {
679 *handled = false;
680 return 0;
681 }
682
683 *handled = true;
684 /* Read in the PROM itself is OK. */
685 if ((prot & VM_PROT_WRITE) == 0)
686 return 0;
687
688 /* PROM data page is OK for read/write. */
689 if ((vaddr_t)ptr >= SUN3X_MONDATA &&
690 (vaddr_t)ptr < SUN3X_MONDATA + PAGE_SIZE)
691 return 0;
692 return EFAULT;
693 }
694
695 #ifdef MODULAR
696 /*
697 * Push any modules loaded by the bootloader etc.
698 */
699 void
700 module_init_md(void)
701 {
702 }
703 #endif
704