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