machdep.c revision 1.339 1 /* $NetBSD: machdep.c,v 1.339 2012/05/21 14:15:16 martin Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999, 2000 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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
35 * All rights reserved.
36 *
37 * Author: Chris G. Demetriou
38 *
39 * Permission to use, copy, modify and distribute this software and
40 * its documentation is hereby granted, provided that both the copyright
41 * notice and this permission notice appear in all copies of the
42 * software, derivative works or modified versions, and any portions
43 * thereof, and that both notices appear in supporting documentation.
44 *
45 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
46 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
47 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 *
49 * Carnegie Mellon requests users of this software to return to
50 *
51 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
52 * School of Computer Science
53 * Carnegie Mellon University
54 * Pittsburgh PA 15213-3890
55 *
56 * any improvements or extensions that they make and grant Carnegie the
57 * rights to redistribute these changes.
58 */
59
60 #include "opt_ddb.h"
61 #include "opt_kgdb.h"
62 #include "opt_modular.h"
63 #include "opt_multiprocessor.h"
64 #include "opt_dec_3000_300.h"
65 #include "opt_dec_3000_500.h"
66 #include "opt_compat_osf1.h"
67 #include "opt_execfmt.h"
68
69 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
70
71 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.339 2012/05/21 14:15:16 martin Exp $");
72
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/signalvar.h>
76 #include <sys/kernel.h>
77 #include <sys/cpu.h>
78 #include <sys/proc.h>
79 #include <sys/ras.h>
80 #include <sys/sched.h>
81 #include <sys/reboot.h>
82 #include <sys/device.h>
83 #include <sys/malloc.h>
84 #include <sys/mman.h>
85 #include <sys/msgbuf.h>
86 #include <sys/ioctl.h>
87 #include <sys/tty.h>
88 #include <sys/exec.h>
89 #include <sys/exec_aout.h> /* for MID_* */
90 #include <sys/exec_ecoff.h>
91 #include <sys/core.h>
92 #include <sys/kcore.h>
93 #include <sys/ucontext.h>
94 #include <sys/conf.h>
95 #include <sys/ksyms.h>
96 #include <sys/kauth.h>
97 #include <sys/atomic.h>
98 #include <sys/cpu.h>
99
100 #include <machine/kcore.h>
101 #include <machine/fpu.h>
102
103 #include <sys/mount.h>
104 #include <sys/syscallargs.h>
105
106 #include <uvm/uvm.h>
107 #include <sys/sysctl.h>
108
109 #include <dev/cons.h>
110 #include <dev/mm.h>
111
112 #include <machine/autoconf.h>
113 #include <machine/reg.h>
114 #include <machine/rpb.h>
115 #include <machine/prom.h>
116 #include <machine/cpuconf.h>
117 #include <machine/ieeefp.h>
118
119 #ifdef DDB
120 #include <machine/db_machdep.h>
121 #include <ddb/db_access.h>
122 #include <ddb/db_sym.h>
123 #include <ddb/db_extern.h>
124 #include <ddb/db_interface.h>
125 #endif
126
127 #ifdef KGDB
128 #include <sys/kgdb.h>
129 #endif
130
131 #ifdef DEBUG
132 #include <machine/sigdebug.h>
133 #endif
134
135 #include <machine/alpha.h>
136
137 #include "ksyms.h"
138
139 struct vm_map *phys_map = NULL;
140
141 void *msgbufaddr;
142
143 int maxmem; /* max memory per process */
144
145 int totalphysmem; /* total amount of physical memory in system */
146 int physmem; /* physical memory used by NetBSD + some rsvd */
147 int resvmem; /* amount of memory reserved for PROM */
148 int unusedmem; /* amount of memory for OS that we don't use */
149 int unknownmem; /* amount of memory with an unknown use */
150
151 int cputype; /* system type, from the RPB */
152
153 int bootdev_debug = 0; /* patchable, or from DDB */
154
155 /*
156 * XXX We need an address to which we can assign things so that they
157 * won't be optimized away because we didn't use the value.
158 */
159 uint32_t no_optimize;
160
161 /* the following is used externally (sysctl_hw) */
162 char machine[] = MACHINE; /* from <machine/param.h> */
163 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */
164 char cpu_model[128];
165
166 /* Number of machine cycles per microsecond */
167 uint64_t cycles_per_usec;
168
169 /* number of CPUs in the box. really! */
170 int ncpus;
171
172 struct bootinfo_kernel bootinfo;
173
174 /* For built-in TCDS */
175 #if defined(DEC_3000_300) || defined(DEC_3000_500)
176 uint8_t dec_3000_scsiid[2], dec_3000_scsifast[2];
177 #endif
178
179 struct platform platform;
180
181 #if NKSYMS || defined(DDB) || defined(MODULAR)
182 /* start and end of kernel symbol table */
183 void *ksym_start, *ksym_end;
184 #endif
185
186 /* for cpu_sysctl() */
187 int alpha_unaligned_print = 1; /* warn about unaligned accesses */
188 int alpha_unaligned_fix = 1; /* fix up unaligned accesses */
189 int alpha_unaligned_sigbus = 0; /* don't SIGBUS on fixed-up accesses */
190 int alpha_fp_sync_complete = 0; /* fp fixup if sync even without /s */
191
192 /*
193 * XXX This should be dynamically sized, but we have the chicken-egg problem!
194 * XXX it should also be larger than it is, because not all of the mddt
195 * XXX clusters end up being used for VM.
196 */
197 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX]; /* low size bits overloaded */
198 int mem_cluster_cnt;
199
200 int cpu_dump(void);
201 int cpu_dumpsize(void);
202 u_long cpu_dump_mempagecnt(void);
203 void dumpsys(void);
204 void identifycpu(void);
205 void printregs(struct reg *);
206
207 const pcu_ops_t fpu_ops = {
208 .pcu_id = PCU_FPU,
209 .pcu_state_load = fpu_state_load,
210 .pcu_state_save = fpu_state_save,
211 .pcu_state_release = fpu_state_release,
212 };
213
214 const pcu_ops_t * const pcu_ops_md_defs[PCU_UNIT_COUNT] = {
215 [PCU_FPU] = &fpu_ops,
216 };
217
218 void
219 alpha_init(u_long pfn, u_long ptb, u_long bim, u_long bip, u_long biv)
220 /* pfn: first free PFN number */
221 /* ptb: PFN of current level 1 page table */
222 /* bim: bootinfo magic */
223 /* bip: bootinfo pointer */
224 /* biv: bootinfo version */
225 {
226 extern char kernel_text[], _end[];
227 struct mddt *mddtp;
228 struct mddt_cluster *memc;
229 int i, mddtweird;
230 struct vm_physseg *vps;
231 struct pcb *pcb0;
232 vaddr_t kernstart, kernend, v;
233 paddr_t kernstartpfn, kernendpfn, pfn0, pfn1;
234 cpuid_t cpu_id;
235 struct cpu_info *ci;
236 char *p;
237 const char *bootinfo_msg;
238 const struct cpuinit *c;
239
240 /* NO OUTPUT ALLOWED UNTIL FURTHER NOTICE */
241
242 /*
243 * Turn off interrupts (not mchecks) and floating point.
244 * Make sure the instruction and data streams are consistent.
245 */
246 (void)alpha_pal_swpipl(ALPHA_PSL_IPL_HIGH);
247 alpha_pal_wrfen(0);
248 ALPHA_TBIA();
249 alpha_pal_imb();
250
251 /* Initialize the SCB. */
252 scb_init();
253
254 cpu_id = cpu_number();
255
256 #if defined(MULTIPROCESSOR)
257 /*
258 * Set our SysValue to the address of our cpu_info structure.
259 * Secondary processors do this in their spinup trampoline.
260 */
261 alpha_pal_wrval((u_long)&cpu_info_primary);
262 cpu_info[cpu_id] = &cpu_info_primary;
263 #endif
264
265 ci = curcpu();
266 ci->ci_cpuid = cpu_id;
267
268 /*
269 * Get critical system information (if possible, from the
270 * information provided by the boot program).
271 */
272 bootinfo_msg = NULL;
273 if (bim == BOOTINFO_MAGIC) {
274 if (biv == 0) { /* backward compat */
275 biv = *(u_long *)bip;
276 bip += 8;
277 }
278 switch (biv) {
279 case 1: {
280 struct bootinfo_v1 *v1p = (struct bootinfo_v1 *)bip;
281
282 bootinfo.ssym = v1p->ssym;
283 bootinfo.esym = v1p->esym;
284 /* hwrpb may not be provided by boot block in v1 */
285 if (v1p->hwrpb != NULL) {
286 bootinfo.hwrpb_phys =
287 ((struct rpb *)v1p->hwrpb)->rpb_phys;
288 bootinfo.hwrpb_size = v1p->hwrpbsize;
289 } else {
290 bootinfo.hwrpb_phys =
291 ((struct rpb *)HWRPB_ADDR)->rpb_phys;
292 bootinfo.hwrpb_size =
293 ((struct rpb *)HWRPB_ADDR)->rpb_size;
294 }
295 memcpy(bootinfo.boot_flags, v1p->boot_flags,
296 min(sizeof v1p->boot_flags,
297 sizeof bootinfo.boot_flags));
298 memcpy(bootinfo.booted_kernel, v1p->booted_kernel,
299 min(sizeof v1p->booted_kernel,
300 sizeof bootinfo.booted_kernel));
301 /* booted dev not provided in bootinfo */
302 init_prom_interface((struct rpb *)
303 ALPHA_PHYS_TO_K0SEG(bootinfo.hwrpb_phys));
304 prom_getenv(PROM_E_BOOTED_DEV, bootinfo.booted_dev,
305 sizeof bootinfo.booted_dev);
306 break;
307 }
308 default:
309 bootinfo_msg = "unknown bootinfo version";
310 goto nobootinfo;
311 }
312 } else {
313 bootinfo_msg = "boot program did not pass bootinfo";
314 nobootinfo:
315 bootinfo.ssym = (u_long)_end;
316 bootinfo.esym = (u_long)_end;
317 bootinfo.hwrpb_phys = ((struct rpb *)HWRPB_ADDR)->rpb_phys;
318 bootinfo.hwrpb_size = ((struct rpb *)HWRPB_ADDR)->rpb_size;
319 init_prom_interface((struct rpb *)HWRPB_ADDR);
320 prom_getenv(PROM_E_BOOTED_OSFLAGS, bootinfo.boot_flags,
321 sizeof bootinfo.boot_flags);
322 prom_getenv(PROM_E_BOOTED_FILE, bootinfo.booted_kernel,
323 sizeof bootinfo.booted_kernel);
324 prom_getenv(PROM_E_BOOTED_DEV, bootinfo.booted_dev,
325 sizeof bootinfo.booted_dev);
326 }
327
328 /*
329 * Initialize the kernel's mapping of the RPB. It's needed for
330 * lots of things.
331 */
332 hwrpb = (struct rpb *)ALPHA_PHYS_TO_K0SEG(bootinfo.hwrpb_phys);
333
334 #if defined(DEC_3000_300) || defined(DEC_3000_500)
335 if (hwrpb->rpb_type == ST_DEC_3000_300 ||
336 hwrpb->rpb_type == ST_DEC_3000_500) {
337 prom_getenv(PROM_E_SCSIID, dec_3000_scsiid,
338 sizeof(dec_3000_scsiid));
339 prom_getenv(PROM_E_SCSIFAST, dec_3000_scsifast,
340 sizeof(dec_3000_scsifast));
341 }
342 #endif
343
344 /*
345 * Remember how many cycles there are per microsecond,
346 * so that we can use delay(). Round up, for safety.
347 */
348 cycles_per_usec = (hwrpb->rpb_cc_freq + 999999) / 1000000;
349
350 /*
351 * Initialize the (temporary) bootstrap console interface, so
352 * we can use printf until the VM system starts being setup.
353 * The real console is initialized before then.
354 */
355 init_bootstrap_console();
356
357 /* OUTPUT NOW ALLOWED */
358
359 /* delayed from above */
360 if (bootinfo_msg)
361 printf("WARNING: %s (0x%lx, 0x%lx, 0x%lx)\n",
362 bootinfo_msg, bim, bip, biv);
363
364 /* Initialize the trap vectors on the primary processor. */
365 trap_init();
366
367 /*
368 * Find out this system's page size, and initialize
369 * PAGE_SIZE-dependent variables.
370 */
371 if (hwrpb->rpb_page_size != ALPHA_PGBYTES)
372 panic("page size %lu != %d?!", hwrpb->rpb_page_size,
373 ALPHA_PGBYTES);
374 uvmexp.pagesize = hwrpb->rpb_page_size;
375 uvm_setpagesize();
376
377 /*
378 * Find out what hardware we're on, and do basic initialization.
379 */
380 cputype = hwrpb->rpb_type;
381 if (cputype < 0) {
382 /*
383 * At least some white-box systems have SRM which
384 * reports a systype that's the negative of their
385 * blue-box counterpart.
386 */
387 cputype = -cputype;
388 }
389 c = platform_lookup(cputype);
390 if (c == NULL) {
391 platform_not_supported();
392 /* NOTREACHED */
393 }
394 (*c->init)();
395 strcpy(cpu_model, platform.model);
396
397 /*
398 * Initialize the real console, so that the bootstrap console is
399 * no longer necessary.
400 */
401 (*platform.cons_init)();
402
403 #ifdef DIAGNOSTIC
404 /* Paranoid sanity checking */
405
406 /* We should always be running on the primary. */
407 assert(hwrpb->rpb_primary_cpu_id == cpu_id);
408
409 /*
410 * On single-CPU systypes, the primary should always be CPU 0,
411 * except on Alpha 8200 systems where the CPU id is related
412 * to the VID, which is related to the Turbo Laser node id.
413 */
414 if (cputype != ST_DEC_21000)
415 assert(hwrpb->rpb_primary_cpu_id == 0);
416 #endif
417
418 /* NO MORE FIRMWARE ACCESS ALLOWED */
419 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
420 /*
421 * XXX (unless _PMAP_MAY_USE_PROM_CONSOLE is defined and
422 * XXX pmap_uses_prom_console() evaluates to non-zero.)
423 */
424 #endif
425
426 /*
427 * Find the beginning and end of the kernel (and leave a
428 * bit of space before the beginning for the bootstrap
429 * stack).
430 */
431 kernstart = trunc_page((vaddr_t)kernel_text) - 2 * PAGE_SIZE;
432 #if NKSYMS || defined(DDB) || defined(MODULAR)
433 ksym_start = (void *)bootinfo.ssym;
434 ksym_end = (void *)bootinfo.esym;
435 kernend = (vaddr_t)round_page((vaddr_t)ksym_end);
436 #else
437 kernend = (vaddr_t)round_page((vaddr_t)_end);
438 #endif
439
440 kernstartpfn = atop(ALPHA_K0SEG_TO_PHYS(kernstart));
441 kernendpfn = atop(ALPHA_K0SEG_TO_PHYS(kernend));
442
443 /*
444 * Find out how much memory is available, by looking at
445 * the memory cluster descriptors. This also tries to do
446 * its best to detect things things that have never been seen
447 * before...
448 */
449 mddtp = (struct mddt *)(((char *)hwrpb) + hwrpb->rpb_memdat_off);
450
451 /* MDDT SANITY CHECKING */
452 mddtweird = 0;
453 if (mddtp->mddt_cluster_cnt < 2) {
454 mddtweird = 1;
455 printf("WARNING: weird number of mem clusters: %lu\n",
456 mddtp->mddt_cluster_cnt);
457 }
458
459 #if 0
460 printf("Memory cluster count: %d\n", mddtp->mddt_cluster_cnt);
461 #endif
462
463 for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
464 memc = &mddtp->mddt_clusters[i];
465 #if 0
466 printf("MEMC %d: pfn 0x%lx cnt 0x%lx usage 0x%lx\n", i,
467 memc->mddt_pfn, memc->mddt_pg_cnt, memc->mddt_usage);
468 #endif
469 totalphysmem += memc->mddt_pg_cnt;
470 if (mem_cluster_cnt < VM_PHYSSEG_MAX) { /* XXX */
471 mem_clusters[mem_cluster_cnt].start =
472 ptoa(memc->mddt_pfn);
473 mem_clusters[mem_cluster_cnt].size =
474 ptoa(memc->mddt_pg_cnt);
475 if (memc->mddt_usage & MDDT_mbz ||
476 memc->mddt_usage & MDDT_NONVOLATILE || /* XXX */
477 memc->mddt_usage & MDDT_PALCODE)
478 mem_clusters[mem_cluster_cnt].size |=
479 PROT_READ;
480 else
481 mem_clusters[mem_cluster_cnt].size |=
482 PROT_READ | PROT_WRITE | PROT_EXEC;
483 mem_cluster_cnt++;
484 }
485
486 if (memc->mddt_usage & MDDT_mbz) {
487 mddtweird = 1;
488 printf("WARNING: mem cluster %d has weird "
489 "usage 0x%lx\n", i, memc->mddt_usage);
490 unknownmem += memc->mddt_pg_cnt;
491 continue;
492 }
493 if (memc->mddt_usage & MDDT_NONVOLATILE) {
494 /* XXX should handle these... */
495 printf("WARNING: skipping non-volatile mem "
496 "cluster %d\n", i);
497 unusedmem += memc->mddt_pg_cnt;
498 continue;
499 }
500 if (memc->mddt_usage & MDDT_PALCODE) {
501 resvmem += memc->mddt_pg_cnt;
502 continue;
503 }
504
505 /*
506 * We have a memory cluster available for system
507 * software use. We must determine if this cluster
508 * holds the kernel.
509 */
510 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
511 /*
512 * XXX If the kernel uses the PROM console, we only use the
513 * XXX memory after the kernel in the first system segment,
514 * XXX to avoid clobbering prom mapping, data, etc.
515 */
516 if (!pmap_uses_prom_console() || physmem == 0) {
517 #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
518 physmem += memc->mddt_pg_cnt;
519 pfn0 = memc->mddt_pfn;
520 pfn1 = memc->mddt_pfn + memc->mddt_pg_cnt;
521 if (pfn0 <= kernstartpfn && kernendpfn <= pfn1) {
522 /*
523 * Must compute the location of the kernel
524 * within the segment.
525 */
526 #if 0
527 printf("Cluster %d contains kernel\n", i);
528 #endif
529 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
530 if (!pmap_uses_prom_console()) {
531 #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
532 if (pfn0 < kernstartpfn) {
533 /*
534 * There is a chunk before the kernel.
535 */
536 #if 0
537 printf("Loading chunk before kernel: "
538 "0x%lx / 0x%lx\n", pfn0, kernstartpfn);
539 #endif
540 uvm_page_physload(pfn0, kernstartpfn,
541 pfn0, kernstartpfn, VM_FREELIST_DEFAULT);
542 }
543 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
544 }
545 #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
546 if (kernendpfn < pfn1) {
547 /*
548 * There is a chunk after the kernel.
549 */
550 #if 0
551 printf("Loading chunk after kernel: "
552 "0x%lx / 0x%lx\n", kernendpfn, pfn1);
553 #endif
554 uvm_page_physload(kernendpfn, pfn1,
555 kernendpfn, pfn1, VM_FREELIST_DEFAULT);
556 }
557 } else {
558 /*
559 * Just load this cluster as one chunk.
560 */
561 #if 0
562 printf("Loading cluster %d: 0x%lx / 0x%lx\n", i,
563 pfn0, pfn1);
564 #endif
565 uvm_page_physload(pfn0, pfn1, pfn0, pfn1,
566 VM_FREELIST_DEFAULT);
567 }
568 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
569 }
570 #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
571 }
572
573 /*
574 * Dump out the MDDT if it looks odd...
575 */
576 if (mddtweird) {
577 printf("\n");
578 printf("complete memory cluster information:\n");
579 for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
580 printf("mddt %d:\n", i);
581 printf("\tpfn %lx\n",
582 mddtp->mddt_clusters[i].mddt_pfn);
583 printf("\tcnt %lx\n",
584 mddtp->mddt_clusters[i].mddt_pg_cnt);
585 printf("\ttest %lx\n",
586 mddtp->mddt_clusters[i].mddt_pg_test);
587 printf("\tbva %lx\n",
588 mddtp->mddt_clusters[i].mddt_v_bitaddr);
589 printf("\tbpa %lx\n",
590 mddtp->mddt_clusters[i].mddt_p_bitaddr);
591 printf("\tbcksum %lx\n",
592 mddtp->mddt_clusters[i].mddt_bit_cksum);
593 printf("\tusage %lx\n",
594 mddtp->mddt_clusters[i].mddt_usage);
595 }
596 printf("\n");
597 }
598
599 if (totalphysmem == 0)
600 panic("can't happen: system seems to have no memory!");
601 maxmem = physmem;
602 #if 0
603 printf("totalphysmem = %d\n", totalphysmem);
604 printf("physmem = %d\n", physmem);
605 printf("resvmem = %d\n", resvmem);
606 printf("unusedmem = %d\n", unusedmem);
607 printf("unknownmem = %d\n", unknownmem);
608 #endif
609
610 /*
611 * Initialize error message buffer (at end of core).
612 */
613 {
614 vsize_t sz = (vsize_t)round_page(MSGBUFSIZE);
615 vsize_t reqsz = sz;
616
617 vps = VM_PHYSMEM_PTR(vm_nphysseg - 1);
618
619 /* shrink so that it'll fit in the last segment */
620 if ((vps->avail_end - vps->avail_start) < atop(sz))
621 sz = ptoa(vps->avail_end - vps->avail_start);
622
623 vps->end -= atop(sz);
624 vps->avail_end -= atop(sz);
625 msgbufaddr = (void *) ALPHA_PHYS_TO_K0SEG(ptoa(vps->end));
626 initmsgbuf(msgbufaddr, sz);
627
628 /* Remove the last segment if it now has no pages. */
629 if (vps->start == vps->end)
630 vm_nphysseg--;
631
632 /* warn if the message buffer had to be shrunk */
633 if (sz != reqsz)
634 printf("WARNING: %ld bytes not available for msgbuf "
635 "in last cluster (%ld used)\n", reqsz, sz);
636
637 }
638
639 /*
640 * NOTE: It is safe to use uvm_pageboot_alloc() before
641 * pmap_bootstrap() because our pmap_virtual_space()
642 * returns compile-time constants.
643 */
644
645 /*
646 * Allocate uarea page for lwp0 and set it.
647 */
648 v = uvm_pageboot_alloc(UPAGES * PAGE_SIZE);
649 uvm_lwp_setuarea(&lwp0, v);
650
651 /*
652 * Initialize the virtual memory system, and set the
653 * page table base register in proc 0's PCB.
654 */
655 pmap_bootstrap(ALPHA_PHYS_TO_K0SEG(ptb << PGSHIFT),
656 hwrpb->rpb_max_asn, hwrpb->rpb_pcs_cnt);
657
658 /*
659 * Initialize the rest of lwp0's PCB and cache its physical address.
660 */
661 pcb0 = lwp_getpcb(&lwp0);
662 lwp0.l_md.md_pcbpaddr = (void *)ALPHA_K0SEG_TO_PHYS((vaddr_t)pcb0);
663
664 /*
665 * Set the kernel sp, reserving space for an (empty) trapframe,
666 * and make lwp0's trapframe pointer point to it for sanity.
667 */
668 pcb0->pcb_hw.apcb_ksp = v + USPACE - sizeof(struct trapframe);
669 lwp0.l_md.md_tf = (struct trapframe *)pcb0->pcb_hw.apcb_ksp;
670
671 /* Indicate that lwp0 has a CPU. */
672 lwp0.l_cpu = ci;
673
674 /*
675 * Look at arguments passed to us and compute boothowto.
676 */
677
678 boothowto = RB_SINGLE;
679 #ifdef KADB
680 boothowto |= RB_KDB;
681 #endif
682 for (p = bootinfo.boot_flags; p && *p != '\0'; p++) {
683 /*
684 * Note that we'd really like to differentiate case here,
685 * but the Alpha AXP Architecture Reference Manual
686 * says that we shouldn't.
687 */
688 switch (*p) {
689 case 'a': /* autoboot */
690 case 'A':
691 boothowto &= ~RB_SINGLE;
692 break;
693
694 #ifdef DEBUG
695 case 'c': /* crash dump immediately after autoconfig */
696 case 'C':
697 boothowto |= RB_DUMP;
698 break;
699 #endif
700
701 #if defined(KGDB) || defined(DDB)
702 case 'd': /* break into the kernel debugger ASAP */
703 case 'D':
704 boothowto |= RB_KDB;
705 break;
706 #endif
707
708 case 'h': /* always halt, never reboot */
709 case 'H':
710 boothowto |= RB_HALT;
711 break;
712
713 #if 0
714 case 'm': /* mini root present in memory */
715 case 'M':
716 boothowto |= RB_MINIROOT;
717 break;
718 #endif
719
720 case 'n': /* askname */
721 case 'N':
722 boothowto |= RB_ASKNAME;
723 break;
724
725 case 's': /* single-user (default, supported for sanity) */
726 case 'S':
727 boothowto |= RB_SINGLE;
728 break;
729
730 case 'q': /* quiet boot */
731 case 'Q':
732 boothowto |= AB_QUIET;
733 break;
734
735 case 'v': /* verbose boot */
736 case 'V':
737 boothowto |= AB_VERBOSE;
738 break;
739
740 case '-':
741 /*
742 * Just ignore this. It's not required, but it's
743 * common for it to be passed regardless.
744 */
745 break;
746
747 default:
748 printf("Unrecognized boot flag '%c'.\n", *p);
749 break;
750 }
751 }
752
753 /*
754 * Perform any initial kernel patches based on the running system.
755 * We may perform more later if we attach additional CPUs.
756 */
757 alpha_patch(false);
758
759 /*
760 * Figure out the number of CPUs in the box, from RPB fields.
761 * Really. We mean it.
762 */
763 for (i = 0; i < hwrpb->rpb_pcs_cnt; i++) {
764 struct pcs *pcsp;
765
766 pcsp = LOCATE_PCS(hwrpb, i);
767 if ((pcsp->pcs_flags & PCS_PP) != 0)
768 ncpus++;
769 }
770
771 /*
772 * Initialize debuggers, and break into them if appropriate.
773 */
774 #if NKSYMS || defined(DDB) || defined(MODULAR)
775 ksyms_addsyms_elf((int)((uint64_t)ksym_end - (uint64_t)ksym_start),
776 ksym_start, ksym_end);
777 #endif
778
779 if (boothowto & RB_KDB) {
780 #if defined(KGDB)
781 kgdb_debug_init = 1;
782 kgdb_connect(1);
783 #elif defined(DDB)
784 Debugger();
785 #endif
786 }
787
788 #ifdef DIAGNOSTIC
789 /*
790 * Check our clock frequency, from RPB fields.
791 */
792 if ((hwrpb->rpb_intr_freq >> 12) != 1024)
793 printf("WARNING: unbelievable rpb_intr_freq: %ld (%d hz)\n",
794 hwrpb->rpb_intr_freq, hz);
795 #endif
796 }
797
798 void
799 consinit(void)
800 {
801
802 /*
803 * Everything related to console initialization is done
804 * in alpha_init().
805 */
806 #if defined(DIAGNOSTIC) && defined(_PMAP_MAY_USE_PROM_CONSOLE)
807 printf("consinit: %susing prom console\n",
808 pmap_uses_prom_console() ? "" : "not ");
809 #endif
810 }
811
812 void
813 cpu_startup(void)
814 {
815 extern struct evcnt fpevent_use, fpevent_reuse;
816 vaddr_t minaddr, maxaddr;
817 char pbuf[9];
818 #if defined(DEBUG)
819 extern int pmapdebug;
820 int opmapdebug = pmapdebug;
821
822 pmapdebug = 0;
823 #endif
824
825 /*
826 * Good {morning,afternoon,evening,night}.
827 */
828 printf("%s%s", copyright, version);
829 identifycpu();
830 format_bytes(pbuf, sizeof(pbuf), ptoa(totalphysmem));
831 printf("total memory = %s\n", pbuf);
832 format_bytes(pbuf, sizeof(pbuf), ptoa(resvmem));
833 printf("(%s reserved for PROM, ", pbuf);
834 format_bytes(pbuf, sizeof(pbuf), ptoa(physmem));
835 printf("%s used by NetBSD)\n", pbuf);
836 if (unusedmem) {
837 format_bytes(pbuf, sizeof(pbuf), ptoa(unusedmem));
838 printf("WARNING: unused memory = %s\n", pbuf);
839 }
840 if (unknownmem) {
841 format_bytes(pbuf, sizeof(pbuf), ptoa(unknownmem));
842 printf("WARNING: %s of memory with unknown purpose\n", pbuf);
843 }
844
845 minaddr = 0;
846
847 /*
848 * Allocate a submap for physio
849 */
850 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
851 VM_PHYS_SIZE, 0, false, NULL);
852
853 /*
854 * No need to allocate an mbuf cluster submap. Mbuf clusters
855 * are allocated via the pool allocator, and we use K0SEG to
856 * map those pages.
857 */
858
859 #if defined(DEBUG)
860 pmapdebug = opmapdebug;
861 #endif
862 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
863 printf("avail memory = %s\n", pbuf);
864 #if 0
865 {
866 extern u_long pmap_pages_stolen;
867
868 format_bytes(pbuf, sizeof(pbuf), pmap_pages_stolen * PAGE_SIZE);
869 printf("stolen memory for VM structures = %s\n", pbuf);
870 }
871 #endif
872
873 /*
874 * Set up the HWPCB so that it's safe to configure secondary
875 * CPUs.
876 */
877 hwrpb_primary_init();
878
879 /*
880 * Initialize some trap event counters.
881 */
882 evcnt_attach_dynamic_nozero(&fpevent_use, EVCNT_TYPE_MISC, NULL,
883 "FP", "proc use");
884 evcnt_attach_dynamic_nozero(&fpevent_reuse, EVCNT_TYPE_MISC, NULL,
885 "FP", "proc re-use");
886 }
887
888 /*
889 * Retrieve the platform name from the DSR.
890 */
891 const char *
892 alpha_dsr_sysname(void)
893 {
894 struct dsrdb *dsr;
895 const char *sysname;
896
897 /*
898 * DSR does not exist on early HWRPB versions.
899 */
900 if (hwrpb->rpb_version < HWRPB_DSRDB_MINVERS)
901 return (NULL);
902
903 dsr = (struct dsrdb *)(((char *)hwrpb) + hwrpb->rpb_dsrdb_off);
904 sysname = (const char *)((char *)dsr + (dsr->dsr_sysname_off +
905 sizeof(uint64_t)));
906 return (sysname);
907 }
908
909 /*
910 * Lookup the system specified system variation in the provided table,
911 * returning the model string on match.
912 */
913 const char *
914 alpha_variation_name(uint64_t variation, const struct alpha_variation_table *avtp)
915 {
916 int i;
917
918 for (i = 0; avtp[i].avt_model != NULL; i++)
919 if (avtp[i].avt_variation == variation)
920 return (avtp[i].avt_model);
921 return (NULL);
922 }
923
924 /*
925 * Generate a default platform name based for unknown system variations.
926 */
927 const char *
928 alpha_unknown_sysname(void)
929 {
930 static char s[128]; /* safe size */
931
932 sprintf(s, "%s family, unknown model variation 0x%lx",
933 platform.family, hwrpb->rpb_variation & SV_ST_MASK);
934 return ((const char *)s);
935 }
936
937 void
938 identifycpu(void)
939 {
940 char *s;
941 int i;
942
943 /*
944 * print out CPU identification information.
945 */
946 printf("%s", cpu_model);
947 for(s = cpu_model; *s; ++s)
948 if(strncasecmp(s, "MHz", 3) == 0)
949 goto skipMHz;
950 printf(", %ldMHz", hwrpb->rpb_cc_freq / 1000000);
951 skipMHz:
952 printf(", s/n ");
953 for (i = 0; i < 10; i++)
954 printf("%c", hwrpb->rpb_ssn[i]);
955 printf("\n");
956 printf("%ld byte page size, %d processor%s.\n",
957 hwrpb->rpb_page_size, ncpus, ncpus == 1 ? "" : "s");
958 #if 0
959 /* this isn't defined for any systems that we run on? */
960 printf("serial number 0x%lx 0x%lx\n",
961 ((long *)hwrpb->rpb_ssn)[0], ((long *)hwrpb->rpb_ssn)[1]);
962
963 /* and these aren't particularly useful! */
964 printf("variation: 0x%lx, revision 0x%lx\n",
965 hwrpb->rpb_variation, *(long *)hwrpb->rpb_revision);
966 #endif
967 }
968
969 int waittime = -1;
970 struct pcb dumppcb;
971
972 void
973 cpu_reboot(int howto, char *bootstr)
974 {
975 #if defined(MULTIPROCESSOR)
976 u_long cpu_id = cpu_number();
977 u_long wait_mask;
978 int i;
979 #endif
980
981 /* If "always halt" was specified as a boot flag, obey. */
982 if ((boothowto & RB_HALT) != 0)
983 howto |= RB_HALT;
984
985 boothowto = howto;
986
987 /* If system is cold, just halt. */
988 if (cold) {
989 boothowto |= RB_HALT;
990 goto haltsys;
991 }
992
993 if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
994 waittime = 0;
995 vfs_shutdown();
996 /*
997 * If we've been adjusting the clock, the todr
998 * will be out of synch; adjust it now.
999 */
1000 resettodr();
1001 }
1002
1003 /* Disable interrupts. */
1004 splhigh();
1005
1006 #if defined(MULTIPROCESSOR)
1007 /*
1008 * Halt all other CPUs. If we're not the primary, the
1009 * primary will spin, waiting for us to halt.
1010 */
1011 cpu_id = cpu_number(); /* may have changed cpu */
1012 wait_mask = (1UL << cpu_id) | (1UL << hwrpb->rpb_primary_cpu_id);
1013
1014 alpha_broadcast_ipi(ALPHA_IPI_HALT);
1015
1016 /* Ensure any CPUs paused by DDB resume execution so they can halt */
1017 cpus_paused = 0;
1018
1019 for (i = 0; i < 10000; i++) {
1020 alpha_mb();
1021 if (cpus_running == wait_mask)
1022 break;
1023 delay(1000);
1024 }
1025 alpha_mb();
1026 if (cpus_running != wait_mask)
1027 printf("WARNING: Unable to halt secondary CPUs (0x%lx)\n",
1028 cpus_running);
1029 #endif /* MULTIPROCESSOR */
1030
1031 /* If rebooting and a dump is requested do it. */
1032 #if 0
1033 if ((boothowto & (RB_DUMP | RB_HALT)) == RB_DUMP)
1034 #else
1035 if (boothowto & RB_DUMP)
1036 #endif
1037 dumpsys();
1038
1039 haltsys:
1040
1041 /* run any shutdown hooks */
1042 doshutdownhooks();
1043
1044 pmf_system_shutdown(boothowto);
1045
1046 #ifdef BOOTKEY
1047 printf("hit any key to %s...\n", howto & RB_HALT ? "halt" : "reboot");
1048 cnpollc(1); /* for proper keyboard command handling */
1049 cngetc();
1050 cnpollc(0);
1051 printf("\n");
1052 #endif
1053
1054 /* Finally, powerdown/halt/reboot the system. */
1055 if ((boothowto & RB_POWERDOWN) == RB_POWERDOWN &&
1056 platform.powerdown != NULL) {
1057 (*platform.powerdown)();
1058 printf("WARNING: powerdown failed!\n");
1059 }
1060 printf("%s\n\n", (boothowto & RB_HALT) ? "halted." : "rebooting...");
1061 #if defined(MULTIPROCESSOR)
1062 if (cpu_id != hwrpb->rpb_primary_cpu_id)
1063 cpu_halt();
1064 else
1065 #endif
1066 prom_halt(boothowto & RB_HALT);
1067 /*NOTREACHED*/
1068 }
1069
1070 /*
1071 * These variables are needed by /sbin/savecore
1072 */
1073 uint32_t dumpmag = 0x8fca0101; /* magic number */
1074 int dumpsize = 0; /* pages */
1075 long dumplo = 0; /* blocks */
1076
1077 /*
1078 * cpu_dumpsize: calculate size of machine-dependent kernel core dump headers.
1079 */
1080 int
1081 cpu_dumpsize(void)
1082 {
1083 int size;
1084
1085 size = ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t)) +
1086 ALIGN(mem_cluster_cnt * sizeof(phys_ram_seg_t));
1087 if (roundup(size, dbtob(1)) != dbtob(1))
1088 return -1;
1089
1090 return (1);
1091 }
1092
1093 /*
1094 * cpu_dump_mempagecnt: calculate size of RAM (in pages) to be dumped.
1095 */
1096 u_long
1097 cpu_dump_mempagecnt(void)
1098 {
1099 u_long i, n;
1100
1101 n = 0;
1102 for (i = 0; i < mem_cluster_cnt; i++)
1103 n += atop(mem_clusters[i].size);
1104 return (n);
1105 }
1106
1107 /*
1108 * cpu_dump: dump machine-dependent kernel core dump headers.
1109 */
1110 int
1111 cpu_dump(void)
1112 {
1113 int (*dump)(dev_t, daddr_t, void *, size_t);
1114 char buf[dbtob(1)];
1115 kcore_seg_t *segp;
1116 cpu_kcore_hdr_t *cpuhdrp;
1117 phys_ram_seg_t *memsegp;
1118 const struct bdevsw *bdev;
1119 int i;
1120
1121 bdev = bdevsw_lookup(dumpdev);
1122 if (bdev == NULL)
1123 return (ENXIO);
1124 dump = bdev->d_dump;
1125
1126 memset(buf, 0, sizeof buf);
1127 segp = (kcore_seg_t *)buf;
1128 cpuhdrp = (cpu_kcore_hdr_t *)&buf[ALIGN(sizeof(*segp))];
1129 memsegp = (phys_ram_seg_t *)&buf[ ALIGN(sizeof(*segp)) +
1130 ALIGN(sizeof(*cpuhdrp))];
1131
1132 /*
1133 * Generate a segment header.
1134 */
1135 CORE_SETMAGIC(*segp, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
1136 segp->c_size = dbtob(1) - ALIGN(sizeof(*segp));
1137
1138 /*
1139 * Add the machine-dependent header info.
1140 */
1141 cpuhdrp->lev1map_pa = ALPHA_K0SEG_TO_PHYS((vaddr_t)kernel_lev1map);
1142 cpuhdrp->page_size = PAGE_SIZE;
1143 cpuhdrp->nmemsegs = mem_cluster_cnt;
1144
1145 /*
1146 * Fill in the memory segment descriptors.
1147 */
1148 for (i = 0; i < mem_cluster_cnt; i++) {
1149 memsegp[i].start = mem_clusters[i].start;
1150 memsegp[i].size = mem_clusters[i].size & ~PAGE_MASK;
1151 }
1152
1153 return (dump(dumpdev, dumplo, (void *)buf, dbtob(1)));
1154 }
1155
1156 /*
1157 * This is called by main to set dumplo and dumpsize.
1158 * Dumps always skip the first PAGE_SIZE of disk space
1159 * in case there might be a disk label stored there.
1160 * If there is extra space, put dump at the end to
1161 * reduce the chance that swapping trashes it.
1162 */
1163 void
1164 cpu_dumpconf(void)
1165 {
1166 int nblks, dumpblks; /* size of dump area */
1167
1168 if (dumpdev == NODEV)
1169 goto bad;
1170 nblks = bdev_size(dumpdev);
1171 if (nblks <= ctod(1))
1172 goto bad;
1173
1174 dumpblks = cpu_dumpsize();
1175 if (dumpblks < 0)
1176 goto bad;
1177 dumpblks += ctod(cpu_dump_mempagecnt());
1178
1179 /* If dump won't fit (incl. room for possible label), punt. */
1180 if (dumpblks > (nblks - ctod(1)))
1181 goto bad;
1182
1183 /* Put dump at end of partition */
1184 dumplo = nblks - dumpblks;
1185
1186 /* dumpsize is in page units, and doesn't include headers. */
1187 dumpsize = cpu_dump_mempagecnt();
1188 return;
1189
1190 bad:
1191 dumpsize = 0;
1192 return;
1193 }
1194
1195 /*
1196 * Dump the kernel's image to the swap partition.
1197 */
1198 #define BYTES_PER_DUMP PAGE_SIZE
1199
1200 void
1201 dumpsys(void)
1202 {
1203 const struct bdevsw *bdev;
1204 u_long totalbytesleft, bytes, i, n, memcl;
1205 u_long maddr;
1206 int psize;
1207 daddr_t blkno;
1208 int (*dump)(dev_t, daddr_t, void *, size_t);
1209 int error;
1210
1211 /* Save registers. */
1212 savectx(&dumppcb);
1213
1214 if (dumpdev == NODEV)
1215 return;
1216 bdev = bdevsw_lookup(dumpdev);
1217 if (bdev == NULL || bdev->d_psize == NULL)
1218 return;
1219
1220 /*
1221 * For dumps during autoconfiguration,
1222 * if dump device has already configured...
1223 */
1224 if (dumpsize == 0)
1225 cpu_dumpconf();
1226 if (dumplo <= 0) {
1227 printf("\ndump to dev %u,%u not possible\n",
1228 major(dumpdev), minor(dumpdev));
1229 return;
1230 }
1231 printf("\ndumping to dev %u,%u offset %ld\n",
1232 major(dumpdev), minor(dumpdev), dumplo);
1233
1234 psize = bdev_size(dumpdev);
1235 printf("dump ");
1236 if (psize == -1) {
1237 printf("area unavailable\n");
1238 return;
1239 }
1240
1241 /* XXX should purge all outstanding keystrokes. */
1242
1243 if ((error = cpu_dump()) != 0)
1244 goto err;
1245
1246 totalbytesleft = ptoa(cpu_dump_mempagecnt());
1247 blkno = dumplo + cpu_dumpsize();
1248 dump = bdev->d_dump;
1249 error = 0;
1250
1251 for (memcl = 0; memcl < mem_cluster_cnt; memcl++) {
1252 maddr = mem_clusters[memcl].start;
1253 bytes = mem_clusters[memcl].size & ~PAGE_MASK;
1254
1255 for (i = 0; i < bytes; i += n, totalbytesleft -= n) {
1256
1257 /* Print out how many MBs we to go. */
1258 if ((totalbytesleft % (1024*1024)) == 0)
1259 printf_nolog("%ld ",
1260 totalbytesleft / (1024 * 1024));
1261
1262 /* Limit size for next transfer. */
1263 n = bytes - i;
1264 if (n > BYTES_PER_DUMP)
1265 n = BYTES_PER_DUMP;
1266
1267 error = (*dump)(dumpdev, blkno,
1268 (void *)ALPHA_PHYS_TO_K0SEG(maddr), n);
1269 if (error)
1270 goto err;
1271 maddr += n;
1272 blkno += btodb(n); /* XXX? */
1273
1274 /* XXX should look for keystrokes, to cancel. */
1275 }
1276 }
1277
1278 err:
1279 switch (error) {
1280
1281 case ENXIO:
1282 printf("device bad\n");
1283 break;
1284
1285 case EFAULT:
1286 printf("device not ready\n");
1287 break;
1288
1289 case EINVAL:
1290 printf("area improper\n");
1291 break;
1292
1293 case EIO:
1294 printf("i/o error\n");
1295 break;
1296
1297 case EINTR:
1298 printf("aborted from console\n");
1299 break;
1300
1301 case 0:
1302 printf("succeeded\n");
1303 break;
1304
1305 default:
1306 printf("error %d\n", error);
1307 break;
1308 }
1309 printf("\n\n");
1310 delay(1000);
1311 }
1312
1313 void
1314 frametoreg(const struct trapframe *framep, struct reg *regp)
1315 {
1316
1317 regp->r_regs[R_V0] = framep->tf_regs[FRAME_V0];
1318 regp->r_regs[R_T0] = framep->tf_regs[FRAME_T0];
1319 regp->r_regs[R_T1] = framep->tf_regs[FRAME_T1];
1320 regp->r_regs[R_T2] = framep->tf_regs[FRAME_T2];
1321 regp->r_regs[R_T3] = framep->tf_regs[FRAME_T3];
1322 regp->r_regs[R_T4] = framep->tf_regs[FRAME_T4];
1323 regp->r_regs[R_T5] = framep->tf_regs[FRAME_T5];
1324 regp->r_regs[R_T6] = framep->tf_regs[FRAME_T6];
1325 regp->r_regs[R_T7] = framep->tf_regs[FRAME_T7];
1326 regp->r_regs[R_S0] = framep->tf_regs[FRAME_S0];
1327 regp->r_regs[R_S1] = framep->tf_regs[FRAME_S1];
1328 regp->r_regs[R_S2] = framep->tf_regs[FRAME_S2];
1329 regp->r_regs[R_S3] = framep->tf_regs[FRAME_S3];
1330 regp->r_regs[R_S4] = framep->tf_regs[FRAME_S4];
1331 regp->r_regs[R_S5] = framep->tf_regs[FRAME_S5];
1332 regp->r_regs[R_S6] = framep->tf_regs[FRAME_S6];
1333 regp->r_regs[R_A0] = framep->tf_regs[FRAME_A0];
1334 regp->r_regs[R_A1] = framep->tf_regs[FRAME_A1];
1335 regp->r_regs[R_A2] = framep->tf_regs[FRAME_A2];
1336 regp->r_regs[R_A3] = framep->tf_regs[FRAME_A3];
1337 regp->r_regs[R_A4] = framep->tf_regs[FRAME_A4];
1338 regp->r_regs[R_A5] = framep->tf_regs[FRAME_A5];
1339 regp->r_regs[R_T8] = framep->tf_regs[FRAME_T8];
1340 regp->r_regs[R_T9] = framep->tf_regs[FRAME_T9];
1341 regp->r_regs[R_T10] = framep->tf_regs[FRAME_T10];
1342 regp->r_regs[R_T11] = framep->tf_regs[FRAME_T11];
1343 regp->r_regs[R_RA] = framep->tf_regs[FRAME_RA];
1344 regp->r_regs[R_T12] = framep->tf_regs[FRAME_T12];
1345 regp->r_regs[R_AT] = framep->tf_regs[FRAME_AT];
1346 regp->r_regs[R_GP] = framep->tf_regs[FRAME_GP];
1347 /* regp->r_regs[R_SP] = framep->tf_regs[FRAME_SP]; XXX */
1348 regp->r_regs[R_ZERO] = 0;
1349 }
1350
1351 void
1352 regtoframe(const struct reg *regp, struct trapframe *framep)
1353 {
1354
1355 framep->tf_regs[FRAME_V0] = regp->r_regs[R_V0];
1356 framep->tf_regs[FRAME_T0] = regp->r_regs[R_T0];
1357 framep->tf_regs[FRAME_T1] = regp->r_regs[R_T1];
1358 framep->tf_regs[FRAME_T2] = regp->r_regs[R_T2];
1359 framep->tf_regs[FRAME_T3] = regp->r_regs[R_T3];
1360 framep->tf_regs[FRAME_T4] = regp->r_regs[R_T4];
1361 framep->tf_regs[FRAME_T5] = regp->r_regs[R_T5];
1362 framep->tf_regs[FRAME_T6] = regp->r_regs[R_T6];
1363 framep->tf_regs[FRAME_T7] = regp->r_regs[R_T7];
1364 framep->tf_regs[FRAME_S0] = regp->r_regs[R_S0];
1365 framep->tf_regs[FRAME_S1] = regp->r_regs[R_S1];
1366 framep->tf_regs[FRAME_S2] = regp->r_regs[R_S2];
1367 framep->tf_regs[FRAME_S3] = regp->r_regs[R_S3];
1368 framep->tf_regs[FRAME_S4] = regp->r_regs[R_S4];
1369 framep->tf_regs[FRAME_S5] = regp->r_regs[R_S5];
1370 framep->tf_regs[FRAME_S6] = regp->r_regs[R_S6];
1371 framep->tf_regs[FRAME_A0] = regp->r_regs[R_A0];
1372 framep->tf_regs[FRAME_A1] = regp->r_regs[R_A1];
1373 framep->tf_regs[FRAME_A2] = regp->r_regs[R_A2];
1374 framep->tf_regs[FRAME_A3] = regp->r_regs[R_A3];
1375 framep->tf_regs[FRAME_A4] = regp->r_regs[R_A4];
1376 framep->tf_regs[FRAME_A5] = regp->r_regs[R_A5];
1377 framep->tf_regs[FRAME_T8] = regp->r_regs[R_T8];
1378 framep->tf_regs[FRAME_T9] = regp->r_regs[R_T9];
1379 framep->tf_regs[FRAME_T10] = regp->r_regs[R_T10];
1380 framep->tf_regs[FRAME_T11] = regp->r_regs[R_T11];
1381 framep->tf_regs[FRAME_RA] = regp->r_regs[R_RA];
1382 framep->tf_regs[FRAME_T12] = regp->r_regs[R_T12];
1383 framep->tf_regs[FRAME_AT] = regp->r_regs[R_AT];
1384 framep->tf_regs[FRAME_GP] = regp->r_regs[R_GP];
1385 /* framep->tf_regs[FRAME_SP] = regp->r_regs[R_SP]; XXX */
1386 /* ??? = regp->r_regs[R_ZERO]; */
1387 }
1388
1389 void
1390 printregs(struct reg *regp)
1391 {
1392 int i;
1393
1394 for (i = 0; i < 32; i++)
1395 printf("R%d:\t0x%016lx%s", i, regp->r_regs[i],
1396 i & 1 ? "\n" : "\t");
1397 }
1398
1399 void
1400 regdump(struct trapframe *framep)
1401 {
1402 struct reg reg;
1403
1404 frametoreg(framep, ®);
1405 reg.r_regs[R_SP] = alpha_pal_rdusp();
1406
1407 printf("REGISTERS:\n");
1408 printregs(®);
1409 }
1410
1411
1412
1413 void *
1414 getframe(const struct lwp *l, int sig, int *onstack)
1415 {
1416 void *frame;
1417
1418 /* Do we need to jump onto the signal stack? */
1419 *onstack =
1420 (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
1421 (SIGACTION(l->l_proc, sig).sa_flags & SA_ONSTACK) != 0;
1422
1423 if (*onstack)
1424 frame = (void *)((char *)l->l_sigstk.ss_sp +
1425 l->l_sigstk.ss_size);
1426 else
1427 frame = (void *)(alpha_pal_rdusp());
1428 return (frame);
1429 }
1430
1431 void
1432 buildcontext(struct lwp *l, const void *catcher, const void *tramp, const void *fp)
1433 {
1434 struct trapframe *tf = l->l_md.md_tf;
1435
1436 tf->tf_regs[FRAME_RA] = (uint64_t)tramp;
1437 tf->tf_regs[FRAME_PC] = (uint64_t)catcher;
1438 tf->tf_regs[FRAME_T12] = (uint64_t)catcher;
1439 alpha_pal_wrusp((unsigned long)fp);
1440 }
1441
1442
1443 /*
1444 * Send an interrupt to process, new style
1445 */
1446 void
1447 sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
1448 {
1449 struct lwp *l = curlwp;
1450 struct proc *p = l->l_proc;
1451 struct sigacts *ps = p->p_sigacts;
1452 int onstack, sig = ksi->ksi_signo, error;
1453 struct sigframe_siginfo *fp, frame;
1454 struct trapframe *tf;
1455 sig_t catcher = SIGACTION(p, ksi->ksi_signo).sa_handler;
1456
1457 fp = (struct sigframe_siginfo *)getframe(l,ksi->ksi_signo,&onstack);
1458 tf = l->l_md.md_tf;
1459
1460 /* Allocate space for the signal handler context. */
1461 fp--;
1462
1463 #ifdef DEBUG
1464 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
1465 printf("sendsig_siginfo(%d): sig %d ssp %p usp %p\n", p->p_pid,
1466 sig, &onstack, fp);
1467 #endif
1468
1469 /* Build stack frame for signal trampoline. */
1470
1471 frame.sf_si._info = ksi->ksi_info;
1472 frame.sf_uc.uc_flags = _UC_SIGMASK;
1473 frame.sf_uc.uc_sigmask = *mask;
1474 frame.sf_uc.uc_link = l->l_ctxlink;
1475 memset(&frame.sf_uc.uc_stack, 0, sizeof(frame.sf_uc.uc_stack));
1476 sendsig_reset(l, sig);
1477 mutex_exit(p->p_lock);
1478 cpu_getmcontext(l, &frame.sf_uc.uc_mcontext, &frame.sf_uc.uc_flags);
1479 error = copyout(&frame, fp, sizeof(frame));
1480 mutex_enter(p->p_lock);
1481
1482 if (error != 0) {
1483 /*
1484 * Process has trashed its stack; give it an illegal
1485 * instruction to halt it in its tracks.
1486 */
1487 #ifdef DEBUG
1488 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
1489 printf("sendsig_siginfo(%d): copyout failed on sig %d\n",
1490 p->p_pid, sig);
1491 #endif
1492 sigexit(l, SIGILL);
1493 /* NOTREACHED */
1494 }
1495
1496 #ifdef DEBUG
1497 if (sigdebug & SDB_FOLLOW)
1498 printf("sendsig_siginfo(%d): sig %d usp %p code %x\n",
1499 p->p_pid, sig, fp, ksi->ksi_code);
1500 #endif
1501
1502 /*
1503 * Set up the registers to directly invoke the signal handler. The
1504 * signal trampoline is then used to return from the signal. Note
1505 * the trampoline version numbers are coordinated with machine-
1506 * dependent code in libc.
1507 */
1508
1509 tf->tf_regs[FRAME_A0] = sig;
1510 tf->tf_regs[FRAME_A1] = (uint64_t)&fp->sf_si;
1511 tf->tf_regs[FRAME_A2] = (uint64_t)&fp->sf_uc;
1512
1513 buildcontext(l,catcher,ps->sa_sigdesc[sig].sd_tramp,fp);
1514
1515 /* Remember that we're now on the signal stack. */
1516 if (onstack)
1517 l->l_sigstk.ss_flags |= SS_ONSTACK;
1518
1519 #ifdef DEBUG
1520 if (sigdebug & SDB_FOLLOW)
1521 printf("sendsig_siginfo(%d): pc %lx, catcher %lx\n", p->p_pid,
1522 tf->tf_regs[FRAME_PC], tf->tf_regs[FRAME_A3]);
1523 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
1524 printf("sendsig_siginfo(%d): sig %d returns\n",
1525 p->p_pid, sig);
1526 #endif
1527 }
1528
1529 /*
1530 * machine dependent system variables.
1531 */
1532 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup")
1533 {
1534
1535 sysctl_createv(clog, 0, NULL, NULL,
1536 CTLFLAG_PERMANENT,
1537 CTLTYPE_NODE, "machdep", NULL,
1538 NULL, 0, NULL, 0,
1539 CTL_MACHDEP, CTL_EOL);
1540
1541 sysctl_createv(clog, 0, NULL, NULL,
1542 CTLFLAG_PERMANENT,
1543 CTLTYPE_STRUCT, "console_device", NULL,
1544 sysctl_consdev, 0, NULL, sizeof(dev_t),
1545 CTL_MACHDEP, CPU_CONSDEV, CTL_EOL);
1546 sysctl_createv(clog, 0, NULL, NULL,
1547 CTLFLAG_PERMANENT,
1548 CTLTYPE_STRING, "root_device", NULL,
1549 sysctl_root_device, 0, NULL, 0,
1550 CTL_MACHDEP, CPU_ROOT_DEVICE, CTL_EOL);
1551 sysctl_createv(clog, 0, NULL, NULL,
1552 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1553 CTLTYPE_INT, "unaligned_print", NULL,
1554 NULL, 0, &alpha_unaligned_print, 0,
1555 CTL_MACHDEP, CPU_UNALIGNED_PRINT, CTL_EOL);
1556 sysctl_createv(clog, 0, NULL, NULL,
1557 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1558 CTLTYPE_INT, "unaligned_fix", NULL,
1559 NULL, 0, &alpha_unaligned_fix, 0,
1560 CTL_MACHDEP, CPU_UNALIGNED_FIX, CTL_EOL);
1561 sysctl_createv(clog, 0, NULL, NULL,
1562 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1563 CTLTYPE_INT, "unaligned_sigbus", NULL,
1564 NULL, 0, &alpha_unaligned_sigbus, 0,
1565 CTL_MACHDEP, CPU_UNALIGNED_SIGBUS, CTL_EOL);
1566 sysctl_createv(clog, 0, NULL, NULL,
1567 CTLFLAG_PERMANENT,
1568 CTLTYPE_STRING, "booted_kernel", NULL,
1569 NULL, 0, bootinfo.booted_kernel, 0,
1570 CTL_MACHDEP, CPU_BOOTED_KERNEL, CTL_EOL);
1571 sysctl_createv(clog, 0, NULL, NULL,
1572 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1573 CTLTYPE_INT, "fp_sync_complete", NULL,
1574 NULL, 0, &alpha_fp_sync_complete, 0,
1575 CTL_MACHDEP, CPU_FP_SYNC_COMPLETE, CTL_EOL);
1576 }
1577
1578 /*
1579 * Set registers on exec.
1580 */
1581 void
1582 setregs(register struct lwp *l, struct exec_package *pack, vaddr_t stack)
1583 {
1584 struct trapframe *tfp = l->l_md.md_tf;
1585 struct pcb *pcb;
1586 #ifdef DEBUG
1587 int i;
1588 #endif
1589
1590 #ifdef DEBUG
1591 /*
1592 * Crash and dump, if the user requested it.
1593 */
1594 if (boothowto & RB_DUMP)
1595 panic("crash requested by boot flags");
1596 #endif
1597
1598 #ifdef DEBUG
1599 for (i = 0; i < FRAME_SIZE; i++)
1600 tfp->tf_regs[i] = 0xbabefacedeadbeef;
1601 #else
1602 memset(tfp->tf_regs, 0, FRAME_SIZE * sizeof tfp->tf_regs[0]);
1603 #endif
1604 pcb = lwp_getpcb(l);
1605 memset(&pcb->pcb_fp, 0, sizeof(pcb->pcb_fp));
1606 alpha_pal_wrusp(stack);
1607 tfp->tf_regs[FRAME_PS] = ALPHA_PSL_USERSET;
1608 tfp->tf_regs[FRAME_PC] = pack->ep_entry & ~3;
1609
1610 tfp->tf_regs[FRAME_A0] = stack; /* a0 = sp */
1611 tfp->tf_regs[FRAME_A1] = 0; /* a1 = rtld cleanup */
1612 tfp->tf_regs[FRAME_A2] = 0; /* a2 = rtld object */
1613 tfp->tf_regs[FRAME_A3] = l->l_proc->p_psstrp; /* a3 = ps_strings */
1614 tfp->tf_regs[FRAME_T12] = tfp->tf_regs[FRAME_PC]; /* a.k.a. PV */
1615
1616 l->l_md.md_flags &= ~MDLWP_FPUSED;
1617 if (__predict_true((l->l_md.md_flags & IEEE_INHERIT) == 0)) {
1618 l->l_md.md_flags &= ~MDLWP_FP_C;
1619 pcb->pcb_fp.fpr_cr = FPCR_DYN(FP_RN);
1620 }
1621 fpu_discard();
1622 }
1623
1624 /*
1625 * Wait "n" microseconds.
1626 */
1627 void
1628 delay(unsigned long n)
1629 {
1630 unsigned long pcc0, pcc1, curcycle, cycles, usec;
1631
1632 if (n == 0)
1633 return;
1634
1635 pcc0 = alpha_rpcc() & 0xffffffffUL;
1636 cycles = 0;
1637 usec = 0;
1638
1639 while (usec <= n) {
1640 /*
1641 * Get the next CPU cycle count- assumes that we cannot
1642 * have had more than one 32 bit overflow.
1643 */
1644 pcc1 = alpha_rpcc() & 0xffffffffUL;
1645 if (pcc1 < pcc0)
1646 curcycle = (pcc1 + 0x100000000UL) - pcc0;
1647 else
1648 curcycle = pcc1 - pcc0;
1649
1650 /*
1651 * We now have the number of processor cycles since we
1652 * last checked. Add the current cycle count to the
1653 * running total. If it's over cycles_per_usec, increment
1654 * the usec counter.
1655 */
1656 cycles += curcycle;
1657 while (cycles > cycles_per_usec) {
1658 usec++;
1659 cycles -= cycles_per_usec;
1660 }
1661 pcc0 = pcc1;
1662 }
1663 }
1664
1665 #ifdef EXEC_ECOFF
1666 void
1667 cpu_exec_ecoff_setregs(struct lwp *l, struct exec_package *epp, vaddr_t stack)
1668 {
1669 struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
1670
1671 l->l_md.md_tf->tf_regs[FRAME_GP] = execp->a.gp_value;
1672 }
1673
1674 /*
1675 * cpu_exec_ecoff_hook():
1676 * cpu-dependent ECOFF format hook for execve().
1677 *
1678 * Do any machine-dependent diddling of the exec package when doing ECOFF.
1679 *
1680 */
1681 int
1682 cpu_exec_ecoff_probe(struct lwp *l, struct exec_package *epp)
1683 {
1684 struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
1685 int error;
1686
1687 if (execp->f.f_magic == ECOFF_MAGIC_NETBSD_ALPHA)
1688 error = 0;
1689 else
1690 error = ENOEXEC;
1691
1692 return (error);
1693 }
1694 #endif /* EXEC_ECOFF */
1695
1696 int
1697 mm_md_physacc(paddr_t pa, vm_prot_t prot)
1698 {
1699 u_quad_t size;
1700 int i;
1701
1702 for (i = 0; i < mem_cluster_cnt; i++) {
1703 if (pa < mem_clusters[i].start)
1704 continue;
1705 size = mem_clusters[i].size & ~PAGE_MASK;
1706 if (pa >= (mem_clusters[i].start + size))
1707 continue;
1708 if ((prot & mem_clusters[i].size & PAGE_MASK) == prot)
1709 return 0;
1710 }
1711 return EFAULT;
1712 }
1713
1714 bool
1715 mm_md_direct_mapped_io(void *addr, paddr_t *paddr)
1716 {
1717 vaddr_t va = (vaddr_t)addr;
1718
1719 if (va >= ALPHA_K0SEG_BASE && va <= ALPHA_K0SEG_END) {
1720 *paddr = ALPHA_K0SEG_TO_PHYS(va);
1721 return true;
1722 }
1723 return false;
1724 }
1725
1726 bool
1727 mm_md_direct_mapped_phys(paddr_t paddr, vaddr_t *vaddr)
1728 {
1729
1730 *vaddr = ALPHA_PHYS_TO_K0SEG(paddr);
1731 return true;
1732 }
1733
1734 /* XXX XXX BEGIN XXX XXX */
1735 paddr_t alpha_XXX_dmamap_or; /* XXX */
1736 /* XXX */
1737 paddr_t /* XXX */
1738 alpha_XXX_dmamap(vaddr_t v) /* XXX */
1739 { /* XXX */
1740 /* XXX */
1741 return (vtophys(v) | alpha_XXX_dmamap_or); /* XXX */
1742 } /* XXX */
1743 /* XXX XXX END XXX XXX */
1744
1745 char *
1746 dot_conv(unsigned long x)
1747 {
1748 int i;
1749 char *xc;
1750 static int next;
1751 static char space[2][20];
1752
1753 xc = space[next ^= 1] + sizeof space[0];
1754 *--xc = '\0';
1755 for (i = 0;; ++i) {
1756 if (i && (i & 3) == 0)
1757 *--xc = '.';
1758 *--xc = hexdigits[x & 0xf];
1759 x >>= 4;
1760 if (x == 0)
1761 break;
1762 }
1763 return xc;
1764 }
1765
1766 void
1767 cpu_getmcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flags)
1768 {
1769 struct trapframe *frame = l->l_md.md_tf;
1770 struct pcb *pcb = lwp_getpcb(l);
1771 __greg_t *gr = mcp->__gregs;
1772 __greg_t ras_pc;
1773
1774 /* Save register context. */
1775 frametoreg(frame, (struct reg *)gr);
1776 /* XXX if there's a better, general way to get the USP of
1777 * an LWP that might or might not be curlwp, I'd like to know
1778 * about it.
1779 */
1780 if (l == curlwp) {
1781 gr[_REG_SP] = alpha_pal_rdusp();
1782 gr[_REG_UNIQUE] = alpha_pal_rdunique();
1783 } else {
1784 gr[_REG_SP] = pcb->pcb_hw.apcb_usp;
1785 gr[_REG_UNIQUE] = pcb->pcb_hw.apcb_unique;
1786 }
1787 gr[_REG_PC] = frame->tf_regs[FRAME_PC];
1788 gr[_REG_PS] = frame->tf_regs[FRAME_PS];
1789
1790 if ((ras_pc = (__greg_t)ras_lookup(l->l_proc,
1791 (void *) gr[_REG_PC])) != -1)
1792 gr[_REG_PC] = ras_pc;
1793
1794 *flags |= _UC_CPU | _UC_UNIQUE;
1795
1796 /* Save floating point register context, if any, and copy it. */
1797 if (fpu_used_p(l)) {
1798 fpu_save();
1799 (void)memcpy(&mcp->__fpregs, &pcb->pcb_fp,
1800 sizeof (mcp->__fpregs));
1801 mcp->__fpregs.__fp_fpcr = alpha_read_fp_c(l);
1802 *flags |= _UC_FPU;
1803 }
1804 }
1805
1806 int
1807 cpu_mcontext_validate(struct lwp *l, const mcontext_t *mcp)
1808 {
1809 const __greg_t *gr = mcp->__gregs;
1810
1811 if ((gr[_REG_PS] & ALPHA_PSL_USERSET) != ALPHA_PSL_USERSET ||
1812 (gr[_REG_PS] & ALPHA_PSL_USERCLR) != 0)
1813 return EINVAL;
1814
1815 return 0;
1816 }
1817
1818 int
1819 cpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags)
1820 {
1821 struct trapframe *frame = l->l_md.md_tf;
1822 struct pcb *pcb = lwp_getpcb(l);
1823 const __greg_t *gr = mcp->__gregs;
1824 int error;
1825
1826 /* Restore register context, if any. */
1827 if (flags & _UC_CPU) {
1828 /* Check for security violations first. */
1829 error = cpu_mcontext_validate(l, mcp);
1830 if (error)
1831 return error;
1832
1833 regtoframe((const struct reg *)gr, l->l_md.md_tf);
1834 if (l == curlwp)
1835 alpha_pal_wrusp(gr[_REG_SP]);
1836 else
1837 pcb->pcb_hw.apcb_usp = gr[_REG_SP];
1838 frame->tf_regs[FRAME_PC] = gr[_REG_PC];
1839 frame->tf_regs[FRAME_PS] = gr[_REG_PS];
1840 }
1841 if (flags & _UC_UNIQUE)
1842 lwp_setprivate(l, (void *)(uintptr_t)gr[_REG_UNIQUE]);
1843 /* Restore floating point register context, if any. */
1844 if (flags & _UC_FPU) {
1845 /* If we have an FP register context, get rid of it. */
1846 fpu_discard();
1847 (void)memcpy(&pcb->pcb_fp, &mcp->__fpregs,
1848 sizeof (pcb->pcb_fp));
1849 l->l_md.md_flags = mcp->__fpregs.__fp_fpcr & MDLWP_FP_C;
1850 fpu_mark_used(l);
1851 }
1852
1853 return (0);
1854 }
1855
1856 /*
1857 * Preempt the current process if in interrupt from user mode,
1858 * or after the current trap/syscall if in system mode.
1859 */
1860 void
1861 cpu_need_resched(struct cpu_info *ci, int flags)
1862 {
1863 #if defined(MULTIPROCESSOR)
1864 bool immed = (flags & RESCHED_IMMED) != 0;
1865 #endif /* defined(MULTIPROCESSOR) */
1866
1867 aston(ci->ci_data.cpu_onproc);
1868 ci->ci_want_resched = 1;
1869 if (ci->ci_data.cpu_onproc != ci->ci_data.cpu_idlelwp) {
1870 #if defined(MULTIPROCESSOR)
1871 if (immed && ci != curcpu()) {
1872 alpha_send_ipi(ci->ci_cpuid, 0);
1873 }
1874 #endif /* defined(MULTIPROCESSOR) */
1875 }
1876 }
1877