xref: /freebsd/sys/amd64/ia32/ia32_signal.c (revision 2ad756a6bbb30fc98ee9000fba5bceec916a6c70)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2003 Peter Wemm
5  * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * William Jolitz.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #include <sys/param.h>
38 #include <sys/exec.h>
39 #include <sys/fcntl.h>
40 #include <sys/imgact.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/mutex.h>
45 #include <sys/mman.h>
46 #include <sys/namei.h>
47 #include <sys/proc.h>
48 #include <sys/procfs.h>
49 #include <sys/resourcevar.h>
50 #include <sys/systm.h>
51 #include <sys/signalvar.h>
52 #include <sys/stat.h>
53 #include <sys/sx.h>
54 #include <sys/syscall.h>
55 #include <sys/syscallsubr.h>
56 #include <sys/sysctl.h>
57 #include <sys/sysent.h>
58 #include <sys/vnode.h>
59 
60 #include <vm/vm.h>
61 #include <vm/vm_kern.h>
62 #include <vm/vm_param.h>
63 #include <vm/pmap.h>
64 #include <vm/vm_map.h>
65 #include <vm/vm_object.h>
66 #include <vm/vm_extern.h>
67 
68 #include <compat/freebsd32/freebsd32_signal.h>
69 #include <compat/freebsd32/freebsd32_util.h>
70 #include <compat/freebsd32/freebsd32_proto.h>
71 #include <compat/freebsd32/freebsd32.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 #include <machine/trap.h>
81 
82 #include "vdso_ia32_offsets.h"
83 
84 extern const char _binary_elf_vdso32_so_1_start[];
85 extern const char _binary_elf_vdso32_so_1_end[];
86 extern char _binary_elf_vdso32_so_1_size;
87 
88 #ifdef COMPAT_FREEBSD4
89 static void freebsd4_ia32_sendsig(sig_t, ksiginfo_t *, sigset_t *);
90 #endif
91 
92 static void
93 ia32_get_fpcontext(struct thread *td, struct ia32_mcontext *mcp,
94     char **xfpusave, size_t *xfpusave_len)
95 {
96 	/*
97 	 * XXX Format of 64bit and 32bit FXSAVE areas differs. FXSAVE
98 	 * in 32bit mode saves %cs and %ds, while on 64bit it saves
99 	 * 64bit instruction and data pointers. Ignore the difference
100 	 * for now, it should be irrelevant for most applications.
101 	 */
102 	mcp->mc_ownedfp = fpugetregs(td);
103 	bcopy(get_pcb_user_save_td(td), &mcp->mc_fpstate[0],
104 	    sizeof(mcp->mc_fpstate));
105 	mcp->mc_fpformat = fpuformat();
106 	if (xfpusave == NULL)
107 		return;
108 	if (!use_xsave || cpu_max_ext_state_size <= sizeof(struct savefpu)) {
109 		*xfpusave_len = 0;
110 		*xfpusave = NULL;
111 	} else {
112 		mcp->mc_flags |= _MC_IA32_HASFPXSTATE;
113 		*xfpusave_len = mcp->mc_xfpustate_len =
114 		    cpu_max_ext_state_size - sizeof(struct savefpu);
115 		*xfpusave = (char *)(get_pcb_user_save_td(td) + 1);
116 	}
117 }
118 
119 static int
120 ia32_set_fpcontext(struct thread *td, struct ia32_mcontext *mcp,
121     char *xfpustate, size_t xfpustate_len)
122 {
123 	int error;
124 
125 	if (mcp->mc_fpformat == _MC_FPFMT_NODEV)
126 		return (0);
127 	else if (mcp->mc_fpformat != _MC_FPFMT_XMM)
128 		return (EINVAL);
129 	else if (mcp->mc_ownedfp == _MC_FPOWNED_NONE) {
130 		/* We don't care what state is left in the FPU or PCB. */
131 		fpstate_drop(td);
132 		error = 0;
133 	} else if (mcp->mc_ownedfp == _MC_FPOWNED_FPU ||
134 	    mcp->mc_ownedfp == _MC_FPOWNED_PCB) {
135 		error = fpusetregs(td, (struct savefpu *)&mcp->mc_fpstate,
136 		    xfpustate, xfpustate_len);
137 	} else
138 		return (EINVAL);
139 	return (error);
140 }
141 
142 /*
143  * Get machine context.
144  */
145 static int
146 ia32_get_mcontext(struct thread *td, struct ia32_mcontext *mcp, int flags)
147 {
148 	struct pcb *pcb;
149 	struct trapframe *tp;
150 
151 	pcb = td->td_pcb;
152 	tp = td->td_frame;
153 
154 	PROC_LOCK(curthread->td_proc);
155 	mcp->mc_onstack = sigonstack(tp->tf_rsp);
156 	PROC_UNLOCK(curthread->td_proc);
157 	/* Entry into kernel always sets TF_HASSEGS */
158 	mcp->mc_gs = tp->tf_gs;
159 	mcp->mc_fs = tp->tf_fs;
160 	mcp->mc_es = tp->tf_es;
161 	mcp->mc_ds = tp->tf_ds;
162 	mcp->mc_edi = tp->tf_rdi;
163 	mcp->mc_esi = tp->tf_rsi;
164 	mcp->mc_ebp = tp->tf_rbp;
165 	mcp->mc_isp = tp->tf_rsp;
166 	mcp->mc_eflags = tp->tf_rflags;
167 	if (flags & GET_MC_CLEAR_RET) {
168 		mcp->mc_eax = 0;
169 		mcp->mc_edx = 0;
170 		mcp->mc_eflags &= ~PSL_C;
171 	} else {
172 		mcp->mc_eax = tp->tf_rax;
173 		mcp->mc_edx = tp->tf_rdx;
174 	}
175 	mcp->mc_ebx = tp->tf_rbx;
176 	mcp->mc_ecx = tp->tf_rcx;
177 	mcp->mc_eip = tp->tf_rip;
178 	mcp->mc_cs = tp->tf_cs;
179 	mcp->mc_esp = tp->tf_rsp;
180 	mcp->mc_ss = tp->tf_ss;
181 	mcp->mc_len = sizeof(*mcp);
182 	mcp->mc_flags = tp->tf_flags;
183 	ia32_get_fpcontext(td, mcp, NULL, 0);
184 	mcp->mc_fsbase = pcb->pcb_fsbase;
185 	mcp->mc_gsbase = pcb->pcb_gsbase;
186 	mcp->mc_xfpustate = 0;
187 	mcp->mc_xfpustate_len = 0;
188 	bzero(mcp->mc_spare2, sizeof(mcp->mc_spare2));
189 	return (0);
190 }
191 
192 /*
193  * Set machine context.
194  *
195  * However, we don't set any but the user modifiable flags, and we won't
196  * touch the cs selector.
197  */
198 static int
199 ia32_set_mcontext(struct thread *td, struct ia32_mcontext *mcp)
200 {
201 	struct trapframe *tp;
202 	char *xfpustate;
203 	long rflags;
204 	int ret;
205 
206 	tp = td->td_frame;
207 	if (mcp->mc_len != sizeof(*mcp))
208 		return (EINVAL);
209 	rflags = (mcp->mc_eflags & PSL_USERCHANGE) |
210 	    (tp->tf_rflags & ~PSL_USERCHANGE);
211 	if (mcp->mc_flags & _MC_IA32_HASFPXSTATE) {
212 		if (mcp->mc_xfpustate_len > cpu_max_ext_state_size -
213 		    sizeof(struct savefpu))
214 			return (EINVAL);
215 		xfpustate = (char *)fpu_save_area_alloc();
216 		ret = copyin(PTRIN(mcp->mc_xfpustate), xfpustate,
217 		    mcp->mc_xfpustate_len);
218 		if (ret != 0) {
219 			fpu_save_area_free((struct savefpu *)xfpustate);
220 			return (ret);
221 		}
222 	} else
223 		xfpustate = NULL;
224 	ret = ia32_set_fpcontext(td, mcp, xfpustate, mcp->mc_xfpustate_len);
225 	fpu_save_area_free((struct savefpu *)xfpustate);
226 	if (ret != 0)
227 		return (ret);
228 	tp->tf_gs = mcp->mc_gs;
229 	tp->tf_fs = mcp->mc_fs;
230 	tp->tf_es = mcp->mc_es;
231 	tp->tf_ds = mcp->mc_ds;
232 	tp->tf_flags = TF_HASSEGS;
233 	tp->tf_rdi = mcp->mc_edi;
234 	tp->tf_rsi = mcp->mc_esi;
235 	tp->tf_rbp = mcp->mc_ebp;
236 	tp->tf_rbx = mcp->mc_ebx;
237 	tp->tf_rdx = mcp->mc_edx;
238 	tp->tf_rcx = mcp->mc_ecx;
239 	tp->tf_rax = mcp->mc_eax;
240 	/* trapno, err */
241 	tp->tf_rip = mcp->mc_eip;
242 	tp->tf_rflags = rflags;
243 	tp->tf_rsp = mcp->mc_esp;
244 	tp->tf_ss = mcp->mc_ss;
245 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
246 	return (0);
247 }
248 
249 /*
250  * The first two fields of a ucontext_t are the signal mask and
251  * the machine context.  The next field is uc_link; we want to
252  * avoid destroying the link when copying out contexts.
253  */
254 #define	UC_COPY_SIZE	offsetof(struct ia32_ucontext, uc_link)
255 
256 int
257 freebsd32_getcontext(struct thread *td, struct freebsd32_getcontext_args *uap)
258 {
259 	struct ia32_ucontext uc;
260 	int ret;
261 
262 	if (uap->ucp == NULL)
263 		ret = EINVAL;
264 	else {
265 		bzero(&uc, sizeof(uc));
266 		ia32_get_mcontext(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
267 		PROC_LOCK(td->td_proc);
268 		uc.uc_sigmask = td->td_sigmask;
269 		PROC_UNLOCK(td->td_proc);
270 		ret = copyout(&uc, uap->ucp, UC_COPY_SIZE);
271 	}
272 	return (ret);
273 }
274 
275 int
276 freebsd32_setcontext(struct thread *td, struct freebsd32_setcontext_args *uap)
277 {
278 	struct ia32_ucontext uc;
279 	int ret;
280 
281 	if (uap->ucp == NULL)
282 		ret = EINVAL;
283 	else {
284 		ret = copyin(uap->ucp, &uc, UC_COPY_SIZE);
285 		if (ret == 0) {
286 			ret = ia32_set_mcontext(td, &uc.uc_mcontext);
287 			if (ret == 0) {
288 				kern_sigprocmask(td, SIG_SETMASK,
289 				    &uc.uc_sigmask, NULL, 0);
290 			}
291 		}
292 	}
293 	return (ret == 0 ? EJUSTRETURN : ret);
294 }
295 
296 int
297 freebsd32_swapcontext(struct thread *td, struct freebsd32_swapcontext_args *uap)
298 {
299 	struct ia32_ucontext uc;
300 	int ret;
301 
302 	if (uap->oucp == NULL || uap->ucp == NULL)
303 		ret = EINVAL;
304 	else {
305 		bzero(&uc, sizeof(uc));
306 		ia32_get_mcontext(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
307 		PROC_LOCK(td->td_proc);
308 		uc.uc_sigmask = td->td_sigmask;
309 		PROC_UNLOCK(td->td_proc);
310 		ret = copyout(&uc, uap->oucp, UC_COPY_SIZE);
311 		if (ret == 0) {
312 			ret = copyin(uap->ucp, &uc, UC_COPY_SIZE);
313 			if (ret == 0) {
314 				ret = ia32_set_mcontext(td, &uc.uc_mcontext);
315 				if (ret == 0) {
316 					kern_sigprocmask(td, SIG_SETMASK,
317 					    &uc.uc_sigmask, NULL, 0);
318 				}
319 			}
320 		}
321 	}
322 	return (ret == 0 ? EJUSTRETURN : ret);
323 }
324 
325 /*
326  * Send an interrupt to process.
327  *
328  * Stack is set up to allow sigcode stored
329  * at top to call routine, followed by kcall
330  * to sigreturn routine below.  After sigreturn
331  * resets the signal mask, the stack, and the
332  * frame pointer, it returns to the user
333  * specified pc, psl.
334  */
335 
336 #ifdef COMPAT_43
337 static void
338 ia32_osendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
339 {
340 	struct ia32_osigframe sf, *fp;
341 	struct proc *p;
342 	struct thread *td;
343 	struct sigacts *psp;
344 	struct trapframe *regs;
345 	int sig;
346 	int oonstack;
347 
348 	td = curthread;
349 	p = td->td_proc;
350 	PROC_LOCK_ASSERT(p, MA_OWNED);
351 	sig = ksi->ksi_signo;
352 	psp = p->p_sigacts;
353 	mtx_assert(&psp->ps_mtx, MA_OWNED);
354 	regs = td->td_frame;
355 	oonstack = sigonstack(regs->tf_rsp);
356 
357 	/* Allocate space for the signal handler context. */
358 	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
359 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
360 		fp = (struct ia32_osigframe *)((uintptr_t)td->td_sigstk.ss_sp +
361 		    td->td_sigstk.ss_size - sizeof(sf));
362 		td->td_sigstk.ss_flags |= SS_ONSTACK;
363 	} else
364 		fp = (struct ia32_osigframe *)regs->tf_rsp - 1;
365 
366 	/* Build the argument list for the signal handler. */
367 	sf.sf_signum = sig;
368 	sf.sf_scp = (register_t)&fp->sf_siginfo.si_sc;
369 	bzero(&sf.sf_siginfo, sizeof(sf.sf_siginfo));
370 	if (SIGISMEMBER(psp->ps_siginfo, sig)) {
371 		/* Signal handler installed with SA_SIGINFO. */
372 		sf.sf_arg2 = (register_t)&fp->sf_siginfo;
373 		sf.sf_siginfo.si_signo = sig;
374 		sf.sf_siginfo.si_code = ksi->ksi_code;
375 		sf.sf_ah = (uintptr_t)catcher;
376 		sf.sf_addr = 0;
377 	} else {
378 		/* Old FreeBSD-style arguments. */
379 		sf.sf_arg2 = ksi->ksi_code;
380 		sf.sf_addr = (register_t)ksi->ksi_addr;
381 		sf.sf_ah = (uintptr_t)catcher;
382 	}
383 	mtx_unlock(&psp->ps_mtx);
384 	PROC_UNLOCK(p);
385 
386 	/* Save most if not all of trap frame. */
387 	sf.sf_siginfo.si_sc.sc_eax = regs->tf_rax;
388 	sf.sf_siginfo.si_sc.sc_ebx = regs->tf_rbx;
389 	sf.sf_siginfo.si_sc.sc_ecx = regs->tf_rcx;
390 	sf.sf_siginfo.si_sc.sc_edx = regs->tf_rdx;
391 	sf.sf_siginfo.si_sc.sc_esi = regs->tf_rsi;
392 	sf.sf_siginfo.si_sc.sc_edi = regs->tf_rdi;
393 	sf.sf_siginfo.si_sc.sc_cs = regs->tf_cs;
394 	sf.sf_siginfo.si_sc.sc_ds = regs->tf_ds;
395 	sf.sf_siginfo.si_sc.sc_ss = regs->tf_ss;
396 	sf.sf_siginfo.si_sc.sc_es = regs->tf_es;
397 	sf.sf_siginfo.si_sc.sc_fs = regs->tf_fs;
398 	sf.sf_siginfo.si_sc.sc_gs = regs->tf_gs;
399 	sf.sf_siginfo.si_sc.sc_isp = regs->tf_rsp;
400 
401 	/* Build the signal context to be used by osigreturn(). */
402 	sf.sf_siginfo.si_sc.sc_onstack = (oonstack) ? 1 : 0;
403 	SIG2OSIG(*mask, sf.sf_siginfo.si_sc.sc_mask);
404 	sf.sf_siginfo.si_sc.sc_esp = regs->tf_rsp;
405 	sf.sf_siginfo.si_sc.sc_ebp = regs->tf_rbp;
406 	sf.sf_siginfo.si_sc.sc_eip = regs->tf_rip;
407 	sf.sf_siginfo.si_sc.sc_eflags = regs->tf_rflags;
408 	sf.sf_siginfo.si_sc.sc_trapno = regs->tf_trapno;
409 	sf.sf_siginfo.si_sc.sc_err = regs->tf_err;
410 
411 	/*
412 	 * Copy the sigframe out to the user's stack.
413 	 */
414 	if (copyout(&sf, fp, sizeof(*fp)) != 0) {
415 #ifdef DEBUG
416 		printf("process %ld has trashed its stack\n", (long)p->p_pid);
417 #endif
418 		PROC_LOCK(p);
419 		sigexit(td, SIGILL);
420 	}
421 
422 	regs->tf_rsp = (uintptr_t)fp;
423 	regs->tf_rip = PROC_PS_STRINGS(p) -
424 	    (_binary_elf_vdso32_so_1_end - _binary_elf_vdso32_so_1_start) +
425 	    VDSO_IA32_OSIGCODE_OFFSET;
426 	regs->tf_rflags &= ~(PSL_T | PSL_D);
427 	regs->tf_cs = _ucode32sel;
428 	regs->tf_ds = _udatasel;
429 	regs->tf_es = _udatasel;
430 	regs->tf_fs = _udatasel;
431 	regs->tf_ss = _udatasel;
432 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
433 	PROC_LOCK(p);
434 	mtx_lock(&psp->ps_mtx);
435 }
436 #endif
437 
438 #ifdef COMPAT_FREEBSD4
439 static void
440 freebsd4_ia32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
441 {
442 	struct ia32_freebsd4_sigframe sf, *sfp;
443 	struct siginfo32 siginfo;
444 	struct proc *p;
445 	struct thread *td;
446 	struct sigacts *psp;
447 	struct trapframe *regs;
448 	int oonstack;
449 	int sig;
450 
451 	td = curthread;
452 	p = td->td_proc;
453 	siginfo_to_siginfo32(&ksi->ksi_info, &siginfo);
454 
455 	PROC_LOCK_ASSERT(p, MA_OWNED);
456 	sig = siginfo.si_signo;
457 	psp = p->p_sigacts;
458 	mtx_assert(&psp->ps_mtx, MA_OWNED);
459 	regs = td->td_frame;
460 	oonstack = sigonstack(regs->tf_rsp);
461 
462 	/* Save user context. */
463 	bzero(&sf, sizeof(sf));
464 	sf.sf_uc.uc_sigmask = *mask;
465 	sf.sf_uc.uc_stack.ss_sp = (uintptr_t)td->td_sigstk.ss_sp;
466 	sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
467 	sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
468 	    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
469 	sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
470 	sf.sf_uc.uc_mcontext.mc_edi = regs->tf_rdi;
471 	sf.sf_uc.uc_mcontext.mc_esi = regs->tf_rsi;
472 	sf.sf_uc.uc_mcontext.mc_ebp = regs->tf_rbp;
473 	sf.sf_uc.uc_mcontext.mc_isp = regs->tf_rsp; /* XXX */
474 	sf.sf_uc.uc_mcontext.mc_ebx = regs->tf_rbx;
475 	sf.sf_uc.uc_mcontext.mc_edx = regs->tf_rdx;
476 	sf.sf_uc.uc_mcontext.mc_ecx = regs->tf_rcx;
477 	sf.sf_uc.uc_mcontext.mc_eax = regs->tf_rax;
478 	sf.sf_uc.uc_mcontext.mc_trapno = regs->tf_trapno;
479 	sf.sf_uc.uc_mcontext.mc_err = regs->tf_err;
480 	sf.sf_uc.uc_mcontext.mc_eip = regs->tf_rip;
481 	sf.sf_uc.uc_mcontext.mc_cs = regs->tf_cs;
482 	sf.sf_uc.uc_mcontext.mc_eflags = regs->tf_rflags;
483 	sf.sf_uc.uc_mcontext.mc_esp = regs->tf_rsp;
484 	sf.sf_uc.uc_mcontext.mc_ss = regs->tf_ss;
485 	sf.sf_uc.uc_mcontext.mc_ds = regs->tf_ds;
486 	sf.sf_uc.uc_mcontext.mc_es = regs->tf_es;
487 	sf.sf_uc.uc_mcontext.mc_fs = regs->tf_fs;
488 	sf.sf_uc.uc_mcontext.mc_gs = regs->tf_gs;
489 	bzero(sf.sf_uc.uc_mcontext.mc_fpregs,
490 	    sizeof(sf.sf_uc.uc_mcontext.mc_fpregs));
491 	bzero(sf.sf_uc.uc_mcontext.__spare__,
492 	    sizeof(sf.sf_uc.uc_mcontext.__spare__));
493 	bzero(sf.sf_uc.__spare__, sizeof(sf.sf_uc.__spare__));
494 
495 	/* Allocate space for the signal handler context. */
496 	if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
497 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
498 		sfp = (struct ia32_freebsd4_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
499 		    td->td_sigstk.ss_size - sizeof(sf));
500 	} else
501 		sfp = (struct ia32_freebsd4_sigframe *)regs->tf_rsp - 1;
502 	PROC_UNLOCK(p);
503 
504 	/* Build the argument list for the signal handler. */
505 	sf.sf_signum = sig;
506 	sf.sf_ucontext = (register_t)&sfp->sf_uc;
507 	bzero(&sf.sf_si, sizeof(sf.sf_si));
508 	if (SIGISMEMBER(psp->ps_siginfo, sig)) {
509 		/* Signal handler installed with SA_SIGINFO. */
510 		sf.sf_siginfo = (u_int32_t)(uintptr_t)&sfp->sf_si;
511 		sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
512 
513 		/* Fill in POSIX parts */
514 		sf.sf_si = siginfo;
515 		sf.sf_si.si_signo = sig;
516 	} else {
517 		/* Old FreeBSD-style arguments. */
518 		sf.sf_siginfo = siginfo.si_code;
519 		sf.sf_addr = (u_int32_t)siginfo.si_addr;
520 		sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
521 	}
522 	mtx_unlock(&psp->ps_mtx);
523 
524 	/*
525 	 * Copy the sigframe out to the user's stack.
526 	 */
527 	if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
528 #ifdef DEBUG
529 		printf("process %ld has trashed its stack\n", (long)p->p_pid);
530 #endif
531 		PROC_LOCK(p);
532 		sigexit(td, SIGILL);
533 	}
534 
535 	regs->tf_rsp = (uintptr_t)sfp;
536 	regs->tf_rip = PROC_SIGCODE(p) +
537 	    VDSO_FREEBSD4_IA32_SIGCODE_OFFSET - VDSO_IA32_SIGCODE_OFFSET;
538 	regs->tf_rflags &= ~(PSL_T | PSL_D);
539 	regs->tf_cs = _ucode32sel;
540 	regs->tf_ss = _udatasel;
541 	regs->tf_ds = _udatasel;
542 	regs->tf_es = _udatasel;
543 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
544 	/* leave user %fs and %gs untouched */
545 	PROC_LOCK(p);
546 	mtx_lock(&psp->ps_mtx);
547 }
548 #endif	/* COMPAT_FREEBSD4 */
549 
550 void
551 ia32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
552 {
553 	struct ia32_sigframe sf, *sfp;
554 	struct siginfo32 siginfo;
555 	struct proc *p;
556 	struct thread *td;
557 	struct sigacts *psp;
558 	char *sp;
559 	struct trapframe *regs;
560 	char *xfpusave;
561 	size_t xfpusave_len;
562 	int oonstack;
563 	int sig;
564 
565 	siginfo_to_siginfo32(&ksi->ksi_info, &siginfo);
566 	td = curthread;
567 	p = td->td_proc;
568 	PROC_LOCK_ASSERT(p, MA_OWNED);
569 	sig = siginfo.si_signo;
570 	psp = p->p_sigacts;
571 #ifdef COMPAT_FREEBSD4
572 	if (SIGISMEMBER(psp->ps_freebsd4, sig)) {
573 		freebsd4_ia32_sendsig(catcher, ksi, mask);
574 		return;
575 	}
576 #endif
577 #ifdef COMPAT_43
578 	if (SIGISMEMBER(psp->ps_osigset, sig)) {
579 		ia32_osendsig(catcher, ksi, mask);
580 		return;
581 	}
582 #endif
583 	mtx_assert(&psp->ps_mtx, MA_OWNED);
584 	regs = td->td_frame;
585 	oonstack = sigonstack(regs->tf_rsp);
586 
587 	/* Save user context. */
588 	bzero(&sf, sizeof(sf));
589 	sf.sf_uc.uc_sigmask = *mask;
590 	sf.sf_uc.uc_stack.ss_sp = (uintptr_t)td->td_sigstk.ss_sp;
591 	sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
592 	sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
593 	    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
594 	sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
595 	sf.sf_uc.uc_mcontext.mc_edi = regs->tf_rdi;
596 	sf.sf_uc.uc_mcontext.mc_esi = regs->tf_rsi;
597 	sf.sf_uc.uc_mcontext.mc_ebp = regs->tf_rbp;
598 	sf.sf_uc.uc_mcontext.mc_isp = regs->tf_rsp; /* XXX */
599 	sf.sf_uc.uc_mcontext.mc_ebx = regs->tf_rbx;
600 	sf.sf_uc.uc_mcontext.mc_edx = regs->tf_rdx;
601 	sf.sf_uc.uc_mcontext.mc_ecx = regs->tf_rcx;
602 	sf.sf_uc.uc_mcontext.mc_eax = regs->tf_rax;
603 	sf.sf_uc.uc_mcontext.mc_trapno = regs->tf_trapno;
604 	sf.sf_uc.uc_mcontext.mc_err = regs->tf_err;
605 	sf.sf_uc.uc_mcontext.mc_eip = regs->tf_rip;
606 	sf.sf_uc.uc_mcontext.mc_cs = regs->tf_cs;
607 	sf.sf_uc.uc_mcontext.mc_eflags = regs->tf_rflags;
608 	sf.sf_uc.uc_mcontext.mc_esp = regs->tf_rsp;
609 	sf.sf_uc.uc_mcontext.mc_ss = regs->tf_ss;
610 	sf.sf_uc.uc_mcontext.mc_ds = regs->tf_ds;
611 	sf.sf_uc.uc_mcontext.mc_es = regs->tf_es;
612 	sf.sf_uc.uc_mcontext.mc_fs = regs->tf_fs;
613 	sf.sf_uc.uc_mcontext.mc_gs = regs->tf_gs;
614 	sf.sf_uc.uc_mcontext.mc_len = sizeof(sf.sf_uc.uc_mcontext); /* magic */
615 	ia32_get_fpcontext(td, &sf.sf_uc.uc_mcontext, &xfpusave, &xfpusave_len);
616 	sf.sf_uc.uc_mcontext.mc_fsbase = td->td_pcb->pcb_fsbase;
617 	sf.sf_uc.uc_mcontext.mc_gsbase = td->td_pcb->pcb_gsbase;
618 
619 	/* Allocate space for the signal handler context. */
620 	if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
621 	    SIGISMEMBER(psp->ps_sigonstack, sig))
622 		sp = (char *)td->td_sigstk.ss_sp + td->td_sigstk.ss_size;
623 	else
624 		sp = (char *)regs->tf_rsp;
625 	if (xfpusave != NULL) {
626 		sp -= xfpusave_len;
627 		sp = (char *)((unsigned long)sp & ~0x3Ful);
628 		sf.sf_uc.uc_mcontext.mc_xfpustate = (register_t)sp;
629 	}
630 	sp -= sizeof(sf);
631 	/* Align to 16 bytes. */
632 	sfp = (struct ia32_sigframe *)((uintptr_t)sp & ~0xF);
633 	PROC_UNLOCK(p);
634 
635 	/* Build the argument list for the signal handler. */
636 	sf.sf_signum = sig;
637 	sf.sf_ucontext = (register_t)&sfp->sf_uc;
638 	bzero(&sf.sf_si, sizeof(sf.sf_si));
639 	if (SIGISMEMBER(psp->ps_siginfo, sig)) {
640 		/* Signal handler installed with SA_SIGINFO. */
641 		sf.sf_siginfo = (u_int32_t)(uintptr_t)&sfp->sf_si;
642 		sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
643 
644 		/* Fill in POSIX parts */
645 		sf.sf_si = siginfo;
646 		sf.sf_si.si_signo = sig;
647 	} else {
648 		/* Old FreeBSD-style arguments. */
649 		sf.sf_siginfo = siginfo.si_code;
650 		sf.sf_addr = (u_int32_t)siginfo.si_addr;
651 		sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
652 	}
653 	mtx_unlock(&psp->ps_mtx);
654 
655 	/*
656 	 * Copy the sigframe out to the user's stack.
657 	 */
658 	if (copyout(&sf, sfp, sizeof(*sfp)) != 0 ||
659 	    (xfpusave != NULL && copyout(xfpusave,
660 	    PTRIN(sf.sf_uc.uc_mcontext.mc_xfpustate), xfpusave_len)
661 	    != 0)) {
662 #ifdef DEBUG
663 		printf("process %ld has trashed its stack\n", (long)p->p_pid);
664 #endif
665 		PROC_LOCK(p);
666 		sigexit(td, SIGILL);
667 	}
668 
669 	fpstate_drop(td);
670 	regs->tf_rsp = (uintptr_t)sfp;
671 	regs->tf_rip = PROC_SIGCODE(p);
672 	regs->tf_rflags &= ~(PSL_T | PSL_D);
673 	regs->tf_cs = _ucode32sel;
674 	regs->tf_ss = _udatasel;
675 	regs->tf_ds = _udatasel;
676 	regs->tf_es = _udatasel;
677 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
678 	/* XXXKIB leave user %fs and %gs untouched */
679 	PROC_LOCK(p);
680 	mtx_lock(&psp->ps_mtx);
681 }
682 
683 /*
684  * System call to cleanup state after a signal
685  * has been taken.  Reset signal mask and
686  * stack state from context left by sendsig (above).
687  * Return to previous pc and psl as specified by
688  * context left by sendsig. Check carefully to
689  * make sure that the user has not modified the
690  * state to gain improper privileges.
691  */
692 
693 #ifdef COMPAT_43
694 int
695 ofreebsd32_sigreturn(struct thread *td, struct ofreebsd32_sigreturn_args *uap)
696 {
697 	struct ia32_osigcontext sc, *scp;
698 	struct trapframe *regs;
699 	int eflags, error;
700 	ksiginfo_t ksi;
701 
702 	regs = td->td_frame;
703 	error = copyin(uap->sigcntxp, &sc, sizeof(sc));
704 	if (error != 0)
705 		return (error);
706 	scp = &sc;
707 	eflags = scp->sc_eflags;
708 	if (!EFL_SECURE(eflags, regs->tf_rflags)) {
709 		return (EINVAL);
710 	}
711 	if (!CS_SECURE(scp->sc_cs)) {
712 		ksiginfo_init_trap(&ksi);
713 		ksi.ksi_signo = SIGBUS;
714 		ksi.ksi_code = BUS_OBJERR;
715 		ksi.ksi_trapno = T_PROTFLT;
716 		ksi.ksi_addr = (void *)regs->tf_rip;
717 		trapsignal(td, &ksi);
718 		return (EINVAL);
719 	}
720 	regs->tf_ds = scp->sc_ds;
721 	regs->tf_es = scp->sc_es;
722 	regs->tf_fs = scp->sc_fs;
723 	regs->tf_gs = scp->sc_gs;
724 
725 	regs->tf_rax = scp->sc_eax;
726 	regs->tf_rbx = scp->sc_ebx;
727 	regs->tf_rcx = scp->sc_ecx;
728 	regs->tf_rdx = scp->sc_edx;
729 	regs->tf_rsi = scp->sc_esi;
730 	regs->tf_rdi = scp->sc_edi;
731 	regs->tf_cs = scp->sc_cs;
732 	regs->tf_ss = scp->sc_ss;
733 	regs->tf_rbp = scp->sc_ebp;
734 	regs->tf_rsp = scp->sc_esp;
735 	regs->tf_rip = scp->sc_eip;
736 	regs->tf_rflags = eflags;
737 
738 	if (scp->sc_onstack & 1)
739 		td->td_sigstk.ss_flags |= SS_ONSTACK;
740 	else
741 		td->td_sigstk.ss_flags &= ~SS_ONSTACK;
742 
743 	kern_sigprocmask(td, SIG_SETMASK, (sigset_t *)&scp->sc_mask, NULL,
744 	    SIGPROCMASK_OLD);
745 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
746 	return (EJUSTRETURN);
747 }
748 #endif
749 
750 #ifdef COMPAT_FREEBSD4
751 int
752 freebsd4_freebsd32_sigreturn(struct thread *td,
753     struct freebsd4_freebsd32_sigreturn_args *uap)
754 {
755 	struct ia32_freebsd4_ucontext uc;
756 	struct trapframe *regs;
757 	struct ia32_freebsd4_ucontext *ucp;
758 	int cs, eflags, error;
759 	ksiginfo_t ksi;
760 
761 	error = copyin(uap->sigcntxp, &uc, sizeof(uc));
762 	if (error != 0)
763 		return (error);
764 	ucp = &uc;
765 	regs = td->td_frame;
766 	eflags = ucp->uc_mcontext.mc_eflags;
767 	/*
768 	 * Don't allow users to change privileged or reserved flags.
769 	 */
770 	if (!EFL_SECURE(eflags, regs->tf_rflags)) {
771 		uprintf("pid %d (%s): freebsd4_freebsd32_sigreturn eflags = 0x%x\n",
772 		    td->td_proc->p_pid, td->td_name, eflags);
773 		return (EINVAL);
774 	}
775 
776 	/*
777 	 * Don't allow users to load a valid privileged %cs.  Let the
778 	 * hardware check for invalid selectors, excess privilege in
779 	 * other selectors, invalid %eip's and invalid %esp's.
780 	 */
781 	cs = ucp->uc_mcontext.mc_cs;
782 	if (!CS_SECURE(cs)) {
783 		uprintf("pid %d (%s): freebsd4_sigreturn cs = 0x%x\n",
784 		    td->td_proc->p_pid, td->td_name, cs);
785 		ksiginfo_init_trap(&ksi);
786 		ksi.ksi_signo = SIGBUS;
787 		ksi.ksi_code = BUS_OBJERR;
788 		ksi.ksi_trapno = T_PROTFLT;
789 		ksi.ksi_addr = (void *)regs->tf_rip;
790 		trapsignal(td, &ksi);
791 		return (EINVAL);
792 	}
793 
794 	regs->tf_rdi = ucp->uc_mcontext.mc_edi;
795 	regs->tf_rsi = ucp->uc_mcontext.mc_esi;
796 	regs->tf_rbp = ucp->uc_mcontext.mc_ebp;
797 	regs->tf_rbx = ucp->uc_mcontext.mc_ebx;
798 	regs->tf_rdx = ucp->uc_mcontext.mc_edx;
799 	regs->tf_rcx = ucp->uc_mcontext.mc_ecx;
800 	regs->tf_rax = ucp->uc_mcontext.mc_eax;
801 	regs->tf_trapno = ucp->uc_mcontext.mc_trapno;
802 	regs->tf_err = ucp->uc_mcontext.mc_err;
803 	regs->tf_rip = ucp->uc_mcontext.mc_eip;
804 	regs->tf_cs = cs;
805 	regs->tf_rflags = ucp->uc_mcontext.mc_eflags;
806 	regs->tf_rsp = ucp->uc_mcontext.mc_esp;
807 	regs->tf_ss = ucp->uc_mcontext.mc_ss;
808 	regs->tf_ds = ucp->uc_mcontext.mc_ds;
809 	regs->tf_es = ucp->uc_mcontext.mc_es;
810 	regs->tf_fs = ucp->uc_mcontext.mc_fs;
811 	regs->tf_gs = ucp->uc_mcontext.mc_gs;
812 
813 	kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0);
814 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
815 	return (EJUSTRETURN);
816 }
817 #endif	/* COMPAT_FREEBSD4 */
818 
819 int
820 freebsd32_sigreturn(struct thread *td, struct freebsd32_sigreturn_args *uap)
821 {
822 	struct ia32_ucontext uc;
823 	struct trapframe *regs;
824 	struct ia32_ucontext *ucp;
825 	char *xfpustate;
826 	size_t xfpustate_len;
827 	int cs, eflags, error, ret;
828 	ksiginfo_t ksi;
829 
830 	error = copyin(uap->sigcntxp, &uc, sizeof(uc));
831 	if (error != 0)
832 		return (error);
833 	ucp = &uc;
834 	regs = td->td_frame;
835 	eflags = ucp->uc_mcontext.mc_eflags;
836 	/*
837 	 * Don't allow users to change privileged or reserved flags.
838 	 */
839 	if (!EFL_SECURE(eflags, regs->tf_rflags)) {
840 		uprintf("pid %d (%s): freebsd32_sigreturn eflags = 0x%x\n",
841 		    td->td_proc->p_pid, td->td_name, eflags);
842 		return (EINVAL);
843 	}
844 
845 	/*
846 	 * Don't allow users to load a valid privileged %cs.  Let the
847 	 * hardware check for invalid selectors, excess privilege in
848 	 * other selectors, invalid %eip's and invalid %esp's.
849 	 */
850 	cs = ucp->uc_mcontext.mc_cs;
851 	if (!CS_SECURE(cs)) {
852 		uprintf("pid %d (%s): sigreturn cs = 0x%x\n",
853 		    td->td_proc->p_pid, td->td_name, cs);
854 		ksiginfo_init_trap(&ksi);
855 		ksi.ksi_signo = SIGBUS;
856 		ksi.ksi_code = BUS_OBJERR;
857 		ksi.ksi_trapno = T_PROTFLT;
858 		ksi.ksi_addr = (void *)regs->tf_rip;
859 		trapsignal(td, &ksi);
860 		return (EINVAL);
861 	}
862 
863 	if ((ucp->uc_mcontext.mc_flags & _MC_HASFPXSTATE) != 0) {
864 		xfpustate_len = uc.uc_mcontext.mc_xfpustate_len;
865 		if (xfpustate_len > cpu_max_ext_state_size -
866 		    sizeof(struct savefpu)) {
867 			uprintf("pid %d (%s): sigreturn xfpusave_len = 0x%zx\n",
868 			    td->td_proc->p_pid, td->td_name, xfpustate_len);
869 			return (EINVAL);
870 		}
871 		xfpustate = (char *)fpu_save_area_alloc();
872 		error = copyin(PTRIN(ucp->uc_mcontext.mc_xfpustate),
873 		    xfpustate, xfpustate_len);
874 		if (error != 0) {
875 			fpu_save_area_free((struct savefpu *)xfpustate);
876 			uprintf(
877 	"pid %d (%s): sigreturn copying xfpustate failed\n",
878 			    td->td_proc->p_pid, td->td_name);
879 			return (error);
880 		}
881 	} else {
882 		xfpustate = NULL;
883 		xfpustate_len = 0;
884 	}
885 	ret = ia32_set_fpcontext(td, &ucp->uc_mcontext, xfpustate,
886 	    xfpustate_len);
887 	fpu_save_area_free((struct savefpu *)xfpustate);
888 	if (ret != 0) {
889 		uprintf("pid %d (%s): sigreturn set_fpcontext err %d\n",
890 		    td->td_proc->p_pid, td->td_name, ret);
891 		return (ret);
892 	}
893 
894 	regs->tf_rdi = ucp->uc_mcontext.mc_edi;
895 	regs->tf_rsi = ucp->uc_mcontext.mc_esi;
896 	regs->tf_rbp = ucp->uc_mcontext.mc_ebp;
897 	regs->tf_rbx = ucp->uc_mcontext.mc_ebx;
898 	regs->tf_rdx = ucp->uc_mcontext.mc_edx;
899 	regs->tf_rcx = ucp->uc_mcontext.mc_ecx;
900 	regs->tf_rax = ucp->uc_mcontext.mc_eax;
901 	regs->tf_trapno = ucp->uc_mcontext.mc_trapno;
902 	regs->tf_err = ucp->uc_mcontext.mc_err;
903 	regs->tf_rip = ucp->uc_mcontext.mc_eip;
904 	regs->tf_cs = cs;
905 	regs->tf_rflags = ucp->uc_mcontext.mc_eflags;
906 	regs->tf_rsp = ucp->uc_mcontext.mc_esp;
907 	regs->tf_ss = ucp->uc_mcontext.mc_ss;
908 	regs->tf_ds = ucp->uc_mcontext.mc_ds;
909 	regs->tf_es = ucp->uc_mcontext.mc_es;
910 	regs->tf_fs = ucp->uc_mcontext.mc_fs;
911 	regs->tf_gs = ucp->uc_mcontext.mc_gs;
912 	regs->tf_flags = TF_HASSEGS;
913 
914 	kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0);
915 	set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
916 	return (EJUSTRETURN);
917 }
918 
919 /*
920  * Clear registers on exec
921  */
922 void
923 ia32_setregs(struct thread *td, struct image_params *imgp, uintptr_t stack)
924 {
925 	struct trapframe *regs;
926 	struct pcb *pcb;
927 	register_t saved_rflags;
928 
929 	regs = td->td_frame;
930 	pcb = td->td_pcb;
931 
932 	if (td->td_proc->p_md.md_ldt != NULL)
933 		user_ldt_free(td);
934 #ifdef COMPAT_43
935 	setup_lcall_gate();
936 #endif
937 
938 	pcb->pcb_fsbase = 0;
939 	pcb->pcb_gsbase = 0;
940 	pcb->pcb_initial_fpucw = __INITIAL_FPUCW_I386__;
941 
942 	saved_rflags = regs->tf_rflags & PSL_T;
943 	bzero((char *)regs, sizeof(struct trapframe));
944 	regs->tf_rip = imgp->entry_addr;
945 	regs->tf_rsp = stack;
946 	regs->tf_rflags = PSL_USER | saved_rflags;
947 	regs->tf_ss = _udatasel;
948 	regs->tf_cs = _ucode32sel;
949 	regs->tf_rbx = (register_t)imgp->ps_strings;
950 	regs->tf_ds = _udatasel;
951 	regs->tf_es = _udatasel;
952 	regs->tf_fs = _ufssel;
953 	regs->tf_gs = _ugssel;
954 	regs->tf_flags = TF_HASSEGS;
955 
956 	x86_clear_dbregs(pcb);
957 
958 	fpstate_drop(td);
959 
960 	/* Return via doreti so that we can change to a different %cs */
961 	set_pcb_flags(pcb, PCB_32BIT | PCB_FULL_IRET);
962 }
963