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 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 22*1048Sraf 230Sstevel@tonic-gate /* 24*1048Sraf * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 250Sstevel@tonic-gate * Use is subject to license terms. 260Sstevel@tonic-gate */ 270Sstevel@tonic-gate 280Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 290Sstevel@tonic-gate /* All Rights Reserved */ 300Sstevel@tonic-gate 310Sstevel@tonic-gate 320Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 330Sstevel@tonic-gate 340Sstevel@tonic-gate #include <sys/param.h> 350Sstevel@tonic-gate #include <sys/types.h> 360Sstevel@tonic-gate #include <sys/vmparam.h> 370Sstevel@tonic-gate #include <sys/systm.h> 380Sstevel@tonic-gate #include <sys/signal.h> 390Sstevel@tonic-gate #include <sys/stack.h> 400Sstevel@tonic-gate #include <sys/frame.h> 410Sstevel@tonic-gate #include <sys/proc.h> 420Sstevel@tonic-gate #include <sys/ucontext.h> 430Sstevel@tonic-gate #include <sys/asm_linkage.h> 440Sstevel@tonic-gate #include <sys/kmem.h> 450Sstevel@tonic-gate #include <sys/errno.h> 460Sstevel@tonic-gate #include <sys/archsystm.h> 470Sstevel@tonic-gate #include <sys/fpu/fpusystm.h> 480Sstevel@tonic-gate #include <sys/debug.h> 490Sstevel@tonic-gate #include <sys/model.h> 500Sstevel@tonic-gate #include <sys/cmn_err.h> 510Sstevel@tonic-gate #include <sys/sysmacros.h> 520Sstevel@tonic-gate #include <sys/privregs.h> 530Sstevel@tonic-gate #include <sys/schedctl.h> 540Sstevel@tonic-gate 550Sstevel@tonic-gate 560Sstevel@tonic-gate /* 570Sstevel@tonic-gate * Save user context. 580Sstevel@tonic-gate */ 590Sstevel@tonic-gate void 600Sstevel@tonic-gate savecontext(ucontext_t *ucp, k_sigset_t mask) 610Sstevel@tonic-gate { 620Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 630Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); 640Sstevel@tonic-gate 650Sstevel@tonic-gate /* 660Sstevel@tonic-gate * We assign to every field through uc_mcontext.fpregs.fpu_en, 670Sstevel@tonic-gate * but we have to bzero() everything after that. 680Sstevel@tonic-gate */ 690Sstevel@tonic-gate bzero(&ucp->uc_mcontext.fpregs.fpu_en, sizeof (ucontext_t) - 700Sstevel@tonic-gate offsetof(ucontext_t, uc_mcontext.fpregs.fpu_en)); 710Sstevel@tonic-gate /* 720Sstevel@tonic-gate * There are unused holes in the ucontext_t structure, zero-fill 730Sstevel@tonic-gate * them so that we don't expose kernel data to the user. 740Sstevel@tonic-gate */ 750Sstevel@tonic-gate (&ucp->uc_flags)[1] = 0; 760Sstevel@tonic-gate (&ucp->uc_stack.ss_flags)[1] = 0; 770Sstevel@tonic-gate 780Sstevel@tonic-gate /* 790Sstevel@tonic-gate * Flushing the user windows isn't strictly necessary; we do 800Sstevel@tonic-gate * it to maintain backward compatibility. 810Sstevel@tonic-gate */ 820Sstevel@tonic-gate (void) flush_user_windows_to_stack(NULL); 830Sstevel@tonic-gate 840Sstevel@tonic-gate ucp->uc_flags = UC_ALL; 850Sstevel@tonic-gate ucp->uc_link = (ucontext_t *)lwp->lwp_oldcontext; 860Sstevel@tonic-gate 870Sstevel@tonic-gate /* 880Sstevel@tonic-gate * Try to copyin() the ustack if one is registered. If the stack 890Sstevel@tonic-gate * has zero size, this indicates that stack bounds checking has 900Sstevel@tonic-gate * been disabled for this LWP. If stack bounds checking is disabled 910Sstevel@tonic-gate * or the copyin() fails, we fall back to the legacy behavior. 920Sstevel@tonic-gate */ 930Sstevel@tonic-gate if (lwp->lwp_ustack == NULL || 940Sstevel@tonic-gate copyin((void *)lwp->lwp_ustack, &ucp->uc_stack, 950Sstevel@tonic-gate sizeof (ucp->uc_stack)) != 0 || 960Sstevel@tonic-gate ucp->uc_stack.ss_size == 0) { 970Sstevel@tonic-gate 980Sstevel@tonic-gate if (lwp->lwp_sigaltstack.ss_flags == SS_ONSTACK) { 990Sstevel@tonic-gate ucp->uc_stack = lwp->lwp_sigaltstack; 1000Sstevel@tonic-gate } else { 1010Sstevel@tonic-gate ucp->uc_stack.ss_sp = p->p_usrstack - p->p_stksize; 1020Sstevel@tonic-gate ucp->uc_stack.ss_size = p->p_stksize; 1030Sstevel@tonic-gate ucp->uc_stack.ss_flags = 0; 1040Sstevel@tonic-gate } 1050Sstevel@tonic-gate } 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate getgregs(lwp, ucp->uc_mcontext.gregs); 1080Sstevel@tonic-gate getasrs(lwp, ucp->uc_mcontext.asrs); 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate getfpregs(lwp, &ucp->uc_mcontext.fpregs); 1110Sstevel@tonic-gate getfpasrs(lwp, ucp->uc_mcontext.asrs); 1120Sstevel@tonic-gate if (ucp->uc_mcontext.fpregs.fpu_en == 0) 1130Sstevel@tonic-gate ucp->uc_flags &= ~UC_FPU; 1140Sstevel@tonic-gate ucp->uc_mcontext.gwins = (gwindows_t *)NULL; 1150Sstevel@tonic-gate 1160Sstevel@tonic-gate /* 1170Sstevel@tonic-gate * Save signal mask. 1180Sstevel@tonic-gate */ 1190Sstevel@tonic-gate sigktou(&mask, &ucp->uc_sigmask); 1200Sstevel@tonic-gate } 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate void 1240Sstevel@tonic-gate restorecontext(ucontext_t *ucp) 1250Sstevel@tonic-gate { 1260Sstevel@tonic-gate kthread_t *t = curthread; 1270Sstevel@tonic-gate klwp_t *lwp = ttolwp(t); 1280Sstevel@tonic-gate mcontext_t *mcp = &ucp->uc_mcontext; 1290Sstevel@tonic-gate model_t model = lwp_getdatamodel(lwp); 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate (void) flush_user_windows_to_stack(NULL); 1320Sstevel@tonic-gate if (lwp->lwp_pcb.pcb_xregstat != XREGNONE) 1330Sstevel@tonic-gate xregrestore(lwp, 0); 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate lwp->lwp_oldcontext = (uintptr_t)ucp->uc_link; 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate if (ucp->uc_flags & UC_STACK) { 1380Sstevel@tonic-gate if (ucp->uc_stack.ss_flags == SS_ONSTACK) 1390Sstevel@tonic-gate lwp->lwp_sigaltstack = ucp->uc_stack; 1400Sstevel@tonic-gate else 1410Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_flags &= ~SS_ONSTACK; 1420Sstevel@tonic-gate } 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate if (ucp->uc_flags & UC_CPU) { 1450Sstevel@tonic-gate if (mcp->gwins != 0) 1460Sstevel@tonic-gate setgwins(lwp, mcp->gwins); 1470Sstevel@tonic-gate setgregs(lwp, mcp->gregs); 1480Sstevel@tonic-gate if (model == DATAMODEL_LP64) 1490Sstevel@tonic-gate setasrs(lwp, mcp->asrs); 1500Sstevel@tonic-gate else 1510Sstevel@tonic-gate xregs_setgregs(lwp, xregs_getptr(lwp, ucp)); 1520Sstevel@tonic-gate } 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate if (ucp->uc_flags & UC_FPU) { 1550Sstevel@tonic-gate fpregset_t *fp = &ucp->uc_mcontext.fpregs; 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate setfpregs(lwp, fp); 1580Sstevel@tonic-gate if (model == DATAMODEL_LP64) 1590Sstevel@tonic-gate setfpasrs(lwp, mcp->asrs); 1600Sstevel@tonic-gate else 1610Sstevel@tonic-gate xregs_setfpregs(lwp, xregs_getptr(lwp, ucp)); 1620Sstevel@tonic-gate run_fpq(lwp, fp); 1630Sstevel@tonic-gate } 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate if (ucp->uc_flags & UC_SIGMASK) { 1660Sstevel@tonic-gate proc_t *p = ttoproc(t); 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate mutex_enter(&p->p_lock); 1690Sstevel@tonic-gate schedctl_finish_sigblock(t); 1700Sstevel@tonic-gate sigutok(&ucp->uc_sigmask, &t->t_hold); 1710Sstevel@tonic-gate if (sigcheck(p, t)) 1720Sstevel@tonic-gate t->t_sig_check = 1; 1730Sstevel@tonic-gate mutex_exit(&p->p_lock); 1740Sstevel@tonic-gate } 1750Sstevel@tonic-gate } 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate int 1790Sstevel@tonic-gate getsetcontext(int flag, void *arg) 1800Sstevel@tonic-gate { 1810Sstevel@tonic-gate ucontext_t uc; 1820Sstevel@tonic-gate struct fq fpu_q[MAXFPQ]; /* to hold floating queue */ 1830Sstevel@tonic-gate fpregset_t *fpp; 1840Sstevel@tonic-gate gwindows_t *gwin = NULL; /* to hold windows */ 1850Sstevel@tonic-gate caddr_t xregs = NULL; 1860Sstevel@tonic-gate int xregs_size = 0; 1870Sstevel@tonic-gate extern int nwindows; 1880Sstevel@tonic-gate ucontext_t *ucp; 1890Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); 1900Sstevel@tonic-gate stack_t dummy_stk; 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate /* 1930Sstevel@tonic-gate * In future releases, when the ucontext structure grows, 1940Sstevel@tonic-gate * getcontext should be modified to only return the fields 1950Sstevel@tonic-gate * specified in the uc_flags. That way, the structure can grow 1960Sstevel@tonic-gate * and still be binary compatible will all .o's which will only 1970Sstevel@tonic-gate * have old fields defined in uc_flags 1980Sstevel@tonic-gate */ 1990Sstevel@tonic-gate 2000Sstevel@tonic-gate switch (flag) { 2010Sstevel@tonic-gate default: 2020Sstevel@tonic-gate return (set_errno(EINVAL)); 2030Sstevel@tonic-gate 2040Sstevel@tonic-gate case GETCONTEXT: 2050Sstevel@tonic-gate if (schedctl_sigblock(curthread)) { 2060Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 2070Sstevel@tonic-gate mutex_enter(&p->p_lock); 2080Sstevel@tonic-gate schedctl_finish_sigblock(curthread); 2090Sstevel@tonic-gate mutex_exit(&p->p_lock); 2100Sstevel@tonic-gate } 2110Sstevel@tonic-gate savecontext(&uc, curthread->t_hold); 2120Sstevel@tonic-gate /* 2130Sstevel@tonic-gate * When using floating point it should not be possible to 2140Sstevel@tonic-gate * get here with a fpu_qcnt other than zero since we go 2150Sstevel@tonic-gate * to great pains to handle all outstanding FP exceptions 2160Sstevel@tonic-gate * before any system call code gets executed. However we 2170Sstevel@tonic-gate * clear fpu_q and fpu_qcnt here before copyout anyway - 2180Sstevel@tonic-gate * this will prevent us from interpreting the garbage we 2190Sstevel@tonic-gate * get back (when FP is not enabled) as valid queue data on 2200Sstevel@tonic-gate * a later setcontext(2). 2210Sstevel@tonic-gate */ 2220Sstevel@tonic-gate uc.uc_mcontext.fpregs.fpu_qcnt = 0; 2230Sstevel@tonic-gate uc.uc_mcontext.fpregs.fpu_q = (struct fq *)NULL; 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate if (copyout(&uc, arg, sizeof (ucontext_t))) 2260Sstevel@tonic-gate return (set_errno(EFAULT)); 2270Sstevel@tonic-gate return (0); 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate case SETCONTEXT: 2300Sstevel@tonic-gate ucp = arg; 2310Sstevel@tonic-gate if (ucp == NULL) 2320Sstevel@tonic-gate exit(CLD_EXITED, 0); 2330Sstevel@tonic-gate /* 2340Sstevel@tonic-gate * Don't copyin filler or floating state unless we need it. 2350Sstevel@tonic-gate * The ucontext_t struct and fields are specified in the ABI. 2360Sstevel@tonic-gate */ 2370Sstevel@tonic-gate if (copyin(ucp, &uc, sizeof (ucontext_t) - 2380Sstevel@tonic-gate sizeof (uc.uc_filler) - 2390Sstevel@tonic-gate sizeof (uc.uc_mcontext.fpregs) - 2400Sstevel@tonic-gate sizeof (uc.uc_mcontext.xrs) - 2410Sstevel@tonic-gate sizeof (uc.uc_mcontext.asrs) - 2420Sstevel@tonic-gate sizeof (uc.uc_mcontext.filler))) { 2430Sstevel@tonic-gate return (set_errno(EFAULT)); 2440Sstevel@tonic-gate } 2450Sstevel@tonic-gate if (copyin(&ucp->uc_mcontext.xrs, &uc.uc_mcontext.xrs, 2460Sstevel@tonic-gate sizeof (uc.uc_mcontext.xrs))) { 2470Sstevel@tonic-gate return (set_errno(EFAULT)); 2480Sstevel@tonic-gate } 2490Sstevel@tonic-gate fpp = &uc.uc_mcontext.fpregs; 2500Sstevel@tonic-gate if (uc.uc_flags & UC_FPU) { 2510Sstevel@tonic-gate /* 2520Sstevel@tonic-gate * Need to copyin floating point state 2530Sstevel@tonic-gate */ 2540Sstevel@tonic-gate if (copyin(&ucp->uc_mcontext.fpregs, 2550Sstevel@tonic-gate &uc.uc_mcontext.fpregs, 2560Sstevel@tonic-gate sizeof (uc.uc_mcontext.fpregs))) 2570Sstevel@tonic-gate return (set_errno(EFAULT)); 2580Sstevel@tonic-gate /* if floating queue not empty */ 2590Sstevel@tonic-gate if ((fpp->fpu_q) && (fpp->fpu_qcnt)) { 2600Sstevel@tonic-gate if (fpp->fpu_qcnt > MAXFPQ || 2610Sstevel@tonic-gate fpp->fpu_q_entrysize <= 0 || 2620Sstevel@tonic-gate fpp->fpu_q_entrysize > sizeof (struct fq)) 2630Sstevel@tonic-gate return (set_errno(EINVAL)); 2640Sstevel@tonic-gate if (copyin(fpp->fpu_q, fpu_q, 2650Sstevel@tonic-gate fpp->fpu_qcnt * fpp->fpu_q_entrysize)) 2660Sstevel@tonic-gate return (set_errno(EFAULT)); 2670Sstevel@tonic-gate fpp->fpu_q = fpu_q; 2680Sstevel@tonic-gate } else { 2690Sstevel@tonic-gate fpp->fpu_qcnt = 0; /* avoid confusion later */ 2700Sstevel@tonic-gate } 2710Sstevel@tonic-gate } else { 2720Sstevel@tonic-gate fpp->fpu_qcnt = 0; 2730Sstevel@tonic-gate } 2740Sstevel@tonic-gate if (uc.uc_mcontext.gwins) { /* if windows in context */ 2750Sstevel@tonic-gate size_t gwin_size; 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate /* 2780Sstevel@tonic-gate * We do the same computation here to determine 2790Sstevel@tonic-gate * how many bytes of gwindows_t to copy in that 2800Sstevel@tonic-gate * is also done in sendsig() to decide how many 2810Sstevel@tonic-gate * bytes to copy out. We just *know* that wbcnt 2820Sstevel@tonic-gate * is the first element of the structure. 2830Sstevel@tonic-gate */ 2840Sstevel@tonic-gate gwin = kmem_zalloc(sizeof (gwindows_t), KM_SLEEP); 2850Sstevel@tonic-gate if (copyin(uc.uc_mcontext.gwins, 2860Sstevel@tonic-gate &gwin->wbcnt, sizeof (gwin->wbcnt))) { 2870Sstevel@tonic-gate kmem_free(gwin, sizeof (gwindows_t)); 2880Sstevel@tonic-gate return (set_errno(EFAULT)); 2890Sstevel@tonic-gate } 2900Sstevel@tonic-gate if (gwin->wbcnt < 0 || gwin->wbcnt > nwindows) { 2910Sstevel@tonic-gate kmem_free(gwin, sizeof (gwindows_t)); 2920Sstevel@tonic-gate return (set_errno(EINVAL)); 2930Sstevel@tonic-gate } 2940Sstevel@tonic-gate gwin_size = gwin->wbcnt * sizeof (struct rwindow) + 2950Sstevel@tonic-gate SPARC_MAXREGWINDOW * sizeof (int *) + sizeof (long); 2960Sstevel@tonic-gate if (gwin_size > sizeof (gwindows_t) || 2970Sstevel@tonic-gate copyin(uc.uc_mcontext.gwins, gwin, gwin_size)) { 2980Sstevel@tonic-gate kmem_free(gwin, sizeof (gwindows_t)); 2990Sstevel@tonic-gate return (set_errno(EFAULT)); 3000Sstevel@tonic-gate } 3010Sstevel@tonic-gate uc.uc_mcontext.gwins = gwin; 3020Sstevel@tonic-gate } 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate /* 3050Sstevel@tonic-gate * get extra register state or asrs if any exists 3060Sstevel@tonic-gate * there is no extra register state for _LP64 user programs 3070Sstevel@tonic-gate */ 3080Sstevel@tonic-gate xregs_clrptr(lwp, &uc); 3090Sstevel@tonic-gate if (copyin(&ucp->uc_mcontext.asrs, &uc.uc_mcontext.asrs, 3100Sstevel@tonic-gate sizeof (asrset_t))) { 3110Sstevel@tonic-gate /* Free up gwin structure if used */ 3120Sstevel@tonic-gate if (gwin) 3130Sstevel@tonic-gate kmem_free(gwin, sizeof (gwindows_t)); 3140Sstevel@tonic-gate return (set_errno(EFAULT)); 3150Sstevel@tonic-gate } 3160Sstevel@tonic-gate 3170Sstevel@tonic-gate restorecontext(&uc); 3180Sstevel@tonic-gate 3190Sstevel@tonic-gate if ((uc.uc_flags & UC_STACK) && (lwp->lwp_ustack != 0)) { 3200Sstevel@tonic-gate (void) copyout(&uc.uc_stack, (stack_t *)lwp->lwp_ustack, 3210Sstevel@tonic-gate sizeof (stack_t)); 3220Sstevel@tonic-gate } 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate /* 3250Sstevel@tonic-gate * free extra register state area 3260Sstevel@tonic-gate */ 3270Sstevel@tonic-gate if (xregs_size) 3280Sstevel@tonic-gate kmem_free(xregs, xregs_size); 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate if (gwin) 3310Sstevel@tonic-gate kmem_free(gwin, sizeof (gwindows_t)); 3320Sstevel@tonic-gate 3330Sstevel@tonic-gate return (0); 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate case GETUSTACK: 3360Sstevel@tonic-gate if (copyout(&lwp->lwp_ustack, arg, sizeof (caddr_t))) 3370Sstevel@tonic-gate return (set_errno(EFAULT)); 3380Sstevel@tonic-gate 3390Sstevel@tonic-gate return (0); 3400Sstevel@tonic-gate 3410Sstevel@tonic-gate case SETUSTACK: 3420Sstevel@tonic-gate if (copyin(arg, &dummy_stk, sizeof (dummy_stk))) 3430Sstevel@tonic-gate return (set_errno(EFAULT)); 3440Sstevel@tonic-gate 3450Sstevel@tonic-gate lwp->lwp_ustack = (uintptr_t)arg; 3460Sstevel@tonic-gate 3470Sstevel@tonic-gate return (0); 3480Sstevel@tonic-gate } 3490Sstevel@tonic-gate } 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate 3520Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 3530Sstevel@tonic-gate 3540Sstevel@tonic-gate /* 3550Sstevel@tonic-gate * Save user context for 32-bit processes. 3560Sstevel@tonic-gate */ 3570Sstevel@tonic-gate void 3580Sstevel@tonic-gate savecontext32(ucontext32_t *ucp, k_sigset_t mask, struct fq32 *dfq) 3590Sstevel@tonic-gate { 3600Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 3610Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); 3620Sstevel@tonic-gate fpregset_t fpregs; 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate /* 3650Sstevel@tonic-gate * We assign to every field through uc_mcontext.fpregs.fpu_en, 3660Sstevel@tonic-gate * but we have to bzero() everything after that. 3670Sstevel@tonic-gate */ 3680Sstevel@tonic-gate bzero(&ucp->uc_mcontext.fpregs.fpu_en, sizeof (ucontext32_t) - 3690Sstevel@tonic-gate offsetof(ucontext32_t, uc_mcontext.fpregs.fpu_en)); 3700Sstevel@tonic-gate /* 3710Sstevel@tonic-gate * There is an unused hole in the ucontext32_t structure; zero-fill 3720Sstevel@tonic-gate * it so that we don't expose kernel data to the user. 3730Sstevel@tonic-gate */ 3740Sstevel@tonic-gate (&ucp->uc_stack.ss_flags)[1] = 0; 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate /* 3770Sstevel@tonic-gate * Flushing the user windows isn't strictly necessary; we do 3780Sstevel@tonic-gate * it to maintain backward compatibility. 3790Sstevel@tonic-gate */ 3800Sstevel@tonic-gate (void) flush_user_windows_to_stack(NULL); 3810Sstevel@tonic-gate 3820Sstevel@tonic-gate ucp->uc_flags = UC_ALL; 3830Sstevel@tonic-gate ucp->uc_link = (caddr32_t)lwp->lwp_oldcontext; 3840Sstevel@tonic-gate 3850Sstevel@tonic-gate /* 3860Sstevel@tonic-gate * Try to copyin() the ustack if one is registered. If the stack 3870Sstevel@tonic-gate * has zero size, this indicates that stack bounds checking has 3880Sstevel@tonic-gate * been disabled for this LWP. If stack bounds checking is disabled 3890Sstevel@tonic-gate * or the copyin() fails, we fall back to the legacy behavior. 3900Sstevel@tonic-gate */ 3910Sstevel@tonic-gate if (lwp->lwp_ustack == NULL || 3920Sstevel@tonic-gate copyin((void *)lwp->lwp_ustack, &ucp->uc_stack, 3930Sstevel@tonic-gate sizeof (ucp->uc_stack)) != 0 || 3940Sstevel@tonic-gate ucp->uc_stack.ss_size == 0) { 3950Sstevel@tonic-gate 3960Sstevel@tonic-gate if (lwp->lwp_sigaltstack.ss_flags == SS_ONSTACK) { 3970Sstevel@tonic-gate ucp->uc_stack.ss_sp = 398*1048Sraf (caddr32_t)(uintptr_t)lwp->lwp_sigaltstack.ss_sp; 3990Sstevel@tonic-gate ucp->uc_stack.ss_size = 4000Sstevel@tonic-gate (size32_t)lwp->lwp_sigaltstack.ss_size; 4010Sstevel@tonic-gate ucp->uc_stack.ss_flags = SS_ONSTACK; 4020Sstevel@tonic-gate } else { 4030Sstevel@tonic-gate ucp->uc_stack.ss_sp = 404*1048Sraf (caddr32_t)(uintptr_t)p->p_usrstack - p->p_stksize; 4050Sstevel@tonic-gate ucp->uc_stack.ss_size = 4060Sstevel@tonic-gate (size32_t)p->p_stksize; 4070Sstevel@tonic-gate ucp->uc_stack.ss_flags = 0; 4080Sstevel@tonic-gate } 4090Sstevel@tonic-gate } 4100Sstevel@tonic-gate 4110Sstevel@tonic-gate getgregs32(lwp, ucp->uc_mcontext.gregs); 4120Sstevel@tonic-gate getfpregs(lwp, &fpregs); 4130Sstevel@tonic-gate fpuregset_nto32(&fpregs, &ucp->uc_mcontext.fpregs, dfq); 4140Sstevel@tonic-gate 4150Sstevel@tonic-gate if (ucp->uc_mcontext.fpregs.fpu_en == 0) 4160Sstevel@tonic-gate ucp->uc_flags &= ~UC_FPU; 4170Sstevel@tonic-gate ucp->uc_mcontext.gwins = (caddr32_t)NULL; 4180Sstevel@tonic-gate 4190Sstevel@tonic-gate /* 4200Sstevel@tonic-gate * Save signal mask (the 32- and 64-bit sigset_t structures are 4210Sstevel@tonic-gate * identical). 4220Sstevel@tonic-gate */ 4230Sstevel@tonic-gate sigktou(&mask, (sigset_t *)&ucp->uc_sigmask); 4240Sstevel@tonic-gate } 4250Sstevel@tonic-gate 4260Sstevel@tonic-gate int 4270Sstevel@tonic-gate getsetcontext32(int flag, void *arg) 4280Sstevel@tonic-gate { 4290Sstevel@tonic-gate ucontext32_t uc; 4300Sstevel@tonic-gate ucontext_t ucnat; 4310Sstevel@tonic-gate struct fq fpu_qnat[MAXFPQ]; /* to hold "native" floating queue */ 4320Sstevel@tonic-gate struct fq32 fpu_q[MAXFPQ]; /* to hold 32 bit floating queue */ 4330Sstevel@tonic-gate fpregset32_t *fpp; 4340Sstevel@tonic-gate gwindows32_t *gwin = NULL; /* to hold windows */ 4350Sstevel@tonic-gate caddr_t xregs; 4360Sstevel@tonic-gate int xregs_size = 0; 4370Sstevel@tonic-gate extern int nwindows; 4380Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); 4390Sstevel@tonic-gate ucontext32_t *ucp; 4400Sstevel@tonic-gate uint32_t ustack32; 4410Sstevel@tonic-gate stack32_t dummy_stk32; 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate /* 4440Sstevel@tonic-gate * In future releases, when the ucontext structure grows, 4450Sstevel@tonic-gate * getcontext should be modified to only return the fields 4460Sstevel@tonic-gate * specified in the uc_flags. That way, the structure can grow 4470Sstevel@tonic-gate * and still be binary compatible will all .o's which will only 4480Sstevel@tonic-gate * have old fields defined in uc_flags 4490Sstevel@tonic-gate */ 4500Sstevel@tonic-gate 4510Sstevel@tonic-gate switch (flag) { 4520Sstevel@tonic-gate default: 4530Sstevel@tonic-gate return (set_errno(EINVAL)); 4540Sstevel@tonic-gate 4550Sstevel@tonic-gate case GETCONTEXT: 4560Sstevel@tonic-gate if (schedctl_sigblock(curthread)) { 4570Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 4580Sstevel@tonic-gate mutex_enter(&p->p_lock); 4590Sstevel@tonic-gate schedctl_finish_sigblock(curthread); 4600Sstevel@tonic-gate mutex_exit(&p->p_lock); 4610Sstevel@tonic-gate } 4620Sstevel@tonic-gate savecontext32(&uc, curthread->t_hold, NULL); 4630Sstevel@tonic-gate /* 4640Sstevel@tonic-gate * When using floating point it should not be possible to 4650Sstevel@tonic-gate * get here with a fpu_qcnt other than zero since we go 4660Sstevel@tonic-gate * to great pains to handle all outstanding FP exceptions 4670Sstevel@tonic-gate * before any system call code gets executed. However we 4680Sstevel@tonic-gate * clear fpu_q and fpu_qcnt here before copyout anyway - 4690Sstevel@tonic-gate * this will prevent us from interpreting the garbage we 4700Sstevel@tonic-gate * get back (when FP is not enabled) as valid queue data on 4710Sstevel@tonic-gate * a later setcontext(2). 4720Sstevel@tonic-gate */ 4730Sstevel@tonic-gate uc.uc_mcontext.fpregs.fpu_qcnt = 0; 4740Sstevel@tonic-gate uc.uc_mcontext.fpregs.fpu_q = (caddr32_t)NULL; 4750Sstevel@tonic-gate 4760Sstevel@tonic-gate if (copyout(&uc, arg, sizeof (ucontext32_t))) 4770Sstevel@tonic-gate return (set_errno(EFAULT)); 4780Sstevel@tonic-gate return (0); 4790Sstevel@tonic-gate 4800Sstevel@tonic-gate case SETCONTEXT: 4810Sstevel@tonic-gate ucp = arg; 4820Sstevel@tonic-gate if (ucp == NULL) 4830Sstevel@tonic-gate exit(CLD_EXITED, 0); 4840Sstevel@tonic-gate /* 4850Sstevel@tonic-gate * Don't copyin filler or floating state unless we need it. 4860Sstevel@tonic-gate * The ucontext_t struct and fields are specified in the ABI. 4870Sstevel@tonic-gate */ 4880Sstevel@tonic-gate if (copyin(ucp, &uc, sizeof (uc) - sizeof (uc.uc_filler) - 4890Sstevel@tonic-gate sizeof (uc.uc_mcontext.fpregs) - 4900Sstevel@tonic-gate sizeof (uc.uc_mcontext.xrs) - 4910Sstevel@tonic-gate sizeof (uc.uc_mcontext.filler))) { 4920Sstevel@tonic-gate return (set_errno(EFAULT)); 4930Sstevel@tonic-gate } 4940Sstevel@tonic-gate if (copyin(&ucp->uc_mcontext.xrs, &uc.uc_mcontext.xrs, 4950Sstevel@tonic-gate sizeof (uc.uc_mcontext.xrs))) { 4960Sstevel@tonic-gate return (set_errno(EFAULT)); 4970Sstevel@tonic-gate } 4980Sstevel@tonic-gate fpp = &uc.uc_mcontext.fpregs; 4990Sstevel@tonic-gate if (uc.uc_flags & UC_FPU) { 5000Sstevel@tonic-gate /* 5010Sstevel@tonic-gate * Need to copyin floating point state 5020Sstevel@tonic-gate */ 5030Sstevel@tonic-gate if (copyin(&ucp->uc_mcontext.fpregs, 5040Sstevel@tonic-gate &uc.uc_mcontext.fpregs, 5050Sstevel@tonic-gate sizeof (uc.uc_mcontext.fpregs))) 5060Sstevel@tonic-gate return (set_errno(EFAULT)); 5070Sstevel@tonic-gate /* if floating queue not empty */ 5080Sstevel@tonic-gate if ((fpp->fpu_q) && (fpp->fpu_qcnt)) { 5090Sstevel@tonic-gate if (fpp->fpu_qcnt > MAXFPQ || 5100Sstevel@tonic-gate fpp->fpu_q_entrysize <= 0 || 5110Sstevel@tonic-gate fpp->fpu_q_entrysize > sizeof (struct fq32)) 5120Sstevel@tonic-gate return (set_errno(EINVAL)); 513*1048Sraf if (copyin((void *)(uintptr_t)fpp->fpu_q, fpu_q, 5140Sstevel@tonic-gate fpp->fpu_qcnt * fpp->fpu_q_entrysize)) 5150Sstevel@tonic-gate return (set_errno(EFAULT)); 5160Sstevel@tonic-gate } else { 5170Sstevel@tonic-gate fpp->fpu_qcnt = 0; /* avoid confusion later */ 5180Sstevel@tonic-gate } 5190Sstevel@tonic-gate } else { 5200Sstevel@tonic-gate fpp->fpu_qcnt = 0; 5210Sstevel@tonic-gate } 5220Sstevel@tonic-gate 5230Sstevel@tonic-gate if (uc.uc_mcontext.gwins) { /* if windows in context */ 5240Sstevel@tonic-gate size_t gwin_size; 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate /* 5270Sstevel@tonic-gate * We do the same computation here to determine 5280Sstevel@tonic-gate * how many bytes of gwindows_t to copy in that 5290Sstevel@tonic-gate * is also done in sendsig() to decide how many 5300Sstevel@tonic-gate * bytes to copy out. We just *know* that wbcnt 5310Sstevel@tonic-gate * is the first element of the structure. 5320Sstevel@tonic-gate */ 5330Sstevel@tonic-gate gwin = kmem_zalloc(sizeof (gwindows32_t), 5340Sstevel@tonic-gate KM_SLEEP); 535*1048Sraf if (copyin((void *)(uintptr_t)uc.uc_mcontext.gwins, 5360Sstevel@tonic-gate &gwin->wbcnt, sizeof (gwin->wbcnt))) { 5370Sstevel@tonic-gate kmem_free(gwin, sizeof (gwindows32_t)); 5380Sstevel@tonic-gate return (set_errno(EFAULT)); 5390Sstevel@tonic-gate } 5400Sstevel@tonic-gate if (gwin->wbcnt < 0 || gwin->wbcnt > nwindows) { 5410Sstevel@tonic-gate kmem_free(gwin, sizeof (gwindows32_t)); 5420Sstevel@tonic-gate return (set_errno(EINVAL)); 5430Sstevel@tonic-gate } 5440Sstevel@tonic-gate gwin_size = gwin->wbcnt * sizeof (struct rwindow32) + 5450Sstevel@tonic-gate SPARC_MAXREGWINDOW * sizeof (caddr32_t) + 5460Sstevel@tonic-gate sizeof (int32_t); 5470Sstevel@tonic-gate if (gwin_size > sizeof (gwindows32_t) || 548*1048Sraf copyin((void *)(uintptr_t)uc.uc_mcontext.gwins, 5490Sstevel@tonic-gate gwin, gwin_size)) { 5500Sstevel@tonic-gate kmem_free(gwin, sizeof (gwindows32_t)); 5510Sstevel@tonic-gate return (set_errno(EFAULT)); 5520Sstevel@tonic-gate } 5530Sstevel@tonic-gate /* restorecontext() should ignore this */ 5540Sstevel@tonic-gate uc.uc_mcontext.gwins = (caddr32_t)0; 5550Sstevel@tonic-gate } 5560Sstevel@tonic-gate 5570Sstevel@tonic-gate ucontext_32ton(&uc, &ucnat, fpu_q, fpu_qnat); 5580Sstevel@tonic-gate 5590Sstevel@tonic-gate /* 5600Sstevel@tonic-gate * get extra register state if any exists 5610Sstevel@tonic-gate */ 5620Sstevel@tonic-gate if (xregs_hasptr32(lwp, &uc) && 5630Sstevel@tonic-gate ((xregs_size = xregs_getsize(curproc)) > 0)) { 5640Sstevel@tonic-gate xregs = kmem_zalloc(xregs_size, KM_SLEEP); 565*1048Sraf if (copyin((void *)(uintptr_t)xregs_getptr32(lwp, &uc), 5660Sstevel@tonic-gate xregs, xregs_size)) { 5670Sstevel@tonic-gate kmem_free(xregs, xregs_size); 5680Sstevel@tonic-gate if (gwin) 5690Sstevel@tonic-gate kmem_free(gwin, sizeof (gwindows32_t)); 5700Sstevel@tonic-gate return (set_errno(EFAULT)); 5710Sstevel@tonic-gate } 5720Sstevel@tonic-gate xregs_setptr(lwp, &ucnat, xregs); 5730Sstevel@tonic-gate } else { 5740Sstevel@tonic-gate xregs_clrptr(lwp, &ucnat); 5750Sstevel@tonic-gate } 5760Sstevel@tonic-gate 5770Sstevel@tonic-gate restorecontext(&ucnat); 5780Sstevel@tonic-gate 5790Sstevel@tonic-gate if ((uc.uc_flags & UC_STACK) && (lwp->lwp_ustack != 0)) { 5800Sstevel@tonic-gate (void) copyout(&uc.uc_stack, 5810Sstevel@tonic-gate (stack32_t *)lwp->lwp_ustack, sizeof (stack32_t)); 5820Sstevel@tonic-gate } 5830Sstevel@tonic-gate 5840Sstevel@tonic-gate if (gwin) 5850Sstevel@tonic-gate setgwins32(lwp, gwin); 5860Sstevel@tonic-gate 5870Sstevel@tonic-gate /* 5880Sstevel@tonic-gate * free extra register state area 5890Sstevel@tonic-gate */ 5900Sstevel@tonic-gate if (xregs_size) 5910Sstevel@tonic-gate kmem_free(xregs, xregs_size); 5920Sstevel@tonic-gate 5930Sstevel@tonic-gate if (gwin) 5940Sstevel@tonic-gate kmem_free(gwin, sizeof (gwindows32_t)); 5950Sstevel@tonic-gate 5960Sstevel@tonic-gate return (0); 5970Sstevel@tonic-gate 5980Sstevel@tonic-gate case GETUSTACK: 5990Sstevel@tonic-gate ustack32 = (uint32_t)lwp->lwp_ustack; 6000Sstevel@tonic-gate if (copyout(&ustack32, arg, sizeof (caddr32_t))) 6010Sstevel@tonic-gate return (set_errno(EFAULT)); 6020Sstevel@tonic-gate 6030Sstevel@tonic-gate return (0); 6040Sstevel@tonic-gate 6050Sstevel@tonic-gate case SETUSTACK: 6060Sstevel@tonic-gate if (copyin(arg, &dummy_stk32, sizeof (dummy_stk32))) 6070Sstevel@tonic-gate return (set_errno(EFAULT)); 6080Sstevel@tonic-gate 6090Sstevel@tonic-gate lwp->lwp_ustack = (uintptr_t)arg; 6100Sstevel@tonic-gate 6110Sstevel@tonic-gate return (0); 6120Sstevel@tonic-gate } 6130Sstevel@tonic-gate } 6140Sstevel@tonic-gate 6150Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */ 616