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