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