]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/amd64/ia32/ia32_reg.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / amd64 / ia32 / ia32_reg.c
1 /*-
2  * Copyright (c) 2005 Peter Wemm
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include "opt_compat.h"
33
34 #include <sys/param.h>
35 #include <sys/exec.h>
36 #include <sys/fcntl.h>
37 #include <sys/imgact.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <sys/mman.h>
43 #include <sys/namei.h>
44 #include <sys/pioctl.h>
45 #include <sys/proc.h>
46 #include <sys/procfs.h>
47 #include <sys/resourcevar.h>
48 #include <sys/systm.h>
49 #include <sys/signalvar.h>
50 #include <sys/stat.h>
51 #include <sys/sx.h>
52 #include <sys/syscall.h>
53 #include <sys/sysctl.h>
54 #include <sys/sysent.h>
55 #include <sys/vnode.h>
56
57 #include <vm/vm.h>
58 #include <vm/vm_kern.h>
59 #include <vm/vm_param.h>
60 #include <vm/pmap.h>
61 #include <vm/vm_map.h>
62 #include <vm/vm_object.h>
63 #include <vm/vm_extern.h>
64
65 #include <compat/freebsd32/freebsd32_util.h>
66 #include <compat/freebsd32/freebsd32_proto.h>
67 #include <machine/fpu.h>
68 #include <machine/psl.h>
69 #include <machine/segments.h>
70 #include <machine/specialreg.h>
71 #include <machine/frame.h>
72 #include <machine/md_var.h>
73 #include <machine/pcb.h>
74 #include <machine/cpufunc.h>
75
76 #define CS_SECURE(cs)           (ISPL(cs) == SEL_UPL)
77 #define EFL_SECURE(ef, oef)     ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
78
79 int
80 fill_regs32(struct thread *td, struct reg32 *regs)
81 {
82         struct pcb *pcb;
83         struct trapframe *tp;
84
85         tp = td->td_frame;
86         pcb = td->td_pcb;
87         if (tp->tf_flags & TF_HASSEGS) {
88                 regs->r_gs = tp->tf_gs;
89                 regs->r_fs = tp->tf_fs;
90                 regs->r_es = tp->tf_es;
91                 regs->r_ds = tp->tf_ds;
92         } else {
93                 regs->r_gs = _ugssel;
94                 regs->r_fs = _ufssel;
95                 regs->r_es = _udatasel;
96                 regs->r_ds = _udatasel;
97         }
98         regs->r_edi = tp->tf_rdi;
99         regs->r_esi = tp->tf_rsi;
100         regs->r_ebp = tp->tf_rbp;
101         regs->r_ebx = tp->tf_rbx;
102         regs->r_edx = tp->tf_rdx;
103         regs->r_ecx = tp->tf_rcx;
104         regs->r_eax = tp->tf_rax;
105         regs->r_eip = tp->tf_rip;
106         regs->r_cs = tp->tf_cs;
107         regs->r_eflags = tp->tf_rflags;
108         regs->r_esp = tp->tf_rsp;
109         regs->r_ss = tp->tf_ss;
110         return (0);
111 }
112
113 int
114 set_regs32(struct thread *td, struct reg32 *regs)
115 {
116         struct pcb *pcb;
117         struct trapframe *tp;
118
119         tp = td->td_frame;
120         if (!EFL_SECURE(regs->r_eflags, tp->tf_rflags) || !CS_SECURE(regs->r_cs))
121                 return (EINVAL);
122         pcb = td->td_pcb;
123         tp->tf_gs = regs->r_gs;
124         tp->tf_fs = regs->r_fs;
125         tp->tf_es = regs->r_es;
126         tp->tf_ds = regs->r_ds;
127         set_pcb_flags(pcb, PCB_FULL_IRET);
128         tp->tf_flags = TF_HASSEGS;
129         tp->tf_rdi = regs->r_edi;
130         tp->tf_rsi = regs->r_esi;
131         tp->tf_rbp = regs->r_ebp;
132         tp->tf_rbx = regs->r_ebx;
133         tp->tf_rdx = regs->r_edx;
134         tp->tf_rcx = regs->r_ecx;
135         tp->tf_rax = regs->r_eax;
136         tp->tf_rip = regs->r_eip;
137         tp->tf_cs = regs->r_cs;
138         tp->tf_rflags = regs->r_eflags;
139         tp->tf_rsp = regs->r_esp;
140         tp->tf_ss = regs->r_ss;
141         return (0);
142 }
143
144 int
145 fill_fpregs32(struct thread *td, struct fpreg32 *regs)
146 {
147         struct savefpu *sv_fpu;
148         struct save87 *sv_87;
149         struct env87 *penv_87;
150         struct envxmm *penv_xmm;
151         int i;
152
153         bzero(regs, sizeof(*regs));
154         sv_87 = (struct save87 *)regs;
155         penv_87 = &sv_87->sv_env;
156         fpugetregs(td);
157         sv_fpu = get_pcb_user_save_td(td);
158         penv_xmm = &sv_fpu->sv_env;
159         
160         /* FPU control/status */
161         penv_87->en_cw = penv_xmm->en_cw;
162         penv_87->en_sw = penv_xmm->en_sw;
163         penv_87->en_tw = penv_xmm->en_tw;
164         /*
165          * XXX for en_fip/fcs/foo/fos, check if the fxsave format
166          * uses the old-style layout for 32 bit user apps.  If so,
167          * read the ip and operand segment registers from there.
168          * For now, use the process's %cs/%ds.
169          */
170         penv_87->en_fip = penv_xmm->en_rip;
171         penv_87->en_fcs = td->td_frame->tf_cs;
172         penv_87->en_opcode = penv_xmm->en_opcode;
173         penv_87->en_foo = penv_xmm->en_rdp;
174         /* Entry into the kernel always sets TF_HASSEGS */
175         penv_87->en_fos = td->td_frame->tf_ds;
176
177         /* FPU registers */
178         for (i = 0; i < 8; ++i)
179                 sv_87->sv_ac[i] = sv_fpu->sv_fp[i].fp_acc;
180
181         return (0);
182 }
183
184 int
185 set_fpregs32(struct thread *td, struct fpreg32 *regs)
186 {
187         struct save87 *sv_87 = (struct save87 *)regs;
188         struct env87 *penv_87 = &sv_87->sv_env;
189         struct savefpu *sv_fpu = get_pcb_user_save_td(td);
190         struct envxmm *penv_xmm = &sv_fpu->sv_env;
191         int i;
192
193         /* FPU control/status */
194         penv_xmm->en_cw = penv_87->en_cw;
195         penv_xmm->en_sw = penv_87->en_sw;
196         penv_xmm->en_tw = penv_87->en_tw;
197         penv_xmm->en_rip = penv_87->en_fip;
198         /* penv_87->en_fcs and en_fos ignored, see above */
199         penv_xmm->en_opcode = penv_87->en_opcode;
200         penv_xmm->en_rdp = penv_87->en_foo;
201
202         /* FPU registers */
203         for (i = 0; i < 8; ++i)
204                 sv_fpu->sv_fp[i].fp_acc = sv_87->sv_ac[i];
205         for (i = 8; i < 16; ++i)
206                 bzero(&sv_fpu->sv_fp[i].fp_acc, sizeof(sv_fpu->sv_fp[i].fp_acc));
207         fpuuserinited(td);
208
209         return (0);
210 }
211
212 int
213 fill_dbregs32(struct thread *td, struct dbreg32 *regs)
214 {
215         struct dbreg dr;
216         int err, i;
217
218         err = fill_dbregs(td, &dr);
219         for (i = 0; i < 8; i++)
220                 regs->dr[i] = dr.dr[i];
221         return (err);
222 }
223
224 int
225 set_dbregs32(struct thread *td, struct dbreg32 *regs)
226 {
227         struct dbreg dr;
228         int i;
229
230         for (i = 0; i < 8; i++)
231                 dr.dr[i] = regs->dr[i];
232         for (i = 8; i < 16; i++)
233                 dr.dr[i] = 0;
234         return (set_dbregs(td, &dr));
235 }