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