machdep.c revision 1.106 1 /* $NetBSD: machdep.c,v 1.106 1998/02/13 02:09:05 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
5 * All rights reserved.
6 *
7 * Author: Chris G. Demetriou
8 *
9 * Permission to use, copy, modify and distribute this software and
10 * its documentation is hereby granted, provided that both the copyright
11 * notice and this permission notice appear in all copies of the
12 * software, derivative works or modified versions, and any portions
13 * thereof, and that both notices appear in supporting documentation.
14 *
15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 *
19 * Carnegie Mellon requests users of this software to return to
20 *
21 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
22 * School of Computer Science
23 * Carnegie Mellon University
24 * Pittsburgh PA 15213-3890
25 *
26 * any improvements or extensions that they make and grant Carnegie the
27 * rights to redistribute these changes.
28 */
29
30 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
31
32 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.106 1998/02/13 02:09:05 cgd Exp $");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/signalvar.h>
37 #include <sys/kernel.h>
38 #include <sys/map.h>
39 #include <sys/proc.h>
40 #include <sys/buf.h>
41 #include <sys/reboot.h>
42 #include <sys/device.h>
43 #include <sys/file.h>
44 #ifdef REAL_CLISTS
45 #include <sys/clist.h>
46 #endif
47 #include <sys/callout.h>
48 #include <sys/malloc.h>
49 #include <sys/mbuf.h>
50 #include <sys/msgbuf.h>
51 #include <sys/ioctl.h>
52 #include <sys/tty.h>
53 #include <sys/user.h>
54 #include <sys/exec.h>
55 #include <sys/exec_ecoff.h>
56 #include <vm/vm.h>
57 #include <sys/sysctl.h>
58 #include <sys/core.h>
59 #include <sys/kcore.h>
60 #include <machine/kcore.h>
61 #ifdef SYSVMSG
62 #include <sys/msg.h>
63 #endif
64 #ifdef SYSVSEM
65 #include <sys/sem.h>
66 #endif
67 #ifdef SYSVSHM
68 #include <sys/shm.h>
69 #endif
70
71 #include <sys/mount.h>
72 #include <sys/syscallargs.h>
73
74 #include <vm/vm_kern.h>
75
76 #include <dev/cons.h>
77
78 #include <machine/autoconf.h>
79 #include <machine/cpu.h>
80 #include <machine/reg.h>
81 #include <machine/rpb.h>
82 #include <machine/prom.h>
83 #include <machine/conf.h>
84
85 #include <net/netisr.h>
86 #include <net/if.h>
87
88 #ifdef INET
89 #include <netinet/in.h>
90 #include <netinet/ip_var.h>
91 #include "arp.h"
92 #if NARP > 0
93 #include <netinet/if_inarp.h>
94 #endif
95 #endif
96 #ifdef NS
97 #include <netns/ns_var.h>
98 #endif
99 #ifdef ISO
100 #include <netiso/iso.h>
101 #include <netiso/clnp.h>
102 #endif
103 #ifdef CCITT
104 #include <netccitt/x25.h>
105 #include <netccitt/pk.h>
106 #include <netccitt/pk_extern.h>
107 #endif
108 #ifdef NATM
109 #include <netnatm/natm.h>
110 #endif
111 #ifdef NETATALK
112 #include <netatalk/at_extern.h>
113 #endif
114 #include "ppp.h"
115 #if NPPP > 0
116 #include <net/ppp_defs.h>
117 #include <net/if_ppp.h>
118 #endif
119
120 #ifdef DDB
121 #include <machine/db_machdep.h>
122 #include <ddb/db_access.h>
123 #include <ddb/db_sym.h>
124 #include <ddb/db_extern.h>
125 #include <ddb/db_interface.h>
126 #endif
127
128 vm_map_t buffer_map;
129
130 /*
131 * Declare these as initialized data so we can patch them.
132 */
133 int nswbuf = 0;
134 #ifdef NBUF
135 int nbuf = NBUF;
136 #else
137 int nbuf = 0;
138 #endif
139 #ifdef BUFPAGES
140 int bufpages = BUFPAGES;
141 #else
142 int bufpages = 0;
143 #endif
144 caddr_t msgbufaddr;
145
146 int maxmem; /* max memory per process */
147
148 int totalphysmem; /* total amount of physical memory in system */
149 int physmem; /* physical memory used by NetBSD + some rsvd */
150 int firstusablepage; /* first usable memory page */
151 int lastusablepage; /* last usable memory page */
152 int resvmem; /* amount of memory reserved for PROM */
153 int unusedmem; /* amount of memory for OS that we don't use */
154 int unknownmem; /* amount of memory with an unknown use */
155
156 int cputype; /* system type, from the RPB */
157
158 /*
159 * XXX We need an address to which we can assign things so that they
160 * won't be optimized away because we didn't use the value.
161 */
162 u_int32_t no_optimize;
163
164 /* the following is used externally (sysctl_hw) */
165 char machine[] = MACHINE; /* from <machine/param.h> */
166 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */
167 char cpu_model[128];
168
169 struct user *proc0paddr;
170
171 /* Number of machine cycles per microsecond */
172 u_int64_t cycles_per_usec;
173
174 /* number of cpus in the box. really! */
175 int ncpus;
176
177 struct bootinfo_kernel bootinfo;
178
179 struct platform platform;
180
181 u_int32_t vm_mbuf_size = _VM_MBUF_SIZE;
182 u_int32_t vm_kmem_size = _VM_KMEM_SIZE;
183 u_int32_t vm_phys_size = _VM_PHYS_SIZE;
184
185 #ifdef DDB
186 /* start and end of kernel symbol table */
187 void *ksym_start, *ksym_end;
188 #endif
189
190 /* for cpu_sysctl() */
191 int alpha_unaligned_print = 1; /* warn about unaligned accesses */
192 int alpha_unaligned_fix = 1; /* fix up unaligned accesses */
193 int alpha_unaligned_sigbus = 0; /* don't SIGBUS on fixed-up accesses */
194
195 caddr_t allocsys __P((caddr_t));
196 int cpu_dump __P((void));
197 int cpu_dumpsize __P((void));
198 void dumpsys __P((void));
199 void identifycpu __P((void));
200 void netintr __P((void));
201 void printregs __P((struct reg *));
202
203 void
204 alpha_init(pfn, ptb, bim, bip, biv)
205 u_long pfn; /* first free PFN number */
206 u_long ptb; /* PFN of current level 1 page table */
207 u_long bim; /* bootinfo magic */
208 u_long bip; /* bootinfo pointer */
209 u_long biv; /* bootinfo version */
210 {
211 extern char kernel_text[], _end[];
212 struct mddt *mddtp;
213 int i, mddtweird;
214 vm_offset_t kernstart, kernend;
215 vm_size_t size;
216 char *p;
217 caddr_t v;
218 caddr_t start, w;
219 char *bootinfo_msg;
220
221 /* NO OUTPUT ALLOWED UNTIL FURTHER NOTICE */
222
223 /*
224 * Turn off interrupts (not mchecks) and floating point.
225 * Make sure the instruction and data streams are consistent.
226 */
227 (void)alpha_pal_swpipl(ALPHA_PSL_IPL_HIGH);
228 alpha_pal_wrfen(0);
229 ALPHA_TBIA();
230 alpha_pal_imb();
231
232 /*
233 * Get critical system information (if possible, from the
234 * information provided by the boot program).
235 */
236 bootinfo_msg = NULL;
237 if (bim == BOOTINFO_MAGIC) {
238 if (biv == 0) { /* backward compat */
239 biv = *(u_long *)bip;
240 bip += 8;
241 }
242 switch (biv) {
243 case 1: {
244 struct bootinfo_v1 *v1p = (struct bootinfo_v1 *)bip;
245
246 bootinfo.ssym = v1p->ssym;
247 bootinfo.esym = v1p->esym;
248 /* hwrpb may not be provided by boot block in v1 */
249 if (v1p->hwrpb != NULL) {
250 bootinfo.hwrpb_phys =
251 ((struct rpb *)v1p->hwrpb)->rpb_phys;
252 bootinfo.hwrpb_size = v1p->hwrpbsize;
253 } else {
254 bootinfo.hwrpb_phys =
255 ((struct rpb *)HWRPB_ADDR)->rpb_phys;
256 bootinfo.hwrpb_size =
257 ((struct rpb *)HWRPB_ADDR)->rpb_size;
258 }
259 bcopy(v1p->boot_flags, bootinfo.boot_flags,
260 min(sizeof v1p->boot_flags,
261 sizeof bootinfo.boot_flags));
262 bcopy(v1p->booted_kernel, bootinfo.booted_kernel,
263 min(sizeof v1p->booted_kernel,
264 sizeof bootinfo.booted_kernel));
265 /* booted dev not provided in bootinfo */
266 init_prom_interface((struct rpb *)
267 ALPHA_PHYS_TO_K0SEG(bootinfo.hwrpb_phys));
268 prom_getenv(PROM_E_BOOTED_DEV, bootinfo.booted_dev,
269 sizeof bootinfo.booted_dev);
270 break;
271 }
272 default:
273 bootinfo_msg = "unknown bootinfo version";
274 goto nobootinfo;
275 }
276 } else {
277 bootinfo_msg = "boot program did not pass bootinfo";
278 nobootinfo:
279 bootinfo.ssym = (u_long)_end;
280 bootinfo.esym = (u_long)_end;
281 bootinfo.hwrpb_phys = ((struct rpb *)HWRPB_ADDR)->rpb_phys;
282 bootinfo.hwrpb_size = ((struct rpb *)HWRPB_ADDR)->rpb_size;
283 init_prom_interface((struct rpb *)HWRPB_ADDR);
284 prom_getenv(PROM_E_BOOTED_OSFLAGS, bootinfo.boot_flags,
285 sizeof bootinfo.boot_flags);
286 prom_getenv(PROM_E_BOOTED_FILE, bootinfo.booted_kernel,
287 sizeof bootinfo.booted_kernel);
288 prom_getenv(PROM_E_BOOTED_DEV, bootinfo.booted_dev,
289 sizeof bootinfo.booted_dev);
290 }
291
292 /*
293 * Initialize the kernel's mapping of the RPB. It's needed for
294 * lots of things.
295 */
296 hwrpb = (struct rpb *)ALPHA_PHYS_TO_K0SEG(bootinfo.hwrpb_phys);
297
298 /*
299 * Remember how many cycles there are per microsecond,
300 * so that we can use delay(). Round up, for safety.
301 */
302 cycles_per_usec = (hwrpb->rpb_cc_freq + 999999) / 1000000;
303
304 /*
305 * Initalize the (temporary) bootstrap console interface, so
306 * we can use printf until the VM system starts being setup.
307 * The real console is initialized before then.
308 */
309 init_bootstrap_console();
310
311 /* OUTPUT NOW ALLOWED */
312
313 /* delayed from above */
314 if (bootinfo_msg)
315 printf("WARNING: %s (0x%lx, 0x%lx, 0x%lx)\n",
316 bootinfo_msg, bim, bip, biv);
317
318 /*
319 * Point interrupt/exception vectors to our own.
320 */
321 alpha_pal_wrent(XentInt, ALPHA_KENTRY_INT);
322 alpha_pal_wrent(XentArith, ALPHA_KENTRY_ARITH);
323 alpha_pal_wrent(XentMM, ALPHA_KENTRY_MM);
324 alpha_pal_wrent(XentIF, ALPHA_KENTRY_IF);
325 alpha_pal_wrent(XentUna, ALPHA_KENTRY_UNA);
326 alpha_pal_wrent(XentSys, ALPHA_KENTRY_SYS);
327
328 /*
329 * Clear pending machine checks and error reports, and enable
330 * system- and processor-correctable error reporting.
331 */
332 alpha_pal_wrmces(alpha_pal_rdmces() &
333 ~(ALPHA_MCES_DSC|ALPHA_MCES_DPC));
334
335 /*
336 * Find out what hardware we're on, and do basic initialization.
337 */
338 cputype = hwrpb->rpb_type;
339 if (cputype >= ncpuinit) {
340 platform_not_supported();
341 /* NOTREACHED */
342 }
343 (*cpuinit[cputype].init)();
344 strcpy(cpu_model, platform.model);
345
346 /*
347 * Initalize the real console, so the the bootstrap console is
348 * no longer necessary.
349 */
350 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
351 if (!pmap_uses_prom_console())
352 #endif
353 (*platform.cons_init)();
354
355 #ifdef DIAGNOSTIC
356 /* Paranoid sanity checking */
357
358 /* We should always be running on the the primary. */
359 assert(hwrpb->rpb_primary_cpu_id == alpha_pal_whami());
360
361 /* On single-CPU systypes, the primary should always be CPU 0. */
362 if (cputype != ST_DEC_21000)
363 assert(hwrpb->rpb_primary_cpu_id == 0);
364 #endif
365
366 /* NO MORE FIRMWARE ACCESS ALLOWED */
367 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
368 /*
369 * XXX (unless _PMAP_MAY_USE_PROM_CONSOLE is defined and
370 * XXX pmap_uses_prom_console() evaluates to non-zero.)
371 */
372 #endif
373
374 /*
375 * find out this system's page size
376 */
377 PAGE_SIZE = hwrpb->rpb_page_size;
378 if (PAGE_SIZE != 8192)
379 panic("page size %d != 8192?!", PAGE_SIZE);
380
381 /*
382 * Initialize PAGE_SIZE-dependent variables.
383 */
384 vm_set_page_size();
385
386 /*
387 * Find the beginning and end of the kernel (and leave a
388 * bit of space before the beginning for the bootstrap
389 * stack).
390 */
391 kernstart = trunc_page(kernel_text) - 2 * PAGE_SIZE;
392 #ifdef DDB
393 ksym_start = (void *)bootinfo.ssym;
394 ksym_end = (void *)bootinfo.esym;
395 kernend = (vm_offset_t)round_page(ksym_end);
396 #else
397 kernend = (vm_offset_t)round_page(_end);
398 #endif
399
400 /*
401 * Find out how much memory is available, by looking at
402 * the memory cluster descriptors. This also tries to do
403 * its best to detect things things that have never been seen
404 * before...
405 *
406 * XXX Assumes that the first "system" cluster is the
407 * only one we can use. Is the second (etc.) system cluster
408 * (if one happens to exist) guaranteed to be contiguous? or...?
409 */
410 mddtp = (struct mddt *)(((caddr_t)hwrpb) + hwrpb->rpb_memdat_off);
411
412 /*
413 * BEGIN MDDT WEIRDNESS CHECKING
414 */
415 mddtweird = 0;
416
417 #define cnt mddtp->mddt_cluster_cnt
418 #define usage(n) mddtp->mddt_clusters[(n)].mddt_usage
419 if (cnt != 2 && cnt != 3) {
420 printf("WARNING: weird number (%ld) of mem clusters\n", cnt);
421 mddtweird = 1;
422 } else if (usage(0) != MDDT_PALCODE ||
423 usage(1) != MDDT_SYSTEM ||
424 (cnt == 3 && usage(2) != MDDT_PALCODE)) {
425 mddtweird = 1;
426 printf("WARNING: %ld mem clusters, but weird config\n", cnt);
427 }
428
429 for (i = 0; i < cnt; i++) {
430 if ((usage(i) & MDDT_mbz) != 0) {
431 printf("WARNING: mem cluster %d has weird usage %lx\n",
432 i, usage(i));
433 mddtweird = 1;
434 }
435 if (mddtp->mddt_clusters[i].mddt_pg_cnt == 0) {
436 printf("WARNING: mem cluster %d has pg cnt == 0\n", i);
437 mddtweird = 1;
438 }
439 /* XXX other things to check? */
440 }
441 #undef cnt
442 #undef usage
443
444 if (mddtweird) {
445 printf("\n");
446 printf("complete memory cluster information:\n");
447 for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
448 printf("mddt %d:\n", i);
449 printf("\tpfn %lx\n",
450 mddtp->mddt_clusters[i].mddt_pfn);
451 printf("\tcnt %lx\n",
452 mddtp->mddt_clusters[i].mddt_pg_cnt);
453 printf("\ttest %lx\n",
454 mddtp->mddt_clusters[i].mddt_pg_test);
455 printf("\tbva %lx\n",
456 mddtp->mddt_clusters[i].mddt_v_bitaddr);
457 printf("\tbpa %lx\n",
458 mddtp->mddt_clusters[i].mddt_p_bitaddr);
459 printf("\tbcksum %lx\n",
460 mddtp->mddt_clusters[i].mddt_bit_cksum);
461 printf("\tusage %lx\n",
462 mddtp->mddt_clusters[i].mddt_usage);
463 }
464 printf("\n");
465 }
466 /*
467 * END MDDT WEIRDNESS CHECKING
468 */
469
470 for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
471 totalphysmem += mddtp->mddt_clusters[i].mddt_pg_cnt;
472 #define usage(n) mddtp->mddt_clusters[(n)].mddt_usage
473 #define pgcnt(n) mddtp->mddt_clusters[(n)].mddt_pg_cnt
474 if ((usage(i) & MDDT_mbz) != 0)
475 unknownmem += pgcnt(i);
476 else if ((usage(i) & ~MDDT_mbz) == MDDT_PALCODE)
477 resvmem += pgcnt(i);
478 else if ((usage(i) & ~MDDT_mbz) == MDDT_SYSTEM) {
479 /*
480 * assumes that the system cluster listed is
481 * one we're in...
482 */
483 if (physmem != resvmem) {
484 physmem += pgcnt(i);
485 firstusablepage =
486 mddtp->mddt_clusters[i].mddt_pfn;
487 lastusablepage = firstusablepage + pgcnt(i) - 1;
488 } else
489 unusedmem += pgcnt(i);
490 }
491 #undef usage
492 #undef pgcnt
493 }
494 if (totalphysmem == 0)
495 panic("can't happen: system seems to have no memory!");
496 #ifdef LIMITMEM
497 if (totalphysmem >= btoc(LIMITMEM << 20)) {
498 u_int64_t ovf = totalphysmem - btoc(LIMITMEM << 20);
499 printf("********LIMITING MEMORY TO %dMB**********\n", LIMITMEM);
500 physmem = totalphysmem = btoc(LIMITMEM << 20);
501 unusedmem += ovf;
502 lastusablepage = firstusablepage + physmem - 1;
503 }
504 #endif
505 maxmem = physmem;
506
507 #if 0
508 printf("totalphysmem = %d\n", totalphysmem);
509 printf("physmem = %d\n", physmem);
510 printf("firstusablepage = %d\n", firstusablepage);
511 printf("lastusablepage = %d\n", lastusablepage);
512 printf("resvmem = %d\n", resvmem);
513 printf("unusedmem = %d\n", unusedmem);
514 printf("unknownmem = %d\n", unknownmem);
515 #endif
516
517 /*
518 * Adjust some parameters if the amount of physmem
519 * available would cause us to croak. This is completely
520 * eyeballed and isn't meant to be the final answer.
521 * vm_phys_size is probably the only one to really worry
522 * about.
523 *
524 * It's for booting a GENERIC kernel on a large memory platform.
525 */
526 if (physmem >= btoc(128 << 20)) {
527 vm_mbuf_size <<= 1;
528 vm_kmem_size <<= 3;
529 vm_phys_size <<= 2;
530 }
531
532 /*
533 * Initialize error message buffer (at end of core).
534 */
535 lastusablepage -= btoc(MSGBUFSIZE);
536 msgbufaddr = (caddr_t) ALPHA_PHYS_TO_K0SEG(ctob(lastusablepage + 1));
537 initmsgbuf(msgbufaddr, alpha_round_page(MSGBUFSIZE));
538
539 /*
540 * Init mapping for u page(s) for proc 0
541 */
542 start = v = (caddr_t)kernend;
543 curproc->p_addr = proc0paddr = (struct user *)v;
544 v += UPAGES * NBPG;
545
546 /*
547 * Allocate space for system data structures. These data structures
548 * are allocated here instead of cpu_startup() because physical
549 * memory is directly addressable. We don't have to map these into
550 * virtual address space.
551 */
552 size = (vm_size_t)allocsys(0);
553 w = allocsys(v);
554 if ((w - v) != size)
555 panic("alpha_init: table size inconsistency");
556 v = w;
557
558 /*
559 * Clear allocated memory.
560 */
561 bzero(start, v - start);
562
563 /*
564 * Initialize the virtual memory system, and set the
565 * page table base register in proc 0's PCB.
566 */
567 #ifndef NEW_PMAP
568 pmap_bootstrap((vm_offset_t)v, ALPHA_PHYS_TO_K0SEG(ptb << PGSHIFT));
569 #else
570 pmap_bootstrap((vm_offset_t)v, ALPHA_PHYS_TO_K0SEG(ptb << PGSHIFT),
571 hwrpb->rpb_max_asn);
572 #endif
573
574 /*
575 * Initialize the rest of proc 0's PCB, and cache its physical
576 * address.
577 */
578 proc0.p_md.md_pcbpaddr =
579 (struct pcb *)ALPHA_K0SEG_TO_PHYS((vm_offset_t)&proc0paddr->u_pcb);
580
581 /*
582 * Set the kernel sp, reserving space for an (empty) trapframe,
583 * and make proc0's trapframe pointer point to it for sanity.
584 */
585 proc0paddr->u_pcb.pcb_hw.apcb_ksp =
586 (u_int64_t)proc0paddr + USPACE - sizeof(struct trapframe);
587 proc0.p_md.md_tf =
588 (struct trapframe *)proc0paddr->u_pcb.pcb_hw.apcb_ksp;
589
590 #ifdef NEW_PMAP
591 /*
592 * Set up the kernel address space in proc0's hwpcb.
593 */
594 PMAP_ACTIVATE(kernel_pmap, &proc0paddr->u_pcb.pcb_hw, 0);
595 #endif
596
597 /*
598 * Look at arguments passed to us and compute boothowto.
599 */
600
601 boothowto = RB_SINGLE;
602 #ifdef KADB
603 boothowto |= RB_KDB;
604 #endif
605 for (p = bootinfo.boot_flags; p && *p != '\0'; p++) {
606 /*
607 * Note that we'd really like to differentiate case here,
608 * but the Alpha AXP Architecture Reference Manual
609 * says that we shouldn't.
610 */
611 switch (*p) {
612 case 'a': /* autoboot */
613 case 'A':
614 boothowto &= ~RB_SINGLE;
615 break;
616
617 #ifdef DEBUG
618 case 'c': /* crash dump immediately after autoconfig */
619 case 'C':
620 boothowto |= RB_DUMP;
621 break;
622 #endif
623
624 #if defined(KGDB) || defined(DDB)
625 case 'd': /* break into the kernel debugger ASAP */
626 case 'D':
627 boothowto |= RB_KDB;
628 break;
629 #endif
630
631 case 'h': /* always halt, never reboot */
632 case 'H':
633 boothowto |= RB_HALT;
634 break;
635
636 #if 0
637 case 'm': /* mini root present in memory */
638 case 'M':
639 boothowto |= RB_MINIROOT;
640 break;
641 #endif
642
643 case 'n': /* askname */
644 case 'N':
645 boothowto |= RB_ASKNAME;
646 break;
647
648 case 's': /* single-user (default, supported for sanity) */
649 case 'S':
650 boothowto |= RB_SINGLE;
651 break;
652
653 default:
654 printf("Unrecognized boot flag '%c'.\n", *p);
655 break;
656 }
657 }
658
659 /*
660 * Initialize debuggers, and break into them if appropriate.
661 */
662 #ifdef DDB
663 db_machine_init();
664 ddb_init(ksym_start, ksym_end);
665 if (boothowto & RB_KDB)
666 Debugger();
667 #endif
668 #ifdef KGDB
669 if (boothowto & RB_KDB)
670 kgdb_connect(0);
671 #endif
672
673 /*
674 * Figure out the number of cpus in the box, from RPB fields.
675 * Really. We mean it.
676 */
677 for (i = 0; i < hwrpb->rpb_pcs_cnt; i++) {
678 struct pcs *pcsp;
679
680 pcsp = (struct pcs *)((char *)hwrpb + hwrpb->rpb_pcs_off +
681 (i * hwrpb->rpb_pcs_size));
682 if ((pcsp->pcs_flags & PCS_PP) != 0)
683 ncpus++;
684 }
685
686 /*
687 * Figure out our clock frequency, from RPB fields.
688 */
689 hz = hwrpb->rpb_intr_freq >> 12;
690 if (!(60 <= hz && hz <= 10240)) {
691 hz = 1024;
692 #ifdef DIAGNOSTIC
693 printf("WARNING: unbelievable rpb_intr_freq: %ld (%d hz)\n",
694 hwrpb->rpb_intr_freq, hz);
695 #endif
696 }
697
698 }
699
700 /*
701 * Allocate space for system data structures. We are given
702 * a starting virtual address and we return a final virtual
703 * address; along the way we set each data structure pointer.
704 *
705 * We call allocsys() with 0 to find out how much space we want,
706 * allocate that much and fill it with zeroes, and the call
707 * allocsys() again with the correct base virtual address.
708 */
709 caddr_t
710 allocsys(v)
711 caddr_t v;
712 {
713
714 #define valloc(name, type, num) \
715 (name) = (type *)v; v = (caddr_t)ALIGN((name)+(num))
716 #ifdef REAL_CLISTS
717 valloc(cfree, struct cblock, nclist);
718 #endif
719 valloc(callout, struct callout, ncallout);
720 #ifdef SYSVSHM
721 valloc(shmsegs, struct shmid_ds, shminfo.shmmni);
722 #endif
723 #ifdef SYSVSEM
724 valloc(sema, struct semid_ds, seminfo.semmni);
725 valloc(sem, struct sem, seminfo.semmns);
726 /* This is pretty disgusting! */
727 valloc(semu, int, (seminfo.semmnu * seminfo.semusz) / sizeof(int));
728 #endif
729 #ifdef SYSVMSG
730 valloc(msgpool, char, msginfo.msgmax);
731 valloc(msgmaps, struct msgmap, msginfo.msgseg);
732 valloc(msghdrs, struct msg, msginfo.msgtql);
733 valloc(msqids, struct msqid_ds, msginfo.msgmni);
734 #endif
735
736 /*
737 * Determine how many buffers to allocate.
738 * We allocate 10% of memory for buffer space. Insure a
739 * minimum of 16 buffers. We allocate 1/2 as many swap buffer
740 * headers as file i/o buffers.
741 */
742 if (bufpages == 0)
743 bufpages = (physmem * 10) / (CLSIZE * 100);
744 if (nbuf == 0) {
745 nbuf = bufpages;
746 if (nbuf < 16)
747 nbuf = 16;
748 }
749 if (nswbuf == 0) {
750 nswbuf = (nbuf / 2) &~ 1; /* force even */
751 if (nswbuf > 256)
752 nswbuf = 256; /* sanity */
753 }
754 valloc(swbuf, struct buf, nswbuf);
755 valloc(buf, struct buf, nbuf);
756 return (v);
757 #undef valloc
758 }
759
760 void
761 consinit()
762 {
763
764 /*
765 * Everything related to console initialization is done
766 * in alpha_init().
767 */
768 #if defined(DIAGNOSTIC) && defined(_PMAP_MAY_USE_PROM_CONSOLE)
769 printf("consinit: %susing prom console\n",
770 pmap_uses_prom_console() ? "" : "not ");
771 #endif
772 }
773
774 void
775 cpu_startup()
776 {
777 register unsigned i;
778 int base, residual;
779 vm_offset_t minaddr, maxaddr;
780 vm_size_t size;
781 #if defined(DEBUG)
782 extern int pmapdebug;
783 int opmapdebug = pmapdebug;
784
785 pmapdebug = 0;
786 #endif
787
788 /*
789 * Good {morning,afternoon,evening,night}.
790 */
791 printf(version);
792 identifycpu();
793 printf("real mem = %u (%u reserved for PROM, %u used by NetBSD)\n",
794 ctob(totalphysmem), ctob(resvmem), ctob(physmem));
795 if (unusedmem)
796 printf("WARNING: unused memory = %d bytes\n", ctob(unusedmem));
797 if (unknownmem)
798 printf("WARNING: %d bytes of memory with unknown purpose\n",
799 ctob(unknownmem));
800
801 /*
802 * Allocate virtual address space for file I/O buffers.
803 * Note they are different than the array of headers, 'buf',
804 * and usually occupy more virtual memory than physical.
805 */
806 size = MAXBSIZE * nbuf;
807 buffer_map = kmem_suballoc(kernel_map, (vm_offset_t *)&buffers,
808 &maxaddr, size, TRUE);
809 minaddr = (vm_offset_t)buffers;
810 if (vm_map_find(buffer_map, vm_object_allocate(size), (vm_offset_t)0,
811 &minaddr, size, FALSE) != KERN_SUCCESS)
812 panic("startup: cannot allocate buffers");
813 base = bufpages / nbuf;
814 residual = bufpages % nbuf;
815 for (i = 0; i < nbuf; i++) {
816 vm_size_t curbufsize;
817 vm_offset_t curbuf;
818
819 /*
820 * First <residual> buffers get (base+1) physical pages
821 * allocated for them. The rest get (base) physical pages.
822 *
823 * The rest of each buffer occupies virtual space,
824 * but has no physical memory allocated for it.
825 */
826 curbuf = (vm_offset_t)buffers + i * MAXBSIZE;
827 curbufsize = CLBYTES * (i < residual ? base+1 : base);
828 vm_map_pageable(buffer_map, curbuf, curbuf+curbufsize, FALSE);
829 vm_map_simplify(buffer_map, curbuf);
830 }
831 /*
832 * Allocate a submap for exec arguments. This map effectively
833 * limits the number of processes exec'ing at any time.
834 */
835 exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
836 16 * NCARGS, TRUE);
837
838 /*
839 * Allocate a submap for physio
840 */
841 phys_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
842 VM_PHYS_SIZE, TRUE);
843
844 /*
845 * Finally, allocate mbuf cluster submap.
846 */
847 mb_map = kmem_suballoc(kernel_map, (vm_offset_t *)&mbutl, &maxaddr,
848 VM_MBUF_SIZE, FALSE);
849 /*
850 * Initialize callouts
851 */
852 callfree = callout;
853 for (i = 1; i < ncallout; i++)
854 callout[i-1].c_next = &callout[i];
855 callout[i-1].c_next = NULL;
856
857 #if defined(DEBUG)
858 pmapdebug = opmapdebug;
859 #endif
860 printf("avail mem = %ld\n", (long)ptoa(cnt.v_free_count));
861 printf("using %ld buffers containing %ld bytes of memory\n",
862 (long)nbuf, (long)(bufpages * CLBYTES));
863
864 /*
865 * Set up buffers, so they can be used to read disk labels.
866 */
867 bufinit();
868
869 /*
870 * Configure the system.
871 */
872 configure();
873
874 /*
875 * Note that bootstrapping is finished, and set the HWRPB up
876 * to do restarts.
877 */
878 hwrpb_restart_setup();
879 }
880
881 /*
882 * Retrieve the platform name from the DSR.
883 */
884 const char *
885 alpha_dsr_sysname()
886 {
887 struct dsrdb *dsr;
888 const char *sysname;
889
890 /*
891 * DSR does not exist on early HWRPB versions.
892 */
893 if (hwrpb->rpb_version < HWRPB_DSRDB_MINVERS)
894 return (NULL);
895
896 dsr = (struct dsrdb *)(((caddr_t)hwrpb) + hwrpb->rpb_dsrdb_off);
897 sysname = (const char *)((caddr_t)dsr + (dsr->dsr_sysname_off +
898 sizeof(u_int64_t)));
899 return (sysname);
900 }
901
902 /*
903 * Lookup the system specified system variation in the provided table,
904 * returning the model string on match.
905 */
906 const char *
907 alpha_variation_name(variation, avtp)
908 u_int64_t variation;
909 const struct alpha_variation_table *avtp;
910 {
911 int i;
912
913 for (i = 0; avtp[i].avt_model != NULL; i++)
914 if (avtp[i].avt_variation == variation)
915 return (avtp[i].avt_model);
916 return (NULL);
917 }
918
919 /*
920 * Generate a default platform name based for unknown system variations.
921 */
922 const char *
923 alpha_unknown_sysname()
924 {
925 static char s[128]; /* safe size */
926
927 sprintf(s, "%s family, unknown model variation 0x%lx",
928 platform.family, hwrpb->rpb_variation & SV_ST_MASK);
929 return ((const char *)s);
930 }
931
932 void
933 identifycpu()
934 {
935
936 /*
937 * print out CPU identification information.
938 */
939 printf("%s, %ldMHz\n", cpu_model,
940 hwrpb->rpb_cc_freq / 1000000); /* XXX true for 21164? */
941 printf("%ld byte page size, %d processor%s.\n",
942 hwrpb->rpb_page_size, ncpus, ncpus == 1 ? "" : "s");
943 #if 0
944 /* this isn't defined for any systems that we run on? */
945 printf("serial number 0x%lx 0x%lx\n",
946 ((long *)hwrpb->rpb_ssn)[0], ((long *)hwrpb->rpb_ssn)[1]);
947
948 /* and these aren't particularly useful! */
949 printf("variation: 0x%lx, revision 0x%lx\n",
950 hwrpb->rpb_variation, *(long *)hwrpb->rpb_revision);
951 #endif
952 }
953
954 int waittime = -1;
955 struct pcb dumppcb;
956
957 void
958 cpu_reboot(howto, bootstr)
959 int howto;
960 char *bootstr;
961 {
962 extern int cold;
963
964 /* If system is cold, just halt. */
965 if (cold) {
966 howto |= RB_HALT;
967 goto haltsys;
968 }
969
970 /* If "always halt" was specified as a boot flag, obey. */
971 if ((boothowto & RB_HALT) != 0)
972 howto |= RB_HALT;
973
974 boothowto = howto;
975 if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
976 waittime = 0;
977 vfs_shutdown();
978 /*
979 * If we've been adjusting the clock, the todr
980 * will be out of synch; adjust it now.
981 */
982 resettodr();
983 }
984
985 /* Disable interrupts. */
986 splhigh();
987
988 /* If rebooting and a dump is requested do it. */
989 #if 0
990 if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
991 #else
992 if (howto & RB_DUMP)
993 #endif
994 dumpsys();
995
996 haltsys:
997
998 /* run any shutdown hooks */
999 doshutdownhooks();
1000
1001 #ifdef BOOTKEY
1002 printf("hit any key to %s...\n", howto & RB_HALT ? "halt" : "reboot");
1003 cngetc();
1004 printf("\n");
1005 #endif
1006
1007 /* Finally, halt/reboot the system. */
1008 printf("%s\n\n", howto & RB_HALT ? "halted." : "rebooting...");
1009 prom_halt(howto & RB_HALT);
1010 /*NOTREACHED*/
1011 }
1012
1013 /*
1014 * These variables are needed by /sbin/savecore
1015 */
1016 u_long dumpmag = 0x8fca0101; /* magic number */
1017 int dumpsize = 0; /* pages */
1018 long dumplo = 0; /* blocks */
1019
1020 /*
1021 * cpu_dumpsize: calculate size of machine-dependent kernel core dump headers.
1022 */
1023 int
1024 cpu_dumpsize()
1025 {
1026 int size;
1027
1028 size = ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t));
1029 if (roundup(size, dbtob(1)) != dbtob(1))
1030 return -1;
1031
1032 return (1);
1033 }
1034
1035 /*
1036 * cpu_dump: dump machine-dependent kernel core dump headers.
1037 */
1038 int
1039 cpu_dump()
1040 {
1041 int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
1042 long buf[dbtob(1) / sizeof (long)];
1043 kcore_seg_t *segp;
1044 cpu_kcore_hdr_t *cpuhdrp;
1045
1046 dump = bdevsw[major(dumpdev)].d_dump;
1047
1048 segp = (kcore_seg_t *)buf;
1049 cpuhdrp =
1050 (cpu_kcore_hdr_t *)&buf[ALIGN(sizeof(*segp)) / sizeof (long)];
1051
1052 /*
1053 * Generate a segment header.
1054 */
1055 CORE_SETMAGIC(*segp, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
1056 segp->c_size = dbtob(1) - ALIGN(sizeof(*segp));
1057
1058 /*
1059 * Add the machine-dependent header info
1060 */
1061 cpuhdrp->lev1map_pa = ALPHA_K0SEG_TO_PHYS((vm_offset_t)Lev1map);
1062 cpuhdrp->page_size = PAGE_SIZE;
1063 cpuhdrp->core_seg.start = ctob(firstusablepage);
1064 cpuhdrp->core_seg.size = ctob(physmem);
1065
1066 return (dump(dumpdev, dumplo, (caddr_t)buf, dbtob(1)));
1067 }
1068
1069 /*
1070 * This is called by main to set dumplo and dumpsize.
1071 * Dumps always skip the first CLBYTES of disk space
1072 * in case there might be a disk label stored there.
1073 * If there is extra space, put dump at the end to
1074 * reduce the chance that swapping trashes it.
1075 */
1076 void
1077 cpu_dumpconf()
1078 {
1079 int nblks, dumpblks; /* size of dump area */
1080 int maj;
1081
1082 if (dumpdev == NODEV)
1083 goto bad;
1084 maj = major(dumpdev);
1085 if (maj < 0 || maj >= nblkdev)
1086 panic("dumpconf: bad dumpdev=0x%x", dumpdev);
1087 if (bdevsw[maj].d_psize == NULL)
1088 goto bad;
1089 nblks = (*bdevsw[maj].d_psize)(dumpdev);
1090 if (nblks <= ctod(1))
1091 goto bad;
1092
1093 dumpblks = cpu_dumpsize();
1094 if (dumpblks < 0)
1095 goto bad;
1096 dumpblks += ctod(physmem);
1097
1098 /* If dump won't fit (incl. room for possible label), punt. */
1099 if (dumpblks > (nblks - ctod(1)))
1100 goto bad;
1101
1102 /* Put dump at end of partition */
1103 dumplo = nblks - dumpblks;
1104
1105 /* dumpsize is in page units, and doesn't include headers. */
1106 dumpsize = physmem;
1107 return;
1108
1109 bad:
1110 dumpsize = 0;
1111 return;
1112 }
1113
1114 /*
1115 * Dump the kernel's image to the swap partition.
1116 */
1117 #define BYTES_PER_DUMP NBPG
1118
1119 void
1120 dumpsys()
1121 {
1122 unsigned bytes, i, n;
1123 int maddr, psize;
1124 daddr_t blkno;
1125 int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
1126 int error;
1127
1128 /* Save registers. */
1129 savectx(&dumppcb);
1130
1131 msgbufmapped = 0; /* don't record dump msgs in msgbuf */
1132 if (dumpdev == NODEV)
1133 return;
1134
1135 /*
1136 * For dumps during autoconfiguration,
1137 * if dump device has already configured...
1138 */
1139 if (dumpsize == 0)
1140 cpu_dumpconf();
1141 if (dumplo <= 0) {
1142 printf("\ndump to dev %u,%u not possible\n", major(dumpdev),
1143 minor(dumpdev));
1144 return;
1145 }
1146 printf("\ndumping to dev %u,%u offset %ld\n", major(dumpdev),
1147 minor(dumpdev), dumplo);
1148
1149 psize = (*bdevsw[major(dumpdev)].d_psize)(dumpdev);
1150 printf("dump ");
1151 if (psize == -1) {
1152 printf("area unavailable\n");
1153 return;
1154 }
1155
1156 /* XXX should purge all outstanding keystrokes. */
1157
1158 if ((error = cpu_dump()) != 0)
1159 goto err;
1160
1161 bytes = ctob(physmem);
1162 maddr = ctob(firstusablepage);
1163 blkno = dumplo + cpu_dumpsize();
1164 dump = bdevsw[major(dumpdev)].d_dump;
1165 error = 0;
1166 for (i = 0; i < bytes; i += n) {
1167
1168 /* Print out how many MBs we to go. */
1169 n = bytes - i;
1170 if (n && (n % (1024*1024)) == 0)
1171 printf("%d ", n / (1024 * 1024));
1172
1173 /* Limit size for next transfer. */
1174 if (n > BYTES_PER_DUMP)
1175 n = BYTES_PER_DUMP;
1176
1177 error = (*dump)(dumpdev, blkno,
1178 (caddr_t)ALPHA_PHYS_TO_K0SEG(maddr), n);
1179 if (error)
1180 break;
1181 maddr += n;
1182 blkno += btodb(n); /* XXX? */
1183
1184 /* XXX should look for keystrokes, to cancel. */
1185 }
1186
1187 err:
1188 switch (error) {
1189
1190 case ENXIO:
1191 printf("device bad\n");
1192 break;
1193
1194 case EFAULT:
1195 printf("device not ready\n");
1196 break;
1197
1198 case EINVAL:
1199 printf("area improper\n");
1200 break;
1201
1202 case EIO:
1203 printf("i/o error\n");
1204 break;
1205
1206 case EINTR:
1207 printf("aborted from console\n");
1208 break;
1209
1210 case 0:
1211 printf("succeeded\n");
1212 break;
1213
1214 default:
1215 printf("error %d\n", error);
1216 break;
1217 }
1218 printf("\n\n");
1219 delay(1000);
1220 }
1221
1222 void
1223 frametoreg(framep, regp)
1224 struct trapframe *framep;
1225 struct reg *regp;
1226 {
1227
1228 regp->r_regs[R_V0] = framep->tf_regs[FRAME_V0];
1229 regp->r_regs[R_T0] = framep->tf_regs[FRAME_T0];
1230 regp->r_regs[R_T1] = framep->tf_regs[FRAME_T1];
1231 regp->r_regs[R_T2] = framep->tf_regs[FRAME_T2];
1232 regp->r_regs[R_T3] = framep->tf_regs[FRAME_T3];
1233 regp->r_regs[R_T4] = framep->tf_regs[FRAME_T4];
1234 regp->r_regs[R_T5] = framep->tf_regs[FRAME_T5];
1235 regp->r_regs[R_T6] = framep->tf_regs[FRAME_T6];
1236 regp->r_regs[R_T7] = framep->tf_regs[FRAME_T7];
1237 regp->r_regs[R_S0] = framep->tf_regs[FRAME_S0];
1238 regp->r_regs[R_S1] = framep->tf_regs[FRAME_S1];
1239 regp->r_regs[R_S2] = framep->tf_regs[FRAME_S2];
1240 regp->r_regs[R_S3] = framep->tf_regs[FRAME_S3];
1241 regp->r_regs[R_S4] = framep->tf_regs[FRAME_S4];
1242 regp->r_regs[R_S5] = framep->tf_regs[FRAME_S5];
1243 regp->r_regs[R_S6] = framep->tf_regs[FRAME_S6];
1244 regp->r_regs[R_A0] = framep->tf_regs[FRAME_A0];
1245 regp->r_regs[R_A1] = framep->tf_regs[FRAME_A1];
1246 regp->r_regs[R_A2] = framep->tf_regs[FRAME_A2];
1247 regp->r_regs[R_A3] = framep->tf_regs[FRAME_A3];
1248 regp->r_regs[R_A4] = framep->tf_regs[FRAME_A4];
1249 regp->r_regs[R_A5] = framep->tf_regs[FRAME_A5];
1250 regp->r_regs[R_T8] = framep->tf_regs[FRAME_T8];
1251 regp->r_regs[R_T9] = framep->tf_regs[FRAME_T9];
1252 regp->r_regs[R_T10] = framep->tf_regs[FRAME_T10];
1253 regp->r_regs[R_T11] = framep->tf_regs[FRAME_T11];
1254 regp->r_regs[R_RA] = framep->tf_regs[FRAME_RA];
1255 regp->r_regs[R_T12] = framep->tf_regs[FRAME_T12];
1256 regp->r_regs[R_AT] = framep->tf_regs[FRAME_AT];
1257 regp->r_regs[R_GP] = framep->tf_regs[FRAME_GP];
1258 /* regp->r_regs[R_SP] = framep->tf_regs[FRAME_SP]; XXX */
1259 regp->r_regs[R_ZERO] = 0;
1260 }
1261
1262 void
1263 regtoframe(regp, framep)
1264 struct reg *regp;
1265 struct trapframe *framep;
1266 {
1267
1268 framep->tf_regs[FRAME_V0] = regp->r_regs[R_V0];
1269 framep->tf_regs[FRAME_T0] = regp->r_regs[R_T0];
1270 framep->tf_regs[FRAME_T1] = regp->r_regs[R_T1];
1271 framep->tf_regs[FRAME_T2] = regp->r_regs[R_T2];
1272 framep->tf_regs[FRAME_T3] = regp->r_regs[R_T3];
1273 framep->tf_regs[FRAME_T4] = regp->r_regs[R_T4];
1274 framep->tf_regs[FRAME_T5] = regp->r_regs[R_T5];
1275 framep->tf_regs[FRAME_T6] = regp->r_regs[R_T6];
1276 framep->tf_regs[FRAME_T7] = regp->r_regs[R_T7];
1277 framep->tf_regs[FRAME_S0] = regp->r_regs[R_S0];
1278 framep->tf_regs[FRAME_S1] = regp->r_regs[R_S1];
1279 framep->tf_regs[FRAME_S2] = regp->r_regs[R_S2];
1280 framep->tf_regs[FRAME_S3] = regp->r_regs[R_S3];
1281 framep->tf_regs[FRAME_S4] = regp->r_regs[R_S4];
1282 framep->tf_regs[FRAME_S5] = regp->r_regs[R_S5];
1283 framep->tf_regs[FRAME_S6] = regp->r_regs[R_S6];
1284 framep->tf_regs[FRAME_A0] = regp->r_regs[R_A0];
1285 framep->tf_regs[FRAME_A1] = regp->r_regs[R_A1];
1286 framep->tf_regs[FRAME_A2] = regp->r_regs[R_A2];
1287 framep->tf_regs[FRAME_A3] = regp->r_regs[R_A3];
1288 framep->tf_regs[FRAME_A4] = regp->r_regs[R_A4];
1289 framep->tf_regs[FRAME_A5] = regp->r_regs[R_A5];
1290 framep->tf_regs[FRAME_T8] = regp->r_regs[R_T8];
1291 framep->tf_regs[FRAME_T9] = regp->r_regs[R_T9];
1292 framep->tf_regs[FRAME_T10] = regp->r_regs[R_T10];
1293 framep->tf_regs[FRAME_T11] = regp->r_regs[R_T11];
1294 framep->tf_regs[FRAME_RA] = regp->r_regs[R_RA];
1295 framep->tf_regs[FRAME_T12] = regp->r_regs[R_T12];
1296 framep->tf_regs[FRAME_AT] = regp->r_regs[R_AT];
1297 framep->tf_regs[FRAME_GP] = regp->r_regs[R_GP];
1298 /* framep->tf_regs[FRAME_SP] = regp->r_regs[R_SP]; XXX */
1299 /* ??? = regp->r_regs[R_ZERO]; */
1300 }
1301
1302 void
1303 printregs(regp)
1304 struct reg *regp;
1305 {
1306 int i;
1307
1308 for (i = 0; i < 32; i++)
1309 printf("R%d:\t0x%016lx%s", i, regp->r_regs[i],
1310 i & 1 ? "\n" : "\t");
1311 }
1312
1313 void
1314 regdump(framep)
1315 struct trapframe *framep;
1316 {
1317 struct reg reg;
1318
1319 frametoreg(framep, ®);
1320 reg.r_regs[R_SP] = alpha_pal_rdusp();
1321
1322 printf("REGISTERS:\n");
1323 printregs(®);
1324 }
1325
1326 #ifdef DEBUG
1327 int sigdebug = 0;
1328 int sigpid = 0;
1329 #define SDB_FOLLOW 0x01
1330 #define SDB_KSTACK 0x02
1331 #endif
1332
1333 /*
1334 * Send an interrupt to process.
1335 */
1336 void
1337 sendsig(catcher, sig, mask, code)
1338 sig_t catcher;
1339 int sig, mask;
1340 u_long code;
1341 {
1342 struct proc *p = curproc;
1343 struct sigcontext *scp, ksc;
1344 struct trapframe *frame;
1345 struct sigacts *psp = p->p_sigacts;
1346 int oonstack, fsize, rndfsize;
1347 extern char sigcode[], esigcode[];
1348 extern struct proc *fpcurproc;
1349
1350 frame = p->p_md.md_tf;
1351 oonstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
1352 fsize = sizeof ksc;
1353 rndfsize = ((fsize + 15) / 16) * 16;
1354 /*
1355 * Allocate and validate space for the signal handler
1356 * context. Note that if the stack is in P0 space, the
1357 * call to grow() is a nop, and the useracc() check
1358 * will fail if the process has not already allocated
1359 * the space with a `brk'.
1360 */
1361 if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
1362 (psp->ps_sigonstack & sigmask(sig))) {
1363 scp = (struct sigcontext *)(psp->ps_sigstk.ss_sp +
1364 psp->ps_sigstk.ss_size - rndfsize);
1365 psp->ps_sigstk.ss_flags |= SS_ONSTACK;
1366 } else
1367 scp = (struct sigcontext *)(alpha_pal_rdusp() - rndfsize);
1368 if ((u_long)scp <= USRSTACK - ctob(p->p_vmspace->vm_ssize))
1369 (void)grow(p, (u_long)scp);
1370 #ifdef DEBUG
1371 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
1372 printf("sendsig(%d): sig %d ssp %p usp %p\n", p->p_pid,
1373 sig, &oonstack, scp);
1374 #endif
1375 if (useracc((caddr_t)scp, fsize, B_WRITE) == 0) {
1376 #ifdef DEBUG
1377 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
1378 printf("sendsig(%d): useracc failed on sig %d\n",
1379 p->p_pid, sig);
1380 #endif
1381 /*
1382 * Process has trashed its stack; give it an illegal
1383 * instruction to halt it in its tracks.
1384 */
1385 SIGACTION(p, SIGILL) = SIG_DFL;
1386 sig = sigmask(SIGILL);
1387 p->p_sigignore &= ~sig;
1388 p->p_sigcatch &= ~sig;
1389 p->p_sigmask &= ~sig;
1390 psignal(p, SIGILL);
1391 return;
1392 }
1393
1394 /*
1395 * Build the signal context to be used by sigreturn.
1396 */
1397 ksc.sc_onstack = oonstack;
1398 ksc.sc_mask = mask;
1399 ksc.sc_pc = frame->tf_regs[FRAME_PC];
1400 ksc.sc_ps = frame->tf_regs[FRAME_PS];
1401
1402 /* copy the registers. */
1403 frametoreg(frame, (struct reg *)ksc.sc_regs);
1404 ksc.sc_regs[R_ZERO] = 0xACEDBADE; /* magic number */
1405 ksc.sc_regs[R_SP] = alpha_pal_rdusp();
1406
1407 /* save the floating-point state, if necessary, then copy it. */
1408 if (p == fpcurproc) {
1409 alpha_pal_wrfen(1);
1410 savefpstate(&p->p_addr->u_pcb.pcb_fp);
1411 alpha_pal_wrfen(0);
1412 fpcurproc = NULL;
1413 }
1414 ksc.sc_ownedfp = p->p_md.md_flags & MDP_FPUSED;
1415 bcopy(&p->p_addr->u_pcb.pcb_fp, (struct fpreg *)ksc.sc_fpregs,
1416 sizeof(struct fpreg));
1417 ksc.sc_fp_control = 0; /* XXX ? */
1418 bzero(ksc.sc_reserved, sizeof ksc.sc_reserved); /* XXX */
1419 bzero(ksc.sc_xxx, sizeof ksc.sc_xxx); /* XXX */
1420
1421
1422 #ifdef COMPAT_OSF1
1423 /*
1424 * XXX Create an OSF/1-style sigcontext and associated goo.
1425 */
1426 #endif
1427
1428 /*
1429 * copy the frame out to userland.
1430 */
1431 (void) copyout((caddr_t)&ksc, (caddr_t)scp, fsize);
1432 #ifdef DEBUG
1433 if (sigdebug & SDB_FOLLOW)
1434 printf("sendsig(%d): sig %d scp %p code %lx\n", p->p_pid, sig,
1435 scp, code);
1436 #endif
1437
1438 /*
1439 * Set up the registers to return to sigcode.
1440 */
1441 frame->tf_regs[FRAME_PC] =
1442 (u_int64_t)PS_STRINGS - (esigcode - sigcode);
1443 frame->tf_regs[FRAME_A0] = sig;
1444 frame->tf_regs[FRAME_A1] = code;
1445 frame->tf_regs[FRAME_A2] = (u_int64_t)scp;
1446 frame->tf_regs[FRAME_T12] = (u_int64_t)catcher; /* t12 is pv */
1447 alpha_pal_wrusp((unsigned long)scp);
1448
1449 #ifdef DEBUG
1450 if (sigdebug & SDB_FOLLOW)
1451 printf("sendsig(%d): pc %lx, catcher %lx\n", p->p_pid,
1452 frame->tf_regs[FRAME_PC], frame->tf_regs[FRAME_A3]);
1453 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
1454 printf("sendsig(%d): sig %d returns\n",
1455 p->p_pid, sig);
1456 #endif
1457 }
1458
1459 /*
1460 * System call to cleanup state after a signal
1461 * has been taken. Reset signal mask and
1462 * stack state from context left by sendsig (above).
1463 * Return to previous pc and psl as specified by
1464 * context left by sendsig. Check carefully to
1465 * make sure that the user has not modified the
1466 * psl to gain improper priviledges or to cause
1467 * a machine fault.
1468 */
1469 /* ARGSUSED */
1470 int
1471 sys_sigreturn(p, v, retval)
1472 struct proc *p;
1473 void *v;
1474 register_t *retval;
1475 {
1476 struct sys_sigreturn_args /* {
1477 syscallarg(struct sigcontext *) sigcntxp;
1478 } */ *uap = v;
1479 struct sigcontext *scp, ksc;
1480 extern struct proc *fpcurproc;
1481
1482 scp = SCARG(uap, sigcntxp);
1483 #ifdef DEBUG
1484 if (sigdebug & SDB_FOLLOW)
1485 printf("sigreturn: pid %d, scp %p\n", p->p_pid, scp);
1486 #endif
1487
1488 if (ALIGN(scp) != (u_int64_t)scp)
1489 return (EINVAL);
1490
1491 /*
1492 * Test and fetch the context structure.
1493 * We grab it all at once for speed.
1494 */
1495 if (useracc((caddr_t)scp, sizeof (*scp), B_WRITE) == 0 ||
1496 copyin((caddr_t)scp, (caddr_t)&ksc, sizeof ksc))
1497 return (EINVAL);
1498
1499 if (ksc.sc_regs[R_ZERO] != 0xACEDBADE) /* magic number */
1500 return (EINVAL);
1501 /*
1502 * Restore the user-supplied information
1503 */
1504 if (ksc.sc_onstack)
1505 p->p_sigacts->ps_sigstk.ss_flags |= SS_ONSTACK;
1506 else
1507 p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
1508 p->p_sigmask = ksc.sc_mask &~ sigcantmask;
1509
1510 p->p_md.md_tf->tf_regs[FRAME_PC] = ksc.sc_pc;
1511 p->p_md.md_tf->tf_regs[FRAME_PS] =
1512 (ksc.sc_ps | ALPHA_PSL_USERSET) & ~ALPHA_PSL_USERCLR;
1513
1514 regtoframe((struct reg *)ksc.sc_regs, p->p_md.md_tf);
1515 alpha_pal_wrusp(ksc.sc_regs[R_SP]);
1516
1517 /* XXX ksc.sc_ownedfp ? */
1518 if (p == fpcurproc)
1519 fpcurproc = NULL;
1520 bcopy((struct fpreg *)ksc.sc_fpregs, &p->p_addr->u_pcb.pcb_fp,
1521 sizeof(struct fpreg));
1522 /* XXX ksc.sc_fp_control ? */
1523
1524 #ifdef DEBUG
1525 if (sigdebug & SDB_FOLLOW)
1526 printf("sigreturn(%d): returns\n", p->p_pid);
1527 #endif
1528 return (EJUSTRETURN);
1529 }
1530
1531 /*
1532 * machine dependent system variables.
1533 */
1534 int
1535 cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
1536 int *name;
1537 u_int namelen;
1538 void *oldp;
1539 size_t *oldlenp;
1540 void *newp;
1541 size_t newlen;
1542 struct proc *p;
1543 {
1544 dev_t consdev;
1545
1546 /* all sysctl names at this level are terminal */
1547 if (namelen != 1)
1548 return (ENOTDIR); /* overloaded */
1549
1550 switch (name[0]) {
1551 case CPU_CONSDEV:
1552 if (cn_tab != NULL)
1553 consdev = cn_tab->cn_dev;
1554 else
1555 consdev = NODEV;
1556 return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
1557 sizeof consdev));
1558
1559 case CPU_ROOT_DEVICE:
1560 return (sysctl_rdstring(oldp, oldlenp, newp,
1561 root_device->dv_xname));
1562
1563 case CPU_UNALIGNED_PRINT:
1564 return (sysctl_int(oldp, oldlenp, newp, newlen,
1565 &alpha_unaligned_print));
1566
1567 case CPU_UNALIGNED_FIX:
1568 return (sysctl_int(oldp, oldlenp, newp, newlen,
1569 &alpha_unaligned_fix));
1570
1571 case CPU_UNALIGNED_SIGBUS:
1572 return (sysctl_int(oldp, oldlenp, newp, newlen,
1573 &alpha_unaligned_sigbus));
1574
1575 case CPU_BOOTED_KERNEL:
1576 return (sysctl_rdstring(oldp, oldlenp, newp,
1577 bootinfo.booted_kernel));
1578
1579 default:
1580 return (EOPNOTSUPP);
1581 }
1582 /* NOTREACHED */
1583 }
1584
1585 /*
1586 * Set registers on exec.
1587 */
1588 void
1589 setregs(p, pack, stack)
1590 register struct proc *p;
1591 struct exec_package *pack;
1592 u_long stack;
1593 {
1594 struct trapframe *tfp = p->p_md.md_tf;
1595 extern struct proc *fpcurproc;
1596 #ifdef DEBUG
1597 int i;
1598 #endif
1599
1600 #ifdef DEBUG
1601 /*
1602 * Crash and dump, if the user requested it.
1603 */
1604 if (boothowto & RB_DUMP)
1605 panic("crash requested by boot flags");
1606 #endif
1607
1608 #ifdef DEBUG
1609 for (i = 0; i < FRAME_SIZE; i++)
1610 tfp->tf_regs[i] = 0xbabefacedeadbeef;
1611 #else
1612 bzero(tfp->tf_regs, FRAME_SIZE * sizeof tfp->tf_regs[0]);
1613 #endif
1614 bzero(&p->p_addr->u_pcb.pcb_fp, sizeof p->p_addr->u_pcb.pcb_fp);
1615 #define FP_RN 2 /* XXX */
1616 p->p_addr->u_pcb.pcb_fp.fpr_cr = (long)FP_RN << 58;
1617 alpha_pal_wrusp(stack);
1618 tfp->tf_regs[FRAME_PS] = ALPHA_PSL_USERSET;
1619 tfp->tf_regs[FRAME_PC] = pack->ep_entry & ~3;
1620
1621 tfp->tf_regs[FRAME_A0] = stack; /* a0 = sp */
1622 tfp->tf_regs[FRAME_A1] = 0; /* a1 = rtld cleanup */
1623 tfp->tf_regs[FRAME_A2] = 0; /* a2 = rtld object */
1624 tfp->tf_regs[FRAME_A3] = (u_int64_t)PS_STRINGS; /* a3 = ps_strings */
1625 tfp->tf_regs[FRAME_T12] = tfp->tf_regs[FRAME_PC]; /* a.k.a. PV */
1626
1627 p->p_md.md_flags &= ~MDP_FPUSED;
1628 if (fpcurproc == p)
1629 fpcurproc = NULL;
1630 }
1631
1632 void
1633 netintr()
1634 {
1635 int n, s;
1636
1637 s = splhigh();
1638 n = netisr;
1639 netisr = 0;
1640 splx(s);
1641
1642 #define DONETISR(bit, fn) \
1643 do { \
1644 if (n & (1 << (bit))) \
1645 fn; \
1646 } while (0)
1647
1648 #ifdef INET
1649 #if NARP > 0
1650 DONETISR(NETISR_ARP, arpintr());
1651 #endif
1652 DONETISR(NETISR_IP, ipintr());
1653 #endif
1654 #ifdef NETATALK
1655 DONETISR(NETISR_ATALK, atintr());
1656 #endif
1657 #ifdef NS
1658 DONETISR(NETISR_NS, nsintr());
1659 #endif
1660 #ifdef ISO
1661 DONETISR(NETISR_ISO, clnlintr());
1662 #endif
1663 #ifdef CCITT
1664 DONETISR(NETISR_CCITT, ccittintr());
1665 #endif
1666 #ifdef NATM
1667 DONETISR(NETISR_NATM, natmintr());
1668 #endif
1669 #if NPPP > 1
1670 DONETISR(NETISR_PPP, pppintr());
1671 #endif
1672
1673 #undef DONETISR
1674 }
1675
1676 void
1677 do_sir()
1678 {
1679 u_int64_t n;
1680
1681 do {
1682 (void)splhigh();
1683 n = ssir;
1684 ssir = 0;
1685 splsoft(); /* don't recurse through spl0() */
1686
1687 #define DO_SIR(bit, fn) \
1688 do { \
1689 if (n & (bit)) { \
1690 cnt.v_soft++; \
1691 fn; \
1692 } \
1693 } while (0)
1694
1695 DO_SIR(SIR_NET, netintr());
1696 DO_SIR(SIR_CLOCK, softclock());
1697
1698 #undef DO_SIR
1699 } while (ssir != 0);
1700 }
1701
1702 int
1703 spl0()
1704 {
1705
1706 if (ssir)
1707 do_sir(); /* it lowers the IPL itself */
1708
1709 return (alpha_pal_swpipl(ALPHA_PSL_IPL_0));
1710 }
1711
1712 /*
1713 * The following primitives manipulate the run queues. _whichqs tells which
1714 * of the 32 queues _qs have processes in them. Setrunqueue puts processes
1715 * into queues, Remrunqueue removes them from queues. The running process is
1716 * on no queue, other processes are on a queue related to p->p_priority,
1717 * divided by 4 actually to shrink the 0-127 range of priorities into the 32
1718 * available queues.
1719 */
1720 /*
1721 * setrunqueue(p)
1722 * proc *p;
1723 *
1724 * Call should be made at splclock(), and p->p_stat should be SRUN.
1725 */
1726
1727 void
1728 setrunqueue(p)
1729 struct proc *p;
1730 {
1731 int bit;
1732
1733 /* firewall: p->p_back must be NULL */
1734 if (p->p_back != NULL)
1735 panic("setrunqueue");
1736
1737 bit = p->p_priority >> 2;
1738 whichqs |= (1 << bit);
1739 p->p_forw = (struct proc *)&qs[bit];
1740 p->p_back = qs[bit].ph_rlink;
1741 p->p_back->p_forw = p;
1742 qs[bit].ph_rlink = p;
1743 }
1744
1745 /*
1746 * remrunqueue(p)
1747 *
1748 * Call should be made at splclock().
1749 */
1750 void
1751 remrunqueue(p)
1752 struct proc *p;
1753 {
1754 int bit;
1755
1756 bit = p->p_priority >> 2;
1757 if ((whichqs & (1 << bit)) == 0)
1758 panic("remrunqueue");
1759
1760 p->p_back->p_forw = p->p_forw;
1761 p->p_forw->p_back = p->p_back;
1762 p->p_back = NULL; /* for firewall checking. */
1763
1764 if ((struct proc *)&qs[bit] == qs[bit].ph_link)
1765 whichqs &= ~(1 << bit);
1766 }
1767
1768 /*
1769 * Return the best possible estimate of the time in the timeval
1770 * to which tvp points. Unfortunately, we can't read the hardware registers.
1771 * We guarantee that the time will be greater than the value obtained by a
1772 * previous call.
1773 */
1774 void
1775 microtime(tvp)
1776 register struct timeval *tvp;
1777 {
1778 int s = splclock();
1779 static struct timeval lasttime;
1780
1781 *tvp = time;
1782 #ifdef notdef
1783 tvp->tv_usec += clkread();
1784 while (tvp->tv_usec > 1000000) {
1785 tvp->tv_sec++;
1786 tvp->tv_usec -= 1000000;
1787 }
1788 #endif
1789 if (tvp->tv_sec == lasttime.tv_sec &&
1790 tvp->tv_usec <= lasttime.tv_usec &&
1791 (tvp->tv_usec = lasttime.tv_usec + 1) > 1000000) {
1792 tvp->tv_sec++;
1793 tvp->tv_usec -= 1000000;
1794 }
1795 lasttime = *tvp;
1796 splx(s);
1797 }
1798
1799 /*
1800 * Wait "n" microseconds.
1801 */
1802 void
1803 delay(n)
1804 unsigned long n;
1805 {
1806 long N = cycles_per_usec * (n);
1807
1808 while (N > 0) /* XXX */
1809 N -= 3; /* XXX */
1810 }
1811
1812 #if defined(COMPAT_OSF1) || 1 /* XXX */
1813 void cpu_exec_ecoff_setregs __P((struct proc *, struct exec_package *,
1814 u_long));
1815
1816 void
1817 cpu_exec_ecoff_setregs(p, epp, stack)
1818 struct proc *p;
1819 struct exec_package *epp;
1820 u_long stack;
1821 {
1822 struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
1823
1824 setregs(p, epp, stack);
1825 p->p_md.md_tf->tf_regs[FRAME_GP] = execp->a.gp_value;
1826 }
1827
1828 /*
1829 * cpu_exec_ecoff_hook():
1830 * cpu-dependent ECOFF format hook for execve().
1831 *
1832 * Do any machine-dependent diddling of the exec package when doing ECOFF.
1833 *
1834 */
1835 int
1836 cpu_exec_ecoff_hook(p, epp)
1837 struct proc *p;
1838 struct exec_package *epp;
1839 {
1840 struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
1841 extern struct emul emul_netbsd;
1842 #ifdef COMPAT_OSF1
1843 extern struct emul emul_osf1;
1844 #endif
1845
1846 switch (execp->f.f_magic) {
1847 #ifdef COMPAT_OSF1
1848 case ECOFF_MAGIC_ALPHA:
1849 epp->ep_emul = &emul_osf1;
1850 break;
1851 #endif
1852
1853 case ECOFF_MAGIC_NETBSD_ALPHA:
1854 epp->ep_emul = &emul_netbsd;
1855 break;
1856
1857 default:
1858 return ENOEXEC;
1859 }
1860 return 0;
1861 }
1862 #endif
1863
1864 /* XXX XXX BEGIN XXX XXX */
1865 vm_offset_t alpha_XXX_dmamap_or; /* XXX */
1866 /* XXX */
1867 vm_offset_t /* XXX */
1868 alpha_XXX_dmamap(v) /* XXX */
1869 vm_offset_t v; /* XXX */
1870 { /* XXX */
1871 /* XXX */
1872 return (vtophys(v) | alpha_XXX_dmamap_or); /* XXX */
1873 } /* XXX */
1874 /* XXX XXX END XXX XXX */
1875