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