machdep.c revision 1.280 1 /* $NetBSD: machdep.c,v 1.280 2004/02/13 11:36:09 wiz 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.280 2004/02/13 11:36:09 wiz 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 #include "pckbc.h"
823 #include "pckbd.h"
824 #if (NPCKBC > 0) && (NPCKBD == 0)
825
826 #include <dev/ic/pckbcvar.h>
827
828 /*
829 * This is called by the pbkbc driver if no pckbd is configured.
830 * On the i386, it is used to glue in the old, deprecated console
831 * code. On the Alpha, it does nothing.
832 */
833 int
834 pckbc_machdep_cnattach(kbctag, kbcslot)
835 pckbc_tag_t kbctag;
836 pckbc_slot_t kbcslot;
837 {
838
839 return (ENXIO);
840 }
841 #endif /* NPCKBC > 0 && NPCKBD == 0 */
842
843 void
844 cpu_startup()
845 {
846 vaddr_t minaddr, maxaddr;
847 char pbuf[9];
848 #if defined(DEBUG)
849 extern int pmapdebug;
850 int opmapdebug = pmapdebug;
851
852 pmapdebug = 0;
853 #endif
854
855 /*
856 * Good {morning,afternoon,evening,night}.
857 */
858 printf(version);
859 identifycpu();
860 format_bytes(pbuf, sizeof(pbuf), ptoa(totalphysmem));
861 printf("total memory = %s\n", pbuf);
862 format_bytes(pbuf, sizeof(pbuf), ptoa(resvmem));
863 printf("(%s reserved for PROM, ", pbuf);
864 format_bytes(pbuf, sizeof(pbuf), ptoa(physmem));
865 printf("%s used by NetBSD)\n", pbuf);
866 if (unusedmem) {
867 format_bytes(pbuf, sizeof(pbuf), ptoa(unusedmem));
868 printf("WARNING: unused memory = %s\n", pbuf);
869 }
870 if (unknownmem) {
871 format_bytes(pbuf, sizeof(pbuf), ptoa(unknownmem));
872 printf("WARNING: %s of memory with unknown purpose\n", pbuf);
873 }
874
875 minaddr = 0;
876
877 /*
878 * Allocate a submap for exec arguments. This map effectively
879 * limits the number of processes exec'ing at any time.
880 */
881 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
882 16 * NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
883
884 /*
885 * Allocate a submap for physio
886 */
887 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
888 VM_PHYS_SIZE, 0, FALSE, NULL);
889
890 /*
891 * No need to allocate an mbuf cluster submap. Mbuf clusters
892 * are allocated via the pool allocator, and we use K0SEG to
893 * map those pages.
894 */
895
896 #if defined(DEBUG)
897 pmapdebug = opmapdebug;
898 #endif
899 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
900 printf("avail memory = %s\n", pbuf);
901 #if 0
902 {
903 extern u_long pmap_pages_stolen;
904
905 format_bytes(pbuf, sizeof(pbuf), pmap_pages_stolen * PAGE_SIZE);
906 printf("stolen memory for VM structures = %s\n", pbuf);
907 }
908 #endif
909
910 /*
911 * Set up the HWPCB so that it's safe to configure secondary
912 * CPUs.
913 */
914 hwrpb_primary_init();
915 }
916
917 /*
918 * Retrieve the platform name from the DSR.
919 */
920 const char *
921 alpha_dsr_sysname()
922 {
923 struct dsrdb *dsr;
924 const char *sysname;
925
926 /*
927 * DSR does not exist on early HWRPB versions.
928 */
929 if (hwrpb->rpb_version < HWRPB_DSRDB_MINVERS)
930 return (NULL);
931
932 dsr = (struct dsrdb *)(((caddr_t)hwrpb) + hwrpb->rpb_dsrdb_off);
933 sysname = (const char *)((caddr_t)dsr + (dsr->dsr_sysname_off +
934 sizeof(u_int64_t)));
935 return (sysname);
936 }
937
938 /*
939 * Lookup the system specified system variation in the provided table,
940 * returning the model string on match.
941 */
942 const char *
943 alpha_variation_name(variation, avtp)
944 u_int64_t variation;
945 const struct alpha_variation_table *avtp;
946 {
947 int i;
948
949 for (i = 0; avtp[i].avt_model != NULL; i++)
950 if (avtp[i].avt_variation == variation)
951 return (avtp[i].avt_model);
952 return (NULL);
953 }
954
955 /*
956 * Generate a default platform name based for unknown system variations.
957 */
958 const char *
959 alpha_unknown_sysname()
960 {
961 static char s[128]; /* safe size */
962
963 sprintf(s, "%s family, unknown model variation 0x%lx",
964 platform.family, hwrpb->rpb_variation & SV_ST_MASK);
965 return ((const char *)s);
966 }
967
968 void
969 identifycpu()
970 {
971 char *s;
972 int i;
973
974 /*
975 * print out CPU identification information.
976 */
977 printf("%s", cpu_model);
978 for(s = cpu_model; *s; ++s)
979 if(strncasecmp(s, "MHz", 3) == 0)
980 goto skipMHz;
981 printf(", %ldMHz", hwrpb->rpb_cc_freq / 1000000);
982 skipMHz:
983 printf(", s/n ");
984 for (i = 0; i < 10; i++)
985 printf("%c", hwrpb->rpb_ssn[i]);
986 printf("\n");
987 printf("%ld byte page size, %d processor%s.\n",
988 hwrpb->rpb_page_size, ncpus, ncpus == 1 ? "" : "s");
989 #if 0
990 /* this isn't defined for any systems that we run on? */
991 printf("serial number 0x%lx 0x%lx\n",
992 ((long *)hwrpb->rpb_ssn)[0], ((long *)hwrpb->rpb_ssn)[1]);
993
994 /* and these aren't particularly useful! */
995 printf("variation: 0x%lx, revision 0x%lx\n",
996 hwrpb->rpb_variation, *(long *)hwrpb->rpb_revision);
997 #endif
998 }
999
1000 int waittime = -1;
1001 struct pcb dumppcb;
1002
1003 void
1004 cpu_reboot(howto, bootstr)
1005 int howto;
1006 char *bootstr;
1007 {
1008 #if defined(MULTIPROCESSOR)
1009 u_long cpu_id = cpu_number();
1010 u_long wait_mask = (1UL << cpu_id) |
1011 (1UL << hwrpb->rpb_primary_cpu_id);
1012 int i;
1013 #endif
1014
1015 /* If "always halt" was specified as a boot flag, obey. */
1016 if ((boothowto & RB_HALT) != 0)
1017 howto |= RB_HALT;
1018
1019 boothowto = howto;
1020
1021 /* If system is cold, just halt. */
1022 if (cold) {
1023 boothowto |= RB_HALT;
1024 goto haltsys;
1025 }
1026
1027 if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
1028 waittime = 0;
1029 vfs_shutdown();
1030 /*
1031 * If we've been adjusting the clock, the todr
1032 * will be out of synch; adjust it now.
1033 */
1034 resettodr();
1035 }
1036
1037 /* Disable interrupts. */
1038 splhigh();
1039
1040 #if defined(MULTIPROCESSOR)
1041 /*
1042 * Halt all other CPUs. If we're not the primary, the
1043 * primary will spin, waiting for us to halt.
1044 */
1045 alpha_broadcast_ipi(ALPHA_IPI_HALT);
1046
1047 for (i = 0; i < 10000; i++) {
1048 alpha_mb();
1049 if (cpus_running == wait_mask)
1050 break;
1051 delay(1000);
1052 }
1053 alpha_mb();
1054 if (cpus_running != wait_mask)
1055 printf("WARNING: Unable to halt secondary CPUs (0x%lx)\n",
1056 cpus_running);
1057 #endif /* MULTIPROCESSOR */
1058
1059 /* If rebooting and a dump is requested do it. */
1060 #if 0
1061 if ((boothowto & (RB_DUMP | RB_HALT)) == RB_DUMP)
1062 #else
1063 if (boothowto & RB_DUMP)
1064 #endif
1065 dumpsys();
1066
1067 haltsys:
1068
1069 /* run any shutdown hooks */
1070 doshutdownhooks();
1071
1072 #ifdef BOOTKEY
1073 printf("hit any key to %s...\n", howto & RB_HALT ? "halt" : "reboot");
1074 cnpollc(1); /* for proper keyboard command handling */
1075 cngetc();
1076 cnpollc(0);
1077 printf("\n");
1078 #endif
1079
1080 /* Finally, powerdown/halt/reboot the system. */
1081 if ((boothowto & RB_POWERDOWN) == RB_POWERDOWN &&
1082 platform.powerdown != NULL) {
1083 (*platform.powerdown)();
1084 printf("WARNING: powerdown failed!\n");
1085 }
1086 printf("%s\n\n", (boothowto & RB_HALT) ? "halted." : "rebooting...");
1087 #if defined(MULTIPROCESSOR)
1088 if (cpu_id != hwrpb->rpb_primary_cpu_id)
1089 cpu_halt();
1090 else
1091 #endif
1092 prom_halt(boothowto & RB_HALT);
1093 /*NOTREACHED*/
1094 }
1095
1096 /*
1097 * These variables are needed by /sbin/savecore
1098 */
1099 u_int32_t dumpmag = 0x8fca0101; /* magic number */
1100 int dumpsize = 0; /* pages */
1101 long dumplo = 0; /* blocks */
1102
1103 /*
1104 * cpu_dumpsize: calculate size of machine-dependent kernel core dump headers.
1105 */
1106 int
1107 cpu_dumpsize()
1108 {
1109 int size;
1110
1111 size = ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t)) +
1112 ALIGN(mem_cluster_cnt * sizeof(phys_ram_seg_t));
1113 if (roundup(size, dbtob(1)) != dbtob(1))
1114 return -1;
1115
1116 return (1);
1117 }
1118
1119 /*
1120 * cpu_dump_mempagecnt: calculate size of RAM (in pages) to be dumped.
1121 */
1122 u_long
1123 cpu_dump_mempagecnt()
1124 {
1125 u_long i, n;
1126
1127 n = 0;
1128 for (i = 0; i < mem_cluster_cnt; i++)
1129 n += atop(mem_clusters[i].size);
1130 return (n);
1131 }
1132
1133 /*
1134 * cpu_dump: dump machine-dependent kernel core dump headers.
1135 */
1136 int
1137 cpu_dump()
1138 {
1139 int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
1140 char buf[dbtob(1)];
1141 kcore_seg_t *segp;
1142 cpu_kcore_hdr_t *cpuhdrp;
1143 phys_ram_seg_t *memsegp;
1144 const struct bdevsw *bdev;
1145 int i;
1146
1147 bdev = bdevsw_lookup(dumpdev);
1148 if (bdev == NULL)
1149 return (ENXIO);
1150 dump = bdev->d_dump;
1151
1152 memset(buf, 0, sizeof buf);
1153 segp = (kcore_seg_t *)buf;
1154 cpuhdrp = (cpu_kcore_hdr_t *)&buf[ALIGN(sizeof(*segp))];
1155 memsegp = (phys_ram_seg_t *)&buf[ ALIGN(sizeof(*segp)) +
1156 ALIGN(sizeof(*cpuhdrp))];
1157
1158 /*
1159 * Generate a segment header.
1160 */
1161 CORE_SETMAGIC(*segp, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
1162 segp->c_size = dbtob(1) - ALIGN(sizeof(*segp));
1163
1164 /*
1165 * Add the machine-dependent header info.
1166 */
1167 cpuhdrp->lev1map_pa = ALPHA_K0SEG_TO_PHYS((vaddr_t)kernel_lev1map);
1168 cpuhdrp->page_size = PAGE_SIZE;
1169 cpuhdrp->nmemsegs = mem_cluster_cnt;
1170
1171 /*
1172 * Fill in the memory segment descriptors.
1173 */
1174 for (i = 0; i < mem_cluster_cnt; i++) {
1175 memsegp[i].start = mem_clusters[i].start;
1176 memsegp[i].size = mem_clusters[i].size & ~PAGE_MASK;
1177 }
1178
1179 return (dump(dumpdev, dumplo, (caddr_t)buf, dbtob(1)));
1180 }
1181
1182 /*
1183 * This is called by main to set dumplo and dumpsize.
1184 * Dumps always skip the first PAGE_SIZE of disk space
1185 * in case there might be a disk label stored there.
1186 * If there is extra space, put dump at the end to
1187 * reduce the chance that swapping trashes it.
1188 */
1189 void
1190 cpu_dumpconf()
1191 {
1192 const struct bdevsw *bdev;
1193 int nblks, dumpblks; /* size of dump area */
1194
1195 if (dumpdev == NODEV)
1196 goto bad;
1197 bdev = bdevsw_lookup(dumpdev);
1198 if (bdev == NULL)
1199 panic("dumpconf: bad dumpdev=0x%x", dumpdev);
1200 if (bdev->d_psize == NULL)
1201 goto bad;
1202 nblks = (*bdev->d_psize)(dumpdev);
1203 if (nblks <= ctod(1))
1204 goto bad;
1205
1206 dumpblks = cpu_dumpsize();
1207 if (dumpblks < 0)
1208 goto bad;
1209 dumpblks += ctod(cpu_dump_mempagecnt());
1210
1211 /* If dump won't fit (incl. room for possible label), punt. */
1212 if (dumpblks > (nblks - ctod(1)))
1213 goto bad;
1214
1215 /* Put dump at end of partition */
1216 dumplo = nblks - dumpblks;
1217
1218 /* dumpsize is in page units, and doesn't include headers. */
1219 dumpsize = cpu_dump_mempagecnt();
1220 return;
1221
1222 bad:
1223 dumpsize = 0;
1224 return;
1225 }
1226
1227 /*
1228 * Dump the kernel's image to the swap partition.
1229 */
1230 #define BYTES_PER_DUMP PAGE_SIZE
1231
1232 void
1233 dumpsys()
1234 {
1235 const struct bdevsw *bdev;
1236 u_long totalbytesleft, bytes, i, n, memcl;
1237 u_long maddr;
1238 int psize;
1239 daddr_t blkno;
1240 int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
1241 int error;
1242
1243 /* Save registers. */
1244 savectx(&dumppcb);
1245
1246 if (dumpdev == NODEV)
1247 return;
1248 bdev = bdevsw_lookup(dumpdev);
1249 if (bdev == NULL || bdev->d_psize == NULL)
1250 return;
1251
1252 /*
1253 * For dumps during autoconfiguration,
1254 * if dump device has already configured...
1255 */
1256 if (dumpsize == 0)
1257 cpu_dumpconf();
1258 if (dumplo <= 0) {
1259 printf("\ndump to dev %u,%u not possible\n", major(dumpdev),
1260 minor(dumpdev));
1261 return;
1262 }
1263 printf("\ndumping to dev %u,%u offset %ld\n", major(dumpdev),
1264 minor(dumpdev), dumplo);
1265
1266 psize = (*bdev->d_psize)(dumpdev);
1267 printf("dump ");
1268 if (psize == -1) {
1269 printf("area unavailable\n");
1270 return;
1271 }
1272
1273 /* XXX should purge all outstanding keystrokes. */
1274
1275 if ((error = cpu_dump()) != 0)
1276 goto err;
1277
1278 totalbytesleft = ptoa(cpu_dump_mempagecnt());
1279 blkno = dumplo + cpu_dumpsize();
1280 dump = bdev->d_dump;
1281 error = 0;
1282
1283 for (memcl = 0; memcl < mem_cluster_cnt; memcl++) {
1284 maddr = mem_clusters[memcl].start;
1285 bytes = mem_clusters[memcl].size & ~PAGE_MASK;
1286
1287 for (i = 0; i < bytes; i += n, totalbytesleft -= n) {
1288
1289 /* Print out how many MBs we to go. */
1290 if ((totalbytesleft % (1024*1024)) == 0)
1291 printf("%ld ", totalbytesleft / (1024 * 1024));
1292
1293 /* Limit size for next transfer. */
1294 n = bytes - i;
1295 if (n > BYTES_PER_DUMP)
1296 n = BYTES_PER_DUMP;
1297
1298 error = (*dump)(dumpdev, blkno,
1299 (caddr_t)ALPHA_PHYS_TO_K0SEG(maddr), n);
1300 if (error)
1301 goto err;
1302 maddr += n;
1303 blkno += btodb(n); /* XXX? */
1304
1305 /* XXX should look for keystrokes, to cancel. */
1306 }
1307 }
1308
1309 err:
1310 switch (error) {
1311
1312 case ENXIO:
1313 printf("device bad\n");
1314 break;
1315
1316 case EFAULT:
1317 printf("device not ready\n");
1318 break;
1319
1320 case EINVAL:
1321 printf("area improper\n");
1322 break;
1323
1324 case EIO:
1325 printf("i/o error\n");
1326 break;
1327
1328 case EINTR:
1329 printf("aborted from console\n");
1330 break;
1331
1332 case 0:
1333 printf("succeeded\n");
1334 break;
1335
1336 default:
1337 printf("error %d\n", error);
1338 break;
1339 }
1340 printf("\n\n");
1341 delay(1000);
1342 }
1343
1344 void
1345 frametoreg(framep, regp)
1346 const struct trapframe *framep;
1347 struct reg *regp;
1348 {
1349
1350 regp->r_regs[R_V0] = framep->tf_regs[FRAME_V0];
1351 regp->r_regs[R_T0] = framep->tf_regs[FRAME_T0];
1352 regp->r_regs[R_T1] = framep->tf_regs[FRAME_T1];
1353 regp->r_regs[R_T2] = framep->tf_regs[FRAME_T2];
1354 regp->r_regs[R_T3] = framep->tf_regs[FRAME_T3];
1355 regp->r_regs[R_T4] = framep->tf_regs[FRAME_T4];
1356 regp->r_regs[R_T5] = framep->tf_regs[FRAME_T5];
1357 regp->r_regs[R_T6] = framep->tf_regs[FRAME_T6];
1358 regp->r_regs[R_T7] = framep->tf_regs[FRAME_T7];
1359 regp->r_regs[R_S0] = framep->tf_regs[FRAME_S0];
1360 regp->r_regs[R_S1] = framep->tf_regs[FRAME_S1];
1361 regp->r_regs[R_S2] = framep->tf_regs[FRAME_S2];
1362 regp->r_regs[R_S3] = framep->tf_regs[FRAME_S3];
1363 regp->r_regs[R_S4] = framep->tf_regs[FRAME_S4];
1364 regp->r_regs[R_S5] = framep->tf_regs[FRAME_S5];
1365 regp->r_regs[R_S6] = framep->tf_regs[FRAME_S6];
1366 regp->r_regs[R_A0] = framep->tf_regs[FRAME_A0];
1367 regp->r_regs[R_A1] = framep->tf_regs[FRAME_A1];
1368 regp->r_regs[R_A2] = framep->tf_regs[FRAME_A2];
1369 regp->r_regs[R_A3] = framep->tf_regs[FRAME_A3];
1370 regp->r_regs[R_A4] = framep->tf_regs[FRAME_A4];
1371 regp->r_regs[R_A5] = framep->tf_regs[FRAME_A5];
1372 regp->r_regs[R_T8] = framep->tf_regs[FRAME_T8];
1373 regp->r_regs[R_T9] = framep->tf_regs[FRAME_T9];
1374 regp->r_regs[R_T10] = framep->tf_regs[FRAME_T10];
1375 regp->r_regs[R_T11] = framep->tf_regs[FRAME_T11];
1376 regp->r_regs[R_RA] = framep->tf_regs[FRAME_RA];
1377 regp->r_regs[R_T12] = framep->tf_regs[FRAME_T12];
1378 regp->r_regs[R_AT] = framep->tf_regs[FRAME_AT];
1379 regp->r_regs[R_GP] = framep->tf_regs[FRAME_GP];
1380 /* regp->r_regs[R_SP] = framep->tf_regs[FRAME_SP]; XXX */
1381 regp->r_regs[R_ZERO] = 0;
1382 }
1383
1384 void
1385 regtoframe(regp, framep)
1386 const struct reg *regp;
1387 struct trapframe *framep;
1388 {
1389
1390 framep->tf_regs[FRAME_V0] = regp->r_regs[R_V0];
1391 framep->tf_regs[FRAME_T0] = regp->r_regs[R_T0];
1392 framep->tf_regs[FRAME_T1] = regp->r_regs[R_T1];
1393 framep->tf_regs[FRAME_T2] = regp->r_regs[R_T2];
1394 framep->tf_regs[FRAME_T3] = regp->r_regs[R_T3];
1395 framep->tf_regs[FRAME_T4] = regp->r_regs[R_T4];
1396 framep->tf_regs[FRAME_T5] = regp->r_regs[R_T5];
1397 framep->tf_regs[FRAME_T6] = regp->r_regs[R_T6];
1398 framep->tf_regs[FRAME_T7] = regp->r_regs[R_T7];
1399 framep->tf_regs[FRAME_S0] = regp->r_regs[R_S0];
1400 framep->tf_regs[FRAME_S1] = regp->r_regs[R_S1];
1401 framep->tf_regs[FRAME_S2] = regp->r_regs[R_S2];
1402 framep->tf_regs[FRAME_S3] = regp->r_regs[R_S3];
1403 framep->tf_regs[FRAME_S4] = regp->r_regs[R_S4];
1404 framep->tf_regs[FRAME_S5] = regp->r_regs[R_S5];
1405 framep->tf_regs[FRAME_S6] = regp->r_regs[R_S6];
1406 framep->tf_regs[FRAME_A0] = regp->r_regs[R_A0];
1407 framep->tf_regs[FRAME_A1] = regp->r_regs[R_A1];
1408 framep->tf_regs[FRAME_A2] = regp->r_regs[R_A2];
1409 framep->tf_regs[FRAME_A3] = regp->r_regs[R_A3];
1410 framep->tf_regs[FRAME_A4] = regp->r_regs[R_A4];
1411 framep->tf_regs[FRAME_A5] = regp->r_regs[R_A5];
1412 framep->tf_regs[FRAME_T8] = regp->r_regs[R_T8];
1413 framep->tf_regs[FRAME_T9] = regp->r_regs[R_T9];
1414 framep->tf_regs[FRAME_T10] = regp->r_regs[R_T10];
1415 framep->tf_regs[FRAME_T11] = regp->r_regs[R_T11];
1416 framep->tf_regs[FRAME_RA] = regp->r_regs[R_RA];
1417 framep->tf_regs[FRAME_T12] = regp->r_regs[R_T12];
1418 framep->tf_regs[FRAME_AT] = regp->r_regs[R_AT];
1419 framep->tf_regs[FRAME_GP] = regp->r_regs[R_GP];
1420 /* framep->tf_regs[FRAME_SP] = regp->r_regs[R_SP]; XXX */
1421 /* ??? = regp->r_regs[R_ZERO]; */
1422 }
1423
1424 void
1425 printregs(regp)
1426 struct reg *regp;
1427 {
1428 int i;
1429
1430 for (i = 0; i < 32; i++)
1431 printf("R%d:\t0x%016lx%s", i, regp->r_regs[i],
1432 i & 1 ? "\n" : "\t");
1433 }
1434
1435 void
1436 regdump(framep)
1437 struct trapframe *framep;
1438 {
1439 struct reg reg;
1440
1441 frametoreg(framep, ®);
1442 reg.r_regs[R_SP] = alpha_pal_rdusp();
1443
1444 printf("REGISTERS:\n");
1445 printregs(®);
1446 }
1447
1448
1449
1450 void *
1451 getframe(const struct lwp *l, int sig, int *onstack)
1452 {
1453 void * frame;
1454 struct proc *p;
1455
1456 p = l->l_proc;
1457
1458 /* Do we need to jump onto the signal stack? */
1459 *onstack =
1460 (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
1461 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
1462
1463 if (*onstack)
1464 frame = (void *)((caddr_t)p->p_sigctx.ps_sigstk.ss_sp +
1465 p->p_sigctx.ps_sigstk.ss_size);
1466 else
1467 frame = (void *)(alpha_pal_rdusp());
1468 return (frame);
1469 }
1470
1471 void
1472 buildcontext(struct lwp *l, const void *catcher, const void *tramp, const void *fp)
1473 {
1474 struct trapframe *tf = l->l_md.md_tf;
1475
1476 tf->tf_regs[FRAME_RA] = (u_int64_t)tramp;
1477 tf->tf_regs[FRAME_PC] = (u_int64_t)catcher;
1478 tf->tf_regs[FRAME_T12] = (u_int64_t)catcher;
1479 alpha_pal_wrusp((unsigned long)fp);
1480 }
1481
1482
1483 /*
1484 * Send an interrupt to process, new style
1485 */
1486 void
1487 sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
1488 {
1489 struct lwp *l = curlwp;
1490 struct proc *p = l->l_proc;
1491 struct sigacts *ps = p->p_sigacts;
1492 int onstack, sig = ksi->ksi_signo;
1493 struct sigframe_siginfo *fp, frame;
1494 struct trapframe *tf;
1495 sig_t catcher = SIGACTION(p, ksi->ksi_signo).sa_handler;
1496
1497 fp = (struct sigframe_siginfo *)getframe(l,ksi->ksi_signo,&onstack);
1498 tf = l->l_md.md_tf;
1499
1500 /* Allocate space for the signal handler context. */
1501 fp--;
1502
1503 /* Build stack frame for signal trampoline. */
1504 switch (ps->sa_sigdesc[sig].sd_vers) {
1505 case 0: /* handled by sendsig_sigcontext */
1506 case 1: /* handled by sendsig_sigcontext */
1507 default: /* unknown version */
1508 printf("nsendsig: bad version %d\n",
1509 ps->sa_sigdesc[sig].sd_vers);
1510 sigexit(l, SIGILL);
1511 case 2:
1512 break;
1513 }
1514
1515 #ifdef DEBUG
1516 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
1517 printf("sendsig_siginfo(%d): sig %d ssp %p usp %p\n", p->p_pid,
1518 sig, &onstack, fp);
1519 #endif
1520
1521 /* Build stack frame for signal trampoline. */
1522
1523 frame.sf_si._info = ksi->ksi_info;
1524 frame.sf_uc.uc_flags = _UC_SIGMASK;
1525 frame.sf_uc.uc_sigmask = *mask;
1526 frame.sf_uc.uc_link = NULL;
1527 memset(&frame.sf_uc.uc_stack, 0, sizeof(frame.sf_uc.uc_stack));
1528 cpu_getmcontext(l, &frame.sf_uc.uc_mcontext, &frame.sf_uc.uc_flags);
1529
1530 if (copyout(&frame, fp, sizeof(frame)) != 0) {
1531 /*
1532 * Process has trashed its stack; give it an illegal
1533 * instruction to halt it in its tracks.
1534 */
1535 #ifdef DEBUG
1536 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
1537 printf("sendsig_siginfo(%d): copyout failed on sig %d\n",
1538 p->p_pid, sig);
1539 #endif
1540 sigexit(l, SIGILL);
1541 /* NOTREACHED */
1542 }
1543
1544 #ifdef DEBUG
1545 if (sigdebug & SDB_FOLLOW)
1546 printf("sendsig_siginfo(%d): sig %d usp %p code %x\n",
1547 p->p_pid, sig, fp, ksi->ksi_code);
1548 #endif
1549
1550 /*
1551 * Set up the registers to directly invoke the signal handler. The
1552 * signal trampoline is then used to return from the signal. Note
1553 * the trampoline version numbers are coordinated with machine-
1554 * dependent code in libc.
1555 */
1556
1557 tf->tf_regs[FRAME_A0] = sig;
1558 tf->tf_regs[FRAME_A1] = (u_int64_t)&fp->sf_si;
1559 tf->tf_regs[FRAME_A2] = (u_int64_t)&fp->sf_uc;
1560
1561 buildcontext(l,catcher,ps->sa_sigdesc[sig].sd_tramp,fp);
1562
1563 /* Remember that we're now on the signal stack. */
1564 if (onstack)
1565 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
1566
1567 #ifdef DEBUG
1568 if (sigdebug & SDB_FOLLOW)
1569 printf("sendsig_siginfo(%d): pc %lx, catcher %lx\n", p->p_pid,
1570 tf->tf_regs[FRAME_PC], tf->tf_regs[FRAME_A3]);
1571 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
1572 printf("sendsig_siginfo(%d): sig %d returns\n",
1573 p->p_pid, sig);
1574 #endif
1575 }
1576
1577
1578 void
1579 sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
1580 {
1581 #ifdef COMPAT_16
1582 if (curproc->p_sigacts->sa_sigdesc[ksi->ksi_signo].sd_vers < 2) {
1583 sendsig_sigcontext(ksi, mask);
1584 } else {
1585 #endif
1586 #ifdef DEBUG
1587 if (sigdebug & SDB_FOLLOW)
1588 printf("sendsig: sendsig called: sig %d vers %d\n",
1589 ksi->ksi_signo,
1590 curproc->p_sigacts->sa_sigdesc[ksi->ksi_signo].sd_vers);
1591 #endif
1592 sendsig_siginfo(ksi, mask);
1593 #ifdef COMPAT_16
1594 }
1595 #endif
1596 }
1597
1598 void
1599 cpu_upcall(struct lwp *l, int type, int nevents, int ninterrupted, void *sas, void *ap, void *sp, sa_upcall_t upcall)
1600 {
1601 struct trapframe *tf;
1602
1603 tf = l->l_md.md_tf;
1604
1605 tf->tf_regs[FRAME_PC] = (u_int64_t)upcall;
1606 tf->tf_regs[FRAME_RA] = 0;
1607 tf->tf_regs[FRAME_A0] = type;
1608 tf->tf_regs[FRAME_A1] = (u_int64_t)sas;
1609 tf->tf_regs[FRAME_A2] = nevents;
1610 tf->tf_regs[FRAME_A3] = ninterrupted;
1611 tf->tf_regs[FRAME_A4] = (u_int64_t)ap;
1612 tf->tf_regs[FRAME_T12] = (u_int64_t)upcall; /* t12 is pv */
1613 alpha_pal_wrusp((unsigned long)sp);
1614 }
1615
1616 /*
1617 * machine dependent system variables.
1618 */
1619 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup")
1620 {
1621
1622 sysctl_createv(SYSCTL_PERMANENT,
1623 CTLTYPE_NODE, "machdep", NULL,
1624 NULL, 0, NULL, 0,
1625 CTL_MACHDEP, CTL_EOL);
1626
1627 sysctl_createv(SYSCTL_PERMANENT,
1628 CTLTYPE_STRUCT, "console_device", NULL,
1629 sysctl_consdev, 0, NULL, sizeof(dev_t),
1630 CTL_MACHDEP, CPU_CONSDEV, CTL_EOL);
1631 sysctl_createv(SYSCTL_PERMANENT,
1632 CTLTYPE_STRING, "root_device", NULL,
1633 sysctl_root_device, 0, NULL, 0,
1634 CTL_MACHDEP, CPU_ROOT_DEVICE, CTL_EOL);
1635 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
1636 CTLTYPE_INT, "unaligned_print", NULL,
1637 NULL, 0, &alpha_unaligned_print, 0,
1638 CTL_MACHDEP, CPU_UNALIGNED_PRINT, CTL_EOL);
1639 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
1640 CTLTYPE_INT, "unaligned_fix", NULL,
1641 NULL, 0, &alpha_unaligned_fix, 0,
1642 CTL_MACHDEP, CPU_UNALIGNED_FIX, CTL_EOL);
1643 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
1644 CTLTYPE_INT, "unaligned_sigbus", NULL,
1645 NULL, 0, &alpha_unaligned_sigbus, 0,
1646 CTL_MACHDEP, CPU_UNALIGNED_SIGBUS, CTL_EOL);
1647 sysctl_createv(SYSCTL_PERMANENT,
1648 CTLTYPE_STRING, "booted_kernel", NULL,
1649 NULL, 0, bootinfo.booted_kernel, 0,
1650 CTL_MACHDEP, CPU_BOOTED_KERNEL, CTL_EOL);
1651 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
1652 CTLTYPE_INT, "fp_sync_complete", NULL,
1653 NULL, 0, &alpha_fp_sync_complete, 0,
1654 CTL_MACHDEP, CPU_FP_SYNC_COMPLETE, CTL_EOL);
1655 }
1656
1657 /*
1658 * Set registers on exec.
1659 */
1660 void
1661 setregs(l, pack, stack)
1662 register struct lwp *l;
1663 struct exec_package *pack;
1664 u_long stack;
1665 {
1666 struct trapframe *tfp = l->l_md.md_tf;
1667 #ifdef DEBUG
1668 int i;
1669 #endif
1670
1671 #ifdef DEBUG
1672 /*
1673 * Crash and dump, if the user requested it.
1674 */
1675 if (boothowto & RB_DUMP)
1676 panic("crash requested by boot flags");
1677 #endif
1678
1679 #ifdef DEBUG
1680 for (i = 0; i < FRAME_SIZE; i++)
1681 tfp->tf_regs[i] = 0xbabefacedeadbeef;
1682 #else
1683 memset(tfp->tf_regs, 0, FRAME_SIZE * sizeof tfp->tf_regs[0]);
1684 #endif
1685 memset(&l->l_addr->u_pcb.pcb_fp, 0, sizeof l->l_addr->u_pcb.pcb_fp);
1686 alpha_pal_wrusp(stack);
1687 tfp->tf_regs[FRAME_PS] = ALPHA_PSL_USERSET;
1688 tfp->tf_regs[FRAME_PC] = pack->ep_entry & ~3;
1689
1690 tfp->tf_regs[FRAME_A0] = stack; /* a0 = sp */
1691 tfp->tf_regs[FRAME_A1] = 0; /* a1 = rtld cleanup */
1692 tfp->tf_regs[FRAME_A2] = 0; /* a2 = rtld object */
1693 tfp->tf_regs[FRAME_A3] = (u_int64_t)l->l_proc->p_psstr; /* a3 = ps_strings */
1694 tfp->tf_regs[FRAME_T12] = tfp->tf_regs[FRAME_PC]; /* a.k.a. PV */
1695
1696 l->l_md.md_flags &= ~MDP_FPUSED;
1697 if (__predict_true((l->l_md.md_flags & IEEE_INHERIT) == 0)) {
1698 l->l_md.md_flags &= ~MDP_FP_C;
1699 l->l_addr->u_pcb.pcb_fp.fpr_cr = FPCR_DYN(FP_RN);
1700 }
1701 if (l->l_addr->u_pcb.pcb_fpcpu != NULL)
1702 fpusave_proc(l, 0);
1703 }
1704
1705 /*
1706 * Release the FPU.
1707 */
1708 void
1709 fpusave_cpu(struct cpu_info *ci, int save)
1710 {
1711 struct lwp *l;
1712 #if defined(MULTIPROCESSOR)
1713 int s;
1714 #endif
1715
1716 KDASSERT(ci == curcpu());
1717
1718 #if defined(MULTIPROCESSOR)
1719 atomic_setbits_ulong(&ci->ci_flags, CPUF_FPUSAVE);
1720 #endif
1721
1722 l = ci->ci_fpcurlwp;
1723 if (l == NULL)
1724 goto out;
1725
1726 if (save) {
1727 alpha_pal_wrfen(1);
1728 savefpstate(&l->l_addr->u_pcb.pcb_fp);
1729 }
1730
1731 alpha_pal_wrfen(0);
1732
1733 FPCPU_LOCK(&l->l_addr->u_pcb, s);
1734
1735 l->l_addr->u_pcb.pcb_fpcpu = NULL;
1736 ci->ci_fpcurlwp = NULL;
1737
1738 FPCPU_UNLOCK(&l->l_addr->u_pcb, s);
1739
1740 out:
1741 #if defined(MULTIPROCESSOR)
1742 atomic_clearbits_ulong(&ci->ci_flags, CPUF_FPUSAVE);
1743 #endif
1744 return;
1745 }
1746
1747 /*
1748 * Synchronize FP state for this process.
1749 */
1750 void
1751 fpusave_proc(struct lwp *l, int save)
1752 {
1753 struct cpu_info *ci = curcpu();
1754 struct cpu_info *oci;
1755 #if defined(MULTIPROCESSOR)
1756 u_long ipi = save ? ALPHA_IPI_SYNCH_FPU : ALPHA_IPI_DISCARD_FPU;
1757 int s, spincount;
1758 #endif
1759
1760 KDASSERT(l->l_addr != NULL);
1761
1762 FPCPU_LOCK(&l->l_addr->u_pcb, s);
1763
1764 oci = l->l_addr->u_pcb.pcb_fpcpu;
1765 if (oci == NULL) {
1766 FPCPU_UNLOCK(&l->l_addr->u_pcb, s);
1767 return;
1768 }
1769
1770 #if defined(MULTIPROCESSOR)
1771 if (oci == ci) {
1772 KASSERT(ci->ci_fpcurlwp == l);
1773 FPCPU_UNLOCK(&l->l_addr->u_pcb, s);
1774 fpusave_cpu(ci, save);
1775 return;
1776 }
1777
1778 KASSERT(oci->ci_fpcurlwp == l);
1779 alpha_send_ipi(oci->ci_cpuid, ipi);
1780 FPCPU_UNLOCK(&l->l_addr->u_pcb, s);
1781
1782 spincount = 0;
1783 while (l->l_addr->u_pcb.pcb_fpcpu != NULL) {
1784 spincount++;
1785 delay(1000); /* XXX */
1786 if (spincount > 10000)
1787 panic("fpsave ipi didn't");
1788 }
1789 #else
1790 KASSERT(ci->ci_fpcurlwp == l);
1791 FPCPU_UNLOCK(&l->l_addr->u_pcb, s);
1792 fpusave_cpu(ci, save);
1793 #endif /* MULTIPROCESSOR */
1794 }
1795
1796 /*
1797 * Wait "n" microseconds.
1798 */
1799 void
1800 delay(n)
1801 unsigned long n;
1802 {
1803 unsigned long pcc0, pcc1, curcycle, cycles, usec;
1804
1805 if (n == 0)
1806 return;
1807
1808 pcc0 = alpha_rpcc() & 0xffffffffUL;
1809 cycles = 0;
1810 usec = 0;
1811
1812 while (usec <= n) {
1813 /*
1814 * Get the next CPU cycle count- assumes that we cannot
1815 * have had more than one 32 bit overflow.
1816 */
1817 pcc1 = alpha_rpcc() & 0xffffffffUL;
1818 if (pcc1 < pcc0)
1819 curcycle = (pcc1 + 0x100000000UL) - pcc0;
1820 else
1821 curcycle = pcc1 - pcc0;
1822
1823 /*
1824 * We now have the number of processor cycles since we
1825 * last checked. Add the current cycle count to the
1826 * running total. If it's over cycles_per_usec, increment
1827 * the usec counter.
1828 */
1829 cycles += curcycle;
1830 while (cycles > cycles_per_usec) {
1831 usec++;
1832 cycles -= cycles_per_usec;
1833 }
1834 pcc0 = pcc1;
1835 }
1836 }
1837
1838 #ifdef EXEC_ECOFF
1839 void
1840 cpu_exec_ecoff_setregs(l, epp, stack)
1841 struct lwp *l;
1842 struct exec_package *epp;
1843 u_long stack;
1844 {
1845 struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
1846
1847 l->l_md.md_tf->tf_regs[FRAME_GP] = execp->a.gp_value;
1848 }
1849
1850 /*
1851 * cpu_exec_ecoff_hook():
1852 * cpu-dependent ECOFF format hook for execve().
1853 *
1854 * Do any machine-dependent diddling of the exec package when doing ECOFF.
1855 *
1856 */
1857 int
1858 cpu_exec_ecoff_probe(p, epp)
1859 struct proc *p;
1860 struct exec_package *epp;
1861 {
1862 struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
1863 int error;
1864
1865 if (execp->f.f_magic == ECOFF_MAGIC_NETBSD_ALPHA)
1866 error = 0;
1867 else
1868 error = ENOEXEC;
1869
1870 return (error);
1871 }
1872 #endif /* EXEC_ECOFF */
1873
1874 int
1875 alpha_pa_access(pa)
1876 u_long pa;
1877 {
1878 int i;
1879
1880 for (i = 0; i < mem_cluster_cnt; i++) {
1881 if (pa < mem_clusters[i].start)
1882 continue;
1883 if ((pa - mem_clusters[i].start) >=
1884 (mem_clusters[i].size & ~PAGE_MASK))
1885 continue;
1886 return (mem_clusters[i].size & PAGE_MASK); /* prot */
1887 }
1888
1889 /*
1890 * Address is not a memory address. If we're secure, disallow
1891 * access. Otherwise, grant read/write.
1892 */
1893 if (securelevel > 0)
1894 return (PROT_NONE);
1895 else
1896 return (PROT_READ | PROT_WRITE);
1897 }
1898
1899 /* XXX XXX BEGIN XXX XXX */
1900 paddr_t alpha_XXX_dmamap_or; /* XXX */
1901 /* XXX */
1902 paddr_t /* XXX */
1903 alpha_XXX_dmamap(v) /* XXX */
1904 vaddr_t v; /* XXX */
1905 { /* XXX */
1906 /* XXX */
1907 return (vtophys(v) | alpha_XXX_dmamap_or); /* XXX */
1908 } /* XXX */
1909 /* XXX XXX END XXX XXX */
1910
1911 char *
1912 dot_conv(x)
1913 unsigned long x;
1914 {
1915 int i;
1916 char *xc;
1917 static int next;
1918 static char space[2][20];
1919
1920 xc = space[next ^= 1] + sizeof space[0];
1921 *--xc = '\0';
1922 for (i = 0;; ++i) {
1923 if (i && (i & 3) == 0)
1924 *--xc = '.';
1925 *--xc = "0123456789abcdef"[x & 0xf];
1926 x >>= 4;
1927 if (x == 0)
1928 break;
1929 }
1930 return xc;
1931 }
1932
1933 void
1934 cpu_getmcontext(l, mcp, flags)
1935 struct lwp *l;
1936 mcontext_t *mcp;
1937 unsigned int *flags;
1938 {
1939 struct trapframe *frame = l->l_md.md_tf;
1940 __greg_t *gr = mcp->__gregs;
1941 __greg_t ras_pc;
1942
1943 /* Save register context. */
1944 frametoreg(frame, (struct reg *)gr);
1945 /* XXX if there's a better, general way to get the USP of
1946 * an LWP that might or might not be curlwp, I'd like to know
1947 * about it.
1948 */
1949 if (l == curlwp) {
1950 gr[_REG_SP] = alpha_pal_rdusp();
1951 gr[_REG_UNIQUE] = alpha_pal_rdunique();
1952 } else {
1953 gr[_REG_SP] = l->l_addr->u_pcb.pcb_hw.apcb_usp;
1954 gr[_REG_UNIQUE] = l->l_addr->u_pcb.pcb_hw.apcb_unique;
1955 }
1956 gr[_REG_PC] = frame->tf_regs[FRAME_PC];
1957 gr[_REG_PS] = frame->tf_regs[FRAME_PS];
1958
1959 if ((ras_pc = (__greg_t)ras_lookup(l->l_proc,
1960 (caddr_t) gr[_REG_PC])) != -1)
1961 gr[_REG_PC] = ras_pc;
1962
1963 *flags |= _UC_CPU | _UC_UNIQUE;
1964
1965 /* Save floating point register context, if any, and copy it. */
1966 if (l->l_md.md_flags & MDP_FPUSED) {
1967 fpusave_proc(l, 1);
1968 (void)memcpy(&mcp->__fpregs, &l->l_addr->u_pcb.pcb_fp,
1969 sizeof (mcp->__fpregs));
1970 mcp->__fpregs.__fp_fpcr = alpha_read_fp_c(l);
1971 *flags |= _UC_FPU;
1972 }
1973 }
1974
1975
1976 int
1977 cpu_setmcontext(l, mcp, flags)
1978 struct lwp *l;
1979 const mcontext_t *mcp;
1980 unsigned int flags;
1981 {
1982 struct trapframe *frame = l->l_md.md_tf;
1983 const __greg_t *gr = mcp->__gregs;
1984
1985 /* Restore register context, if any. */
1986 if (flags & _UC_CPU) {
1987 /* Check for security violations first. */
1988 if ((gr[_REG_PS] & ALPHA_PSL_USERSET) != ALPHA_PSL_USERSET ||
1989 (gr[_REG_PS] & ALPHA_PSL_USERCLR) != 0)
1990 return (EINVAL);
1991
1992 regtoframe((struct reg *)gr, l->l_md.md_tf);
1993 if (l == curlwp)
1994 alpha_pal_wrusp(gr[_REG_SP]);
1995 else
1996 l->l_addr->u_pcb.pcb_hw.apcb_usp = gr[_REG_SP];
1997 frame->tf_regs[FRAME_PC] = gr[_REG_PC];
1998 frame->tf_regs[FRAME_PS] = gr[_REG_PS];
1999 }
2000 if (flags & _UC_UNIQUE) {
2001 if (l == curlwp)
2002 alpha_pal_wrunique(gr[_REG_UNIQUE]);
2003 else
2004 l->l_addr->u_pcb.pcb_hw.apcb_unique = gr[_REG_UNIQUE];
2005 }
2006 /* Restore floating point register context, if any. */
2007 if (flags & _UC_FPU) {
2008 /* If we have an FP register context, get rid of it. */
2009 if (l->l_addr->u_pcb.pcb_fpcpu != NULL)
2010 fpusave_proc(l, 0);
2011 (void)memcpy(&l->l_addr->u_pcb.pcb_fp, &mcp->__fpregs,
2012 sizeof (l->l_addr->u_pcb.pcb_fp));
2013 l->l_md.md_flags = mcp->__fpregs.__fp_fpcr & MDP_FP_C;
2014 l->l_md.md_flags |= MDP_FPUSED;
2015 }
2016
2017 return (0);
2018 }
2019