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