2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2003 Peter Wemm
5 * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
8 * This code is derived from software contributed to Berkeley by
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
37 #include <sys/param.h>
39 #include <sys/fcntl.h>
40 #include <sys/imgact.h>
41 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/mutex.h>
46 #include <sys/namei.h>
48 #include <sys/procfs.h>
49 #include <sys/resourcevar.h>
50 #include <sys/systm.h>
51 #include <sys/signalvar.h>
54 #include <sys/syscall.h>
55 #include <sys/syscallsubr.h>
56 #include <sys/sysctl.h>
57 #include <sys/sysent.h>
58 #include <sys/vnode.h>
61 #include <vm/vm_kern.h>
62 #include <vm/vm_param.h>
64 #include <vm/vm_map.h>
65 #include <vm/vm_object.h>
66 #include <vm/vm_extern.h>
68 #include <compat/freebsd32/freebsd32_signal.h>
69 #include <compat/freebsd32/freebsd32_util.h>
70 #include <compat/freebsd32/freebsd32_proto.h>
71 #include <compat/freebsd32/freebsd32.h>
72 #include <compat/ia32/ia32_signal.h>
73 #include <machine/psl.h>
74 #include <machine/segments.h>
75 #include <machine/specialreg.h>
76 #include <machine/frame.h>
77 #include <machine/md_var.h>
78 #include <machine/pcb.h>
79 #include <machine/cpufunc.h>
80 #include <machine/trap.h>
82 #include "vdso_ia32_offsets.h"
84 extern const char _binary_elf_vdso32_so_1_start[];
85 extern const char _binary_elf_vdso32_so_1_end[];
86 extern char _binary_elf_vdso32_so_1_size;
88 #ifdef COMPAT_FREEBSD4
89 static void freebsd4_ia32_sendsig(sig_t, ksiginfo_t *, sigset_t *);
93 ia32_get_fpcontext(struct thread *td, struct ia32_mcontext *mcp,
94 char **xfpusave, size_t *xfpusave_len)
97 * XXX Format of 64bit and 32bit FXSAVE areas differs. FXSAVE
98 * in 32bit mode saves %cs and %ds, while on 64bit it saves
99 * 64bit instruction and data pointers. Ignore the difference
100 * for now, it should be irrelevant for most applications.
102 mcp->mc_ownedfp = fpugetregs(td);
103 bcopy(get_pcb_user_save_td(td), &mcp->mc_fpstate[0],
104 sizeof(mcp->mc_fpstate));
105 mcp->mc_fpformat = fpuformat();
106 if (xfpusave == NULL)
108 if (!use_xsave || cpu_max_ext_state_size <= sizeof(struct savefpu)) {
112 mcp->mc_flags |= _MC_IA32_HASFPXSTATE;
113 *xfpusave_len = mcp->mc_xfpustate_len =
114 cpu_max_ext_state_size - sizeof(struct savefpu);
115 *xfpusave = (char *)(get_pcb_user_save_td(td) + 1);
120 ia32_set_fpcontext(struct thread *td, struct ia32_mcontext *mcp,
121 char *xfpustate, size_t xfpustate_len)
125 if (mcp->mc_fpformat == _MC_FPFMT_NODEV)
127 else if (mcp->mc_fpformat != _MC_FPFMT_XMM)
129 else if (mcp->mc_ownedfp == _MC_FPOWNED_NONE) {
130 /* We don't care what state is left in the FPU or PCB. */
133 } else if (mcp->mc_ownedfp == _MC_FPOWNED_FPU ||
134 mcp->mc_ownedfp == _MC_FPOWNED_PCB) {
135 error = fpusetregs(td, (struct savefpu *)&mcp->mc_fpstate,
136 xfpustate, xfpustate_len);
143 * Get machine context.
146 ia32_get_mcontext(struct thread *td, struct ia32_mcontext *mcp, int flags)
149 struct trapframe *tp;
154 PROC_LOCK(curthread->td_proc);
155 mcp->mc_onstack = sigonstack(tp->tf_rsp);
156 PROC_UNLOCK(curthread->td_proc);
157 /* Entry into kernel always sets TF_HASSEGS */
158 mcp->mc_gs = tp->tf_gs;
159 mcp->mc_fs = tp->tf_fs;
160 mcp->mc_es = tp->tf_es;
161 mcp->mc_ds = tp->tf_ds;
162 mcp->mc_edi = tp->tf_rdi;
163 mcp->mc_esi = tp->tf_rsi;
164 mcp->mc_ebp = tp->tf_rbp;
165 mcp->mc_isp = tp->tf_rsp;
166 mcp->mc_eflags = tp->tf_rflags;
167 if (flags & GET_MC_CLEAR_RET) {
170 mcp->mc_eflags &= ~PSL_C;
172 mcp->mc_eax = tp->tf_rax;
173 mcp->mc_edx = tp->tf_rdx;
175 mcp->mc_ebx = tp->tf_rbx;
176 mcp->mc_ecx = tp->tf_rcx;
177 mcp->mc_eip = tp->tf_rip;
178 mcp->mc_cs = tp->tf_cs;
179 mcp->mc_esp = tp->tf_rsp;
180 mcp->mc_ss = tp->tf_ss;
181 mcp->mc_len = sizeof(*mcp);
182 mcp->mc_flags = tp->tf_flags;
183 ia32_get_fpcontext(td, mcp, NULL, 0);
184 mcp->mc_fsbase = pcb->pcb_fsbase;
185 mcp->mc_gsbase = pcb->pcb_gsbase;
186 mcp->mc_xfpustate = 0;
187 mcp->mc_xfpustate_len = 0;
188 bzero(mcp->mc_spare2, sizeof(mcp->mc_spare2));
193 * Set machine context.
195 * However, we don't set any but the user modifiable flags, and we won't
196 * touch the cs selector.
199 ia32_set_mcontext(struct thread *td, struct ia32_mcontext *mcp)
201 struct trapframe *tp;
207 if (mcp->mc_len != sizeof(*mcp))
209 rflags = (mcp->mc_eflags & PSL_USERCHANGE) |
210 (tp->tf_rflags & ~PSL_USERCHANGE);
211 if (mcp->mc_flags & _MC_IA32_HASFPXSTATE) {
212 if (mcp->mc_xfpustate_len > cpu_max_ext_state_size -
213 sizeof(struct savefpu))
215 xfpustate = (char *)fpu_save_area_alloc();
216 ret = copyin(PTRIN(mcp->mc_xfpustate), xfpustate,
217 mcp->mc_xfpustate_len);
219 fpu_save_area_free((struct savefpu *)xfpustate);
224 ret = ia32_set_fpcontext(td, mcp, xfpustate, mcp->mc_xfpustate_len);
225 fpu_save_area_free((struct savefpu *)xfpustate);
228 tp->tf_gs = mcp->mc_gs;
229 tp->tf_fs = mcp->mc_fs;
230 tp->tf_es = mcp->mc_es;
231 tp->tf_ds = mcp->mc_ds;
232 tp->tf_flags = TF_HASSEGS;
233 tp->tf_rdi = mcp->mc_edi;
234 tp->tf_rsi = mcp->mc_esi;
235 tp->tf_rbp = mcp->mc_ebp;
236 tp->tf_rbx = mcp->mc_ebx;
237 tp->tf_rdx = mcp->mc_edx;
238 tp->tf_rcx = mcp->mc_ecx;
239 tp->tf_rax = mcp->mc_eax;
241 tp->tf_rip = mcp->mc_eip;
242 tp->tf_rflags = rflags;
243 tp->tf_rsp = mcp->mc_esp;
244 tp->tf_ss = mcp->mc_ss;
245 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
250 * The first two fields of a ucontext_t are the signal mask and
251 * the machine context. The next field is uc_link; we want to
252 * avoid destroying the link when copying out contexts.
254 #define UC_COPY_SIZE offsetof(struct ia32_ucontext, uc_link)
257 freebsd32_getcontext(struct thread *td, struct freebsd32_getcontext_args *uap)
259 struct ia32_ucontext uc;
262 if (uap->ucp == NULL)
265 bzero(&uc, sizeof(uc));
266 ia32_get_mcontext(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
267 PROC_LOCK(td->td_proc);
268 uc.uc_sigmask = td->td_sigmask;
269 PROC_UNLOCK(td->td_proc);
270 ret = copyout(&uc, uap->ucp, UC_COPY_SIZE);
276 freebsd32_setcontext(struct thread *td, struct freebsd32_setcontext_args *uap)
278 struct ia32_ucontext uc;
281 if (uap->ucp == NULL)
284 ret = copyin(uap->ucp, &uc, UC_COPY_SIZE);
286 ret = ia32_set_mcontext(td, &uc.uc_mcontext);
288 kern_sigprocmask(td, SIG_SETMASK,
289 &uc.uc_sigmask, NULL, 0);
293 return (ret == 0 ? EJUSTRETURN : ret);
297 freebsd32_swapcontext(struct thread *td, struct freebsd32_swapcontext_args *uap)
299 struct ia32_ucontext uc;
302 if (uap->oucp == NULL || uap->ucp == NULL)
305 bzero(&uc, sizeof(uc));
306 ia32_get_mcontext(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
307 PROC_LOCK(td->td_proc);
308 uc.uc_sigmask = td->td_sigmask;
309 PROC_UNLOCK(td->td_proc);
310 ret = copyout(&uc, uap->oucp, UC_COPY_SIZE);
312 ret = copyin(uap->ucp, &uc, UC_COPY_SIZE);
314 ret = ia32_set_mcontext(td, &uc.uc_mcontext);
316 kern_sigprocmask(td, SIG_SETMASK,
317 &uc.uc_sigmask, NULL, 0);
322 return (ret == 0 ? EJUSTRETURN : ret);
326 * Send an interrupt to process.
328 * Stack is set up to allow sigcode stored
329 * at top to call routine, followed by kcall
330 * to sigreturn routine below. After sigreturn
331 * resets the signal mask, the stack, and the
332 * frame pointer, it returns to the user
338 ia32_osendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
340 struct ia32_osigframe sf, *fp;
344 struct trapframe *regs;
350 PROC_LOCK_ASSERT(p, MA_OWNED);
351 sig = ksi->ksi_signo;
353 mtx_assert(&psp->ps_mtx, MA_OWNED);
355 oonstack = sigonstack(regs->tf_rsp);
357 /* Allocate space for the signal handler context. */
358 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
359 SIGISMEMBER(psp->ps_sigonstack, sig)) {
360 fp = (struct ia32_osigframe *)((uintptr_t)td->td_sigstk.ss_sp +
361 td->td_sigstk.ss_size - sizeof(sf));
362 td->td_sigstk.ss_flags |= SS_ONSTACK;
364 fp = (struct ia32_osigframe *)regs->tf_rsp - 1;
366 /* Build the argument list for the signal handler. */
368 sf.sf_scp = (register_t)&fp->sf_siginfo.si_sc;
369 bzero(&sf.sf_siginfo, sizeof(sf.sf_siginfo));
370 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
371 /* Signal handler installed with SA_SIGINFO. */
372 sf.sf_arg2 = (register_t)&fp->sf_siginfo;
373 sf.sf_siginfo.si_signo = sig;
374 sf.sf_siginfo.si_code = ksi->ksi_code;
375 sf.sf_ah = (uintptr_t)catcher;
378 /* Old FreeBSD-style arguments. */
379 sf.sf_arg2 = ksi->ksi_code;
380 sf.sf_addr = (register_t)ksi->ksi_addr;
381 sf.sf_ah = (uintptr_t)catcher;
383 mtx_unlock(&psp->ps_mtx);
386 /* Save most if not all of trap frame. */
387 sf.sf_siginfo.si_sc.sc_eax = regs->tf_rax;
388 sf.sf_siginfo.si_sc.sc_ebx = regs->tf_rbx;
389 sf.sf_siginfo.si_sc.sc_ecx = regs->tf_rcx;
390 sf.sf_siginfo.si_sc.sc_edx = regs->tf_rdx;
391 sf.sf_siginfo.si_sc.sc_esi = regs->tf_rsi;
392 sf.sf_siginfo.si_sc.sc_edi = regs->tf_rdi;
393 sf.sf_siginfo.si_sc.sc_cs = regs->tf_cs;
394 sf.sf_siginfo.si_sc.sc_ds = regs->tf_ds;
395 sf.sf_siginfo.si_sc.sc_ss = regs->tf_ss;
396 sf.sf_siginfo.si_sc.sc_es = regs->tf_es;
397 sf.sf_siginfo.si_sc.sc_fs = regs->tf_fs;
398 sf.sf_siginfo.si_sc.sc_gs = regs->tf_gs;
399 sf.sf_siginfo.si_sc.sc_isp = regs->tf_rsp;
401 /* Build the signal context to be used by osigreturn(). */
402 sf.sf_siginfo.si_sc.sc_onstack = (oonstack) ? 1 : 0;
403 SIG2OSIG(*mask, sf.sf_siginfo.si_sc.sc_mask);
404 sf.sf_siginfo.si_sc.sc_esp = regs->tf_rsp;
405 sf.sf_siginfo.si_sc.sc_ebp = regs->tf_rbp;
406 sf.sf_siginfo.si_sc.sc_eip = regs->tf_rip;
407 sf.sf_siginfo.si_sc.sc_eflags = regs->tf_rflags;
408 sf.sf_siginfo.si_sc.sc_trapno = regs->tf_trapno;
409 sf.sf_siginfo.si_sc.sc_err = regs->tf_err;
412 * Copy the sigframe out to the user's stack.
414 if (copyout(&sf, fp, sizeof(*fp)) != 0) {
416 printf("process %ld has trashed its stack\n", (long)p->p_pid);
422 regs->tf_rsp = (uintptr_t)fp;
423 regs->tf_rip = PROC_PS_STRINGS(p) -
424 (_binary_elf_vdso32_so_1_end - _binary_elf_vdso32_so_1_start) +
425 VDSO_IA32_OSIGCODE_OFFSET;
426 regs->tf_rflags &= ~(PSL_T | PSL_D);
427 regs->tf_cs = _ucode32sel;
428 regs->tf_ds = _udatasel;
429 regs->tf_es = _udatasel;
430 regs->tf_fs = _udatasel;
431 regs->tf_ss = _udatasel;
432 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
434 mtx_lock(&psp->ps_mtx);
438 #ifdef COMPAT_FREEBSD4
440 freebsd4_ia32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
442 struct ia32_freebsd4_sigframe sf, *sfp;
443 struct siginfo32 siginfo;
447 struct trapframe *regs;
453 siginfo_to_siginfo32(&ksi->ksi_info, &siginfo);
455 PROC_LOCK_ASSERT(p, MA_OWNED);
456 sig = siginfo.si_signo;
458 mtx_assert(&psp->ps_mtx, MA_OWNED);
460 oonstack = sigonstack(regs->tf_rsp);
462 /* Save user context. */
463 bzero(&sf, sizeof(sf));
464 sf.sf_uc.uc_sigmask = *mask;
465 sf.sf_uc.uc_stack.ss_sp = (uintptr_t)td->td_sigstk.ss_sp;
466 sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
467 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
468 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
469 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
470 sf.sf_uc.uc_mcontext.mc_edi = regs->tf_rdi;
471 sf.sf_uc.uc_mcontext.mc_esi = regs->tf_rsi;
472 sf.sf_uc.uc_mcontext.mc_ebp = regs->tf_rbp;
473 sf.sf_uc.uc_mcontext.mc_isp = regs->tf_rsp; /* XXX */
474 sf.sf_uc.uc_mcontext.mc_ebx = regs->tf_rbx;
475 sf.sf_uc.uc_mcontext.mc_edx = regs->tf_rdx;
476 sf.sf_uc.uc_mcontext.mc_ecx = regs->tf_rcx;
477 sf.sf_uc.uc_mcontext.mc_eax = regs->tf_rax;
478 sf.sf_uc.uc_mcontext.mc_trapno = regs->tf_trapno;
479 sf.sf_uc.uc_mcontext.mc_err = regs->tf_err;
480 sf.sf_uc.uc_mcontext.mc_eip = regs->tf_rip;
481 sf.sf_uc.uc_mcontext.mc_cs = regs->tf_cs;
482 sf.sf_uc.uc_mcontext.mc_eflags = regs->tf_rflags;
483 sf.sf_uc.uc_mcontext.mc_esp = regs->tf_rsp;
484 sf.sf_uc.uc_mcontext.mc_ss = regs->tf_ss;
485 sf.sf_uc.uc_mcontext.mc_ds = regs->tf_ds;
486 sf.sf_uc.uc_mcontext.mc_es = regs->tf_es;
487 sf.sf_uc.uc_mcontext.mc_fs = regs->tf_fs;
488 sf.sf_uc.uc_mcontext.mc_gs = regs->tf_gs;
489 bzero(sf.sf_uc.uc_mcontext.mc_fpregs,
490 sizeof(sf.sf_uc.uc_mcontext.mc_fpregs));
491 bzero(sf.sf_uc.uc_mcontext.__spare__,
492 sizeof(sf.sf_uc.uc_mcontext.__spare__));
493 bzero(sf.sf_uc.__spare__, sizeof(sf.sf_uc.__spare__));
495 /* Allocate space for the signal handler context. */
496 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
497 SIGISMEMBER(psp->ps_sigonstack, sig)) {
498 sfp = (struct ia32_freebsd4_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
499 td->td_sigstk.ss_size - sizeof(sf));
501 sfp = (struct ia32_freebsd4_sigframe *)regs->tf_rsp - 1;
504 /* Build the argument list for the signal handler. */
506 sf.sf_ucontext = (register_t)&sfp->sf_uc;
507 bzero(&sf.sf_si, sizeof(sf.sf_si));
508 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
509 /* Signal handler installed with SA_SIGINFO. */
510 sf.sf_siginfo = (u_int32_t)(uintptr_t)&sfp->sf_si;
511 sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
513 /* Fill in POSIX parts */
515 sf.sf_si.si_signo = sig;
517 /* Old FreeBSD-style arguments. */
518 sf.sf_siginfo = siginfo.si_code;
519 sf.sf_addr = (u_int32_t)siginfo.si_addr;
520 sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
522 mtx_unlock(&psp->ps_mtx);
525 * Copy the sigframe out to the user's stack.
527 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
529 printf("process %ld has trashed its stack\n", (long)p->p_pid);
535 regs->tf_rsp = (uintptr_t)sfp;
536 regs->tf_rip = PROC_SIGCODE(p) +
537 VDSO_FREEBSD4_IA32_SIGCODE_OFFSET - VDSO_IA32_SIGCODE_OFFSET;
538 regs->tf_rflags &= ~(PSL_T | PSL_D);
539 regs->tf_cs = _ucode32sel;
540 regs->tf_ss = _udatasel;
541 regs->tf_ds = _udatasel;
542 regs->tf_es = _udatasel;
543 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
544 /* leave user %fs and %gs untouched */
546 mtx_lock(&psp->ps_mtx);
548 #endif /* COMPAT_FREEBSD4 */
551 ia32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
553 struct ia32_sigframe sf, *sfp;
554 struct siginfo32 siginfo;
559 struct trapframe *regs;
565 siginfo_to_siginfo32(&ksi->ksi_info, &siginfo);
568 PROC_LOCK_ASSERT(p, MA_OWNED);
569 sig = siginfo.si_signo;
571 #ifdef COMPAT_FREEBSD4
572 if (SIGISMEMBER(psp->ps_freebsd4, sig)) {
573 freebsd4_ia32_sendsig(catcher, ksi, mask);
578 if (SIGISMEMBER(psp->ps_osigset, sig)) {
579 ia32_osendsig(catcher, ksi, mask);
583 mtx_assert(&psp->ps_mtx, MA_OWNED);
585 oonstack = sigonstack(regs->tf_rsp);
587 /* Save user context. */
588 bzero(&sf, sizeof(sf));
589 sf.sf_uc.uc_sigmask = *mask;
590 sf.sf_uc.uc_stack.ss_sp = (uintptr_t)td->td_sigstk.ss_sp;
591 sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
592 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
593 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
594 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
595 sf.sf_uc.uc_mcontext.mc_edi = regs->tf_rdi;
596 sf.sf_uc.uc_mcontext.mc_esi = regs->tf_rsi;
597 sf.sf_uc.uc_mcontext.mc_ebp = regs->tf_rbp;
598 sf.sf_uc.uc_mcontext.mc_isp = regs->tf_rsp; /* XXX */
599 sf.sf_uc.uc_mcontext.mc_ebx = regs->tf_rbx;
600 sf.sf_uc.uc_mcontext.mc_edx = regs->tf_rdx;
601 sf.sf_uc.uc_mcontext.mc_ecx = regs->tf_rcx;
602 sf.sf_uc.uc_mcontext.mc_eax = regs->tf_rax;
603 sf.sf_uc.uc_mcontext.mc_trapno = regs->tf_trapno;
604 sf.sf_uc.uc_mcontext.mc_err = regs->tf_err;
605 sf.sf_uc.uc_mcontext.mc_eip = regs->tf_rip;
606 sf.sf_uc.uc_mcontext.mc_cs = regs->tf_cs;
607 sf.sf_uc.uc_mcontext.mc_eflags = regs->tf_rflags;
608 sf.sf_uc.uc_mcontext.mc_esp = regs->tf_rsp;
609 sf.sf_uc.uc_mcontext.mc_ss = regs->tf_ss;
610 sf.sf_uc.uc_mcontext.mc_ds = regs->tf_ds;
611 sf.sf_uc.uc_mcontext.mc_es = regs->tf_es;
612 sf.sf_uc.uc_mcontext.mc_fs = regs->tf_fs;
613 sf.sf_uc.uc_mcontext.mc_gs = regs->tf_gs;
614 sf.sf_uc.uc_mcontext.mc_len = sizeof(sf.sf_uc.uc_mcontext); /* magic */
615 ia32_get_fpcontext(td, &sf.sf_uc.uc_mcontext, &xfpusave, &xfpusave_len);
616 sf.sf_uc.uc_mcontext.mc_fsbase = td->td_pcb->pcb_fsbase;
617 sf.sf_uc.uc_mcontext.mc_gsbase = td->td_pcb->pcb_gsbase;
619 /* Allocate space for the signal handler context. */
620 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
621 SIGISMEMBER(psp->ps_sigonstack, sig))
622 sp = (char *)td->td_sigstk.ss_sp + td->td_sigstk.ss_size;
624 sp = (char *)regs->tf_rsp;
625 if (xfpusave != NULL) {
627 sp = (char *)((unsigned long)sp & ~0x3Ful);
628 sf.sf_uc.uc_mcontext.mc_xfpustate = (register_t)sp;
631 /* Align to 16 bytes. */
632 sfp = (struct ia32_sigframe *)((uintptr_t)sp & ~0xF);
635 /* Build the argument list for the signal handler. */
637 sf.sf_ucontext = (register_t)&sfp->sf_uc;
638 bzero(&sf.sf_si, sizeof(sf.sf_si));
639 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
640 /* Signal handler installed with SA_SIGINFO. */
641 sf.sf_siginfo = (u_int32_t)(uintptr_t)&sfp->sf_si;
642 sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
644 /* Fill in POSIX parts */
646 sf.sf_si.si_signo = sig;
648 /* Old FreeBSD-style arguments. */
649 sf.sf_siginfo = siginfo.si_code;
650 sf.sf_addr = (u_int32_t)siginfo.si_addr;
651 sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
653 mtx_unlock(&psp->ps_mtx);
656 * Copy the sigframe out to the user's stack.
658 if (copyout(&sf, sfp, sizeof(*sfp)) != 0 ||
659 (xfpusave != NULL && copyout(xfpusave,
660 PTRIN(sf.sf_uc.uc_mcontext.mc_xfpustate), xfpusave_len)
663 printf("process %ld has trashed its stack\n", (long)p->p_pid);
670 regs->tf_rsp = (uintptr_t)sfp;
671 regs->tf_rip = PROC_SIGCODE(p);
672 regs->tf_rflags &= ~(PSL_T | PSL_D);
673 regs->tf_cs = _ucode32sel;
674 regs->tf_ss = _udatasel;
675 regs->tf_ds = _udatasel;
676 regs->tf_es = _udatasel;
677 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
678 /* XXXKIB leave user %fs and %gs untouched */
680 mtx_lock(&psp->ps_mtx);
684 * System call to cleanup state after a signal
685 * has been taken. Reset signal mask and
686 * stack state from context left by sendsig (above).
687 * Return to previous pc and psl as specified by
688 * context left by sendsig. Check carefully to
689 * make sure that the user has not modified the
690 * state to gain improper privileges.
695 ofreebsd32_sigreturn(struct thread *td, struct ofreebsd32_sigreturn_args *uap)
697 struct ia32_osigcontext sc, *scp;
698 struct trapframe *regs;
703 error = copyin(uap->sigcntxp, &sc, sizeof(sc));
707 eflags = scp->sc_eflags;
708 if (!EFL_SECURE(eflags, regs->tf_rflags)) {
711 if (!CS_SECURE(scp->sc_cs)) {
712 ksiginfo_init_trap(&ksi);
713 ksi.ksi_signo = SIGBUS;
714 ksi.ksi_code = BUS_OBJERR;
715 ksi.ksi_trapno = T_PROTFLT;
716 ksi.ksi_addr = (void *)regs->tf_rip;
717 trapsignal(td, &ksi);
720 regs->tf_ds = scp->sc_ds;
721 regs->tf_es = scp->sc_es;
722 regs->tf_fs = scp->sc_fs;
723 regs->tf_gs = scp->sc_gs;
725 regs->tf_rax = scp->sc_eax;
726 regs->tf_rbx = scp->sc_ebx;
727 regs->tf_rcx = scp->sc_ecx;
728 regs->tf_rdx = scp->sc_edx;
729 regs->tf_rsi = scp->sc_esi;
730 regs->tf_rdi = scp->sc_edi;
731 regs->tf_cs = scp->sc_cs;
732 regs->tf_ss = scp->sc_ss;
733 regs->tf_rbp = scp->sc_ebp;
734 regs->tf_rsp = scp->sc_esp;
735 regs->tf_rip = scp->sc_eip;
736 regs->tf_rflags = eflags;
738 if (scp->sc_onstack & 1)
739 td->td_sigstk.ss_flags |= SS_ONSTACK;
741 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
743 kern_sigprocmask(td, SIG_SETMASK, (sigset_t *)&scp->sc_mask, NULL,
745 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
746 return (EJUSTRETURN);
750 #ifdef COMPAT_FREEBSD4
752 freebsd4_freebsd32_sigreturn(struct thread *td,
753 struct freebsd4_freebsd32_sigreturn_args *uap)
755 struct ia32_freebsd4_ucontext uc;
756 struct trapframe *regs;
757 struct ia32_freebsd4_ucontext *ucp;
758 int cs, eflags, error;
761 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
766 eflags = ucp->uc_mcontext.mc_eflags;
768 * Don't allow users to change privileged or reserved flags.
770 if (!EFL_SECURE(eflags, regs->tf_rflags)) {
771 uprintf("pid %d (%s): freebsd4_freebsd32_sigreturn eflags = 0x%x\n",
772 td->td_proc->p_pid, td->td_name, eflags);
777 * Don't allow users to load a valid privileged %cs. Let the
778 * hardware check for invalid selectors, excess privilege in
779 * other selectors, invalid %eip's and invalid %esp's.
781 cs = ucp->uc_mcontext.mc_cs;
782 if (!CS_SECURE(cs)) {
783 uprintf("pid %d (%s): freebsd4_sigreturn cs = 0x%x\n",
784 td->td_proc->p_pid, td->td_name, cs);
785 ksiginfo_init_trap(&ksi);
786 ksi.ksi_signo = SIGBUS;
787 ksi.ksi_code = BUS_OBJERR;
788 ksi.ksi_trapno = T_PROTFLT;
789 ksi.ksi_addr = (void *)regs->tf_rip;
790 trapsignal(td, &ksi);
794 regs->tf_rdi = ucp->uc_mcontext.mc_edi;
795 regs->tf_rsi = ucp->uc_mcontext.mc_esi;
796 regs->tf_rbp = ucp->uc_mcontext.mc_ebp;
797 regs->tf_rbx = ucp->uc_mcontext.mc_ebx;
798 regs->tf_rdx = ucp->uc_mcontext.mc_edx;
799 regs->tf_rcx = ucp->uc_mcontext.mc_ecx;
800 regs->tf_rax = ucp->uc_mcontext.mc_eax;
801 regs->tf_trapno = ucp->uc_mcontext.mc_trapno;
802 regs->tf_err = ucp->uc_mcontext.mc_err;
803 regs->tf_rip = ucp->uc_mcontext.mc_eip;
805 regs->tf_rflags = ucp->uc_mcontext.mc_eflags;
806 regs->tf_rsp = ucp->uc_mcontext.mc_esp;
807 regs->tf_ss = ucp->uc_mcontext.mc_ss;
808 regs->tf_ds = ucp->uc_mcontext.mc_ds;
809 regs->tf_es = ucp->uc_mcontext.mc_es;
810 regs->tf_fs = ucp->uc_mcontext.mc_fs;
811 regs->tf_gs = ucp->uc_mcontext.mc_gs;
813 kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0);
814 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
815 return (EJUSTRETURN);
817 #endif /* COMPAT_FREEBSD4 */
820 freebsd32_sigreturn(struct thread *td, struct freebsd32_sigreturn_args *uap)
822 struct ia32_ucontext uc;
823 struct trapframe *regs;
824 struct ia32_ucontext *ucp;
826 size_t xfpustate_len;
827 int cs, eflags, error, ret;
830 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
835 eflags = ucp->uc_mcontext.mc_eflags;
837 * Don't allow users to change privileged or reserved flags.
839 if (!EFL_SECURE(eflags, regs->tf_rflags)) {
840 uprintf("pid %d (%s): freebsd32_sigreturn eflags = 0x%x\n",
841 td->td_proc->p_pid, td->td_name, eflags);
846 * Don't allow users to load a valid privileged %cs. Let the
847 * hardware check for invalid selectors, excess privilege in
848 * other selectors, invalid %eip's and invalid %esp's.
850 cs = ucp->uc_mcontext.mc_cs;
851 if (!CS_SECURE(cs)) {
852 uprintf("pid %d (%s): sigreturn cs = 0x%x\n",
853 td->td_proc->p_pid, td->td_name, cs);
854 ksiginfo_init_trap(&ksi);
855 ksi.ksi_signo = SIGBUS;
856 ksi.ksi_code = BUS_OBJERR;
857 ksi.ksi_trapno = T_PROTFLT;
858 ksi.ksi_addr = (void *)regs->tf_rip;
859 trapsignal(td, &ksi);
863 if ((ucp->uc_mcontext.mc_flags & _MC_HASFPXSTATE) != 0) {
864 xfpustate_len = uc.uc_mcontext.mc_xfpustate_len;
865 if (xfpustate_len > cpu_max_ext_state_size -
866 sizeof(struct savefpu)) {
867 uprintf("pid %d (%s): sigreturn xfpusave_len = 0x%zx\n",
868 td->td_proc->p_pid, td->td_name, xfpustate_len);
871 xfpustate = (char *)fpu_save_area_alloc();
872 error = copyin(PTRIN(ucp->uc_mcontext.mc_xfpustate),
873 xfpustate, xfpustate_len);
875 fpu_save_area_free((struct savefpu *)xfpustate);
877 "pid %d (%s): sigreturn copying xfpustate failed\n",
878 td->td_proc->p_pid, td->td_name);
885 ret = ia32_set_fpcontext(td, &ucp->uc_mcontext, xfpustate,
887 fpu_save_area_free((struct savefpu *)xfpustate);
889 uprintf("pid %d (%s): sigreturn set_fpcontext err %d\n",
890 td->td_proc->p_pid, td->td_name, ret);
894 regs->tf_rdi = ucp->uc_mcontext.mc_edi;
895 regs->tf_rsi = ucp->uc_mcontext.mc_esi;
896 regs->tf_rbp = ucp->uc_mcontext.mc_ebp;
897 regs->tf_rbx = ucp->uc_mcontext.mc_ebx;
898 regs->tf_rdx = ucp->uc_mcontext.mc_edx;
899 regs->tf_rcx = ucp->uc_mcontext.mc_ecx;
900 regs->tf_rax = ucp->uc_mcontext.mc_eax;
901 regs->tf_trapno = ucp->uc_mcontext.mc_trapno;
902 regs->tf_err = ucp->uc_mcontext.mc_err;
903 regs->tf_rip = ucp->uc_mcontext.mc_eip;
905 regs->tf_rflags = ucp->uc_mcontext.mc_eflags;
906 regs->tf_rsp = ucp->uc_mcontext.mc_esp;
907 regs->tf_ss = ucp->uc_mcontext.mc_ss;
908 regs->tf_ds = ucp->uc_mcontext.mc_ds;
909 regs->tf_es = ucp->uc_mcontext.mc_es;
910 regs->tf_fs = ucp->uc_mcontext.mc_fs;
911 regs->tf_gs = ucp->uc_mcontext.mc_gs;
912 regs->tf_flags = TF_HASSEGS;
914 kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0);
915 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
916 return (EJUSTRETURN);
920 * Clear registers on exec
923 ia32_setregs(struct thread *td, struct image_params *imgp, uintptr_t stack)
925 struct trapframe *regs;
927 register_t saved_rflags;
932 if (td->td_proc->p_md.md_ldt != NULL)
940 pcb->pcb_initial_fpucw = __INITIAL_FPUCW_I386__;
942 saved_rflags = regs->tf_rflags & PSL_T;
943 bzero((char *)regs, sizeof(struct trapframe));
944 regs->tf_rip = imgp->entry_addr;
945 regs->tf_rsp = stack;
946 regs->tf_rflags = PSL_USER | saved_rflags;
947 regs->tf_ss = _udatasel;
948 regs->tf_cs = _ucode32sel;
949 regs->tf_rbx = (register_t)imgp->ps_strings;
950 regs->tf_ds = _udatasel;
951 regs->tf_es = _udatasel;
952 regs->tf_fs = _ufssel;
953 regs->tf_gs = _ugssel;
954 regs->tf_flags = TF_HASSEGS;
956 x86_clear_dbregs(pcb);
960 /* Return via doreti so that we can change to a different %cs */
961 set_pcb_flags(pcb, PCB_32BIT | PCB_FULL_IRET);