machdep.c revision 1.323 1 /* $NetBSD: machdep.c,v 1.323 2009/11/26 00:19:11 matt 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.323 2009/11/26 00:19:11 matt 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_aout.h> /* for MID_* */
93 #include <sys/exec_ecoff.h>
94 #include <sys/core.h>
95 #include <sys/kcore.h>
96 #include <sys/ucontext.h>
97 #include <sys/conf.h>
98 #include <sys/ksyms.h>
99 #include <sys/kauth.h>
100 #include <sys/atomic.h>
101 #include <sys/cpu.h>
102
103 #include <machine/kcore.h>
104 #include <machine/fpu.h>
105
106 #include <sys/mount.h>
107 #include <sys/syscallargs.h>
108
109 #include <uvm/uvm_extern.h>
110 #include <sys/sysctl.h>
111
112 #include <dev/cons.h>
113
114 #include <machine/autoconf.h>
115 #include <machine/reg.h>
116 #include <machine/rpb.h>
117 #include <machine/prom.h>
118 #include <machine/cpuconf.h>
119 #include <machine/ieeefp.h>
120
121 #ifdef DDB
122 #include <machine/db_machdep.h>
123 #include <ddb/db_access.h>
124 #include <ddb/db_sym.h>
125 #include <ddb/db_extern.h>
126 #include <ddb/db_interface.h>
127 #endif
128
129 #ifdef KGDB
130 #include <sys/kgdb.h>
131 #endif
132
133 #ifdef DEBUG
134 #include <machine/sigdebug.h>
135 #endif
136
137 #include <machine/alpha.h>
138
139 #include "ksyms.h"
140
141 struct vm_map *mb_map = NULL;
142 struct vm_map *phys_map = NULL;
143
144 void *msgbufaddr;
145
146 int maxmem; /* max memory per process */
147
148 int totalphysmem; /* total amount of physical memory in system */
149 int physmem; /* physical memory used by NetBSD + some rsvd */
150 int resvmem; /* amount of memory reserved for PROM */
151 int unusedmem; /* amount of memory for OS that we don't use */
152 int unknownmem; /* amount of memory with an unknown use */
153
154 int cputype; /* system type, from the RPB */
155
156 int bootdev_debug = 0; /* patchable, or from DDB */
157
158 /*
159 * XXX We need an address to which we can assign things so that they
160 * won't be optimized away because we didn't use the value.
161 */
162 u_int32_t no_optimize;
163
164 /* the following is used externally (sysctl_hw) */
165 char machine[] = MACHINE; /* from <machine/param.h> */
166 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */
167 char cpu_model[128];
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(void);
204 int cpu_dumpsize(void);
205 u_long cpu_dump_mempagecnt(void);
206 void dumpsys(void);
207 void identifycpu(void);
208 void printregs(struct reg *);
209
210 void
211 alpha_init(u_long pfn, u_long ptb, u_long bim, u_long bip, u_long biv)
212 /* pfn: first free PFN number */
213 /* ptb: PFN of current level 1 page table */
214 /* bim: bootinfo magic */
215 /* bip: bootinfo pointer */
216 /* 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 = (struct user *)uvm_pageboot_alloc(UPAGES * PAGE_SIZE);
640
641 /*
642 * Initialize the virtual memory system, and set the
643 * page table base register in proc 0's PCB.
644 */
645 pmap_bootstrap(ALPHA_PHYS_TO_K0SEG(ptb << PGSHIFT),
646 hwrpb->rpb_max_asn, hwrpb->rpb_pcs_cnt);
647
648 /*
649 * Initialize the rest of proc 0's PCB, and cache its physical
650 * address.
651 */
652 lwp0.l_md.md_pcbpaddr =
653 (struct pcb *)ALPHA_K0SEG_TO_PHYS((vaddr_t)&lwp0.l_addr->u_pcb);
654
655 /*
656 * Set the kernel sp, reserving space for an (empty) trapframe,
657 * and make lwp0's trapframe pointer point to it for sanity.
658 */
659 lwp0.l_addr->u_pcb.pcb_hw.apcb_ksp =
660 (vaddr_t)lwp0.l_addr + USPACE - sizeof(struct trapframe);
661 lwp0.l_md.md_tf =
662 (struct trapframe *)lwp0.l_addr->u_pcb.pcb_hw.apcb_ksp;
663 simple_lock_init(&lwp0.l_addr->u_pcb.pcb_fpcpu_slock);
664
665 /* Indicate that lwp0 has a CPU. */
666 lwp0.l_cpu = ci;
667
668 /*
669 * Look at arguments passed to us and compute boothowto.
670 */
671
672 boothowto = RB_SINGLE;
673 #ifdef KADB
674 boothowto |= RB_KDB;
675 #endif
676 for (p = bootinfo.boot_flags; p && *p != '\0'; p++) {
677 /*
678 * Note that we'd really like to differentiate case here,
679 * but the Alpha AXP Architecture Reference Manual
680 * says that we shouldn't.
681 */
682 switch (*p) {
683 case 'a': /* autoboot */
684 case 'A':
685 boothowto &= ~RB_SINGLE;
686 break;
687
688 #ifdef DEBUG
689 case 'c': /* crash dump immediately after autoconfig */
690 case 'C':
691 boothowto |= RB_DUMP;
692 break;
693 #endif
694
695 #if defined(KGDB) || defined(DDB)
696 case 'd': /* break into the kernel debugger ASAP */
697 case 'D':
698 boothowto |= RB_KDB;
699 break;
700 #endif
701
702 case 'h': /* always halt, never reboot */
703 case 'H':
704 boothowto |= RB_HALT;
705 break;
706
707 #if 0
708 case 'm': /* mini root present in memory */
709 case 'M':
710 boothowto |= RB_MINIROOT;
711 break;
712 #endif
713
714 case 'n': /* askname */
715 case 'N':
716 boothowto |= RB_ASKNAME;
717 break;
718
719 case 's': /* single-user (default, supported for sanity) */
720 case 'S':
721 boothowto |= RB_SINGLE;
722 break;
723
724 case 'q': /* quiet boot */
725 case 'Q':
726 boothowto |= AB_QUIET;
727 break;
728
729 case 'v': /* verbose boot */
730 case 'V':
731 boothowto |= AB_VERBOSE;
732 break;
733
734 case '-':
735 /*
736 * Just ignore this. It's not required, but it's
737 * common for it to be passed regardless.
738 */
739 break;
740
741 default:
742 printf("Unrecognized boot flag '%c'.\n", *p);
743 break;
744 }
745 }
746
747 /*
748 * Perform any initial kernel patches based on the running system.
749 * We may perform more later if we attach additional CPUs.
750 */
751 alpha_patch(false);
752
753 /*
754 * Figure out the number of CPUs in the box, from RPB fields.
755 * Really. We mean it.
756 */
757 for (i = 0; i < hwrpb->rpb_pcs_cnt; i++) {
758 struct pcs *pcsp;
759
760 pcsp = LOCATE_PCS(hwrpb, i);
761 if ((pcsp->pcs_flags & PCS_PP) != 0)
762 ncpus++;
763 }
764
765 /*
766 * Initialize debuggers, and break into them if appropriate.
767 */
768 #if NKSYMS || defined(DDB) || defined(MODULAR)
769 ksyms_addsyms_elf((int)((u_int64_t)ksym_end - (u_int64_t)ksym_start),
770 ksym_start, ksym_end);
771 #endif
772
773 if (boothowto & RB_KDB) {
774 #if defined(KGDB)
775 kgdb_debug_init = 1;
776 kgdb_connect(1);
777 #elif defined(DDB)
778 Debugger();
779 #endif
780 }
781
782 #ifdef DIAGNOSTIC
783 /*
784 * Check our clock frequency, from RPB fields.
785 */
786 if ((hwrpb->rpb_intr_freq >> 12) != 1024)
787 printf("WARNING: unbelievable rpb_intr_freq: %ld (%d hz)\n",
788 hwrpb->rpb_intr_freq, hz);
789 #endif
790 }
791
792 void
793 consinit(void)
794 {
795
796 /*
797 * Everything related to console initialization is done
798 * in alpha_init().
799 */
800 #if defined(DIAGNOSTIC) && defined(_PMAP_MAY_USE_PROM_CONSOLE)
801 printf("consinit: %susing prom console\n",
802 pmap_uses_prom_console() ? "" : "not ");
803 #endif
804 }
805
806 void
807 cpu_startup(void)
808 {
809 vaddr_t minaddr, maxaddr;
810 char pbuf[9];
811 #if defined(DEBUG)
812 extern int pmapdebug;
813 int opmapdebug = pmapdebug;
814
815 pmapdebug = 0;
816 #endif
817
818 /*
819 * Good {morning,afternoon,evening,night}.
820 */
821 printf("%s%s", copyright, version);
822 identifycpu();
823 format_bytes(pbuf, sizeof(pbuf), ptoa(totalphysmem));
824 printf("total memory = %s\n", pbuf);
825 format_bytes(pbuf, sizeof(pbuf), ptoa(resvmem));
826 printf("(%s reserved for PROM, ", pbuf);
827 format_bytes(pbuf, sizeof(pbuf), ptoa(physmem));
828 printf("%s used by NetBSD)\n", pbuf);
829 if (unusedmem) {
830 format_bytes(pbuf, sizeof(pbuf), ptoa(unusedmem));
831 printf("WARNING: unused memory = %s\n", pbuf);
832 }
833 if (unknownmem) {
834 format_bytes(pbuf, sizeof(pbuf), ptoa(unknownmem));
835 printf("WARNING: %s of memory with unknown purpose\n", pbuf);
836 }
837
838 minaddr = 0;
839
840 /*
841 * Allocate a submap for physio
842 */
843 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
844 VM_PHYS_SIZE, 0, false, NULL);
845
846 /*
847 * No need to allocate an mbuf cluster submap. Mbuf clusters
848 * are allocated via the pool allocator, and we use K0SEG to
849 * map those pages.
850 */
851
852 #if defined(DEBUG)
853 pmapdebug = opmapdebug;
854 #endif
855 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
856 printf("avail memory = %s\n", pbuf);
857 #if 0
858 {
859 extern u_long pmap_pages_stolen;
860
861 format_bytes(pbuf, sizeof(pbuf), pmap_pages_stolen * PAGE_SIZE);
862 printf("stolen memory for VM structures = %s\n", pbuf);
863 }
864 #endif
865
866 /*
867 * Set up the HWPCB so that it's safe to configure secondary
868 * CPUs.
869 */
870 hwrpb_primary_init();
871 }
872
873 /*
874 * Retrieve the platform name from the DSR.
875 */
876 const char *
877 alpha_dsr_sysname(void)
878 {
879 struct dsrdb *dsr;
880 const char *sysname;
881
882 /*
883 * DSR does not exist on early HWRPB versions.
884 */
885 if (hwrpb->rpb_version < HWRPB_DSRDB_MINVERS)
886 return (NULL);
887
888 dsr = (struct dsrdb *)(((char *)hwrpb) + hwrpb->rpb_dsrdb_off);
889 sysname = (const char *)((char *)dsr + (dsr->dsr_sysname_off +
890 sizeof(u_int64_t)));
891 return (sysname);
892 }
893
894 /*
895 * Lookup the system specified system variation in the provided table,
896 * returning the model string on match.
897 */
898 const char *
899 alpha_variation_name(u_int64_t variation, const struct alpha_variation_table *avtp)
900 {
901 int i;
902
903 for (i = 0; avtp[i].avt_model != NULL; i++)
904 if (avtp[i].avt_variation == variation)
905 return (avtp[i].avt_model);
906 return (NULL);
907 }
908
909 /*
910 * Generate a default platform name based for unknown system variations.
911 */
912 const char *
913 alpha_unknown_sysname(void)
914 {
915 static char s[128]; /* safe size */
916
917 sprintf(s, "%s family, unknown model variation 0x%lx",
918 platform.family, hwrpb->rpb_variation & SV_ST_MASK);
919 return ((const char *)s);
920 }
921
922 void
923 identifycpu(void)
924 {
925 char *s;
926 int i;
927
928 /*
929 * print out CPU identification information.
930 */
931 printf("%s", cpu_model);
932 for(s = cpu_model; *s; ++s)
933 if(strncasecmp(s, "MHz", 3) == 0)
934 goto skipMHz;
935 printf(", %ldMHz", hwrpb->rpb_cc_freq / 1000000);
936 skipMHz:
937 printf(", s/n ");
938 for (i = 0; i < 10; i++)
939 printf("%c", hwrpb->rpb_ssn[i]);
940 printf("\n");
941 printf("%ld byte page size, %d processor%s.\n",
942 hwrpb->rpb_page_size, ncpus, ncpus == 1 ? "" : "s");
943 #if 0
944 /* this isn't defined for any systems that we run on? */
945 printf("serial number 0x%lx 0x%lx\n",
946 ((long *)hwrpb->rpb_ssn)[0], ((long *)hwrpb->rpb_ssn)[1]);
947
948 /* and these aren't particularly useful! */
949 printf("variation: 0x%lx, revision 0x%lx\n",
950 hwrpb->rpb_variation, *(long *)hwrpb->rpb_revision);
951 #endif
952 }
953
954 int waittime = -1;
955 struct pcb dumppcb;
956
957 void
958 cpu_reboot(int howto, char *bootstr)
959 {
960 #if defined(MULTIPROCESSOR)
961 u_long cpu_id = cpu_number();
962 u_long wait_mask;
963 int i;
964 #endif
965
966 /* If "always halt" was specified as a boot flag, obey. */
967 if ((boothowto & RB_HALT) != 0)
968 howto |= RB_HALT;
969
970 boothowto = howto;
971
972 /* If system is cold, just halt. */
973 if (cold) {
974 boothowto |= RB_HALT;
975 goto haltsys;
976 }
977
978 if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
979 waittime = 0;
980 vfs_shutdown();
981 /*
982 * If we've been adjusting the clock, the todr
983 * will be out of synch; adjust it now.
984 */
985 resettodr();
986 }
987
988 /* Disable interrupts. */
989 splhigh();
990
991 #if defined(MULTIPROCESSOR)
992 /*
993 * Halt all other CPUs. If we're not the primary, the
994 * primary will spin, waiting for us to halt.
995 */
996 cpu_id = cpu_number(); /* may have changed cpu */
997 wait_mask = (1UL << cpu_id) | (1UL << hwrpb->rpb_primary_cpu_id);
998
999 alpha_broadcast_ipi(ALPHA_IPI_HALT);
1000
1001 /* Ensure any CPUs paused by DDB resume execution so they can halt */
1002 cpus_paused = 0;
1003
1004 for (i = 0; i < 10000; i++) {
1005 alpha_mb();
1006 if (cpus_running == wait_mask)
1007 break;
1008 delay(1000);
1009 }
1010 alpha_mb();
1011 if (cpus_running != wait_mask)
1012 printf("WARNING: Unable to halt secondary CPUs (0x%lx)\n",
1013 cpus_running);
1014 #endif /* MULTIPROCESSOR */
1015
1016 /* If rebooting and a dump is requested do it. */
1017 #if 0
1018 if ((boothowto & (RB_DUMP | RB_HALT)) == RB_DUMP)
1019 #else
1020 if (boothowto & RB_DUMP)
1021 #endif
1022 dumpsys();
1023
1024 haltsys:
1025
1026 /* run any shutdown hooks */
1027 doshutdownhooks();
1028
1029 pmf_system_shutdown(boothowto);
1030
1031 #ifdef BOOTKEY
1032 printf("hit any key to %s...\n", howto & RB_HALT ? "halt" : "reboot");
1033 cnpollc(1); /* for proper keyboard command handling */
1034 cngetc();
1035 cnpollc(0);
1036 printf("\n");
1037 #endif
1038
1039 /* Finally, powerdown/halt/reboot the system. */
1040 if ((boothowto & RB_POWERDOWN) == RB_POWERDOWN &&
1041 platform.powerdown != NULL) {
1042 (*platform.powerdown)();
1043 printf("WARNING: powerdown failed!\n");
1044 }
1045 printf("%s\n\n", (boothowto & RB_HALT) ? "halted." : "rebooting...");
1046 #if defined(MULTIPROCESSOR)
1047 if (cpu_id != hwrpb->rpb_primary_cpu_id)
1048 cpu_halt();
1049 else
1050 #endif
1051 prom_halt(boothowto & RB_HALT);
1052 /*NOTREACHED*/
1053 }
1054
1055 /*
1056 * These variables are needed by /sbin/savecore
1057 */
1058 u_int32_t dumpmag = 0x8fca0101; /* magic number */
1059 int dumpsize = 0; /* pages */
1060 long dumplo = 0; /* blocks */
1061
1062 /*
1063 * cpu_dumpsize: calculate size of machine-dependent kernel core dump headers.
1064 */
1065 int
1066 cpu_dumpsize(void)
1067 {
1068 int size;
1069
1070 size = ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t)) +
1071 ALIGN(mem_cluster_cnt * sizeof(phys_ram_seg_t));
1072 if (roundup(size, dbtob(1)) != dbtob(1))
1073 return -1;
1074
1075 return (1);
1076 }
1077
1078 /*
1079 * cpu_dump_mempagecnt: calculate size of RAM (in pages) to be dumped.
1080 */
1081 u_long
1082 cpu_dump_mempagecnt(void)
1083 {
1084 u_long i, n;
1085
1086 n = 0;
1087 for (i = 0; i < mem_cluster_cnt; i++)
1088 n += atop(mem_clusters[i].size);
1089 return (n);
1090 }
1091
1092 /*
1093 * cpu_dump: dump machine-dependent kernel core dump headers.
1094 */
1095 int
1096 cpu_dump(void)
1097 {
1098 int (*dump)(dev_t, daddr_t, void *, size_t);
1099 char buf[dbtob(1)];
1100 kcore_seg_t *segp;
1101 cpu_kcore_hdr_t *cpuhdrp;
1102 phys_ram_seg_t *memsegp;
1103 const struct bdevsw *bdev;
1104 int i;
1105
1106 bdev = bdevsw_lookup(dumpdev);
1107 if (bdev == NULL)
1108 return (ENXIO);
1109 dump = bdev->d_dump;
1110
1111 memset(buf, 0, sizeof buf);
1112 segp = (kcore_seg_t *)buf;
1113 cpuhdrp = (cpu_kcore_hdr_t *)&buf[ALIGN(sizeof(*segp))];
1114 memsegp = (phys_ram_seg_t *)&buf[ ALIGN(sizeof(*segp)) +
1115 ALIGN(sizeof(*cpuhdrp))];
1116
1117 /*
1118 * Generate a segment header.
1119 */
1120 CORE_SETMAGIC(*segp, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
1121 segp->c_size = dbtob(1) - ALIGN(sizeof(*segp));
1122
1123 /*
1124 * Add the machine-dependent header info.
1125 */
1126 cpuhdrp->lev1map_pa = ALPHA_K0SEG_TO_PHYS((vaddr_t)kernel_lev1map);
1127 cpuhdrp->page_size = PAGE_SIZE;
1128 cpuhdrp->nmemsegs = mem_cluster_cnt;
1129
1130 /*
1131 * Fill in the memory segment descriptors.
1132 */
1133 for (i = 0; i < mem_cluster_cnt; i++) {
1134 memsegp[i].start = mem_clusters[i].start;
1135 memsegp[i].size = mem_clusters[i].size & ~PAGE_MASK;
1136 }
1137
1138 return (dump(dumpdev, dumplo, (void *)buf, dbtob(1)));
1139 }
1140
1141 /*
1142 * This is called by main to set dumplo and dumpsize.
1143 * Dumps always skip the first PAGE_SIZE of disk space
1144 * in case there might be a disk label stored there.
1145 * If there is extra space, put dump at the end to
1146 * reduce the chance that swapping trashes it.
1147 */
1148 void
1149 cpu_dumpconf(void)
1150 {
1151 const struct bdevsw *bdev;
1152 int nblks, dumpblks; /* size of dump area */
1153
1154 if (dumpdev == NODEV)
1155 goto bad;
1156 bdev = bdevsw_lookup(dumpdev);
1157 if (bdev == NULL) {
1158 dumpdev = NODEV;
1159 goto bad;
1160 }
1161 if (bdev->d_psize == NULL)
1162 goto bad;
1163 nblks = (*bdev->d_psize)(dumpdev);
1164 if (nblks <= ctod(1))
1165 goto bad;
1166
1167 dumpblks = cpu_dumpsize();
1168 if (dumpblks < 0)
1169 goto bad;
1170 dumpblks += ctod(cpu_dump_mempagecnt());
1171
1172 /* If dump won't fit (incl. room for possible label), punt. */
1173 if (dumpblks > (nblks - ctod(1)))
1174 goto bad;
1175
1176 /* Put dump at end of partition */
1177 dumplo = nblks - dumpblks;
1178
1179 /* dumpsize is in page units, and doesn't include headers. */
1180 dumpsize = cpu_dump_mempagecnt();
1181 return;
1182
1183 bad:
1184 dumpsize = 0;
1185 return;
1186 }
1187
1188 /*
1189 * Dump the kernel's image to the swap partition.
1190 */
1191 #define BYTES_PER_DUMP PAGE_SIZE
1192
1193 void
1194 dumpsys(void)
1195 {
1196 const struct bdevsw *bdev;
1197 u_long totalbytesleft, bytes, i, n, memcl;
1198 u_long maddr;
1199 int psize;
1200 daddr_t blkno;
1201 int (*dump)(dev_t, daddr_t, void *, size_t);
1202 int error;
1203
1204 /* Save registers. */
1205 savectx(&dumppcb);
1206
1207 if (dumpdev == NODEV)
1208 return;
1209 bdev = bdevsw_lookup(dumpdev);
1210 if (bdev == NULL || bdev->d_psize == NULL)
1211 return;
1212
1213 /*
1214 * For dumps during autoconfiguration,
1215 * if dump device has already configured...
1216 */
1217 if (dumpsize == 0)
1218 cpu_dumpconf();
1219 if (dumplo <= 0) {
1220 printf("\ndump to dev %u,%u not possible\n",
1221 major(dumpdev), minor(dumpdev));
1222 return;
1223 }
1224 printf("\ndumping to dev %u,%u offset %ld\n",
1225 major(dumpdev), minor(dumpdev), dumplo);
1226
1227 psize = (*bdev->d_psize)(dumpdev);
1228 printf("dump ");
1229 if (psize == -1) {
1230 printf("area unavailable\n");
1231 return;
1232 }
1233
1234 /* XXX should purge all outstanding keystrokes. */
1235
1236 if ((error = cpu_dump()) != 0)
1237 goto err;
1238
1239 totalbytesleft = ptoa(cpu_dump_mempagecnt());
1240 blkno = dumplo + cpu_dumpsize();
1241 dump = bdev->d_dump;
1242 error = 0;
1243
1244 for (memcl = 0; memcl < mem_cluster_cnt; memcl++) {
1245 maddr = mem_clusters[memcl].start;
1246 bytes = mem_clusters[memcl].size & ~PAGE_MASK;
1247
1248 for (i = 0; i < bytes; i += n, totalbytesleft -= n) {
1249
1250 /* Print out how many MBs we to go. */
1251 if ((totalbytesleft % (1024*1024)) == 0)
1252 printf_nolog("%ld ",
1253 totalbytesleft / (1024 * 1024));
1254
1255 /* Limit size for next transfer. */
1256 n = bytes - i;
1257 if (n > BYTES_PER_DUMP)
1258 n = BYTES_PER_DUMP;
1259
1260 error = (*dump)(dumpdev, blkno,
1261 (void *)ALPHA_PHYS_TO_K0SEG(maddr), n);
1262 if (error)
1263 goto err;
1264 maddr += n;
1265 blkno += btodb(n); /* XXX? */
1266
1267 /* XXX should look for keystrokes, to cancel. */
1268 }
1269 }
1270
1271 err:
1272 switch (error) {
1273
1274 case ENXIO:
1275 printf("device bad\n");
1276 break;
1277
1278 case EFAULT:
1279 printf("device not ready\n");
1280 break;
1281
1282 case EINVAL:
1283 printf("area improper\n");
1284 break;
1285
1286 case EIO:
1287 printf("i/o error\n");
1288 break;
1289
1290 case EINTR:
1291 printf("aborted from console\n");
1292 break;
1293
1294 case 0:
1295 printf("succeeded\n");
1296 break;
1297
1298 default:
1299 printf("error %d\n", error);
1300 break;
1301 }
1302 printf("\n\n");
1303 delay(1000);
1304 }
1305
1306 void
1307 frametoreg(const struct trapframe *framep, struct reg *regp)
1308 {
1309
1310 regp->r_regs[R_V0] = framep->tf_regs[FRAME_V0];
1311 regp->r_regs[R_T0] = framep->tf_regs[FRAME_T0];
1312 regp->r_regs[R_T1] = framep->tf_regs[FRAME_T1];
1313 regp->r_regs[R_T2] = framep->tf_regs[FRAME_T2];
1314 regp->r_regs[R_T3] = framep->tf_regs[FRAME_T3];
1315 regp->r_regs[R_T4] = framep->tf_regs[FRAME_T4];
1316 regp->r_regs[R_T5] = framep->tf_regs[FRAME_T5];
1317 regp->r_regs[R_T6] = framep->tf_regs[FRAME_T6];
1318 regp->r_regs[R_T7] = framep->tf_regs[FRAME_T7];
1319 regp->r_regs[R_S0] = framep->tf_regs[FRAME_S0];
1320 regp->r_regs[R_S1] = framep->tf_regs[FRAME_S1];
1321 regp->r_regs[R_S2] = framep->tf_regs[FRAME_S2];
1322 regp->r_regs[R_S3] = framep->tf_regs[FRAME_S3];
1323 regp->r_regs[R_S4] = framep->tf_regs[FRAME_S4];
1324 regp->r_regs[R_S5] = framep->tf_regs[FRAME_S5];
1325 regp->r_regs[R_S6] = framep->tf_regs[FRAME_S6];
1326 regp->r_regs[R_A0] = framep->tf_regs[FRAME_A0];
1327 regp->r_regs[R_A1] = framep->tf_regs[FRAME_A1];
1328 regp->r_regs[R_A2] = framep->tf_regs[FRAME_A2];
1329 regp->r_regs[R_A3] = framep->tf_regs[FRAME_A3];
1330 regp->r_regs[R_A4] = framep->tf_regs[FRAME_A4];
1331 regp->r_regs[R_A5] = framep->tf_regs[FRAME_A5];
1332 regp->r_regs[R_T8] = framep->tf_regs[FRAME_T8];
1333 regp->r_regs[R_T9] = framep->tf_regs[FRAME_T9];
1334 regp->r_regs[R_T10] = framep->tf_regs[FRAME_T10];
1335 regp->r_regs[R_T11] = framep->tf_regs[FRAME_T11];
1336 regp->r_regs[R_RA] = framep->tf_regs[FRAME_RA];
1337 regp->r_regs[R_T12] = framep->tf_regs[FRAME_T12];
1338 regp->r_regs[R_AT] = framep->tf_regs[FRAME_AT];
1339 regp->r_regs[R_GP] = framep->tf_regs[FRAME_GP];
1340 /* regp->r_regs[R_SP] = framep->tf_regs[FRAME_SP]; XXX */
1341 regp->r_regs[R_ZERO] = 0;
1342 }
1343
1344 void
1345 regtoframe(const struct reg *regp, struct trapframe *framep)
1346 {
1347
1348 framep->tf_regs[FRAME_V0] = regp->r_regs[R_V0];
1349 framep->tf_regs[FRAME_T0] = regp->r_regs[R_T0];
1350 framep->tf_regs[FRAME_T1] = regp->r_regs[R_T1];
1351 framep->tf_regs[FRAME_T2] = regp->r_regs[R_T2];
1352 framep->tf_regs[FRAME_T3] = regp->r_regs[R_T3];
1353 framep->tf_regs[FRAME_T4] = regp->r_regs[R_T4];
1354 framep->tf_regs[FRAME_T5] = regp->r_regs[R_T5];
1355 framep->tf_regs[FRAME_T6] = regp->r_regs[R_T6];
1356 framep->tf_regs[FRAME_T7] = regp->r_regs[R_T7];
1357 framep->tf_regs[FRAME_S0] = regp->r_regs[R_S0];
1358 framep->tf_regs[FRAME_S1] = regp->r_regs[R_S1];
1359 framep->tf_regs[FRAME_S2] = regp->r_regs[R_S2];
1360 framep->tf_regs[FRAME_S3] = regp->r_regs[R_S3];
1361 framep->tf_regs[FRAME_S4] = regp->r_regs[R_S4];
1362 framep->tf_regs[FRAME_S5] = regp->r_regs[R_S5];
1363 framep->tf_regs[FRAME_S6] = regp->r_regs[R_S6];
1364 framep->tf_regs[FRAME_A0] = regp->r_regs[R_A0];
1365 framep->tf_regs[FRAME_A1] = regp->r_regs[R_A1];
1366 framep->tf_regs[FRAME_A2] = regp->r_regs[R_A2];
1367 framep->tf_regs[FRAME_A3] = regp->r_regs[R_A3];
1368 framep->tf_regs[FRAME_A4] = regp->r_regs[R_A4];
1369 framep->tf_regs[FRAME_A5] = regp->r_regs[R_A5];
1370 framep->tf_regs[FRAME_T8] = regp->r_regs[R_T8];
1371 framep->tf_regs[FRAME_T9] = regp->r_regs[R_T9];
1372 framep->tf_regs[FRAME_T10] = regp->r_regs[R_T10];
1373 framep->tf_regs[FRAME_T11] = regp->r_regs[R_T11];
1374 framep->tf_regs[FRAME_RA] = regp->r_regs[R_RA];
1375 framep->tf_regs[FRAME_T12] = regp->r_regs[R_T12];
1376 framep->tf_regs[FRAME_AT] = regp->r_regs[R_AT];
1377 framep->tf_regs[FRAME_GP] = regp->r_regs[R_GP];
1378 /* framep->tf_regs[FRAME_SP] = regp->r_regs[R_SP]; XXX */
1379 /* ??? = regp->r_regs[R_ZERO]; */
1380 }
1381
1382 void
1383 printregs(struct reg *regp)
1384 {
1385 int i;
1386
1387 for (i = 0; i < 32; i++)
1388 printf("R%d:\t0x%016lx%s", i, regp->r_regs[i],
1389 i & 1 ? "\n" : "\t");
1390 }
1391
1392 void
1393 regdump(struct trapframe *framep)
1394 {
1395 struct reg reg;
1396
1397 frametoreg(framep, ®);
1398 reg.r_regs[R_SP] = alpha_pal_rdusp();
1399
1400 printf("REGISTERS:\n");
1401 printregs(®);
1402 }
1403
1404
1405
1406 void *
1407 getframe(const struct lwp *l, int sig, int *onstack)
1408 {
1409 void *frame;
1410
1411 /* Do we need to jump onto the signal stack? */
1412 *onstack =
1413 (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
1414 (SIGACTION(l->l_proc, sig).sa_flags & SA_ONSTACK) != 0;
1415
1416 if (*onstack)
1417 frame = (void *)((char *)l->l_sigstk.ss_sp +
1418 l->l_sigstk.ss_size);
1419 else
1420 frame = (void *)(alpha_pal_rdusp());
1421 return (frame);
1422 }
1423
1424 void
1425 buildcontext(struct lwp *l, const void *catcher, const void *tramp, const void *fp)
1426 {
1427 struct trapframe *tf = l->l_md.md_tf;
1428
1429 tf->tf_regs[FRAME_RA] = (u_int64_t)tramp;
1430 tf->tf_regs[FRAME_PC] = (u_int64_t)catcher;
1431 tf->tf_regs[FRAME_T12] = (u_int64_t)catcher;
1432 alpha_pal_wrusp((unsigned long)fp);
1433 }
1434
1435
1436 /*
1437 * Send an interrupt to process, new style
1438 */
1439 void
1440 sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
1441 {
1442 struct lwp *l = curlwp;
1443 struct proc *p = l->l_proc;
1444 struct sigacts *ps = p->p_sigacts;
1445 int onstack, sig = ksi->ksi_signo, error;
1446 struct sigframe_siginfo *fp, frame;
1447 struct trapframe *tf;
1448 sig_t catcher = SIGACTION(p, ksi->ksi_signo).sa_handler;
1449
1450 fp = (struct sigframe_siginfo *)getframe(l,ksi->ksi_signo,&onstack);
1451 tf = l->l_md.md_tf;
1452
1453 /* Allocate space for the signal handler context. */
1454 fp--;
1455
1456 #ifdef DEBUG
1457 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
1458 printf("sendsig_siginfo(%d): sig %d ssp %p usp %p\n", p->p_pid,
1459 sig, &onstack, fp);
1460 #endif
1461
1462 /* Build stack frame for signal trampoline. */
1463
1464 frame.sf_si._info = ksi->ksi_info;
1465 frame.sf_uc.uc_flags = _UC_SIGMASK;
1466 frame.sf_uc.uc_sigmask = *mask;
1467 frame.sf_uc.uc_link = l->l_ctxlink;
1468 memset(&frame.sf_uc.uc_stack, 0, sizeof(frame.sf_uc.uc_stack));
1469 sendsig_reset(l, sig);
1470 mutex_exit(p->p_lock);
1471 cpu_getmcontext(l, &frame.sf_uc.uc_mcontext, &frame.sf_uc.uc_flags);
1472 error = copyout(&frame, fp, sizeof(frame));
1473 mutex_enter(p->p_lock);
1474
1475 if (error != 0) {
1476 /*
1477 * Process has trashed its stack; give it an illegal
1478 * instruction to halt it in its tracks.
1479 */
1480 #ifdef DEBUG
1481 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
1482 printf("sendsig_siginfo(%d): copyout failed on sig %d\n",
1483 p->p_pid, sig);
1484 #endif
1485 sigexit(l, SIGILL);
1486 /* NOTREACHED */
1487 }
1488
1489 #ifdef DEBUG
1490 if (sigdebug & SDB_FOLLOW)
1491 printf("sendsig_siginfo(%d): sig %d usp %p code %x\n",
1492 p->p_pid, sig, fp, ksi->ksi_code);
1493 #endif
1494
1495 /*
1496 * Set up the registers to directly invoke the signal handler. The
1497 * signal trampoline is then used to return from the signal. Note
1498 * the trampoline version numbers are coordinated with machine-
1499 * dependent code in libc.
1500 */
1501
1502 tf->tf_regs[FRAME_A0] = sig;
1503 tf->tf_regs[FRAME_A1] = (u_int64_t)&fp->sf_si;
1504 tf->tf_regs[FRAME_A2] = (u_int64_t)&fp->sf_uc;
1505
1506 buildcontext(l,catcher,ps->sa_sigdesc[sig].sd_tramp,fp);
1507
1508 /* Remember that we're now on the signal stack. */
1509 if (onstack)
1510 l->l_sigstk.ss_flags |= SS_ONSTACK;
1511
1512 #ifdef DEBUG
1513 if (sigdebug & SDB_FOLLOW)
1514 printf("sendsig_siginfo(%d): pc %lx, catcher %lx\n", p->p_pid,
1515 tf->tf_regs[FRAME_PC], tf->tf_regs[FRAME_A3]);
1516 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
1517 printf("sendsig_siginfo(%d): sig %d returns\n",
1518 p->p_pid, sig);
1519 #endif
1520 }
1521
1522
1523 void
1524 cpu_upcall(struct lwp *l, int type, int nevents, int ninterrupted, void *sas, void *ap, void *sp, sa_upcall_t upcall)
1525 {
1526 struct trapframe *tf;
1527
1528 tf = l->l_md.md_tf;
1529
1530 tf->tf_regs[FRAME_PC] = (u_int64_t)upcall;
1531 tf->tf_regs[FRAME_RA] = 0;
1532 tf->tf_regs[FRAME_A0] = type;
1533 tf->tf_regs[FRAME_A1] = (u_int64_t)sas;
1534 tf->tf_regs[FRAME_A2] = nevents;
1535 tf->tf_regs[FRAME_A3] = ninterrupted;
1536 tf->tf_regs[FRAME_A4] = (u_int64_t)ap;
1537 tf->tf_regs[FRAME_T12] = (u_int64_t)upcall; /* t12 is pv */
1538 alpha_pal_wrusp((unsigned long)sp);
1539 }
1540
1541 /*
1542 * machine dependent system variables.
1543 */
1544 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup")
1545 {
1546
1547 sysctl_createv(clog, 0, NULL, NULL,
1548 CTLFLAG_PERMANENT,
1549 CTLTYPE_NODE, "machdep", NULL,
1550 NULL, 0, NULL, 0,
1551 CTL_MACHDEP, CTL_EOL);
1552
1553 sysctl_createv(clog, 0, NULL, NULL,
1554 CTLFLAG_PERMANENT,
1555 CTLTYPE_STRUCT, "console_device", NULL,
1556 sysctl_consdev, 0, NULL, sizeof(dev_t),
1557 CTL_MACHDEP, CPU_CONSDEV, CTL_EOL);
1558 sysctl_createv(clog, 0, NULL, NULL,
1559 CTLFLAG_PERMANENT,
1560 CTLTYPE_STRING, "root_device", NULL,
1561 sysctl_root_device, 0, NULL, 0,
1562 CTL_MACHDEP, CPU_ROOT_DEVICE, CTL_EOL);
1563 sysctl_createv(clog, 0, NULL, NULL,
1564 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1565 CTLTYPE_INT, "unaligned_print", NULL,
1566 NULL, 0, &alpha_unaligned_print, 0,
1567 CTL_MACHDEP, CPU_UNALIGNED_PRINT, CTL_EOL);
1568 sysctl_createv(clog, 0, NULL, NULL,
1569 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1570 CTLTYPE_INT, "unaligned_fix", NULL,
1571 NULL, 0, &alpha_unaligned_fix, 0,
1572 CTL_MACHDEP, CPU_UNALIGNED_FIX, CTL_EOL);
1573 sysctl_createv(clog, 0, NULL, NULL,
1574 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1575 CTLTYPE_INT, "unaligned_sigbus", NULL,
1576 NULL, 0, &alpha_unaligned_sigbus, 0,
1577 CTL_MACHDEP, CPU_UNALIGNED_SIGBUS, CTL_EOL);
1578 sysctl_createv(clog, 0, NULL, NULL,
1579 CTLFLAG_PERMANENT,
1580 CTLTYPE_STRING, "booted_kernel", NULL,
1581 NULL, 0, bootinfo.booted_kernel, 0,
1582 CTL_MACHDEP, CPU_BOOTED_KERNEL, CTL_EOL);
1583 sysctl_createv(clog, 0, NULL, NULL,
1584 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1585 CTLTYPE_INT, "fp_sync_complete", NULL,
1586 NULL, 0, &alpha_fp_sync_complete, 0,
1587 CTL_MACHDEP, CPU_FP_SYNC_COMPLETE, CTL_EOL);
1588 }
1589
1590 /*
1591 * Set registers on exec.
1592 */
1593 void
1594 setregs(register struct lwp *l, struct exec_package *pack, u_long stack)
1595 {
1596 struct trapframe *tfp = l->l_md.md_tf;
1597 struct pcb *pcb;
1598 #ifdef DEBUG
1599 int i;
1600 #endif
1601
1602 #ifdef DEBUG
1603 /*
1604 * Crash and dump, if the user requested it.
1605 */
1606 if (boothowto & RB_DUMP)
1607 panic("crash requested by boot flags");
1608 #endif
1609
1610 #ifdef DEBUG
1611 for (i = 0; i < FRAME_SIZE; i++)
1612 tfp->tf_regs[i] = 0xbabefacedeadbeef;
1613 #else
1614 memset(tfp->tf_regs, 0, FRAME_SIZE * sizeof tfp->tf_regs[0]);
1615 #endif
1616 pcb = lwp_getpcb(l);
1617 memset(&pcb->pcb_fp, 0, sizeof(pcb->pcb_fp));
1618 alpha_pal_wrusp(stack);
1619 tfp->tf_regs[FRAME_PS] = ALPHA_PSL_USERSET;
1620 tfp->tf_regs[FRAME_PC] = pack->ep_entry & ~3;
1621
1622 tfp->tf_regs[FRAME_A0] = stack; /* a0 = sp */
1623 tfp->tf_regs[FRAME_A1] = 0; /* a1 = rtld cleanup */
1624 tfp->tf_regs[FRAME_A2] = 0; /* a2 = rtld object */
1625 tfp->tf_regs[FRAME_A3] = (u_int64_t)l->l_proc->p_psstr; /* a3 = ps_strings */
1626 tfp->tf_regs[FRAME_T12] = tfp->tf_regs[FRAME_PC]; /* a.k.a. PV */
1627
1628 l->l_md.md_flags &= ~MDP_FPUSED;
1629 if (__predict_true((l->l_md.md_flags & IEEE_INHERIT) == 0)) {
1630 l->l_md.md_flags &= ~MDP_FP_C;
1631 pcb->pcb_fp.fpr_cr = FPCR_DYN(FP_RN);
1632 }
1633 if (pcb->pcb_fpcpu != NULL)
1634 fpusave_proc(l, 0);
1635 }
1636
1637 /*
1638 * Release the FPU.
1639 */
1640 void
1641 fpusave_cpu(struct cpu_info *ci, int save)
1642 {
1643 struct lwp *l;
1644 struct pcb *pcb;
1645 #if defined(MULTIPROCESSOR)
1646 int s;
1647 #endif
1648
1649 KDASSERT(ci == curcpu());
1650
1651 #if defined(MULTIPROCESSOR)
1652 s = splhigh(); /* block IPIs for the duration */
1653 atomic_or_ulong(&ci->ci_flags, CPUF_FPUSAVE);
1654 #endif
1655
1656 l = ci->ci_fpcurlwp;
1657 if (l == NULL)
1658 goto out;
1659
1660 pcb = lwp_getpcb(l);
1661 if (save) {
1662 alpha_pal_wrfen(1);
1663 savefpstate(&pcb->pcb_fp);
1664 }
1665
1666 alpha_pal_wrfen(0);
1667
1668 FPCPU_LOCK(pcb);
1669
1670 pcb->pcb_fpcpu = NULL;
1671 ci->ci_fpcurlwp = NULL;
1672
1673 FPCPU_UNLOCK(pcb);
1674
1675 out:
1676 #if defined(MULTIPROCESSOR)
1677 atomic_and_ulong(&ci->ci_flags, ~CPUF_FPUSAVE);
1678 splx(s);
1679 #endif
1680 return;
1681 }
1682
1683 /*
1684 * Synchronize FP state for this process.
1685 */
1686 void
1687 fpusave_proc(struct lwp *l, int save)
1688 {
1689 struct cpu_info *ci = curcpu();
1690 struct cpu_info *oci;
1691 struct pcb *pcb;
1692 #if defined(MULTIPROCESSOR)
1693 u_long ipi = save ? ALPHA_IPI_SYNCH_FPU : ALPHA_IPI_DISCARD_FPU;
1694 int s, spincount;
1695 #endif
1696
1697 pcb = lwp_getpcb(l);
1698 KDASSERT(pcb != NULL);
1699
1700 #if defined(MULTIPROCESSOR)
1701 s = splhigh(); /* block IPIs for the duration */
1702 #endif
1703 FPCPU_LOCK(pcb);
1704
1705 oci = pcb->pcb_fpcpu;
1706 if (oci == NULL) {
1707 FPCPU_UNLOCK(pcb);
1708 #if defined(MULTIPROCESSOR)
1709 splx(s);
1710 #endif
1711 return;
1712 }
1713
1714 #if defined(MULTIPROCESSOR)
1715 if (oci == ci) {
1716 KASSERT(ci->ci_fpcurlwp == l);
1717 FPCPU_UNLOCK(pcb);
1718 splx(s);
1719 fpusave_cpu(ci, save);
1720 return;
1721 }
1722
1723 KASSERT(oci->ci_fpcurlwp == l);
1724 alpha_send_ipi(oci->ci_cpuid, ipi);
1725 FPCPU_UNLOCK(pcb);
1726
1727 spincount = 0;
1728 while (pcb->pcb_fpcpu != NULL) {
1729 spincount++;
1730 delay(1000); /* XXX */
1731 if (spincount > 10000)
1732 panic("fpsave ipi didn't");
1733 }
1734 #else
1735 KASSERT(ci->ci_fpcurlwp == l);
1736 FPCPU_UNLOCK(pcb);
1737 fpusave_cpu(ci, save);
1738 #endif /* MULTIPROCESSOR */
1739 }
1740
1741 /*
1742 * Wait "n" microseconds.
1743 */
1744 void
1745 delay(unsigned long n)
1746 {
1747 unsigned long pcc0, pcc1, curcycle, cycles, usec;
1748
1749 if (n == 0)
1750 return;
1751
1752 pcc0 = alpha_rpcc() & 0xffffffffUL;
1753 cycles = 0;
1754 usec = 0;
1755
1756 while (usec <= n) {
1757 /*
1758 * Get the next CPU cycle count- assumes that we cannot
1759 * have had more than one 32 bit overflow.
1760 */
1761 pcc1 = alpha_rpcc() & 0xffffffffUL;
1762 if (pcc1 < pcc0)
1763 curcycle = (pcc1 + 0x100000000UL) - pcc0;
1764 else
1765 curcycle = pcc1 - pcc0;
1766
1767 /*
1768 * We now have the number of processor cycles since we
1769 * last checked. Add the current cycle count to the
1770 * running total. If it's over cycles_per_usec, increment
1771 * the usec counter.
1772 */
1773 cycles += curcycle;
1774 while (cycles > cycles_per_usec) {
1775 usec++;
1776 cycles -= cycles_per_usec;
1777 }
1778 pcc0 = pcc1;
1779 }
1780 }
1781
1782 #ifdef EXEC_ECOFF
1783 void
1784 cpu_exec_ecoff_setregs(struct lwp *l, struct exec_package *epp, u_long stack)
1785 {
1786 struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
1787
1788 l->l_md.md_tf->tf_regs[FRAME_GP] = execp->a.gp_value;
1789 }
1790
1791 /*
1792 * cpu_exec_ecoff_hook():
1793 * cpu-dependent ECOFF format hook for execve().
1794 *
1795 * Do any machine-dependent diddling of the exec package when doing ECOFF.
1796 *
1797 */
1798 int
1799 cpu_exec_ecoff_probe(struct lwp *l, struct exec_package *epp)
1800 {
1801 struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
1802 int error;
1803
1804 if (execp->f.f_magic == ECOFF_MAGIC_NETBSD_ALPHA)
1805 error = 0;
1806 else
1807 error = ENOEXEC;
1808
1809 return (error);
1810 }
1811 #endif /* EXEC_ECOFF */
1812
1813 int
1814 alpha_pa_access(u_long pa)
1815 {
1816 int i;
1817
1818 for (i = 0; i < mem_cluster_cnt; i++) {
1819 if (pa < mem_clusters[i].start)
1820 continue;
1821 if ((pa - mem_clusters[i].start) >=
1822 (mem_clusters[i].size & ~PAGE_MASK))
1823 continue;
1824 return (mem_clusters[i].size & PAGE_MASK); /* prot */
1825 }
1826
1827 /*
1828 * Address is not a memory address. If we're secure, disallow
1829 * access. Otherwise, grant read/write.
1830 */
1831 if (kauth_authorize_machdep(kauth_cred_get(),
1832 KAUTH_MACHDEP_UNMANAGEDMEM, NULL, NULL, NULL, NULL) != 0)
1833 return (PROT_NONE);
1834 else
1835 return (PROT_READ | PROT_WRITE);
1836 }
1837
1838 /* XXX XXX BEGIN XXX XXX */
1839 paddr_t alpha_XXX_dmamap_or; /* XXX */
1840 /* XXX */
1841 paddr_t /* XXX */
1842 alpha_XXX_dmamap(v) /* XXX */
1843 vaddr_t v; /* XXX */
1844 { /* XXX */
1845 /* XXX */
1846 return (vtophys(v) | alpha_XXX_dmamap_or); /* XXX */
1847 } /* XXX */
1848 /* XXX XXX END XXX XXX */
1849
1850 char *
1851 dot_conv(unsigned long x)
1852 {
1853 int i;
1854 char *xc;
1855 static int next;
1856 static char space[2][20];
1857
1858 xc = space[next ^= 1] + sizeof space[0];
1859 *--xc = '\0';
1860 for (i = 0;; ++i) {
1861 if (i && (i & 3) == 0)
1862 *--xc = '.';
1863 *--xc = hexdigits[x & 0xf];
1864 x >>= 4;
1865 if (x == 0)
1866 break;
1867 }
1868 return xc;
1869 }
1870
1871 void
1872 cpu_getmcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flags)
1873 {
1874 struct trapframe *frame = l->l_md.md_tf;
1875 struct pcb *pcb = lwp_getpcb(l);
1876 __greg_t *gr = mcp->__gregs;
1877 __greg_t ras_pc;
1878
1879 /* Save register context. */
1880 frametoreg(frame, (struct reg *)gr);
1881 /* XXX if there's a better, general way to get the USP of
1882 * an LWP that might or might not be curlwp, I'd like to know
1883 * about it.
1884 */
1885 if (l == curlwp) {
1886 gr[_REG_SP] = alpha_pal_rdusp();
1887 gr[_REG_UNIQUE] = alpha_pal_rdunique();
1888 } else {
1889 gr[_REG_SP] = pcb->pcb_hw.apcb_usp;
1890 gr[_REG_UNIQUE] = pcb->pcb_hw.apcb_unique;
1891 }
1892 gr[_REG_PC] = frame->tf_regs[FRAME_PC];
1893 gr[_REG_PS] = frame->tf_regs[FRAME_PS];
1894
1895 if ((ras_pc = (__greg_t)ras_lookup(l->l_proc,
1896 (void *) gr[_REG_PC])) != -1)
1897 gr[_REG_PC] = ras_pc;
1898
1899 *flags |= _UC_CPU | _UC_UNIQUE;
1900
1901 /* Save floating point register context, if any, and copy it. */
1902 if (l->l_md.md_flags & MDP_FPUSED) {
1903 fpusave_proc(l, 1);
1904 (void)memcpy(&mcp->__fpregs, &pcb->pcb_fp,
1905 sizeof (mcp->__fpregs));
1906 mcp->__fpregs.__fp_fpcr = alpha_read_fp_c(l);
1907 *flags |= _UC_FPU;
1908 }
1909 }
1910
1911
1912 int
1913 cpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags)
1914 {
1915 struct trapframe *frame = l->l_md.md_tf;
1916 struct pcb *pcb = lwp_getpcb(l);
1917 const __greg_t *gr = mcp->__gregs;
1918
1919 /* Restore register context, if any. */
1920 if (flags & _UC_CPU) {
1921 /* Check for security violations first. */
1922 if ((gr[_REG_PS] & ALPHA_PSL_USERSET) != ALPHA_PSL_USERSET ||
1923 (gr[_REG_PS] & ALPHA_PSL_USERCLR) != 0)
1924 return (EINVAL);
1925
1926 regtoframe((const struct reg *)gr, l->l_md.md_tf);
1927 if (l == curlwp)
1928 alpha_pal_wrusp(gr[_REG_SP]);
1929 else
1930 pcb->pcb_hw.apcb_usp = gr[_REG_SP];
1931 frame->tf_regs[FRAME_PC] = gr[_REG_PC];
1932 frame->tf_regs[FRAME_PS] = gr[_REG_PS];
1933 }
1934 if (flags & _UC_UNIQUE) {
1935 if (l == curlwp)
1936 alpha_pal_wrunique(gr[_REG_UNIQUE]);
1937 else
1938 pcb->pcb_hw.apcb_unique = gr[_REG_UNIQUE];
1939 }
1940 /* Restore floating point register context, if any. */
1941 if (flags & _UC_FPU) {
1942 /* If we have an FP register context, get rid of it. */
1943 if (pcb->pcb_fpcpu != NULL)
1944 fpusave_proc(l, 0);
1945 (void)memcpy(&pcb->pcb_fp, &mcp->__fpregs,
1946 sizeof (pcb->pcb_fp));
1947 l->l_md.md_flags = mcp->__fpregs.__fp_fpcr & MDP_FP_C;
1948 l->l_md.md_flags |= MDP_FPUSED;
1949 }
1950
1951 return (0);
1952 }
1953
1954 /*
1955 * Preempt the current process if in interrupt from user mode,
1956 * or after the current trap/syscall if in system mode.
1957 */
1958 void
1959 cpu_need_resched(struct cpu_info *ci, int flags)
1960 {
1961 #if defined(MULTIPROCESSOR)
1962 bool immed = (flags & RESCHED_IMMED) != 0;
1963 #endif /* defined(MULTIPROCESSOR) */
1964
1965 aston(ci->ci_data.cpu_onproc);
1966 ci->ci_want_resched = 1;
1967 if (ci->ci_data.cpu_onproc != ci->ci_data.cpu_idlelwp) {
1968 #if defined(MULTIPROCESSOR)
1969 if (immed && ci != curcpu()) {
1970 alpha_send_ipi(ci->ci_cpuid, 0);
1971 }
1972 #endif /* defined(MULTIPROCESSOR) */
1973 }
1974 }
1975