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