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/sysctl.h>
59 #include <sys/sysent.h>
60 #include <sys/vnode.h>
63 #include <vm/vm_kern.h>
64 #include <vm/vm_param.h>
66 #include <vm/vm_map.h>
67 #include <vm/vm_object.h>
68 #include <vm/vm_extern.h>
70 #include <compat/freebsd32/freebsd32_util.h>
71 #include <compat/freebsd32/freebsd32_proto.h>
72 #include <compat/ia32/ia32_signal.h>
73 #include <machine/psl.h>
74 #include <machine/segments.h>
75 #include <machine/specialreg.h>
76 #include <machine/frame.h>
77 #include <machine/md_var.h>
78 #include <machine/pcb.h>
79 #include <machine/cpufunc.h>
81 #ifdef COMPAT_FREEBSD4
82 static void freebsd4_ia32_sendsig(sig_t, ksiginfo_t *, sigset_t *);
84 static void ia32_get_fpcontext(struct thread *td, struct ia32_mcontext *mcp);
85 static int ia32_set_fpcontext(struct thread *td, const struct ia32_mcontext *mcp);
87 extern int _ucode32sel, _udatasel;
89 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
90 #define EFL_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
93 ia32_get_fpcontext(struct thread *td, struct ia32_mcontext *mcp)
96 mcp->mc_ownedfp = fpugetregs(td, (struct savefpu *)&mcp->mc_fpstate);
97 mcp->mc_fpformat = fpuformat();
101 ia32_set_fpcontext(struct thread *td, const struct ia32_mcontext *mcp)
104 if (mcp->mc_fpformat == _MC_FPFMT_NODEV)
106 else if (mcp->mc_fpformat != _MC_FPFMT_XMM)
108 else if (mcp->mc_ownedfp == _MC_FPOWNED_NONE)
109 /* We don't care what state is left in the FPU or PCB. */
111 else if (mcp->mc_ownedfp == _MC_FPOWNED_FPU ||
112 mcp->mc_ownedfp == _MC_FPOWNED_PCB) {
114 * XXX we violate the dubious requirement that fpusetregs()
115 * be called with interrupts disabled.
117 fpusetregs(td, (struct savefpu *)&mcp->mc_fpstate);
124 * Get machine context.
127 ia32_get_mcontext(struct thread *td, struct ia32_mcontext *mcp, int flags)
129 struct trapframe *tp;
133 PROC_LOCK(curthread->td_proc);
134 mcp->mc_onstack = sigonstack(tp->tf_rsp);
135 PROC_UNLOCK(curthread->td_proc);
136 mcp->mc_gs = td->td_pcb->pcb_gs;
137 mcp->mc_fs = td->td_pcb->pcb_fs;
138 mcp->mc_es = td->td_pcb->pcb_es;
139 mcp->mc_ds = td->td_pcb->pcb_ds;
140 mcp->mc_edi = tp->tf_rdi;
141 mcp->mc_esi = tp->tf_rsi;
142 mcp->mc_ebp = tp->tf_rbp;
143 mcp->mc_isp = tp->tf_rsp;
144 if (flags & GET_MC_CLEAR_RET) {
148 mcp->mc_eax = tp->tf_rax;
149 mcp->mc_edx = tp->tf_rdx;
151 mcp->mc_ebx = tp->tf_rbx;
152 mcp->mc_ecx = tp->tf_rcx;
153 mcp->mc_eip = tp->tf_rip;
154 mcp->mc_cs = tp->tf_cs;
155 mcp->mc_eflags = tp->tf_rflags;
156 mcp->mc_esp = tp->tf_rsp;
157 mcp->mc_ss = tp->tf_ss;
158 mcp->mc_len = sizeof(*mcp);
159 ia32_get_fpcontext(td, mcp);
164 * Set machine context.
166 * However, we don't set any but the user modifiable flags, and we won't
167 * touch the cs selector.
170 ia32_set_mcontext(struct thread *td, const struct ia32_mcontext *mcp)
172 struct trapframe *tp;
177 if (mcp->mc_len != sizeof(*mcp))
179 rflags = (mcp->mc_eflags & PSL_USERCHANGE) |
180 (tp->tf_rflags & ~PSL_USERCHANGE);
181 ret = ia32_set_fpcontext(td, mcp);
184 #if 0 /* XXX deal with load_fs() and friends */
185 tp->tf_fs = mcp->mc_fs;
186 tp->tf_es = mcp->mc_es;
187 tp->tf_ds = mcp->mc_ds;
189 tp->tf_rdi = mcp->mc_edi;
190 tp->tf_rsi = mcp->mc_esi;
191 tp->tf_rbp = mcp->mc_ebp;
192 tp->tf_rbx = mcp->mc_ebx;
193 tp->tf_rdx = mcp->mc_edx;
194 tp->tf_rcx = mcp->mc_ecx;
195 tp->tf_rax = mcp->mc_eax;
197 tp->tf_rip = mcp->mc_eip;
198 tp->tf_rflags = rflags;
199 tp->tf_rsp = mcp->mc_esp;
200 tp->tf_ss = mcp->mc_ss;
201 #if 0 /* XXX deal with load_gs() and friends */
202 td->td_pcb->pcb_gs = mcp->mc_gs;
204 td->td_pcb->pcb_flags |= PCB_FULLCTX;
209 * The first two fields of a ucontext_t are the signal mask and
210 * the machine context. The next field is uc_link; we want to
211 * avoid destroying the link when copying out contexts.
213 #define UC_COPY_SIZE offsetof(struct ia32_ucontext, uc_link)
216 freebsd32_getcontext(struct thread *td, struct freebsd32_getcontext_args *uap)
218 struct ia32_ucontext uc;
221 if (uap->ucp == NULL)
224 ia32_get_mcontext(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
225 PROC_LOCK(td->td_proc);
226 uc.uc_sigmask = td->td_sigmask;
227 PROC_UNLOCK(td->td_proc);
228 ret = copyout(&uc, uap->ucp, UC_COPY_SIZE);
234 freebsd32_setcontext(struct thread *td, struct freebsd32_setcontext_args *uap)
236 struct ia32_ucontext uc;
239 if (uap->ucp == NULL)
242 ret = copyin(uap->ucp, &uc, UC_COPY_SIZE);
244 ret = ia32_set_mcontext(td, &uc.uc_mcontext);
246 SIG_CANTMASK(uc.uc_sigmask);
247 PROC_LOCK(td->td_proc);
248 td->td_sigmask = uc.uc_sigmask;
249 PROC_UNLOCK(td->td_proc);
253 return (ret == 0 ? EJUSTRETURN : ret);
257 freebsd32_swapcontext(struct thread *td, struct freebsd32_swapcontext_args *uap)
259 struct ia32_ucontext uc;
262 if (uap->oucp == NULL || uap->ucp == NULL)
265 ia32_get_mcontext(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
266 PROC_LOCK(td->td_proc);
267 uc.uc_sigmask = td->td_sigmask;
268 PROC_UNLOCK(td->td_proc);
269 ret = copyout(&uc, uap->oucp, UC_COPY_SIZE);
271 ret = copyin(uap->ucp, &uc, UC_COPY_SIZE);
273 ret = ia32_set_mcontext(td, &uc.uc_mcontext);
275 SIG_CANTMASK(uc.uc_sigmask);
276 PROC_LOCK(td->td_proc);
277 td->td_sigmask = uc.uc_sigmask;
278 PROC_UNLOCK(td->td_proc);
283 return (ret == 0 ? EJUSTRETURN : ret);
287 * Send an interrupt to process.
289 * Stack is set up to allow sigcode stored
290 * at top to call routine, followed by kcall
291 * to sigreturn routine below. After sigreturn
292 * resets the signal mask, the stack, and the
293 * frame pointer, it returns to the user
296 #ifdef COMPAT_FREEBSD4
298 freebsd4_ia32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
300 struct ia32_sigframe4 sf, *sfp;
301 struct ia32_siginfo siginfo;
305 struct trapframe *regs;
311 siginfo_to_ia32siginfo(&ksi->ksi_info, &siginfo);
313 PROC_LOCK_ASSERT(p, MA_OWNED);
314 sig = siginfo.si_signo;
316 mtx_assert(&psp->ps_mtx, MA_OWNED);
318 oonstack = sigonstack(regs->tf_rsp);
320 /* Save user context. */
321 bzero(&sf, sizeof(sf));
322 sf.sf_uc.uc_sigmask = *mask;
323 sf.sf_uc.uc_stack.ss_sp = (uintptr_t)td->td_sigstk.ss_sp;
324 sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
325 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
326 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
327 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
328 sf.sf_uc.uc_mcontext.mc_gs = rgs();
329 sf.sf_uc.uc_mcontext.mc_fs = rfs();
330 __asm __volatile("movl %%es,%0" : "=rm" (sf.sf_uc.uc_mcontext.mc_es));
331 __asm __volatile("movl %%ds,%0" : "=rm" (sf.sf_uc.uc_mcontext.mc_ds));
332 sf.sf_uc.uc_mcontext.mc_edi = regs->tf_rdi;
333 sf.sf_uc.uc_mcontext.mc_esi = regs->tf_rsi;
334 sf.sf_uc.uc_mcontext.mc_ebp = regs->tf_rbp;
335 sf.sf_uc.uc_mcontext.mc_isp = regs->tf_rsp; /* XXX */
336 sf.sf_uc.uc_mcontext.mc_ebx = regs->tf_rbx;
337 sf.sf_uc.uc_mcontext.mc_edx = regs->tf_rdx;
338 sf.sf_uc.uc_mcontext.mc_ecx = regs->tf_rcx;
339 sf.sf_uc.uc_mcontext.mc_eax = regs->tf_rax;
340 sf.sf_uc.uc_mcontext.mc_trapno = regs->tf_trapno;
341 sf.sf_uc.uc_mcontext.mc_err = regs->tf_err;
342 sf.sf_uc.uc_mcontext.mc_eip = regs->tf_rip;
343 sf.sf_uc.uc_mcontext.mc_cs = regs->tf_cs;
344 sf.sf_uc.uc_mcontext.mc_eflags = regs->tf_rflags;
345 sf.sf_uc.uc_mcontext.mc_esp = regs->tf_rsp;
346 sf.sf_uc.uc_mcontext.mc_ss = regs->tf_ss;
348 /* Allocate space for the signal handler context. */
349 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
350 SIGISMEMBER(psp->ps_sigonstack, sig)) {
351 sfp = (struct ia32_sigframe4 *)(td->td_sigstk.ss_sp +
352 td->td_sigstk.ss_size - sizeof(sf));
354 sfp = (struct ia32_sigframe4 *)regs->tf_rsp - 1;
357 /* Translate the signal if appropriate. */
358 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
359 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
361 /* Build the argument list for the signal handler. */
363 sf.sf_ucontext = (register_t)&sfp->sf_uc;
364 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
365 /* Signal handler installed with SA_SIGINFO. */
366 sf.sf_siginfo = (u_int32_t)(uintptr_t)&sfp->sf_si;
367 sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
369 /* Fill in POSIX parts */
371 sf.sf_si.si_signo = sig;
373 /* Old FreeBSD-style arguments. */
374 sf.sf_siginfo = siginfo.si_code;
375 sf.sf_addr = (u_int32_t)siginfo.si_addr;
376 sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
378 mtx_unlock(&psp->ps_mtx);
381 * Copy the sigframe out to the user's stack.
383 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
385 printf("process %ld has trashed its stack\n", (long)p->p_pid);
391 regs->tf_rsp = (uintptr_t)sfp;
392 regs->tf_rip = FREEBSD32_PS_STRINGS - sz_freebsd4_ia32_sigcode;
393 regs->tf_rflags &= ~PSL_T;
394 regs->tf_cs = _ucode32sel;
395 regs->tf_ss = _udatasel;
397 td->td_pcb->pcb_ds = _udatasel;
399 td->td_pcb->pcb_es = _udatasel;
400 /* leave user %fs and %gs untouched */
402 mtx_lock(&psp->ps_mtx);
404 #endif /* COMPAT_FREEBSD4 */
407 ia32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
409 struct ia32_sigframe sf, *sfp;
410 struct ia32_siginfo siginfo;
415 struct trapframe *regs;
419 siginfo_to_ia32siginfo(&ksi->ksi_info, &siginfo);
422 PROC_LOCK_ASSERT(p, MA_OWNED);
423 sig = siginfo.si_signo;
425 #ifdef COMPAT_FREEBSD4
426 if (SIGISMEMBER(psp->ps_freebsd4, sig)) {
427 freebsd4_ia32_sendsig(catcher, ksi, mask);
431 mtx_assert(&psp->ps_mtx, MA_OWNED);
433 oonstack = sigonstack(regs->tf_rsp);
435 /* Save user context. */
436 bzero(&sf, sizeof(sf));
437 sf.sf_uc.uc_sigmask = *mask;
438 sf.sf_uc.uc_stack.ss_sp = (uintptr_t)td->td_sigstk.ss_sp;
439 sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
440 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
441 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
442 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
443 sf.sf_uc.uc_mcontext.mc_gs = rgs();
444 sf.sf_uc.uc_mcontext.mc_fs = rfs();
445 __asm __volatile("movl %%es,%0" : "=rm" (sf.sf_uc.uc_mcontext.mc_es));
446 __asm __volatile("movl %%ds,%0" : "=rm" (sf.sf_uc.uc_mcontext.mc_ds));
447 sf.sf_uc.uc_mcontext.mc_edi = regs->tf_rdi;
448 sf.sf_uc.uc_mcontext.mc_esi = regs->tf_rsi;
449 sf.sf_uc.uc_mcontext.mc_ebp = regs->tf_rbp;
450 sf.sf_uc.uc_mcontext.mc_isp = regs->tf_rsp; /* XXX */
451 sf.sf_uc.uc_mcontext.mc_ebx = regs->tf_rbx;
452 sf.sf_uc.uc_mcontext.mc_edx = regs->tf_rdx;
453 sf.sf_uc.uc_mcontext.mc_ecx = regs->tf_rcx;
454 sf.sf_uc.uc_mcontext.mc_eax = regs->tf_rax;
455 sf.sf_uc.uc_mcontext.mc_trapno = regs->tf_trapno;
456 sf.sf_uc.uc_mcontext.mc_err = regs->tf_err;
457 sf.sf_uc.uc_mcontext.mc_eip = regs->tf_rip;
458 sf.sf_uc.uc_mcontext.mc_cs = regs->tf_cs;
459 sf.sf_uc.uc_mcontext.mc_eflags = regs->tf_rflags;
460 sf.sf_uc.uc_mcontext.mc_esp = regs->tf_rsp;
461 sf.sf_uc.uc_mcontext.mc_ss = regs->tf_ss;
462 sf.sf_uc.uc_mcontext.mc_len = sizeof(sf.sf_uc.uc_mcontext); /* magic */
463 ia32_get_fpcontext(td, &sf.sf_uc.uc_mcontext);
466 /* Allocate space for the signal handler context. */
467 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
468 SIGISMEMBER(psp->ps_sigonstack, sig)) {
469 sp = td->td_sigstk.ss_sp +
470 td->td_sigstk.ss_size - sizeof(sf);
472 sp = (char *)regs->tf_rsp - sizeof(sf);
473 /* Align to 16 bytes. */
474 sfp = (struct ia32_sigframe *)((uintptr_t)sp & ~0xF);
477 /* Translate the signal if appropriate. */
478 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
479 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
481 /* Build the argument list for the signal handler. */
483 sf.sf_ucontext = (register_t)&sfp->sf_uc;
484 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
485 /* Signal handler installed with SA_SIGINFO. */
486 sf.sf_siginfo = (u_int32_t)(uintptr_t)&sfp->sf_si;
487 sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
489 /* Fill in POSIX parts */
491 sf.sf_si.si_signo = sig;
493 /* Old FreeBSD-style arguments. */
494 sf.sf_siginfo = siginfo.si_code;
495 sf.sf_addr = (u_int32_t)siginfo.si_addr;
496 sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
498 mtx_unlock(&psp->ps_mtx);
501 * Copy the sigframe out to the user's stack.
503 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
505 printf("process %ld has trashed its stack\n", (long)p->p_pid);
511 regs->tf_rsp = (uintptr_t)sfp;
512 regs->tf_rip = FREEBSD32_PS_STRINGS - *(p->p_sysent->sv_szsigcode);
513 regs->tf_rflags &= ~PSL_T;
514 regs->tf_cs = _ucode32sel;
515 regs->tf_ss = _udatasel;
517 td->td_pcb->pcb_ds = _udatasel;
519 td->td_pcb->pcb_es = _udatasel;
520 /* leave user %fs and %gs untouched */
522 mtx_lock(&psp->ps_mtx);
526 * System call to cleanup state after a signal
527 * has been taken. Reset signal mask and
528 * stack state from context left by sendsig (above).
529 * Return to previous pc and psl as specified by
530 * context left by sendsig. Check carefully to
531 * make sure that the user has not modified the
532 * state to gain improper privileges.
534 #ifdef COMPAT_FREEBSD4
539 freebsd4_freebsd32_sigreturn(td, uap)
541 struct freebsd4_freebsd32_sigreturn_args /* {
542 const struct freebsd4_freebsd32_ucontext *sigcntxp;
545 struct ia32_ucontext4 uc;
546 struct proc *p = td->td_proc;
547 struct trapframe *regs;
548 const struct ia32_ucontext4 *ucp;
549 int cs, eflags, error;
552 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
557 eflags = ucp->uc_mcontext.mc_eflags;
559 * Don't allow users to change privileged or reserved flags.
562 * XXX do allow users to change the privileged flag PSL_RF.
563 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
564 * should sometimes set it there too. tf_eflags is kept in
565 * the signal context during signal handling and there is no
566 * other place to remember it, so the PSL_RF bit may be
567 * corrupted by the signal handler without us knowing.
568 * Corruption of the PSL_RF bit at worst causes one more or
569 * one less debugger trap, so allowing it is fairly harmless.
571 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) {
572 printf("freebsd4_freebsd32_sigreturn: eflags = 0x%x\n", eflags);
577 * Don't allow users to load a valid privileged %cs. Let the
578 * hardware check for invalid selectors, excess privilege in
579 * other selectors, invalid %eip's and invalid %esp's.
581 cs = ucp->uc_mcontext.mc_cs;
582 if (!CS_SECURE(cs)) {
583 printf("freebsd4_sigreturn: cs = 0x%x\n", cs);
584 ksiginfo_init_trap(&ksi);
585 ksi.ksi_signo = SIGBUS;
586 ksi.ksi_code = BUS_OBJERR;
587 ksi.ksi_trapno = T_PROTFLT;
588 ksi.ksi_addr = (void *)regs->tf_rip;
589 trapsignal(td, &ksi);
593 /* Segment selectors restored by sigtramp.S */
594 regs->tf_rdi = ucp->uc_mcontext.mc_edi;
595 regs->tf_rsi = ucp->uc_mcontext.mc_esi;
596 regs->tf_rbp = ucp->uc_mcontext.mc_ebp;
597 regs->tf_rbx = ucp->uc_mcontext.mc_ebx;
598 regs->tf_rdx = ucp->uc_mcontext.mc_edx;
599 regs->tf_rcx = ucp->uc_mcontext.mc_ecx;
600 regs->tf_rax = ucp->uc_mcontext.mc_eax;
601 regs->tf_trapno = ucp->uc_mcontext.mc_trapno;
602 regs->tf_err = ucp->uc_mcontext.mc_err;
603 regs->tf_rip = ucp->uc_mcontext.mc_eip;
605 regs->tf_rflags = ucp->uc_mcontext.mc_eflags;
606 regs->tf_rsp = ucp->uc_mcontext.mc_esp;
607 regs->tf_ss = ucp->uc_mcontext.mc_ss;
610 td->td_sigmask = ucp->uc_sigmask;
611 SIG_CANTMASK(td->td_sigmask);
614 return (EJUSTRETURN);
616 #endif /* COMPAT_FREEBSD4 */
622 freebsd32_sigreturn(td, uap)
624 struct freebsd32_sigreturn_args /* {
625 const struct freebsd32_ucontext *sigcntxp;
628 struct ia32_ucontext uc;
629 struct proc *p = td->td_proc;
630 struct trapframe *regs;
631 const struct ia32_ucontext *ucp;
632 int cs, eflags, error, ret;
635 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
640 eflags = ucp->uc_mcontext.mc_eflags;
642 * Don't allow users to change privileged or reserved flags.
645 * XXX do allow users to change the privileged flag PSL_RF.
646 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
647 * should sometimes set it there too. tf_eflags is kept in
648 * the signal context during signal handling and there is no
649 * other place to remember it, so the PSL_RF bit may be
650 * corrupted by the signal handler without us knowing.
651 * Corruption of the PSL_RF bit at worst causes one more or
652 * one less debugger trap, so allowing it is fairly harmless.
654 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) {
655 printf("freebsd32_sigreturn: eflags = 0x%x\n", eflags);
660 * Don't allow users to load a valid privileged %cs. Let the
661 * hardware check for invalid selectors, excess privilege in
662 * other selectors, invalid %eip's and invalid %esp's.
664 cs = ucp->uc_mcontext.mc_cs;
665 if (!CS_SECURE(cs)) {
666 printf("sigreturn: cs = 0x%x\n", cs);
667 ksiginfo_init_trap(&ksi);
668 ksi.ksi_signo = SIGBUS;
669 ksi.ksi_code = BUS_OBJERR;
670 ksi.ksi_trapno = T_PROTFLT;
671 ksi.ksi_addr = (void *)regs->tf_rip;
672 trapsignal(td, &ksi);
676 ret = ia32_set_fpcontext(td, &ucp->uc_mcontext);
680 /* Segment selectors restored by sigtramp.S */
681 regs->tf_rdi = ucp->uc_mcontext.mc_edi;
682 regs->tf_rsi = ucp->uc_mcontext.mc_esi;
683 regs->tf_rbp = ucp->uc_mcontext.mc_ebp;
684 regs->tf_rbx = ucp->uc_mcontext.mc_ebx;
685 regs->tf_rdx = ucp->uc_mcontext.mc_edx;
686 regs->tf_rcx = ucp->uc_mcontext.mc_ecx;
687 regs->tf_rax = ucp->uc_mcontext.mc_eax;
688 regs->tf_trapno = ucp->uc_mcontext.mc_trapno;
689 regs->tf_err = ucp->uc_mcontext.mc_err;
690 regs->tf_rip = ucp->uc_mcontext.mc_eip;
692 regs->tf_rflags = ucp->uc_mcontext.mc_eflags;
693 regs->tf_rsp = ucp->uc_mcontext.mc_esp;
694 regs->tf_ss = ucp->uc_mcontext.mc_ss;
697 td->td_sigmask = ucp->uc_sigmask;
698 SIG_CANTMASK(td->td_sigmask);
701 return (EJUSTRETURN);
705 * Clear registers on exec
708 ia32_setregs(td, entry, stack, ps_strings)
714 struct trapframe *regs = td->td_frame;
715 struct pcb *pcb = td->td_pcb;
717 wrmsr(MSR_FSBASE, 0);
718 wrmsr(MSR_KGSBASE, 0); /* User value while we're in the kernel */
725 pcb->pcb_ds = _udatasel;
726 pcb->pcb_es = _udatasel;
727 pcb->pcb_fs = _udatasel;
728 pcb->pcb_gs = _udatasel;
730 bzero((char *)regs, sizeof(struct trapframe));
731 regs->tf_rip = entry;
732 regs->tf_rsp = stack;
733 regs->tf_rflags = PSL_USER | (regs->tf_rflags & PSL_T);
734 regs->tf_ss = _udatasel;
735 regs->tf_cs = _ucode32sel;
736 regs->tf_rbx = ps_strings;
737 load_cr0(rcr0() | CR0_MP | CR0_TS);
740 /* Return via doreti so that we can change to a different %cs */
741 pcb->pcb_flags |= PCB_FULLCTX;
742 td->td_retval[1] = 0;
746 siginfo_to_ia32siginfo(siginfo_t *src, struct ia32_siginfo *dst)
748 dst->si_signo = src->si_signo;
749 dst->si_errno = src->si_errno;
750 dst->si_code = src->si_code;
751 dst->si_pid = src->si_pid;
752 dst->si_uid = src->si_uid;
753 dst->si_status = src->si_status;
754 dst->si_addr = dst->si_addr;
755 dst->si_value.sigval_int = src->si_value.sival_int;
756 dst->si_band = src->si_band;
757 dst->si_trapno = src->si_trapno;
758 dst->si_timerid = src->si_timerid;
759 dst->si_overrun = src->si_overrun;