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