getcontext.c revision 10341:cb88cd4df985
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28/*	  All Rights Reserved  	*/
29
30#include <sys/param.h>
31#include <sys/types.h>
32#include <sys/vmparam.h>
33#include <sys/systm.h>
34#include <sys/signal.h>
35#include <sys/stack.h>
36#include <sys/regset.h>
37#include <sys/privregs.h>
38#include <sys/frame.h>
39#include <sys/proc.h>
40#include <sys/psw.h>
41#include <sys/ucontext.h>
42#include <sys/asm_linkage.h>
43#include <sys/errno.h>
44#include <sys/archsystm.h>
45#include <sys/schedctl.h>
46#include <sys/debug.h>
47#include <sys/sysmacros.h>
48
49/*
50 * Save user context.
51 */
52void
53savecontext(ucontext_t *ucp, k_sigset_t mask)
54{
55	proc_t *p = ttoproc(curthread);
56	klwp_t *lwp = ttolwp(curthread);
57	struct regs *rp = lwptoregs(lwp);
58
59	/*
60	 * We unconditionally assign to every field through the end
61	 * of the gregs, but we need to bzero() everything -after- that
62	 * to avoid having any kernel stack garbage escape to userland.
63	 */
64	bzero(&ucp->uc_mcontext.fpregs, sizeof (ucontext_t) -
65	    offsetof(ucontext_t, uc_mcontext.fpregs));
66
67	ucp->uc_flags = UC_ALL;
68	ucp->uc_link = (struct ucontext *)lwp->lwp_oldcontext;
69
70	/*
71	 * Try to copyin() the ustack if one is registered. If the stack
72	 * has zero size, this indicates that stack bounds checking has
73	 * been disabled for this LWP. If stack bounds checking is disabled
74	 * or the copyin() fails, we fall back to the legacy behavior.
75	 */
76	if (lwp->lwp_ustack == NULL ||
77	    copyin((void *)lwp->lwp_ustack, &ucp->uc_stack,
78	    sizeof (ucp->uc_stack)) != 0 ||
79	    ucp->uc_stack.ss_size == 0) {
80
81		if (lwp->lwp_sigaltstack.ss_flags == SS_ONSTACK) {
82			ucp->uc_stack = lwp->lwp_sigaltstack;
83		} else {
84			ucp->uc_stack.ss_sp = p->p_usrstack - p->p_stksize;
85			ucp->uc_stack.ss_size = p->p_stksize;
86			ucp->uc_stack.ss_flags = 0;
87		}
88	}
89
90	/*
91	 * If either the trace flag or REQUEST_STEP is set,
92	 * arrange for single-stepping and turn off the trace flag.
93	 */
94	if ((rp->r_ps & PS_T) || (lwp->lwp_pcb.pcb_flags & REQUEST_STEP)) {
95		/*
96		 * Clear PS_T so that saved user context won't have trace
97		 * flag set.
98		 */
99		rp->r_ps &= ~PS_T;
100
101		if (!(lwp->lwp_pcb.pcb_flags & REQUEST_NOSTEP)) {
102			lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING;
103			/*
104			 * trap() always checks DEBUG_PENDING before
105			 * checking for any pending signal. This at times
106			 * can potentially lead to DEBUG_PENDING not being
107			 * honoured. (for eg: the lwp is stopped by
108			 * stop_on_fault() called from trap(), after being
109			 * awakened it might see a pending signal and call
110			 * savecontext(), however on the way back to userland
111			 * there is no place it can be detected). Hence in
112			 * anticipation of such occassions, set AST flag for
113			 * the thread which will make the thread take an
114			 * excursion through trap() where it will be handled
115			 * appropriately.
116			 */
117			aston(curthread);
118		}
119	}
120
121	getgregs(lwp, ucp->uc_mcontext.gregs);
122	if (lwp->lwp_pcb.pcb_fpu.fpu_flags & FPU_EN)
123		getfpregs(lwp, &ucp->uc_mcontext.fpregs);
124	else
125		ucp->uc_flags &= ~UC_FPU;
126
127	sigktou(&mask, &ucp->uc_sigmask);
128}
129
130/*
131 * Restore user context.
132 */
133void
134restorecontext(ucontext_t *ucp)
135{
136	kthread_t *t = curthread;
137	klwp_t *lwp = ttolwp(t);
138
139	lwp->lwp_oldcontext = (uintptr_t)ucp->uc_link;
140
141	if (ucp->uc_flags & UC_STACK) {
142		if (ucp->uc_stack.ss_flags == SS_ONSTACK)
143			lwp->lwp_sigaltstack = ucp->uc_stack;
144		else
145			lwp->lwp_sigaltstack.ss_flags &= ~SS_ONSTACK;
146	}
147
148	if (ucp->uc_flags & UC_CPU) {
149		/*
150		 * If the trace flag is set, mark the lwp to take a
151		 * single-step trap on return to user level (below).
152		 * The x86 lcall interface and sysenter has already done this,
153		 * and turned off the flag, but amd64 syscall interface has not.
154		 */
155		if (lwptoregs(lwp)->r_ps & PS_T)
156			lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING;
157		setgregs(lwp, ucp->uc_mcontext.gregs);
158		lwp->lwp_eosys = JUSTRETURN;
159		t->t_post_sys = 1;
160		aston(curthread);
161	}
162
163	if (ucp->uc_flags & UC_FPU)
164		setfpregs(lwp, &ucp->uc_mcontext.fpregs);
165
166	if (ucp->uc_flags & UC_SIGMASK) {
167		/*
168		 * We don't need to acquire p->p_lock here;
169		 * we are manipulating thread-private data.
170		 */
171		schedctl_finish_sigblock(t);
172		sigutok(&ucp->uc_sigmask, &t->t_hold);
173		if (sigcheck(ttoproc(t), t))
174			t->t_sig_check = 1;
175	}
176}
177
178
179int
180getsetcontext(int flag, void *arg)
181{
182	ucontext_t uc;
183	ucontext_t *ucp;
184	klwp_t *lwp = ttolwp(curthread);
185	stack_t dummy_stk;
186
187	/*
188	 * In future releases, when the ucontext structure grows,
189	 * getcontext should be modified to only return the fields
190	 * specified in the uc_flags.  That way, the structure can grow
191	 * and still be binary compatible will all .o's which will only
192	 * have old fields defined in uc_flags
193	 */
194
195	switch (flag) {
196	default:
197		return (set_errno(EINVAL));
198
199	case GETCONTEXT:
200		schedctl_finish_sigblock(curthread);
201		savecontext(&uc, curthread->t_hold);
202		if (copyout(&uc, arg, sizeof (uc)))
203			return (set_errno(EFAULT));
204		return (0);
205
206	case SETCONTEXT:
207		ucp = arg;
208		if (ucp == NULL)
209			exit(CLD_EXITED, 0);
210		/*
211		 * Don't copyin filler or floating state unless we need it.
212		 * The ucontext_t struct and fields are specified in the ABI.
213		 */
214		if (copyin(ucp, &uc, sizeof (ucontext_t) -
215		    sizeof (uc.uc_filler) -
216		    sizeof (uc.uc_mcontext.fpregs))) {
217			return (set_errno(EFAULT));
218		}
219
220		if ((uc.uc_flags & UC_FPU) &&
221		    copyin(&ucp->uc_mcontext.fpregs, &uc.uc_mcontext.fpregs,
222		    sizeof (uc.uc_mcontext.fpregs))) {
223			return (set_errno(EFAULT));
224		}
225
226		restorecontext(&uc);
227
228		if ((uc.uc_flags & UC_STACK) && (lwp->lwp_ustack != 0))
229			(void) copyout(&uc.uc_stack, (stack_t *)lwp->lwp_ustack,
230			    sizeof (uc.uc_stack));
231		return (0);
232
233	case GETUSTACK:
234		if (copyout(&lwp->lwp_ustack, arg, sizeof (caddr_t)))
235			return (set_errno(EFAULT));
236		return (0);
237
238	case SETUSTACK:
239		if (copyin(arg, &dummy_stk, sizeof (dummy_stk)))
240			return (set_errno(EFAULT));
241		lwp->lwp_ustack = (uintptr_t)arg;
242		return (0);
243	}
244}
245
246#ifdef _SYSCALL32_IMPL
247
248/*
249 * Save user context for 32-bit processes.
250 */
251void
252savecontext32(ucontext32_t *ucp, k_sigset_t mask)
253{
254	proc_t *p = ttoproc(curthread);
255	klwp_t *lwp = ttolwp(curthread);
256	struct regs *rp = lwptoregs(lwp);
257
258	bzero(&ucp->uc_mcontext.fpregs, sizeof (ucontext32_t) -
259	    offsetof(ucontext32_t, uc_mcontext.fpregs));
260
261	ucp->uc_flags = UC_ALL;
262	ucp->uc_link = (caddr32_t)lwp->lwp_oldcontext;
263
264	if (lwp->lwp_ustack == NULL ||
265	    copyin((void *)lwp->lwp_ustack, &ucp->uc_stack,
266	    sizeof (ucp->uc_stack)) != 0 ||
267	    ucp->uc_stack.ss_size == 0) {
268
269		if (lwp->lwp_sigaltstack.ss_flags == SS_ONSTACK) {
270			ucp->uc_stack.ss_sp =
271			    (caddr32_t)(uintptr_t)lwp->lwp_sigaltstack.ss_sp;
272			ucp->uc_stack.ss_size =
273			    (size32_t)lwp->lwp_sigaltstack.ss_size;
274			ucp->uc_stack.ss_flags = SS_ONSTACK;
275		} else {
276			ucp->uc_stack.ss_sp = (caddr32_t)(uintptr_t)
277			    (p->p_usrstack - p->p_stksize);
278			ucp->uc_stack.ss_size = (size32_t)p->p_stksize;
279			ucp->uc_stack.ss_flags = 0;
280		}
281	}
282
283	/*
284	 * If either the trace flag or REQUEST_STEP is set, arrange
285	 * for single-stepping and turn off the trace flag.
286	 */
287	if ((rp->r_ps & PS_T) || (lwp->lwp_pcb.pcb_flags & REQUEST_STEP)) {
288		/*
289		 * Clear PS_T so that saved user context won't have trace
290		 * flag set.
291		 */
292		rp->r_ps &= ~PS_T;
293
294		if (!(lwp->lwp_pcb.pcb_flags & REQUEST_NOSTEP)) {
295			lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING;
296			/*
297			 * See comments in savecontext().
298			 */
299			aston(curthread);
300		}
301	}
302
303	getgregs32(lwp, ucp->uc_mcontext.gregs);
304	if (lwp->lwp_pcb.pcb_fpu.fpu_flags & FPU_EN)
305		getfpregs32(lwp, &ucp->uc_mcontext.fpregs);
306	else
307		ucp->uc_flags &= ~UC_FPU;
308
309	sigktou(&mask, &ucp->uc_sigmask);
310}
311
312int
313getsetcontext32(int flag, void *arg)
314{
315	ucontext32_t uc;
316	ucontext_t ucnat;
317	ucontext32_t *ucp;
318	klwp_t *lwp = ttolwp(curthread);
319	caddr32_t ustack32;
320	stack32_t dummy_stk32;
321
322	switch (flag) {
323	default:
324		return (set_errno(EINVAL));
325
326	case GETCONTEXT:
327		schedctl_finish_sigblock(curthread);
328		savecontext32(&uc, curthread->t_hold);
329		if (copyout(&uc, arg, sizeof (uc)))
330			return (set_errno(EFAULT));
331		return (0);
332
333	case SETCONTEXT:
334		ucp = arg;
335		if (ucp == NULL)
336			exit(CLD_EXITED, 0);
337		if (copyin(ucp, &uc, sizeof (uc) -
338		    sizeof (uc.uc_filler) -
339		    sizeof (uc.uc_mcontext.fpregs))) {
340			return (set_errno(EFAULT));
341		}
342		if ((uc.uc_flags & UC_FPU) &&
343		    copyin(&ucp->uc_mcontext.fpregs, &uc.uc_mcontext.fpregs,
344		    sizeof (uc.uc_mcontext.fpregs))) {
345			return (set_errno(EFAULT));
346		}
347
348		ucontext_32ton(&uc, &ucnat);
349		restorecontext(&ucnat);
350
351		if ((uc.uc_flags & UC_STACK) && (lwp->lwp_ustack != 0))
352			(void) copyout(&uc.uc_stack,
353			    (stack32_t *)lwp->lwp_ustack, sizeof (uc.uc_stack));
354		return (0);
355
356	case GETUSTACK:
357		ustack32 = (caddr32_t)lwp->lwp_ustack;
358		if (copyout(&ustack32, arg, sizeof (ustack32)))
359			return (set_errno(EFAULT));
360		return (0);
361
362	case SETUSTACK:
363		if (copyin(arg, &dummy_stk32, sizeof (dummy_stk32)))
364			return (set_errno(EFAULT));
365		lwp->lwp_ustack = (uintptr_t)arg;
366		return (0);
367	}
368}
369
370#endif	/* _SYSCALL32_IMPL */
371