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