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