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