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