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*1829Ssudheer  * Common Development and Distribution License (the "License").
6*1829Ssudheer  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
220Sstevel@tonic-gate /*	  All Rights Reserved  	*/
230Sstevel@tonic-gate 
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
26*1829Ssudheer  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
270Sstevel@tonic-gate  * Use is subject to license terms.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <sys/param.h>
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/vmparam.h>
350Sstevel@tonic-gate #include <sys/systm.h>
360Sstevel@tonic-gate #include <sys/signal.h>
370Sstevel@tonic-gate #include <sys/stack.h>
380Sstevel@tonic-gate #include <sys/regset.h>
390Sstevel@tonic-gate #include <sys/privregs.h>
400Sstevel@tonic-gate #include <sys/frame.h>
410Sstevel@tonic-gate #include <sys/proc.h>
420Sstevel@tonic-gate #include <sys/psw.h>
430Sstevel@tonic-gate #include <sys/ucontext.h>
440Sstevel@tonic-gate #include <sys/asm_linkage.h>
450Sstevel@tonic-gate #include <sys/errno.h>
460Sstevel@tonic-gate #include <sys/archsystm.h>
470Sstevel@tonic-gate #include <sys/schedctl.h>
480Sstevel@tonic-gate #include <sys/debug.h>
490Sstevel@tonic-gate #include <sys/sysmacros.h>
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate  * Save user context.
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate void
550Sstevel@tonic-gate savecontext(ucontext_t *ucp, k_sigset_t mask)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
580Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
59*1829Ssudheer 	struct regs *rp = lwptoregs(lwp);
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	/*
620Sstevel@tonic-gate 	 * We unconditionally assign to every field through the end
630Sstevel@tonic-gate 	 * of the gregs, but we need to bzero() everything -after- that
640Sstevel@tonic-gate 	 * to avoid having any kernel stack garbage escape to userland.
650Sstevel@tonic-gate 	 */
660Sstevel@tonic-gate 	bzero(&ucp->uc_mcontext.fpregs, sizeof (ucontext_t) -
670Sstevel@tonic-gate 	    offsetof(ucontext_t, uc_mcontext.fpregs));
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	ucp->uc_flags = UC_ALL;
700Sstevel@tonic-gate 	ucp->uc_link = (struct ucontext *)lwp->lwp_oldcontext;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	/*
730Sstevel@tonic-gate 	 * Try to copyin() the ustack if one is registered. If the stack
740Sstevel@tonic-gate 	 * has zero size, this indicates that stack bounds checking has
750Sstevel@tonic-gate 	 * been disabled for this LWP. If stack bounds checking is disabled
760Sstevel@tonic-gate 	 * or the copyin() fails, we fall back to the legacy behavior.
770Sstevel@tonic-gate 	 */
780Sstevel@tonic-gate 	if (lwp->lwp_ustack == NULL ||
790Sstevel@tonic-gate 	    copyin((void *)lwp->lwp_ustack, &ucp->uc_stack,
800Sstevel@tonic-gate 	    sizeof (ucp->uc_stack)) != 0 ||
810Sstevel@tonic-gate 	    ucp->uc_stack.ss_size == 0) {
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 		if (lwp->lwp_sigaltstack.ss_flags == SS_ONSTACK) {
840Sstevel@tonic-gate 			ucp->uc_stack = lwp->lwp_sigaltstack;
850Sstevel@tonic-gate 		} else {
860Sstevel@tonic-gate 			ucp->uc_stack.ss_sp = p->p_usrstack - p->p_stksize;
870Sstevel@tonic-gate 			ucp->uc_stack.ss_size = p->p_stksize;
880Sstevel@tonic-gate 			ucp->uc_stack.ss_flags = 0;
890Sstevel@tonic-gate 		}
900Sstevel@tonic-gate 	}
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	getgregs(lwp, ucp->uc_mcontext.gregs);
930Sstevel@tonic-gate 	if (lwp->lwp_pcb.pcb_fpu.fpu_flags & FPU_EN)
940Sstevel@tonic-gate 		getfpregs(lwp, &ucp->uc_mcontext.fpregs);
950Sstevel@tonic-gate 	else
960Sstevel@tonic-gate 		ucp->uc_flags &= ~UC_FPU;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	sigktou(&mask, &ucp->uc_sigmask);
99*1829Ssudheer 	/*
100*1829Ssudheer 	 * If the trace flag is set, arrange for single-stepping and
101*1829Ssudheer 	 * turn off the trace flag.
102*1829Ssudheer 	 */
103*1829Ssudheer 	if (rp->r_ps & PS_T) {
104*1829Ssudheer 		lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING;
105*1829Ssudheer 		rp->r_ps &= ~PS_T;
106*1829Ssudheer 	}
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate  * Restore user context.
1110Sstevel@tonic-gate  */
1120Sstevel@tonic-gate void
1130Sstevel@tonic-gate restorecontext(ucontext_t *ucp)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate 	kthread_t *t = curthread;
1160Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	lwp->lwp_oldcontext = (uintptr_t)ucp->uc_link;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	if (ucp->uc_flags & UC_STACK) {
1210Sstevel@tonic-gate 		if (ucp->uc_stack.ss_flags == SS_ONSTACK)
1220Sstevel@tonic-gate 			lwp->lwp_sigaltstack = ucp->uc_stack;
1230Sstevel@tonic-gate 		else
1240Sstevel@tonic-gate 			lwp->lwp_sigaltstack.ss_flags &= ~SS_ONSTACK;
1250Sstevel@tonic-gate 	}
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	if (ucp->uc_flags & UC_CPU) {
128*1829Ssudheer 		/*
129*1829Ssudheer 		 * If the trace flag is set, mark the lwp to take a
130*1829Ssudheer 		 * single-step trap on return to user level (below).
131*1829Ssudheer 		 * The x86 lcall interface and sysenter has already done this,
132*1829Ssudheer 		 * and turned off the flag, but amd64 syscall interface has not.
133*1829Ssudheer 		 */
134*1829Ssudheer 		if (lwptoregs(lwp)->r_ps & PS_T)
135*1829Ssudheer 			lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING;
1360Sstevel@tonic-gate 		setgregs(lwp, ucp->uc_mcontext.gregs);
1370Sstevel@tonic-gate 		lwp->lwp_eosys = JUSTRETURN;
1380Sstevel@tonic-gate 		t->t_post_sys = 1;
1390Sstevel@tonic-gate 	}
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	if (ucp->uc_flags & UC_FPU)
1420Sstevel@tonic-gate 		setfpregs(lwp, &ucp->uc_mcontext.fpregs);
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	if (ucp->uc_flags & UC_SIGMASK) {
1450Sstevel@tonic-gate 		proc_t *p = ttoproc(t);
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
1480Sstevel@tonic-gate 		schedctl_finish_sigblock(t);
1490Sstevel@tonic-gate 		sigutok(&ucp->uc_sigmask, &t->t_hold);
1500Sstevel@tonic-gate 		if (sigcheck(p, t))
1510Sstevel@tonic-gate 			t->t_sig_check = 1;
1520Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
1530Sstevel@tonic-gate 	}
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate int
1580Sstevel@tonic-gate getsetcontext(int flag, void *arg)
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate 	ucontext_t uc;
1610Sstevel@tonic-gate 	ucontext_t *ucp;
1620Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
1630Sstevel@tonic-gate 	stack_t dummy_stk;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	/*
1660Sstevel@tonic-gate 	 * In future releases, when the ucontext structure grows,
1670Sstevel@tonic-gate 	 * getcontext should be modified to only return the fields
1680Sstevel@tonic-gate 	 * specified in the uc_flags.  That way, the structure can grow
1690Sstevel@tonic-gate 	 * and still be binary compatible will all .o's which will only
1700Sstevel@tonic-gate 	 * have old fields defined in uc_flags
1710Sstevel@tonic-gate 	 */
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	switch (flag) {
1740Sstevel@tonic-gate 	default:
1750Sstevel@tonic-gate 		return (set_errno(EINVAL));
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	case GETCONTEXT:
1780Sstevel@tonic-gate 		if (schedctl_sigblock(curthread)) {
1790Sstevel@tonic-gate 			proc_t *p = ttoproc(curthread);
1800Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
1810Sstevel@tonic-gate 			schedctl_finish_sigblock(curthread);
1820Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
1830Sstevel@tonic-gate 		}
1840Sstevel@tonic-gate 		savecontext(&uc, curthread->t_hold);
1850Sstevel@tonic-gate 		if (copyout(&uc, arg, sizeof (uc)))
1860Sstevel@tonic-gate 			return (set_errno(EFAULT));
1870Sstevel@tonic-gate 		return (0);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	case SETCONTEXT:
1900Sstevel@tonic-gate 		ucp = arg;
1910Sstevel@tonic-gate 		if (ucp == NULL)
1920Sstevel@tonic-gate 			exit(CLD_EXITED, 0);
1930Sstevel@tonic-gate 		/*
1940Sstevel@tonic-gate 		 * Don't copyin filler or floating state unless we need it.
1950Sstevel@tonic-gate 		 * The ucontext_t struct and fields are specified in the ABI.
1960Sstevel@tonic-gate 		 */
1970Sstevel@tonic-gate 		if (copyin(ucp, &uc, sizeof (ucontext_t) -
1980Sstevel@tonic-gate 		    sizeof (uc.uc_filler) -
1990Sstevel@tonic-gate 		    sizeof (uc.uc_mcontext.fpregs))) {
2000Sstevel@tonic-gate 			return (set_errno(EFAULT));
2010Sstevel@tonic-gate 		}
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 		if ((uc.uc_flags & UC_FPU) &&
2040Sstevel@tonic-gate 		    copyin(&ucp->uc_mcontext.fpregs, &uc.uc_mcontext.fpregs,
2050Sstevel@tonic-gate 		    sizeof (uc.uc_mcontext.fpregs))) {
2060Sstevel@tonic-gate 			return (set_errno(EFAULT));
2070Sstevel@tonic-gate 		}
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 		restorecontext(&uc);
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 		if ((uc.uc_flags & UC_STACK) && (lwp->lwp_ustack != 0))
2120Sstevel@tonic-gate 			(void) copyout(&uc.uc_stack, (stack_t *)lwp->lwp_ustack,
2130Sstevel@tonic-gate 			    sizeof (uc.uc_stack));
2140Sstevel@tonic-gate 		return (0);
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	case GETUSTACK:
2170Sstevel@tonic-gate 		if (copyout(&lwp->lwp_ustack, arg, sizeof (caddr_t)))
2180Sstevel@tonic-gate 			return (set_errno(EFAULT));
2190Sstevel@tonic-gate 		return (0);
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	case SETUSTACK:
2220Sstevel@tonic-gate 		if (copyin(arg, &dummy_stk, sizeof (dummy_stk)))
2230Sstevel@tonic-gate 			return (set_errno(EFAULT));
2240Sstevel@tonic-gate 		lwp->lwp_ustack = (uintptr_t)arg;
2250Sstevel@tonic-gate 		return (0);
2260Sstevel@tonic-gate 	}
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate /*
2320Sstevel@tonic-gate  * Save user context for 32-bit processes.
2330Sstevel@tonic-gate  */
2340Sstevel@tonic-gate void
2350Sstevel@tonic-gate savecontext32(ucontext32_t *ucp, k_sigset_t mask)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
2380Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
239*1829Ssudheer 	struct regs *rp = lwptoregs(lwp);
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	bzero(&ucp->uc_mcontext.fpregs, sizeof (ucontext32_t) -
2420Sstevel@tonic-gate 	    offsetof(ucontext32_t, uc_mcontext.fpregs));
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	ucp->uc_flags = UC_ALL;
2450Sstevel@tonic-gate 	ucp->uc_link = (caddr32_t)lwp->lwp_oldcontext;
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	if (lwp->lwp_ustack == NULL ||
2480Sstevel@tonic-gate 	    copyin((void *)lwp->lwp_ustack, &ucp->uc_stack,
2490Sstevel@tonic-gate 	    sizeof (ucp->uc_stack)) != 0 ||
2500Sstevel@tonic-gate 	    ucp->uc_stack.ss_size == 0) {
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 		if (lwp->lwp_sigaltstack.ss_flags == SS_ONSTACK) {
2530Sstevel@tonic-gate 			ucp->uc_stack.ss_sp =
2540Sstevel@tonic-gate 			    (caddr32_t)(uintptr_t)lwp->lwp_sigaltstack.ss_sp;
2550Sstevel@tonic-gate 			ucp->uc_stack.ss_size =
2560Sstevel@tonic-gate 			    (size32_t)lwp->lwp_sigaltstack.ss_size;
2570Sstevel@tonic-gate 			ucp->uc_stack.ss_flags = SS_ONSTACK;
2580Sstevel@tonic-gate 		} else {
2590Sstevel@tonic-gate 			ucp->uc_stack.ss_sp = (caddr32_t)(uintptr_t)
2600Sstevel@tonic-gate 			    (p->p_usrstack - p->p_stksize);
2610Sstevel@tonic-gate 			ucp->uc_stack.ss_size = (size32_t)p->p_stksize;
2620Sstevel@tonic-gate 			ucp->uc_stack.ss_flags = 0;
2630Sstevel@tonic-gate 		}
2640Sstevel@tonic-gate 	}
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	getgregs32(lwp, ucp->uc_mcontext.gregs);
2670Sstevel@tonic-gate 	if (lwp->lwp_pcb.pcb_fpu.fpu_flags & FPU_EN)
2680Sstevel@tonic-gate 		getfpregs32(lwp, &ucp->uc_mcontext.fpregs);
2690Sstevel@tonic-gate 	else
2700Sstevel@tonic-gate 		ucp->uc_flags &= ~UC_FPU;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	sigktou(&mask, &ucp->uc_sigmask);
273*1829Ssudheer 	/*
274*1829Ssudheer 	 * If the trace flag is set, arrange for single-stepping and
275*1829Ssudheer 	 * turn off the trace flag.
276*1829Ssudheer 	 */
277*1829Ssudheer 	if (rp->r_ps & PS_T) {
278*1829Ssudheer 		lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING;
279*1829Ssudheer 		rp->r_ps &= ~PS_T;
280*1829Ssudheer 	}
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate int
2840Sstevel@tonic-gate getsetcontext32(int flag, void *arg)
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate 	ucontext32_t uc;
2870Sstevel@tonic-gate 	ucontext_t ucnat;
2880Sstevel@tonic-gate 	ucontext32_t *ucp;
2890Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
2900Sstevel@tonic-gate 	caddr32_t ustack32;
2910Sstevel@tonic-gate 	stack32_t dummy_stk32;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	switch (flag) {
2940Sstevel@tonic-gate 	default:
2950Sstevel@tonic-gate 		return (set_errno(EINVAL));
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	case GETCONTEXT:
2980Sstevel@tonic-gate 		if (schedctl_sigblock(curthread)) {
2990Sstevel@tonic-gate 			proc_t *p = ttoproc(curthread);
3000Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
3010Sstevel@tonic-gate 			schedctl_finish_sigblock(curthread);
3020Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
3030Sstevel@tonic-gate 		}
3040Sstevel@tonic-gate 		savecontext32(&uc, curthread->t_hold);
3050Sstevel@tonic-gate 		if (copyout(&uc, arg, sizeof (uc)))
3060Sstevel@tonic-gate 			return (set_errno(EFAULT));
3070Sstevel@tonic-gate 		return (0);
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	case SETCONTEXT:
3100Sstevel@tonic-gate 		ucp = arg;
3110Sstevel@tonic-gate 		if (ucp == NULL)
3120Sstevel@tonic-gate 			exit(CLD_EXITED, 0);
3130Sstevel@tonic-gate 		if (copyin(ucp, &uc, sizeof (uc) -
3140Sstevel@tonic-gate 		    sizeof (uc.uc_filler) -
3150Sstevel@tonic-gate 		    sizeof (uc.uc_mcontext.fpregs))) {
3160Sstevel@tonic-gate 			return (set_errno(EFAULT));
3170Sstevel@tonic-gate 		}
3180Sstevel@tonic-gate 		if ((uc.uc_flags & UC_FPU) &&
3190Sstevel@tonic-gate 		    copyin(&ucp->uc_mcontext.fpregs, &uc.uc_mcontext.fpregs,
3200Sstevel@tonic-gate 		    sizeof (uc.uc_mcontext.fpregs))) {
3210Sstevel@tonic-gate 			return (set_errno(EFAULT));
3220Sstevel@tonic-gate 		}
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 		ucontext_32ton(&uc, &ucnat);
3250Sstevel@tonic-gate 		restorecontext(&ucnat);
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 		if ((uc.uc_flags & UC_STACK) && (lwp->lwp_ustack != 0))
3280Sstevel@tonic-gate 			(void) copyout(&uc.uc_stack,
3290Sstevel@tonic-gate 			    (stack32_t *)lwp->lwp_ustack, sizeof (uc.uc_stack));
3300Sstevel@tonic-gate 		return (0);
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	case GETUSTACK:
3330Sstevel@tonic-gate 		ustack32 = (caddr32_t)lwp->lwp_ustack;
3340Sstevel@tonic-gate 		if (copyout(&ustack32, arg, sizeof (ustack32)))
3350Sstevel@tonic-gate 			return (set_errno(EFAULT));
3360Sstevel@tonic-gate 		return (0);
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	case SETUSTACK:
3390Sstevel@tonic-gate 		if (copyin(arg, &dummy_stk32, sizeof (dummy_stk32)))
3400Sstevel@tonic-gate 			return (set_errno(EFAULT));
3410Sstevel@tonic-gate 		lwp->lwp_ustack = (uintptr_t)arg;
3420Sstevel@tonic-gate 		return (0);
3430Sstevel@tonic-gate 	}
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
347