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