machdep.c revision 1.178 1 /* $NetBSD: machdep.c,v 1.178 1999/08/10 18:53:03 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center and by Chris G. Demetriou.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
42 * All rights reserved.
43 *
44 * Author: Chris G. Demetriou
45 *
46 * Permission to use, copy, modify and distribute this software and
47 * its documentation is hereby granted, provided that both the copyright
48 * notice and this permission notice appear in all copies of the
49 * software, derivative works or modified versions, and any portions
50 * thereof, and that both notices appear in supporting documentation.
51 *
52 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
53 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
54 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
55 *
56 * Carnegie Mellon requests users of this software to return to
57 *
58 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
59 * School of Computer Science
60 * Carnegie Mellon University
61 * Pittsburgh PA 15213-3890
62 *
63 * any improvements or extensions that they make and grant Carnegie the
64 * rights to redistribute these changes.
65 */
66
67 #include "opt_ddb.h"
68 #include "opt_multiprocessor.h"
69 #include "opt_pmap_new.h"
70 #include "opt_dec_3000_300.h"
71 #include "opt_dec_3000_500.h"
72 #include "opt_compat_osf1.h"
73 #include "opt_compat_netbsd.h"
74 #include "opt_inet.h"
75 #include "opt_atalk.h"
76 #include "opt_ccitt.h"
77 #include "opt_iso.h"
78 #include "opt_ns.h"
79 #include "opt_natm.h"
80
81 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
82
83 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.178 1999/08/10 18:53:03 thorpej Exp $");
84
85 #include <sys/param.h>
86 #include <sys/systm.h>
87 #include <sys/signalvar.h>
88 #include <sys/kernel.h>
89 #include <sys/map.h>
90 #include <sys/proc.h>
91 #include <sys/buf.h>
92 #include <sys/reboot.h>
93 #include <sys/device.h>
94 #include <sys/file.h>
95 #include <sys/callout.h>
96 #include <sys/malloc.h>
97 #include <sys/mbuf.h>
98 #include <sys/mman.h>
99 #include <sys/msgbuf.h>
100 #include <sys/ioctl.h>
101 #include <sys/tty.h>
102 #include <sys/user.h>
103 #include <sys/exec.h>
104 #include <sys/exec_ecoff.h>
105 #include <vm/vm.h>
106 #include <sys/sysctl.h>
107 #include <sys/core.h>
108 #include <sys/kcore.h>
109 #include <machine/kcore.h>
110
111 #include <sys/mount.h>
112 #include <sys/syscallargs.h>
113
114 #include <vm/vm_kern.h>
115
116 #include <uvm/uvm_extern.h>
117
118 #include <dev/cons.h>
119
120 #include <machine/autoconf.h>
121 #include <machine/cpu.h>
122 #include <machine/reg.h>
123 #include <machine/rpb.h>
124 #include <machine/prom.h>
125 #include <machine/conf.h>
126 #include <machine/ieeefp.h>
127
128 #include <alpha/alpha/cpuvar.h>
129
130 #include <net/netisr.h>
131 #include <net/if.h>
132
133 #ifdef INET
134 #include <net/route.h>
135 #include <netinet/in.h>
136 #include <netinet/ip_var.h>
137 #include "arp.h"
138 #if NARP > 0
139 #include <netinet/if_inarp.h>
140 #endif
141 #endif
142 #ifdef INET6
143 # ifndef INET
144 # include <netinet/in.h>
145 # endif
146 #include <netinet6/ip6.h>
147 #include <netinet6/ip6_var.h>
148 #endif
149 #ifdef NS
150 #include <netns/ns_var.h>
151 #endif
152 #ifdef ISO
153 #include <netiso/iso.h>
154 #include <netiso/clnp.h>
155 #endif
156 #ifdef CCITT
157 #include <netccitt/x25.h>
158 #include <netccitt/pk.h>
159 #include <netccitt/pk_extern.h>
160 #endif
161 #ifdef NATM
162 #include <netnatm/natm.h>
163 #endif
164 #ifdef NETATALK
165 #include <netatalk/at_extern.h>
166 #endif
167 #include "ppp.h"
168 #if NPPP > 0
169 #include <net/ppp_defs.h>
170 #include <net/if_ppp.h>
171 #endif
172
173 #ifdef DDB
174 #include <machine/db_machdep.h>
175 #include <ddb/db_access.h>
176 #include <ddb/db_sym.h>
177 #include <ddb/db_extern.h>
178 #include <ddb/db_interface.h>
179 #endif
180
181 #include <machine/alpha.h>
182 #include <machine/intrcnt.h>
183
184 #include "com.h"
185 #if NCOM > 0
186 extern void comsoft __P((void));
187 #endif
188
189 vm_map_t exec_map = NULL;
190 vm_map_t mb_map = NULL;
191 vm_map_t phys_map = NULL;
192
193 caddr_t msgbufaddr;
194
195 int maxmem; /* max memory per process */
196
197 int totalphysmem; /* total amount of physical memory in system */
198 int physmem; /* physical memory used by NetBSD + some rsvd */
199 int resvmem; /* amount of memory reserved for PROM */
200 int unusedmem; /* amount of memory for OS that we don't use */
201 int unknownmem; /* amount of memory with an unknown use */
202
203 int cputype; /* system type, from the RPB */
204
205 /*
206 * XXX We need an address to which we can assign things so that they
207 * won't be optimized away because we didn't use the value.
208 */
209 u_int32_t no_optimize;
210
211 /* the following is used externally (sysctl_hw) */
212 char machine[] = MACHINE; /* from <machine/param.h> */
213 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */
214 char cpu_model[128];
215
216 struct user *proc0paddr;
217
218 /* Number of machine cycles per microsecond */
219 u_int64_t cycles_per_usec;
220
221 /* number of cpus in the box. really! */
222 int ncpus;
223
224 /* machine check info array, valloc()'ed in mdallocsys() */
225
226 static struct mchkinfo startup_info,
227 *mchkinfo_all_cpus;
228
229 struct bootinfo_kernel bootinfo;
230
231 /* For built-in TCDS */
232 #if defined(DEC_3000_300) || defined(DEC_3000_500)
233 u_int8_t dec_3000_scsiid[2], dec_3000_scsifast[2];
234 #endif
235
236 struct platform platform;
237
238 u_int32_t vm_kmem_size = _VM_KMEM_SIZE;
239 u_int32_t vm_phys_size = _VM_PHYS_SIZE;
240
241 #ifdef DDB
242 /* start and end of kernel symbol table */
243 void *ksym_start, *ksym_end;
244 #endif
245
246 /* for cpu_sysctl() */
247 int alpha_unaligned_print = 1; /* warn about unaligned accesses */
248 int alpha_unaligned_fix = 1; /* fix up unaligned accesses */
249 int alpha_unaligned_sigbus = 0; /* don't SIGBUS on fixed-up accesses */
250
251 /*
252 * XXX This should be dynamically sized, but we have the chicken-egg problem!
253 * XXX it should also be larger than it is, because not all of the mddt
254 * XXX clusters end up being used for VM.
255 */
256 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX]; /* low size bits overloaded */
257 int mem_cluster_cnt;
258
259 int cpu_dump __P((void));
260 int cpu_dumpsize __P((void));
261 u_long cpu_dump_mempagecnt __P((void));
262 void dumpsys __P((void));
263 void identifycpu __P((void));
264 caddr_t mdallocsys __P((caddr_t));
265 void netintr __P((void));
266 void printregs __P((struct reg *));
267
268 void
269 alpha_init(pfn, ptb, bim, bip, biv)
270 u_long pfn; /* first free PFN number */
271 u_long ptb; /* PFN of current level 1 page table */
272 u_long bim; /* bootinfo magic */
273 u_long bip; /* bootinfo pointer */
274 u_long biv; /* bootinfo version */
275 {
276 extern char kernel_text[], _end[];
277 struct mddt *mddtp;
278 struct mddt_cluster *memc;
279 int i, mddtweird;
280 struct vm_physseg *vps;
281 vaddr_t kernstart, kernend;
282 paddr_t kernstartpfn, kernendpfn, pfn0, pfn1;
283 vsize_t size;
284 char *p;
285 caddr_t v;
286 char *bootinfo_msg;
287
288 /* NO OUTPUT ALLOWED UNTIL FURTHER NOTICE */
289
290 /*
291 * Turn off interrupts (not mchecks) and floating point.
292 * Make sure the instruction and data streams are consistent.
293 */
294 (void)alpha_pal_swpipl(ALPHA_PSL_IPL_HIGH);
295 alpha_pal_wrfen(0);
296 ALPHA_TBIA();
297 alpha_pal_imb();
298
299 /*
300 * Get critical system information (if possible, from the
301 * information provided by the boot program).
302 */
303 bootinfo_msg = NULL;
304 if (bim == BOOTINFO_MAGIC) {
305 if (biv == 0) { /* backward compat */
306 biv = *(u_long *)bip;
307 bip += 8;
308 }
309 switch (biv) {
310 case 1: {
311 struct bootinfo_v1 *v1p = (struct bootinfo_v1 *)bip;
312
313 bootinfo.ssym = v1p->ssym;
314 bootinfo.esym = v1p->esym;
315 /* hwrpb may not be provided by boot block in v1 */
316 if (v1p->hwrpb != NULL) {
317 bootinfo.hwrpb_phys =
318 ((struct rpb *)v1p->hwrpb)->rpb_phys;
319 bootinfo.hwrpb_size = v1p->hwrpbsize;
320 } else {
321 bootinfo.hwrpb_phys =
322 ((struct rpb *)HWRPB_ADDR)->rpb_phys;
323 bootinfo.hwrpb_size =
324 ((struct rpb *)HWRPB_ADDR)->rpb_size;
325 }
326 bcopy(v1p->boot_flags, bootinfo.boot_flags,
327 min(sizeof v1p->boot_flags,
328 sizeof bootinfo.boot_flags));
329 bcopy(v1p->booted_kernel, bootinfo.booted_kernel,
330 min(sizeof v1p->booted_kernel,
331 sizeof bootinfo.booted_kernel));
332 /* booted dev not provided in bootinfo */
333 init_prom_interface((struct rpb *)
334 ALPHA_PHYS_TO_K0SEG(bootinfo.hwrpb_phys));
335 prom_getenv(PROM_E_BOOTED_DEV, bootinfo.booted_dev,
336 sizeof bootinfo.booted_dev);
337 break;
338 }
339 default:
340 bootinfo_msg = "unknown bootinfo version";
341 goto nobootinfo;
342 }
343 } else {
344 bootinfo_msg = "boot program did not pass bootinfo";
345 nobootinfo:
346 bootinfo.ssym = (u_long)_end;
347 bootinfo.esym = (u_long)_end;
348 bootinfo.hwrpb_phys = ((struct rpb *)HWRPB_ADDR)->rpb_phys;
349 bootinfo.hwrpb_size = ((struct rpb *)HWRPB_ADDR)->rpb_size;
350 init_prom_interface((struct rpb *)HWRPB_ADDR);
351 prom_getenv(PROM_E_BOOTED_OSFLAGS, bootinfo.boot_flags,
352 sizeof bootinfo.boot_flags);
353 prom_getenv(PROM_E_BOOTED_FILE, bootinfo.booted_kernel,
354 sizeof bootinfo.booted_kernel);
355 prom_getenv(PROM_E_BOOTED_DEV, bootinfo.booted_dev,
356 sizeof bootinfo.booted_dev);
357 }
358
359 /*
360 * Initialize the kernel's mapping of the RPB. It's needed for
361 * lots of things.
362 */
363 hwrpb = (struct rpb *)ALPHA_PHYS_TO_K0SEG(bootinfo.hwrpb_phys);
364
365 #if defined(DEC_3000_300) || defined(DEC_3000_500)
366 if (hwrpb->rpb_type == ST_DEC_3000_300 ||
367 hwrpb->rpb_type == ST_DEC_3000_500) {
368 prom_getenv(PROM_E_SCSIID, dec_3000_scsiid,
369 sizeof(dec_3000_scsiid));
370 prom_getenv(PROM_E_SCSIFAST, dec_3000_scsifast,
371 sizeof(dec_3000_scsifast));
372 }
373 #endif
374
375 /*
376 * Remember how many cycles there are per microsecond,
377 * so that we can use delay(). Round up, for safety.
378 */
379 cycles_per_usec = (hwrpb->rpb_cc_freq + 999999) / 1000000;
380
381 /*
382 * Initalize the (temporary) bootstrap console interface, so
383 * we can use printf until the VM system starts being setup.
384 * The real console is initialized before then.
385 */
386 init_bootstrap_console();
387
388 /* OUTPUT NOW ALLOWED */
389
390 /* delayed from above */
391 if (bootinfo_msg)
392 printf("WARNING: %s (0x%lx, 0x%lx, 0x%lx)\n",
393 bootinfo_msg, bim, bip, biv);
394
395 /* Initialize the trap vectors on the primary processor. */
396 trap_init();
397
398 /*
399 * Find out what hardware we're on, and do basic initialization.
400 */
401 cputype = hwrpb->rpb_type;
402 if (cputype < 0) {
403 /*
404 * At least some white-box systems have SRM which
405 * reports a systype that's the negative of their
406 * blue-box counterpart.
407 */
408 cputype = -cputype;
409 }
410 if (cputype >= ncpuinit) {
411 platform_not_supported();
412 /* NOTREACHED */
413 }
414 (*cpuinit[cputype].init)();
415 strcpy(cpu_model, platform.model);
416
417 /*
418 * Initalize the real console, so the the bootstrap console is
419 * no longer necessary.
420 */
421 (*platform.cons_init)();
422
423 #ifdef DIAGNOSTIC
424 /* Paranoid sanity checking */
425
426 /* We should always be running on the the primary. */
427 assert(hwrpb->rpb_primary_cpu_id == alpha_pal_whami());
428
429 /*
430 * On single-CPU systypes, the primary should always be CPU 0,
431 * except on Alpha 8200 systems where the CPU id is related
432 * to the VID, which is related to the Turbo Laser node id.
433 */
434 if (cputype != ST_DEC_21000)
435 assert(hwrpb->rpb_primary_cpu_id == 0);
436 #endif
437
438 /* NO MORE FIRMWARE ACCESS ALLOWED */
439 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
440 /*
441 * XXX (unless _PMAP_MAY_USE_PROM_CONSOLE is defined and
442 * XXX pmap_uses_prom_console() evaluates to non-zero.)
443 */
444 #endif
445
446 /*
447 * find out this system's page size
448 */
449 PAGE_SIZE = hwrpb->rpb_page_size;
450 if (PAGE_SIZE != 8192)
451 panic("page size %d != 8192?!", PAGE_SIZE);
452
453 /*
454 * Initialize PAGE_SIZE-dependent variables.
455 */
456 uvm_setpagesize();
457
458 /*
459 * Find the beginning and end of the kernel (and leave a
460 * bit of space before the beginning for the bootstrap
461 * stack).
462 */
463 kernstart = trunc_page(kernel_text) - 2 * PAGE_SIZE;
464 #ifdef DDB
465 ksym_start = (void *)bootinfo.ssym;
466 ksym_end = (void *)bootinfo.esym;
467 kernend = (vaddr_t)round_page(ksym_end);
468 #else
469 kernend = (vaddr_t)round_page(_end);
470 #endif
471
472 kernstartpfn = atop(ALPHA_K0SEG_TO_PHYS(kernstart));
473 kernendpfn = atop(ALPHA_K0SEG_TO_PHYS(kernend));
474
475 /*
476 * Find out how much memory is available, by looking at
477 * the memory cluster descriptors. This also tries to do
478 * its best to detect things things that have never been seen
479 * before...
480 */
481 mddtp = (struct mddt *)(((caddr_t)hwrpb) + hwrpb->rpb_memdat_off);
482
483 /* MDDT SANITY CHECKING */
484 mddtweird = 0;
485 if (mddtp->mddt_cluster_cnt < 2) {
486 mddtweird = 1;
487 printf("WARNING: weird number of mem clusters: %lu\n",
488 mddtp->mddt_cluster_cnt);
489 }
490
491 #if 0
492 printf("Memory cluster count: %d\n", mddtp->mddt_cluster_cnt);
493 #endif
494
495 for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
496 memc = &mddtp->mddt_clusters[i];
497 #if 0
498 printf("MEMC %d: pfn 0x%lx cnt 0x%lx usage 0x%lx\n", i,
499 memc->mddt_pfn, memc->mddt_pg_cnt, memc->mddt_usage);
500 #endif
501 totalphysmem += memc->mddt_pg_cnt;
502 if (mem_cluster_cnt < VM_PHYSSEG_MAX) { /* XXX */
503 mem_clusters[mem_cluster_cnt].start =
504 ptoa(memc->mddt_pfn);
505 mem_clusters[mem_cluster_cnt].size =
506 ptoa(memc->mddt_pg_cnt);
507 if (memc->mddt_usage & MDDT_mbz ||
508 memc->mddt_usage & MDDT_NONVOLATILE || /* XXX */
509 memc->mddt_usage & MDDT_PALCODE)
510 mem_clusters[mem_cluster_cnt].size |=
511 PROT_READ;
512 else
513 mem_clusters[mem_cluster_cnt].size |=
514 PROT_READ | PROT_WRITE | PROT_EXEC;
515 mem_cluster_cnt++;
516 }
517
518 if (memc->mddt_usage & MDDT_mbz) {
519 mddtweird = 1;
520 printf("WARNING: mem cluster %d has weird "
521 "usage 0x%lx\n", i, memc->mddt_usage);
522 unknownmem += memc->mddt_pg_cnt;
523 continue;
524 }
525 if (memc->mddt_usage & MDDT_NONVOLATILE) {
526 /* XXX should handle these... */
527 printf("WARNING: skipping non-volatile mem "
528 "cluster %d\n", i);
529 unusedmem += memc->mddt_pg_cnt;
530 continue;
531 }
532 if (memc->mddt_usage & MDDT_PALCODE) {
533 resvmem += memc->mddt_pg_cnt;
534 continue;
535 }
536
537 /*
538 * We have a memory cluster available for system
539 * software use. We must determine if this cluster
540 * holds the kernel.
541 */
542 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
543 /*
544 * XXX If the kernel uses the PROM console, we only use the
545 * XXX memory after the kernel in the first system segment,
546 * XXX to avoid clobbering prom mapping, data, etc.
547 */
548 if (!pmap_uses_prom_console() || physmem == 0) {
549 #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
550 physmem += memc->mddt_pg_cnt;
551 pfn0 = memc->mddt_pfn;
552 pfn1 = memc->mddt_pfn + memc->mddt_pg_cnt;
553 if (pfn0 <= kernstartpfn && kernendpfn <= pfn1) {
554 /*
555 * Must compute the location of the kernel
556 * within the segment.
557 */
558 #if 0
559 printf("Cluster %d contains kernel\n", i);
560 #endif
561 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
562 if (!pmap_uses_prom_console()) {
563 #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
564 if (pfn0 < kernstartpfn) {
565 /*
566 * There is a chunk before the kernel.
567 */
568 #if 0
569 printf("Loading chunk before kernel: "
570 "0x%lx / 0x%lx\n", pfn0, kernstartpfn);
571 #endif
572 uvm_page_physload(pfn0, kernstartpfn,
573 pfn0, kernstartpfn, VM_FREELIST_DEFAULT);
574 }
575 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
576 }
577 #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
578 if (kernendpfn < pfn1) {
579 /*
580 * There is a chunk after the kernel.
581 */
582 #if 0
583 printf("Loading chunk after kernel: "
584 "0x%lx / 0x%lx\n", kernendpfn, pfn1);
585 #endif
586 uvm_page_physload(kernendpfn, pfn1,
587 kernendpfn, pfn1, VM_FREELIST_DEFAULT);
588 }
589 } else {
590 /*
591 * Just load this cluster as one chunk.
592 */
593 #if 0
594 printf("Loading cluster %d: 0x%lx / 0x%lx\n", i,
595 pfn0, pfn1);
596 #endif
597 uvm_page_physload(pfn0, pfn1, pfn0, pfn1,
598 VM_FREELIST_DEFAULT);
599 }
600 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
601 }
602 #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
603 }
604
605 /*
606 * Dump out the MDDT if it looks odd...
607 */
608 if (mddtweird) {
609 printf("\n");
610 printf("complete memory cluster information:\n");
611 for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
612 printf("mddt %d:\n", i);
613 printf("\tpfn %lx\n",
614 mddtp->mddt_clusters[i].mddt_pfn);
615 printf("\tcnt %lx\n",
616 mddtp->mddt_clusters[i].mddt_pg_cnt);
617 printf("\ttest %lx\n",
618 mddtp->mddt_clusters[i].mddt_pg_test);
619 printf("\tbva %lx\n",
620 mddtp->mddt_clusters[i].mddt_v_bitaddr);
621 printf("\tbpa %lx\n",
622 mddtp->mddt_clusters[i].mddt_p_bitaddr);
623 printf("\tbcksum %lx\n",
624 mddtp->mddt_clusters[i].mddt_bit_cksum);
625 printf("\tusage %lx\n",
626 mddtp->mddt_clusters[i].mddt_usage);
627 }
628 printf("\n");
629 }
630
631 if (totalphysmem == 0)
632 panic("can't happen: system seems to have no memory!");
633
634 #ifdef LIMITMEM
635 /*
636 * XXX Kludge so we can run on machines with memory larger
637 * XXX than 1G until all device drivers are converted to
638 * XXX use bus_dma. (Relies on the fact that vm_physmem
639 * XXX sorted in order of increasing addresses.)
640 */
641 if (vm_physmem[vm_nphysseg - 1].end > atop(LIMITMEM * 1024 * 1024)) {
642
643 printf("******** LIMITING MEMORY TO %dMB **********\n",
644 LIMITMEM);
645
646 do {
647 u_long ovf;
648
649 vps = &vm_physmem[vm_nphysseg - 1];
650
651 if (vps->start >= atop(LIMITMEM * 1024 * 1024)) {
652 /*
653 * If the start is too high, just drop
654 * the whole segment.
655 *
656 * XXX can start != avail_start in this
657 * XXX case? wouldn't that mean that
658 * XXX some memory was stolen above the
659 * XXX limit? What to do?
660 */
661 ovf = vps->end - vps->start;
662 vm_nphysseg--;
663 } else {
664 /*
665 * If the start is OK, calculate how much
666 * to drop and drop it.
667 */
668 ovf = vps->end - atop(LIMITMEM * 1024 * 1024);
669 vps->end -= ovf;
670 vps->avail_end -= ovf;
671 }
672 physmem -= ovf;
673 unusedmem += ovf;
674 } while (vps->end > atop(LIMITMEM * 1024 * 1024));
675 }
676 #endif /* LIMITMEM */
677
678 maxmem = physmem;
679
680 #if 0
681 printf("totalphysmem = %d\n", totalphysmem);
682 printf("physmem = %d\n", physmem);
683 printf("resvmem = %d\n", resvmem);
684 printf("unusedmem = %d\n", unusedmem);
685 printf("unknownmem = %d\n", unknownmem);
686 #endif
687
688 /*
689 * Adjust some parameters if the amount of physmem
690 * available would cause us to croak. This is completely
691 * eyeballed and isn't meant to be the final answer.
692 * vm_phys_size is probably the only one to really worry
693 * about.
694 *
695 * It's for booting a GENERIC kernel on a large memory platform.
696 */
697 if (physmem >= atop(128 * 1024 * 1024)) {
698 vm_kmem_size <<= 3;
699 vm_phys_size <<= 2;
700 }
701
702 /*
703 * Initialize error message buffer (at end of core).
704 */
705 {
706 size_t sz = round_page(MSGBUFSIZE);
707
708 vps = &vm_physmem[vm_nphysseg - 1];
709
710 /* shrink so that it'll fit in the last segment */
711 if ((vps->avail_end - vps->avail_start) < atop(sz))
712 sz = ptoa(vps->avail_end - vps->avail_start);
713
714 vps->end -= atop(sz);
715 vps->avail_end -= atop(sz);
716 msgbufaddr = (caddr_t) ALPHA_PHYS_TO_K0SEG(ptoa(vps->end));
717 initmsgbuf(msgbufaddr, sz);
718
719 /* Remove the last segment if it now has no pages. */
720 if (vps->start == vps->end)
721 vm_nphysseg--;
722
723 /* warn if the message buffer had to be shrunk */
724 if (sz != round_page(MSGBUFSIZE))
725 printf("WARNING: %ld bytes not available for msgbuf in last cluster (%ld used)\n",
726 round_page(MSGBUFSIZE), sz);
727
728 }
729
730 /*
731 * Init mapping for u page(s) for proc 0
732 */
733 proc0.p_addr = proc0paddr =
734 (struct user *)pmap_steal_memory(UPAGES * PAGE_SIZE, NULL, NULL);
735
736 /*
737 * Allocate space for system data structures. These data structures
738 * are allocated here instead of cpu_startup() because physical
739 * memory is directly addressable. We don't have to map these into
740 * virtual address space.
741 */
742 size = (vsize_t)allocsys(NULL, mdallocsys);
743 v = (caddr_t)pmap_steal_memory(size, NULL, NULL);
744 if ((allocsys(v, mdallocsys) - v) != size)
745 panic("alpha_init: table size inconsistency");
746
747 /*
748 * Initialize the virtual memory system, and set the
749 * page table base register in proc 0's PCB.
750 */
751 pmap_bootstrap(ALPHA_PHYS_TO_K0SEG(ptb << PGSHIFT),
752 hwrpb->rpb_max_asn, hwrpb->rpb_pcs_cnt);
753
754 /*
755 * Initialize the rest of proc 0's PCB, and cache its physical
756 * address.
757 */
758 proc0.p_md.md_pcbpaddr =
759 (struct pcb *)ALPHA_K0SEG_TO_PHYS((vaddr_t)&proc0paddr->u_pcb);
760
761 /*
762 * Set the kernel sp, reserving space for an (empty) trapframe,
763 * and make proc0's trapframe pointer point to it for sanity.
764 */
765 proc0paddr->u_pcb.pcb_hw.apcb_ksp =
766 (u_int64_t)proc0paddr + USPACE - sizeof(struct trapframe);
767 proc0.p_md.md_tf =
768 (struct trapframe *)proc0paddr->u_pcb.pcb_hw.apcb_ksp;
769
770 /*
771 * Look at arguments passed to us and compute boothowto.
772 */
773
774 boothowto = RB_SINGLE;
775 #ifdef KADB
776 boothowto |= RB_KDB;
777 #endif
778 for (p = bootinfo.boot_flags; p && *p != '\0'; p++) {
779 /*
780 * Note that we'd really like to differentiate case here,
781 * but the Alpha AXP Architecture Reference Manual
782 * says that we shouldn't.
783 */
784 switch (*p) {
785 case 'a': /* autoboot */
786 case 'A':
787 boothowto &= ~RB_SINGLE;
788 break;
789
790 #ifdef DEBUG
791 case 'c': /* crash dump immediately after autoconfig */
792 case 'C':
793 boothowto |= RB_DUMP;
794 break;
795 #endif
796
797 #if defined(KGDB) || defined(DDB)
798 case 'd': /* break into the kernel debugger ASAP */
799 case 'D':
800 boothowto |= RB_KDB;
801 break;
802 #endif
803
804 case 'h': /* always halt, never reboot */
805 case 'H':
806 boothowto |= RB_HALT;
807 break;
808
809 #if 0
810 case 'm': /* mini root present in memory */
811 case 'M':
812 boothowto |= RB_MINIROOT;
813 break;
814 #endif
815
816 case 'n': /* askname */
817 case 'N':
818 boothowto |= RB_ASKNAME;
819 break;
820
821 case 's': /* single-user (default, supported for sanity) */
822 case 'S':
823 boothowto |= RB_SINGLE;
824 break;
825
826 case '-':
827 /*
828 * Just ignore this. It's not required, but it's
829 * common for it to be passed regardless.
830 */
831 break;
832
833 default:
834 printf("Unrecognized boot flag '%c'.\n", *p);
835 break;
836 }
837 }
838
839
840 /*
841 * Figure out the number of cpus in the box, from RPB fields.
842 * Really. We mean it.
843 */
844 for (i = 0; i < hwrpb->rpb_pcs_cnt; i++) {
845 struct pcs *pcsp;
846
847 pcsp = LOCATE_PCS(hwrpb, i);
848 if ((pcsp->pcs_flags & PCS_PP) != 0)
849 ncpus++;
850 }
851
852 /*
853 * Initialize debuggers, and break into them if appropriate.
854 */
855 #ifdef DDB
856 db_machine_init();
857 ddb_init((int)((u_int64_t)ksym_end - (u_int64_t)ksym_start),
858 ksym_start, ksym_end);
859 if (boothowto & RB_KDB)
860 Debugger();
861 #endif
862 #ifdef KGDB
863 if (boothowto & RB_KDB)
864 kgdb_connect(0);
865 #endif
866 /*
867 * Figure out our clock frequency, from RPB fields.
868 */
869 hz = hwrpb->rpb_intr_freq >> 12;
870 if (!(60 <= hz && hz <= 10240)) {
871 hz = 1024;
872 #ifdef DIAGNOSTIC
873 printf("WARNING: unbelievable rpb_intr_freq: %ld (%d hz)\n",
874 hwrpb->rpb_intr_freq, hz);
875 #endif
876 }
877 }
878
879 caddr_t
880 mdallocsys(v)
881 caddr_t v;
882 {
883 /*
884 * There appears to be a correlation between the number
885 * of processor slots defined in the HWRPB and the whami
886 * value that can be returned.
887 */
888 ALLOCSYS(v, mchkinfo_all_cpus, struct mchkinfo, hwrpb->rpb_pcs_cnt);
889 return (v);
890 }
891
892 void
893 consinit()
894 {
895
896 /*
897 * Everything related to console initialization is done
898 * in alpha_init().
899 */
900 #if defined(DIAGNOSTIC) && defined(_PMAP_MAY_USE_PROM_CONSOLE)
901 printf("consinit: %susing prom console\n",
902 pmap_uses_prom_console() ? "" : "not ");
903 #endif
904 }
905
906 #include "pckbc.h"
907 #include "pckbd.h"
908 #if (NPCKBC > 0) && (NPCKBD == 0)
909
910 #include <dev/isa/pckbcvar.h>
911
912 /*
913 * This is called by the pbkbc driver if no pckbd is configured.
914 * On the i386, it is used to glue in the old, deprecated console
915 * code. On the Alpha, it does nothing.
916 */
917 int
918 pckbc_machdep_cnattach(kbctag, kbcslot)
919 pckbc_tag_t kbctag;
920 pckbc_slot_t kbcslot;
921 {
922
923 return (ENXIO);
924 }
925 #endif /* NPCKBC > 0 && NPCKBD == 0 */
926
927 void
928 cpu_startup()
929 {
930 register unsigned i;
931 int base, residual;
932 vaddr_t minaddr, maxaddr;
933 vsize_t size;
934 char pbuf[9];
935 #if defined(DEBUG)
936 extern int pmapdebug;
937 int opmapdebug = pmapdebug;
938
939 pmapdebug = 0;
940 #endif
941
942 /*
943 * Good {morning,afternoon,evening,night}.
944 */
945 printf(version);
946 identifycpu();
947 format_bytes(pbuf, sizeof(pbuf), totalphysmem << PAGE_SHIFT);
948 printf("total memory = %s\n", pbuf);
949 format_bytes(pbuf, sizeof(pbuf), ptoa(resvmem));
950 printf("(%s reserved for PROM, ", pbuf);
951 format_bytes(pbuf, sizeof(pbuf), ptoa(physmem));
952 printf("%s used by NetBSD)\n", pbuf);
953 if (unusedmem) {
954 format_bytes(pbuf, sizeof(pbuf), ctob(unusedmem));
955 printf("WARNING: unused memory = %s\n", pbuf);
956 }
957 if (unknownmem) {
958 format_bytes(pbuf, sizeof(pbuf), ctob(unknownmem));
959 printf("WARNING: %s of memory with unknown purpose\n", pbuf);
960 }
961
962 /*
963 * Allocate virtual address space for file I/O buffers.
964 * Note they are different than the array of headers, 'buf',
965 * and usually occupy more virtual memory than physical.
966 */
967 size = MAXBSIZE * nbuf;
968 if (uvm_map(kernel_map, (vaddr_t *) &buffers, round_page(size),
969 NULL, UVM_UNKNOWN_OFFSET,
970 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
971 UVM_ADV_NORMAL, 0)) != KERN_SUCCESS)
972 panic("startup: cannot allocate VM for buffers");
973 base = bufpages / nbuf;
974 residual = bufpages % nbuf;
975 for (i = 0; i < nbuf; i++) {
976 vsize_t curbufsize;
977 vaddr_t curbuf;
978 struct vm_page *pg;
979
980 /*
981 * Each buffer has MAXBSIZE bytes of VM space allocated. Of
982 * that MAXBSIZE space, we allocate and map (base+1) pages
983 * for the first "residual" buffers, and then we allocate
984 * "base" pages for the rest.
985 */
986 curbuf = (vaddr_t) buffers + (i * MAXBSIZE);
987 curbufsize = CLBYTES * ((i < residual) ? (base+1) : base);
988
989 while (curbufsize) {
990 pg = uvm_pagealloc(NULL, 0, NULL, 0);
991 if (pg == NULL)
992 panic("cpu_startup: not enough memory for "
993 "buffer cache");
994 #if defined(PMAP_NEW)
995 pmap_kenter_pgs(curbuf, &pg, 1);
996 #else
997 pmap_enter(kernel_map->pmap, curbuf,
998 VM_PAGE_TO_PHYS(pg), VM_PROT_READ|VM_PROT_WRITE,
999 TRUE, VM_PROT_READ|VM_PROT_WRITE);
1000 #endif
1001 curbuf += PAGE_SIZE;
1002 curbufsize -= PAGE_SIZE;
1003 }
1004 }
1005 /*
1006 * Allocate a submap for exec arguments. This map effectively
1007 * limits the number of processes exec'ing at any time.
1008 */
1009 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
1010 16 * NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
1011
1012 /*
1013 * Allocate a submap for physio
1014 */
1015 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
1016 VM_PHYS_SIZE, 0, FALSE, NULL);
1017
1018 /*
1019 * No need to allocate an mbuf cluster submap. Mbuf clusters
1020 * are allocated via the pool allocator, and we use K0SEG to
1021 * map those pages.
1022 */
1023
1024 /*
1025 * Initialize callouts
1026 */
1027 callfree = callout;
1028 for (i = 1; i < ncallout; i++)
1029 callout[i-1].c_next = &callout[i];
1030 callout[i-1].c_next = NULL;
1031
1032 #if defined(DEBUG)
1033 pmapdebug = opmapdebug;
1034 #endif
1035 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
1036 printf("avail memory = %s\n", pbuf);
1037 #if 0
1038 {
1039 extern u_long pmap_pages_stolen;
1040
1041 format_bytes(pbuf, sizeof(pbuf), pmap_pages_stolen * PAGE_SIZE);
1042 printf("stolen memory for VM structures = %s\n", pbuf);
1043 }
1044 #endif
1045 format_bytes(pbuf, sizeof(pbuf), bufpages * CLBYTES);
1046 printf("using %ld buffers containing %s of memory\n", (long)nbuf, pbuf);
1047
1048 /*
1049 * Set up buffers, so they can be used to read disk labels.
1050 */
1051 bufinit();
1052
1053 /*
1054 * Set up the HWPCB so that it's safe to configure secondary
1055 * CPUs.
1056 */
1057 hwrpb_primary_init();
1058 }
1059
1060 /*
1061 * Retrieve the platform name from the DSR.
1062 */
1063 const char *
1064 alpha_dsr_sysname()
1065 {
1066 struct dsrdb *dsr;
1067 const char *sysname;
1068
1069 /*
1070 * DSR does not exist on early HWRPB versions.
1071 */
1072 if (hwrpb->rpb_version < HWRPB_DSRDB_MINVERS)
1073 return (NULL);
1074
1075 dsr = (struct dsrdb *)(((caddr_t)hwrpb) + hwrpb->rpb_dsrdb_off);
1076 sysname = (const char *)((caddr_t)dsr + (dsr->dsr_sysname_off +
1077 sizeof(u_int64_t)));
1078 return (sysname);
1079 }
1080
1081 /*
1082 * Lookup the system specified system variation in the provided table,
1083 * returning the model string on match.
1084 */
1085 const char *
1086 alpha_variation_name(variation, avtp)
1087 u_int64_t variation;
1088 const struct alpha_variation_table *avtp;
1089 {
1090 int i;
1091
1092 for (i = 0; avtp[i].avt_model != NULL; i++)
1093 if (avtp[i].avt_variation == variation)
1094 return (avtp[i].avt_model);
1095 return (NULL);
1096 }
1097
1098 /*
1099 * Generate a default platform name based for unknown system variations.
1100 */
1101 const char *
1102 alpha_unknown_sysname()
1103 {
1104 static char s[128]; /* safe size */
1105
1106 sprintf(s, "%s family, unknown model variation 0x%lx",
1107 platform.family, hwrpb->rpb_variation & SV_ST_MASK);
1108 return ((const char *)s);
1109 }
1110
1111 void
1112 identifycpu()
1113 {
1114 char *s;
1115
1116 /*
1117 * print out CPU identification information.
1118 */
1119 printf("%s", cpu_model);
1120 for(s = cpu_model; *s; ++s)
1121 if(strncasecmp(s, "MHz", 3) == 0)
1122 goto skipMHz;
1123 printf(", %ldMHz", hwrpb->rpb_cc_freq / 1000000);
1124 skipMHz:
1125 printf("\n");
1126 printf("%ld byte page size, %d processor%s.\n",
1127 hwrpb->rpb_page_size, ncpus, ncpus == 1 ? "" : "s");
1128 #if 0
1129 /* this isn't defined for any systems that we run on? */
1130 printf("serial number 0x%lx 0x%lx\n",
1131 ((long *)hwrpb->rpb_ssn)[0], ((long *)hwrpb->rpb_ssn)[1]);
1132
1133 /* and these aren't particularly useful! */
1134 printf("variation: 0x%lx, revision 0x%lx\n",
1135 hwrpb->rpb_variation, *(long *)hwrpb->rpb_revision);
1136 #endif
1137 }
1138
1139 int waittime = -1;
1140 struct pcb dumppcb;
1141
1142 void
1143 cpu_reboot(howto, bootstr)
1144 int howto;
1145 char *bootstr;
1146 {
1147 extern int cold;
1148 #if defined(MULTIPROCESSOR)
1149 #if 0 /* XXX See below. */
1150 u_long cpu_id;
1151 #endif
1152 #endif
1153
1154 #if defined(MULTIPROCESSOR)
1155 /* We must be running on the primary CPU. */
1156 if (alpha_pal_whami() != hwrpb->rpb_primary_cpu_id)
1157 panic("cpu_reboot: not on primary CPU!");
1158 #endif
1159
1160 /* If system is cold, just halt. */
1161 if (cold) {
1162 howto |= RB_HALT;
1163 goto haltsys;
1164 }
1165
1166 /* If "always halt" was specified as a boot flag, obey. */
1167 if ((boothowto & RB_HALT) != 0)
1168 howto |= RB_HALT;
1169
1170 boothowto = howto;
1171 if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
1172 waittime = 0;
1173 vfs_shutdown();
1174 /*
1175 * If we've been adjusting the clock, the todr
1176 * will be out of synch; adjust it now.
1177 */
1178 resettodr();
1179 }
1180
1181 /* Disable interrupts. */
1182 splhigh();
1183
1184 /* If rebooting and a dump is requested do it. */
1185 #if 0
1186 if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
1187 #else
1188 if (howto & RB_DUMP)
1189 #endif
1190 dumpsys();
1191
1192 haltsys:
1193
1194 /* run any shutdown hooks */
1195 doshutdownhooks();
1196
1197 #if defined(MULTIPROCESSOR)
1198 #if 0 /* XXX doesn't work when called from here?! */
1199 /* Kill off any secondary CPUs. */
1200 for (cpu_id = 0; cpu_id < hwrpb->rpb_pcs_cnt; cpu_id++) {
1201 if (cpu_id == hwrpb->rpb_primary_cpu_id ||
1202 cpu_info[cpu_id].ci_softc == NULL)
1203 continue;
1204 cpu_halt_secondary(cpu_id);
1205 }
1206 #endif
1207 #endif
1208
1209 #ifdef BOOTKEY
1210 printf("hit any key to %s...\n", howto & RB_HALT ? "halt" : "reboot");
1211 cnpollc(1); /* for proper keyboard command handling */
1212 cngetc();
1213 cnpollc(0);
1214 printf("\n");
1215 #endif
1216
1217 /* Finally, powerdown/halt/reboot the system. */
1218 if ((howto && RB_POWERDOWN) == RB_POWERDOWN &&
1219 platform.powerdown != NULL) {
1220 (*platform.powerdown)();
1221 printf("WARNING: powerdown failed!\n");
1222 }
1223 printf("%s\n\n", howto & RB_HALT ? "halted." : "rebooting...");
1224 prom_halt(howto & RB_HALT);
1225 /*NOTREACHED*/
1226 }
1227
1228 /*
1229 * These variables are needed by /sbin/savecore
1230 */
1231 u_long dumpmag = 0x8fca0101; /* magic number */
1232 int dumpsize = 0; /* pages */
1233 long dumplo = 0; /* blocks */
1234
1235 /*
1236 * cpu_dumpsize: calculate size of machine-dependent kernel core dump headers.
1237 */
1238 int
1239 cpu_dumpsize()
1240 {
1241 int size;
1242
1243 size = ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t)) +
1244 ALIGN(mem_cluster_cnt * sizeof(phys_ram_seg_t));
1245 if (roundup(size, dbtob(1)) != dbtob(1))
1246 return -1;
1247
1248 return (1);
1249 }
1250
1251 /*
1252 * cpu_dump_mempagecnt: calculate size of RAM (in pages) to be dumped.
1253 */
1254 u_long
1255 cpu_dump_mempagecnt()
1256 {
1257 u_long i, n;
1258
1259 n = 0;
1260 for (i = 0; i < mem_cluster_cnt; i++)
1261 n += atop(mem_clusters[i].size);
1262 return (n);
1263 }
1264
1265 /*
1266 * cpu_dump: dump machine-dependent kernel core dump headers.
1267 */
1268 int
1269 cpu_dump()
1270 {
1271 int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
1272 char buf[dbtob(1)];
1273 kcore_seg_t *segp;
1274 cpu_kcore_hdr_t *cpuhdrp;
1275 phys_ram_seg_t *memsegp;
1276 int i;
1277
1278 dump = bdevsw[major(dumpdev)].d_dump;
1279
1280 bzero(buf, sizeof buf);
1281 segp = (kcore_seg_t *)buf;
1282 cpuhdrp = (cpu_kcore_hdr_t *)&buf[ALIGN(sizeof(*segp))];
1283 memsegp = (phys_ram_seg_t *)&buf[ ALIGN(sizeof(*segp)) +
1284 ALIGN(sizeof(*cpuhdrp))];
1285
1286 /*
1287 * Generate a segment header.
1288 */
1289 CORE_SETMAGIC(*segp, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
1290 segp->c_size = dbtob(1) - ALIGN(sizeof(*segp));
1291
1292 /*
1293 * Add the machine-dependent header info.
1294 */
1295 cpuhdrp->lev1map_pa = ALPHA_K0SEG_TO_PHYS((vaddr_t)kernel_lev1map);
1296 cpuhdrp->page_size = PAGE_SIZE;
1297 cpuhdrp->nmemsegs = mem_cluster_cnt;
1298
1299 /*
1300 * Fill in the memory segment descriptors.
1301 */
1302 for (i = 0; i < mem_cluster_cnt; i++) {
1303 memsegp[i].start = mem_clusters[i].start;
1304 memsegp[i].size = mem_clusters[i].size & ~PAGE_MASK;
1305 }
1306
1307 return (dump(dumpdev, dumplo, (caddr_t)buf, dbtob(1)));
1308 }
1309
1310 /*
1311 * This is called by main to set dumplo and dumpsize.
1312 * Dumps always skip the first CLBYTES of disk space
1313 * in case there might be a disk label stored there.
1314 * If there is extra space, put dump at the end to
1315 * reduce the chance that swapping trashes it.
1316 */
1317 void
1318 cpu_dumpconf()
1319 {
1320 int nblks, dumpblks; /* size of dump area */
1321 int maj;
1322
1323 if (dumpdev == NODEV)
1324 goto bad;
1325 maj = major(dumpdev);
1326 if (maj < 0 || maj >= nblkdev)
1327 panic("dumpconf: bad dumpdev=0x%x", dumpdev);
1328 if (bdevsw[maj].d_psize == NULL)
1329 goto bad;
1330 nblks = (*bdevsw[maj].d_psize)(dumpdev);
1331 if (nblks <= ctod(1))
1332 goto bad;
1333
1334 dumpblks = cpu_dumpsize();
1335 if (dumpblks < 0)
1336 goto bad;
1337 dumpblks += ctod(cpu_dump_mempagecnt());
1338
1339 /* If dump won't fit (incl. room for possible label), punt. */
1340 if (dumpblks > (nblks - ctod(1)))
1341 goto bad;
1342
1343 /* Put dump at end of partition */
1344 dumplo = nblks - dumpblks;
1345
1346 /* dumpsize is in page units, and doesn't include headers. */
1347 dumpsize = cpu_dump_mempagecnt();
1348 return;
1349
1350 bad:
1351 dumpsize = 0;
1352 return;
1353 }
1354
1355 /*
1356 * Dump the kernel's image to the swap partition.
1357 */
1358 #define BYTES_PER_DUMP NBPG
1359
1360 void
1361 dumpsys()
1362 {
1363 u_long totalbytesleft, bytes, i, n, memcl;
1364 u_long maddr;
1365 int psize;
1366 daddr_t blkno;
1367 int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
1368 int error;
1369
1370 /* Save registers. */
1371 savectx(&dumppcb);
1372
1373 msgbufenabled = 0; /* don't record dump msgs in msgbuf */
1374 if (dumpdev == NODEV)
1375 return;
1376
1377 /*
1378 * For dumps during autoconfiguration,
1379 * if dump device has already configured...
1380 */
1381 if (dumpsize == 0)
1382 cpu_dumpconf();
1383 if (dumplo <= 0) {
1384 printf("\ndump to dev %u,%u not possible\n", major(dumpdev),
1385 minor(dumpdev));
1386 return;
1387 }
1388 printf("\ndumping to dev %u,%u offset %ld\n", major(dumpdev),
1389 minor(dumpdev), dumplo);
1390
1391 psize = (*bdevsw[major(dumpdev)].d_psize)(dumpdev);
1392 printf("dump ");
1393 if (psize == -1) {
1394 printf("area unavailable\n");
1395 return;
1396 }
1397
1398 /* XXX should purge all outstanding keystrokes. */
1399
1400 if ((error = cpu_dump()) != 0)
1401 goto err;
1402
1403 totalbytesleft = ptoa(cpu_dump_mempagecnt());
1404 blkno = dumplo + cpu_dumpsize();
1405 dump = bdevsw[major(dumpdev)].d_dump;
1406 error = 0;
1407
1408 for (memcl = 0; memcl < mem_cluster_cnt; memcl++) {
1409 maddr = mem_clusters[memcl].start;
1410 bytes = mem_clusters[memcl].size & ~PAGE_MASK;
1411
1412 for (i = 0; i < bytes; i += n, totalbytesleft -= n) {
1413
1414 /* Print out how many MBs we to go. */
1415 if ((totalbytesleft % (1024*1024)) == 0)
1416 printf("%ld ", totalbytesleft / (1024 * 1024));
1417
1418 /* Limit size for next transfer. */
1419 n = bytes - i;
1420 if (n > BYTES_PER_DUMP)
1421 n = BYTES_PER_DUMP;
1422
1423 error = (*dump)(dumpdev, blkno,
1424 (caddr_t)ALPHA_PHYS_TO_K0SEG(maddr), n);
1425 if (error)
1426 goto err;
1427 maddr += n;
1428 blkno += btodb(n); /* XXX? */
1429
1430 /* XXX should look for keystrokes, to cancel. */
1431 }
1432 }
1433
1434 err:
1435 switch (error) {
1436
1437 case ENXIO:
1438 printf("device bad\n");
1439 break;
1440
1441 case EFAULT:
1442 printf("device not ready\n");
1443 break;
1444
1445 case EINVAL:
1446 printf("area improper\n");
1447 break;
1448
1449 case EIO:
1450 printf("i/o error\n");
1451 break;
1452
1453 case EINTR:
1454 printf("aborted from console\n");
1455 break;
1456
1457 case 0:
1458 printf("succeeded\n");
1459 break;
1460
1461 default:
1462 printf("error %d\n", error);
1463 break;
1464 }
1465 printf("\n\n");
1466 delay(1000);
1467 }
1468
1469 void
1470 frametoreg(framep, regp)
1471 struct trapframe *framep;
1472 struct reg *regp;
1473 {
1474
1475 regp->r_regs[R_V0] = framep->tf_regs[FRAME_V0];
1476 regp->r_regs[R_T0] = framep->tf_regs[FRAME_T0];
1477 regp->r_regs[R_T1] = framep->tf_regs[FRAME_T1];
1478 regp->r_regs[R_T2] = framep->tf_regs[FRAME_T2];
1479 regp->r_regs[R_T3] = framep->tf_regs[FRAME_T3];
1480 regp->r_regs[R_T4] = framep->tf_regs[FRAME_T4];
1481 regp->r_regs[R_T5] = framep->tf_regs[FRAME_T5];
1482 regp->r_regs[R_T6] = framep->tf_regs[FRAME_T6];
1483 regp->r_regs[R_T7] = framep->tf_regs[FRAME_T7];
1484 regp->r_regs[R_S0] = framep->tf_regs[FRAME_S0];
1485 regp->r_regs[R_S1] = framep->tf_regs[FRAME_S1];
1486 regp->r_regs[R_S2] = framep->tf_regs[FRAME_S2];
1487 regp->r_regs[R_S3] = framep->tf_regs[FRAME_S3];
1488 regp->r_regs[R_S4] = framep->tf_regs[FRAME_S4];
1489 regp->r_regs[R_S5] = framep->tf_regs[FRAME_S5];
1490 regp->r_regs[R_S6] = framep->tf_regs[FRAME_S6];
1491 regp->r_regs[R_A0] = framep->tf_regs[FRAME_A0];
1492 regp->r_regs[R_A1] = framep->tf_regs[FRAME_A1];
1493 regp->r_regs[R_A2] = framep->tf_regs[FRAME_A2];
1494 regp->r_regs[R_A3] = framep->tf_regs[FRAME_A3];
1495 regp->r_regs[R_A4] = framep->tf_regs[FRAME_A4];
1496 regp->r_regs[R_A5] = framep->tf_regs[FRAME_A5];
1497 regp->r_regs[R_T8] = framep->tf_regs[FRAME_T8];
1498 regp->r_regs[R_T9] = framep->tf_regs[FRAME_T9];
1499 regp->r_regs[R_T10] = framep->tf_regs[FRAME_T10];
1500 regp->r_regs[R_T11] = framep->tf_regs[FRAME_T11];
1501 regp->r_regs[R_RA] = framep->tf_regs[FRAME_RA];
1502 regp->r_regs[R_T12] = framep->tf_regs[FRAME_T12];
1503 regp->r_regs[R_AT] = framep->tf_regs[FRAME_AT];
1504 regp->r_regs[R_GP] = framep->tf_regs[FRAME_GP];
1505 /* regp->r_regs[R_SP] = framep->tf_regs[FRAME_SP]; XXX */
1506 regp->r_regs[R_ZERO] = 0;
1507 }
1508
1509 void
1510 regtoframe(regp, framep)
1511 struct reg *regp;
1512 struct trapframe *framep;
1513 {
1514
1515 framep->tf_regs[FRAME_V0] = regp->r_regs[R_V0];
1516 framep->tf_regs[FRAME_T0] = regp->r_regs[R_T0];
1517 framep->tf_regs[FRAME_T1] = regp->r_regs[R_T1];
1518 framep->tf_regs[FRAME_T2] = regp->r_regs[R_T2];
1519 framep->tf_regs[FRAME_T3] = regp->r_regs[R_T3];
1520 framep->tf_regs[FRAME_T4] = regp->r_regs[R_T4];
1521 framep->tf_regs[FRAME_T5] = regp->r_regs[R_T5];
1522 framep->tf_regs[FRAME_T6] = regp->r_regs[R_T6];
1523 framep->tf_regs[FRAME_T7] = regp->r_regs[R_T7];
1524 framep->tf_regs[FRAME_S0] = regp->r_regs[R_S0];
1525 framep->tf_regs[FRAME_S1] = regp->r_regs[R_S1];
1526 framep->tf_regs[FRAME_S2] = regp->r_regs[R_S2];
1527 framep->tf_regs[FRAME_S3] = regp->r_regs[R_S3];
1528 framep->tf_regs[FRAME_S4] = regp->r_regs[R_S4];
1529 framep->tf_regs[FRAME_S5] = regp->r_regs[R_S5];
1530 framep->tf_regs[FRAME_S6] = regp->r_regs[R_S6];
1531 framep->tf_regs[FRAME_A0] = regp->r_regs[R_A0];
1532 framep->tf_regs[FRAME_A1] = regp->r_regs[R_A1];
1533 framep->tf_regs[FRAME_A2] = regp->r_regs[R_A2];
1534 framep->tf_regs[FRAME_A3] = regp->r_regs[R_A3];
1535 framep->tf_regs[FRAME_A4] = regp->r_regs[R_A4];
1536 framep->tf_regs[FRAME_A5] = regp->r_regs[R_A5];
1537 framep->tf_regs[FRAME_T8] = regp->r_regs[R_T8];
1538 framep->tf_regs[FRAME_T9] = regp->r_regs[R_T9];
1539 framep->tf_regs[FRAME_T10] = regp->r_regs[R_T10];
1540 framep->tf_regs[FRAME_T11] = regp->r_regs[R_T11];
1541 framep->tf_regs[FRAME_RA] = regp->r_regs[R_RA];
1542 framep->tf_regs[FRAME_T12] = regp->r_regs[R_T12];
1543 framep->tf_regs[FRAME_AT] = regp->r_regs[R_AT];
1544 framep->tf_regs[FRAME_GP] = regp->r_regs[R_GP];
1545 /* framep->tf_regs[FRAME_SP] = regp->r_regs[R_SP]; XXX */
1546 /* ??? = regp->r_regs[R_ZERO]; */
1547 }
1548
1549 void
1550 printregs(regp)
1551 struct reg *regp;
1552 {
1553 int i;
1554
1555 for (i = 0; i < 32; i++)
1556 printf("R%d:\t0x%016lx%s", i, regp->r_regs[i],
1557 i & 1 ? "\n" : "\t");
1558 }
1559
1560 void
1561 regdump(framep)
1562 struct trapframe *framep;
1563 {
1564 struct reg reg;
1565
1566 frametoreg(framep, ®);
1567 reg.r_regs[R_SP] = alpha_pal_rdusp();
1568
1569 printf("REGISTERS:\n");
1570 printregs(®);
1571 }
1572
1573 #ifdef DEBUG
1574 int sigdebug = 0;
1575 int sigpid = 0;
1576 #define SDB_FOLLOW 0x01
1577 #define SDB_KSTACK 0x02
1578 #endif
1579
1580 /*
1581 * Send an interrupt to process.
1582 */
1583 void
1584 sendsig(catcher, sig, mask, code)
1585 sig_t catcher;
1586 int sig;
1587 sigset_t *mask;
1588 u_long code;
1589 {
1590 struct proc *p = curproc;
1591 struct sigcontext *scp, ksc;
1592 struct trapframe *frame;
1593 struct sigacts *psp = p->p_sigacts;
1594 int onstack, fsize, rndfsize;
1595
1596 frame = p->p_md.md_tf;
1597
1598 /* Do we need to jump onto the signal stack? */
1599 onstack =
1600 (psp->ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
1601 (psp->ps_sigact[sig].sa_flags & SA_ONSTACK) != 0;
1602
1603 /* Allocate space for the signal handler context. */
1604 fsize = sizeof(ksc);
1605 rndfsize = ((fsize + 15) / 16) * 16;
1606
1607 if (onstack)
1608 scp = (struct sigcontext *)((caddr_t)psp->ps_sigstk.ss_sp +
1609 psp->ps_sigstk.ss_size);
1610 else
1611 scp = (struct sigcontext *)(alpha_pal_rdusp());
1612 scp = (struct sigcontext *)((caddr_t)scp - rndfsize);
1613
1614 #ifdef DEBUG
1615 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
1616 printf("sendsig(%d): sig %d ssp %p usp %p\n", p->p_pid,
1617 sig, &onstack, scp);
1618 #endif
1619
1620 /* Build stack frame for signal trampoline. */
1621 ksc.sc_pc = frame->tf_regs[FRAME_PC];
1622 ksc.sc_ps = frame->tf_regs[FRAME_PS];
1623
1624 /* Save register context. */
1625 frametoreg(frame, (struct reg *)ksc.sc_regs);
1626 ksc.sc_regs[R_ZERO] = 0xACEDBADE; /* magic number */
1627 ksc.sc_regs[R_SP] = alpha_pal_rdusp();
1628
1629 /* save the floating-point state, if necessary, then copy it. */
1630 if (p == fpcurproc) {
1631 alpha_pal_wrfen(1);
1632 savefpstate(&p->p_addr->u_pcb.pcb_fp);
1633 alpha_pal_wrfen(0);
1634 fpcurproc = NULL;
1635 }
1636 ksc.sc_ownedfp = p->p_md.md_flags & MDP_FPUSED;
1637 bcopy(&p->p_addr->u_pcb.pcb_fp, (struct fpreg *)ksc.sc_fpregs,
1638 sizeof(struct fpreg));
1639 ksc.sc_fp_control = 0; /* XXX ? */
1640 bzero(ksc.sc_reserved, sizeof ksc.sc_reserved); /* XXX */
1641 bzero(ksc.sc_xxx, sizeof ksc.sc_xxx); /* XXX */
1642
1643 /* Save signal stack. */
1644 ksc.sc_onstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
1645
1646 /* Save signal mask. */
1647 ksc.sc_mask = *mask;
1648
1649 #ifdef COMPAT_13
1650 /*
1651 * XXX We always have to save an old style signal mask because
1652 * XXX we might be delivering a signal to a process which will
1653 * XXX escape from the signal in a non-standard way and invoke
1654 * XXX sigreturn() directly.
1655 */
1656 {
1657 /* Note: it's a long in the stack frame. */
1658 sigset13_t mask13;
1659
1660 native_sigset_to_sigset13(mask, &mask13);
1661 ksc.__sc_mask13 = mask13;
1662 }
1663 #endif
1664
1665 #ifdef COMPAT_OSF1
1666 /*
1667 * XXX Create an OSF/1-style sigcontext and associated goo.
1668 */
1669 #endif
1670
1671 if (copyout(&ksc, (caddr_t)scp, fsize) != 0) {
1672 /*
1673 * Process has trashed its stack; give it an illegal
1674 * instruction to halt it in its tracks.
1675 */
1676 #ifdef DEBUG
1677 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
1678 printf("sendsig(%d): copyout failed on sig %d\n",
1679 p->p_pid, sig);
1680 #endif
1681 sigexit(p, SIGILL);
1682 /* NOTREACHED */
1683 }
1684 #ifdef DEBUG
1685 if (sigdebug & SDB_FOLLOW)
1686 printf("sendsig(%d): sig %d scp %p code %lx\n", p->p_pid, sig,
1687 scp, code);
1688 #endif
1689
1690 /* Set up the registers to return to sigcode. */
1691 frame->tf_regs[FRAME_PC] = (u_int64_t)psp->ps_sigcode;
1692 frame->tf_regs[FRAME_A0] = sig;
1693 frame->tf_regs[FRAME_A1] = code;
1694 frame->tf_regs[FRAME_A2] = (u_int64_t)scp;
1695 frame->tf_regs[FRAME_T12] = (u_int64_t)catcher; /* t12 is pv */
1696 alpha_pal_wrusp((unsigned long)scp);
1697
1698 /* Remember that we're now on the signal stack. */
1699 if (onstack)
1700 psp->ps_sigstk.ss_flags |= SS_ONSTACK;
1701
1702 #ifdef DEBUG
1703 if (sigdebug & SDB_FOLLOW)
1704 printf("sendsig(%d): pc %lx, catcher %lx\n", p->p_pid,
1705 frame->tf_regs[FRAME_PC], frame->tf_regs[FRAME_A3]);
1706 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
1707 printf("sendsig(%d): sig %d returns\n",
1708 p->p_pid, sig);
1709 #endif
1710 }
1711
1712 /*
1713 * System call to cleanup state after a signal
1714 * has been taken. Reset signal mask and
1715 * stack state from context left by sendsig (above).
1716 * Return to previous pc and psl as specified by
1717 * context left by sendsig. Check carefully to
1718 * make sure that the user has not modified the
1719 * psl to gain improper priviledges or to cause
1720 * a machine fault.
1721 */
1722 /* ARGSUSED */
1723 int
1724 sys___sigreturn14(p, v, retval)
1725 struct proc *p;
1726 void *v;
1727 register_t *retval;
1728 {
1729 struct sys___sigreturn14_args /* {
1730 syscallarg(struct sigcontext *) sigcntxp;
1731 } */ *uap = v;
1732 struct sigcontext *scp, ksc;
1733
1734 /*
1735 * The trampoline code hands us the context.
1736 * It is unsafe to keep track of it ourselves, in the event that a
1737 * program jumps out of a signal handler.
1738 */
1739 scp = SCARG(uap, sigcntxp);
1740 #ifdef DEBUG
1741 if (sigdebug & SDB_FOLLOW)
1742 printf("sigreturn: pid %d, scp %p\n", p->p_pid, scp);
1743 #endif
1744 if (ALIGN(scp) != (u_int64_t)scp)
1745 return (EINVAL);
1746
1747 if (copyin((caddr_t)scp, &ksc, sizeof(ksc)) != 0)
1748 return (EFAULT);
1749
1750 if (ksc.sc_regs[R_ZERO] != 0xACEDBADE) /* magic number */
1751 return (EINVAL);
1752
1753 /* Restore register context. */
1754 p->p_md.md_tf->tf_regs[FRAME_PC] = ksc.sc_pc;
1755 p->p_md.md_tf->tf_regs[FRAME_PS] =
1756 (ksc.sc_ps | ALPHA_PSL_USERSET) & ~ALPHA_PSL_USERCLR;
1757
1758 regtoframe((struct reg *)ksc.sc_regs, p->p_md.md_tf);
1759 alpha_pal_wrusp(ksc.sc_regs[R_SP]);
1760
1761 /* XXX ksc.sc_ownedfp ? */
1762 if (p == fpcurproc)
1763 fpcurproc = NULL;
1764 bcopy((struct fpreg *)ksc.sc_fpregs, &p->p_addr->u_pcb.pcb_fp,
1765 sizeof(struct fpreg));
1766 /* XXX ksc.sc_fp_control ? */
1767
1768 /* Restore signal stack. */
1769 if (ksc.sc_onstack & SS_ONSTACK)
1770 p->p_sigacts->ps_sigstk.ss_flags |= SS_ONSTACK;
1771 else
1772 p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
1773
1774 /* Restore signal mask. */
1775 (void) sigprocmask1(p, SIG_SETMASK, &ksc.sc_mask, 0);
1776
1777 #ifdef DEBUG
1778 if (sigdebug & SDB_FOLLOW)
1779 printf("sigreturn(%d): returns\n", p->p_pid);
1780 #endif
1781 return (EJUSTRETURN);
1782 }
1783
1784 /*
1785 * machine dependent system variables.
1786 */
1787 int
1788 cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
1789 int *name;
1790 u_int namelen;
1791 void *oldp;
1792 size_t *oldlenp;
1793 void *newp;
1794 size_t newlen;
1795 struct proc *p;
1796 {
1797 dev_t consdev;
1798
1799 /* all sysctl names at this level are terminal */
1800 if (namelen != 1)
1801 return (ENOTDIR); /* overloaded */
1802
1803 switch (name[0]) {
1804 case CPU_CONSDEV:
1805 if (cn_tab != NULL)
1806 consdev = cn_tab->cn_dev;
1807 else
1808 consdev = NODEV;
1809 return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
1810 sizeof consdev));
1811
1812 case CPU_ROOT_DEVICE:
1813 return (sysctl_rdstring(oldp, oldlenp, newp,
1814 root_device->dv_xname));
1815
1816 case CPU_UNALIGNED_PRINT:
1817 return (sysctl_int(oldp, oldlenp, newp, newlen,
1818 &alpha_unaligned_print));
1819
1820 case CPU_UNALIGNED_FIX:
1821 return (sysctl_int(oldp, oldlenp, newp, newlen,
1822 &alpha_unaligned_fix));
1823
1824 case CPU_UNALIGNED_SIGBUS:
1825 return (sysctl_int(oldp, oldlenp, newp, newlen,
1826 &alpha_unaligned_sigbus));
1827
1828 case CPU_BOOTED_KERNEL:
1829 return (sysctl_rdstring(oldp, oldlenp, newp,
1830 bootinfo.booted_kernel));
1831
1832 default:
1833 return (EOPNOTSUPP);
1834 }
1835 /* NOTREACHED */
1836 }
1837
1838 /*
1839 * Set registers on exec.
1840 */
1841 void
1842 setregs(p, pack, stack)
1843 register struct proc *p;
1844 struct exec_package *pack;
1845 u_long stack;
1846 {
1847 struct trapframe *tfp = p->p_md.md_tf;
1848 #ifdef DEBUG
1849 int i;
1850 #endif
1851
1852 #ifdef DEBUG
1853 /*
1854 * Crash and dump, if the user requested it.
1855 */
1856 if (boothowto & RB_DUMP)
1857 panic("crash requested by boot flags");
1858 #endif
1859
1860 #ifdef DEBUG
1861 for (i = 0; i < FRAME_SIZE; i++)
1862 tfp->tf_regs[i] = 0xbabefacedeadbeef;
1863 #else
1864 bzero(tfp->tf_regs, FRAME_SIZE * sizeof tfp->tf_regs[0]);
1865 #endif
1866 bzero(&p->p_addr->u_pcb.pcb_fp, sizeof p->p_addr->u_pcb.pcb_fp);
1867 p->p_addr->u_pcb.pcb_fp.fpr_cr = FPCR_INED
1868 | FPCR_UNFD
1869 | FPCR_UNDZ
1870 | FPCR_DYN(FP_RN)
1871 | FPCR_OVFD
1872 | FPCR_DZED
1873 | FPCR_INVD
1874 | FPCR_DNZ;
1875 alpha_pal_wrusp(stack);
1876 tfp->tf_regs[FRAME_PS] = ALPHA_PSL_USERSET;
1877 tfp->tf_regs[FRAME_PC] = pack->ep_entry & ~3;
1878
1879 tfp->tf_regs[FRAME_A0] = stack; /* a0 = sp */
1880 tfp->tf_regs[FRAME_A1] = 0; /* a1 = rtld cleanup */
1881 tfp->tf_regs[FRAME_A2] = 0; /* a2 = rtld object */
1882 tfp->tf_regs[FRAME_A3] = (u_int64_t)PS_STRINGS; /* a3 = ps_strings */
1883 tfp->tf_regs[FRAME_T12] = tfp->tf_regs[FRAME_PC]; /* a.k.a. PV */
1884
1885 p->p_md.md_flags &= ~MDP_FPUSED;
1886 if (fpcurproc == p)
1887 fpcurproc = NULL;
1888 }
1889
1890 void
1891 netintr()
1892 {
1893 int n, s;
1894
1895 s = splhigh();
1896 n = netisr;
1897 netisr = 0;
1898 splx(s);
1899
1900 #define DONETISR(bit, fn) \
1901 do { \
1902 if (n & (1 << (bit))) \
1903 fn; \
1904 } while (0)
1905
1906 #ifdef INET
1907 #if NARP > 0
1908 DONETISR(NETISR_ARP, arpintr());
1909 #endif
1910 DONETISR(NETISR_IP, ipintr());
1911 #endif
1912 #ifdef INET6
1913 DONETISR(NETISR_IPV6, ip6intr());
1914 #endif
1915 #ifdef NETATALK
1916 DONETISR(NETISR_ATALK, atintr());
1917 #endif
1918 #ifdef NS
1919 DONETISR(NETISR_NS, nsintr());
1920 #endif
1921 #ifdef ISO
1922 DONETISR(NETISR_ISO, clnlintr());
1923 #endif
1924 #ifdef CCITT
1925 DONETISR(NETISR_CCITT, ccittintr());
1926 #endif
1927 #ifdef NATM
1928 DONETISR(NETISR_NATM, natmintr());
1929 #endif
1930 #if NPPP > 1
1931 DONETISR(NETISR_PPP, pppintr());
1932 #endif
1933
1934 #undef DONETISR
1935 }
1936
1937 void
1938 do_sir()
1939 {
1940 u_int64_t n;
1941
1942 while ((n = alpha_atomic_loadlatch_q(&ssir, 0)) != 0) {
1943 #define COUNT_SOFT uvmexp.softs++
1944
1945 #define DO_SIR(bit, fn) \
1946 do { \
1947 if (n & (bit)) { \
1948 COUNT_SOFT; \
1949 fn; \
1950 } \
1951 } while (0)
1952
1953 DO_SIR(SIR_NET, netintr());
1954 DO_SIR(SIR_CLOCK, softclock());
1955 #if NCOM > 0
1956 DO_SIR(SIR_SERIAL, comsoft());
1957 #endif
1958
1959 #undef COUNT_SOFT
1960 #undef DO_SIR
1961 }
1962 }
1963
1964 int
1965 spl0()
1966 {
1967
1968 if (ssir) {
1969 (void) alpha_pal_swpipl(ALPHA_PSL_IPL_SOFT);
1970 do_sir();
1971 }
1972
1973 return (alpha_pal_swpipl(ALPHA_PSL_IPL_0));
1974 }
1975
1976 /*
1977 * The following primitives manipulate the run queues. _whichqs tells which
1978 * of the 32 queues _qs have processes in them. Setrunqueue puts processes
1979 * into queues, Remrunqueue removes them from queues. The running process is
1980 * on no queue, other processes are on a queue related to p->p_priority,
1981 * divided by 4 actually to shrink the 0-127 range of priorities into the 32
1982 * available queues.
1983 */
1984 /*
1985 * setrunqueue(p)
1986 * proc *p;
1987 *
1988 * Call should be made at splclock(), and p->p_stat should be SRUN.
1989 */
1990
1991 void
1992 setrunqueue(p)
1993 struct proc *p;
1994 {
1995 int bit;
1996
1997 /* firewall: p->p_back must be NULL */
1998 if (p->p_back != NULL)
1999 panic("setrunqueue");
2000
2001 bit = p->p_priority >> 2;
2002 whichqs |= (1 << bit);
2003 p->p_forw = (struct proc *)&qs[bit];
2004 p->p_back = qs[bit].ph_rlink;
2005 p->p_back->p_forw = p;
2006 qs[bit].ph_rlink = p;
2007 }
2008
2009 /*
2010 * remrunqueue(p)
2011 *
2012 * Call should be made at splclock().
2013 */
2014 void
2015 remrunqueue(p)
2016 struct proc *p;
2017 {
2018 int bit;
2019
2020 bit = p->p_priority >> 2;
2021 if ((whichqs & (1 << bit)) == 0)
2022 panic("remrunqueue");
2023
2024 p->p_back->p_forw = p->p_forw;
2025 p->p_forw->p_back = p->p_back;
2026 p->p_back = NULL; /* for firewall checking. */
2027
2028 if ((struct proc *)&qs[bit] == qs[bit].ph_link)
2029 whichqs &= ~(1 << bit);
2030 }
2031
2032 /*
2033 * Return the best possible estimate of the time in the timeval
2034 * to which tvp points. Unfortunately, we can't read the hardware registers.
2035 * We guarantee that the time will be greater than the value obtained by a
2036 * previous call.
2037 */
2038 void
2039 microtime(tvp)
2040 register struct timeval *tvp;
2041 {
2042 int s = splclock();
2043 static struct timeval lasttime;
2044
2045 *tvp = time;
2046 #ifdef notdef
2047 tvp->tv_usec += clkread();
2048 while (tvp->tv_usec > 1000000) {
2049 tvp->tv_sec++;
2050 tvp->tv_usec -= 1000000;
2051 }
2052 #endif
2053 if (tvp->tv_sec == lasttime.tv_sec &&
2054 tvp->tv_usec <= lasttime.tv_usec &&
2055 (tvp->tv_usec = lasttime.tv_usec + 1) > 1000000) {
2056 tvp->tv_sec++;
2057 tvp->tv_usec -= 1000000;
2058 }
2059 lasttime = *tvp;
2060 splx(s);
2061 }
2062
2063 /*
2064 * Wait "n" microseconds.
2065 */
2066 void
2067 delay(n)
2068 unsigned long n;
2069 {
2070 long N = cycles_per_usec * (n);
2071
2072 while (N > 0) /* XXX */
2073 N -= 3; /* XXX */
2074 }
2075
2076 #if defined(COMPAT_OSF1) || 1 /* XXX */
2077 void cpu_exec_ecoff_setregs __P((struct proc *, struct exec_package *,
2078 u_long));
2079
2080 void
2081 cpu_exec_ecoff_setregs(p, epp, stack)
2082 struct proc *p;
2083 struct exec_package *epp;
2084 u_long stack;
2085 {
2086 struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
2087
2088 setregs(p, epp, stack);
2089 p->p_md.md_tf->tf_regs[FRAME_GP] = execp->a.gp_value;
2090 }
2091
2092 /*
2093 * cpu_exec_ecoff_hook():
2094 * cpu-dependent ECOFF format hook for execve().
2095 *
2096 * Do any machine-dependent diddling of the exec package when doing ECOFF.
2097 *
2098 */
2099 int
2100 cpu_exec_ecoff_hook(p, epp)
2101 struct proc *p;
2102 struct exec_package *epp;
2103 {
2104 struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
2105 extern struct emul emul_netbsd;
2106 int error;
2107 extern int osf1_exec_ecoff_hook(struct proc *p,
2108 struct exec_package *epp);
2109
2110 switch (execp->f.f_magic) {
2111 #ifdef COMPAT_OSF1
2112 case ECOFF_MAGIC_ALPHA:
2113 error = osf1_exec_ecoff_hook(p, epp);
2114 break;
2115 #endif
2116
2117 case ECOFF_MAGIC_NETBSD_ALPHA:
2118 epp->ep_emul = &emul_netbsd;
2119 error = 0;
2120 break;
2121
2122 default:
2123 error = ENOEXEC;
2124 }
2125 return (error);
2126 }
2127 #endif
2128
2129 int
2130 alpha_pa_access(pa)
2131 u_long pa;
2132 {
2133 int i;
2134
2135 for (i = 0; i < mem_cluster_cnt; i++) {
2136 if (pa < mem_clusters[i].start)
2137 continue;
2138 if ((pa - mem_clusters[i].start) >=
2139 (mem_clusters[i].size & ~PAGE_MASK))
2140 continue;
2141 return (mem_clusters[i].size & PAGE_MASK); /* prot */
2142 }
2143 return (PROT_NONE);
2144 }
2145
2146 /* XXX XXX BEGIN XXX XXX */
2147 paddr_t alpha_XXX_dmamap_or; /* XXX */
2148 /* XXX */
2149 paddr_t /* XXX */
2150 alpha_XXX_dmamap(v) /* XXX */
2151 vaddr_t v; /* XXX */
2152 { /* XXX */
2153 /* XXX */
2154 return (vtophys(v) | alpha_XXX_dmamap_or); /* XXX */
2155 } /* XXX */
2156 /* XXX XXX END XXX XXX */
2157
2158 struct mchkinfo *
2159 cpu_mchkinfo()
2160 {
2161 if (mchkinfo_all_cpus == NULL)
2162 return &startup_info;
2163 return mchkinfo_all_cpus + alpha_pal_whami();
2164 }
2165
2166 char *
2167 dot_conv(x)
2168 unsigned long x;
2169 {
2170 int i;
2171 char *xc;
2172 static int next;
2173 static char space[2][20];
2174
2175 xc = space[next ^= 1] + sizeof space[0];
2176 *--xc = '\0';
2177 for (i = 0;; ++i) {
2178 if (i && (i & 3) == 0)
2179 *--xc = '.';
2180 *--xc = "0123456789abcdef"[x & 0xf];
2181 x >>= 4;
2182 if (x == 0)
2183 break;
2184 }
2185 return xc;
2186 }
2187