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