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