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