1/*- 2 * Copyright (c) 2001 Jake Burkholder. 3 * Copyright (c) 1992 Terrence R. Lambert. 4 * Copyright (c) 1982, 1987, 1990 The Regents of the University of California. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * William Jolitz. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * from: @(#)machdep.c 7.4 (Berkeley) 6/3/91 35 * from: FreeBSD: src/sys/i386/i386/machdep.c,v 1.477 2001/08/27 36 */ 37 38#include <sys/cdefs.h> 39__FBSDID("$FreeBSD: stable/11/sys/sparc64/sparc64/machdep.c 341491 2018-12-04 19:07:10Z markj $"); 40 41#include "opt_compat.h" 42#include "opt_ddb.h" 43#include "opt_kstack_pages.h" 44 45#include <sys/param.h> 46#include <sys/malloc.h> 47#include <sys/proc.h> 48#include <sys/systm.h> 49#include <sys/bio.h> 50#include <sys/buf.h> 51#include <sys/bus.h> 52#include <sys/cpu.h> 53#include <sys/cons.h> 54#include <sys/eventhandler.h> 55#include <sys/exec.h> 56#include <sys/imgact.h> 57#include <sys/interrupt.h> 58#include <sys/kdb.h> 59#include <sys/kernel.h> 60#include <sys/ktr.h> 61#include <sys/linker.h> 62#include <sys/lock.h> 63#include <sys/msgbuf.h> 64#include <sys/mutex.h> 65#include <sys/pcpu.h> 66#include <sys/ptrace.h> 67#include <sys/reboot.h> 68#include <sys/rwlock.h> 69#include <sys/signalvar.h> 70#include <sys/smp.h> 71#include <sys/syscallsubr.h> 72#include <sys/sysent.h> 73#include <sys/sysproto.h> 74#include <sys/timetc.h> 75#include <sys/ucontext.h> 76#include <sys/vmmeter.h> 77 78#include <dev/ofw/openfirm.h> 79 80#include <vm/vm.h> 81#include <vm/vm_extern.h> 82#include <vm/vm_kern.h> 83#include <vm/vm_page.h> 84#include <vm/vm_map.h> 85#include <vm/vm_object.h> 86#include <vm/vm_pager.h> 87#include <vm/vm_param.h> 88 89#include <ddb/ddb.h> 90 91#include <machine/bus.h> 92#include <machine/cache.h> 93#include <machine/cmt.h> 94#include <machine/cpu.h> 95#include <machine/fireplane.h> 96#include <machine/fp.h> 97#include <machine/fsr.h> 98#include <machine/intr_machdep.h> 99#include <machine/jbus.h> 100#include <machine/md_var.h> 101#include <machine/metadata.h> 102#include <machine/ofw_machdep.h> 103#include <machine/ofw_mem.h> 104#include <machine/pcb.h> 105#include <machine/pmap.h> 106#include <machine/pstate.h> 107#include <machine/reg.h> 108#include <machine/sigframe.h> 109#include <machine/smp.h> 110#include <machine/tick.h> 111#include <machine/tlb.h> 112#include <machine/tstate.h> 113#include <machine/upa.h> 114#include <machine/ver.h> 115 116typedef int ofw_vec_t(void *); 117 118int dtlb_slots; 119int itlb_slots; 120struct tlb_entry *kernel_tlbs; 121int kernel_tlb_slots; 122 123int cold = 1; 124long Maxmem; 125long realmem; 126 127void *dpcpu0; 128char pcpu0[PCPU_PAGES * PAGE_SIZE]; 129struct trapframe frame0; 130 131vm_offset_t kstack0; 132vm_paddr_t kstack0_phys; 133 134struct kva_md_info kmi; 135 136u_long ofw_vec; 137u_long ofw_tba; 138u_int tba_taken_over; 139 140char sparc64_model[32]; 141 142static int cpu_use_vis = 1; 143 144cpu_block_copy_t *cpu_block_copy; 145cpu_block_zero_t *cpu_block_zero; 146 147static phandle_t find_bsp(phandle_t node, uint32_t bspid, u_int cpu_impl); 148void sparc64_init(caddr_t mdp, u_long o1, u_long o2, u_long o3, 149 ofw_vec_t *vec); 150static void sparc64_shutdown_final(void *dummy, int howto); 151 152static void cpu_startup(void *arg); 153SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL); 154 155CTASSERT((1 << INT_SHIFT) == sizeof(int)); 156CTASSERT((1 << PTR_SHIFT) == sizeof(char *)); 157 158CTASSERT(sizeof(struct reg) == 256); 159CTASSERT(sizeof(struct fpreg) == 272); 160CTASSERT(sizeof(struct __mcontext) == 512); 161 162CTASSERT((sizeof(struct pcb) & (64 - 1)) == 0); 163CTASSERT((offsetof(struct pcb, pcb_kfp) & (64 - 1)) == 0); 164CTASSERT((offsetof(struct pcb, pcb_ufp) & (64 - 1)) == 0); 165CTASSERT(sizeof(struct pcb) <= ((KSTACK_PAGES * PAGE_SIZE) / 8)); 166 167CTASSERT(sizeof(struct pcpu) <= ((PCPU_PAGES * PAGE_SIZE) / 2)); 168 169static void 170cpu_startup(void *arg) 171{ 172 vm_paddr_t physsz; 173 int i; 174 175 physsz = 0; 176 for (i = 0; i < sparc64_nmemreg; i++) 177 physsz += sparc64_memreg[i].mr_size; 178 printf("real memory = %lu (%lu MB)\n", physsz, 179 physsz / (1024 * 1024)); 180 realmem = (long)physsz / PAGE_SIZE; 181 182 vm_ksubmap_init(&kmi); 183 184 bufinit(); 185 vm_pager_bufferinit(); 186 187 EVENTHANDLER_REGISTER(shutdown_final, sparc64_shutdown_final, NULL, 188 SHUTDOWN_PRI_LAST); 189 190 printf("avail memory = %lu (%lu MB)\n", vm_cnt.v_free_count * PAGE_SIZE, 191 vm_cnt.v_free_count / ((1024 * 1024) / PAGE_SIZE)); 192 193 if (bootverbose) 194 printf("machine: %s\n", sparc64_model); 195 196 cpu_identify(rdpr(ver), PCPU_GET(clock), curcpu); 197} 198 199void 200cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size) 201{ 202 struct intr_request *ir; 203 int i; 204 205 pcpu->pc_irtail = &pcpu->pc_irhead; 206 for (i = 0; i < IR_FREE; i++) { 207 ir = &pcpu->pc_irpool[i]; 208 ir->ir_next = pcpu->pc_irfree; 209 pcpu->pc_irfree = ir; 210 } 211} 212 213void 214spinlock_enter(void) 215{ 216 struct thread *td; 217 register_t pil; 218 219 td = curthread; 220 if (td->td_md.md_spinlock_count == 0) { 221 pil = rdpr(pil); 222 wrpr(pil, 0, PIL_TICK); 223 td->td_md.md_spinlock_count = 1; 224 td->td_md.md_saved_pil = pil; 225 } else 226 td->td_md.md_spinlock_count++; 227 critical_enter(); 228} 229 230void 231spinlock_exit(void) 232{ 233 struct thread *td; 234 register_t pil; 235 236 td = curthread; 237 critical_exit(); 238 pil = td->td_md.md_saved_pil; 239 td->td_md.md_spinlock_count--; 240 if (td->td_md.md_spinlock_count == 0) 241 wrpr(pil, pil, 0); 242} 243 244static phandle_t 245find_bsp(phandle_t node, uint32_t bspid, u_int cpu_impl) 246{ 247 char type[sizeof("cpu")]; 248 phandle_t child; 249 uint32_t portid; 250 251 for (; node != 0; node = OF_peer(node)) { 252 child = OF_child(node); 253 if (child > 0) { 254 child = find_bsp(child, bspid, cpu_impl); 255 if (child > 0) 256 return (child); 257 } else { 258 if (OF_getprop(node, "device_type", type, 259 sizeof(type)) <= 0) 260 continue; 261 if (strcmp(type, "cpu") != 0) 262 continue; 263 if (OF_getprop(node, cpu_portid_prop(cpu_impl), 264 &portid, sizeof(portid)) <= 0) 265 continue; 266 if (portid == bspid) 267 return (node); 268 } 269 } 270 return (0); 271} 272 273const char * 274cpu_portid_prop(u_int cpu_impl) 275{ 276 277 switch (cpu_impl) { 278 case CPU_IMPL_SPARC64: 279 case CPU_IMPL_SPARC64V: 280 case CPU_IMPL_ULTRASPARCI: 281 case CPU_IMPL_ULTRASPARCII: 282 case CPU_IMPL_ULTRASPARCIIi: 283 case CPU_IMPL_ULTRASPARCIIe: 284 return ("upa-portid"); 285 case CPU_IMPL_ULTRASPARCIII: 286 case CPU_IMPL_ULTRASPARCIIIp: 287 case CPU_IMPL_ULTRASPARCIIIi: 288 case CPU_IMPL_ULTRASPARCIIIip: 289 return ("portid"); 290 case CPU_IMPL_ULTRASPARCIV: 291 case CPU_IMPL_ULTRASPARCIVp: 292 return ("cpuid"); 293 default: 294 return (""); 295 } 296} 297 298uint32_t 299cpu_get_mid(u_int cpu_impl) 300{ 301 302 switch (cpu_impl) { 303 case CPU_IMPL_SPARC64: 304 case CPU_IMPL_SPARC64V: 305 case CPU_IMPL_ULTRASPARCI: 306 case CPU_IMPL_ULTRASPARCII: 307 case CPU_IMPL_ULTRASPARCIIi: 308 case CPU_IMPL_ULTRASPARCIIe: 309 return (UPA_CR_GET_MID(ldxa(0, ASI_UPA_CONFIG_REG))); 310 case CPU_IMPL_ULTRASPARCIII: 311 case CPU_IMPL_ULTRASPARCIIIp: 312 return (FIREPLANE_CR_GET_AID(ldxa(AA_FIREPLANE_CONFIG, 313 ASI_FIREPLANE_CONFIG_REG))); 314 case CPU_IMPL_ULTRASPARCIIIi: 315 case CPU_IMPL_ULTRASPARCIIIip: 316 return (JBUS_CR_GET_JID(ldxa(0, ASI_JBUS_CONFIG_REG))); 317 case CPU_IMPL_ULTRASPARCIV: 318 case CPU_IMPL_ULTRASPARCIVp: 319 return (INTR_ID_GET_ID(ldxa(AA_INTR_ID, ASI_INTR_ID))); 320 default: 321 return (0); 322 } 323} 324 325void 326sparc64_init(caddr_t mdp, u_long o1, u_long o2, u_long o3, ofw_vec_t *vec) 327{ 328 char *env; 329 struct pcpu *pc; 330 vm_offset_t end; 331 vm_offset_t va; 332 caddr_t kmdp; 333 phandle_t root; 334 u_int cpu_impl; 335 336 end = 0; 337 kmdp = NULL; 338 339 /* 340 * Find out what kind of CPU we have first, for anything that changes 341 * behaviour. 342 */ 343 cpu_impl = VER_IMPL(rdpr(ver)); 344 345 /* 346 * Do CPU-specific initialization. 347 */ 348 if (cpu_impl >= CPU_IMPL_ULTRASPARCIII) 349 cheetah_init(cpu_impl); 350 else if (cpu_impl == CPU_IMPL_SPARC64V) 351 zeus_init(cpu_impl); 352 353 /* 354 * Clear (S)TICK timer (including NPT). 355 */ 356 tick_clear(cpu_impl); 357 358 /* 359 * UltraSparc II[e,i] based systems come up with the tick interrupt 360 * enabled and a handler that resets the tick counter, causing DELAY() 361 * to not work properly when used early in boot. 362 * UltraSPARC III based systems come up with the system tick interrupt 363 * enabled, causing an interrupt storm on startup since they are not 364 * handled. 365 */ 366 tick_stop(cpu_impl); 367 368 /* 369 * Set up Open Firmware entry points. 370 */ 371 ofw_tba = rdpr(tba); 372 ofw_vec = (u_long)vec; 373 374 /* 375 * Parse metadata if present and fetch parameters. Must be before the 376 * console is inited so cninit() gets the right value of boothowto. 377 */ 378 if (mdp != NULL) { 379 preload_metadata = mdp; 380 kmdp = preload_search_by_type("elf kernel"); 381 if (kmdp != NULL) { 382 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int); 383 init_static_kenv(MD_FETCH(kmdp, MODINFOMD_ENVP, char *), 384 0); 385 end = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t); 386 kernel_tlb_slots = MD_FETCH(kmdp, MODINFOMD_DTLB_SLOTS, 387 int); 388 kernel_tlbs = (void *)preload_search_info(kmdp, 389 MODINFO_METADATA | MODINFOMD_DTLB); 390 } 391 } 392 393 init_param1(); 394 395 /* 396 * Initialize Open Firmware (needed for console). 397 */ 398 OF_install(OFW_STD_DIRECT, 0); 399 OF_init(ofw_entry); 400 401 /* 402 * Prime our per-CPU data page for use. Note, we are using it for 403 * our stack, so don't pass the real size (PAGE_SIZE) to pcpu_init 404 * or it'll zero it out from under us. 405 */ 406 pc = (struct pcpu *)(pcpu0 + (PCPU_PAGES * PAGE_SIZE)) - 1; 407 pcpu_init(pc, 0, sizeof(struct pcpu)); 408 pc->pc_addr = (vm_offset_t)pcpu0; 409 pc->pc_impl = cpu_impl; 410 pc->pc_mid = cpu_get_mid(cpu_impl); 411 pc->pc_tlb_ctx = TLB_CTX_USER_MIN; 412 pc->pc_tlb_ctx_min = TLB_CTX_USER_MIN; 413 pc->pc_tlb_ctx_max = TLB_CTX_USER_MAX; 414 415 /* 416 * Determine the OFW node and frequency of the BSP (and ensure the 417 * BSP is in the device tree in the first place). 418 */ 419 root = OF_peer(0); 420 pc->pc_node = find_bsp(root, pc->pc_mid, cpu_impl); 421 if (pc->pc_node == 0) 422 OF_panic("%s: cannot find boot CPU node", __func__); 423 if (OF_getprop(pc->pc_node, "clock-frequency", &pc->pc_clock, 424 sizeof(pc->pc_clock)) <= 0) 425 OF_panic("%s: cannot determine boot CPU clock", __func__); 426 427 /* 428 * Panic if there is no metadata. Most likely the kernel was booted 429 * directly, instead of through loader(8). 430 */ 431 if (mdp == NULL || kmdp == NULL || end == 0 || 432 kernel_tlb_slots == 0 || kernel_tlbs == NULL) 433 OF_panic("%s: missing loader metadata.\nThis probably means " 434 "you are not using loader(8).", __func__); 435 436 /* 437 * Work around the broken loader behavior of not demapping no 438 * longer used kernel TLB slots when unloading the kernel or 439 * modules. 440 */ 441 for (va = KERNBASE + (kernel_tlb_slots - 1) * PAGE_SIZE_4M; 442 va >= roundup2(end, PAGE_SIZE_4M); va -= PAGE_SIZE_4M) { 443 if (bootverbose) 444 OF_printf("demapping unused kernel TLB slot " 445 "(va %#lx - %#lx)\n", va, va + PAGE_SIZE_4M - 1); 446 stxa(TLB_DEMAP_VA(va) | TLB_DEMAP_PRIMARY | TLB_DEMAP_PAGE, 447 ASI_DMMU_DEMAP, 0); 448 stxa(TLB_DEMAP_VA(va) | TLB_DEMAP_PRIMARY | TLB_DEMAP_PAGE, 449 ASI_IMMU_DEMAP, 0); 450 flush(KERNBASE); 451 kernel_tlb_slots--; 452 } 453 454 /* 455 * Determine the TLB slot maxima, which are expected to be 456 * equal across all CPUs. 457 * NB: for cheetah-class CPUs, these properties only refer 458 * to the t16s. 459 */ 460 if (OF_getprop(pc->pc_node, "#dtlb-entries", &dtlb_slots, 461 sizeof(dtlb_slots)) == -1) 462 OF_panic("%s: cannot determine number of dTLB slots", 463 __func__); 464 if (OF_getprop(pc->pc_node, "#itlb-entries", &itlb_slots, 465 sizeof(itlb_slots)) == -1) 466 OF_panic("%s: cannot determine number of iTLB slots", 467 __func__); 468 469 /* 470 * Initialize and enable the caches. Note that this may include 471 * applying workarounds. 472 */ 473 cache_init(pc); 474 cache_enable(cpu_impl); 475 uma_set_align(pc->pc_cache.dc_linesize - 1); 476 477 cpu_block_copy = bcopy; 478 cpu_block_zero = bzero; 479 getenv_int("machdep.use_vis", &cpu_use_vis); 480 if (cpu_use_vis) { 481 switch (cpu_impl) { 482 case CPU_IMPL_SPARC64: 483 case CPU_IMPL_ULTRASPARCI: 484 case CPU_IMPL_ULTRASPARCII: 485 case CPU_IMPL_ULTRASPARCIIi: 486 case CPU_IMPL_ULTRASPARCIIe: 487 case CPU_IMPL_ULTRASPARCIII: /* NB: we've disabled P$. */ 488 case CPU_IMPL_ULTRASPARCIIIp: 489 case CPU_IMPL_ULTRASPARCIIIi: 490 case CPU_IMPL_ULTRASPARCIV: 491 case CPU_IMPL_ULTRASPARCIVp: 492 case CPU_IMPL_ULTRASPARCIIIip: 493 cpu_block_copy = spitfire_block_copy; 494 cpu_block_zero = spitfire_block_zero; 495 break; 496 case CPU_IMPL_SPARC64V: 497 cpu_block_copy = zeus_block_copy; 498 cpu_block_zero = zeus_block_zero; 499 break; 500 } 501 } 502 503#ifdef SMP 504 mp_init(); 505#endif 506 507 /* 508 * Initialize virtual memory and calculate physmem. 509 */ 510 pmap_bootstrap(cpu_impl); 511 512 /* 513 * Initialize tunables. 514 */ 515 init_param2(physmem); 516 env = kern_getenv("kernelname"); 517 if (env != NULL) { 518 strlcpy(kernelname, env, sizeof(kernelname)); 519 freeenv(env); 520 } 521 522 /* 523 * Initialize the interrupt tables. 524 */ 525 intr_init1(); 526 527 /* 528 * Initialize proc0, set kstack0, frame0, curthread and curpcb. 529 */ 530 proc_linkup0(&proc0, &thread0); 531 proc0.p_md.md_sigtramp = NULL; 532 proc0.p_md.md_utrap = NULL; 533 thread0.td_kstack = kstack0; 534 thread0.td_kstack_pages = KSTACK_PAGES; 535 thread0.td_pcb = (struct pcb *) 536 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1; 537 frame0.tf_tstate = TSTATE_IE | TSTATE_PEF | TSTATE_PRIV; 538 thread0.td_frame = &frame0; 539 pc->pc_curthread = &thread0; 540 pc->pc_curpcb = thread0.td_pcb; 541 542 /* 543 * Initialize global registers. 544 */ 545 cpu_setregs(pc); 546 547 /* 548 * Take over the trap table via the PROM. Using the PROM for this 549 * is necessary in order to set obp-control-relinquished to true 550 * within the PROM so obtaining /virtual-memory/translations doesn't 551 * trigger a fatal reset error or worse things further down the road. 552 * XXX it should be possible to use this solely instead of writing 553 * %tba in cpu_setregs(). Doing so causes a hang however. 554 * 555 * NB: the low-level console drivers require a working DELAY() and 556 * some compiler optimizations may cause the curthread accesses of 557 * mutex(9) to be factored out even if the latter aren't actually 558 * called. Both of these require PCPU_REG to be set. However, we 559 * can't set PCPU_REG without also taking over the trap table or the 560 * firmware will overwrite it. 561 */ 562 sun4u_set_traptable(tl0_base); 563 564 /* 565 * Initialize the dynamic per-CPU area for the BSP and the message 566 * buffer (after setting the trap table). 567 */ 568 dpcpu_init(dpcpu0, 0); 569 msgbufinit(msgbufp, msgbufsize); 570 571 /* 572 * Initialize mutexes. 573 */ 574 mutex_init(); 575 576 /* 577 * Initialize console now that we have a reasonable set of system 578 * services. 579 */ 580 cninit(); 581 582 /* 583 * Finish the interrupt initialization now that mutexes work and 584 * enable them. 585 */ 586 intr_init2(); 587 wrpr(pil, 0, 0); 588 wrpr(pstate, 0, PSTATE_KERNEL); 589 590 OF_getprop(root, "name", sparc64_model, sizeof(sparc64_model) - 1); 591 592 kdb_init(); 593 594#ifdef KDB 595 if (boothowto & RB_KDB) 596 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger"); 597#endif 598} 599 600void 601sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 602{ 603 struct trapframe *tf; 604 struct sigframe *sfp; 605 struct sigacts *psp; 606 struct sigframe sf; 607 struct thread *td; 608 struct frame *fp; 609 struct proc *p; 610 u_long sp; 611 int oonstack; 612 int sig; 613 614 oonstack = 0; 615 td = curthread; 616 p = td->td_proc; 617 PROC_LOCK_ASSERT(p, MA_OWNED); 618 sig = ksi->ksi_signo; 619 psp = p->p_sigacts; 620 mtx_assert(&psp->ps_mtx, MA_OWNED); 621 tf = td->td_frame; 622 sp = tf->tf_sp + SPOFF; 623 oonstack = sigonstack(sp); 624 625 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm, 626 catcher, sig); 627 628 /* Make sure we have a signal trampoline to return to. */ 629 if (p->p_md.md_sigtramp == NULL) { 630 /* 631 * No signal trampoline... kill the process. 632 */ 633 CTR0(KTR_SIG, "sendsig: no sigtramp"); 634 printf("sendsig: %s is too old, rebuild it\n", p->p_comm); 635 sigexit(td, sig); 636 /* NOTREACHED */ 637 } 638 639 /* Save user context. */ 640 bzero(&sf, sizeof(sf)); 641 get_mcontext(td, &sf.sf_uc.uc_mcontext, 0); 642 sf.sf_uc.uc_sigmask = *mask; 643 sf.sf_uc.uc_stack = td->td_sigstk; 644 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) ? 645 ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 646 647 /* Allocate and validate space for the signal handler context. */ 648 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack && 649 SIGISMEMBER(psp->ps_sigonstack, sig)) { 650 sfp = (struct sigframe *)((uintptr_t)td->td_sigstk.ss_sp + 651 td->td_sigstk.ss_size - sizeof(struct sigframe)); 652 } else 653 sfp = (struct sigframe *)sp - 1; 654 mtx_unlock(&psp->ps_mtx); 655 PROC_UNLOCK(p); 656 657 fp = (struct frame *)sfp - 1; 658 659 /* Build the argument list for the signal handler. */ 660 tf->tf_out[0] = sig; 661 tf->tf_out[2] = (register_t)&sfp->sf_uc; 662 tf->tf_out[4] = (register_t)catcher; 663 if (SIGISMEMBER(psp->ps_siginfo, sig)) { 664 /* Signal handler installed with SA_SIGINFO. */ 665 tf->tf_out[1] = (register_t)&sfp->sf_si; 666 667 /* Fill in POSIX parts. */ 668 sf.sf_si = ksi->ksi_info; 669 sf.sf_si.si_signo = sig; /* maybe a translated signal */ 670 } else { 671 /* Old FreeBSD-style arguments. */ 672 tf->tf_out[1] = ksi->ksi_code; 673 tf->tf_out[3] = (register_t)ksi->ksi_addr; 674 } 675 676 /* Copy the sigframe out to the user's stack. */ 677 if (rwindow_save(td) != 0 || copyout(&sf, sfp, sizeof(*sfp)) != 0 || 678 suword(&fp->fr_in[6], tf->tf_out[6]) != 0) { 679 /* 680 * Something is wrong with the stack pointer. 681 * ...Kill the process. 682 */ 683 CTR2(KTR_SIG, "sendsig: sigexit td=%p sfp=%p", td, sfp); 684 PROC_LOCK(p); 685 sigexit(td, SIGILL); 686 /* NOTREACHED */ 687 } 688 689 tf->tf_tpc = (u_long)p->p_md.md_sigtramp; 690 tf->tf_tnpc = tf->tf_tpc + 4; 691 tf->tf_sp = (u_long)fp - SPOFF; 692 693 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#lx sp=%#lx", td, tf->tf_tpc, 694 tf->tf_sp); 695 696 PROC_LOCK(p); 697 mtx_lock(&psp->ps_mtx); 698} 699 700#ifndef _SYS_SYSPROTO_H_ 701struct sigreturn_args { 702 ucontext_t *ucp; 703}; 704#endif 705 706/* 707 * MPSAFE 708 */ 709int 710sys_sigreturn(struct thread *td, struct sigreturn_args *uap) 711{ 712 struct proc *p; 713 mcontext_t *mc; 714 ucontext_t uc; 715 int error; 716 717 p = td->td_proc; 718 if (rwindow_save(td)) { 719 PROC_LOCK(p); 720 sigexit(td, SIGILL); 721 } 722 723 CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp); 724 if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) { 725 CTR1(KTR_SIG, "sigreturn: efault td=%p", td); 726 return (EFAULT); 727 } 728 729 mc = &uc.uc_mcontext; 730 error = set_mcontext(td, mc); 731 if (error != 0) 732 return (error); 733 734 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0); 735 736 CTR4(KTR_SIG, "sigreturn: return td=%p pc=%#lx sp=%#lx tstate=%#lx", 737 td, mc->_mc_tpc, mc->_mc_sp, mc->_mc_tstate); 738 return (EJUSTRETURN); 739} 740 741/* 742 * Construct a PCB from a trapframe. This is called from kdb_trap() where 743 * we want to start a backtrace from the function that caused us to enter 744 * the debugger. We have the context in the trapframe, but base the trace 745 * on the PCB. The PCB doesn't have to be perfect, as long as it contains 746 * enough for a backtrace. 747 */ 748void 749makectx(struct trapframe *tf, struct pcb *pcb) 750{ 751 752 pcb->pcb_pc = tf->tf_tpc; 753 pcb->pcb_sp = tf->tf_sp; 754} 755 756int 757get_mcontext(struct thread *td, mcontext_t *mc, int flags) 758{ 759 struct trapframe *tf; 760 struct pcb *pcb; 761 762 tf = td->td_frame; 763 pcb = td->td_pcb; 764 /* 765 * Copy the registers which will be restored by tl0_ret() from the 766 * trapframe. 767 * Note that we skip %g7 which is used as the userland TLS register 768 * and %wstate. 769 */ 770 mc->_mc_flags = _MC_VERSION; 771 mc->mc_global[1] = tf->tf_global[1]; 772 mc->mc_global[2] = tf->tf_global[2]; 773 mc->mc_global[3] = tf->tf_global[3]; 774 mc->mc_global[4] = tf->tf_global[4]; 775 mc->mc_global[5] = tf->tf_global[5]; 776 mc->mc_global[6] = tf->tf_global[6]; 777 if (flags & GET_MC_CLEAR_RET) { 778 mc->mc_out[0] = 0; 779 mc->mc_out[1] = 0; 780 } else { 781 mc->mc_out[0] = tf->tf_out[0]; 782 mc->mc_out[1] = tf->tf_out[1]; 783 } 784 mc->mc_out[2] = tf->tf_out[2]; 785 mc->mc_out[3] = tf->tf_out[3]; 786 mc->mc_out[4] = tf->tf_out[4]; 787 mc->mc_out[5] = tf->tf_out[5]; 788 mc->mc_out[6] = tf->tf_out[6]; 789 mc->mc_out[7] = tf->tf_out[7]; 790 mc->_mc_fprs = tf->tf_fprs; 791 mc->_mc_fsr = tf->tf_fsr; 792 mc->_mc_gsr = tf->tf_gsr; 793 mc->_mc_tnpc = tf->tf_tnpc; 794 mc->_mc_tpc = tf->tf_tpc; 795 mc->_mc_tstate = tf->tf_tstate; 796 mc->_mc_y = tf->tf_y; 797 critical_enter(); 798 if ((tf->tf_fprs & FPRS_FEF) != 0) { 799 savefpctx(pcb->pcb_ufp); 800 tf->tf_fprs &= ~FPRS_FEF; 801 pcb->pcb_flags |= PCB_FEF; 802 } 803 if ((pcb->pcb_flags & PCB_FEF) != 0) { 804 bcopy(pcb->pcb_ufp, mc->mc_fp, sizeof(mc->mc_fp)); 805 mc->_mc_fprs |= FPRS_FEF; 806 } 807 critical_exit(); 808 return (0); 809} 810 811int 812set_mcontext(struct thread *td, mcontext_t *mc) 813{ 814 struct trapframe *tf; 815 struct pcb *pcb; 816 817 if (!TSTATE_SECURE(mc->_mc_tstate) || 818 (mc->_mc_flags & ((1L << _MC_VERSION_BITS) - 1)) != _MC_VERSION) 819 return (EINVAL); 820 tf = td->td_frame; 821 pcb = td->td_pcb; 822 /* Make sure the windows are spilled first. */ 823 flushw(); 824 /* 825 * Copy the registers which will be restored by tl0_ret() to the 826 * trapframe. 827 * Note that we skip %g7 which is used as the userland TLS register 828 * and %wstate. 829 */ 830 tf->tf_global[1] = mc->mc_global[1]; 831 tf->tf_global[2] = mc->mc_global[2]; 832 tf->tf_global[3] = mc->mc_global[3]; 833 tf->tf_global[4] = mc->mc_global[4]; 834 tf->tf_global[5] = mc->mc_global[5]; 835 tf->tf_global[6] = mc->mc_global[6]; 836 tf->tf_out[0] = mc->mc_out[0]; 837 tf->tf_out[1] = mc->mc_out[1]; 838 tf->tf_out[2] = mc->mc_out[2]; 839 tf->tf_out[3] = mc->mc_out[3]; 840 tf->tf_out[4] = mc->mc_out[4]; 841 tf->tf_out[5] = mc->mc_out[5]; 842 tf->tf_out[6] = mc->mc_out[6]; 843 tf->tf_out[7] = mc->mc_out[7]; 844 tf->tf_fprs = mc->_mc_fprs; 845 tf->tf_fsr = mc->_mc_fsr; 846 tf->tf_gsr = mc->_mc_gsr; 847 tf->tf_tnpc = mc->_mc_tnpc; 848 tf->tf_tpc = mc->_mc_tpc; 849 tf->tf_tstate = mc->_mc_tstate; 850 tf->tf_y = mc->_mc_y; 851 if ((mc->_mc_fprs & FPRS_FEF) != 0) { 852 tf->tf_fprs = 0; 853 bcopy(mc->mc_fp, pcb->pcb_ufp, sizeof(pcb->pcb_ufp)); 854 pcb->pcb_flags |= PCB_FEF; 855 } 856 return (0); 857} 858 859/* 860 * Exit the kernel and execute a firmware call that will not return, as 861 * specified by the arguments. 862 */ 863void 864cpu_shutdown(void *args) 865{ 866 867#ifdef SMP 868 cpu_mp_shutdown(); 869#endif 870 ofw_exit(args); 871} 872 873/* 874 * Flush the D-cache for non-DMA I/O so that the I-cache can 875 * be made coherent later. 876 */ 877void 878cpu_flush_dcache(void *ptr, size_t len) 879{ 880 881 /* TBD */ 882} 883 884/* Get current clock frequency for the given CPU ID. */ 885int 886cpu_est_clockrate(int cpu_id, uint64_t *rate) 887{ 888 struct pcpu *pc; 889 890 pc = pcpu_find(cpu_id); 891 if (pc == NULL || rate == NULL) 892 return (EINVAL); 893 *rate = pc->pc_clock; 894 return (0); 895} 896 897/* 898 * Duplicate OF_exit() with a different firmware call function that restores 899 * the trap table, otherwise a RED state exception is triggered in at least 900 * some firmware versions. 901 */ 902void 903cpu_halt(void) 904{ 905 static struct { 906 cell_t name; 907 cell_t nargs; 908 cell_t nreturns; 909 } args = { 910 (cell_t)"exit", 911 0, 912 0 913 }; 914 915 cpu_shutdown(&args); 916} 917 918static void 919sparc64_shutdown_final(void *dummy, int howto) 920{ 921 static struct { 922 cell_t name; 923 cell_t nargs; 924 cell_t nreturns; 925 } args = { 926 (cell_t)"SUNW,power-off", 927 0, 928 0 929 }; 930 931 /* Turn the power off? */ 932 if ((howto & RB_POWEROFF) != 0) 933 cpu_shutdown(&args); 934 /* In case of halt, return to the firmware. */ 935 if ((howto & RB_HALT) != 0) 936 cpu_halt(); 937} 938 939void 940cpu_idle(int busy) 941{ 942 943 /* Insert code to halt (until next interrupt) for the idle loop. */ 944} 945 946int 947cpu_idle_wakeup(int cpu) 948{ 949 950 return (1); 951} 952 953int 954ptrace_set_pc(struct thread *td, u_long addr) 955{ 956 957 td->td_frame->tf_tpc = addr; 958 td->td_frame->tf_tnpc = addr + 4; 959 return (0); 960} 961 962int 963ptrace_single_step(struct thread *td) 964{ 965 966 /* TODO; */ 967 return (0); 968} 969 970int 971ptrace_clear_single_step(struct thread *td) 972{ 973 974 /* TODO; */ 975 return (0); 976} 977 978void 979exec_setregs(struct thread *td, struct image_params *imgp, u_long stack) 980{ 981 struct trapframe *tf; 982 struct pcb *pcb; 983 struct proc *p; 984 u_long sp; 985 986 /* XXX no cpu_exec */ 987 p = td->td_proc; 988 p->p_md.md_sigtramp = NULL; 989 if (p->p_md.md_utrap != NULL) { 990 utrap_free(p->p_md.md_utrap); 991 p->p_md.md_utrap = NULL; 992 } 993 994 pcb = td->td_pcb; 995 tf = td->td_frame; 996 sp = rounddown(stack, 16); 997 bzero(pcb, sizeof(*pcb)); 998 bzero(tf, sizeof(*tf)); 999 tf->tf_out[0] = stack; 1000 tf->tf_out[3] = p->p_sysent->sv_psstrings; 1001 tf->tf_out[6] = sp - SPOFF - sizeof(struct frame); 1002 tf->tf_tnpc = imgp->entry_addr + 4; 1003 tf->tf_tpc = imgp->entry_addr; 1004 /* 1005 * While we could adhere to the memory model indicated in the ELF 1006 * header, it turns out that just always using TSO performs best. 1007 */ 1008 tf->tf_tstate = TSTATE_IE | TSTATE_PEF | TSTATE_MM_TSO; 1009 1010 td->td_retval[0] = tf->tf_out[0]; 1011 td->td_retval[1] = tf->tf_out[1]; 1012} 1013 1014int 1015fill_regs(struct thread *td, struct reg *regs) 1016{ 1017 1018 bcopy(td->td_frame, regs, sizeof(*regs)); 1019 return (0); 1020} 1021 1022int 1023set_regs(struct thread *td, struct reg *regs) 1024{ 1025 struct trapframe *tf; 1026 1027 if (!TSTATE_SECURE(regs->r_tstate)) 1028 return (EINVAL); 1029 tf = td->td_frame; 1030 regs->r_wstate = tf->tf_wstate; 1031 bcopy(regs, tf, sizeof(*regs)); 1032 return (0); 1033} 1034 1035int 1036fill_dbregs(struct thread *td, struct dbreg *dbregs) 1037{ 1038 1039 return (ENOSYS); 1040} 1041 1042int 1043set_dbregs(struct thread *td, struct dbreg *dbregs) 1044{ 1045 1046 return (ENOSYS); 1047} 1048 1049int 1050fill_fpregs(struct thread *td, struct fpreg *fpregs) 1051{ 1052 struct trapframe *tf; 1053 struct pcb *pcb; 1054 1055 pcb = td->td_pcb; 1056 tf = td->td_frame; 1057 bcopy(pcb->pcb_ufp, fpregs->fr_regs, sizeof(fpregs->fr_regs)); 1058 fpregs->fr_fsr = tf->tf_fsr; 1059 fpregs->fr_gsr = tf->tf_gsr; 1060 fpregs->fr_pad[0] = 0; 1061 return (0); 1062} 1063 1064int 1065set_fpregs(struct thread *td, struct fpreg *fpregs) 1066{ 1067 struct trapframe *tf; 1068 struct pcb *pcb; 1069 1070 pcb = td->td_pcb; 1071 tf = td->td_frame; 1072 tf->tf_fprs &= ~FPRS_FEF; 1073 bcopy(fpregs->fr_regs, pcb->pcb_ufp, sizeof(pcb->pcb_ufp)); 1074 tf->tf_fsr = fpregs->fr_fsr; 1075 tf->tf_gsr = fpregs->fr_gsr; 1076 return (0); 1077} 1078 1079struct md_utrap * 1080utrap_alloc(void) 1081{ 1082 struct md_utrap *ut; 1083 1084 ut = malloc(sizeof(struct md_utrap), M_SUBPROC, M_WAITOK | M_ZERO); 1085 ut->ut_refcnt = 1; 1086 return (ut); 1087} 1088 1089void 1090utrap_free(struct md_utrap *ut) 1091{ 1092 int refcnt; 1093 1094 if (ut == NULL) 1095 return; 1096 mtx_pool_lock(mtxpool_sleep, ut); 1097 ut->ut_refcnt--; 1098 refcnt = ut->ut_refcnt; 1099 mtx_pool_unlock(mtxpool_sleep, ut); 1100 if (refcnt == 0) 1101 free(ut, M_SUBPROC); 1102} 1103 1104struct md_utrap * 1105utrap_hold(struct md_utrap *ut) 1106{ 1107 1108 if (ut == NULL) 1109 return (NULL); 1110 mtx_pool_lock(mtxpool_sleep, ut); 1111 ut->ut_refcnt++; 1112 mtx_pool_unlock(mtxpool_sleep, ut); 1113 return (ut); 1114} 1115