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