xref: /freebsd/sys/amd64/ia32/ia32_signal.c (revision 87569f75a91f298c52a71823c04d41cf53c88889)
1 /*-
2  * Copyright (c) 2003 Peter Wemm
3  * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * William Jolitz.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include "opt_compat.h"
38 
39 #include <sys/param.h>
40 #include <sys/exec.h>
41 #include <sys/fcntl.h>
42 #include <sys/imgact.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/mutex.h>
47 #include <sys/mman.h>
48 #include <sys/namei.h>
49 #include <sys/pioctl.h>
50 #include <sys/proc.h>
51 #include <sys/procfs.h>
52 #include <sys/resourcevar.h>
53 #include <sys/systm.h>
54 #include <sys/signalvar.h>
55 #include <sys/stat.h>
56 #include <sys/sx.h>
57 #include <sys/syscall.h>
58 #include <sys/sysctl.h>
59 #include <sys/sysent.h>
60 #include <sys/vnode.h>
61 
62 #include <vm/vm.h>
63 #include <vm/vm_kern.h>
64 #include <vm/vm_param.h>
65 #include <vm/pmap.h>
66 #include <vm/vm_map.h>
67 #include <vm/vm_object.h>
68 #include <vm/vm_extern.h>
69 
70 #include <compat/freebsd32/freebsd32_util.h>
71 #include <compat/freebsd32/freebsd32_proto.h>
72 #include <compat/ia32/ia32_signal.h>
73 #include <machine/psl.h>
74 #include <machine/segments.h>
75 #include <machine/specialreg.h>
76 #include <machine/frame.h>
77 #include <machine/md_var.h>
78 #include <machine/pcb.h>
79 #include <machine/cpufunc.h>
80 
81 #ifdef COMPAT_FREEBSD4
82 static void freebsd4_ia32_sendsig(sig_t, ksiginfo_t *, sigset_t *);
83 #endif
84 static void ia32_get_fpcontext(struct thread *td, struct ia32_mcontext *mcp);
85 static int ia32_set_fpcontext(struct thread *td, const struct ia32_mcontext *mcp);
86 
87 extern int _ucode32sel, _udatasel;
88 
89 #define	CS_SECURE(cs)		(ISPL(cs) == SEL_UPL)
90 #define	EFL_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
91 
92 static void
93 ia32_get_fpcontext(struct thread *td, struct ia32_mcontext *mcp)
94 {
95 
96 	mcp->mc_ownedfp = fpugetregs(td, (struct savefpu *)&mcp->mc_fpstate);
97 	mcp->mc_fpformat = fpuformat();
98 }
99 
100 static int
101 ia32_set_fpcontext(struct thread *td, const struct ia32_mcontext *mcp)
102 {
103 
104 	if (mcp->mc_fpformat == _MC_FPFMT_NODEV)
105 		return (0);
106 	else if (mcp->mc_fpformat != _MC_FPFMT_XMM)
107 		return (EINVAL);
108 	else if (mcp->mc_ownedfp == _MC_FPOWNED_NONE)
109 		/* We don't care what state is left in the FPU or PCB. */
110 		fpstate_drop(td);
111 	else if (mcp->mc_ownedfp == _MC_FPOWNED_FPU ||
112 	    mcp->mc_ownedfp == _MC_FPOWNED_PCB) {
113 		/*
114 		 * XXX we violate the dubious requirement that fpusetregs()
115 		 * be called with interrupts disabled.
116 		 */
117 		fpusetregs(td, (struct savefpu *)&mcp->mc_fpstate);
118 	} else
119 		return (EINVAL);
120 	return (0);
121 }
122 
123 /*
124  * Get machine context.
125  */
126 static int
127 ia32_get_mcontext(struct thread *td, struct ia32_mcontext *mcp, int flags)
128 {
129 	struct trapframe *tp;
130 
131 	tp = td->td_frame;
132 
133 	PROC_LOCK(curthread->td_proc);
134 	mcp->mc_onstack = sigonstack(tp->tf_rsp);
135 	PROC_UNLOCK(curthread->td_proc);
136 	mcp->mc_gs = td->td_pcb->pcb_gs;
137 	mcp->mc_fs = td->td_pcb->pcb_fs;
138 	mcp->mc_es = td->td_pcb->pcb_es;
139 	mcp->mc_ds = td->td_pcb->pcb_ds;
140 	mcp->mc_edi = tp->tf_rdi;
141 	mcp->mc_esi = tp->tf_rsi;
142 	mcp->mc_ebp = tp->tf_rbp;
143 	mcp->mc_isp = tp->tf_rsp;
144 	if (flags & GET_MC_CLEAR_RET) {
145 		mcp->mc_eax = 0;
146 		mcp->mc_edx = 0;
147 	} else {
148 		mcp->mc_eax = tp->tf_rax;
149 		mcp->mc_edx = tp->tf_rdx;
150 	}
151 	mcp->mc_ebx = tp->tf_rbx;
152 	mcp->mc_ecx = tp->tf_rcx;
153 	mcp->mc_eip = tp->tf_rip;
154 	mcp->mc_cs = tp->tf_cs;
155 	mcp->mc_eflags = tp->tf_rflags;
156 	mcp->mc_esp = tp->tf_rsp;
157 	mcp->mc_ss = tp->tf_ss;
158 	mcp->mc_len = sizeof(*mcp);
159 	ia32_get_fpcontext(td, mcp);
160 	return (0);
161 }
162 
163 /*
164  * Set machine context.
165  *
166  * However, we don't set any but the user modifiable flags, and we won't
167  * touch the cs selector.
168  */
169 static int
170 ia32_set_mcontext(struct thread *td, const struct ia32_mcontext *mcp)
171 {
172 	struct trapframe *tp;
173 	long rflags;
174 	int ret;
175 
176 	tp = td->td_frame;
177 	if (mcp->mc_len != sizeof(*mcp))
178 		return (EINVAL);
179 	rflags = (mcp->mc_eflags & PSL_USERCHANGE) |
180 	    (tp->tf_rflags & ~PSL_USERCHANGE);
181 	ret = ia32_set_fpcontext(td, mcp);
182 	if (ret != 0)
183 		return (ret);
184 #if 0	/* XXX deal with load_fs() and friends */
185 	tp->tf_fs = mcp->mc_fs;
186 	tp->tf_es = mcp->mc_es;
187 	tp->tf_ds = mcp->mc_ds;
188 #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 
215 int
216 freebsd32_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 
233 int
234 freebsd32_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 
256 int
257 freebsd32_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
297 static void
298 freebsd4_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 
406 void
407 ia32_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  */
538 int
539 freebsd4_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  */
621 int
622 freebsd32_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  */
707 void
708 ia32_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 
745 void
746 siginfo_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.sival_int;
756 	dst->si_band = src->si_band;
757 	dst->si_trapno = src->si_trapno;
758 	dst->si_timerid = src->si_timerid;
759 	dst->si_overrun = src->si_overrun;
760 }
761