ia32_signal.c revision 151316
1203954Srdivacky/*-
2203954Srdivacky * Copyright (c) 2003 Peter Wemm
3203954Srdivacky * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
4203954Srdivacky * All rights reserved.
5203954Srdivacky *
6203954Srdivacky * This code is derived from software contributed to Berkeley by
7203954Srdivacky * William Jolitz.
8203954Srdivacky *
9203954Srdivacky * Redistribution and use in source and binary forms, with or without
10203954Srdivacky * modification, are permitted provided that the following conditions
11203954Srdivacky * are met:
12203954Srdivacky * 1. Redistributions of source code must retain the above copyright
13203954Srdivacky *    notice, this list of conditions and the following disclaimer.
14203954Srdivacky * 2. Redistributions in binary form must reproduce the above copyright
15203954Srdivacky *    notice, this list of conditions and the following disclaimer in the
16203954Srdivacky *    documentation and/or other materials provided with the distribution.
17203954Srdivacky * 4. Neither the name of the University nor the names of its contributors
18203954Srdivacky *    may be used to endorse or promote products derived from this software
19203954Srdivacky *    without specific prior written permission.
20203954Srdivacky *
21203954Srdivacky * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22203954Srdivacky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23203954Srdivacky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24203954Srdivacky * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25203954Srdivacky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26203954Srdivacky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27203954Srdivacky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28203954Srdivacky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29203954Srdivacky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30203954Srdivacky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31203954Srdivacky * SUCH DAMAGE.
32203954Srdivacky */
33203954Srdivacky
34203954Srdivacky#include <sys/cdefs.h>
35203954Srdivacky__FBSDID("$FreeBSD: head/sys/amd64/ia32/ia32_signal.c 151316 2005-10-14 12:43:47Z davidxu $");
36212904Sdim
37203954Srdivacky#include "opt_compat.h"
38203954Srdivacky
39203954Srdivacky#include <sys/param.h>
40203954Srdivacky#include <sys/exec.h>
41203954Srdivacky#include <sys/fcntl.h>
42203954Srdivacky#include <sys/imgact.h>
43203954Srdivacky#include <sys/kernel.h>
44203954Srdivacky#include <sys/lock.h>
45203954Srdivacky#include <sys/malloc.h>
46203954Srdivacky#include <sys/mutex.h>
47203954Srdivacky#include <sys/mman.h>
48203954Srdivacky#include <sys/namei.h>
49203954Srdivacky#include <sys/pioctl.h>
50203954Srdivacky#include <sys/proc.h>
51203954Srdivacky#include <sys/procfs.h>
52203954Srdivacky#include <sys/resourcevar.h>
53203954Srdivacky#include <sys/systm.h>
54203954Srdivacky#include <sys/signalvar.h>
55203954Srdivacky#include <sys/stat.h>
56203954Srdivacky#include <sys/sx.h>
57212904Sdim#include <sys/syscall.h>
58212904Sdim#include <sys/sysctl.h>
59203954Srdivacky#include <sys/sysent.h>
60203954Srdivacky#include <sys/vnode.h>
61203954Srdivacky
62203954Srdivacky#include <vm/vm.h>
63203954Srdivacky#include <vm/vm_kern.h>
64203954Srdivacky#include <vm/vm_param.h>
65203954Srdivacky#include <vm/pmap.h>
66203954Srdivacky#include <vm/vm_map.h>
67203954Srdivacky#include <vm/vm_object.h>
68203954Srdivacky#include <vm/vm_extern.h>
69203954Srdivacky
70203954Srdivacky#include <compat/freebsd32/freebsd32_util.h>
71203954Srdivacky#include <compat/freebsd32/freebsd32_proto.h>
72203954Srdivacky#include <compat/ia32/ia32_signal.h>
73203954Srdivacky#include <machine/psl.h>
74203954Srdivacky#include <machine/segments.h>
75203954Srdivacky#include <machine/specialreg.h>
76203954Srdivacky#include <machine/frame.h>
77203954Srdivacky#include <machine/md_var.h>
78203954Srdivacky#include <machine/pcb.h>
79203954Srdivacky#include <machine/cpufunc.h>
80203954Srdivacky
81203954Srdivacky#ifdef COMPAT_FREEBSD4
82203954Srdivackystatic void freebsd4_ia32_sendsig(sig_t, ksiginfo_t *, sigset_t *);
83203954Srdivacky#endif
84203954Srdivackystatic void ia32_get_fpcontext(struct thread *td, struct ia32_mcontext *mcp);
85203954Srdivackystatic int ia32_set_fpcontext(struct thread *td, const struct ia32_mcontext *mcp);
86203954Srdivacky
87203954Srdivackyextern int _ucode32sel, _udatasel;
88203954Srdivacky
89203954Srdivacky#define	CS_SECURE(cs)		(ISPL(cs) == SEL_UPL)
90203954Srdivacky#define	EFL_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
91203954Srdivacky
92203954Srdivackystatic void
93203954Srdivackyia32_get_fpcontext(struct thread *td, struct ia32_mcontext *mcp)
94203954Srdivacky{
95203954Srdivacky
96203954Srdivacky	mcp->mc_ownedfp = fpugetregs(td, (struct savefpu *)&mcp->mc_fpstate);
97203954Srdivacky	mcp->mc_fpformat = fpuformat();
98203954Srdivacky}
99203954Srdivacky
100203954Srdivackystatic int
101203954Srdivackyia32_set_fpcontext(struct thread *td, const struct ia32_mcontext *mcp)
102203954Srdivacky{
103203954Srdivacky
104212904Sdim	if (mcp->mc_fpformat == _MC_FPFMT_NODEV)
105212904Sdim		return (0);
106212904Sdim	else if (mcp->mc_fpformat != _MC_FPFMT_XMM)
107212904Sdim		return (EINVAL);
108210299Sed	else if (mcp->mc_ownedfp == _MC_FPOWNED_NONE)
109203954Srdivacky		/* We don't care what state is left in the FPU or PCB. */
110203954Srdivacky		fpstate_drop(td);
111203954Srdivacky	else if (mcp->mc_ownedfp == _MC_FPOWNED_FPU ||
112203954Srdivacky	    mcp->mc_ownedfp == _MC_FPOWNED_PCB) {
113203954Srdivacky		/*
114203954Srdivacky		 * XXX we violate the dubious requirement that fpusetregs()
115203954Srdivacky		 * be called with interrupts disabled.
116203954Srdivacky		 */
117203954Srdivacky		fpusetregs(td, (struct savefpu *)&mcp->mc_fpstate);
118203954Srdivacky	} else
119203954Srdivacky		return (EINVAL);
120203954Srdivacky	return (0);
121203954Srdivacky}
122203954Srdivacky
123203954Srdivacky/*
124203954Srdivacky * Get machine context.
125203954Srdivacky */
126203954Srdivackystatic int
127203954Srdivackyia32_get_mcontext(struct thread *td, struct ia32_mcontext *mcp, int flags)
128203954Srdivacky{
129203954Srdivacky	struct trapframe *tp;
130203954Srdivacky
131203954Srdivacky	tp = td->td_frame;
132203954Srdivacky
133203954Srdivacky	PROC_LOCK(curthread->td_proc);
134203954Srdivacky	mcp->mc_onstack = sigonstack(tp->tf_rsp);
135203954Srdivacky	PROC_UNLOCK(curthread->td_proc);
136203954Srdivacky	mcp->mc_gs = td->td_pcb->pcb_gs;
137203954Srdivacky	mcp->mc_fs = td->td_pcb->pcb_fs;
138203954Srdivacky	mcp->mc_es = td->td_pcb->pcb_es;
139203954Srdivacky	mcp->mc_ds = td->td_pcb->pcb_ds;
140203954Srdivacky	mcp->mc_edi = tp->tf_rdi;
141203954Srdivacky	mcp->mc_esi = tp->tf_rsi;
142203954Srdivacky	mcp->mc_ebp = tp->tf_rbp;
143203954Srdivacky	mcp->mc_isp = tp->tf_rsp;
144203954Srdivacky	if (flags & GET_MC_CLEAR_RET) {
145203954Srdivacky		mcp->mc_eax = 0;
146203954Srdivacky		mcp->mc_edx = 0;
147203954Srdivacky	} else {
148203954Srdivacky		mcp->mc_eax = tp->tf_rax;
149203954Srdivacky		mcp->mc_edx = tp->tf_rdx;
150203954Srdivacky	}
151203954Srdivacky	mcp->mc_ebx = tp->tf_rbx;
152203954Srdivacky	mcp->mc_ecx = tp->tf_rcx;
153203954Srdivacky	mcp->mc_eip = tp->tf_rip;
154203954Srdivacky	mcp->mc_cs = tp->tf_cs;
155203954Srdivacky	mcp->mc_eflags = tp->tf_rflags;
156203954Srdivacky	mcp->mc_esp = tp->tf_rsp;
157203954Srdivacky	mcp->mc_ss = tp->tf_ss;
158203954Srdivacky	mcp->mc_len = sizeof(*mcp);
159203954Srdivacky	ia32_get_fpcontext(td, mcp);
160203954Srdivacky	return (0);
161203954Srdivacky}
162203954Srdivacky
163203954Srdivacky/*
164203954Srdivacky * Set machine context.
165203954Srdivacky *
166203954Srdivacky * However, we don't set any but the user modifiable flags, and we won't
167203954Srdivacky * touch the cs selector.
168203954Srdivacky */
169203954Srdivackystatic int
170203954Srdivackyia32_set_mcontext(struct thread *td, const struct ia32_mcontext *mcp)
171203954Srdivacky{
172203954Srdivacky	struct trapframe *tp;
173203954Srdivacky	long rflags;
174203954Srdivacky	int ret;
175203954Srdivacky
176203954Srdivacky	tp = td->td_frame;
177203954Srdivacky	if (mcp->mc_len != sizeof(*mcp))
178203954Srdivacky		return (EINVAL);
179203954Srdivacky	rflags = (mcp->mc_eflags & PSL_USERCHANGE) |
180203954Srdivacky	    (tp->tf_rflags & ~PSL_USERCHANGE);
181203954Srdivacky	ret = ia32_set_fpcontext(td, mcp);
182203954Srdivacky	if (ret != 0)
183203954Srdivacky		return (ret);
184203954Srdivacky#if 0	/* XXX deal with load_fs() and friends */
185203954Srdivacky	tp->tf_fs = mcp->mc_fs;
186203954Srdivacky	tp->tf_es = mcp->mc_es;
187203954Srdivacky	tp->tf_ds = mcp->mc_ds;
188203954Srdivacky#endif
189	tp->tf_rdi = mcp->mc_edi;
190	tp->tf_rsi = mcp->mc_esi;
191	tp->tf_rbp = mcp->mc_ebp;
192	tp->tf_rbx = mcp->mc_ebx;
193	tp->tf_rdx = mcp->mc_edx;
194	tp->tf_rcx = mcp->mc_ecx;
195	tp->tf_rax = mcp->mc_eax;
196	/* trapno, err */
197	tp->tf_rip = mcp->mc_eip;
198	tp->tf_rflags = rflags;
199	tp->tf_rsp = mcp->mc_esp;
200	tp->tf_ss = mcp->mc_ss;
201#if 0	/* XXX deal with load_gs() and friends */
202	td->td_pcb->pcb_gs = mcp->mc_gs;
203#endif
204	td->td_pcb->pcb_flags |= PCB_FULLCTX;
205	return (0);
206}
207
208/*
209 * The first two fields of a ucontext_t are the signal mask and
210 * the machine context.  The next field is uc_link; we want to
211 * avoid destroying the link when copying out contexts.
212 */
213#define	UC_COPY_SIZE	offsetof(struct ia32_ucontext, uc_link)
214
215int
216freebsd32_getcontext(struct thread *td, struct freebsd32_getcontext_args *uap)
217{
218	struct ia32_ucontext uc;
219	int ret;
220
221	if (uap->ucp == NULL)
222		ret = EINVAL;
223	else {
224		ia32_get_mcontext(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
225		PROC_LOCK(td->td_proc);
226		uc.uc_sigmask = td->td_sigmask;
227		PROC_UNLOCK(td->td_proc);
228		ret = copyout(&uc, uap->ucp, UC_COPY_SIZE);
229	}
230	return (ret);
231}
232
233int
234freebsd32_setcontext(struct thread *td, struct freebsd32_setcontext_args *uap)
235{
236	struct ia32_ucontext uc;
237	int ret;
238
239	if (uap->ucp == NULL)
240		ret = EINVAL;
241	else {
242		ret = copyin(uap->ucp, &uc, UC_COPY_SIZE);
243		if (ret == 0) {
244			ret = ia32_set_mcontext(td, &uc.uc_mcontext);
245			if (ret == 0) {
246				SIG_CANTMASK(uc.uc_sigmask);
247				PROC_LOCK(td->td_proc);
248				td->td_sigmask = uc.uc_sigmask;
249				PROC_UNLOCK(td->td_proc);
250			}
251		}
252	}
253	return (ret == 0 ? EJUSTRETURN : ret);
254}
255
256int
257freebsd32_swapcontext(struct thread *td, struct freebsd32_swapcontext_args *uap)
258{
259	struct ia32_ucontext uc;
260	int ret;
261
262	if (uap->oucp == NULL || uap->ucp == NULL)
263		ret = EINVAL;
264	else {
265		ia32_get_mcontext(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
266		PROC_LOCK(td->td_proc);
267		uc.uc_sigmask = td->td_sigmask;
268		PROC_UNLOCK(td->td_proc);
269		ret = copyout(&uc, uap->oucp, UC_COPY_SIZE);
270		if (ret == 0) {
271			ret = copyin(uap->ucp, &uc, UC_COPY_SIZE);
272			if (ret == 0) {
273				ret = ia32_set_mcontext(td, &uc.uc_mcontext);
274				if (ret == 0) {
275					SIG_CANTMASK(uc.uc_sigmask);
276					PROC_LOCK(td->td_proc);
277					td->td_sigmask = uc.uc_sigmask;
278					PROC_UNLOCK(td->td_proc);
279				}
280			}
281		}
282	}
283	return (ret == 0 ? EJUSTRETURN : ret);
284}
285
286/*
287 * Send an interrupt to process.
288 *
289 * Stack is set up to allow sigcode stored
290 * at top to call routine, followed by kcall
291 * to sigreturn routine below.  After sigreturn
292 * resets the signal mask, the stack, and the
293 * frame pointer, it returns to the user
294 * specified pc, psl.
295 */
296#ifdef COMPAT_FREEBSD4
297static void
298freebsd4_ia32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
299{
300	struct ia32_sigframe4 sf, *sfp;
301	struct ia32_siginfo siginfo;
302	struct proc *p;
303	struct thread *td;
304	struct sigacts *psp;
305	struct trapframe *regs;
306	int oonstack;
307	int sig;
308
309	td = curthread;
310	p = td->td_proc;
311	siginfo_to_ia32siginfo(&ksi->ksi_info, &siginfo);
312
313	PROC_LOCK_ASSERT(p, MA_OWNED);
314	sig = siginfo.si_signo;
315	psp = p->p_sigacts;
316	mtx_assert(&psp->ps_mtx, MA_OWNED);
317	regs = td->td_frame;
318	oonstack = sigonstack(regs->tf_rsp);
319
320	/* Save user context. */
321	bzero(&sf, sizeof(sf));
322	sf.sf_uc.uc_sigmask = *mask;
323	sf.sf_uc.uc_stack.ss_sp = (uintptr_t)td->td_sigstk.ss_sp;
324	sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
325	sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
326	    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
327	sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
328	sf.sf_uc.uc_mcontext.mc_gs = rgs();
329	sf.sf_uc.uc_mcontext.mc_fs = rfs();
330	__asm __volatile("movl %%es,%0" : "=rm" (sf.sf_uc.uc_mcontext.mc_es));
331	__asm __volatile("movl %%ds,%0" : "=rm" (sf.sf_uc.uc_mcontext.mc_ds));
332	sf.sf_uc.uc_mcontext.mc_edi = regs->tf_rdi;
333	sf.sf_uc.uc_mcontext.mc_esi = regs->tf_rsi;
334	sf.sf_uc.uc_mcontext.mc_ebp = regs->tf_rbp;
335	sf.sf_uc.uc_mcontext.mc_isp = regs->tf_rsp; /* XXX */
336	sf.sf_uc.uc_mcontext.mc_ebx = regs->tf_rbx;
337	sf.sf_uc.uc_mcontext.mc_edx = regs->tf_rdx;
338	sf.sf_uc.uc_mcontext.mc_ecx = regs->tf_rcx;
339	sf.sf_uc.uc_mcontext.mc_eax = regs->tf_rax;
340	sf.sf_uc.uc_mcontext.mc_trapno = regs->tf_trapno;
341	sf.sf_uc.uc_mcontext.mc_err = regs->tf_err;
342	sf.sf_uc.uc_mcontext.mc_eip = regs->tf_rip;
343	sf.sf_uc.uc_mcontext.mc_cs = regs->tf_cs;
344	sf.sf_uc.uc_mcontext.mc_eflags = regs->tf_rflags;
345	sf.sf_uc.uc_mcontext.mc_esp = regs->tf_rsp;
346	sf.sf_uc.uc_mcontext.mc_ss = regs->tf_ss;
347
348	/* Allocate space for the signal handler context. */
349	if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
350	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
351		sfp = (struct ia32_sigframe4 *)(td->td_sigstk.ss_sp +
352		    td->td_sigstk.ss_size - sizeof(sf));
353	} else
354		sfp = (struct ia32_sigframe4 *)regs->tf_rsp - 1;
355	PROC_UNLOCK(p);
356
357	/* Translate the signal if appropriate. */
358	if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
359		sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
360
361	/* Build the argument list for the signal handler. */
362	sf.sf_signum = sig;
363	sf.sf_ucontext = (register_t)&sfp->sf_uc;
364	if (SIGISMEMBER(psp->ps_siginfo, sig)) {
365		/* Signal handler installed with SA_SIGINFO. */
366		sf.sf_siginfo = (u_int32_t)(uintptr_t)&sfp->sf_si;
367		sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
368
369		/* Fill in POSIX parts */
370		sf.sf_si = siginfo;
371		sf.sf_si.si_signo = sig;
372	} else {
373		/* Old FreeBSD-style arguments. */
374		sf.sf_siginfo = siginfo.si_code;
375		sf.sf_addr = (u_int32_t)siginfo.si_addr;
376		sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
377	}
378	mtx_unlock(&psp->ps_mtx);
379
380	/*
381	 * Copy the sigframe out to the user's stack.
382	 */
383	if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
384#ifdef DEBUG
385		printf("process %ld has trashed its stack\n", (long)p->p_pid);
386#endif
387		PROC_LOCK(p);
388		sigexit(td, SIGILL);
389	}
390
391	regs->tf_rsp = (uintptr_t)sfp;
392	regs->tf_rip = FREEBSD32_PS_STRINGS - sz_freebsd4_ia32_sigcode;
393	regs->tf_rflags &= ~PSL_T;
394	regs->tf_cs = _ucode32sel;
395	regs->tf_ss = _udatasel;
396	load_ds(_udatasel);
397	td->td_pcb->pcb_ds = _udatasel;
398	load_es(_udatasel);
399	td->td_pcb->pcb_es = _udatasel;
400	/* leave user %fs and %gs untouched */
401	PROC_LOCK(p);
402	mtx_lock(&psp->ps_mtx);
403}
404#endif	/* COMPAT_FREEBSD4 */
405
406void
407ia32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
408{
409	struct ia32_sigframe sf, *sfp;
410	struct ia32_siginfo siginfo;
411	struct proc *p;
412	struct thread *td;
413	struct sigacts *psp;
414	char *sp;
415	struct trapframe *regs;
416	int oonstack;
417	int sig;
418
419	siginfo_to_ia32siginfo(&ksi->ksi_info, &siginfo);
420	td = curthread;
421	p = td->td_proc;
422	PROC_LOCK_ASSERT(p, MA_OWNED);
423	sig = siginfo.si_signo;
424	psp = p->p_sigacts;
425#ifdef COMPAT_FREEBSD4
426	if (SIGISMEMBER(psp->ps_freebsd4, sig)) {
427		freebsd4_ia32_sendsig(catcher, ksi, mask);
428		return;
429	}
430#endif
431	mtx_assert(&psp->ps_mtx, MA_OWNED);
432	regs = td->td_frame;
433	oonstack = sigonstack(regs->tf_rsp);
434
435	/* Save user context. */
436	bzero(&sf, sizeof(sf));
437	sf.sf_uc.uc_sigmask = *mask;
438	sf.sf_uc.uc_stack.ss_sp = (uintptr_t)td->td_sigstk.ss_sp;
439	sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
440	sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
441	    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
442	sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
443	sf.sf_uc.uc_mcontext.mc_gs = rgs();
444	sf.sf_uc.uc_mcontext.mc_fs = rfs();
445	__asm __volatile("movl %%es,%0" : "=rm" (sf.sf_uc.uc_mcontext.mc_es));
446	__asm __volatile("movl %%ds,%0" : "=rm" (sf.sf_uc.uc_mcontext.mc_ds));
447	sf.sf_uc.uc_mcontext.mc_edi = regs->tf_rdi;
448	sf.sf_uc.uc_mcontext.mc_esi = regs->tf_rsi;
449	sf.sf_uc.uc_mcontext.mc_ebp = regs->tf_rbp;
450	sf.sf_uc.uc_mcontext.mc_isp = regs->tf_rsp; /* XXX */
451	sf.sf_uc.uc_mcontext.mc_ebx = regs->tf_rbx;
452	sf.sf_uc.uc_mcontext.mc_edx = regs->tf_rdx;
453	sf.sf_uc.uc_mcontext.mc_ecx = regs->tf_rcx;
454	sf.sf_uc.uc_mcontext.mc_eax = regs->tf_rax;
455	sf.sf_uc.uc_mcontext.mc_trapno = regs->tf_trapno;
456	sf.sf_uc.uc_mcontext.mc_err = regs->tf_err;
457	sf.sf_uc.uc_mcontext.mc_eip = regs->tf_rip;
458	sf.sf_uc.uc_mcontext.mc_cs = regs->tf_cs;
459	sf.sf_uc.uc_mcontext.mc_eflags = regs->tf_rflags;
460	sf.sf_uc.uc_mcontext.mc_esp = regs->tf_rsp;
461	sf.sf_uc.uc_mcontext.mc_ss = regs->tf_ss;
462	sf.sf_uc.uc_mcontext.mc_len = sizeof(sf.sf_uc.uc_mcontext); /* magic */
463	ia32_get_fpcontext(td, &sf.sf_uc.uc_mcontext);
464	fpstate_drop(td);
465
466	/* Allocate space for the signal handler context. */
467	if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
468	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
469		sp = td->td_sigstk.ss_sp +
470		    td->td_sigstk.ss_size - sizeof(sf);
471	} else
472		sp = (char *)regs->tf_rsp - sizeof(sf);
473	/* Align to 16 bytes. */
474	sfp = (struct ia32_sigframe *)((uintptr_t)sp & ~0xF);
475	PROC_UNLOCK(p);
476
477	/* Translate the signal if appropriate. */
478	if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
479		sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
480
481	/* Build the argument list for the signal handler. */
482	sf.sf_signum = sig;
483	sf.sf_ucontext = (register_t)&sfp->sf_uc;
484	if (SIGISMEMBER(psp->ps_siginfo, sig)) {
485		/* Signal handler installed with SA_SIGINFO. */
486		sf.sf_siginfo = (u_int32_t)(uintptr_t)&sfp->sf_si;
487		sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
488
489		/* Fill in POSIX parts */
490		sf.sf_si = siginfo;
491		sf.sf_si.si_signo = sig;
492	} else {
493		/* Old FreeBSD-style arguments. */
494		sf.sf_siginfo = siginfo.si_code;
495		sf.sf_addr = (u_int32_t)siginfo.si_addr;
496		sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
497	}
498	mtx_unlock(&psp->ps_mtx);
499
500	/*
501	 * Copy the sigframe out to the user's stack.
502	 */
503	if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
504#ifdef DEBUG
505		printf("process %ld has trashed its stack\n", (long)p->p_pid);
506#endif
507		PROC_LOCK(p);
508		sigexit(td, SIGILL);
509	}
510
511	regs->tf_rsp = (uintptr_t)sfp;
512	regs->tf_rip = FREEBSD32_PS_STRINGS - *(p->p_sysent->sv_szsigcode);
513	regs->tf_rflags &= ~PSL_T;
514	regs->tf_cs = _ucode32sel;
515	regs->tf_ss = _udatasel;
516	load_ds(_udatasel);
517	td->td_pcb->pcb_ds = _udatasel;
518	load_es(_udatasel);
519	td->td_pcb->pcb_es = _udatasel;
520	/* leave user %fs and %gs untouched */
521	PROC_LOCK(p);
522	mtx_lock(&psp->ps_mtx);
523}
524
525/*
526 * System call to cleanup state after a signal
527 * has been taken.  Reset signal mask and
528 * stack state from context left by sendsig (above).
529 * Return to previous pc and psl as specified by
530 * context left by sendsig. Check carefully to
531 * make sure that the user has not modified the
532 * state to gain improper privileges.
533 */
534#ifdef COMPAT_FREEBSD4
535/*
536 * MPSAFE
537 */
538int
539freebsd4_freebsd32_sigreturn(td, uap)
540	struct thread *td;
541	struct freebsd4_freebsd32_sigreturn_args /* {
542		const struct freebsd4_freebsd32_ucontext *sigcntxp;
543	} */ *uap;
544{
545	struct ia32_ucontext4 uc;
546	struct proc *p = td->td_proc;
547	struct trapframe *regs;
548	const struct ia32_ucontext4 *ucp;
549	int cs, eflags, error;
550	ksiginfo_t ksi;
551
552	error = copyin(uap->sigcntxp, &uc, sizeof(uc));
553	if (error != 0)
554		return (error);
555	ucp = &uc;
556	regs = td->td_frame;
557	eflags = ucp->uc_mcontext.mc_eflags;
558	/*
559	 * Don't allow users to change privileged or reserved flags.
560	 */
561	/*
562	 * XXX do allow users to change the privileged flag PSL_RF.
563	 * The cpu sets PSL_RF in tf_eflags for faults.  Debuggers
564	 * should sometimes set it there too.  tf_eflags is kept in
565	 * the signal context during signal handling and there is no
566	 * other place to remember it, so the PSL_RF bit may be
567	 * corrupted by the signal handler without us knowing.
568	 * Corruption of the PSL_RF bit at worst causes one more or
569	 * one less debugger trap, so allowing it is fairly harmless.
570	 */
571	if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) {
572		printf("freebsd4_freebsd32_sigreturn: eflags = 0x%x\n", eflags);
573		return (EINVAL);
574	}
575
576	/*
577	 * Don't allow users to load a valid privileged %cs.  Let the
578	 * hardware check for invalid selectors, excess privilege in
579	 * other selectors, invalid %eip's and invalid %esp's.
580	 */
581	cs = ucp->uc_mcontext.mc_cs;
582	if (!CS_SECURE(cs)) {
583		printf("freebsd4_sigreturn: cs = 0x%x\n", cs);
584		ksiginfo_init_trap(&ksi);
585		ksi.ksi_signo = SIGBUS;
586		ksi.ksi_code = BUS_OBJERR;
587		ksi.ksi_trapno = T_PROTFLT;
588		ksi.ksi_addr = (void *)regs->tf_rip;
589		trapsignal(td, &ksi);
590		return (EINVAL);
591	}
592
593	/* Segment selectors restored by sigtramp.S */
594	regs->tf_rdi = ucp->uc_mcontext.mc_edi;
595	regs->tf_rsi = ucp->uc_mcontext.mc_esi;
596	regs->tf_rbp = ucp->uc_mcontext.mc_ebp;
597	regs->tf_rbx = ucp->uc_mcontext.mc_ebx;
598	regs->tf_rdx = ucp->uc_mcontext.mc_edx;
599	regs->tf_rcx = ucp->uc_mcontext.mc_ecx;
600	regs->tf_rax = ucp->uc_mcontext.mc_eax;
601	regs->tf_trapno = ucp->uc_mcontext.mc_trapno;
602	regs->tf_err = ucp->uc_mcontext.mc_err;
603	regs->tf_rip = ucp->uc_mcontext.mc_eip;
604	regs->tf_cs = cs;
605	regs->tf_rflags = ucp->uc_mcontext.mc_eflags;
606	regs->tf_rsp = ucp->uc_mcontext.mc_esp;
607	regs->tf_ss = ucp->uc_mcontext.mc_ss;
608
609	PROC_LOCK(p);
610	td->td_sigmask = ucp->uc_sigmask;
611	SIG_CANTMASK(td->td_sigmask);
612	signotify(td);
613	PROC_UNLOCK(p);
614	return (EJUSTRETURN);
615}
616#endif	/* COMPAT_FREEBSD4 */
617
618/*
619 * MPSAFE
620 */
621int
622freebsd32_sigreturn(td, uap)
623	struct thread *td;
624	struct freebsd32_sigreturn_args /* {
625		const struct freebsd32_ucontext *sigcntxp;
626	} */ *uap;
627{
628	struct ia32_ucontext uc;
629	struct proc *p = td->td_proc;
630	struct trapframe *regs;
631	const struct ia32_ucontext *ucp;
632	int cs, eflags, error, ret;
633	ksiginfo_t ksi;
634
635	error = copyin(uap->sigcntxp, &uc, sizeof(uc));
636	if (error != 0)
637		return (error);
638	ucp = &uc;
639	regs = td->td_frame;
640	eflags = ucp->uc_mcontext.mc_eflags;
641	/*
642	 * Don't allow users to change privileged or reserved flags.
643	 */
644	/*
645	 * XXX do allow users to change the privileged flag PSL_RF.
646	 * The cpu sets PSL_RF in tf_eflags for faults.  Debuggers
647	 * should sometimes set it there too.  tf_eflags is kept in
648	 * the signal context during signal handling and there is no
649	 * other place to remember it, so the PSL_RF bit may be
650	 * corrupted by the signal handler without us knowing.
651	 * Corruption of the PSL_RF bit at worst causes one more or
652	 * one less debugger trap, so allowing it is fairly harmless.
653	 */
654	if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) {
655		printf("freebsd32_sigreturn: eflags = 0x%x\n", eflags);
656		return (EINVAL);
657	}
658
659	/*
660	 * Don't allow users to load a valid privileged %cs.  Let the
661	 * hardware check for invalid selectors, excess privilege in
662	 * other selectors, invalid %eip's and invalid %esp's.
663	 */
664	cs = ucp->uc_mcontext.mc_cs;
665	if (!CS_SECURE(cs)) {
666		printf("sigreturn: cs = 0x%x\n", cs);
667		ksiginfo_init_trap(&ksi);
668		ksi.ksi_signo = SIGBUS;
669		ksi.ksi_code = BUS_OBJERR;
670		ksi.ksi_trapno = T_PROTFLT;
671		ksi.ksi_addr = (void *)regs->tf_rip;
672		trapsignal(td, &ksi);
673		return (EINVAL);
674	}
675
676	ret = ia32_set_fpcontext(td, &ucp->uc_mcontext);
677	if (ret != 0)
678		return (ret);
679
680	/* Segment selectors restored by sigtramp.S */
681	regs->tf_rdi = ucp->uc_mcontext.mc_edi;
682	regs->tf_rsi = ucp->uc_mcontext.mc_esi;
683	regs->tf_rbp = ucp->uc_mcontext.mc_ebp;
684	regs->tf_rbx = ucp->uc_mcontext.mc_ebx;
685	regs->tf_rdx = ucp->uc_mcontext.mc_edx;
686	regs->tf_rcx = ucp->uc_mcontext.mc_ecx;
687	regs->tf_rax = ucp->uc_mcontext.mc_eax;
688	regs->tf_trapno = ucp->uc_mcontext.mc_trapno;
689	regs->tf_err = ucp->uc_mcontext.mc_err;
690	regs->tf_rip = ucp->uc_mcontext.mc_eip;
691	regs->tf_cs = cs;
692	regs->tf_rflags = ucp->uc_mcontext.mc_eflags;
693	regs->tf_rsp = ucp->uc_mcontext.mc_esp;
694	regs->tf_ss = ucp->uc_mcontext.mc_ss;
695
696	PROC_LOCK(p);
697	td->td_sigmask = ucp->uc_sigmask;
698	SIG_CANTMASK(td->td_sigmask);
699	signotify(td);
700	PROC_UNLOCK(p);
701	return (EJUSTRETURN);
702}
703
704/*
705 * Clear registers on exec
706 */
707void
708ia32_setregs(td, entry, stack, ps_strings)
709	struct thread *td;
710	u_long entry;
711	u_long stack;
712	u_long ps_strings;
713{
714	struct trapframe *regs = td->td_frame;
715	struct pcb *pcb = td->td_pcb;
716
717	wrmsr(MSR_FSBASE, 0);
718	wrmsr(MSR_KGSBASE, 0);	/* User value while we're in the kernel */
719	pcb->pcb_fsbase = 0;
720	pcb->pcb_gsbase = 0;
721	load_ds(_udatasel);
722	load_es(_udatasel);
723	load_fs(_udatasel);
724	load_gs(_udatasel);
725	pcb->pcb_ds = _udatasel;
726	pcb->pcb_es = _udatasel;
727	pcb->pcb_fs = _udatasel;
728	pcb->pcb_gs = _udatasel;
729
730	bzero((char *)regs, sizeof(struct trapframe));
731	regs->tf_rip = entry;
732	regs->tf_rsp = stack;
733	regs->tf_rflags = PSL_USER | (regs->tf_rflags & PSL_T);
734	regs->tf_ss = _udatasel;
735	regs->tf_cs = _ucode32sel;
736	regs->tf_rbx = ps_strings;
737	load_cr0(rcr0() | CR0_MP | CR0_TS);
738	fpstate_drop(td);
739
740	/* Return via doreti so that we can change to a different %cs */
741	pcb->pcb_flags |= PCB_FULLCTX;
742	td->td_retval[1] = 0;
743}
744
745void
746siginfo_to_ia32siginfo(siginfo_t *src, struct ia32_siginfo *dst)
747{
748	dst->si_signo = src->si_signo;
749	dst->si_errno = src->si_errno;
750	dst->si_code = src->si_code;
751	dst->si_pid = src->si_pid;
752	dst->si_uid = src->si_uid;
753	dst->si_status = src->si_status;
754	dst->si_addr = dst->si_addr;
755	dst->si_value.sigval_int = src->si_value.sigval_int;
756	dst->si_band = src->si_band;
757	dst->__spare__[0] = src->si_trapno;
758}
759