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
332 #ifdef COMPAT_FREEBSD4
334 freebsd4_ia32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
336 struct ia32_sigframe4 sf, *sfp;
337 struct siginfo32 siginfo;
341 struct trapframe *regs;
347 siginfo_to_siginfo32(&ksi->ksi_info, &siginfo);
349 PROC_LOCK_ASSERT(p, MA_OWNED);
350 sig = siginfo.si_signo;
352 mtx_assert(&psp->ps_mtx, MA_OWNED);
354 oonstack = sigonstack(regs->tf_rsp);
356 /* Save user context. */
357 bzero(&sf, sizeof(sf));
358 sf.sf_uc.uc_sigmask = *mask;
359 sf.sf_uc.uc_stack.ss_sp = (uintptr_t)td->td_sigstk.ss_sp;
360 sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
361 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
362 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
363 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
364 sf.sf_uc.uc_mcontext.mc_edi = regs->tf_rdi;
365 sf.sf_uc.uc_mcontext.mc_esi = regs->tf_rsi;
366 sf.sf_uc.uc_mcontext.mc_ebp = regs->tf_rbp;
367 sf.sf_uc.uc_mcontext.mc_isp = regs->tf_rsp; /* XXX */
368 sf.sf_uc.uc_mcontext.mc_ebx = regs->tf_rbx;
369 sf.sf_uc.uc_mcontext.mc_edx = regs->tf_rdx;
370 sf.sf_uc.uc_mcontext.mc_ecx = regs->tf_rcx;
371 sf.sf_uc.uc_mcontext.mc_eax = regs->tf_rax;
372 sf.sf_uc.uc_mcontext.mc_trapno = regs->tf_trapno;
373 sf.sf_uc.uc_mcontext.mc_err = regs->tf_err;
374 sf.sf_uc.uc_mcontext.mc_eip = regs->tf_rip;
375 sf.sf_uc.uc_mcontext.mc_cs = regs->tf_cs;
376 sf.sf_uc.uc_mcontext.mc_eflags = regs->tf_rflags;
377 sf.sf_uc.uc_mcontext.mc_esp = regs->tf_rsp;
378 sf.sf_uc.uc_mcontext.mc_ss = regs->tf_ss;
379 sf.sf_uc.uc_mcontext.mc_ds = regs->tf_ds;
380 sf.sf_uc.uc_mcontext.mc_es = regs->tf_es;
381 sf.sf_uc.uc_mcontext.mc_fs = regs->tf_fs;
382 sf.sf_uc.uc_mcontext.mc_gs = regs->tf_gs;
383 bzero(sf.sf_uc.uc_mcontext.mc_fpregs,
384 sizeof(sf.sf_uc.uc_mcontext.mc_fpregs));
385 bzero(sf.sf_uc.uc_mcontext.__spare__,
386 sizeof(sf.sf_uc.uc_mcontext.__spare__));
387 bzero(sf.sf_uc.__spare__, sizeof(sf.sf_uc.__spare__));
389 /* Allocate space for the signal handler context. */
390 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
391 SIGISMEMBER(psp->ps_sigonstack, sig)) {
392 sfp = (struct ia32_sigframe4 *)(td->td_sigstk.ss_sp +
393 td->td_sigstk.ss_size - sizeof(sf));
395 sfp = (struct ia32_sigframe4 *)regs->tf_rsp - 1;
398 /* Translate the signal if appropriate. */
399 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
400 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
402 /* Build the argument list for the signal handler. */
404 sf.sf_ucontext = (register_t)&sfp->sf_uc;
405 bzero(&sf.sf_si, sizeof(sf.sf_si));
406 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
407 /* Signal handler installed with SA_SIGINFO. */
408 sf.sf_siginfo = (u_int32_t)(uintptr_t)&sfp->sf_si;
409 sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
411 /* Fill in POSIX parts */
413 sf.sf_si.si_signo = sig;
415 /* Old FreeBSD-style arguments. */
416 sf.sf_siginfo = siginfo.si_code;
417 sf.sf_addr = (u_int32_t)siginfo.si_addr;
418 sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
420 mtx_unlock(&psp->ps_mtx);
423 * Copy the sigframe out to the user's stack.
425 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
427 printf("process %ld has trashed its stack\n", (long)p->p_pid);
433 regs->tf_rsp = (uintptr_t)sfp;
434 regs->tf_rip = p->p_sysent->sv_psstrings - sz_freebsd4_ia32_sigcode;
435 regs->tf_rflags &= ~(PSL_T | PSL_D);
436 regs->tf_cs = _ucode32sel;
437 regs->tf_ss = _udatasel;
438 regs->tf_ds = _udatasel;
439 regs->tf_es = _udatasel;
440 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
441 /* leave user %fs and %gs untouched */
443 mtx_lock(&psp->ps_mtx);
445 #endif /* COMPAT_FREEBSD4 */
448 ia32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
450 struct ia32_sigframe sf, *sfp;
451 struct siginfo32 siginfo;
456 struct trapframe *regs;
462 siginfo_to_siginfo32(&ksi->ksi_info, &siginfo);
465 PROC_LOCK_ASSERT(p, MA_OWNED);
466 sig = siginfo.si_signo;
468 #ifdef COMPAT_FREEBSD4
469 if (SIGISMEMBER(psp->ps_freebsd4, sig)) {
470 freebsd4_ia32_sendsig(catcher, ksi, mask);
474 mtx_assert(&psp->ps_mtx, MA_OWNED);
476 oonstack = sigonstack(regs->tf_rsp);
478 if (cpu_max_ext_state_size > sizeof(struct savefpu) && use_xsave) {
479 xfpusave_len = cpu_max_ext_state_size - sizeof(struct savefpu);
480 xfpusave = __builtin_alloca(xfpusave_len);
486 /* Save user context. */
487 bzero(&sf, sizeof(sf));
488 sf.sf_uc.uc_sigmask = *mask;
489 sf.sf_uc.uc_stack.ss_sp = (uintptr_t)td->td_sigstk.ss_sp;
490 sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
491 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
492 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
493 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
494 sf.sf_uc.uc_mcontext.mc_edi = regs->tf_rdi;
495 sf.sf_uc.uc_mcontext.mc_esi = regs->tf_rsi;
496 sf.sf_uc.uc_mcontext.mc_ebp = regs->tf_rbp;
497 sf.sf_uc.uc_mcontext.mc_isp = regs->tf_rsp; /* XXX */
498 sf.sf_uc.uc_mcontext.mc_ebx = regs->tf_rbx;
499 sf.sf_uc.uc_mcontext.mc_edx = regs->tf_rdx;
500 sf.sf_uc.uc_mcontext.mc_ecx = regs->tf_rcx;
501 sf.sf_uc.uc_mcontext.mc_eax = regs->tf_rax;
502 sf.sf_uc.uc_mcontext.mc_trapno = regs->tf_trapno;
503 sf.sf_uc.uc_mcontext.mc_err = regs->tf_err;
504 sf.sf_uc.uc_mcontext.mc_eip = regs->tf_rip;
505 sf.sf_uc.uc_mcontext.mc_cs = regs->tf_cs;
506 sf.sf_uc.uc_mcontext.mc_eflags = regs->tf_rflags;
507 sf.sf_uc.uc_mcontext.mc_esp = regs->tf_rsp;
508 sf.sf_uc.uc_mcontext.mc_ss = regs->tf_ss;
509 sf.sf_uc.uc_mcontext.mc_ds = regs->tf_ds;
510 sf.sf_uc.uc_mcontext.mc_es = regs->tf_es;
511 sf.sf_uc.uc_mcontext.mc_fs = regs->tf_fs;
512 sf.sf_uc.uc_mcontext.mc_gs = regs->tf_gs;
513 sf.sf_uc.uc_mcontext.mc_len = sizeof(sf.sf_uc.uc_mcontext); /* magic */
514 ia32_get_fpcontext(td, &sf.sf_uc.uc_mcontext, xfpusave, xfpusave_len);
516 sf.sf_uc.uc_mcontext.mc_fsbase = td->td_pcb->pcb_fsbase;
517 sf.sf_uc.uc_mcontext.mc_gsbase = td->td_pcb->pcb_gsbase;
518 bzero(sf.sf_uc.__spare__, sizeof(sf.sf_uc.__spare__));
520 /* Allocate space for the signal handler context. */
521 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
522 SIGISMEMBER(psp->ps_sigonstack, sig))
523 sp = td->td_sigstk.ss_sp + td->td_sigstk.ss_size;
525 sp = (char *)regs->tf_rsp;
526 if (xfpusave != NULL) {
528 sp = (char *)((unsigned long)sp & ~0x3Ful);
529 sf.sf_uc.uc_mcontext.mc_xfpustate = (register_t)sp;
532 /* Align to 16 bytes. */
533 sfp = (struct ia32_sigframe *)((uintptr_t)sp & ~0xF);
536 /* Translate the signal if appropriate. */
537 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
538 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
540 /* Build the argument list for the signal handler. */
542 sf.sf_ucontext = (register_t)&sfp->sf_uc;
543 bzero(&sf.sf_si, sizeof(sf.sf_si));
544 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
545 /* Signal handler installed with SA_SIGINFO. */
546 sf.sf_siginfo = (u_int32_t)(uintptr_t)&sfp->sf_si;
547 sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
549 /* Fill in POSIX parts */
551 sf.sf_si.si_signo = sig;
553 /* Old FreeBSD-style arguments. */
554 sf.sf_siginfo = siginfo.si_code;
555 sf.sf_addr = (u_int32_t)siginfo.si_addr;
556 sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
558 mtx_unlock(&psp->ps_mtx);
561 * Copy the sigframe out to the user's stack.
563 if (copyout(&sf, sfp, sizeof(*sfp)) != 0 ||
564 (xfpusave != NULL && copyout(xfpusave,
565 PTRIN(sf.sf_uc.uc_mcontext.mc_xfpustate), xfpusave_len)
568 printf("process %ld has trashed its stack\n", (long)p->p_pid);
574 regs->tf_rsp = (uintptr_t)sfp;
575 regs->tf_rip = p->p_sysent->sv_psstrings - *(p->p_sysent->sv_szsigcode);
576 regs->tf_rflags &= ~(PSL_T | PSL_D);
577 regs->tf_cs = _ucode32sel;
578 regs->tf_ss = _udatasel;
579 regs->tf_ds = _udatasel;
580 regs->tf_es = _udatasel;
581 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
582 /* XXXKIB leave user %fs and %gs untouched */
584 mtx_lock(&psp->ps_mtx);
588 * System call to cleanup state after a signal
589 * has been taken. Reset signal mask and
590 * stack state from context left by sendsig (above).
591 * Return to previous pc and psl as specified by
592 * context left by sendsig. Check carefully to
593 * make sure that the user has not modified the
594 * state to gain improper privileges.
596 #ifdef COMPAT_FREEBSD4
601 freebsd4_freebsd32_sigreturn(td, uap)
603 struct freebsd4_freebsd32_sigreturn_args /* {
604 const struct freebsd4_freebsd32_ucontext *sigcntxp;
607 struct ia32_ucontext4 uc;
608 struct trapframe *regs;
609 struct ia32_ucontext4 *ucp;
610 int cs, eflags, error;
613 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
618 eflags = ucp->uc_mcontext.mc_eflags;
620 * Don't allow users to change privileged or reserved flags.
623 * XXX do allow users to change the privileged flag PSL_RF.
624 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
625 * should sometimes set it there too. tf_eflags is kept in
626 * the signal context during signal handling and there is no
627 * other place to remember it, so the PSL_RF bit may be
628 * corrupted by the signal handler without us knowing.
629 * Corruption of the PSL_RF bit at worst causes one more or
630 * one less debugger trap, so allowing it is fairly harmless.
632 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) {
633 uprintf("pid %d (%s): freebsd4_freebsd32_sigreturn eflags = 0x%x\n",
634 td->td_proc->p_pid, td->td_name, eflags);
639 * Don't allow users to load a valid privileged %cs. Let the
640 * hardware check for invalid selectors, excess privilege in
641 * other selectors, invalid %eip's and invalid %esp's.
643 cs = ucp->uc_mcontext.mc_cs;
644 if (!CS_SECURE(cs)) {
645 uprintf("pid %d (%s): freebsd4_sigreturn cs = 0x%x\n",
646 td->td_proc->p_pid, td->td_name, cs);
647 ksiginfo_init_trap(&ksi);
648 ksi.ksi_signo = SIGBUS;
649 ksi.ksi_code = BUS_OBJERR;
650 ksi.ksi_trapno = T_PROTFLT;
651 ksi.ksi_addr = (void *)regs->tf_rip;
652 trapsignal(td, &ksi);
656 regs->tf_rdi = ucp->uc_mcontext.mc_edi;
657 regs->tf_rsi = ucp->uc_mcontext.mc_esi;
658 regs->tf_rbp = ucp->uc_mcontext.mc_ebp;
659 regs->tf_rbx = ucp->uc_mcontext.mc_ebx;
660 regs->tf_rdx = ucp->uc_mcontext.mc_edx;
661 regs->tf_rcx = ucp->uc_mcontext.mc_ecx;
662 regs->tf_rax = ucp->uc_mcontext.mc_eax;
663 regs->tf_trapno = ucp->uc_mcontext.mc_trapno;
664 regs->tf_err = ucp->uc_mcontext.mc_err;
665 regs->tf_rip = ucp->uc_mcontext.mc_eip;
667 regs->tf_rflags = ucp->uc_mcontext.mc_eflags;
668 regs->tf_rsp = ucp->uc_mcontext.mc_esp;
669 regs->tf_ss = ucp->uc_mcontext.mc_ss;
670 regs->tf_ds = ucp->uc_mcontext.mc_ds;
671 regs->tf_es = ucp->uc_mcontext.mc_es;
672 regs->tf_fs = ucp->uc_mcontext.mc_fs;
673 regs->tf_gs = ucp->uc_mcontext.mc_gs;
675 kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0);
676 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
677 return (EJUSTRETURN);
679 #endif /* COMPAT_FREEBSD4 */
685 freebsd32_sigreturn(td, uap)
687 struct freebsd32_sigreturn_args /* {
688 const struct freebsd32_ucontext *sigcntxp;
691 struct ia32_ucontext uc;
692 struct trapframe *regs;
693 struct ia32_ucontext *ucp;
695 size_t xfpustate_len;
696 int cs, eflags, error, ret;
699 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
704 eflags = ucp->uc_mcontext.mc_eflags;
706 * Don't allow users to change privileged or reserved flags.
709 * XXX do allow users to change the privileged flag PSL_RF.
710 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
711 * should sometimes set it there too. tf_eflags is kept in
712 * the signal context during signal handling and there is no
713 * other place to remember it, so the PSL_RF bit may be
714 * corrupted by the signal handler without us knowing.
715 * Corruption of the PSL_RF bit at worst causes one more or
716 * one less debugger trap, so allowing it is fairly harmless.
718 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) {
719 uprintf("pid %d (%s): freebsd32_sigreturn eflags = 0x%x\n",
720 td->td_proc->p_pid, td->td_name, eflags);
725 * Don't allow users to load a valid privileged %cs. Let the
726 * hardware check for invalid selectors, excess privilege in
727 * other selectors, invalid %eip's and invalid %esp's.
729 cs = ucp->uc_mcontext.mc_cs;
730 if (!CS_SECURE(cs)) {
731 uprintf("pid %d (%s): sigreturn cs = 0x%x\n",
732 td->td_proc->p_pid, td->td_name, cs);
733 ksiginfo_init_trap(&ksi);
734 ksi.ksi_signo = SIGBUS;
735 ksi.ksi_code = BUS_OBJERR;
736 ksi.ksi_trapno = T_PROTFLT;
737 ksi.ksi_addr = (void *)regs->tf_rip;
738 trapsignal(td, &ksi);
742 if ((ucp->uc_mcontext.mc_flags & _MC_HASFPXSTATE) != 0) {
743 xfpustate_len = uc.uc_mcontext.mc_xfpustate_len;
744 if (xfpustate_len > cpu_max_ext_state_size -
745 sizeof(struct savefpu)) {
746 uprintf("pid %d (%s): sigreturn xfpusave_len = 0x%zx\n",
747 td->td_proc->p_pid, td->td_name, xfpustate_len);
750 xfpustate = __builtin_alloca(xfpustate_len);
751 error = copyin(PTRIN(ucp->uc_mcontext.mc_xfpustate),
752 xfpustate, xfpustate_len);
755 "pid %d (%s): sigreturn copying xfpustate failed\n",
756 td->td_proc->p_pid, td->td_name);
763 ret = ia32_set_fpcontext(td, &ucp->uc_mcontext, xfpustate,
766 uprintf("pid %d (%s): sigreturn set_fpcontext err %d\n",
767 td->td_proc->p_pid, td->td_name, ret);
771 regs->tf_rdi = ucp->uc_mcontext.mc_edi;
772 regs->tf_rsi = ucp->uc_mcontext.mc_esi;
773 regs->tf_rbp = ucp->uc_mcontext.mc_ebp;
774 regs->tf_rbx = ucp->uc_mcontext.mc_ebx;
775 regs->tf_rdx = ucp->uc_mcontext.mc_edx;
776 regs->tf_rcx = ucp->uc_mcontext.mc_ecx;
777 regs->tf_rax = ucp->uc_mcontext.mc_eax;
778 regs->tf_trapno = ucp->uc_mcontext.mc_trapno;
779 regs->tf_err = ucp->uc_mcontext.mc_err;
780 regs->tf_rip = ucp->uc_mcontext.mc_eip;
782 regs->tf_rflags = ucp->uc_mcontext.mc_eflags;
783 regs->tf_rsp = ucp->uc_mcontext.mc_esp;
784 regs->tf_ss = ucp->uc_mcontext.mc_ss;
785 regs->tf_ds = ucp->uc_mcontext.mc_ds;
786 regs->tf_es = ucp->uc_mcontext.mc_es;
787 regs->tf_fs = ucp->uc_mcontext.mc_fs;
788 regs->tf_gs = ucp->uc_mcontext.mc_gs;
789 regs->tf_flags = TF_HASSEGS;
791 kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0);
792 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
793 return (EJUSTRETURN);
797 * Clear registers on exec
800 ia32_setregs(td, entry, stack, ps_strings)
806 struct trapframe *regs = td->td_frame;
807 struct pcb *pcb = td->td_pcb;
810 if (td->td_proc->p_md.md_ldt != NULL)
813 mtx_unlock(&dt_lock);
817 pcb->pcb_initial_fpucw = __INITIAL_FPUCW_I386__;
819 bzero((char *)regs, sizeof(struct trapframe));
820 regs->tf_rip = entry;
821 regs->tf_rsp = stack;
822 regs->tf_rflags = PSL_USER | (regs->tf_rflags & PSL_T);
823 regs->tf_ss = _udatasel;
824 regs->tf_cs = _ucode32sel;
825 regs->tf_rbx = ps_strings;
826 regs->tf_ds = _udatasel;
827 regs->tf_es = _udatasel;
828 regs->tf_fs = _ufssel;
829 regs->tf_gs = _ugssel;
830 regs->tf_flags = TF_HASSEGS;
834 /* Return via doreti so that we can change to a different %cs */
835 set_pcb_flags(pcb, PCB_32BIT | PCB_FULL_IRET);
836 clear_pcb_flags(pcb, PCB_GS32BIT);
837 td->td_retval[1] = 0;