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