machdep.c revision 1.37 1 /* $NetBSD: machdep.c,v 1.37 1998/11/24 16:48:26 kleink 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. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * from: Utah Hdr: machdep.c 1.74 92/12/20
41 * from: @(#)machdep.c 8.10 (Berkeley) 4/20/94
42 */
43
44 #include "opt_ddb.h"
45 #include "opt_uvm.h"
46 #include "opt_sysv.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/map.h>
52 #include <sys/proc.h>
53 #include <sys/buf.h>
54 #include <sys/reboot.h>
55 #include <sys/conf.h>
56 #include <sys/file.h>
57 #include <sys/clist.h>
58 #include <sys/callout.h>
59 #include <sys/device.h>
60 #include <sys/malloc.h>
61 #include <sys/mbuf.h>
62 #include <sys/msgbuf.h>
63 #include <sys/ioctl.h>
64 #include <sys/tty.h>
65 #include <sys/mount.h>
66 #include <sys/user.h>
67 #include <sys/exec.h>
68 #include <sys/core.h>
69 #include <sys/kcore.h>
70 #include <sys/vnode.h>
71 #include <sys/syscallargs.h>
72 #ifdef SYSVMSG
73 #include <sys/msg.h>
74 #endif
75 #ifdef SYSVSEM
76 #include <sys/sem.h>
77 #endif
78 #ifdef SYSVSHM
79 #include <sys/shm.h>
80 #endif
81 #ifdef KGDB
82 #include <sys/kgdb.h>
83 #endif
84
85 #include <vm/vm.h>
86 #include <vm/vm_map.h>
87 #include <vm/vm_kern.h>
88 #include <vm/vm_page.h>
89
90 #if defined(UVM)
91 #include <uvm/uvm.h> /* XXX: not _extern ... need vm_map_create */
92 #endif
93
94 #include <sys/sysctl.h>
95
96 #include <dev/cons.h>
97
98 #include <machine/cpu.h>
99 #include <machine/dvma.h>
100 #include <machine/idprom.h>
101 #include <machine/kcore.h>
102 #include <machine/reg.h>
103 #include <machine/psl.h>
104 #include <machine/pte.h>
105
106 #include <machine/db_machdep.h>
107 #include <ddb/db_sym.h>
108 #include <ddb/db_extern.h>
109
110 #include <sun3/sun3/machdep.h>
111
112 /* Defined in locore.s */
113 extern char kernel_text[];
114 /* Defined by the linker */
115 extern char etext[];
116
117 #if defined(UVM)
118 /* XXX - Gratuitous name changes... */
119 #define kmem_alloc uvm_km_alloc
120 vm_map_t exec_map = NULL;
121 vm_map_t mb_map = NULL;
122 vm_map_t phys_map = NULL;
123 #else
124 vm_map_t buffer_map;
125 #endif
126
127 int physmem;
128 int fputype;
129 caddr_t msgbufaddr;
130
131 /* Virtual page frame for /dev/mem (see mem.c) */
132 vm_offset_t vmmap;
133
134 /*
135 * safepri is a safe priority for sleep to set for a spin-wait
136 * during autoconfiguration or after a panic.
137 */
138 int safepri = PSL_LOWIPL;
139
140 /*
141 * Declare these as initialized data so we can patch them.
142 */
143 int nswbuf = 0;
144 #ifdef NBUF
145 int nbuf = NBUF;
146 #else
147 int nbuf = 0;
148 #endif
149 #ifdef BUFPAGES
150 int bufpages = BUFPAGES;
151 #else
152 int bufpages = 0;
153 #endif
154
155 u_char cpu_machine_id = 0;
156 char *cpu_string = NULL;
157 int cpu_has_vme = 0;
158 int has_iocache = 0;
159
160 static void identifycpu __P((void));
161 static void initcpu __P((void));
162
163 /*
164 * Console initialization: called early on from main,
165 * before vm init or cpu_startup. This system is able
166 * to use the console for output immediately (via PROM)
167 * but can not use it for input until after this point.
168 */
169 void
170 consinit()
171 {
172
173 /*
174 * Switch from the PROM console (output only)
175 * to our own console driver.
176 */
177 cninit();
178
179 #ifdef DDB
180 db_machine_init();
181 {
182 extern int end[];
183 extern char *esym;
184
185 /* symsize, symstart, symend */
186 ddb_init(end[0], end + 1, (int*)esym);
187 }
188 #endif DDB
189
190 /*
191 * Now that the console can do input as well as
192 * output, consider stopping for a debugger.
193 */
194 if (boothowto & RB_KDB) {
195 #ifdef KGDB
196 /* XXX - Ask on console for kgdb_dev? */
197 /* Note: this will just return if kgdb_dev==NODEV */
198 kgdb_connect(1);
199 #else /* KGDB */
200 /* Either DDB or no debugger (just PROM). */
201 Debugger();
202 #endif /* KGDB */
203 }
204 }
205
206 /*
207 * allocsys() - Private routine used by cpu_startup() below.
208 *
209 * Allocate space for system data structures. We are given
210 * a starting virtual address and we return a final virtual
211 * address; along the way we set each data structure pointer.
212 *
213 * We call allocsys() with 0 to find out how much space we want,
214 * allocate that much and fill it with zeroes, and then call
215 * allocsys() again with the correct base virtual address.
216 */
217 #define valloc(name, type, num) \
218 v = (caddr_t)(((name) = (type *)v) + (num))
219 static caddr_t allocsys __P((caddr_t));
220 static caddr_t
221 allocsys(v)
222 register caddr_t v;
223 {
224
225 #ifdef REAL_CLISTS
226 valloc(cfree, struct cblock, nclist);
227 #endif
228 valloc(callout, struct callout, ncallout);
229 #ifdef SYSVSHM
230 valloc(shmsegs, struct shmid_ds, shminfo.shmmni);
231 #endif
232 #ifdef SYSVSEM
233 valloc(sema, struct semid_ds, seminfo.semmni);
234 valloc(sem, struct sem, seminfo.semmns);
235 /* This is pretty disgusting! */
236 valloc(semu, int, (seminfo.semmnu * seminfo.semusz) / sizeof(int));
237 #endif
238 #ifdef SYSVMSG
239 valloc(msgpool, char, msginfo.msgmax);
240 valloc(msgmaps, struct msgmap, msginfo.msgseg);
241 valloc(msghdrs, struct msg, msginfo.msgtql);
242 valloc(msqids, struct msqid_ds, msginfo.msgmni);
243 #endif
244
245 /*
246 * Determine how many buffers to allocate. We allocate
247 * the BSD standard of use 10% of memory for the first 2 Meg,
248 * 5% of remaining. Insure a minimum of 16 buffers.
249 * Allocate 1/2 as many swap buffer headers as file i/o buffers.
250 */
251 if (bufpages == 0) {
252 /* We always have more than 2MB of memory. */
253 bufpages = ((btoc(2 * 1024 * 1024) + physmem) /
254 (20 * CLSIZE));
255 }
256 if (nbuf == 0) {
257 nbuf = bufpages;
258 if (nbuf < 16)
259 nbuf = 16;
260 }
261 if (nswbuf == 0) {
262 nswbuf = (nbuf / 2) &~ 1; /* force even */
263 if (nswbuf > 256)
264 nswbuf = 256; /* sanity */
265 }
266 #if !defined(UVM)
267 valloc(swbuf, struct buf, nswbuf);
268 #endif
269 valloc(buf, struct buf, nbuf);
270 return v;
271 }
272 #undef valloc
273
274 /*
275 * cpu_startup: allocate memory for variable-sized tables,
276 * initialize cpu, and do autoconfiguration.
277 *
278 * This is called early in init_main.c:main(), after the
279 * kernel memory allocator is ready for use, but before
280 * the creation of processes 1,2, and mountroot, etc.
281 */
282 void
283 cpu_startup()
284 {
285 caddr_t v;
286 int sz, i;
287 vm_size_t size;
288 int base, residual;
289 vm_offset_t minaddr, maxaddr;
290
291 /*
292 * Initialize message buffer (for kernel printf).
293 * This is put in physical page zero so it will
294 * always be in the same place after a reboot.
295 * Its mapping was prepared in pmap_bootstrap().
296 * Also, offset some to avoid PROM scribbles.
297 */
298 v = (caddr_t) KERNBASE;
299 msgbufaddr = (caddr_t)(v + MSGBUFOFF);
300 initmsgbuf(msgbufaddr, MSGBUFSIZE);
301
302 /*
303 * Good {morning,afternoon,evening,night}.
304 */
305 printf(version);
306 identifycpu();
307 initfpu(); /* also prints FPU type */
308
309 size = ptoa(physmem);
310 printf("real mem = %dK (0x%lx)\n", (size >> 10), size);
311
312 /*
313 * Find out how much space we need, allocate it,
314 * and then give everything true virtual addresses.
315 */
316 sz = (int)allocsys((caddr_t)0);
317 if ((v = (caddr_t)kmem_alloc(kernel_map, round_page(sz))) == 0)
318 panic("startup: no room for tables");
319 if (allocsys(v) - v != sz)
320 panic("startup: table size inconsistency");
321
322 /*
323 * Now allocate buffers proper. They are different than the above
324 * in that they usually occupy more virtual memory than physical.
325 */
326 size = MAXBSIZE * nbuf;
327 #if defined(UVM)
328 if (uvm_map(kernel_map, (vm_offset_t *) &buffers, round_page(size),
329 NULL, UVM_UNKNOWN_OFFSET,
330 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
331 UVM_ADV_NORMAL, 0)) != KERN_SUCCESS)
332 panic("startup: cannot allocate VM for buffers");
333 minaddr = (vm_offset_t)buffers;
334 #else
335 buffer_map = kmem_suballoc(kernel_map, (vm_offset_t *)&buffers,
336 &maxaddr, size, TRUE);
337 minaddr = (vm_offset_t)buffers;
338 if (vm_map_find(buffer_map, vm_object_allocate(size), (vm_offset_t)0,
339 &minaddr, size, FALSE) != KERN_SUCCESS)
340 panic("startup: cannot allocate buffers");
341 #endif /* UVM */
342 if ((bufpages / nbuf) >= btoc(MAXBSIZE)) {
343 /* don't want to alloc more physical mem than needed */
344 bufpages = btoc(MAXBSIZE) * nbuf;
345 }
346 base = bufpages / nbuf;
347 residual = bufpages % nbuf;
348 for (i = 0; i < nbuf; i++) {
349 #if defined(UVM)
350 vm_size_t curbufsize;
351 vm_offset_t curbuf;
352 struct vm_page *pg;
353
354 /*
355 * Each buffer has MAXBSIZE bytes of VM space allocated. Of
356 * that MAXBSIZE space, we allocate and map (base+1) pages
357 * for the first "residual" buffers, and then we allocate
358 * "base" pages for the rest.
359 */
360 curbuf = (vm_offset_t) buffers + (i * MAXBSIZE);
361 curbufsize = CLBYTES * ((i < residual) ? (base+1) : base);
362
363 while (curbufsize) {
364 pg = uvm_pagealloc(NULL, 0, NULL);
365 if (pg == NULL)
366 panic("cpu_startup: not enough memory for "
367 "buffer cache");
368 #if defined(PMAP_NEW)
369 pmap_kenter_pgs(curbuf, &pg, 1);
370 #else
371 pmap_enter(kernel_map->pmap, curbuf,
372 VM_PAGE_TO_PHYS(pg), VM_PROT_ALL, TRUE);
373 #endif
374 curbuf += PAGE_SIZE;
375 curbufsize -= PAGE_SIZE;
376 }
377 #else /* ! UVM */
378 vm_size_t curbufsize;
379 vm_offset_t curbuf;
380
381 /*
382 * First <residual> buffers get (base+1) physical pages
383 * allocated for them. The rest get (base) physical pages.
384 *
385 * The rest of each buffer occupies virtual space,
386 * but has no physical memory allocated for it.
387 */
388 curbuf = (vm_offset_t)buffers + i * MAXBSIZE;
389 curbufsize = CLBYTES * (i < residual ? base+1 : base);
390 vm_map_pageable(buffer_map, curbuf, curbuf+curbufsize, FALSE);
391 vm_map_simplify(buffer_map, curbuf);
392 #endif /* UVM */
393 }
394
395 /*
396 * Allocate a submap for exec arguments. This map effectively
397 * limits the number of processes exec'ing at any time.
398 */
399 #if defined(UVM)
400 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
401 16*NCARGS, TRUE, FALSE, NULL);
402 #else
403 exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
404 16*NCARGS, TRUE);
405 #endif
406
407 /*
408 * We don't use a submap for physio, and use a separate map
409 * for DVMA allocations. Our vmapbuf just maps pages into
410 * the kernel map (any kernel mapping is OK) and then the
411 * device drivers clone the kernel mappings into DVMA space.
412 */
413
414 /*
415 * Finally, allocate mbuf cluster submap.
416 */
417 #if defined(UVM)
418 mb_map = uvm_km_suballoc(kernel_map, (vm_offset_t *)&mbutl, &maxaddr,
419 VM_MBUF_SIZE, FALSE, FALSE, NULL);
420 #else
421 mb_map = kmem_suballoc(kernel_map, (vm_offset_t *)&mbutl, &maxaddr,
422 VM_MBUF_SIZE, FALSE);
423 #endif
424
425 /*
426 * Initialize callouts
427 */
428 callfree = callout;
429 for (i = 1; i < ncallout; i++)
430 callout[i-1].c_next = &callout[i];
431 callout[i-1].c_next = NULL;
432
433 #if defined(UVM)
434 size = ptoa(uvmexp.free);
435 #else
436 size = ptoa(cnt.v_free_count);
437 #endif
438 printf("avail mem = %dK (0x%lx)\n", (size >> 10), size);
439 printf("using %d buffers containing %d bytes of memory\n",
440 nbuf, bufpages * CLBYTES);
441
442 /*
443 * Tell the VM system that writing to kernel text isn't allowed.
444 * If we don't, we might end up COW'ing the text segment!
445 */
446 #if defined(UVM)
447 if (uvm_map_protect(kernel_map, (vm_offset_t) kernel_text,
448 m68k_trunc_page((vm_offset_t) etext),
449 UVM_PROT_READ|UVM_PROT_EXEC, TRUE) != KERN_SUCCESS)
450 panic("can't protect kernel text");
451 #else
452 if (vm_map_protect(kernel_map, (vm_offset_t) kernel_text,
453 m68k_trunc_page((vm_offset_t) etext),
454 VM_PROT_READ|VM_PROT_EXECUTE, TRUE) != KERN_SUCCESS)
455 panic("can't protect kernel text");
456 #endif
457
458 /*
459 * Allocate a virtual page (for use by /dev/mem)
460 * This page is handed to pmap_enter() therefore
461 * it has to be in the normal kernel VA range.
462 */
463 #if defined(UVM)
464 vmmap = uvm_km_valloc_wait(kernel_map, NBPG);
465 #else
466 vmmap = kmem_alloc_wait(kernel_map, NBPG);
467 #endif
468
469 /*
470 * Create the DVMA maps.
471 */
472 dvma_init();
473
474 /*
475 * Set up CPU-specific registers, cache, etc.
476 */
477 initcpu();
478
479 /*
480 * Set up buffers, so they can be used to read disk labels.
481 */
482 bufinit();
483
484 /*
485 * Configure the system.
486 */
487 configure();
488 }
489
490 /*
491 * Set registers on exec.
492 */
493 void
494 setregs(p, pack, stack)
495 struct proc *p;
496 struct exec_package *pack;
497 u_long stack;
498 {
499 struct trapframe *tf = (struct trapframe *)p->p_md.md_regs;
500
501 tf->tf_sr = PSL_USERSET;
502 tf->tf_pc = pack->ep_entry & ~1;
503 tf->tf_regs[D0] = 0;
504 tf->tf_regs[D1] = 0;
505 tf->tf_regs[D2] = 0;
506 tf->tf_regs[D3] = 0;
507 tf->tf_regs[D4] = 0;
508 tf->tf_regs[D5] = 0;
509 tf->tf_regs[D6] = 0;
510 tf->tf_regs[D7] = 0;
511 tf->tf_regs[A0] = 0;
512 tf->tf_regs[A1] = 0;
513 tf->tf_regs[A2] = (int)PS_STRINGS;
514 tf->tf_regs[A3] = 0;
515 tf->tf_regs[A4] = 0;
516 tf->tf_regs[A5] = 0;
517 tf->tf_regs[A6] = 0;
518 tf->tf_regs[SP] = stack;
519
520 /* restore a null state frame */
521 p->p_addr->u_pcb.pcb_fpregs.fpf_null = 0;
522 if (fputype)
523 m68881_restore(&p->p_addr->u_pcb.pcb_fpregs);
524
525 p->p_md.md_flags = 0;
526 }
527
528 /*
529 * Info for CTL_HW
530 */
531 char machine[16] = MACHINE; /* from <machine/param.h> */
532 char cpu_model[120];
533
534 /*
535 * XXX - Should empirically estimate the divisor...
536 * Note that the value of delay_divisor is roughly
537 * 2048 / cpuclock (where cpuclock is in MHz).
538 */
539 int delay_divisor = 62; /* assume the fastest (33 MHz) */
540
541 void
542 identifycpu()
543 {
544 u_char machtype;
545
546 machtype = identity_prom.idp_machtype;
547 if ((machtype & IDM_ARCH_MASK) != IDM_ARCH_SUN3X) {
548 printf("Bad IDPROM arch!\n");
549 sunmon_abort();
550 }
551
552 cpu_machine_id = machtype;
553 switch (cpu_machine_id) {
554
555 case SUN3X_MACH_80:
556 cpu_string = "80"; /* Hydra */
557 delay_divisor = 102; /* 20 MHz */
558 cpu_has_vme = FALSE;
559 break;
560
561 case SUN3X_MACH_470:
562 cpu_string = "470"; /* Pegasus */
563 delay_divisor = 62; /* 33 MHz */
564 cpu_has_vme = TRUE;
565 break;
566
567 default:
568 printf("unknown sun3x model\n");
569 sunmon_abort();
570 }
571
572 /* Other stuff? (VAC, mc6888x version, etc.) */
573 sprintf(cpu_model, "Sun-3X (3/%s)", cpu_string);
574
575 printf("Model: %s\n", cpu_model);
576 }
577
578 /*
579 * machine dependent system variables.
580 */
581 int
582 cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
583 int *name;
584 u_int namelen;
585 void *oldp;
586 size_t *oldlenp;
587 void *newp;
588 size_t newlen;
589 struct proc *p;
590 {
591 int error;
592 dev_t consdev;
593
594 /* all sysctl names at this level are terminal */
595 if (namelen != 1)
596 return (ENOTDIR); /* overloaded */
597
598 switch (name[0]) {
599 case CPU_CONSDEV:
600 if (cn_tab != NULL)
601 consdev = cn_tab->cn_dev;
602 else
603 consdev = NODEV;
604 error = sysctl_rdstruct(oldp, oldlenp, newp,
605 &consdev, sizeof consdev);
606 break;
607
608 #if 0 /* XXX - Not yet... */
609 case CPU_ROOT_DEVICE:
610 error = sysctl_rdstring(oldp, oldlenp, newp, root_device);
611 break;
612
613 case CPU_BOOTED_KERNEL:
614 error = sysctl_rdstring(oldp, oldlenp, newp, booted_kernel);
615 break;
616 #endif
617
618 default:
619 error = EOPNOTSUPP;
620 }
621 return (error);
622 }
623
624 /* See: sig_machdep.c */
625
626 /*
627 * Do a sync in preparation for a reboot.
628 * XXX - This could probably be common code.
629 * XXX - And now, most of it is in vfs_shutdown()
630 * XXX - Put waittime checks in there too?
631 */
632 int waittime = -1; /* XXX - Who else looks at this? -gwr */
633 static void
634 reboot_sync __P((void))
635 {
636
637 /* Check waittime here to localize its use to this function. */
638 if (waittime >= 0)
639 return;
640 waittime = 0;
641 vfs_shutdown();
642 }
643
644 /*
645 * Common part of the BSD and SunOS reboot system calls.
646 */
647 __dead void
648 cpu_reboot(howto, user_boot_string)
649 int howto;
650 char *user_boot_string;
651 {
652 /* Note: this string MUST be static! */
653 static char bootstr[128];
654 char *p;
655
656 /* If system is cold, just halt. (early panic?) */
657 if (cold)
658 goto haltsys;
659
660 /* Un-blank the screen if appropriate. */
661 cnpollc(1);
662
663 if ((howto & RB_NOSYNC) == 0) {
664 reboot_sync();
665 /*
666 * If we've been adjusting the clock, the todr
667 * will be out of synch; adjust it now.
668 *
669 * XXX - However, if the kernel has been sitting in ddb,
670 * the time will be way off, so don't set the HW clock!
671 * XXX - Should do sanity check against HW clock. -gwr
672 */
673 /* resettodr(); */
674 }
675
676 /* Disable interrupts. */
677 splhigh();
678
679 /* Write out a crash dump if asked. */
680 if (howto & RB_DUMP)
681 dumpsys();
682
683 /* run any shutdown hooks */
684 doshutdownhooks();
685
686 if (howto & RB_HALT) {
687 haltsys:
688 printf("Kernel halted.\n");
689 #if 0
690 /*
691 * This calls the PROM monitor "exit_to_mon" function
692 * which appears to have problems... SunOS uses the
693 * "abort" function when you halt (bug work-around?)
694 * so we might as well do the same.
695 */
696 sunmon_halt(); /* provokes PROM monitor bug */
697 #else
698 sunmon_abort();
699 #endif
700 }
701
702 /*
703 * Automatic reboot.
704 */
705 if (user_boot_string)
706 strncpy(bootstr, user_boot_string, sizeof(bootstr));
707 else {
708 /*
709 * Build our own boot string with an empty
710 * boot device/file and (maybe) some flags.
711 * The PROM will supply the device/file name.
712 */
713 p = bootstr;
714 *p = '\0';
715 if (howto & (RB_KDB|RB_ASKNAME|RB_SINGLE)) {
716 /* Append the boot flags. */
717 *p++ = ' ';
718 *p++ = '-';
719 if (howto & RB_KDB)
720 *p++ = 'd';
721 if (howto & RB_ASKNAME)
722 *p++ = 'a';
723 if (howto & RB_SINGLE)
724 *p++ = 's';
725 *p = '\0';
726 }
727 }
728 printf("Kernel rebooting...\n");
729 sunmon_reboot(bootstr);
730 for (;;) ;
731 /*NOTREACHED*/
732 }
733
734 /*
735 * These variables are needed by /sbin/savecore
736 */
737 u_long dumpmag = 0x8fca0101; /* magic number */
738 int dumpsize = 0; /* pages */
739 long dumplo = 0; /* blocks */
740
741 /*
742 * This is called by main to set dumplo, dumpsize.
743 * Dumps always skip the first CLBYTES of disk space
744 * in case there might be a disk label stored there.
745 * If there is extra space, put dump at the end to
746 * reduce the chance that swapping trashes it.
747 */
748 void
749 cpu_dumpconf()
750 {
751 int nblks; /* size of dump area */
752 int maj;
753 int (*getsize)__P((dev_t));
754
755 /* Validate space in page zero for the kcore header. */
756 if (MSGBUFOFF < (sizeof(kcore_seg_t) + sizeof(cpu_kcore_hdr_t)))
757 panic("cpu_dumpconf: MSGBUFOFF too small");
758
759 if (dumpdev == NODEV)
760 return;
761
762 maj = major(dumpdev);
763 if (maj < 0 || maj >= nblkdev)
764 panic("dumpconf: bad dumpdev=0x%x", dumpdev);
765 getsize = bdevsw[maj].d_psize;
766 if (getsize == NULL)
767 return;
768 nblks = (*getsize)(dumpdev);
769 if (nblks <= ctod(1))
770 return;
771
772 /* Position dump image near end of space, page aligned. */
773 dumpsize = physmem; /* pages */
774 dumplo = nblks - ctod(dumpsize);
775 dumplo &= ~(ctod(1)-1);
776
777 /* If it does not fit, truncate it by moving dumplo. */
778 /* Note: Must force signed comparison. */
779 if (dumplo < ((long)ctod(1))) {
780 dumplo = ctod(1);
781 dumpsize = dtoc(nblks - dumplo);
782 }
783 }
784
785 /* Note: gdb looks for "dumppcb" in a kernel crash dump. */
786 struct pcb dumppcb;
787
788 /*
789 * Write a crash dump. The format while in swap is:
790 * kcore_seg_t cpu_hdr;
791 * cpu_kcore_hdr_t cpu_data;
792 * padding (NBPG-sizeof(kcore_seg_t))
793 * pagemap (2*NBPG)
794 * physical memory...
795 */
796 void
797 dumpsys()
798 {
799 struct bdevsw *dsw;
800 kcore_seg_t *kseg_p;
801 cpu_kcore_hdr_t *chdr_p;
802 struct sun3x_kcore_hdr *sh;
803 phys_ram_seg_t *crs_p;
804 char *vaddr;
805 vm_offset_t paddr;
806 int psize, todo, seg, segsz;
807 daddr_t blkno;
808 int error = 0;
809
810 msgbufenabled = 0;
811 if (dumpdev == NODEV)
812 return;
813
814 /*
815 * For dumps during autoconfiguration,
816 * if dump device has already configured...
817 */
818 if (dumpsize == 0)
819 cpu_dumpconf();
820 if (dumplo <= 0) {
821 printf("\ndump to dev %u,%u not possible\n", major(dumpdev),
822 minor(dumpdev));
823 return;
824 }
825 savectx(&dumppcb);
826
827 dsw = &bdevsw[major(dumpdev)];
828 psize = (*(dsw->d_psize))(dumpdev);
829 if (psize == -1) {
830 printf("dump area unavailable\n");
831 return;
832 }
833
834 printf("\ndumping to dev %u,%u offset %ld\n", major(dumpdev),
835 minor(dumpdev), dumplo);
836
837 /*
838 * We put the dump header is in physical page zero,
839 * so there is no extra work here to write it out.
840 * All we do is initialize the header.
841 */
842
843 /* Set pointers to all three parts. */
844 kseg_p = (kcore_seg_t *)KERNBASE;
845 chdr_p = (cpu_kcore_hdr_t *) (kseg_p + 1);
846 sh = &chdr_p->un._sun3x;
847
848 /* Fill in kcore_seg_t part. */
849 CORE_SETMAGIC(*kseg_p, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
850 kseg_p->c_size = sizeof(*chdr_p);
851
852 /* Fill in cpu_kcore_hdr_t part. */
853 /* Can NOT use machine[] as the name! */
854 strncpy(chdr_p->name, "sun3x", sizeof(chdr_p->name));
855 chdr_p->page_size = NBPG;
856 chdr_p->kernbase = KERNBASE;
857
858 /* Fill in the sun3x_kcore_hdr part. */
859 pmap_kcore_hdr(sh);
860
861 /*
862 * Now dump physical memory. Note that physical memory
863 * might NOT be congiguous, so do it by segments.
864 */
865
866 blkno = dumplo;
867 todo = dumpsize; /* pages */
868 vaddr = (char*)vmmap; /* Borrow /dev/mem VA */
869
870 for (seg = 0; seg < SUN3X_NPHYS_RAM_SEGS; seg++) {
871 crs_p = &sh->ram_segs[seg];
872 paddr = crs_p->start;
873 segsz = crs_p->size;
874 /*
875 * Our header lives in the first little bit of
876 * physical memory (not written separately), so
877 * we have to adjust the first ram segment size
878 * and start address to reflect the stolen RAM.
879 * (Nothing interesing in that RAM anyway 8^).
880 */
881 if (seg == 0) {
882 int adj = sizeof(*kseg_p) + sizeof(*chdr_p);
883 crs_p->start += adj;
884 crs_p->size -= adj;
885 }
886
887 while (todo && (segsz > 0)) {
888
889 /* Print pages left after every 16. */
890 if ((todo & 0xf) == 0)
891 printf("\r%4d", todo);
892
893 /* Make a temporary mapping for the page. */
894 pmap_enter(pmap_kernel(), vmmap, paddr | PMAP_NC,
895 VM_PROT_READ, FALSE);
896 error = (*dsw->d_dump)(dumpdev, blkno, vaddr, NBPG);
897 pmap_remove(pmap_kernel(), vmmap, vmmap + NBPG);
898 if (error)
899 goto fail;
900 paddr += NBPG;
901 segsz -= NBPG;
902 blkno += btodb(NBPG);
903 todo--;
904 }
905 }
906 printf("\rdump succeeded\n");
907 return;
908 fail:
909 printf(" dump error=%d\n", error);
910 }
911
912 static void
913 initcpu()
914 {
915 /* XXX: Enable RAM parity/ECC checking? */
916 /* XXX: parityenable(); */
917
918 #ifdef HAVECACHE
919 cache_enable();
920 #endif
921 }
922
923 /* straptrap() in trap.c */
924
925 /* from hp300: badaddr() */
926 /* peek_byte(), peek_word() moved to bus_subr.c */
927
928 /* XXX: parityenable() ? */
929 /* regdump() moved to regdump.c */
930
931 /*
932 * cpu_exec_aout_makecmds():
933 * cpu-dependent a.out format hook for execve().
934 *
935 * Determine if the given exec package refers to something which we
936 * understand and, if so, set up the vmcmds for it.
937 */
938 int
939 cpu_exec_aout_makecmds(p, epp)
940 struct proc *p;
941 struct exec_package *epp;
942 {
943 return ENOEXEC;
944 }
945