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