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