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