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