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