1 | /* $Id: IEMAllCImpl.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IEM - Instruction Implementation in C/C++ (code include).
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #define LOG_GROUP LOG_GROUP_IEM
|
---|
33 | #define VMCPU_INCL_CPUM_GST_CTX
|
---|
34 | #include <VBox/vmm/iem.h>
|
---|
35 | #include <VBox/vmm/cpum.h>
|
---|
36 | #include <VBox/vmm/apic.h>
|
---|
37 | #include <VBox/vmm/pdm.h>
|
---|
38 | #include <VBox/vmm/pgm.h>
|
---|
39 | #include <VBox/vmm/iom.h>
|
---|
40 | #include <VBox/vmm/em.h>
|
---|
41 | #include <VBox/vmm/hm.h>
|
---|
42 | #include <VBox/vmm/nem.h>
|
---|
43 | #include <VBox/vmm/gim.h>
|
---|
44 | #ifdef VBOX_WITH_NESTED_HWVIRT_SVM
|
---|
45 | # include <VBox/vmm/em.h>
|
---|
46 | # include <VBox/vmm/hm_svm.h>
|
---|
47 | #endif
|
---|
48 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
49 | # include <VBox/vmm/hmvmxinline.h>
|
---|
50 | #endif
|
---|
51 | #ifndef VBOX_WITHOUT_CPUID_HOST_CALL
|
---|
52 | # include <VBox/vmm/cpuidcall.h>
|
---|
53 | #endif
|
---|
54 | #include <VBox/vmm/tm.h>
|
---|
55 | #include <VBox/vmm/dbgf.h>
|
---|
56 | #include <VBox/vmm/dbgftrace.h>
|
---|
57 | #include "IEMInternal.h"
|
---|
58 | #include <VBox/vmm/vmcc.h>
|
---|
59 | #include <VBox/log.h>
|
---|
60 | #include <VBox/err.h>
|
---|
61 | #include <VBox/param.h>
|
---|
62 | #include <VBox/dis.h>
|
---|
63 | #include <VBox/disopcode.h>
|
---|
64 | #include <iprt/asm-math.h>
|
---|
65 | #include <iprt/assert.h>
|
---|
66 | #include <iprt/string.h>
|
---|
67 | #include <iprt/x86.h>
|
---|
68 |
|
---|
69 | #include "IEMInline.h"
|
---|
70 |
|
---|
71 |
|
---|
72 | /*********************************************************************************************************************************
|
---|
73 | * Defined Constants And Macros *
|
---|
74 | *********************************************************************************************************************************/
|
---|
75 | /**
|
---|
76 | * Flushes the prefetch buffer, light version.
|
---|
77 | */
|
---|
78 | #ifndef IEM_WITH_CODE_TLB
|
---|
79 | # define IEM_FLUSH_PREFETCH_LIGHT(a_pVCpu, a_cbInstr) do { (a_pVCpu)->iem.s.cbOpcode = (a_cbInstr); } while (0)
|
---|
80 | #else
|
---|
81 | # define IEM_FLUSH_PREFETCH_LIGHT(a_pVCpu, a_cbInstr) do { } while (0)
|
---|
82 | #endif
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Flushes the prefetch buffer, heavy version.
|
---|
86 | */
|
---|
87 | #ifndef IEM_WITH_CODE_TLB
|
---|
88 | # define IEM_FLUSH_PREFETCH_HEAVY(a_pVCpu, a_cbInstr) do { (a_pVCpu)->iem.s.cbOpcode = (a_cbInstr); } while (0)
|
---|
89 | #else
|
---|
90 | # if 1
|
---|
91 | # define IEM_FLUSH_PREFETCH_HEAVY(a_pVCpu, a_cbInstr) do { (a_pVCpu)->iem.s.pbInstrBuf = NULL; } while (0)
|
---|
92 | # else
|
---|
93 | # define IEM_FLUSH_PREFETCH_HEAVY(a_pVCpu, a_cbInstr) do { } while (0)
|
---|
94 | # endif
|
---|
95 | #endif
|
---|
96 |
|
---|
97 |
|
---|
98 |
|
---|
99 | /** @name Misc Helpers
|
---|
100 | * @{
|
---|
101 | */
|
---|
102 |
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Worker function for iemHlpCheckPortIOPermission, don't call directly.
|
---|
106 | *
|
---|
107 | * @returns Strict VBox status code.
|
---|
108 | *
|
---|
109 | * @param pVCpu The cross context virtual CPU structure of the calling thread.
|
---|
110 | * @param u16Port The port number.
|
---|
111 | * @param cbOperand The operand size.
|
---|
112 | */
|
---|
113 | static VBOXSTRICTRC iemHlpCheckPortIOPermissionBitmap(PVMCPUCC pVCpu, uint16_t u16Port, uint8_t cbOperand)
|
---|
114 | {
|
---|
115 | /* The TSS bits we're interested in are the same on 386 and AMD64. */
|
---|
116 | AssertCompile(AMD64_SEL_TYPE_SYS_TSS_BUSY == X86_SEL_TYPE_SYS_386_TSS_BUSY);
|
---|
117 | AssertCompile(AMD64_SEL_TYPE_SYS_TSS_AVAIL == X86_SEL_TYPE_SYS_386_TSS_AVAIL);
|
---|
118 | AssertCompileMembersAtSameOffset(X86TSS32, offIoBitmap, X86TSS64, offIoBitmap);
|
---|
119 | AssertCompile(sizeof(X86TSS32) == sizeof(X86TSS64));
|
---|
120 |
|
---|
121 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_TR);
|
---|
122 |
|
---|
123 | /*
|
---|
124 | * Check the TSS type, 16-bit TSSes doesn't have any I/O permission bitmap.
|
---|
125 | */
|
---|
126 | Assert(!pVCpu->cpum.GstCtx.tr.Attr.n.u1DescType);
|
---|
127 | if (RT_UNLIKELY( pVCpu->cpum.GstCtx.tr.Attr.n.u4Type != AMD64_SEL_TYPE_SYS_TSS_BUSY
|
---|
128 | && pVCpu->cpum.GstCtx.tr.Attr.n.u4Type != AMD64_SEL_TYPE_SYS_TSS_AVAIL))
|
---|
129 | {
|
---|
130 | Log(("iemHlpCheckPortIOPermissionBitmap: Port=%#x cb=%d - TSS type %#x (attr=%#x) has no I/O bitmap -> #GP(0)\n",
|
---|
131 | u16Port, cbOperand, pVCpu->cpum.GstCtx.tr.Attr.n.u4Type, pVCpu->cpum.GstCtx.tr.Attr.u));
|
---|
132 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
133 | }
|
---|
134 |
|
---|
135 | /*
|
---|
136 | * Read the bitmap offset (may #PF).
|
---|
137 | */
|
---|
138 | uint16_t offBitmap;
|
---|
139 | VBOXSTRICTRC rcStrict = iemMemFetchSysU16(pVCpu, &offBitmap, UINT8_MAX,
|
---|
140 | pVCpu->cpum.GstCtx.tr.u64Base + RT_UOFFSETOF(X86TSS64, offIoBitmap));
|
---|
141 | if (rcStrict != VINF_SUCCESS)
|
---|
142 | {
|
---|
143 | Log(("iemHlpCheckPortIOPermissionBitmap: Error reading offIoBitmap (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
144 | return rcStrict;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /*
|
---|
148 | * The bit range from u16Port to (u16Port + cbOperand - 1), however intel
|
---|
149 | * describes the CPU actually reading two bytes regardless of whether the
|
---|
150 | * bit range crosses a byte boundrary. Thus the + 1 in the test below.
|
---|
151 | */
|
---|
152 | uint32_t offFirstBit = (uint32_t)u16Port / 8 + offBitmap;
|
---|
153 | /** @todo check if real CPUs ensures that offBitmap has a minimum value of
|
---|
154 | * for instance sizeof(X86TSS32). */
|
---|
155 | if (offFirstBit + 1 > pVCpu->cpum.GstCtx.tr.u32Limit) /* the limit is inclusive */
|
---|
156 | {
|
---|
157 | Log(("iemHlpCheckPortIOPermissionBitmap: offFirstBit=%#x + 1 is beyond u32Limit=%#x -> #GP(0)\n",
|
---|
158 | offFirstBit, pVCpu->cpum.GstCtx.tr.u32Limit));
|
---|
159 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
160 | }
|
---|
161 |
|
---|
162 | /*
|
---|
163 | * Read the necessary bits.
|
---|
164 | */
|
---|
165 | /** @todo Test the assertion in the intel manual that the CPU reads two
|
---|
166 | * bytes. The question is how this works wrt to \#PF and \#GP on the
|
---|
167 | * 2nd byte when it's not required. */
|
---|
168 | uint16_t bmBytes = UINT16_MAX;
|
---|
169 | rcStrict = iemMemFetchSysU16(pVCpu, &bmBytes, UINT8_MAX, pVCpu->cpum.GstCtx.tr.u64Base + offFirstBit);
|
---|
170 | if (rcStrict != VINF_SUCCESS)
|
---|
171 | {
|
---|
172 | Log(("iemHlpCheckPortIOPermissionBitmap: Error reading I/O bitmap @%#x (%Rrc)\n", offFirstBit, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
173 | return rcStrict;
|
---|
174 | }
|
---|
175 |
|
---|
176 | /*
|
---|
177 | * Perform the check.
|
---|
178 | */
|
---|
179 | uint16_t fPortMask = (1 << cbOperand) - 1;
|
---|
180 | bmBytes >>= (u16Port & 7);
|
---|
181 | if (bmBytes & fPortMask)
|
---|
182 | {
|
---|
183 | Log(("iemHlpCheckPortIOPermissionBitmap: u16Port=%#x LB %u - access denied (bm=%#x mask=%#x) -> #GP(0)\n",
|
---|
184 | u16Port, cbOperand, bmBytes, fPortMask));
|
---|
185 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
186 | }
|
---|
187 |
|
---|
188 | return VINF_SUCCESS;
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Checks if we are allowed to access the given I/O port, raising the
|
---|
194 | * appropriate exceptions if we aren't (or if the I/O bitmap is not
|
---|
195 | * accessible).
|
---|
196 | *
|
---|
197 | * @returns Strict VBox status code.
|
---|
198 | *
|
---|
199 | * @param pVCpu The cross context virtual CPU structure of the calling thread.
|
---|
200 | * @param u16Port The port number.
|
---|
201 | * @param cbOperand The operand size.
|
---|
202 | */
|
---|
203 | DECLINLINE(VBOXSTRICTRC) iemHlpCheckPortIOPermission(PVMCPUCC pVCpu, uint16_t u16Port, uint8_t cbOperand)
|
---|
204 | {
|
---|
205 | X86EFLAGS Efl;
|
---|
206 | Efl.u = IEMMISC_GET_EFL(pVCpu);
|
---|
207 | if ( (pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE)
|
---|
208 | && ( pVCpu->iem.s.uCpl > Efl.Bits.u2IOPL
|
---|
209 | || Efl.Bits.u1VM) )
|
---|
210 | return iemHlpCheckPortIOPermissionBitmap(pVCpu, u16Port, cbOperand);
|
---|
211 | return VINF_SUCCESS;
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 | #if 0
|
---|
216 | /**
|
---|
217 | * Calculates the parity bit.
|
---|
218 | *
|
---|
219 | * @returns true if the bit is set, false if not.
|
---|
220 | * @param u8Result The least significant byte of the result.
|
---|
221 | */
|
---|
222 | static bool iemHlpCalcParityFlag(uint8_t u8Result)
|
---|
223 | {
|
---|
224 | /*
|
---|
225 | * Parity is set if the number of bits in the least significant byte of
|
---|
226 | * the result is even.
|
---|
227 | */
|
---|
228 | uint8_t cBits;
|
---|
229 | cBits = u8Result & 1; /* 0 */
|
---|
230 | u8Result >>= 1;
|
---|
231 | cBits += u8Result & 1;
|
---|
232 | u8Result >>= 1;
|
---|
233 | cBits += u8Result & 1;
|
---|
234 | u8Result >>= 1;
|
---|
235 | cBits += u8Result & 1;
|
---|
236 | u8Result >>= 1;
|
---|
237 | cBits += u8Result & 1; /* 4 */
|
---|
238 | u8Result >>= 1;
|
---|
239 | cBits += u8Result & 1;
|
---|
240 | u8Result >>= 1;
|
---|
241 | cBits += u8Result & 1;
|
---|
242 | u8Result >>= 1;
|
---|
243 | cBits += u8Result & 1;
|
---|
244 | return !(cBits & 1);
|
---|
245 | }
|
---|
246 | #endif /* not used */
|
---|
247 |
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * Updates the specified flags according to a 8-bit result.
|
---|
251 | *
|
---|
252 | * @param pVCpu The cross context virtual CPU structure of the calling thread.
|
---|
253 | * @param u8Result The result to set the flags according to.
|
---|
254 | * @param fToUpdate The flags to update.
|
---|
255 | * @param fUndefined The flags that are specified as undefined.
|
---|
256 | */
|
---|
257 | static void iemHlpUpdateArithEFlagsU8(PVMCPUCC pVCpu, uint8_t u8Result, uint32_t fToUpdate, uint32_t fUndefined)
|
---|
258 | {
|
---|
259 | uint32_t fEFlags = pVCpu->cpum.GstCtx.eflags.u;
|
---|
260 | iemAImpl_test_u8(&u8Result, u8Result, &fEFlags);
|
---|
261 | pVCpu->cpum.GstCtx.eflags.u &= ~(fToUpdate | fUndefined);
|
---|
262 | pVCpu->cpum.GstCtx.eflags.u |= (fToUpdate | fUndefined) & fEFlags;
|
---|
263 | }
|
---|
264 |
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * Updates the specified flags according to a 16-bit result.
|
---|
268 | *
|
---|
269 | * @param pVCpu The cross context virtual CPU structure of the calling thread.
|
---|
270 | * @param u16Result The result to set the flags according to.
|
---|
271 | * @param fToUpdate The flags to update.
|
---|
272 | * @param fUndefined The flags that are specified as undefined.
|
---|
273 | */
|
---|
274 | static void iemHlpUpdateArithEFlagsU16(PVMCPUCC pVCpu, uint16_t u16Result, uint32_t fToUpdate, uint32_t fUndefined)
|
---|
275 | {
|
---|
276 | uint32_t fEFlags = pVCpu->cpum.GstCtx.eflags.u;
|
---|
277 | iemAImpl_test_u16(&u16Result, u16Result, &fEFlags);
|
---|
278 | pVCpu->cpum.GstCtx.eflags.u &= ~(fToUpdate | fUndefined);
|
---|
279 | pVCpu->cpum.GstCtx.eflags.u |= (fToUpdate | fUndefined) & fEFlags;
|
---|
280 | }
|
---|
281 |
|
---|
282 |
|
---|
283 | /**
|
---|
284 | * Helper used by iret.
|
---|
285 | *
|
---|
286 | * @param pVCpu The cross context virtual CPU structure of the calling thread.
|
---|
287 | * @param uCpl The new CPL.
|
---|
288 | * @param pSReg Pointer to the segment register.
|
---|
289 | */
|
---|
290 | static void iemHlpAdjustSelectorForNewCpl(PVMCPUCC pVCpu, uint8_t uCpl, PCPUMSELREG pSReg)
|
---|
291 | {
|
---|
292 | Assert(CPUMSELREG_ARE_HIDDEN_PARTS_VALID(pVCpu, pSReg));
|
---|
293 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_SREG_MASK);
|
---|
294 |
|
---|
295 | if ( uCpl > pSReg->Attr.n.u2Dpl
|
---|
296 | && pSReg->Attr.n.u1DescType /* code or data, not system */
|
---|
297 | && (pSReg->Attr.n.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
|
---|
298 | != (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF)) /* not conforming code */
|
---|
299 | iemHlpLoadNullDataSelectorProt(pVCpu, pSReg, 0);
|
---|
300 | }
|
---|
301 |
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * Indicates that we have modified the FPU state.
|
---|
305 | *
|
---|
306 | * @param pVCpu The cross context virtual CPU structure of the calling thread.
|
---|
307 | */
|
---|
308 | DECLINLINE(void) iemHlpUsedFpu(PVMCPUCC pVCpu)
|
---|
309 | {
|
---|
310 | CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_FPU_REM);
|
---|
311 | }
|
---|
312 |
|
---|
313 | /** @} */
|
---|
314 |
|
---|
315 | /** @name C Implementations
|
---|
316 | * @{
|
---|
317 | */
|
---|
318 |
|
---|
319 | /**
|
---|
320 | * Implements a 16-bit popa.
|
---|
321 | */
|
---|
322 | IEM_CIMPL_DEF_0(iemCImpl_popa_16)
|
---|
323 | {
|
---|
324 | RTGCPTR GCPtrStart = iemRegGetEffRsp(pVCpu);
|
---|
325 | RTGCPTR GCPtrLast = GCPtrStart + 15;
|
---|
326 | VBOXSTRICTRC rcStrict;
|
---|
327 |
|
---|
328 | /*
|
---|
329 | * The docs are a bit hard to comprehend here, but it looks like we wrap
|
---|
330 | * around in real mode as long as none of the individual "popa" crosses the
|
---|
331 | * end of the stack segment. In protected mode we check the whole access
|
---|
332 | * in one go. For efficiency, only do the word-by-word thing if we're in
|
---|
333 | * danger of wrapping around.
|
---|
334 | */
|
---|
335 | /** @todo do popa boundary / wrap-around checks. */
|
---|
336 | if (RT_UNLIKELY( IEM_IS_REAL_OR_V86_MODE(pVCpu)
|
---|
337 | && (pVCpu->cpum.GstCtx.cs.u32Limit < GCPtrLast)) ) /* ASSUMES 64-bit RTGCPTR */
|
---|
338 | {
|
---|
339 | /* word-by-word */
|
---|
340 | RTUINT64U TmpRsp;
|
---|
341 | TmpRsp.u = pVCpu->cpum.GstCtx.rsp;
|
---|
342 | rcStrict = iemMemStackPopU16Ex(pVCpu, &pVCpu->cpum.GstCtx.di, &TmpRsp);
|
---|
343 | if (rcStrict == VINF_SUCCESS)
|
---|
344 | rcStrict = iemMemStackPopU16Ex(pVCpu, &pVCpu->cpum.GstCtx.si, &TmpRsp);
|
---|
345 | if (rcStrict == VINF_SUCCESS)
|
---|
346 | rcStrict = iemMemStackPopU16Ex(pVCpu, &pVCpu->cpum.GstCtx.bp, &TmpRsp);
|
---|
347 | if (rcStrict == VINF_SUCCESS)
|
---|
348 | {
|
---|
349 | iemRegAddToRspEx(pVCpu, &TmpRsp, 2); /* sp */
|
---|
350 | rcStrict = iemMemStackPopU16Ex(pVCpu, &pVCpu->cpum.GstCtx.bx, &TmpRsp);
|
---|
351 | }
|
---|
352 | if (rcStrict == VINF_SUCCESS)
|
---|
353 | rcStrict = iemMemStackPopU16Ex(pVCpu, &pVCpu->cpum.GstCtx.dx, &TmpRsp);
|
---|
354 | if (rcStrict == VINF_SUCCESS)
|
---|
355 | rcStrict = iemMemStackPopU16Ex(pVCpu, &pVCpu->cpum.GstCtx.cx, &TmpRsp);
|
---|
356 | if (rcStrict == VINF_SUCCESS)
|
---|
357 | rcStrict = iemMemStackPopU16Ex(pVCpu, &pVCpu->cpum.GstCtx.ax, &TmpRsp);
|
---|
358 | if (rcStrict == VINF_SUCCESS)
|
---|
359 | {
|
---|
360 | pVCpu->cpum.GstCtx.rsp = TmpRsp.u;
|
---|
361 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
362 | }
|
---|
363 | }
|
---|
364 | else
|
---|
365 | {
|
---|
366 | uint16_t const *pa16Mem = NULL;
|
---|
367 | rcStrict = iemMemMap(pVCpu, (void **)&pa16Mem, 16, X86_SREG_SS, GCPtrStart, IEM_ACCESS_STACK_R, sizeof(*pa16Mem) - 1);
|
---|
368 | if (rcStrict == VINF_SUCCESS)
|
---|
369 | {
|
---|
370 | pVCpu->cpum.GstCtx.di = pa16Mem[7 - X86_GREG_xDI];
|
---|
371 | pVCpu->cpum.GstCtx.si = pa16Mem[7 - X86_GREG_xSI];
|
---|
372 | pVCpu->cpum.GstCtx.bp = pa16Mem[7 - X86_GREG_xBP];
|
---|
373 | /* skip sp */
|
---|
374 | pVCpu->cpum.GstCtx.bx = pa16Mem[7 - X86_GREG_xBX];
|
---|
375 | pVCpu->cpum.GstCtx.dx = pa16Mem[7 - X86_GREG_xDX];
|
---|
376 | pVCpu->cpum.GstCtx.cx = pa16Mem[7 - X86_GREG_xCX];
|
---|
377 | pVCpu->cpum.GstCtx.ax = pa16Mem[7 - X86_GREG_xAX];
|
---|
378 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)pa16Mem, IEM_ACCESS_STACK_R);
|
---|
379 | if (rcStrict == VINF_SUCCESS)
|
---|
380 | {
|
---|
381 | iemRegAddToRsp(pVCpu, 16);
|
---|
382 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
383 | }
|
---|
384 | }
|
---|
385 | }
|
---|
386 | return rcStrict;
|
---|
387 | }
|
---|
388 |
|
---|
389 |
|
---|
390 | /**
|
---|
391 | * Implements a 32-bit popa.
|
---|
392 | */
|
---|
393 | IEM_CIMPL_DEF_0(iemCImpl_popa_32)
|
---|
394 | {
|
---|
395 | RTGCPTR GCPtrStart = iemRegGetEffRsp(pVCpu);
|
---|
396 | RTGCPTR GCPtrLast = GCPtrStart + 31;
|
---|
397 | VBOXSTRICTRC rcStrict;
|
---|
398 |
|
---|
399 | /*
|
---|
400 | * The docs are a bit hard to comprehend here, but it looks like we wrap
|
---|
401 | * around in real mode as long as none of the individual "popa" crosses the
|
---|
402 | * end of the stack segment. In protected mode we check the whole access
|
---|
403 | * in one go. For efficiency, only do the word-by-word thing if we're in
|
---|
404 | * danger of wrapping around.
|
---|
405 | */
|
---|
406 | /** @todo do popa boundary / wrap-around checks. */
|
---|
407 | if (RT_UNLIKELY( IEM_IS_REAL_OR_V86_MODE(pVCpu)
|
---|
408 | && (pVCpu->cpum.GstCtx.cs.u32Limit < GCPtrLast)) ) /* ASSUMES 64-bit RTGCPTR */
|
---|
409 | {
|
---|
410 | /* word-by-word */
|
---|
411 | RTUINT64U TmpRsp;
|
---|
412 | TmpRsp.u = pVCpu->cpum.GstCtx.rsp;
|
---|
413 | rcStrict = iemMemStackPopU32Ex(pVCpu, &pVCpu->cpum.GstCtx.edi, &TmpRsp);
|
---|
414 | if (rcStrict == VINF_SUCCESS)
|
---|
415 | rcStrict = iemMemStackPopU32Ex(pVCpu, &pVCpu->cpum.GstCtx.esi, &TmpRsp);
|
---|
416 | if (rcStrict == VINF_SUCCESS)
|
---|
417 | rcStrict = iemMemStackPopU32Ex(pVCpu, &pVCpu->cpum.GstCtx.ebp, &TmpRsp);
|
---|
418 | if (rcStrict == VINF_SUCCESS)
|
---|
419 | {
|
---|
420 | iemRegAddToRspEx(pVCpu, &TmpRsp, 2); /* sp */
|
---|
421 | rcStrict = iemMemStackPopU32Ex(pVCpu, &pVCpu->cpum.GstCtx.ebx, &TmpRsp);
|
---|
422 | }
|
---|
423 | if (rcStrict == VINF_SUCCESS)
|
---|
424 | rcStrict = iemMemStackPopU32Ex(pVCpu, &pVCpu->cpum.GstCtx.edx, &TmpRsp);
|
---|
425 | if (rcStrict == VINF_SUCCESS)
|
---|
426 | rcStrict = iemMemStackPopU32Ex(pVCpu, &pVCpu->cpum.GstCtx.ecx, &TmpRsp);
|
---|
427 | if (rcStrict == VINF_SUCCESS)
|
---|
428 | rcStrict = iemMemStackPopU32Ex(pVCpu, &pVCpu->cpum.GstCtx.eax, &TmpRsp);
|
---|
429 | if (rcStrict == VINF_SUCCESS)
|
---|
430 | {
|
---|
431 | #if 1 /** @todo what actually happens with the high bits when we're in 16-bit mode? */
|
---|
432 | pVCpu->cpum.GstCtx.rdi &= UINT32_MAX;
|
---|
433 | pVCpu->cpum.GstCtx.rsi &= UINT32_MAX;
|
---|
434 | pVCpu->cpum.GstCtx.rbp &= UINT32_MAX;
|
---|
435 | pVCpu->cpum.GstCtx.rbx &= UINT32_MAX;
|
---|
436 | pVCpu->cpum.GstCtx.rdx &= UINT32_MAX;
|
---|
437 | pVCpu->cpum.GstCtx.rcx &= UINT32_MAX;
|
---|
438 | pVCpu->cpum.GstCtx.rax &= UINT32_MAX;
|
---|
439 | #endif
|
---|
440 | pVCpu->cpum.GstCtx.rsp = TmpRsp.u;
|
---|
441 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
442 | }
|
---|
443 | }
|
---|
444 | else
|
---|
445 | {
|
---|
446 | uint32_t const *pa32Mem;
|
---|
447 | rcStrict = iemMemMap(pVCpu, (void **)&pa32Mem, 32, X86_SREG_SS, GCPtrStart, IEM_ACCESS_STACK_R, sizeof(*pa32Mem) - 1);
|
---|
448 | if (rcStrict == VINF_SUCCESS)
|
---|
449 | {
|
---|
450 | pVCpu->cpum.GstCtx.rdi = pa32Mem[7 - X86_GREG_xDI];
|
---|
451 | pVCpu->cpum.GstCtx.rsi = pa32Mem[7 - X86_GREG_xSI];
|
---|
452 | pVCpu->cpum.GstCtx.rbp = pa32Mem[7 - X86_GREG_xBP];
|
---|
453 | /* skip esp */
|
---|
454 | pVCpu->cpum.GstCtx.rbx = pa32Mem[7 - X86_GREG_xBX];
|
---|
455 | pVCpu->cpum.GstCtx.rdx = pa32Mem[7 - X86_GREG_xDX];
|
---|
456 | pVCpu->cpum.GstCtx.rcx = pa32Mem[7 - X86_GREG_xCX];
|
---|
457 | pVCpu->cpum.GstCtx.rax = pa32Mem[7 - X86_GREG_xAX];
|
---|
458 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)pa32Mem, IEM_ACCESS_STACK_R);
|
---|
459 | if (rcStrict == VINF_SUCCESS)
|
---|
460 | {
|
---|
461 | iemRegAddToRsp(pVCpu, 32);
|
---|
462 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
463 | }
|
---|
464 | }
|
---|
465 | }
|
---|
466 | return rcStrict;
|
---|
467 | }
|
---|
468 |
|
---|
469 |
|
---|
470 | /**
|
---|
471 | * Implements a 16-bit pusha.
|
---|
472 | */
|
---|
473 | IEM_CIMPL_DEF_0(iemCImpl_pusha_16)
|
---|
474 | {
|
---|
475 | RTGCPTR GCPtrTop = iemRegGetEffRsp(pVCpu);
|
---|
476 | RTGCPTR GCPtrBottom = GCPtrTop - 15;
|
---|
477 | VBOXSTRICTRC rcStrict;
|
---|
478 |
|
---|
479 | /*
|
---|
480 | * The docs are a bit hard to comprehend here, but it looks like we wrap
|
---|
481 | * around in real mode as long as none of the individual "pushd" crosses the
|
---|
482 | * end of the stack segment. In protected mode we check the whole access
|
---|
483 | * in one go. For efficiency, only do the word-by-word thing if we're in
|
---|
484 | * danger of wrapping around.
|
---|
485 | */
|
---|
486 | /** @todo do pusha boundary / wrap-around checks. */
|
---|
487 | if (RT_UNLIKELY( GCPtrBottom > GCPtrTop
|
---|
488 | && IEM_IS_REAL_OR_V86_MODE(pVCpu) ) )
|
---|
489 | {
|
---|
490 | /* word-by-word */
|
---|
491 | RTUINT64U TmpRsp;
|
---|
492 | TmpRsp.u = pVCpu->cpum.GstCtx.rsp;
|
---|
493 | rcStrict = iemMemStackPushU16Ex(pVCpu, pVCpu->cpum.GstCtx.ax, &TmpRsp);
|
---|
494 | if (rcStrict == VINF_SUCCESS)
|
---|
495 | rcStrict = iemMemStackPushU16Ex(pVCpu, pVCpu->cpum.GstCtx.cx, &TmpRsp);
|
---|
496 | if (rcStrict == VINF_SUCCESS)
|
---|
497 | rcStrict = iemMemStackPushU16Ex(pVCpu, pVCpu->cpum.GstCtx.dx, &TmpRsp);
|
---|
498 | if (rcStrict == VINF_SUCCESS)
|
---|
499 | rcStrict = iemMemStackPushU16Ex(pVCpu, pVCpu->cpum.GstCtx.bx, &TmpRsp);
|
---|
500 | if (rcStrict == VINF_SUCCESS)
|
---|
501 | rcStrict = iemMemStackPushU16Ex(pVCpu, pVCpu->cpum.GstCtx.sp, &TmpRsp);
|
---|
502 | if (rcStrict == VINF_SUCCESS)
|
---|
503 | rcStrict = iemMemStackPushU16Ex(pVCpu, pVCpu->cpum.GstCtx.bp, &TmpRsp);
|
---|
504 | if (rcStrict == VINF_SUCCESS)
|
---|
505 | rcStrict = iemMemStackPushU16Ex(pVCpu, pVCpu->cpum.GstCtx.si, &TmpRsp);
|
---|
506 | if (rcStrict == VINF_SUCCESS)
|
---|
507 | rcStrict = iemMemStackPushU16Ex(pVCpu, pVCpu->cpum.GstCtx.di, &TmpRsp);
|
---|
508 | if (rcStrict == VINF_SUCCESS)
|
---|
509 | {
|
---|
510 | pVCpu->cpum.GstCtx.rsp = TmpRsp.u;
|
---|
511 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
512 | }
|
---|
513 | }
|
---|
514 | else
|
---|
515 | {
|
---|
516 | GCPtrBottom--;
|
---|
517 | uint16_t *pa16Mem = NULL;
|
---|
518 | rcStrict = iemMemMap(pVCpu, (void **)&pa16Mem, 16, X86_SREG_SS, GCPtrBottom, IEM_ACCESS_STACK_W, sizeof(*pa16Mem) - 1);
|
---|
519 | if (rcStrict == VINF_SUCCESS)
|
---|
520 | {
|
---|
521 | pa16Mem[7 - X86_GREG_xDI] = pVCpu->cpum.GstCtx.di;
|
---|
522 | pa16Mem[7 - X86_GREG_xSI] = pVCpu->cpum.GstCtx.si;
|
---|
523 | pa16Mem[7 - X86_GREG_xBP] = pVCpu->cpum.GstCtx.bp;
|
---|
524 | pa16Mem[7 - X86_GREG_xSP] = pVCpu->cpum.GstCtx.sp;
|
---|
525 | pa16Mem[7 - X86_GREG_xBX] = pVCpu->cpum.GstCtx.bx;
|
---|
526 | pa16Mem[7 - X86_GREG_xDX] = pVCpu->cpum.GstCtx.dx;
|
---|
527 | pa16Mem[7 - X86_GREG_xCX] = pVCpu->cpum.GstCtx.cx;
|
---|
528 | pa16Mem[7 - X86_GREG_xAX] = pVCpu->cpum.GstCtx.ax;
|
---|
529 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)pa16Mem, IEM_ACCESS_STACK_W);
|
---|
530 | if (rcStrict == VINF_SUCCESS)
|
---|
531 | {
|
---|
532 | iemRegSubFromRsp(pVCpu, 16);
|
---|
533 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
534 | }
|
---|
535 | }
|
---|
536 | }
|
---|
537 | return rcStrict;
|
---|
538 | }
|
---|
539 |
|
---|
540 |
|
---|
541 | /**
|
---|
542 | * Implements a 32-bit pusha.
|
---|
543 | */
|
---|
544 | IEM_CIMPL_DEF_0(iemCImpl_pusha_32)
|
---|
545 | {
|
---|
546 | RTGCPTR GCPtrTop = iemRegGetEffRsp(pVCpu);
|
---|
547 | RTGCPTR GCPtrBottom = GCPtrTop - 31;
|
---|
548 | VBOXSTRICTRC rcStrict;
|
---|
549 |
|
---|
550 | /*
|
---|
551 | * The docs are a bit hard to comprehend here, but it looks like we wrap
|
---|
552 | * around in real mode as long as none of the individual "pusha" crosses the
|
---|
553 | * end of the stack segment. In protected mode we check the whole access
|
---|
554 | * in one go. For efficiency, only do the word-by-word thing if we're in
|
---|
555 | * danger of wrapping around.
|
---|
556 | */
|
---|
557 | /** @todo do pusha boundary / wrap-around checks. */
|
---|
558 | if (RT_UNLIKELY( GCPtrBottom > GCPtrTop
|
---|
559 | && IEM_IS_REAL_OR_V86_MODE(pVCpu) ) )
|
---|
560 | {
|
---|
561 | /* word-by-word */
|
---|
562 | RTUINT64U TmpRsp;
|
---|
563 | TmpRsp.u = pVCpu->cpum.GstCtx.rsp;
|
---|
564 | rcStrict = iemMemStackPushU32Ex(pVCpu, pVCpu->cpum.GstCtx.eax, &TmpRsp);
|
---|
565 | if (rcStrict == VINF_SUCCESS)
|
---|
566 | rcStrict = iemMemStackPushU32Ex(pVCpu, pVCpu->cpum.GstCtx.ecx, &TmpRsp);
|
---|
567 | if (rcStrict == VINF_SUCCESS)
|
---|
568 | rcStrict = iemMemStackPushU32Ex(pVCpu, pVCpu->cpum.GstCtx.edx, &TmpRsp);
|
---|
569 | if (rcStrict == VINF_SUCCESS)
|
---|
570 | rcStrict = iemMemStackPushU32Ex(pVCpu, pVCpu->cpum.GstCtx.ebx, &TmpRsp);
|
---|
571 | if (rcStrict == VINF_SUCCESS)
|
---|
572 | rcStrict = iemMemStackPushU32Ex(pVCpu, pVCpu->cpum.GstCtx.esp, &TmpRsp);
|
---|
573 | if (rcStrict == VINF_SUCCESS)
|
---|
574 | rcStrict = iemMemStackPushU32Ex(pVCpu, pVCpu->cpum.GstCtx.ebp, &TmpRsp);
|
---|
575 | if (rcStrict == VINF_SUCCESS)
|
---|
576 | rcStrict = iemMemStackPushU32Ex(pVCpu, pVCpu->cpum.GstCtx.esi, &TmpRsp);
|
---|
577 | if (rcStrict == VINF_SUCCESS)
|
---|
578 | rcStrict = iemMemStackPushU32Ex(pVCpu, pVCpu->cpum.GstCtx.edi, &TmpRsp);
|
---|
579 | if (rcStrict == VINF_SUCCESS)
|
---|
580 | {
|
---|
581 | pVCpu->cpum.GstCtx.rsp = TmpRsp.u;
|
---|
582 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
583 | }
|
---|
584 | }
|
---|
585 | else
|
---|
586 | {
|
---|
587 | GCPtrBottom--;
|
---|
588 | uint32_t *pa32Mem;
|
---|
589 | rcStrict = iemMemMap(pVCpu, (void **)&pa32Mem, 32, X86_SREG_SS, GCPtrBottom, IEM_ACCESS_STACK_W, sizeof(*pa32Mem) - 1);
|
---|
590 | if (rcStrict == VINF_SUCCESS)
|
---|
591 | {
|
---|
592 | pa32Mem[7 - X86_GREG_xDI] = pVCpu->cpum.GstCtx.edi;
|
---|
593 | pa32Mem[7 - X86_GREG_xSI] = pVCpu->cpum.GstCtx.esi;
|
---|
594 | pa32Mem[7 - X86_GREG_xBP] = pVCpu->cpum.GstCtx.ebp;
|
---|
595 | pa32Mem[7 - X86_GREG_xSP] = pVCpu->cpum.GstCtx.esp;
|
---|
596 | pa32Mem[7 - X86_GREG_xBX] = pVCpu->cpum.GstCtx.ebx;
|
---|
597 | pa32Mem[7 - X86_GREG_xDX] = pVCpu->cpum.GstCtx.edx;
|
---|
598 | pa32Mem[7 - X86_GREG_xCX] = pVCpu->cpum.GstCtx.ecx;
|
---|
599 | pa32Mem[7 - X86_GREG_xAX] = pVCpu->cpum.GstCtx.eax;
|
---|
600 | rcStrict = iemMemCommitAndUnmap(pVCpu, pa32Mem, IEM_ACCESS_STACK_W);
|
---|
601 | if (rcStrict == VINF_SUCCESS)
|
---|
602 | {
|
---|
603 | iemRegSubFromRsp(pVCpu, 32);
|
---|
604 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
605 | }
|
---|
606 | }
|
---|
607 | }
|
---|
608 | return rcStrict;
|
---|
609 | }
|
---|
610 |
|
---|
611 |
|
---|
612 | /**
|
---|
613 | * Implements pushf.
|
---|
614 | *
|
---|
615 | *
|
---|
616 | * @param enmEffOpSize The effective operand size.
|
---|
617 | */
|
---|
618 | IEM_CIMPL_DEF_1(iemCImpl_pushf, IEMMODE, enmEffOpSize)
|
---|
619 | {
|
---|
620 | VBOXSTRICTRC rcStrict;
|
---|
621 |
|
---|
622 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_PUSHF))
|
---|
623 | {
|
---|
624 | Log2(("pushf: Guest intercept -> #VMEXIT\n"));
|
---|
625 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
626 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_PUSHF, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
627 | }
|
---|
628 |
|
---|
629 | /*
|
---|
630 | * If we're in V8086 mode some care is required (which is why we're in
|
---|
631 | * doing this in a C implementation).
|
---|
632 | */
|
---|
633 | uint32_t fEfl = IEMMISC_GET_EFL(pVCpu);
|
---|
634 | if ( (fEfl & X86_EFL_VM)
|
---|
635 | && X86_EFL_GET_IOPL(fEfl) != 3 )
|
---|
636 | {
|
---|
637 | Assert(pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE);
|
---|
638 | if ( enmEffOpSize != IEMMODE_16BIT
|
---|
639 | || !(pVCpu->cpum.GstCtx.cr4 & X86_CR4_VME))
|
---|
640 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
641 | fEfl &= ~X86_EFL_IF; /* (RF and VM are out of range) */
|
---|
642 | fEfl |= (fEfl & X86_EFL_VIF) >> (19 - 9);
|
---|
643 | rcStrict = iemMemStackPushU16(pVCpu, (uint16_t)fEfl);
|
---|
644 | }
|
---|
645 | else
|
---|
646 | {
|
---|
647 |
|
---|
648 | /*
|
---|
649 | * Ok, clear RF and VM, adjust for ancient CPUs, and push the flags.
|
---|
650 | */
|
---|
651 | fEfl &= ~(X86_EFL_RF | X86_EFL_VM);
|
---|
652 |
|
---|
653 | switch (enmEffOpSize)
|
---|
654 | {
|
---|
655 | case IEMMODE_16BIT:
|
---|
656 | AssertCompile(IEMTARGETCPU_8086 <= IEMTARGETCPU_186 && IEMTARGETCPU_V20 <= IEMTARGETCPU_186 && IEMTARGETCPU_286 > IEMTARGETCPU_186);
|
---|
657 | if (IEM_GET_TARGET_CPU(pVCpu) <= IEMTARGETCPU_186)
|
---|
658 | fEfl |= UINT16_C(0xf000);
|
---|
659 | rcStrict = iemMemStackPushU16(pVCpu, (uint16_t)fEfl);
|
---|
660 | break;
|
---|
661 | case IEMMODE_32BIT:
|
---|
662 | rcStrict = iemMemStackPushU32(pVCpu, fEfl);
|
---|
663 | break;
|
---|
664 | case IEMMODE_64BIT:
|
---|
665 | rcStrict = iemMemStackPushU64(pVCpu, fEfl);
|
---|
666 | break;
|
---|
667 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
668 | }
|
---|
669 | }
|
---|
670 |
|
---|
671 | if (rcStrict == VINF_SUCCESS)
|
---|
672 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
673 | return rcStrict;
|
---|
674 | }
|
---|
675 |
|
---|
676 |
|
---|
677 | /**
|
---|
678 | * Implements popf.
|
---|
679 | *
|
---|
680 | * @param enmEffOpSize The effective operand size.
|
---|
681 | */
|
---|
682 | IEM_CIMPL_DEF_1(iemCImpl_popf, IEMMODE, enmEffOpSize)
|
---|
683 | {
|
---|
684 | uint32_t const fEflOld = IEMMISC_GET_EFL(pVCpu);
|
---|
685 | VBOXSTRICTRC rcStrict;
|
---|
686 | uint32_t fEflNew;
|
---|
687 |
|
---|
688 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_POPF))
|
---|
689 | {
|
---|
690 | Log2(("popf: Guest intercept -> #VMEXIT\n"));
|
---|
691 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
692 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_POPF, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
693 | }
|
---|
694 |
|
---|
695 | /*
|
---|
696 | * V8086 is special as usual.
|
---|
697 | */
|
---|
698 | if (fEflOld & X86_EFL_VM)
|
---|
699 | {
|
---|
700 | /*
|
---|
701 | * Almost anything goes if IOPL is 3.
|
---|
702 | */
|
---|
703 | if (X86_EFL_GET_IOPL(fEflOld) == 3)
|
---|
704 | {
|
---|
705 | switch (enmEffOpSize)
|
---|
706 | {
|
---|
707 | case IEMMODE_16BIT:
|
---|
708 | {
|
---|
709 | uint16_t u16Value;
|
---|
710 | rcStrict = iemMemStackPopU16(pVCpu, &u16Value);
|
---|
711 | if (rcStrict != VINF_SUCCESS)
|
---|
712 | return rcStrict;
|
---|
713 | fEflNew = u16Value | (fEflOld & UINT32_C(0xffff0000));
|
---|
714 | break;
|
---|
715 | }
|
---|
716 | case IEMMODE_32BIT:
|
---|
717 | rcStrict = iemMemStackPopU32(pVCpu, &fEflNew);
|
---|
718 | if (rcStrict != VINF_SUCCESS)
|
---|
719 | return rcStrict;
|
---|
720 | break;
|
---|
721 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
722 | }
|
---|
723 |
|
---|
724 | const uint32_t fPopfBits = pVCpu->CTX_SUFF(pVM)->cpum.ro.GuestFeatures.enmMicroarch != kCpumMicroarch_Intel_80386
|
---|
725 | ? X86_EFL_POPF_BITS : X86_EFL_POPF_BITS_386;
|
---|
726 | fEflNew &= fPopfBits & ~(X86_EFL_IOPL);
|
---|
727 | fEflNew |= ~(fPopfBits & ~(X86_EFL_IOPL)) & fEflOld;
|
---|
728 | }
|
---|
729 | /*
|
---|
730 | * Interrupt flag virtualization with CR4.VME=1.
|
---|
731 | */
|
---|
732 | else if ( enmEffOpSize == IEMMODE_16BIT
|
---|
733 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_VME) )
|
---|
734 | {
|
---|
735 | uint16_t u16Value;
|
---|
736 | RTUINT64U TmpRsp;
|
---|
737 | TmpRsp.u = pVCpu->cpum.GstCtx.rsp;
|
---|
738 | rcStrict = iemMemStackPopU16Ex(pVCpu, &u16Value, &TmpRsp);
|
---|
739 | if (rcStrict != VINF_SUCCESS)
|
---|
740 | return rcStrict;
|
---|
741 |
|
---|
742 | /** @todo Is the popf VME \#GP(0) delivered after updating RSP+RIP
|
---|
743 | * or before? */
|
---|
744 | if ( ( (u16Value & X86_EFL_IF)
|
---|
745 | && (fEflOld & X86_EFL_VIP))
|
---|
746 | || (u16Value & X86_EFL_TF) )
|
---|
747 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
748 |
|
---|
749 | fEflNew = u16Value | (fEflOld & UINT32_C(0xffff0000) & ~X86_EFL_VIF);
|
---|
750 | fEflNew |= (fEflNew & X86_EFL_IF) << (19 - 9);
|
---|
751 | fEflNew &= X86_EFL_POPF_BITS & ~(X86_EFL_IOPL | X86_EFL_IF);
|
---|
752 | fEflNew |= ~(X86_EFL_POPF_BITS & ~(X86_EFL_IOPL | X86_EFL_IF)) & fEflOld;
|
---|
753 |
|
---|
754 | pVCpu->cpum.GstCtx.rsp = TmpRsp.u;
|
---|
755 | }
|
---|
756 | else
|
---|
757 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
758 |
|
---|
759 | }
|
---|
760 | /*
|
---|
761 | * Not in V8086 mode.
|
---|
762 | */
|
---|
763 | else
|
---|
764 | {
|
---|
765 | /* Pop the flags. */
|
---|
766 | switch (enmEffOpSize)
|
---|
767 | {
|
---|
768 | case IEMMODE_16BIT:
|
---|
769 | {
|
---|
770 | uint16_t u16Value;
|
---|
771 | rcStrict = iemMemStackPopU16(pVCpu, &u16Value);
|
---|
772 | if (rcStrict != VINF_SUCCESS)
|
---|
773 | return rcStrict;
|
---|
774 | fEflNew = u16Value | (fEflOld & UINT32_C(0xffff0000));
|
---|
775 |
|
---|
776 | /*
|
---|
777 | * Ancient CPU adjustments:
|
---|
778 | * - 8086, 80186, V20/30:
|
---|
779 | * Fixed bits 15:12 bits are not kept correctly internally, mostly for
|
---|
780 | * practical reasons (masking below). We add them when pushing flags.
|
---|
781 | * - 80286:
|
---|
782 | * The NT and IOPL flags cannot be popped from real mode and are
|
---|
783 | * therefore always zero (since a 286 can never exit from PM and
|
---|
784 | * their initial value is zero). This changed on a 386 and can
|
---|
785 | * therefore be used to detect 286 or 386 CPU in real mode.
|
---|
786 | */
|
---|
787 | if ( IEM_GET_TARGET_CPU(pVCpu) == IEMTARGETCPU_286
|
---|
788 | && !(pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE) )
|
---|
789 | fEflNew &= ~(X86_EFL_NT | X86_EFL_IOPL);
|
---|
790 | break;
|
---|
791 | }
|
---|
792 | case IEMMODE_32BIT:
|
---|
793 | rcStrict = iemMemStackPopU32(pVCpu, &fEflNew);
|
---|
794 | if (rcStrict != VINF_SUCCESS)
|
---|
795 | return rcStrict;
|
---|
796 | break;
|
---|
797 | case IEMMODE_64BIT:
|
---|
798 | {
|
---|
799 | uint64_t u64Value;
|
---|
800 | rcStrict = iemMemStackPopU64(pVCpu, &u64Value);
|
---|
801 | if (rcStrict != VINF_SUCCESS)
|
---|
802 | return rcStrict;
|
---|
803 | fEflNew = u64Value; /** @todo testcase: Check exactly what happens if high bits are set. */
|
---|
804 | break;
|
---|
805 | }
|
---|
806 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
807 | }
|
---|
808 |
|
---|
809 | /* Merge them with the current flags. */
|
---|
810 | const uint32_t fPopfBits = pVCpu->CTX_SUFF(pVM)->cpum.ro.GuestFeatures.enmMicroarch != kCpumMicroarch_Intel_80386
|
---|
811 | ? X86_EFL_POPF_BITS : X86_EFL_POPF_BITS_386;
|
---|
812 | if ( (fEflNew & (X86_EFL_IOPL | X86_EFL_IF)) == (fEflOld & (X86_EFL_IOPL | X86_EFL_IF))
|
---|
813 | || pVCpu->iem.s.uCpl == 0)
|
---|
814 | {
|
---|
815 | fEflNew &= fPopfBits;
|
---|
816 | fEflNew |= ~fPopfBits & fEflOld;
|
---|
817 | }
|
---|
818 | else if (pVCpu->iem.s.uCpl <= X86_EFL_GET_IOPL(fEflOld))
|
---|
819 | {
|
---|
820 | fEflNew &= fPopfBits & ~(X86_EFL_IOPL);
|
---|
821 | fEflNew |= ~(fPopfBits & ~(X86_EFL_IOPL)) & fEflOld;
|
---|
822 | }
|
---|
823 | else
|
---|
824 | {
|
---|
825 | fEflNew &= fPopfBits & ~(X86_EFL_IOPL | X86_EFL_IF);
|
---|
826 | fEflNew |= ~(fPopfBits & ~(X86_EFL_IOPL | X86_EFL_IF)) & fEflOld;
|
---|
827 | }
|
---|
828 | }
|
---|
829 |
|
---|
830 | /*
|
---|
831 | * Commit the flags.
|
---|
832 | */
|
---|
833 | Assert(fEflNew & RT_BIT_32(1));
|
---|
834 | IEMMISC_SET_EFL(pVCpu, fEflNew);
|
---|
835 | return iemRegAddToRipAndFinishingClearingRfEx(pVCpu, cbInstr, fEflOld);
|
---|
836 | }
|
---|
837 |
|
---|
838 |
|
---|
839 | /**
|
---|
840 | * Implements an indirect call.
|
---|
841 | *
|
---|
842 | * @param uNewPC The new program counter (RIP) value (loaded from the
|
---|
843 | * operand).
|
---|
844 | */
|
---|
845 | IEM_CIMPL_DEF_1(iemCImpl_call_16, uint16_t, uNewPC)
|
---|
846 | {
|
---|
847 | uint16_t const uOldPC = pVCpu->cpum.GstCtx.ip + cbInstr;
|
---|
848 | if (uNewPC <= pVCpu->cpum.GstCtx.cs.u32Limit)
|
---|
849 | {
|
---|
850 | VBOXSTRICTRC rcStrict = iemMemStackPushU16(pVCpu, uOldPC);
|
---|
851 | if (rcStrict == VINF_SUCCESS)
|
---|
852 | {
|
---|
853 | pVCpu->cpum.GstCtx.rip = uNewPC;
|
---|
854 | IEM_FLUSH_PREFETCH_LIGHT(pVCpu, cbInstr);
|
---|
855 | return iemRegFinishClearingRF(pVCpu);
|
---|
856 | }
|
---|
857 | return rcStrict;
|
---|
858 | }
|
---|
859 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
860 | }
|
---|
861 |
|
---|
862 |
|
---|
863 | /**
|
---|
864 | * Implements a 16-bit relative call.
|
---|
865 | *
|
---|
866 | * @param offDisp The displacment offset.
|
---|
867 | */
|
---|
868 | IEM_CIMPL_DEF_1(iemCImpl_call_rel_16, int16_t, offDisp)
|
---|
869 | {
|
---|
870 | uint16_t const uOldPC = pVCpu->cpum.GstCtx.ip + cbInstr;
|
---|
871 | uint16_t const uNewPC = uOldPC + offDisp;
|
---|
872 | if (uNewPC <= pVCpu->cpum.GstCtx.cs.u32Limit)
|
---|
873 | {
|
---|
874 | VBOXSTRICTRC rcStrict = iemMemStackPushU16(pVCpu, uOldPC);
|
---|
875 | if (rcStrict == VINF_SUCCESS)
|
---|
876 | {
|
---|
877 | pVCpu->cpum.GstCtx.rip = uNewPC;
|
---|
878 | IEM_FLUSH_PREFETCH_LIGHT(pVCpu, cbInstr);
|
---|
879 | return iemRegFinishClearingRF(pVCpu);
|
---|
880 | }
|
---|
881 | return rcStrict;
|
---|
882 | }
|
---|
883 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
884 | }
|
---|
885 |
|
---|
886 |
|
---|
887 | /**
|
---|
888 | * Implements a 32-bit indirect call.
|
---|
889 | *
|
---|
890 | * @param uNewPC The new program counter (RIP) value (loaded from the
|
---|
891 | * operand).
|
---|
892 | */
|
---|
893 | IEM_CIMPL_DEF_1(iemCImpl_call_32, uint32_t, uNewPC)
|
---|
894 | {
|
---|
895 | uint32_t const uOldPC = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
896 | if (uNewPC <= pVCpu->cpum.GstCtx.cs.u32Limit)
|
---|
897 | {
|
---|
898 | VBOXSTRICTRC rcStrict = iemMemStackPushU32(pVCpu, uOldPC);
|
---|
899 | if (rcStrict == VINF_SUCCESS)
|
---|
900 | {
|
---|
901 | pVCpu->cpum.GstCtx.rip = uNewPC;
|
---|
902 | IEM_FLUSH_PREFETCH_LIGHT(pVCpu, cbInstr);
|
---|
903 | return iemRegFinishClearingRF(pVCpu);
|
---|
904 | }
|
---|
905 | return rcStrict;
|
---|
906 | }
|
---|
907 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
908 | }
|
---|
909 |
|
---|
910 |
|
---|
911 | /**
|
---|
912 | * Implements a 32-bit relative call.
|
---|
913 | *
|
---|
914 | * @param offDisp The displacment offset.
|
---|
915 | */
|
---|
916 | IEM_CIMPL_DEF_1(iemCImpl_call_rel_32, int32_t, offDisp)
|
---|
917 | {
|
---|
918 | uint32_t const uOldPC = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
919 | uint32_t const uNewPC = uOldPC + offDisp;
|
---|
920 | if (uNewPC <= pVCpu->cpum.GstCtx.cs.u32Limit)
|
---|
921 | {
|
---|
922 | VBOXSTRICTRC rcStrict = iemMemStackPushU32(pVCpu, uOldPC);
|
---|
923 | if (rcStrict == VINF_SUCCESS)
|
---|
924 | {
|
---|
925 | pVCpu->cpum.GstCtx.rip = uNewPC;
|
---|
926 | IEM_FLUSH_PREFETCH_LIGHT(pVCpu, cbInstr);
|
---|
927 | return iemRegFinishClearingRF(pVCpu);
|
---|
928 | }
|
---|
929 | return rcStrict;
|
---|
930 | }
|
---|
931 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
932 | }
|
---|
933 |
|
---|
934 |
|
---|
935 | /**
|
---|
936 | * Implements a 64-bit indirect call.
|
---|
937 | *
|
---|
938 | * @param uNewPC The new program counter (RIP) value (loaded from the
|
---|
939 | * operand).
|
---|
940 | */
|
---|
941 | IEM_CIMPL_DEF_1(iemCImpl_call_64, uint64_t, uNewPC)
|
---|
942 | {
|
---|
943 | uint64_t const uOldPC = pVCpu->cpum.GstCtx.rip + cbInstr;
|
---|
944 | if (IEM_IS_CANONICAL(uNewPC))
|
---|
945 | {
|
---|
946 | VBOXSTRICTRC rcStrict = iemMemStackPushU64(pVCpu, uOldPC);
|
---|
947 | if (rcStrict == VINF_SUCCESS)
|
---|
948 | {
|
---|
949 | pVCpu->cpum.GstCtx.rip = uNewPC;
|
---|
950 | IEM_FLUSH_PREFETCH_LIGHT(pVCpu, cbInstr);
|
---|
951 | return iemRegFinishClearingRF(pVCpu);
|
---|
952 | }
|
---|
953 | return rcStrict;
|
---|
954 | }
|
---|
955 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
956 | }
|
---|
957 |
|
---|
958 |
|
---|
959 | /**
|
---|
960 | * Implements a 64-bit relative call.
|
---|
961 | *
|
---|
962 | * @param offDisp The displacment offset.
|
---|
963 | */
|
---|
964 | IEM_CIMPL_DEF_1(iemCImpl_call_rel_64, int64_t, offDisp)
|
---|
965 | {
|
---|
966 | uint64_t const uOldPC = pVCpu->cpum.GstCtx.rip + cbInstr;
|
---|
967 | uint64_t const uNewPC = uOldPC + offDisp;
|
---|
968 | if (IEM_IS_CANONICAL(uNewPC))
|
---|
969 | {
|
---|
970 | VBOXSTRICTRC rcStrict = iemMemStackPushU64(pVCpu, uOldPC);
|
---|
971 | if (rcStrict == VINF_SUCCESS)
|
---|
972 | {
|
---|
973 | pVCpu->cpum.GstCtx.rip = uNewPC;
|
---|
974 | IEM_FLUSH_PREFETCH_LIGHT(pVCpu, cbInstr);
|
---|
975 | return iemRegFinishClearingRF(pVCpu);
|
---|
976 | }
|
---|
977 | return rcStrict;
|
---|
978 | }
|
---|
979 | return iemRaiseNotCanonical(pVCpu);
|
---|
980 | }
|
---|
981 |
|
---|
982 |
|
---|
983 | /**
|
---|
984 | * Implements far jumps and calls thru task segments (TSS).
|
---|
985 | *
|
---|
986 | * @returns VBox strict status code.
|
---|
987 | * @param pVCpu The cross context virtual CPU structure of the
|
---|
988 | * calling thread.
|
---|
989 | * @param cbInstr The current instruction length.
|
---|
990 | * @param uSel The selector.
|
---|
991 | * @param enmBranch The kind of branching we're performing.
|
---|
992 | * @param enmEffOpSize The effective operand size.
|
---|
993 | * @param pDesc The descriptor corresponding to @a uSel. The type is
|
---|
994 | * task gate.
|
---|
995 | */
|
---|
996 | static VBOXSTRICTRC iemCImpl_BranchTaskSegment(PVMCPUCC pVCpu, uint8_t cbInstr, uint16_t uSel, IEMBRANCH enmBranch,
|
---|
997 | IEMMODE enmEffOpSize, PIEMSELDESC pDesc)
|
---|
998 | {
|
---|
999 | #ifndef IEM_IMPLEMENTS_TASKSWITCH
|
---|
1000 | IEM_RETURN_ASPECT_NOT_IMPLEMENTED();
|
---|
1001 | #else
|
---|
1002 | Assert(enmBranch == IEMBRANCH_JUMP || enmBranch == IEMBRANCH_CALL);
|
---|
1003 | Assert( pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_286_TSS_AVAIL
|
---|
1004 | || pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_386_TSS_AVAIL);
|
---|
1005 | RT_NOREF_PV(enmEffOpSize);
|
---|
1006 | IEM_CTX_ASSERT(pVCpu, IEM_CPUMCTX_EXTRN_XCPT_MASK);
|
---|
1007 |
|
---|
1008 | if ( pDesc->Legacy.Gate.u2Dpl < pVCpu->iem.s.uCpl
|
---|
1009 | || pDesc->Legacy.Gate.u2Dpl < (uSel & X86_SEL_RPL))
|
---|
1010 | {
|
---|
1011 | Log(("BranchTaskSegment invalid priv. uSel=%04x TSS DPL=%d CPL=%u Sel RPL=%u -> #GP\n", uSel, pDesc->Legacy.Gate.u2Dpl,
|
---|
1012 | pVCpu->iem.s.uCpl, (uSel & X86_SEL_RPL)));
|
---|
1013 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel & X86_SEL_MASK_OFF_RPL);
|
---|
1014 | }
|
---|
1015 |
|
---|
1016 | /** @todo This is checked earlier for far jumps (see iemCImpl_FarJmp) but not
|
---|
1017 | * far calls (see iemCImpl_callf). Most likely in both cases it should be
|
---|
1018 | * checked here, need testcases. */
|
---|
1019 | if (!pDesc->Legacy.Gen.u1Present)
|
---|
1020 | {
|
---|
1021 | Log(("BranchTaskSegment TSS not present uSel=%04x -> #NP\n", uSel));
|
---|
1022 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uSel & X86_SEL_MASK_OFF_RPL);
|
---|
1023 | }
|
---|
1024 |
|
---|
1025 | uint32_t uNextEip = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
1026 | return iemTaskSwitch(pVCpu, enmBranch == IEMBRANCH_JUMP ? IEMTASKSWITCH_JUMP : IEMTASKSWITCH_CALL,
|
---|
1027 | uNextEip, 0 /* fFlags */, 0 /* uErr */, 0 /* uCr2 */, uSel, pDesc);
|
---|
1028 | #endif
|
---|
1029 | }
|
---|
1030 |
|
---|
1031 |
|
---|
1032 | /**
|
---|
1033 | * Implements far jumps and calls thru task gates.
|
---|
1034 | *
|
---|
1035 | * @returns VBox strict status code.
|
---|
1036 | * @param pVCpu The cross context virtual CPU structure of the
|
---|
1037 | * calling thread.
|
---|
1038 | * @param cbInstr The current instruction length.
|
---|
1039 | * @param uSel The selector.
|
---|
1040 | * @param enmBranch The kind of branching we're performing.
|
---|
1041 | * @param enmEffOpSize The effective operand size.
|
---|
1042 | * @param pDesc The descriptor corresponding to @a uSel. The type is
|
---|
1043 | * task gate.
|
---|
1044 | */
|
---|
1045 | static VBOXSTRICTRC iemCImpl_BranchTaskGate(PVMCPUCC pVCpu, uint8_t cbInstr, uint16_t uSel, IEMBRANCH enmBranch,
|
---|
1046 | IEMMODE enmEffOpSize, PIEMSELDESC pDesc)
|
---|
1047 | {
|
---|
1048 | #ifndef IEM_IMPLEMENTS_TASKSWITCH
|
---|
1049 | IEM_RETURN_ASPECT_NOT_IMPLEMENTED();
|
---|
1050 | #else
|
---|
1051 | Assert(enmBranch == IEMBRANCH_JUMP || enmBranch == IEMBRANCH_CALL);
|
---|
1052 | RT_NOREF_PV(enmEffOpSize);
|
---|
1053 | IEM_CTX_ASSERT(pVCpu, IEM_CPUMCTX_EXTRN_XCPT_MASK);
|
---|
1054 |
|
---|
1055 | if ( pDesc->Legacy.Gate.u2Dpl < pVCpu->iem.s.uCpl
|
---|
1056 | || pDesc->Legacy.Gate.u2Dpl < (uSel & X86_SEL_RPL))
|
---|
1057 | {
|
---|
1058 | Log(("BranchTaskGate invalid priv. uSel=%04x TSS DPL=%d CPL=%u Sel RPL=%u -> #GP\n", uSel, pDesc->Legacy.Gate.u2Dpl,
|
---|
1059 | pVCpu->iem.s.uCpl, (uSel & X86_SEL_RPL)));
|
---|
1060 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel & X86_SEL_MASK_OFF_RPL);
|
---|
1061 | }
|
---|
1062 |
|
---|
1063 | /** @todo This is checked earlier for far jumps (see iemCImpl_FarJmp) but not
|
---|
1064 | * far calls (see iemCImpl_callf). Most likely in both cases it should be
|
---|
1065 | * checked here, need testcases. */
|
---|
1066 | if (!pDesc->Legacy.Gen.u1Present)
|
---|
1067 | {
|
---|
1068 | Log(("BranchTaskSegment segment not present uSel=%04x -> #NP\n", uSel));
|
---|
1069 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uSel & X86_SEL_MASK_OFF_RPL);
|
---|
1070 | }
|
---|
1071 |
|
---|
1072 | /*
|
---|
1073 | * Fetch the new TSS descriptor from the GDT.
|
---|
1074 | */
|
---|
1075 | RTSEL uSelTss = pDesc->Legacy.Gate.u16Sel;
|
---|
1076 | if (uSelTss & X86_SEL_LDT)
|
---|
1077 | {
|
---|
1078 | Log(("BranchTaskGate TSS is in LDT. uSel=%04x uSelTss=%04x -> #GP\n", uSel, uSelTss));
|
---|
1079 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel & X86_SEL_MASK_OFF_RPL);
|
---|
1080 | }
|
---|
1081 |
|
---|
1082 | IEMSELDESC TssDesc;
|
---|
1083 | VBOXSTRICTRC rcStrict = iemMemFetchSelDesc(pVCpu, &TssDesc, uSelTss, X86_XCPT_GP);
|
---|
1084 | if (rcStrict != VINF_SUCCESS)
|
---|
1085 | return rcStrict;
|
---|
1086 |
|
---|
1087 | if (TssDesc.Legacy.Gate.u4Type & X86_SEL_TYPE_SYS_TSS_BUSY_MASK)
|
---|
1088 | {
|
---|
1089 | Log(("BranchTaskGate TSS is busy. uSel=%04x uSelTss=%04x DescType=%#x -> #GP\n", uSel, uSelTss,
|
---|
1090 | TssDesc.Legacy.Gate.u4Type));
|
---|
1091 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel & X86_SEL_MASK_OFF_RPL);
|
---|
1092 | }
|
---|
1093 |
|
---|
1094 | if (!TssDesc.Legacy.Gate.u1Present)
|
---|
1095 | {
|
---|
1096 | Log(("BranchTaskGate TSS is not present. uSel=%04x uSelTss=%04x -> #NP\n", uSel, uSelTss));
|
---|
1097 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uSelTss & X86_SEL_MASK_OFF_RPL);
|
---|
1098 | }
|
---|
1099 |
|
---|
1100 | uint32_t uNextEip = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
1101 | return iemTaskSwitch(pVCpu, enmBranch == IEMBRANCH_JUMP ? IEMTASKSWITCH_JUMP : IEMTASKSWITCH_CALL,
|
---|
1102 | uNextEip, 0 /* fFlags */, 0 /* uErr */, 0 /* uCr2 */, uSelTss, &TssDesc);
|
---|
1103 | #endif
|
---|
1104 | }
|
---|
1105 |
|
---|
1106 |
|
---|
1107 | /**
|
---|
1108 | * Implements far jumps and calls thru call gates.
|
---|
1109 | *
|
---|
1110 | * @returns VBox strict status code.
|
---|
1111 | * @param pVCpu The cross context virtual CPU structure of the
|
---|
1112 | * calling thread.
|
---|
1113 | * @param cbInstr The current instruction length.
|
---|
1114 | * @param uSel The selector.
|
---|
1115 | * @param enmBranch The kind of branching we're performing.
|
---|
1116 | * @param enmEffOpSize The effective operand size.
|
---|
1117 | * @param pDesc The descriptor corresponding to @a uSel. The type is
|
---|
1118 | * call gate.
|
---|
1119 | */
|
---|
1120 | static VBOXSTRICTRC iemCImpl_BranchCallGate(PVMCPUCC pVCpu, uint8_t cbInstr, uint16_t uSel, IEMBRANCH enmBranch,
|
---|
1121 | IEMMODE enmEffOpSize, PIEMSELDESC pDesc)
|
---|
1122 | {
|
---|
1123 | #define IEM_IMPLEMENTS_CALLGATE
|
---|
1124 | #ifndef IEM_IMPLEMENTS_CALLGATE
|
---|
1125 | IEM_RETURN_ASPECT_NOT_IMPLEMENTED();
|
---|
1126 | #else
|
---|
1127 | RT_NOREF_PV(enmEffOpSize);
|
---|
1128 | IEM_CTX_ASSERT(pVCpu, IEM_CPUMCTX_EXTRN_XCPT_MASK);
|
---|
1129 |
|
---|
1130 | /* NB: Far jumps can only do intra-privilege transfers. Far calls support
|
---|
1131 | * inter-privilege calls and are much more complex.
|
---|
1132 | *
|
---|
1133 | * NB: 64-bit call gate has the same type as a 32-bit call gate! If
|
---|
1134 | * EFER.LMA=1, the gate must be 64-bit. Conversely if EFER.LMA=0, the gate
|
---|
1135 | * must be 16-bit or 32-bit.
|
---|
1136 | */
|
---|
1137 | /** @todo effective operand size is probably irrelevant here, only the
|
---|
1138 | * call gate bitness matters??
|
---|
1139 | */
|
---|
1140 | VBOXSTRICTRC rcStrict;
|
---|
1141 | RTPTRUNION uPtrRet;
|
---|
1142 | uint64_t uNewRsp;
|
---|
1143 | uint64_t uNewRip;
|
---|
1144 | uint64_t u64Base;
|
---|
1145 | uint32_t cbLimit;
|
---|
1146 | RTSEL uNewCS;
|
---|
1147 | IEMSELDESC DescCS;
|
---|
1148 |
|
---|
1149 | AssertCompile(X86_SEL_TYPE_SYS_386_CALL_GATE == AMD64_SEL_TYPE_SYS_CALL_GATE);
|
---|
1150 | Assert(enmBranch == IEMBRANCH_JUMP || enmBranch == IEMBRANCH_CALL);
|
---|
1151 | Assert( pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_286_CALL_GATE
|
---|
1152 | || pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_386_CALL_GATE);
|
---|
1153 |
|
---|
1154 | /* Determine the new instruction pointer from the gate descriptor. */
|
---|
1155 | uNewRip = pDesc->Legacy.Gate.u16OffsetLow
|
---|
1156 | | ((uint32_t)pDesc->Legacy.Gate.u16OffsetHigh << 16)
|
---|
1157 | | ((uint64_t)pDesc->Long.Gate.u32OffsetTop << 32);
|
---|
1158 |
|
---|
1159 | /* Perform DPL checks on the gate descriptor. */
|
---|
1160 | if ( pDesc->Legacy.Gate.u2Dpl < pVCpu->iem.s.uCpl
|
---|
1161 | || pDesc->Legacy.Gate.u2Dpl < (uSel & X86_SEL_RPL))
|
---|
1162 | {
|
---|
1163 | Log(("BranchCallGate invalid priv. uSel=%04x Gate DPL=%d CPL=%u Sel RPL=%u -> #GP\n", uSel, pDesc->Legacy.Gate.u2Dpl,
|
---|
1164 | pVCpu->iem.s.uCpl, (uSel & X86_SEL_RPL)));
|
---|
1165 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1166 | }
|
---|
1167 |
|
---|
1168 | /** @todo does this catch NULL selectors, too? */
|
---|
1169 | if (!pDesc->Legacy.Gen.u1Present)
|
---|
1170 | {
|
---|
1171 | Log(("BranchCallGate Gate not present uSel=%04x -> #NP\n", uSel));
|
---|
1172 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uSel);
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 | /*
|
---|
1176 | * Fetch the target CS descriptor from the GDT or LDT.
|
---|
1177 | */
|
---|
1178 | uNewCS = pDesc->Legacy.Gate.u16Sel;
|
---|
1179 | rcStrict = iemMemFetchSelDesc(pVCpu, &DescCS, uNewCS, X86_XCPT_GP);
|
---|
1180 | if (rcStrict != VINF_SUCCESS)
|
---|
1181 | return rcStrict;
|
---|
1182 |
|
---|
1183 | /* Target CS must be a code selector. */
|
---|
1184 | if ( !DescCS.Legacy.Gen.u1DescType
|
---|
1185 | || !(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE) )
|
---|
1186 | {
|
---|
1187 | Log(("BranchCallGate %04x:%08RX64 -> not a code selector (u1DescType=%u u4Type=%#x).\n",
|
---|
1188 | uNewCS, uNewRip, DescCS.Legacy.Gen.u1DescType, DescCS.Legacy.Gen.u4Type));
|
---|
1189 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCS);
|
---|
1190 | }
|
---|
1191 |
|
---|
1192 | /* Privilege checks on target CS. */
|
---|
1193 | if (enmBranch == IEMBRANCH_JUMP)
|
---|
1194 | {
|
---|
1195 | if (DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF)
|
---|
1196 | {
|
---|
1197 | if (DescCS.Legacy.Gen.u2Dpl > pVCpu->iem.s.uCpl)
|
---|
1198 | {
|
---|
1199 | Log(("BranchCallGate jump (conforming) bad DPL uNewCS=%04x Gate DPL=%d CPL=%u -> #GP\n",
|
---|
1200 | uNewCS, DescCS.Legacy.Gen.u2Dpl, pVCpu->iem.s.uCpl));
|
---|
1201 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCS);
|
---|
1202 | }
|
---|
1203 | }
|
---|
1204 | else
|
---|
1205 | {
|
---|
1206 | if (DescCS.Legacy.Gen.u2Dpl != pVCpu->iem.s.uCpl)
|
---|
1207 | {
|
---|
1208 | Log(("BranchCallGate jump (non-conforming) bad DPL uNewCS=%04x Gate DPL=%d CPL=%u -> #GP\n",
|
---|
1209 | uNewCS, DescCS.Legacy.Gen.u2Dpl, pVCpu->iem.s.uCpl));
|
---|
1210 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCS);
|
---|
1211 | }
|
---|
1212 | }
|
---|
1213 | }
|
---|
1214 | else
|
---|
1215 | {
|
---|
1216 | Assert(enmBranch == IEMBRANCH_CALL);
|
---|
1217 | if (DescCS.Legacy.Gen.u2Dpl > pVCpu->iem.s.uCpl)
|
---|
1218 | {
|
---|
1219 | Log(("BranchCallGate call invalid priv. uNewCS=%04x Gate DPL=%d CPL=%u -> #GP\n",
|
---|
1220 | uNewCS, DescCS.Legacy.Gen.u2Dpl, pVCpu->iem.s.uCpl));
|
---|
1221 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCS & X86_SEL_MASK_OFF_RPL);
|
---|
1222 | }
|
---|
1223 | }
|
---|
1224 |
|
---|
1225 | /* Additional long mode checks. */
|
---|
1226 | if (IEM_IS_LONG_MODE(pVCpu))
|
---|
1227 | {
|
---|
1228 | if (!DescCS.Legacy.Gen.u1Long)
|
---|
1229 | {
|
---|
1230 | Log(("BranchCallGate uNewCS %04x -> not a 64-bit code segment.\n", uNewCS));
|
---|
1231 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCS);
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 | /* L vs D. */
|
---|
1235 | if ( DescCS.Legacy.Gen.u1Long
|
---|
1236 | && DescCS.Legacy.Gen.u1DefBig)
|
---|
1237 | {
|
---|
1238 | Log(("BranchCallGate uNewCS %04x -> both L and D are set.\n", uNewCS));
|
---|
1239 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCS);
|
---|
1240 | }
|
---|
1241 | }
|
---|
1242 |
|
---|
1243 | if (!DescCS.Legacy.Gate.u1Present)
|
---|
1244 | {
|
---|
1245 | Log(("BranchCallGate target CS is not present. uSel=%04x uNewCS=%04x -> #NP(CS)\n", uSel, uNewCS));
|
---|
1246 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uNewCS);
|
---|
1247 | }
|
---|
1248 |
|
---|
1249 | if (enmBranch == IEMBRANCH_JUMP)
|
---|
1250 | {
|
---|
1251 | /** @todo This is very similar to regular far jumps; merge! */
|
---|
1252 | /* Jumps are fairly simple... */
|
---|
1253 |
|
---|
1254 | /* Chop the high bits off if 16-bit gate (Intel says so). */
|
---|
1255 | if (pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_286_CALL_GATE)
|
---|
1256 | uNewRip = (uint16_t)uNewRip;
|
---|
1257 |
|
---|
1258 | /* Limit check for non-long segments. */
|
---|
1259 | cbLimit = X86DESC_LIMIT_G(&DescCS.Legacy);
|
---|
1260 | if (DescCS.Legacy.Gen.u1Long)
|
---|
1261 | u64Base = 0;
|
---|
1262 | else
|
---|
1263 | {
|
---|
1264 | if (uNewRip > cbLimit)
|
---|
1265 | {
|
---|
1266 | Log(("BranchCallGate jump %04x:%08RX64 -> out of bounds (%#x) -> #GP(0)\n", uNewCS, uNewRip, cbLimit));
|
---|
1267 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, 0);
|
---|
1268 | }
|
---|
1269 | u64Base = X86DESC_BASE(&DescCS.Legacy);
|
---|
1270 | }
|
---|
1271 |
|
---|
1272 | /* Canonical address check. */
|
---|
1273 | if (!IEM_IS_CANONICAL(uNewRip))
|
---|
1274 | {
|
---|
1275 | Log(("BranchCallGate jump %04x:%016RX64 - not canonical -> #GP\n", uNewCS, uNewRip));
|
---|
1276 | return iemRaiseNotCanonical(pVCpu);
|
---|
1277 | }
|
---|
1278 |
|
---|
1279 | /*
|
---|
1280 | * Ok, everything checked out fine. Now set the accessed bit before
|
---|
1281 | * committing the result into CS, CSHID and RIP.
|
---|
1282 | */
|
---|
1283 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
1284 | {
|
---|
1285 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewCS);
|
---|
1286 | if (rcStrict != VINF_SUCCESS)
|
---|
1287 | return rcStrict;
|
---|
1288 | /** @todo check what VT-x and AMD-V does. */
|
---|
1289 | DescCS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
1290 | }
|
---|
1291 |
|
---|
1292 | /* commit */
|
---|
1293 | pVCpu->cpum.GstCtx.rip = uNewRip;
|
---|
1294 | pVCpu->cpum.GstCtx.cs.Sel = uNewCS & X86_SEL_MASK_OFF_RPL;
|
---|
1295 | pVCpu->cpum.GstCtx.cs.Sel |= pVCpu->iem.s.uCpl; /** @todo is this right for conforming segs? or in general? */
|
---|
1296 | pVCpu->cpum.GstCtx.cs.ValidSel = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
1297 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
1298 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCS.Legacy);
|
---|
1299 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimit;
|
---|
1300 | pVCpu->cpum.GstCtx.cs.u64Base = u64Base;
|
---|
1301 | pVCpu->iem.s.enmCpuMode = iemCalcCpuMode(pVCpu);
|
---|
1302 | }
|
---|
1303 | else
|
---|
1304 | {
|
---|
1305 | Assert(enmBranch == IEMBRANCH_CALL);
|
---|
1306 | /* Calls are much more complicated. */
|
---|
1307 |
|
---|
1308 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF) && (DescCS.Legacy.Gen.u2Dpl < pVCpu->iem.s.uCpl))
|
---|
1309 | {
|
---|
1310 | uint16_t offNewStack; /* Offset of new stack in TSS. */
|
---|
1311 | uint16_t cbNewStack; /* Number of bytes the stack information takes up in TSS. */
|
---|
1312 | uint8_t uNewCSDpl;
|
---|
1313 | uint8_t cbWords;
|
---|
1314 | RTSEL uNewSS;
|
---|
1315 | RTSEL uOldSS;
|
---|
1316 | uint64_t uOldRsp;
|
---|
1317 | IEMSELDESC DescSS;
|
---|
1318 | RTPTRUNION uPtrTSS;
|
---|
1319 | RTGCPTR GCPtrTSS;
|
---|
1320 | RTPTRUNION uPtrParmWds;
|
---|
1321 | RTGCPTR GCPtrParmWds;
|
---|
1322 |
|
---|
1323 | /* More privilege. This is the fun part. */
|
---|
1324 | Assert(!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF)); /* Filtered out above. */
|
---|
1325 |
|
---|
1326 | /*
|
---|
1327 | * Determine new SS:rSP from the TSS.
|
---|
1328 | */
|
---|
1329 | Assert(!pVCpu->cpum.GstCtx.tr.Attr.n.u1DescType);
|
---|
1330 |
|
---|
1331 | /* Figure out where the new stack pointer is stored in the TSS. */
|
---|
1332 | uNewCSDpl = DescCS.Legacy.Gen.u2Dpl;
|
---|
1333 | if (!IEM_IS_LONG_MODE(pVCpu))
|
---|
1334 | {
|
---|
1335 | if (pVCpu->cpum.GstCtx.tr.Attr.n.u4Type == X86_SEL_TYPE_SYS_386_TSS_BUSY)
|
---|
1336 | {
|
---|
1337 | offNewStack = RT_UOFFSETOF(X86TSS32, esp0) + uNewCSDpl * 8;
|
---|
1338 | cbNewStack = RT_SIZEOFMEMB(X86TSS32, esp0) + RT_SIZEOFMEMB(X86TSS32, ss0);
|
---|
1339 | }
|
---|
1340 | else
|
---|
1341 | {
|
---|
1342 | Assert(pVCpu->cpum.GstCtx.tr.Attr.n.u4Type == X86_SEL_TYPE_SYS_286_TSS_BUSY);
|
---|
1343 | offNewStack = RT_UOFFSETOF(X86TSS16, sp0) + uNewCSDpl * 4;
|
---|
1344 | cbNewStack = RT_SIZEOFMEMB(X86TSS16, sp0) + RT_SIZEOFMEMB(X86TSS16, ss0);
|
---|
1345 | }
|
---|
1346 | }
|
---|
1347 | else
|
---|
1348 | {
|
---|
1349 | Assert(pVCpu->cpum.GstCtx.tr.Attr.n.u4Type == AMD64_SEL_TYPE_SYS_TSS_BUSY);
|
---|
1350 | offNewStack = RT_UOFFSETOF(X86TSS64, rsp0) + uNewCSDpl * RT_SIZEOFMEMB(X86TSS64, rsp0);
|
---|
1351 | cbNewStack = RT_SIZEOFMEMB(X86TSS64, rsp0);
|
---|
1352 | }
|
---|
1353 |
|
---|
1354 | /* Check against TSS limit. */
|
---|
1355 | if ((uint16_t)(offNewStack + cbNewStack - 1) > pVCpu->cpum.GstCtx.tr.u32Limit)
|
---|
1356 | {
|
---|
1357 | Log(("BranchCallGate inner stack past TSS limit - %u > %u -> #TS(TSS)\n", offNewStack + cbNewStack - 1, pVCpu->cpum.GstCtx.tr.u32Limit));
|
---|
1358 | return iemRaiseTaskSwitchFaultBySelector(pVCpu, pVCpu->cpum.GstCtx.tr.Sel);
|
---|
1359 | }
|
---|
1360 |
|
---|
1361 | GCPtrTSS = pVCpu->cpum.GstCtx.tr.u64Base + offNewStack;
|
---|
1362 | rcStrict = iemMemMap(pVCpu, &uPtrTSS.pv, cbNewStack, UINT8_MAX, GCPtrTSS, IEM_ACCESS_SYS_R, 0);
|
---|
1363 | if (rcStrict != VINF_SUCCESS)
|
---|
1364 | {
|
---|
1365 | Log(("BranchCallGate: TSS mapping failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1366 | return rcStrict;
|
---|
1367 | }
|
---|
1368 |
|
---|
1369 | if (!IEM_IS_LONG_MODE(pVCpu))
|
---|
1370 | {
|
---|
1371 | if (pVCpu->cpum.GstCtx.tr.Attr.n.u4Type == X86_SEL_TYPE_SYS_386_TSS_BUSY)
|
---|
1372 | {
|
---|
1373 | uNewRsp = uPtrTSS.pu32[0];
|
---|
1374 | uNewSS = uPtrTSS.pu16[2];
|
---|
1375 | }
|
---|
1376 | else
|
---|
1377 | {
|
---|
1378 | Assert(pVCpu->cpum.GstCtx.tr.Attr.n.u4Type == X86_SEL_TYPE_SYS_286_TSS_BUSY);
|
---|
1379 | uNewRsp = uPtrTSS.pu16[0];
|
---|
1380 | uNewSS = uPtrTSS.pu16[1];
|
---|
1381 | }
|
---|
1382 | }
|
---|
1383 | else
|
---|
1384 | {
|
---|
1385 | Assert(pVCpu->cpum.GstCtx.tr.Attr.n.u4Type == AMD64_SEL_TYPE_SYS_TSS_BUSY);
|
---|
1386 | /* SS will be a NULL selector, but that's valid. */
|
---|
1387 | uNewRsp = uPtrTSS.pu64[0];
|
---|
1388 | uNewSS = uNewCSDpl;
|
---|
1389 | }
|
---|
1390 |
|
---|
1391 | /* Done with the TSS now. */
|
---|
1392 | rcStrict = iemMemCommitAndUnmap(pVCpu, uPtrTSS.pv, IEM_ACCESS_SYS_R);
|
---|
1393 | if (rcStrict != VINF_SUCCESS)
|
---|
1394 | {
|
---|
1395 | Log(("BranchCallGate: TSS unmapping failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1396 | return rcStrict;
|
---|
1397 | }
|
---|
1398 |
|
---|
1399 | /* Only used outside of long mode. */
|
---|
1400 | cbWords = pDesc->Legacy.Gate.u5ParmCount;
|
---|
1401 |
|
---|
1402 | /* If EFER.LMA is 0, there's extra work to do. */
|
---|
1403 | if (!IEM_IS_LONG_MODE(pVCpu))
|
---|
1404 | {
|
---|
1405 | if ((uNewSS & X86_SEL_MASK_OFF_RPL) == 0)
|
---|
1406 | {
|
---|
1407 | Log(("BranchCallGate new SS NULL -> #TS(NewSS)\n"));
|
---|
1408 | return iemRaiseTaskSwitchFaultBySelector(pVCpu, uNewSS);
|
---|
1409 | }
|
---|
1410 |
|
---|
1411 | /* Grab the new SS descriptor. */
|
---|
1412 | rcStrict = iemMemFetchSelDesc(pVCpu, &DescSS, uNewSS, X86_XCPT_SS);
|
---|
1413 | if (rcStrict != VINF_SUCCESS)
|
---|
1414 | return rcStrict;
|
---|
1415 |
|
---|
1416 | /* Ensure that CS.DPL == SS.RPL == SS.DPL. */
|
---|
1417 | if ( (DescCS.Legacy.Gen.u2Dpl != (uNewSS & X86_SEL_RPL))
|
---|
1418 | || (DescCS.Legacy.Gen.u2Dpl != DescSS.Legacy.Gen.u2Dpl))
|
---|
1419 | {
|
---|
1420 | Log(("BranchCallGate call bad RPL/DPL uNewSS=%04x SS DPL=%d CS DPL=%u -> #TS(NewSS)\n",
|
---|
1421 | uNewSS, DescCS.Legacy.Gen.u2Dpl, DescCS.Legacy.Gen.u2Dpl));
|
---|
1422 | return iemRaiseTaskSwitchFaultBySelector(pVCpu, uNewSS);
|
---|
1423 | }
|
---|
1424 |
|
---|
1425 | /* Ensure new SS is a writable data segment. */
|
---|
1426 | if ((DescSS.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_WRITE)) != X86_SEL_TYPE_WRITE)
|
---|
1427 | {
|
---|
1428 | Log(("BranchCallGate call new SS -> not a writable data selector (u4Type=%#x)\n", DescSS.Legacy.Gen.u4Type));
|
---|
1429 | return iemRaiseTaskSwitchFaultBySelector(pVCpu, uNewSS);
|
---|
1430 | }
|
---|
1431 |
|
---|
1432 | if (!DescSS.Legacy.Gen.u1Present)
|
---|
1433 | {
|
---|
1434 | Log(("BranchCallGate New stack not present uSel=%04x -> #SS(NewSS)\n", uNewSS));
|
---|
1435 | return iemRaiseStackSelectorNotPresentBySelector(pVCpu, uNewSS);
|
---|
1436 | }
|
---|
1437 | if (pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_386_CALL_GATE)
|
---|
1438 | cbNewStack = (uint16_t)sizeof(uint32_t) * (4 + cbWords);
|
---|
1439 | else
|
---|
1440 | cbNewStack = (uint16_t)sizeof(uint16_t) * (4 + cbWords);
|
---|
1441 | }
|
---|
1442 | else
|
---|
1443 | {
|
---|
1444 | /* Just grab the new (NULL) SS descriptor. */
|
---|
1445 | /** @todo testcase: Check whether the zero GDT entry is actually loaded here
|
---|
1446 | * like we do... */
|
---|
1447 | rcStrict = iemMemFetchSelDesc(pVCpu, &DescSS, uNewSS, X86_XCPT_SS);
|
---|
1448 | if (rcStrict != VINF_SUCCESS)
|
---|
1449 | return rcStrict;
|
---|
1450 |
|
---|
1451 | cbNewStack = sizeof(uint64_t) * 4;
|
---|
1452 | }
|
---|
1453 |
|
---|
1454 | /** @todo According to Intel, new stack is checked for enough space first,
|
---|
1455 | * then switched. According to AMD, the stack is switched first and
|
---|
1456 | * then pushes might fault!
|
---|
1457 | * NB: OS/2 Warp 3/4 actively relies on the fact that possible
|
---|
1458 | * incoming stack \#PF happens before actual stack switch. AMD is
|
---|
1459 | * either lying or implicitly assumes that new state is committed
|
---|
1460 | * only if and when an instruction doesn't fault.
|
---|
1461 | */
|
---|
1462 |
|
---|
1463 | /** @todo According to AMD, CS is loaded first, then SS.
|
---|
1464 | * According to Intel, it's the other way around!?
|
---|
1465 | */
|
---|
1466 |
|
---|
1467 | /** @todo Intel and AMD disagree on when exactly the CPL changes! */
|
---|
1468 |
|
---|
1469 | /* Set the accessed bit before committing new SS. */
|
---|
1470 | if (!(DescSS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
1471 | {
|
---|
1472 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewSS);
|
---|
1473 | if (rcStrict != VINF_SUCCESS)
|
---|
1474 | return rcStrict;
|
---|
1475 | DescSS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
1476 | }
|
---|
1477 |
|
---|
1478 | /* Remember the old SS:rSP and their linear address. */
|
---|
1479 | uOldSS = pVCpu->cpum.GstCtx.ss.Sel;
|
---|
1480 | uOldRsp = pVCpu->cpum.GstCtx.ss.Attr.n.u1DefBig ? pVCpu->cpum.GstCtx.rsp : pVCpu->cpum.GstCtx.sp;
|
---|
1481 |
|
---|
1482 | GCPtrParmWds = pVCpu->cpum.GstCtx.ss.u64Base + uOldRsp;
|
---|
1483 |
|
---|
1484 | /* HACK ALERT! Probe if the write to the new stack will succeed. May #SS(NewSS)
|
---|
1485 | or #PF, the former is not implemented in this workaround. */
|
---|
1486 | /** @todo Proper fix callgate target stack exceptions. */
|
---|
1487 | /** @todo testcase: Cover callgates with partially or fully inaccessible
|
---|
1488 | * target stacks. */
|
---|
1489 | void *pvNewFrame;
|
---|
1490 | RTGCPTR GCPtrNewStack = X86DESC_BASE(&DescSS.Legacy) + uNewRsp - cbNewStack;
|
---|
1491 | rcStrict = iemMemMap(pVCpu, &pvNewFrame, cbNewStack, UINT8_MAX, GCPtrNewStack, IEM_ACCESS_SYS_RW, 0);
|
---|
1492 | if (rcStrict != VINF_SUCCESS)
|
---|
1493 | {
|
---|
1494 | Log(("BranchCallGate: Incoming stack (%04x:%08RX64) not accessible, rc=%Rrc\n", uNewSS, uNewRsp, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1495 | return rcStrict;
|
---|
1496 | }
|
---|
1497 | rcStrict = iemMemCommitAndUnmap(pVCpu, pvNewFrame, IEM_ACCESS_SYS_RW);
|
---|
1498 | if (rcStrict != VINF_SUCCESS)
|
---|
1499 | {
|
---|
1500 | Log(("BranchCallGate: New stack probe unmapping failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1501 | return rcStrict;
|
---|
1502 | }
|
---|
1503 |
|
---|
1504 | /* Commit new SS:rSP. */
|
---|
1505 | pVCpu->cpum.GstCtx.ss.Sel = uNewSS;
|
---|
1506 | pVCpu->cpum.GstCtx.ss.ValidSel = uNewSS;
|
---|
1507 | pVCpu->cpum.GstCtx.ss.Attr.u = X86DESC_GET_HID_ATTR(&DescSS.Legacy);
|
---|
1508 | pVCpu->cpum.GstCtx.ss.u32Limit = X86DESC_LIMIT_G(&DescSS.Legacy);
|
---|
1509 | pVCpu->cpum.GstCtx.ss.u64Base = X86DESC_BASE(&DescSS.Legacy);
|
---|
1510 | pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
1511 | pVCpu->cpum.GstCtx.rsp = uNewRsp;
|
---|
1512 | pVCpu->iem.s.uCpl = uNewCSDpl; /** @todo is the parameter words accessed using the new CPL or the old CPL? */
|
---|
1513 | Assert(CPUMSELREG_ARE_HIDDEN_PARTS_VALID(pVCpu, &pVCpu->cpum.GstCtx.ss));
|
---|
1514 | CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_HIDDEN_SEL_REGS);
|
---|
1515 |
|
---|
1516 | /* At this point the stack access must not fail because new state was already committed. */
|
---|
1517 | /** @todo this can still fail due to SS.LIMIT not check. */
|
---|
1518 | rcStrict = iemMemStackPushBeginSpecial(pVCpu, cbNewStack,
|
---|
1519 | IEM_IS_LONG_MODE(pVCpu) ? 7
|
---|
1520 | : pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_386_CALL_GATE ? 3 : 1,
|
---|
1521 | &uPtrRet.pv, &uNewRsp);
|
---|
1522 | AssertMsgReturn(rcStrict == VINF_SUCCESS, ("BranchCallGate: New stack mapping failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)),
|
---|
1523 | VERR_INTERNAL_ERROR_5);
|
---|
1524 |
|
---|
1525 | if (!IEM_IS_LONG_MODE(pVCpu))
|
---|
1526 | {
|
---|
1527 | if (pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_386_CALL_GATE)
|
---|
1528 | {
|
---|
1529 | if (cbWords)
|
---|
1530 | {
|
---|
1531 | /* Map the relevant chunk of the old stack. */
|
---|
1532 | rcStrict = iemMemMap(pVCpu, &uPtrParmWds.pv, cbWords * 4, UINT8_MAX, GCPtrParmWds,
|
---|
1533 | IEM_ACCESS_DATA_R, 0 /** @todo Can uNewCSDpl == 3? Then we need alignment mask here! */);
|
---|
1534 | if (rcStrict != VINF_SUCCESS)
|
---|
1535 | {
|
---|
1536 | Log(("BranchCallGate: Old stack mapping (32-bit) failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1537 | return rcStrict;
|
---|
1538 | }
|
---|
1539 |
|
---|
1540 | /* Copy the parameter (d)words. */
|
---|
1541 | for (int i = 0; i < cbWords; ++i)
|
---|
1542 | uPtrRet.pu32[2 + i] = uPtrParmWds.pu32[i];
|
---|
1543 |
|
---|
1544 | /* Unmap the old stack. */
|
---|
1545 | rcStrict = iemMemCommitAndUnmap(pVCpu, uPtrParmWds.pv, IEM_ACCESS_DATA_R);
|
---|
1546 | if (rcStrict != VINF_SUCCESS)
|
---|
1547 | {
|
---|
1548 | Log(("BranchCallGate: Old stack unmapping (32-bit) failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1549 | return rcStrict;
|
---|
1550 | }
|
---|
1551 | }
|
---|
1552 |
|
---|
1553 | /* Push the old CS:rIP. */
|
---|
1554 | uPtrRet.pu32[0] = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
1555 | uPtrRet.pu32[1] = pVCpu->cpum.GstCtx.cs.Sel; /** @todo Testcase: What is written to the high word when pushing CS? */
|
---|
1556 |
|
---|
1557 | /* Push the old SS:rSP. */
|
---|
1558 | uPtrRet.pu32[2 + cbWords + 0] = uOldRsp;
|
---|
1559 | uPtrRet.pu32[2 + cbWords + 1] = uOldSS;
|
---|
1560 | }
|
---|
1561 | else
|
---|
1562 | {
|
---|
1563 | Assert(pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_286_CALL_GATE);
|
---|
1564 |
|
---|
1565 | if (cbWords)
|
---|
1566 | {
|
---|
1567 | /* Map the relevant chunk of the old stack. */
|
---|
1568 | rcStrict = iemMemMap(pVCpu, &uPtrParmWds.pv, cbWords * 2, UINT8_MAX, GCPtrParmWds,
|
---|
1569 | IEM_ACCESS_DATA_R, 0 /** @todo Can uNewCSDpl == 3? Then we need alignment mask here! */);
|
---|
1570 | if (rcStrict != VINF_SUCCESS)
|
---|
1571 | {
|
---|
1572 | Log(("BranchCallGate: Old stack mapping (16-bit) failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1573 | return rcStrict;
|
---|
1574 | }
|
---|
1575 |
|
---|
1576 | /* Copy the parameter words. */
|
---|
1577 | for (int i = 0; i < cbWords; ++i)
|
---|
1578 | uPtrRet.pu16[2 + i] = uPtrParmWds.pu16[i];
|
---|
1579 |
|
---|
1580 | /* Unmap the old stack. */
|
---|
1581 | rcStrict = iemMemCommitAndUnmap(pVCpu, uPtrParmWds.pv, IEM_ACCESS_DATA_R);
|
---|
1582 | if (rcStrict != VINF_SUCCESS)
|
---|
1583 | {
|
---|
1584 | Log(("BranchCallGate: Old stack unmapping (32-bit) failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1585 | return rcStrict;
|
---|
1586 | }
|
---|
1587 | }
|
---|
1588 |
|
---|
1589 | /* Push the old CS:rIP. */
|
---|
1590 | uPtrRet.pu16[0] = pVCpu->cpum.GstCtx.ip + cbInstr;
|
---|
1591 | uPtrRet.pu16[1] = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
1592 |
|
---|
1593 | /* Push the old SS:rSP. */
|
---|
1594 | uPtrRet.pu16[2 + cbWords + 0] = uOldRsp;
|
---|
1595 | uPtrRet.pu16[2 + cbWords + 1] = uOldSS;
|
---|
1596 | }
|
---|
1597 | }
|
---|
1598 | else
|
---|
1599 | {
|
---|
1600 | Assert(pDesc->Legacy.Gate.u4Type == AMD64_SEL_TYPE_SYS_CALL_GATE);
|
---|
1601 |
|
---|
1602 | /* For 64-bit gates, no parameters are copied. Just push old SS:rSP and CS:rIP. */
|
---|
1603 | uPtrRet.pu64[0] = pVCpu->cpum.GstCtx.rip + cbInstr;
|
---|
1604 | uPtrRet.pu64[1] = pVCpu->cpum.GstCtx.cs.Sel; /** @todo Testcase: What is written to the high words when pushing CS? */
|
---|
1605 | uPtrRet.pu64[2] = uOldRsp;
|
---|
1606 | uPtrRet.pu64[3] = uOldSS; /** @todo Testcase: What is written to the high words when pushing SS? */
|
---|
1607 | }
|
---|
1608 |
|
---|
1609 | rcStrict = iemMemStackPushCommitSpecial(pVCpu, uPtrRet.pv, uNewRsp);
|
---|
1610 | if (rcStrict != VINF_SUCCESS)
|
---|
1611 | {
|
---|
1612 | Log(("BranchCallGate: New stack unmapping failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1613 | return rcStrict;
|
---|
1614 | }
|
---|
1615 |
|
---|
1616 | /* Chop the high bits off if 16-bit gate (Intel says so). */
|
---|
1617 | if (pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_286_CALL_GATE)
|
---|
1618 | uNewRip = (uint16_t)uNewRip;
|
---|
1619 |
|
---|
1620 | /* Limit / canonical check. */
|
---|
1621 | cbLimit = X86DESC_LIMIT_G(&DescCS.Legacy);
|
---|
1622 | if (!IEM_IS_LONG_MODE(pVCpu))
|
---|
1623 | {
|
---|
1624 | if (uNewRip > cbLimit)
|
---|
1625 | {
|
---|
1626 | Log(("BranchCallGate %04x:%08RX64 -> out of bounds (%#x)\n", uNewCS, uNewRip, cbLimit));
|
---|
1627 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, 0);
|
---|
1628 | }
|
---|
1629 | u64Base = X86DESC_BASE(&DescCS.Legacy);
|
---|
1630 | }
|
---|
1631 | else
|
---|
1632 | {
|
---|
1633 | Assert(pDesc->Legacy.Gate.u4Type == AMD64_SEL_TYPE_SYS_CALL_GATE);
|
---|
1634 | if (!IEM_IS_CANONICAL(uNewRip))
|
---|
1635 | {
|
---|
1636 | Log(("BranchCallGate call %04x:%016RX64 - not canonical -> #GP\n", uNewCS, uNewRip));
|
---|
1637 | return iemRaiseNotCanonical(pVCpu);
|
---|
1638 | }
|
---|
1639 | u64Base = 0;
|
---|
1640 | }
|
---|
1641 |
|
---|
1642 | /*
|
---|
1643 | * Now set the accessed bit before
|
---|
1644 | * writing the return address to the stack and committing the result into
|
---|
1645 | * CS, CSHID and RIP.
|
---|
1646 | */
|
---|
1647 | /** @todo Testcase: Need to check WHEN exactly the accessed bit is set. */
|
---|
1648 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
1649 | {
|
---|
1650 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewCS);
|
---|
1651 | if (rcStrict != VINF_SUCCESS)
|
---|
1652 | return rcStrict;
|
---|
1653 | /** @todo check what VT-x and AMD-V does. */
|
---|
1654 | DescCS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
1655 | }
|
---|
1656 |
|
---|
1657 | /* Commit new CS:rIP. */
|
---|
1658 | pVCpu->cpum.GstCtx.rip = uNewRip;
|
---|
1659 | pVCpu->cpum.GstCtx.cs.Sel = uNewCS & X86_SEL_MASK_OFF_RPL;
|
---|
1660 | pVCpu->cpum.GstCtx.cs.Sel |= pVCpu->iem.s.uCpl;
|
---|
1661 | pVCpu->cpum.GstCtx.cs.ValidSel = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
1662 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
1663 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCS.Legacy);
|
---|
1664 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimit;
|
---|
1665 | pVCpu->cpum.GstCtx.cs.u64Base = u64Base;
|
---|
1666 | pVCpu->iem.s.enmCpuMode = iemCalcCpuMode(pVCpu);
|
---|
1667 | }
|
---|
1668 | else
|
---|
1669 | {
|
---|
1670 | /* Same privilege. */
|
---|
1671 | /** @todo This is very similar to regular far calls; merge! */
|
---|
1672 |
|
---|
1673 | /* Check stack first - may #SS(0). */
|
---|
1674 | /** @todo check how gate size affects pushing of CS! Does callf 16:32 in
|
---|
1675 | * 16-bit code cause a two or four byte CS to be pushed? */
|
---|
1676 | rcStrict = iemMemStackPushBeginSpecial(pVCpu,
|
---|
1677 | IEM_IS_LONG_MODE(pVCpu) ? 8+8
|
---|
1678 | : pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_386_CALL_GATE ? 4+4 : 2+2,
|
---|
1679 | IEM_IS_LONG_MODE(pVCpu) ? 7
|
---|
1680 | : pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_386_CALL_GATE ? 3 : 2,
|
---|
1681 | &uPtrRet.pv, &uNewRsp);
|
---|
1682 | if (rcStrict != VINF_SUCCESS)
|
---|
1683 | return rcStrict;
|
---|
1684 |
|
---|
1685 | /* Chop the high bits off if 16-bit gate (Intel says so). */
|
---|
1686 | if (pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_286_CALL_GATE)
|
---|
1687 | uNewRip = (uint16_t)uNewRip;
|
---|
1688 |
|
---|
1689 | /* Limit / canonical check. */
|
---|
1690 | cbLimit = X86DESC_LIMIT_G(&DescCS.Legacy);
|
---|
1691 | if (!IEM_IS_LONG_MODE(pVCpu))
|
---|
1692 | {
|
---|
1693 | if (uNewRip > cbLimit)
|
---|
1694 | {
|
---|
1695 | Log(("BranchCallGate %04x:%08RX64 -> out of bounds (%#x)\n", uNewCS, uNewRip, cbLimit));
|
---|
1696 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, 0);
|
---|
1697 | }
|
---|
1698 | u64Base = X86DESC_BASE(&DescCS.Legacy);
|
---|
1699 | }
|
---|
1700 | else
|
---|
1701 | {
|
---|
1702 | if (!IEM_IS_CANONICAL(uNewRip))
|
---|
1703 | {
|
---|
1704 | Log(("BranchCallGate call %04x:%016RX64 - not canonical -> #GP\n", uNewCS, uNewRip));
|
---|
1705 | return iemRaiseNotCanonical(pVCpu);
|
---|
1706 | }
|
---|
1707 | u64Base = 0;
|
---|
1708 | }
|
---|
1709 |
|
---|
1710 | /*
|
---|
1711 | * Now set the accessed bit before
|
---|
1712 | * writing the return address to the stack and committing the result into
|
---|
1713 | * CS, CSHID and RIP.
|
---|
1714 | */
|
---|
1715 | /** @todo Testcase: Need to check WHEN exactly the accessed bit is set. */
|
---|
1716 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
1717 | {
|
---|
1718 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewCS);
|
---|
1719 | if (rcStrict != VINF_SUCCESS)
|
---|
1720 | return rcStrict;
|
---|
1721 | /** @todo check what VT-x and AMD-V does. */
|
---|
1722 | DescCS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
1723 | }
|
---|
1724 |
|
---|
1725 | /* stack */
|
---|
1726 | if (!IEM_IS_LONG_MODE(pVCpu))
|
---|
1727 | {
|
---|
1728 | if (pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_386_CALL_GATE)
|
---|
1729 | {
|
---|
1730 | uPtrRet.pu32[0] = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
1731 | uPtrRet.pu32[1] = pVCpu->cpum.GstCtx.cs.Sel; /** @todo Testcase: What is written to the high word when pushing CS? */
|
---|
1732 | }
|
---|
1733 | else
|
---|
1734 | {
|
---|
1735 | Assert(pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_286_CALL_GATE);
|
---|
1736 | uPtrRet.pu16[0] = pVCpu->cpum.GstCtx.ip + cbInstr;
|
---|
1737 | uPtrRet.pu16[1] = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
1738 | }
|
---|
1739 | }
|
---|
1740 | else
|
---|
1741 | {
|
---|
1742 | Assert(pDesc->Legacy.Gate.u4Type == AMD64_SEL_TYPE_SYS_CALL_GATE);
|
---|
1743 | uPtrRet.pu64[0] = pVCpu->cpum.GstCtx.rip + cbInstr;
|
---|
1744 | uPtrRet.pu64[1] = pVCpu->cpum.GstCtx.cs.Sel; /** @todo Testcase: What is written to the high words when pushing CS? */
|
---|
1745 | }
|
---|
1746 |
|
---|
1747 | rcStrict = iemMemStackPushCommitSpecial(pVCpu, uPtrRet.pv, uNewRsp);
|
---|
1748 | if (rcStrict != VINF_SUCCESS)
|
---|
1749 | return rcStrict;
|
---|
1750 |
|
---|
1751 | /* commit */
|
---|
1752 | pVCpu->cpum.GstCtx.rip = uNewRip;
|
---|
1753 | pVCpu->cpum.GstCtx.cs.Sel = uNewCS & X86_SEL_MASK_OFF_RPL;
|
---|
1754 | pVCpu->cpum.GstCtx.cs.Sel |= pVCpu->iem.s.uCpl;
|
---|
1755 | pVCpu->cpum.GstCtx.cs.ValidSel = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
1756 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
1757 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCS.Legacy);
|
---|
1758 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimit;
|
---|
1759 | pVCpu->cpum.GstCtx.cs.u64Base = u64Base;
|
---|
1760 | pVCpu->iem.s.enmCpuMode = iemCalcCpuMode(pVCpu);
|
---|
1761 | }
|
---|
1762 | }
|
---|
1763 | pVCpu->cpum.GstCtx.eflags.Bits.u1RF = 0;
|
---|
1764 | /** @todo single stepping */
|
---|
1765 |
|
---|
1766 | /* Flush the prefetch buffer. */
|
---|
1767 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr);
|
---|
1768 | return VINF_SUCCESS;
|
---|
1769 | #endif /* IEM_IMPLEMENTS_CALLGATE */
|
---|
1770 | }
|
---|
1771 |
|
---|
1772 |
|
---|
1773 | /**
|
---|
1774 | * Implements far jumps and calls thru system selectors.
|
---|
1775 | *
|
---|
1776 | * @returns VBox strict status code.
|
---|
1777 | * @param pVCpu The cross context virtual CPU structure of the
|
---|
1778 | * calling thread.
|
---|
1779 | * @param cbInstr The current instruction length.
|
---|
1780 | * @param uSel The selector.
|
---|
1781 | * @param enmBranch The kind of branching we're performing.
|
---|
1782 | * @param enmEffOpSize The effective operand size.
|
---|
1783 | * @param pDesc The descriptor corresponding to @a uSel.
|
---|
1784 | */
|
---|
1785 | static VBOXSTRICTRC iemCImpl_BranchSysSel(PVMCPUCC pVCpu, uint8_t cbInstr, uint16_t uSel, IEMBRANCH enmBranch,
|
---|
1786 | IEMMODE enmEffOpSize, PIEMSELDESC pDesc)
|
---|
1787 | {
|
---|
1788 | Assert(enmBranch == IEMBRANCH_JUMP || enmBranch == IEMBRANCH_CALL);
|
---|
1789 | Assert((uSel & X86_SEL_MASK_OFF_RPL));
|
---|
1790 | IEM_CTX_IMPORT_RET(pVCpu, IEM_CPUMCTX_EXTRN_XCPT_MASK);
|
---|
1791 |
|
---|
1792 | if (IEM_IS_LONG_MODE(pVCpu))
|
---|
1793 | switch (pDesc->Legacy.Gen.u4Type)
|
---|
1794 | {
|
---|
1795 | case AMD64_SEL_TYPE_SYS_CALL_GATE:
|
---|
1796 | return iemCImpl_BranchCallGate(pVCpu, cbInstr, uSel, enmBranch, enmEffOpSize, pDesc);
|
---|
1797 |
|
---|
1798 | default:
|
---|
1799 | case AMD64_SEL_TYPE_SYS_LDT:
|
---|
1800 | case AMD64_SEL_TYPE_SYS_TSS_BUSY:
|
---|
1801 | case AMD64_SEL_TYPE_SYS_TSS_AVAIL:
|
---|
1802 | case AMD64_SEL_TYPE_SYS_TRAP_GATE:
|
---|
1803 | case AMD64_SEL_TYPE_SYS_INT_GATE:
|
---|
1804 | Log(("branch %04x -> wrong sys selector (64-bit): %d\n", uSel, pDesc->Legacy.Gen.u4Type));
|
---|
1805 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1806 | }
|
---|
1807 |
|
---|
1808 | switch (pDesc->Legacy.Gen.u4Type)
|
---|
1809 | {
|
---|
1810 | case X86_SEL_TYPE_SYS_286_CALL_GATE:
|
---|
1811 | case X86_SEL_TYPE_SYS_386_CALL_GATE:
|
---|
1812 | return iemCImpl_BranchCallGate(pVCpu, cbInstr, uSel, enmBranch, enmEffOpSize, pDesc);
|
---|
1813 |
|
---|
1814 | case X86_SEL_TYPE_SYS_TASK_GATE:
|
---|
1815 | return iemCImpl_BranchTaskGate(pVCpu, cbInstr, uSel, enmBranch, enmEffOpSize, pDesc);
|
---|
1816 |
|
---|
1817 | case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
|
---|
1818 | case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
|
---|
1819 | return iemCImpl_BranchTaskSegment(pVCpu, cbInstr, uSel, enmBranch, enmEffOpSize, pDesc);
|
---|
1820 |
|
---|
1821 | case X86_SEL_TYPE_SYS_286_TSS_BUSY:
|
---|
1822 | Log(("branch %04x -> busy 286 TSS\n", uSel));
|
---|
1823 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1824 |
|
---|
1825 | case X86_SEL_TYPE_SYS_386_TSS_BUSY:
|
---|
1826 | Log(("branch %04x -> busy 386 TSS\n", uSel));
|
---|
1827 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1828 |
|
---|
1829 | default:
|
---|
1830 | case X86_SEL_TYPE_SYS_LDT:
|
---|
1831 | case X86_SEL_TYPE_SYS_286_INT_GATE:
|
---|
1832 | case X86_SEL_TYPE_SYS_286_TRAP_GATE:
|
---|
1833 | case X86_SEL_TYPE_SYS_386_INT_GATE:
|
---|
1834 | case X86_SEL_TYPE_SYS_386_TRAP_GATE:
|
---|
1835 | Log(("branch %04x -> wrong sys selector: %d\n", uSel, pDesc->Legacy.Gen.u4Type));
|
---|
1836 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1837 | }
|
---|
1838 | }
|
---|
1839 |
|
---|
1840 |
|
---|
1841 | /**
|
---|
1842 | * Implements far jumps.
|
---|
1843 | *
|
---|
1844 | * @param uSel The selector.
|
---|
1845 | * @param offSeg The segment offset.
|
---|
1846 | * @param enmEffOpSize The effective operand size.
|
---|
1847 | */
|
---|
1848 | IEM_CIMPL_DEF_3(iemCImpl_FarJmp, uint16_t, uSel, uint64_t, offSeg, IEMMODE, enmEffOpSize)
|
---|
1849 | {
|
---|
1850 | NOREF(cbInstr);
|
---|
1851 | Assert(offSeg <= UINT32_MAX || (!IEM_IS_GUEST_CPU_AMD(pVCpu) && pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT));
|
---|
1852 |
|
---|
1853 | /*
|
---|
1854 | * Real mode and V8086 mode are easy. The only snag seems to be that
|
---|
1855 | * CS.limit doesn't change and the limit check is done against the current
|
---|
1856 | * limit.
|
---|
1857 | */
|
---|
1858 | /** @todo Robert Collins claims (The Segment Descriptor Cache, DDJ August
|
---|
1859 | * 1998) that up to and including the Intel 486, far control
|
---|
1860 | * transfers in real mode set default CS attributes (0x93) and also
|
---|
1861 | * set a 64K segment limit. Starting with the Pentium, the
|
---|
1862 | * attributes and limit are left alone but the access rights are
|
---|
1863 | * ignored. We only implement the Pentium+ behavior.
|
---|
1864 | * */
|
---|
1865 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
1866 | {
|
---|
1867 | Assert(enmEffOpSize == IEMMODE_16BIT || enmEffOpSize == IEMMODE_32BIT);
|
---|
1868 | if (offSeg > pVCpu->cpum.GstCtx.cs.u32Limit)
|
---|
1869 | {
|
---|
1870 | Log(("iemCImpl_FarJmp: 16-bit limit\n"));
|
---|
1871 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
1872 | }
|
---|
1873 |
|
---|
1874 | if (enmEffOpSize == IEMMODE_16BIT) /** @todo WRONG, must pass this. */
|
---|
1875 | pVCpu->cpum.GstCtx.rip = offSeg;
|
---|
1876 | else
|
---|
1877 | pVCpu->cpum.GstCtx.rip = offSeg & UINT16_MAX;
|
---|
1878 | pVCpu->cpum.GstCtx.cs.Sel = uSel;
|
---|
1879 | pVCpu->cpum.GstCtx.cs.ValidSel = uSel;
|
---|
1880 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
1881 | pVCpu->cpum.GstCtx.cs.u64Base = (uint32_t)uSel << 4;
|
---|
1882 |
|
---|
1883 | return iemRegFinishClearingRF(pVCpu);
|
---|
1884 | }
|
---|
1885 |
|
---|
1886 | /*
|
---|
1887 | * Protected mode. Need to parse the specified descriptor...
|
---|
1888 | */
|
---|
1889 | if (!(uSel & X86_SEL_MASK_OFF_RPL))
|
---|
1890 | {
|
---|
1891 | Log(("jmpf %04x:%08RX64 -> invalid selector, #GP(0)\n", uSel, offSeg));
|
---|
1892 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
1893 | }
|
---|
1894 |
|
---|
1895 | /* Fetch the descriptor. */
|
---|
1896 | IEMSELDESC Desc;
|
---|
1897 | VBOXSTRICTRC rcStrict = iemMemFetchSelDesc(pVCpu, &Desc, uSel, X86_XCPT_GP);
|
---|
1898 | if (rcStrict != VINF_SUCCESS)
|
---|
1899 | return rcStrict;
|
---|
1900 |
|
---|
1901 | /* Is it there? */
|
---|
1902 | if (!Desc.Legacy.Gen.u1Present) /** @todo this is probably checked too early. Testcase! */
|
---|
1903 | {
|
---|
1904 | Log(("jmpf %04x:%08RX64 -> segment not present\n", uSel, offSeg));
|
---|
1905 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uSel);
|
---|
1906 | }
|
---|
1907 |
|
---|
1908 | /*
|
---|
1909 | * Deal with it according to its type. We do the standard code selectors
|
---|
1910 | * here and dispatch the system selectors to worker functions.
|
---|
1911 | */
|
---|
1912 | if (!Desc.Legacy.Gen.u1DescType)
|
---|
1913 | return iemCImpl_BranchSysSel(pVCpu, cbInstr, uSel, IEMBRANCH_JUMP, enmEffOpSize, &Desc);
|
---|
1914 |
|
---|
1915 | /* Only code segments. */
|
---|
1916 | if (!(Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE))
|
---|
1917 | {
|
---|
1918 | Log(("jmpf %04x:%08RX64 -> not a code selector (u4Type=%#x).\n", uSel, offSeg, Desc.Legacy.Gen.u4Type));
|
---|
1919 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1920 | }
|
---|
1921 |
|
---|
1922 | /* L vs D. */
|
---|
1923 | if ( Desc.Legacy.Gen.u1Long
|
---|
1924 | && Desc.Legacy.Gen.u1DefBig
|
---|
1925 | && IEM_IS_LONG_MODE(pVCpu))
|
---|
1926 | {
|
---|
1927 | Log(("jmpf %04x:%08RX64 -> both L and D are set.\n", uSel, offSeg));
|
---|
1928 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1929 | }
|
---|
1930 |
|
---|
1931 | /* DPL/RPL/CPL check, where conforming segments makes a difference. */
|
---|
1932 | if (Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF)
|
---|
1933 | {
|
---|
1934 | if (pVCpu->iem.s.uCpl < Desc.Legacy.Gen.u2Dpl)
|
---|
1935 | {
|
---|
1936 | Log(("jmpf %04x:%08RX64 -> DPL violation (conforming); DPL=%d CPL=%u\n",
|
---|
1937 | uSel, offSeg, Desc.Legacy.Gen.u2Dpl, pVCpu->iem.s.uCpl));
|
---|
1938 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1939 | }
|
---|
1940 | }
|
---|
1941 | else
|
---|
1942 | {
|
---|
1943 | if (pVCpu->iem.s.uCpl != Desc.Legacy.Gen.u2Dpl)
|
---|
1944 | {
|
---|
1945 | Log(("jmpf %04x:%08RX64 -> CPL != DPL; DPL=%d CPL=%u\n", uSel, offSeg, Desc.Legacy.Gen.u2Dpl, pVCpu->iem.s.uCpl));
|
---|
1946 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1947 | }
|
---|
1948 | if ((uSel & X86_SEL_RPL) > pVCpu->iem.s.uCpl)
|
---|
1949 | {
|
---|
1950 | Log(("jmpf %04x:%08RX64 -> RPL > DPL; RPL=%d CPL=%u\n", uSel, offSeg, (uSel & X86_SEL_RPL), pVCpu->iem.s.uCpl));
|
---|
1951 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1952 | }
|
---|
1953 | }
|
---|
1954 |
|
---|
1955 | /* Chop the high bits if 16-bit (Intel says so). */
|
---|
1956 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
1957 | offSeg &= UINT16_MAX;
|
---|
1958 |
|
---|
1959 | /* Limit check and get the base. */
|
---|
1960 | uint64_t u64Base;
|
---|
1961 | uint32_t cbLimit = X86DESC_LIMIT_G(&Desc.Legacy);
|
---|
1962 | if ( !Desc.Legacy.Gen.u1Long
|
---|
1963 | || !IEM_IS_LONG_MODE(pVCpu))
|
---|
1964 | {
|
---|
1965 | if (RT_LIKELY(offSeg <= cbLimit))
|
---|
1966 | u64Base = X86DESC_BASE(&Desc.Legacy);
|
---|
1967 | else
|
---|
1968 | {
|
---|
1969 | Log(("jmpf %04x:%08RX64 -> out of bounds (%#x)\n", uSel, offSeg, cbLimit));
|
---|
1970 | /** @todo Intel says this is \#GP(0)! */
|
---|
1971 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1972 | }
|
---|
1973 | }
|
---|
1974 | else
|
---|
1975 | u64Base = 0;
|
---|
1976 |
|
---|
1977 | /*
|
---|
1978 | * Ok, everything checked out fine. Now set the accessed bit before
|
---|
1979 | * committing the result into CS, CSHID and RIP.
|
---|
1980 | */
|
---|
1981 | if (!(Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
1982 | {
|
---|
1983 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uSel);
|
---|
1984 | if (rcStrict != VINF_SUCCESS)
|
---|
1985 | return rcStrict;
|
---|
1986 | /** @todo check what VT-x and AMD-V does. */
|
---|
1987 | Desc.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
1988 | }
|
---|
1989 |
|
---|
1990 | /* commit */
|
---|
1991 | pVCpu->cpum.GstCtx.rip = offSeg;
|
---|
1992 | pVCpu->cpum.GstCtx.cs.Sel = uSel & X86_SEL_MASK_OFF_RPL;
|
---|
1993 | pVCpu->cpum.GstCtx.cs.Sel |= pVCpu->iem.s.uCpl; /** @todo is this right for conforming segs? or in general? */
|
---|
1994 | pVCpu->cpum.GstCtx.cs.ValidSel = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
1995 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
1996 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&Desc.Legacy);
|
---|
1997 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimit;
|
---|
1998 | pVCpu->cpum.GstCtx.cs.u64Base = u64Base;
|
---|
1999 | pVCpu->iem.s.enmCpuMode = iemCalcCpuMode(pVCpu);
|
---|
2000 | /** @todo check if the hidden bits are loaded correctly for 64-bit
|
---|
2001 | * mode. */
|
---|
2002 |
|
---|
2003 | /* Flush the prefetch buffer. */
|
---|
2004 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr);
|
---|
2005 |
|
---|
2006 | return iemRegFinishClearingRF(pVCpu);
|
---|
2007 | }
|
---|
2008 |
|
---|
2009 |
|
---|
2010 | /**
|
---|
2011 | * Implements far calls.
|
---|
2012 | *
|
---|
2013 | * This very similar to iemCImpl_FarJmp.
|
---|
2014 | *
|
---|
2015 | * @param uSel The selector.
|
---|
2016 | * @param offSeg The segment offset.
|
---|
2017 | * @param enmEffOpSize The operand size (in case we need it).
|
---|
2018 | */
|
---|
2019 | IEM_CIMPL_DEF_3(iemCImpl_callf, uint16_t, uSel, uint64_t, offSeg, IEMMODE, enmEffOpSize)
|
---|
2020 | {
|
---|
2021 | VBOXSTRICTRC rcStrict;
|
---|
2022 | uint64_t uNewRsp;
|
---|
2023 | RTPTRUNION uPtrRet;
|
---|
2024 |
|
---|
2025 | /*
|
---|
2026 | * Real mode and V8086 mode are easy. The only snag seems to be that
|
---|
2027 | * CS.limit doesn't change and the limit check is done against the current
|
---|
2028 | * limit.
|
---|
2029 | */
|
---|
2030 | /** @todo See comment for similar code in iemCImpl_FarJmp */
|
---|
2031 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
2032 | {
|
---|
2033 | Assert(enmEffOpSize == IEMMODE_16BIT || enmEffOpSize == IEMMODE_32BIT);
|
---|
2034 |
|
---|
2035 | /* Check stack first - may #SS(0). */
|
---|
2036 | rcStrict = iemMemStackPushBeginSpecial(pVCpu, enmEffOpSize == IEMMODE_32BIT ? 4+4 : 2+2,
|
---|
2037 | enmEffOpSize == IEMMODE_32BIT ? 3 : 1,
|
---|
2038 | &uPtrRet.pv, &uNewRsp);
|
---|
2039 | if (rcStrict != VINF_SUCCESS)
|
---|
2040 | return rcStrict;
|
---|
2041 |
|
---|
2042 | /* Check the target address range. */
|
---|
2043 | /** @todo this must be wrong! Write unreal mode tests! */
|
---|
2044 | if (offSeg > UINT32_MAX)
|
---|
2045 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
2046 |
|
---|
2047 | /* Everything is fine, push the return address. */
|
---|
2048 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
2049 | {
|
---|
2050 | uPtrRet.pu16[0] = pVCpu->cpum.GstCtx.ip + cbInstr;
|
---|
2051 | uPtrRet.pu16[1] = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
2052 | }
|
---|
2053 | else
|
---|
2054 | {
|
---|
2055 | uPtrRet.pu32[0] = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
2056 | uPtrRet.pu16[2] = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
2057 | }
|
---|
2058 | rcStrict = iemMemStackPushCommitSpecial(pVCpu, uPtrRet.pv, uNewRsp);
|
---|
2059 | if (rcStrict != VINF_SUCCESS)
|
---|
2060 | return rcStrict;
|
---|
2061 |
|
---|
2062 | /* Branch. */
|
---|
2063 | pVCpu->cpum.GstCtx.rip = offSeg;
|
---|
2064 | pVCpu->cpum.GstCtx.cs.Sel = uSel;
|
---|
2065 | pVCpu->cpum.GstCtx.cs.ValidSel = uSel;
|
---|
2066 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
2067 | pVCpu->cpum.GstCtx.cs.u64Base = (uint32_t)uSel << 4;
|
---|
2068 |
|
---|
2069 | return iemRegFinishClearingRF(pVCpu);
|
---|
2070 | }
|
---|
2071 |
|
---|
2072 | /*
|
---|
2073 | * Protected mode. Need to parse the specified descriptor...
|
---|
2074 | */
|
---|
2075 | if (!(uSel & X86_SEL_MASK_OFF_RPL))
|
---|
2076 | {
|
---|
2077 | Log(("callf %04x:%08RX64 -> invalid selector, #GP(0)\n", uSel, offSeg));
|
---|
2078 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
2079 | }
|
---|
2080 |
|
---|
2081 | /* Fetch the descriptor. */
|
---|
2082 | IEMSELDESC Desc;
|
---|
2083 | rcStrict = iemMemFetchSelDesc(pVCpu, &Desc, uSel, X86_XCPT_GP);
|
---|
2084 | if (rcStrict != VINF_SUCCESS)
|
---|
2085 | return rcStrict;
|
---|
2086 |
|
---|
2087 | /*
|
---|
2088 | * Deal with it according to its type. We do the standard code selectors
|
---|
2089 | * here and dispatch the system selectors to worker functions.
|
---|
2090 | */
|
---|
2091 | if (!Desc.Legacy.Gen.u1DescType)
|
---|
2092 | return iemCImpl_BranchSysSel(pVCpu, cbInstr, uSel, IEMBRANCH_CALL, enmEffOpSize, &Desc);
|
---|
2093 |
|
---|
2094 | /* Only code segments. */
|
---|
2095 | if (!(Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE))
|
---|
2096 | {
|
---|
2097 | Log(("callf %04x:%08RX64 -> not a code selector (u4Type=%#x).\n", uSel, offSeg, Desc.Legacy.Gen.u4Type));
|
---|
2098 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
2099 | }
|
---|
2100 |
|
---|
2101 | /* L vs D. */
|
---|
2102 | if ( Desc.Legacy.Gen.u1Long
|
---|
2103 | && Desc.Legacy.Gen.u1DefBig
|
---|
2104 | && IEM_IS_LONG_MODE(pVCpu))
|
---|
2105 | {
|
---|
2106 | Log(("callf %04x:%08RX64 -> both L and D are set.\n", uSel, offSeg));
|
---|
2107 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
2108 | }
|
---|
2109 |
|
---|
2110 | /* DPL/RPL/CPL check, where conforming segments makes a difference. */
|
---|
2111 | if (Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF)
|
---|
2112 | {
|
---|
2113 | if (pVCpu->iem.s.uCpl < Desc.Legacy.Gen.u2Dpl)
|
---|
2114 | {
|
---|
2115 | Log(("callf %04x:%08RX64 -> DPL violation (conforming); DPL=%d CPL=%u\n",
|
---|
2116 | uSel, offSeg, Desc.Legacy.Gen.u2Dpl, pVCpu->iem.s.uCpl));
|
---|
2117 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
2118 | }
|
---|
2119 | }
|
---|
2120 | else
|
---|
2121 | {
|
---|
2122 | if (pVCpu->iem.s.uCpl != Desc.Legacy.Gen.u2Dpl)
|
---|
2123 | {
|
---|
2124 | Log(("callf %04x:%08RX64 -> CPL != DPL; DPL=%d CPL=%u\n", uSel, offSeg, Desc.Legacy.Gen.u2Dpl, pVCpu->iem.s.uCpl));
|
---|
2125 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
2126 | }
|
---|
2127 | if ((uSel & X86_SEL_RPL) > pVCpu->iem.s.uCpl)
|
---|
2128 | {
|
---|
2129 | Log(("callf %04x:%08RX64 -> RPL > DPL; RPL=%d CPL=%u\n", uSel, offSeg, (uSel & X86_SEL_RPL), pVCpu->iem.s.uCpl));
|
---|
2130 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
2131 | }
|
---|
2132 | }
|
---|
2133 |
|
---|
2134 | /* Is it there? */
|
---|
2135 | if (!Desc.Legacy.Gen.u1Present)
|
---|
2136 | {
|
---|
2137 | Log(("callf %04x:%08RX64 -> segment not present\n", uSel, offSeg));
|
---|
2138 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uSel);
|
---|
2139 | }
|
---|
2140 |
|
---|
2141 | /* Check stack first - may #SS(0). */
|
---|
2142 | /** @todo check how operand prefix affects pushing of CS! Does callf 16:32 in
|
---|
2143 | * 16-bit code cause a two or four byte CS to be pushed? */
|
---|
2144 | rcStrict = iemMemStackPushBeginSpecial(pVCpu,
|
---|
2145 | enmEffOpSize == IEMMODE_64BIT ? 8+8 : enmEffOpSize == IEMMODE_32BIT ? 4+4 : 2+2,
|
---|
2146 | enmEffOpSize == IEMMODE_64BIT ? 7 : enmEffOpSize == IEMMODE_32BIT ? 3 : 1,
|
---|
2147 | &uPtrRet.pv, &uNewRsp);
|
---|
2148 | if (rcStrict != VINF_SUCCESS)
|
---|
2149 | return rcStrict;
|
---|
2150 |
|
---|
2151 | /* Chop the high bits if 16-bit (Intel says so). */
|
---|
2152 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
2153 | offSeg &= UINT16_MAX;
|
---|
2154 |
|
---|
2155 | /* Limit / canonical check. */
|
---|
2156 | uint64_t u64Base;
|
---|
2157 | uint32_t cbLimit = X86DESC_LIMIT_G(&Desc.Legacy);
|
---|
2158 | if ( !Desc.Legacy.Gen.u1Long
|
---|
2159 | || !IEM_IS_LONG_MODE(pVCpu))
|
---|
2160 | {
|
---|
2161 | if (RT_LIKELY(offSeg <= cbLimit))
|
---|
2162 | u64Base = X86DESC_BASE(&Desc.Legacy);
|
---|
2163 | else
|
---|
2164 | {
|
---|
2165 | Log(("jmpf %04x:%08RX64 -> out of bounds (%#x)\n", uSel, offSeg, cbLimit));
|
---|
2166 | /** @todo Intel says this is \#GP(0)! */
|
---|
2167 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
2168 | }
|
---|
2169 | }
|
---|
2170 | else if (IEM_IS_CANONICAL(offSeg))
|
---|
2171 | u64Base = 0;
|
---|
2172 | else
|
---|
2173 | {
|
---|
2174 | Log(("callf %04x:%016RX64 - not canonical -> #GP\n", uSel, offSeg));
|
---|
2175 | return iemRaiseNotCanonical(pVCpu);
|
---|
2176 | }
|
---|
2177 |
|
---|
2178 | /*
|
---|
2179 | * Now set the accessed bit before
|
---|
2180 | * writing the return address to the stack and committing the result into
|
---|
2181 | * CS, CSHID and RIP.
|
---|
2182 | */
|
---|
2183 | /** @todo Testcase: Need to check WHEN exactly the accessed bit is set. */
|
---|
2184 | if (!(Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
2185 | {
|
---|
2186 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uSel);
|
---|
2187 | if (rcStrict != VINF_SUCCESS)
|
---|
2188 | return rcStrict;
|
---|
2189 | /** @todo check what VT-x and AMD-V does. */
|
---|
2190 | Desc.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
2191 | }
|
---|
2192 |
|
---|
2193 | /* stack */
|
---|
2194 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
2195 | {
|
---|
2196 | uPtrRet.pu16[0] = pVCpu->cpum.GstCtx.ip + cbInstr;
|
---|
2197 | uPtrRet.pu16[1] = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
2198 | }
|
---|
2199 | else if (enmEffOpSize == IEMMODE_32BIT)
|
---|
2200 | {
|
---|
2201 | uPtrRet.pu32[0] = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
2202 | uPtrRet.pu32[1] = pVCpu->cpum.GstCtx.cs.Sel; /** @todo Testcase: What is written to the high word when callf is pushing CS? */
|
---|
2203 | }
|
---|
2204 | else
|
---|
2205 | {
|
---|
2206 | uPtrRet.pu64[0] = pVCpu->cpum.GstCtx.rip + cbInstr;
|
---|
2207 | uPtrRet.pu64[1] = pVCpu->cpum.GstCtx.cs.Sel; /** @todo Testcase: What is written to the high words when callf is pushing CS? */
|
---|
2208 | }
|
---|
2209 | rcStrict = iemMemStackPushCommitSpecial(pVCpu, uPtrRet.pv, uNewRsp);
|
---|
2210 | if (rcStrict != VINF_SUCCESS)
|
---|
2211 | return rcStrict;
|
---|
2212 |
|
---|
2213 | /* commit */
|
---|
2214 | pVCpu->cpum.GstCtx.rip = offSeg;
|
---|
2215 | pVCpu->cpum.GstCtx.cs.Sel = uSel & X86_SEL_MASK_OFF_RPL;
|
---|
2216 | pVCpu->cpum.GstCtx.cs.Sel |= pVCpu->iem.s.uCpl;
|
---|
2217 | pVCpu->cpum.GstCtx.cs.ValidSel = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
2218 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
2219 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&Desc.Legacy);
|
---|
2220 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimit;
|
---|
2221 | pVCpu->cpum.GstCtx.cs.u64Base = u64Base;
|
---|
2222 | pVCpu->iem.s.enmCpuMode = iemCalcCpuMode(pVCpu);
|
---|
2223 | /** @todo check if the hidden bits are loaded correctly for 64-bit
|
---|
2224 | * mode. */
|
---|
2225 |
|
---|
2226 | /* Flush the prefetch buffer. */
|
---|
2227 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr);
|
---|
2228 |
|
---|
2229 | return iemRegFinishClearingRF(pVCpu);
|
---|
2230 | }
|
---|
2231 |
|
---|
2232 |
|
---|
2233 | /**
|
---|
2234 | * Implements retf.
|
---|
2235 | *
|
---|
2236 | * @param enmEffOpSize The effective operand size.
|
---|
2237 | * @param cbPop The amount of arguments to pop from the stack
|
---|
2238 | * (bytes).
|
---|
2239 | */
|
---|
2240 | IEM_CIMPL_DEF_2(iemCImpl_retf, IEMMODE, enmEffOpSize, uint16_t, cbPop)
|
---|
2241 | {
|
---|
2242 | VBOXSTRICTRC rcStrict;
|
---|
2243 | RTCPTRUNION uPtrFrame;
|
---|
2244 | RTUINT64U NewRsp;
|
---|
2245 | uint64_t uNewRip;
|
---|
2246 | uint16_t uNewCs;
|
---|
2247 | NOREF(cbInstr);
|
---|
2248 |
|
---|
2249 | /*
|
---|
2250 | * Read the stack values first.
|
---|
2251 | */
|
---|
2252 | uint32_t cbRetPtr = enmEffOpSize == IEMMODE_16BIT ? 2+2
|
---|
2253 | : enmEffOpSize == IEMMODE_32BIT ? 4+4 : 8+8;
|
---|
2254 | rcStrict = iemMemStackPopBeginSpecial(pVCpu, cbRetPtr,
|
---|
2255 | enmEffOpSize == IEMMODE_16BIT ? 1 : enmEffOpSize == IEMMODE_32BIT ? 3 : 7,
|
---|
2256 | &uPtrFrame.pv, &NewRsp.u);
|
---|
2257 | if (rcStrict != VINF_SUCCESS)
|
---|
2258 | return rcStrict;
|
---|
2259 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
2260 | {
|
---|
2261 | uNewRip = uPtrFrame.pu16[0];
|
---|
2262 | uNewCs = uPtrFrame.pu16[1];
|
---|
2263 | }
|
---|
2264 | else if (enmEffOpSize == IEMMODE_32BIT)
|
---|
2265 | {
|
---|
2266 | uNewRip = uPtrFrame.pu32[0];
|
---|
2267 | uNewCs = uPtrFrame.pu16[2];
|
---|
2268 | }
|
---|
2269 | else
|
---|
2270 | {
|
---|
2271 | uNewRip = uPtrFrame.pu64[0];
|
---|
2272 | uNewCs = uPtrFrame.pu16[4];
|
---|
2273 | }
|
---|
2274 | rcStrict = iemMemStackPopDoneSpecial(pVCpu, uPtrFrame.pv);
|
---|
2275 | if (RT_LIKELY(rcStrict == VINF_SUCCESS))
|
---|
2276 | { /* extremely likely */ }
|
---|
2277 | else
|
---|
2278 | return rcStrict;
|
---|
2279 |
|
---|
2280 | /*
|
---|
2281 | * Real mode and V8086 mode are easy.
|
---|
2282 | */
|
---|
2283 | /** @todo See comment for similar code in iemCImpl_FarJmp */
|
---|
2284 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
2285 | {
|
---|
2286 | Assert(enmEffOpSize == IEMMODE_32BIT || enmEffOpSize == IEMMODE_16BIT);
|
---|
2287 | /** @todo check how this is supposed to work if sp=0xfffe. */
|
---|
2288 |
|
---|
2289 | /* Check the limit of the new EIP. */
|
---|
2290 | /** @todo Intel pseudo code only does the limit check for 16-bit
|
---|
2291 | * operands, AMD does not make any distinction. What is right? */
|
---|
2292 | if (uNewRip > pVCpu->cpum.GstCtx.cs.u32Limit)
|
---|
2293 | return iemRaiseSelectorBounds(pVCpu, X86_SREG_CS, IEM_ACCESS_INSTRUCTION);
|
---|
2294 |
|
---|
2295 | /* commit the operation. */
|
---|
2296 | if (cbPop)
|
---|
2297 | iemRegAddToRspEx(pVCpu, &NewRsp, cbPop);
|
---|
2298 | pVCpu->cpum.GstCtx.rsp = NewRsp.u;
|
---|
2299 | pVCpu->cpum.GstCtx.rip = uNewRip;
|
---|
2300 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
|
---|
2301 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
|
---|
2302 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
2303 | pVCpu->cpum.GstCtx.cs.u64Base = (uint32_t)uNewCs << 4;
|
---|
2304 | return iemRegFinishClearingRF(pVCpu);
|
---|
2305 | }
|
---|
2306 |
|
---|
2307 | /*
|
---|
2308 | * Protected mode is complicated, of course.
|
---|
2309 | */
|
---|
2310 | if (!(uNewCs & X86_SEL_MASK_OFF_RPL))
|
---|
2311 | {
|
---|
2312 | Log(("retf %04x:%08RX64 -> invalid selector, #GP(0)\n", uNewCs, uNewRip));
|
---|
2313 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
2314 | }
|
---|
2315 |
|
---|
2316 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SREG_MASK | CPUMCTX_EXTRN_GDTR | CPUMCTX_EXTRN_LDTR);
|
---|
2317 |
|
---|
2318 | /* Fetch the descriptor. */
|
---|
2319 | IEMSELDESC DescCs;
|
---|
2320 | rcStrict = iemMemFetchSelDesc(pVCpu, &DescCs, uNewCs, X86_XCPT_GP);
|
---|
2321 | if (rcStrict != VINF_SUCCESS)
|
---|
2322 | return rcStrict;
|
---|
2323 |
|
---|
2324 | /* Can only return to a code selector. */
|
---|
2325 | if ( !DescCs.Legacy.Gen.u1DescType
|
---|
2326 | || !(DescCs.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE) )
|
---|
2327 | {
|
---|
2328 | Log(("retf %04x:%08RX64 -> not a code selector (u1DescType=%u u4Type=%#x).\n",
|
---|
2329 | uNewCs, uNewRip, DescCs.Legacy.Gen.u1DescType, DescCs.Legacy.Gen.u4Type));
|
---|
2330 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
2331 | }
|
---|
2332 |
|
---|
2333 | /* L vs D. */
|
---|
2334 | if ( DescCs.Legacy.Gen.u1Long /** @todo Testcase: far return to a selector with both L and D set. */
|
---|
2335 | && DescCs.Legacy.Gen.u1DefBig
|
---|
2336 | && IEM_IS_LONG_MODE(pVCpu))
|
---|
2337 | {
|
---|
2338 | Log(("retf %04x:%08RX64 -> both L & D set.\n", uNewCs, uNewRip));
|
---|
2339 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
2340 | }
|
---|
2341 |
|
---|
2342 | /* DPL/RPL/CPL checks. */
|
---|
2343 | if ((uNewCs & X86_SEL_RPL) < pVCpu->iem.s.uCpl)
|
---|
2344 | {
|
---|
2345 | Log(("retf %04x:%08RX64 -> RPL < CPL(%d).\n", uNewCs, uNewRip, pVCpu->iem.s.uCpl));
|
---|
2346 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
2347 | }
|
---|
2348 |
|
---|
2349 | if (DescCs.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF)
|
---|
2350 | {
|
---|
2351 | if ((uNewCs & X86_SEL_RPL) < DescCs.Legacy.Gen.u2Dpl)
|
---|
2352 | {
|
---|
2353 | Log(("retf %04x:%08RX64 -> DPL violation (conforming); DPL=%u RPL=%u\n",
|
---|
2354 | uNewCs, uNewRip, DescCs.Legacy.Gen.u2Dpl, (uNewCs & X86_SEL_RPL)));
|
---|
2355 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
2356 | }
|
---|
2357 | }
|
---|
2358 | else
|
---|
2359 | {
|
---|
2360 | if ((uNewCs & X86_SEL_RPL) != DescCs.Legacy.Gen.u2Dpl)
|
---|
2361 | {
|
---|
2362 | Log(("retf %04x:%08RX64 -> RPL != DPL; DPL=%u RPL=%u\n",
|
---|
2363 | uNewCs, uNewRip, DescCs.Legacy.Gen.u2Dpl, (uNewCs & X86_SEL_RPL)));
|
---|
2364 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
2365 | }
|
---|
2366 | }
|
---|
2367 |
|
---|
2368 | /* Is it there? */
|
---|
2369 | if (!DescCs.Legacy.Gen.u1Present)
|
---|
2370 | {
|
---|
2371 | Log(("retf %04x:%08RX64 -> segment not present\n", uNewCs, uNewRip));
|
---|
2372 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uNewCs);
|
---|
2373 | }
|
---|
2374 |
|
---|
2375 | /*
|
---|
2376 | * Return to outer privilege? (We'll typically have entered via a call gate.)
|
---|
2377 | */
|
---|
2378 | if ((uNewCs & X86_SEL_RPL) != pVCpu->iem.s.uCpl)
|
---|
2379 | {
|
---|
2380 | /* Read the outer stack pointer stored *after* the parameters. */
|
---|
2381 | rcStrict = iemMemStackPopContinueSpecial(pVCpu, cbPop /*off*/, cbRetPtr, &uPtrFrame.pv, NewRsp.u);
|
---|
2382 | if (rcStrict != VINF_SUCCESS)
|
---|
2383 | return rcStrict;
|
---|
2384 |
|
---|
2385 | uint16_t uNewOuterSs;
|
---|
2386 | RTUINT64U NewOuterRsp;
|
---|
2387 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
2388 | {
|
---|
2389 | NewOuterRsp.u = uPtrFrame.pu16[0];
|
---|
2390 | uNewOuterSs = uPtrFrame.pu16[1];
|
---|
2391 | }
|
---|
2392 | else if (enmEffOpSize == IEMMODE_32BIT)
|
---|
2393 | {
|
---|
2394 | NewOuterRsp.u = uPtrFrame.pu32[0];
|
---|
2395 | uNewOuterSs = uPtrFrame.pu16[2];
|
---|
2396 | }
|
---|
2397 | else
|
---|
2398 | {
|
---|
2399 | NewOuterRsp.u = uPtrFrame.pu64[0];
|
---|
2400 | uNewOuterSs = uPtrFrame.pu16[4];
|
---|
2401 | }
|
---|
2402 | rcStrict = iemMemStackPopDoneSpecial(pVCpu, uPtrFrame.pv);
|
---|
2403 | if (RT_LIKELY(rcStrict == VINF_SUCCESS))
|
---|
2404 | { /* extremely likely */ }
|
---|
2405 | else
|
---|
2406 | return rcStrict;
|
---|
2407 |
|
---|
2408 | /* Check for NULL stack selector (invalid in ring-3 and non-long mode)
|
---|
2409 | and read the selector. */
|
---|
2410 | IEMSELDESC DescSs;
|
---|
2411 | if (!(uNewOuterSs & X86_SEL_MASK_OFF_RPL))
|
---|
2412 | {
|
---|
2413 | if ( !DescCs.Legacy.Gen.u1Long
|
---|
2414 | || (uNewOuterSs & X86_SEL_RPL) == 3)
|
---|
2415 | {
|
---|
2416 | Log(("retf %04x:%08RX64 %04x:%08RX64 -> invalid stack selector, #GP\n",
|
---|
2417 | uNewCs, uNewRip, uNewOuterSs, NewOuterRsp.u));
|
---|
2418 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
2419 | }
|
---|
2420 | /** @todo Testcase: Return far to ring-1 or ring-2 with SS=0. */
|
---|
2421 | iemMemFakeStackSelDesc(&DescSs, (uNewOuterSs & X86_SEL_RPL));
|
---|
2422 | }
|
---|
2423 | else
|
---|
2424 | {
|
---|
2425 | /* Fetch the descriptor for the new stack segment. */
|
---|
2426 | rcStrict = iemMemFetchSelDesc(pVCpu, &DescSs, uNewOuterSs, X86_XCPT_GP);
|
---|
2427 | if (rcStrict != VINF_SUCCESS)
|
---|
2428 | return rcStrict;
|
---|
2429 | }
|
---|
2430 |
|
---|
2431 | /* Check that RPL of stack and code selectors match. */
|
---|
2432 | if ((uNewCs & X86_SEL_RPL) != (uNewOuterSs & X86_SEL_RPL))
|
---|
2433 | {
|
---|
2434 | Log(("retf %04x:%08RX64 %04x:%08RX64 - SS.RPL != CS.RPL -> #GP(SS)\n", uNewCs, uNewRip, uNewOuterSs, NewOuterRsp.u));
|
---|
2435 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewOuterSs);
|
---|
2436 | }
|
---|
2437 |
|
---|
2438 | /* Must be a writable data segment. */
|
---|
2439 | if ( !DescSs.Legacy.Gen.u1DescType
|
---|
2440 | || (DescSs.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE)
|
---|
2441 | || !(DescSs.Legacy.Gen.u4Type & X86_SEL_TYPE_WRITE) )
|
---|
2442 | {
|
---|
2443 | Log(("retf %04x:%08RX64 %04x:%08RX64 - SS not a writable data segment (u1DescType=%u u4Type=%#x) -> #GP(SS).\n",
|
---|
2444 | uNewCs, uNewRip, uNewOuterSs, NewOuterRsp.u, DescSs.Legacy.Gen.u1DescType, DescSs.Legacy.Gen.u4Type));
|
---|
2445 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewOuterSs);
|
---|
2446 | }
|
---|
2447 |
|
---|
2448 | /* L vs D. (Not mentioned by intel.) */
|
---|
2449 | if ( DescSs.Legacy.Gen.u1Long /** @todo Testcase: far return to a stack selector with both L and D set. */
|
---|
2450 | && DescSs.Legacy.Gen.u1DefBig
|
---|
2451 | && IEM_IS_LONG_MODE(pVCpu))
|
---|
2452 | {
|
---|
2453 | Log(("retf %04x:%08RX64 %04x:%08RX64 - SS has both L & D set -> #GP(SS).\n",
|
---|
2454 | uNewCs, uNewRip, uNewOuterSs, NewOuterRsp.u));
|
---|
2455 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewOuterSs);
|
---|
2456 | }
|
---|
2457 |
|
---|
2458 | /* DPL/RPL/CPL checks. */
|
---|
2459 | if (DescSs.Legacy.Gen.u2Dpl != (uNewCs & X86_SEL_RPL))
|
---|
2460 | {
|
---|
2461 | Log(("retf %04x:%08RX64 %04x:%08RX64 - SS.DPL(%u) != CS.RPL (%u) -> #GP(SS).\n",
|
---|
2462 | uNewCs, uNewRip, uNewOuterSs, NewOuterRsp.u, DescSs.Legacy.Gen.u2Dpl, uNewCs & X86_SEL_RPL));
|
---|
2463 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewOuterSs);
|
---|
2464 | }
|
---|
2465 |
|
---|
2466 | /* Is it there? */
|
---|
2467 | if (!DescSs.Legacy.Gen.u1Present)
|
---|
2468 | {
|
---|
2469 | Log(("retf %04x:%08RX64 %04x:%08RX64 - SS not present -> #NP(SS).\n", uNewCs, uNewRip, uNewOuterSs, NewOuterRsp.u));
|
---|
2470 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uNewCs);
|
---|
2471 | }
|
---|
2472 |
|
---|
2473 | /* Calc SS limit.*/
|
---|
2474 | uint32_t cbLimitSs = X86DESC_LIMIT_G(&DescSs.Legacy);
|
---|
2475 |
|
---|
2476 | /* Is RIP canonical or within CS.limit? */
|
---|
2477 | uint64_t u64Base;
|
---|
2478 | uint32_t cbLimitCs = X86DESC_LIMIT_G(&DescCs.Legacy);
|
---|
2479 |
|
---|
2480 | /** @todo Testcase: Is this correct? */
|
---|
2481 | if ( DescCs.Legacy.Gen.u1Long
|
---|
2482 | && IEM_IS_LONG_MODE(pVCpu) )
|
---|
2483 | {
|
---|
2484 | if (!IEM_IS_CANONICAL(uNewRip))
|
---|
2485 | {
|
---|
2486 | Log(("retf %04x:%08RX64 %04x:%08RX64 - not canonical -> #GP.\n", uNewCs, uNewRip, uNewOuterSs, NewOuterRsp.u));
|
---|
2487 | return iemRaiseNotCanonical(pVCpu);
|
---|
2488 | }
|
---|
2489 | u64Base = 0;
|
---|
2490 | }
|
---|
2491 | else
|
---|
2492 | {
|
---|
2493 | if (uNewRip > cbLimitCs)
|
---|
2494 | {
|
---|
2495 | Log(("retf %04x:%08RX64 %04x:%08RX64 - out of bounds (%#x)-> #GP(CS).\n",
|
---|
2496 | uNewCs, uNewRip, uNewOuterSs, NewOuterRsp.u, cbLimitCs));
|
---|
2497 | /** @todo Intel says this is \#GP(0)! */
|
---|
2498 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
2499 | }
|
---|
2500 | u64Base = X86DESC_BASE(&DescCs.Legacy);
|
---|
2501 | }
|
---|
2502 |
|
---|
2503 | /*
|
---|
2504 | * Now set the accessed bit before
|
---|
2505 | * writing the return address to the stack and committing the result into
|
---|
2506 | * CS, CSHID and RIP.
|
---|
2507 | */
|
---|
2508 | /** @todo Testcase: Need to check WHEN exactly the CS accessed bit is set. */
|
---|
2509 | if (!(DescCs.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
2510 | {
|
---|
2511 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewCs);
|
---|
2512 | if (rcStrict != VINF_SUCCESS)
|
---|
2513 | return rcStrict;
|
---|
2514 | /** @todo check what VT-x and AMD-V does. */
|
---|
2515 | DescCs.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
2516 | }
|
---|
2517 | /** @todo Testcase: Need to check WHEN exactly the SS accessed bit is set. */
|
---|
2518 | if (!(DescSs.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
2519 | {
|
---|
2520 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewOuterSs);
|
---|
2521 | if (rcStrict != VINF_SUCCESS)
|
---|
2522 | return rcStrict;
|
---|
2523 | /** @todo check what VT-x and AMD-V does. */
|
---|
2524 | DescSs.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
2525 | }
|
---|
2526 |
|
---|
2527 | /* commit */
|
---|
2528 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
2529 | pVCpu->cpum.GstCtx.rip = uNewRip & UINT16_MAX; /** @todo Testcase: When exactly does this occur? With call it happens prior to the limit check according to Intel... */
|
---|
2530 | else
|
---|
2531 | pVCpu->cpum.GstCtx.rip = uNewRip;
|
---|
2532 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
|
---|
2533 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
|
---|
2534 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
2535 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCs.Legacy);
|
---|
2536 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimitCs;
|
---|
2537 | pVCpu->cpum.GstCtx.cs.u64Base = u64Base;
|
---|
2538 | pVCpu->iem.s.enmCpuMode = iemCalcCpuMode(pVCpu);
|
---|
2539 | pVCpu->cpum.GstCtx.ss.Sel = uNewOuterSs;
|
---|
2540 | pVCpu->cpum.GstCtx.ss.ValidSel = uNewOuterSs;
|
---|
2541 | pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
2542 | pVCpu->cpum.GstCtx.ss.Attr.u = X86DESC_GET_HID_ATTR(&DescSs.Legacy);
|
---|
2543 | pVCpu->cpum.GstCtx.ss.u32Limit = cbLimitSs;
|
---|
2544 | if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
|
---|
2545 | pVCpu->cpum.GstCtx.ss.u64Base = 0;
|
---|
2546 | else
|
---|
2547 | pVCpu->cpum.GstCtx.ss.u64Base = X86DESC_BASE(&DescSs.Legacy);
|
---|
2548 | if (cbPop)
|
---|
2549 | iemRegAddToRspEx(pVCpu, &NewOuterRsp, cbPop);
|
---|
2550 | if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
|
---|
2551 | pVCpu->cpum.GstCtx.rsp = NewOuterRsp.u;
|
---|
2552 | else if (pVCpu->cpum.GstCtx.ss.Attr.n.u1DefBig)
|
---|
2553 | pVCpu->cpum.GstCtx.rsp = (uint32_t)NewOuterRsp.u;
|
---|
2554 | else
|
---|
2555 | pVCpu->cpum.GstCtx.sp = (uint16_t)NewOuterRsp.u;
|
---|
2556 |
|
---|
2557 | pVCpu->iem.s.uCpl = (uNewCs & X86_SEL_RPL);
|
---|
2558 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.ds);
|
---|
2559 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.es);
|
---|
2560 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.fs);
|
---|
2561 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.gs);
|
---|
2562 |
|
---|
2563 | /** @todo check if the hidden bits are loaded correctly for 64-bit
|
---|
2564 | * mode. */
|
---|
2565 | }
|
---|
2566 | /*
|
---|
2567 | * Return to the same privilege level
|
---|
2568 | */
|
---|
2569 | else
|
---|
2570 | {
|
---|
2571 | /* Limit / canonical check. */
|
---|
2572 | uint64_t u64Base;
|
---|
2573 | uint32_t cbLimitCs = X86DESC_LIMIT_G(&DescCs.Legacy);
|
---|
2574 |
|
---|
2575 | /** @todo Testcase: Is this correct? */
|
---|
2576 | if ( DescCs.Legacy.Gen.u1Long
|
---|
2577 | && IEM_IS_LONG_MODE(pVCpu) )
|
---|
2578 | {
|
---|
2579 | if (!IEM_IS_CANONICAL(uNewRip))
|
---|
2580 | {
|
---|
2581 | Log(("retf %04x:%08RX64 - not canonical -> #GP\n", uNewCs, uNewRip));
|
---|
2582 | return iemRaiseNotCanonical(pVCpu);
|
---|
2583 | }
|
---|
2584 | u64Base = 0;
|
---|
2585 | }
|
---|
2586 | else
|
---|
2587 | {
|
---|
2588 | if (uNewRip > cbLimitCs)
|
---|
2589 | {
|
---|
2590 | Log(("retf %04x:%08RX64 -> out of bounds (%#x)\n", uNewCs, uNewRip, cbLimitCs));
|
---|
2591 | /** @todo Intel says this is \#GP(0)! */
|
---|
2592 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
2593 | }
|
---|
2594 | u64Base = X86DESC_BASE(&DescCs.Legacy);
|
---|
2595 | }
|
---|
2596 |
|
---|
2597 | /*
|
---|
2598 | * Now set the accessed bit before
|
---|
2599 | * writing the return address to the stack and committing the result into
|
---|
2600 | * CS, CSHID and RIP.
|
---|
2601 | */
|
---|
2602 | /** @todo Testcase: Need to check WHEN exactly the accessed bit is set. */
|
---|
2603 | if (!(DescCs.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
2604 | {
|
---|
2605 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewCs);
|
---|
2606 | if (rcStrict != VINF_SUCCESS)
|
---|
2607 | return rcStrict;
|
---|
2608 | /** @todo check what VT-x and AMD-V does. */
|
---|
2609 | DescCs.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
2610 | }
|
---|
2611 |
|
---|
2612 | /* commit */
|
---|
2613 | if (cbPop)
|
---|
2614 | iemRegAddToRspEx(pVCpu, &NewRsp, cbPop);
|
---|
2615 | if (!pVCpu->cpum.GstCtx.ss.Attr.n.u1DefBig)
|
---|
2616 | pVCpu->cpum.GstCtx.sp = (uint16_t)NewRsp.u;
|
---|
2617 | else
|
---|
2618 | pVCpu->cpum.GstCtx.rsp = NewRsp.u;
|
---|
2619 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
2620 | pVCpu->cpum.GstCtx.rip = uNewRip & UINT16_MAX; /** @todo Testcase: When exactly does this occur? With call it happens prior to the limit check according to Intel... */
|
---|
2621 | else
|
---|
2622 | pVCpu->cpum.GstCtx.rip = uNewRip;
|
---|
2623 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
|
---|
2624 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
|
---|
2625 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
2626 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCs.Legacy);
|
---|
2627 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimitCs;
|
---|
2628 | pVCpu->cpum.GstCtx.cs.u64Base = u64Base;
|
---|
2629 | /** @todo check if the hidden bits are loaded correctly for 64-bit
|
---|
2630 | * mode. */
|
---|
2631 | pVCpu->iem.s.enmCpuMode = iemCalcCpuMode(pVCpu);
|
---|
2632 | }
|
---|
2633 |
|
---|
2634 | /* Flush the prefetch buffer. */
|
---|
2635 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr); /** @todo use light flush for same privlege? */
|
---|
2636 |
|
---|
2637 | return iemRegFinishClearingRF(pVCpu);
|
---|
2638 | }
|
---|
2639 |
|
---|
2640 |
|
---|
2641 | /**
|
---|
2642 | * Implements retn and retn imm16.
|
---|
2643 | *
|
---|
2644 | * We're doing this in C because of the \#GP that might be raised if the popped
|
---|
2645 | * program counter is out of bounds.
|
---|
2646 | *
|
---|
2647 | * The hope with this forced inline worker function, is that the compiler will
|
---|
2648 | * be clever enough to eliminate unused code for the constant enmEffOpSize and
|
---|
2649 | * maybe cbPop parameters.
|
---|
2650 | *
|
---|
2651 | * @param pVCpu The cross context virtual CPU structure of the
|
---|
2652 | * calling thread.
|
---|
2653 | * @param cbInstr The current instruction length.
|
---|
2654 | * @param enmEffOpSize The effective operand size. This is constant.
|
---|
2655 | * @param cbPop The amount of arguments to pop from the stack
|
---|
2656 | * (bytes). This can be constant (zero).
|
---|
2657 | */
|
---|
2658 | DECL_FORCE_INLINE(VBOXSTRICTRC) iemCImpl_ReturnNearCommon(PVMCPUCC pVCpu, uint8_t cbInstr, IEMMODE enmEffOpSize, uint16_t cbPop)
|
---|
2659 | {
|
---|
2660 | /* Fetch the RSP from the stack. */
|
---|
2661 | VBOXSTRICTRC rcStrict;
|
---|
2662 | RTUINT64U NewRip;
|
---|
2663 | RTUINT64U NewRsp;
|
---|
2664 | NewRsp.u = pVCpu->cpum.GstCtx.rsp;
|
---|
2665 |
|
---|
2666 | switch (enmEffOpSize)
|
---|
2667 | {
|
---|
2668 | case IEMMODE_16BIT:
|
---|
2669 | NewRip.u = 0;
|
---|
2670 | rcStrict = iemMemStackPopU16Ex(pVCpu, &NewRip.Words.w0, &NewRsp);
|
---|
2671 | break;
|
---|
2672 | case IEMMODE_32BIT:
|
---|
2673 | NewRip.u = 0;
|
---|
2674 | rcStrict = iemMemStackPopU32Ex(pVCpu, &NewRip.DWords.dw0, &NewRsp);
|
---|
2675 | break;
|
---|
2676 | case IEMMODE_64BIT:
|
---|
2677 | rcStrict = iemMemStackPopU64Ex(pVCpu, &NewRip.u, &NewRsp);
|
---|
2678 | break;
|
---|
2679 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
2680 | }
|
---|
2681 | if (rcStrict != VINF_SUCCESS)
|
---|
2682 | return rcStrict;
|
---|
2683 |
|
---|
2684 | /* Check the new RSP before loading it. */
|
---|
2685 | /** @todo Should test this as the intel+amd pseudo code doesn't mention half
|
---|
2686 | * of it. The canonical test is performed here and for call. */
|
---|
2687 | if (enmEffOpSize != IEMMODE_64BIT)
|
---|
2688 | {
|
---|
2689 | if (RT_LIKELY(NewRip.DWords.dw0 <= pVCpu->cpum.GstCtx.cs.u32Limit))
|
---|
2690 | { /* likely */ }
|
---|
2691 | else
|
---|
2692 | {
|
---|
2693 | Log(("retn newrip=%llx - out of bounds (%x) -> #GP\n", NewRip.u, pVCpu->cpum.GstCtx.cs.u32Limit));
|
---|
2694 | return iemRaiseSelectorBounds(pVCpu, X86_SREG_CS, IEM_ACCESS_INSTRUCTION);
|
---|
2695 | }
|
---|
2696 | }
|
---|
2697 | else
|
---|
2698 | {
|
---|
2699 | if (RT_LIKELY(IEM_IS_CANONICAL(NewRip.u)))
|
---|
2700 | { /* likely */ }
|
---|
2701 | else
|
---|
2702 | {
|
---|
2703 | Log(("retn newrip=%llx - not canonical -> #GP\n", NewRip.u));
|
---|
2704 | return iemRaiseNotCanonical(pVCpu);
|
---|
2705 | }
|
---|
2706 | }
|
---|
2707 |
|
---|
2708 | /* Apply cbPop */
|
---|
2709 | if (cbPop)
|
---|
2710 | iemRegAddToRspEx(pVCpu, &NewRsp, cbPop);
|
---|
2711 |
|
---|
2712 | /* Commit it. */
|
---|
2713 | pVCpu->cpum.GstCtx.rip = NewRip.u;
|
---|
2714 | pVCpu->cpum.GstCtx.rsp = NewRsp.u;
|
---|
2715 |
|
---|
2716 | /* Flush the prefetch buffer. */
|
---|
2717 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr); /** @todo only need a light flush here, don't we? We don't really need any flushing... */
|
---|
2718 | RT_NOREF(cbInstr);
|
---|
2719 |
|
---|
2720 | return iemRegFinishClearingRF(pVCpu);
|
---|
2721 | }
|
---|
2722 |
|
---|
2723 |
|
---|
2724 | /**
|
---|
2725 | * Implements retn imm16 with 16-bit effective operand size.
|
---|
2726 | *
|
---|
2727 | * @param cbPop The amount of arguments to pop from the stack (bytes).
|
---|
2728 | */
|
---|
2729 | IEM_CIMPL_DEF_1(iemCImpl_retn_iw_16, uint16_t, cbPop)
|
---|
2730 | {
|
---|
2731 | return iemCImpl_ReturnNearCommon(pVCpu, cbInstr, IEMMODE_16BIT, cbPop);
|
---|
2732 | }
|
---|
2733 |
|
---|
2734 |
|
---|
2735 | /**
|
---|
2736 | * Implements retn imm16 with 32-bit effective operand size.
|
---|
2737 | *
|
---|
2738 | * @param cbPop The amount of arguments to pop from the stack (bytes).
|
---|
2739 | */
|
---|
2740 | IEM_CIMPL_DEF_1(iemCImpl_retn_iw_32, uint16_t, cbPop)
|
---|
2741 | {
|
---|
2742 | return iemCImpl_ReturnNearCommon(pVCpu, cbInstr, IEMMODE_32BIT, cbPop);
|
---|
2743 | }
|
---|
2744 |
|
---|
2745 |
|
---|
2746 | /**
|
---|
2747 | * Implements retn imm16 with 64-bit effective operand size.
|
---|
2748 | *
|
---|
2749 | * @param cbPop The amount of arguments to pop from the stack (bytes).
|
---|
2750 | */
|
---|
2751 | IEM_CIMPL_DEF_1(iemCImpl_retn_iw_64, uint16_t, cbPop)
|
---|
2752 | {
|
---|
2753 | return iemCImpl_ReturnNearCommon(pVCpu, cbInstr, IEMMODE_64BIT, cbPop);
|
---|
2754 | }
|
---|
2755 |
|
---|
2756 |
|
---|
2757 | /**
|
---|
2758 | * Implements retn with 16-bit effective operand size.
|
---|
2759 | */
|
---|
2760 | IEM_CIMPL_DEF_0(iemCImpl_retn_16)
|
---|
2761 | {
|
---|
2762 | return iemCImpl_ReturnNearCommon(pVCpu, cbInstr, IEMMODE_16BIT, 0);
|
---|
2763 | }
|
---|
2764 |
|
---|
2765 |
|
---|
2766 | /**
|
---|
2767 | * Implements retn with 32-bit effective operand size.
|
---|
2768 | */
|
---|
2769 | IEM_CIMPL_DEF_0(iemCImpl_retn_32)
|
---|
2770 | {
|
---|
2771 | return iemCImpl_ReturnNearCommon(pVCpu, cbInstr, IEMMODE_32BIT, 0);
|
---|
2772 | }
|
---|
2773 |
|
---|
2774 |
|
---|
2775 | /**
|
---|
2776 | * Implements retn with 64-bit effective operand size.
|
---|
2777 | */
|
---|
2778 | IEM_CIMPL_DEF_0(iemCImpl_retn_64)
|
---|
2779 | {
|
---|
2780 | return iemCImpl_ReturnNearCommon(pVCpu, cbInstr, IEMMODE_64BIT, 0);
|
---|
2781 | }
|
---|
2782 |
|
---|
2783 |
|
---|
2784 | /**
|
---|
2785 | * Implements enter.
|
---|
2786 | *
|
---|
2787 | * We're doing this in C because the instruction is insane, even for the
|
---|
2788 | * u8NestingLevel=0 case dealing with the stack is tedious.
|
---|
2789 | *
|
---|
2790 | * @param enmEffOpSize The effective operand size.
|
---|
2791 | * @param cbFrame Frame size.
|
---|
2792 | * @param cParameters Frame parameter count.
|
---|
2793 | */
|
---|
2794 | IEM_CIMPL_DEF_3(iemCImpl_enter, IEMMODE, enmEffOpSize, uint16_t, cbFrame, uint8_t, cParameters)
|
---|
2795 | {
|
---|
2796 | /* Push RBP, saving the old value in TmpRbp. */
|
---|
2797 | RTUINT64U NewRsp; NewRsp.u = pVCpu->cpum.GstCtx.rsp;
|
---|
2798 | RTUINT64U TmpRbp; TmpRbp.u = pVCpu->cpum.GstCtx.rbp;
|
---|
2799 | RTUINT64U NewRbp;
|
---|
2800 | VBOXSTRICTRC rcStrict;
|
---|
2801 | if (enmEffOpSize == IEMMODE_64BIT)
|
---|
2802 | {
|
---|
2803 | rcStrict = iemMemStackPushU64Ex(pVCpu, TmpRbp.u, &NewRsp);
|
---|
2804 | NewRbp = NewRsp;
|
---|
2805 | }
|
---|
2806 | else if (enmEffOpSize == IEMMODE_32BIT)
|
---|
2807 | {
|
---|
2808 | rcStrict = iemMemStackPushU32Ex(pVCpu, TmpRbp.DWords.dw0, &NewRsp);
|
---|
2809 | NewRbp = NewRsp;
|
---|
2810 | }
|
---|
2811 | else
|
---|
2812 | {
|
---|
2813 | rcStrict = iemMemStackPushU16Ex(pVCpu, TmpRbp.Words.w0, &NewRsp);
|
---|
2814 | NewRbp = TmpRbp;
|
---|
2815 | NewRbp.Words.w0 = NewRsp.Words.w0;
|
---|
2816 | }
|
---|
2817 | if (rcStrict != VINF_SUCCESS)
|
---|
2818 | return rcStrict;
|
---|
2819 |
|
---|
2820 | /* Copy the parameters (aka nesting levels by Intel). */
|
---|
2821 | cParameters &= 0x1f;
|
---|
2822 | if (cParameters > 0)
|
---|
2823 | {
|
---|
2824 | switch (enmEffOpSize)
|
---|
2825 | {
|
---|
2826 | case IEMMODE_16BIT:
|
---|
2827 | if (pVCpu->cpum.GstCtx.ss.Attr.n.u1DefBig)
|
---|
2828 | TmpRbp.DWords.dw0 -= 2;
|
---|
2829 | else
|
---|
2830 | TmpRbp.Words.w0 -= 2;
|
---|
2831 | do
|
---|
2832 | {
|
---|
2833 | uint16_t u16Tmp;
|
---|
2834 | rcStrict = iemMemStackPopU16Ex(pVCpu, &u16Tmp, &TmpRbp);
|
---|
2835 | if (rcStrict != VINF_SUCCESS)
|
---|
2836 | break;
|
---|
2837 | rcStrict = iemMemStackPushU16Ex(pVCpu, u16Tmp, &NewRsp);
|
---|
2838 | } while (--cParameters > 0 && rcStrict == VINF_SUCCESS);
|
---|
2839 | break;
|
---|
2840 |
|
---|
2841 | case IEMMODE_32BIT:
|
---|
2842 | if (pVCpu->cpum.GstCtx.ss.Attr.n.u1DefBig)
|
---|
2843 | TmpRbp.DWords.dw0 -= 4;
|
---|
2844 | else
|
---|
2845 | TmpRbp.Words.w0 -= 4;
|
---|
2846 | do
|
---|
2847 | {
|
---|
2848 | uint32_t u32Tmp;
|
---|
2849 | rcStrict = iemMemStackPopU32Ex(pVCpu, &u32Tmp, &TmpRbp);
|
---|
2850 | if (rcStrict != VINF_SUCCESS)
|
---|
2851 | break;
|
---|
2852 | rcStrict = iemMemStackPushU32Ex(pVCpu, u32Tmp, &NewRsp);
|
---|
2853 | } while (--cParameters > 0 && rcStrict == VINF_SUCCESS);
|
---|
2854 | break;
|
---|
2855 |
|
---|
2856 | case IEMMODE_64BIT:
|
---|
2857 | TmpRbp.u -= 8;
|
---|
2858 | do
|
---|
2859 | {
|
---|
2860 | uint64_t u64Tmp;
|
---|
2861 | rcStrict = iemMemStackPopU64Ex(pVCpu, &u64Tmp, &TmpRbp);
|
---|
2862 | if (rcStrict != VINF_SUCCESS)
|
---|
2863 | break;
|
---|
2864 | rcStrict = iemMemStackPushU64Ex(pVCpu, u64Tmp, &NewRsp);
|
---|
2865 | } while (--cParameters > 0 && rcStrict == VINF_SUCCESS);
|
---|
2866 | break;
|
---|
2867 |
|
---|
2868 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
2869 | }
|
---|
2870 | if (rcStrict != VINF_SUCCESS)
|
---|
2871 | return VINF_SUCCESS;
|
---|
2872 |
|
---|
2873 | /* Push the new RBP */
|
---|
2874 | if (enmEffOpSize == IEMMODE_64BIT)
|
---|
2875 | rcStrict = iemMemStackPushU64Ex(pVCpu, NewRbp.u, &NewRsp);
|
---|
2876 | else if (enmEffOpSize == IEMMODE_32BIT)
|
---|
2877 | rcStrict = iemMemStackPushU32Ex(pVCpu, NewRbp.DWords.dw0, &NewRsp);
|
---|
2878 | else
|
---|
2879 | rcStrict = iemMemStackPushU16Ex(pVCpu, NewRbp.Words.w0, &NewRsp);
|
---|
2880 | if (rcStrict != VINF_SUCCESS)
|
---|
2881 | return rcStrict;
|
---|
2882 |
|
---|
2883 | }
|
---|
2884 |
|
---|
2885 | /* Recalc RSP. */
|
---|
2886 | iemRegSubFromRspEx(pVCpu, &NewRsp, cbFrame);
|
---|
2887 |
|
---|
2888 | /** @todo Should probe write access at the new RSP according to AMD. */
|
---|
2889 | /** @todo Should handle accesses to the VMX APIC-access page. */
|
---|
2890 |
|
---|
2891 | /* Commit it. */
|
---|
2892 | pVCpu->cpum.GstCtx.rbp = NewRbp.u;
|
---|
2893 | pVCpu->cpum.GstCtx.rsp = NewRsp.u;
|
---|
2894 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
2895 | }
|
---|
2896 |
|
---|
2897 |
|
---|
2898 |
|
---|
2899 | /**
|
---|
2900 | * Implements leave.
|
---|
2901 | *
|
---|
2902 | * We're doing this in C because messing with the stack registers is annoying
|
---|
2903 | * since they depends on SS attributes.
|
---|
2904 | *
|
---|
2905 | * @param enmEffOpSize The effective operand size.
|
---|
2906 | */
|
---|
2907 | IEM_CIMPL_DEF_1(iemCImpl_leave, IEMMODE, enmEffOpSize)
|
---|
2908 | {
|
---|
2909 | /* Calculate the intermediate RSP from RBP and the stack attributes. */
|
---|
2910 | RTUINT64U NewRsp;
|
---|
2911 | if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
|
---|
2912 | NewRsp.u = pVCpu->cpum.GstCtx.rbp;
|
---|
2913 | else if (pVCpu->cpum.GstCtx.ss.Attr.n.u1DefBig)
|
---|
2914 | NewRsp.u = pVCpu->cpum.GstCtx.ebp;
|
---|
2915 | else
|
---|
2916 | {
|
---|
2917 | /** @todo Check that LEAVE actually preserve the high EBP bits. */
|
---|
2918 | NewRsp.u = pVCpu->cpum.GstCtx.rsp;
|
---|
2919 | NewRsp.Words.w0 = pVCpu->cpum.GstCtx.bp;
|
---|
2920 | }
|
---|
2921 |
|
---|
2922 | /* Pop RBP according to the operand size. */
|
---|
2923 | VBOXSTRICTRC rcStrict;
|
---|
2924 | RTUINT64U NewRbp;
|
---|
2925 | switch (enmEffOpSize)
|
---|
2926 | {
|
---|
2927 | case IEMMODE_16BIT:
|
---|
2928 | NewRbp.u = pVCpu->cpum.GstCtx.rbp;
|
---|
2929 | rcStrict = iemMemStackPopU16Ex(pVCpu, &NewRbp.Words.w0, &NewRsp);
|
---|
2930 | break;
|
---|
2931 | case IEMMODE_32BIT:
|
---|
2932 | NewRbp.u = 0;
|
---|
2933 | rcStrict = iemMemStackPopU32Ex(pVCpu, &NewRbp.DWords.dw0, &NewRsp);
|
---|
2934 | break;
|
---|
2935 | case IEMMODE_64BIT:
|
---|
2936 | rcStrict = iemMemStackPopU64Ex(pVCpu, &NewRbp.u, &NewRsp);
|
---|
2937 | break;
|
---|
2938 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
2939 | }
|
---|
2940 | if (rcStrict != VINF_SUCCESS)
|
---|
2941 | return rcStrict;
|
---|
2942 |
|
---|
2943 |
|
---|
2944 | /* Commit it. */
|
---|
2945 | pVCpu->cpum.GstCtx.rbp = NewRbp.u;
|
---|
2946 | pVCpu->cpum.GstCtx.rsp = NewRsp.u;
|
---|
2947 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
2948 | }
|
---|
2949 |
|
---|
2950 |
|
---|
2951 | /**
|
---|
2952 | * Implements int3 and int XX.
|
---|
2953 | *
|
---|
2954 | * @param u8Int The interrupt vector number.
|
---|
2955 | * @param enmInt The int instruction type.
|
---|
2956 | */
|
---|
2957 | IEM_CIMPL_DEF_2(iemCImpl_int, uint8_t, u8Int, IEMINT, enmInt)
|
---|
2958 | {
|
---|
2959 | Assert(pVCpu->iem.s.cXcptRecursions == 0);
|
---|
2960 |
|
---|
2961 | /*
|
---|
2962 | * We must check if this INT3 might belong to DBGF before raising a #BP.
|
---|
2963 | */
|
---|
2964 | if (u8Int == 3)
|
---|
2965 | {
|
---|
2966 | PVMCC pVM = pVCpu->CTX_SUFF(pVM);
|
---|
2967 | if (pVM->dbgf.ro.cEnabledInt3Breakpoints == 0)
|
---|
2968 | { /* likely: No vbox debugger breakpoints */ }
|
---|
2969 | else
|
---|
2970 | {
|
---|
2971 | VBOXSTRICTRC rcStrict = DBGFTrap03Handler(pVM, pVCpu, &pVCpu->cpum.GstCtx);
|
---|
2972 | Log(("iemCImpl_int: DBGFTrap03Handler -> %Rrc\n", VBOXSTRICTRC_VAL(rcStrict) ));
|
---|
2973 | if (rcStrict != VINF_EM_RAW_GUEST_TRAP)
|
---|
2974 | return iemSetPassUpStatus(pVCpu, rcStrict);
|
---|
2975 | }
|
---|
2976 | }
|
---|
2977 | /** @todo single stepping */
|
---|
2978 | return iemRaiseXcptOrInt(pVCpu,
|
---|
2979 | cbInstr,
|
---|
2980 | u8Int,
|
---|
2981 | IEM_XCPT_FLAGS_T_SOFT_INT | enmInt,
|
---|
2982 | 0,
|
---|
2983 | 0);
|
---|
2984 | }
|
---|
2985 |
|
---|
2986 |
|
---|
2987 | /**
|
---|
2988 | * Implements iret for real mode and V8086 mode.
|
---|
2989 | *
|
---|
2990 | * @param enmEffOpSize The effective operand size.
|
---|
2991 | */
|
---|
2992 | IEM_CIMPL_DEF_1(iemCImpl_iret_real_v8086, IEMMODE, enmEffOpSize)
|
---|
2993 | {
|
---|
2994 | X86EFLAGS Efl;
|
---|
2995 | Efl.u = IEMMISC_GET_EFL(pVCpu);
|
---|
2996 | NOREF(cbInstr);
|
---|
2997 |
|
---|
2998 | /*
|
---|
2999 | * iret throws an exception if VME isn't enabled.
|
---|
3000 | */
|
---|
3001 | if ( Efl.Bits.u1VM
|
---|
3002 | && Efl.Bits.u2IOPL != 3
|
---|
3003 | && !(pVCpu->cpum.GstCtx.cr4 & X86_CR4_VME))
|
---|
3004 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
3005 |
|
---|
3006 | /*
|
---|
3007 | * Do the stack bits, but don't commit RSP before everything checks
|
---|
3008 | * out right.
|
---|
3009 | */
|
---|
3010 | Assert(enmEffOpSize == IEMMODE_32BIT || enmEffOpSize == IEMMODE_16BIT);
|
---|
3011 | VBOXSTRICTRC rcStrict;
|
---|
3012 | RTCPTRUNION uFrame;
|
---|
3013 | uint16_t uNewCs;
|
---|
3014 | uint32_t uNewEip;
|
---|
3015 | uint32_t uNewFlags;
|
---|
3016 | uint64_t uNewRsp;
|
---|
3017 | if (enmEffOpSize == IEMMODE_32BIT)
|
---|
3018 | {
|
---|
3019 | rcStrict = iemMemStackPopBeginSpecial(pVCpu, 12, 1, &uFrame.pv, &uNewRsp);
|
---|
3020 | if (rcStrict != VINF_SUCCESS)
|
---|
3021 | return rcStrict;
|
---|
3022 | uNewEip = uFrame.pu32[0];
|
---|
3023 | if (uNewEip > UINT16_MAX)
|
---|
3024 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
3025 |
|
---|
3026 | uNewCs = (uint16_t)uFrame.pu32[1];
|
---|
3027 | uNewFlags = uFrame.pu32[2];
|
---|
3028 | uNewFlags &= X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF
|
---|
3029 | | X86_EFL_TF | X86_EFL_IF | X86_EFL_DF | X86_EFL_OF | X86_EFL_IOPL | X86_EFL_NT
|
---|
3030 | | X86_EFL_RF /*| X86_EFL_VM*/ | X86_EFL_AC /*|X86_EFL_VIF*/ /*|X86_EFL_VIP*/
|
---|
3031 | | X86_EFL_ID;
|
---|
3032 | if (IEM_GET_TARGET_CPU(pVCpu) <= IEMTARGETCPU_386)
|
---|
3033 | uNewFlags &= ~(X86_EFL_AC | X86_EFL_ID | X86_EFL_VIF | X86_EFL_VIP);
|
---|
3034 | uNewFlags |= Efl.u & (X86_EFL_VM | X86_EFL_VIF | X86_EFL_VIP | X86_EFL_1);
|
---|
3035 | }
|
---|
3036 | else
|
---|
3037 | {
|
---|
3038 | rcStrict = iemMemStackPopBeginSpecial(pVCpu, 6, 1, &uFrame.pv, &uNewRsp);
|
---|
3039 | if (rcStrict != VINF_SUCCESS)
|
---|
3040 | return rcStrict;
|
---|
3041 | uNewEip = uFrame.pu16[0];
|
---|
3042 | uNewCs = uFrame.pu16[1];
|
---|
3043 | uNewFlags = uFrame.pu16[2];
|
---|
3044 | uNewFlags &= X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF
|
---|
3045 | | X86_EFL_TF | X86_EFL_IF | X86_EFL_DF | X86_EFL_OF | X86_EFL_IOPL | X86_EFL_NT;
|
---|
3046 | uNewFlags |= Efl.u & ((UINT32_C(0xffff0000) | X86_EFL_1) & ~X86_EFL_RF);
|
---|
3047 | /** @todo The intel pseudo code does not indicate what happens to
|
---|
3048 | * reserved flags. We just ignore them. */
|
---|
3049 | /* Ancient CPU adjustments: See iemCImpl_popf. */
|
---|
3050 | if (IEM_GET_TARGET_CPU(pVCpu) == IEMTARGETCPU_286)
|
---|
3051 | uNewFlags &= ~(X86_EFL_NT | X86_EFL_IOPL);
|
---|
3052 | }
|
---|
3053 | rcStrict = iemMemStackPopDoneSpecial(pVCpu, uFrame.pv);
|
---|
3054 | if (RT_LIKELY(rcStrict == VINF_SUCCESS))
|
---|
3055 | { /* extremely likely */ }
|
---|
3056 | else
|
---|
3057 | return rcStrict;
|
---|
3058 |
|
---|
3059 | /** @todo Check how this is supposed to work if sp=0xfffe. */
|
---|
3060 | Log7(("iemCImpl_iret_real_v8086: uNewCs=%#06x uNewRip=%#010x uNewFlags=%#x uNewRsp=%#18llx\n",
|
---|
3061 | uNewCs, uNewEip, uNewFlags, uNewRsp));
|
---|
3062 |
|
---|
3063 | /*
|
---|
3064 | * Check the limit of the new EIP.
|
---|
3065 | */
|
---|
3066 | /** @todo Only the AMD pseudo code check the limit here, what's
|
---|
3067 | * right? */
|
---|
3068 | if (uNewEip > pVCpu->cpum.GstCtx.cs.u32Limit)
|
---|
3069 | return iemRaiseSelectorBounds(pVCpu, X86_SREG_CS, IEM_ACCESS_INSTRUCTION);
|
---|
3070 |
|
---|
3071 | /*
|
---|
3072 | * V8086 checks and flag adjustments
|
---|
3073 | */
|
---|
3074 | if (Efl.Bits.u1VM)
|
---|
3075 | {
|
---|
3076 | if (Efl.Bits.u2IOPL == 3)
|
---|
3077 | {
|
---|
3078 | /* Preserve IOPL and clear RF. */
|
---|
3079 | uNewFlags &= ~(X86_EFL_IOPL | X86_EFL_RF);
|
---|
3080 | uNewFlags |= Efl.u & (X86_EFL_IOPL);
|
---|
3081 | }
|
---|
3082 | else if ( enmEffOpSize == IEMMODE_16BIT
|
---|
3083 | && ( !(uNewFlags & X86_EFL_IF)
|
---|
3084 | || !Efl.Bits.u1VIP )
|
---|
3085 | && !(uNewFlags & X86_EFL_TF) )
|
---|
3086 | {
|
---|
3087 | /* Move IF to VIF, clear RF and preserve IF and IOPL.*/
|
---|
3088 | uNewFlags &= ~X86_EFL_VIF;
|
---|
3089 | uNewFlags |= (uNewFlags & X86_EFL_IF) << (19 - 9);
|
---|
3090 | uNewFlags &= ~(X86_EFL_IF | X86_EFL_IOPL | X86_EFL_RF);
|
---|
3091 | uNewFlags |= Efl.u & (X86_EFL_IF | X86_EFL_IOPL);
|
---|
3092 | }
|
---|
3093 | else
|
---|
3094 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
3095 | Log7(("iemCImpl_iret_real_v8086: u1VM=1: adjusted uNewFlags=%#x\n", uNewFlags));
|
---|
3096 | }
|
---|
3097 |
|
---|
3098 | /*
|
---|
3099 | * Commit the operation.
|
---|
3100 | */
|
---|
3101 | #ifdef DBGFTRACE_ENABLED
|
---|
3102 | RTTraceBufAddMsgF(pVCpu->CTX_SUFF(pVM)->CTX_SUFF(hTraceBuf), "iret/rm %04x:%04x -> %04x:%04x %x %04llx",
|
---|
3103 | pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.eip, uNewCs, uNewEip, uNewFlags, uNewRsp);
|
---|
3104 | #endif
|
---|
3105 | pVCpu->cpum.GstCtx.rsp = uNewRsp;
|
---|
3106 | pVCpu->cpum.GstCtx.rip = uNewEip;
|
---|
3107 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
|
---|
3108 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
|
---|
3109 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
3110 | pVCpu->cpum.GstCtx.cs.u64Base = (uint32_t)uNewCs << 4;
|
---|
3111 | /** @todo do we load attribs and limit as well? */
|
---|
3112 | Assert(uNewFlags & X86_EFL_1);
|
---|
3113 | IEMMISC_SET_EFL(pVCpu, uNewFlags);
|
---|
3114 |
|
---|
3115 | /* Flush the prefetch buffer. */
|
---|
3116 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr); /** @todo can do light flush in real mode at least */
|
---|
3117 |
|
---|
3118 | /** @todo single stepping */
|
---|
3119 | return VINF_SUCCESS;
|
---|
3120 | }
|
---|
3121 |
|
---|
3122 |
|
---|
3123 | /**
|
---|
3124 | * Loads a segment register when entering V8086 mode.
|
---|
3125 | *
|
---|
3126 | * @param pSReg The segment register.
|
---|
3127 | * @param uSeg The segment to load.
|
---|
3128 | */
|
---|
3129 | static void iemCImplCommonV8086LoadSeg(PCPUMSELREG pSReg, uint16_t uSeg)
|
---|
3130 | {
|
---|
3131 | pSReg->Sel = uSeg;
|
---|
3132 | pSReg->ValidSel = uSeg;
|
---|
3133 | pSReg->fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
3134 | pSReg->u64Base = (uint32_t)uSeg << 4;
|
---|
3135 | pSReg->u32Limit = 0xffff;
|
---|
3136 | pSReg->Attr.u = X86_SEL_TYPE_RW_ACC | RT_BIT(4) /*!sys*/ | RT_BIT(7) /*P*/ | (3 /*DPL*/ << 5); /* VT-x wants 0xf3 */
|
---|
3137 | /** @todo Testcase: Check if VT-x really needs this and what it does itself when
|
---|
3138 | * IRET'ing to V8086. */
|
---|
3139 | }
|
---|
3140 |
|
---|
3141 |
|
---|
3142 | /**
|
---|
3143 | * Implements iret for protected mode returning to V8086 mode.
|
---|
3144 | *
|
---|
3145 | * @param uNewEip The new EIP.
|
---|
3146 | * @param uNewCs The new CS.
|
---|
3147 | * @param uNewFlags The new EFLAGS.
|
---|
3148 | * @param uNewRsp The RSP after the initial IRET frame.
|
---|
3149 | *
|
---|
3150 | * @note This can only be a 32-bit iret du to the X86_EFL_VM position.
|
---|
3151 | */
|
---|
3152 | IEM_CIMPL_DEF_4(iemCImpl_iret_prot_v8086, uint32_t, uNewEip, uint16_t, uNewCs, uint32_t, uNewFlags, uint64_t, uNewRsp)
|
---|
3153 | {
|
---|
3154 | RT_NOREF_PV(cbInstr);
|
---|
3155 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SREG_MASK);
|
---|
3156 |
|
---|
3157 | /*
|
---|
3158 | * Pop the V8086 specific frame bits off the stack.
|
---|
3159 | */
|
---|
3160 | VBOXSTRICTRC rcStrict;
|
---|
3161 | RTCPTRUNION uFrame;
|
---|
3162 | rcStrict = iemMemStackPopContinueSpecial(pVCpu, 0 /*off*/, 24 /*cbMem*/, &uFrame.pv, uNewRsp);
|
---|
3163 | if (rcStrict != VINF_SUCCESS)
|
---|
3164 | return rcStrict;
|
---|
3165 | uint32_t uNewEsp = uFrame.pu32[0];
|
---|
3166 | uint16_t uNewSs = uFrame.pu32[1];
|
---|
3167 | uint16_t uNewEs = uFrame.pu32[2];
|
---|
3168 | uint16_t uNewDs = uFrame.pu32[3];
|
---|
3169 | uint16_t uNewFs = uFrame.pu32[4];
|
---|
3170 | uint16_t uNewGs = uFrame.pu32[5];
|
---|
3171 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)uFrame.pv, IEM_ACCESS_STACK_R); /* don't use iemMemStackPopCommitSpecial here. */
|
---|
3172 | if (rcStrict != VINF_SUCCESS)
|
---|
3173 | return rcStrict;
|
---|
3174 |
|
---|
3175 | /*
|
---|
3176 | * Commit the operation.
|
---|
3177 | */
|
---|
3178 | uNewFlags &= X86_EFL_LIVE_MASK;
|
---|
3179 | uNewFlags |= X86_EFL_RA1_MASK;
|
---|
3180 | #ifdef DBGFTRACE_ENABLED
|
---|
3181 | RTTraceBufAddMsgF(pVCpu->CTX_SUFF(pVM)->CTX_SUFF(hTraceBuf), "iret/p/v %04x:%08x -> %04x:%04x %x %04x:%04x",
|
---|
3182 | pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.eip, uNewCs, uNewEip, uNewFlags, uNewSs, uNewEsp);
|
---|
3183 | #endif
|
---|
3184 | Log7(("iemCImpl_iret_prot_v8086: %04x:%08x -> %04x:%04x %x %04x:%04x\n", pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.eip, uNewCs, uNewEip, uNewFlags, uNewSs, uNewEsp));
|
---|
3185 |
|
---|
3186 | IEMMISC_SET_EFL(pVCpu, uNewFlags);
|
---|
3187 | iemCImplCommonV8086LoadSeg(&pVCpu->cpum.GstCtx.cs, uNewCs);
|
---|
3188 | iemCImplCommonV8086LoadSeg(&pVCpu->cpum.GstCtx.ss, uNewSs);
|
---|
3189 | iemCImplCommonV8086LoadSeg(&pVCpu->cpum.GstCtx.es, uNewEs);
|
---|
3190 | iemCImplCommonV8086LoadSeg(&pVCpu->cpum.GstCtx.ds, uNewDs);
|
---|
3191 | iemCImplCommonV8086LoadSeg(&pVCpu->cpum.GstCtx.fs, uNewFs);
|
---|
3192 | iemCImplCommonV8086LoadSeg(&pVCpu->cpum.GstCtx.gs, uNewGs);
|
---|
3193 | pVCpu->cpum.GstCtx.rip = (uint16_t)uNewEip;
|
---|
3194 | pVCpu->cpum.GstCtx.rsp = uNewEsp; /** @todo check this out! */
|
---|
3195 | pVCpu->iem.s.uCpl = 3;
|
---|
3196 |
|
---|
3197 | /* Flush the prefetch buffer. */
|
---|
3198 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr);
|
---|
3199 |
|
---|
3200 | /** @todo single stepping */
|
---|
3201 | return VINF_SUCCESS;
|
---|
3202 | }
|
---|
3203 |
|
---|
3204 |
|
---|
3205 | /**
|
---|
3206 | * Implements iret for protected mode returning via a nested task.
|
---|
3207 | *
|
---|
3208 | * @param enmEffOpSize The effective operand size.
|
---|
3209 | */
|
---|
3210 | IEM_CIMPL_DEF_1(iemCImpl_iret_prot_NestedTask, IEMMODE, enmEffOpSize)
|
---|
3211 | {
|
---|
3212 | Log7(("iemCImpl_iret_prot_NestedTask:\n"));
|
---|
3213 | #ifndef IEM_IMPLEMENTS_TASKSWITCH
|
---|
3214 | IEM_RETURN_ASPECT_NOT_IMPLEMENTED();
|
---|
3215 | #else
|
---|
3216 | RT_NOREF_PV(enmEffOpSize);
|
---|
3217 |
|
---|
3218 | /*
|
---|
3219 | * Read the segment selector in the link-field of the current TSS.
|
---|
3220 | */
|
---|
3221 | RTSEL uSelRet;
|
---|
3222 | VBOXSTRICTRC rcStrict = iemMemFetchSysU16(pVCpu, &uSelRet, UINT8_MAX, pVCpu->cpum.GstCtx.tr.u64Base);
|
---|
3223 | if (rcStrict != VINF_SUCCESS)
|
---|
3224 | return rcStrict;
|
---|
3225 |
|
---|
3226 | /*
|
---|
3227 | * Fetch the returning task's TSS descriptor from the GDT.
|
---|
3228 | */
|
---|
3229 | if (uSelRet & X86_SEL_LDT)
|
---|
3230 | {
|
---|
3231 | Log(("iret_prot_NestedTask TSS not in LDT. uSelRet=%04x -> #TS\n", uSelRet));
|
---|
3232 | return iemRaiseTaskSwitchFaultBySelector(pVCpu, uSelRet);
|
---|
3233 | }
|
---|
3234 |
|
---|
3235 | IEMSELDESC TssDesc;
|
---|
3236 | rcStrict = iemMemFetchSelDesc(pVCpu, &TssDesc, uSelRet, X86_XCPT_GP);
|
---|
3237 | if (rcStrict != VINF_SUCCESS)
|
---|
3238 | return rcStrict;
|
---|
3239 |
|
---|
3240 | if (TssDesc.Legacy.Gate.u1DescType)
|
---|
3241 | {
|
---|
3242 | Log(("iret_prot_NestedTask Invalid TSS type. uSelRet=%04x -> #TS\n", uSelRet));
|
---|
3243 | return iemRaiseTaskSwitchFaultBySelector(pVCpu, uSelRet & X86_SEL_MASK_OFF_RPL);
|
---|
3244 | }
|
---|
3245 |
|
---|
3246 | if ( TssDesc.Legacy.Gate.u4Type != X86_SEL_TYPE_SYS_286_TSS_BUSY
|
---|
3247 | && TssDesc.Legacy.Gate.u4Type != X86_SEL_TYPE_SYS_386_TSS_BUSY)
|
---|
3248 | {
|
---|
3249 | Log(("iret_prot_NestedTask TSS is not busy. uSelRet=%04x DescType=%#x -> #TS\n", uSelRet, TssDesc.Legacy.Gate.u4Type));
|
---|
3250 | return iemRaiseTaskSwitchFaultBySelector(pVCpu, uSelRet & X86_SEL_MASK_OFF_RPL);
|
---|
3251 | }
|
---|
3252 |
|
---|
3253 | if (!TssDesc.Legacy.Gate.u1Present)
|
---|
3254 | {
|
---|
3255 | Log(("iret_prot_NestedTask TSS is not present. uSelRet=%04x -> #NP\n", uSelRet));
|
---|
3256 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uSelRet & X86_SEL_MASK_OFF_RPL);
|
---|
3257 | }
|
---|
3258 |
|
---|
3259 | uint32_t uNextEip = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
3260 | return iemTaskSwitch(pVCpu, IEMTASKSWITCH_IRET, uNextEip, 0 /* fFlags */, 0 /* uErr */,
|
---|
3261 | 0 /* uCr2 */, uSelRet, &TssDesc);
|
---|
3262 | #endif
|
---|
3263 | }
|
---|
3264 |
|
---|
3265 |
|
---|
3266 | /**
|
---|
3267 | * Implements iret for protected mode
|
---|
3268 | *
|
---|
3269 | * @param enmEffOpSize The effective operand size.
|
---|
3270 | */
|
---|
3271 | IEM_CIMPL_DEF_1(iemCImpl_iret_prot, IEMMODE, enmEffOpSize)
|
---|
3272 | {
|
---|
3273 | NOREF(cbInstr);
|
---|
3274 | Assert(enmEffOpSize == IEMMODE_32BIT || enmEffOpSize == IEMMODE_16BIT);
|
---|
3275 |
|
---|
3276 | /*
|
---|
3277 | * Nested task return.
|
---|
3278 | */
|
---|
3279 | if (pVCpu->cpum.GstCtx.eflags.Bits.u1NT)
|
---|
3280 | return IEM_CIMPL_CALL_1(iemCImpl_iret_prot_NestedTask, enmEffOpSize);
|
---|
3281 |
|
---|
3282 | /*
|
---|
3283 | * Normal return.
|
---|
3284 | *
|
---|
3285 | * Do the stack bits, but don't commit RSP before everything checks
|
---|
3286 | * out right.
|
---|
3287 | */
|
---|
3288 | Assert(enmEffOpSize == IEMMODE_32BIT || enmEffOpSize == IEMMODE_16BIT);
|
---|
3289 | VBOXSTRICTRC rcStrict;
|
---|
3290 | RTCPTRUNION uFrame;
|
---|
3291 | uint16_t uNewCs;
|
---|
3292 | uint32_t uNewEip;
|
---|
3293 | uint32_t uNewFlags;
|
---|
3294 | uint64_t uNewRsp;
|
---|
3295 | if (enmEffOpSize == IEMMODE_32BIT)
|
---|
3296 | {
|
---|
3297 | rcStrict = iemMemStackPopBeginSpecial(pVCpu, 12, 3, &uFrame.pv, &uNewRsp);
|
---|
3298 | if (rcStrict != VINF_SUCCESS)
|
---|
3299 | return rcStrict;
|
---|
3300 | uNewEip = uFrame.pu32[0];
|
---|
3301 | uNewCs = (uint16_t)uFrame.pu32[1];
|
---|
3302 | uNewFlags = uFrame.pu32[2];
|
---|
3303 | }
|
---|
3304 | else
|
---|
3305 | {
|
---|
3306 | rcStrict = iemMemStackPopBeginSpecial(pVCpu, 6, 1, &uFrame.pv, &uNewRsp);
|
---|
3307 | if (rcStrict != VINF_SUCCESS)
|
---|
3308 | return rcStrict;
|
---|
3309 | uNewEip = uFrame.pu16[0];
|
---|
3310 | uNewCs = uFrame.pu16[1];
|
---|
3311 | uNewFlags = uFrame.pu16[2];
|
---|
3312 | }
|
---|
3313 | rcStrict = iemMemStackPopDoneSpecial(pVCpu, (void *)uFrame.pv); /* don't use iemMemStackPopCommitSpecial here. */
|
---|
3314 | if (RT_LIKELY(rcStrict == VINF_SUCCESS))
|
---|
3315 | { /* extremely likely */ }
|
---|
3316 | else
|
---|
3317 | return rcStrict;
|
---|
3318 | Log7(("iemCImpl_iret_prot: uNewCs=%#06x uNewEip=%#010x uNewFlags=%#x uNewRsp=%#18llx uCpl=%u\n", uNewCs, uNewEip, uNewFlags, uNewRsp, pVCpu->iem.s.uCpl));
|
---|
3319 |
|
---|
3320 | /*
|
---|
3321 | * We're hopefully not returning to V8086 mode...
|
---|
3322 | */
|
---|
3323 | if ( (uNewFlags & X86_EFL_VM)
|
---|
3324 | && pVCpu->iem.s.uCpl == 0)
|
---|
3325 | {
|
---|
3326 | Assert(enmEffOpSize == IEMMODE_32BIT);
|
---|
3327 | return IEM_CIMPL_CALL_4(iemCImpl_iret_prot_v8086, uNewEip, uNewCs, uNewFlags, uNewRsp);
|
---|
3328 | }
|
---|
3329 |
|
---|
3330 | /*
|
---|
3331 | * Protected mode.
|
---|
3332 | */
|
---|
3333 | /* Read the CS descriptor. */
|
---|
3334 | if (!(uNewCs & X86_SEL_MASK_OFF_RPL))
|
---|
3335 | {
|
---|
3336 | Log(("iret %04x:%08x -> invalid CS selector, #GP(0)\n", uNewCs, uNewEip));
|
---|
3337 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
3338 | }
|
---|
3339 |
|
---|
3340 | IEMSELDESC DescCS;
|
---|
3341 | rcStrict = iemMemFetchSelDesc(pVCpu, &DescCS, uNewCs, X86_XCPT_GP);
|
---|
3342 | if (rcStrict != VINF_SUCCESS)
|
---|
3343 | {
|
---|
3344 | Log(("iret %04x:%08x - rcStrict=%Rrc when fetching CS\n", uNewCs, uNewEip, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
3345 | return rcStrict;
|
---|
3346 | }
|
---|
3347 |
|
---|
3348 | /* Must be a code descriptor. */
|
---|
3349 | if (!DescCS.Legacy.Gen.u1DescType)
|
---|
3350 | {
|
---|
3351 | Log(("iret %04x:%08x - CS is system segment (%#x) -> #GP\n", uNewCs, uNewEip, DescCS.Legacy.Gen.u4Type));
|
---|
3352 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
3353 | }
|
---|
3354 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE))
|
---|
3355 | {
|
---|
3356 | Log(("iret %04x:%08x - not code segment (%#x) -> #GP\n", uNewCs, uNewEip, DescCS.Legacy.Gen.u4Type));
|
---|
3357 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
3358 | }
|
---|
3359 |
|
---|
3360 | /* Privilege checks. */
|
---|
3361 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF))
|
---|
3362 | {
|
---|
3363 | if ((uNewCs & X86_SEL_RPL) != DescCS.Legacy.Gen.u2Dpl)
|
---|
3364 | {
|
---|
3365 | Log(("iret %04x:%08x - RPL != DPL (%d) -> #GP\n", uNewCs, uNewEip, DescCS.Legacy.Gen.u2Dpl));
|
---|
3366 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
3367 | }
|
---|
3368 | }
|
---|
3369 | else if ((uNewCs & X86_SEL_RPL) < DescCS.Legacy.Gen.u2Dpl)
|
---|
3370 | {
|
---|
3371 | Log(("iret %04x:%08x - RPL < DPL (%d) -> #GP\n", uNewCs, uNewEip, DescCS.Legacy.Gen.u2Dpl));
|
---|
3372 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
3373 | }
|
---|
3374 | if ((uNewCs & X86_SEL_RPL) < pVCpu->iem.s.uCpl)
|
---|
3375 | {
|
---|
3376 | Log(("iret %04x:%08x - RPL < CPL (%d) -> #GP\n", uNewCs, uNewEip, pVCpu->iem.s.uCpl));
|
---|
3377 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
3378 | }
|
---|
3379 |
|
---|
3380 | /* Present? */
|
---|
3381 | if (!DescCS.Legacy.Gen.u1Present)
|
---|
3382 | {
|
---|
3383 | Log(("iret %04x:%08x - CS not present -> #NP\n", uNewCs, uNewEip));
|
---|
3384 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uNewCs);
|
---|
3385 | }
|
---|
3386 |
|
---|
3387 | uint32_t cbLimitCS = X86DESC_LIMIT_G(&DescCS.Legacy);
|
---|
3388 |
|
---|
3389 | /*
|
---|
3390 | * Return to outer level?
|
---|
3391 | */
|
---|
3392 | if ((uNewCs & X86_SEL_RPL) != pVCpu->iem.s.uCpl)
|
---|
3393 | {
|
---|
3394 | uint16_t uNewSS;
|
---|
3395 | uint32_t uNewESP;
|
---|
3396 | if (enmEffOpSize == IEMMODE_32BIT)
|
---|
3397 | {
|
---|
3398 | rcStrict = iemMemStackPopContinueSpecial(pVCpu, 0/*off*/, 8 /*cbMem*/, &uFrame.pv, uNewRsp);
|
---|
3399 | if (rcStrict != VINF_SUCCESS)
|
---|
3400 | return rcStrict;
|
---|
3401 | /** @todo We might be popping a 32-bit ESP from the IRET frame, but whether
|
---|
3402 | * 16-bit or 32-bit are being loaded into SP depends on the D/B
|
---|
3403 | * bit of the popped SS selector it turns out. */
|
---|
3404 | uNewESP = uFrame.pu32[0];
|
---|
3405 | uNewSS = (uint16_t)uFrame.pu32[1];
|
---|
3406 | }
|
---|
3407 | else
|
---|
3408 | {
|
---|
3409 | rcStrict = iemMemStackPopContinueSpecial(pVCpu, 0 /*off*/, 4 /*cbMem*/, &uFrame.pv, uNewRsp);
|
---|
3410 | if (rcStrict != VINF_SUCCESS)
|
---|
3411 | return rcStrict;
|
---|
3412 | uNewESP = uFrame.pu16[0];
|
---|
3413 | uNewSS = uFrame.pu16[1];
|
---|
3414 | }
|
---|
3415 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)uFrame.pv, IEM_ACCESS_STACK_R);
|
---|
3416 | if (rcStrict != VINF_SUCCESS)
|
---|
3417 | return rcStrict;
|
---|
3418 | Log7(("iemCImpl_iret_prot: uNewSS=%#06x uNewESP=%#010x\n", uNewSS, uNewESP));
|
---|
3419 |
|
---|
3420 | /* Read the SS descriptor. */
|
---|
3421 | if (!(uNewSS & X86_SEL_MASK_OFF_RPL))
|
---|
3422 | {
|
---|
3423 | Log(("iret %04x:%08x/%04x:%08x -> invalid SS selector, #GP(0)\n", uNewCs, uNewEip, uNewSS, uNewESP));
|
---|
3424 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
3425 | }
|
---|
3426 |
|
---|
3427 | IEMSELDESC DescSS;
|
---|
3428 | rcStrict = iemMemFetchSelDesc(pVCpu, &DescSS, uNewSS, X86_XCPT_GP); /** @todo Correct exception? */
|
---|
3429 | if (rcStrict != VINF_SUCCESS)
|
---|
3430 | {
|
---|
3431 | Log(("iret %04x:%08x/%04x:%08x - %Rrc when fetching SS\n",
|
---|
3432 | uNewCs, uNewEip, uNewSS, uNewESP, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
3433 | return rcStrict;
|
---|
3434 | }
|
---|
3435 |
|
---|
3436 | /* Privilege checks. */
|
---|
3437 | if ((uNewSS & X86_SEL_RPL) != (uNewCs & X86_SEL_RPL))
|
---|
3438 | {
|
---|
3439 | Log(("iret %04x:%08x/%04x:%08x -> SS.RPL != CS.RPL -> #GP\n", uNewCs, uNewEip, uNewSS, uNewESP));
|
---|
3440 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSS);
|
---|
3441 | }
|
---|
3442 | if (DescSS.Legacy.Gen.u2Dpl != (uNewCs & X86_SEL_RPL))
|
---|
3443 | {
|
---|
3444 | Log(("iret %04x:%08x/%04x:%08x -> SS.DPL (%d) != CS.RPL -> #GP\n",
|
---|
3445 | uNewCs, uNewEip, uNewSS, uNewESP, DescSS.Legacy.Gen.u2Dpl));
|
---|
3446 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSS);
|
---|
3447 | }
|
---|
3448 |
|
---|
3449 | /* Must be a writeable data segment descriptor. */
|
---|
3450 | if (!DescSS.Legacy.Gen.u1DescType)
|
---|
3451 | {
|
---|
3452 | Log(("iret %04x:%08x/%04x:%08x -> SS is system segment (%#x) -> #GP\n",
|
---|
3453 | uNewCs, uNewEip, uNewSS, uNewESP, DescSS.Legacy.Gen.u4Type));
|
---|
3454 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSS);
|
---|
3455 | }
|
---|
3456 | if ((DescSS.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_WRITE)) != X86_SEL_TYPE_WRITE)
|
---|
3457 | {
|
---|
3458 | Log(("iret %04x:%08x/%04x:%08x - not writable data segment (%#x) -> #GP\n",
|
---|
3459 | uNewCs, uNewEip, uNewSS, uNewESP, DescSS.Legacy.Gen.u4Type));
|
---|
3460 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSS);
|
---|
3461 | }
|
---|
3462 |
|
---|
3463 | /* Present? */
|
---|
3464 | if (!DescSS.Legacy.Gen.u1Present)
|
---|
3465 | {
|
---|
3466 | Log(("iret %04x:%08x/%04x:%08x -> SS not present -> #SS\n", uNewCs, uNewEip, uNewSS, uNewESP));
|
---|
3467 | return iemRaiseStackSelectorNotPresentBySelector(pVCpu, uNewSS);
|
---|
3468 | }
|
---|
3469 |
|
---|
3470 | uint32_t cbLimitSs = X86DESC_LIMIT_G(&DescSS.Legacy);
|
---|
3471 |
|
---|
3472 | /* Check EIP. */
|
---|
3473 | if (uNewEip > cbLimitCS)
|
---|
3474 | {
|
---|
3475 | Log(("iret %04x:%08x/%04x:%08x -> EIP is out of bounds (%#x) -> #GP(0)\n",
|
---|
3476 | uNewCs, uNewEip, uNewSS, uNewESP, cbLimitCS));
|
---|
3477 | /** @todo Which is it, \#GP(0) or \#GP(sel)? */
|
---|
3478 | return iemRaiseSelectorBoundsBySelector(pVCpu, uNewCs);
|
---|
3479 | }
|
---|
3480 |
|
---|
3481 | /*
|
---|
3482 | * Commit the changes, marking CS and SS accessed first since
|
---|
3483 | * that may fail.
|
---|
3484 | */
|
---|
3485 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
3486 | {
|
---|
3487 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewCs);
|
---|
3488 | if (rcStrict != VINF_SUCCESS)
|
---|
3489 | return rcStrict;
|
---|
3490 | DescCS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
3491 | }
|
---|
3492 | if (!(DescSS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
3493 | {
|
---|
3494 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewSS);
|
---|
3495 | if (rcStrict != VINF_SUCCESS)
|
---|
3496 | return rcStrict;
|
---|
3497 | DescSS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
3498 | }
|
---|
3499 |
|
---|
3500 | uint32_t fEFlagsMask = X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF
|
---|
3501 | | X86_EFL_TF | X86_EFL_DF | X86_EFL_OF | X86_EFL_NT;
|
---|
3502 | if (enmEffOpSize != IEMMODE_16BIT)
|
---|
3503 | fEFlagsMask |= X86_EFL_RF | X86_EFL_AC | X86_EFL_ID;
|
---|
3504 | if (pVCpu->iem.s.uCpl == 0)
|
---|
3505 | fEFlagsMask |= X86_EFL_IF | X86_EFL_IOPL | X86_EFL_VIF | X86_EFL_VIP; /* VM is 0 */
|
---|
3506 | else if (pVCpu->iem.s.uCpl <= pVCpu->cpum.GstCtx.eflags.Bits.u2IOPL)
|
---|
3507 | fEFlagsMask |= X86_EFL_IF;
|
---|
3508 | if (IEM_GET_TARGET_CPU(pVCpu) <= IEMTARGETCPU_386)
|
---|
3509 | fEFlagsMask &= ~(X86_EFL_AC | X86_EFL_ID | X86_EFL_VIF | X86_EFL_VIP);
|
---|
3510 | uint32_t fEFlagsNew = IEMMISC_GET_EFL(pVCpu);
|
---|
3511 | fEFlagsNew &= ~fEFlagsMask;
|
---|
3512 | fEFlagsNew |= uNewFlags & fEFlagsMask;
|
---|
3513 | #ifdef DBGFTRACE_ENABLED
|
---|
3514 | RTTraceBufAddMsgF(pVCpu->CTX_SUFF(pVM)->CTX_SUFF(hTraceBuf), "iret/%up%u %04x:%08x -> %04x:%04x %x %04x:%04x",
|
---|
3515 | pVCpu->iem.s.uCpl, uNewCs & X86_SEL_RPL, pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.eip,
|
---|
3516 | uNewCs, uNewEip, uNewFlags, uNewSS, uNewESP);
|
---|
3517 | #endif
|
---|
3518 |
|
---|
3519 | IEMMISC_SET_EFL(pVCpu, fEFlagsNew);
|
---|
3520 | pVCpu->cpum.GstCtx.rip = uNewEip;
|
---|
3521 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
|
---|
3522 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
|
---|
3523 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
3524 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCS.Legacy);
|
---|
3525 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimitCS;
|
---|
3526 | pVCpu->cpum.GstCtx.cs.u64Base = X86DESC_BASE(&DescCS.Legacy);
|
---|
3527 | pVCpu->iem.s.enmCpuMode = iemCalcCpuMode(pVCpu);
|
---|
3528 |
|
---|
3529 | pVCpu->cpum.GstCtx.ss.Sel = uNewSS;
|
---|
3530 | pVCpu->cpum.GstCtx.ss.ValidSel = uNewSS;
|
---|
3531 | pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
3532 | pVCpu->cpum.GstCtx.ss.Attr.u = X86DESC_GET_HID_ATTR(&DescSS.Legacy);
|
---|
3533 | pVCpu->cpum.GstCtx.ss.u32Limit = cbLimitSs;
|
---|
3534 | pVCpu->cpum.GstCtx.ss.u64Base = X86DESC_BASE(&DescSS.Legacy);
|
---|
3535 | if (!pVCpu->cpum.GstCtx.ss.Attr.n.u1DefBig)
|
---|
3536 | pVCpu->cpum.GstCtx.sp = (uint16_t)uNewESP;
|
---|
3537 | else
|
---|
3538 | pVCpu->cpum.GstCtx.rsp = uNewESP;
|
---|
3539 |
|
---|
3540 | pVCpu->iem.s.uCpl = uNewCs & X86_SEL_RPL;
|
---|
3541 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.ds);
|
---|
3542 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.es);
|
---|
3543 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.fs);
|
---|
3544 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.gs);
|
---|
3545 |
|
---|
3546 | /* Done! */
|
---|
3547 |
|
---|
3548 | }
|
---|
3549 | /*
|
---|
3550 | * Return to the same level.
|
---|
3551 | */
|
---|
3552 | else
|
---|
3553 | {
|
---|
3554 | /* Check EIP. */
|
---|
3555 | if (uNewEip > cbLimitCS)
|
---|
3556 | {
|
---|
3557 | Log(("iret %04x:%08x - EIP is out of bounds (%#x) -> #GP(0)\n", uNewCs, uNewEip, cbLimitCS));
|
---|
3558 | /** @todo Which is it, \#GP(0) or \#GP(sel)? */
|
---|
3559 | return iemRaiseSelectorBoundsBySelector(pVCpu, uNewCs);
|
---|
3560 | }
|
---|
3561 |
|
---|
3562 | /*
|
---|
3563 | * Commit the changes, marking CS first since it may fail.
|
---|
3564 | */
|
---|
3565 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
3566 | {
|
---|
3567 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewCs);
|
---|
3568 | if (rcStrict != VINF_SUCCESS)
|
---|
3569 | return rcStrict;
|
---|
3570 | DescCS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
3571 | }
|
---|
3572 |
|
---|
3573 | X86EFLAGS NewEfl;
|
---|
3574 | NewEfl.u = IEMMISC_GET_EFL(pVCpu);
|
---|
3575 | uint32_t fEFlagsMask = X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF
|
---|
3576 | | X86_EFL_TF | X86_EFL_DF | X86_EFL_OF | X86_EFL_NT;
|
---|
3577 | if (enmEffOpSize != IEMMODE_16BIT)
|
---|
3578 | fEFlagsMask |= X86_EFL_RF | X86_EFL_AC | X86_EFL_ID;
|
---|
3579 | if (pVCpu->iem.s.uCpl == 0)
|
---|
3580 | fEFlagsMask |= X86_EFL_IF | X86_EFL_IOPL | X86_EFL_VIF | X86_EFL_VIP; /* VM is 0 */
|
---|
3581 | else if (pVCpu->iem.s.uCpl <= NewEfl.Bits.u2IOPL)
|
---|
3582 | fEFlagsMask |= X86_EFL_IF;
|
---|
3583 | if (IEM_GET_TARGET_CPU(pVCpu) <= IEMTARGETCPU_386)
|
---|
3584 | fEFlagsMask &= ~(X86_EFL_AC | X86_EFL_ID | X86_EFL_VIF | X86_EFL_VIP);
|
---|
3585 | NewEfl.u &= ~fEFlagsMask;
|
---|
3586 | NewEfl.u |= fEFlagsMask & uNewFlags;
|
---|
3587 | #ifdef DBGFTRACE_ENABLED
|
---|
3588 | RTTraceBufAddMsgF(pVCpu->CTX_SUFF(pVM)->CTX_SUFF(hTraceBuf), "iret/%up %04x:%08x -> %04x:%04x %x %04x:%04llx",
|
---|
3589 | pVCpu->iem.s.uCpl, pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.eip,
|
---|
3590 | uNewCs, uNewEip, uNewFlags, pVCpu->cpum.GstCtx.ss.Sel, uNewRsp);
|
---|
3591 | #endif
|
---|
3592 |
|
---|
3593 | IEMMISC_SET_EFL(pVCpu, NewEfl.u);
|
---|
3594 | pVCpu->cpum.GstCtx.rip = uNewEip;
|
---|
3595 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
|
---|
3596 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
|
---|
3597 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
3598 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCS.Legacy);
|
---|
3599 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimitCS;
|
---|
3600 | pVCpu->cpum.GstCtx.cs.u64Base = X86DESC_BASE(&DescCS.Legacy);
|
---|
3601 | pVCpu->iem.s.enmCpuMode = iemCalcCpuMode(pVCpu);
|
---|
3602 | if (!pVCpu->cpum.GstCtx.ss.Attr.n.u1DefBig)
|
---|
3603 | pVCpu->cpum.GstCtx.sp = (uint16_t)uNewRsp;
|
---|
3604 | else
|
---|
3605 | pVCpu->cpum.GstCtx.rsp = uNewRsp;
|
---|
3606 | /* Done! */
|
---|
3607 | }
|
---|
3608 |
|
---|
3609 | /* Flush the prefetch buffer. */
|
---|
3610 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr); /** @todo may light flush if same ring? */
|
---|
3611 |
|
---|
3612 | /** @todo single stepping */
|
---|
3613 | return VINF_SUCCESS;
|
---|
3614 | }
|
---|
3615 |
|
---|
3616 |
|
---|
3617 | /**
|
---|
3618 | * Implements iret for long mode
|
---|
3619 | *
|
---|
3620 | * @param enmEffOpSize The effective operand size.
|
---|
3621 | */
|
---|
3622 | IEM_CIMPL_DEF_1(iemCImpl_iret_64bit, IEMMODE, enmEffOpSize)
|
---|
3623 | {
|
---|
3624 | NOREF(cbInstr);
|
---|
3625 |
|
---|
3626 | /*
|
---|
3627 | * Nested task return is not supported in long mode.
|
---|
3628 | */
|
---|
3629 | if (pVCpu->cpum.GstCtx.eflags.Bits.u1NT)
|
---|
3630 | {
|
---|
3631 | Log(("iretq with NT=1 (eflags=%#x) -> #GP(0)\n", pVCpu->cpum.GstCtx.eflags.u));
|
---|
3632 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
3633 | }
|
---|
3634 |
|
---|
3635 | /*
|
---|
3636 | * Normal return.
|
---|
3637 | *
|
---|
3638 | * Do the stack bits, but don't commit RSP before everything checks
|
---|
3639 | * out right.
|
---|
3640 | */
|
---|
3641 | VBOXSTRICTRC rcStrict;
|
---|
3642 | RTCPTRUNION uFrame;
|
---|
3643 | uint64_t uNewRip;
|
---|
3644 | uint16_t uNewCs;
|
---|
3645 | uint16_t uNewSs;
|
---|
3646 | uint32_t uNewFlags;
|
---|
3647 | uint64_t uNewRsp;
|
---|
3648 | if (enmEffOpSize == IEMMODE_64BIT)
|
---|
3649 | {
|
---|
3650 | rcStrict = iemMemStackPopBeginSpecial(pVCpu, 5*8, 7, &uFrame.pv, &uNewRsp);
|
---|
3651 | if (rcStrict != VINF_SUCCESS)
|
---|
3652 | return rcStrict;
|
---|
3653 | uNewRip = uFrame.pu64[0];
|
---|
3654 | uNewCs = (uint16_t)uFrame.pu64[1];
|
---|
3655 | uNewFlags = (uint32_t)uFrame.pu64[2];
|
---|
3656 | uNewRsp = uFrame.pu64[3];
|
---|
3657 | uNewSs = (uint16_t)uFrame.pu64[4];
|
---|
3658 | }
|
---|
3659 | else if (enmEffOpSize == IEMMODE_32BIT)
|
---|
3660 | {
|
---|
3661 | rcStrict = iemMemStackPopBeginSpecial(pVCpu, 5*4, 3, &uFrame.pv, &uNewRsp);
|
---|
3662 | if (rcStrict != VINF_SUCCESS)
|
---|
3663 | return rcStrict;
|
---|
3664 | uNewRip = uFrame.pu32[0];
|
---|
3665 | uNewCs = (uint16_t)uFrame.pu32[1];
|
---|
3666 | uNewFlags = uFrame.pu32[2];
|
---|
3667 | uNewRsp = uFrame.pu32[3];
|
---|
3668 | uNewSs = (uint16_t)uFrame.pu32[4];
|
---|
3669 | }
|
---|
3670 | else
|
---|
3671 | {
|
---|
3672 | Assert(enmEffOpSize == IEMMODE_16BIT);
|
---|
3673 | rcStrict = iemMemStackPopBeginSpecial(pVCpu, 5*2, 1, &uFrame.pv, &uNewRsp);
|
---|
3674 | if (rcStrict != VINF_SUCCESS)
|
---|
3675 | return rcStrict;
|
---|
3676 | uNewRip = uFrame.pu16[0];
|
---|
3677 | uNewCs = uFrame.pu16[1];
|
---|
3678 | uNewFlags = uFrame.pu16[2];
|
---|
3679 | uNewRsp = uFrame.pu16[3];
|
---|
3680 | uNewSs = uFrame.pu16[4];
|
---|
3681 | }
|
---|
3682 | rcStrict = iemMemStackPopDoneSpecial(pVCpu, (void *)uFrame.pv); /* don't use iemMemStackPopCommitSpecial here. */
|
---|
3683 | if (RT_LIKELY(rcStrict == VINF_SUCCESS))
|
---|
3684 | { /* extremely like */ }
|
---|
3685 | else
|
---|
3686 | return rcStrict;
|
---|
3687 | Log7(("iretq stack: cs:rip=%04x:%016RX64 rflags=%016RX64 ss:rsp=%04x:%016RX64\n", uNewCs, uNewRip, uNewFlags, uNewSs, uNewRsp));
|
---|
3688 |
|
---|
3689 | /*
|
---|
3690 | * Check stuff.
|
---|
3691 | */
|
---|
3692 | /* Read the CS descriptor. */
|
---|
3693 | if (!(uNewCs & X86_SEL_MASK_OFF_RPL))
|
---|
3694 | {
|
---|
3695 | Log(("iret %04x:%016RX64/%04x:%016RX64 -> invalid CS selector, #GP(0)\n", uNewCs, uNewRip, uNewSs, uNewRsp));
|
---|
3696 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
3697 | }
|
---|
3698 |
|
---|
3699 | IEMSELDESC DescCS;
|
---|
3700 | rcStrict = iemMemFetchSelDesc(pVCpu, &DescCS, uNewCs, X86_XCPT_GP);
|
---|
3701 | if (rcStrict != VINF_SUCCESS)
|
---|
3702 | {
|
---|
3703 | Log(("iret %04x:%016RX64/%04x:%016RX64 - rcStrict=%Rrc when fetching CS\n",
|
---|
3704 | uNewCs, uNewRip, uNewSs, uNewRsp, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
3705 | return rcStrict;
|
---|
3706 | }
|
---|
3707 |
|
---|
3708 | /* Must be a code descriptor. */
|
---|
3709 | if ( !DescCS.Legacy.Gen.u1DescType
|
---|
3710 | || !(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE))
|
---|
3711 | {
|
---|
3712 | Log(("iret %04x:%016RX64/%04x:%016RX64 - CS is not a code segment T=%u T=%#xu -> #GP\n",
|
---|
3713 | uNewCs, uNewRip, uNewSs, uNewRsp, DescCS.Legacy.Gen.u1DescType, DescCS.Legacy.Gen.u4Type));
|
---|
3714 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
3715 | }
|
---|
3716 |
|
---|
3717 | /* Privilege checks. */
|
---|
3718 | uint8_t const uNewCpl = uNewCs & X86_SEL_RPL;
|
---|
3719 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF))
|
---|
3720 | {
|
---|
3721 | if ((uNewCs & X86_SEL_RPL) != DescCS.Legacy.Gen.u2Dpl)
|
---|
3722 | {
|
---|
3723 | Log(("iret %04x:%016RX64 - RPL != DPL (%d) -> #GP\n", uNewCs, uNewRip, DescCS.Legacy.Gen.u2Dpl));
|
---|
3724 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
3725 | }
|
---|
3726 | }
|
---|
3727 | else if ((uNewCs & X86_SEL_RPL) < DescCS.Legacy.Gen.u2Dpl)
|
---|
3728 | {
|
---|
3729 | Log(("iret %04x:%016RX64 - RPL < DPL (%d) -> #GP\n", uNewCs, uNewRip, DescCS.Legacy.Gen.u2Dpl));
|
---|
3730 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
3731 | }
|
---|
3732 | if ((uNewCs & X86_SEL_RPL) < pVCpu->iem.s.uCpl)
|
---|
3733 | {
|
---|
3734 | Log(("iret %04x:%016RX64 - RPL < CPL (%d) -> #GP\n", uNewCs, uNewRip, pVCpu->iem.s.uCpl));
|
---|
3735 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
3736 | }
|
---|
3737 |
|
---|
3738 | /* Present? */
|
---|
3739 | if (!DescCS.Legacy.Gen.u1Present)
|
---|
3740 | {
|
---|
3741 | Log(("iret %04x:%016RX64/%04x:%016RX64 - CS not present -> #NP\n", uNewCs, uNewRip, uNewSs, uNewRsp));
|
---|
3742 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uNewCs);
|
---|
3743 | }
|
---|
3744 |
|
---|
3745 | uint32_t cbLimitCS = X86DESC_LIMIT_G(&DescCS.Legacy);
|
---|
3746 |
|
---|
3747 | /* Read the SS descriptor. */
|
---|
3748 | IEMSELDESC DescSS;
|
---|
3749 | if (!(uNewSs & X86_SEL_MASK_OFF_RPL))
|
---|
3750 | {
|
---|
3751 | if ( !DescCS.Legacy.Gen.u1Long
|
---|
3752 | || DescCS.Legacy.Gen.u1DefBig /** @todo exactly how does iret (and others) behave with u1Long=1 and u1DefBig=1? \#GP(sel)? */
|
---|
3753 | || uNewCpl > 2) /** @todo verify SS=0 impossible for ring-3. */
|
---|
3754 | {
|
---|
3755 | Log(("iret %04x:%016RX64/%04x:%016RX64 -> invalid SS selector, #GP(0)\n", uNewCs, uNewRip, uNewSs, uNewRsp));
|
---|
3756 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
3757 | }
|
---|
3758 | /* Make sure SS is sensible, marked as accessed etc. */
|
---|
3759 | iemMemFakeStackSelDesc(&DescSS, (uNewSs & X86_SEL_RPL));
|
---|
3760 | }
|
---|
3761 | else
|
---|
3762 | {
|
---|
3763 | rcStrict = iemMemFetchSelDesc(pVCpu, &DescSS, uNewSs, X86_XCPT_GP); /** @todo Correct exception? */
|
---|
3764 | if (rcStrict != VINF_SUCCESS)
|
---|
3765 | {
|
---|
3766 | Log(("iret %04x:%016RX64/%04x:%016RX64 - %Rrc when fetching SS\n",
|
---|
3767 | uNewCs, uNewRip, uNewSs, uNewRsp, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
3768 | return rcStrict;
|
---|
3769 | }
|
---|
3770 | }
|
---|
3771 |
|
---|
3772 | /* Privilege checks. */
|
---|
3773 | if ((uNewSs & X86_SEL_RPL) != (uNewCs & X86_SEL_RPL))
|
---|
3774 | {
|
---|
3775 | Log(("iret %04x:%016RX64/%04x:%016RX64 -> SS.RPL != CS.RPL -> #GP\n", uNewCs, uNewRip, uNewSs, uNewRsp));
|
---|
3776 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSs);
|
---|
3777 | }
|
---|
3778 |
|
---|
3779 | uint32_t cbLimitSs;
|
---|
3780 | if (!(uNewSs & X86_SEL_MASK_OFF_RPL))
|
---|
3781 | cbLimitSs = UINT32_MAX;
|
---|
3782 | else
|
---|
3783 | {
|
---|
3784 | if (DescSS.Legacy.Gen.u2Dpl != (uNewCs & X86_SEL_RPL))
|
---|
3785 | {
|
---|
3786 | Log(("iret %04x:%016RX64/%04x:%016RX64 -> SS.DPL (%d) != CS.RPL -> #GP\n",
|
---|
3787 | uNewCs, uNewRip, uNewSs, uNewRsp, DescSS.Legacy.Gen.u2Dpl));
|
---|
3788 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSs);
|
---|
3789 | }
|
---|
3790 |
|
---|
3791 | /* Must be a writeable data segment descriptor. */
|
---|
3792 | if (!DescSS.Legacy.Gen.u1DescType)
|
---|
3793 | {
|
---|
3794 | Log(("iret %04x:%016RX64/%04x:%016RX64 -> SS is system segment (%#x) -> #GP\n",
|
---|
3795 | uNewCs, uNewRip, uNewSs, uNewRsp, DescSS.Legacy.Gen.u4Type));
|
---|
3796 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSs);
|
---|
3797 | }
|
---|
3798 | if ((DescSS.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_WRITE)) != X86_SEL_TYPE_WRITE)
|
---|
3799 | {
|
---|
3800 | Log(("iret %04x:%016RX64/%04x:%016RX64 - not writable data segment (%#x) -> #GP\n",
|
---|
3801 | uNewCs, uNewRip, uNewSs, uNewRsp, DescSS.Legacy.Gen.u4Type));
|
---|
3802 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSs);
|
---|
3803 | }
|
---|
3804 |
|
---|
3805 | /* Present? */
|
---|
3806 | if (!DescSS.Legacy.Gen.u1Present)
|
---|
3807 | {
|
---|
3808 | Log(("iret %04x:%016RX64/%04x:%016RX64 -> SS not present -> #SS\n", uNewCs, uNewRip, uNewSs, uNewRsp));
|
---|
3809 | return iemRaiseStackSelectorNotPresentBySelector(pVCpu, uNewSs);
|
---|
3810 | }
|
---|
3811 | cbLimitSs = X86DESC_LIMIT_G(&DescSS.Legacy);
|
---|
3812 | }
|
---|
3813 |
|
---|
3814 | /* Check EIP. */
|
---|
3815 | if (DescCS.Legacy.Gen.u1Long)
|
---|
3816 | {
|
---|
3817 | if (!IEM_IS_CANONICAL(uNewRip))
|
---|
3818 | {
|
---|
3819 | Log(("iret %04x:%016RX64/%04x:%016RX64 -> RIP is not canonical -> #GP(0)\n",
|
---|
3820 | uNewCs, uNewRip, uNewSs, uNewRsp));
|
---|
3821 | return iemRaiseSelectorBoundsBySelector(pVCpu, uNewCs);
|
---|
3822 | }
|
---|
3823 | }
|
---|
3824 | else
|
---|
3825 | {
|
---|
3826 | if (uNewRip > cbLimitCS)
|
---|
3827 | {
|
---|
3828 | Log(("iret %04x:%016RX64/%04x:%016RX64 -> EIP is out of bounds (%#x) -> #GP(0)\n",
|
---|
3829 | uNewCs, uNewRip, uNewSs, uNewRsp, cbLimitCS));
|
---|
3830 | /** @todo Which is it, \#GP(0) or \#GP(sel)? */
|
---|
3831 | return iemRaiseSelectorBoundsBySelector(pVCpu, uNewCs);
|
---|
3832 | }
|
---|
3833 | }
|
---|
3834 |
|
---|
3835 | /*
|
---|
3836 | * Commit the changes, marking CS and SS accessed first since
|
---|
3837 | * that may fail.
|
---|
3838 | */
|
---|
3839 | /** @todo where exactly are these actually marked accessed by a real CPU? */
|
---|
3840 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
3841 | {
|
---|
3842 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewCs);
|
---|
3843 | if (rcStrict != VINF_SUCCESS)
|
---|
3844 | return rcStrict;
|
---|
3845 | DescCS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
3846 | }
|
---|
3847 | if (!(DescSS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
3848 | {
|
---|
3849 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewSs);
|
---|
3850 | if (rcStrict != VINF_SUCCESS)
|
---|
3851 | return rcStrict;
|
---|
3852 | DescSS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
3853 | }
|
---|
3854 |
|
---|
3855 | uint32_t fEFlagsMask = X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF
|
---|
3856 | | X86_EFL_TF | X86_EFL_DF | X86_EFL_OF | X86_EFL_NT;
|
---|
3857 | if (enmEffOpSize != IEMMODE_16BIT)
|
---|
3858 | fEFlagsMask |= X86_EFL_RF | X86_EFL_AC | X86_EFL_ID;
|
---|
3859 | if (pVCpu->iem.s.uCpl == 0)
|
---|
3860 | fEFlagsMask |= X86_EFL_IF | X86_EFL_IOPL | X86_EFL_VIF | X86_EFL_VIP; /* VM is ignored */
|
---|
3861 | else if (pVCpu->iem.s.uCpl <= pVCpu->cpum.GstCtx.eflags.Bits.u2IOPL)
|
---|
3862 | fEFlagsMask |= X86_EFL_IF;
|
---|
3863 | uint32_t fEFlagsNew = IEMMISC_GET_EFL(pVCpu);
|
---|
3864 | fEFlagsNew &= ~fEFlagsMask;
|
---|
3865 | fEFlagsNew |= uNewFlags & fEFlagsMask;
|
---|
3866 | #ifdef DBGFTRACE_ENABLED
|
---|
3867 | RTTraceBufAddMsgF(pVCpu->CTX_SUFF(pVM)->CTX_SUFF(hTraceBuf), "iret/%ul%u %08llx -> %04x:%04llx %llx %04x:%04llx",
|
---|
3868 | pVCpu->iem.s.uCpl, uNewCpl, pVCpu->cpum.GstCtx.rip, uNewCs, uNewRip, uNewFlags, uNewSs, uNewRsp);
|
---|
3869 | #endif
|
---|
3870 |
|
---|
3871 | IEMMISC_SET_EFL(pVCpu, fEFlagsNew);
|
---|
3872 | pVCpu->cpum.GstCtx.rip = uNewRip;
|
---|
3873 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
|
---|
3874 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
|
---|
3875 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
3876 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCS.Legacy);
|
---|
3877 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimitCS;
|
---|
3878 | pVCpu->cpum.GstCtx.cs.u64Base = X86DESC_BASE(&DescCS.Legacy);
|
---|
3879 | pVCpu->iem.s.enmCpuMode = iemCalcCpuMode(pVCpu);
|
---|
3880 | if (pVCpu->cpum.GstCtx.cs.Attr.n.u1Long || pVCpu->cpum.GstCtx.cs.Attr.n.u1DefBig)
|
---|
3881 | pVCpu->cpum.GstCtx.rsp = uNewRsp;
|
---|
3882 | else
|
---|
3883 | pVCpu->cpum.GstCtx.sp = (uint16_t)uNewRsp;
|
---|
3884 | pVCpu->cpum.GstCtx.ss.Sel = uNewSs;
|
---|
3885 | pVCpu->cpum.GstCtx.ss.ValidSel = uNewSs;
|
---|
3886 | if (!(uNewSs & X86_SEL_MASK_OFF_RPL))
|
---|
3887 | {
|
---|
3888 | pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
3889 | pVCpu->cpum.GstCtx.ss.Attr.u = X86DESCATTR_UNUSABLE | (uNewCpl << X86DESCATTR_DPL_SHIFT);
|
---|
3890 | pVCpu->cpum.GstCtx.ss.u32Limit = UINT32_MAX;
|
---|
3891 | pVCpu->cpum.GstCtx.ss.u64Base = 0;
|
---|
3892 | Log2(("iretq new SS: NULL\n"));
|
---|
3893 | }
|
---|
3894 | else
|
---|
3895 | {
|
---|
3896 | pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
3897 | pVCpu->cpum.GstCtx.ss.Attr.u = X86DESC_GET_HID_ATTR(&DescSS.Legacy);
|
---|
3898 | pVCpu->cpum.GstCtx.ss.u32Limit = cbLimitSs;
|
---|
3899 | pVCpu->cpum.GstCtx.ss.u64Base = X86DESC_BASE(&DescSS.Legacy);
|
---|
3900 | Log2(("iretq new SS: base=%#RX64 lim=%#x attr=%#x\n", pVCpu->cpum.GstCtx.ss.u64Base, pVCpu->cpum.GstCtx.ss.u32Limit, pVCpu->cpum.GstCtx.ss.Attr.u));
|
---|
3901 | }
|
---|
3902 |
|
---|
3903 | if (pVCpu->iem.s.uCpl != uNewCpl)
|
---|
3904 | {
|
---|
3905 | pVCpu->iem.s.uCpl = uNewCpl;
|
---|
3906 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCpl, &pVCpu->cpum.GstCtx.ds);
|
---|
3907 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCpl, &pVCpu->cpum.GstCtx.es);
|
---|
3908 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCpl, &pVCpu->cpum.GstCtx.fs);
|
---|
3909 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCpl, &pVCpu->cpum.GstCtx.gs);
|
---|
3910 | }
|
---|
3911 |
|
---|
3912 | /* Flush the prefetch buffer. */
|
---|
3913 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr); /** @todo may light flush if the ring + mode doesn't change */
|
---|
3914 |
|
---|
3915 | /** @todo single stepping */
|
---|
3916 | return VINF_SUCCESS;
|
---|
3917 | }
|
---|
3918 |
|
---|
3919 |
|
---|
3920 | /**
|
---|
3921 | * Implements iret.
|
---|
3922 | *
|
---|
3923 | * @param enmEffOpSize The effective operand size.
|
---|
3924 | */
|
---|
3925 | IEM_CIMPL_DEF_1(iemCImpl_iret, IEMMODE, enmEffOpSize)
|
---|
3926 | {
|
---|
3927 | bool fBlockingNmi = CPUMAreInterruptsInhibitedByNmi(&pVCpu->cpum.GstCtx);
|
---|
3928 |
|
---|
3929 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
3930 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
3931 | {
|
---|
3932 | /*
|
---|
3933 | * Record whether NMI (or virtual-NMI) blocking is in effect during the execution
|
---|
3934 | * of this IRET instruction. We need to provide this information as part of some
|
---|
3935 | * VM-exits.
|
---|
3936 | *
|
---|
3937 | * See Intel spec. 27.2.2 "Information for VM Exits Due to Vectored Events".
|
---|
3938 | */
|
---|
3939 | if (IEM_VMX_IS_PINCTLS_SET(pVCpu, VMX_PIN_CTLS_VIRT_NMI))
|
---|
3940 | pVCpu->cpum.GstCtx.hwvirt.vmx.fNmiUnblockingIret = pVCpu->cpum.GstCtx.hwvirt.vmx.fVirtNmiBlocking;
|
---|
3941 | else
|
---|
3942 | pVCpu->cpum.GstCtx.hwvirt.vmx.fNmiUnblockingIret = fBlockingNmi;
|
---|
3943 |
|
---|
3944 | /*
|
---|
3945 | * If "NMI exiting" is set, IRET does not affect blocking of NMIs.
|
---|
3946 | * See Intel Spec. 25.3 "Changes To Instruction Behavior In VMX Non-root Operation".
|
---|
3947 | */
|
---|
3948 | if (IEM_VMX_IS_PINCTLS_SET(pVCpu, VMX_PIN_CTLS_NMI_EXIT))
|
---|
3949 | fBlockingNmi = false;
|
---|
3950 |
|
---|
3951 | /* Clear virtual-NMI blocking, if any, before causing any further exceptions. */
|
---|
3952 | pVCpu->cpum.GstCtx.hwvirt.vmx.fVirtNmiBlocking = false;
|
---|
3953 | }
|
---|
3954 | #endif
|
---|
3955 |
|
---|
3956 | /*
|
---|
3957 | * The SVM nested-guest intercept for IRET takes priority over all exceptions,
|
---|
3958 | * The NMI is still held pending (which I assume means blocking of further NMIs
|
---|
3959 | * is in effect).
|
---|
3960 | *
|
---|
3961 | * See AMD spec. 15.9 "Instruction Intercepts".
|
---|
3962 | * See AMD spec. 15.21.9 "NMI Support".
|
---|
3963 | */
|
---|
3964 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_IRET))
|
---|
3965 | {
|
---|
3966 | Log(("iret: Guest intercept -> #VMEXIT\n"));
|
---|
3967 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
3968 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_IRET, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
3969 | }
|
---|
3970 |
|
---|
3971 | /*
|
---|
3972 | * Clear NMI blocking, if any, before causing any further exceptions.
|
---|
3973 | * See Intel spec. 6.7.1 "Handling Multiple NMIs".
|
---|
3974 | */
|
---|
3975 | if (fBlockingNmi)
|
---|
3976 | CPUMClearInterruptInhibitingByNmi(&pVCpu->cpum.GstCtx);
|
---|
3977 |
|
---|
3978 | /*
|
---|
3979 | * Call a mode specific worker.
|
---|
3980 | */
|
---|
3981 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
3982 | return IEM_CIMPL_CALL_1(iemCImpl_iret_real_v8086, enmEffOpSize);
|
---|
3983 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SREG_MASK | CPUMCTX_EXTRN_GDTR | CPUMCTX_EXTRN_LDTR);
|
---|
3984 | if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
|
---|
3985 | return IEM_CIMPL_CALL_1(iemCImpl_iret_64bit, enmEffOpSize);
|
---|
3986 | return IEM_CIMPL_CALL_1(iemCImpl_iret_prot, enmEffOpSize);
|
---|
3987 | }
|
---|
3988 |
|
---|
3989 |
|
---|
3990 | static void iemLoadallSetSelector(PVMCPUCC pVCpu, uint8_t iSegReg, uint16_t uSel)
|
---|
3991 | {
|
---|
3992 | PCPUMSELREGHID pHid = iemSRegGetHid(pVCpu, iSegReg);
|
---|
3993 |
|
---|
3994 | pHid->Sel = uSel;
|
---|
3995 | pHid->ValidSel = uSel;
|
---|
3996 | pHid->fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
3997 | }
|
---|
3998 |
|
---|
3999 |
|
---|
4000 | static void iemLoadall286SetDescCache(PVMCPUCC pVCpu, uint8_t iSegReg, uint8_t const *pbMem)
|
---|
4001 | {
|
---|
4002 | PCPUMSELREGHID pHid = iemSRegGetHid(pVCpu, iSegReg);
|
---|
4003 |
|
---|
4004 | /* The base is in the first three bytes. */
|
---|
4005 | pHid->u64Base = pbMem[0] + (pbMem[1] << 8) + (pbMem[2] << 16);
|
---|
4006 | /* The attributes are in the fourth byte. */
|
---|
4007 | pHid->Attr.u = pbMem[3];
|
---|
4008 | /* The limit is in the last two bytes. */
|
---|
4009 | pHid->u32Limit = pbMem[4] + (pbMem[5] << 8);
|
---|
4010 | }
|
---|
4011 |
|
---|
4012 |
|
---|
4013 | /**
|
---|
4014 | * Implements 286 LOADALL (286 CPUs only).
|
---|
4015 | */
|
---|
4016 | IEM_CIMPL_DEF_0(iemCImpl_loadall286)
|
---|
4017 | {
|
---|
4018 | NOREF(cbInstr);
|
---|
4019 |
|
---|
4020 | /* Data is loaded from a buffer at 800h. No checks are done on the
|
---|
4021 | * validity of loaded state.
|
---|
4022 | *
|
---|
4023 | * LOADALL only loads the internal CPU state, it does not access any
|
---|
4024 | * GDT, LDT, or similar tables.
|
---|
4025 | */
|
---|
4026 |
|
---|
4027 | if (pVCpu->iem.s.uCpl != 0)
|
---|
4028 | {
|
---|
4029 | Log(("loadall286: CPL must be 0 not %u -> #GP(0)\n", pVCpu->iem.s.uCpl));
|
---|
4030 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4031 | }
|
---|
4032 |
|
---|
4033 | uint8_t const *pbMem = NULL;
|
---|
4034 | uint16_t const *pa16Mem;
|
---|
4035 | uint8_t const *pa8Mem;
|
---|
4036 | RTGCPHYS GCPtrStart = 0x800; /* Fixed table location. */
|
---|
4037 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, (void **)&pbMem, 0x66, UINT8_MAX, GCPtrStart, IEM_ACCESS_SYS_R, 0);
|
---|
4038 | if (rcStrict != VINF_SUCCESS)
|
---|
4039 | return rcStrict;
|
---|
4040 |
|
---|
4041 | /* The MSW is at offset 0x06. */
|
---|
4042 | pa16Mem = (uint16_t const *)(pbMem + 0x06);
|
---|
4043 | /* Even LOADALL can't clear the MSW.PE bit, though it can set it. */
|
---|
4044 | uint64_t uNewCr0 = pVCpu->cpum.GstCtx.cr0 & ~(X86_CR0_MP | X86_CR0_EM | X86_CR0_TS);
|
---|
4045 | uNewCr0 |= *pa16Mem & (X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS);
|
---|
4046 | uint64_t const uOldCr0 = pVCpu->cpum.GstCtx.cr0;
|
---|
4047 |
|
---|
4048 | CPUMSetGuestCR0(pVCpu, uNewCr0);
|
---|
4049 | Assert(pVCpu->cpum.GstCtx.cr0 == uNewCr0);
|
---|
4050 |
|
---|
4051 | /* Inform PGM if mode changed. */
|
---|
4052 | if ((uNewCr0 & X86_CR0_PE) != (uOldCr0 & X86_CR0_PE))
|
---|
4053 | {
|
---|
4054 | int rc = PGMFlushTLB(pVCpu, pVCpu->cpum.GstCtx.cr3, true /* global */);
|
---|
4055 | AssertRCReturn(rc, rc);
|
---|
4056 | /* ignore informational status codes */
|
---|
4057 | }
|
---|
4058 | rcStrict = PGMChangeMode(pVCpu, pVCpu->cpum.GstCtx.cr0, pVCpu->cpum.GstCtx.cr4, pVCpu->cpum.GstCtx.msrEFER,
|
---|
4059 | false /* fForce */);
|
---|
4060 |
|
---|
4061 | /* TR selector is at offset 0x16. */
|
---|
4062 | pa16Mem = (uint16_t const *)(pbMem + 0x16);
|
---|
4063 | pVCpu->cpum.GstCtx.tr.Sel = pa16Mem[0];
|
---|
4064 | pVCpu->cpum.GstCtx.tr.ValidSel = pa16Mem[0];
|
---|
4065 | pVCpu->cpum.GstCtx.tr.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4066 |
|
---|
4067 | /* Followed by FLAGS... */
|
---|
4068 | pVCpu->cpum.GstCtx.eflags.u = pa16Mem[1] | X86_EFL_1;
|
---|
4069 | pVCpu->cpum.GstCtx.ip = pa16Mem[2]; /* ...and IP. */
|
---|
4070 |
|
---|
4071 | /* LDT is at offset 0x1C. */
|
---|
4072 | pa16Mem = (uint16_t const *)(pbMem + 0x1C);
|
---|
4073 | pVCpu->cpum.GstCtx.ldtr.Sel = pa16Mem[0];
|
---|
4074 | pVCpu->cpum.GstCtx.ldtr.ValidSel = pa16Mem[0];
|
---|
4075 | pVCpu->cpum.GstCtx.ldtr.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4076 |
|
---|
4077 | /* Segment registers are at offset 0x1E. */
|
---|
4078 | pa16Mem = (uint16_t const *)(pbMem + 0x1E);
|
---|
4079 | iemLoadallSetSelector(pVCpu, X86_SREG_DS, pa16Mem[0]);
|
---|
4080 | iemLoadallSetSelector(pVCpu, X86_SREG_SS, pa16Mem[1]);
|
---|
4081 | iemLoadallSetSelector(pVCpu, X86_SREG_CS, pa16Mem[2]);
|
---|
4082 | iemLoadallSetSelector(pVCpu, X86_SREG_ES, pa16Mem[3]);
|
---|
4083 |
|
---|
4084 | /* GPRs are at offset 0x26. */
|
---|
4085 | pa16Mem = (uint16_t const *)(pbMem + 0x26);
|
---|
4086 | pVCpu->cpum.GstCtx.di = pa16Mem[0];
|
---|
4087 | pVCpu->cpum.GstCtx.si = pa16Mem[1];
|
---|
4088 | pVCpu->cpum.GstCtx.bp = pa16Mem[2];
|
---|
4089 | pVCpu->cpum.GstCtx.sp = pa16Mem[3];
|
---|
4090 | pVCpu->cpum.GstCtx.bx = pa16Mem[4];
|
---|
4091 | pVCpu->cpum.GstCtx.dx = pa16Mem[5];
|
---|
4092 | pVCpu->cpum.GstCtx.cx = pa16Mem[6];
|
---|
4093 | pVCpu->cpum.GstCtx.ax = pa16Mem[7];
|
---|
4094 |
|
---|
4095 | /* Descriptor caches are at offset 0x36, 6 bytes per entry. */
|
---|
4096 | iemLoadall286SetDescCache(pVCpu, X86_SREG_ES, pbMem + 0x36);
|
---|
4097 | iemLoadall286SetDescCache(pVCpu, X86_SREG_CS, pbMem + 0x3C);
|
---|
4098 | iemLoadall286SetDescCache(pVCpu, X86_SREG_SS, pbMem + 0x42);
|
---|
4099 | iemLoadall286SetDescCache(pVCpu, X86_SREG_DS, pbMem + 0x48);
|
---|
4100 |
|
---|
4101 | /* GDTR contents are at offset 0x4E, 6 bytes. */
|
---|
4102 | RTGCPHYS GCPtrBase;
|
---|
4103 | uint16_t cbLimit;
|
---|
4104 | pa8Mem = pbMem + 0x4E;
|
---|
4105 | /* NB: Fourth byte "should be zero"; we are ignoring it. */
|
---|
4106 | GCPtrBase = pa8Mem[0] + (pa8Mem[1] << 8) + (pa8Mem[2] << 16);
|
---|
4107 | cbLimit = pa8Mem[4] + (pa8Mem[5] << 8);
|
---|
4108 | CPUMSetGuestGDTR(pVCpu, GCPtrBase, cbLimit);
|
---|
4109 |
|
---|
4110 | /* IDTR contents are at offset 0x5A, 6 bytes. */
|
---|
4111 | pa8Mem = pbMem + 0x5A;
|
---|
4112 | GCPtrBase = pa8Mem[0] + (pa8Mem[1] << 8) + (pa8Mem[2] << 16);
|
---|
4113 | cbLimit = pa8Mem[4] + (pa8Mem[5] << 8);
|
---|
4114 | CPUMSetGuestIDTR(pVCpu, GCPtrBase, cbLimit);
|
---|
4115 |
|
---|
4116 | Log(("LOADALL: GDTR:%08RX64/%04X, IDTR:%08RX64/%04X\n", pVCpu->cpum.GstCtx.gdtr.pGdt, pVCpu->cpum.GstCtx.gdtr.cbGdt, pVCpu->cpum.GstCtx.idtr.pIdt, pVCpu->cpum.GstCtx.idtr.cbIdt));
|
---|
4117 | Log(("LOADALL: CS:%04X, CS base:%08X, limit:%04X, attrs:%02X\n", pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.cs.u64Base, pVCpu->cpum.GstCtx.cs.u32Limit, pVCpu->cpum.GstCtx.cs.Attr.u));
|
---|
4118 | Log(("LOADALL: DS:%04X, DS base:%08X, limit:%04X, attrs:%02X\n", pVCpu->cpum.GstCtx.ds.Sel, pVCpu->cpum.GstCtx.ds.u64Base, pVCpu->cpum.GstCtx.ds.u32Limit, pVCpu->cpum.GstCtx.ds.Attr.u));
|
---|
4119 | Log(("LOADALL: ES:%04X, ES base:%08X, limit:%04X, attrs:%02X\n", pVCpu->cpum.GstCtx.es.Sel, pVCpu->cpum.GstCtx.es.u64Base, pVCpu->cpum.GstCtx.es.u32Limit, pVCpu->cpum.GstCtx.es.Attr.u));
|
---|
4120 | Log(("LOADALL: SS:%04X, SS base:%08X, limit:%04X, attrs:%02X\n", pVCpu->cpum.GstCtx.ss.Sel, pVCpu->cpum.GstCtx.ss.u64Base, pVCpu->cpum.GstCtx.ss.u32Limit, pVCpu->cpum.GstCtx.ss.Attr.u));
|
---|
4121 | Log(("LOADALL: SI:%04X, DI:%04X, AX:%04X, BX:%04X, CX:%04X, DX:%04X\n", pVCpu->cpum.GstCtx.si, pVCpu->cpum.GstCtx.di, pVCpu->cpum.GstCtx.bx, pVCpu->cpum.GstCtx.bx, pVCpu->cpum.GstCtx.cx, pVCpu->cpum.GstCtx.dx));
|
---|
4122 |
|
---|
4123 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)pbMem, IEM_ACCESS_SYS_R);
|
---|
4124 | if (rcStrict != VINF_SUCCESS)
|
---|
4125 | return rcStrict;
|
---|
4126 |
|
---|
4127 | /* The CPL may change. It is taken from the "DPL fields of the SS and CS
|
---|
4128 | * descriptor caches" but there is no word as to what happens if those are
|
---|
4129 | * not identical (probably bad things).
|
---|
4130 | */
|
---|
4131 | pVCpu->iem.s.uCpl = pVCpu->cpum.GstCtx.cs.Attr.n.u2Dpl;
|
---|
4132 |
|
---|
4133 | CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_HIDDEN_SEL_REGS | CPUM_CHANGED_IDTR | CPUM_CHANGED_GDTR | CPUM_CHANGED_TR | CPUM_CHANGED_LDTR);
|
---|
4134 |
|
---|
4135 | /* Flush the prefetch buffer. */
|
---|
4136 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr);
|
---|
4137 |
|
---|
4138 | /** @todo single stepping */
|
---|
4139 | return rcStrict;
|
---|
4140 | }
|
---|
4141 |
|
---|
4142 |
|
---|
4143 | /**
|
---|
4144 | * Implements SYSCALL (AMD and Intel64).
|
---|
4145 | */
|
---|
4146 | IEM_CIMPL_DEF_0(iemCImpl_syscall)
|
---|
4147 | {
|
---|
4148 | /** @todo hack, LOADALL should be decoded as such on a 286. */
|
---|
4149 | if (RT_UNLIKELY(pVCpu->iem.s.uTargetCpu == IEMTARGETCPU_286))
|
---|
4150 | return iemCImpl_loadall286(pVCpu, cbInstr);
|
---|
4151 |
|
---|
4152 | /*
|
---|
4153 | * Check preconditions.
|
---|
4154 | *
|
---|
4155 | * Note that CPUs described in the documentation may load a few odd values
|
---|
4156 | * into CS and SS than we allow here. This has yet to be checked on real
|
---|
4157 | * hardware.
|
---|
4158 | */
|
---|
4159 | if (!(pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_SCE))
|
---|
4160 | {
|
---|
4161 | Log(("syscall: Not enabled in EFER -> #UD\n"));
|
---|
4162 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4163 | }
|
---|
4164 | if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE))
|
---|
4165 | {
|
---|
4166 | Log(("syscall: Protected mode is required -> #GP(0)\n"));
|
---|
4167 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4168 | }
|
---|
4169 | if (IEM_IS_GUEST_CPU_INTEL(pVCpu) && !CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu)))
|
---|
4170 | {
|
---|
4171 | Log(("syscall: Only available in long mode on intel -> #UD\n"));
|
---|
4172 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4173 | }
|
---|
4174 |
|
---|
4175 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SYSCALL_MSRS);
|
---|
4176 |
|
---|
4177 | /** @todo verify RPL ignoring and CS=0xfff8 (i.e. SS == 0). */
|
---|
4178 | /** @todo what about LDT selectors? Shouldn't matter, really. */
|
---|
4179 | uint16_t uNewCs = (pVCpu->cpum.GstCtx.msrSTAR >> MSR_K6_STAR_SYSCALL_CS_SS_SHIFT) & X86_SEL_MASK_OFF_RPL;
|
---|
4180 | uint16_t uNewSs = uNewCs + 8;
|
---|
4181 | if (uNewCs == 0 || uNewSs == 0)
|
---|
4182 | {
|
---|
4183 | /** @todo Neither Intel nor AMD document this check. */
|
---|
4184 | Log(("syscall: msrSTAR.CS = 0 or SS = 0 -> #GP(0)\n"));
|
---|
4185 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4186 | }
|
---|
4187 |
|
---|
4188 | /* Long mode and legacy mode differs. */
|
---|
4189 | if (CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu)))
|
---|
4190 | {
|
---|
4191 | uint64_t uNewRip = pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT ? pVCpu->cpum.GstCtx.msrLSTAR : pVCpu->cpum.GstCtx. msrCSTAR;
|
---|
4192 |
|
---|
4193 | /* This test isn't in the docs, but I'm not trusting the guys writing
|
---|
4194 | the MSRs to have validated the values as canonical like they should. */
|
---|
4195 | if (!IEM_IS_CANONICAL(uNewRip))
|
---|
4196 | {
|
---|
4197 | /** @todo Intel claims this can't happen because IA32_LSTAR MSR can't be written with non-canonical address. */
|
---|
4198 | Log(("syscall: New RIP not canonical -> #UD\n"));
|
---|
4199 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4200 | }
|
---|
4201 |
|
---|
4202 | /*
|
---|
4203 | * Commit it.
|
---|
4204 | */
|
---|
4205 | Log(("syscall: %04x:%016RX64 [efl=%#llx] -> %04x:%016RX64\n", pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip, pVCpu->cpum.GstCtx.rflags.u, uNewCs, uNewRip));
|
---|
4206 | pVCpu->cpum.GstCtx.rcx = pVCpu->cpum.GstCtx.rip + cbInstr;
|
---|
4207 | pVCpu->cpum.GstCtx.rip = uNewRip;
|
---|
4208 |
|
---|
4209 | pVCpu->cpum.GstCtx.rflags.u &= ~X86_EFL_RF;
|
---|
4210 | pVCpu->cpum.GstCtx.r11 = pVCpu->cpum.GstCtx.rflags.u;
|
---|
4211 | pVCpu->cpum.GstCtx.rflags.u &= ~pVCpu->cpum.GstCtx.msrSFMASK;
|
---|
4212 | pVCpu->cpum.GstCtx.rflags.u |= X86_EFL_1;
|
---|
4213 |
|
---|
4214 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_L | X86DESCATTR_DT | X86_SEL_TYPE_ER_ACC;
|
---|
4215 | pVCpu->cpum.GstCtx.ss.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_D | X86DESCATTR_DT | X86_SEL_TYPE_RW_ACC;
|
---|
4216 | }
|
---|
4217 | else
|
---|
4218 | {
|
---|
4219 | /*
|
---|
4220 | * Commit it.
|
---|
4221 | */
|
---|
4222 | Log(("syscall: %04x:%08RX32 [efl=%#x] -> %04x:%08RX32\n", pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.eip, pVCpu->cpum.GstCtx.eflags.u, uNewCs, (uint32_t)(pVCpu->cpum.GstCtx.msrSTAR & MSR_K6_STAR_SYSCALL_EIP_MASK)));
|
---|
4223 | pVCpu->cpum.GstCtx.rcx = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
4224 | pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.msrSTAR & MSR_K6_STAR_SYSCALL_EIP_MASK;
|
---|
4225 | pVCpu->cpum.GstCtx.rflags.u &= ~(X86_EFL_VM | X86_EFL_IF | X86_EFL_RF);
|
---|
4226 |
|
---|
4227 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_D | X86DESCATTR_DT | X86_SEL_TYPE_ER_ACC;
|
---|
4228 | pVCpu->cpum.GstCtx.ss.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_D | X86DESCATTR_DT | X86_SEL_TYPE_RW_ACC;
|
---|
4229 | }
|
---|
4230 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
|
---|
4231 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
|
---|
4232 | pVCpu->cpum.GstCtx.cs.u64Base = 0;
|
---|
4233 | pVCpu->cpum.GstCtx.cs.u32Limit = UINT32_MAX;
|
---|
4234 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4235 |
|
---|
4236 | pVCpu->cpum.GstCtx.ss.Sel = uNewSs;
|
---|
4237 | pVCpu->cpum.GstCtx.ss.ValidSel = uNewSs;
|
---|
4238 | pVCpu->cpum.GstCtx.ss.u64Base = 0;
|
---|
4239 | pVCpu->cpum.GstCtx.ss.u32Limit = UINT32_MAX;
|
---|
4240 | pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4241 |
|
---|
4242 | pVCpu->iem.s.uCpl = 0;
|
---|
4243 | pVCpu->iem.s.enmCpuMode = iemCalcCpuMode(pVCpu);
|
---|
4244 |
|
---|
4245 | /* Flush the prefetch buffer. */
|
---|
4246 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr);
|
---|
4247 |
|
---|
4248 | /** @todo single step */
|
---|
4249 | return VINF_SUCCESS;
|
---|
4250 | }
|
---|
4251 |
|
---|
4252 |
|
---|
4253 | /**
|
---|
4254 | * Implements SYSRET (AMD and Intel64).
|
---|
4255 | */
|
---|
4256 | IEM_CIMPL_DEF_0(iemCImpl_sysret)
|
---|
4257 |
|
---|
4258 | {
|
---|
4259 | RT_NOREF_PV(cbInstr);
|
---|
4260 |
|
---|
4261 | /*
|
---|
4262 | * Check preconditions.
|
---|
4263 | *
|
---|
4264 | * Note that CPUs described in the documentation may load a few odd values
|
---|
4265 | * into CS and SS than we allow here. This has yet to be checked on real
|
---|
4266 | * hardware.
|
---|
4267 | */
|
---|
4268 | if (!(pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_SCE))
|
---|
4269 | {
|
---|
4270 | Log(("sysret: Not enabled in EFER -> #UD\n"));
|
---|
4271 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4272 | }
|
---|
4273 | if (IEM_IS_GUEST_CPU_INTEL(pVCpu) && !CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu)))
|
---|
4274 | {
|
---|
4275 | Log(("sysret: Only available in long mode on intel -> #UD\n"));
|
---|
4276 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4277 | }
|
---|
4278 | if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE))
|
---|
4279 | {
|
---|
4280 | Log(("sysret: Protected mode is required -> #GP(0)\n"));
|
---|
4281 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4282 | }
|
---|
4283 | if (pVCpu->iem.s.uCpl != 0)
|
---|
4284 | {
|
---|
4285 | Log(("sysret: CPL must be 0 not %u -> #GP(0)\n", pVCpu->iem.s.uCpl));
|
---|
4286 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4287 | }
|
---|
4288 |
|
---|
4289 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SYSCALL_MSRS);
|
---|
4290 |
|
---|
4291 | /** @todo Does SYSRET verify CS != 0 and SS != 0? Neither is valid in ring-3. */
|
---|
4292 | uint16_t uNewCs = (pVCpu->cpum.GstCtx.msrSTAR >> MSR_K6_STAR_SYSRET_CS_SS_SHIFT) & X86_SEL_MASK_OFF_RPL;
|
---|
4293 | uint16_t uNewSs = uNewCs + 8;
|
---|
4294 | if (pVCpu->iem.s.enmEffOpSize == IEMMODE_64BIT)
|
---|
4295 | uNewCs += 16;
|
---|
4296 | if (uNewCs == 0 || uNewSs == 0)
|
---|
4297 | {
|
---|
4298 | Log(("sysret: msrSTAR.CS = 0 or SS = 0 -> #GP(0)\n"));
|
---|
4299 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4300 | }
|
---|
4301 |
|
---|
4302 | /*
|
---|
4303 | * Commit it.
|
---|
4304 | */
|
---|
4305 | if (CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu)))
|
---|
4306 | {
|
---|
4307 | if (pVCpu->iem.s.enmEffOpSize == IEMMODE_64BIT)
|
---|
4308 | {
|
---|
4309 | Log(("sysret: %04x:%016RX64 [efl=%#llx] -> %04x:%016RX64 [r11=%#llx]\n", pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip, pVCpu->cpum.GstCtx.rflags.u, uNewCs, pVCpu->cpum.GstCtx.rcx, pVCpu->cpum.GstCtx.r11));
|
---|
4310 | /* Note! We disregard intel manual regarding the RCX canonical
|
---|
4311 | check, ask intel+xen why AMD doesn't do it. */
|
---|
4312 | pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.rcx;
|
---|
4313 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_L | X86DESCATTR_DT | X86_SEL_TYPE_ER_ACC
|
---|
4314 | | (3 << X86DESCATTR_DPL_SHIFT);
|
---|
4315 | }
|
---|
4316 | else
|
---|
4317 | {
|
---|
4318 | Log(("sysret: %04x:%016RX64 [efl=%#llx] -> %04x:%08RX32 [r11=%#llx]\n", pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip, pVCpu->cpum.GstCtx.rflags.u, uNewCs, pVCpu->cpum.GstCtx.ecx, pVCpu->cpum.GstCtx.r11));
|
---|
4319 | pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.ecx;
|
---|
4320 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_D | X86DESCATTR_DT | X86_SEL_TYPE_ER_ACC
|
---|
4321 | | (3 << X86DESCATTR_DPL_SHIFT);
|
---|
4322 | }
|
---|
4323 | /** @todo testcase: See what kind of flags we can make SYSRET restore and
|
---|
4324 | * what it really ignores. RF and VM are hinted at being zero, by AMD.
|
---|
4325 | * Intel says: RFLAGS := (R11 & 3C7FD7H) | 2; */
|
---|
4326 | pVCpu->cpum.GstCtx.rflags.u = pVCpu->cpum.GstCtx.r11 & (X86_EFL_POPF_BITS | X86_EFL_VIF | X86_EFL_VIP);
|
---|
4327 | pVCpu->cpum.GstCtx.rflags.u |= X86_EFL_1;
|
---|
4328 | }
|
---|
4329 | else
|
---|
4330 | {
|
---|
4331 | Log(("sysret: %04x:%08RX32 [efl=%#x] -> %04x:%08RX32\n", pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.eip, pVCpu->cpum.GstCtx.eflags.u, uNewCs, pVCpu->cpum.GstCtx.ecx));
|
---|
4332 | pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.rcx;
|
---|
4333 | pVCpu->cpum.GstCtx.rflags.u |= X86_EFL_IF;
|
---|
4334 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_D | X86DESCATTR_DT | X86_SEL_TYPE_ER_ACC
|
---|
4335 | | (3 << X86DESCATTR_DPL_SHIFT);
|
---|
4336 | }
|
---|
4337 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs | 3;
|
---|
4338 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs | 3;
|
---|
4339 | pVCpu->cpum.GstCtx.cs.u64Base = 0;
|
---|
4340 | pVCpu->cpum.GstCtx.cs.u32Limit = UINT32_MAX;
|
---|
4341 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4342 |
|
---|
4343 | pVCpu->cpum.GstCtx.ss.Sel = uNewSs | 3;
|
---|
4344 | pVCpu->cpum.GstCtx.ss.ValidSel = uNewSs | 3;
|
---|
4345 | pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4346 | /* The SS hidden bits remains unchanged says AMD. To that I say "Yeah, right!". */
|
---|
4347 | pVCpu->cpum.GstCtx.ss.Attr.u |= (3 << X86DESCATTR_DPL_SHIFT);
|
---|
4348 | /** @todo Testcase: verify that SS.u1Long and SS.u1DefBig are left unchanged
|
---|
4349 | * on sysret. */
|
---|
4350 |
|
---|
4351 | pVCpu->iem.s.uCpl = 3;
|
---|
4352 | pVCpu->iem.s.enmCpuMode = iemCalcCpuMode(pVCpu);
|
---|
4353 |
|
---|
4354 | /* Flush the prefetch buffer. */
|
---|
4355 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr);
|
---|
4356 |
|
---|
4357 | /** @todo single step */
|
---|
4358 | return VINF_SUCCESS;
|
---|
4359 | }
|
---|
4360 |
|
---|
4361 |
|
---|
4362 | /**
|
---|
4363 | * Implements SYSENTER (Intel, 32-bit AMD).
|
---|
4364 | */
|
---|
4365 | IEM_CIMPL_DEF_0(iemCImpl_sysenter)
|
---|
4366 | {
|
---|
4367 | RT_NOREF(cbInstr);
|
---|
4368 |
|
---|
4369 | /*
|
---|
4370 | * Check preconditions.
|
---|
4371 | *
|
---|
4372 | * Note that CPUs described in the documentation may load a few odd values
|
---|
4373 | * into CS and SS than we allow here. This has yet to be checked on real
|
---|
4374 | * hardware.
|
---|
4375 | */
|
---|
4376 | if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fSysEnter)
|
---|
4377 | {
|
---|
4378 | Log(("sysenter: not supported -=> #UD\n"));
|
---|
4379 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4380 | }
|
---|
4381 | if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE))
|
---|
4382 | {
|
---|
4383 | Log(("sysenter: Protected or long mode is required -> #GP(0)\n"));
|
---|
4384 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4385 | }
|
---|
4386 | bool fIsLongMode = CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu));
|
---|
4387 | if (IEM_IS_GUEST_CPU_AMD(pVCpu) && fIsLongMode)
|
---|
4388 | {
|
---|
4389 | Log(("sysenter: Only available in protected mode on AMD -> #UD\n"));
|
---|
4390 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4391 | }
|
---|
4392 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SYSENTER_MSRS);
|
---|
4393 | uint16_t uNewCs = pVCpu->cpum.GstCtx.SysEnter.cs;
|
---|
4394 | if ((uNewCs & X86_SEL_MASK_OFF_RPL) == 0)
|
---|
4395 | {
|
---|
4396 | Log(("sysenter: SYSENTER_CS = %#x -> #GP(0)\n", uNewCs));
|
---|
4397 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4398 | }
|
---|
4399 |
|
---|
4400 | /* This test isn't in the docs, it's just a safeguard against missing
|
---|
4401 | canonical checks when writing the registers. */
|
---|
4402 | if (RT_LIKELY( !fIsLongMode
|
---|
4403 | || ( IEM_IS_CANONICAL(pVCpu->cpum.GstCtx.SysEnter.eip)
|
---|
4404 | && IEM_IS_CANONICAL(pVCpu->cpum.GstCtx.SysEnter.esp))))
|
---|
4405 | { /* likely */ }
|
---|
4406 | else
|
---|
4407 | {
|
---|
4408 | Log(("sysenter: SYSENTER_EIP = %#RX64 or/and SYSENTER_ESP = %#RX64 not canonical -> #GP(0)\n",
|
---|
4409 | pVCpu->cpum.GstCtx.SysEnter.eip, pVCpu->cpum.GstCtx.SysEnter.esp));
|
---|
4410 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4411 | }
|
---|
4412 |
|
---|
4413 | /** @todo Test: Sysenter from ring-0, ring-1 and ring-2. */
|
---|
4414 |
|
---|
4415 | /*
|
---|
4416 | * Update registers and commit.
|
---|
4417 | */
|
---|
4418 | if (fIsLongMode)
|
---|
4419 | {
|
---|
4420 | Log(("sysenter: %04x:%016RX64 [efl=%#llx] -> %04x:%016RX64\n", pVCpu->cpum.GstCtx.cs, pVCpu->cpum.GstCtx.rip,
|
---|
4421 | pVCpu->cpum.GstCtx.rflags.u, uNewCs & X86_SEL_MASK_OFF_RPL, pVCpu->cpum.GstCtx.SysEnter.eip));
|
---|
4422 | pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.SysEnter.eip;
|
---|
4423 | pVCpu->cpum.GstCtx.rsp = pVCpu->cpum.GstCtx.SysEnter.esp;
|
---|
4424 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_L | X86DESCATTR_G | X86DESCATTR_P | X86DESCATTR_DT
|
---|
4425 | | X86DESCATTR_LIMIT_HIGH | X86_SEL_TYPE_ER_ACC;
|
---|
4426 | }
|
---|
4427 | else
|
---|
4428 | {
|
---|
4429 | Log(("sysenter: %04x:%08RX32 [efl=%#llx] -> %04x:%08RX32\n", pVCpu->cpum.GstCtx.cs, (uint32_t)pVCpu->cpum.GstCtx.rip,
|
---|
4430 | pVCpu->cpum.GstCtx.rflags.u, uNewCs & X86_SEL_MASK_OFF_RPL, (uint32_t)pVCpu->cpum.GstCtx.SysEnter.eip));
|
---|
4431 | pVCpu->cpum.GstCtx.rip = (uint32_t)pVCpu->cpum.GstCtx.SysEnter.eip;
|
---|
4432 | pVCpu->cpum.GstCtx.rsp = (uint32_t)pVCpu->cpum.GstCtx.SysEnter.esp;
|
---|
4433 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_D | X86DESCATTR_G | X86DESCATTR_P | X86DESCATTR_DT
|
---|
4434 | | X86DESCATTR_LIMIT_HIGH | X86_SEL_TYPE_ER_ACC;
|
---|
4435 | }
|
---|
4436 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs & X86_SEL_MASK_OFF_RPL;
|
---|
4437 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs & X86_SEL_MASK_OFF_RPL;
|
---|
4438 | pVCpu->cpum.GstCtx.cs.u64Base = 0;
|
---|
4439 | pVCpu->cpum.GstCtx.cs.u32Limit = UINT32_MAX;
|
---|
4440 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4441 |
|
---|
4442 | pVCpu->cpum.GstCtx.ss.Sel = (uNewCs & X86_SEL_MASK_OFF_RPL) + 8;
|
---|
4443 | pVCpu->cpum.GstCtx.ss.ValidSel = (uNewCs & X86_SEL_MASK_OFF_RPL) + 8;
|
---|
4444 | pVCpu->cpum.GstCtx.ss.u64Base = 0;
|
---|
4445 | pVCpu->cpum.GstCtx.ss.u32Limit = UINT32_MAX;
|
---|
4446 | pVCpu->cpum.GstCtx.ss.Attr.u = X86DESCATTR_D | X86DESCATTR_G | X86DESCATTR_P | X86DESCATTR_DT
|
---|
4447 | | X86DESCATTR_LIMIT_HIGH | X86_SEL_TYPE_RW_ACC;
|
---|
4448 | pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4449 |
|
---|
4450 | pVCpu->cpum.GstCtx.rflags.Bits.u1IF = 0;
|
---|
4451 | pVCpu->cpum.GstCtx.rflags.Bits.u1VM = 0;
|
---|
4452 | pVCpu->cpum.GstCtx.rflags.Bits.u1RF = 0;
|
---|
4453 |
|
---|
4454 | pVCpu->iem.s.uCpl = 0;
|
---|
4455 |
|
---|
4456 | /* Flush the prefetch buffer. */
|
---|
4457 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr);
|
---|
4458 |
|
---|
4459 | /** @todo single stepping */
|
---|
4460 | return VINF_SUCCESS;
|
---|
4461 | }
|
---|
4462 |
|
---|
4463 |
|
---|
4464 | /**
|
---|
4465 | * Implements SYSEXIT (Intel, 32-bit AMD).
|
---|
4466 | *
|
---|
4467 | * @param enmEffOpSize The effective operand size.
|
---|
4468 | */
|
---|
4469 | IEM_CIMPL_DEF_1(iemCImpl_sysexit, IEMMODE, enmEffOpSize)
|
---|
4470 | {
|
---|
4471 | RT_NOREF(cbInstr);
|
---|
4472 |
|
---|
4473 | /*
|
---|
4474 | * Check preconditions.
|
---|
4475 | *
|
---|
4476 | * Note that CPUs described in the documentation may load a few odd values
|
---|
4477 | * into CS and SS than we allow here. This has yet to be checked on real
|
---|
4478 | * hardware.
|
---|
4479 | */
|
---|
4480 | if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fSysEnter)
|
---|
4481 | {
|
---|
4482 | Log(("sysexit: not supported -=> #UD\n"));
|
---|
4483 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4484 | }
|
---|
4485 | if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE))
|
---|
4486 | {
|
---|
4487 | Log(("sysexit: Protected or long mode is required -> #GP(0)\n"));
|
---|
4488 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4489 | }
|
---|
4490 | bool fIsLongMode = CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu));
|
---|
4491 | if (IEM_IS_GUEST_CPU_AMD(pVCpu) && fIsLongMode)
|
---|
4492 | {
|
---|
4493 | Log(("sysexit: Only available in protected mode on AMD -> #UD\n"));
|
---|
4494 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4495 | }
|
---|
4496 | if (pVCpu->iem.s.uCpl != 0)
|
---|
4497 | {
|
---|
4498 | Log(("sysexit: CPL(=%u) != 0 -> #GP(0)\n", pVCpu->iem.s.uCpl));
|
---|
4499 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4500 | }
|
---|
4501 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SYSENTER_MSRS);
|
---|
4502 | uint16_t uNewCs = pVCpu->cpum.GstCtx.SysEnter.cs;
|
---|
4503 | if ((uNewCs & X86_SEL_MASK_OFF_RPL) == 0)
|
---|
4504 | {
|
---|
4505 | Log(("sysexit: SYSENTER_CS = %#x -> #GP(0)\n", uNewCs));
|
---|
4506 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4507 | }
|
---|
4508 |
|
---|
4509 | /*
|
---|
4510 | * Update registers and commit.
|
---|
4511 | */
|
---|
4512 | if (enmEffOpSize == IEMMODE_64BIT)
|
---|
4513 | {
|
---|
4514 | Log(("sysexit: %04x:%016RX64 [efl=%#llx] -> %04x:%016RX64\n", pVCpu->cpum.GstCtx.cs, pVCpu->cpum.GstCtx.rip,
|
---|
4515 | pVCpu->cpum.GstCtx.rflags.u, (uNewCs | 3) + 32, pVCpu->cpum.GstCtx.rcx));
|
---|
4516 | pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.rdx;
|
---|
4517 | pVCpu->cpum.GstCtx.rsp = pVCpu->cpum.GstCtx.rcx;
|
---|
4518 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_L | X86DESCATTR_G | X86DESCATTR_P | X86DESCATTR_DT
|
---|
4519 | | X86DESCATTR_LIMIT_HIGH | X86_SEL_TYPE_ER_ACC | (3 << X86DESCATTR_DPL_SHIFT);
|
---|
4520 | pVCpu->cpum.GstCtx.cs.Sel = (uNewCs | 3) + 32;
|
---|
4521 | pVCpu->cpum.GstCtx.cs.ValidSel = (uNewCs | 3) + 32;
|
---|
4522 | pVCpu->cpum.GstCtx.ss.Sel = (uNewCs | 3) + 40;
|
---|
4523 | pVCpu->cpum.GstCtx.ss.ValidSel = (uNewCs | 3) + 40;
|
---|
4524 | }
|
---|
4525 | else
|
---|
4526 | {
|
---|
4527 | Log(("sysexit: %04x:%08RX64 [efl=%#llx] -> %04x:%08RX32\n", pVCpu->cpum.GstCtx.cs, pVCpu->cpum.GstCtx.rip,
|
---|
4528 | pVCpu->cpum.GstCtx.rflags.u, (uNewCs | 3) + 16, (uint32_t)pVCpu->cpum.GstCtx.edx));
|
---|
4529 | pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.edx;
|
---|
4530 | pVCpu->cpum.GstCtx.rsp = pVCpu->cpum.GstCtx.ecx;
|
---|
4531 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_D | X86DESCATTR_G | X86DESCATTR_P | X86DESCATTR_DT
|
---|
4532 | | X86DESCATTR_LIMIT_HIGH | X86_SEL_TYPE_ER_ACC | (3 << X86DESCATTR_DPL_SHIFT);
|
---|
4533 | pVCpu->cpum.GstCtx.cs.Sel = (uNewCs | 3) + 16;
|
---|
4534 | pVCpu->cpum.GstCtx.cs.ValidSel = (uNewCs | 3) + 16;
|
---|
4535 | pVCpu->cpum.GstCtx.ss.Sel = (uNewCs | 3) + 24;
|
---|
4536 | pVCpu->cpum.GstCtx.ss.ValidSel = (uNewCs | 3) + 24;
|
---|
4537 | }
|
---|
4538 | pVCpu->cpum.GstCtx.cs.u64Base = 0;
|
---|
4539 | pVCpu->cpum.GstCtx.cs.u32Limit = UINT32_MAX;
|
---|
4540 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4541 |
|
---|
4542 | pVCpu->cpum.GstCtx.ss.u64Base = 0;
|
---|
4543 | pVCpu->cpum.GstCtx.ss.u32Limit = UINT32_MAX;
|
---|
4544 | pVCpu->cpum.GstCtx.ss.Attr.u = X86DESCATTR_D | X86DESCATTR_G | X86DESCATTR_P | X86DESCATTR_DT
|
---|
4545 | | X86DESCATTR_LIMIT_HIGH | X86_SEL_TYPE_RW_ACC | (3 << X86DESCATTR_DPL_SHIFT);
|
---|
4546 | pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4547 | pVCpu->cpum.GstCtx.rflags.Bits.u1RF = 0;
|
---|
4548 |
|
---|
4549 | pVCpu->iem.s.uCpl = 3;
|
---|
4550 | /** @todo single stepping */
|
---|
4551 |
|
---|
4552 | /* Flush the prefetch buffer. */
|
---|
4553 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr);
|
---|
4554 |
|
---|
4555 | return VINF_SUCCESS;
|
---|
4556 | }
|
---|
4557 |
|
---|
4558 |
|
---|
4559 | /**
|
---|
4560 | * Completes a MOV SReg,XXX or POP SReg instruction.
|
---|
4561 | *
|
---|
4562 | * When not modifying SS or when we're already in an interrupt shadow we
|
---|
4563 | * can update RIP and finish the instruction the normal way.
|
---|
4564 | *
|
---|
4565 | * Otherwise, the MOV/POP SS interrupt shadow that we now enable will block
|
---|
4566 | * both TF and DBx events. The TF will be ignored while the DBx ones will
|
---|
4567 | * be delayed till the next instruction boundrary. For more details see
|
---|
4568 | * @sdmv3{077,200,6.8.3,Masking Exceptions and Interrupts When Switching Stacks}.
|
---|
4569 | */
|
---|
4570 | DECLINLINE(VBOXSTRICTRC) iemCImpl_LoadSRegFinish(PVMCPUCC pVCpu, uint8_t cbInstr, uint8_t iSegReg)
|
---|
4571 | {
|
---|
4572 | if (iSegReg != X86_SREG_SS || CPUMIsInInterruptShadow(&pVCpu->cpum.GstCtx))
|
---|
4573 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
4574 |
|
---|
4575 | iemRegAddToRip(pVCpu, cbInstr);
|
---|
4576 | pVCpu->cpum.GstCtx.eflags.uBoth &= ~X86_EFL_RF; /* Shadow int isn't set and DRx is delayed, so only clear RF. */
|
---|
4577 | CPUMSetInInterruptShadowSs(&pVCpu->cpum.GstCtx);
|
---|
4578 |
|
---|
4579 | return VINF_SUCCESS;
|
---|
4580 | }
|
---|
4581 |
|
---|
4582 |
|
---|
4583 | /**
|
---|
4584 | * Common worker for 'pop SReg', 'mov SReg, GReg' and 'lXs GReg, reg/mem'.
|
---|
4585 | *
|
---|
4586 | * @param pVCpu The cross context virtual CPU structure of the calling
|
---|
4587 | * thread.
|
---|
4588 | * @param iSegReg The segment register number (valid).
|
---|
4589 | * @param uSel The new selector value.
|
---|
4590 | */
|
---|
4591 | static VBOXSTRICTRC iemCImpl_LoadSRegWorker(PVMCPUCC pVCpu, uint8_t iSegReg, uint16_t uSel)
|
---|
4592 | {
|
---|
4593 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SREG_FROM_IDX(iSegReg));
|
---|
4594 | uint16_t *pSel = iemSRegRef(pVCpu, iSegReg);
|
---|
4595 | PCPUMSELREGHID pHid = iemSRegGetHid(pVCpu, iSegReg);
|
---|
4596 |
|
---|
4597 | Assert(iSegReg <= X86_SREG_GS && iSegReg != X86_SREG_CS);
|
---|
4598 |
|
---|
4599 | /*
|
---|
4600 | * Real mode and V8086 mode are easy.
|
---|
4601 | */
|
---|
4602 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
4603 | {
|
---|
4604 | *pSel = uSel;
|
---|
4605 | pHid->u64Base = (uint32_t)uSel << 4;
|
---|
4606 | pHid->ValidSel = uSel;
|
---|
4607 | pHid->fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4608 | #if 0 /* AMD Volume 2, chapter 4.1 - "real mode segmentation" - states that limit and attributes are untouched. */
|
---|
4609 | /** @todo Does the CPU actually load limits and attributes in the
|
---|
4610 | * real/V8086 mode segment load case? It doesn't for CS in far
|
---|
4611 | * jumps... Affects unreal mode. */
|
---|
4612 | pHid->u32Limit = 0xffff;
|
---|
4613 | pHid->Attr.u = 0;
|
---|
4614 | pHid->Attr.n.u1Present = 1;
|
---|
4615 | pHid->Attr.n.u1DescType = 1;
|
---|
4616 | pHid->Attr.n.u4Type = iSegReg != X86_SREG_CS
|
---|
4617 | ? X86_SEL_TYPE_RW
|
---|
4618 | : X86_SEL_TYPE_READ | X86_SEL_TYPE_CODE;
|
---|
4619 | #endif
|
---|
4620 | }
|
---|
4621 | /*
|
---|
4622 | * Protected mode.
|
---|
4623 | *
|
---|
4624 | * Check if it's a null segment selector value first, that's OK for DS, ES,
|
---|
4625 | * FS and GS. If not null, then we have to load and parse the descriptor.
|
---|
4626 | */
|
---|
4627 | else if (!(uSel & X86_SEL_MASK_OFF_RPL))
|
---|
4628 | {
|
---|
4629 | Assert(iSegReg != X86_SREG_CS); /** @todo testcase for \#UD on MOV CS, ax! */
|
---|
4630 | if (iSegReg == X86_SREG_SS)
|
---|
4631 | {
|
---|
4632 | /* In 64-bit kernel mode, the stack can be 0 because of the way
|
---|
4633 | interrupts are dispatched. AMD seems to have a slighly more
|
---|
4634 | relaxed relationship to SS.RPL than intel does. */
|
---|
4635 | /** @todo We cannot 'mov ss, 3' in 64-bit kernel mode, can we? There is a testcase (bs-cpu-xcpt-1), but double check this! */
|
---|
4636 | if ( pVCpu->iem.s.enmCpuMode != IEMMODE_64BIT
|
---|
4637 | || pVCpu->iem.s.uCpl > 2
|
---|
4638 | || ( uSel != pVCpu->iem.s.uCpl
|
---|
4639 | && !IEM_IS_GUEST_CPU_AMD(pVCpu)) )
|
---|
4640 | {
|
---|
4641 | Log(("load sreg %#x -> invalid stack selector, #GP(0)\n", uSel));
|
---|
4642 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4643 | }
|
---|
4644 | }
|
---|
4645 |
|
---|
4646 | *pSel = uSel; /* Not RPL, remember :-) */
|
---|
4647 | iemHlpLoadNullDataSelectorProt(pVCpu, pHid, uSel);
|
---|
4648 | if (iSegReg == X86_SREG_SS)
|
---|
4649 | pHid->Attr.u |= pVCpu->iem.s.uCpl << X86DESCATTR_DPL_SHIFT;
|
---|
4650 | }
|
---|
4651 | else
|
---|
4652 | {
|
---|
4653 |
|
---|
4654 | /* Fetch the descriptor. */
|
---|
4655 | IEMSELDESC Desc;
|
---|
4656 | VBOXSTRICTRC rcStrict = iemMemFetchSelDesc(pVCpu, &Desc, uSel, X86_XCPT_GP); /** @todo Correct exception? */
|
---|
4657 | if (rcStrict != VINF_SUCCESS)
|
---|
4658 | return rcStrict;
|
---|
4659 |
|
---|
4660 | /* Check GPs first. */
|
---|
4661 | if (!Desc.Legacy.Gen.u1DescType)
|
---|
4662 | {
|
---|
4663 | Log(("load sreg %d (=%#x) - system selector (%#x) -> #GP\n", iSegReg, uSel, Desc.Legacy.Gen.u4Type));
|
---|
4664 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
4665 | }
|
---|
4666 | if (iSegReg == X86_SREG_SS) /* SS gets different treatment */
|
---|
4667 | {
|
---|
4668 | if ( (Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE)
|
---|
4669 | || !(Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_WRITE) )
|
---|
4670 | {
|
---|
4671 | Log(("load sreg SS, %#x - code or read only (%#x) -> #GP\n", uSel, Desc.Legacy.Gen.u4Type));
|
---|
4672 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
4673 | }
|
---|
4674 | if ((uSel & X86_SEL_RPL) != pVCpu->iem.s.uCpl)
|
---|
4675 | {
|
---|
4676 | Log(("load sreg SS, %#x - RPL and CPL (%d) differs -> #GP\n", uSel, pVCpu->iem.s.uCpl));
|
---|
4677 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
4678 | }
|
---|
4679 | if (Desc.Legacy.Gen.u2Dpl != pVCpu->iem.s.uCpl)
|
---|
4680 | {
|
---|
4681 | Log(("load sreg SS, %#x - DPL (%d) and CPL (%d) differs -> #GP\n", uSel, Desc.Legacy.Gen.u2Dpl, pVCpu->iem.s.uCpl));
|
---|
4682 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
4683 | }
|
---|
4684 | }
|
---|
4685 | else
|
---|
4686 | {
|
---|
4687 | if ((Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_READ)) == X86_SEL_TYPE_CODE)
|
---|
4688 | {
|
---|
4689 | Log(("load sreg%u, %#x - execute only segment -> #GP\n", iSegReg, uSel));
|
---|
4690 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
4691 | }
|
---|
4692 | if ( (Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
|
---|
4693 | != (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
|
---|
4694 | {
|
---|
4695 | #if 0 /* this is what intel says. */
|
---|
4696 | if ( (uSel & X86_SEL_RPL) > Desc.Legacy.Gen.u2Dpl
|
---|
4697 | && pVCpu->iem.s.uCpl > Desc.Legacy.Gen.u2Dpl)
|
---|
4698 | {
|
---|
4699 | Log(("load sreg%u, %#x - both RPL (%d) and CPL (%d) are greater than DPL (%d) -> #GP\n",
|
---|
4700 | iSegReg, uSel, (uSel & X86_SEL_RPL), pVCpu->iem.s.uCpl, Desc.Legacy.Gen.u2Dpl));
|
---|
4701 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
4702 | }
|
---|
4703 | #else /* this is what makes more sense. */
|
---|
4704 | if ((unsigned)(uSel & X86_SEL_RPL) > Desc.Legacy.Gen.u2Dpl)
|
---|
4705 | {
|
---|
4706 | Log(("load sreg%u, %#x - RPL (%d) is greater than DPL (%d) -> #GP\n",
|
---|
4707 | iSegReg, uSel, (uSel & X86_SEL_RPL), Desc.Legacy.Gen.u2Dpl));
|
---|
4708 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
4709 | }
|
---|
4710 | if (pVCpu->iem.s.uCpl > Desc.Legacy.Gen.u2Dpl)
|
---|
4711 | {
|
---|
4712 | Log(("load sreg%u, %#x - CPL (%d) is greater than DPL (%d) -> #GP\n",
|
---|
4713 | iSegReg, uSel, pVCpu->iem.s.uCpl, Desc.Legacy.Gen.u2Dpl));
|
---|
4714 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
4715 | }
|
---|
4716 | #endif
|
---|
4717 | }
|
---|
4718 | }
|
---|
4719 |
|
---|
4720 | /* Is it there? */
|
---|
4721 | if (!Desc.Legacy.Gen.u1Present)
|
---|
4722 | {
|
---|
4723 | Log(("load sreg%d,%#x - segment not present -> #NP\n", iSegReg, uSel));
|
---|
4724 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uSel);
|
---|
4725 | }
|
---|
4726 |
|
---|
4727 | /* The base and limit. */
|
---|
4728 | uint32_t cbLimit = X86DESC_LIMIT_G(&Desc.Legacy);
|
---|
4729 | uint64_t u64Base = X86DESC_BASE(&Desc.Legacy);
|
---|
4730 |
|
---|
4731 | /*
|
---|
4732 | * Ok, everything checked out fine. Now set the accessed bit before
|
---|
4733 | * committing the result into the registers.
|
---|
4734 | */
|
---|
4735 | if (!(Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
4736 | {
|
---|
4737 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uSel);
|
---|
4738 | if (rcStrict != VINF_SUCCESS)
|
---|
4739 | return rcStrict;
|
---|
4740 | Desc.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
4741 | }
|
---|
4742 |
|
---|
4743 | /* commit */
|
---|
4744 | *pSel = uSel;
|
---|
4745 | pHid->Attr.u = X86DESC_GET_HID_ATTR(&Desc.Legacy);
|
---|
4746 | pHid->u32Limit = cbLimit;
|
---|
4747 | pHid->u64Base = u64Base;
|
---|
4748 | pHid->ValidSel = uSel;
|
---|
4749 | pHid->fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4750 |
|
---|
4751 | /** @todo check if the hidden bits are loaded correctly for 64-bit
|
---|
4752 | * mode. */
|
---|
4753 | }
|
---|
4754 |
|
---|
4755 | Assert(CPUMSELREG_ARE_HIDDEN_PARTS_VALID(pVCpu, pHid));
|
---|
4756 | CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_HIDDEN_SEL_REGS);
|
---|
4757 | return VINF_SUCCESS;
|
---|
4758 | }
|
---|
4759 |
|
---|
4760 |
|
---|
4761 | /**
|
---|
4762 | * Implements 'mov SReg, r/m'.
|
---|
4763 | *
|
---|
4764 | * @param iSegReg The segment register number (valid).
|
---|
4765 | * @param uSel The new selector value.
|
---|
4766 | */
|
---|
4767 | IEM_CIMPL_DEF_2(iemCImpl_load_SReg, uint8_t, iSegReg, uint16_t, uSel)
|
---|
4768 | {
|
---|
4769 | VBOXSTRICTRC rcStrict = iemCImpl_LoadSRegWorker(pVCpu, iSegReg, uSel);
|
---|
4770 | if (rcStrict == VINF_SUCCESS)
|
---|
4771 | rcStrict = iemCImpl_LoadSRegFinish(pVCpu, cbInstr, iSegReg);
|
---|
4772 | return rcStrict;
|
---|
4773 | }
|
---|
4774 |
|
---|
4775 |
|
---|
4776 | /**
|
---|
4777 | * Implements 'pop SReg'.
|
---|
4778 | *
|
---|
4779 | * @param iSegReg The segment register number (valid).
|
---|
4780 | * @param enmEffOpSize The efficient operand size (valid).
|
---|
4781 | */
|
---|
4782 | IEM_CIMPL_DEF_2(iemCImpl_pop_Sreg, uint8_t, iSegReg, IEMMODE, enmEffOpSize)
|
---|
4783 | {
|
---|
4784 | VBOXSTRICTRC rcStrict;
|
---|
4785 |
|
---|
4786 | /*
|
---|
4787 | * Read the selector off the stack and join paths with mov ss, reg.
|
---|
4788 | */
|
---|
4789 | RTUINT64U TmpRsp;
|
---|
4790 | TmpRsp.u = pVCpu->cpum.GstCtx.rsp;
|
---|
4791 | switch (enmEffOpSize)
|
---|
4792 | {
|
---|
4793 | case IEMMODE_16BIT:
|
---|
4794 | {
|
---|
4795 | uint16_t uSel;
|
---|
4796 | rcStrict = iemMemStackPopU16Ex(pVCpu, &uSel, &TmpRsp);
|
---|
4797 | if (rcStrict == VINF_SUCCESS)
|
---|
4798 | rcStrict = iemCImpl_LoadSRegWorker(pVCpu, iSegReg, uSel);
|
---|
4799 | break;
|
---|
4800 | }
|
---|
4801 |
|
---|
4802 | case IEMMODE_32BIT:
|
---|
4803 | {
|
---|
4804 | uint32_t u32Value;
|
---|
4805 | rcStrict = iemMemStackPopU32Ex(pVCpu, &u32Value, &TmpRsp);
|
---|
4806 | if (rcStrict == VINF_SUCCESS)
|
---|
4807 | rcStrict = iemCImpl_LoadSRegWorker(pVCpu, iSegReg, (uint16_t)u32Value);
|
---|
4808 | break;
|
---|
4809 | }
|
---|
4810 |
|
---|
4811 | case IEMMODE_64BIT:
|
---|
4812 | {
|
---|
4813 | uint64_t u64Value;
|
---|
4814 | rcStrict = iemMemStackPopU64Ex(pVCpu, &u64Value, &TmpRsp);
|
---|
4815 | if (rcStrict == VINF_SUCCESS)
|
---|
4816 | rcStrict = iemCImpl_LoadSRegWorker(pVCpu, iSegReg, (uint16_t)u64Value);
|
---|
4817 | break;
|
---|
4818 | }
|
---|
4819 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
4820 | }
|
---|
4821 |
|
---|
4822 | /*
|
---|
4823 | * If the load succeeded, commit the stack change and finish the instruction.
|
---|
4824 | */
|
---|
4825 | if (rcStrict == VINF_SUCCESS)
|
---|
4826 | {
|
---|
4827 | pVCpu->cpum.GstCtx.rsp = TmpRsp.u;
|
---|
4828 | rcStrict = iemCImpl_LoadSRegFinish(pVCpu, cbInstr, iSegReg);
|
---|
4829 | }
|
---|
4830 |
|
---|
4831 | return rcStrict;
|
---|
4832 | }
|
---|
4833 |
|
---|
4834 |
|
---|
4835 | /**
|
---|
4836 | * Implements lgs, lfs, les, lds & lss.
|
---|
4837 | */
|
---|
4838 | IEM_CIMPL_DEF_5(iemCImpl_load_SReg_Greg, uint16_t, uSel, uint64_t, offSeg, uint8_t, iSegReg, uint8_t, iGReg, IEMMODE, enmEffOpSize)
|
---|
4839 | {
|
---|
4840 | /*
|
---|
4841 | * Use iemCImpl_LoadSRegWorker to do the tricky segment register loading.
|
---|
4842 | */
|
---|
4843 | /** @todo verify and test that mov, pop and lXs works the segment
|
---|
4844 | * register loading in the exact same way. */
|
---|
4845 | VBOXSTRICTRC rcStrict = iemCImpl_LoadSRegWorker(pVCpu, iSegReg, uSel);
|
---|
4846 | if (rcStrict == VINF_SUCCESS)
|
---|
4847 | {
|
---|
4848 | switch (enmEffOpSize)
|
---|
4849 | {
|
---|
4850 | case IEMMODE_16BIT:
|
---|
4851 | *(uint16_t *)iemGRegRef(pVCpu, iGReg) = offSeg;
|
---|
4852 | break;
|
---|
4853 | case IEMMODE_32BIT:
|
---|
4854 | case IEMMODE_64BIT:
|
---|
4855 | *(uint64_t *)iemGRegRef(pVCpu, iGReg) = offSeg;
|
---|
4856 | break;
|
---|
4857 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
4858 | }
|
---|
4859 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
4860 | }
|
---|
4861 | return rcStrict;
|
---|
4862 | }
|
---|
4863 |
|
---|
4864 |
|
---|
4865 | /**
|
---|
4866 | * Helper for VERR, VERW, LAR, and LSL and loads the descriptor into memory.
|
---|
4867 | *
|
---|
4868 | * @retval VINF_SUCCESS on success.
|
---|
4869 | * @retval VINF_IEM_SELECTOR_NOT_OK if the selector isn't ok.
|
---|
4870 | * @retval iemMemFetchSysU64 return value.
|
---|
4871 | *
|
---|
4872 | * @param pVCpu The cross context virtual CPU structure of the calling thread.
|
---|
4873 | * @param uSel The selector value.
|
---|
4874 | * @param fAllowSysDesc Whether system descriptors are OK or not.
|
---|
4875 | * @param pDesc Where to return the descriptor on success.
|
---|
4876 | */
|
---|
4877 | static VBOXSTRICTRC iemCImpl_LoadDescHelper(PVMCPUCC pVCpu, uint16_t uSel, bool fAllowSysDesc, PIEMSELDESC pDesc)
|
---|
4878 | {
|
---|
4879 | pDesc->Long.au64[0] = 0;
|
---|
4880 | pDesc->Long.au64[1] = 0;
|
---|
4881 |
|
---|
4882 | if (!(uSel & X86_SEL_MASK_OFF_RPL)) /** @todo test this on 64-bit. */
|
---|
4883 | return VINF_IEM_SELECTOR_NOT_OK;
|
---|
4884 |
|
---|
4885 | /* Within the table limits? */
|
---|
4886 | RTGCPTR GCPtrBase;
|
---|
4887 | if (uSel & X86_SEL_LDT)
|
---|
4888 | {
|
---|
4889 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_LDTR);
|
---|
4890 | if ( !pVCpu->cpum.GstCtx.ldtr.Attr.n.u1Present
|
---|
4891 | || (uSel | X86_SEL_RPL_LDT) > pVCpu->cpum.GstCtx.ldtr.u32Limit )
|
---|
4892 | return VINF_IEM_SELECTOR_NOT_OK;
|
---|
4893 | GCPtrBase = pVCpu->cpum.GstCtx.ldtr.u64Base;
|
---|
4894 | }
|
---|
4895 | else
|
---|
4896 | {
|
---|
4897 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_GDTR);
|
---|
4898 | if ((uSel | X86_SEL_RPL_LDT) > pVCpu->cpum.GstCtx.gdtr.cbGdt)
|
---|
4899 | return VINF_IEM_SELECTOR_NOT_OK;
|
---|
4900 | GCPtrBase = pVCpu->cpum.GstCtx.gdtr.pGdt;
|
---|
4901 | }
|
---|
4902 |
|
---|
4903 | /* Fetch the descriptor. */
|
---|
4904 | VBOXSTRICTRC rcStrict = iemMemFetchSysU64(pVCpu, &pDesc->Legacy.u, UINT8_MAX, GCPtrBase + (uSel & X86_SEL_MASK));
|
---|
4905 | if (rcStrict != VINF_SUCCESS)
|
---|
4906 | return rcStrict;
|
---|
4907 | if (!pDesc->Legacy.Gen.u1DescType)
|
---|
4908 | {
|
---|
4909 | if (!fAllowSysDesc)
|
---|
4910 | return VINF_IEM_SELECTOR_NOT_OK;
|
---|
4911 | if (CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu)))
|
---|
4912 | {
|
---|
4913 | rcStrict = iemMemFetchSysU64(pVCpu, &pDesc->Long.au64[1], UINT8_MAX, GCPtrBase + (uSel & X86_SEL_MASK) + 8);
|
---|
4914 | if (rcStrict != VINF_SUCCESS)
|
---|
4915 | return rcStrict;
|
---|
4916 | }
|
---|
4917 |
|
---|
4918 | }
|
---|
4919 |
|
---|
4920 | return VINF_SUCCESS;
|
---|
4921 | }
|
---|
4922 |
|
---|
4923 |
|
---|
4924 | /**
|
---|
4925 | * Implements verr (fWrite = false) and verw (fWrite = true).
|
---|
4926 | */
|
---|
4927 | IEM_CIMPL_DEF_2(iemCImpl_VerX, uint16_t, uSel, bool, fWrite)
|
---|
4928 | {
|
---|
4929 | Assert(!IEM_IS_REAL_OR_V86_MODE(pVCpu));
|
---|
4930 |
|
---|
4931 | /** @todo figure whether the accessed bit is set or not. */
|
---|
4932 |
|
---|
4933 | bool fAccessible = true;
|
---|
4934 | IEMSELDESC Desc;
|
---|
4935 | VBOXSTRICTRC rcStrict = iemCImpl_LoadDescHelper(pVCpu, uSel, false /*fAllowSysDesc*/, &Desc);
|
---|
4936 | if (rcStrict == VINF_SUCCESS)
|
---|
4937 | {
|
---|
4938 | /* Check the descriptor, order doesn't matter much here. */
|
---|
4939 | if ( !Desc.Legacy.Gen.u1DescType
|
---|
4940 | || !Desc.Legacy.Gen.u1Present)
|
---|
4941 | fAccessible = false;
|
---|
4942 | else
|
---|
4943 | {
|
---|
4944 | if ( fWrite
|
---|
4945 | ? (Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_WRITE)) != X86_SEL_TYPE_WRITE
|
---|
4946 | : (Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_READ)) == X86_SEL_TYPE_CODE)
|
---|
4947 | fAccessible = false;
|
---|
4948 |
|
---|
4949 | /** @todo testcase for the conforming behavior. */
|
---|
4950 | if ( (Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
|
---|
4951 | != (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
|
---|
4952 | {
|
---|
4953 | if ((unsigned)(uSel & X86_SEL_RPL) > Desc.Legacy.Gen.u2Dpl)
|
---|
4954 | fAccessible = false;
|
---|
4955 | else if (pVCpu->iem.s.uCpl > Desc.Legacy.Gen.u2Dpl)
|
---|
4956 | fAccessible = false;
|
---|
4957 | }
|
---|
4958 | }
|
---|
4959 |
|
---|
4960 | }
|
---|
4961 | else if (rcStrict == VINF_IEM_SELECTOR_NOT_OK)
|
---|
4962 | fAccessible = false;
|
---|
4963 | else
|
---|
4964 | return rcStrict;
|
---|
4965 |
|
---|
4966 | /* commit */
|
---|
4967 | pVCpu->cpum.GstCtx.eflags.Bits.u1ZF = fAccessible;
|
---|
4968 |
|
---|
4969 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
4970 | }
|
---|
4971 |
|
---|
4972 |
|
---|
4973 | /**
|
---|
4974 | * Implements LAR and LSL with 64-bit operand size.
|
---|
4975 | *
|
---|
4976 | * @returns VINF_SUCCESS.
|
---|
4977 | * @param pu64Dst Pointer to the destination register.
|
---|
4978 | * @param uSel The selector to load details for.
|
---|
4979 | * @param fIsLar true = LAR, false = LSL.
|
---|
4980 | */
|
---|
4981 | IEM_CIMPL_DEF_3(iemCImpl_LarLsl_u64, uint64_t *, pu64Dst, uint16_t, uSel, bool, fIsLar)
|
---|
4982 | {
|
---|
4983 | Assert(!IEM_IS_REAL_OR_V86_MODE(pVCpu));
|
---|
4984 |
|
---|
4985 | /** @todo figure whether the accessed bit is set or not. */
|
---|
4986 |
|
---|
4987 | bool fDescOk = true;
|
---|
4988 | IEMSELDESC Desc;
|
---|
4989 | VBOXSTRICTRC rcStrict = iemCImpl_LoadDescHelper(pVCpu, uSel, true /*fAllowSysDesc*/, &Desc);
|
---|
4990 | if (rcStrict == VINF_SUCCESS)
|
---|
4991 | {
|
---|
4992 | /*
|
---|
4993 | * Check the descriptor type.
|
---|
4994 | */
|
---|
4995 | if (!Desc.Legacy.Gen.u1DescType)
|
---|
4996 | {
|
---|
4997 | if (CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu)))
|
---|
4998 | {
|
---|
4999 | if (Desc.Long.Gen.u5Zeros)
|
---|
5000 | fDescOk = false;
|
---|
5001 | else
|
---|
5002 | switch (Desc.Long.Gen.u4Type)
|
---|
5003 | {
|
---|
5004 | /** @todo Intel lists 0 as valid for LSL, verify whether that's correct */
|
---|
5005 | case AMD64_SEL_TYPE_SYS_TSS_AVAIL:
|
---|
5006 | case AMD64_SEL_TYPE_SYS_TSS_BUSY:
|
---|
5007 | case AMD64_SEL_TYPE_SYS_LDT: /** @todo Intel lists this as invalid for LAR, AMD and 32-bit does otherwise. */
|
---|
5008 | break;
|
---|
5009 | case AMD64_SEL_TYPE_SYS_CALL_GATE:
|
---|
5010 | fDescOk = fIsLar;
|
---|
5011 | break;
|
---|
5012 | default:
|
---|
5013 | fDescOk = false;
|
---|
5014 | break;
|
---|
5015 | }
|
---|
5016 | }
|
---|
5017 | else
|
---|
5018 | {
|
---|
5019 | switch (Desc.Long.Gen.u4Type)
|
---|
5020 | {
|
---|
5021 | case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
|
---|
5022 | case X86_SEL_TYPE_SYS_286_TSS_BUSY:
|
---|
5023 | case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
|
---|
5024 | case X86_SEL_TYPE_SYS_386_TSS_BUSY:
|
---|
5025 | case X86_SEL_TYPE_SYS_LDT:
|
---|
5026 | break;
|
---|
5027 | case X86_SEL_TYPE_SYS_286_CALL_GATE:
|
---|
5028 | case X86_SEL_TYPE_SYS_TASK_GATE:
|
---|
5029 | case X86_SEL_TYPE_SYS_386_CALL_GATE:
|
---|
5030 | fDescOk = fIsLar;
|
---|
5031 | break;
|
---|
5032 | default:
|
---|
5033 | fDescOk = false;
|
---|
5034 | break;
|
---|
5035 | }
|
---|
5036 | }
|
---|
5037 | }
|
---|
5038 | if (fDescOk)
|
---|
5039 | {
|
---|
5040 | /*
|
---|
5041 | * Check the RPL/DPL/CPL interaction..
|
---|
5042 | */
|
---|
5043 | /** @todo testcase for the conforming behavior. */
|
---|
5044 | if ( (Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF)) != (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF)
|
---|
5045 | || !Desc.Legacy.Gen.u1DescType)
|
---|
5046 | {
|
---|
5047 | if ((unsigned)(uSel & X86_SEL_RPL) > Desc.Legacy.Gen.u2Dpl)
|
---|
5048 | fDescOk = false;
|
---|
5049 | else if (pVCpu->iem.s.uCpl > Desc.Legacy.Gen.u2Dpl)
|
---|
5050 | fDescOk = false;
|
---|
5051 | }
|
---|
5052 | }
|
---|
5053 |
|
---|
5054 | if (fDescOk)
|
---|
5055 | {
|
---|
5056 | /*
|
---|
5057 | * All fine, start committing the result.
|
---|
5058 | */
|
---|
5059 | if (fIsLar)
|
---|
5060 | *pu64Dst = Desc.Legacy.au32[1] & UINT32_C(0x00ffff00);
|
---|
5061 | else
|
---|
5062 | *pu64Dst = X86DESC_LIMIT_G(&Desc.Legacy);
|
---|
5063 | }
|
---|
5064 |
|
---|
5065 | }
|
---|
5066 | else if (rcStrict == VINF_IEM_SELECTOR_NOT_OK)
|
---|
5067 | fDescOk = false;
|
---|
5068 | else
|
---|
5069 | return rcStrict;
|
---|
5070 |
|
---|
5071 | /* commit flags value and advance rip. */
|
---|
5072 | pVCpu->cpum.GstCtx.eflags.Bits.u1ZF = fDescOk;
|
---|
5073 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5074 | }
|
---|
5075 |
|
---|
5076 |
|
---|
5077 | /**
|
---|
5078 | * Implements LAR and LSL with 16-bit operand size.
|
---|
5079 | *
|
---|
5080 | * @returns VINF_SUCCESS.
|
---|
5081 | * @param pu16Dst Pointer to the destination register.
|
---|
5082 | * @param uSel The selector to load details for.
|
---|
5083 | * @param fIsLar true = LAR, false = LSL.
|
---|
5084 | */
|
---|
5085 | IEM_CIMPL_DEF_3(iemCImpl_LarLsl_u16, uint16_t *, pu16Dst, uint16_t, uSel, bool, fIsLar)
|
---|
5086 | {
|
---|
5087 | uint64_t u64TmpDst = *pu16Dst;
|
---|
5088 | IEM_CIMPL_CALL_3(iemCImpl_LarLsl_u64, &u64TmpDst, uSel, fIsLar);
|
---|
5089 | *pu16Dst = u64TmpDst;
|
---|
5090 | return VINF_SUCCESS;
|
---|
5091 | }
|
---|
5092 |
|
---|
5093 |
|
---|
5094 | /**
|
---|
5095 | * Implements lgdt.
|
---|
5096 | *
|
---|
5097 | * @param iEffSeg The segment of the new gdtr contents
|
---|
5098 | * @param GCPtrEffSrc The address of the new gdtr contents.
|
---|
5099 | * @param enmEffOpSize The effective operand size.
|
---|
5100 | */
|
---|
5101 | IEM_CIMPL_DEF_3(iemCImpl_lgdt, uint8_t, iEffSeg, RTGCPTR, GCPtrEffSrc, IEMMODE, enmEffOpSize)
|
---|
5102 | {
|
---|
5103 | if (pVCpu->iem.s.uCpl != 0)
|
---|
5104 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5105 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
5106 |
|
---|
5107 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
5108 | && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
|
---|
5109 | {
|
---|
5110 | Log(("lgdt: Guest intercept -> VM-exit\n"));
|
---|
5111 | IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_GDTR_IDTR_ACCESS, VMXINSTRID_LGDT, cbInstr);
|
---|
5112 | }
|
---|
5113 |
|
---|
5114 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_GDTR_WRITES))
|
---|
5115 | {
|
---|
5116 | Log(("lgdt: Guest intercept -> #VMEXIT\n"));
|
---|
5117 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
5118 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_GDTR_WRITE, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
5119 | }
|
---|
5120 |
|
---|
5121 | /*
|
---|
5122 | * Fetch the limit and base address.
|
---|
5123 | */
|
---|
5124 | uint16_t cbLimit;
|
---|
5125 | RTGCPTR GCPtrBase;
|
---|
5126 | VBOXSTRICTRC rcStrict = iemMemFetchDataXdtr(pVCpu, &cbLimit, &GCPtrBase, iEffSeg, GCPtrEffSrc, enmEffOpSize);
|
---|
5127 | if (rcStrict == VINF_SUCCESS)
|
---|
5128 | {
|
---|
5129 | if ( pVCpu->iem.s.enmCpuMode != IEMMODE_64BIT
|
---|
5130 | || X86_IS_CANONICAL(GCPtrBase))
|
---|
5131 | {
|
---|
5132 | rcStrict = CPUMSetGuestGDTR(pVCpu, GCPtrBase, cbLimit);
|
---|
5133 | if (rcStrict == VINF_SUCCESS)
|
---|
5134 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5135 | }
|
---|
5136 | else
|
---|
5137 | {
|
---|
5138 | Log(("iemCImpl_lgdt: Non-canonical base %04x:%RGv\n", cbLimit, GCPtrBase));
|
---|
5139 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5140 | }
|
---|
5141 | }
|
---|
5142 | return rcStrict;
|
---|
5143 | }
|
---|
5144 |
|
---|
5145 |
|
---|
5146 | /**
|
---|
5147 | * Implements sgdt.
|
---|
5148 | *
|
---|
5149 | * @param iEffSeg The segment where to store the gdtr content.
|
---|
5150 | * @param GCPtrEffDst The address where to store the gdtr content.
|
---|
5151 | */
|
---|
5152 | IEM_CIMPL_DEF_2(iemCImpl_sgdt, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
|
---|
5153 | {
|
---|
5154 | /*
|
---|
5155 | * Join paths with sidt.
|
---|
5156 | * Note! No CPL or V8086 checks here, it's a really sad story, ask Intel if
|
---|
5157 | * you really must know.
|
---|
5158 | */
|
---|
5159 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
5160 | && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
|
---|
5161 | {
|
---|
5162 | Log(("sgdt: Guest intercept -> VM-exit\n"));
|
---|
5163 | IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_GDTR_IDTR_ACCESS, VMXINSTRID_SGDT, cbInstr);
|
---|
5164 | }
|
---|
5165 |
|
---|
5166 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_GDTR_READS))
|
---|
5167 | {
|
---|
5168 | Log(("sgdt: Guest intercept -> #VMEXIT\n"));
|
---|
5169 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
5170 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_GDTR_READ, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
5171 | }
|
---|
5172 |
|
---|
5173 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_GDTR);
|
---|
5174 | VBOXSTRICTRC rcStrict = iemMemStoreDataXdtr(pVCpu, pVCpu->cpum.GstCtx.gdtr.cbGdt, pVCpu->cpum.GstCtx.gdtr.pGdt, iEffSeg, GCPtrEffDst);
|
---|
5175 | if (rcStrict == VINF_SUCCESS)
|
---|
5176 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5177 | return rcStrict;
|
---|
5178 | }
|
---|
5179 |
|
---|
5180 |
|
---|
5181 | /**
|
---|
5182 | * Implements lidt.
|
---|
5183 | *
|
---|
5184 | * @param iEffSeg The segment of the new idtr contents
|
---|
5185 | * @param GCPtrEffSrc The address of the new idtr contents.
|
---|
5186 | * @param enmEffOpSize The effective operand size.
|
---|
5187 | */
|
---|
5188 | IEM_CIMPL_DEF_3(iemCImpl_lidt, uint8_t, iEffSeg, RTGCPTR, GCPtrEffSrc, IEMMODE, enmEffOpSize)
|
---|
5189 | {
|
---|
5190 | if (pVCpu->iem.s.uCpl != 0)
|
---|
5191 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5192 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
5193 |
|
---|
5194 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_IDTR_WRITES))
|
---|
5195 | {
|
---|
5196 | Log(("lidt: Guest intercept -> #VMEXIT\n"));
|
---|
5197 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
5198 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_IDTR_WRITE, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
5199 | }
|
---|
5200 |
|
---|
5201 | /*
|
---|
5202 | * Fetch the limit and base address.
|
---|
5203 | */
|
---|
5204 | uint16_t cbLimit;
|
---|
5205 | RTGCPTR GCPtrBase;
|
---|
5206 | VBOXSTRICTRC rcStrict = iemMemFetchDataXdtr(pVCpu, &cbLimit, &GCPtrBase, iEffSeg, GCPtrEffSrc, enmEffOpSize);
|
---|
5207 | if (rcStrict == VINF_SUCCESS)
|
---|
5208 | {
|
---|
5209 | if ( pVCpu->iem.s.enmCpuMode != IEMMODE_64BIT
|
---|
5210 | || X86_IS_CANONICAL(GCPtrBase))
|
---|
5211 | {
|
---|
5212 | CPUMSetGuestIDTR(pVCpu, GCPtrBase, cbLimit);
|
---|
5213 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5214 | }
|
---|
5215 | else
|
---|
5216 | {
|
---|
5217 | Log(("iemCImpl_lidt: Non-canonical base %04x:%RGv\n", cbLimit, GCPtrBase));
|
---|
5218 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5219 | }
|
---|
5220 | }
|
---|
5221 | return rcStrict;
|
---|
5222 | }
|
---|
5223 |
|
---|
5224 |
|
---|
5225 | /**
|
---|
5226 | * Implements sidt.
|
---|
5227 | *
|
---|
5228 | * @param iEffSeg The segment where to store the idtr content.
|
---|
5229 | * @param GCPtrEffDst The address where to store the idtr content.
|
---|
5230 | */
|
---|
5231 | IEM_CIMPL_DEF_2(iemCImpl_sidt, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
|
---|
5232 | {
|
---|
5233 | /*
|
---|
5234 | * Join paths with sgdt.
|
---|
5235 | * Note! No CPL or V8086 checks here, it's a really sad story, ask Intel if
|
---|
5236 | * you really must know.
|
---|
5237 | */
|
---|
5238 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_IDTR_READS))
|
---|
5239 | {
|
---|
5240 | Log(("sidt: Guest intercept -> #VMEXIT\n"));
|
---|
5241 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
5242 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_IDTR_READ, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
5243 | }
|
---|
5244 |
|
---|
5245 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_IDTR);
|
---|
5246 | VBOXSTRICTRC rcStrict = iemMemStoreDataXdtr(pVCpu, pVCpu->cpum.GstCtx.idtr.cbIdt, pVCpu->cpum.GstCtx.idtr.pIdt, iEffSeg, GCPtrEffDst);
|
---|
5247 | if (rcStrict == VINF_SUCCESS)
|
---|
5248 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5249 | return rcStrict;
|
---|
5250 | }
|
---|
5251 |
|
---|
5252 |
|
---|
5253 | /**
|
---|
5254 | * Implements lldt.
|
---|
5255 | *
|
---|
5256 | * @param uNewLdt The new LDT selector value.
|
---|
5257 | */
|
---|
5258 | IEM_CIMPL_DEF_1(iemCImpl_lldt, uint16_t, uNewLdt)
|
---|
5259 | {
|
---|
5260 | /*
|
---|
5261 | * Check preconditions.
|
---|
5262 | */
|
---|
5263 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
5264 | {
|
---|
5265 | Log(("lldt %04x - real or v8086 mode -> #GP(0)\n", uNewLdt));
|
---|
5266 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
5267 | }
|
---|
5268 | if (pVCpu->iem.s.uCpl != 0)
|
---|
5269 | {
|
---|
5270 | Log(("lldt %04x - CPL is %d -> #GP(0)\n", uNewLdt, pVCpu->iem.s.uCpl));
|
---|
5271 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5272 | }
|
---|
5273 | /* Nested-guest VMX intercept. */
|
---|
5274 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
5275 | && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
|
---|
5276 | {
|
---|
5277 | Log(("lldt: Guest intercept -> VM-exit\n"));
|
---|
5278 | IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_LDTR_TR_ACCESS, VMXINSTRID_LLDT, cbInstr);
|
---|
5279 | }
|
---|
5280 | if (uNewLdt & X86_SEL_LDT)
|
---|
5281 | {
|
---|
5282 | Log(("lldt %04x - LDT selector -> #GP\n", uNewLdt));
|
---|
5283 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewLdt);
|
---|
5284 | }
|
---|
5285 |
|
---|
5286 | /*
|
---|
5287 | * Now, loading a NULL selector is easy.
|
---|
5288 | */
|
---|
5289 | if (!(uNewLdt & X86_SEL_MASK_OFF_RPL))
|
---|
5290 | {
|
---|
5291 | /* Nested-guest SVM intercept. */
|
---|
5292 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_LDTR_WRITES))
|
---|
5293 | {
|
---|
5294 | Log(("lldt: Guest intercept -> #VMEXIT\n"));
|
---|
5295 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
5296 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_LDTR_WRITE, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
5297 | }
|
---|
5298 |
|
---|
5299 | Log(("lldt %04x: Loading NULL selector.\n", uNewLdt));
|
---|
5300 | pVCpu->cpum.GstCtx.fExtrn &= ~CPUMCTX_EXTRN_LDTR;
|
---|
5301 | CPUMSetGuestLDTR(pVCpu, uNewLdt);
|
---|
5302 | pVCpu->cpum.GstCtx.ldtr.ValidSel = uNewLdt;
|
---|
5303 | pVCpu->cpum.GstCtx.ldtr.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
5304 | if (IEM_IS_GUEST_CPU_AMD(pVCpu))
|
---|
5305 | {
|
---|
5306 | /* AMD-V seems to leave the base and limit alone. */
|
---|
5307 | pVCpu->cpum.GstCtx.ldtr.Attr.u = X86DESCATTR_UNUSABLE;
|
---|
5308 | }
|
---|
5309 | else
|
---|
5310 | {
|
---|
5311 | /* VT-x (Intel 3960x) seems to be doing the following. */
|
---|
5312 | pVCpu->cpum.GstCtx.ldtr.Attr.u = X86DESCATTR_UNUSABLE | X86DESCATTR_G | X86DESCATTR_D;
|
---|
5313 | pVCpu->cpum.GstCtx.ldtr.u64Base = 0;
|
---|
5314 | pVCpu->cpum.GstCtx.ldtr.u32Limit = UINT32_MAX;
|
---|
5315 | }
|
---|
5316 |
|
---|
5317 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5318 | }
|
---|
5319 |
|
---|
5320 | /*
|
---|
5321 | * Read the descriptor.
|
---|
5322 | */
|
---|
5323 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_LDTR | CPUMCTX_EXTRN_GDTR);
|
---|
5324 | IEMSELDESC Desc;
|
---|
5325 | VBOXSTRICTRC rcStrict = iemMemFetchSelDesc(pVCpu, &Desc, uNewLdt, X86_XCPT_GP); /** @todo Correct exception? */
|
---|
5326 | if (rcStrict != VINF_SUCCESS)
|
---|
5327 | return rcStrict;
|
---|
5328 |
|
---|
5329 | /* Check GPs first. */
|
---|
5330 | if (Desc.Legacy.Gen.u1DescType)
|
---|
5331 | {
|
---|
5332 | Log(("lldt %#x - not system selector (type %x) -> #GP\n", uNewLdt, Desc.Legacy.Gen.u4Type));
|
---|
5333 | return iemRaiseGeneralProtectionFault(pVCpu, uNewLdt & X86_SEL_MASK_OFF_RPL);
|
---|
5334 | }
|
---|
5335 | if (Desc.Legacy.Gen.u4Type != X86_SEL_TYPE_SYS_LDT)
|
---|
5336 | {
|
---|
5337 | Log(("lldt %#x - not LDT selector (type %x) -> #GP\n", uNewLdt, Desc.Legacy.Gen.u4Type));
|
---|
5338 | return iemRaiseGeneralProtectionFault(pVCpu, uNewLdt & X86_SEL_MASK_OFF_RPL);
|
---|
5339 | }
|
---|
5340 | uint64_t u64Base;
|
---|
5341 | if (!IEM_IS_LONG_MODE(pVCpu))
|
---|
5342 | u64Base = X86DESC_BASE(&Desc.Legacy);
|
---|
5343 | else
|
---|
5344 | {
|
---|
5345 | if (Desc.Long.Gen.u5Zeros)
|
---|
5346 | {
|
---|
5347 | Log(("lldt %#x - u5Zeros=%#x -> #GP\n", uNewLdt, Desc.Long.Gen.u5Zeros));
|
---|
5348 | return iemRaiseGeneralProtectionFault(pVCpu, uNewLdt & X86_SEL_MASK_OFF_RPL);
|
---|
5349 | }
|
---|
5350 |
|
---|
5351 | u64Base = X86DESC64_BASE(&Desc.Long);
|
---|
5352 | if (!IEM_IS_CANONICAL(u64Base))
|
---|
5353 | {
|
---|
5354 | Log(("lldt %#x - non-canonical base address %#llx -> #GP\n", uNewLdt, u64Base));
|
---|
5355 | return iemRaiseGeneralProtectionFault(pVCpu, uNewLdt & X86_SEL_MASK_OFF_RPL);
|
---|
5356 | }
|
---|
5357 | }
|
---|
5358 |
|
---|
5359 | /* NP */
|
---|
5360 | if (!Desc.Legacy.Gen.u1Present)
|
---|
5361 | {
|
---|
5362 | Log(("lldt %#x - segment not present -> #NP\n", uNewLdt));
|
---|
5363 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uNewLdt);
|
---|
5364 | }
|
---|
5365 |
|
---|
5366 | /* Nested-guest SVM intercept. */
|
---|
5367 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_LDTR_WRITES))
|
---|
5368 | {
|
---|
5369 | Log(("lldt: Guest intercept -> #VMEXIT\n"));
|
---|
5370 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
5371 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_LDTR_WRITE, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
5372 | }
|
---|
5373 |
|
---|
5374 | /*
|
---|
5375 | * It checks out alright, update the registers.
|
---|
5376 | */
|
---|
5377 | /** @todo check if the actual value is loaded or if the RPL is dropped */
|
---|
5378 | CPUMSetGuestLDTR(pVCpu, uNewLdt & X86_SEL_MASK_OFF_RPL);
|
---|
5379 | pVCpu->cpum.GstCtx.ldtr.ValidSel = uNewLdt & X86_SEL_MASK_OFF_RPL;
|
---|
5380 | pVCpu->cpum.GstCtx.ldtr.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
5381 | pVCpu->cpum.GstCtx.ldtr.Attr.u = X86DESC_GET_HID_ATTR(&Desc.Legacy);
|
---|
5382 | pVCpu->cpum.GstCtx.ldtr.u32Limit = X86DESC_LIMIT_G(&Desc.Legacy);
|
---|
5383 | pVCpu->cpum.GstCtx.ldtr.u64Base = u64Base;
|
---|
5384 |
|
---|
5385 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5386 | }
|
---|
5387 |
|
---|
5388 |
|
---|
5389 | /**
|
---|
5390 | * Implements sldt GReg
|
---|
5391 | *
|
---|
5392 | * @param iGReg The general register to store the CRx value in.
|
---|
5393 | * @param enmEffOpSize The operand size.
|
---|
5394 | */
|
---|
5395 | IEM_CIMPL_DEF_2(iemCImpl_sldt_reg, uint8_t, iGReg, uint8_t, enmEffOpSize)
|
---|
5396 | {
|
---|
5397 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
5398 | && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
|
---|
5399 | {
|
---|
5400 | Log(("sldt: Guest intercept -> VM-exit\n"));
|
---|
5401 | IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_LDTR_TR_ACCESS, VMXINSTRID_SLDT, cbInstr);
|
---|
5402 | }
|
---|
5403 |
|
---|
5404 | IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_LDTR_READS, SVM_EXIT_LDTR_READ, 0, 0);
|
---|
5405 |
|
---|
5406 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_LDTR);
|
---|
5407 | switch (enmEffOpSize)
|
---|
5408 | {
|
---|
5409 | case IEMMODE_16BIT: *(uint16_t *)iemGRegRef(pVCpu, iGReg) = pVCpu->cpum.GstCtx.ldtr.Sel; break;
|
---|
5410 | case IEMMODE_32BIT: *(uint64_t *)iemGRegRef(pVCpu, iGReg) = pVCpu->cpum.GstCtx.ldtr.Sel; break;
|
---|
5411 | case IEMMODE_64BIT: *(uint64_t *)iemGRegRef(pVCpu, iGReg) = pVCpu->cpum.GstCtx.ldtr.Sel; break;
|
---|
5412 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
5413 | }
|
---|
5414 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5415 | }
|
---|
5416 |
|
---|
5417 |
|
---|
5418 | /**
|
---|
5419 | * Implements sldt mem.
|
---|
5420 | *
|
---|
5421 | * @param iEffSeg The effective segment register to use with @a GCPtrMem.
|
---|
5422 | * @param GCPtrEffDst Where to store the 16-bit CR0 value.
|
---|
5423 | */
|
---|
5424 | IEM_CIMPL_DEF_2(iemCImpl_sldt_mem, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
|
---|
5425 | {
|
---|
5426 | IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_LDTR_READS, SVM_EXIT_LDTR_READ, 0, 0);
|
---|
5427 |
|
---|
5428 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_LDTR);
|
---|
5429 | VBOXSTRICTRC rcStrict = iemMemStoreDataU16(pVCpu, iEffSeg, GCPtrEffDst, pVCpu->cpum.GstCtx.ldtr.Sel);
|
---|
5430 | if (rcStrict == VINF_SUCCESS)
|
---|
5431 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5432 | return rcStrict;
|
---|
5433 | }
|
---|
5434 |
|
---|
5435 |
|
---|
5436 | /**
|
---|
5437 | * Implements ltr.
|
---|
5438 | *
|
---|
5439 | * @param uNewTr The new TSS selector value.
|
---|
5440 | */
|
---|
5441 | IEM_CIMPL_DEF_1(iemCImpl_ltr, uint16_t, uNewTr)
|
---|
5442 | {
|
---|
5443 | /*
|
---|
5444 | * Check preconditions.
|
---|
5445 | */
|
---|
5446 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
5447 | {
|
---|
5448 | Log(("ltr %04x - real or v8086 mode -> #GP(0)\n", uNewTr));
|
---|
5449 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
5450 | }
|
---|
5451 | if (pVCpu->iem.s.uCpl != 0)
|
---|
5452 | {
|
---|
5453 | Log(("ltr %04x - CPL is %d -> #GP(0)\n", uNewTr, pVCpu->iem.s.uCpl));
|
---|
5454 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5455 | }
|
---|
5456 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
5457 | && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
|
---|
5458 | {
|
---|
5459 | Log(("ltr: Guest intercept -> VM-exit\n"));
|
---|
5460 | IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_LDTR_TR_ACCESS, VMXINSTRID_LTR, cbInstr);
|
---|
5461 | }
|
---|
5462 | if (uNewTr & X86_SEL_LDT)
|
---|
5463 | {
|
---|
5464 | Log(("ltr %04x - LDT selector -> #GP\n", uNewTr));
|
---|
5465 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewTr);
|
---|
5466 | }
|
---|
5467 | if (!(uNewTr & X86_SEL_MASK_OFF_RPL))
|
---|
5468 | {
|
---|
5469 | Log(("ltr %04x - NULL selector -> #GP(0)\n", uNewTr));
|
---|
5470 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5471 | }
|
---|
5472 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_TR_WRITES))
|
---|
5473 | {
|
---|
5474 | Log(("ltr: Guest intercept -> #VMEXIT\n"));
|
---|
5475 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
5476 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_TR_WRITE, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
5477 | }
|
---|
5478 |
|
---|
5479 | /*
|
---|
5480 | * Read the descriptor.
|
---|
5481 | */
|
---|
5482 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_LDTR | CPUMCTX_EXTRN_GDTR | CPUMCTX_EXTRN_TR);
|
---|
5483 | IEMSELDESC Desc;
|
---|
5484 | VBOXSTRICTRC rcStrict = iemMemFetchSelDesc(pVCpu, &Desc, uNewTr, X86_XCPT_GP); /** @todo Correct exception? */
|
---|
5485 | if (rcStrict != VINF_SUCCESS)
|
---|
5486 | return rcStrict;
|
---|
5487 |
|
---|
5488 | /* Check GPs first. */
|
---|
5489 | if (Desc.Legacy.Gen.u1DescType)
|
---|
5490 | {
|
---|
5491 | Log(("ltr %#x - not system selector (type %x) -> #GP\n", uNewTr, Desc.Legacy.Gen.u4Type));
|
---|
5492 | return iemRaiseGeneralProtectionFault(pVCpu, uNewTr & X86_SEL_MASK_OFF_RPL);
|
---|
5493 | }
|
---|
5494 | if ( Desc.Legacy.Gen.u4Type != X86_SEL_TYPE_SYS_386_TSS_AVAIL /* same as AMD64_SEL_TYPE_SYS_TSS_AVAIL */
|
---|
5495 | && ( Desc.Legacy.Gen.u4Type != X86_SEL_TYPE_SYS_286_TSS_AVAIL
|
---|
5496 | || IEM_IS_LONG_MODE(pVCpu)) )
|
---|
5497 | {
|
---|
5498 | Log(("ltr %#x - not an available TSS selector (type %x) -> #GP\n", uNewTr, Desc.Legacy.Gen.u4Type));
|
---|
5499 | return iemRaiseGeneralProtectionFault(pVCpu, uNewTr & X86_SEL_MASK_OFF_RPL);
|
---|
5500 | }
|
---|
5501 | uint64_t u64Base;
|
---|
5502 | if (!IEM_IS_LONG_MODE(pVCpu))
|
---|
5503 | u64Base = X86DESC_BASE(&Desc.Legacy);
|
---|
5504 | else
|
---|
5505 | {
|
---|
5506 | if (Desc.Long.Gen.u5Zeros)
|
---|
5507 | {
|
---|
5508 | Log(("ltr %#x - u5Zeros=%#x -> #GP\n", uNewTr, Desc.Long.Gen.u5Zeros));
|
---|
5509 | return iemRaiseGeneralProtectionFault(pVCpu, uNewTr & X86_SEL_MASK_OFF_RPL);
|
---|
5510 | }
|
---|
5511 |
|
---|
5512 | u64Base = X86DESC64_BASE(&Desc.Long);
|
---|
5513 | if (!IEM_IS_CANONICAL(u64Base))
|
---|
5514 | {
|
---|
5515 | Log(("ltr %#x - non-canonical base address %#llx -> #GP\n", uNewTr, u64Base));
|
---|
5516 | return iemRaiseGeneralProtectionFault(pVCpu, uNewTr & X86_SEL_MASK_OFF_RPL);
|
---|
5517 | }
|
---|
5518 | }
|
---|
5519 |
|
---|
5520 | /* NP */
|
---|
5521 | if (!Desc.Legacy.Gen.u1Present)
|
---|
5522 | {
|
---|
5523 | Log(("ltr %#x - segment not present -> #NP\n", uNewTr));
|
---|
5524 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uNewTr);
|
---|
5525 | }
|
---|
5526 |
|
---|
5527 | /*
|
---|
5528 | * Set it busy.
|
---|
5529 | * Note! Intel says this should lock down the whole descriptor, but we'll
|
---|
5530 | * restrict our selves to 32-bit for now due to lack of inline
|
---|
5531 | * assembly and such.
|
---|
5532 | */
|
---|
5533 | void *pvDesc;
|
---|
5534 | rcStrict = iemMemMap(pVCpu, &pvDesc, 8, UINT8_MAX, pVCpu->cpum.GstCtx.gdtr.pGdt + (uNewTr & X86_SEL_MASK_OFF_RPL),
|
---|
5535 | IEM_ACCESS_DATA_RW, 0);
|
---|
5536 | if (rcStrict != VINF_SUCCESS)
|
---|
5537 | return rcStrict;
|
---|
5538 | switch ((uintptr_t)pvDesc & 3)
|
---|
5539 | {
|
---|
5540 | case 0: ASMAtomicBitSet(pvDesc, 40 + 1); break;
|
---|
5541 | case 1: ASMAtomicBitSet((uint8_t *)pvDesc + 3, 40 + 1 - 24); break;
|
---|
5542 | case 2: ASMAtomicBitSet((uint8_t *)pvDesc + 2, 40 + 1 - 16); break;
|
---|
5543 | case 3: ASMAtomicBitSet((uint8_t *)pvDesc + 1, 40 + 1 - 8); break;
|
---|
5544 | }
|
---|
5545 | rcStrict = iemMemCommitAndUnmap(pVCpu, pvDesc, IEM_ACCESS_DATA_RW);
|
---|
5546 | if (rcStrict != VINF_SUCCESS)
|
---|
5547 | return rcStrict;
|
---|
5548 | Desc.Legacy.Gen.u4Type |= X86_SEL_TYPE_SYS_TSS_BUSY_MASK;
|
---|
5549 |
|
---|
5550 | /*
|
---|
5551 | * It checks out alright, update the registers.
|
---|
5552 | */
|
---|
5553 | /** @todo check if the actual value is loaded or if the RPL is dropped */
|
---|
5554 | CPUMSetGuestTR(pVCpu, uNewTr & X86_SEL_MASK_OFF_RPL);
|
---|
5555 | pVCpu->cpum.GstCtx.tr.ValidSel = uNewTr & X86_SEL_MASK_OFF_RPL;
|
---|
5556 | pVCpu->cpum.GstCtx.tr.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
5557 | pVCpu->cpum.GstCtx.tr.Attr.u = X86DESC_GET_HID_ATTR(&Desc.Legacy);
|
---|
5558 | pVCpu->cpum.GstCtx.tr.u32Limit = X86DESC_LIMIT_G(&Desc.Legacy);
|
---|
5559 | pVCpu->cpum.GstCtx.tr.u64Base = u64Base;
|
---|
5560 |
|
---|
5561 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5562 | }
|
---|
5563 |
|
---|
5564 |
|
---|
5565 | /**
|
---|
5566 | * Implements str GReg
|
---|
5567 | *
|
---|
5568 | * @param iGReg The general register to store the CRx value in.
|
---|
5569 | * @param enmEffOpSize The operand size.
|
---|
5570 | */
|
---|
5571 | IEM_CIMPL_DEF_2(iemCImpl_str_reg, uint8_t, iGReg, uint8_t, enmEffOpSize)
|
---|
5572 | {
|
---|
5573 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
5574 | && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
|
---|
5575 | {
|
---|
5576 | Log(("str_reg: Guest intercept -> VM-exit\n"));
|
---|
5577 | IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_LDTR_TR_ACCESS, VMXINSTRID_STR, cbInstr);
|
---|
5578 | }
|
---|
5579 |
|
---|
5580 | IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_TR_READS, SVM_EXIT_TR_READ, 0, 0);
|
---|
5581 |
|
---|
5582 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_TR);
|
---|
5583 | switch (enmEffOpSize)
|
---|
5584 | {
|
---|
5585 | case IEMMODE_16BIT: *(uint16_t *)iemGRegRef(pVCpu, iGReg) = pVCpu->cpum.GstCtx.tr.Sel; break;
|
---|
5586 | case IEMMODE_32BIT: *(uint64_t *)iemGRegRef(pVCpu, iGReg) = pVCpu->cpum.GstCtx.tr.Sel; break;
|
---|
5587 | case IEMMODE_64BIT: *(uint64_t *)iemGRegRef(pVCpu, iGReg) = pVCpu->cpum.GstCtx.tr.Sel; break;
|
---|
5588 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
5589 | }
|
---|
5590 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5591 | }
|
---|
5592 |
|
---|
5593 |
|
---|
5594 | /**
|
---|
5595 | * Implements str mem.
|
---|
5596 | *
|
---|
5597 | * @param iEffSeg The effective segment register to use with @a GCPtrMem.
|
---|
5598 | * @param GCPtrEffDst Where to store the 16-bit CR0 value.
|
---|
5599 | */
|
---|
5600 | IEM_CIMPL_DEF_2(iemCImpl_str_mem, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
|
---|
5601 | {
|
---|
5602 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
5603 | && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
|
---|
5604 | {
|
---|
5605 | Log(("str_mem: Guest intercept -> VM-exit\n"));
|
---|
5606 | IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_LDTR_TR_ACCESS, VMXINSTRID_STR, cbInstr);
|
---|
5607 | }
|
---|
5608 |
|
---|
5609 | IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_TR_READS, SVM_EXIT_TR_READ, 0, 0);
|
---|
5610 |
|
---|
5611 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_TR);
|
---|
5612 | VBOXSTRICTRC rcStrict = iemMemStoreDataU16(pVCpu, iEffSeg, GCPtrEffDst, pVCpu->cpum.GstCtx.tr.Sel);
|
---|
5613 | if (rcStrict == VINF_SUCCESS)
|
---|
5614 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5615 | return rcStrict;
|
---|
5616 | }
|
---|
5617 |
|
---|
5618 |
|
---|
5619 | /**
|
---|
5620 | * Implements mov GReg,CRx.
|
---|
5621 | *
|
---|
5622 | * @param iGReg The general register to store the CRx value in.
|
---|
5623 | * @param iCrReg The CRx register to read (valid).
|
---|
5624 | */
|
---|
5625 | IEM_CIMPL_DEF_2(iemCImpl_mov_Rd_Cd, uint8_t, iGReg, uint8_t, iCrReg)
|
---|
5626 | {
|
---|
5627 | if (pVCpu->iem.s.uCpl != 0)
|
---|
5628 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5629 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
5630 |
|
---|
5631 | if (IEM_SVM_IS_READ_CR_INTERCEPT_SET(pVCpu, iCrReg))
|
---|
5632 | {
|
---|
5633 | Log(("iemCImpl_mov_Rd_Cd: Guest intercept CR%u -> #VMEXIT\n", iCrReg));
|
---|
5634 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
5635 | IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_READ_CR0 + iCrReg, IEMACCESSCRX_MOV_CRX, iGReg);
|
---|
5636 | }
|
---|
5637 |
|
---|
5638 | /* Read it. */
|
---|
5639 | uint64_t crX;
|
---|
5640 | switch (iCrReg)
|
---|
5641 | {
|
---|
5642 | case 0:
|
---|
5643 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0);
|
---|
5644 | crX = pVCpu->cpum.GstCtx.cr0;
|
---|
5645 | if (IEM_GET_TARGET_CPU(pVCpu) <= IEMTARGETCPU_386)
|
---|
5646 | crX |= UINT32_C(0x7fffffe0); /* All reserved CR0 flags are set on a 386, just like MSW on 286. */
|
---|
5647 | break;
|
---|
5648 | case 2:
|
---|
5649 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_CR2);
|
---|
5650 | crX = pVCpu->cpum.GstCtx.cr2;
|
---|
5651 | break;
|
---|
5652 | case 3:
|
---|
5653 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR3);
|
---|
5654 | crX = pVCpu->cpum.GstCtx.cr3;
|
---|
5655 | break;
|
---|
5656 | case 4:
|
---|
5657 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR4);
|
---|
5658 | crX = pVCpu->cpum.GstCtx.cr4;
|
---|
5659 | break;
|
---|
5660 | case 8:
|
---|
5661 | {
|
---|
5662 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_APIC_TPR);
|
---|
5663 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
5664 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
5665 | {
|
---|
5666 | VBOXSTRICTRC rcStrict = iemVmxVmexitInstrMovFromCr8(pVCpu, iGReg, cbInstr);
|
---|
5667 | if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
|
---|
5668 | return rcStrict;
|
---|
5669 |
|
---|
5670 | /*
|
---|
5671 | * If the Mov-from-CR8 doesn't cause a VM-exit, bits 7:4 of the VTPR is copied
|
---|
5672 | * to bits 0:3 of the destination operand. Bits 63:4 of the destination operand
|
---|
5673 | * are cleared.
|
---|
5674 | *
|
---|
5675 | * See Intel Spec. 29.3 "Virtualizing CR8-based TPR Accesses"
|
---|
5676 | */
|
---|
5677 | if (IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_USE_TPR_SHADOW))
|
---|
5678 | {
|
---|
5679 | uint32_t const uTpr = iemVmxVirtApicReadRaw32(pVCpu, XAPIC_OFF_TPR);
|
---|
5680 | crX = (uTpr >> 4) & 0xf;
|
---|
5681 | break;
|
---|
5682 | }
|
---|
5683 | }
|
---|
5684 | #endif
|
---|
5685 | #ifdef VBOX_WITH_NESTED_HWVIRT_SVM
|
---|
5686 | if (CPUMIsGuestInSvmNestedHwVirtMode(IEM_GET_CTX(pVCpu)))
|
---|
5687 | {
|
---|
5688 | PCSVMVMCBCTRL pVmcbCtrl = &pVCpu->cpum.GstCtx.hwvirt.svm.Vmcb.ctrl;
|
---|
5689 | if (CPUMIsGuestSvmVirtIntrMasking(pVCpu, IEM_GET_CTX(pVCpu)))
|
---|
5690 | {
|
---|
5691 | crX = pVmcbCtrl->IntCtrl.n.u8VTPR & 0xf;
|
---|
5692 | break;
|
---|
5693 | }
|
---|
5694 | }
|
---|
5695 | #endif
|
---|
5696 | uint8_t uTpr;
|
---|
5697 | int rc = APICGetTpr(pVCpu, &uTpr, NULL, NULL);
|
---|
5698 | if (RT_SUCCESS(rc))
|
---|
5699 | crX = uTpr >> 4;
|
---|
5700 | else
|
---|
5701 | crX = 0;
|
---|
5702 | break;
|
---|
5703 | }
|
---|
5704 | IEM_NOT_REACHED_DEFAULT_CASE_RET(); /* call checks */
|
---|
5705 | }
|
---|
5706 |
|
---|
5707 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
5708 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
5709 | {
|
---|
5710 | switch (iCrReg)
|
---|
5711 | {
|
---|
5712 | /* CR0/CR4 reads are subject to masking when in VMX non-root mode. */
|
---|
5713 | case 0: crX = CPUMGetGuestVmxMaskedCr0(&pVCpu->cpum.GstCtx, pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64Cr0Mask.u); break;
|
---|
5714 | case 4: crX = CPUMGetGuestVmxMaskedCr4(&pVCpu->cpum.GstCtx, pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64Cr4Mask.u); break;
|
---|
5715 |
|
---|
5716 | case 3:
|
---|
5717 | {
|
---|
5718 | VBOXSTRICTRC rcStrict = iemVmxVmexitInstrMovFromCr3(pVCpu, iGReg, cbInstr);
|
---|
5719 | if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
|
---|
5720 | return rcStrict;
|
---|
5721 | break;
|
---|
5722 | }
|
---|
5723 | }
|
---|
5724 | }
|
---|
5725 | #endif
|
---|
5726 |
|
---|
5727 | /* Store it. */
|
---|
5728 | if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
|
---|
5729 | *(uint64_t *)iemGRegRef(pVCpu, iGReg) = crX;
|
---|
5730 | else
|
---|
5731 | *(uint64_t *)iemGRegRef(pVCpu, iGReg) = (uint32_t)crX;
|
---|
5732 |
|
---|
5733 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5734 | }
|
---|
5735 |
|
---|
5736 |
|
---|
5737 | /**
|
---|
5738 | * Implements smsw GReg.
|
---|
5739 | *
|
---|
5740 | * @param iGReg The general register to store the CRx value in.
|
---|
5741 | * @param enmEffOpSize The operand size.
|
---|
5742 | */
|
---|
5743 | IEM_CIMPL_DEF_2(iemCImpl_smsw_reg, uint8_t, iGReg, uint8_t, enmEffOpSize)
|
---|
5744 | {
|
---|
5745 | IEM_SVM_CHECK_READ_CR0_INTERCEPT(pVCpu, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
5746 |
|
---|
5747 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
5748 | uint64_t u64MaskedCr0;
|
---|
5749 | if (!IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
5750 | u64MaskedCr0 = pVCpu->cpum.GstCtx.cr0;
|
---|
5751 | else
|
---|
5752 | u64MaskedCr0 = CPUMGetGuestVmxMaskedCr0(&pVCpu->cpum.GstCtx, pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64Cr0Mask.u);
|
---|
5753 | uint64_t const u64GuestCr0 = u64MaskedCr0;
|
---|
5754 | #else
|
---|
5755 | uint64_t const u64GuestCr0 = pVCpu->cpum.GstCtx.cr0;
|
---|
5756 | #endif
|
---|
5757 |
|
---|
5758 | switch (enmEffOpSize)
|
---|
5759 | {
|
---|
5760 | case IEMMODE_16BIT:
|
---|
5761 | if (IEM_GET_TARGET_CPU(pVCpu) > IEMTARGETCPU_386)
|
---|
5762 | *(uint16_t *)iemGRegRef(pVCpu, iGReg) = (uint16_t)u64GuestCr0;
|
---|
5763 | else if (IEM_GET_TARGET_CPU(pVCpu) >= IEMTARGETCPU_386)
|
---|
5764 | *(uint16_t *)iemGRegRef(pVCpu, iGReg) = (uint16_t)u64GuestCr0 | 0xffe0;
|
---|
5765 | else
|
---|
5766 | *(uint16_t *)iemGRegRef(pVCpu, iGReg) = (uint16_t)u64GuestCr0 | 0xfff0;
|
---|
5767 | break;
|
---|
5768 |
|
---|
5769 | case IEMMODE_32BIT:
|
---|
5770 | *(uint32_t *)iemGRegRef(pVCpu, iGReg) = (uint32_t)u64GuestCr0;
|
---|
5771 | break;
|
---|
5772 |
|
---|
5773 | case IEMMODE_64BIT:
|
---|
5774 | *(uint64_t *)iemGRegRef(pVCpu, iGReg) = u64GuestCr0;
|
---|
5775 | break;
|
---|
5776 |
|
---|
5777 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
5778 | }
|
---|
5779 |
|
---|
5780 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5781 | }
|
---|
5782 |
|
---|
5783 |
|
---|
5784 | /**
|
---|
5785 | * Implements smsw mem.
|
---|
5786 | *
|
---|
5787 | * @param iEffSeg The effective segment register to use with @a GCPtrMem.
|
---|
5788 | * @param GCPtrEffDst Where to store the 16-bit CR0 value.
|
---|
5789 | */
|
---|
5790 | IEM_CIMPL_DEF_2(iemCImpl_smsw_mem, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
|
---|
5791 | {
|
---|
5792 | IEM_SVM_CHECK_READ_CR0_INTERCEPT(pVCpu, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
5793 |
|
---|
5794 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
5795 | uint64_t u64MaskedCr0;
|
---|
5796 | if (!IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
5797 | u64MaskedCr0 = pVCpu->cpum.GstCtx.cr0;
|
---|
5798 | else
|
---|
5799 | u64MaskedCr0 = CPUMGetGuestVmxMaskedCr0(&pVCpu->cpum.GstCtx, pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64Cr0Mask.u);
|
---|
5800 | uint64_t const u64GuestCr0 = u64MaskedCr0;
|
---|
5801 | #else
|
---|
5802 | uint64_t const u64GuestCr0 = pVCpu->cpum.GstCtx.cr0;
|
---|
5803 | #endif
|
---|
5804 |
|
---|
5805 | uint16_t u16Value;
|
---|
5806 | if (IEM_GET_TARGET_CPU(pVCpu) > IEMTARGETCPU_386)
|
---|
5807 | u16Value = (uint16_t)u64GuestCr0;
|
---|
5808 | else if (IEM_GET_TARGET_CPU(pVCpu) >= IEMTARGETCPU_386)
|
---|
5809 | u16Value = (uint16_t)u64GuestCr0 | 0xffe0;
|
---|
5810 | else
|
---|
5811 | u16Value = (uint16_t)u64GuestCr0 | 0xfff0;
|
---|
5812 |
|
---|
5813 | VBOXSTRICTRC rcStrict = iemMemStoreDataU16(pVCpu, iEffSeg, GCPtrEffDst, u16Value);
|
---|
5814 | if (rcStrict == VINF_SUCCESS)
|
---|
5815 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5816 | return rcStrict;
|
---|
5817 | }
|
---|
5818 |
|
---|
5819 |
|
---|
5820 | /**
|
---|
5821 | * Helper for mapping CR3 and PAE PDPEs for 'mov CRx,GReg'.
|
---|
5822 | */
|
---|
5823 | #define IEM_MAP_PAE_PDPES_AT_CR3_RET(a_pVCpu, a_iCrReg, a_uCr3) \
|
---|
5824 | do \
|
---|
5825 | { \
|
---|
5826 | int const rcX = PGMGstMapPaePdpesAtCr3(a_pVCpu, a_uCr3); \
|
---|
5827 | if (RT_SUCCESS(rcX)) \
|
---|
5828 | { /* likely */ } \
|
---|
5829 | else \
|
---|
5830 | { \
|
---|
5831 | /* Either invalid PDPTEs or CR3 second-level translation failed. Raise #GP(0) either way. */ \
|
---|
5832 | Log(("iemCImpl_load_Cr%#x: Trying to load invalid PAE PDPEs\n", a_iCrReg)); \
|
---|
5833 | return iemRaiseGeneralProtectionFault0(a_pVCpu); \
|
---|
5834 | } \
|
---|
5835 | } while (0)
|
---|
5836 |
|
---|
5837 |
|
---|
5838 | /**
|
---|
5839 | * Used to implemented 'mov CRx,GReg' and 'lmsw r/m16'.
|
---|
5840 | *
|
---|
5841 | * @param iCrReg The CRx register to write (valid).
|
---|
5842 | * @param uNewCrX The new value.
|
---|
5843 | * @param enmAccessCrX The instruction that caused the CrX load.
|
---|
5844 | * @param iGReg The general register in case of a 'mov CRx,GReg'
|
---|
5845 | * instruction.
|
---|
5846 | */
|
---|
5847 | IEM_CIMPL_DEF_4(iemCImpl_load_CrX, uint8_t, iCrReg, uint64_t, uNewCrX, IEMACCESSCRX, enmAccessCrX, uint8_t, iGReg)
|
---|
5848 | {
|
---|
5849 | VBOXSTRICTRC rcStrict;
|
---|
5850 | int rc;
|
---|
5851 | #ifndef VBOX_WITH_NESTED_HWVIRT_SVM
|
---|
5852 | RT_NOREF2(iGReg, enmAccessCrX);
|
---|
5853 | #endif
|
---|
5854 |
|
---|
5855 | /*
|
---|
5856 | * Try store it.
|
---|
5857 | * Unfortunately, CPUM only does a tiny bit of the work.
|
---|
5858 | */
|
---|
5859 | switch (iCrReg)
|
---|
5860 | {
|
---|
5861 | case 0:
|
---|
5862 | {
|
---|
5863 | /*
|
---|
5864 | * Perform checks.
|
---|
5865 | */
|
---|
5866 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0);
|
---|
5867 |
|
---|
5868 | uint64_t const uOldCrX = pVCpu->cpum.GstCtx.cr0;
|
---|
5869 | uint32_t const fValid = CPUMGetGuestCR0ValidMask();
|
---|
5870 |
|
---|
5871 | /* ET is hardcoded on 486 and later. */
|
---|
5872 | if (IEM_GET_TARGET_CPU(pVCpu) > IEMTARGETCPU_486)
|
---|
5873 | uNewCrX |= X86_CR0_ET;
|
---|
5874 | /* The 386 and 486 didn't #GP(0) on attempting to set reserved CR0 bits. ET was settable on 386. */
|
---|
5875 | else if (IEM_GET_TARGET_CPU(pVCpu) == IEMTARGETCPU_486)
|
---|
5876 | {
|
---|
5877 | uNewCrX &= fValid;
|
---|
5878 | uNewCrX |= X86_CR0_ET;
|
---|
5879 | }
|
---|
5880 | else
|
---|
5881 | uNewCrX &= X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG | X86_CR0_ET;
|
---|
5882 |
|
---|
5883 | /* Check for reserved bits. */
|
---|
5884 | if (uNewCrX & ~(uint64_t)fValid)
|
---|
5885 | {
|
---|
5886 | Log(("Trying to set reserved CR0 bits: NewCR0=%#llx InvalidBits=%#llx\n", uNewCrX, uNewCrX & ~(uint64_t)fValid));
|
---|
5887 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5888 | }
|
---|
5889 |
|
---|
5890 | /* Check for invalid combinations. */
|
---|
5891 | if ( (uNewCrX & X86_CR0_PG)
|
---|
5892 | && !(uNewCrX & X86_CR0_PE) )
|
---|
5893 | {
|
---|
5894 | Log(("Trying to set CR0.PG without CR0.PE\n"));
|
---|
5895 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5896 | }
|
---|
5897 |
|
---|
5898 | if ( !(uNewCrX & X86_CR0_CD)
|
---|
5899 | && (uNewCrX & X86_CR0_NW) )
|
---|
5900 | {
|
---|
5901 | Log(("Trying to clear CR0.CD while leaving CR0.NW set\n"));
|
---|
5902 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5903 | }
|
---|
5904 |
|
---|
5905 | if ( !(uNewCrX & X86_CR0_PG)
|
---|
5906 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_PCIDE))
|
---|
5907 | {
|
---|
5908 | Log(("Trying to clear CR0.PG while leaving CR4.PCID set\n"));
|
---|
5909 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5910 | }
|
---|
5911 |
|
---|
5912 | /* Long mode consistency checks. */
|
---|
5913 | if ( (uNewCrX & X86_CR0_PG)
|
---|
5914 | && !(uOldCrX & X86_CR0_PG)
|
---|
5915 | && (pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_LME) )
|
---|
5916 | {
|
---|
5917 | if (!(pVCpu->cpum.GstCtx.cr4 & X86_CR4_PAE))
|
---|
5918 | {
|
---|
5919 | Log(("Trying to enabled long mode paging without CR4.PAE set\n"));
|
---|
5920 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5921 | }
|
---|
5922 | if (pVCpu->cpum.GstCtx.cs.Attr.n.u1Long)
|
---|
5923 | {
|
---|
5924 | Log(("Trying to enabled long mode paging with a long CS descriptor loaded.\n"));
|
---|
5925 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5926 | }
|
---|
5927 | }
|
---|
5928 |
|
---|
5929 | /* Check for bits that must remain set or cleared in VMX operation,
|
---|
5930 | see Intel spec. 23.8 "Restrictions on VMX operation". */
|
---|
5931 | if (IEM_VMX_IS_ROOT_MODE(pVCpu))
|
---|
5932 | {
|
---|
5933 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
5934 | uint64_t const uCr0Fixed0 = IEM_VMX_IS_NON_ROOT_MODE(pVCpu) ? iemVmxGetCr0Fixed0(pVCpu) : VMX_V_CR0_FIXED0;
|
---|
5935 | #else
|
---|
5936 | uint64_t const uCr0Fixed0 = VMX_V_CR0_FIXED0;
|
---|
5937 | #endif
|
---|
5938 | if ((uNewCrX & uCr0Fixed0) != uCr0Fixed0)
|
---|
5939 | {
|
---|
5940 | Log(("Trying to clear reserved CR0 bits in VMX operation: NewCr0=%#llx MB1=%#llx\n", uNewCrX, uCr0Fixed0));
|
---|
5941 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5942 | }
|
---|
5943 |
|
---|
5944 | uint64_t const uCr0Fixed1 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr0Fixed1;
|
---|
5945 | if (uNewCrX & ~uCr0Fixed1)
|
---|
5946 | {
|
---|
5947 | Log(("Trying to set reserved CR0 bits in VMX operation: NewCr0=%#llx MB0=%#llx\n", uNewCrX, uCr0Fixed1));
|
---|
5948 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5949 | }
|
---|
5950 | }
|
---|
5951 |
|
---|
5952 | /*
|
---|
5953 | * SVM nested-guest CR0 write intercepts.
|
---|
5954 | */
|
---|
5955 | if (IEM_SVM_IS_WRITE_CR_INTERCEPT_SET(pVCpu, iCrReg))
|
---|
5956 | {
|
---|
5957 | Log(("iemCImpl_load_Cr%#x: Guest intercept -> #VMEXIT\n", iCrReg));
|
---|
5958 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
5959 | IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_WRITE_CR0, enmAccessCrX, iGReg);
|
---|
5960 | }
|
---|
5961 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_CR0_SEL_WRITE))
|
---|
5962 | {
|
---|
5963 | /* 'lmsw' intercepts regardless of whether the TS/MP bits are actually toggled. */
|
---|
5964 | if ( enmAccessCrX == IEMACCESSCRX_LMSW
|
---|
5965 | || (uNewCrX & ~(X86_CR0_TS | X86_CR0_MP)) != (uOldCrX & ~(X86_CR0_TS | X86_CR0_MP)))
|
---|
5966 | {
|
---|
5967 | Assert(enmAccessCrX != IEMACCESSCRX_CLTS);
|
---|
5968 | Log(("iemCImpl_load_Cr%#x: lmsw or bits other than TS/MP changed: Guest intercept -> #VMEXIT\n", iCrReg));
|
---|
5969 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
5970 | IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_CR0_SEL_WRITE, enmAccessCrX, iGReg);
|
---|
5971 | }
|
---|
5972 | }
|
---|
5973 |
|
---|
5974 | /*
|
---|
5975 | * Change EFER.LMA if entering or leaving long mode.
|
---|
5976 | */
|
---|
5977 | uint64_t NewEFER = pVCpu->cpum.GstCtx.msrEFER;
|
---|
5978 | if ( (uNewCrX & X86_CR0_PG) != (uOldCrX & X86_CR0_PG)
|
---|
5979 | && (pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_LME) )
|
---|
5980 | {
|
---|
5981 | if (uNewCrX & X86_CR0_PG)
|
---|
5982 | NewEFER |= MSR_K6_EFER_LMA;
|
---|
5983 | else
|
---|
5984 | NewEFER &= ~MSR_K6_EFER_LMA;
|
---|
5985 |
|
---|
5986 | CPUMSetGuestEFER(pVCpu, NewEFER);
|
---|
5987 | Assert(pVCpu->cpum.GstCtx.msrEFER == NewEFER);
|
---|
5988 | }
|
---|
5989 |
|
---|
5990 | /*
|
---|
5991 | * Inform PGM.
|
---|
5992 | */
|
---|
5993 | if ( (uNewCrX & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE | X86_CR0_CD | X86_CR0_NW))
|
---|
5994 | != (uOldCrX & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE | X86_CR0_CD | X86_CR0_NW)) )
|
---|
5995 | {
|
---|
5996 | if ( enmAccessCrX != IEMACCESSCRX_MOV_CRX
|
---|
5997 | || !CPUMIsPaePagingEnabled(uNewCrX, pVCpu->cpum.GstCtx.cr4, NewEFER)
|
---|
5998 | || CPUMIsGuestInSvmNestedHwVirtMode(IEM_GET_CTX(pVCpu)))
|
---|
5999 | { /* likely */ }
|
---|
6000 | else
|
---|
6001 | IEM_MAP_PAE_PDPES_AT_CR3_RET(pVCpu, iCrReg, pVCpu->cpum.GstCtx.cr3);
|
---|
6002 | rc = PGMFlushTLB(pVCpu, pVCpu->cpum.GstCtx.cr3, true /* global */);
|
---|
6003 | AssertRCReturn(rc, rc);
|
---|
6004 | /* ignore informational status codes */
|
---|
6005 | }
|
---|
6006 |
|
---|
6007 | /*
|
---|
6008 | * Change CR0.
|
---|
6009 | */
|
---|
6010 | CPUMSetGuestCR0(pVCpu, uNewCrX);
|
---|
6011 | Assert(pVCpu->cpum.GstCtx.cr0 == uNewCrX);
|
---|
6012 |
|
---|
6013 | rcStrict = PGMChangeMode(pVCpu, pVCpu->cpum.GstCtx.cr0, pVCpu->cpum.GstCtx.cr4, pVCpu->cpum.GstCtx.msrEFER,
|
---|
6014 | false /* fForce */);
|
---|
6015 | break;
|
---|
6016 | }
|
---|
6017 |
|
---|
6018 | /*
|
---|
6019 | * CR2 can be changed without any restrictions.
|
---|
6020 | */
|
---|
6021 | case 2:
|
---|
6022 | {
|
---|
6023 | if (IEM_SVM_IS_WRITE_CR_INTERCEPT_SET(pVCpu, /*cr*/ 2))
|
---|
6024 | {
|
---|
6025 | Log(("iemCImpl_load_Cr%#x: Guest intercept -> #VMEXIT\n", iCrReg));
|
---|
6026 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
6027 | IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_WRITE_CR2, enmAccessCrX, iGReg);
|
---|
6028 | }
|
---|
6029 | pVCpu->cpum.GstCtx.cr2 = uNewCrX;
|
---|
6030 | pVCpu->cpum.GstCtx.fExtrn &= ~CPUMCTX_EXTRN_CR2;
|
---|
6031 | rcStrict = VINF_SUCCESS;
|
---|
6032 | break;
|
---|
6033 | }
|
---|
6034 |
|
---|
6035 | /*
|
---|
6036 | * CR3 is relatively simple, although AMD and Intel have different
|
---|
6037 | * accounts of how setting reserved bits are handled. We take intel's
|
---|
6038 | * word for the lower bits and AMD's for the high bits (63:52). The
|
---|
6039 | * lower reserved bits are ignored and left alone; OpenBSD 5.8 relies
|
---|
6040 | * on this.
|
---|
6041 | */
|
---|
6042 | /** @todo Testcase: Setting reserved bits in CR3, especially before
|
---|
6043 | * enabling paging. */
|
---|
6044 | case 3:
|
---|
6045 | {
|
---|
6046 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR3);
|
---|
6047 |
|
---|
6048 | /* Bit 63 being clear in the source operand with PCIDE indicates no invalidations are required. */
|
---|
6049 | if ( (pVCpu->cpum.GstCtx.cr4 & X86_CR4_PCIDE)
|
---|
6050 | && (uNewCrX & RT_BIT_64(63)))
|
---|
6051 | {
|
---|
6052 | /** @todo r=ramshankar: avoiding a TLB flush altogether here causes Windows 10
|
---|
6053 | * SMP(w/o nested-paging) to hang during bootup on Skylake systems, see
|
---|
6054 | * Intel spec. 4.10.4.1 "Operations that Invalidate TLBs and
|
---|
6055 | * Paging-Structure Caches". */
|
---|
6056 | uNewCrX &= ~RT_BIT_64(63);
|
---|
6057 | }
|
---|
6058 |
|
---|
6059 | /* Check / mask the value. */
|
---|
6060 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX_EPT
|
---|
6061 | /* See Intel spec. 27.2.2 "EPT Translation Mechanism" footnote. */
|
---|
6062 | uint64_t const fInvPhysMask = !CPUMIsGuestVmxEptPagingEnabledEx(IEM_GET_CTX(pVCpu))
|
---|
6063 | ? (UINT64_MAX << IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cMaxPhysAddrWidth)
|
---|
6064 | : (~X86_CR3_EPT_PAGE_MASK & X86_PAGE_4K_BASE_MASK);
|
---|
6065 | #else
|
---|
6066 | uint64_t const fInvPhysMask = UINT64_C(0xfff0000000000000);
|
---|
6067 | #endif
|
---|
6068 | if (uNewCrX & fInvPhysMask)
|
---|
6069 | {
|
---|
6070 | /** @todo Should we raise this only for 64-bit mode like Intel claims? AMD is
|
---|
6071 | * very vague in this area. As mentioned above, need testcase on real
|
---|
6072 | * hardware... Sigh. */
|
---|
6073 | Log(("Trying to load CR3 with invalid high bits set: %#llx\n", uNewCrX));
|
---|
6074 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6075 | }
|
---|
6076 |
|
---|
6077 | uint64_t fValid;
|
---|
6078 | if ( (pVCpu->cpum.GstCtx.cr4 & X86_CR4_PAE)
|
---|
6079 | && (pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_LME))
|
---|
6080 | {
|
---|
6081 | /** @todo Redundant? This value has already been validated above. */
|
---|
6082 | fValid = UINT64_C(0x000fffffffffffff);
|
---|
6083 | }
|
---|
6084 | else
|
---|
6085 | fValid = UINT64_C(0xffffffff);
|
---|
6086 | if (uNewCrX & ~fValid)
|
---|
6087 | {
|
---|
6088 | Log(("Automatically clearing reserved MBZ bits in CR3 load: NewCR3=%#llx ClearedBits=%#llx\n",
|
---|
6089 | uNewCrX, uNewCrX & ~fValid));
|
---|
6090 | uNewCrX &= fValid;
|
---|
6091 | }
|
---|
6092 |
|
---|
6093 | if (IEM_SVM_IS_WRITE_CR_INTERCEPT_SET(pVCpu, /*cr*/ 3))
|
---|
6094 | {
|
---|
6095 | Log(("iemCImpl_load_Cr%#x: Guest intercept -> #VMEXIT\n", iCrReg));
|
---|
6096 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
6097 | IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_WRITE_CR3, enmAccessCrX, iGReg);
|
---|
6098 | }
|
---|
6099 |
|
---|
6100 | /* Inform PGM. */
|
---|
6101 | if (pVCpu->cpum.GstCtx.cr0 & X86_CR0_PG)
|
---|
6102 | {
|
---|
6103 | if ( !CPUMIsGuestInPAEModeEx(IEM_GET_CTX(pVCpu))
|
---|
6104 | || CPUMIsGuestInSvmNestedHwVirtMode(IEM_GET_CTX(pVCpu)))
|
---|
6105 | { /* likely */ }
|
---|
6106 | else
|
---|
6107 | {
|
---|
6108 | Assert(enmAccessCrX == IEMACCESSCRX_MOV_CRX);
|
---|
6109 | IEM_MAP_PAE_PDPES_AT_CR3_RET(pVCpu, iCrReg, uNewCrX);
|
---|
6110 | }
|
---|
6111 | rc = PGMFlushTLB(pVCpu, uNewCrX, !(pVCpu->cpum.GstCtx.cr4 & X86_CR4_PGE));
|
---|
6112 | AssertRCReturn(rc, rc);
|
---|
6113 | /* ignore informational status codes */
|
---|
6114 | }
|
---|
6115 |
|
---|
6116 | /* Make the change. */
|
---|
6117 | rc = CPUMSetGuestCR3(pVCpu, uNewCrX);
|
---|
6118 | AssertRCSuccessReturn(rc, rc);
|
---|
6119 |
|
---|
6120 | rcStrict = VINF_SUCCESS;
|
---|
6121 | break;
|
---|
6122 | }
|
---|
6123 |
|
---|
6124 | /*
|
---|
6125 | * CR4 is a bit more tedious as there are bits which cannot be cleared
|
---|
6126 | * under some circumstances and such.
|
---|
6127 | */
|
---|
6128 | case 4:
|
---|
6129 | {
|
---|
6130 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR4);
|
---|
6131 | uint64_t const uOldCrX = pVCpu->cpum.GstCtx.cr4;
|
---|
6132 |
|
---|
6133 | /* Reserved bits. */
|
---|
6134 | uint32_t const fValid = CPUMGetGuestCR4ValidMask(pVCpu->CTX_SUFF(pVM));
|
---|
6135 | if (uNewCrX & ~(uint64_t)fValid)
|
---|
6136 | {
|
---|
6137 | Log(("Trying to set reserved CR4 bits: NewCR4=%#llx InvalidBits=%#llx\n", uNewCrX, uNewCrX & ~(uint64_t)fValid));
|
---|
6138 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6139 | }
|
---|
6140 |
|
---|
6141 | bool const fPcide = !(uOldCrX & X86_CR4_PCIDE) && (uNewCrX & X86_CR4_PCIDE);
|
---|
6142 | bool const fLongMode = CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu));
|
---|
6143 |
|
---|
6144 | /* PCIDE check. */
|
---|
6145 | if ( fPcide
|
---|
6146 | && ( !fLongMode
|
---|
6147 | || (pVCpu->cpum.GstCtx.cr3 & UINT64_C(0xfff))))
|
---|
6148 | {
|
---|
6149 | Log(("Trying to set PCIDE with invalid PCID or outside long mode. Pcid=%#x\n", (pVCpu->cpum.GstCtx.cr3 & UINT64_C(0xfff))));
|
---|
6150 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6151 | }
|
---|
6152 |
|
---|
6153 | /* PAE check. */
|
---|
6154 | if ( fLongMode
|
---|
6155 | && (uOldCrX & X86_CR4_PAE)
|
---|
6156 | && !(uNewCrX & X86_CR4_PAE))
|
---|
6157 | {
|
---|
6158 | Log(("Trying to set clear CR4.PAE while long mode is active\n"));
|
---|
6159 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6160 | }
|
---|
6161 |
|
---|
6162 | if (IEM_SVM_IS_WRITE_CR_INTERCEPT_SET(pVCpu, /*cr*/ 4))
|
---|
6163 | {
|
---|
6164 | Log(("iemCImpl_load_Cr%#x: Guest intercept -> #VMEXIT\n", iCrReg));
|
---|
6165 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
6166 | IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_WRITE_CR4, enmAccessCrX, iGReg);
|
---|
6167 | }
|
---|
6168 |
|
---|
6169 | /* Check for bits that must remain set or cleared in VMX operation,
|
---|
6170 | see Intel spec. 23.8 "Restrictions on VMX operation". */
|
---|
6171 | if (IEM_VMX_IS_ROOT_MODE(pVCpu))
|
---|
6172 | {
|
---|
6173 | uint64_t const uCr4Fixed0 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr4Fixed0;
|
---|
6174 | if ((uNewCrX & uCr4Fixed0) != uCr4Fixed0)
|
---|
6175 | {
|
---|
6176 | Log(("Trying to clear reserved CR4 bits in VMX operation: NewCr4=%#llx MB1=%#llx\n", uNewCrX, uCr4Fixed0));
|
---|
6177 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6178 | }
|
---|
6179 |
|
---|
6180 | uint64_t const uCr4Fixed1 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr4Fixed1;
|
---|
6181 | if (uNewCrX & ~uCr4Fixed1)
|
---|
6182 | {
|
---|
6183 | Log(("Trying to set reserved CR4 bits in VMX operation: NewCr4=%#llx MB0=%#llx\n", uNewCrX, uCr4Fixed1));
|
---|
6184 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6185 | }
|
---|
6186 | }
|
---|
6187 |
|
---|
6188 | /*
|
---|
6189 | * Notify PGM.
|
---|
6190 | */
|
---|
6191 | if ((uNewCrX ^ uOldCrX) & (X86_CR4_PSE | X86_CR4_PAE | X86_CR4_PGE | X86_CR4_PCIDE /* | X86_CR4_SMEP */))
|
---|
6192 | {
|
---|
6193 | if ( !CPUMIsPaePagingEnabled(pVCpu->cpum.GstCtx.cr0, uNewCrX, pVCpu->cpum.GstCtx.msrEFER)
|
---|
6194 | || CPUMIsGuestInSvmNestedHwVirtMode(IEM_GET_CTX(pVCpu)))
|
---|
6195 | { /* likely */ }
|
---|
6196 | else
|
---|
6197 | {
|
---|
6198 | Assert(enmAccessCrX == IEMACCESSCRX_MOV_CRX);
|
---|
6199 | IEM_MAP_PAE_PDPES_AT_CR3_RET(pVCpu, iCrReg, pVCpu->cpum.GstCtx.cr3);
|
---|
6200 | }
|
---|
6201 | rc = PGMFlushTLB(pVCpu, pVCpu->cpum.GstCtx.cr3, true /* global */);
|
---|
6202 | AssertRCReturn(rc, rc);
|
---|
6203 | /* ignore informational status codes */
|
---|
6204 | }
|
---|
6205 |
|
---|
6206 | /*
|
---|
6207 | * Change it.
|
---|
6208 | */
|
---|
6209 | rc = CPUMSetGuestCR4(pVCpu, uNewCrX);
|
---|
6210 | AssertRCSuccessReturn(rc, rc);
|
---|
6211 | Assert(pVCpu->cpum.GstCtx.cr4 == uNewCrX);
|
---|
6212 |
|
---|
6213 | rcStrict = PGMChangeMode(pVCpu, pVCpu->cpum.GstCtx.cr0, pVCpu->cpum.GstCtx.cr4, pVCpu->cpum.GstCtx.msrEFER,
|
---|
6214 | false /* fForce */);
|
---|
6215 | break;
|
---|
6216 | }
|
---|
6217 |
|
---|
6218 | /*
|
---|
6219 | * CR8 maps to the APIC TPR.
|
---|
6220 | */
|
---|
6221 | case 8:
|
---|
6222 | {
|
---|
6223 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_APIC_TPR);
|
---|
6224 | if (uNewCrX & ~(uint64_t)0xf)
|
---|
6225 | {
|
---|
6226 | Log(("Trying to set reserved CR8 bits (%#RX64)\n", uNewCrX));
|
---|
6227 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6228 | }
|
---|
6229 |
|
---|
6230 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
6231 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
6232 | && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_USE_TPR_SHADOW))
|
---|
6233 | {
|
---|
6234 | /*
|
---|
6235 | * If the Mov-to-CR8 doesn't cause a VM-exit, bits 0:3 of the source operand
|
---|
6236 | * is copied to bits 7:4 of the VTPR. Bits 0:3 and bits 31:8 of the VTPR are
|
---|
6237 | * cleared. Following this the processor performs TPR virtualization.
|
---|
6238 | *
|
---|
6239 | * However, we should not perform TPR virtualization immediately here but
|
---|
6240 | * after this instruction has completed.
|
---|
6241 | *
|
---|
6242 | * See Intel spec. 29.3 "Virtualizing CR8-based TPR Accesses"
|
---|
6243 | * See Intel spec. 27.1 "Architectural State Before A VM-exit"
|
---|
6244 | */
|
---|
6245 | uint32_t const uTpr = (uNewCrX & 0xf) << 4;
|
---|
6246 | Log(("iemCImpl_load_Cr%#x: Virtualizing TPR (%#x) write\n", iCrReg, uTpr));
|
---|
6247 | iemVmxVirtApicWriteRaw32(pVCpu, XAPIC_OFF_TPR, uTpr);
|
---|
6248 | iemVmxVirtApicSetPendingWrite(pVCpu, XAPIC_OFF_TPR);
|
---|
6249 | rcStrict = VINF_SUCCESS;
|
---|
6250 | break;
|
---|
6251 | }
|
---|
6252 | #endif
|
---|
6253 |
|
---|
6254 | #ifdef VBOX_WITH_NESTED_HWVIRT_SVM
|
---|
6255 | if (CPUMIsGuestInSvmNestedHwVirtMode(IEM_GET_CTX(pVCpu)))
|
---|
6256 | {
|
---|
6257 | if (IEM_SVM_IS_WRITE_CR_INTERCEPT_SET(pVCpu, /*cr*/ 8))
|
---|
6258 | {
|
---|
6259 | Log(("iemCImpl_load_Cr%#x: Guest intercept -> #VMEXIT\n", iCrReg));
|
---|
6260 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
6261 | IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_WRITE_CR8, enmAccessCrX, iGReg);
|
---|
6262 | }
|
---|
6263 |
|
---|
6264 | pVCpu->cpum.GstCtx.hwvirt.svm.Vmcb.ctrl.IntCtrl.n.u8VTPR = uNewCrX;
|
---|
6265 | if (CPUMIsGuestSvmVirtIntrMasking(pVCpu, IEM_GET_CTX(pVCpu)))
|
---|
6266 | {
|
---|
6267 | rcStrict = VINF_SUCCESS;
|
---|
6268 | break;
|
---|
6269 | }
|
---|
6270 | }
|
---|
6271 | #endif
|
---|
6272 | uint8_t const u8Tpr = (uint8_t)uNewCrX << 4;
|
---|
6273 | APICSetTpr(pVCpu, u8Tpr);
|
---|
6274 | rcStrict = VINF_SUCCESS;
|
---|
6275 | break;
|
---|
6276 | }
|
---|
6277 |
|
---|
6278 | IEM_NOT_REACHED_DEFAULT_CASE_RET(); /* call checks */
|
---|
6279 | }
|
---|
6280 |
|
---|
6281 | /*
|
---|
6282 | * Advance the RIP on success.
|
---|
6283 | */
|
---|
6284 | if (RT_SUCCESS(rcStrict))
|
---|
6285 | {
|
---|
6286 | if (rcStrict != VINF_SUCCESS)
|
---|
6287 | iemSetPassUpStatus(pVCpu, rcStrict);
|
---|
6288 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
6289 | }
|
---|
6290 |
|
---|
6291 | return rcStrict;
|
---|
6292 | }
|
---|
6293 |
|
---|
6294 |
|
---|
6295 | /**
|
---|
6296 | * Implements mov CRx,GReg.
|
---|
6297 | *
|
---|
6298 | * @param iCrReg The CRx register to write (valid).
|
---|
6299 | * @param iGReg The general register to load the CRx value from.
|
---|
6300 | */
|
---|
6301 | IEM_CIMPL_DEF_2(iemCImpl_mov_Cd_Rd, uint8_t, iCrReg, uint8_t, iGReg)
|
---|
6302 | {
|
---|
6303 | if (pVCpu->iem.s.uCpl != 0)
|
---|
6304 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6305 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
6306 |
|
---|
6307 | /*
|
---|
6308 | * Read the new value from the source register and call common worker.
|
---|
6309 | */
|
---|
6310 | uint64_t uNewCrX;
|
---|
6311 | if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
|
---|
6312 | uNewCrX = iemGRegFetchU64(pVCpu, iGReg);
|
---|
6313 | else
|
---|
6314 | uNewCrX = iemGRegFetchU32(pVCpu, iGReg);
|
---|
6315 |
|
---|
6316 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
6317 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
6318 | {
|
---|
6319 | VBOXSTRICTRC rcStrict = VINF_VMX_INTERCEPT_NOT_ACTIVE;
|
---|
6320 | switch (iCrReg)
|
---|
6321 | {
|
---|
6322 | case 0:
|
---|
6323 | case 4: rcStrict = iemVmxVmexitInstrMovToCr0Cr4(pVCpu, iCrReg, &uNewCrX, iGReg, cbInstr); break;
|
---|
6324 | case 3: rcStrict = iemVmxVmexitInstrMovToCr3(pVCpu, uNewCrX, iGReg, cbInstr); break;
|
---|
6325 | case 8: rcStrict = iemVmxVmexitInstrMovToCr8(pVCpu, iGReg, cbInstr); break;
|
---|
6326 | }
|
---|
6327 | if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
|
---|
6328 | return rcStrict;
|
---|
6329 | }
|
---|
6330 | #endif
|
---|
6331 |
|
---|
6332 | return IEM_CIMPL_CALL_4(iemCImpl_load_CrX, iCrReg, uNewCrX, IEMACCESSCRX_MOV_CRX, iGReg);
|
---|
6333 | }
|
---|
6334 |
|
---|
6335 |
|
---|
6336 | /**
|
---|
6337 | * Implements 'LMSW r/m16'
|
---|
6338 | *
|
---|
6339 | * @param u16NewMsw The new value.
|
---|
6340 | * @param GCPtrEffDst The guest-linear address of the source operand in case
|
---|
6341 | * of a memory operand. For register operand, pass
|
---|
6342 | * NIL_RTGCPTR.
|
---|
6343 | */
|
---|
6344 | IEM_CIMPL_DEF_2(iemCImpl_lmsw, uint16_t, u16NewMsw, RTGCPTR, GCPtrEffDst)
|
---|
6345 | {
|
---|
6346 | if (pVCpu->iem.s.uCpl != 0)
|
---|
6347 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6348 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
6349 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0);
|
---|
6350 |
|
---|
6351 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
6352 | /* Check nested-guest VMX intercept and get updated MSW if there's no VM-exit. */
|
---|
6353 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
6354 | {
|
---|
6355 | VBOXSTRICTRC rcStrict = iemVmxVmexitInstrLmsw(pVCpu, pVCpu->cpum.GstCtx.cr0, &u16NewMsw, GCPtrEffDst, cbInstr);
|
---|
6356 | if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
|
---|
6357 | return rcStrict;
|
---|
6358 | }
|
---|
6359 | #else
|
---|
6360 | RT_NOREF_PV(GCPtrEffDst);
|
---|
6361 | #endif
|
---|
6362 |
|
---|
6363 | /*
|
---|
6364 | * Compose the new CR0 value and call common worker.
|
---|
6365 | */
|
---|
6366 | uint64_t uNewCr0 = pVCpu->cpum.GstCtx.cr0 & ~(X86_CR0_MP | X86_CR0_EM | X86_CR0_TS);
|
---|
6367 | uNewCr0 |= u16NewMsw & (X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS);
|
---|
6368 | return IEM_CIMPL_CALL_4(iemCImpl_load_CrX, /*cr*/ 0, uNewCr0, IEMACCESSCRX_LMSW, UINT8_MAX /* iGReg */);
|
---|
6369 | }
|
---|
6370 |
|
---|
6371 |
|
---|
6372 | /**
|
---|
6373 | * Implements 'CLTS'.
|
---|
6374 | */
|
---|
6375 | IEM_CIMPL_DEF_0(iemCImpl_clts)
|
---|
6376 | {
|
---|
6377 | if (pVCpu->iem.s.uCpl != 0)
|
---|
6378 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6379 |
|
---|
6380 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0);
|
---|
6381 | uint64_t uNewCr0 = pVCpu->cpum.GstCtx.cr0;
|
---|
6382 | uNewCr0 &= ~X86_CR0_TS;
|
---|
6383 |
|
---|
6384 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
6385 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
6386 | {
|
---|
6387 | VBOXSTRICTRC rcStrict = iemVmxVmexitInstrClts(pVCpu, cbInstr);
|
---|
6388 | if (rcStrict == VINF_VMX_MODIFIES_BEHAVIOR)
|
---|
6389 | uNewCr0 |= (pVCpu->cpum.GstCtx.cr0 & X86_CR0_TS);
|
---|
6390 | else if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
|
---|
6391 | return rcStrict;
|
---|
6392 | }
|
---|
6393 | #endif
|
---|
6394 |
|
---|
6395 | return IEM_CIMPL_CALL_4(iemCImpl_load_CrX, /*cr*/ 0, uNewCr0, IEMACCESSCRX_CLTS, UINT8_MAX /* iGReg */);
|
---|
6396 | }
|
---|
6397 |
|
---|
6398 |
|
---|
6399 | /**
|
---|
6400 | * Implements mov GReg,DRx.
|
---|
6401 | *
|
---|
6402 | * @param iGReg The general register to store the DRx value in.
|
---|
6403 | * @param iDrReg The DRx register to read (0-7).
|
---|
6404 | */
|
---|
6405 | IEM_CIMPL_DEF_2(iemCImpl_mov_Rd_Dd, uint8_t, iGReg, uint8_t, iDrReg)
|
---|
6406 | {
|
---|
6407 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
6408 | /*
|
---|
6409 | * Check nested-guest VMX intercept.
|
---|
6410 | * Unlike most other intercepts, the Mov DRx intercept takes preceedence
|
---|
6411 | * over CPL and CR4.DE and even DR4/DR5 checks.
|
---|
6412 | *
|
---|
6413 | * See Intel spec. 25.1.3 "Instructions That Cause VM Exits Conditionally".
|
---|
6414 | */
|
---|
6415 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
6416 | {
|
---|
6417 | VBOXSTRICTRC rcStrict = iemVmxVmexitInstrMovDrX(pVCpu, VMXINSTRID_MOV_FROM_DRX, iDrReg, iGReg, cbInstr);
|
---|
6418 | if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
|
---|
6419 | return rcStrict;
|
---|
6420 | }
|
---|
6421 | #endif
|
---|
6422 |
|
---|
6423 | /*
|
---|
6424 | * Check preconditions.
|
---|
6425 | */
|
---|
6426 | /* Raise GPs. */
|
---|
6427 | if (pVCpu->iem.s.uCpl != 0)
|
---|
6428 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6429 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
6430 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_DR7);
|
---|
6431 |
|
---|
6432 | /** @todo \#UD in outside ring-0 too? */
|
---|
6433 | if (iDrReg == 4 || iDrReg == 5)
|
---|
6434 | {
|
---|
6435 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_CR4);
|
---|
6436 | if (pVCpu->cpum.GstCtx.cr4 & X86_CR4_DE)
|
---|
6437 | {
|
---|
6438 | Log(("mov r%u,dr%u: CR4.DE=1 -> #GP(0)\n", iGReg, iDrReg));
|
---|
6439 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6440 | }
|
---|
6441 | iDrReg += 2;
|
---|
6442 | }
|
---|
6443 |
|
---|
6444 | /* Raise #DB if general access detect is enabled. */
|
---|
6445 | if (pVCpu->cpum.GstCtx.dr[7] & X86_DR7_GD)
|
---|
6446 | {
|
---|
6447 | Log(("mov r%u,dr%u: DR7.GD=1 -> #DB\n", iGReg, iDrReg));
|
---|
6448 | return iemRaiseDebugException(pVCpu);
|
---|
6449 | }
|
---|
6450 |
|
---|
6451 | /*
|
---|
6452 | * Read the debug register and store it in the specified general register.
|
---|
6453 | */
|
---|
6454 | uint64_t drX;
|
---|
6455 | switch (iDrReg)
|
---|
6456 | {
|
---|
6457 | case 0:
|
---|
6458 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3);
|
---|
6459 | drX = pVCpu->cpum.GstCtx.dr[0];
|
---|
6460 | break;
|
---|
6461 | case 1:
|
---|
6462 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3);
|
---|
6463 | drX = pVCpu->cpum.GstCtx.dr[1];
|
---|
6464 | break;
|
---|
6465 | case 2:
|
---|
6466 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3);
|
---|
6467 | drX = pVCpu->cpum.GstCtx.dr[2];
|
---|
6468 | break;
|
---|
6469 | case 3:
|
---|
6470 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3);
|
---|
6471 | drX = pVCpu->cpum.GstCtx.dr[3];
|
---|
6472 | break;
|
---|
6473 | case 6:
|
---|
6474 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR6);
|
---|
6475 | drX = pVCpu->cpum.GstCtx.dr[6];
|
---|
6476 | drX |= X86_DR6_RA1_MASK;
|
---|
6477 | drX &= ~X86_DR6_RAZ_MASK;
|
---|
6478 | break;
|
---|
6479 | case 7:
|
---|
6480 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_DR7);
|
---|
6481 | drX = pVCpu->cpum.GstCtx.dr[7];
|
---|
6482 | drX |=X86_DR7_RA1_MASK;
|
---|
6483 | drX &= ~X86_DR7_RAZ_MASK;
|
---|
6484 | break;
|
---|
6485 | IEM_NOT_REACHED_DEFAULT_CASE_RET(); /* caller checks */
|
---|
6486 | }
|
---|
6487 |
|
---|
6488 | /** @todo SVM nested-guest intercept for DR8-DR15? */
|
---|
6489 | /*
|
---|
6490 | * Check for any SVM nested-guest intercepts for the DRx read.
|
---|
6491 | */
|
---|
6492 | if (IEM_SVM_IS_READ_DR_INTERCEPT_SET(pVCpu, iDrReg))
|
---|
6493 | {
|
---|
6494 | Log(("mov r%u,dr%u: Guest intercept -> #VMEXIT\n", iGReg, iDrReg));
|
---|
6495 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
6496 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_READ_DR0 + (iDrReg & 0xf),
|
---|
6497 | IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fSvmDecodeAssists ? (iGReg & 7) : 0, 0 /* uExitInfo2 */);
|
---|
6498 | }
|
---|
6499 |
|
---|
6500 | if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
|
---|
6501 | *(uint64_t *)iemGRegRef(pVCpu, iGReg) = drX;
|
---|
6502 | else
|
---|
6503 | *(uint64_t *)iemGRegRef(pVCpu, iGReg) = (uint32_t)drX;
|
---|
6504 |
|
---|
6505 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
6506 | }
|
---|
6507 |
|
---|
6508 |
|
---|
6509 | /**
|
---|
6510 | * Implements mov DRx,GReg.
|
---|
6511 | *
|
---|
6512 | * @param iDrReg The DRx register to write (valid).
|
---|
6513 | * @param iGReg The general register to load the DRx value from.
|
---|
6514 | */
|
---|
6515 | IEM_CIMPL_DEF_2(iemCImpl_mov_Dd_Rd, uint8_t, iDrReg, uint8_t, iGReg)
|
---|
6516 | {
|
---|
6517 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
6518 | /*
|
---|
6519 | * Check nested-guest VMX intercept.
|
---|
6520 | * Unlike most other intercepts, the Mov DRx intercept takes preceedence
|
---|
6521 | * over CPL and CR4.DE and even DR4/DR5 checks.
|
---|
6522 | *
|
---|
6523 | * See Intel spec. 25.1.3 "Instructions That Cause VM Exits Conditionally".
|
---|
6524 | */
|
---|
6525 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
6526 | {
|
---|
6527 | VBOXSTRICTRC rcStrict = iemVmxVmexitInstrMovDrX(pVCpu, VMXINSTRID_MOV_TO_DRX, iDrReg, iGReg, cbInstr);
|
---|
6528 | if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
|
---|
6529 | return rcStrict;
|
---|
6530 | }
|
---|
6531 | #endif
|
---|
6532 |
|
---|
6533 | /*
|
---|
6534 | * Check preconditions.
|
---|
6535 | */
|
---|
6536 | if (pVCpu->iem.s.uCpl != 0)
|
---|
6537 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6538 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
6539 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_DR7);
|
---|
6540 |
|
---|
6541 | if (iDrReg == 4 || iDrReg == 5)
|
---|
6542 | {
|
---|
6543 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_CR4);
|
---|
6544 | if (pVCpu->cpum.GstCtx.cr4 & X86_CR4_DE)
|
---|
6545 | {
|
---|
6546 | Log(("mov dr%u,r%u: CR4.DE=1 -> #GP(0)\n", iDrReg, iGReg));
|
---|
6547 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6548 | }
|
---|
6549 | iDrReg += 2;
|
---|
6550 | }
|
---|
6551 |
|
---|
6552 | /* Raise #DB if general access detect is enabled. */
|
---|
6553 | /** @todo is \#DB/DR7.GD raised before any reserved high bits in DR7/DR6
|
---|
6554 | * \#GP? */
|
---|
6555 | if (pVCpu->cpum.GstCtx.dr[7] & X86_DR7_GD)
|
---|
6556 | {
|
---|
6557 | Log(("mov dr%u,r%u: DR7.GD=1 -> #DB\n", iDrReg, iGReg));
|
---|
6558 | return iemRaiseDebugException(pVCpu);
|
---|
6559 | }
|
---|
6560 |
|
---|
6561 | /*
|
---|
6562 | * Read the new value from the source register.
|
---|
6563 | */
|
---|
6564 | uint64_t uNewDrX;
|
---|
6565 | if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
|
---|
6566 | uNewDrX = iemGRegFetchU64(pVCpu, iGReg);
|
---|
6567 | else
|
---|
6568 | uNewDrX = iemGRegFetchU32(pVCpu, iGReg);
|
---|
6569 |
|
---|
6570 | /*
|
---|
6571 | * Adjust it.
|
---|
6572 | */
|
---|
6573 | switch (iDrReg)
|
---|
6574 | {
|
---|
6575 | case 0:
|
---|
6576 | case 1:
|
---|
6577 | case 2:
|
---|
6578 | case 3:
|
---|
6579 | /* nothing to adjust */
|
---|
6580 | break;
|
---|
6581 |
|
---|
6582 | case 6:
|
---|
6583 | if (uNewDrX & X86_DR6_MBZ_MASK)
|
---|
6584 | {
|
---|
6585 | Log(("mov dr%u,%#llx: DR6 high bits are not zero -> #GP(0)\n", iDrReg, uNewDrX));
|
---|
6586 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6587 | }
|
---|
6588 | uNewDrX |= X86_DR6_RA1_MASK;
|
---|
6589 | uNewDrX &= ~X86_DR6_RAZ_MASK;
|
---|
6590 | break;
|
---|
6591 |
|
---|
6592 | case 7:
|
---|
6593 | if (uNewDrX & X86_DR7_MBZ_MASK)
|
---|
6594 | {
|
---|
6595 | Log(("mov dr%u,%#llx: DR7 high bits are not zero -> #GP(0)\n", iDrReg, uNewDrX));
|
---|
6596 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6597 | }
|
---|
6598 | uNewDrX |= X86_DR7_RA1_MASK;
|
---|
6599 | uNewDrX &= ~X86_DR7_RAZ_MASK;
|
---|
6600 | break;
|
---|
6601 |
|
---|
6602 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
6603 | }
|
---|
6604 |
|
---|
6605 | /** @todo SVM nested-guest intercept for DR8-DR15? */
|
---|
6606 | /*
|
---|
6607 | * Check for any SVM nested-guest intercepts for the DRx write.
|
---|
6608 | */
|
---|
6609 | if (IEM_SVM_IS_WRITE_DR_INTERCEPT_SET(pVCpu, iDrReg))
|
---|
6610 | {
|
---|
6611 | Log2(("mov dr%u,r%u: Guest intercept -> #VMEXIT\n", iDrReg, iGReg));
|
---|
6612 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
6613 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_WRITE_DR0 + (iDrReg & 0xf),
|
---|
6614 | IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fSvmDecodeAssists ? (iGReg & 7) : 0, 0 /* uExitInfo2 */);
|
---|
6615 | }
|
---|
6616 |
|
---|
6617 | /*
|
---|
6618 | * Do the actual setting.
|
---|
6619 | */
|
---|
6620 | if (iDrReg < 4)
|
---|
6621 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3);
|
---|
6622 | else if (iDrReg == 6)
|
---|
6623 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR6);
|
---|
6624 |
|
---|
6625 | int rc = CPUMSetGuestDRx(pVCpu, iDrReg, uNewDrX);
|
---|
6626 | AssertRCSuccessReturn(rc, RT_SUCCESS_NP(rc) ? VERR_IEM_IPE_1 : rc);
|
---|
6627 |
|
---|
6628 | /*
|
---|
6629 | * Re-init hardware breakpoint summary if it was DR7 that got changed.
|
---|
6630 | */
|
---|
6631 | if (iDrReg == 7)
|
---|
6632 | {
|
---|
6633 | pVCpu->iem.s.fPendingInstructionBreakpoints = false;
|
---|
6634 | pVCpu->iem.s.fPendingDataBreakpoints = false;
|
---|
6635 | pVCpu->iem.s.fPendingIoBreakpoints = false;
|
---|
6636 | iemInitPendingBreakpointsSlow(pVCpu);
|
---|
6637 | }
|
---|
6638 |
|
---|
6639 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
6640 | }
|
---|
6641 |
|
---|
6642 |
|
---|
6643 | /**
|
---|
6644 | * Implements mov GReg,TRx.
|
---|
6645 | *
|
---|
6646 | * @param iGReg The general register to store the
|
---|
6647 | * TRx value in.
|
---|
6648 | * @param iTrReg The TRx register to read (6/7).
|
---|
6649 | */
|
---|
6650 | IEM_CIMPL_DEF_2(iemCImpl_mov_Rd_Td, uint8_t, iGReg, uint8_t, iTrReg)
|
---|
6651 | {
|
---|
6652 | /*
|
---|
6653 | * Check preconditions. NB: This instruction is 386/486 only.
|
---|
6654 | */
|
---|
6655 |
|
---|
6656 | /* Raise GPs. */
|
---|
6657 | if (pVCpu->iem.s.uCpl != 0)
|
---|
6658 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6659 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
6660 |
|
---|
6661 | if (iTrReg < 6 || iTrReg > 7)
|
---|
6662 | {
|
---|
6663 | /** @todo Do Intel CPUs reject this or are the TRs aliased? */
|
---|
6664 | Log(("mov r%u,tr%u: invalid register -> #GP(0)\n", iGReg, iTrReg));
|
---|
6665 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6666 | }
|
---|
6667 |
|
---|
6668 | /*
|
---|
6669 | * Read the test register and store it in the specified general register.
|
---|
6670 | * This is currently a dummy implementation that only exists to satisfy
|
---|
6671 | * old debuggers like WDEB386 or OS/2 KDB which unconditionally read the
|
---|
6672 | * TR6/TR7 registers. Software which actually depends on the TR values
|
---|
6673 | * (different on 386/486) is exceedingly rare.
|
---|
6674 | */
|
---|
6675 | uint64_t trX;
|
---|
6676 | switch (iTrReg)
|
---|
6677 | {
|
---|
6678 | case 6:
|
---|
6679 | trX = 0; /* Currently a dummy. */
|
---|
6680 | break;
|
---|
6681 | case 7:
|
---|
6682 | trX = 0; /* Currently a dummy. */
|
---|
6683 | break;
|
---|
6684 | IEM_NOT_REACHED_DEFAULT_CASE_RET(); /* call checks */
|
---|
6685 | }
|
---|
6686 |
|
---|
6687 | *(uint64_t *)iemGRegRef(pVCpu, iGReg) = (uint32_t)trX;
|
---|
6688 |
|
---|
6689 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
6690 | }
|
---|
6691 |
|
---|
6692 |
|
---|
6693 | /**
|
---|
6694 | * Implements mov TRx,GReg.
|
---|
6695 | *
|
---|
6696 | * @param iTrReg The TRx register to write (valid).
|
---|
6697 | * @param iGReg The general register to load the TRx
|
---|
6698 | * value from.
|
---|
6699 | */
|
---|
6700 | IEM_CIMPL_DEF_2(iemCImpl_mov_Td_Rd, uint8_t, iTrReg, uint8_t, iGReg)
|
---|
6701 | {
|
---|
6702 | /*
|
---|
6703 | * Check preconditions. NB: This instruction is 386/486 only.
|
---|
6704 | */
|
---|
6705 |
|
---|
6706 | /* Raise GPs. */
|
---|
6707 | if (pVCpu->iem.s.uCpl != 0)
|
---|
6708 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6709 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
6710 |
|
---|
6711 | if (iTrReg < 6 || iTrReg > 7)
|
---|
6712 | {
|
---|
6713 | /** @todo Do Intel CPUs reject this or are the TRs aliased? */
|
---|
6714 | Log(("mov r%u,tr%u: invalid register -> #GP(0)\n", iGReg, iTrReg));
|
---|
6715 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6716 | }
|
---|
6717 |
|
---|
6718 | /*
|
---|
6719 | * Read the new value from the source register.
|
---|
6720 | */
|
---|
6721 | uint64_t uNewTrX;
|
---|
6722 | if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
|
---|
6723 | uNewTrX = iemGRegFetchU64(pVCpu, iGReg);
|
---|
6724 | else
|
---|
6725 | uNewTrX = iemGRegFetchU32(pVCpu, iGReg);
|
---|
6726 |
|
---|
6727 | /*
|
---|
6728 | * Here we would do the actual setting if this weren't a dummy implementation.
|
---|
6729 | * This is currently a dummy implementation that only exists to prevent
|
---|
6730 | * old debuggers like WDEB386 or OS/2 KDB from crashing.
|
---|
6731 | */
|
---|
6732 | RT_NOREF(uNewTrX);
|
---|
6733 |
|
---|
6734 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
6735 | }
|
---|
6736 |
|
---|
6737 |
|
---|
6738 | /**
|
---|
6739 | * Implements 'INVLPG m'.
|
---|
6740 | *
|
---|
6741 | * @param GCPtrPage The effective address of the page to invalidate.
|
---|
6742 | * @remarks Updates the RIP.
|
---|
6743 | */
|
---|
6744 | IEM_CIMPL_DEF_1(iemCImpl_invlpg, RTGCPTR, GCPtrPage)
|
---|
6745 | {
|
---|
6746 | /* ring-0 only. */
|
---|
6747 | if (pVCpu->iem.s.uCpl != 0)
|
---|
6748 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6749 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
6750 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_CR3 | CPUMCTX_EXTRN_CR4 | CPUMCTX_EXTRN_EFER);
|
---|
6751 |
|
---|
6752 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
6753 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
6754 | && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_INVLPG_EXIT))
|
---|
6755 | {
|
---|
6756 | Log(("invlpg: Guest intercept (%RGp) -> VM-exit\n", GCPtrPage));
|
---|
6757 | return iemVmxVmexitInstrInvlpg(pVCpu, GCPtrPage, cbInstr);
|
---|
6758 | }
|
---|
6759 | #endif
|
---|
6760 |
|
---|
6761 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_INVLPG))
|
---|
6762 | {
|
---|
6763 | Log(("invlpg: Guest intercept (%RGp) -> #VMEXIT\n", GCPtrPage));
|
---|
6764 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
6765 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_INVLPG,
|
---|
6766 | IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fSvmDecodeAssists ? GCPtrPage : 0, 0 /* uExitInfo2 */);
|
---|
6767 | }
|
---|
6768 |
|
---|
6769 | int rc = PGMInvalidatePage(pVCpu, GCPtrPage);
|
---|
6770 | if (rc == VINF_SUCCESS)
|
---|
6771 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
6772 | if (rc == VINF_PGM_SYNC_CR3)
|
---|
6773 | {
|
---|
6774 | iemSetPassUpStatus(pVCpu, rc);
|
---|
6775 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
6776 | }
|
---|
6777 |
|
---|
6778 | AssertMsg(RT_FAILURE_NP(rc), ("%Rrc\n", rc));
|
---|
6779 | Log(("PGMInvalidatePage(%RGv) -> %Rrc\n", GCPtrPage, rc));
|
---|
6780 | return rc;
|
---|
6781 | }
|
---|
6782 |
|
---|
6783 |
|
---|
6784 | /**
|
---|
6785 | * Implements INVPCID.
|
---|
6786 | *
|
---|
6787 | * @param iEffSeg The segment of the invpcid descriptor.
|
---|
6788 | * @param GCPtrInvpcidDesc The address of invpcid descriptor.
|
---|
6789 | * @param uInvpcidType The invalidation type.
|
---|
6790 | * @remarks Updates the RIP.
|
---|
6791 | */
|
---|
6792 | IEM_CIMPL_DEF_3(iemCImpl_invpcid, uint8_t, iEffSeg, RTGCPTR, GCPtrInvpcidDesc, uint64_t, uInvpcidType)
|
---|
6793 | {
|
---|
6794 | /*
|
---|
6795 | * Check preconditions.
|
---|
6796 | */
|
---|
6797 | if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fInvpcid)
|
---|
6798 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
6799 |
|
---|
6800 | /* When in VMX non-root mode and INVPCID is not enabled, it results in #UD. */
|
---|
6801 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
6802 | && !IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_INVPCID))
|
---|
6803 | {
|
---|
6804 | Log(("invpcid: Not enabled for nested-guest execution -> #UD\n"));
|
---|
6805 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
6806 | }
|
---|
6807 |
|
---|
6808 | if (pVCpu->iem.s.uCpl != 0)
|
---|
6809 | {
|
---|
6810 | Log(("invpcid: CPL != 0 -> #GP(0)\n"));
|
---|
6811 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6812 | }
|
---|
6813 |
|
---|
6814 | if (IEM_IS_V86_MODE(pVCpu))
|
---|
6815 | {
|
---|
6816 | Log(("invpcid: v8086 mode -> #GP(0)\n"));
|
---|
6817 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6818 | }
|
---|
6819 |
|
---|
6820 | /*
|
---|
6821 | * Check nested-guest intercept.
|
---|
6822 | *
|
---|
6823 | * INVPCID causes a VM-exit if "enable INVPCID" and "INVLPG exiting" are
|
---|
6824 | * both set. We have already checked the former earlier in this function.
|
---|
6825 | *
|
---|
6826 | * CPL and virtual-8086 mode checks take priority over this VM-exit.
|
---|
6827 | * See Intel spec. "25.1.1 Relative Priority of Faults and VM Exits".
|
---|
6828 | */
|
---|
6829 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
6830 | && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_INVLPG_EXIT))
|
---|
6831 | {
|
---|
6832 | Log(("invpcid: Guest intercept -> #VM-exit\n"));
|
---|
6833 | IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_INVPCID, VMXINSTRID_NONE, cbInstr);
|
---|
6834 | }
|
---|
6835 |
|
---|
6836 | if (uInvpcidType > X86_INVPCID_TYPE_MAX_VALID)
|
---|
6837 | {
|
---|
6838 | Log(("invpcid: invalid/unrecognized invpcid type %#RX64 -> #GP(0)\n", uInvpcidType));
|
---|
6839 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6840 | }
|
---|
6841 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_CR3 | CPUMCTX_EXTRN_CR4 | CPUMCTX_EXTRN_EFER);
|
---|
6842 |
|
---|
6843 | /*
|
---|
6844 | * Fetch the invpcid descriptor from guest memory.
|
---|
6845 | */
|
---|
6846 | RTUINT128U uDesc;
|
---|
6847 | VBOXSTRICTRC rcStrict = iemMemFetchDataU128(pVCpu, &uDesc, iEffSeg, GCPtrInvpcidDesc);
|
---|
6848 | if (rcStrict == VINF_SUCCESS)
|
---|
6849 | {
|
---|
6850 | /*
|
---|
6851 | * Validate the descriptor.
|
---|
6852 | */
|
---|
6853 | if (uDesc.s.Lo > 0xfff)
|
---|
6854 | {
|
---|
6855 | Log(("invpcid: reserved bits set in invpcid descriptor %#RX64 -> #GP(0)\n", uDesc.s.Lo));
|
---|
6856 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6857 | }
|
---|
6858 |
|
---|
6859 | RTGCUINTPTR64 const GCPtrInvAddr = uDesc.s.Hi;
|
---|
6860 | uint8_t const uPcid = uDesc.s.Lo & UINT64_C(0xfff);
|
---|
6861 | uint32_t const uCr4 = pVCpu->cpum.GstCtx.cr4;
|
---|
6862 | uint64_t const uCr3 = pVCpu->cpum.GstCtx.cr3;
|
---|
6863 | switch (uInvpcidType)
|
---|
6864 | {
|
---|
6865 | case X86_INVPCID_TYPE_INDV_ADDR:
|
---|
6866 | {
|
---|
6867 | if (!IEM_IS_CANONICAL(GCPtrInvAddr))
|
---|
6868 | {
|
---|
6869 | Log(("invpcid: invalidation address %#RGP is not canonical -> #GP(0)\n", GCPtrInvAddr));
|
---|
6870 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6871 | }
|
---|
6872 | if ( !(uCr4 & X86_CR4_PCIDE)
|
---|
6873 | && uPcid != 0)
|
---|
6874 | {
|
---|
6875 | Log(("invpcid: invalid pcid %#x\n", uPcid));
|
---|
6876 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6877 | }
|
---|
6878 |
|
---|
6879 | /* Invalidate mappings for the linear address tagged with PCID except global translations. */
|
---|
6880 | PGMFlushTLB(pVCpu, uCr3, false /* fGlobal */);
|
---|
6881 | break;
|
---|
6882 | }
|
---|
6883 |
|
---|
6884 | case X86_INVPCID_TYPE_SINGLE_CONTEXT:
|
---|
6885 | {
|
---|
6886 | if ( !(uCr4 & X86_CR4_PCIDE)
|
---|
6887 | && uPcid != 0)
|
---|
6888 | {
|
---|
6889 | Log(("invpcid: invalid pcid %#x\n", uPcid));
|
---|
6890 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6891 | }
|
---|
6892 | /* Invalidate all mappings associated with PCID except global translations. */
|
---|
6893 | PGMFlushTLB(pVCpu, uCr3, false /* fGlobal */);
|
---|
6894 | break;
|
---|
6895 | }
|
---|
6896 |
|
---|
6897 | case X86_INVPCID_TYPE_ALL_CONTEXT_INCL_GLOBAL:
|
---|
6898 | {
|
---|
6899 | PGMFlushTLB(pVCpu, uCr3, true /* fGlobal */);
|
---|
6900 | break;
|
---|
6901 | }
|
---|
6902 |
|
---|
6903 | case X86_INVPCID_TYPE_ALL_CONTEXT_EXCL_GLOBAL:
|
---|
6904 | {
|
---|
6905 | PGMFlushTLB(pVCpu, uCr3, false /* fGlobal */);
|
---|
6906 | break;
|
---|
6907 | }
|
---|
6908 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
6909 | }
|
---|
6910 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
6911 | }
|
---|
6912 | return rcStrict;
|
---|
6913 | }
|
---|
6914 |
|
---|
6915 |
|
---|
6916 | /**
|
---|
6917 | * Implements INVD.
|
---|
6918 | */
|
---|
6919 | IEM_CIMPL_DEF_0(iemCImpl_invd)
|
---|
6920 | {
|
---|
6921 | if (pVCpu->iem.s.uCpl != 0)
|
---|
6922 | {
|
---|
6923 | Log(("invd: CPL != 0 -> #GP(0)\n"));
|
---|
6924 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6925 | }
|
---|
6926 |
|
---|
6927 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
6928 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_INVD, cbInstr);
|
---|
6929 |
|
---|
6930 | IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_INVD, SVM_EXIT_INVD, 0, 0);
|
---|
6931 |
|
---|
6932 | /* We currently take no action here. */
|
---|
6933 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
6934 | }
|
---|
6935 |
|
---|
6936 |
|
---|
6937 | /**
|
---|
6938 | * Implements WBINVD.
|
---|
6939 | */
|
---|
6940 | IEM_CIMPL_DEF_0(iemCImpl_wbinvd)
|
---|
6941 | {
|
---|
6942 | if (pVCpu->iem.s.uCpl != 0)
|
---|
6943 | {
|
---|
6944 | Log(("wbinvd: CPL != 0 -> #GP(0)\n"));
|
---|
6945 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6946 | }
|
---|
6947 |
|
---|
6948 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
6949 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_WBINVD, cbInstr);
|
---|
6950 |
|
---|
6951 | IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_WBINVD, SVM_EXIT_WBINVD, 0, 0);
|
---|
6952 |
|
---|
6953 | /* We currently take no action here. */
|
---|
6954 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
6955 | }
|
---|
6956 |
|
---|
6957 |
|
---|
6958 | /** Opcode 0x0f 0xaa. */
|
---|
6959 | IEM_CIMPL_DEF_0(iemCImpl_rsm)
|
---|
6960 | {
|
---|
6961 | IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_RSM, SVM_EXIT_RSM, 0, 0);
|
---|
6962 | NOREF(cbInstr);
|
---|
6963 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
6964 | }
|
---|
6965 |
|
---|
6966 |
|
---|
6967 | /**
|
---|
6968 | * Implements RDTSC.
|
---|
6969 | */
|
---|
6970 | IEM_CIMPL_DEF_0(iemCImpl_rdtsc)
|
---|
6971 | {
|
---|
6972 | /*
|
---|
6973 | * Check preconditions.
|
---|
6974 | */
|
---|
6975 | if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fTsc)
|
---|
6976 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
6977 |
|
---|
6978 | if (pVCpu->iem.s.uCpl != 0)
|
---|
6979 | {
|
---|
6980 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR4);
|
---|
6981 | if (pVCpu->cpum.GstCtx.cr4 & X86_CR4_TSD)
|
---|
6982 | {
|
---|
6983 | Log(("rdtsc: CR4.TSD and CPL=%u -> #GP(0)\n", pVCpu->iem.s.uCpl));
|
---|
6984 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6985 | }
|
---|
6986 | }
|
---|
6987 |
|
---|
6988 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
6989 | && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_RDTSC_EXIT))
|
---|
6990 | {
|
---|
6991 | Log(("rdtsc: Guest intercept -> VM-exit\n"));
|
---|
6992 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_RDTSC, cbInstr);
|
---|
6993 | }
|
---|
6994 |
|
---|
6995 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_RDTSC))
|
---|
6996 | {
|
---|
6997 | Log(("rdtsc: Guest intercept -> #VMEXIT\n"));
|
---|
6998 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
6999 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_RDTSC, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
7000 | }
|
---|
7001 |
|
---|
7002 | /*
|
---|
7003 | * Do the job.
|
---|
7004 | */
|
---|
7005 | uint64_t uTicks = TMCpuTickGet(pVCpu);
|
---|
7006 | #if defined(VBOX_WITH_NESTED_HWVIRT_SVM) || defined(VBOX_WITH_NESTED_HWVIRT_VMX)
|
---|
7007 | uTicks = CPUMApplyNestedGuestTscOffset(pVCpu, uTicks);
|
---|
7008 | #endif
|
---|
7009 | pVCpu->cpum.GstCtx.rax = RT_LO_U32(uTicks);
|
---|
7010 | pVCpu->cpum.GstCtx.rdx = RT_HI_U32(uTicks);
|
---|
7011 | pVCpu->cpum.GstCtx.fExtrn &= ~(CPUMCTX_EXTRN_RAX | CPUMCTX_EXTRN_RDX); /* For IEMExecDecodedRdtsc. */
|
---|
7012 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7013 | }
|
---|
7014 |
|
---|
7015 |
|
---|
7016 | /**
|
---|
7017 | * Implements RDTSC.
|
---|
7018 | */
|
---|
7019 | IEM_CIMPL_DEF_0(iemCImpl_rdtscp)
|
---|
7020 | {
|
---|
7021 | /*
|
---|
7022 | * Check preconditions.
|
---|
7023 | */
|
---|
7024 | if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fRdTscP)
|
---|
7025 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
7026 |
|
---|
7027 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
7028 | && !IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_RDTSCP))
|
---|
7029 | {
|
---|
7030 | Log(("rdtscp: Not enabled for VMX non-root mode -> #UD\n"));
|
---|
7031 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
7032 | }
|
---|
7033 |
|
---|
7034 | if (pVCpu->iem.s.uCpl != 0)
|
---|
7035 | {
|
---|
7036 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR4);
|
---|
7037 | if (pVCpu->cpum.GstCtx.cr4 & X86_CR4_TSD)
|
---|
7038 | {
|
---|
7039 | Log(("rdtscp: CR4.TSD and CPL=%u -> #GP(0)\n", pVCpu->iem.s.uCpl));
|
---|
7040 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7041 | }
|
---|
7042 | }
|
---|
7043 |
|
---|
7044 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
7045 | && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_RDTSC_EXIT))
|
---|
7046 | {
|
---|
7047 | Log(("rdtscp: Guest intercept -> VM-exit\n"));
|
---|
7048 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_RDTSCP, cbInstr);
|
---|
7049 | }
|
---|
7050 | else if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_RDTSCP))
|
---|
7051 | {
|
---|
7052 | Log(("rdtscp: Guest intercept -> #VMEXIT\n"));
|
---|
7053 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
7054 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_RDTSCP, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
7055 | }
|
---|
7056 |
|
---|
7057 | /*
|
---|
7058 | * Do the job.
|
---|
7059 | * Query the MSR first in case of trips to ring-3.
|
---|
7060 | */
|
---|
7061 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_TSC_AUX);
|
---|
7062 | VBOXSTRICTRC rcStrict = CPUMQueryGuestMsr(pVCpu, MSR_K8_TSC_AUX, &pVCpu->cpum.GstCtx.rcx);
|
---|
7063 | if (rcStrict == VINF_SUCCESS)
|
---|
7064 | {
|
---|
7065 | /* Low dword of the TSC_AUX msr only. */
|
---|
7066 | pVCpu->cpum.GstCtx.rcx &= UINT32_C(0xffffffff);
|
---|
7067 |
|
---|
7068 | uint64_t uTicks = TMCpuTickGet(pVCpu);
|
---|
7069 | #if defined(VBOX_WITH_NESTED_HWVIRT_SVM) || defined(VBOX_WITH_NESTED_HWVIRT_VMX)
|
---|
7070 | uTicks = CPUMApplyNestedGuestTscOffset(pVCpu, uTicks);
|
---|
7071 | #endif
|
---|
7072 | pVCpu->cpum.GstCtx.rax = RT_LO_U32(uTicks);
|
---|
7073 | pVCpu->cpum.GstCtx.rdx = RT_HI_U32(uTicks);
|
---|
7074 | pVCpu->cpum.GstCtx.fExtrn &= ~(CPUMCTX_EXTRN_RAX | CPUMCTX_EXTRN_RDX | CPUMCTX_EXTRN_RCX); /* For IEMExecDecodedRdtscp. */
|
---|
7075 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7076 | }
|
---|
7077 | return rcStrict;
|
---|
7078 | }
|
---|
7079 |
|
---|
7080 |
|
---|
7081 | /**
|
---|
7082 | * Implements RDPMC.
|
---|
7083 | */
|
---|
7084 | IEM_CIMPL_DEF_0(iemCImpl_rdpmc)
|
---|
7085 | {
|
---|
7086 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR4);
|
---|
7087 |
|
---|
7088 | if ( pVCpu->iem.s.uCpl != 0
|
---|
7089 | && !(pVCpu->cpum.GstCtx.cr4 & X86_CR4_PCE))
|
---|
7090 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7091 |
|
---|
7092 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
7093 | && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_RDPMC_EXIT))
|
---|
7094 | {
|
---|
7095 | Log(("rdpmc: Guest intercept -> VM-exit\n"));
|
---|
7096 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_RDPMC, cbInstr);
|
---|
7097 | }
|
---|
7098 |
|
---|
7099 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_RDPMC))
|
---|
7100 | {
|
---|
7101 | Log(("rdpmc: Guest intercept -> #VMEXIT\n"));
|
---|
7102 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
7103 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_RDPMC, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
7104 | }
|
---|
7105 |
|
---|
7106 | /** @todo Emulate performance counters, for now just return 0. */
|
---|
7107 | pVCpu->cpum.GstCtx.rax = 0;
|
---|
7108 | pVCpu->cpum.GstCtx.rdx = 0;
|
---|
7109 | pVCpu->cpum.GstCtx.fExtrn &= ~(CPUMCTX_EXTRN_RAX | CPUMCTX_EXTRN_RDX);
|
---|
7110 | /** @todo We should trigger a \#GP here if the CPU doesn't support the index in
|
---|
7111 | * ecx but see @bugref{3472}! */
|
---|
7112 |
|
---|
7113 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7114 | }
|
---|
7115 |
|
---|
7116 |
|
---|
7117 | /**
|
---|
7118 | * Implements RDMSR.
|
---|
7119 | */
|
---|
7120 | IEM_CIMPL_DEF_0(iemCImpl_rdmsr)
|
---|
7121 | {
|
---|
7122 | /*
|
---|
7123 | * Check preconditions.
|
---|
7124 | */
|
---|
7125 | if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fMsr)
|
---|
7126 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
7127 | if (pVCpu->iem.s.uCpl != 0)
|
---|
7128 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7129 |
|
---|
7130 | /*
|
---|
7131 | * Check nested-guest intercepts.
|
---|
7132 | */
|
---|
7133 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
7134 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
7135 | {
|
---|
7136 | if (iemVmxIsRdmsrWrmsrInterceptSet(pVCpu, VMX_EXIT_RDMSR, pVCpu->cpum.GstCtx.ecx))
|
---|
7137 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_RDMSR, cbInstr);
|
---|
7138 | }
|
---|
7139 | #endif
|
---|
7140 |
|
---|
7141 | #ifdef VBOX_WITH_NESTED_HWVIRT_SVM
|
---|
7142 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_MSR_PROT))
|
---|
7143 | {
|
---|
7144 | VBOXSTRICTRC rcStrict = iemSvmHandleMsrIntercept(pVCpu, pVCpu->cpum.GstCtx.ecx, false /* fWrite */);
|
---|
7145 | if (rcStrict == VINF_SVM_VMEXIT)
|
---|
7146 | return VINF_SUCCESS;
|
---|
7147 | if (rcStrict != VINF_SVM_INTERCEPT_NOT_ACTIVE)
|
---|
7148 | {
|
---|
7149 | Log(("IEM: SVM intercepted rdmsr(%#x) failed. rc=%Rrc\n", pVCpu->cpum.GstCtx.ecx, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
7150 | return rcStrict;
|
---|
7151 | }
|
---|
7152 | }
|
---|
7153 | #endif
|
---|
7154 |
|
---|
7155 | /*
|
---|
7156 | * Do the job.
|
---|
7157 | */
|
---|
7158 | RTUINT64U uValue;
|
---|
7159 | /** @todo make CPUMAllMsrs.cpp import the necessary MSR state. */
|
---|
7160 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_ALL_MSRS);
|
---|
7161 |
|
---|
7162 | VBOXSTRICTRC rcStrict = CPUMQueryGuestMsr(pVCpu, pVCpu->cpum.GstCtx.ecx, &uValue.u);
|
---|
7163 | if (rcStrict == VINF_SUCCESS)
|
---|
7164 | {
|
---|
7165 | pVCpu->cpum.GstCtx.rax = uValue.s.Lo;
|
---|
7166 | pVCpu->cpum.GstCtx.rdx = uValue.s.Hi;
|
---|
7167 | pVCpu->cpum.GstCtx.fExtrn &= ~(CPUMCTX_EXTRN_RAX | CPUMCTX_EXTRN_RDX);
|
---|
7168 |
|
---|
7169 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7170 | }
|
---|
7171 |
|
---|
7172 | #ifndef IN_RING3
|
---|
7173 | /* Deferred to ring-3. */
|
---|
7174 | if (rcStrict == VINF_CPUM_R3_MSR_READ)
|
---|
7175 | {
|
---|
7176 | Log(("IEM: rdmsr(%#x) -> ring-3\n", pVCpu->cpum.GstCtx.ecx));
|
---|
7177 | return rcStrict;
|
---|
7178 | }
|
---|
7179 | #endif
|
---|
7180 |
|
---|
7181 | /* Often a unimplemented MSR or MSR bit, so worth logging. */
|
---|
7182 | if (pVCpu->iem.s.cLogRelRdMsr < 32)
|
---|
7183 | {
|
---|
7184 | pVCpu->iem.s.cLogRelRdMsr++;
|
---|
7185 | LogRel(("IEM: rdmsr(%#x) -> #GP(0)\n", pVCpu->cpum.GstCtx.ecx));
|
---|
7186 | }
|
---|
7187 | else
|
---|
7188 | Log(( "IEM: rdmsr(%#x) -> #GP(0)\n", pVCpu->cpum.GstCtx.ecx));
|
---|
7189 | AssertMsgReturn(rcStrict == VERR_CPUM_RAISE_GP_0, ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)), VERR_IPE_UNEXPECTED_STATUS);
|
---|
7190 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7191 | }
|
---|
7192 |
|
---|
7193 |
|
---|
7194 | /**
|
---|
7195 | * Implements WRMSR.
|
---|
7196 | */
|
---|
7197 | IEM_CIMPL_DEF_0(iemCImpl_wrmsr)
|
---|
7198 | {
|
---|
7199 | /*
|
---|
7200 | * Check preconditions.
|
---|
7201 | */
|
---|
7202 | if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fMsr)
|
---|
7203 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
7204 | if (pVCpu->iem.s.uCpl != 0)
|
---|
7205 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7206 |
|
---|
7207 | RTUINT64U uValue;
|
---|
7208 | uValue.s.Lo = pVCpu->cpum.GstCtx.eax;
|
---|
7209 | uValue.s.Hi = pVCpu->cpum.GstCtx.edx;
|
---|
7210 |
|
---|
7211 | uint32_t const idMsr = pVCpu->cpum.GstCtx.ecx;
|
---|
7212 |
|
---|
7213 | /** @todo make CPUMAllMsrs.cpp import the necessary MSR state. */
|
---|
7214 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_ALL_MSRS);
|
---|
7215 |
|
---|
7216 | /*
|
---|
7217 | * Check nested-guest intercepts.
|
---|
7218 | */
|
---|
7219 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
7220 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
7221 | {
|
---|
7222 | if (iemVmxIsRdmsrWrmsrInterceptSet(pVCpu, VMX_EXIT_WRMSR, idMsr))
|
---|
7223 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_WRMSR, cbInstr);
|
---|
7224 | }
|
---|
7225 | #endif
|
---|
7226 |
|
---|
7227 | #ifdef VBOX_WITH_NESTED_HWVIRT_SVM
|
---|
7228 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_MSR_PROT))
|
---|
7229 | {
|
---|
7230 | VBOXSTRICTRC rcStrict = iemSvmHandleMsrIntercept(pVCpu, idMsr, true /* fWrite */);
|
---|
7231 | if (rcStrict == VINF_SVM_VMEXIT)
|
---|
7232 | return VINF_SUCCESS;
|
---|
7233 | if (rcStrict != VINF_SVM_INTERCEPT_NOT_ACTIVE)
|
---|
7234 | {
|
---|
7235 | Log(("IEM: SVM intercepted rdmsr(%#x) failed. rc=%Rrc\n", idMsr, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
7236 | return rcStrict;
|
---|
7237 | }
|
---|
7238 | }
|
---|
7239 | #endif
|
---|
7240 |
|
---|
7241 | /*
|
---|
7242 | * Do the job.
|
---|
7243 | */
|
---|
7244 | VBOXSTRICTRC rcStrict = CPUMSetGuestMsr(pVCpu, idMsr, uValue.u);
|
---|
7245 | if (rcStrict == VINF_SUCCESS)
|
---|
7246 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7247 |
|
---|
7248 | #ifndef IN_RING3
|
---|
7249 | /* Deferred to ring-3. */
|
---|
7250 | if (rcStrict == VINF_CPUM_R3_MSR_WRITE)
|
---|
7251 | {
|
---|
7252 | Log(("IEM: wrmsr(%#x) -> ring-3\n", idMsr));
|
---|
7253 | return rcStrict;
|
---|
7254 | }
|
---|
7255 | #endif
|
---|
7256 |
|
---|
7257 | /* Often a unimplemented MSR or MSR bit, so worth logging. */
|
---|
7258 | if (pVCpu->iem.s.cLogRelWrMsr < 32)
|
---|
7259 | {
|
---|
7260 | pVCpu->iem.s.cLogRelWrMsr++;
|
---|
7261 | LogRel(("IEM: wrmsr(%#x,%#x`%08x) -> #GP(0)\n", idMsr, uValue.s.Hi, uValue.s.Lo));
|
---|
7262 | }
|
---|
7263 | else
|
---|
7264 | Log(( "IEM: wrmsr(%#x,%#x`%08x) -> #GP(0)\n", idMsr, uValue.s.Hi, uValue.s.Lo));
|
---|
7265 | AssertMsgReturn(rcStrict == VERR_CPUM_RAISE_GP_0, ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)), VERR_IPE_UNEXPECTED_STATUS);
|
---|
7266 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7267 | }
|
---|
7268 |
|
---|
7269 |
|
---|
7270 | /**
|
---|
7271 | * Implements 'IN eAX, port'.
|
---|
7272 | *
|
---|
7273 | * @param u16Port The source port.
|
---|
7274 | * @param fImm Whether the port was specified through an immediate operand
|
---|
7275 | * or the implicit DX register.
|
---|
7276 | * @param cbReg The register size.
|
---|
7277 | */
|
---|
7278 | IEM_CIMPL_DEF_3(iemCImpl_in, uint16_t, u16Port, bool, fImm, uint8_t, cbReg)
|
---|
7279 | {
|
---|
7280 | /*
|
---|
7281 | * CPL check
|
---|
7282 | */
|
---|
7283 | VBOXSTRICTRC rcStrict = iemHlpCheckPortIOPermission(pVCpu, u16Port, cbReg);
|
---|
7284 | if (rcStrict != VINF_SUCCESS)
|
---|
7285 | return rcStrict;
|
---|
7286 |
|
---|
7287 | /*
|
---|
7288 | * Check VMX nested-guest IO intercept.
|
---|
7289 | */
|
---|
7290 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
7291 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
7292 | {
|
---|
7293 | rcStrict = iemVmxVmexitInstrIo(pVCpu, VMXINSTRID_IO_IN, u16Port, fImm, cbReg, cbInstr);
|
---|
7294 | if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
|
---|
7295 | return rcStrict;
|
---|
7296 | }
|
---|
7297 | #else
|
---|
7298 | RT_NOREF(fImm);
|
---|
7299 | #endif
|
---|
7300 |
|
---|
7301 | /*
|
---|
7302 | * Check SVM nested-guest IO intercept.
|
---|
7303 | */
|
---|
7304 | #ifdef VBOX_WITH_NESTED_HWVIRT_SVM
|
---|
7305 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_IOIO_PROT))
|
---|
7306 | {
|
---|
7307 | uint8_t cAddrSizeBits;
|
---|
7308 | switch (pVCpu->iem.s.enmEffAddrMode)
|
---|
7309 | {
|
---|
7310 | case IEMMODE_16BIT: cAddrSizeBits = 16; break;
|
---|
7311 | case IEMMODE_32BIT: cAddrSizeBits = 32; break;
|
---|
7312 | case IEMMODE_64BIT: cAddrSizeBits = 64; break;
|
---|
7313 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
7314 | }
|
---|
7315 | rcStrict = iemSvmHandleIOIntercept(pVCpu, u16Port, SVMIOIOTYPE_IN, cbReg, cAddrSizeBits, 0 /* N/A - iEffSeg */,
|
---|
7316 | false /* fRep */, false /* fStrIo */, cbInstr);
|
---|
7317 | if (rcStrict == VINF_SVM_VMEXIT)
|
---|
7318 | return VINF_SUCCESS;
|
---|
7319 | if (rcStrict != VINF_SVM_INTERCEPT_NOT_ACTIVE)
|
---|
7320 | {
|
---|
7321 | Log(("iemCImpl_in: iemSvmHandleIOIntercept failed (u16Port=%#x, cbReg=%u) rc=%Rrc\n", u16Port, cbReg,
|
---|
7322 | VBOXSTRICTRC_VAL(rcStrict)));
|
---|
7323 | return rcStrict;
|
---|
7324 | }
|
---|
7325 | }
|
---|
7326 | #endif
|
---|
7327 |
|
---|
7328 | /*
|
---|
7329 | * Perform the I/O.
|
---|
7330 | */
|
---|
7331 | PVMCC const pVM = pVCpu->CTX_SUFF(pVM);
|
---|
7332 | uint32_t u32Value = 0;
|
---|
7333 | rcStrict = IOMIOPortRead(pVM, pVCpu, u16Port, &u32Value, cbReg);
|
---|
7334 | if (IOM_SUCCESS(rcStrict))
|
---|
7335 | {
|
---|
7336 | switch (cbReg)
|
---|
7337 | {
|
---|
7338 | case 1: pVCpu->cpum.GstCtx.al = (uint8_t)u32Value; break;
|
---|
7339 | case 2: pVCpu->cpum.GstCtx.ax = (uint16_t)u32Value; break;
|
---|
7340 | case 4: pVCpu->cpum.GstCtx.rax = u32Value; break;
|
---|
7341 | default: AssertFailedReturn(VERR_IEM_IPE_3);
|
---|
7342 | }
|
---|
7343 |
|
---|
7344 | pVCpu->iem.s.cPotentialExits++;
|
---|
7345 | if (rcStrict != VINF_SUCCESS)
|
---|
7346 | iemSetPassUpStatus(pVCpu, rcStrict);
|
---|
7347 |
|
---|
7348 | /*
|
---|
7349 | * Check for I/O breakpoints before we complete the instruction.
|
---|
7350 | */
|
---|
7351 | uint32_t const fDr7 = pVCpu->cpum.GstCtx.dr[7];
|
---|
7352 | if (RT_UNLIKELY( ( ( (fDr7 & X86_DR7_ENABLED_MASK)
|
---|
7353 | && X86_DR7_ANY_RW_IO(fDr7)
|
---|
7354 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_DE))
|
---|
7355 | || pVM->dbgf.ro.cEnabledHwIoBreakpoints > 0)
|
---|
7356 | && rcStrict == VINF_SUCCESS))
|
---|
7357 | {
|
---|
7358 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3 | CPUMCTX_EXTRN_DR6);
|
---|
7359 | pVCpu->cpum.GstCtx.eflags.uBoth |= DBGFBpCheckIo2(pVM, pVCpu, u16Port, cbReg);
|
---|
7360 | }
|
---|
7361 |
|
---|
7362 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7363 | }
|
---|
7364 |
|
---|
7365 | return rcStrict;
|
---|
7366 | }
|
---|
7367 |
|
---|
7368 |
|
---|
7369 | /**
|
---|
7370 | * Implements 'IN eAX, DX'.
|
---|
7371 | *
|
---|
7372 | * @param cbReg The register size.
|
---|
7373 | */
|
---|
7374 | IEM_CIMPL_DEF_1(iemCImpl_in_eAX_DX, uint8_t, cbReg)
|
---|
7375 | {
|
---|
7376 | return IEM_CIMPL_CALL_3(iemCImpl_in, pVCpu->cpum.GstCtx.dx, false /* fImm */, cbReg);
|
---|
7377 | }
|
---|
7378 |
|
---|
7379 |
|
---|
7380 | /**
|
---|
7381 | * Implements 'OUT port, eAX'.
|
---|
7382 | *
|
---|
7383 | * @param u16Port The destination port.
|
---|
7384 | * @param fImm Whether the port was specified through an immediate operand
|
---|
7385 | * or the implicit DX register.
|
---|
7386 | * @param cbReg The register size.
|
---|
7387 | */
|
---|
7388 | IEM_CIMPL_DEF_3(iemCImpl_out, uint16_t, u16Port, bool, fImm, uint8_t, cbReg)
|
---|
7389 | {
|
---|
7390 | /*
|
---|
7391 | * CPL check
|
---|
7392 | */
|
---|
7393 | VBOXSTRICTRC rcStrict = iemHlpCheckPortIOPermission(pVCpu, u16Port, cbReg);
|
---|
7394 | if (rcStrict != VINF_SUCCESS)
|
---|
7395 | return rcStrict;
|
---|
7396 |
|
---|
7397 | /*
|
---|
7398 | * Check VMX nested-guest I/O intercept.
|
---|
7399 | */
|
---|
7400 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
7401 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
7402 | {
|
---|
7403 | rcStrict = iemVmxVmexitInstrIo(pVCpu, VMXINSTRID_IO_OUT, u16Port, fImm, cbReg, cbInstr);
|
---|
7404 | if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
|
---|
7405 | return rcStrict;
|
---|
7406 | }
|
---|
7407 | #else
|
---|
7408 | RT_NOREF(fImm);
|
---|
7409 | #endif
|
---|
7410 |
|
---|
7411 | /*
|
---|
7412 | * Check SVM nested-guest I/O intercept.
|
---|
7413 | */
|
---|
7414 | #ifdef VBOX_WITH_NESTED_HWVIRT_SVM
|
---|
7415 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_IOIO_PROT))
|
---|
7416 | {
|
---|
7417 | uint8_t cAddrSizeBits;
|
---|
7418 | switch (pVCpu->iem.s.enmEffAddrMode)
|
---|
7419 | {
|
---|
7420 | case IEMMODE_16BIT: cAddrSizeBits = 16; break;
|
---|
7421 | case IEMMODE_32BIT: cAddrSizeBits = 32; break;
|
---|
7422 | case IEMMODE_64BIT: cAddrSizeBits = 64; break;
|
---|
7423 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
7424 | }
|
---|
7425 | rcStrict = iemSvmHandleIOIntercept(pVCpu, u16Port, SVMIOIOTYPE_OUT, cbReg, cAddrSizeBits, 0 /* N/A - iEffSeg */,
|
---|
7426 | false /* fRep */, false /* fStrIo */, cbInstr);
|
---|
7427 | if (rcStrict == VINF_SVM_VMEXIT)
|
---|
7428 | return VINF_SUCCESS;
|
---|
7429 | if (rcStrict != VINF_SVM_INTERCEPT_NOT_ACTIVE)
|
---|
7430 | {
|
---|
7431 | Log(("iemCImpl_out: iemSvmHandleIOIntercept failed (u16Port=%#x, cbReg=%u) rc=%Rrc\n", u16Port, cbReg,
|
---|
7432 | VBOXSTRICTRC_VAL(rcStrict)));
|
---|
7433 | return rcStrict;
|
---|
7434 | }
|
---|
7435 | }
|
---|
7436 | #endif
|
---|
7437 |
|
---|
7438 | /*
|
---|
7439 | * Perform the I/O.
|
---|
7440 | */
|
---|
7441 | PVMCC const pVM = pVCpu->CTX_SUFF(pVM);
|
---|
7442 | uint32_t u32Value;
|
---|
7443 | switch (cbReg)
|
---|
7444 | {
|
---|
7445 | case 1: u32Value = pVCpu->cpum.GstCtx.al; break;
|
---|
7446 | case 2: u32Value = pVCpu->cpum.GstCtx.ax; break;
|
---|
7447 | case 4: u32Value = pVCpu->cpum.GstCtx.eax; break;
|
---|
7448 | default: AssertFailedReturn(VERR_IEM_IPE_4);
|
---|
7449 | }
|
---|
7450 | rcStrict = IOMIOPortWrite(pVM, pVCpu, u16Port, u32Value, cbReg);
|
---|
7451 | if (IOM_SUCCESS(rcStrict))
|
---|
7452 | {
|
---|
7453 | pVCpu->iem.s.cPotentialExits++;
|
---|
7454 | if (rcStrict != VINF_SUCCESS)
|
---|
7455 | iemSetPassUpStatus(pVCpu, rcStrict);
|
---|
7456 |
|
---|
7457 | /*
|
---|
7458 | * Check for I/O breakpoints before we complete the instruction.
|
---|
7459 | */
|
---|
7460 | uint32_t const fDr7 = pVCpu->cpum.GstCtx.dr[7];
|
---|
7461 | if (RT_UNLIKELY( ( ( (fDr7 & X86_DR7_ENABLED_MASK)
|
---|
7462 | && X86_DR7_ANY_RW_IO(fDr7)
|
---|
7463 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_DE))
|
---|
7464 | || pVM->dbgf.ro.cEnabledHwIoBreakpoints > 0)
|
---|
7465 | && rcStrict == VINF_SUCCESS))
|
---|
7466 | {
|
---|
7467 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3 | CPUMCTX_EXTRN_DR6);
|
---|
7468 | pVCpu->cpum.GstCtx.eflags.uBoth |= DBGFBpCheckIo2(pVM, pVCpu, u16Port, cbReg);
|
---|
7469 | }
|
---|
7470 |
|
---|
7471 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7472 | }
|
---|
7473 | return rcStrict;
|
---|
7474 | }
|
---|
7475 |
|
---|
7476 |
|
---|
7477 | /**
|
---|
7478 | * Implements 'OUT DX, eAX'.
|
---|
7479 | *
|
---|
7480 | * @param cbReg The register size.
|
---|
7481 | */
|
---|
7482 | IEM_CIMPL_DEF_1(iemCImpl_out_DX_eAX, uint8_t, cbReg)
|
---|
7483 | {
|
---|
7484 | return IEM_CIMPL_CALL_3(iemCImpl_out, pVCpu->cpum.GstCtx.dx, false /* fImm */, cbReg);
|
---|
7485 | }
|
---|
7486 |
|
---|
7487 |
|
---|
7488 | /**
|
---|
7489 | * Implements 'CLI'.
|
---|
7490 | */
|
---|
7491 | IEM_CIMPL_DEF_0(iemCImpl_cli)
|
---|
7492 | {
|
---|
7493 | uint32_t fEfl = IEMMISC_GET_EFL(pVCpu);
|
---|
7494 | #ifdef LOG_ENABLED
|
---|
7495 | uint32_t const fEflOld = fEfl;
|
---|
7496 | #endif
|
---|
7497 |
|
---|
7498 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_CR4);
|
---|
7499 | if (pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE)
|
---|
7500 | {
|
---|
7501 | uint8_t const uIopl = X86_EFL_GET_IOPL(fEfl);
|
---|
7502 | if (!(fEfl & X86_EFL_VM))
|
---|
7503 | {
|
---|
7504 | if (pVCpu->iem.s.uCpl <= uIopl)
|
---|
7505 | fEfl &= ~X86_EFL_IF;
|
---|
7506 | else if ( pVCpu->iem.s.uCpl == 3
|
---|
7507 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_PVI) )
|
---|
7508 | fEfl &= ~X86_EFL_VIF;
|
---|
7509 | else
|
---|
7510 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7511 | }
|
---|
7512 | /* V8086 */
|
---|
7513 | else if (uIopl == 3)
|
---|
7514 | fEfl &= ~X86_EFL_IF;
|
---|
7515 | else if ( uIopl < 3
|
---|
7516 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_VME) )
|
---|
7517 | fEfl &= ~X86_EFL_VIF;
|
---|
7518 | else
|
---|
7519 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7520 | }
|
---|
7521 | /* real mode */
|
---|
7522 | else
|
---|
7523 | fEfl &= ~X86_EFL_IF;
|
---|
7524 |
|
---|
7525 | /* Commit. */
|
---|
7526 | IEMMISC_SET_EFL(pVCpu, fEfl);
|
---|
7527 | VBOXSTRICTRC const rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7528 | Log2(("CLI: %#x -> %#x\n", fEflOld, fEfl));
|
---|
7529 | return rcStrict;
|
---|
7530 | }
|
---|
7531 |
|
---|
7532 |
|
---|
7533 | /**
|
---|
7534 | * Implements 'STI'.
|
---|
7535 | */
|
---|
7536 | IEM_CIMPL_DEF_0(iemCImpl_sti)
|
---|
7537 | {
|
---|
7538 | uint32_t fEfl = IEMMISC_GET_EFL(pVCpu);
|
---|
7539 | uint32_t const fEflOld = fEfl;
|
---|
7540 |
|
---|
7541 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_CR4);
|
---|
7542 | if (pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE)
|
---|
7543 | {
|
---|
7544 | uint8_t const uIopl = X86_EFL_GET_IOPL(fEfl);
|
---|
7545 | if (!(fEfl & X86_EFL_VM))
|
---|
7546 | {
|
---|
7547 | if (pVCpu->iem.s.uCpl <= uIopl)
|
---|
7548 | fEfl |= X86_EFL_IF;
|
---|
7549 | else if ( pVCpu->iem.s.uCpl == 3
|
---|
7550 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_PVI)
|
---|
7551 | && !(fEfl & X86_EFL_VIP) )
|
---|
7552 | fEfl |= X86_EFL_VIF;
|
---|
7553 | else
|
---|
7554 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7555 | }
|
---|
7556 | /* V8086 */
|
---|
7557 | else if (uIopl == 3)
|
---|
7558 | fEfl |= X86_EFL_IF;
|
---|
7559 | else if ( uIopl < 3
|
---|
7560 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_VME)
|
---|
7561 | && !(fEfl & X86_EFL_VIP) )
|
---|
7562 | fEfl |= X86_EFL_VIF;
|
---|
7563 | else
|
---|
7564 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7565 | }
|
---|
7566 | /* real mode */
|
---|
7567 | else
|
---|
7568 | fEfl |= X86_EFL_IF;
|
---|
7569 |
|
---|
7570 | /*
|
---|
7571 | * Commit.
|
---|
7572 | *
|
---|
7573 | * Note! Setting the shadow interrupt flag must be done after RIP updating.
|
---|
7574 | */
|
---|
7575 | IEMMISC_SET_EFL(pVCpu, fEfl);
|
---|
7576 | VBOXSTRICTRC const rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7577 | if (!(fEflOld & X86_EFL_IF) && (fEfl & X86_EFL_IF))
|
---|
7578 | {
|
---|
7579 | /** @todo only set it the shadow flag if it was clear before? */
|
---|
7580 | CPUMSetInInterruptShadowSti(&pVCpu->cpum.GstCtx);
|
---|
7581 | }
|
---|
7582 | Log2(("STI: %#x -> %#x\n", fEflOld, fEfl));
|
---|
7583 | return rcStrict;
|
---|
7584 | }
|
---|
7585 |
|
---|
7586 |
|
---|
7587 | /**
|
---|
7588 | * Implements 'HLT'.
|
---|
7589 | */
|
---|
7590 | IEM_CIMPL_DEF_0(iemCImpl_hlt)
|
---|
7591 | {
|
---|
7592 | if (pVCpu->iem.s.uCpl != 0)
|
---|
7593 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7594 |
|
---|
7595 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
7596 | && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_HLT_EXIT))
|
---|
7597 | {
|
---|
7598 | Log2(("hlt: Guest intercept -> VM-exit\n"));
|
---|
7599 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_HLT, cbInstr);
|
---|
7600 | }
|
---|
7601 |
|
---|
7602 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_HLT))
|
---|
7603 | {
|
---|
7604 | Log2(("hlt: Guest intercept -> #VMEXIT\n"));
|
---|
7605 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
7606 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_HLT, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
7607 | }
|
---|
7608 |
|
---|
7609 | /** @todo finish: This ASSUMES that iemRegAddToRipAndFinishingClearingRF won't
|
---|
7610 | * be returning any status codes relating to non-guest events being raised, as
|
---|
7611 | * we'll mess up the guest HALT otherwise. */
|
---|
7612 | VBOXSTRICTRC rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7613 | if (rcStrict == VINF_SUCCESS)
|
---|
7614 | rcStrict = VINF_EM_HALT;
|
---|
7615 | return rcStrict;
|
---|
7616 | }
|
---|
7617 |
|
---|
7618 |
|
---|
7619 | /**
|
---|
7620 | * Implements 'MONITOR'.
|
---|
7621 | */
|
---|
7622 | IEM_CIMPL_DEF_1(iemCImpl_monitor, uint8_t, iEffSeg)
|
---|
7623 | {
|
---|
7624 | /*
|
---|
7625 | * Permission checks.
|
---|
7626 | */
|
---|
7627 | if (pVCpu->iem.s.uCpl != 0)
|
---|
7628 | {
|
---|
7629 | Log2(("monitor: CPL != 0\n"));
|
---|
7630 | return iemRaiseUndefinedOpcode(pVCpu); /** @todo MSR[0xC0010015].MonMwaitUserEn if we care. */
|
---|
7631 | }
|
---|
7632 | if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fMonitorMWait)
|
---|
7633 | {
|
---|
7634 | Log2(("monitor: Not in CPUID\n"));
|
---|
7635 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
7636 | }
|
---|
7637 |
|
---|
7638 | /*
|
---|
7639 | * Check VMX guest-intercept.
|
---|
7640 | * This should be considered a fault-like VM-exit.
|
---|
7641 | * See Intel spec. 25.1.1 "Relative Priority of Faults and VM Exits".
|
---|
7642 | */
|
---|
7643 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
7644 | && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_MONITOR_EXIT))
|
---|
7645 | {
|
---|
7646 | Log2(("monitor: Guest intercept -> #VMEXIT\n"));
|
---|
7647 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_MONITOR, cbInstr);
|
---|
7648 | }
|
---|
7649 |
|
---|
7650 | /*
|
---|
7651 | * Gather the operands and validate them.
|
---|
7652 | */
|
---|
7653 | RTGCPTR GCPtrMem = pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT ? pVCpu->cpum.GstCtx.rax : pVCpu->cpum.GstCtx.eax;
|
---|
7654 | uint32_t uEcx = pVCpu->cpum.GstCtx.ecx;
|
---|
7655 | uint32_t uEdx = pVCpu->cpum.GstCtx.edx;
|
---|
7656 | /** @todo Test whether EAX or ECX is processed first, i.e. do we get \#PF or
|
---|
7657 | * \#GP first. */
|
---|
7658 | if (uEcx != 0)
|
---|
7659 | {
|
---|
7660 | Log2(("monitor rax=%RX64, ecx=%RX32, edx=%RX32; ECX != 0 -> #GP(0)\n", GCPtrMem, uEcx, uEdx)); NOREF(uEdx);
|
---|
7661 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7662 | }
|
---|
7663 |
|
---|
7664 | VBOXSTRICTRC rcStrict = iemMemApplySegment(pVCpu, IEM_ACCESS_TYPE_READ | IEM_ACCESS_WHAT_DATA, iEffSeg, 1, &GCPtrMem);
|
---|
7665 | if (rcStrict != VINF_SUCCESS)
|
---|
7666 | return rcStrict;
|
---|
7667 |
|
---|
7668 | RTGCPHYS GCPhysMem;
|
---|
7669 | /** @todo access size */
|
---|
7670 | rcStrict = iemMemPageTranslateAndCheckAccess(pVCpu, GCPtrMem, 1, IEM_ACCESS_TYPE_READ | IEM_ACCESS_WHAT_DATA, &GCPhysMem);
|
---|
7671 | if (rcStrict != VINF_SUCCESS)
|
---|
7672 | return rcStrict;
|
---|
7673 |
|
---|
7674 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
7675 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
7676 | && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_VIRT_APIC_ACCESS))
|
---|
7677 | {
|
---|
7678 | /*
|
---|
7679 | * MONITOR does not access the memory, just monitors the address. However,
|
---|
7680 | * if the address falls in the APIC-access page, the address monitored must
|
---|
7681 | * instead be the corresponding address in the virtual-APIC page.
|
---|
7682 | *
|
---|
7683 | * See Intel spec. 29.4.4 "Instruction-Specific Considerations".
|
---|
7684 | */
|
---|
7685 | rcStrict = iemVmxVirtApicAccessUnused(pVCpu, &GCPhysMem, 1, IEM_ACCESS_TYPE_READ | IEM_ACCESS_WHAT_DATA);
|
---|
7686 | if ( rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE
|
---|
7687 | && rcStrict != VINF_VMX_MODIFIES_BEHAVIOR)
|
---|
7688 | return rcStrict;
|
---|
7689 | }
|
---|
7690 | #endif
|
---|
7691 |
|
---|
7692 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_MONITOR))
|
---|
7693 | {
|
---|
7694 | Log2(("monitor: Guest intercept -> #VMEXIT\n"));
|
---|
7695 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
7696 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_MONITOR, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
7697 | }
|
---|
7698 |
|
---|
7699 | /*
|
---|
7700 | * Call EM to prepare the monitor/wait.
|
---|
7701 | */
|
---|
7702 | rcStrict = EMMonitorWaitPrepare(pVCpu, pVCpu->cpum.GstCtx.rax, pVCpu->cpum.GstCtx.rcx, pVCpu->cpum.GstCtx.rdx, GCPhysMem);
|
---|
7703 | Assert(rcStrict == VINF_SUCCESS);
|
---|
7704 | if (rcStrict == VINF_SUCCESS)
|
---|
7705 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7706 | return rcStrict;
|
---|
7707 | }
|
---|
7708 |
|
---|
7709 |
|
---|
7710 | /**
|
---|
7711 | * Implements 'MWAIT'.
|
---|
7712 | */
|
---|
7713 | IEM_CIMPL_DEF_0(iemCImpl_mwait)
|
---|
7714 | {
|
---|
7715 | /*
|
---|
7716 | * Permission checks.
|
---|
7717 | */
|
---|
7718 | if (pVCpu->iem.s.uCpl != 0)
|
---|
7719 | {
|
---|
7720 | Log2(("mwait: CPL != 0\n"));
|
---|
7721 | /** @todo MSR[0xC0010015].MonMwaitUserEn if we care. (Remember to check
|
---|
7722 | * EFLAGS.VM then.) */
|
---|
7723 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
7724 | }
|
---|
7725 | if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fMonitorMWait)
|
---|
7726 | {
|
---|
7727 | Log2(("mwait: Not in CPUID\n"));
|
---|
7728 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
7729 | }
|
---|
7730 |
|
---|
7731 | /* Check VMX nested-guest intercept. */
|
---|
7732 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
7733 | && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_MWAIT_EXIT))
|
---|
7734 | IEM_VMX_VMEXIT_MWAIT_RET(pVCpu, EMMonitorIsArmed(pVCpu), cbInstr);
|
---|
7735 |
|
---|
7736 | /*
|
---|
7737 | * Gather the operands and validate them.
|
---|
7738 | */
|
---|
7739 | uint32_t const uEax = pVCpu->cpum.GstCtx.eax;
|
---|
7740 | uint32_t const uEcx = pVCpu->cpum.GstCtx.ecx;
|
---|
7741 | if (uEcx != 0)
|
---|
7742 | {
|
---|
7743 | /* Only supported extension is break on IRQ when IF=0. */
|
---|
7744 | if (uEcx > 1)
|
---|
7745 | {
|
---|
7746 | Log2(("mwait eax=%RX32, ecx=%RX32; ECX > 1 -> #GP(0)\n", uEax, uEcx));
|
---|
7747 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7748 | }
|
---|
7749 | uint32_t fMWaitFeatures = 0;
|
---|
7750 | uint32_t uIgnore = 0;
|
---|
7751 | CPUMGetGuestCpuId(pVCpu, 5, 0, -1 /*f64BitMode*/, &uIgnore, &uIgnore, &fMWaitFeatures, &uIgnore);
|
---|
7752 | if ( (fMWaitFeatures & (X86_CPUID_MWAIT_ECX_EXT | X86_CPUID_MWAIT_ECX_BREAKIRQIF0))
|
---|
7753 | != (X86_CPUID_MWAIT_ECX_EXT | X86_CPUID_MWAIT_ECX_BREAKIRQIF0))
|
---|
7754 | {
|
---|
7755 | Log2(("mwait eax=%RX32, ecx=%RX32; break-on-IRQ-IF=0 extension not enabled -> #GP(0)\n", uEax, uEcx));
|
---|
7756 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7757 | }
|
---|
7758 |
|
---|
7759 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
7760 | /*
|
---|
7761 | * If the interrupt-window exiting control is set or a virtual-interrupt is pending
|
---|
7762 | * for delivery; and interrupts are disabled the processor does not enter its
|
---|
7763 | * mwait state but rather passes control to the next instruction.
|
---|
7764 | *
|
---|
7765 | * See Intel spec. 25.3 "Changes to Instruction Behavior In VMX Non-root Operation".
|
---|
7766 | */
|
---|
7767 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
7768 | && !pVCpu->cpum.GstCtx.eflags.Bits.u1IF)
|
---|
7769 | {
|
---|
7770 | if ( IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_INT_WINDOW_EXIT)
|
---|
7771 | || VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_NESTED_GUEST))
|
---|
7772 | /** @todo finish: check up this out after we move int window stuff out of the
|
---|
7773 | * run loop and into the instruction finishing logic here. */
|
---|
7774 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7775 | }
|
---|
7776 | #endif
|
---|
7777 | }
|
---|
7778 |
|
---|
7779 | /*
|
---|
7780 | * Check SVM nested-guest mwait intercepts.
|
---|
7781 | */
|
---|
7782 | if ( IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_MWAIT_ARMED)
|
---|
7783 | && EMMonitorIsArmed(pVCpu))
|
---|
7784 | {
|
---|
7785 | Log2(("mwait: Guest intercept (monitor hardware armed) -> #VMEXIT\n"));
|
---|
7786 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
7787 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_MWAIT_ARMED, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
7788 | }
|
---|
7789 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_MWAIT))
|
---|
7790 | {
|
---|
7791 | Log2(("mwait: Guest intercept -> #VMEXIT\n"));
|
---|
7792 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
7793 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_MWAIT, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
7794 | }
|
---|
7795 |
|
---|
7796 | /*
|
---|
7797 | * Call EM to prepare the monitor/wait.
|
---|
7798 | *
|
---|
7799 | * This will return VINF_EM_HALT. If there the trap flag is set, we may
|
---|
7800 | * override it when executing iemRegAddToRipAndFinishingClearingRF ASSUMING
|
---|
7801 | * that will only return guest related events.
|
---|
7802 | */
|
---|
7803 | VBOXSTRICTRC rcStrict = EMMonitorWaitPerform(pVCpu, uEax, uEcx);
|
---|
7804 |
|
---|
7805 | /** @todo finish: This needs more thinking as we should suppress internal
|
---|
7806 | * debugger events here, or we'll bugger up the guest state even more than we
|
---|
7807 | * alread do around VINF_EM_HALT. */
|
---|
7808 | VBOXSTRICTRC rcStrict2 = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7809 | if (rcStrict2 != VINF_SUCCESS)
|
---|
7810 | {
|
---|
7811 | Log2(("mwait: %Rrc (perform) -> %Rrc (finish)!\n", VBOXSTRICTRC_VAL(rcStrict), VBOXSTRICTRC_VAL(rcStrict2) ));
|
---|
7812 | rcStrict = rcStrict2;
|
---|
7813 | }
|
---|
7814 |
|
---|
7815 | return rcStrict;
|
---|
7816 | }
|
---|
7817 |
|
---|
7818 |
|
---|
7819 | /**
|
---|
7820 | * Implements 'SWAPGS'.
|
---|
7821 | */
|
---|
7822 | IEM_CIMPL_DEF_0(iemCImpl_swapgs)
|
---|
7823 | {
|
---|
7824 | Assert(pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT); /* Caller checks this. */
|
---|
7825 |
|
---|
7826 | /*
|
---|
7827 | * Permission checks.
|
---|
7828 | */
|
---|
7829 | if (pVCpu->iem.s.uCpl != 0)
|
---|
7830 | {
|
---|
7831 | Log2(("swapgs: CPL != 0\n"));
|
---|
7832 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
7833 | }
|
---|
7834 |
|
---|
7835 | /*
|
---|
7836 | * Do the job.
|
---|
7837 | */
|
---|
7838 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_KERNEL_GS_BASE | CPUMCTX_EXTRN_GS);
|
---|
7839 | uint64_t uOtherGsBase = pVCpu->cpum.GstCtx.msrKERNELGSBASE;
|
---|
7840 | pVCpu->cpum.GstCtx.msrKERNELGSBASE = pVCpu->cpum.GstCtx.gs.u64Base;
|
---|
7841 | pVCpu->cpum.GstCtx.gs.u64Base = uOtherGsBase;
|
---|
7842 |
|
---|
7843 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7844 | }
|
---|
7845 |
|
---|
7846 |
|
---|
7847 | #ifndef VBOX_WITHOUT_CPUID_HOST_CALL
|
---|
7848 | /**
|
---|
7849 | * Handles a CPUID call.
|
---|
7850 | */
|
---|
7851 | static VBOXSTRICTRC iemCpuIdVBoxCall(PVMCPUCC pVCpu, uint32_t iFunction,
|
---|
7852 | uint32_t *pEax, uint32_t *pEbx, uint32_t *pEcx, uint32_t *pEdx)
|
---|
7853 | {
|
---|
7854 | switch (iFunction)
|
---|
7855 | {
|
---|
7856 | case VBOX_CPUID_FN_ID:
|
---|
7857 | LogFlow(("iemCpuIdVBoxCall: VBOX_CPUID_FN_ID\n"));
|
---|
7858 | *pEax = VBOX_CPUID_RESP_ID_EAX;
|
---|
7859 | *pEbx = VBOX_CPUID_RESP_ID_EBX;
|
---|
7860 | *pEcx = VBOX_CPUID_RESP_ID_ECX;
|
---|
7861 | *pEdx = VBOX_CPUID_RESP_ID_EDX;
|
---|
7862 | break;
|
---|
7863 |
|
---|
7864 | case VBOX_CPUID_FN_LOG:
|
---|
7865 | {
|
---|
7866 | CPUM_IMPORT_EXTRN_RET(pVCpu, CPUMCTX_EXTRN_RDX | CPUMCTX_EXTRN_RBX | CPUMCTX_EXTRN_RSI
|
---|
7867 | | IEM_CPUMCTX_EXTRN_EXEC_DECODED_MEM_MASK);
|
---|
7868 |
|
---|
7869 | /* Validate input. */
|
---|
7870 | uint32_t cchToLog = *pEdx;
|
---|
7871 | if (cchToLog <= _2M)
|
---|
7872 | {
|
---|
7873 | uint32_t const uLogPicker = *pEbx;
|
---|
7874 | if (uLogPicker <= 1)
|
---|
7875 | {
|
---|
7876 | /* Resolve the logger. */
|
---|
7877 | PRTLOGGER const pLogger = !uLogPicker
|
---|
7878 | ? RTLogDefaultInstanceEx(UINT32_MAX) : RTLogRelGetDefaultInstanceEx(UINT32_MAX);
|
---|
7879 | if (pLogger)
|
---|
7880 | {
|
---|
7881 | /* Copy over the data: */
|
---|
7882 | RTGCPTR GCPtrSrc = pVCpu->cpum.GstCtx.rsi;
|
---|
7883 | while (cchToLog > 0)
|
---|
7884 | {
|
---|
7885 | uint32_t cbToMap = GUEST_PAGE_SIZE - (GCPtrSrc & GUEST_PAGE_OFFSET_MASK);
|
---|
7886 | if (cbToMap > cchToLog)
|
---|
7887 | cbToMap = cchToLog;
|
---|
7888 | /** @todo Extend iemMemMap to allowing page size accessing and avoid 7
|
---|
7889 | * unnecessary calls & iterations per pages. */
|
---|
7890 | if (cbToMap > 512)
|
---|
7891 | cbToMap = 512;
|
---|
7892 | void *pvSrc = NULL;
|
---|
7893 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, &pvSrc, cbToMap, UINT8_MAX, GCPtrSrc, IEM_ACCESS_DATA_R, 0);
|
---|
7894 | if (rcStrict == VINF_SUCCESS)
|
---|
7895 | {
|
---|
7896 | RTLogBulkNestedWrite(pLogger, (const char *)pvSrc, cbToMap, "Gst:");
|
---|
7897 | rcStrict = iemMemCommitAndUnmap(pVCpu, pvSrc, IEM_ACCESS_DATA_R);
|
---|
7898 | AssertRCSuccessReturn(VBOXSTRICTRC_VAL(rcStrict), rcStrict);
|
---|
7899 | }
|
---|
7900 | else
|
---|
7901 | {
|
---|
7902 | Log(("iemCpuIdVBoxCall: %Rrc at %RGp LB %#x\n", VBOXSTRICTRC_VAL(rcStrict), GCPtrSrc, cbToMap));
|
---|
7903 | return rcStrict;
|
---|
7904 | }
|
---|
7905 |
|
---|
7906 | /* Advance. */
|
---|
7907 | pVCpu->cpum.GstCtx.rsi = GCPtrSrc += cbToMap;
|
---|
7908 | *pEdx = cchToLog -= cbToMap;
|
---|
7909 | }
|
---|
7910 | *pEax = VINF_SUCCESS;
|
---|
7911 | }
|
---|
7912 | else
|
---|
7913 | *pEax = (uint32_t)VERR_NOT_FOUND;
|
---|
7914 | }
|
---|
7915 | else
|
---|
7916 | *pEax = (uint32_t)VERR_NOT_FOUND;
|
---|
7917 | }
|
---|
7918 | else
|
---|
7919 | *pEax = (uint32_t)VERR_TOO_MUCH_DATA;
|
---|
7920 | *pEdx = VBOX_CPUID_RESP_GEN_EDX;
|
---|
7921 | *pEcx = VBOX_CPUID_RESP_GEN_ECX;
|
---|
7922 | *pEbx = VBOX_CPUID_RESP_GEN_EBX;
|
---|
7923 | break;
|
---|
7924 | }
|
---|
7925 |
|
---|
7926 | default:
|
---|
7927 | LogFlow(("iemCpuIdVBoxCall: Invalid function %#x (%#x, %#x)\n", iFunction, *pEbx, *pEdx));
|
---|
7928 | *pEax = (uint32_t)VERR_INVALID_FUNCTION;
|
---|
7929 | *pEbx = (uint32_t)VERR_INVALID_FUNCTION;
|
---|
7930 | *pEcx = (uint32_t)VERR_INVALID_FUNCTION;
|
---|
7931 | *pEdx = (uint32_t)VERR_INVALID_FUNCTION;
|
---|
7932 | break;
|
---|
7933 | }
|
---|
7934 | return VINF_SUCCESS;
|
---|
7935 | }
|
---|
7936 | #endif /* VBOX_WITHOUT_CPUID_HOST_CALL */
|
---|
7937 |
|
---|
7938 | /**
|
---|
7939 | * Implements 'CPUID'.
|
---|
7940 | */
|
---|
7941 | IEM_CIMPL_DEF_0(iemCImpl_cpuid)
|
---|
7942 | {
|
---|
7943 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
7944 | {
|
---|
7945 | Log2(("cpuid: Guest intercept -> VM-exit\n"));
|
---|
7946 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_CPUID, cbInstr);
|
---|
7947 | }
|
---|
7948 |
|
---|
7949 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_CPUID))
|
---|
7950 | {
|
---|
7951 | Log2(("cpuid: Guest intercept -> #VMEXIT\n"));
|
---|
7952 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
7953 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_CPUID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
7954 | }
|
---|
7955 |
|
---|
7956 |
|
---|
7957 | uint32_t const uEax = pVCpu->cpum.GstCtx.eax;
|
---|
7958 | uint32_t const uEcx = pVCpu->cpum.GstCtx.ecx;
|
---|
7959 |
|
---|
7960 | #ifndef VBOX_WITHOUT_CPUID_HOST_CALL
|
---|
7961 | /*
|
---|
7962 | * CPUID host call backdoor.
|
---|
7963 | */
|
---|
7964 | if ( uEax == VBOX_CPUID_REQ_EAX_FIXED
|
---|
7965 | && (uEcx & VBOX_CPUID_REQ_ECX_FIXED_MASK) == VBOX_CPUID_REQ_ECX_FIXED
|
---|
7966 | && pVCpu->CTX_SUFF(pVM)->iem.s.fCpuIdHostCall)
|
---|
7967 | {
|
---|
7968 | VBOXSTRICTRC rcStrict = iemCpuIdVBoxCall(pVCpu, uEcx & VBOX_CPUID_REQ_ECX_FN_MASK,
|
---|
7969 | &pVCpu->cpum.GstCtx.eax, &pVCpu->cpum.GstCtx.ebx,
|
---|
7970 | &pVCpu->cpum.GstCtx.ecx, &pVCpu->cpum.GstCtx.edx);
|
---|
7971 | if (rcStrict != VINF_SUCCESS)
|
---|
7972 | return rcStrict;
|
---|
7973 | }
|
---|
7974 | /*
|
---|
7975 | * Regular CPUID.
|
---|
7976 | */
|
---|
7977 | else
|
---|
7978 | #endif
|
---|
7979 | CPUMGetGuestCpuId(pVCpu, uEax, uEcx, pVCpu->cpum.GstCtx.cs.Attr.n.u1Long,
|
---|
7980 | &pVCpu->cpum.GstCtx.eax, &pVCpu->cpum.GstCtx.ebx, &pVCpu->cpum.GstCtx.ecx, &pVCpu->cpum.GstCtx.edx);
|
---|
7981 |
|
---|
7982 | pVCpu->cpum.GstCtx.rax &= UINT32_C(0xffffffff);
|
---|
7983 | pVCpu->cpum.GstCtx.rbx &= UINT32_C(0xffffffff);
|
---|
7984 | pVCpu->cpum.GstCtx.rcx &= UINT32_C(0xffffffff);
|
---|
7985 | pVCpu->cpum.GstCtx.rdx &= UINT32_C(0xffffffff);
|
---|
7986 | pVCpu->cpum.GstCtx.fExtrn &= ~(CPUMCTX_EXTRN_RAX | CPUMCTX_EXTRN_RCX | CPUMCTX_EXTRN_RDX | CPUMCTX_EXTRN_RBX);
|
---|
7987 |
|
---|
7988 | pVCpu->iem.s.cPotentialExits++;
|
---|
7989 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7990 | }
|
---|
7991 |
|
---|
7992 |
|
---|
7993 | /**
|
---|
7994 | * Implements 'AAD'.
|
---|
7995 | *
|
---|
7996 | * @param bImm The immediate operand.
|
---|
7997 | */
|
---|
7998 | IEM_CIMPL_DEF_1(iemCImpl_aad, uint8_t, bImm)
|
---|
7999 | {
|
---|
8000 | uint16_t const ax = pVCpu->cpum.GstCtx.ax;
|
---|
8001 | uint8_t const al = (uint8_t)ax + (uint8_t)(ax >> 8) * bImm;
|
---|
8002 | pVCpu->cpum.GstCtx.ax = al;
|
---|
8003 | iemHlpUpdateArithEFlagsU8(pVCpu, al,
|
---|
8004 | X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF,
|
---|
8005 | X86_EFL_OF | X86_EFL_AF | X86_EFL_CF);
|
---|
8006 |
|
---|
8007 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8008 | }
|
---|
8009 |
|
---|
8010 |
|
---|
8011 | /**
|
---|
8012 | * Implements 'AAM'.
|
---|
8013 | *
|
---|
8014 | * @param bImm The immediate operand. Cannot be 0.
|
---|
8015 | */
|
---|
8016 | IEM_CIMPL_DEF_1(iemCImpl_aam, uint8_t, bImm)
|
---|
8017 | {
|
---|
8018 | Assert(bImm != 0); /* #DE on 0 is handled in the decoder. */
|
---|
8019 |
|
---|
8020 | uint16_t const ax = pVCpu->cpum.GstCtx.ax;
|
---|
8021 | uint8_t const al = (uint8_t)ax % bImm;
|
---|
8022 | uint8_t const ah = (uint8_t)ax / bImm;
|
---|
8023 | pVCpu->cpum.GstCtx.ax = (ah << 8) + al;
|
---|
8024 | iemHlpUpdateArithEFlagsU8(pVCpu, al,
|
---|
8025 | X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF,
|
---|
8026 | X86_EFL_OF | X86_EFL_AF | X86_EFL_CF);
|
---|
8027 |
|
---|
8028 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8029 | }
|
---|
8030 |
|
---|
8031 |
|
---|
8032 | /**
|
---|
8033 | * Implements 'DAA'.
|
---|
8034 | */
|
---|
8035 | IEM_CIMPL_DEF_0(iemCImpl_daa)
|
---|
8036 | {
|
---|
8037 | uint8_t const al = pVCpu->cpum.GstCtx.al;
|
---|
8038 | bool const fCarry = pVCpu->cpum.GstCtx.eflags.Bits.u1CF;
|
---|
8039 |
|
---|
8040 | if ( pVCpu->cpum.GstCtx.eflags.Bits.u1AF
|
---|
8041 | || (al & 0xf) >= 10)
|
---|
8042 | {
|
---|
8043 | pVCpu->cpum.GstCtx.al = al + 6;
|
---|
8044 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 1;
|
---|
8045 | }
|
---|
8046 | else
|
---|
8047 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 0;
|
---|
8048 |
|
---|
8049 | if (al >= 0x9a || fCarry)
|
---|
8050 | {
|
---|
8051 | pVCpu->cpum.GstCtx.al += 0x60;
|
---|
8052 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
|
---|
8053 | }
|
---|
8054 | else
|
---|
8055 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 0;
|
---|
8056 |
|
---|
8057 | iemHlpUpdateArithEFlagsU8(pVCpu, pVCpu->cpum.GstCtx.al, X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF, X86_EFL_OF);
|
---|
8058 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8059 | }
|
---|
8060 |
|
---|
8061 |
|
---|
8062 | /**
|
---|
8063 | * Implements 'DAS'.
|
---|
8064 | */
|
---|
8065 | IEM_CIMPL_DEF_0(iemCImpl_das)
|
---|
8066 | {
|
---|
8067 | uint8_t const uInputAL = pVCpu->cpum.GstCtx.al;
|
---|
8068 | bool const fCarry = pVCpu->cpum.GstCtx.eflags.Bits.u1CF;
|
---|
8069 |
|
---|
8070 | if ( pVCpu->cpum.GstCtx.eflags.Bits.u1AF
|
---|
8071 | || (uInputAL & 0xf) >= 10)
|
---|
8072 | {
|
---|
8073 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 1;
|
---|
8074 | if (uInputAL < 6)
|
---|
8075 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
|
---|
8076 | pVCpu->cpum.GstCtx.al = uInputAL - 6;
|
---|
8077 | }
|
---|
8078 | else
|
---|
8079 | {
|
---|
8080 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 0;
|
---|
8081 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 0;
|
---|
8082 | }
|
---|
8083 |
|
---|
8084 | if (uInputAL >= 0x9a || fCarry)
|
---|
8085 | {
|
---|
8086 | pVCpu->cpum.GstCtx.al -= 0x60;
|
---|
8087 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
|
---|
8088 | }
|
---|
8089 |
|
---|
8090 | iemHlpUpdateArithEFlagsU8(pVCpu, pVCpu->cpum.GstCtx.al, X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF, X86_EFL_OF);
|
---|
8091 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8092 | }
|
---|
8093 |
|
---|
8094 |
|
---|
8095 | /**
|
---|
8096 | * Implements 'AAA'.
|
---|
8097 | */
|
---|
8098 | IEM_CIMPL_DEF_0(iemCImpl_aaa)
|
---|
8099 | {
|
---|
8100 | if (IEM_IS_GUEST_CPU_AMD(pVCpu))
|
---|
8101 | {
|
---|
8102 | if ( pVCpu->cpum.GstCtx.eflags.Bits.u1AF
|
---|
8103 | || (pVCpu->cpum.GstCtx.ax & 0xf) >= 10)
|
---|
8104 | {
|
---|
8105 | iemAImpl_add_u16(&pVCpu->cpum.GstCtx.ax, 0x106, &pVCpu->cpum.GstCtx.eflags.uBoth);
|
---|
8106 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 1;
|
---|
8107 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
|
---|
8108 | }
|
---|
8109 | else
|
---|
8110 | {
|
---|
8111 | iemHlpUpdateArithEFlagsU16(pVCpu, pVCpu->cpum.GstCtx.ax, X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF, X86_EFL_OF);
|
---|
8112 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 0;
|
---|
8113 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 0;
|
---|
8114 | }
|
---|
8115 | pVCpu->cpum.GstCtx.ax &= UINT16_C(0xff0f);
|
---|
8116 | }
|
---|
8117 | else
|
---|
8118 | {
|
---|
8119 | if ( pVCpu->cpum.GstCtx.eflags.Bits.u1AF
|
---|
8120 | || (pVCpu->cpum.GstCtx.ax & 0xf) >= 10)
|
---|
8121 | {
|
---|
8122 | pVCpu->cpum.GstCtx.ax += UINT16_C(0x106);
|
---|
8123 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 1;
|
---|
8124 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
|
---|
8125 | }
|
---|
8126 | else
|
---|
8127 | {
|
---|
8128 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 0;
|
---|
8129 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 0;
|
---|
8130 | }
|
---|
8131 | pVCpu->cpum.GstCtx.ax &= UINT16_C(0xff0f);
|
---|
8132 | iemHlpUpdateArithEFlagsU8(pVCpu, pVCpu->cpum.GstCtx.al, X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF, X86_EFL_OF);
|
---|
8133 | }
|
---|
8134 |
|
---|
8135 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8136 | }
|
---|
8137 |
|
---|
8138 |
|
---|
8139 | /**
|
---|
8140 | * Implements 'AAS'.
|
---|
8141 | */
|
---|
8142 | IEM_CIMPL_DEF_0(iemCImpl_aas)
|
---|
8143 | {
|
---|
8144 | if (IEM_IS_GUEST_CPU_AMD(pVCpu))
|
---|
8145 | {
|
---|
8146 | if ( pVCpu->cpum.GstCtx.eflags.Bits.u1AF
|
---|
8147 | || (pVCpu->cpum.GstCtx.ax & 0xf) >= 10)
|
---|
8148 | {
|
---|
8149 | iemAImpl_sub_u16(&pVCpu->cpum.GstCtx.ax, 0x106, &pVCpu->cpum.GstCtx.eflags.uBoth);
|
---|
8150 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 1;
|
---|
8151 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
|
---|
8152 | }
|
---|
8153 | else
|
---|
8154 | {
|
---|
8155 | iemHlpUpdateArithEFlagsU16(pVCpu, pVCpu->cpum.GstCtx.ax, X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF, X86_EFL_OF);
|
---|
8156 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 0;
|
---|
8157 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 0;
|
---|
8158 | }
|
---|
8159 | pVCpu->cpum.GstCtx.ax &= UINT16_C(0xff0f);
|
---|
8160 | }
|
---|
8161 | else
|
---|
8162 | {
|
---|
8163 | if ( pVCpu->cpum.GstCtx.eflags.Bits.u1AF
|
---|
8164 | || (pVCpu->cpum.GstCtx.ax & 0xf) >= 10)
|
---|
8165 | {
|
---|
8166 | pVCpu->cpum.GstCtx.ax -= UINT16_C(0x106);
|
---|
8167 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 1;
|
---|
8168 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
|
---|
8169 | }
|
---|
8170 | else
|
---|
8171 | {
|
---|
8172 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 0;
|
---|
8173 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 0;
|
---|
8174 | }
|
---|
8175 | pVCpu->cpum.GstCtx.ax &= UINT16_C(0xff0f);
|
---|
8176 | iemHlpUpdateArithEFlagsU8(pVCpu, pVCpu->cpum.GstCtx.al, X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF, X86_EFL_OF);
|
---|
8177 | }
|
---|
8178 |
|
---|
8179 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8180 | }
|
---|
8181 |
|
---|
8182 |
|
---|
8183 | /**
|
---|
8184 | * Implements the 16-bit version of 'BOUND'.
|
---|
8185 | *
|
---|
8186 | * @note We have separate 16-bit and 32-bit variants of this function due to
|
---|
8187 | * the decoder using unsigned parameters, whereas we want signed one to
|
---|
8188 | * do the job. This is significant for a recompiler.
|
---|
8189 | */
|
---|
8190 | IEM_CIMPL_DEF_3(iemCImpl_bound_16, int16_t, idxArray, int16_t, idxLowerBound, int16_t, idxUpperBound)
|
---|
8191 | {
|
---|
8192 | /*
|
---|
8193 | * Check if the index is inside the bounds, otherwise raise #BR.
|
---|
8194 | */
|
---|
8195 | if ( idxArray >= idxLowerBound
|
---|
8196 | && idxArray <= idxUpperBound)
|
---|
8197 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8198 | return iemRaiseBoundRangeExceeded(pVCpu);
|
---|
8199 | }
|
---|
8200 |
|
---|
8201 |
|
---|
8202 | /**
|
---|
8203 | * Implements the 32-bit version of 'BOUND'.
|
---|
8204 | */
|
---|
8205 | IEM_CIMPL_DEF_3(iemCImpl_bound_32, int32_t, idxArray, int32_t, idxLowerBound, int32_t, idxUpperBound)
|
---|
8206 | {
|
---|
8207 | /*
|
---|
8208 | * Check if the index is inside the bounds, otherwise raise #BR.
|
---|
8209 | */
|
---|
8210 | if ( idxArray >= idxLowerBound
|
---|
8211 | && idxArray <= idxUpperBound)
|
---|
8212 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8213 | return iemRaiseBoundRangeExceeded(pVCpu);
|
---|
8214 | }
|
---|
8215 |
|
---|
8216 |
|
---|
8217 |
|
---|
8218 | /*
|
---|
8219 | * Instantiate the various string operation combinations.
|
---|
8220 | */
|
---|
8221 | #define OP_SIZE 8
|
---|
8222 | #define ADDR_SIZE 16
|
---|
8223 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
8224 | #define OP_SIZE 8
|
---|
8225 | #define ADDR_SIZE 32
|
---|
8226 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
8227 | #define OP_SIZE 8
|
---|
8228 | #define ADDR_SIZE 64
|
---|
8229 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
8230 |
|
---|
8231 | #define OP_SIZE 16
|
---|
8232 | #define ADDR_SIZE 16
|
---|
8233 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
8234 | #define OP_SIZE 16
|
---|
8235 | #define ADDR_SIZE 32
|
---|
8236 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
8237 | #define OP_SIZE 16
|
---|
8238 | #define ADDR_SIZE 64
|
---|
8239 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
8240 |
|
---|
8241 | #define OP_SIZE 32
|
---|
8242 | #define ADDR_SIZE 16
|
---|
8243 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
8244 | #define OP_SIZE 32
|
---|
8245 | #define ADDR_SIZE 32
|
---|
8246 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
8247 | #define OP_SIZE 32
|
---|
8248 | #define ADDR_SIZE 64
|
---|
8249 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
8250 |
|
---|
8251 | #define OP_SIZE 64
|
---|
8252 | #define ADDR_SIZE 32
|
---|
8253 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
8254 | #define OP_SIZE 64
|
---|
8255 | #define ADDR_SIZE 64
|
---|
8256 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
8257 |
|
---|
8258 |
|
---|
8259 | /**
|
---|
8260 | * Implements 'XGETBV'.
|
---|
8261 | */
|
---|
8262 | IEM_CIMPL_DEF_0(iemCImpl_xgetbv)
|
---|
8263 | {
|
---|
8264 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR4);
|
---|
8265 | if (pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSXSAVE)
|
---|
8266 | {
|
---|
8267 | uint32_t uEcx = pVCpu->cpum.GstCtx.ecx;
|
---|
8268 | switch (uEcx)
|
---|
8269 | {
|
---|
8270 | case 0:
|
---|
8271 | break;
|
---|
8272 |
|
---|
8273 | case 1: /** @todo Implement XCR1 support. */
|
---|
8274 | default:
|
---|
8275 | Log(("xgetbv ecx=%RX32 -> #GP(0)\n", uEcx));
|
---|
8276 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
8277 |
|
---|
8278 | }
|
---|
8279 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_XCRx);
|
---|
8280 | pVCpu->cpum.GstCtx.rax = RT_LO_U32(pVCpu->cpum.GstCtx.aXcr[uEcx]);
|
---|
8281 | pVCpu->cpum.GstCtx.rdx = RT_HI_U32(pVCpu->cpum.GstCtx.aXcr[uEcx]);
|
---|
8282 |
|
---|
8283 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8284 | }
|
---|
8285 | Log(("xgetbv CR4.OSXSAVE=0 -> UD\n"));
|
---|
8286 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
8287 | }
|
---|
8288 |
|
---|
8289 |
|
---|
8290 | /**
|
---|
8291 | * Implements 'XSETBV'.
|
---|
8292 | */
|
---|
8293 | IEM_CIMPL_DEF_0(iemCImpl_xsetbv)
|
---|
8294 | {
|
---|
8295 | if (pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSXSAVE)
|
---|
8296 | {
|
---|
8297 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_XSETBV))
|
---|
8298 | {
|
---|
8299 | Log2(("xsetbv: Guest intercept -> #VMEXIT\n"));
|
---|
8300 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
8301 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_XSETBV, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
8302 | }
|
---|
8303 |
|
---|
8304 | if (pVCpu->iem.s.uCpl == 0)
|
---|
8305 | {
|
---|
8306 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_XCRx);
|
---|
8307 |
|
---|
8308 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
8309 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_XSETBV, cbInstr);
|
---|
8310 |
|
---|
8311 | uint32_t uEcx = pVCpu->cpum.GstCtx.ecx;
|
---|
8312 | uint64_t uNewValue = RT_MAKE_U64(pVCpu->cpum.GstCtx.eax, pVCpu->cpum.GstCtx.edx);
|
---|
8313 | switch (uEcx)
|
---|
8314 | {
|
---|
8315 | case 0:
|
---|
8316 | {
|
---|
8317 | int rc = CPUMSetGuestXcr0(pVCpu, uNewValue);
|
---|
8318 | if (rc == VINF_SUCCESS)
|
---|
8319 | break;
|
---|
8320 | Assert(rc == VERR_CPUM_RAISE_GP_0);
|
---|
8321 | Log(("xsetbv ecx=%RX32 (newvalue=%RX64) -> #GP(0)\n", uEcx, uNewValue));
|
---|
8322 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
8323 | }
|
---|
8324 |
|
---|
8325 | case 1: /** @todo Implement XCR1 support. */
|
---|
8326 | default:
|
---|
8327 | Log(("xsetbv ecx=%RX32 (newvalue=%RX64) -> #GP(0)\n", uEcx, uNewValue));
|
---|
8328 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
8329 |
|
---|
8330 | }
|
---|
8331 |
|
---|
8332 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8333 | }
|
---|
8334 |
|
---|
8335 | Log(("xsetbv cpl=%u -> GP(0)\n", pVCpu->iem.s.uCpl));
|
---|
8336 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
8337 | }
|
---|
8338 | Log(("xsetbv CR4.OSXSAVE=0 -> UD\n"));
|
---|
8339 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
8340 | }
|
---|
8341 |
|
---|
8342 | #ifndef RT_ARCH_ARM64
|
---|
8343 | # ifdef IN_RING3
|
---|
8344 |
|
---|
8345 | /** Argument package for iemCImpl_cmpxchg16b_fallback_rendezvous_callback. */
|
---|
8346 | struct IEMCIMPLCX16ARGS
|
---|
8347 | {
|
---|
8348 | PRTUINT128U pu128Dst;
|
---|
8349 | PRTUINT128U pu128RaxRdx;
|
---|
8350 | PRTUINT128U pu128RbxRcx;
|
---|
8351 | uint32_t *pEFlags;
|
---|
8352 | # ifdef VBOX_STRICT
|
---|
8353 | uint32_t cCalls;
|
---|
8354 | # endif
|
---|
8355 | };
|
---|
8356 |
|
---|
8357 | /**
|
---|
8358 | * @callback_method_impl{FNVMMEMTRENDEZVOUS,
|
---|
8359 | * Worker for iemCImpl_cmpxchg16b_fallback_rendezvous}
|
---|
8360 | */
|
---|
8361 | static DECLCALLBACK(VBOXSTRICTRC) iemCImpl_cmpxchg16b_fallback_rendezvous_callback(PVM pVM, PVMCPUCC pVCpu, void *pvUser)
|
---|
8362 | {
|
---|
8363 | RT_NOREF(pVM, pVCpu);
|
---|
8364 | struct IEMCIMPLCX16ARGS *pArgs = (struct IEMCIMPLCX16ARGS *)pvUser;
|
---|
8365 | # ifdef VBOX_STRICT
|
---|
8366 | Assert(pArgs->cCalls == 0);
|
---|
8367 | pArgs->cCalls++;
|
---|
8368 | # endif
|
---|
8369 |
|
---|
8370 | iemAImpl_cmpxchg16b_fallback(pArgs->pu128Dst, pArgs->pu128RaxRdx, pArgs->pu128RbxRcx, pArgs->pEFlags);
|
---|
8371 | return VINF_SUCCESS;
|
---|
8372 | }
|
---|
8373 |
|
---|
8374 | # endif /* IN_RING3 */
|
---|
8375 |
|
---|
8376 | /**
|
---|
8377 | * Implements 'CMPXCHG16B' fallback using rendezvous.
|
---|
8378 | */
|
---|
8379 | IEM_CIMPL_DEF_4(iemCImpl_cmpxchg16b_fallback_rendezvous, PRTUINT128U, pu128Dst, PRTUINT128U, pu128RaxRdx,
|
---|
8380 | PRTUINT128U, pu128RbxRcx, uint32_t *, pEFlags)
|
---|
8381 | {
|
---|
8382 | # ifdef IN_RING3
|
---|
8383 | struct IEMCIMPLCX16ARGS Args;
|
---|
8384 | Args.pu128Dst = pu128Dst;
|
---|
8385 | Args.pu128RaxRdx = pu128RaxRdx;
|
---|
8386 | Args.pu128RbxRcx = pu128RbxRcx;
|
---|
8387 | Args.pEFlags = pEFlags;
|
---|
8388 | # ifdef VBOX_STRICT
|
---|
8389 | Args.cCalls = 0;
|
---|
8390 | # endif
|
---|
8391 | VBOXSTRICTRC rcStrict = VMMR3EmtRendezvous(pVCpu->CTX_SUFF(pVM), VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE,
|
---|
8392 | iemCImpl_cmpxchg16b_fallback_rendezvous_callback, &Args);
|
---|
8393 | Assert(Args.cCalls == 1);
|
---|
8394 | if (rcStrict == VINF_SUCCESS)
|
---|
8395 | {
|
---|
8396 | /* Duplicated tail code. */
|
---|
8397 | rcStrict = iemMemCommitAndUnmap(pVCpu, pu128Dst, IEM_ACCESS_DATA_RW);
|
---|
8398 | if (rcStrict == VINF_SUCCESS)
|
---|
8399 | {
|
---|
8400 | pVCpu->cpum.GstCtx.eflags.u = *pEFlags; /* IEM_MC_COMMIT_EFLAGS */
|
---|
8401 | if (!(*pEFlags & X86_EFL_ZF))
|
---|
8402 | {
|
---|
8403 | pVCpu->cpum.GstCtx.rax = pu128RaxRdx->s.Lo;
|
---|
8404 | pVCpu->cpum.GstCtx.rdx = pu128RaxRdx->s.Hi;
|
---|
8405 | }
|
---|
8406 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8407 | }
|
---|
8408 | }
|
---|
8409 | return rcStrict;
|
---|
8410 | # else
|
---|
8411 | RT_NOREF(pVCpu, cbInstr, pu128Dst, pu128RaxRdx, pu128RbxRcx, pEFlags);
|
---|
8412 | return VERR_IEM_ASPECT_NOT_IMPLEMENTED; /* This should get us to ring-3 for now. Should perhaps be replaced later. */
|
---|
8413 | # endif
|
---|
8414 | }
|
---|
8415 |
|
---|
8416 | #endif /* RT_ARCH_ARM64 */
|
---|
8417 |
|
---|
8418 | /**
|
---|
8419 | * Implements 'CLFLUSH' and 'CLFLUSHOPT'.
|
---|
8420 | *
|
---|
8421 | * This is implemented in C because it triggers a load like behaviour without
|
---|
8422 | * actually reading anything. Since that's not so common, it's implemented
|
---|
8423 | * here.
|
---|
8424 | *
|
---|
8425 | * @param iEffSeg The effective segment.
|
---|
8426 | * @param GCPtrEff The address of the image.
|
---|
8427 | */
|
---|
8428 | IEM_CIMPL_DEF_2(iemCImpl_clflush_clflushopt, uint8_t, iEffSeg, RTGCPTR, GCPtrEff)
|
---|
8429 | {
|
---|
8430 | /*
|
---|
8431 | * Pretend to do a load w/o reading (see also iemCImpl_monitor and iemMemMap).
|
---|
8432 | */
|
---|
8433 | VBOXSTRICTRC rcStrict = iemMemApplySegment(pVCpu, IEM_ACCESS_TYPE_READ | IEM_ACCESS_WHAT_DATA, iEffSeg, 1, &GCPtrEff);
|
---|
8434 | if (rcStrict == VINF_SUCCESS)
|
---|
8435 | {
|
---|
8436 | RTGCPHYS GCPhysMem;
|
---|
8437 | /** @todo access size. */
|
---|
8438 | rcStrict = iemMemPageTranslateAndCheckAccess(pVCpu, GCPtrEff, 1, IEM_ACCESS_TYPE_READ | IEM_ACCESS_WHAT_DATA, &GCPhysMem);
|
---|
8439 | if (rcStrict == VINF_SUCCESS)
|
---|
8440 | {
|
---|
8441 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
8442 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
8443 | && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_VIRT_APIC_ACCESS))
|
---|
8444 | {
|
---|
8445 | /*
|
---|
8446 | * CLFLUSH/CLFLUSHOPT does not access the memory, but flushes the cache-line
|
---|
8447 | * that contains the address. However, if the address falls in the APIC-access
|
---|
8448 | * page, the address flushed must instead be the corresponding address in the
|
---|
8449 | * virtual-APIC page.
|
---|
8450 | *
|
---|
8451 | * See Intel spec. 29.4.4 "Instruction-Specific Considerations".
|
---|
8452 | */
|
---|
8453 | rcStrict = iemVmxVirtApicAccessUnused(pVCpu, &GCPhysMem, 1, IEM_ACCESS_TYPE_READ | IEM_ACCESS_WHAT_DATA);
|
---|
8454 | if ( rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE
|
---|
8455 | && rcStrict != VINF_VMX_MODIFIES_BEHAVIOR)
|
---|
8456 | return rcStrict;
|
---|
8457 | }
|
---|
8458 | #endif
|
---|
8459 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8460 | }
|
---|
8461 | }
|
---|
8462 |
|
---|
8463 | return rcStrict;
|
---|
8464 | }
|
---|
8465 |
|
---|
8466 |
|
---|
8467 | /**
|
---|
8468 | * Implements 'FINIT' and 'FNINIT'.
|
---|
8469 | *
|
---|
8470 | * @param fCheckXcpts Whether to check for umasked pending exceptions or
|
---|
8471 | * not.
|
---|
8472 | */
|
---|
8473 | IEM_CIMPL_DEF_1(iemCImpl_finit, bool, fCheckXcpts)
|
---|
8474 | {
|
---|
8475 | /*
|
---|
8476 | * Exceptions.
|
---|
8477 | */
|
---|
8478 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0);
|
---|
8479 | if (pVCpu->cpum.GstCtx.cr0 & (X86_CR0_EM | X86_CR0_TS))
|
---|
8480 | return iemRaiseDeviceNotAvailable(pVCpu);
|
---|
8481 |
|
---|
8482 | iemFpuActualizeStateForChange(pVCpu);
|
---|
8483 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_X87);
|
---|
8484 |
|
---|
8485 | /* FINIT: Raise #MF on pending exception(s): */
|
---|
8486 | if (fCheckXcpts && (pVCpu->cpum.GstCtx.XState.x87.FSW & X86_FSW_ES))
|
---|
8487 | return iemRaiseMathFault(pVCpu);
|
---|
8488 |
|
---|
8489 | /*
|
---|
8490 | * Reset the state.
|
---|
8491 | */
|
---|
8492 | PX86XSAVEAREA pXState = &pVCpu->cpum.GstCtx.XState;
|
---|
8493 |
|
---|
8494 | /* Rotate the stack to account for changed TOS. */
|
---|
8495 | iemFpuRotateStackSetTop(&pXState->x87, 0);
|
---|
8496 |
|
---|
8497 | pXState->x87.FCW = 0x37f;
|
---|
8498 | pXState->x87.FSW = 0;
|
---|
8499 | pXState->x87.FTW = 0x00; /* 0 - empty. */
|
---|
8500 | /** @todo Intel says the instruction and data pointers are not cleared on
|
---|
8501 | * 387, presume that 8087 and 287 doesn't do so either. */
|
---|
8502 | /** @todo test this stuff. */
|
---|
8503 | if (IEM_GET_TARGET_CPU(pVCpu) > IEMTARGETCPU_386)
|
---|
8504 | {
|
---|
8505 | pXState->x87.FPUDP = 0;
|
---|
8506 | pXState->x87.DS = 0; //??
|
---|
8507 | pXState->x87.Rsrvd2 = 0;
|
---|
8508 | pXState->x87.FPUIP = 0;
|
---|
8509 | pXState->x87.CS = 0; //??
|
---|
8510 | pXState->x87.Rsrvd1 = 0;
|
---|
8511 | }
|
---|
8512 | pXState->x87.FOP = 0;
|
---|
8513 |
|
---|
8514 | iemHlpUsedFpu(pVCpu);
|
---|
8515 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8516 | }
|
---|
8517 |
|
---|
8518 |
|
---|
8519 | /**
|
---|
8520 | * Implements 'FXSAVE'.
|
---|
8521 | *
|
---|
8522 | * @param iEffSeg The effective segment.
|
---|
8523 | * @param GCPtrEff The address of the image.
|
---|
8524 | * @param enmEffOpSize The operand size (only REX.W really matters).
|
---|
8525 | */
|
---|
8526 | IEM_CIMPL_DEF_3(iemCImpl_fxsave, uint8_t, iEffSeg, RTGCPTR, GCPtrEff, IEMMODE, enmEffOpSize)
|
---|
8527 | {
|
---|
8528 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX);
|
---|
8529 |
|
---|
8530 | /*
|
---|
8531 | * Raise exceptions.
|
---|
8532 | */
|
---|
8533 | if (pVCpu->cpum.GstCtx.cr0 & (X86_CR0_TS | X86_CR0_EM))
|
---|
8534 | return iemRaiseDeviceNotAvailable(pVCpu);
|
---|
8535 |
|
---|
8536 | /*
|
---|
8537 | * Access the memory.
|
---|
8538 | */
|
---|
8539 | void *pvMem512;
|
---|
8540 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, &pvMem512, 512, iEffSeg, GCPtrEff, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE,
|
---|
8541 | 15 | IEM_MEMMAP_F_ALIGN_GP | IEM_MEMMAP_F_ALIGN_GP_OR_AC);
|
---|
8542 | if (rcStrict != VINF_SUCCESS)
|
---|
8543 | return rcStrict;
|
---|
8544 | PX86FXSTATE pDst = (PX86FXSTATE)pvMem512;
|
---|
8545 | PCX86FXSTATE pSrc = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
8546 |
|
---|
8547 | /*
|
---|
8548 | * Store the registers.
|
---|
8549 | */
|
---|
8550 | /** @todo CPU/VM detection possible! If CR4.OSFXSR=0 MXCSR it's
|
---|
8551 | * implementation specific whether MXCSR and XMM0-XMM7 are saved. */
|
---|
8552 |
|
---|
8553 | /* common for all formats */
|
---|
8554 | pDst->FCW = pSrc->FCW;
|
---|
8555 | pDst->FSW = pSrc->FSW;
|
---|
8556 | pDst->FTW = pSrc->FTW & UINT16_C(0xff);
|
---|
8557 | pDst->FOP = pSrc->FOP;
|
---|
8558 | pDst->MXCSR = pSrc->MXCSR;
|
---|
8559 | pDst->MXCSR_MASK = CPUMGetGuestMxCsrMask(pVCpu->CTX_SUFF(pVM));
|
---|
8560 | for (uint32_t i = 0; i < RT_ELEMENTS(pDst->aRegs); i++)
|
---|
8561 | {
|
---|
8562 | /** @todo Testcase: What actually happens to the 6 reserved bytes? I'm clearing
|
---|
8563 | * them for now... */
|
---|
8564 | pDst->aRegs[i].au32[0] = pSrc->aRegs[i].au32[0];
|
---|
8565 | pDst->aRegs[i].au32[1] = pSrc->aRegs[i].au32[1];
|
---|
8566 | pDst->aRegs[i].au32[2] = pSrc->aRegs[i].au32[2] & UINT32_C(0xffff);
|
---|
8567 | pDst->aRegs[i].au32[3] = 0;
|
---|
8568 | }
|
---|
8569 |
|
---|
8570 | /* FPU IP, CS, DP and DS. */
|
---|
8571 | pDst->FPUIP = pSrc->FPUIP;
|
---|
8572 | pDst->CS = pSrc->CS;
|
---|
8573 | pDst->FPUDP = pSrc->FPUDP;
|
---|
8574 | pDst->DS = pSrc->DS;
|
---|
8575 | if (enmEffOpSize == IEMMODE_64BIT)
|
---|
8576 | {
|
---|
8577 | /* Save upper 16-bits of FPUIP (IP:CS:Rsvd1) and FPUDP (DP:DS:Rsvd2). */
|
---|
8578 | pDst->Rsrvd1 = pSrc->Rsrvd1;
|
---|
8579 | pDst->Rsrvd2 = pSrc->Rsrvd2;
|
---|
8580 | }
|
---|
8581 | else
|
---|
8582 | {
|
---|
8583 | pDst->Rsrvd1 = 0;
|
---|
8584 | pDst->Rsrvd2 = 0;
|
---|
8585 | }
|
---|
8586 |
|
---|
8587 | /* XMM registers. Skipped in 64-bit CPL0 if EFER.FFXSR (AMD only) is set. */
|
---|
8588 | if ( !(pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_FFXSR)
|
---|
8589 | || pVCpu->iem.s.enmCpuMode != IEMMODE_64BIT
|
---|
8590 | || pVCpu->iem.s.uCpl != 0)
|
---|
8591 | {
|
---|
8592 | uint32_t cXmmRegs = pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT ? 16 : 8;
|
---|
8593 | for (uint32_t i = 0; i < cXmmRegs; i++)
|
---|
8594 | pDst->aXMM[i] = pSrc->aXMM[i];
|
---|
8595 | /** @todo Testcase: What happens to the reserved XMM registers? Untouched,
|
---|
8596 | * right? */
|
---|
8597 | }
|
---|
8598 |
|
---|
8599 | /*
|
---|
8600 | * Commit the memory.
|
---|
8601 | */
|
---|
8602 | rcStrict = iemMemCommitAndUnmap(pVCpu, pvMem512, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
|
---|
8603 | if (rcStrict != VINF_SUCCESS)
|
---|
8604 | return rcStrict;
|
---|
8605 |
|
---|
8606 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8607 | }
|
---|
8608 |
|
---|
8609 |
|
---|
8610 | /**
|
---|
8611 | * Implements 'FXRSTOR'.
|
---|
8612 | *
|
---|
8613 | * @param iEffSeg The effective segment register for @a GCPtrEff.
|
---|
8614 | * @param GCPtrEff The address of the image.
|
---|
8615 | * @param enmEffOpSize The operand size (only REX.W really matters).
|
---|
8616 | */
|
---|
8617 | IEM_CIMPL_DEF_3(iemCImpl_fxrstor, uint8_t, iEffSeg, RTGCPTR, GCPtrEff, IEMMODE, enmEffOpSize)
|
---|
8618 | {
|
---|
8619 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX);
|
---|
8620 |
|
---|
8621 | /*
|
---|
8622 | * Raise exceptions.
|
---|
8623 | */
|
---|
8624 | if (pVCpu->cpum.GstCtx.cr0 & (X86_CR0_TS | X86_CR0_EM))
|
---|
8625 | return iemRaiseDeviceNotAvailable(pVCpu);
|
---|
8626 |
|
---|
8627 | /*
|
---|
8628 | * Access the memory.
|
---|
8629 | */
|
---|
8630 | void *pvMem512;
|
---|
8631 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, &pvMem512, 512, iEffSeg, GCPtrEff, IEM_ACCESS_DATA_R,
|
---|
8632 | 15 | IEM_MEMMAP_F_ALIGN_GP | IEM_MEMMAP_F_ALIGN_GP_OR_AC);
|
---|
8633 | if (rcStrict != VINF_SUCCESS)
|
---|
8634 | return rcStrict;
|
---|
8635 | PCX86FXSTATE pSrc = (PCX86FXSTATE)pvMem512;
|
---|
8636 | PX86FXSTATE pDst = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
8637 |
|
---|
8638 | /*
|
---|
8639 | * Check the state for stuff which will #GP(0).
|
---|
8640 | */
|
---|
8641 | uint32_t const fMXCSR = pSrc->MXCSR;
|
---|
8642 | uint32_t const fMXCSR_MASK = CPUMGetGuestMxCsrMask(pVCpu->CTX_SUFF(pVM));
|
---|
8643 | if (fMXCSR & ~fMXCSR_MASK)
|
---|
8644 | {
|
---|
8645 | Log(("fxrstor: MXCSR=%#x (MXCSR_MASK=%#x) -> #GP(0)\n", fMXCSR, fMXCSR_MASK));
|
---|
8646 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
8647 | }
|
---|
8648 |
|
---|
8649 | /*
|
---|
8650 | * Load the registers.
|
---|
8651 | */
|
---|
8652 | /** @todo CPU/VM detection possible! If CR4.OSFXSR=0 MXCSR it's
|
---|
8653 | * implementation specific whether MXCSR and XMM0-XMM7 are
|
---|
8654 | * restored according to Intel.
|
---|
8655 | * AMD says MXCSR and XMM registers are never loaded if
|
---|
8656 | * CR4.OSFXSR=0.
|
---|
8657 | */
|
---|
8658 |
|
---|
8659 | /* common for all formats */
|
---|
8660 | pDst->FCW = pSrc->FCW;
|
---|
8661 | pDst->FSW = pSrc->FSW;
|
---|
8662 | pDst->FTW = pSrc->FTW & UINT16_C(0xff);
|
---|
8663 | pDst->FOP = pSrc->FOP;
|
---|
8664 | pDst->MXCSR = fMXCSR;
|
---|
8665 | /* (MXCSR_MASK is read-only) */
|
---|
8666 | for (uint32_t i = 0; i < RT_ELEMENTS(pSrc->aRegs); i++)
|
---|
8667 | {
|
---|
8668 | pDst->aRegs[i].au32[0] = pSrc->aRegs[i].au32[0];
|
---|
8669 | pDst->aRegs[i].au32[1] = pSrc->aRegs[i].au32[1];
|
---|
8670 | pDst->aRegs[i].au32[2] = pSrc->aRegs[i].au32[2] & UINT32_C(0xffff);
|
---|
8671 | pDst->aRegs[i].au32[3] = 0;
|
---|
8672 | }
|
---|
8673 |
|
---|
8674 | /* FPU IP, CS, DP and DS. */
|
---|
8675 | /** @todo AMD says this is only done if FSW.ES is set after loading. */
|
---|
8676 | if (enmEffOpSize == IEMMODE_64BIT)
|
---|
8677 | {
|
---|
8678 | pDst->FPUIP = pSrc->FPUIP;
|
---|
8679 | pDst->CS = pSrc->CS;
|
---|
8680 | pDst->Rsrvd1 = pSrc->Rsrvd1;
|
---|
8681 | pDst->FPUDP = pSrc->FPUDP;
|
---|
8682 | pDst->DS = pSrc->DS;
|
---|
8683 | pDst->Rsrvd2 = pSrc->Rsrvd2;
|
---|
8684 | }
|
---|
8685 | else
|
---|
8686 | {
|
---|
8687 | pDst->FPUIP = pSrc->FPUIP;
|
---|
8688 | pDst->CS = pSrc->CS;
|
---|
8689 | pDst->Rsrvd1 = 0;
|
---|
8690 | pDst->FPUDP = pSrc->FPUDP;
|
---|
8691 | pDst->DS = pSrc->DS;
|
---|
8692 | pDst->Rsrvd2 = 0;
|
---|
8693 | }
|
---|
8694 |
|
---|
8695 | /* XMM registers. Skipped in 64-bit CPL0 if EFER.FFXSR (AMD only) is set.
|
---|
8696 | * Does not affect MXCSR, only registers.
|
---|
8697 | */
|
---|
8698 | if ( !(pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_FFXSR)
|
---|
8699 | || pVCpu->iem.s.enmCpuMode != IEMMODE_64BIT
|
---|
8700 | || pVCpu->iem.s.uCpl != 0)
|
---|
8701 | {
|
---|
8702 | uint32_t cXmmRegs = pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT ? 16 : 8;
|
---|
8703 | for (uint32_t i = 0; i < cXmmRegs; i++)
|
---|
8704 | pDst->aXMM[i] = pSrc->aXMM[i];
|
---|
8705 | }
|
---|
8706 |
|
---|
8707 | pDst->FCW &= ~X86_FCW_ZERO_MASK;
|
---|
8708 | iemFpuRecalcExceptionStatus(pDst);
|
---|
8709 |
|
---|
8710 | if (pDst->FSW & X86_FSW_ES)
|
---|
8711 | Log11(("fxrstor: %04x:%08RX64: loading state with pending FPU exception (FSW=%#x)\n",
|
---|
8712 | pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip, pSrc->FSW));
|
---|
8713 |
|
---|
8714 | /*
|
---|
8715 | * Unmap the memory.
|
---|
8716 | */
|
---|
8717 | rcStrict = iemMemCommitAndUnmap(pVCpu, pvMem512, IEM_ACCESS_DATA_R);
|
---|
8718 | if (rcStrict != VINF_SUCCESS)
|
---|
8719 | return rcStrict;
|
---|
8720 |
|
---|
8721 | iemHlpUsedFpu(pVCpu);
|
---|
8722 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8723 | }
|
---|
8724 |
|
---|
8725 |
|
---|
8726 | /**
|
---|
8727 | * Implements 'XSAVE'.
|
---|
8728 | *
|
---|
8729 | * @param iEffSeg The effective segment.
|
---|
8730 | * @param GCPtrEff The address of the image.
|
---|
8731 | * @param enmEffOpSize The operand size (only REX.W really matters).
|
---|
8732 | */
|
---|
8733 | IEM_CIMPL_DEF_3(iemCImpl_xsave, uint8_t, iEffSeg, RTGCPTR, GCPtrEff, IEMMODE, enmEffOpSize)
|
---|
8734 | {
|
---|
8735 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX | CPUMCTX_EXTRN_OTHER_XSAVE | CPUMCTX_EXTRN_XCRx);
|
---|
8736 |
|
---|
8737 | /*
|
---|
8738 | * Raise exceptions.
|
---|
8739 | */
|
---|
8740 | if (!(pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSXSAVE))
|
---|
8741 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
8742 | /* When in VMX non-root mode and XSAVE/XRSTOR is not enabled, it results in #UD. */
|
---|
8743 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
8744 | && !IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_XSAVES_XRSTORS))
|
---|
8745 | {
|
---|
8746 | Log(("xrstor: Not enabled for nested-guest execution -> #UD\n"));
|
---|
8747 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
8748 | }
|
---|
8749 | if (pVCpu->cpum.GstCtx.cr0 & X86_CR0_TS)
|
---|
8750 | return iemRaiseDeviceNotAvailable(pVCpu);
|
---|
8751 |
|
---|
8752 | /*
|
---|
8753 | * Calc the requested mask.
|
---|
8754 | */
|
---|
8755 | uint64_t const fReqComponents = RT_MAKE_U64(pVCpu->cpum.GstCtx.eax, pVCpu->cpum.GstCtx.edx) & pVCpu->cpum.GstCtx.aXcr[0];
|
---|
8756 | AssertLogRelReturn(!(fReqComponents & ~(XSAVE_C_X87 | XSAVE_C_SSE | XSAVE_C_YMM)), VERR_IEM_ASPECT_NOT_IMPLEMENTED);
|
---|
8757 | uint64_t const fXInUse = pVCpu->cpum.GstCtx.aXcr[0];
|
---|
8758 |
|
---|
8759 | /** @todo figure out the exact protocol for the memory access. Currently we
|
---|
8760 | * just need this crap to work halfways to make it possible to test
|
---|
8761 | * AVX instructions. */
|
---|
8762 | /** @todo figure out the XINUSE and XMODIFIED */
|
---|
8763 |
|
---|
8764 | /*
|
---|
8765 | * Access the x87 memory state.
|
---|
8766 | */
|
---|
8767 | /* The x87+SSE state. */
|
---|
8768 | void *pvMem512;
|
---|
8769 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, &pvMem512, 512, iEffSeg, GCPtrEff, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE,
|
---|
8770 | 63 | IEM_MEMMAP_F_ALIGN_GP | IEM_MEMMAP_F_ALIGN_GP_OR_AC);
|
---|
8771 | if (rcStrict != VINF_SUCCESS)
|
---|
8772 | return rcStrict;
|
---|
8773 | PX86FXSTATE pDst = (PX86FXSTATE)pvMem512;
|
---|
8774 | PCX86FXSTATE pSrc = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
8775 |
|
---|
8776 | /* The header. */
|
---|
8777 | PX86XSAVEHDR pHdr;
|
---|
8778 | rcStrict = iemMemMap(pVCpu, (void **)&pHdr, sizeof(&pHdr), iEffSeg, GCPtrEff + 512, IEM_ACCESS_DATA_RW, 0 /* checked above */);
|
---|
8779 | if (rcStrict != VINF_SUCCESS)
|
---|
8780 | return rcStrict;
|
---|
8781 |
|
---|
8782 | /*
|
---|
8783 | * Store the X87 state.
|
---|
8784 | */
|
---|
8785 | if (fReqComponents & XSAVE_C_X87)
|
---|
8786 | {
|
---|
8787 | /* common for all formats */
|
---|
8788 | pDst->FCW = pSrc->FCW;
|
---|
8789 | pDst->FSW = pSrc->FSW;
|
---|
8790 | pDst->FTW = pSrc->FTW & UINT16_C(0xff);
|
---|
8791 | pDst->FOP = pSrc->FOP;
|
---|
8792 | pDst->FPUIP = pSrc->FPUIP;
|
---|
8793 | pDst->CS = pSrc->CS;
|
---|
8794 | pDst->FPUDP = pSrc->FPUDP;
|
---|
8795 | pDst->DS = pSrc->DS;
|
---|
8796 | if (enmEffOpSize == IEMMODE_64BIT)
|
---|
8797 | {
|
---|
8798 | /* Save upper 16-bits of FPUIP (IP:CS:Rsvd1) and FPUDP (DP:DS:Rsvd2). */
|
---|
8799 | pDst->Rsrvd1 = pSrc->Rsrvd1;
|
---|
8800 | pDst->Rsrvd2 = pSrc->Rsrvd2;
|
---|
8801 | }
|
---|
8802 | else
|
---|
8803 | {
|
---|
8804 | pDst->Rsrvd1 = 0;
|
---|
8805 | pDst->Rsrvd2 = 0;
|
---|
8806 | }
|
---|
8807 | for (uint32_t i = 0; i < RT_ELEMENTS(pDst->aRegs); i++)
|
---|
8808 | {
|
---|
8809 | /** @todo Testcase: What actually happens to the 6 reserved bytes? I'm clearing
|
---|
8810 | * them for now... */
|
---|
8811 | pDst->aRegs[i].au32[0] = pSrc->aRegs[i].au32[0];
|
---|
8812 | pDst->aRegs[i].au32[1] = pSrc->aRegs[i].au32[1];
|
---|
8813 | pDst->aRegs[i].au32[2] = pSrc->aRegs[i].au32[2] & UINT32_C(0xffff);
|
---|
8814 | pDst->aRegs[i].au32[3] = 0;
|
---|
8815 | }
|
---|
8816 |
|
---|
8817 | }
|
---|
8818 |
|
---|
8819 | if (fReqComponents & (XSAVE_C_SSE | XSAVE_C_YMM))
|
---|
8820 | {
|
---|
8821 | pDst->MXCSR = pSrc->MXCSR;
|
---|
8822 | pDst->MXCSR_MASK = CPUMGetGuestMxCsrMask(pVCpu->CTX_SUFF(pVM));
|
---|
8823 | }
|
---|
8824 |
|
---|
8825 | if (fReqComponents & XSAVE_C_SSE)
|
---|
8826 | {
|
---|
8827 | /* XMM registers. */
|
---|
8828 | uint32_t cXmmRegs = enmEffOpSize == IEMMODE_64BIT ? 16 : 8;
|
---|
8829 | for (uint32_t i = 0; i < cXmmRegs; i++)
|
---|
8830 | pDst->aXMM[i] = pSrc->aXMM[i];
|
---|
8831 | /** @todo Testcase: What happens to the reserved XMM registers? Untouched,
|
---|
8832 | * right? */
|
---|
8833 | }
|
---|
8834 |
|
---|
8835 | /* Commit the x87 state bits. (probably wrong) */
|
---|
8836 | rcStrict = iemMemCommitAndUnmap(pVCpu, pvMem512, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
|
---|
8837 | if (rcStrict != VINF_SUCCESS)
|
---|
8838 | return rcStrict;
|
---|
8839 |
|
---|
8840 | /*
|
---|
8841 | * Store AVX state.
|
---|
8842 | */
|
---|
8843 | if (fReqComponents & XSAVE_C_YMM)
|
---|
8844 | {
|
---|
8845 | /** @todo testcase: xsave64 vs xsave32 wrt XSAVE_C_YMM. */
|
---|
8846 | AssertLogRelReturn(pVCpu->cpum.GstCtx.aoffXState[XSAVE_C_YMM_BIT] != UINT16_MAX, VERR_IEM_IPE_9);
|
---|
8847 | PCX86XSAVEYMMHI pCompSrc = CPUMCTX_XSAVE_C_PTR(IEM_GET_CTX(pVCpu), XSAVE_C_YMM_BIT, PCX86XSAVEYMMHI);
|
---|
8848 | PX86XSAVEYMMHI pCompDst;
|
---|
8849 | rcStrict = iemMemMap(pVCpu, (void **)&pCompDst, sizeof(*pCompDst), iEffSeg, GCPtrEff + pVCpu->cpum.GstCtx.aoffXState[XSAVE_C_YMM_BIT],
|
---|
8850 | IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE, 0 /* checked above */);
|
---|
8851 | if (rcStrict != VINF_SUCCESS)
|
---|
8852 | return rcStrict;
|
---|
8853 |
|
---|
8854 | uint32_t cXmmRegs = enmEffOpSize == IEMMODE_64BIT ? 16 : 8;
|
---|
8855 | for (uint32_t i = 0; i < cXmmRegs; i++)
|
---|
8856 | pCompDst->aYmmHi[i] = pCompSrc->aYmmHi[i];
|
---|
8857 |
|
---|
8858 | rcStrict = iemMemCommitAndUnmap(pVCpu, pCompDst, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
|
---|
8859 | if (rcStrict != VINF_SUCCESS)
|
---|
8860 | return rcStrict;
|
---|
8861 | }
|
---|
8862 |
|
---|
8863 | /*
|
---|
8864 | * Update the header.
|
---|
8865 | */
|
---|
8866 | pHdr->bmXState = (pHdr->bmXState & ~fReqComponents)
|
---|
8867 | | (fReqComponents & fXInUse);
|
---|
8868 |
|
---|
8869 | rcStrict = iemMemCommitAndUnmap(pVCpu, pHdr, IEM_ACCESS_DATA_RW);
|
---|
8870 | if (rcStrict != VINF_SUCCESS)
|
---|
8871 | return rcStrict;
|
---|
8872 |
|
---|
8873 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8874 | }
|
---|
8875 |
|
---|
8876 |
|
---|
8877 | /**
|
---|
8878 | * Implements 'XRSTOR'.
|
---|
8879 | *
|
---|
8880 | * @param iEffSeg The effective segment.
|
---|
8881 | * @param GCPtrEff The address of the image.
|
---|
8882 | * @param enmEffOpSize The operand size (only REX.W really matters).
|
---|
8883 | */
|
---|
8884 | IEM_CIMPL_DEF_3(iemCImpl_xrstor, uint8_t, iEffSeg, RTGCPTR, GCPtrEff, IEMMODE, enmEffOpSize)
|
---|
8885 | {
|
---|
8886 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX | CPUMCTX_EXTRN_OTHER_XSAVE | CPUMCTX_EXTRN_XCRx);
|
---|
8887 |
|
---|
8888 | /*
|
---|
8889 | * Raise exceptions.
|
---|
8890 | */
|
---|
8891 | if (!(pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSXSAVE))
|
---|
8892 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
8893 | /* When in VMX non-root mode and XSAVE/XRSTOR is not enabled, it results in #UD. */
|
---|
8894 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
8895 | && !IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_XSAVES_XRSTORS))
|
---|
8896 | {
|
---|
8897 | Log(("xrstor: Not enabled for nested-guest execution -> #UD\n"));
|
---|
8898 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
8899 | }
|
---|
8900 | if (pVCpu->cpum.GstCtx.cr0 & X86_CR0_TS)
|
---|
8901 | return iemRaiseDeviceNotAvailable(pVCpu);
|
---|
8902 | if (GCPtrEff & 63)
|
---|
8903 | {
|
---|
8904 | /** @todo CPU/VM detection possible! \#AC might not be signal for
|
---|
8905 | * all/any misalignment sizes, intel says its an implementation detail. */
|
---|
8906 | if ( (pVCpu->cpum.GstCtx.cr0 & X86_CR0_AM)
|
---|
8907 | && pVCpu->cpum.GstCtx.eflags.Bits.u1AC
|
---|
8908 | && pVCpu->iem.s.uCpl == 3)
|
---|
8909 | return iemRaiseAlignmentCheckException(pVCpu);
|
---|
8910 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
8911 | }
|
---|
8912 |
|
---|
8913 | /** @todo figure out the exact protocol for the memory access. Currently we
|
---|
8914 | * just need this crap to work halfways to make it possible to test
|
---|
8915 | * AVX instructions. */
|
---|
8916 | /** @todo figure out the XINUSE and XMODIFIED */
|
---|
8917 |
|
---|
8918 | /*
|
---|
8919 | * Access the x87 memory state.
|
---|
8920 | */
|
---|
8921 | /* The x87+SSE state. */
|
---|
8922 | void *pvMem512;
|
---|
8923 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, &pvMem512, 512, iEffSeg, GCPtrEff, IEM_ACCESS_DATA_R,
|
---|
8924 | 63 | IEM_MEMMAP_F_ALIGN_GP | IEM_MEMMAP_F_ALIGN_GP_OR_AC);
|
---|
8925 | if (rcStrict != VINF_SUCCESS)
|
---|
8926 | return rcStrict;
|
---|
8927 | PCX86FXSTATE pSrc = (PCX86FXSTATE)pvMem512;
|
---|
8928 | PX86FXSTATE pDst = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
8929 |
|
---|
8930 | /*
|
---|
8931 | * Calc the requested mask
|
---|
8932 | */
|
---|
8933 | PX86XSAVEHDR pHdrDst = &pVCpu->cpum.GstCtx.XState.Hdr;
|
---|
8934 | PCX86XSAVEHDR pHdrSrc;
|
---|
8935 | rcStrict = iemMemMap(pVCpu, (void **)&pHdrSrc, sizeof(&pHdrSrc), iEffSeg, GCPtrEff + 512,
|
---|
8936 | IEM_ACCESS_DATA_R, 0 /* checked above */);
|
---|
8937 | if (rcStrict != VINF_SUCCESS)
|
---|
8938 | return rcStrict;
|
---|
8939 |
|
---|
8940 | uint64_t const fReqComponents = RT_MAKE_U64(pVCpu->cpum.GstCtx.eax, pVCpu->cpum.GstCtx.edx) & pVCpu->cpum.GstCtx.aXcr[0];
|
---|
8941 | AssertLogRelReturn(!(fReqComponents & ~(XSAVE_C_X87 | XSAVE_C_SSE | XSAVE_C_YMM)), VERR_IEM_ASPECT_NOT_IMPLEMENTED);
|
---|
8942 | //uint64_t const fXInUse = pVCpu->cpum.GstCtx.aXcr[0];
|
---|
8943 | uint64_t const fRstorMask = pHdrSrc->bmXState;
|
---|
8944 | uint64_t const fCompMask = pHdrSrc->bmXComp;
|
---|
8945 |
|
---|
8946 | AssertLogRelReturn(!(fCompMask & XSAVE_C_X), VERR_IEM_ASPECT_NOT_IMPLEMENTED);
|
---|
8947 |
|
---|
8948 | uint32_t const cXmmRegs = enmEffOpSize == IEMMODE_64BIT ? 16 : 8;
|
---|
8949 |
|
---|
8950 | /* We won't need this any longer. */
|
---|
8951 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)pHdrSrc, IEM_ACCESS_DATA_R);
|
---|
8952 | if (rcStrict != VINF_SUCCESS)
|
---|
8953 | return rcStrict;
|
---|
8954 |
|
---|
8955 | /*
|
---|
8956 | * Load the X87 state.
|
---|
8957 | */
|
---|
8958 | if (fReqComponents & XSAVE_C_X87)
|
---|
8959 | {
|
---|
8960 | if (fRstorMask & XSAVE_C_X87)
|
---|
8961 | {
|
---|
8962 | pDst->FCW = pSrc->FCW;
|
---|
8963 | pDst->FSW = pSrc->FSW;
|
---|
8964 | pDst->FTW = pSrc->FTW & UINT16_C(0xff);
|
---|
8965 | pDst->FOP = pSrc->FOP;
|
---|
8966 | pDst->FPUIP = pSrc->FPUIP;
|
---|
8967 | pDst->CS = pSrc->CS;
|
---|
8968 | pDst->FPUDP = pSrc->FPUDP;
|
---|
8969 | pDst->DS = pSrc->DS;
|
---|
8970 | if (enmEffOpSize == IEMMODE_64BIT)
|
---|
8971 | {
|
---|
8972 | /* Load upper 16-bits of FPUIP (IP:CS:Rsvd1) and FPUDP (DP:DS:Rsvd2). */
|
---|
8973 | pDst->Rsrvd1 = pSrc->Rsrvd1;
|
---|
8974 | pDst->Rsrvd2 = pSrc->Rsrvd2;
|
---|
8975 | }
|
---|
8976 | else
|
---|
8977 | {
|
---|
8978 | pDst->Rsrvd1 = 0;
|
---|
8979 | pDst->Rsrvd2 = 0;
|
---|
8980 | }
|
---|
8981 | for (uint32_t i = 0; i < RT_ELEMENTS(pDst->aRegs); i++)
|
---|
8982 | {
|
---|
8983 | pDst->aRegs[i].au32[0] = pSrc->aRegs[i].au32[0];
|
---|
8984 | pDst->aRegs[i].au32[1] = pSrc->aRegs[i].au32[1];
|
---|
8985 | pDst->aRegs[i].au32[2] = pSrc->aRegs[i].au32[2] & UINT32_C(0xffff);
|
---|
8986 | pDst->aRegs[i].au32[3] = 0;
|
---|
8987 | }
|
---|
8988 |
|
---|
8989 | pDst->FCW &= ~X86_FCW_ZERO_MASK;
|
---|
8990 | iemFpuRecalcExceptionStatus(pDst);
|
---|
8991 |
|
---|
8992 | if (pDst->FSW & X86_FSW_ES)
|
---|
8993 | Log11(("xrstor: %04x:%08RX64: loading state with pending FPU exception (FSW=%#x)\n",
|
---|
8994 | pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip, pSrc->FSW));
|
---|
8995 | }
|
---|
8996 | else
|
---|
8997 | {
|
---|
8998 | pDst->FCW = 0x37f;
|
---|
8999 | pDst->FSW = 0;
|
---|
9000 | pDst->FTW = 0x00; /* 0 - empty. */
|
---|
9001 | pDst->FPUDP = 0;
|
---|
9002 | pDst->DS = 0; //??
|
---|
9003 | pDst->Rsrvd2= 0;
|
---|
9004 | pDst->FPUIP = 0;
|
---|
9005 | pDst->CS = 0; //??
|
---|
9006 | pDst->Rsrvd1= 0;
|
---|
9007 | pDst->FOP = 0;
|
---|
9008 | for (uint32_t i = 0; i < RT_ELEMENTS(pSrc->aRegs); i++)
|
---|
9009 | {
|
---|
9010 | pDst->aRegs[i].au32[0] = 0;
|
---|
9011 | pDst->aRegs[i].au32[1] = 0;
|
---|
9012 | pDst->aRegs[i].au32[2] = 0;
|
---|
9013 | pDst->aRegs[i].au32[3] = 0;
|
---|
9014 | }
|
---|
9015 | }
|
---|
9016 | pHdrDst->bmXState |= XSAVE_C_X87; /* playing safe for now */
|
---|
9017 | }
|
---|
9018 |
|
---|
9019 | /* MXCSR */
|
---|
9020 | if (fReqComponents & (XSAVE_C_SSE | XSAVE_C_YMM))
|
---|
9021 | {
|
---|
9022 | if (fRstorMask & (XSAVE_C_SSE | XSAVE_C_YMM))
|
---|
9023 | pDst->MXCSR = pSrc->MXCSR;
|
---|
9024 | else
|
---|
9025 | pDst->MXCSR = 0x1f80;
|
---|
9026 | }
|
---|
9027 |
|
---|
9028 | /* XMM registers. */
|
---|
9029 | if (fReqComponents & XSAVE_C_SSE)
|
---|
9030 | {
|
---|
9031 | if (fRstorMask & XSAVE_C_SSE)
|
---|
9032 | {
|
---|
9033 | for (uint32_t i = 0; i < cXmmRegs; i++)
|
---|
9034 | pDst->aXMM[i] = pSrc->aXMM[i];
|
---|
9035 | /** @todo Testcase: What happens to the reserved XMM registers? Untouched,
|
---|
9036 | * right? */
|
---|
9037 | }
|
---|
9038 | else
|
---|
9039 | {
|
---|
9040 | for (uint32_t i = 0; i < cXmmRegs; i++)
|
---|
9041 | {
|
---|
9042 | pDst->aXMM[i].au64[0] = 0;
|
---|
9043 | pDst->aXMM[i].au64[1] = 0;
|
---|
9044 | }
|
---|
9045 | }
|
---|
9046 | pHdrDst->bmXState |= XSAVE_C_SSE; /* playing safe for now */
|
---|
9047 | }
|
---|
9048 |
|
---|
9049 | /* Unmap the x87 state bits (so we've don't run out of mapping). */
|
---|
9050 | rcStrict = iemMemCommitAndUnmap(pVCpu, pvMem512, IEM_ACCESS_DATA_R);
|
---|
9051 | if (rcStrict != VINF_SUCCESS)
|
---|
9052 | return rcStrict;
|
---|
9053 |
|
---|
9054 | /*
|
---|
9055 | * Restore AVX state.
|
---|
9056 | */
|
---|
9057 | if (fReqComponents & XSAVE_C_YMM)
|
---|
9058 | {
|
---|
9059 | AssertLogRelReturn(pVCpu->cpum.GstCtx.aoffXState[XSAVE_C_YMM_BIT] != UINT16_MAX, VERR_IEM_IPE_9);
|
---|
9060 | PX86XSAVEYMMHI pCompDst = CPUMCTX_XSAVE_C_PTR(IEM_GET_CTX(pVCpu), XSAVE_C_YMM_BIT, PX86XSAVEYMMHI);
|
---|
9061 |
|
---|
9062 | if (fRstorMask & XSAVE_C_YMM)
|
---|
9063 | {
|
---|
9064 | /** @todo testcase: xsave64 vs xsave32 wrt XSAVE_C_YMM. */
|
---|
9065 | PCX86XSAVEYMMHI pCompSrc;
|
---|
9066 | rcStrict = iemMemMap(pVCpu, (void **)&pCompSrc, sizeof(*pCompDst),
|
---|
9067 | iEffSeg, GCPtrEff + pVCpu->cpum.GstCtx.aoffXState[XSAVE_C_YMM_BIT],
|
---|
9068 | IEM_ACCESS_DATA_R, 0 /* checked above */);
|
---|
9069 | if (rcStrict != VINF_SUCCESS)
|
---|
9070 | return rcStrict;
|
---|
9071 |
|
---|
9072 | for (uint32_t i = 0; i < cXmmRegs; i++)
|
---|
9073 | {
|
---|
9074 | pCompDst->aYmmHi[i].au64[0] = pCompSrc->aYmmHi[i].au64[0];
|
---|
9075 | pCompDst->aYmmHi[i].au64[1] = pCompSrc->aYmmHi[i].au64[1];
|
---|
9076 | }
|
---|
9077 |
|
---|
9078 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)pCompSrc, IEM_ACCESS_DATA_R);
|
---|
9079 | if (rcStrict != VINF_SUCCESS)
|
---|
9080 | return rcStrict;
|
---|
9081 | }
|
---|
9082 | else
|
---|
9083 | {
|
---|
9084 | for (uint32_t i = 0; i < cXmmRegs; i++)
|
---|
9085 | {
|
---|
9086 | pCompDst->aYmmHi[i].au64[0] = 0;
|
---|
9087 | pCompDst->aYmmHi[i].au64[1] = 0;
|
---|
9088 | }
|
---|
9089 | }
|
---|
9090 | pHdrDst->bmXState |= XSAVE_C_YMM; /* playing safe for now */
|
---|
9091 | }
|
---|
9092 |
|
---|
9093 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
9094 | }
|
---|
9095 |
|
---|
9096 |
|
---|
9097 |
|
---|
9098 |
|
---|
9099 | /**
|
---|
9100 | * Implements 'STMXCSR'.
|
---|
9101 | *
|
---|
9102 | * @param iEffSeg The effective segment register for @a GCPtrEff.
|
---|
9103 | * @param GCPtrEff The address of the image.
|
---|
9104 | */
|
---|
9105 | IEM_CIMPL_DEF_2(iemCImpl_stmxcsr, uint8_t, iEffSeg, RTGCPTR, GCPtrEff)
|
---|
9106 | {
|
---|
9107 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX);
|
---|
9108 |
|
---|
9109 | /*
|
---|
9110 | * Raise exceptions.
|
---|
9111 | */
|
---|
9112 | if ( !(pVCpu->cpum.GstCtx.cr0 & X86_CR0_EM)
|
---|
9113 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSFXSR))
|
---|
9114 | {
|
---|
9115 | if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_TS))
|
---|
9116 | {
|
---|
9117 | /*
|
---|
9118 | * Do the job.
|
---|
9119 | */
|
---|
9120 | VBOXSTRICTRC rcStrict = iemMemStoreDataU32(pVCpu, iEffSeg, GCPtrEff, pVCpu->cpum.GstCtx.XState.x87.MXCSR);
|
---|
9121 | if (rcStrict == VINF_SUCCESS)
|
---|
9122 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
9123 | return rcStrict;
|
---|
9124 | }
|
---|
9125 | return iemRaiseDeviceNotAvailable(pVCpu);
|
---|
9126 | }
|
---|
9127 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
9128 | }
|
---|
9129 |
|
---|
9130 |
|
---|
9131 | /**
|
---|
9132 | * Implements 'VSTMXCSR'.
|
---|
9133 | *
|
---|
9134 | * @param iEffSeg The effective segment register for @a GCPtrEff.
|
---|
9135 | * @param GCPtrEff The address of the image.
|
---|
9136 | */
|
---|
9137 | IEM_CIMPL_DEF_2(iemCImpl_vstmxcsr, uint8_t, iEffSeg, RTGCPTR, GCPtrEff)
|
---|
9138 | {
|
---|
9139 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX | CPUMCTX_EXTRN_XCRx);
|
---|
9140 |
|
---|
9141 | /*
|
---|
9142 | * Raise exceptions.
|
---|
9143 | */
|
---|
9144 | if ( ( !IEM_IS_GUEST_CPU_AMD(pVCpu)
|
---|
9145 | ? (pVCpu->cpum.GstCtx.aXcr[0] & (XSAVE_C_SSE | XSAVE_C_YMM)) == (XSAVE_C_SSE | XSAVE_C_YMM)
|
---|
9146 | : !(pVCpu->cpum.GstCtx.cr0 & X86_CR0_EM)) /* AMD Jaguar CPU (f0x16,m0,s1) behaviour */
|
---|
9147 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSXSAVE))
|
---|
9148 | {
|
---|
9149 | if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_TS))
|
---|
9150 | {
|
---|
9151 | /*
|
---|
9152 | * Do the job.
|
---|
9153 | */
|
---|
9154 | VBOXSTRICTRC rcStrict = iemMemStoreDataU32(pVCpu, iEffSeg, GCPtrEff, pVCpu->cpum.GstCtx.XState.x87.MXCSR);
|
---|
9155 | if (rcStrict == VINF_SUCCESS)
|
---|
9156 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
9157 | return rcStrict;
|
---|
9158 | }
|
---|
9159 | return iemRaiseDeviceNotAvailable(pVCpu);
|
---|
9160 | }
|
---|
9161 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
9162 | }
|
---|
9163 |
|
---|
9164 |
|
---|
9165 | /**
|
---|
9166 | * Implements 'LDMXCSR'.
|
---|
9167 | *
|
---|
9168 | * @param iEffSeg The effective segment register for @a GCPtrEff.
|
---|
9169 | * @param GCPtrEff The address of the image.
|
---|
9170 | */
|
---|
9171 | IEM_CIMPL_DEF_2(iemCImpl_ldmxcsr, uint8_t, iEffSeg, RTGCPTR, GCPtrEff)
|
---|
9172 | {
|
---|
9173 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX);
|
---|
9174 |
|
---|
9175 | /*
|
---|
9176 | * Raise exceptions.
|
---|
9177 | */
|
---|
9178 | /** @todo testcase - order of LDMXCSR faults. Does \#PF, \#GP and \#SS
|
---|
9179 | * happen after or before \#UD and \#EM? */
|
---|
9180 | if ( !(pVCpu->cpum.GstCtx.cr0 & X86_CR0_EM)
|
---|
9181 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSFXSR))
|
---|
9182 | {
|
---|
9183 | if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_TS))
|
---|
9184 | {
|
---|
9185 | /*
|
---|
9186 | * Do the job.
|
---|
9187 | */
|
---|
9188 | uint32_t fNewMxCsr;
|
---|
9189 | VBOXSTRICTRC rcStrict = iemMemFetchDataU32(pVCpu, &fNewMxCsr, iEffSeg, GCPtrEff);
|
---|
9190 | if (rcStrict == VINF_SUCCESS)
|
---|
9191 | {
|
---|
9192 | uint32_t const fMxCsrMask = CPUMGetGuestMxCsrMask(pVCpu->CTX_SUFF(pVM));
|
---|
9193 | if (!(fNewMxCsr & ~fMxCsrMask))
|
---|
9194 | {
|
---|
9195 | pVCpu->cpum.GstCtx.XState.x87.MXCSR = fNewMxCsr;
|
---|
9196 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
9197 | }
|
---|
9198 | Log(("ldmxcsr: New MXCSR=%#RX32 & ~MASK=%#RX32 = %#RX32 -> #GP(0)\n",
|
---|
9199 | fNewMxCsr, fMxCsrMask, fNewMxCsr & ~fMxCsrMask));
|
---|
9200 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
9201 | }
|
---|
9202 | return rcStrict;
|
---|
9203 | }
|
---|
9204 | return iemRaiseDeviceNotAvailable(pVCpu);
|
---|
9205 | }
|
---|
9206 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
9207 | }
|
---|
9208 |
|
---|
9209 |
|
---|
9210 | /**
|
---|
9211 | * Commmon routine for fnstenv and fnsave.
|
---|
9212 | *
|
---|
9213 | * @param pVCpu The cross context virtual CPU structure of the calling thread.
|
---|
9214 | * @param enmEffOpSize The effective operand size.
|
---|
9215 | * @param uPtr Where to store the state.
|
---|
9216 | */
|
---|
9217 | static void iemCImplCommonFpuStoreEnv(PVMCPUCC pVCpu, IEMMODE enmEffOpSize, RTPTRUNION uPtr)
|
---|
9218 | {
|
---|
9219 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87);
|
---|
9220 | PCX86FXSTATE pSrcX87 = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
9221 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
9222 | {
|
---|
9223 | uPtr.pu16[0] = pSrcX87->FCW;
|
---|
9224 | uPtr.pu16[1] = pSrcX87->FSW;
|
---|
9225 | uPtr.pu16[2] = iemFpuCalcFullFtw(pSrcX87);
|
---|
9226 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
9227 | {
|
---|
9228 | /** @todo Testcase: How does this work when the FPUIP/CS was saved in
|
---|
9229 | * protected mode or long mode and we save it in real mode? And vice
|
---|
9230 | * versa? And with 32-bit operand size? I think CPU is storing the
|
---|
9231 | * effective address ((CS << 4) + IP) in the offset register and not
|
---|
9232 | * doing any address calculations here. */
|
---|
9233 | uPtr.pu16[3] = (uint16_t)pSrcX87->FPUIP;
|
---|
9234 | uPtr.pu16[4] = ((pSrcX87->FPUIP >> 4) & UINT16_C(0xf000)) | pSrcX87->FOP;
|
---|
9235 | uPtr.pu16[5] = (uint16_t)pSrcX87->FPUDP;
|
---|
9236 | uPtr.pu16[6] = (pSrcX87->FPUDP >> 4) & UINT16_C(0xf000);
|
---|
9237 | }
|
---|
9238 | else
|
---|
9239 | {
|
---|
9240 | uPtr.pu16[3] = pSrcX87->FPUIP;
|
---|
9241 | uPtr.pu16[4] = pSrcX87->CS;
|
---|
9242 | uPtr.pu16[5] = pSrcX87->FPUDP;
|
---|
9243 | uPtr.pu16[6] = pSrcX87->DS;
|
---|
9244 | }
|
---|
9245 | }
|
---|
9246 | else
|
---|
9247 | {
|
---|
9248 | /** @todo Testcase: what is stored in the "gray" areas? (figure 8-9 and 8-10) */
|
---|
9249 | uPtr.pu16[0*2] = pSrcX87->FCW;
|
---|
9250 | uPtr.pu16[0*2+1] = 0xffff; /* (0xffff observed on intel skylake.) */
|
---|
9251 | uPtr.pu16[1*2] = pSrcX87->FSW;
|
---|
9252 | uPtr.pu16[1*2+1] = 0xffff;
|
---|
9253 | uPtr.pu16[2*2] = iemFpuCalcFullFtw(pSrcX87);
|
---|
9254 | uPtr.pu16[2*2+1] = 0xffff;
|
---|
9255 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
9256 | {
|
---|
9257 | uPtr.pu16[3*2] = (uint16_t)pSrcX87->FPUIP;
|
---|
9258 | uPtr.pu32[4] = ((pSrcX87->FPUIP & UINT32_C(0xffff0000)) >> 4) | pSrcX87->FOP;
|
---|
9259 | uPtr.pu16[5*2] = (uint16_t)pSrcX87->FPUDP;
|
---|
9260 | uPtr.pu32[6] = (pSrcX87->FPUDP & UINT32_C(0xffff0000)) >> 4;
|
---|
9261 | }
|
---|
9262 | else
|
---|
9263 | {
|
---|
9264 | uPtr.pu32[3] = pSrcX87->FPUIP;
|
---|
9265 | uPtr.pu16[4*2] = pSrcX87->CS;
|
---|
9266 | uPtr.pu16[4*2+1] = pSrcX87->FOP;
|
---|
9267 | uPtr.pu32[5] = pSrcX87->FPUDP;
|
---|
9268 | uPtr.pu16[6*2] = pSrcX87->DS;
|
---|
9269 | uPtr.pu16[6*2+1] = 0xffff;
|
---|
9270 | }
|
---|
9271 | }
|
---|
9272 | }
|
---|
9273 |
|
---|
9274 |
|
---|
9275 | /**
|
---|
9276 | * Commmon routine for fldenv and frstor
|
---|
9277 | *
|
---|
9278 | * @param pVCpu The cross context virtual CPU structure of the calling thread.
|
---|
9279 | * @param enmEffOpSize The effective operand size.
|
---|
9280 | * @param uPtr Where to store the state.
|
---|
9281 | */
|
---|
9282 | static void iemCImplCommonFpuRestoreEnv(PVMCPUCC pVCpu, IEMMODE enmEffOpSize, RTCPTRUNION uPtr)
|
---|
9283 | {
|
---|
9284 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87);
|
---|
9285 | PX86FXSTATE pDstX87 = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
9286 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
9287 | {
|
---|
9288 | pDstX87->FCW = uPtr.pu16[0];
|
---|
9289 | pDstX87->FSW = uPtr.pu16[1];
|
---|
9290 | pDstX87->FTW = uPtr.pu16[2];
|
---|
9291 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
9292 | {
|
---|
9293 | pDstX87->FPUIP = uPtr.pu16[3] | ((uint32_t)(uPtr.pu16[4] & UINT16_C(0xf000)) << 4);
|
---|
9294 | pDstX87->FPUDP = uPtr.pu16[5] | ((uint32_t)(uPtr.pu16[6] & UINT16_C(0xf000)) << 4);
|
---|
9295 | pDstX87->FOP = uPtr.pu16[4] & UINT16_C(0x07ff);
|
---|
9296 | pDstX87->CS = 0;
|
---|
9297 | pDstX87->Rsrvd1= 0;
|
---|
9298 | pDstX87->DS = 0;
|
---|
9299 | pDstX87->Rsrvd2= 0;
|
---|
9300 | }
|
---|
9301 | else
|
---|
9302 | {
|
---|
9303 | pDstX87->FPUIP = uPtr.pu16[3];
|
---|
9304 | pDstX87->CS = uPtr.pu16[4];
|
---|
9305 | pDstX87->Rsrvd1= 0;
|
---|
9306 | pDstX87->FPUDP = uPtr.pu16[5];
|
---|
9307 | pDstX87->DS = uPtr.pu16[6];
|
---|
9308 | pDstX87->Rsrvd2= 0;
|
---|
9309 | /** @todo Testcase: Is FOP cleared when doing 16-bit protected mode fldenv? */
|
---|
9310 | }
|
---|
9311 | }
|
---|
9312 | else
|
---|
9313 | {
|
---|
9314 | pDstX87->FCW = uPtr.pu16[0*2];
|
---|
9315 | pDstX87->FSW = uPtr.pu16[1*2];
|
---|
9316 | pDstX87->FTW = uPtr.pu16[2*2];
|
---|
9317 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
9318 | {
|
---|
9319 | pDstX87->FPUIP = uPtr.pu16[3*2] | ((uPtr.pu32[4] & UINT32_C(0x0ffff000)) << 4);
|
---|
9320 | pDstX87->FOP = uPtr.pu32[4] & UINT16_C(0x07ff);
|
---|
9321 | pDstX87->FPUDP = uPtr.pu16[5*2] | ((uPtr.pu32[6] & UINT32_C(0x0ffff000)) << 4);
|
---|
9322 | pDstX87->CS = 0;
|
---|
9323 | pDstX87->Rsrvd1= 0;
|
---|
9324 | pDstX87->DS = 0;
|
---|
9325 | pDstX87->Rsrvd2= 0;
|
---|
9326 | }
|
---|
9327 | else
|
---|
9328 | {
|
---|
9329 | pDstX87->FPUIP = uPtr.pu32[3];
|
---|
9330 | pDstX87->CS = uPtr.pu16[4*2];
|
---|
9331 | pDstX87->Rsrvd1= 0;
|
---|
9332 | pDstX87->FOP = uPtr.pu16[4*2+1];
|
---|
9333 | pDstX87->FPUDP = uPtr.pu32[5];
|
---|
9334 | pDstX87->DS = uPtr.pu16[6*2];
|
---|
9335 | pDstX87->Rsrvd2= 0;
|
---|
9336 | }
|
---|
9337 | }
|
---|
9338 |
|
---|
9339 | /* Make adjustments. */
|
---|
9340 | pDstX87->FTW = iemFpuCompressFtw(pDstX87->FTW);
|
---|
9341 | #ifdef LOG_ENABLED
|
---|
9342 | uint16_t const fOldFsw = pDstX87->FSW;
|
---|
9343 | #endif
|
---|
9344 | pDstX87->FCW &= ~X86_FCW_ZERO_MASK;
|
---|
9345 | iemFpuRecalcExceptionStatus(pDstX87);
|
---|
9346 | #ifdef LOG_ENABLED
|
---|
9347 | if ((pDstX87->FSW & X86_FSW_ES) ^ (fOldFsw & X86_FSW_ES))
|
---|
9348 | Log11(("iemCImplCommonFpuRestoreEnv: %04x:%08RX64: %s FPU exception (FCW=%#x FSW=%#x -> %#x)\n",
|
---|
9349 | pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip, fOldFsw & X86_FSW_ES ? "Supressed" : "Raised",
|
---|
9350 | pDstX87->FCW, fOldFsw, pDstX87->FSW));
|
---|
9351 | #endif
|
---|
9352 |
|
---|
9353 | /** @todo Testcase: Check if ES and/or B are automatically cleared if no
|
---|
9354 | * exceptions are pending after loading the saved state? */
|
---|
9355 | }
|
---|
9356 |
|
---|
9357 |
|
---|
9358 | /**
|
---|
9359 | * Implements 'FNSTENV'.
|
---|
9360 | *
|
---|
9361 | * @param enmEffOpSize The operand size (only REX.W really matters).
|
---|
9362 | * @param iEffSeg The effective segment register for @a GCPtrEffDst.
|
---|
9363 | * @param GCPtrEffDst The address of the image.
|
---|
9364 | */
|
---|
9365 | IEM_CIMPL_DEF_3(iemCImpl_fnstenv, IEMMODE, enmEffOpSize, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
|
---|
9366 | {
|
---|
9367 | RTPTRUNION uPtr;
|
---|
9368 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, &uPtr.pv, enmEffOpSize == IEMMODE_16BIT ? 14 : 28,
|
---|
9369 | iEffSeg, GCPtrEffDst, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE,
|
---|
9370 | enmEffOpSize == IEMMODE_16BIT ? 1 : 3 /** @todo ? */);
|
---|
9371 | if (rcStrict != VINF_SUCCESS)
|
---|
9372 | return rcStrict;
|
---|
9373 |
|
---|
9374 | iemCImplCommonFpuStoreEnv(pVCpu, enmEffOpSize, uPtr);
|
---|
9375 |
|
---|
9376 | rcStrict = iemMemCommitAndUnmap(pVCpu, uPtr.pv, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
|
---|
9377 | if (rcStrict != VINF_SUCCESS)
|
---|
9378 | return rcStrict;
|
---|
9379 |
|
---|
9380 | /* Mask all math exceptions. Any possibly pending exceptions will be cleared. */
|
---|
9381 | PX86FXSTATE pFpuCtx = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
9382 | pFpuCtx->FCW |= X86_FCW_XCPT_MASK;
|
---|
9383 | #ifdef LOG_ENABLED
|
---|
9384 | uint16_t fOldFsw = pFpuCtx->FSW;
|
---|
9385 | #endif
|
---|
9386 | iemFpuRecalcExceptionStatus(pFpuCtx);
|
---|
9387 | #ifdef LOG_ENABLED
|
---|
9388 | if ((pFpuCtx->FSW & X86_FSW_ES) ^ (fOldFsw & X86_FSW_ES))
|
---|
9389 | Log11(("fnstenv: %04x:%08RX64: %s FPU exception (FCW=%#x, FSW %#x -> %#x)\n", pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip,
|
---|
9390 | fOldFsw & X86_FSW_ES ? "Supressed" : "Raised", pFpuCtx->FCW, fOldFsw, pFpuCtx->FSW));
|
---|
9391 | #endif
|
---|
9392 |
|
---|
9393 | iemHlpUsedFpu(pVCpu);
|
---|
9394 |
|
---|
9395 | /* Note: C0, C1, C2 and C3 are documented as undefined, we leave them untouched! */
|
---|
9396 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
9397 | }
|
---|
9398 |
|
---|
9399 |
|
---|
9400 | /**
|
---|
9401 | * Implements 'FNSAVE'.
|
---|
9402 | *
|
---|
9403 | * @param enmEffOpSize The operand size.
|
---|
9404 | * @param iEffSeg The effective segment register for @a GCPtrEffDst.
|
---|
9405 | * @param GCPtrEffDst The address of the image.
|
---|
9406 | */
|
---|
9407 | IEM_CIMPL_DEF_3(iemCImpl_fnsave, IEMMODE, enmEffOpSize, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
|
---|
9408 | {
|
---|
9409 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87);
|
---|
9410 |
|
---|
9411 | RTPTRUNION uPtr;
|
---|
9412 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, &uPtr.pv, enmEffOpSize == IEMMODE_16BIT ? 94 : 108,
|
---|
9413 | iEffSeg, GCPtrEffDst, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE, 3 /** @todo ? */);
|
---|
9414 | if (rcStrict != VINF_SUCCESS)
|
---|
9415 | return rcStrict;
|
---|
9416 |
|
---|
9417 | PX86FXSTATE pFpuCtx = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
9418 | iemCImplCommonFpuStoreEnv(pVCpu, enmEffOpSize, uPtr);
|
---|
9419 | PRTFLOAT80U paRegs = (PRTFLOAT80U)(uPtr.pu8 + (enmEffOpSize == IEMMODE_16BIT ? 14 : 28));
|
---|
9420 | for (uint32_t i = 0; i < RT_ELEMENTS(pFpuCtx->aRegs); i++)
|
---|
9421 | {
|
---|
9422 | paRegs[i].au32[0] = pFpuCtx->aRegs[i].au32[0];
|
---|
9423 | paRegs[i].au32[1] = pFpuCtx->aRegs[i].au32[1];
|
---|
9424 | paRegs[i].au16[4] = pFpuCtx->aRegs[i].au16[4];
|
---|
9425 | }
|
---|
9426 |
|
---|
9427 | rcStrict = iemMemCommitAndUnmap(pVCpu, uPtr.pv, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
|
---|
9428 | if (rcStrict != VINF_SUCCESS)
|
---|
9429 | return rcStrict;
|
---|
9430 |
|
---|
9431 | /* Rotate the stack to account for changed TOS. */
|
---|
9432 | iemFpuRotateStackSetTop(pFpuCtx, 0);
|
---|
9433 |
|
---|
9434 | /*
|
---|
9435 | * Re-initialize the FPU context.
|
---|
9436 | */
|
---|
9437 | pFpuCtx->FCW = 0x37f;
|
---|
9438 | pFpuCtx->FSW = 0;
|
---|
9439 | pFpuCtx->FTW = 0x00; /* 0 - empty */
|
---|
9440 | pFpuCtx->FPUDP = 0;
|
---|
9441 | pFpuCtx->DS = 0;
|
---|
9442 | pFpuCtx->Rsrvd2= 0;
|
---|
9443 | pFpuCtx->FPUIP = 0;
|
---|
9444 | pFpuCtx->CS = 0;
|
---|
9445 | pFpuCtx->Rsrvd1= 0;
|
---|
9446 | pFpuCtx->FOP = 0;
|
---|
9447 |
|
---|
9448 | iemHlpUsedFpu(pVCpu);
|
---|
9449 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
9450 | }
|
---|
9451 |
|
---|
9452 |
|
---|
9453 |
|
---|
9454 | /**
|
---|
9455 | * Implements 'FLDENV'.
|
---|
9456 | *
|
---|
9457 | * @param enmEffOpSize The operand size (only REX.W really matters).
|
---|
9458 | * @param iEffSeg The effective segment register for @a GCPtrEffSrc.
|
---|
9459 | * @param GCPtrEffSrc The address of the image.
|
---|
9460 | */
|
---|
9461 | IEM_CIMPL_DEF_3(iemCImpl_fldenv, IEMMODE, enmEffOpSize, uint8_t, iEffSeg, RTGCPTR, GCPtrEffSrc)
|
---|
9462 | {
|
---|
9463 | RTCPTRUNION uPtr;
|
---|
9464 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, (void **)&uPtr.pv, enmEffOpSize == IEMMODE_16BIT ? 14 : 28,
|
---|
9465 | iEffSeg, GCPtrEffSrc, IEM_ACCESS_DATA_R,
|
---|
9466 | enmEffOpSize == IEMMODE_16BIT ? 1 : 3 /** @todo ?*/);
|
---|
9467 | if (rcStrict != VINF_SUCCESS)
|
---|
9468 | return rcStrict;
|
---|
9469 |
|
---|
9470 | iemCImplCommonFpuRestoreEnv(pVCpu, enmEffOpSize, uPtr);
|
---|
9471 |
|
---|
9472 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)uPtr.pv, IEM_ACCESS_DATA_R);
|
---|
9473 | if (rcStrict != VINF_SUCCESS)
|
---|
9474 | return rcStrict;
|
---|
9475 |
|
---|
9476 | iemHlpUsedFpu(pVCpu);
|
---|
9477 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
9478 | }
|
---|
9479 |
|
---|
9480 |
|
---|
9481 | /**
|
---|
9482 | * Implements 'FRSTOR'.
|
---|
9483 | *
|
---|
9484 | * @param enmEffOpSize The operand size.
|
---|
9485 | * @param iEffSeg The effective segment register for @a GCPtrEffSrc.
|
---|
9486 | * @param GCPtrEffSrc The address of the image.
|
---|
9487 | */
|
---|
9488 | IEM_CIMPL_DEF_3(iemCImpl_frstor, IEMMODE, enmEffOpSize, uint8_t, iEffSeg, RTGCPTR, GCPtrEffSrc)
|
---|
9489 | {
|
---|
9490 | RTCPTRUNION uPtr;
|
---|
9491 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, (void **)&uPtr.pv, enmEffOpSize == IEMMODE_16BIT ? 94 : 108,
|
---|
9492 | iEffSeg, GCPtrEffSrc, IEM_ACCESS_DATA_R, 3 /** @todo ?*/ );
|
---|
9493 | if (rcStrict != VINF_SUCCESS)
|
---|
9494 | return rcStrict;
|
---|
9495 |
|
---|
9496 | PX86FXSTATE pFpuCtx = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
9497 | iemCImplCommonFpuRestoreEnv(pVCpu, enmEffOpSize, uPtr);
|
---|
9498 | PCRTFLOAT80U paRegs = (PCRTFLOAT80U)(uPtr.pu8 + (enmEffOpSize == IEMMODE_16BIT ? 14 : 28));
|
---|
9499 | for (uint32_t i = 0; i < RT_ELEMENTS(pFpuCtx->aRegs); i++)
|
---|
9500 | {
|
---|
9501 | pFpuCtx->aRegs[i].au32[0] = paRegs[i].au32[0];
|
---|
9502 | pFpuCtx->aRegs[i].au32[1] = paRegs[i].au32[1];
|
---|
9503 | pFpuCtx->aRegs[i].au32[2] = paRegs[i].au16[4];
|
---|
9504 | pFpuCtx->aRegs[i].au32[3] = 0;
|
---|
9505 | }
|
---|
9506 |
|
---|
9507 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)uPtr.pv, IEM_ACCESS_DATA_R);
|
---|
9508 | if (rcStrict != VINF_SUCCESS)
|
---|
9509 | return rcStrict;
|
---|
9510 |
|
---|
9511 | iemHlpUsedFpu(pVCpu);
|
---|
9512 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
9513 | }
|
---|
9514 |
|
---|
9515 |
|
---|
9516 | /**
|
---|
9517 | * Implements 'FLDCW'.
|
---|
9518 | *
|
---|
9519 | * @param u16Fcw The new FCW.
|
---|
9520 | */
|
---|
9521 | IEM_CIMPL_DEF_1(iemCImpl_fldcw, uint16_t, u16Fcw)
|
---|
9522 | {
|
---|
9523 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87);
|
---|
9524 |
|
---|
9525 | /** @todo Testcase: Check what happens when trying to load X86_FCW_PC_RSVD. */
|
---|
9526 | /** @todo Testcase: Try see what happens when trying to set undefined bits
|
---|
9527 | * (other than 6 and 7). Currently ignoring them. */
|
---|
9528 | /** @todo Testcase: Test that it raises and loweres the FPU exception bits
|
---|
9529 | * according to FSW. (This is what is currently implemented.) */
|
---|
9530 | PX86FXSTATE pFpuCtx = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
9531 | pFpuCtx->FCW = u16Fcw & ~X86_FCW_ZERO_MASK;
|
---|
9532 | #ifdef LOG_ENABLED
|
---|
9533 | uint16_t fOldFsw = pFpuCtx->FSW;
|
---|
9534 | #endif
|
---|
9535 | iemFpuRecalcExceptionStatus(pFpuCtx);
|
---|
9536 | #ifdef LOG_ENABLED
|
---|
9537 | if ((pFpuCtx->FSW & X86_FSW_ES) ^ (fOldFsw & X86_FSW_ES))
|
---|
9538 | Log11(("fldcw: %04x:%08RX64: %s FPU exception (FCW=%#x, FSW %#x -> %#x)\n", pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip,
|
---|
9539 | fOldFsw & X86_FSW_ES ? "Supressed" : "Raised", pFpuCtx->FCW, fOldFsw, pFpuCtx->FSW));
|
---|
9540 | #endif
|
---|
9541 |
|
---|
9542 | /* Note: C0, C1, C2 and C3 are documented as undefined, we leave them untouched! */
|
---|
9543 | iemHlpUsedFpu(pVCpu);
|
---|
9544 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
9545 | }
|
---|
9546 |
|
---|
9547 |
|
---|
9548 |
|
---|
9549 | /**
|
---|
9550 | * Implements the underflow case of fxch.
|
---|
9551 | *
|
---|
9552 | * @param iStReg The other stack register.
|
---|
9553 | */
|
---|
9554 | IEM_CIMPL_DEF_1(iemCImpl_fxch_underflow, uint8_t, iStReg)
|
---|
9555 | {
|
---|
9556 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87);
|
---|
9557 |
|
---|
9558 | PX86FXSTATE pFpuCtx = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
9559 | unsigned const iReg1 = X86_FSW_TOP_GET(pFpuCtx->FSW);
|
---|
9560 | unsigned const iReg2 = (iReg1 + iStReg) & X86_FSW_TOP_SMASK;
|
---|
9561 | Assert(!(RT_BIT(iReg1) & pFpuCtx->FTW) || !(RT_BIT(iReg2) & pFpuCtx->FTW));
|
---|
9562 |
|
---|
9563 | /** @todo Testcase: fxch underflow. Making assumptions that underflowed
|
---|
9564 | * registers are read as QNaN and then exchanged. This could be
|
---|
9565 | * wrong... */
|
---|
9566 | if (pFpuCtx->FCW & X86_FCW_IM)
|
---|
9567 | {
|
---|
9568 | if (RT_BIT(iReg1) & pFpuCtx->FTW)
|
---|
9569 | {
|
---|
9570 | if (RT_BIT(iReg2) & pFpuCtx->FTW)
|
---|
9571 | iemFpuStoreQNan(&pFpuCtx->aRegs[0].r80);
|
---|
9572 | else
|
---|
9573 | pFpuCtx->aRegs[0].r80 = pFpuCtx->aRegs[iStReg].r80;
|
---|
9574 | iemFpuStoreQNan(&pFpuCtx->aRegs[iStReg].r80);
|
---|
9575 | }
|
---|
9576 | else
|
---|
9577 | {
|
---|
9578 | pFpuCtx->aRegs[iStReg].r80 = pFpuCtx->aRegs[0].r80;
|
---|
9579 | iemFpuStoreQNan(&pFpuCtx->aRegs[0].r80);
|
---|
9580 | }
|
---|
9581 | pFpuCtx->FSW &= ~X86_FSW_C_MASK;
|
---|
9582 | pFpuCtx->FSW |= X86_FSW_C1 | X86_FSW_IE | X86_FSW_SF;
|
---|
9583 | }
|
---|
9584 | else
|
---|
9585 | {
|
---|
9586 | /* raise underflow exception, don't change anything. */
|
---|
9587 | pFpuCtx->FSW &= ~(X86_FSW_TOP_MASK | X86_FSW_XCPT_MASK);
|
---|
9588 | pFpuCtx->FSW |= X86_FSW_C1 | X86_FSW_IE | X86_FSW_SF | X86_FSW_ES | X86_FSW_B;
|
---|
9589 | Log11(("fxch: %04x:%08RX64: Underflow exception (FSW=%#x)\n",
|
---|
9590 | pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip, pFpuCtx->FSW));
|
---|
9591 | }
|
---|
9592 |
|
---|
9593 | iemFpuUpdateOpcodeAndIpWorker(pVCpu, pFpuCtx);
|
---|
9594 | iemHlpUsedFpu(pVCpu);
|
---|
9595 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
9596 | }
|
---|
9597 |
|
---|
9598 |
|
---|
9599 | /**
|
---|
9600 | * Implements 'FCOMI', 'FCOMIP', 'FUCOMI', and 'FUCOMIP'.
|
---|
9601 | *
|
---|
9602 | * @param iStReg The other stack register.
|
---|
9603 | * @param pfnAImpl The assembly comparison implementation.
|
---|
9604 | * @param fPop Whether we should pop the stack when done or not.
|
---|
9605 | */
|
---|
9606 | IEM_CIMPL_DEF_3(iemCImpl_fcomi_fucomi, uint8_t, iStReg, PFNIEMAIMPLFPUR80EFL, pfnAImpl, bool, fPop)
|
---|
9607 | {
|
---|
9608 | Assert(iStReg < 8);
|
---|
9609 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87);
|
---|
9610 |
|
---|
9611 | /*
|
---|
9612 | * Raise exceptions.
|
---|
9613 | */
|
---|
9614 | if (pVCpu->cpum.GstCtx.cr0 & (X86_CR0_EM | X86_CR0_TS))
|
---|
9615 | return iemRaiseDeviceNotAvailable(pVCpu);
|
---|
9616 |
|
---|
9617 | PX86FXSTATE pFpuCtx = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
9618 | uint16_t u16Fsw = pFpuCtx->FSW;
|
---|
9619 | if (u16Fsw & X86_FSW_ES)
|
---|
9620 | return iemRaiseMathFault(pVCpu);
|
---|
9621 |
|
---|
9622 | /*
|
---|
9623 | * Check if any of the register accesses causes #SF + #IA.
|
---|
9624 | */
|
---|
9625 | unsigned const iReg1 = X86_FSW_TOP_GET(u16Fsw);
|
---|
9626 | unsigned const iReg2 = (iReg1 + iStReg) & X86_FSW_TOP_SMASK;
|
---|
9627 | if ((pFpuCtx->FTW & (RT_BIT(iReg1) | RT_BIT(iReg2))) == (RT_BIT(iReg1) | RT_BIT(iReg2)))
|
---|
9628 | {
|
---|
9629 | uint32_t u32Eflags = pfnAImpl(pFpuCtx, &u16Fsw, &pFpuCtx->aRegs[0].r80, &pFpuCtx->aRegs[iStReg].r80);
|
---|
9630 |
|
---|
9631 | pFpuCtx->FSW &= ~X86_FSW_C1;
|
---|
9632 | pFpuCtx->FSW |= u16Fsw & ~X86_FSW_TOP_MASK;
|
---|
9633 | if ( !(u16Fsw & X86_FSW_IE)
|
---|
9634 | || (pFpuCtx->FCW & X86_FCW_IM) )
|
---|
9635 | {
|
---|
9636 | pVCpu->cpum.GstCtx.eflags.u &= ~(X86_EFL_OF | X86_EFL_SF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_PF | X86_EFL_CF);
|
---|
9637 | pVCpu->cpum.GstCtx.eflags.u |= u32Eflags & (X86_EFL_ZF | X86_EFL_PF | X86_EFL_CF);
|
---|
9638 | }
|
---|
9639 | }
|
---|
9640 | else if (pFpuCtx->FCW & X86_FCW_IM)
|
---|
9641 | {
|
---|
9642 | /* Masked underflow. */
|
---|
9643 | pFpuCtx->FSW &= ~X86_FSW_C1;
|
---|
9644 | pFpuCtx->FSW |= X86_FSW_IE | X86_FSW_SF;
|
---|
9645 | pVCpu->cpum.GstCtx.eflags.u &= ~(X86_EFL_OF | X86_EFL_SF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_PF | X86_EFL_CF);
|
---|
9646 | pVCpu->cpum.GstCtx.eflags.u |= X86_EFL_ZF | X86_EFL_PF | X86_EFL_CF;
|
---|
9647 | }
|
---|
9648 | else
|
---|
9649 | {
|
---|
9650 | /* Raise underflow - don't touch EFLAGS or TOP. */
|
---|
9651 | pFpuCtx->FSW &= ~X86_FSW_C1;
|
---|
9652 | pFpuCtx->FSW |= X86_FSW_IE | X86_FSW_SF | X86_FSW_ES | X86_FSW_B;
|
---|
9653 | Log11(("fxch: %04x:%08RX64: Raising IE+SF exception (FSW=%#x)\n",
|
---|
9654 | pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip, pFpuCtx->FSW));
|
---|
9655 | fPop = false;
|
---|
9656 | }
|
---|
9657 |
|
---|
9658 | /*
|
---|
9659 | * Pop if necessary.
|
---|
9660 | */
|
---|
9661 | if (fPop)
|
---|
9662 | {
|
---|
9663 | pFpuCtx->FTW &= ~RT_BIT(iReg1);
|
---|
9664 | iemFpuStackIncTop(pVCpu);
|
---|
9665 | }
|
---|
9666 |
|
---|
9667 | iemFpuUpdateOpcodeAndIpWorker(pVCpu, pFpuCtx);
|
---|
9668 | iemHlpUsedFpu(pVCpu);
|
---|
9669 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
9670 | }
|
---|
9671 |
|
---|
9672 | /** @} */
|
---|
9673 |
|
---|