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