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