10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51829Ssudheer * Common Development and Distribution License (the "License"). 61829Ssudheer * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 220Sstevel@tonic-gate /* All Rights Reserved */ 230Sstevel@tonic-gate 240Sstevel@tonic-gate 250Sstevel@tonic-gate /* 261829Ssudheer * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 270Sstevel@tonic-gate * Use is subject to license terms. 280Sstevel@tonic-gate */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 310Sstevel@tonic-gate 320Sstevel@tonic-gate #include <sys/param.h> 330Sstevel@tonic-gate #include <sys/types.h> 340Sstevel@tonic-gate #include <sys/vmparam.h> 350Sstevel@tonic-gate #include <sys/systm.h> 360Sstevel@tonic-gate #include <sys/signal.h> 370Sstevel@tonic-gate #include <sys/stack.h> 380Sstevel@tonic-gate #include <sys/regset.h> 390Sstevel@tonic-gate #include <sys/privregs.h> 400Sstevel@tonic-gate #include <sys/frame.h> 410Sstevel@tonic-gate #include <sys/proc.h> 420Sstevel@tonic-gate #include <sys/psw.h> 430Sstevel@tonic-gate #include <sys/ucontext.h> 440Sstevel@tonic-gate #include <sys/asm_linkage.h> 450Sstevel@tonic-gate #include <sys/errno.h> 460Sstevel@tonic-gate #include <sys/archsystm.h> 470Sstevel@tonic-gate #include <sys/schedctl.h> 480Sstevel@tonic-gate #include <sys/debug.h> 490Sstevel@tonic-gate #include <sys/sysmacros.h> 500Sstevel@tonic-gate 510Sstevel@tonic-gate /* 520Sstevel@tonic-gate * Save user context. 530Sstevel@tonic-gate */ 540Sstevel@tonic-gate void 550Sstevel@tonic-gate savecontext(ucontext_t *ucp, k_sigset_t mask) 560Sstevel@tonic-gate { 570Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 580Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); 591829Ssudheer struct regs *rp = lwptoregs(lwp); 600Sstevel@tonic-gate 610Sstevel@tonic-gate /* 620Sstevel@tonic-gate * We unconditionally assign to every field through the end 630Sstevel@tonic-gate * of the gregs, but we need to bzero() everything -after- that 640Sstevel@tonic-gate * to avoid having any kernel stack garbage escape to userland. 650Sstevel@tonic-gate */ 660Sstevel@tonic-gate bzero(&ucp->uc_mcontext.fpregs, sizeof (ucontext_t) - 670Sstevel@tonic-gate offsetof(ucontext_t, uc_mcontext.fpregs)); 680Sstevel@tonic-gate 690Sstevel@tonic-gate ucp->uc_flags = UC_ALL; 700Sstevel@tonic-gate ucp->uc_link = (struct ucontext *)lwp->lwp_oldcontext; 710Sstevel@tonic-gate 720Sstevel@tonic-gate /* 730Sstevel@tonic-gate * Try to copyin() the ustack if one is registered. If the stack 740Sstevel@tonic-gate * has zero size, this indicates that stack bounds checking has 750Sstevel@tonic-gate * been disabled for this LWP. If stack bounds checking is disabled 760Sstevel@tonic-gate * or the copyin() fails, we fall back to the legacy behavior. 770Sstevel@tonic-gate */ 780Sstevel@tonic-gate if (lwp->lwp_ustack == NULL || 790Sstevel@tonic-gate copyin((void *)lwp->lwp_ustack, &ucp->uc_stack, 800Sstevel@tonic-gate sizeof (ucp->uc_stack)) != 0 || 810Sstevel@tonic-gate ucp->uc_stack.ss_size == 0) { 820Sstevel@tonic-gate 830Sstevel@tonic-gate if (lwp->lwp_sigaltstack.ss_flags == SS_ONSTACK) { 840Sstevel@tonic-gate ucp->uc_stack = lwp->lwp_sigaltstack; 850Sstevel@tonic-gate } else { 860Sstevel@tonic-gate ucp->uc_stack.ss_sp = p->p_usrstack - p->p_stksize; 870Sstevel@tonic-gate ucp->uc_stack.ss_size = p->p_stksize; 880Sstevel@tonic-gate ucp->uc_stack.ss_flags = 0; 890Sstevel@tonic-gate } 900Sstevel@tonic-gate } 910Sstevel@tonic-gate 920Sstevel@tonic-gate getgregs(lwp, ucp->uc_mcontext.gregs); 930Sstevel@tonic-gate if (lwp->lwp_pcb.pcb_fpu.fpu_flags & FPU_EN) 940Sstevel@tonic-gate getfpregs(lwp, &ucp->uc_mcontext.fpregs); 950Sstevel@tonic-gate else 960Sstevel@tonic-gate ucp->uc_flags &= ~UC_FPU; 970Sstevel@tonic-gate 980Sstevel@tonic-gate sigktou(&mask, &ucp->uc_sigmask); 991829Ssudheer /* 1001829Ssudheer * If the trace flag is set, arrange for single-stepping and 1011829Ssudheer * turn off the trace flag. 1021829Ssudheer */ 1031829Ssudheer if (rp->r_ps & PS_T) { 1041829Ssudheer lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING; 1051829Ssudheer rp->r_ps &= ~PS_T; 106*2086Ssudheer /* 107*2086Ssudheer * We always check DEBUG_PENDING before checking for any 108*2086Ssudheer * pending signal. This at times can potentially lead to 109*2086Ssudheer * DEBUG_PENDING not being honoured. (for eg: the lwp is 110*2086Ssudheer * stopped by stop_on_fault() called from trap(), after 111*2086Ssudheer * being awakened it might see a pending signal and call 112*2086Ssudheer * savecontext(), however on the way back to userland there 113*2086Ssudheer * is no place it can be detected). Hence in anticipation of 114*2086Ssudheer * such occassion, we set AST flag for the thread which will 115*2086Ssudheer * make the thread take an excursion through trap() where 116*2086Ssudheer * we will handle it appropriately. 117*2086Ssudheer */ 118*2086Ssudheer aston(curthread); 1191829Ssudheer } 1200Sstevel@tonic-gate } 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate /* 1230Sstevel@tonic-gate * Restore user context. 1240Sstevel@tonic-gate */ 1250Sstevel@tonic-gate void 1260Sstevel@tonic-gate restorecontext(ucontext_t *ucp) 1270Sstevel@tonic-gate { 1280Sstevel@tonic-gate kthread_t *t = curthread; 1290Sstevel@tonic-gate klwp_t *lwp = ttolwp(t); 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate lwp->lwp_oldcontext = (uintptr_t)ucp->uc_link; 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate if (ucp->uc_flags & UC_STACK) { 1340Sstevel@tonic-gate if (ucp->uc_stack.ss_flags == SS_ONSTACK) 1350Sstevel@tonic-gate lwp->lwp_sigaltstack = ucp->uc_stack; 1360Sstevel@tonic-gate else 1370Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_flags &= ~SS_ONSTACK; 1380Sstevel@tonic-gate } 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate if (ucp->uc_flags & UC_CPU) { 1411829Ssudheer /* 1421829Ssudheer * If the trace flag is set, mark the lwp to take a 1431829Ssudheer * single-step trap on return to user level (below). 1441829Ssudheer * The x86 lcall interface and sysenter has already done this, 1451829Ssudheer * and turned off the flag, but amd64 syscall interface has not. 1461829Ssudheer */ 1471829Ssudheer if (lwptoregs(lwp)->r_ps & PS_T) 1481829Ssudheer lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING; 1490Sstevel@tonic-gate setgregs(lwp, ucp->uc_mcontext.gregs); 1500Sstevel@tonic-gate lwp->lwp_eosys = JUSTRETURN; 1510Sstevel@tonic-gate t->t_post_sys = 1; 152*2086Ssudheer aston(curthread); 1530Sstevel@tonic-gate } 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate if (ucp->uc_flags & UC_FPU) 1560Sstevel@tonic-gate setfpregs(lwp, &ucp->uc_mcontext.fpregs); 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate if (ucp->uc_flags & UC_SIGMASK) { 1590Sstevel@tonic-gate proc_t *p = ttoproc(t); 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate mutex_enter(&p->p_lock); 1620Sstevel@tonic-gate schedctl_finish_sigblock(t); 1630Sstevel@tonic-gate sigutok(&ucp->uc_sigmask, &t->t_hold); 1640Sstevel@tonic-gate if (sigcheck(p, t)) 1650Sstevel@tonic-gate t->t_sig_check = 1; 1660Sstevel@tonic-gate mutex_exit(&p->p_lock); 1670Sstevel@tonic-gate } 1680Sstevel@tonic-gate } 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate int 1720Sstevel@tonic-gate getsetcontext(int flag, void *arg) 1730Sstevel@tonic-gate { 1740Sstevel@tonic-gate ucontext_t uc; 1750Sstevel@tonic-gate ucontext_t *ucp; 1760Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); 1770Sstevel@tonic-gate stack_t dummy_stk; 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate /* 1800Sstevel@tonic-gate * In future releases, when the ucontext structure grows, 1810Sstevel@tonic-gate * getcontext should be modified to only return the fields 1820Sstevel@tonic-gate * specified in the uc_flags. That way, the structure can grow 1830Sstevel@tonic-gate * and still be binary compatible will all .o's which will only 1840Sstevel@tonic-gate * have old fields defined in uc_flags 1850Sstevel@tonic-gate */ 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate switch (flag) { 1880Sstevel@tonic-gate default: 1890Sstevel@tonic-gate return (set_errno(EINVAL)); 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate case GETCONTEXT: 1920Sstevel@tonic-gate if (schedctl_sigblock(curthread)) { 1930Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 1940Sstevel@tonic-gate mutex_enter(&p->p_lock); 1950Sstevel@tonic-gate schedctl_finish_sigblock(curthread); 1960Sstevel@tonic-gate mutex_exit(&p->p_lock); 1970Sstevel@tonic-gate } 1980Sstevel@tonic-gate savecontext(&uc, curthread->t_hold); 1990Sstevel@tonic-gate if (copyout(&uc, arg, sizeof (uc))) 2000Sstevel@tonic-gate return (set_errno(EFAULT)); 2010Sstevel@tonic-gate return (0); 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate case SETCONTEXT: 2040Sstevel@tonic-gate ucp = arg; 2050Sstevel@tonic-gate if (ucp == NULL) 2060Sstevel@tonic-gate exit(CLD_EXITED, 0); 2070Sstevel@tonic-gate /* 2080Sstevel@tonic-gate * Don't copyin filler or floating state unless we need it. 2090Sstevel@tonic-gate * The ucontext_t struct and fields are specified in the ABI. 2100Sstevel@tonic-gate */ 2110Sstevel@tonic-gate if (copyin(ucp, &uc, sizeof (ucontext_t) - 2120Sstevel@tonic-gate sizeof (uc.uc_filler) - 2130Sstevel@tonic-gate sizeof (uc.uc_mcontext.fpregs))) { 2140Sstevel@tonic-gate return (set_errno(EFAULT)); 2150Sstevel@tonic-gate } 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate if ((uc.uc_flags & UC_FPU) && 2180Sstevel@tonic-gate copyin(&ucp->uc_mcontext.fpregs, &uc.uc_mcontext.fpregs, 2190Sstevel@tonic-gate sizeof (uc.uc_mcontext.fpregs))) { 2200Sstevel@tonic-gate return (set_errno(EFAULT)); 2210Sstevel@tonic-gate } 2220Sstevel@tonic-gate 2230Sstevel@tonic-gate restorecontext(&uc); 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate if ((uc.uc_flags & UC_STACK) && (lwp->lwp_ustack != 0)) 2260Sstevel@tonic-gate (void) copyout(&uc.uc_stack, (stack_t *)lwp->lwp_ustack, 2270Sstevel@tonic-gate sizeof (uc.uc_stack)); 2280Sstevel@tonic-gate return (0); 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate case GETUSTACK: 2310Sstevel@tonic-gate if (copyout(&lwp->lwp_ustack, arg, sizeof (caddr_t))) 2320Sstevel@tonic-gate return (set_errno(EFAULT)); 2330Sstevel@tonic-gate return (0); 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate case SETUSTACK: 2360Sstevel@tonic-gate if (copyin(arg, &dummy_stk, sizeof (dummy_stk))) 2370Sstevel@tonic-gate return (set_errno(EFAULT)); 2380Sstevel@tonic-gate lwp->lwp_ustack = (uintptr_t)arg; 2390Sstevel@tonic-gate return (0); 2400Sstevel@tonic-gate } 2410Sstevel@tonic-gate } 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate /* 2460Sstevel@tonic-gate * Save user context for 32-bit processes. 2470Sstevel@tonic-gate */ 2480Sstevel@tonic-gate void 2490Sstevel@tonic-gate savecontext32(ucontext32_t *ucp, k_sigset_t mask) 2500Sstevel@tonic-gate { 2510Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 2520Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); 2531829Ssudheer struct regs *rp = lwptoregs(lwp); 2540Sstevel@tonic-gate 2550Sstevel@tonic-gate bzero(&ucp->uc_mcontext.fpregs, sizeof (ucontext32_t) - 2560Sstevel@tonic-gate offsetof(ucontext32_t, uc_mcontext.fpregs)); 2570Sstevel@tonic-gate 2580Sstevel@tonic-gate ucp->uc_flags = UC_ALL; 2590Sstevel@tonic-gate ucp->uc_link = (caddr32_t)lwp->lwp_oldcontext; 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate if (lwp->lwp_ustack == NULL || 2620Sstevel@tonic-gate copyin((void *)lwp->lwp_ustack, &ucp->uc_stack, 2630Sstevel@tonic-gate sizeof (ucp->uc_stack)) != 0 || 2640Sstevel@tonic-gate ucp->uc_stack.ss_size == 0) { 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate if (lwp->lwp_sigaltstack.ss_flags == SS_ONSTACK) { 2670Sstevel@tonic-gate ucp->uc_stack.ss_sp = 2680Sstevel@tonic-gate (caddr32_t)(uintptr_t)lwp->lwp_sigaltstack.ss_sp; 2690Sstevel@tonic-gate ucp->uc_stack.ss_size = 2700Sstevel@tonic-gate (size32_t)lwp->lwp_sigaltstack.ss_size; 2710Sstevel@tonic-gate ucp->uc_stack.ss_flags = SS_ONSTACK; 2720Sstevel@tonic-gate } else { 2730Sstevel@tonic-gate ucp->uc_stack.ss_sp = (caddr32_t)(uintptr_t) 2740Sstevel@tonic-gate (p->p_usrstack - p->p_stksize); 2750Sstevel@tonic-gate ucp->uc_stack.ss_size = (size32_t)p->p_stksize; 2760Sstevel@tonic-gate ucp->uc_stack.ss_flags = 0; 2770Sstevel@tonic-gate } 2780Sstevel@tonic-gate } 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate getgregs32(lwp, ucp->uc_mcontext.gregs); 2810Sstevel@tonic-gate if (lwp->lwp_pcb.pcb_fpu.fpu_flags & FPU_EN) 2820Sstevel@tonic-gate getfpregs32(lwp, &ucp->uc_mcontext.fpregs); 2830Sstevel@tonic-gate else 2840Sstevel@tonic-gate ucp->uc_flags &= ~UC_FPU; 2850Sstevel@tonic-gate 2860Sstevel@tonic-gate sigktou(&mask, &ucp->uc_sigmask); 2871829Ssudheer /* 2881829Ssudheer * If the trace flag is set, arrange for single-stepping and 2891829Ssudheer * turn off the trace flag. 2901829Ssudheer */ 2911829Ssudheer if (rp->r_ps & PS_T) { 2921829Ssudheer lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING; 2931829Ssudheer rp->r_ps &= ~PS_T; 294*2086Ssudheer /* 295*2086Ssudheer * See comments in savecontext(). 296*2086Ssudheer */ 297*2086Ssudheer aston(curthread); 2981829Ssudheer } 2990Sstevel@tonic-gate } 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate int 3020Sstevel@tonic-gate getsetcontext32(int flag, void *arg) 3030Sstevel@tonic-gate { 3040Sstevel@tonic-gate ucontext32_t uc; 3050Sstevel@tonic-gate ucontext_t ucnat; 3060Sstevel@tonic-gate ucontext32_t *ucp; 3070Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); 3080Sstevel@tonic-gate caddr32_t ustack32; 3090Sstevel@tonic-gate stack32_t dummy_stk32; 3100Sstevel@tonic-gate 3110Sstevel@tonic-gate switch (flag) { 3120Sstevel@tonic-gate default: 3130Sstevel@tonic-gate return (set_errno(EINVAL)); 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate case GETCONTEXT: 3160Sstevel@tonic-gate if (schedctl_sigblock(curthread)) { 3170Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 3180Sstevel@tonic-gate mutex_enter(&p->p_lock); 3190Sstevel@tonic-gate schedctl_finish_sigblock(curthread); 3200Sstevel@tonic-gate mutex_exit(&p->p_lock); 3210Sstevel@tonic-gate } 3220Sstevel@tonic-gate savecontext32(&uc, curthread->t_hold); 3230Sstevel@tonic-gate if (copyout(&uc, arg, sizeof (uc))) 3240Sstevel@tonic-gate return (set_errno(EFAULT)); 3250Sstevel@tonic-gate return (0); 3260Sstevel@tonic-gate 3270Sstevel@tonic-gate case SETCONTEXT: 3280Sstevel@tonic-gate ucp = arg; 3290Sstevel@tonic-gate if (ucp == NULL) 3300Sstevel@tonic-gate exit(CLD_EXITED, 0); 3310Sstevel@tonic-gate if (copyin(ucp, &uc, sizeof (uc) - 3320Sstevel@tonic-gate sizeof (uc.uc_filler) - 3330Sstevel@tonic-gate sizeof (uc.uc_mcontext.fpregs))) { 3340Sstevel@tonic-gate return (set_errno(EFAULT)); 3350Sstevel@tonic-gate } 3360Sstevel@tonic-gate if ((uc.uc_flags & UC_FPU) && 3370Sstevel@tonic-gate copyin(&ucp->uc_mcontext.fpregs, &uc.uc_mcontext.fpregs, 3380Sstevel@tonic-gate sizeof (uc.uc_mcontext.fpregs))) { 3390Sstevel@tonic-gate return (set_errno(EFAULT)); 3400Sstevel@tonic-gate } 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate ucontext_32ton(&uc, &ucnat); 3430Sstevel@tonic-gate restorecontext(&ucnat); 3440Sstevel@tonic-gate 3450Sstevel@tonic-gate if ((uc.uc_flags & UC_STACK) && (lwp->lwp_ustack != 0)) 3460Sstevel@tonic-gate (void) copyout(&uc.uc_stack, 3470Sstevel@tonic-gate (stack32_t *)lwp->lwp_ustack, sizeof (uc.uc_stack)); 3480Sstevel@tonic-gate return (0); 3490Sstevel@tonic-gate 3500Sstevel@tonic-gate case GETUSTACK: 3510Sstevel@tonic-gate ustack32 = (caddr32_t)lwp->lwp_ustack; 3520Sstevel@tonic-gate if (copyout(&ustack32, arg, sizeof (ustack32))) 3530Sstevel@tonic-gate return (set_errno(EFAULT)); 3540Sstevel@tonic-gate return (0); 3550Sstevel@tonic-gate 3560Sstevel@tonic-gate case SETUSTACK: 3570Sstevel@tonic-gate if (copyin(arg, &dummy_stk32, sizeof (dummy_stk32))) 3580Sstevel@tonic-gate return (set_errno(EFAULT)); 3590Sstevel@tonic-gate lwp->lwp_ustack = (uintptr_t)arg; 3600Sstevel@tonic-gate return (0); 3610Sstevel@tonic-gate } 3620Sstevel@tonic-gate } 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */ 365