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