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