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 220Sstevel@tonic-gate /* 23*10341SRoger.Faulkner@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 27*10341SRoger.Faulkner@Sun.COM /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28*10341SRoger.Faulkner@Sun.COM /* All Rights Reserved */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate #include <sys/param.h> 310Sstevel@tonic-gate #include <sys/types.h> 320Sstevel@tonic-gate #include <sys/vmparam.h> 330Sstevel@tonic-gate #include <sys/systm.h> 340Sstevel@tonic-gate #include <sys/signal.h> 350Sstevel@tonic-gate #include <sys/stack.h> 360Sstevel@tonic-gate #include <sys/regset.h> 370Sstevel@tonic-gate #include <sys/privregs.h> 380Sstevel@tonic-gate #include <sys/frame.h> 390Sstevel@tonic-gate #include <sys/proc.h> 400Sstevel@tonic-gate #include <sys/psw.h> 410Sstevel@tonic-gate #include <sys/ucontext.h> 420Sstevel@tonic-gate #include <sys/asm_linkage.h> 430Sstevel@tonic-gate #include <sys/errno.h> 440Sstevel@tonic-gate #include <sys/archsystm.h> 450Sstevel@tonic-gate #include <sys/schedctl.h> 460Sstevel@tonic-gate #include <sys/debug.h> 470Sstevel@tonic-gate #include <sys/sysmacros.h> 480Sstevel@tonic-gate 490Sstevel@tonic-gate /* 500Sstevel@tonic-gate * Save user context. 510Sstevel@tonic-gate */ 520Sstevel@tonic-gate void 530Sstevel@tonic-gate savecontext(ucontext_t *ucp, k_sigset_t mask) 540Sstevel@tonic-gate { 550Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 560Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); 571829Ssudheer struct regs *rp = lwptoregs(lwp); 580Sstevel@tonic-gate 590Sstevel@tonic-gate /* 600Sstevel@tonic-gate * We unconditionally assign to every field through the end 610Sstevel@tonic-gate * of the gregs, but we need to bzero() everything -after- that 620Sstevel@tonic-gate * to avoid having any kernel stack garbage escape to userland. 630Sstevel@tonic-gate */ 640Sstevel@tonic-gate bzero(&ucp->uc_mcontext.fpregs, sizeof (ucontext_t) - 650Sstevel@tonic-gate offsetof(ucontext_t, uc_mcontext.fpregs)); 660Sstevel@tonic-gate 670Sstevel@tonic-gate ucp->uc_flags = UC_ALL; 680Sstevel@tonic-gate ucp->uc_link = (struct ucontext *)lwp->lwp_oldcontext; 690Sstevel@tonic-gate 700Sstevel@tonic-gate /* 710Sstevel@tonic-gate * Try to copyin() the ustack if one is registered. If the stack 720Sstevel@tonic-gate * has zero size, this indicates that stack bounds checking has 730Sstevel@tonic-gate * been disabled for this LWP. If stack bounds checking is disabled 740Sstevel@tonic-gate * or the copyin() fails, we fall back to the legacy behavior. 750Sstevel@tonic-gate */ 760Sstevel@tonic-gate if (lwp->lwp_ustack == NULL || 770Sstevel@tonic-gate copyin((void *)lwp->lwp_ustack, &ucp->uc_stack, 780Sstevel@tonic-gate sizeof (ucp->uc_stack)) != 0 || 790Sstevel@tonic-gate ucp->uc_stack.ss_size == 0) { 800Sstevel@tonic-gate 810Sstevel@tonic-gate if (lwp->lwp_sigaltstack.ss_flags == SS_ONSTACK) { 820Sstevel@tonic-gate ucp->uc_stack = lwp->lwp_sigaltstack; 830Sstevel@tonic-gate } else { 840Sstevel@tonic-gate ucp->uc_stack.ss_sp = p->p_usrstack - p->p_stksize; 850Sstevel@tonic-gate ucp->uc_stack.ss_size = p->p_stksize; 860Sstevel@tonic-gate ucp->uc_stack.ss_flags = 0; 870Sstevel@tonic-gate } 880Sstevel@tonic-gate } 890Sstevel@tonic-gate 905235Sksadhukh /* 915235Sksadhukh * If either the trace flag or REQUEST_STEP is set, 925235Sksadhukh * arrange for single-stepping and turn off the trace flag. 935235Sksadhukh */ 945235Sksadhukh if ((rp->r_ps & PS_T) || (lwp->lwp_pcb.pcb_flags & REQUEST_STEP)) { 955235Sksadhukh /* 965235Sksadhukh * Clear PS_T so that saved user context won't have trace 975235Sksadhukh * flag set. 985235Sksadhukh */ 995235Sksadhukh rp->r_ps &= ~PS_T; 1005235Sksadhukh 1015235Sksadhukh if (!(lwp->lwp_pcb.pcb_flags & REQUEST_NOSTEP)) { 1025235Sksadhukh lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING; 1035235Sksadhukh /* 1045235Sksadhukh * trap() always checks DEBUG_PENDING before 1055235Sksadhukh * checking for any pending signal. This at times 1065235Sksadhukh * can potentially lead to DEBUG_PENDING not being 1075235Sksadhukh * honoured. (for eg: the lwp is stopped by 1085235Sksadhukh * stop_on_fault() called from trap(), after being 1095235Sksadhukh * awakened it might see a pending signal and call 1105235Sksadhukh * savecontext(), however on the way back to userland 1115235Sksadhukh * there is no place it can be detected). Hence in 1125235Sksadhukh * anticipation of such occassions, set AST flag for 1135235Sksadhukh * the thread which will make the thread take an 1145235Sksadhukh * excursion through trap() where it will be handled 1155235Sksadhukh * appropriately. 1165235Sksadhukh */ 1175235Sksadhukh aston(curthread); 1185235Sksadhukh } 1195235Sksadhukh } 1205235Sksadhukh 1210Sstevel@tonic-gate getgregs(lwp, ucp->uc_mcontext.gregs); 1220Sstevel@tonic-gate if (lwp->lwp_pcb.pcb_fpu.fpu_flags & FPU_EN) 1230Sstevel@tonic-gate getfpregs(lwp, &ucp->uc_mcontext.fpregs); 1240Sstevel@tonic-gate else 1250Sstevel@tonic-gate ucp->uc_flags &= ~UC_FPU; 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate sigktou(&mask, &ucp->uc_sigmask); 1280Sstevel@tonic-gate } 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate /* 1310Sstevel@tonic-gate * Restore user context. 1320Sstevel@tonic-gate */ 1330Sstevel@tonic-gate void 1340Sstevel@tonic-gate restorecontext(ucontext_t *ucp) 1350Sstevel@tonic-gate { 1360Sstevel@tonic-gate kthread_t *t = curthread; 1370Sstevel@tonic-gate klwp_t *lwp = ttolwp(t); 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate lwp->lwp_oldcontext = (uintptr_t)ucp->uc_link; 1400Sstevel@tonic-gate 1410Sstevel@tonic-gate if (ucp->uc_flags & UC_STACK) { 1420Sstevel@tonic-gate if (ucp->uc_stack.ss_flags == SS_ONSTACK) 1430Sstevel@tonic-gate lwp->lwp_sigaltstack = ucp->uc_stack; 1440Sstevel@tonic-gate else 1450Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_flags &= ~SS_ONSTACK; 1460Sstevel@tonic-gate } 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate if (ucp->uc_flags & UC_CPU) { 1491829Ssudheer /* 1501829Ssudheer * If the trace flag is set, mark the lwp to take a 1511829Ssudheer * single-step trap on return to user level (below). 1521829Ssudheer * The x86 lcall interface and sysenter has already done this, 1531829Ssudheer * and turned off the flag, but amd64 syscall interface has not. 1541829Ssudheer */ 1551829Ssudheer if (lwptoregs(lwp)->r_ps & PS_T) 1561829Ssudheer lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING; 1570Sstevel@tonic-gate setgregs(lwp, ucp->uc_mcontext.gregs); 1580Sstevel@tonic-gate lwp->lwp_eosys = JUSTRETURN; 1590Sstevel@tonic-gate t->t_post_sys = 1; 1602086Ssudheer aston(curthread); 1610Sstevel@tonic-gate } 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate if (ucp->uc_flags & UC_FPU) 1640Sstevel@tonic-gate setfpregs(lwp, &ucp->uc_mcontext.fpregs); 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate if (ucp->uc_flags & UC_SIGMASK) { 167*10341SRoger.Faulkner@Sun.COM /* 168*10341SRoger.Faulkner@Sun.COM * We don't need to acquire p->p_lock here; 169*10341SRoger.Faulkner@Sun.COM * we are manipulating thread-private data. 170*10341SRoger.Faulkner@Sun.COM */ 1710Sstevel@tonic-gate schedctl_finish_sigblock(t); 1720Sstevel@tonic-gate sigutok(&ucp->uc_sigmask, &t->t_hold); 173*10341SRoger.Faulkner@Sun.COM if (sigcheck(ttoproc(t), t)) 1740Sstevel@tonic-gate t->t_sig_check = 1; 1750Sstevel@tonic-gate } 1760Sstevel@tonic-gate } 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate int 1800Sstevel@tonic-gate getsetcontext(int flag, void *arg) 1810Sstevel@tonic-gate { 1820Sstevel@tonic-gate ucontext_t uc; 1830Sstevel@tonic-gate ucontext_t *ucp; 1840Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); 1850Sstevel@tonic-gate stack_t dummy_stk; 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate /* 1880Sstevel@tonic-gate * In future releases, when the ucontext structure grows, 1890Sstevel@tonic-gate * getcontext should be modified to only return the fields 1900Sstevel@tonic-gate * specified in the uc_flags. That way, the structure can grow 1910Sstevel@tonic-gate * and still be binary compatible will all .o's which will only 1920Sstevel@tonic-gate * have old fields defined in uc_flags 1930Sstevel@tonic-gate */ 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate switch (flag) { 1960Sstevel@tonic-gate default: 1970Sstevel@tonic-gate return (set_errno(EINVAL)); 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate case GETCONTEXT: 200*10341SRoger.Faulkner@Sun.COM schedctl_finish_sigblock(curthread); 2010Sstevel@tonic-gate savecontext(&uc, curthread->t_hold); 2020Sstevel@tonic-gate if (copyout(&uc, arg, sizeof (uc))) 2030Sstevel@tonic-gate return (set_errno(EFAULT)); 2040Sstevel@tonic-gate return (0); 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate case SETCONTEXT: 2070Sstevel@tonic-gate ucp = arg; 2080Sstevel@tonic-gate if (ucp == NULL) 2090Sstevel@tonic-gate exit(CLD_EXITED, 0); 2100Sstevel@tonic-gate /* 2110Sstevel@tonic-gate * Don't copyin filler or floating state unless we need it. 2120Sstevel@tonic-gate * The ucontext_t struct and fields are specified in the ABI. 2130Sstevel@tonic-gate */ 2140Sstevel@tonic-gate if (copyin(ucp, &uc, sizeof (ucontext_t) - 2150Sstevel@tonic-gate sizeof (uc.uc_filler) - 2160Sstevel@tonic-gate sizeof (uc.uc_mcontext.fpregs))) { 2170Sstevel@tonic-gate return (set_errno(EFAULT)); 2180Sstevel@tonic-gate } 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate if ((uc.uc_flags & UC_FPU) && 2210Sstevel@tonic-gate copyin(&ucp->uc_mcontext.fpregs, &uc.uc_mcontext.fpregs, 2220Sstevel@tonic-gate sizeof (uc.uc_mcontext.fpregs))) { 2230Sstevel@tonic-gate return (set_errno(EFAULT)); 2240Sstevel@tonic-gate } 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate restorecontext(&uc); 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate if ((uc.uc_flags & UC_STACK) && (lwp->lwp_ustack != 0)) 2290Sstevel@tonic-gate (void) copyout(&uc.uc_stack, (stack_t *)lwp->lwp_ustack, 2300Sstevel@tonic-gate sizeof (uc.uc_stack)); 2310Sstevel@tonic-gate return (0); 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate case GETUSTACK: 2340Sstevel@tonic-gate if (copyout(&lwp->lwp_ustack, arg, sizeof (caddr_t))) 2350Sstevel@tonic-gate return (set_errno(EFAULT)); 2360Sstevel@tonic-gate return (0); 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate case SETUSTACK: 2390Sstevel@tonic-gate if (copyin(arg, &dummy_stk, sizeof (dummy_stk))) 2400Sstevel@tonic-gate return (set_errno(EFAULT)); 2410Sstevel@tonic-gate lwp->lwp_ustack = (uintptr_t)arg; 2420Sstevel@tonic-gate return (0); 2430Sstevel@tonic-gate } 2440Sstevel@tonic-gate } 2450Sstevel@tonic-gate 2460Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 2470Sstevel@tonic-gate 2480Sstevel@tonic-gate /* 2490Sstevel@tonic-gate * Save user context for 32-bit processes. 2500Sstevel@tonic-gate */ 2510Sstevel@tonic-gate void 2520Sstevel@tonic-gate savecontext32(ucontext32_t *ucp, k_sigset_t mask) 2530Sstevel@tonic-gate { 2540Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 2550Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); 2561829Ssudheer struct regs *rp = lwptoregs(lwp); 2570Sstevel@tonic-gate 2580Sstevel@tonic-gate bzero(&ucp->uc_mcontext.fpregs, sizeof (ucontext32_t) - 2590Sstevel@tonic-gate offsetof(ucontext32_t, uc_mcontext.fpregs)); 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate ucp->uc_flags = UC_ALL; 2620Sstevel@tonic-gate ucp->uc_link = (caddr32_t)lwp->lwp_oldcontext; 2630Sstevel@tonic-gate 2640Sstevel@tonic-gate if (lwp->lwp_ustack == NULL || 2650Sstevel@tonic-gate copyin((void *)lwp->lwp_ustack, &ucp->uc_stack, 2660Sstevel@tonic-gate sizeof (ucp->uc_stack)) != 0 || 2670Sstevel@tonic-gate ucp->uc_stack.ss_size == 0) { 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate if (lwp->lwp_sigaltstack.ss_flags == SS_ONSTACK) { 2700Sstevel@tonic-gate ucp->uc_stack.ss_sp = 2710Sstevel@tonic-gate (caddr32_t)(uintptr_t)lwp->lwp_sigaltstack.ss_sp; 2720Sstevel@tonic-gate ucp->uc_stack.ss_size = 2730Sstevel@tonic-gate (size32_t)lwp->lwp_sigaltstack.ss_size; 2740Sstevel@tonic-gate ucp->uc_stack.ss_flags = SS_ONSTACK; 2750Sstevel@tonic-gate } else { 2760Sstevel@tonic-gate ucp->uc_stack.ss_sp = (caddr32_t)(uintptr_t) 2770Sstevel@tonic-gate (p->p_usrstack - p->p_stksize); 2780Sstevel@tonic-gate ucp->uc_stack.ss_size = (size32_t)p->p_stksize; 2790Sstevel@tonic-gate ucp->uc_stack.ss_flags = 0; 2800Sstevel@tonic-gate } 2810Sstevel@tonic-gate } 2820Sstevel@tonic-gate 2835235Sksadhukh /* 2845235Sksadhukh * If either the trace flag or REQUEST_STEP is set, arrange 2855235Sksadhukh * for single-stepping and turn off the trace flag. 2865235Sksadhukh */ 2875235Sksadhukh if ((rp->r_ps & PS_T) || (lwp->lwp_pcb.pcb_flags & REQUEST_STEP)) { 2885235Sksadhukh /* 2895235Sksadhukh * Clear PS_T so that saved user context won't have trace 2905235Sksadhukh * flag set. 2915235Sksadhukh */ 2925235Sksadhukh rp->r_ps &= ~PS_T; 2935235Sksadhukh 2945235Sksadhukh if (!(lwp->lwp_pcb.pcb_flags & REQUEST_NOSTEP)) { 2955235Sksadhukh lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING; 2965235Sksadhukh /* 2975235Sksadhukh * See comments in savecontext(). 2985235Sksadhukh */ 2995235Sksadhukh aston(curthread); 3005235Sksadhukh } 3015235Sksadhukh } 3025235Sksadhukh 3030Sstevel@tonic-gate getgregs32(lwp, ucp->uc_mcontext.gregs); 3040Sstevel@tonic-gate if (lwp->lwp_pcb.pcb_fpu.fpu_flags & FPU_EN) 3050Sstevel@tonic-gate getfpregs32(lwp, &ucp->uc_mcontext.fpregs); 3060Sstevel@tonic-gate else 3070Sstevel@tonic-gate ucp->uc_flags &= ~UC_FPU; 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate sigktou(&mask, &ucp->uc_sigmask); 3100Sstevel@tonic-gate } 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate int 3130Sstevel@tonic-gate getsetcontext32(int flag, void *arg) 3140Sstevel@tonic-gate { 3150Sstevel@tonic-gate ucontext32_t uc; 3160Sstevel@tonic-gate ucontext_t ucnat; 3170Sstevel@tonic-gate ucontext32_t *ucp; 3180Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); 3190Sstevel@tonic-gate caddr32_t ustack32; 3200Sstevel@tonic-gate stack32_t dummy_stk32; 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate switch (flag) { 3230Sstevel@tonic-gate default: 3240Sstevel@tonic-gate return (set_errno(EINVAL)); 3250Sstevel@tonic-gate 3260Sstevel@tonic-gate case GETCONTEXT: 327*10341SRoger.Faulkner@Sun.COM schedctl_finish_sigblock(curthread); 3280Sstevel@tonic-gate savecontext32(&uc, curthread->t_hold); 3290Sstevel@tonic-gate if (copyout(&uc, arg, sizeof (uc))) 3300Sstevel@tonic-gate return (set_errno(EFAULT)); 3310Sstevel@tonic-gate return (0); 3320Sstevel@tonic-gate 3330Sstevel@tonic-gate case SETCONTEXT: 3340Sstevel@tonic-gate ucp = arg; 3350Sstevel@tonic-gate if (ucp == NULL) 3360Sstevel@tonic-gate exit(CLD_EXITED, 0); 3370Sstevel@tonic-gate if (copyin(ucp, &uc, sizeof (uc) - 3380Sstevel@tonic-gate sizeof (uc.uc_filler) - 3390Sstevel@tonic-gate sizeof (uc.uc_mcontext.fpregs))) { 3400Sstevel@tonic-gate return (set_errno(EFAULT)); 3410Sstevel@tonic-gate } 3420Sstevel@tonic-gate if ((uc.uc_flags & UC_FPU) && 3430Sstevel@tonic-gate copyin(&ucp->uc_mcontext.fpregs, &uc.uc_mcontext.fpregs, 3440Sstevel@tonic-gate sizeof (uc.uc_mcontext.fpregs))) { 3450Sstevel@tonic-gate return (set_errno(EFAULT)); 3460Sstevel@tonic-gate } 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate ucontext_32ton(&uc, &ucnat); 3490Sstevel@tonic-gate restorecontext(&ucnat); 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate if ((uc.uc_flags & UC_STACK) && (lwp->lwp_ustack != 0)) 3520Sstevel@tonic-gate (void) copyout(&uc.uc_stack, 3530Sstevel@tonic-gate (stack32_t *)lwp->lwp_ustack, sizeof (uc.uc_stack)); 3540Sstevel@tonic-gate return (0); 3550Sstevel@tonic-gate 3560Sstevel@tonic-gate case GETUSTACK: 3570Sstevel@tonic-gate ustack32 = (caddr32_t)lwp->lwp_ustack; 3580Sstevel@tonic-gate if (copyout(&ustack32, arg, sizeof (ustack32))) 3590Sstevel@tonic-gate return (set_errno(EFAULT)); 3600Sstevel@tonic-gate return (0); 3610Sstevel@tonic-gate 3620Sstevel@tonic-gate case SETUSTACK: 3630Sstevel@tonic-gate if (copyin(arg, &dummy_stk32, sizeof (dummy_stk32))) 3640Sstevel@tonic-gate return (set_errno(EFAULT)); 3650Sstevel@tonic-gate lwp->lwp_ustack = (uintptr_t)arg; 3660Sstevel@tonic-gate return (0); 3670Sstevel@tonic-gate } 3680Sstevel@tonic-gate } 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */ 371