]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - sys/amd64/ia32/ia32_signal.c
Copy stable/8 to releng/8.2 in preparation for FreeBSD-8.2 release.
[FreeBSD/releng/8.2.git] / sys / amd64 / ia32 / ia32_signal.c
1 /*-
2  * Copyright (c) 2003 Peter Wemm
3  * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * William Jolitz.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include "opt_compat.h"
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/pioctl.h>
50 #include <sys/proc.h>
51 #include <sys/procfs.h>
52 #include <sys/resourcevar.h>
53 #include <sys/systm.h>
54 #include <sys/signalvar.h>
55 #include <sys/stat.h>
56 #include <sys/sx.h>
57 #include <sys/syscall.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/ia32/ia32_signal.h>
74 #include <machine/psl.h>
75 #include <machine/segments.h>
76 #include <machine/specialreg.h>
77 #include <machine/frame.h>
78 #include <machine/md_var.h>
79 #include <machine/pcb.h>
80 #include <machine/cpufunc.h>
81
82 #ifdef COMPAT_FREEBSD4
83 static void freebsd4_ia32_sendsig(sig_t, ksiginfo_t *, sigset_t *);
84 #endif
85 static void ia32_get_fpcontext(struct thread *td, struct ia32_mcontext *mcp);
86 static int ia32_set_fpcontext(struct thread *td, const struct ia32_mcontext *mcp);
87
88 #define CS_SECURE(cs)           (ISPL(cs) == SEL_UPL)
89 #define EFL_SECURE(ef, oef)     ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
90
91 static void
92 ia32_get_fpcontext(struct thread *td, struct ia32_mcontext *mcp)
93 {
94
95         /*
96          * XXX Format of 64bit and 32bit FXSAVE areas differs. FXSAVE
97          * in 32bit mode saves %cs and %ds, while on 64bit it saves
98          * 64bit instruction and data pointers. Ignore the difference
99          * for now, it should be irrelevant for most applications.
100          */
101         mcp->mc_ownedfp = fpugetregs(td);
102         bcopy(&td->td_pcb->pcb_user_save, &mcp->mc_fpstate,
103             sizeof(mcp->mc_fpstate));
104         mcp->mc_fpformat = fpuformat();
105 }
106
107 static int
108 ia32_set_fpcontext(struct thread *td, const struct ia32_mcontext *mcp)
109 {
110
111         if (mcp->mc_fpformat == _MC_FPFMT_NODEV)
112                 return (0);
113         else if (mcp->mc_fpformat != _MC_FPFMT_XMM)
114                 return (EINVAL);
115         else if (mcp->mc_ownedfp == _MC_FPOWNED_NONE)
116                 /* We don't care what state is left in the FPU or PCB. */
117                 fpstate_drop(td);
118         else if (mcp->mc_ownedfp == _MC_FPOWNED_FPU ||
119             mcp->mc_ownedfp == _MC_FPOWNED_PCB) {
120                 fpusetregs(td, (struct savefpu *)&mcp->mc_fpstate);
121         } else
122                 return (EINVAL);
123         return (0);
124 }
125
126 /*
127  * Get machine context.
128  */
129 static int
130 ia32_get_mcontext(struct thread *td, struct ia32_mcontext *mcp, int flags)
131 {
132         struct trapframe *tp;
133
134         tp = td->td_frame;
135
136         PROC_LOCK(curthread->td_proc);
137         mcp->mc_onstack = sigonstack(tp->tf_rsp);
138         PROC_UNLOCK(curthread->td_proc);
139         /* Entry into kernel always sets TF_HASSEGS */
140         mcp->mc_gs = tp->tf_gs;
141         mcp->mc_fs = tp->tf_fs;
142         mcp->mc_es = tp->tf_es;
143         mcp->mc_ds = tp->tf_ds;
144         mcp->mc_edi = tp->tf_rdi;
145         mcp->mc_esi = tp->tf_rsi;
146         mcp->mc_ebp = tp->tf_rbp;
147         mcp->mc_isp = tp->tf_rsp;
148         mcp->mc_eflags = tp->tf_rflags;
149         if (flags & GET_MC_CLEAR_RET) {
150                 mcp->mc_eax = 0;
151                 mcp->mc_edx = 0;
152                 mcp->mc_eflags &= ~PSL_C;
153         } else {
154                 mcp->mc_eax = tp->tf_rax;
155                 mcp->mc_edx = tp->tf_rdx;
156         }
157         mcp->mc_ebx = tp->tf_rbx;
158         mcp->mc_ecx = tp->tf_rcx;
159         mcp->mc_eip = tp->tf_rip;
160         mcp->mc_cs = tp->tf_cs;
161         mcp->mc_esp = tp->tf_rsp;
162         mcp->mc_ss = tp->tf_ss;
163         mcp->mc_len = sizeof(*mcp);
164         ia32_get_fpcontext(td, mcp);
165         mcp->mc_fsbase = td->td_pcb->pcb_fsbase;
166         mcp->mc_gsbase = td->td_pcb->pcb_gsbase;
167         td->td_pcb->pcb_full_iret = 1;
168         return (0);
169 }
170
171 /*
172  * Set machine context.
173  *
174  * However, we don't set any but the user modifiable flags, and we won't
175  * touch the cs selector.
176  */
177 static int
178 ia32_set_mcontext(struct thread *td, const struct ia32_mcontext *mcp)
179 {
180         struct trapframe *tp;
181         long rflags;
182         int ret;
183
184         tp = td->td_frame;
185         if (mcp->mc_len != sizeof(*mcp))
186                 return (EINVAL);
187         rflags = (mcp->mc_eflags & PSL_USERCHANGE) |
188             (tp->tf_rflags & ~PSL_USERCHANGE);
189         ret = ia32_set_fpcontext(td, mcp);
190         if (ret != 0)
191                 return (ret);
192         tp->tf_gs = mcp->mc_gs;
193         tp->tf_fs = mcp->mc_fs;
194         tp->tf_es = mcp->mc_es;
195         tp->tf_ds = mcp->mc_ds;
196         tp->tf_flags = TF_HASSEGS;
197         tp->tf_rdi = mcp->mc_edi;
198         tp->tf_rsi = mcp->mc_esi;
199         tp->tf_rbp = mcp->mc_ebp;
200         tp->tf_rbx = mcp->mc_ebx;
201         tp->tf_rdx = mcp->mc_edx;
202         tp->tf_rcx = mcp->mc_ecx;
203         tp->tf_rax = mcp->mc_eax;
204         /* trapno, err */
205         tp->tf_rip = mcp->mc_eip;
206         tp->tf_rflags = rflags;
207         tp->tf_rsp = mcp->mc_esp;
208         tp->tf_ss = mcp->mc_ss;
209         td->td_pcb->pcb_full_iret = 1;
210         return (0);
211 }
212
213 /*
214  * The first two fields of a ucontext_t are the signal mask and
215  * the machine context.  The next field is uc_link; we want to
216  * avoid destroying the link when copying out contexts.
217  */
218 #define UC_COPY_SIZE    offsetof(struct ia32_ucontext, uc_link)
219
220 int
221 freebsd32_getcontext(struct thread *td, struct freebsd32_getcontext_args *uap)
222 {
223         struct ia32_ucontext uc;
224         int ret;
225
226         if (uap->ucp == NULL)
227                 ret = EINVAL;
228         else {
229                 ia32_get_mcontext(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
230                 PROC_LOCK(td->td_proc);
231                 uc.uc_sigmask = td->td_sigmask;
232                 PROC_UNLOCK(td->td_proc);
233                 ret = copyout(&uc, uap->ucp, UC_COPY_SIZE);
234         }
235         return (ret);
236 }
237
238 int
239 freebsd32_setcontext(struct thread *td, struct freebsd32_setcontext_args *uap)
240 {
241         struct ia32_ucontext uc;
242         int ret;        
243
244         if (uap->ucp == NULL)
245                 ret = EINVAL;
246         else {
247                 ret = copyin(uap->ucp, &uc, UC_COPY_SIZE);
248                 if (ret == 0) {
249                         ret = ia32_set_mcontext(td, &uc.uc_mcontext);
250                         if (ret == 0) {
251                                 kern_sigprocmask(td, SIG_SETMASK,
252                                     &uc.uc_sigmask, NULL, 0);
253                         }
254                 }
255         }
256         return (ret == 0 ? EJUSTRETURN : ret);
257 }
258
259 int
260 freebsd32_swapcontext(struct thread *td, struct freebsd32_swapcontext_args *uap)
261 {
262         struct ia32_ucontext uc;
263         int ret;        
264
265         if (uap->oucp == NULL || uap->ucp == NULL)
266                 ret = EINVAL;
267         else {
268                 ia32_get_mcontext(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
269                 PROC_LOCK(td->td_proc);
270                 uc.uc_sigmask = td->td_sigmask;
271                 PROC_UNLOCK(td->td_proc);
272                 ret = copyout(&uc, uap->oucp, UC_COPY_SIZE);
273                 if (ret == 0) {
274                         ret = copyin(uap->ucp, &uc, UC_COPY_SIZE);
275                         if (ret == 0) {
276                                 ret = ia32_set_mcontext(td, &uc.uc_mcontext);
277                                 if (ret == 0) {
278                                         kern_sigprocmask(td, SIG_SETMASK,
279                                             &uc.uc_sigmask, NULL, 0);
280                                 }
281                         }
282                 }
283         }
284         return (ret == 0 ? EJUSTRETURN : ret);
285 }
286
287 /*
288  * Send an interrupt to process.
289  *
290  * Stack is set up to allow sigcode stored
291  * at top to call routine, followed by kcall
292  * to sigreturn routine below.  After sigreturn
293  * resets the signal mask, the stack, and the
294  * frame pointer, it returns to the user
295  * specified pc, psl.
296  */
297 #ifdef COMPAT_FREEBSD4
298 static void
299 freebsd4_ia32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
300 {
301         struct ia32_sigframe4 sf, *sfp;
302         struct siginfo32 siginfo;
303         struct proc *p;
304         struct thread *td;
305         struct sigacts *psp;
306         struct trapframe *regs;
307         int oonstack;
308         int sig;
309
310         td = curthread;
311         p = td->td_proc;
312         siginfo_to_siginfo32(&ksi->ksi_info, &siginfo);
313
314         PROC_LOCK_ASSERT(p, MA_OWNED);
315         sig = siginfo.si_signo;
316         psp = p->p_sigacts;
317         mtx_assert(&psp->ps_mtx, MA_OWNED);
318         regs = td->td_frame;
319         oonstack = sigonstack(regs->tf_rsp);
320
321         /* Save user context. */
322         bzero(&sf, sizeof(sf));
323         sf.sf_uc.uc_sigmask = *mask;
324         sf.sf_uc.uc_stack.ss_sp = (uintptr_t)td->td_sigstk.ss_sp;
325         sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
326         sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
327             ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
328         sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
329         sf.sf_uc.uc_mcontext.mc_edi = regs->tf_rdi;
330         sf.sf_uc.uc_mcontext.mc_esi = regs->tf_rsi;
331         sf.sf_uc.uc_mcontext.mc_ebp = regs->tf_rbp;
332         sf.sf_uc.uc_mcontext.mc_isp = regs->tf_rsp; /* XXX */
333         sf.sf_uc.uc_mcontext.mc_ebx = regs->tf_rbx;
334         sf.sf_uc.uc_mcontext.mc_edx = regs->tf_rdx;
335         sf.sf_uc.uc_mcontext.mc_ecx = regs->tf_rcx;
336         sf.sf_uc.uc_mcontext.mc_eax = regs->tf_rax;
337         sf.sf_uc.uc_mcontext.mc_trapno = regs->tf_trapno;
338         sf.sf_uc.uc_mcontext.mc_err = regs->tf_err;
339         sf.sf_uc.uc_mcontext.mc_eip = regs->tf_rip;
340         sf.sf_uc.uc_mcontext.mc_cs = regs->tf_cs;
341         sf.sf_uc.uc_mcontext.mc_eflags = regs->tf_rflags;
342         sf.sf_uc.uc_mcontext.mc_esp = regs->tf_rsp;
343         sf.sf_uc.uc_mcontext.mc_ss = regs->tf_ss;
344         sf.sf_uc.uc_mcontext.mc_ds = regs->tf_ds;
345         sf.sf_uc.uc_mcontext.mc_es = regs->tf_es;
346         sf.sf_uc.uc_mcontext.mc_fs = regs->tf_fs;
347         sf.sf_uc.uc_mcontext.mc_gs = regs->tf_gs;
348
349         /* Allocate space for the signal handler context. */
350         if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
351             SIGISMEMBER(psp->ps_sigonstack, sig)) {
352                 sfp = (struct ia32_sigframe4 *)(td->td_sigstk.ss_sp +
353                     td->td_sigstk.ss_size - sizeof(sf));
354         } else
355                 sfp = (struct ia32_sigframe4 *)regs->tf_rsp - 1;
356         PROC_UNLOCK(p);
357
358         /* Translate the signal if appropriate. */
359         if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
360                 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
361
362         /* Build the argument list for the signal handler. */
363         sf.sf_signum = sig;
364         sf.sf_ucontext = (register_t)&sfp->sf_uc;
365         if (SIGISMEMBER(psp->ps_siginfo, sig)) {
366                 /* Signal handler installed with SA_SIGINFO. */
367                 sf.sf_siginfo = (u_int32_t)(uintptr_t)&sfp->sf_si;
368                 sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
369
370                 /* Fill in POSIX parts */
371                 sf.sf_si = siginfo;
372                 sf.sf_si.si_signo = sig;
373         } else {
374                 /* Old FreeBSD-style arguments. */
375                 sf.sf_siginfo = siginfo.si_code;
376                 sf.sf_addr = (u_int32_t)siginfo.si_addr;
377                 sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
378         }
379         mtx_unlock(&psp->ps_mtx);
380
381         /*
382          * Copy the sigframe out to the user's stack.
383          */
384         if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
385 #ifdef DEBUG
386                 printf("process %ld has trashed its stack\n", (long)p->p_pid);
387 #endif
388                 PROC_LOCK(p);
389                 sigexit(td, SIGILL);
390         }
391
392         regs->tf_rsp = (uintptr_t)sfp;
393         regs->tf_rip = p->p_sysent->sv_psstrings - sz_freebsd4_ia32_sigcode;
394         regs->tf_rflags &= ~(PSL_T | PSL_D);
395         regs->tf_cs = _ucode32sel;
396         regs->tf_ss = _udatasel;
397         regs->tf_ds = _udatasel;
398         regs->tf_es = _udatasel;
399         td->td_pcb->pcb_full_iret = 1;
400         /* leave user %fs and %gs untouched */
401         PROC_LOCK(p);
402         mtx_lock(&psp->ps_mtx);
403 }
404 #endif  /* COMPAT_FREEBSD4 */
405
406 void
407 ia32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
408 {
409         struct ia32_sigframe sf, *sfp;
410         struct siginfo32 siginfo;
411         struct proc *p;
412         struct thread *td;
413         struct sigacts *psp;
414         char *sp;
415         struct trapframe *regs;
416         int oonstack;
417         int sig;
418
419         siginfo_to_siginfo32(&ksi->ksi_info, &siginfo);
420         td = curthread;
421         p = td->td_proc;
422         PROC_LOCK_ASSERT(p, MA_OWNED);
423         sig = siginfo.si_signo;
424         psp = p->p_sigacts;
425 #ifdef COMPAT_FREEBSD4
426         if (SIGISMEMBER(psp->ps_freebsd4, sig)) {
427                 freebsd4_ia32_sendsig(catcher, ksi, mask);
428                 return;
429         }
430 #endif
431         mtx_assert(&psp->ps_mtx, MA_OWNED);
432         regs = td->td_frame;
433         oonstack = sigonstack(regs->tf_rsp);
434
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_edi = regs->tf_rdi;
444         sf.sf_uc.uc_mcontext.mc_esi = regs->tf_rsi;
445         sf.sf_uc.uc_mcontext.mc_ebp = regs->tf_rbp;
446         sf.sf_uc.uc_mcontext.mc_isp = regs->tf_rsp; /* XXX */
447         sf.sf_uc.uc_mcontext.mc_ebx = regs->tf_rbx;
448         sf.sf_uc.uc_mcontext.mc_edx = regs->tf_rdx;
449         sf.sf_uc.uc_mcontext.mc_ecx = regs->tf_rcx;
450         sf.sf_uc.uc_mcontext.mc_eax = regs->tf_rax;
451         sf.sf_uc.uc_mcontext.mc_trapno = regs->tf_trapno;
452         sf.sf_uc.uc_mcontext.mc_err = regs->tf_err;
453         sf.sf_uc.uc_mcontext.mc_eip = regs->tf_rip;
454         sf.sf_uc.uc_mcontext.mc_cs = regs->tf_cs;
455         sf.sf_uc.uc_mcontext.mc_eflags = regs->tf_rflags;
456         sf.sf_uc.uc_mcontext.mc_esp = regs->tf_rsp;
457         sf.sf_uc.uc_mcontext.mc_ss = regs->tf_ss;
458         sf.sf_uc.uc_mcontext.mc_ds = regs->tf_ds;
459         sf.sf_uc.uc_mcontext.mc_es = regs->tf_es;
460         sf.sf_uc.uc_mcontext.mc_fs = regs->tf_fs;
461         sf.sf_uc.uc_mcontext.mc_gs = regs->tf_gs;
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);
464         fpstate_drop(td);
465         sf.sf_uc.uc_mcontext.mc_fsbase = td->td_pcb->pcb_fsbase;
466         sf.sf_uc.uc_mcontext.mc_gsbase = td->td_pcb->pcb_gsbase;
467
468         /* Allocate space for the signal handler context. */
469         if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
470             SIGISMEMBER(psp->ps_sigonstack, sig)) {
471                 sp = td->td_sigstk.ss_sp +
472                     td->td_sigstk.ss_size - sizeof(sf);
473         } else
474                 sp = (char *)regs->tf_rsp - sizeof(sf);
475         /* Align to 16 bytes. */
476         sfp = (struct ia32_sigframe *)((uintptr_t)sp & ~0xF);
477         PROC_UNLOCK(p);
478
479         /* Translate the signal if appropriate. */
480         if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
481                 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
482
483         /* Build the argument list for the signal handler. */
484         sf.sf_signum = sig;
485         sf.sf_ucontext = (register_t)&sfp->sf_uc;
486         if (SIGISMEMBER(psp->ps_siginfo, sig)) {
487                 /* Signal handler installed with SA_SIGINFO. */
488                 sf.sf_siginfo = (u_int32_t)(uintptr_t)&sfp->sf_si;
489                 sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
490
491                 /* Fill in POSIX parts */
492                 sf.sf_si = siginfo;
493                 sf.sf_si.si_signo = sig;
494         } else {
495                 /* Old FreeBSD-style arguments. */
496                 sf.sf_siginfo = siginfo.si_code;
497                 sf.sf_addr = (u_int32_t)siginfo.si_addr;
498                 sf.sf_ah = (u_int32_t)(uintptr_t)catcher;
499         }
500         mtx_unlock(&psp->ps_mtx);
501
502         /*
503          * Copy the sigframe out to the user's stack.
504          */
505         if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
506 #ifdef DEBUG
507                 printf("process %ld has trashed its stack\n", (long)p->p_pid);
508 #endif
509                 PROC_LOCK(p);
510                 sigexit(td, SIGILL);
511         }
512
513         regs->tf_rsp = (uintptr_t)sfp;
514         regs->tf_rip = p->p_sysent->sv_psstrings - *(p->p_sysent->sv_szsigcode);
515         regs->tf_rflags &= ~(PSL_T | PSL_D);
516         regs->tf_cs = _ucode32sel;
517         regs->tf_ss = _udatasel;
518         regs->tf_ds = _udatasel;
519         regs->tf_es = _udatasel;
520         td->td_pcb->pcb_full_iret = 1;
521         /* XXXKIB leave user %fs and %gs untouched */
522         PROC_LOCK(p);
523         mtx_lock(&psp->ps_mtx);
524 }
525
526 /*
527  * System call to cleanup state after a signal
528  * has been taken.  Reset signal mask and
529  * stack state from context left by sendsig (above).
530  * Return to previous pc and psl as specified by
531  * context left by sendsig. Check carefully to
532  * make sure that the user has not modified the
533  * state to gain improper privileges.
534  */
535 #ifdef COMPAT_FREEBSD4
536 /*
537  * MPSAFE
538  */
539 int
540 freebsd4_freebsd32_sigreturn(td, uap)
541         struct thread *td;
542         struct freebsd4_freebsd32_sigreturn_args /* {
543                 const struct freebsd4_freebsd32_ucontext *sigcntxp;
544         } */ *uap;
545 {
546         struct ia32_ucontext4 uc;
547         struct trapframe *regs;
548         struct ia32_ucontext4 *ucp;
549         int cs, eflags, error;
550         ksiginfo_t ksi;
551
552         error = copyin(uap->sigcntxp, &uc, sizeof(uc));
553         if (error != 0)
554                 return (error);
555         ucp = &uc;
556         regs = td->td_frame;
557         eflags = ucp->uc_mcontext.mc_eflags;
558         /*
559          * Don't allow users to change privileged or reserved flags.
560          */
561         /*
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.
570          */
571         if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) {
572                 uprintf("pid %d (%s): freebsd4_freebsd32_sigreturn eflags = 0x%x\n",
573                     td->td_proc->p_pid, td->td_name, eflags);
574                 return (EINVAL);
575         }
576
577         /*
578          * Don't allow users to load a valid privileged %cs.  Let the
579          * hardware check for invalid selectors, excess privilege in
580          * other selectors, invalid %eip's and invalid %esp's.
581          */
582         cs = ucp->uc_mcontext.mc_cs;
583         if (!CS_SECURE(cs)) {
584                 uprintf("pid %d (%s): freebsd4_sigreturn cs = 0x%x\n",
585                     td->td_proc->p_pid, td->td_name, cs);
586                 ksiginfo_init_trap(&ksi);
587                 ksi.ksi_signo = SIGBUS;
588                 ksi.ksi_code = BUS_OBJERR;
589                 ksi.ksi_trapno = T_PROTFLT;
590                 ksi.ksi_addr = (void *)regs->tf_rip;
591                 trapsignal(td, &ksi);
592                 return (EINVAL);
593         }
594
595         regs->tf_rdi = ucp->uc_mcontext.mc_edi;
596         regs->tf_rsi = ucp->uc_mcontext.mc_esi;
597         regs->tf_rbp = ucp->uc_mcontext.mc_ebp;
598         regs->tf_rbx = ucp->uc_mcontext.mc_ebx;
599         regs->tf_rdx = ucp->uc_mcontext.mc_edx;
600         regs->tf_rcx = ucp->uc_mcontext.mc_ecx;
601         regs->tf_rax = ucp->uc_mcontext.mc_eax;
602         regs->tf_trapno = ucp->uc_mcontext.mc_trapno;
603         regs->tf_err = ucp->uc_mcontext.mc_err;
604         regs->tf_rip = ucp->uc_mcontext.mc_eip;
605         regs->tf_cs = cs;
606         regs->tf_rflags = ucp->uc_mcontext.mc_eflags;
607         regs->tf_rsp = ucp->uc_mcontext.mc_esp;
608         regs->tf_ss = ucp->uc_mcontext.mc_ss;
609         regs->tf_ds = ucp->uc_mcontext.mc_ds;
610         regs->tf_es = ucp->uc_mcontext.mc_es;
611         regs->tf_fs = ucp->uc_mcontext.mc_fs;
612         regs->tf_gs = ucp->uc_mcontext.mc_gs;
613
614         kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0);
615         td->td_pcb->pcb_full_iret = 1;
616         return (EJUSTRETURN);
617 }
618 #endif  /* COMPAT_FREEBSD4 */
619
620 /*
621  * MPSAFE
622  */
623 int
624 freebsd32_sigreturn(td, uap)
625         struct thread *td;
626         struct freebsd32_sigreturn_args /* {
627                 const struct freebsd32_ucontext *sigcntxp;
628         } */ *uap;
629 {
630         struct ia32_ucontext uc;
631         struct trapframe *regs;
632         struct ia32_ucontext *ucp;
633         int cs, eflags, error, ret;
634         ksiginfo_t ksi;
635
636         error = copyin(uap->sigcntxp, &uc, sizeof(uc));
637         if (error != 0)
638                 return (error);
639         ucp = &uc;
640         regs = td->td_frame;
641         eflags = ucp->uc_mcontext.mc_eflags;
642         /*
643          * Don't allow users to change privileged or reserved flags.
644          */
645         /*
646          * XXX do allow users to change the privileged flag PSL_RF.
647          * The cpu sets PSL_RF in tf_eflags for faults.  Debuggers
648          * should sometimes set it there too.  tf_eflags is kept in
649          * the signal context during signal handling and there is no
650          * other place to remember it, so the PSL_RF bit may be
651          * corrupted by the signal handler without us knowing.
652          * Corruption of the PSL_RF bit at worst causes one more or
653          * one less debugger trap, so allowing it is fairly harmless.
654          */
655         if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) {
656                 uprintf("pid %d (%s): freebsd32_sigreturn eflags = 0x%x\n",
657                     td->td_proc->p_pid, td->td_name, eflags);
658                 return (EINVAL);
659         }
660
661         /*
662          * Don't allow users to load a valid privileged %cs.  Let the
663          * hardware check for invalid selectors, excess privilege in
664          * other selectors, invalid %eip's and invalid %esp's.
665          */
666         cs = ucp->uc_mcontext.mc_cs;
667         if (!CS_SECURE(cs)) {
668                 uprintf("pid %d (%s): sigreturn cs = 0x%x\n",
669                     td->td_proc->p_pid, td->td_name, cs);
670                 ksiginfo_init_trap(&ksi);
671                 ksi.ksi_signo = SIGBUS;
672                 ksi.ksi_code = BUS_OBJERR;
673                 ksi.ksi_trapno = T_PROTFLT;
674                 ksi.ksi_addr = (void *)regs->tf_rip;
675                 trapsignal(td, &ksi);
676                 return (EINVAL);
677         }
678
679         ret = ia32_set_fpcontext(td, &ucp->uc_mcontext);
680         if (ret != 0)
681                 return (ret);
682
683         regs->tf_rdi = ucp->uc_mcontext.mc_edi;
684         regs->tf_rsi = ucp->uc_mcontext.mc_esi;
685         regs->tf_rbp = ucp->uc_mcontext.mc_ebp;
686         regs->tf_rbx = ucp->uc_mcontext.mc_ebx;
687         regs->tf_rdx = ucp->uc_mcontext.mc_edx;
688         regs->tf_rcx = ucp->uc_mcontext.mc_ecx;
689         regs->tf_rax = ucp->uc_mcontext.mc_eax;
690         regs->tf_trapno = ucp->uc_mcontext.mc_trapno;
691         regs->tf_err = ucp->uc_mcontext.mc_err;
692         regs->tf_rip = ucp->uc_mcontext.mc_eip;
693         regs->tf_cs = cs;
694         regs->tf_rflags = ucp->uc_mcontext.mc_eflags;
695         regs->tf_rsp = ucp->uc_mcontext.mc_esp;
696         regs->tf_ss = ucp->uc_mcontext.mc_ss;
697         regs->tf_ds = ucp->uc_mcontext.mc_ds;
698         regs->tf_es = ucp->uc_mcontext.mc_es;
699         regs->tf_fs = ucp->uc_mcontext.mc_fs;
700         regs->tf_gs = ucp->uc_mcontext.mc_gs;
701         regs->tf_flags = TF_HASSEGS;
702
703         kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0);
704         td->td_pcb->pcb_full_iret = 1;
705         return (EJUSTRETURN);
706 }
707
708 /*
709  * Clear registers on exec
710  */
711 void
712 ia32_setregs(td, entry, stack, ps_strings)
713         struct thread *td;
714         u_long entry;
715         u_long stack;
716         u_long ps_strings;
717 {
718         struct trapframe *regs = td->td_frame;
719         struct pcb *pcb = td->td_pcb;
720         
721         mtx_lock(&dt_lock);
722         if (td->td_proc->p_md.md_ldt != NULL)
723                 user_ldt_free(td);
724         else
725                 mtx_unlock(&dt_lock);
726
727         pcb->pcb_fsbase = 0;
728         pcb->pcb_gsbase = 0;
729         pcb->pcb_initial_fpucw = __INITIAL_FPUCW_I386__;
730
731         bzero((char *)regs, sizeof(struct trapframe));
732         regs->tf_rip = entry;
733         regs->tf_rsp = stack;
734         regs->tf_rflags = PSL_USER | (regs->tf_rflags & PSL_T);
735         regs->tf_ss = _udatasel;
736         regs->tf_cs = _ucode32sel;
737         regs->tf_rbx = ps_strings;
738         regs->tf_ds = _udatasel;
739         regs->tf_es = _udatasel;
740         regs->tf_fs = _ufssel;
741         regs->tf_gs = _ugssel;
742         regs->tf_flags = TF_HASSEGS;
743
744         load_cr0(rcr0() | CR0_MP | CR0_TS);
745         fpstate_drop(td);
746
747         /* Return via doreti so that we can change to a different %cs */
748         pcb->pcb_flags |= PCB_32BIT;
749         pcb->pcb_flags &= ~PCB_GS32BIT;
750         td->td_pcb->pcb_full_iret = 1;
751         td->td_retval[1] = 0;
752 }