17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57be238fRoger A. Faulkner * Common Development and Distribution License (the "License").
67be238fRoger A. Faulkner * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217552190raf
227c478bdstevel@tonic-gate/*
23bdf0047Roger A. Faulkner * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
281255103Toomas Soome/*	  All Rights Reserved	*/
297c478bdstevel@tonic-gate
307c478bdstevel@tonic-gate#include <sys/param.h>
317c478bdstevel@tonic-gate#include <sys/types.h>
327c478bdstevel@tonic-gate#include <sys/vmparam.h>
337c478bdstevel@tonic-gate#include <sys/systm.h>
347c478bdstevel@tonic-gate#include <sys/signal.h>
357c478bdstevel@tonic-gate#include <sys/stack.h>
367c478bdstevel@tonic-gate#include <sys/frame.h>
377c478bdstevel@tonic-gate#include <sys/proc.h>
388f798d3Roger A. Faulkner#include <sys/brand.h>
397c478bdstevel@tonic-gate#include <sys/ucontext.h>
407c478bdstevel@tonic-gate#include <sys/asm_linkage.h>
417c478bdstevel@tonic-gate#include <sys/kmem.h>
427c478bdstevel@tonic-gate#include <sys/errno.h>
437c478bdstevel@tonic-gate#include <sys/archsystm.h>
447c478bdstevel@tonic-gate#include <sys/fpu/fpusystm.h>
457c478bdstevel@tonic-gate#include <sys/debug.h>
467c478bdstevel@tonic-gate#include <sys/model.h>
477c478bdstevel@tonic-gate#include <sys/cmn_err.h>
487c478bdstevel@tonic-gate#include <sys/sysmacros.h>
497c478bdstevel@tonic-gate#include <sys/privregs.h>
507c478bdstevel@tonic-gate#include <sys/schedctl.h>
517c478bdstevel@tonic-gate
527c478bdstevel@tonic-gate
537c478bdstevel@tonic-gate/*
547c478bdstevel@tonic-gate * Save user context.
557c478bdstevel@tonic-gate */
567c478bdstevel@tonic-gatevoid
57bdf0047Roger A. Faulknersavecontext(ucontext_t *ucp, const k_sigset_t *mask)
587c478bdstevel@tonic-gate{
597c478bdstevel@tonic-gate	proc_t *p = ttoproc(curthread);
607c478bdstevel@tonic-gate	klwp_t *lwp = ttolwp(curthread);
617c478bdstevel@tonic-gate
627c478bdstevel@tonic-gate	/*
637c478bdstevel@tonic-gate	 * We assign to every field through uc_mcontext.fpregs.fpu_en,
647c478bdstevel@tonic-gate	 * but we have to bzero() everything after that.
657c478bdstevel@tonic-gate	 */
667c478bdstevel@tonic-gate	bzero(&ucp->uc_mcontext.fpregs.fpu_en, sizeof (ucontext_t) -
677c478bdstevel@tonic-gate	    offsetof(ucontext_t, uc_mcontext.fpregs.fpu_en));
687c478bdstevel@tonic-gate	/*
697c478bdstevel@tonic-gate	 * There are unused holes in the ucontext_t structure, zero-fill
707c478bdstevel@tonic-gate	 * them so that we don't expose kernel data to the user.
717c478bdstevel@tonic-gate	 */
727c478bdstevel@tonic-gate	(&ucp->uc_flags)[1] = 0;
737c478bdstevel@tonic-gate	(&ucp->uc_stack.ss_flags)[1] = 0;
747c478bdstevel@tonic-gate
757c478bdstevel@tonic-gate	/*
767c478bdstevel@tonic-gate	 * Flushing the user windows isn't strictly necessary; we do
777c478bdstevel@tonic-gate	 * it to maintain backward compatibility.
787c478bdstevel@tonic-gate	 */
797c478bdstevel@tonic-gate	(void) flush_user_windows_to_stack(NULL);
807c478bdstevel@tonic-gate
817c478bdstevel@tonic-gate	ucp->uc_flags = UC_ALL;
827c478bdstevel@tonic-gate	ucp->uc_link = (ucontext_t *)lwp->lwp_oldcontext;
837c478bdstevel@tonic-gate
847c478bdstevel@tonic-gate	/*
857c478bdstevel@tonic-gate	 * Try to copyin() the ustack if one is registered. If the stack
867c478bdstevel@tonic-gate	 * has zero size, this indicates that stack bounds checking has
877c478bdstevel@tonic-gate	 * been disabled for this LWP. If stack bounds checking is disabled
887c478bdstevel@tonic-gate	 * or the copyin() fails, we fall back to the legacy behavior.
897c478bdstevel@tonic-gate	 */
901255103Toomas Soome	if (lwp->lwp_ustack == (uintptr_t)NULL ||
917c478bdstevel@tonic-gate	    copyin((void *)lwp->lwp_ustack, &ucp->uc_stack,
927c478bdstevel@tonic-gate	    sizeof (ucp->uc_stack)) != 0 ||
937c478bdstevel@tonic-gate	    ucp->uc_stack.ss_size == 0) {
947c478bdstevel@tonic-gate
957c478bdstevel@tonic-gate		if (lwp->lwp_sigaltstack.ss_flags == SS_ONSTACK) {
967c478bdstevel@tonic-gate			ucp->uc_stack = lwp->lwp_sigaltstack;
977c478bdstevel@tonic-gate		} else {
987c478bdstevel@tonic-gate			ucp->uc_stack.ss_sp = p->p_usrstack - p->p_stksize;
997c478bdstevel@tonic-gate			ucp->uc_stack.ss_size = p->p_stksize;
1007c478bdstevel@tonic-gate			ucp->uc_stack.ss_flags = 0;
1017c478bdstevel@tonic-gate		}
1027c478bdstevel@tonic-gate	}
1037c478bdstevel@tonic-gate
1047c478bdstevel@tonic-gate	getgregs(lwp, ucp->uc_mcontext.gregs);
1057c478bdstevel@tonic-gate	getasrs(lwp, ucp->uc_mcontext.asrs);
1067c478bdstevel@tonic-gate
1077c478bdstevel@tonic-gate	getfpregs(lwp, &ucp->uc_mcontext.fpregs);
1087c478bdstevel@tonic-gate	getfpasrs(lwp, ucp->uc_mcontext.asrs);
1097c478bdstevel@tonic-gate	if (ucp->uc_mcontext.fpregs.fpu_en == 0)
1107c478bdstevel@tonic-gate		ucp->uc_flags &= ~UC_FPU;
1117c478bdstevel@tonic-gate	ucp->uc_mcontext.gwins = (gwindows_t *)NULL;
1127c478bdstevel@tonic-gate
1137c478bdstevel@tonic-gate	/*
1147c478bdstevel@tonic-gate	 * Save signal mask.
1157c478bdstevel@tonic-gate	 */
116bdf0047Roger A. Faulkner	sigktou(mask, &ucp->uc_sigmask);
1177c478bdstevel@tonic-gate}
1187c478bdstevel@tonic-gate
1197c478bdstevel@tonic-gate
1207c478bdstevel@tonic-gatevoid
1217c478bdstevel@tonic-gaterestorecontext(ucontext_t *ucp)
1227c478bdstevel@tonic-gate{
1237c478bdstevel@tonic-gate	kthread_t *t = curthread;
1247c478bdstevel@tonic-gate	klwp_t *lwp = ttolwp(t);
1257c478bdstevel@tonic-gate	mcontext_t *mcp = &ucp->uc_mcontext;
1267c478bdstevel@tonic-gate	model_t model = lwp_getdatamodel(lwp);
1277c478bdstevel@tonic-gate
1287c478bdstevel@tonic-gate	(void) flush_user_windows_to_stack(NULL);
1297c478bdstevel@tonic-gate	if (lwp->lwp_pcb.pcb_xregstat != XREGNONE)
1307c478bdstevel@tonic-gate		xregrestore(lwp, 0);
1317c478bdstevel@tonic-gate
1327c478bdstevel@tonic-gate	lwp->lwp_oldcontext = (uintptr_t)ucp->uc_link;
1337c478bdstevel@tonic-gate
1347c478bdstevel@tonic-gate	if (ucp->uc_flags & UC_STACK) {
1357c478bdstevel@tonic-gate		if (ucp->uc_stack.ss_flags == SS_ONSTACK)
1367c478bdstevel@tonic-gate			lwp->lwp_sigaltstack = ucp->uc_stack;
1377c478bdstevel@tonic-gate		else
1387c478bdstevel@tonic-gate			lwp->lwp_sigaltstack.ss_flags &= ~SS_ONSTACK;
1397c478bdstevel@tonic-gate	}
1407c478bdstevel@tonic-gate
1417c478bdstevel@tonic-gate	if (ucp->uc_flags & UC_CPU) {
1427c478bdstevel@tonic-gate		if (mcp->gwins != 0)
1437c478bdstevel@tonic-gate			setgwins(lwp, mcp->gwins);
1447c478bdstevel@tonic-gate		setgregs(lwp, mcp->gregs);
1457c478bdstevel@tonic-gate		if (model == DATAMODEL_LP64)
1467c478bdstevel@tonic-gate			setasrs(lwp, mcp->asrs);
1477c478bdstevel@tonic-gate		else
1487c478bdstevel@tonic-gate			xregs_setgregs(lwp, xregs_getptr(lwp, ucp));
1497c478bdstevel@tonic-gate	}
1507c478bdstevel@tonic-gate
1517c478bdstevel@tonic-gate	if (ucp->uc_flags & UC_FPU) {
1527c478bdstevel@tonic-gate		fpregset_t *fp = &ucp->uc_mcontext.fpregs;
1537c478bdstevel@tonic-gate
1547c478bdstevel@tonic-gate		setfpregs(lwp, fp);
1557c478bdstevel@tonic-gate		if (model == DATAMODEL_LP64)
1567c478bdstevel@tonic-gate			setfpasrs(lwp, mcp->asrs);
1577c478bdstevel@tonic-gate		else
1587c478bdstevel@tonic-gate			xregs_setfpregs(lwp, xregs_getptr(lwp, ucp));
1597c478bdstevel@tonic-gate		run_fpq(lwp, fp);
1607c478bdstevel@tonic-gate	}
1617c478bdstevel@tonic-gate
1627c478bdstevel@tonic-gate	if (ucp->uc_flags & UC_SIGMASK) {
1637be238fRoger A. Faulkner		/*
1647be238fRoger A. Faulkner		 * We don't need to acquire p->p_lock here;
1657be238fRoger A. Faulkner		 * we are manipulating thread-private data.
1667be238fRoger A. Faulkner		 */
1677c478bdstevel@tonic-gate		schedctl_finish_sigblock(t);
1687c478bdstevel@tonic-gate		sigutok(&ucp->uc_sigmask, &t->t_hold);
1697be238fRoger A. Faulkner		if (sigcheck(ttoproc(t), t))
1707c478bdstevel@tonic-gate			t->t_sig_check = 1;
1717c478bdstevel@tonic-gate	}
1727c478bdstevel@tonic-gate}
1737c478bdstevel@tonic-gate
1747c478bdstevel@tonic-gate
1757c478bdstevel@tonic-gateint
1767c478bdstevel@tonic-gategetsetcontext(int flag, void *arg)
1777c478bdstevel@tonic-gate{
1787c478bdstevel@tonic-gate	ucontext_t uc;
179bc0e913Gordon Ross	struct _fq fpu_q[MAXFPQ]; /* to hold floating queue */
1807c478bdstevel@tonic-gate	fpregset_t *fpp;
1817c478bdstevel@tonic-gate	gwindows_t *gwin = NULL;	/* to hold windows */
1827c478bdstevel@tonic-gate	caddr_t xregs = NULL;
1837c478bdstevel@tonic-gate	int xregs_size = 0;
1847c478bdstevel@tonic-gate	extern int nwindows;
1857c478bdstevel@tonic-gate	ucontext_t *ucp;
1867c478bdstevel@tonic-gate	klwp_t *lwp = ttolwp(curthread);
1877c478bdstevel@tonic-gate	stack_t dummy_stk;
1887c478bdstevel@tonic-gate
1897c478bdstevel@tonic-gate	/*
1907c478bdstevel@tonic-gate	 * In future releases, when the ucontext structure grows,
1917c478bdstevel@tonic-gate	 * getcontext should be modified to only return the fields
1927c478bdstevel@tonic-gate	 * specified in the uc_flags.  That way, the structure can grow
1937c478bdstevel@tonic-gate	 * and still be binary compatible will all .o's which will only
1947c478bdstevel@tonic-gate	 * have old fields defined in uc_flags
1957c478bdstevel@tonic-gate	 */
1967c478bdstevel@tonic-gate
1977c478bdstevel@tonic-gate	switch (flag) {
1987c478bdstevel@tonic-gate	default:
1997c478bdstevel@tonic-gate		return (set_errno(EINVAL));
2007c478bdstevel@tonic-gate
2017c478bdstevel@tonic-gate	case GETCONTEXT:
2027be238fRoger A. Faulkner		schedctl_finish_sigblock(curthread);
203bdf0047Roger A. Faulkner		savecontext(&uc, &curthread->t_hold);
2048f798d3Roger A. Faulkner		if (uc.uc_flags & UC_SIGMASK)
2058f798d3Roger A. Faulkner			SIGSET_NATIVE_TO_BRAND(&uc.uc_sigmask);
2067c478bdstevel@tonic-gate		/*
2077c478bdstevel@tonic-gate		 * When using floating point it should not be possible to
2087c478bdstevel@tonic-gate		 * get here with a fpu_qcnt other than zero since we go
2097c478bdstevel@tonic-gate		 * to great pains to handle all outstanding FP exceptions
2107c478bdstevel@tonic-gate		 * before any system call code gets executed. However we
2117c478bdstevel@tonic-gate		 * clear fpu_q and fpu_qcnt here before copyout anyway -
2127c478bdstevel@tonic-gate		 * this will prevent us from interpreting the garbage we
2137c478bdstevel@tonic-gate		 * get back (when FP is not enabled) as valid queue data on
2147c478bdstevel@tonic-gate		 * a later setcontext(2).
2157c478bdstevel@tonic-gate		 */
2167c478bdstevel@tonic-gate		uc.uc_mcontext.fpregs.fpu_qcnt = 0;
217bc0e913Gordon Ross		uc.uc_mcontext.fpregs.fpu_q = (struct _fq *)NULL;
2187c478bdstevel@tonic-gate
2197c478bdstevel@tonic-gate		if (copyout(&uc, arg, sizeof (ucontext_t)))
2207c478bdstevel@tonic-gate			return (set_errno(EFAULT));
2217c478bdstevel@tonic-gate		return (0);
2227c478bdstevel@tonic-gate
2237c478bdstevel@tonic-gate	case SETCONTEXT:
2247c478bdstevel@tonic-gate		ucp = arg;
2257c478bdstevel@tonic-gate		if (ucp == NULL)
2267c478bdstevel@tonic-gate			exit(CLD_EXITED, 0);
2277c478bdstevel@tonic-gate		/*
2287c478bdstevel@tonic-gate		 * Don't copyin filler or floating state unless we need it.
2297c478bdstevel@tonic-gate		 * The ucontext_t struct and fields are specified in the ABI.
2307c478bdstevel@tonic-gate		 */
2317c478bdstevel@tonic-gate		if (copyin(ucp, &uc, sizeof (ucontext_t) -
2327c478bdstevel@tonic-gate		    sizeof (uc.uc_filler) -
2337c478bdstevel@tonic-gate		    sizeof (uc.uc_mcontext.fpregs) -
2347c478bdstevel@tonic-gate		    sizeof (uc.uc_mcontext.xrs) -
2357c478bdstevel@tonic-gate		    sizeof (uc.uc_mcontext.asrs) -
2367c478bdstevel@tonic-gate		    sizeof (uc.uc_mcontext.filler))) {
2377c478bdstevel@tonic-gate			return (set_errno(EFAULT));
2387c478bdstevel@tonic-gate		}
2398f798d3Roger A. Faulkner		if (uc.uc_flags & UC_SIGMASK)
2408f798d3Roger A. Faulkner			SIGSET_BRAND_TO_NATIVE(&uc.uc_sigmask);
2417c478bdstevel@tonic-gate		if (copyin(&ucp->uc_mcontext.xrs, &uc.uc_mcontext.xrs,
2427c478bdstevel@tonic-gate		    sizeof (uc.uc_mcontext.xrs))) {
2437c478bdstevel@tonic-gate			return (set_errno(EFAULT));
2447c478bdstevel@tonic-gate		}
2457c478bdstevel@tonic-gate		fpp = &uc.uc_mcontext.fpregs;
2467c478bdstevel@tonic-gate		if (uc.uc_flags & UC_FPU) {
2477c478bdstevel@tonic-gate			/*
2487c478bdstevel@tonic-gate			 * Need to copyin floating point state
2497c478bdstevel@tonic-gate			 */
2507c478bdstevel@tonic-gate			if (copyin(&ucp->uc_mcontext.fpregs,
2517c478bdstevel@tonic-gate			    &uc.uc_mcontext.fpregs,
2527c478bdstevel@tonic-gate			    sizeof (uc.uc_mcontext.fpregs)))
2537c478bdstevel@tonic-gate				return (set_errno(EFAULT));
2547c478bdstevel@tonic-gate			/* if floating queue not empty */
2557c478bdstevel@tonic-gate			if ((fpp->fpu_q) && (fpp->fpu_qcnt)) {
2567c478bdstevel@tonic-gate				if (fpp->fpu_qcnt > MAXFPQ ||
2577c478bdstevel@tonic-gate				    fpp->fpu_q_entrysize <= 0 ||
258bc0e913Gordon Ross				    fpp->fpu_q_entrysize > sizeof (struct _fq))
2597c478bdstevel@tonic-gate					return (set_errno(EINVAL));
2607c478bdstevel@tonic-gate				if (copyin(fpp->fpu_q, fpu_q,
2617c478bdstevel@tonic-gate				    fpp->fpu_qcnt * fpp->fpu_q_entrysize))
2627c478bdstevel@tonic-gate					return (set_errno(EFAULT));
2637c478bdstevel@tonic-gate				fpp->fpu_q = fpu_q;
2647c478bdstevel@tonic-gate			} else {
2657c478bdstevel@tonic-gate				fpp->fpu_qcnt = 0; /* avoid confusion later */
2667c478bdstevel@tonic-gate			}
2677c478bdstevel@tonic-gate		} else {
2687c478bdstevel@tonic-gate			fpp->fpu_qcnt = 0;
2697c478bdstevel@tonic-gate		}
2707c478bdstevel@tonic-gate		if (uc.uc_mcontext.gwins) {	/* if windows in context */
2717c478bdstevel@tonic-gate			size_t gwin_size;
2727c478bdstevel@tonic-gate
2737c478bdstevel@tonic-gate			/*
2747c478bdstevel@tonic-gate			 * We do the same computation here to determine
2757c478bdstevel@tonic-gate			 * how many bytes of gwindows_t to copy in that
2767c478bdstevel@tonic-gate			 * is also done in sendsig() to decide how many
2777c478bdstevel@tonic-gate			 * bytes to copy out.  We just *know* that wbcnt
2787c478bdstevel@tonic-gate			 * is the first element of the structure.
2797c478bdstevel@tonic-gate			 */
2807c478bdstevel@tonic-gate			gwin = kmem_zalloc(sizeof (gwindows_t), KM_SLEEP);
2817c478bdstevel@tonic-gate			if (copyin(uc.uc_mcontext.gwins,
2827c478bdstevel@tonic-gate			    &gwin->wbcnt, sizeof (gwin->wbcnt))) {
2837c478bdstevel@tonic-gate				kmem_free(gwin, sizeof (gwindows_t));
2847c478bdstevel@tonic-gate				return (set_errno(EFAULT));
2857c478bdstevel@tonic-gate			}
2867c478bdstevel@tonic-gate			if (gwin->wbcnt < 0 || gwin->wbcnt > nwindows) {
2877c478bdstevel@tonic-gate				kmem_free(gwin, sizeof (gwindows_t));
2887c478bdstevel@tonic-gate				return (set_errno(EINVAL));
2897c478bdstevel@tonic-gate			}
2907c478bdstevel@tonic-gate			gwin_size = gwin->wbcnt * sizeof (struct rwindow) +
2917c478bdstevel@tonic-gate			    SPARC_MAXREGWINDOW * sizeof (int *) + sizeof (long);
2927c478bdstevel@tonic-gate			if (gwin_size > sizeof (gwindows_t) ||
2937c478bdstevel@tonic-gate			    copyin(uc.uc_mcontext.gwins, gwin, gwin_size)) {
2947c478bdstevel@tonic-gate				kmem_free(gwin, sizeof (gwindows_t));
2957c478bdstevel@tonic-gate				return (set_errno(EFAULT));
2967c478bdstevel@tonic-gate			}
2977c478bdstevel@tonic-gate			uc.uc_mcontext.gwins = gwin;
2987c478bdstevel@tonic-gate		}
2997c478bdstevel@tonic-gate
3007c478bdstevel@tonic-gate		/*
3017c478bdstevel@tonic-gate		 * get extra register state or asrs if any exists
3027c478bdstevel@tonic-gate		 * there is no extra register state for _LP64 user programs
3037c478bdstevel@tonic-gate		 */
3047c478bdstevel@tonic-gate		xregs_clrptr(lwp, &uc);
3057c478bdstevel@tonic-gate		if (copyin(&ucp->uc_mcontext.asrs, &uc.uc_mcontext.asrs,
3067c478bdstevel@tonic-gate		    sizeof (asrset_t))) {
3077c478bdstevel@tonic-gate			/* Free up gwin structure if used */
3087c478bdstevel@tonic-gate			if (gwin)
3097c478bdstevel@tonic-gate				kmem_free(gwin, sizeof (gwindows_t));
3107c478bdstevel@tonic-gate			return (set_errno(EFAULT));
3117c478bdstevel@tonic-gate		}
3127c478bdstevel@tonic-gate
3137c478bdstevel@tonic-gate		restorecontext(&uc);
3147c478bdstevel@tonic-gate
3157c478bdstevel@tonic-gate		if ((uc.uc_flags & UC_STACK) && (lwp->lwp_ustack != 0)) {
3167c478bdstevel@tonic-gate			(void) copyout(&uc.uc_stack, (stack_t *)lwp->lwp_ustack,
3177c478bdstevel@tonic-gate			    sizeof (stack_t));
3187c478bdstevel@tonic-gate		}
3197c478bdstevel@tonic-gate
3207c478bdstevel@tonic-gate		/*
3217c478bdstevel@tonic-gate		 * free extra register state area
3227c478bdstevel@tonic-gate		 */
3237c478bdstevel@tonic-gate		if (xregs_size)
3247c478bdstevel@tonic-gate			kmem_free(xregs, xregs_size);
3257c478bdstevel@tonic-gate
3267c478bdstevel@tonic-gate		if (gwin)
3277c478bdstevel@tonic-gate			kmem_free(gwin, sizeof (gwindows_t));
3287c478bdstevel@tonic-gate
3297c478bdstevel@tonic-gate		return (0);
3307c478bdstevel@tonic-gate
3317c478bdstevel@tonic-gate	case GETUSTACK:
3327c478bdstevel@tonic-gate		if (copyout(&lwp->lwp_ustack, arg, sizeof (caddr_t)))
3337c478bdstevel@tonic-gate			return (set_errno(EFAULT));
3347c478bdstevel@tonic-gate
3357c478bdstevel@tonic-gate		return (0);
3367c478bdstevel@tonic-gate
3377c478bdstevel@tonic-gate	case SETUSTACK:
3387c478bdstevel@tonic-gate		if (copyin(arg, &dummy_stk, sizeof (dummy_stk)))
3397c478bdstevel@tonic-gate			return (set_errno(EFAULT));
3407c478bdstevel@tonic-gate
3417c478bdstevel@tonic-gate		lwp->lwp_ustack = (uintptr_t)arg;
3427c478bdstevel@tonic-gate
3437c478bdstevel@tonic-gate		return (0);
3447c478bdstevel@tonic-gate	}
3457c478bdstevel@tonic-gate}
3467c478bdstevel@tonic-gate
3477c478bdstevel@tonic-gate
3487c478bdstevel@tonic-gate#ifdef _SYSCALL32_IMPL
3497c478bdstevel@tonic-gate
3507c478bdstevel@tonic-gate/*
3517c478bdstevel@tonic-gate * Save user context for 32-bit processes.
3527c478bdstevel@tonic-gate */
3537c478bdstevel@tonic-gatevoid
354bdf0047Roger A. Faulknersavecontext32(ucontext32_t *ucp, const k_sigset_t *mask, struct fq32 *dfq)
3557c478bdstevel@tonic-gate{
3567c478bdstevel@tonic-gate	proc_t *p = ttoproc(curthread);
3577c478bdstevel@tonic-gate	klwp_t *lwp = ttolwp(curthread);
3587c478bdstevel@tonic-gate	fpregset_t fpregs;
3597c478bdstevel@tonic-gate
3607c478bdstevel@tonic-gate	/*
3617c478bdstevel@tonic-gate	 * We assign to every field through uc_mcontext.fpregs.fpu_en,
3627c478bdstevel@tonic-gate	 * but we have to bzero() everything after that.
3637c478bdstevel@tonic-gate	 */
3647c478bdstevel@tonic-gate	bzero(&ucp->uc_mcontext.fpregs.fpu_en, sizeof (ucontext32_t) -
3657c478bdstevel@tonic-gate	    offsetof(ucontext32_t, uc_mcontext.fpregs.fpu_en));
3667c478bdstevel@tonic-gate	/*
3677c478bdstevel@tonic-gate	 * There is an unused hole in the ucontext32_t structure; zero-fill
3687c478bdstevel@tonic-gate	 * it so that we don't expose kernel data to the user.
3697c478bdstevel@tonic-gate	 */
3707c478bdstevel@tonic-gate	(&ucp->uc_stack.ss_flags)[1] = 0;
3717c478bdstevel@tonic-gate
3727c478bdstevel@tonic-gate	/*
3737c478bdstevel@tonic-gate	 * Flushing the user windows isn't strictly necessary; we do
3747c478bdstevel@tonic-gate	 * it to maintain backward compatibility.
3757c478bdstevel@tonic-gate	 */
3767c478bdstevel@tonic-gate	(void) flush_user_windows_to_stack(NULL);
3777c478bdstevel@tonic-gate
3787c478bdstevel@tonic-gate	ucp->uc_flags = UC_ALL;
3797c478bdstevel@tonic-gate	ucp->uc_link = (caddr32_t)lwp->lwp_oldcontext;
3807c478bdstevel@tonic-gate
3817c478bdstevel@tonic-gate	/*
3827c478bdstevel@tonic-gate	 * Try to copyin() the ustack if one is registered. If the stack
3837c478bdstevel@tonic-gate	 * has zero size, this indicates that stack bounds checking has
3847c478bdstevel@tonic-gate	 * been disabled for this LWP. If stack bounds checking is disabled
3857c478bdstevel@tonic-gate	 * or the copyin() fails, we fall back to the legacy behavior.
3867c478bdstevel@tonic-gate	 */
3871255103Toomas Soome	if (lwp->lwp_ustack == (uintptr_t)NULL ||
3887c478bdstevel@tonic-gate	    copyin((void *)lwp->lwp_ustack, &ucp->uc_stack,
3897c478bdstevel@tonic-gate	    sizeof (ucp->uc_stack)) != 0 ||
3907c478bdstevel@tonic-gate	    ucp->uc_stack.ss_size == 0) {
3917c478bdstevel@tonic-gate
3927c478bdstevel@tonic-gate		if (lwp->lwp_sigaltstack.ss_flags == SS_ONSTACK) {
3937c478bdstevel@tonic-gate			ucp->uc_stack.ss_sp =
3947552190raf			    (caddr32_t)(uintptr_t)lwp->lwp_sigaltstack.ss_sp;
3957c478bdstevel@tonic-gate			ucp->uc_stack.ss_size =
3967c478bdstevel@tonic-gate			    (size32_t)lwp->lwp_sigaltstack.ss_size;
3977c478bdstevel@tonic-gate			ucp->uc_stack.ss_flags = SS_ONSTACK;
3987c478bdstevel@tonic-gate		} else {
3997c478bdstevel@tonic-gate			ucp->uc_stack.ss_sp =
4007552190raf			    (caddr32_t)(uintptr_t)p->p_usrstack - p->p_stksize;
4017c478bdstevel@tonic-gate			ucp->uc_stack.ss_size =
4027c478bdstevel@tonic-gate			    (size32_t)p->p_stksize;
4037c478bdstevel@tonic-gate			ucp->uc_stack.ss_flags = 0;
4047c478bdstevel@tonic-gate		}
4057c478bdstevel@tonic-gate	}
4067c478bdstevel@tonic-gate
4077c478bdstevel@tonic-gate	getgregs32(lwp, ucp->uc_mcontext.gregs);
4087c478bdstevel@tonic-gate	getfpregs(lwp, &fpregs);
4097c478bdstevel@tonic-gate	fpuregset_nto32(&fpregs, &ucp->uc_mcontext.fpregs, dfq);
4107c478bdstevel@tonic-gate
4117c478bdstevel@tonic-gate	if (ucp->uc_mcontext.fpregs.fpu_en == 0)
4127c478bdstevel@tonic-gate		ucp->uc_flags &= ~UC_FPU;
4131255103Toomas Soome	ucp->uc_mcontext.gwins = (caddr32_t)(uintptr_t)NULL;
4147c478bdstevel@tonic-gate
4157c478bdstevel@tonic-gate	/*
4167c478bdstevel@tonic-gate	 * Save signal mask (the 32- and 64-bit sigset_t structures are
4177c478bdstevel@tonic-gate	 * identical).
4187c478bdstevel@tonic-gate	 */
419bdf0047Roger A. Faulkner	sigktou(mask, (sigset_t *)&ucp->uc_sigmask);
4207c478bdstevel@tonic-gate}
4217c478bdstevel@tonic-gate
4227c478bdstevel@tonic-gateint
4237c478bdstevel@tonic-gategetsetcontext32(int flag, void *arg)
4247c478bdstevel@tonic-gate{
4257c478bdstevel@tonic-gate	ucontext32_t uc;
4267c478bdstevel@tonic-gate	ucontext_t   ucnat;
427bc0e913Gordon Ross	struct _fq fpu_qnat[MAXFPQ]; /* to hold "native" floating queue */
4287c478bdstevel@tonic-gate	struct fq32 fpu_q[MAXFPQ]; /* to hold 32 bit floating queue */
4297c478bdstevel@tonic-gate	fpregset32_t *fpp;
4307c478bdstevel@tonic-gate	gwindows32_t *gwin = NULL;	/* to hold windows */
4317c478bdstevel@tonic-gate	caddr_t xregs;
4327c478bdstevel@tonic-gate	int xregs_size = 0;
4337c478bdstevel@tonic-gate	extern int nwindows;
4347c478bdstevel@tonic-gate	klwp_t *lwp = ttolwp(curthread);
4357c478bdstevel@tonic-gate	ucontext32_t *ucp;
4367c478bdstevel@tonic-gate	uint32_t ustack32;
4377c478bdstevel@tonic-gate	stack32_t dummy_stk32;
4387c478bdstevel@tonic-gate
4397c478bdstevel@tonic-gate	/*
4407c478bdstevel@tonic-gate	 * In future releases, when the ucontext structure grows,
4417c478bdstevel@tonic-gate	 * getcontext should be modified to only return the fields
4427c478bdstevel@tonic-gate	 * specified in the uc_flags.  That way, the structure can grow
4437c478bdstevel@tonic-gate	 * and still be binary compatible will all .o's which will only
4447c478bdstevel@tonic-gate	 * have old fields defined in uc_flags
4457c478bdstevel@tonic-gate	 */
4467c478bdstevel@tonic-gate
4477c478bdstevel@tonic-gate	switch (flag) {
4487c478bdstevel@tonic-gate	default:
4497c478bdstevel@tonic-gate		return (set_errno(EINVAL));
4507c478bdstevel@tonic-gate
4517c478bdstevel@tonic-gate	case GETCONTEXT:
4527be238fRoger A. Faulkner		schedctl_finish_sigblock(curthread);
453bdf0047Roger A. Faulkner		savecontext32(&uc, &curthread->t_hold, NULL);
4548f798d3Roger A. Faulkner		if (uc.uc_flags & UC_SIGMASK)
4558f798d3Roger A. Faulkner			SIGSET_NATIVE_TO_BRAND(&uc.uc_sigmask);
4567c478bdstevel@tonic-gate		/*
4577c478bdstevel@tonic-gate		 * When using floating point it should not be possible to
4587c478bdstevel@tonic-gate		 * get here with a fpu_qcnt other than zero since we go
4597c478bdstevel@tonic-gate		 * to great pains to handle all outstanding FP exceptions
4607c478bdstevel@tonic-gate		 * before any system call code gets executed. However we
4617c478bdstevel@tonic-gate		 * clear fpu_q and fpu_qcnt here before copyout anyway -
4627c478bdstevel@tonic-gate		 * this will prevent us from interpreting the garbage we
4637c478bdstevel@tonic-gate		 * get back (when FP is not enabled) as valid queue data on
4647c478bdstevel@tonic-gate		 * a later setcontext(2).
4657c478bdstevel@tonic-gate		 */
4667c478bdstevel@tonic-gate		uc.uc_mcontext.fpregs.fpu_qcnt = 0;
4671255103Toomas Soome		uc.uc_mcontext.fpregs.fpu_q = (caddr32_t)(uintptr_t)NULL;
4687c478bdstevel@tonic-gate
4697c478bdstevel@tonic-gate		if (copyout(&uc, arg, sizeof (ucontext32_t)))
4707c478bdstevel@tonic-gate			return (set_errno(EFAULT));
4717c478bdstevel@tonic-gate		return (0);
4727c478bdstevel@tonic-gate
4737c478bdstevel@tonic-gate	case SETCONTEXT:
4747c478bdstevel@tonic-gate		ucp = arg;
4757c478bdstevel@tonic-gate		if (ucp == NULL)
4767c478bdstevel@tonic-gate			exit(CLD_EXITED, 0);
4777c478bdstevel@tonic-gate		/*
4787c478bdstevel@tonic-gate		 * Don't copyin filler or floating state unless we need it.
4797c478bdstevel@tonic-gate		 * The ucontext_t struct and fields are specified in the ABI.
4807c478bdstevel@tonic-gate		 */
4817c478bdstevel@tonic-gate		if (copyin(ucp, &uc, sizeof (uc) - sizeof (uc.uc_filler) -
4827c478bdstevel@tonic-gate		    sizeof (uc.uc_mcontext.fpregs) -
4837c478bdstevel@tonic-gate		    sizeof (uc.uc_mcontext.xrs) -
4847c478bdstevel@tonic-gate		    sizeof (uc.uc_mcontext.filler))) {
4857c478bdstevel@tonic-gate			return (set_errno(EFAULT));
4867c478bdstevel@tonic-gate		}
4878f798d3Roger A. Faulkner		if (uc.uc_flags & UC_SIGMASK)
4888f798d3Roger A. Faulkner			SIGSET_BRAND_TO_NATIVE(&uc.uc_sigmask);
4897c478bdstevel@tonic-gate		if (copyin(&ucp->uc_mcontext.xrs, &uc.uc_mcontext.xrs,
4907c478bdstevel@tonic-gate		    sizeof (uc.uc_mcontext.xrs))) {
4917c478bdstevel@tonic-gate			return (set_errno(EFAULT));
4927c478bdstevel@tonic-gate		}
4937c478bdstevel@tonic-gate		fpp = &uc.uc_mcontext.fpregs;
4947c478bdstevel@tonic-gate		if (uc.uc_flags & UC_FPU) {
4957c478bdstevel@tonic-gate			/*
4967c478bdstevel@tonic-gate			 * Need to copyin floating point state
4977c478bdstevel@tonic-gate			 */
4987c478bdstevel@tonic-gate			if (copyin(&ucp->uc_mcontext.fpregs,
4997c478bdstevel@tonic-gate			    &uc.uc_mcontext.fpregs,
5007c478bdstevel@tonic-gate			    sizeof (uc.uc_mcontext.fpregs)))
5017c478bdstevel@tonic-gate				return (set_errno(EFAULT));
5027c478bdstevel@tonic-gate			/* if floating queue not empty */
5037c478bdstevel@tonic-gate			if ((fpp->fpu_q) && (fpp->fpu_qcnt)) {
5047c478bdstevel@tonic-gate				if (fpp->fpu_qcnt > MAXFPQ ||
5057c478bdstevel@tonic-gate				    fpp->fpu_q_entrysize <= 0 ||
5067c478bdstevel@tonic-gate				    fpp->fpu_q_entrysize > sizeof (struct fq32))
5077c478bdstevel@tonic-gate					return (set_errno(EINVAL));
5087552190raf				if (copyin((void *)(uintptr_t)fpp->fpu_q, fpu_q,
5097c478bdstevel@tonic-gate				    fpp->fpu_qcnt * fpp->fpu_q_entrysize))
5107c478bdstevel@tonic-gate					return (set_errno(EFAULT));
5117c478bdstevel@tonic-gate			} else {
5127c478bdstevel@tonic-gate				fpp->fpu_qcnt = 0; /* avoid confusion later */
5137c478bdstevel@tonic-gate			}
5147c478bdstevel@tonic-gate		} else {
5157c478bdstevel@tonic-gate			fpp->fpu_qcnt = 0;
5167c478bdstevel@tonic-gate		}
5177c478bdstevel@tonic-gate
5187c478bdstevel@tonic-gate		if (uc.uc_mcontext.gwins) {	/* if windows in context */
5197c478bdstevel@tonic-gate			size_t gwin_size;
5207c478bdstevel@tonic-gate
5217c478bdstevel@tonic-gate			/*
5227c478bdstevel@tonic-gate			 * We do the same computation here to determine
5237c478bdstevel@tonic-gate			 * how many bytes of gwindows_t to copy in that
5247c478bdstevel@tonic-gate			 * is also done in sendsig() to decide how many
5257c478bdstevel@tonic-gate			 * bytes to copy out.  We just *know* that wbcnt
5267c478bdstevel@tonic-gate			 * is the first element of the structure.
5277c478bdstevel@tonic-gate			 */
5287be238fRoger A. Faulkner			gwin = kmem_zalloc(sizeof (gwindows32_t), KM_SLEEP);
5297552190raf			if (copyin((void *)(uintptr_t)uc.uc_mcontext.gwins,
5307c478bdstevel@tonic-gate			    &gwin->wbcnt, sizeof (gwin->wbcnt))) {
5317c478bdstevel@tonic-gate				kmem_free(gwin, sizeof (gwindows32_t));
5327c478bdstevel@tonic-gate				return (set_errno(EFAULT));
5337c478bdstevel@tonic-gate			}
5347c478bdstevel@tonic-gate			if (gwin->wbcnt < 0 || gwin->wbcnt > nwindows) {
5357c478bdstevel@tonic-gate				kmem_free(gwin, sizeof (gwindows32_t));
5367c478bdstevel@tonic-gate				return (set_errno(EINVAL));
5377c478bdstevel@tonic-gate			}
5387c478bdstevel@tonic-gate			gwin_size = gwin->wbcnt * sizeof (struct rwindow32) +
5397c478bdstevel@tonic-gate			    SPARC_MAXREGWINDOW * sizeof (caddr32_t) +
5407c478bdstevel@tonic-gate			    sizeof (int32_t);
5417c478bdstevel@tonic-gate			if (gwin_size > sizeof (gwindows32_t) ||
5427552190raf			    copyin((void *)(uintptr_t)uc.uc_mcontext.gwins,
5437c478bdstevel@tonic-gate			    gwin, gwin_size)) {
5447c478bdstevel@tonic-gate				kmem_free(gwin, sizeof (gwindows32_t));
5457c478bdstevel@tonic-gate				return (set_errno(EFAULT));
5467c478bdstevel@tonic-gate			}
5477c478bdstevel@tonic-gate			/* restorecontext() should ignore this */
5487c478bdstevel@tonic-gate			uc.uc_mcontext.gwins = (caddr32_t)0;
5497c478bdstevel@tonic-gate		}
5507c478bdstevel@tonic-gate
5517c478bdstevel@tonic-gate		ucontext_32ton(&uc, &ucnat, fpu_q, fpu_qnat);
5527c478bdstevel@tonic-gate
5537c478bdstevel@tonic-gate		/*
5547c478bdstevel@tonic-gate		 * get extra register state if any exists
5557c478bdstevel@tonic-gate		 */
5567c478bdstevel@tonic-gate		if (xregs_hasptr32(lwp, &uc) &&
5577c478bdstevel@tonic-gate		    ((xregs_size = xregs_getsize(curproc)) > 0)) {
5587c478bdstevel@tonic-gate			xregs = kmem_zalloc(xregs_size, KM_SLEEP);
5597552190raf			if (copyin((void *)(uintptr_t)xregs_getptr32(lwp, &uc),
5607c478bdstevel@tonic-gate			    xregs, xregs_size)) {
5617c478bdstevel@tonic-gate				kmem_free(xregs, xregs_size);
5627c478bdstevel@tonic-gate				if (gwin)
5637c478bdstevel@tonic-gate					kmem_free(gwin, sizeof (gwindows32_t));
5647c478bdstevel@tonic-gate				return (set_errno(EFAULT));
5657c478bdstevel@tonic-gate			}
5667c478bdstevel@tonic-gate			xregs_setptr(lwp, &ucnat, xregs);
5677c478bdstevel@tonic-gate		} else {
5687c478bdstevel@tonic-gate			xregs_clrptr(lwp, &ucnat);
5697c478bdstevel@tonic-gate		}
5707c478bdstevel@tonic-gate
5717c478bdstevel@tonic-gate		restorecontext(&ucnat);
5727c478bdstevel@tonic-gate
5737c478bdstevel@tonic-gate		if ((uc.uc_flags & UC_STACK) && (lwp->lwp_ustack != 0)) {
5747c478bdstevel@tonic-gate			(void) copyout(&uc.uc_stack,
5757c478bdstevel@tonic-gate			    (stack32_t *)lwp->lwp_ustack, sizeof (stack32_t));
5767c478bdstevel@tonic-gate		}
5777c478bdstevel@tonic-gate
5787c478bdstevel@tonic-gate		if (gwin)
5797c478bdstevel@tonic-gate			setgwins32(lwp, gwin);
5807c478bdstevel@tonic-gate
5817c478bdstevel@tonic-gate		/*
5827c478bdstevel@tonic-gate		 * free extra register state area
5837c478bdstevel@tonic-gate		 */
5847c478bdstevel@tonic-gate		if (xregs_size)
5857c478bdstevel@tonic-gate			kmem_free(xregs, xregs_size);
5867c478bdstevel@tonic-gate
5877c478bdstevel@tonic-gate		if (gwin)
5887c478bdstevel@tonic-gate			kmem_free(gwin, sizeof (gwindows32_t));
5897c478bdstevel@tonic-gate
5907c478bdstevel@tonic-gate		return (0);
5917c478bdstevel@tonic-gate
5927c478bdstevel@tonic-gate	case GETUSTACK:
5937c478bdstevel@tonic-gate		ustack32 = (uint32_t)lwp->lwp_ustack;
5947c478bdstevel@tonic-gate		if (copyout(&ustack32, arg, sizeof (caddr32_t)))
5957c478bdstevel@tonic-gate			return (set_errno(EFAULT));
5967c478bdstevel@tonic-gate
5977c478bdstevel@tonic-gate		return (0);
5987c478bdstevel@tonic-gate
5997c478bdstevel@tonic-gate	case SETUSTACK:
6007c478bdstevel@tonic-gate		if (copyin(arg, &dummy_stk32, sizeof (dummy_stk32)))
6017c478bdstevel@tonic-gate			return (set_errno(EFAULT));
6027c478bdstevel@tonic-gate
6037c478bdstevel@tonic-gate		lwp->lwp_ustack = (uintptr_t)arg;
6047c478bdstevel@tonic-gate
6057c478bdstevel@tonic-gate		return (0);
6067c478bdstevel@tonic-gate	}
6077c478bdstevel@tonic-gate}
6087c478bdstevel@tonic-gate
6097c478bdstevel@tonic-gate#endif	/* _SYSCALL32_IMPL */
610