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