machdep.c revision 1.38 1 /* $NetBSD: machdep.c,v 1.38 2004/03/24 15:34:49 atatat Exp $ */
2
3 /*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Tohru Nishimura.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
40
41 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.38 2004/03/24 15:34:49 atatat Exp $");
42
43 #include "opt_ddb.h"
44 #include "opt_kgdb.h"
45 #include "opt_compat_sunos.h"
46 #include "opt_panicbutton.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.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/device.h>
57 #include <sys/malloc.h>
58 #include <sys/mbuf.h>
59 #include <sys/msgbuf.h>
60 #include <sys/ioctl.h>
61 #include <sys/tty.h>
62 #include <sys/mount.h>
63 #include <sys/user.h>
64 #include <sys/exec.h>
65 #include <sys/core.h>
66 #include <sys/kcore.h>
67 #include <sys/vnode.h>
68 #include <sys/sa.h>
69 #include <sys/syscallargs.h>
70 #include <sys/ksyms.h>
71 #ifdef KGDB
72 #include <sys/kgdb.h>
73 #endif
74 #include <sys/boot_flag.h>
75
76 #include <uvm/uvm_extern.h>
77
78 #include <sys/sysctl.h>
79
80 #include <machine/cpu.h>
81 #include <machine/reg.h>
82 #include <machine/psl.h>
83 #include <machine/pte.h>
84 #include <machine/kcore.h> /* XXX should be pulled in by sys/kcore.h */
85
86 #include <dev/cons.h>
87
88 #if defined(DDB)
89 #include <machine/db_machdep.h>
90 #include <ddb/db_sym.h>
91 #include <ddb/db_extern.h>
92 #endif
93
94 #include "ksyms.h"
95
96 /*
97 * Info for CTL_HW
98 */
99 char machine[] = MACHINE;
100 char cpu_model[60];
101
102 /* Our exported CPU info; we can have only one. */
103 struct cpu_info cpu_info_store;
104
105 extern char kernel_text[];
106 extern char etext[];
107
108 struct vm_map *exec_map = NULL;
109 struct vm_map *mb_map = NULL;
110 struct vm_map *phys_map = NULL;
111
112 caddr_t msgbufaddr;
113 int maxmem; /* max memory per process */
114 int physmem; /* set by locore */
115 /*
116 * safepri is a safe priority for sleep to set for a spin-wait
117 * during autoconfiguration or after a panic.
118 */
119 int safepri = PSL_LOWIPL;
120
121 void luna68k_init __P((void));
122 void identifycpu __P((void));
123 void dumpsys __P((void));
124
125 void straytrap __P((int, u_short));
126 void nmihand __P((struct frame));
127
128 int cpu_dumpsize __P((void));
129 int cpu_dump __P((int (*)(dev_t, daddr_t, caddr_t, size_t), daddr_t *));
130 void cpu_init_kcore_hdr __P((void));
131
132 /*
133 * Machine-independent crash dump header info.
134 */
135 cpu_kcore_hdr_t cpu_kcore_hdr;
136
137 int machtype; /* model: 1 for LUNA-1, 2 for LUNA-2 */
138 int sysconsole; /* console: 0 for ttya, 1 for video */
139
140 extern struct consdev syscons;
141 extern void omfb_cnattach __P((void));
142 extern void ws_cnattach __P((void));
143 extern void syscnattach __P((int));
144
145 /*
146 * On the 68020/68030, the value of delay_divisor is roughly
147 * 2048 / cpuspeed (where cpuspeed is in MHz).
148 *
149 * On the 68040/68060(?), the value of delay_divisor is roughly
150 * 759 / cpuspeed (where cpuspeed is in MHz).
151 * XXX -- is the above formula correct?
152 */
153 int cpuspeed = 25; /* only used for printing later */
154 int delay_divisor = 300; /* for delay() loop count */
155
156 /*
157 * Early initialization, before main() is called.
158 */
159 void
160 luna68k_init()
161 {
162 volatile unsigned char *pio0 = (void *)0x49000000;
163 int sw1, i;
164 char *cp;
165 extern char bootarg[64];
166
167 extern paddr_t avail_start, avail_end;
168
169 /*
170 * Tell the VM system about available physical memory. The
171 * luna68k only has one segment.
172 */
173 uvm_page_physload(atop(avail_start), atop(avail_end),
174 atop(avail_start), atop(avail_end), VM_FREELIST_DEFAULT);
175
176 /*
177 * Initialize error message buffer (at end of core).
178 * avail_end was pre-decremented in pmap_bootstrap to compensate.
179 */
180 for (i = 0; i < btoc(MSGBUFSIZE); i++)
181 pmap_enter(pmap_kernel(), (vaddr_t)msgbufaddr + i * PAGE_SIZE,
182 avail_end + i * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE,
183 VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
184 pmap_update(pmap_kernel());
185 initmsgbuf(msgbufaddr, m68k_round_page(MSGBUFSIZE));
186
187
188 pio0[3] = 0xb6;
189 pio0[2] = 1 << 6; /* enable parity check */
190 pio0[3] = 0xb6;
191 sw1 = pio0[0]; /* dipssw1 value */
192 sw1 ^= 0xff;
193 sysconsole = !(sw1 & 0x2); /* console selection */
194
195 boothowto = 0;
196 i = 0;
197 /*
198 * 'bootarg' has;
199 * "<args of x command> ENADDR=<addr> HOST=<host> SERVER=<name>"
200 * where <addr> is MAC address of which network loader used (not
201 * necessarily same as one at 0x4101.FFE0), <host> and <name>
202 * are the values of HOST and SERVER environment variables,
203 *
204 * NetBSD/luna68k cares only the first argment; any of "sda".
205 */
206 for (cp = bootarg; *cp != ' '; cp++) {
207 BOOT_FLAG(*cp, boothowto);
208 if (i++ >= sizeof(bootarg))
209 break;
210 }
211 #if 0 /* overload 1:sw1, which now means 'go ROM monitor' after poweron */
212 if (boothowto == 0)
213 boothowto = (sw1 & 0x1) ? RB_SINGLE : 0;
214 #endif
215 }
216
217 /*
218 * Console initialization: called early on from main,
219 */
220 void
221 consinit()
222 {
223 if (sysconsole == 0)
224 syscnattach(0);
225 else {
226 omfb_cnattach();
227 ws_cnattach();
228 }
229
230 #if NKSYMS || defined(DDB) || defined(LKM)
231 {
232 extern int end;
233 extern int *esym;
234
235 ksyms_init(*(int *)&end, ((int *)&end) + 1, esym);
236 }
237 #endif
238 #ifdef DDB
239 if (boothowto & RB_KDB)
240 cpu_Debugger();
241 #endif
242 }
243
244 /*
245 * cpu_startup: allocate memory for variable-sized tables.
246 */
247 void
248 cpu_startup()
249 {
250 vaddr_t minaddr, maxaddr;
251 char pbuf[9];
252 extern void greeting __P((void));
253
254 if (fputype != FPU_NONE)
255 m68k_make_fpu_idle_frame();
256
257 /*
258 * Initialize the kernel crash dump header.
259 */
260 cpu_init_kcore_hdr();
261
262 /*
263 * Good {morning,afternoon,evening,night}.
264 */
265 printf(version);
266 identifycpu();
267
268 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
269 printf("total memory = %s\n", pbuf);
270
271 minaddr = 0;
272 /*
273 * Allocate a submap for exec arguments. This map effectively
274 * limits the number of processes exec'ing at any time.
275 */
276 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
277 16*NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
278
279 /*
280 * Allocate a submap for physio
281 */
282 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
283 VM_PHYS_SIZE, 0, FALSE, NULL);
284
285 /*
286 * Finally, allocate mbuf cluster submap.
287 */
288 mb_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
289 nmbclusters * mclbytes, VM_MAP_INTRSAFE,
290 FALSE, NULL);
291
292 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
293 printf("avail memory = %s\n", pbuf);
294
295 /*
296 * Tell the VM system that the area before the text segment
297 * is invalid.
298 *
299 * XXX Should just change KERNBASE and VM_MIN_KERNEL_ADDRESS,
300 * XXX but not right now.
301 */
302 if (uvm_map_protect(kernel_map, 0, round_page((vaddr_t)&kernel_text),
303 UVM_PROT_NONE, TRUE) != 0)
304 panic("can't mark pre-text pages off-limits");
305
306 /*
307 * Tell the VM system that writing to kernel text isn't allowed.
308 * If we don't, we might end up COW'ing the text segment!
309 */
310 if (uvm_map_protect(kernel_map, trunc_page((vaddr_t)&kernel_text),
311 trunc_page((vaddr_t)&etext), UVM_PROT_READ|UVM_PROT_EXEC, TRUE)
312 != 0)
313 panic("can't protect kernel text");
314
315 /*
316 * Say "Hi" to the world
317 */
318 greeting();
319 }
320
321 /*
322 * Set registers on exec.
323 */
324 void
325 setregs(l, pack, stack)
326 struct lwp *l;
327 struct exec_package *pack;
328 u_long stack;
329 {
330 struct frame *frame = (struct frame *)l->l_md.md_regs;
331 extern int fputype;
332
333 frame->f_sr = PSL_USERSET;
334 frame->f_pc = pack->ep_entry & ~1;
335 frame->f_regs[D0] = 0;
336 frame->f_regs[D1] = 0;
337 frame->f_regs[D2] = 0;
338 frame->f_regs[D3] = 0;
339 frame->f_regs[D4] = 0;
340 frame->f_regs[D5] = 0;
341 frame->f_regs[D6] = 0;
342 frame->f_regs[D7] = 0;
343 frame->f_regs[A0] = 0;
344 frame->f_regs[A1] = 0;
345 frame->f_regs[A2] = (int)l->l_proc->p_psstr;
346 frame->f_regs[A3] = 0;
347 frame->f_regs[A4] = 0;
348 frame->f_regs[A5] = 0;
349 frame->f_regs[A6] = 0;
350 frame->f_regs[SP] = stack;
351
352 /* restore a null state frame */
353 l->l_addr->u_pcb.pcb_fpregs.fpf_null = 0;
354 if (fputype)
355 m68881_restore(&l->l_addr->u_pcb.pcb_fpregs);
356 }
357
358 void
359 identifycpu()
360 {
361 extern int cputype;
362 char *cpu;
363
364 bzero(cpu_model, sizeof(cpu_model));
365 switch (cputype) {
366 case CPU_68030:
367 cpu = "MC68030 CPU+MMU, MC68882 FPU";
368 machtype = LUNA_I;
369 cpuspeed = 20; delay_divisor = 102; /* 20MHz 68030 */
370 hz = 60;
371 break;
372 #if defined(M68040)
373 case CPU_68040:
374 cpu = "MC68040 CPU+MMU+FPU, 4k on-chip physical I/D caches";
375 machtype = LUNA_II;
376 cpuspeed = 25; delay_divisor = 300; /* 25MHz 68040 */
377 break;
378 #endif
379 default:
380 panic("unknown CPU type");
381 }
382 strcpy(cpu_model, cpu);
383 printf("%s\n", cpu_model);
384 }
385
386 /*
387 * machine dependent system variables.
388 */
389 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup")
390 {
391
392 sysctl_createv(clog, 0, NULL, NULL,
393 CTLFLAG_PERMANENT,
394 CTLTYPE_NODE, "machdep", NULL,
395 NULL, 0, NULL, 0,
396 CTL_MACHDEP, CTL_EOL);
397
398 sysctl_createv(clog, 0, NULL, NULL,
399 CTLFLAG_PERMANENT,
400 CTLTYPE_STRUCT, "console_device", NULL,
401 sysctl_consdev, 0, NULL, sizeof(dev_t),
402 CTL_MACHDEP, CPU_CONSDEV, CTL_EOL);
403 }
404
405 int waittime = -1;
406
407 void
408 cpu_reboot(howto, bootstr)
409 volatile int howto; /* XXX to shutup GCC XXX */
410 char *bootstr;
411 {
412 extern void doboot __P((void));
413
414 /* take a snap shot before clobbering any registers */
415 if (curlwp && curlwp->l_addr)
416 savectx(&curlwp->l_addr->u_pcb);
417
418 /* If system is hold, just halt. */
419 if (cold) {
420 howto |= RB_HALT;
421 goto haltsys;
422 }
423
424 boothowto = howto;
425 if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
426 waittime = 0;
427 vfs_shutdown();
428 /*
429 * If we've been adjusting the clock, the todr
430 * will be out of synch; adjust it now.
431 */
432 resettodr();
433 }
434
435 /* Disable interrupts. */
436 splhigh();
437
438 /* If rebooting and a dump is requested, do it. */
439 if (howto & RB_DUMP)
440 dumpsys();
441
442 haltsys:
443 /* Run any shutdown hooks. */
444 doshutdownhooks();
445
446 /* Finally, halt/reboot the system. */
447 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
448 u_int8_t *pio = (void *)0x4d000000;
449
450 printf("power is going down.\n");
451 DELAY(100000);
452 pio[3] = 0x94;
453 pio[2] = 0 << 4;
454 for (;;) /* NOP */;
455 }
456 if (howto & RB_HALT) {
457 printf("System halted. Hit any key to reboot.\n\n");
458 (void)cngetc();
459 }
460
461 printf("rebooting...\n");
462 DELAY(100000);
463 doboot();
464 /*NOTREACHED*/
465 while (1) ;
466 }
467
468 /*
469 * Initialize the kernel crash dump header.
470 */
471 void
472 cpu_init_kcore_hdr()
473 {
474 cpu_kcore_hdr_t *h = &cpu_kcore_hdr;
475 struct m68k_kcore_hdr *m = &h->un._m68k;
476 extern char end[];
477
478 bzero(&cpu_kcore_hdr, sizeof(cpu_kcore_hdr));
479
480 /*
481 * Initialize the `dispatcher' portion of the header.
482 */
483 strcpy(h->name, machine);
484 h->page_size = PAGE_SIZE;
485 h->kernbase = KERNBASE;
486
487 /*
488 * Fill in information about our MMU configuration.
489 */
490 m->mmutype = mmutype;
491 m->sg_v = SG_V;
492 m->sg_frame = SG_FRAME;
493 m->sg_ishift = SG_ISHIFT;
494 m->sg_pmask = SG_PMASK;
495 m->sg40_shift1 = SG4_SHIFT1;
496 m->sg40_mask2 = SG4_MASK2;
497 m->sg40_shift2 = SG4_SHIFT2;
498 m->sg40_mask3 = SG4_MASK3;
499 m->sg40_shift3 = SG4_SHIFT3;
500 m->sg40_addr1 = SG4_ADDR1;
501 m->sg40_addr2 = SG4_ADDR2;
502 m->pg_v = PG_V;
503 m->pg_frame = PG_FRAME;
504
505 /*
506 * Initialize pointer to kernel segment table.
507 */
508 m->sysseg_pa = (u_int32_t)(pmap_kernel()->pm_stpa);
509
510 /*
511 * Initialize relocation value such that:
512 *
513 * pa = (va - KERNBASE) + reloc
514 *
515 * Since we're linked and loaded at the same place,
516 * and the kernel is mapped va == pa, this is 0.
517 */
518 m->reloc = 0;
519
520 /*
521 * Define the end of the relocatable range.
522 */
523 m->relocend = (u_int32_t)end;
524
525 /*
526 * The luna68k has one contiguous memory segment.
527 */
528 m->ram_segs[0].start = 0 /* lowram */;
529 m->ram_segs[0].size = ctob(physmem);
530 }
531
532 /*
533 * Compute the size of the machine-dependent crash dump header.
534 * Returns size in disk blocks.
535 */
536 int
537 cpu_dumpsize()
538 {
539 int size;
540
541 size = ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t));
542 return (btodb(roundup(size, dbtob(1))));
543 }
544
545 /*
546 * Called by dumpsys() to dump the machine-dependent header.
547 */
548 int
549 cpu_dump(dump, blknop)
550 int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
551 daddr_t *blknop;
552 {
553 int buf[dbtob(1) / sizeof(int)];
554 cpu_kcore_hdr_t *chdr;
555 kcore_seg_t *kseg;
556 int error;
557
558 kseg = (kcore_seg_t *)buf;
559 chdr = (cpu_kcore_hdr_t *)&buf[ALIGN(sizeof(kcore_seg_t)) /
560 sizeof(int)];
561
562 /* Create the segment header. */
563 CORE_SETMAGIC(*kseg, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
564 kseg->c_size = dbtob(1) - ALIGN(sizeof(kcore_seg_t));
565
566 bcopy(&cpu_kcore_hdr, chdr, sizeof(cpu_kcore_hdr_t));
567 error = (*dump)(dumpdev, *blknop, (caddr_t)buf, sizeof(buf));
568 *blknop += btodb(sizeof(buf));
569 return (error);
570 }
571
572 /*
573 * These variables are needed by /sbin/savecore
574 */
575 u_int32_t dumpmag = 0x8fca0101; /* magic number */
576 int dumpsize = 0; /* pages */
577 long dumplo = 0; /* blocks */
578
579 /*
580 * This is called by main to set dumplo and dumpsize.
581 * Dumps always skip the first PAGE_SIZE of disk space
582 * in case there might be a disk label stored there.
583 * If there is extra space, put dump at the end to
584 * reduce the chance that swapping trashes it.
585 */
586 void
587 cpu_dumpconf()
588 {
589 const struct bdevsw *bdev;
590 int chdrsize; /* size of dump header */
591 int nblks; /* size of dump area */
592
593 if (dumpdev == NODEV)
594 return;
595 bdev = bdevsw_lookup(dumpdev);
596 if (bdev == NULL)
597 panic("dumpconf: bad dumpdev=0x%x", dumpdev);
598 if (bdev->d_psize == NULL)
599 return;
600 nblks = (*bdev->d_psize)(dumpdev);
601 chdrsize = cpu_dumpsize();
602
603 dumpsize = btoc(cpu_kcore_hdr.un._m68k.ram_segs[0].size);
604
605 /*
606 * Check do see if we will fit. Note we always skip the
607 * first PAGE_SIZE in case there is a disk label there.
608 */
609 if (nblks < (ctod(dumpsize) + chdrsize + ctod(1))) {
610 dumpsize = 0;
611 dumplo = -1;
612 return;
613 }
614
615 /*
616 * Put dump at the end of the partition.
617 */
618 dumplo = (nblks - 1) - ctod(dumpsize) - chdrsize;
619 }
620
621 /*
622 * Dump physical memory onto the dump device. Called by cpu_reboot().
623 */
624 void
625 dumpsys()
626 {
627 const struct bdevsw *bdev;
628 daddr_t blkno; /* current block to write */
629 /* dump routine */
630 int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
631 int pg; /* page being dumped */
632 paddr_t maddr; /* PA being dumped */
633 int error; /* error code from (*dump)() */
634
635 /* XXX initialized here because of gcc lossage */
636 maddr = 0 /* lowram */;
637 pg = 0;
638
639 /* Make sure dump device is valid. */
640 if (dumpdev == NODEV)
641 return;
642 bdev = bdevsw_lookup(dumpdev);
643 if (bdev == NULL)
644 return;
645 if (dumpsize == 0) {
646 cpu_dumpconf();
647 if (dumpsize == 0)
648 return;
649 }
650 if (dumplo <= 0) {
651 printf("\ndump to dev %u,%u not possible\n", major(dumpdev),
652 minor(dumpdev));
653 return;
654 }
655 dump = bdev->d_dump;
656 blkno = dumplo;
657
658 printf("\ndumping to dev %u,%u offset %ld\n", major(dumpdev),
659 minor(dumpdev), dumplo);
660
661 printf("dump ");
662
663 /* Write the dump header. */
664 error = cpu_dump(dump, &blkno);
665 if (error)
666 goto bad;
667
668 for (pg = 0; pg < dumpsize; pg++) {
669 #define NPGMB (1024*1024/PAGE_SIZE)
670 /* print out how many MBs we have dumped */
671 if (pg && (pg % NPGMB) == 0)
672 printf("%d ", pg / NPGMB);
673 #undef NPGMB
674 pmap_enter(pmap_kernel(), (vaddr_t)vmmap, maddr,
675 VM_PROT_READ, VM_PROT_READ|PMAP_WIRED);
676
677 pmap_update(pmap_kernel());
678 error = (*dump)(dumpdev, blkno, vmmap, PAGE_SIZE);
679 bad:
680 switch (error) {
681 case 0:
682 maddr += PAGE_SIZE;
683 blkno += btodb(PAGE_SIZE);
684 break;
685
686 case ENXIO:
687 printf("device bad\n");
688 return;
689
690 case EFAULT:
691 printf("device not ready\n");
692 return;
693
694 case EINVAL:
695 printf("area improper\n");
696 return;
697
698 case EIO:
699 printf("i/o error\n");
700 return;
701
702 case EINTR:
703 printf("aborted from console\n");
704 return;
705
706 default:
707 printf("error %d\n", error);
708 return;
709 }
710 }
711 printf("succeeded\n");
712 }
713
714 void
715 straytrap(pc, evec)
716 int pc;
717 u_short evec;
718 {
719 printf("unexpected trap (vector offset %x) from %x\n",
720 evec & 0xFFF, pc);
721 }
722
723 int *nofault;
724
725 int
726 badaddr(addr, nbytes)
727 register caddr_t addr;
728 int nbytes;
729 {
730 register int i;
731 label_t faultbuf;
732
733 #ifdef lint
734 i = *addr; if (i) return (0);
735 #endif
736
737 nofault = (int *) &faultbuf;
738 if (setjmp((label_t *)nofault)) {
739 nofault = (int *) 0;
740 return(1);
741 }
742
743 switch (nbytes) {
744 case 1:
745 i = *(volatile char *)addr;
746 break;
747
748 case 2:
749 i = *(volatile short *)addr;
750 break;
751
752 case 4:
753 i = *(volatile int *)addr;
754 break;
755
756 default:
757 panic("badaddr: bad request");
758 }
759 nofault = (int *) 0;
760 return (0);
761 }
762
763 void luna68k_abort __P((char *));
764
765 static int innmihand; /* simple mutex */
766
767 /*
768 * Level 7 interrupts are caused by e.g. the ABORT switch.
769 *
770 * If we have DDB, then break into DDB on ABORT. In a production
771 * environment, bumping the ABORT switch would be bad, so we enable
772 * panic'ing on ABORT with the kernel option "PANICBUTTON".
773 */
774 void
775 nmihand(frame)
776 struct frame frame;
777 {
778 /* Prevent unwanted recursion */
779 if (innmihand)
780 return;
781 innmihand = 1;
782
783 luna68k_abort("ABORT SWITCH");
784 }
785
786 /*
787 * Common code for handling ABORT signals from buttons, switches,
788 * serial lines, etc.
789 */
790 void
791 luna68k_abort(cp)
792 char *cp;
793 {
794 #ifdef DDB
795 printf("%s\n", cp);
796 cpu_Debugger();
797 #else
798 #ifdef PANICBUTTON
799 panic(cp);
800 #else
801 printf("%s ignored\n", cp);
802 #endif /* PANICBUTTON */
803 #endif /* DDB */
804 }
805
806 /*
807 * cpu_exec_aout_makecmds():
808 * CPU-dependent a.out format hook for execve().
809 *
810 * Determine of the given exec package refers to something which we
811 * understand and, if so, set up the vmcmds for it.
812 */
813 int
814 cpu_exec_aout_makecmds(p, epp)
815 struct proc *p;
816 struct exec_package *epp;
817 {
818 int error = ENOEXEC;
819 #ifdef COMPAT_SUNOS
820 extern sunos_exec_aout_makecmds
821 __P((struct proc *, struct exec_package *));
822 if ((error = sunos_exec_aout_makecmds(p, epp)) == 0)
823 return 0;
824 #endif
825 return error;
826 }
827
828 /*
829 * Return the best possible estimate of the time in the timeval
830 * to which tvp points. Unfortunately, we can't read the hardware registers.
831 * We guarantee that the time will be greater than the value obtained by a
832 * previous call.
833 */
834 void
835 microtime(tvp)
836 register struct timeval *tvp;
837 {
838 int s = splclock();
839 static struct timeval lasttime;
840
841 *tvp = time;
842 #ifdef notdef
843 tvp->tv_usec += clkread();
844 while (tvp->tv_usec >= 1000000) {
845 tvp->tv_sec++;
846 tvp->tv_usec -= 1000000;
847 }
848 #endif
849 if (tvp->tv_sec == lasttime.tv_sec &&
850 tvp->tv_usec <= lasttime.tv_usec &&
851 (tvp->tv_usec = lasttime.tv_usec + 1) >= 1000000) {
852 tvp->tv_sec++;
853 tvp->tv_usec -= 1000000;
854 }
855 lasttime = *tvp;
856 splx(s);
857 }
858
859 #if 1
860
861 struct consdev *cn_tab = &syscons;
862
863 #else
864
865 /*
866 * romcons is useful until m68k TC register is initialized.
867 */
868 int romcngetc __P((dev_t));
869 void romcnputc __P((dev_t, int));
870
871 struct consdev romcons = {
872 NULL,
873 NULL,
874 romcngetc,
875 romcnputc,
876 nullcnpollc,
877 makedev(7, 0), /* XXX */
878 CN_DEAD,
879 };
880 struct consdev *cn_tab = &romcons;
881
882 #define __ ((int **)0x41000000)
883 #define GETC() (*(int (*)())__[6])()
884 #define PUTC(x) (*(void (*)())__[7])(x)
885
886 #define ROMPUTC(x) \
887 ({ \
888 register _r; \
889 asm volatile (" \
890 movc %%vbr,%0 ; \
891 movel %0,%%sp@- ; \
892 clrl %0 ; \
893 movc %0,%%vbr" \
894 : "=r" (_r)); \
895 PUTC(x); \
896 asm volatile (" \
897 movel %%sp@+,%0 ; \
898 movc %0,%%vbr" \
899 : "=r" (_r)); \
900 })
901
902 #define ROMGETC() \
903 ({ \
904 register _r, _c; \
905 asm volatile (" \
906 movc %%vbr,%0 ; \
907 movel %0,%%sp@- ; \
908 clrl %0 ; \
909 movc %0,%%vbr" \
910 : "=r" (_r)); \
911 _c = GETC(); \
912 asm volatile (" \
913 movel %%sp@+,%0 ; \
914 movc %0,%%vbr" \
915 : "=r" (_r)); \
916 _c; \
917 })
918
919 void
920 romcnputc(dev, c)
921 dev_t dev;
922 int c;
923 {
924 int s;
925
926 s = splhigh();
927 ROMPUTC(c);
928 splx(s);
929 }
930
931 int
932 romcngetc(dev)
933 dev_t dev;
934 {
935 int s, c;
936
937 do {
938 s = splhigh();
939 c = ROMGETC();
940 splx(s);
941 } while (c == -1);
942 return c;
943 }
944 #endif
945