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 <sys/param.h> 33 #include <sys/exec.h> 34 #include <sys/fcntl.h> 35 #include <sys/imgact.h> 36 #include <sys/kernel.h> 37 #include <sys/lock.h> 38 #include <sys/malloc.h> 39 #include <sys/mutex.h> 40 #include <sys/mman.h> 41 #include <sys/namei.h> 42 #include <sys/pioctl.h> 43 #include <sys/proc.h> 44 #include <sys/procfs.h> 45 #include <sys/resourcevar.h> 46 #include <sys/systm.h> 47 #include <sys/signalvar.h> 48 #include <sys/stat.h> 49 #include <sys/sx.h> 50 #include <sys/syscall.h> 51 #include <sys/sysctl.h> 52 #include <sys/sysent.h> 53 #include <sys/vnode.h> 54 55 #include <vm/vm.h> 56 #include <vm/vm_kern.h> 57 #include <vm/vm_param.h> 58 #include <vm/pmap.h> 59 #include <vm/vm_map.h> 60 #include <vm/vm_object.h> 61 #include <vm/vm_extern.h> 62 63 #include <compat/freebsd32/freebsd32_util.h> 64 #include <compat/freebsd32/freebsd32_proto.h> 65 #include <machine/fpu.h> 66 #include <compat/ia32/ia32_reg.h> 67 #include <machine/psl.h> 68 #include <machine/segments.h> 69 #include <machine/specialreg.h> 70 #include <machine/frame.h> 71 #include <machine/md_var.h> 72 #include <machine/pcb.h> 73 #include <machine/cpufunc.h> 74 75 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL) 76 #define EFL_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0) 77 78 int 79 fill_regs32(struct thread *td, struct reg32 *regs) 80 { 81 struct pcb *pcb; 82 struct trapframe *tp; 83 84 tp = td->td_frame; 85 pcb = td->td_pcb; 86 regs->r_fs = pcb->pcb_fs; 87 regs->r_es = pcb->pcb_es; 88 regs->r_ds = pcb->pcb_ds; 89 regs->r_edi = tp->tf_rdi; 90 regs->r_esi = tp->tf_rsi; 91 regs->r_ebp = tp->tf_rbp; 92 regs->r_ebx = tp->tf_rbx; 93 regs->r_edx = tp->tf_rdx; 94 regs->r_ecx = tp->tf_rcx; 95 regs->r_eax = tp->tf_rax; 96 regs->r_eip = tp->tf_rip; 97 regs->r_cs = tp->tf_cs; 98 regs->r_eflags = tp->tf_rflags; 99 regs->r_esp = tp->tf_rsp; 100 regs->r_ss = tp->tf_ss; 101 regs->r_gs = pcb->pcb_gs; 102 return (0); 103 } 104 105 int 106 set_regs32(struct thread *td, struct reg32 *regs) 107 { 108 struct pcb *pcb; 109 struct trapframe *tp; 110 111 tp = td->td_frame; 112 if (!EFL_SECURE(regs->r_eflags, tp->tf_rflags) || !CS_SECURE(regs->r_cs)) 113 return (EINVAL); 114 pcb = td->td_pcb; 115 load_fs(regs->r_fs); 116 pcb->pcb_fs = regs->r_fs; 117 load_es(regs->r_es); 118 pcb->pcb_es = regs->r_es; 119 load_ds(regs->r_ds); 120 pcb->pcb_ds = regs->r_ds; 121 tp->tf_rdi = regs->r_edi; 122 tp->tf_rsi = regs->r_esi; 123 tp->tf_rbp = regs->r_ebp; 124 tp->tf_rbx = regs->r_ebx; 125 tp->tf_rdx = regs->r_edx; 126 tp->tf_rcx = regs->r_ecx; 127 tp->tf_rax = regs->r_eax; 128 tp->tf_rip = regs->r_eip; 129 tp->tf_cs = regs->r_cs; 130 tp->tf_rflags = regs->r_eflags; 131 tp->tf_rsp = regs->r_esp; 132 tp->tf_ss = regs->r_ss; 133 load_gs(regs->r_gs); 134 pcb->pcb_gs = regs->r_gs; 135 return (0); 136 } 137 138 int 139 fill_fpregs32(struct thread *td, struct fpreg32 *regs) 140 { 141 struct save87 *sv_87 = (struct save87 *)regs; 142 struct env87 *penv_87 = &sv_87->sv_env; 143 struct savefpu *sv_fpu = &td->td_pcb->pcb_save; 144 struct envxmm *penv_xmm = &sv_fpu->sv_env; 145 int i; 146 147 bzero(regs, sizeof(*regs)); 148 149 /* FPU control/status */ 150 penv_87->en_cw = penv_xmm->en_cw; 151 penv_87->en_sw = penv_xmm->en_sw; 152 penv_87->en_tw = penv_xmm->en_tw; 153 /* 154 * XXX for en_fip/fcs/foo/fos, check if the fxsave format 155 * uses the old-style layout for 32 bit user apps. If so, 156 * read the ip and operand segment registers from there. 157 * For now, use the process's %cs/%ds. 158 */ 159 penv_87->en_fip = penv_xmm->en_rip; 160 penv_87->en_fcs = td->td_frame->tf_cs; 161 penv_87->en_opcode = penv_xmm->en_opcode; 162 penv_87->en_foo = penv_xmm->en_rdp; 163 penv_87->en_fos = td->td_pcb->pcb_ds; 164 165 /* FPU registers */ 166 for (i = 0; i < 8; ++i) 167 sv_87->sv_ac[i] = sv_fpu->sv_fp[i].fp_acc; 168 169 return (0); 170 } 171 172 int 173 set_fpregs32(struct thread *td, struct fpreg32 *regs) 174 { 175 struct save87 *sv_87 = (struct save87 *)regs; 176 struct env87 *penv_87 = &sv_87->sv_env; 177 struct savefpu *sv_fpu = &td->td_pcb->pcb_save; 178 struct envxmm *penv_xmm = &sv_fpu->sv_env; 179 int i; 180 181 /* FPU control/status */ 182 penv_xmm->en_cw = penv_87->en_cw; 183 penv_xmm->en_sw = penv_87->en_sw; 184 penv_xmm->en_tw = penv_87->en_tw; 185 penv_xmm->en_rip = penv_87->en_fip; 186 /* penv_87->en_fcs and en_fos ignored, see above */ 187 penv_xmm->en_opcode = penv_87->en_opcode; 188 penv_xmm->en_rdp = penv_87->en_foo; 189 190 /* FPU registers */ 191 for (i = 0; i < 8; ++i) 192 sv_fpu->sv_fp[i].fp_acc = sv_87->sv_ac[i]; 193 for (i = 8; i < 16; ++i) 194 bzero(&sv_fpu->sv_fp[i].fp_acc, sizeof(sv_fpu->sv_fp[i].fp_acc)); 195 196 return (0); 197 } 198 199 int 200 fill_dbregs32(struct thread *td, struct dbreg32 *regs) 201 { 202 struct dbreg dr; 203 int err, i; 204 205 err = fill_dbregs(td, &dr); 206 for (i = 0; i < 8; i++) 207 regs->dr[i] = dr.dr[i]; 208 for (i = 8; i < 16; i++) 209 regs->dr[i] = 0; 210 return (err); 211 } 212 213 int 214 set_dbregs32(struct thread *td, struct dbreg32 *regs) 215 { 216 struct dbreg dr; 217 int i; 218 219 for (i = 0; i < 8; i++) 220 dr.dr[i] = regs->dr[i]; 221 for (i = 8; i < 16; i++) 222 dr.dr[i] = 0; 223 return (set_dbregs(td, &dr)); 224 } 225