2 * Copyright (c) 2003 Peter Wemm
3 * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "opt_compat.h"
39 #include <sys/param.h>
41 #include <sys/fcntl.h>
42 #include <sys/imgact.h>
43 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/mutex.h>
48 #include <sys/namei.h>
49 #include <sys/pioctl.h>
51 #include <sys/procfs.h>
52 #include <sys/resourcevar.h>
53 #include <sys/systm.h>
54 #include <sys/signalvar.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>
64 #include <vm/vm_kern.h>
65 #include <vm/vm_param.h>
67 #include <vm/vm_map.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_extern.h>
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>
84 #ifdef COMPAT_FREEBSD4
85 static void freebsd4_ia32_sendsig(sig_t, ksiginfo_t *, sigset_t *);
88 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
89 #define EFL_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
92 ia32_get_fpcontext(struct thread *td, struct ia32_mcontext *mcp,
93 char *xfpusave, size_t xfpusave_len)
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.
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)
109 max_len = cpu_max_ext_state_size - sizeof(struct savefpu);
113 bzero(xfpusave + max_len, len - max_len);
115 mcp->mc_flags |= _MC_IA32_HASFPXSTATE;
116 mcp->mc_xfpustate_len = len;
117 bcopy(get_pcb_user_save_td(td) + 1, xfpusave, len);
121 ia32_set_fpcontext(struct thread *td, const struct ia32_mcontext *mcp,
122 char *xfpustate, size_t xfpustate_len)
126 if (mcp->mc_fpformat == _MC_FPFMT_NODEV)
128 else if (mcp->mc_fpformat != _MC_FPFMT_XMM)
130 else if (mcp->mc_ownedfp == _MC_FPOWNED_NONE) {
131 /* We don't care what state is left in the FPU or PCB. */
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);
144 * Get machine context.
147 ia32_get_mcontext(struct thread *td, struct ia32_mcontext *mcp, int flags)
150 struct trapframe *tp;
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) {
171 mcp->mc_eflags &= ~PSL_C;
173 mcp->mc_eax = tp->tf_rax;
174 mcp->mc_edx = tp->tf_rdx;
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));
194 * Set machine context.
196 * However, we don't set any but the user modifiable flags, and we won't
197 * touch the cs selector.
200 ia32_set_mcontext(struct thread *td, const struct ia32_mcontext *mcp)
202 struct trapframe *tp;
208 if (mcp->mc_len != sizeof(*mcp))
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))
216 xfpustate = __builtin_alloca(mcp->mc_xfpustate_len);
217 ret = copyin(PTRIN(mcp->mc_xfpustate), xfpustate,
218 mcp->mc_xfpustate_len);
223 ret = ia32_set_fpcontext(td, mcp, xfpustate, mcp->mc_xfpustate_len);
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;
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);
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.
252 #define UC_COPY_SIZE offsetof(struct ia32_ucontext, uc_link)
255 freebsd32_getcontext(struct thread *td, struct freebsd32_getcontext_args *uap)
257 struct ia32_ucontext uc;
260 if (uap->ucp == NULL)
263 ia32_get_mcontext(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
264 PROC_LOCK(td->td_proc);
265 uc.uc_sigmask = td->td_sigmask;
266 PROC_UNLOCK(td->td_proc);
267 bzero(&uc.__spare__, sizeof(uc.__spare__));
268 ret = copyout(&uc, uap->ucp, UC_COPY_SIZE);
274 freebsd32_setcontext(struct thread *td, struct freebsd32_setcontext_args *uap)
276 struct ia32_ucontext uc;
279 if (uap->ucp == NULL)
282 ret = copyin(uap->ucp, &uc, UC_COPY_SIZE);
284 ret = ia32_set_mcontext(td, &uc.uc_mcontext);
286 kern_sigprocmask(td, SIG_SETMASK,
287 &uc.uc_sigmask, NULL, 0);
291 return (ret == 0 ? EJUSTRETURN : ret);
295 freebsd32_swapcontext(struct thread *td, struct freebsd32_swapcontext_args *uap)
297 struct ia32_ucontext uc;
300 if (uap->oucp == NULL || uap->ucp == NULL)
303 ia32_get_mcontext(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
304 PROC_LOCK(td->td_proc);
305 uc.uc_sigmask = td->td_sigmask;
306 PROC_UNLOCK(td->td_proc);
307 ret = copyout(&uc, uap->oucp, UC_COPY_SIZE);
309 ret = copyin(uap->ucp, &uc, UC_COPY_SIZE);
311 ret = ia32_set_mcontext(td, &uc.uc_mcontext);
313 kern_sigprocmask(td, SIG_SETMASK,
314 &uc.uc_sigmask, NULL, 0);
319 return (ret == 0 ? EJUSTRETURN : ret);
323 * Send an interrupt to process.
325 * Stack is set up to allow sigcode stored
326 * at top to call routine, followed by kcall
327 * to sigreturn routine below. After sigreturn
328 * resets the signal mask, the stack, and the
329 * frame pointer, it returns to the user
335 ia32_osendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
337 struct ia32_sigframe3 sf, *fp;
341 struct trapframe *regs;
347 PROC_LOCK_ASSERT(p, MA_OWNED);
348 sig = ksi->ksi_signo;
350 mtx_assert(&psp->ps_mtx, MA_OWNED);
352 oonstack = sigonstack(regs->tf_rsp);
354 /* Allocate space for the signal handler context. */
355 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
356 SIGISMEMBER(psp->ps_sigonstack, sig)) {
357 fp = (struct ia32_sigframe3 *)(td->td_sigstk.ss_sp +
358 td->td_sigstk.ss_size - sizeof(sf));
359 td->td_sigstk.ss_flags |= SS_ONSTACK;
361 fp = (struct ia32_sigframe3 *)regs->tf_rsp - 1;
363 /* Translate the signal if appropriate. */
364 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
365 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
367 /* Build the argument list for the signal handler. */
369 sf.sf_scp = (register_t)&fp->sf_siginfo.si_sc;
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;
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;
382 mtx_unlock(&psp->ps_mtx);
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;
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;
411 * Copy the sigframe out to the user's stack.
413 if (copyout(&sf, fp, sizeof(*fp)) != 0) {
415 printf("process %ld has trashed its stack\n", (long)p->p_pid);
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);
431 mtx_lock(&psp->ps_mtx);
435 #ifdef COMPAT_FREEBSD4
437 freebsd4_ia32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
439 struct ia32_sigframe4 sf, *sfp;
440 struct siginfo32 siginfo;
444 struct trapframe *regs;
450 siginfo_to_siginfo32(&ksi->ksi_info, &siginfo);
452 PROC_LOCK_ASSERT(p, MA_OWNED);
453 sig = siginfo.si_signo;
455 mtx_assert(&psp->ps_mtx, MA_OWNED);
457 oonstack = sigonstack(regs->tf_rsp);
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__));
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 *)(td->td_sigstk.ss_sp +
496 td->td_sigstk.ss_size - sizeof(sf));
498 sfp = (struct ia32_sigframe4 *)regs->tf_rsp - 1;
501 /* Translate the signal if appropriate. */
502 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
503 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
505 /* Build the argument list for the signal handler. */
507 sf.sf_ucontext = (register_t)&sfp->sf_uc;
508 bzero(&sf.sf_si, sizeof(sf.sf_si));
509 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
510 /* Signal handler installed with SA_SIGINFO. */
511 sf.sf_siginfo = (u_int32_t)(uintptr_t)&sfp->sf_si;
512 sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
514 /* Fill in POSIX parts */
516 sf.sf_si.si_signo = sig;
518 /* Old FreeBSD-style arguments. */
519 sf.sf_siginfo = siginfo.si_code;
520 sf.sf_addr = (u_int32_t)siginfo.si_addr;
521 sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
523 mtx_unlock(&psp->ps_mtx);
526 * Copy the sigframe out to the user's stack.
528 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
530 printf("process %ld has trashed its stack\n", (long)p->p_pid);
536 regs->tf_rsp = (uintptr_t)sfp;
537 regs->tf_rip = p->p_sysent->sv_sigcode_base + sz_ia32_sigcode -
538 sz_freebsd4_ia32_sigcode;
539 regs->tf_rflags &= ~(PSL_T | PSL_D);
540 regs->tf_cs = _ucode32sel;
541 regs->tf_ss = _udatasel;
542 regs->tf_ds = _udatasel;
543 regs->tf_es = _udatasel;
544 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
545 /* leave user %fs and %gs untouched */
547 mtx_lock(&psp->ps_mtx);
549 #endif /* COMPAT_FREEBSD4 */
552 ia32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
554 struct ia32_sigframe sf, *sfp;
555 struct siginfo32 siginfo;
560 struct trapframe *regs;
566 siginfo_to_siginfo32(&ksi->ksi_info, &siginfo);
569 PROC_LOCK_ASSERT(p, MA_OWNED);
570 sig = siginfo.si_signo;
572 #ifdef COMPAT_FREEBSD4
573 if (SIGISMEMBER(psp->ps_freebsd4, sig)) {
574 freebsd4_ia32_sendsig(catcher, ksi, mask);
579 if (SIGISMEMBER(psp->ps_osigset, sig)) {
580 ia32_osendsig(catcher, ksi, mask);
584 mtx_assert(&psp->ps_mtx, MA_OWNED);
586 oonstack = sigonstack(regs->tf_rsp);
588 if (cpu_max_ext_state_size > sizeof(struct savefpu) && use_xsave) {
589 xfpusave_len = cpu_max_ext_state_size - sizeof(struct savefpu);
590 xfpusave = __builtin_alloca(xfpusave_len);
596 /* Save user context. */
597 bzero(&sf, sizeof(sf));
598 sf.sf_uc.uc_sigmask = *mask;
599 sf.sf_uc.uc_stack.ss_sp = (uintptr_t)td->td_sigstk.ss_sp;
600 sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
601 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
602 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
603 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
604 sf.sf_uc.uc_mcontext.mc_edi = regs->tf_rdi;
605 sf.sf_uc.uc_mcontext.mc_esi = regs->tf_rsi;
606 sf.sf_uc.uc_mcontext.mc_ebp = regs->tf_rbp;
607 sf.sf_uc.uc_mcontext.mc_isp = regs->tf_rsp; /* XXX */
608 sf.sf_uc.uc_mcontext.mc_ebx = regs->tf_rbx;
609 sf.sf_uc.uc_mcontext.mc_edx = regs->tf_rdx;
610 sf.sf_uc.uc_mcontext.mc_ecx = regs->tf_rcx;
611 sf.sf_uc.uc_mcontext.mc_eax = regs->tf_rax;
612 sf.sf_uc.uc_mcontext.mc_trapno = regs->tf_trapno;
613 sf.sf_uc.uc_mcontext.mc_err = regs->tf_err;
614 sf.sf_uc.uc_mcontext.mc_eip = regs->tf_rip;
615 sf.sf_uc.uc_mcontext.mc_cs = regs->tf_cs;
616 sf.sf_uc.uc_mcontext.mc_eflags = regs->tf_rflags;
617 sf.sf_uc.uc_mcontext.mc_esp = regs->tf_rsp;
618 sf.sf_uc.uc_mcontext.mc_ss = regs->tf_ss;
619 sf.sf_uc.uc_mcontext.mc_ds = regs->tf_ds;
620 sf.sf_uc.uc_mcontext.mc_es = regs->tf_es;
621 sf.sf_uc.uc_mcontext.mc_fs = regs->tf_fs;
622 sf.sf_uc.uc_mcontext.mc_gs = regs->tf_gs;
623 sf.sf_uc.uc_mcontext.mc_len = sizeof(sf.sf_uc.uc_mcontext); /* magic */
624 ia32_get_fpcontext(td, &sf.sf_uc.uc_mcontext, xfpusave, xfpusave_len);
626 sf.sf_uc.uc_mcontext.mc_fsbase = td->td_pcb->pcb_fsbase;
627 sf.sf_uc.uc_mcontext.mc_gsbase = td->td_pcb->pcb_gsbase;
628 bzero(sf.sf_uc.__spare__, sizeof(sf.sf_uc.__spare__));
630 /* Allocate space for the signal handler context. */
631 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
632 SIGISMEMBER(psp->ps_sigonstack, sig))
633 sp = td->td_sigstk.ss_sp + td->td_sigstk.ss_size;
635 sp = (char *)regs->tf_rsp;
636 if (xfpusave != NULL) {
638 sp = (char *)((unsigned long)sp & ~0x3Ful);
639 sf.sf_uc.uc_mcontext.mc_xfpustate = (register_t)sp;
642 /* Align to 16 bytes. */
643 sfp = (struct ia32_sigframe *)((uintptr_t)sp & ~0xF);
646 /* Translate the signal if appropriate. */
647 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
648 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
650 /* Build the argument list for the signal handler. */
652 sf.sf_ucontext = (register_t)&sfp->sf_uc;
653 bzero(&sf.sf_si, sizeof(sf.sf_si));
654 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
655 /* Signal handler installed with SA_SIGINFO. */
656 sf.sf_siginfo = (u_int32_t)(uintptr_t)&sfp->sf_si;
657 sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
659 /* Fill in POSIX parts */
661 sf.sf_si.si_signo = sig;
663 /* Old FreeBSD-style arguments. */
664 sf.sf_siginfo = siginfo.si_code;
665 sf.sf_addr = (u_int32_t)siginfo.si_addr;
666 sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
668 mtx_unlock(&psp->ps_mtx);
671 * Copy the sigframe out to the user's stack.
673 if (copyout(&sf, sfp, sizeof(*sfp)) != 0 ||
674 (xfpusave != NULL && copyout(xfpusave,
675 PTRIN(sf.sf_uc.uc_mcontext.mc_xfpustate), xfpusave_len)
678 printf("process %ld has trashed its stack\n", (long)p->p_pid);
684 regs->tf_rsp = (uintptr_t)sfp;
685 regs->tf_rip = p->p_sysent->sv_sigcode_base;
686 regs->tf_rflags &= ~(PSL_T | PSL_D);
687 regs->tf_cs = _ucode32sel;
688 regs->tf_ss = _udatasel;
689 regs->tf_ds = _udatasel;
690 regs->tf_es = _udatasel;
691 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
692 /* XXXKIB leave user %fs and %gs untouched */
694 mtx_lock(&psp->ps_mtx);
698 * System call to cleanup state after a signal
699 * has been taken. Reset signal mask and
700 * stack state from context left by sendsig (above).
701 * Return to previous pc and psl as specified by
702 * context left by sendsig. Check carefully to
703 * make sure that the user has not modified the
704 * state to gain improper privileges.
709 ofreebsd32_sigreturn(struct thread *td, struct ofreebsd32_sigreturn_args *uap)
711 struct ia32_sigcontext3 sc, *scp;
712 struct trapframe *regs;
717 error = copyin(uap->sigcntxp, &sc, sizeof(sc));
721 eflags = scp->sc_eflags;
722 if (!EFL_SECURE(eflags, regs->tf_rflags)) {
725 if (!CS_SECURE(scp->sc_cs)) {
726 ksiginfo_init_trap(&ksi);
727 ksi.ksi_signo = SIGBUS;
728 ksi.ksi_code = BUS_OBJERR;
729 ksi.ksi_trapno = T_PROTFLT;
730 ksi.ksi_addr = (void *)regs->tf_rip;
731 trapsignal(td, &ksi);
734 regs->tf_ds = scp->sc_ds;
735 regs->tf_es = scp->sc_es;
736 regs->tf_fs = scp->sc_fs;
737 regs->tf_gs = scp->sc_gs;
739 regs->tf_rax = scp->sc_eax;
740 regs->tf_rbx = scp->sc_ebx;
741 regs->tf_rcx = scp->sc_ecx;
742 regs->tf_rdx = scp->sc_edx;
743 regs->tf_rsi = scp->sc_esi;
744 regs->tf_rdi = scp->sc_edi;
745 regs->tf_cs = scp->sc_cs;
746 regs->tf_ss = scp->sc_ss;
747 regs->tf_rbp = scp->sc_ebp;
748 regs->tf_rsp = scp->sc_esp;
749 regs->tf_rip = scp->sc_eip;
750 regs->tf_rflags = eflags;
752 if (scp->sc_onstack & 1)
753 td->td_sigstk.ss_flags |= SS_ONSTACK;
755 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
757 kern_sigprocmask(td, SIG_SETMASK, (sigset_t *)&scp->sc_mask, NULL,
759 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
760 return (EJUSTRETURN);
764 #ifdef COMPAT_FREEBSD4
769 freebsd4_freebsd32_sigreturn(td, uap)
771 struct freebsd4_freebsd32_sigreturn_args /* {
772 const struct freebsd4_freebsd32_ucontext *sigcntxp;
775 struct ia32_ucontext4 uc;
776 struct trapframe *regs;
777 struct ia32_ucontext4 *ucp;
778 int cs, eflags, error;
781 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
786 eflags = ucp->uc_mcontext.mc_eflags;
788 * Don't allow users to change privileged or reserved flags.
790 if (!EFL_SECURE(eflags, regs->tf_rflags)) {
791 uprintf("pid %d (%s): freebsd4_freebsd32_sigreturn eflags = 0x%x\n",
792 td->td_proc->p_pid, td->td_name, eflags);
797 * Don't allow users to load a valid privileged %cs. Let the
798 * hardware check for invalid selectors, excess privilege in
799 * other selectors, invalid %eip's and invalid %esp's.
801 cs = ucp->uc_mcontext.mc_cs;
802 if (!CS_SECURE(cs)) {
803 uprintf("pid %d (%s): freebsd4_sigreturn cs = 0x%x\n",
804 td->td_proc->p_pid, td->td_name, cs);
805 ksiginfo_init_trap(&ksi);
806 ksi.ksi_signo = SIGBUS;
807 ksi.ksi_code = BUS_OBJERR;
808 ksi.ksi_trapno = T_PROTFLT;
809 ksi.ksi_addr = (void *)regs->tf_rip;
810 trapsignal(td, &ksi);
814 regs->tf_rdi = ucp->uc_mcontext.mc_edi;
815 regs->tf_rsi = ucp->uc_mcontext.mc_esi;
816 regs->tf_rbp = ucp->uc_mcontext.mc_ebp;
817 regs->tf_rbx = ucp->uc_mcontext.mc_ebx;
818 regs->tf_rdx = ucp->uc_mcontext.mc_edx;
819 regs->tf_rcx = ucp->uc_mcontext.mc_ecx;
820 regs->tf_rax = ucp->uc_mcontext.mc_eax;
821 regs->tf_trapno = ucp->uc_mcontext.mc_trapno;
822 regs->tf_err = ucp->uc_mcontext.mc_err;
823 regs->tf_rip = ucp->uc_mcontext.mc_eip;
825 regs->tf_rflags = ucp->uc_mcontext.mc_eflags;
826 regs->tf_rsp = ucp->uc_mcontext.mc_esp;
827 regs->tf_ss = ucp->uc_mcontext.mc_ss;
828 regs->tf_ds = ucp->uc_mcontext.mc_ds;
829 regs->tf_es = ucp->uc_mcontext.mc_es;
830 regs->tf_fs = ucp->uc_mcontext.mc_fs;
831 regs->tf_gs = ucp->uc_mcontext.mc_gs;
833 kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0);
834 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
835 return (EJUSTRETURN);
837 #endif /* COMPAT_FREEBSD4 */
843 freebsd32_sigreturn(td, uap)
845 struct freebsd32_sigreturn_args /* {
846 const struct freebsd32_ucontext *sigcntxp;
849 struct ia32_ucontext uc;
850 struct trapframe *regs;
851 struct ia32_ucontext *ucp;
853 size_t xfpustate_len;
854 int cs, eflags, error, ret;
857 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
862 eflags = ucp->uc_mcontext.mc_eflags;
864 * Don't allow users to change privileged or reserved flags.
866 if (!EFL_SECURE(eflags, regs->tf_rflags)) {
867 uprintf("pid %d (%s): freebsd32_sigreturn eflags = 0x%x\n",
868 td->td_proc->p_pid, td->td_name, eflags);
873 * Don't allow users to load a valid privileged %cs. Let the
874 * hardware check for invalid selectors, excess privilege in
875 * other selectors, invalid %eip's and invalid %esp's.
877 cs = ucp->uc_mcontext.mc_cs;
878 if (!CS_SECURE(cs)) {
879 uprintf("pid %d (%s): sigreturn cs = 0x%x\n",
880 td->td_proc->p_pid, td->td_name, cs);
881 ksiginfo_init_trap(&ksi);
882 ksi.ksi_signo = SIGBUS;
883 ksi.ksi_code = BUS_OBJERR;
884 ksi.ksi_trapno = T_PROTFLT;
885 ksi.ksi_addr = (void *)regs->tf_rip;
886 trapsignal(td, &ksi);
890 if ((ucp->uc_mcontext.mc_flags & _MC_HASFPXSTATE) != 0) {
891 xfpustate_len = uc.uc_mcontext.mc_xfpustate_len;
892 if (xfpustate_len > cpu_max_ext_state_size -
893 sizeof(struct savefpu)) {
894 uprintf("pid %d (%s): sigreturn xfpusave_len = 0x%zx\n",
895 td->td_proc->p_pid, td->td_name, xfpustate_len);
898 xfpustate = __builtin_alloca(xfpustate_len);
899 error = copyin(PTRIN(ucp->uc_mcontext.mc_xfpustate),
900 xfpustate, xfpustate_len);
903 "pid %d (%s): sigreturn copying xfpustate failed\n",
904 td->td_proc->p_pid, td->td_name);
911 ret = ia32_set_fpcontext(td, &ucp->uc_mcontext, xfpustate,
914 uprintf("pid %d (%s): sigreturn set_fpcontext err %d\n",
915 td->td_proc->p_pid, td->td_name, ret);
919 regs->tf_rdi = ucp->uc_mcontext.mc_edi;
920 regs->tf_rsi = ucp->uc_mcontext.mc_esi;
921 regs->tf_rbp = ucp->uc_mcontext.mc_ebp;
922 regs->tf_rbx = ucp->uc_mcontext.mc_ebx;
923 regs->tf_rdx = ucp->uc_mcontext.mc_edx;
924 regs->tf_rcx = ucp->uc_mcontext.mc_ecx;
925 regs->tf_rax = ucp->uc_mcontext.mc_eax;
926 regs->tf_trapno = ucp->uc_mcontext.mc_trapno;
927 regs->tf_err = ucp->uc_mcontext.mc_err;
928 regs->tf_rip = ucp->uc_mcontext.mc_eip;
930 regs->tf_rflags = ucp->uc_mcontext.mc_eflags;
931 regs->tf_rsp = ucp->uc_mcontext.mc_esp;
932 regs->tf_ss = ucp->uc_mcontext.mc_ss;
933 regs->tf_ds = ucp->uc_mcontext.mc_ds;
934 regs->tf_es = ucp->uc_mcontext.mc_es;
935 regs->tf_fs = ucp->uc_mcontext.mc_fs;
936 regs->tf_gs = ucp->uc_mcontext.mc_gs;
937 regs->tf_flags = TF_HASSEGS;
939 kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0);
940 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
941 return (EJUSTRETURN);
945 * Clear registers on exec
948 ia32_setregs(struct thread *td, struct image_params *imgp, u_long stack)
950 struct trapframe *regs = td->td_frame;
951 struct pcb *pcb = td->td_pcb;
954 if (td->td_proc->p_md.md_ldt != NULL)
957 mtx_unlock(&dt_lock);
964 pcb->pcb_initial_fpucw = __INITIAL_FPUCW_I386__;
966 bzero((char *)regs, sizeof(struct trapframe));
967 regs->tf_rip = imgp->entry_addr;
968 regs->tf_rsp = stack;
969 regs->tf_rflags = PSL_USER | (regs->tf_rflags & PSL_T);
970 regs->tf_ss = _udatasel;
971 regs->tf_cs = _ucode32sel;
972 regs->tf_rbx = imgp->ps_strings;
973 regs->tf_ds = _udatasel;
974 regs->tf_es = _udatasel;
975 regs->tf_fs = _ufssel;
976 regs->tf_gs = _ugssel;
977 regs->tf_flags = TF_HASSEGS;
981 /* Return via doreti so that we can change to a different %cs */
982 set_pcb_flags(pcb, PCB_32BIT | PCB_FULL_IRET);
983 td->td_retval[1] = 0;