1 | /* $Id: IEMAllCImpl.cpp 100061 2023-06-03 01:09:25Z 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 | #define IEM_WITH_OPAQUE_DECODER_STATE
|
---|
35 | #include <VBox/vmm/iem.h>
|
---|
36 | #include <VBox/vmm/cpum.h>
|
---|
37 | #include <VBox/vmm/apic.h>
|
---|
38 | #include <VBox/vmm/pdm.h>
|
---|
39 | #include <VBox/vmm/pgm.h>
|
---|
40 | #include <VBox/vmm/iom.h>
|
---|
41 | #include <VBox/vmm/em.h>
|
---|
42 | #include <VBox/vmm/hm.h>
|
---|
43 | #include <VBox/vmm/nem.h>
|
---|
44 | #include <VBox/vmm/gim.h>
|
---|
45 | #ifdef VBOX_WITH_NESTED_HWVIRT_SVM
|
---|
46 | # include <VBox/vmm/em.h>
|
---|
47 | # include <VBox/vmm/hm_svm.h>
|
---|
48 | #endif
|
---|
49 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
50 | # include <VBox/vmm/hmvmxinline.h>
|
---|
51 | #endif
|
---|
52 | #ifndef VBOX_WITHOUT_CPUID_HOST_CALL
|
---|
53 | # include <VBox/vmm/cpuidcall.h>
|
---|
54 | #endif
|
---|
55 | #include <VBox/vmm/tm.h>
|
---|
56 | #include <VBox/vmm/dbgf.h>
|
---|
57 | #include <VBox/vmm/dbgftrace.h>
|
---|
58 | #include "IEMInternal.h"
|
---|
59 | #include <VBox/vmm/vmcc.h>
|
---|
60 | #include <VBox/log.h>
|
---|
61 | #include <VBox/err.h>
|
---|
62 | #include <VBox/param.h>
|
---|
63 | #include <VBox/dis.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 | * @todo The \#if conditions here must match the ones in iemOpcodeFlushLight().
|
---|
78 | */
|
---|
79 | #ifndef IEM_WITH_CODE_TLB
|
---|
80 | # define IEM_FLUSH_PREFETCH_LIGHT(a_pVCpu, a_cbInstr) iemOpcodeFlushLight(a_pVCpu, a_cbInstr)
|
---|
81 | #else
|
---|
82 | # define IEM_FLUSH_PREFETCH_LIGHT(a_pVCpu, a_cbInstr) do { } while (0)
|
---|
83 | #endif
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Flushes the prefetch buffer, heavy version.
|
---|
87 | * @todo The \#if conditions here must match the ones in iemOpcodeFlushHeavy().
|
---|
88 | */
|
---|
89 | #if !defined(IEM_WITH_CODE_TLB) || 1
|
---|
90 | # define IEM_FLUSH_PREFETCH_HEAVY(a_pVCpu, a_cbInstr) iemOpcodeFlushHeavy(a_pVCpu, a_cbInstr)
|
---|
91 | #else
|
---|
92 | # define IEM_FLUSH_PREFETCH_HEAVY(a_pVCpu, a_cbInstr) do { } while (0)
|
---|
93 | #endif
|
---|
94 |
|
---|
95 |
|
---|
96 |
|
---|
97 | /** @name Misc Helpers
|
---|
98 | * @{
|
---|
99 | */
|
---|
100 |
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Worker function for iemHlpCheckPortIOPermission, don't call directly.
|
---|
104 | *
|
---|
105 | * @returns Strict VBox status code.
|
---|
106 | *
|
---|
107 | * @param pVCpu The cross context virtual CPU structure of the calling thread.
|
---|
108 | * @param u16Port The port number.
|
---|
109 | * @param cbOperand The operand size.
|
---|
110 | */
|
---|
111 | static VBOXSTRICTRC iemHlpCheckPortIOPermissionBitmap(PVMCPUCC pVCpu, uint16_t u16Port, uint8_t cbOperand)
|
---|
112 | {
|
---|
113 | /* The TSS bits we're interested in are the same on 386 and AMD64. */
|
---|
114 | AssertCompile(AMD64_SEL_TYPE_SYS_TSS_BUSY == X86_SEL_TYPE_SYS_386_TSS_BUSY);
|
---|
115 | AssertCompile(AMD64_SEL_TYPE_SYS_TSS_AVAIL == X86_SEL_TYPE_SYS_386_TSS_AVAIL);
|
---|
116 | AssertCompileMembersAtSameOffset(X86TSS32, offIoBitmap, X86TSS64, offIoBitmap);
|
---|
117 | AssertCompile(sizeof(X86TSS32) == sizeof(X86TSS64));
|
---|
118 |
|
---|
119 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_TR);
|
---|
120 |
|
---|
121 | /*
|
---|
122 | * Check the TSS type, 16-bit TSSes doesn't have any I/O permission bitmap.
|
---|
123 | */
|
---|
124 | Assert(!pVCpu->cpum.GstCtx.tr.Attr.n.u1DescType);
|
---|
125 | if (RT_UNLIKELY( pVCpu->cpum.GstCtx.tr.Attr.n.u4Type != AMD64_SEL_TYPE_SYS_TSS_BUSY
|
---|
126 | && pVCpu->cpum.GstCtx.tr.Attr.n.u4Type != AMD64_SEL_TYPE_SYS_TSS_AVAIL))
|
---|
127 | {
|
---|
128 | Log(("iemHlpCheckPortIOPermissionBitmap: Port=%#x cb=%d - TSS type %#x (attr=%#x) has no I/O bitmap -> #GP(0)\n",
|
---|
129 | u16Port, cbOperand, pVCpu->cpum.GstCtx.tr.Attr.n.u4Type, pVCpu->cpum.GstCtx.tr.Attr.u));
|
---|
130 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
131 | }
|
---|
132 |
|
---|
133 | /*
|
---|
134 | * Read the bitmap offset (may #PF).
|
---|
135 | */
|
---|
136 | uint16_t offBitmap;
|
---|
137 | VBOXSTRICTRC rcStrict = iemMemFetchSysU16(pVCpu, &offBitmap, UINT8_MAX,
|
---|
138 | pVCpu->cpum.GstCtx.tr.u64Base + RT_UOFFSETOF(X86TSS64, offIoBitmap));
|
---|
139 | if (rcStrict != VINF_SUCCESS)
|
---|
140 | {
|
---|
141 | Log(("iemHlpCheckPortIOPermissionBitmap: Error reading offIoBitmap (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
142 | return rcStrict;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /*
|
---|
146 | * The bit range from u16Port to (u16Port + cbOperand - 1), however intel
|
---|
147 | * describes the CPU actually reading two bytes regardless of whether the
|
---|
148 | * bit range crosses a byte boundrary. Thus the + 1 in the test below.
|
---|
149 | */
|
---|
150 | uint32_t offFirstBit = (uint32_t)u16Port / 8 + offBitmap;
|
---|
151 | /** @todo check if real CPUs ensures that offBitmap has a minimum value of
|
---|
152 | * for instance sizeof(X86TSS32). */
|
---|
153 | if (offFirstBit + 1 > pVCpu->cpum.GstCtx.tr.u32Limit) /* the limit is inclusive */
|
---|
154 | {
|
---|
155 | Log(("iemHlpCheckPortIOPermissionBitmap: offFirstBit=%#x + 1 is beyond u32Limit=%#x -> #GP(0)\n",
|
---|
156 | offFirstBit, pVCpu->cpum.GstCtx.tr.u32Limit));
|
---|
157 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
158 | }
|
---|
159 |
|
---|
160 | /*
|
---|
161 | * Read the necessary bits.
|
---|
162 | */
|
---|
163 | /** @todo Test the assertion in the intel manual that the CPU reads two
|
---|
164 | * bytes. The question is how this works wrt to \#PF and \#GP on the
|
---|
165 | * 2nd byte when it's not required. */
|
---|
166 | uint16_t bmBytes = UINT16_MAX;
|
---|
167 | rcStrict = iemMemFetchSysU16(pVCpu, &bmBytes, UINT8_MAX, pVCpu->cpum.GstCtx.tr.u64Base + offFirstBit);
|
---|
168 | if (rcStrict != VINF_SUCCESS)
|
---|
169 | {
|
---|
170 | Log(("iemHlpCheckPortIOPermissionBitmap: Error reading I/O bitmap @%#x (%Rrc)\n", offFirstBit, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
171 | return rcStrict;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /*
|
---|
175 | * Perform the check.
|
---|
176 | */
|
---|
177 | uint16_t fPortMask = (1 << cbOperand) - 1;
|
---|
178 | bmBytes >>= (u16Port & 7);
|
---|
179 | if (bmBytes & fPortMask)
|
---|
180 | {
|
---|
181 | Log(("iemHlpCheckPortIOPermissionBitmap: u16Port=%#x LB %u - access denied (bm=%#x mask=%#x) -> #GP(0)\n",
|
---|
182 | u16Port, cbOperand, bmBytes, fPortMask));
|
---|
183 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
184 | }
|
---|
185 |
|
---|
186 | return VINF_SUCCESS;
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Checks if we are allowed to access the given I/O port, raising the
|
---|
192 | * appropriate exceptions if we aren't (or if the I/O bitmap is not
|
---|
193 | * accessible).
|
---|
194 | *
|
---|
195 | * @returns Strict VBox status code.
|
---|
196 | *
|
---|
197 | * @param pVCpu The cross context virtual CPU structure of the calling thread.
|
---|
198 | * @param u16Port The port number.
|
---|
199 | * @param cbOperand The operand size.
|
---|
200 | */
|
---|
201 | DECLINLINE(VBOXSTRICTRC) iemHlpCheckPortIOPermission(PVMCPUCC pVCpu, uint16_t u16Port, uint8_t cbOperand)
|
---|
202 | {
|
---|
203 | X86EFLAGS Efl;
|
---|
204 | Efl.u = IEMMISC_GET_EFL(pVCpu);
|
---|
205 | if ( (pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE)
|
---|
206 | && ( IEM_GET_CPL(pVCpu) > Efl.Bits.u2IOPL
|
---|
207 | || Efl.Bits.u1VM) )
|
---|
208 | return iemHlpCheckPortIOPermissionBitmap(pVCpu, u16Port, cbOperand);
|
---|
209 | return VINF_SUCCESS;
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | #if 0
|
---|
214 | /**
|
---|
215 | * Calculates the parity bit.
|
---|
216 | *
|
---|
217 | * @returns true if the bit is set, false if not.
|
---|
218 | * @param u8Result The least significant byte of the result.
|
---|
219 | */
|
---|
220 | static bool iemHlpCalcParityFlag(uint8_t u8Result)
|
---|
221 | {
|
---|
222 | /*
|
---|
223 | * Parity is set if the number of bits in the least significant byte of
|
---|
224 | * the result is even.
|
---|
225 | */
|
---|
226 | uint8_t cBits;
|
---|
227 | cBits = u8Result & 1; /* 0 */
|
---|
228 | u8Result >>= 1;
|
---|
229 | cBits += u8Result & 1;
|
---|
230 | u8Result >>= 1;
|
---|
231 | cBits += u8Result & 1;
|
---|
232 | u8Result >>= 1;
|
---|
233 | cBits += u8Result & 1;
|
---|
234 | u8Result >>= 1;
|
---|
235 | cBits += u8Result & 1; /* 4 */
|
---|
236 | u8Result >>= 1;
|
---|
237 | cBits += u8Result & 1;
|
---|
238 | u8Result >>= 1;
|
---|
239 | cBits += u8Result & 1;
|
---|
240 | u8Result >>= 1;
|
---|
241 | cBits += u8Result & 1;
|
---|
242 | return !(cBits & 1);
|
---|
243 | }
|
---|
244 | #endif /* not used */
|
---|
245 |
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * Updates the specified flags according to a 8-bit result.
|
---|
249 | *
|
---|
250 | * @param pVCpu The cross context virtual CPU structure of the calling thread.
|
---|
251 | * @param u8Result The result to set the flags according to.
|
---|
252 | * @param fToUpdate The flags to update.
|
---|
253 | * @param fUndefined The flags that are specified as undefined.
|
---|
254 | */
|
---|
255 | static void iemHlpUpdateArithEFlagsU8(PVMCPUCC pVCpu, uint8_t u8Result, uint32_t fToUpdate, uint32_t fUndefined)
|
---|
256 | {
|
---|
257 | uint32_t fEFlags = pVCpu->cpum.GstCtx.eflags.u;
|
---|
258 | iemAImpl_test_u8(&u8Result, u8Result, &fEFlags);
|
---|
259 | pVCpu->cpum.GstCtx.eflags.u &= ~(fToUpdate | fUndefined);
|
---|
260 | pVCpu->cpum.GstCtx.eflags.u |= (fToUpdate | fUndefined) & fEFlags;
|
---|
261 | }
|
---|
262 |
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * Updates the specified flags according to a 16-bit result.
|
---|
266 | *
|
---|
267 | * @param pVCpu The cross context virtual CPU structure of the calling thread.
|
---|
268 | * @param u16Result The result to set the flags according to.
|
---|
269 | * @param fToUpdate The flags to update.
|
---|
270 | * @param fUndefined The flags that are specified as undefined.
|
---|
271 | */
|
---|
272 | static void iemHlpUpdateArithEFlagsU16(PVMCPUCC pVCpu, uint16_t u16Result, uint32_t fToUpdate, uint32_t fUndefined)
|
---|
273 | {
|
---|
274 | uint32_t fEFlags = pVCpu->cpum.GstCtx.eflags.u;
|
---|
275 | iemAImpl_test_u16(&u16Result, u16Result, &fEFlags);
|
---|
276 | pVCpu->cpum.GstCtx.eflags.u &= ~(fToUpdate | fUndefined);
|
---|
277 | pVCpu->cpum.GstCtx.eflags.u |= (fToUpdate | fUndefined) & fEFlags;
|
---|
278 | }
|
---|
279 |
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * Helper used by iret.
|
---|
283 | *
|
---|
284 | * @param pVCpu The cross context virtual CPU structure of the calling thread.
|
---|
285 | * @param uCpl The new CPL.
|
---|
286 | * @param pSReg Pointer to the segment register.
|
---|
287 | */
|
---|
288 | static void iemHlpAdjustSelectorForNewCpl(PVMCPUCC pVCpu, uint8_t uCpl, PCPUMSELREG pSReg)
|
---|
289 | {
|
---|
290 | Assert(CPUMSELREG_ARE_HIDDEN_PARTS_VALID(pVCpu, pSReg));
|
---|
291 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_SREG_MASK);
|
---|
292 |
|
---|
293 | if ( uCpl > pSReg->Attr.n.u2Dpl
|
---|
294 | && pSReg->Attr.n.u1DescType /* code or data, not system */
|
---|
295 | && (pSReg->Attr.n.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
|
---|
296 | != (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF)) /* not conforming code */
|
---|
297 | iemHlpLoadNullDataSelectorProt(pVCpu, pSReg, 0);
|
---|
298 | }
|
---|
299 |
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * Indicates that we have modified the FPU state.
|
---|
303 | *
|
---|
304 | * @param pVCpu The cross context virtual CPU structure of the calling thread.
|
---|
305 | */
|
---|
306 | DECLINLINE(void) iemHlpUsedFpu(PVMCPUCC pVCpu)
|
---|
307 | {
|
---|
308 | CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_FPU_REM);
|
---|
309 | }
|
---|
310 |
|
---|
311 | /** @} */
|
---|
312 |
|
---|
313 | /** @name C Implementations
|
---|
314 | * @{
|
---|
315 | */
|
---|
316 |
|
---|
317 | /**
|
---|
318 | * Implements a 16-bit popa.
|
---|
319 | */
|
---|
320 | IEM_CIMPL_DEF_0(iemCImpl_popa_16)
|
---|
321 | {
|
---|
322 | RTGCPTR GCPtrStart = iemRegGetEffRsp(pVCpu);
|
---|
323 | RTGCPTR GCPtrLast = GCPtrStart + 15;
|
---|
324 | VBOXSTRICTRC rcStrict;
|
---|
325 |
|
---|
326 | /*
|
---|
327 | * The docs are a bit hard to comprehend here, but it looks like we wrap
|
---|
328 | * around in real mode as long as none of the individual "popa" crosses the
|
---|
329 | * end of the stack segment. In protected mode we check the whole access
|
---|
330 | * in one go. For efficiency, only do the word-by-word thing if we're in
|
---|
331 | * danger of wrapping around.
|
---|
332 | */
|
---|
333 | /** @todo do popa boundary / wrap-around checks. */
|
---|
334 | if (RT_UNLIKELY( IEM_IS_REAL_OR_V86_MODE(pVCpu)
|
---|
335 | && (pVCpu->cpum.GstCtx.cs.u32Limit < GCPtrLast)) ) /* ASSUMES 64-bit RTGCPTR */
|
---|
336 | {
|
---|
337 | /* word-by-word */
|
---|
338 | RTUINT64U TmpRsp;
|
---|
339 | TmpRsp.u = pVCpu->cpum.GstCtx.rsp;
|
---|
340 | rcStrict = iemMemStackPopU16Ex(pVCpu, &pVCpu->cpum.GstCtx.di, &TmpRsp);
|
---|
341 | if (rcStrict == VINF_SUCCESS)
|
---|
342 | rcStrict = iemMemStackPopU16Ex(pVCpu, &pVCpu->cpum.GstCtx.si, &TmpRsp);
|
---|
343 | if (rcStrict == VINF_SUCCESS)
|
---|
344 | rcStrict = iemMemStackPopU16Ex(pVCpu, &pVCpu->cpum.GstCtx.bp, &TmpRsp);
|
---|
345 | if (rcStrict == VINF_SUCCESS)
|
---|
346 | {
|
---|
347 | iemRegAddToRspEx(pVCpu, &TmpRsp, 2); /* sp */
|
---|
348 | rcStrict = iemMemStackPopU16Ex(pVCpu, &pVCpu->cpum.GstCtx.bx, &TmpRsp);
|
---|
349 | }
|
---|
350 | if (rcStrict == VINF_SUCCESS)
|
---|
351 | rcStrict = iemMemStackPopU16Ex(pVCpu, &pVCpu->cpum.GstCtx.dx, &TmpRsp);
|
---|
352 | if (rcStrict == VINF_SUCCESS)
|
---|
353 | rcStrict = iemMemStackPopU16Ex(pVCpu, &pVCpu->cpum.GstCtx.cx, &TmpRsp);
|
---|
354 | if (rcStrict == VINF_SUCCESS)
|
---|
355 | rcStrict = iemMemStackPopU16Ex(pVCpu, &pVCpu->cpum.GstCtx.ax, &TmpRsp);
|
---|
356 | if (rcStrict == VINF_SUCCESS)
|
---|
357 | {
|
---|
358 | pVCpu->cpum.GstCtx.rsp = TmpRsp.u;
|
---|
359 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
360 | }
|
---|
361 | }
|
---|
362 | else
|
---|
363 | {
|
---|
364 | uint16_t const *pa16Mem = NULL;
|
---|
365 | rcStrict = iemMemMap(pVCpu, (void **)&pa16Mem, 16, X86_SREG_SS, GCPtrStart, IEM_ACCESS_STACK_R, sizeof(*pa16Mem) - 1);
|
---|
366 | if (rcStrict == VINF_SUCCESS)
|
---|
367 | {
|
---|
368 | pVCpu->cpum.GstCtx.di = pa16Mem[7 - X86_GREG_xDI];
|
---|
369 | pVCpu->cpum.GstCtx.si = pa16Mem[7 - X86_GREG_xSI];
|
---|
370 | pVCpu->cpum.GstCtx.bp = pa16Mem[7 - X86_GREG_xBP];
|
---|
371 | /* skip sp */
|
---|
372 | pVCpu->cpum.GstCtx.bx = pa16Mem[7 - X86_GREG_xBX];
|
---|
373 | pVCpu->cpum.GstCtx.dx = pa16Mem[7 - X86_GREG_xDX];
|
---|
374 | pVCpu->cpum.GstCtx.cx = pa16Mem[7 - X86_GREG_xCX];
|
---|
375 | pVCpu->cpum.GstCtx.ax = pa16Mem[7 - X86_GREG_xAX];
|
---|
376 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)pa16Mem, IEM_ACCESS_STACK_R);
|
---|
377 | if (rcStrict == VINF_SUCCESS)
|
---|
378 | {
|
---|
379 | iemRegAddToRsp(pVCpu, 16);
|
---|
380 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
381 | }
|
---|
382 | }
|
---|
383 | }
|
---|
384 | return rcStrict;
|
---|
385 | }
|
---|
386 |
|
---|
387 |
|
---|
388 | /**
|
---|
389 | * Implements a 32-bit popa.
|
---|
390 | */
|
---|
391 | IEM_CIMPL_DEF_0(iemCImpl_popa_32)
|
---|
392 | {
|
---|
393 | RTGCPTR GCPtrStart = iemRegGetEffRsp(pVCpu);
|
---|
394 | RTGCPTR GCPtrLast = GCPtrStart + 31;
|
---|
395 | VBOXSTRICTRC rcStrict;
|
---|
396 |
|
---|
397 | /*
|
---|
398 | * The docs are a bit hard to comprehend here, but it looks like we wrap
|
---|
399 | * around in real mode as long as none of the individual "popa" crosses the
|
---|
400 | * end of the stack segment. In protected mode we check the whole access
|
---|
401 | * in one go. For efficiency, only do the word-by-word thing if we're in
|
---|
402 | * danger of wrapping around.
|
---|
403 | */
|
---|
404 | /** @todo do popa boundary / wrap-around checks. */
|
---|
405 | if (RT_UNLIKELY( IEM_IS_REAL_OR_V86_MODE(pVCpu)
|
---|
406 | && (pVCpu->cpum.GstCtx.cs.u32Limit < GCPtrLast)) ) /* ASSUMES 64-bit RTGCPTR */
|
---|
407 | {
|
---|
408 | /* word-by-word */
|
---|
409 | RTUINT64U TmpRsp;
|
---|
410 | TmpRsp.u = pVCpu->cpum.GstCtx.rsp;
|
---|
411 | rcStrict = iemMemStackPopU32Ex(pVCpu, &pVCpu->cpum.GstCtx.edi, &TmpRsp);
|
---|
412 | if (rcStrict == VINF_SUCCESS)
|
---|
413 | rcStrict = iemMemStackPopU32Ex(pVCpu, &pVCpu->cpum.GstCtx.esi, &TmpRsp);
|
---|
414 | if (rcStrict == VINF_SUCCESS)
|
---|
415 | rcStrict = iemMemStackPopU32Ex(pVCpu, &pVCpu->cpum.GstCtx.ebp, &TmpRsp);
|
---|
416 | if (rcStrict == VINF_SUCCESS)
|
---|
417 | {
|
---|
418 | iemRegAddToRspEx(pVCpu, &TmpRsp, 2); /* sp */
|
---|
419 | rcStrict = iemMemStackPopU32Ex(pVCpu, &pVCpu->cpum.GstCtx.ebx, &TmpRsp);
|
---|
420 | }
|
---|
421 | if (rcStrict == VINF_SUCCESS)
|
---|
422 | rcStrict = iemMemStackPopU32Ex(pVCpu, &pVCpu->cpum.GstCtx.edx, &TmpRsp);
|
---|
423 | if (rcStrict == VINF_SUCCESS)
|
---|
424 | rcStrict = iemMemStackPopU32Ex(pVCpu, &pVCpu->cpum.GstCtx.ecx, &TmpRsp);
|
---|
425 | if (rcStrict == VINF_SUCCESS)
|
---|
426 | rcStrict = iemMemStackPopU32Ex(pVCpu, &pVCpu->cpum.GstCtx.eax, &TmpRsp);
|
---|
427 | if (rcStrict == VINF_SUCCESS)
|
---|
428 | {
|
---|
429 | #if 1 /** @todo what actually happens with the high bits when we're in 16-bit mode? */
|
---|
430 | pVCpu->cpum.GstCtx.rdi &= UINT32_MAX;
|
---|
431 | pVCpu->cpum.GstCtx.rsi &= UINT32_MAX;
|
---|
432 | pVCpu->cpum.GstCtx.rbp &= UINT32_MAX;
|
---|
433 | pVCpu->cpum.GstCtx.rbx &= UINT32_MAX;
|
---|
434 | pVCpu->cpum.GstCtx.rdx &= UINT32_MAX;
|
---|
435 | pVCpu->cpum.GstCtx.rcx &= UINT32_MAX;
|
---|
436 | pVCpu->cpum.GstCtx.rax &= UINT32_MAX;
|
---|
437 | #endif
|
---|
438 | pVCpu->cpum.GstCtx.rsp = TmpRsp.u;
|
---|
439 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
440 | }
|
---|
441 | }
|
---|
442 | else
|
---|
443 | {
|
---|
444 | uint32_t const *pa32Mem;
|
---|
445 | rcStrict = iemMemMap(pVCpu, (void **)&pa32Mem, 32, X86_SREG_SS, GCPtrStart, IEM_ACCESS_STACK_R, sizeof(*pa32Mem) - 1);
|
---|
446 | if (rcStrict == VINF_SUCCESS)
|
---|
447 | {
|
---|
448 | pVCpu->cpum.GstCtx.rdi = pa32Mem[7 - X86_GREG_xDI];
|
---|
449 | pVCpu->cpum.GstCtx.rsi = pa32Mem[7 - X86_GREG_xSI];
|
---|
450 | pVCpu->cpum.GstCtx.rbp = pa32Mem[7 - X86_GREG_xBP];
|
---|
451 | /* skip esp */
|
---|
452 | pVCpu->cpum.GstCtx.rbx = pa32Mem[7 - X86_GREG_xBX];
|
---|
453 | pVCpu->cpum.GstCtx.rdx = pa32Mem[7 - X86_GREG_xDX];
|
---|
454 | pVCpu->cpum.GstCtx.rcx = pa32Mem[7 - X86_GREG_xCX];
|
---|
455 | pVCpu->cpum.GstCtx.rax = pa32Mem[7 - X86_GREG_xAX];
|
---|
456 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)pa32Mem, IEM_ACCESS_STACK_R);
|
---|
457 | if (rcStrict == VINF_SUCCESS)
|
---|
458 | {
|
---|
459 | iemRegAddToRsp(pVCpu, 32);
|
---|
460 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
461 | }
|
---|
462 | }
|
---|
463 | }
|
---|
464 | return rcStrict;
|
---|
465 | }
|
---|
466 |
|
---|
467 |
|
---|
468 | /**
|
---|
469 | * Implements a 16-bit pusha.
|
---|
470 | */
|
---|
471 | IEM_CIMPL_DEF_0(iemCImpl_pusha_16)
|
---|
472 | {
|
---|
473 | RTGCPTR GCPtrTop = iemRegGetEffRsp(pVCpu);
|
---|
474 | RTGCPTR GCPtrBottom = GCPtrTop - 15;
|
---|
475 | VBOXSTRICTRC rcStrict;
|
---|
476 |
|
---|
477 | /*
|
---|
478 | * The docs are a bit hard to comprehend here, but it looks like we wrap
|
---|
479 | * around in real mode as long as none of the individual "pushd" crosses the
|
---|
480 | * end of the stack segment. In protected mode we check the whole access
|
---|
481 | * in one go. For efficiency, only do the word-by-word thing if we're in
|
---|
482 | * danger of wrapping around.
|
---|
483 | */
|
---|
484 | /** @todo do pusha boundary / wrap-around checks. */
|
---|
485 | if (RT_UNLIKELY( GCPtrBottom > GCPtrTop
|
---|
486 | && IEM_IS_REAL_OR_V86_MODE(pVCpu) ) )
|
---|
487 | {
|
---|
488 | /* word-by-word */
|
---|
489 | RTUINT64U TmpRsp;
|
---|
490 | TmpRsp.u = pVCpu->cpum.GstCtx.rsp;
|
---|
491 | rcStrict = iemMemStackPushU16Ex(pVCpu, pVCpu->cpum.GstCtx.ax, &TmpRsp);
|
---|
492 | if (rcStrict == VINF_SUCCESS)
|
---|
493 | rcStrict = iemMemStackPushU16Ex(pVCpu, pVCpu->cpum.GstCtx.cx, &TmpRsp);
|
---|
494 | if (rcStrict == VINF_SUCCESS)
|
---|
495 | rcStrict = iemMemStackPushU16Ex(pVCpu, pVCpu->cpum.GstCtx.dx, &TmpRsp);
|
---|
496 | if (rcStrict == VINF_SUCCESS)
|
---|
497 | rcStrict = iemMemStackPushU16Ex(pVCpu, pVCpu->cpum.GstCtx.bx, &TmpRsp);
|
---|
498 | if (rcStrict == VINF_SUCCESS)
|
---|
499 | rcStrict = iemMemStackPushU16Ex(pVCpu, pVCpu->cpum.GstCtx.sp, &TmpRsp);
|
---|
500 | if (rcStrict == VINF_SUCCESS)
|
---|
501 | rcStrict = iemMemStackPushU16Ex(pVCpu, pVCpu->cpum.GstCtx.bp, &TmpRsp);
|
---|
502 | if (rcStrict == VINF_SUCCESS)
|
---|
503 | rcStrict = iemMemStackPushU16Ex(pVCpu, pVCpu->cpum.GstCtx.si, &TmpRsp);
|
---|
504 | if (rcStrict == VINF_SUCCESS)
|
---|
505 | rcStrict = iemMemStackPushU16Ex(pVCpu, pVCpu->cpum.GstCtx.di, &TmpRsp);
|
---|
506 | if (rcStrict == VINF_SUCCESS)
|
---|
507 | {
|
---|
508 | pVCpu->cpum.GstCtx.rsp = TmpRsp.u;
|
---|
509 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
510 | }
|
---|
511 | }
|
---|
512 | else
|
---|
513 | {
|
---|
514 | GCPtrBottom--;
|
---|
515 | uint16_t *pa16Mem = NULL;
|
---|
516 | rcStrict = iemMemMap(pVCpu, (void **)&pa16Mem, 16, X86_SREG_SS, GCPtrBottom, IEM_ACCESS_STACK_W, sizeof(*pa16Mem) - 1);
|
---|
517 | if (rcStrict == VINF_SUCCESS)
|
---|
518 | {
|
---|
519 | pa16Mem[7 - X86_GREG_xDI] = pVCpu->cpum.GstCtx.di;
|
---|
520 | pa16Mem[7 - X86_GREG_xSI] = pVCpu->cpum.GstCtx.si;
|
---|
521 | pa16Mem[7 - X86_GREG_xBP] = pVCpu->cpum.GstCtx.bp;
|
---|
522 | pa16Mem[7 - X86_GREG_xSP] = pVCpu->cpum.GstCtx.sp;
|
---|
523 | pa16Mem[7 - X86_GREG_xBX] = pVCpu->cpum.GstCtx.bx;
|
---|
524 | pa16Mem[7 - X86_GREG_xDX] = pVCpu->cpum.GstCtx.dx;
|
---|
525 | pa16Mem[7 - X86_GREG_xCX] = pVCpu->cpum.GstCtx.cx;
|
---|
526 | pa16Mem[7 - X86_GREG_xAX] = pVCpu->cpum.GstCtx.ax;
|
---|
527 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)pa16Mem, IEM_ACCESS_STACK_W);
|
---|
528 | if (rcStrict == VINF_SUCCESS)
|
---|
529 | {
|
---|
530 | iemRegSubFromRsp(pVCpu, 16);
|
---|
531 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
532 | }
|
---|
533 | }
|
---|
534 | }
|
---|
535 | return rcStrict;
|
---|
536 | }
|
---|
537 |
|
---|
538 |
|
---|
539 | /**
|
---|
540 | * Implements a 32-bit pusha.
|
---|
541 | */
|
---|
542 | IEM_CIMPL_DEF_0(iemCImpl_pusha_32)
|
---|
543 | {
|
---|
544 | RTGCPTR GCPtrTop = iemRegGetEffRsp(pVCpu);
|
---|
545 | RTGCPTR GCPtrBottom = GCPtrTop - 31;
|
---|
546 | VBOXSTRICTRC rcStrict;
|
---|
547 |
|
---|
548 | /*
|
---|
549 | * The docs are a bit hard to comprehend here, but it looks like we wrap
|
---|
550 | * around in real mode as long as none of the individual "pusha" crosses the
|
---|
551 | * end of the stack segment. In protected mode we check the whole access
|
---|
552 | * in one go. For efficiency, only do the word-by-word thing if we're in
|
---|
553 | * danger of wrapping around.
|
---|
554 | */
|
---|
555 | /** @todo do pusha boundary / wrap-around checks. */
|
---|
556 | if (RT_UNLIKELY( GCPtrBottom > GCPtrTop
|
---|
557 | && IEM_IS_REAL_OR_V86_MODE(pVCpu) ) )
|
---|
558 | {
|
---|
559 | /* word-by-word */
|
---|
560 | RTUINT64U TmpRsp;
|
---|
561 | TmpRsp.u = pVCpu->cpum.GstCtx.rsp;
|
---|
562 | rcStrict = iemMemStackPushU32Ex(pVCpu, pVCpu->cpum.GstCtx.eax, &TmpRsp);
|
---|
563 | if (rcStrict == VINF_SUCCESS)
|
---|
564 | rcStrict = iemMemStackPushU32Ex(pVCpu, pVCpu->cpum.GstCtx.ecx, &TmpRsp);
|
---|
565 | if (rcStrict == VINF_SUCCESS)
|
---|
566 | rcStrict = iemMemStackPushU32Ex(pVCpu, pVCpu->cpum.GstCtx.edx, &TmpRsp);
|
---|
567 | if (rcStrict == VINF_SUCCESS)
|
---|
568 | rcStrict = iemMemStackPushU32Ex(pVCpu, pVCpu->cpum.GstCtx.ebx, &TmpRsp);
|
---|
569 | if (rcStrict == VINF_SUCCESS)
|
---|
570 | rcStrict = iemMemStackPushU32Ex(pVCpu, pVCpu->cpum.GstCtx.esp, &TmpRsp);
|
---|
571 | if (rcStrict == VINF_SUCCESS)
|
---|
572 | rcStrict = iemMemStackPushU32Ex(pVCpu, pVCpu->cpum.GstCtx.ebp, &TmpRsp);
|
---|
573 | if (rcStrict == VINF_SUCCESS)
|
---|
574 | rcStrict = iemMemStackPushU32Ex(pVCpu, pVCpu->cpum.GstCtx.esi, &TmpRsp);
|
---|
575 | if (rcStrict == VINF_SUCCESS)
|
---|
576 | rcStrict = iemMemStackPushU32Ex(pVCpu, pVCpu->cpum.GstCtx.edi, &TmpRsp);
|
---|
577 | if (rcStrict == VINF_SUCCESS)
|
---|
578 | {
|
---|
579 | pVCpu->cpum.GstCtx.rsp = TmpRsp.u;
|
---|
580 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
581 | }
|
---|
582 | }
|
---|
583 | else
|
---|
584 | {
|
---|
585 | GCPtrBottom--;
|
---|
586 | uint32_t *pa32Mem;
|
---|
587 | rcStrict = iemMemMap(pVCpu, (void **)&pa32Mem, 32, X86_SREG_SS, GCPtrBottom, IEM_ACCESS_STACK_W, sizeof(*pa32Mem) - 1);
|
---|
588 | if (rcStrict == VINF_SUCCESS)
|
---|
589 | {
|
---|
590 | pa32Mem[7 - X86_GREG_xDI] = pVCpu->cpum.GstCtx.edi;
|
---|
591 | pa32Mem[7 - X86_GREG_xSI] = pVCpu->cpum.GstCtx.esi;
|
---|
592 | pa32Mem[7 - X86_GREG_xBP] = pVCpu->cpum.GstCtx.ebp;
|
---|
593 | pa32Mem[7 - X86_GREG_xSP] = pVCpu->cpum.GstCtx.esp;
|
---|
594 | pa32Mem[7 - X86_GREG_xBX] = pVCpu->cpum.GstCtx.ebx;
|
---|
595 | pa32Mem[7 - X86_GREG_xDX] = pVCpu->cpum.GstCtx.edx;
|
---|
596 | pa32Mem[7 - X86_GREG_xCX] = pVCpu->cpum.GstCtx.ecx;
|
---|
597 | pa32Mem[7 - X86_GREG_xAX] = pVCpu->cpum.GstCtx.eax;
|
---|
598 | rcStrict = iemMemCommitAndUnmap(pVCpu, pa32Mem, IEM_ACCESS_STACK_W);
|
---|
599 | if (rcStrict == VINF_SUCCESS)
|
---|
600 | {
|
---|
601 | iemRegSubFromRsp(pVCpu, 32);
|
---|
602 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
603 | }
|
---|
604 | }
|
---|
605 | }
|
---|
606 | return rcStrict;
|
---|
607 | }
|
---|
608 |
|
---|
609 |
|
---|
610 | /**
|
---|
611 | * Implements pushf.
|
---|
612 | *
|
---|
613 | *
|
---|
614 | * @param enmEffOpSize The effective operand size.
|
---|
615 | */
|
---|
616 | IEM_CIMPL_DEF_1(iemCImpl_pushf, IEMMODE, enmEffOpSize)
|
---|
617 | {
|
---|
618 | VBOXSTRICTRC rcStrict;
|
---|
619 |
|
---|
620 | if (!IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_PUSHF))
|
---|
621 | { /* probable */ }
|
---|
622 | else
|
---|
623 | {
|
---|
624 | Log2(("pushf: Guest intercept -> #VMEXIT\n"));
|
---|
625 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
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 | { /* probable */ }
|
---|
690 | else
|
---|
691 | {
|
---|
692 | Log2(("popf: Guest intercept -> #VMEXIT\n"));
|
---|
693 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
694 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_POPF, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
695 | }
|
---|
696 |
|
---|
697 | /*
|
---|
698 | * V8086 is special as usual.
|
---|
699 | */
|
---|
700 | if (fEflOld & X86_EFL_VM)
|
---|
701 | {
|
---|
702 | /*
|
---|
703 | * Almost anything goes if IOPL is 3.
|
---|
704 | */
|
---|
705 | if (X86_EFL_GET_IOPL(fEflOld) == 3)
|
---|
706 | {
|
---|
707 | switch (enmEffOpSize)
|
---|
708 | {
|
---|
709 | case IEMMODE_16BIT:
|
---|
710 | {
|
---|
711 | uint16_t u16Value;
|
---|
712 | rcStrict = iemMemStackPopU16(pVCpu, &u16Value);
|
---|
713 | if (rcStrict != VINF_SUCCESS)
|
---|
714 | return rcStrict;
|
---|
715 | fEflNew = u16Value | (fEflOld & UINT32_C(0xffff0000));
|
---|
716 | break;
|
---|
717 | }
|
---|
718 | case IEMMODE_32BIT:
|
---|
719 | rcStrict = iemMemStackPopU32(pVCpu, &fEflNew);
|
---|
720 | if (rcStrict != VINF_SUCCESS)
|
---|
721 | return rcStrict;
|
---|
722 | break;
|
---|
723 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
724 | }
|
---|
725 |
|
---|
726 | const uint32_t fPopfBits = pVCpu->CTX_SUFF(pVM)->cpum.ro.GuestFeatures.enmMicroarch != kCpumMicroarch_Intel_80386
|
---|
727 | ? X86_EFL_POPF_BITS : X86_EFL_POPF_BITS_386;
|
---|
728 | fEflNew &= fPopfBits & ~(X86_EFL_IOPL);
|
---|
729 | fEflNew |= ~(fPopfBits & ~(X86_EFL_IOPL)) & fEflOld;
|
---|
730 | }
|
---|
731 | /*
|
---|
732 | * Interrupt flag virtualization with CR4.VME=1.
|
---|
733 | */
|
---|
734 | else if ( enmEffOpSize == IEMMODE_16BIT
|
---|
735 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_VME) )
|
---|
736 | {
|
---|
737 | uint16_t u16Value;
|
---|
738 | RTUINT64U TmpRsp;
|
---|
739 | TmpRsp.u = pVCpu->cpum.GstCtx.rsp;
|
---|
740 | rcStrict = iemMemStackPopU16Ex(pVCpu, &u16Value, &TmpRsp);
|
---|
741 | if (rcStrict != VINF_SUCCESS)
|
---|
742 | return rcStrict;
|
---|
743 |
|
---|
744 | /** @todo Is the popf VME \#GP(0) delivered after updating RSP+RIP
|
---|
745 | * or before? */
|
---|
746 | if ( ( (u16Value & X86_EFL_IF)
|
---|
747 | && (fEflOld & X86_EFL_VIP))
|
---|
748 | || (u16Value & X86_EFL_TF) )
|
---|
749 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
750 |
|
---|
751 | fEflNew = u16Value | (fEflOld & UINT32_C(0xffff0000) & ~X86_EFL_VIF);
|
---|
752 | fEflNew |= (fEflNew & X86_EFL_IF) << (19 - 9);
|
---|
753 | fEflNew &= X86_EFL_POPF_BITS & ~(X86_EFL_IOPL | X86_EFL_IF);
|
---|
754 | fEflNew |= ~(X86_EFL_POPF_BITS & ~(X86_EFL_IOPL | X86_EFL_IF)) & fEflOld;
|
---|
755 |
|
---|
756 | pVCpu->cpum.GstCtx.rsp = TmpRsp.u;
|
---|
757 | }
|
---|
758 | else
|
---|
759 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
760 |
|
---|
761 | }
|
---|
762 | /*
|
---|
763 | * Not in V8086 mode.
|
---|
764 | */
|
---|
765 | else
|
---|
766 | {
|
---|
767 | /* Pop the flags. */
|
---|
768 | switch (enmEffOpSize)
|
---|
769 | {
|
---|
770 | case IEMMODE_16BIT:
|
---|
771 | {
|
---|
772 | uint16_t u16Value;
|
---|
773 | rcStrict = iemMemStackPopU16(pVCpu, &u16Value);
|
---|
774 | if (rcStrict != VINF_SUCCESS)
|
---|
775 | return rcStrict;
|
---|
776 | fEflNew = u16Value | (fEflOld & UINT32_C(0xffff0000));
|
---|
777 |
|
---|
778 | /*
|
---|
779 | * Ancient CPU adjustments:
|
---|
780 | * - 8086, 80186, V20/30:
|
---|
781 | * Fixed bits 15:12 bits are not kept correctly internally, mostly for
|
---|
782 | * practical reasons (masking below). We add them when pushing flags.
|
---|
783 | * - 80286:
|
---|
784 | * The NT and IOPL flags cannot be popped from real mode and are
|
---|
785 | * therefore always zero (since a 286 can never exit from PM and
|
---|
786 | * their initial value is zero). This changed on a 386 and can
|
---|
787 | * therefore be used to detect 286 or 386 CPU in real mode.
|
---|
788 | */
|
---|
789 | if ( IEM_GET_TARGET_CPU(pVCpu) == IEMTARGETCPU_286
|
---|
790 | && !(pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE) )
|
---|
791 | fEflNew &= ~(X86_EFL_NT | X86_EFL_IOPL);
|
---|
792 | break;
|
---|
793 | }
|
---|
794 | case IEMMODE_32BIT:
|
---|
795 | rcStrict = iemMemStackPopU32(pVCpu, &fEflNew);
|
---|
796 | if (rcStrict != VINF_SUCCESS)
|
---|
797 | return rcStrict;
|
---|
798 | break;
|
---|
799 | case IEMMODE_64BIT:
|
---|
800 | {
|
---|
801 | uint64_t u64Value;
|
---|
802 | rcStrict = iemMemStackPopU64(pVCpu, &u64Value);
|
---|
803 | if (rcStrict != VINF_SUCCESS)
|
---|
804 | return rcStrict;
|
---|
805 | fEflNew = u64Value; /** @todo testcase: Check exactly what happens if high bits are set. */
|
---|
806 | break;
|
---|
807 | }
|
---|
808 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
809 | }
|
---|
810 |
|
---|
811 | /* Merge them with the current flags. */
|
---|
812 | const uint32_t fPopfBits = pVCpu->CTX_SUFF(pVM)->cpum.ro.GuestFeatures.enmMicroarch != kCpumMicroarch_Intel_80386
|
---|
813 | ? X86_EFL_POPF_BITS : X86_EFL_POPF_BITS_386;
|
---|
814 | if ( (fEflNew & (X86_EFL_IOPL | X86_EFL_IF)) == (fEflOld & (X86_EFL_IOPL | X86_EFL_IF))
|
---|
815 | || IEM_GET_CPL(pVCpu) == 0)
|
---|
816 | {
|
---|
817 | fEflNew &= fPopfBits;
|
---|
818 | fEflNew |= ~fPopfBits & fEflOld;
|
---|
819 | }
|
---|
820 | else if (IEM_GET_CPL(pVCpu) <= X86_EFL_GET_IOPL(fEflOld))
|
---|
821 | {
|
---|
822 | fEflNew &= fPopfBits & ~(X86_EFL_IOPL);
|
---|
823 | fEflNew |= ~(fPopfBits & ~(X86_EFL_IOPL)) & fEflOld;
|
---|
824 | }
|
---|
825 | else
|
---|
826 | {
|
---|
827 | fEflNew &= fPopfBits & ~(X86_EFL_IOPL | X86_EFL_IF);
|
---|
828 | fEflNew |= ~(fPopfBits & ~(X86_EFL_IOPL | X86_EFL_IF)) & fEflOld;
|
---|
829 | }
|
---|
830 | }
|
---|
831 |
|
---|
832 | /*
|
---|
833 | * Commit the flags.
|
---|
834 | */
|
---|
835 | Assert(fEflNew & RT_BIT_32(1));
|
---|
836 | IEMMISC_SET_EFL(pVCpu, fEflNew);
|
---|
837 | return iemRegAddToRipAndFinishingClearingRfEx(pVCpu, cbInstr, fEflOld);
|
---|
838 | }
|
---|
839 |
|
---|
840 |
|
---|
841 | /**
|
---|
842 | * Implements an indirect call.
|
---|
843 | *
|
---|
844 | * @param uNewPC The new program counter (RIP) value (loaded from the
|
---|
845 | * operand).
|
---|
846 | */
|
---|
847 | IEM_CIMPL_DEF_1(iemCImpl_call_16, uint16_t, uNewPC)
|
---|
848 | {
|
---|
849 | uint16_t const uOldPC = pVCpu->cpum.GstCtx.ip + cbInstr;
|
---|
850 | if (uNewPC <= pVCpu->cpum.GstCtx.cs.u32Limit)
|
---|
851 | {
|
---|
852 | VBOXSTRICTRC rcStrict = iemMemStackPushU16(pVCpu, uOldPC);
|
---|
853 | if (rcStrict == VINF_SUCCESS)
|
---|
854 | {
|
---|
855 | pVCpu->cpum.GstCtx.rip = uNewPC;
|
---|
856 | IEM_FLUSH_PREFETCH_LIGHT(pVCpu, cbInstr);
|
---|
857 | return iemRegFinishClearingRF(pVCpu);
|
---|
858 | }
|
---|
859 | return rcStrict;
|
---|
860 | }
|
---|
861 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
862 | }
|
---|
863 |
|
---|
864 |
|
---|
865 | /**
|
---|
866 | * Implements a 16-bit relative call.
|
---|
867 | *
|
---|
868 | * @param offDisp The displacment offset.
|
---|
869 | */
|
---|
870 | IEM_CIMPL_DEF_1(iemCImpl_call_rel_16, int16_t, offDisp)
|
---|
871 | {
|
---|
872 | uint16_t const uOldPC = pVCpu->cpum.GstCtx.ip + cbInstr;
|
---|
873 | uint16_t const uNewPC = uOldPC + offDisp;
|
---|
874 | if (uNewPC <= pVCpu->cpum.GstCtx.cs.u32Limit)
|
---|
875 | {
|
---|
876 | VBOXSTRICTRC rcStrict = iemMemStackPushU16(pVCpu, uOldPC);
|
---|
877 | if (rcStrict == VINF_SUCCESS)
|
---|
878 | {
|
---|
879 | pVCpu->cpum.GstCtx.rip = uNewPC;
|
---|
880 | IEM_FLUSH_PREFETCH_LIGHT(pVCpu, cbInstr);
|
---|
881 | return iemRegFinishClearingRF(pVCpu);
|
---|
882 | }
|
---|
883 | return rcStrict;
|
---|
884 | }
|
---|
885 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
886 | }
|
---|
887 |
|
---|
888 |
|
---|
889 | /**
|
---|
890 | * Implements a 32-bit indirect call.
|
---|
891 | *
|
---|
892 | * @param uNewPC The new program counter (RIP) value (loaded from the
|
---|
893 | * operand).
|
---|
894 | */
|
---|
895 | IEM_CIMPL_DEF_1(iemCImpl_call_32, uint32_t, uNewPC)
|
---|
896 | {
|
---|
897 | uint32_t const uOldPC = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
898 | if (uNewPC <= pVCpu->cpum.GstCtx.cs.u32Limit)
|
---|
899 | {
|
---|
900 | VBOXSTRICTRC rcStrict = iemMemStackPushU32(pVCpu, uOldPC);
|
---|
901 | if (rcStrict == VINF_SUCCESS)
|
---|
902 | {
|
---|
903 | pVCpu->cpum.GstCtx.rip = uNewPC;
|
---|
904 | IEM_FLUSH_PREFETCH_LIGHT(pVCpu, cbInstr);
|
---|
905 | return iemRegFinishClearingRF(pVCpu);
|
---|
906 | }
|
---|
907 | return rcStrict;
|
---|
908 | }
|
---|
909 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
910 | }
|
---|
911 |
|
---|
912 |
|
---|
913 | /**
|
---|
914 | * Implements a 32-bit relative call.
|
---|
915 | *
|
---|
916 | * @param offDisp The displacment offset.
|
---|
917 | */
|
---|
918 | IEM_CIMPL_DEF_1(iemCImpl_call_rel_32, int32_t, offDisp)
|
---|
919 | {
|
---|
920 | uint32_t const uOldPC = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
921 | uint32_t const uNewPC = uOldPC + offDisp;
|
---|
922 | if (uNewPC <= pVCpu->cpum.GstCtx.cs.u32Limit)
|
---|
923 | {
|
---|
924 | VBOXSTRICTRC rcStrict = iemMemStackPushU32(pVCpu, uOldPC);
|
---|
925 | if (rcStrict == VINF_SUCCESS)
|
---|
926 | {
|
---|
927 | pVCpu->cpum.GstCtx.rip = uNewPC;
|
---|
928 | IEM_FLUSH_PREFETCH_LIGHT(pVCpu, cbInstr);
|
---|
929 | return iemRegFinishClearingRF(pVCpu);
|
---|
930 | }
|
---|
931 | return rcStrict;
|
---|
932 | }
|
---|
933 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
934 | }
|
---|
935 |
|
---|
936 |
|
---|
937 | /**
|
---|
938 | * Implements a 64-bit indirect call.
|
---|
939 | *
|
---|
940 | * @param uNewPC The new program counter (RIP) value (loaded from the
|
---|
941 | * operand).
|
---|
942 | */
|
---|
943 | IEM_CIMPL_DEF_1(iemCImpl_call_64, uint64_t, uNewPC)
|
---|
944 | {
|
---|
945 | uint64_t const uOldPC = pVCpu->cpum.GstCtx.rip + cbInstr;
|
---|
946 | if (IEM_IS_CANONICAL(uNewPC))
|
---|
947 | {
|
---|
948 | VBOXSTRICTRC rcStrict = iemMemStackPushU64(pVCpu, uOldPC);
|
---|
949 | if (rcStrict == VINF_SUCCESS)
|
---|
950 | {
|
---|
951 | pVCpu->cpum.GstCtx.rip = uNewPC;
|
---|
952 | IEM_FLUSH_PREFETCH_LIGHT(pVCpu, cbInstr);
|
---|
953 | return iemRegFinishClearingRF(pVCpu);
|
---|
954 | }
|
---|
955 | return rcStrict;
|
---|
956 | }
|
---|
957 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
958 | }
|
---|
959 |
|
---|
960 |
|
---|
961 | /**
|
---|
962 | * Implements a 64-bit relative call.
|
---|
963 | *
|
---|
964 | * @param offDisp The displacment offset.
|
---|
965 | */
|
---|
966 | IEM_CIMPL_DEF_1(iemCImpl_call_rel_64, int64_t, offDisp)
|
---|
967 | {
|
---|
968 | uint64_t const uOldPC = pVCpu->cpum.GstCtx.rip + cbInstr;
|
---|
969 | uint64_t const uNewPC = uOldPC + offDisp;
|
---|
970 | if (IEM_IS_CANONICAL(uNewPC))
|
---|
971 | {
|
---|
972 | VBOXSTRICTRC rcStrict = iemMemStackPushU64(pVCpu, uOldPC);
|
---|
973 | if (rcStrict == VINF_SUCCESS)
|
---|
974 | {
|
---|
975 | pVCpu->cpum.GstCtx.rip = uNewPC;
|
---|
976 | IEM_FLUSH_PREFETCH_LIGHT(pVCpu, cbInstr);
|
---|
977 | return iemRegFinishClearingRF(pVCpu);
|
---|
978 | }
|
---|
979 | return rcStrict;
|
---|
980 | }
|
---|
981 | return iemRaiseNotCanonical(pVCpu);
|
---|
982 | }
|
---|
983 |
|
---|
984 |
|
---|
985 | /**
|
---|
986 | * Implements far jumps and calls thru task segments (TSS).
|
---|
987 | *
|
---|
988 | * @returns VBox strict status code.
|
---|
989 | * @param pVCpu The cross context virtual CPU structure of the
|
---|
990 | * calling thread.
|
---|
991 | * @param cbInstr The current instruction length.
|
---|
992 | * @param uSel The selector.
|
---|
993 | * @param enmBranch The kind of branching we're performing.
|
---|
994 | * @param enmEffOpSize The effective operand size.
|
---|
995 | * @param pDesc The descriptor corresponding to @a uSel. The type is
|
---|
996 | * task gate.
|
---|
997 | */
|
---|
998 | static VBOXSTRICTRC iemCImpl_BranchTaskSegment(PVMCPUCC pVCpu, uint8_t cbInstr, uint16_t uSel, IEMBRANCH enmBranch,
|
---|
999 | IEMMODE enmEffOpSize, PIEMSELDESC pDesc)
|
---|
1000 | {
|
---|
1001 | #ifndef IEM_IMPLEMENTS_TASKSWITCH
|
---|
1002 | IEM_RETURN_ASPECT_NOT_IMPLEMENTED();
|
---|
1003 | #else
|
---|
1004 | Assert(enmBranch == IEMBRANCH_JUMP || enmBranch == IEMBRANCH_CALL);
|
---|
1005 | Assert( pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_286_TSS_AVAIL
|
---|
1006 | || pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_386_TSS_AVAIL);
|
---|
1007 | RT_NOREF_PV(enmEffOpSize);
|
---|
1008 | IEM_CTX_ASSERT(pVCpu, IEM_CPUMCTX_EXTRN_XCPT_MASK);
|
---|
1009 |
|
---|
1010 | if ( pDesc->Legacy.Gate.u2Dpl < IEM_GET_CPL(pVCpu)
|
---|
1011 | || pDesc->Legacy.Gate.u2Dpl < (uSel & X86_SEL_RPL))
|
---|
1012 | {
|
---|
1013 | Log(("BranchTaskSegment invalid priv. uSel=%04x TSS DPL=%d CPL=%u Sel RPL=%u -> #GP\n", uSel, pDesc->Legacy.Gate.u2Dpl,
|
---|
1014 | IEM_GET_CPL(pVCpu), (uSel & X86_SEL_RPL)));
|
---|
1015 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel & X86_SEL_MASK_OFF_RPL);
|
---|
1016 | }
|
---|
1017 |
|
---|
1018 | /** @todo This is checked earlier for far jumps (see iemCImpl_FarJmp) but not
|
---|
1019 | * far calls (see iemCImpl_callf). Most likely in both cases it should be
|
---|
1020 | * checked here, need testcases. */
|
---|
1021 | if (!pDesc->Legacy.Gen.u1Present)
|
---|
1022 | {
|
---|
1023 | Log(("BranchTaskSegment TSS not present uSel=%04x -> #NP\n", uSel));
|
---|
1024 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uSel & X86_SEL_MASK_OFF_RPL);
|
---|
1025 | }
|
---|
1026 |
|
---|
1027 | uint32_t uNextEip = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
1028 | return iemTaskSwitch(pVCpu, enmBranch == IEMBRANCH_JUMP ? IEMTASKSWITCH_JUMP : IEMTASKSWITCH_CALL,
|
---|
1029 | uNextEip, 0 /* fFlags */, 0 /* uErr */, 0 /* uCr2 */, uSel, pDesc);
|
---|
1030 | #endif
|
---|
1031 | }
|
---|
1032 |
|
---|
1033 |
|
---|
1034 | /**
|
---|
1035 | * Implements far jumps and calls thru task gates.
|
---|
1036 | *
|
---|
1037 | * @returns VBox strict status code.
|
---|
1038 | * @param pVCpu The cross context virtual CPU structure of the
|
---|
1039 | * calling thread.
|
---|
1040 | * @param cbInstr The current instruction length.
|
---|
1041 | * @param uSel The selector.
|
---|
1042 | * @param enmBranch The kind of branching we're performing.
|
---|
1043 | * @param enmEffOpSize The effective operand size.
|
---|
1044 | * @param pDesc The descriptor corresponding to @a uSel. The type is
|
---|
1045 | * task gate.
|
---|
1046 | */
|
---|
1047 | static VBOXSTRICTRC iemCImpl_BranchTaskGate(PVMCPUCC pVCpu, uint8_t cbInstr, uint16_t uSel, IEMBRANCH enmBranch,
|
---|
1048 | IEMMODE enmEffOpSize, PIEMSELDESC pDesc)
|
---|
1049 | {
|
---|
1050 | #ifndef IEM_IMPLEMENTS_TASKSWITCH
|
---|
1051 | IEM_RETURN_ASPECT_NOT_IMPLEMENTED();
|
---|
1052 | #else
|
---|
1053 | Assert(enmBranch == IEMBRANCH_JUMP || enmBranch == IEMBRANCH_CALL);
|
---|
1054 | RT_NOREF_PV(enmEffOpSize);
|
---|
1055 | IEM_CTX_ASSERT(pVCpu, IEM_CPUMCTX_EXTRN_XCPT_MASK);
|
---|
1056 |
|
---|
1057 | if ( pDesc->Legacy.Gate.u2Dpl < IEM_GET_CPL(pVCpu)
|
---|
1058 | || pDesc->Legacy.Gate.u2Dpl < (uSel & X86_SEL_RPL))
|
---|
1059 | {
|
---|
1060 | Log(("BranchTaskGate invalid priv. uSel=%04x TSS DPL=%d CPL=%u Sel RPL=%u -> #GP\n", uSel, pDesc->Legacy.Gate.u2Dpl,
|
---|
1061 | IEM_GET_CPL(pVCpu), (uSel & X86_SEL_RPL)));
|
---|
1062 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel & X86_SEL_MASK_OFF_RPL);
|
---|
1063 | }
|
---|
1064 |
|
---|
1065 | /** @todo This is checked earlier for far jumps (see iemCImpl_FarJmp) but not
|
---|
1066 | * far calls (see iemCImpl_callf). Most likely in both cases it should be
|
---|
1067 | * checked here, need testcases. */
|
---|
1068 | if (!pDesc->Legacy.Gen.u1Present)
|
---|
1069 | {
|
---|
1070 | Log(("BranchTaskSegment segment not present uSel=%04x -> #NP\n", uSel));
|
---|
1071 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uSel & X86_SEL_MASK_OFF_RPL);
|
---|
1072 | }
|
---|
1073 |
|
---|
1074 | /*
|
---|
1075 | * Fetch the new TSS descriptor from the GDT.
|
---|
1076 | */
|
---|
1077 | RTSEL uSelTss = pDesc->Legacy.Gate.u16Sel;
|
---|
1078 | if (uSelTss & X86_SEL_LDT)
|
---|
1079 | {
|
---|
1080 | Log(("BranchTaskGate TSS is in LDT. uSel=%04x uSelTss=%04x -> #GP\n", uSel, uSelTss));
|
---|
1081 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel & X86_SEL_MASK_OFF_RPL);
|
---|
1082 | }
|
---|
1083 |
|
---|
1084 | IEMSELDESC TssDesc;
|
---|
1085 | VBOXSTRICTRC rcStrict = iemMemFetchSelDesc(pVCpu, &TssDesc, uSelTss, X86_XCPT_GP);
|
---|
1086 | if (rcStrict != VINF_SUCCESS)
|
---|
1087 | return rcStrict;
|
---|
1088 |
|
---|
1089 | if (TssDesc.Legacy.Gate.u4Type & X86_SEL_TYPE_SYS_TSS_BUSY_MASK)
|
---|
1090 | {
|
---|
1091 | Log(("BranchTaskGate TSS is busy. uSel=%04x uSelTss=%04x DescType=%#x -> #GP\n", uSel, uSelTss,
|
---|
1092 | TssDesc.Legacy.Gate.u4Type));
|
---|
1093 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel & X86_SEL_MASK_OFF_RPL);
|
---|
1094 | }
|
---|
1095 |
|
---|
1096 | if (!TssDesc.Legacy.Gate.u1Present)
|
---|
1097 | {
|
---|
1098 | Log(("BranchTaskGate TSS is not present. uSel=%04x uSelTss=%04x -> #NP\n", uSel, uSelTss));
|
---|
1099 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uSelTss & X86_SEL_MASK_OFF_RPL);
|
---|
1100 | }
|
---|
1101 |
|
---|
1102 | uint32_t uNextEip = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
1103 | return iemTaskSwitch(pVCpu, enmBranch == IEMBRANCH_JUMP ? IEMTASKSWITCH_JUMP : IEMTASKSWITCH_CALL,
|
---|
1104 | uNextEip, 0 /* fFlags */, 0 /* uErr */, 0 /* uCr2 */, uSelTss, &TssDesc);
|
---|
1105 | #endif
|
---|
1106 | }
|
---|
1107 |
|
---|
1108 |
|
---|
1109 | /**
|
---|
1110 | * Implements far jumps and calls thru call gates.
|
---|
1111 | *
|
---|
1112 | * @returns VBox strict status code.
|
---|
1113 | * @param pVCpu The cross context virtual CPU structure of the
|
---|
1114 | * calling thread.
|
---|
1115 | * @param cbInstr The current instruction length.
|
---|
1116 | * @param uSel The selector.
|
---|
1117 | * @param enmBranch The kind of branching we're performing.
|
---|
1118 | * @param enmEffOpSize The effective operand size.
|
---|
1119 | * @param pDesc The descriptor corresponding to @a uSel. The type is
|
---|
1120 | * call gate.
|
---|
1121 | */
|
---|
1122 | static VBOXSTRICTRC iemCImpl_BranchCallGate(PVMCPUCC pVCpu, uint8_t cbInstr, uint16_t uSel, IEMBRANCH enmBranch,
|
---|
1123 | IEMMODE enmEffOpSize, PIEMSELDESC pDesc)
|
---|
1124 | {
|
---|
1125 | #define IEM_IMPLEMENTS_CALLGATE
|
---|
1126 | #ifndef IEM_IMPLEMENTS_CALLGATE
|
---|
1127 | IEM_RETURN_ASPECT_NOT_IMPLEMENTED();
|
---|
1128 | #else
|
---|
1129 | RT_NOREF_PV(enmEffOpSize);
|
---|
1130 | IEM_CTX_ASSERT(pVCpu, IEM_CPUMCTX_EXTRN_XCPT_MASK);
|
---|
1131 |
|
---|
1132 | /* NB: Far jumps can only do intra-privilege transfers. Far calls support
|
---|
1133 | * inter-privilege calls and are much more complex.
|
---|
1134 | *
|
---|
1135 | * NB: 64-bit call gate has the same type as a 32-bit call gate! If
|
---|
1136 | * EFER.LMA=1, the gate must be 64-bit. Conversely if EFER.LMA=0, the gate
|
---|
1137 | * must be 16-bit or 32-bit.
|
---|
1138 | */
|
---|
1139 | /** @todo effective operand size is probably irrelevant here, only the
|
---|
1140 | * call gate bitness matters??
|
---|
1141 | */
|
---|
1142 | VBOXSTRICTRC rcStrict;
|
---|
1143 | RTPTRUNION uPtrRet;
|
---|
1144 | uint64_t uNewRsp;
|
---|
1145 | uint64_t uNewRip;
|
---|
1146 | uint64_t u64Base;
|
---|
1147 | uint32_t cbLimit;
|
---|
1148 | RTSEL uNewCS;
|
---|
1149 | IEMSELDESC DescCS;
|
---|
1150 |
|
---|
1151 | AssertCompile(X86_SEL_TYPE_SYS_386_CALL_GATE == AMD64_SEL_TYPE_SYS_CALL_GATE);
|
---|
1152 | Assert(enmBranch == IEMBRANCH_JUMP || enmBranch == IEMBRANCH_CALL);
|
---|
1153 | Assert( pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_286_CALL_GATE
|
---|
1154 | || pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_386_CALL_GATE);
|
---|
1155 |
|
---|
1156 | /* Determine the new instruction pointer from the gate descriptor. */
|
---|
1157 | uNewRip = pDesc->Legacy.Gate.u16OffsetLow
|
---|
1158 | | ((uint32_t)pDesc->Legacy.Gate.u16OffsetHigh << 16)
|
---|
1159 | | ((uint64_t)pDesc->Long.Gate.u32OffsetTop << 32);
|
---|
1160 |
|
---|
1161 | /* Perform DPL checks on the gate descriptor. */
|
---|
1162 | if ( pDesc->Legacy.Gate.u2Dpl < IEM_GET_CPL(pVCpu)
|
---|
1163 | || pDesc->Legacy.Gate.u2Dpl < (uSel & X86_SEL_RPL))
|
---|
1164 | {
|
---|
1165 | Log(("BranchCallGate invalid priv. uSel=%04x Gate DPL=%d CPL=%u Sel RPL=%u -> #GP\n", uSel, pDesc->Legacy.Gate.u2Dpl,
|
---|
1166 | IEM_GET_CPL(pVCpu), (uSel & X86_SEL_RPL)));
|
---|
1167 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1168 | }
|
---|
1169 |
|
---|
1170 | /** @todo does this catch NULL selectors, too? */
|
---|
1171 | if (!pDesc->Legacy.Gen.u1Present)
|
---|
1172 | {
|
---|
1173 | Log(("BranchCallGate Gate not present uSel=%04x -> #NP\n", uSel));
|
---|
1174 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uSel);
|
---|
1175 | }
|
---|
1176 |
|
---|
1177 | /*
|
---|
1178 | * Fetch the target CS descriptor from the GDT or LDT.
|
---|
1179 | */
|
---|
1180 | uNewCS = pDesc->Legacy.Gate.u16Sel;
|
---|
1181 | rcStrict = iemMemFetchSelDesc(pVCpu, &DescCS, uNewCS, X86_XCPT_GP);
|
---|
1182 | if (rcStrict != VINF_SUCCESS)
|
---|
1183 | return rcStrict;
|
---|
1184 |
|
---|
1185 | /* Target CS must be a code selector. */
|
---|
1186 | if ( !DescCS.Legacy.Gen.u1DescType
|
---|
1187 | || !(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE) )
|
---|
1188 | {
|
---|
1189 | Log(("BranchCallGate %04x:%08RX64 -> not a code selector (u1DescType=%u u4Type=%#x).\n",
|
---|
1190 | uNewCS, uNewRip, DescCS.Legacy.Gen.u1DescType, DescCS.Legacy.Gen.u4Type));
|
---|
1191 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCS);
|
---|
1192 | }
|
---|
1193 |
|
---|
1194 | /* Privilege checks on target CS. */
|
---|
1195 | if (enmBranch == IEMBRANCH_JUMP)
|
---|
1196 | {
|
---|
1197 | if (DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF)
|
---|
1198 | {
|
---|
1199 | if (DescCS.Legacy.Gen.u2Dpl > IEM_GET_CPL(pVCpu))
|
---|
1200 | {
|
---|
1201 | Log(("BranchCallGate jump (conforming) bad DPL uNewCS=%04x Gate DPL=%d CPL=%u -> #GP\n",
|
---|
1202 | uNewCS, DescCS.Legacy.Gen.u2Dpl, IEM_GET_CPL(pVCpu)));
|
---|
1203 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCS);
|
---|
1204 | }
|
---|
1205 | }
|
---|
1206 | else
|
---|
1207 | {
|
---|
1208 | if (DescCS.Legacy.Gen.u2Dpl != IEM_GET_CPL(pVCpu))
|
---|
1209 | {
|
---|
1210 | Log(("BranchCallGate jump (non-conforming) bad DPL uNewCS=%04x Gate DPL=%d CPL=%u -> #GP\n",
|
---|
1211 | uNewCS, DescCS.Legacy.Gen.u2Dpl, IEM_GET_CPL(pVCpu)));
|
---|
1212 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCS);
|
---|
1213 | }
|
---|
1214 | }
|
---|
1215 | }
|
---|
1216 | else
|
---|
1217 | {
|
---|
1218 | Assert(enmBranch == IEMBRANCH_CALL);
|
---|
1219 | if (DescCS.Legacy.Gen.u2Dpl > IEM_GET_CPL(pVCpu))
|
---|
1220 | {
|
---|
1221 | Log(("BranchCallGate call invalid priv. uNewCS=%04x Gate DPL=%d CPL=%u -> #GP\n",
|
---|
1222 | uNewCS, DescCS.Legacy.Gen.u2Dpl, IEM_GET_CPL(pVCpu)));
|
---|
1223 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCS & X86_SEL_MASK_OFF_RPL);
|
---|
1224 | }
|
---|
1225 | }
|
---|
1226 |
|
---|
1227 | /* Additional long mode checks. */
|
---|
1228 | if (IEM_IS_LONG_MODE(pVCpu))
|
---|
1229 | {
|
---|
1230 | if (!DescCS.Legacy.Gen.u1Long)
|
---|
1231 | {
|
---|
1232 | Log(("BranchCallGate uNewCS %04x -> not a 64-bit code segment.\n", uNewCS));
|
---|
1233 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCS);
|
---|
1234 | }
|
---|
1235 |
|
---|
1236 | /* L vs D. */
|
---|
1237 | if ( DescCS.Legacy.Gen.u1Long
|
---|
1238 | && DescCS.Legacy.Gen.u1DefBig)
|
---|
1239 | {
|
---|
1240 | Log(("BranchCallGate uNewCS %04x -> both L and D are set.\n", uNewCS));
|
---|
1241 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCS);
|
---|
1242 | }
|
---|
1243 | }
|
---|
1244 |
|
---|
1245 | if (!DescCS.Legacy.Gate.u1Present)
|
---|
1246 | {
|
---|
1247 | Log(("BranchCallGate target CS is not present. uSel=%04x uNewCS=%04x -> #NP(CS)\n", uSel, uNewCS));
|
---|
1248 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uNewCS);
|
---|
1249 | }
|
---|
1250 |
|
---|
1251 | if (enmBranch == IEMBRANCH_JUMP)
|
---|
1252 | {
|
---|
1253 | /** @todo This is very similar to regular far jumps; merge! */
|
---|
1254 | /* Jumps are fairly simple... */
|
---|
1255 |
|
---|
1256 | /* Chop the high bits off if 16-bit gate (Intel says so). */
|
---|
1257 | if (pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_286_CALL_GATE)
|
---|
1258 | uNewRip = (uint16_t)uNewRip;
|
---|
1259 |
|
---|
1260 | /* Limit check for non-long segments. */
|
---|
1261 | cbLimit = X86DESC_LIMIT_G(&DescCS.Legacy);
|
---|
1262 | if (DescCS.Legacy.Gen.u1Long)
|
---|
1263 | u64Base = 0;
|
---|
1264 | else
|
---|
1265 | {
|
---|
1266 | if (uNewRip > cbLimit)
|
---|
1267 | {
|
---|
1268 | Log(("BranchCallGate jump %04x:%08RX64 -> out of bounds (%#x) -> #GP(0)\n", uNewCS, uNewRip, cbLimit));
|
---|
1269 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, 0);
|
---|
1270 | }
|
---|
1271 | u64Base = X86DESC_BASE(&DescCS.Legacy);
|
---|
1272 | }
|
---|
1273 |
|
---|
1274 | /* Canonical address check. */
|
---|
1275 | if (!IEM_IS_CANONICAL(uNewRip))
|
---|
1276 | {
|
---|
1277 | Log(("BranchCallGate jump %04x:%016RX64 - not canonical -> #GP\n", uNewCS, uNewRip));
|
---|
1278 | return iemRaiseNotCanonical(pVCpu);
|
---|
1279 | }
|
---|
1280 |
|
---|
1281 | /*
|
---|
1282 | * Ok, everything checked out fine. Now set the accessed bit before
|
---|
1283 | * committing the result into CS, CSHID and RIP.
|
---|
1284 | */
|
---|
1285 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
1286 | {
|
---|
1287 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewCS);
|
---|
1288 | if (rcStrict != VINF_SUCCESS)
|
---|
1289 | return rcStrict;
|
---|
1290 | /** @todo check what VT-x and AMD-V does. */
|
---|
1291 | DescCS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
1292 | }
|
---|
1293 |
|
---|
1294 | /* commit */
|
---|
1295 | pVCpu->cpum.GstCtx.rip = uNewRip;
|
---|
1296 | pVCpu->cpum.GstCtx.cs.Sel = uNewCS & X86_SEL_MASK_OFF_RPL;
|
---|
1297 | pVCpu->cpum.GstCtx.cs.Sel |= IEM_GET_CPL(pVCpu); /** @todo is this right for conforming segs? or in general? */
|
---|
1298 | pVCpu->cpum.GstCtx.cs.ValidSel = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
1299 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
1300 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCS.Legacy);
|
---|
1301 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimit;
|
---|
1302 | pVCpu->cpum.GstCtx.cs.u64Base = u64Base;
|
---|
1303 | }
|
---|
1304 | else
|
---|
1305 | {
|
---|
1306 | Assert(enmBranch == IEMBRANCH_CALL);
|
---|
1307 | /* Calls are much more complicated. */
|
---|
1308 |
|
---|
1309 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF) && (DescCS.Legacy.Gen.u2Dpl < IEM_GET_CPL(pVCpu)))
|
---|
1310 | {
|
---|
1311 | uint16_t offNewStack; /* Offset of new stack in TSS. */
|
---|
1312 | uint16_t cbNewStack; /* Number of bytes the stack information takes up in TSS. */
|
---|
1313 | uint8_t uNewCSDpl;
|
---|
1314 | uint8_t cbWords;
|
---|
1315 | RTSEL uNewSS;
|
---|
1316 | RTSEL uOldSS;
|
---|
1317 | uint64_t uOldRsp;
|
---|
1318 | IEMSELDESC DescSS;
|
---|
1319 | RTPTRUNION uPtrTSS;
|
---|
1320 | RTGCPTR GCPtrTSS;
|
---|
1321 | RTPTRUNION uPtrParmWds;
|
---|
1322 | RTGCPTR GCPtrParmWds;
|
---|
1323 |
|
---|
1324 | /* More privilege. This is the fun part. */
|
---|
1325 | Assert(!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF)); /* Filtered out above. */
|
---|
1326 |
|
---|
1327 | /*
|
---|
1328 | * Determine new SS:rSP from the TSS.
|
---|
1329 | */
|
---|
1330 | Assert(!pVCpu->cpum.GstCtx.tr.Attr.n.u1DescType);
|
---|
1331 |
|
---|
1332 | /* Figure out where the new stack pointer is stored in the TSS. */
|
---|
1333 | uNewCSDpl = DescCS.Legacy.Gen.u2Dpl;
|
---|
1334 | if (!IEM_IS_LONG_MODE(pVCpu))
|
---|
1335 | {
|
---|
1336 | if (pVCpu->cpum.GstCtx.tr.Attr.n.u4Type == X86_SEL_TYPE_SYS_386_TSS_BUSY)
|
---|
1337 | {
|
---|
1338 | offNewStack = RT_UOFFSETOF(X86TSS32, esp0) + uNewCSDpl * 8;
|
---|
1339 | cbNewStack = RT_SIZEOFMEMB(X86TSS32, esp0) + RT_SIZEOFMEMB(X86TSS32, ss0);
|
---|
1340 | }
|
---|
1341 | else
|
---|
1342 | {
|
---|
1343 | Assert(pVCpu->cpum.GstCtx.tr.Attr.n.u4Type == X86_SEL_TYPE_SYS_286_TSS_BUSY);
|
---|
1344 | offNewStack = RT_UOFFSETOF(X86TSS16, sp0) + uNewCSDpl * 4;
|
---|
1345 | cbNewStack = RT_SIZEOFMEMB(X86TSS16, sp0) + RT_SIZEOFMEMB(X86TSS16, ss0);
|
---|
1346 | }
|
---|
1347 | }
|
---|
1348 | else
|
---|
1349 | {
|
---|
1350 | Assert(pVCpu->cpum.GstCtx.tr.Attr.n.u4Type == AMD64_SEL_TYPE_SYS_TSS_BUSY);
|
---|
1351 | offNewStack = RT_UOFFSETOF(X86TSS64, rsp0) + uNewCSDpl * RT_SIZEOFMEMB(X86TSS64, rsp0);
|
---|
1352 | cbNewStack = RT_SIZEOFMEMB(X86TSS64, rsp0);
|
---|
1353 | }
|
---|
1354 |
|
---|
1355 | /* Check against TSS limit. */
|
---|
1356 | if ((uint16_t)(offNewStack + cbNewStack - 1) > pVCpu->cpum.GstCtx.tr.u32Limit)
|
---|
1357 | {
|
---|
1358 | Log(("BranchCallGate inner stack past TSS limit - %u > %u -> #TS(TSS)\n", offNewStack + cbNewStack - 1, pVCpu->cpum.GstCtx.tr.u32Limit));
|
---|
1359 | return iemRaiseTaskSwitchFaultBySelector(pVCpu, pVCpu->cpum.GstCtx.tr.Sel);
|
---|
1360 | }
|
---|
1361 |
|
---|
1362 | GCPtrTSS = pVCpu->cpum.GstCtx.tr.u64Base + offNewStack;
|
---|
1363 | rcStrict = iemMemMap(pVCpu, &uPtrTSS.pv, cbNewStack, UINT8_MAX, GCPtrTSS, IEM_ACCESS_SYS_R, 0);
|
---|
1364 | if (rcStrict != VINF_SUCCESS)
|
---|
1365 | {
|
---|
1366 | Log(("BranchCallGate: TSS mapping failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1367 | return rcStrict;
|
---|
1368 | }
|
---|
1369 |
|
---|
1370 | if (!IEM_IS_LONG_MODE(pVCpu))
|
---|
1371 | {
|
---|
1372 | if (pVCpu->cpum.GstCtx.tr.Attr.n.u4Type == X86_SEL_TYPE_SYS_386_TSS_BUSY)
|
---|
1373 | {
|
---|
1374 | uNewRsp = uPtrTSS.pu32[0];
|
---|
1375 | uNewSS = uPtrTSS.pu16[2];
|
---|
1376 | }
|
---|
1377 | else
|
---|
1378 | {
|
---|
1379 | Assert(pVCpu->cpum.GstCtx.tr.Attr.n.u4Type == X86_SEL_TYPE_SYS_286_TSS_BUSY);
|
---|
1380 | uNewRsp = uPtrTSS.pu16[0];
|
---|
1381 | uNewSS = uPtrTSS.pu16[1];
|
---|
1382 | }
|
---|
1383 | }
|
---|
1384 | else
|
---|
1385 | {
|
---|
1386 | Assert(pVCpu->cpum.GstCtx.tr.Attr.n.u4Type == AMD64_SEL_TYPE_SYS_TSS_BUSY);
|
---|
1387 | /* SS will be a NULL selector, but that's valid. */
|
---|
1388 | uNewRsp = uPtrTSS.pu64[0];
|
---|
1389 | uNewSS = uNewCSDpl;
|
---|
1390 | }
|
---|
1391 |
|
---|
1392 | /* Done with the TSS now. */
|
---|
1393 | rcStrict = iemMemCommitAndUnmap(pVCpu, uPtrTSS.pv, IEM_ACCESS_SYS_R);
|
---|
1394 | if (rcStrict != VINF_SUCCESS)
|
---|
1395 | {
|
---|
1396 | Log(("BranchCallGate: TSS unmapping failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1397 | return rcStrict;
|
---|
1398 | }
|
---|
1399 |
|
---|
1400 | /* Only used outside of long mode. */
|
---|
1401 | cbWords = pDesc->Legacy.Gate.u5ParmCount;
|
---|
1402 |
|
---|
1403 | /* If EFER.LMA is 0, there's extra work to do. */
|
---|
1404 | if (!IEM_IS_LONG_MODE(pVCpu))
|
---|
1405 | {
|
---|
1406 | if ((uNewSS & X86_SEL_MASK_OFF_RPL) == 0)
|
---|
1407 | {
|
---|
1408 | Log(("BranchCallGate new SS NULL -> #TS(NewSS)\n"));
|
---|
1409 | return iemRaiseTaskSwitchFaultBySelector(pVCpu, uNewSS);
|
---|
1410 | }
|
---|
1411 |
|
---|
1412 | /* Grab the new SS descriptor. */
|
---|
1413 | rcStrict = iemMemFetchSelDesc(pVCpu, &DescSS, uNewSS, X86_XCPT_SS);
|
---|
1414 | if (rcStrict != VINF_SUCCESS)
|
---|
1415 | return rcStrict;
|
---|
1416 |
|
---|
1417 | /* Ensure that CS.DPL == SS.RPL == SS.DPL. */
|
---|
1418 | if ( (DescCS.Legacy.Gen.u2Dpl != (uNewSS & X86_SEL_RPL))
|
---|
1419 | || (DescCS.Legacy.Gen.u2Dpl != DescSS.Legacy.Gen.u2Dpl))
|
---|
1420 | {
|
---|
1421 | Log(("BranchCallGate call bad RPL/DPL uNewSS=%04x SS DPL=%d CS DPL=%u -> #TS(NewSS)\n",
|
---|
1422 | uNewSS, DescCS.Legacy.Gen.u2Dpl, DescCS.Legacy.Gen.u2Dpl));
|
---|
1423 | return iemRaiseTaskSwitchFaultBySelector(pVCpu, uNewSS);
|
---|
1424 | }
|
---|
1425 |
|
---|
1426 | /* Ensure new SS is a writable data segment. */
|
---|
1427 | if ((DescSS.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_WRITE)) != X86_SEL_TYPE_WRITE)
|
---|
1428 | {
|
---|
1429 | Log(("BranchCallGate call new SS -> not a writable data selector (u4Type=%#x)\n", DescSS.Legacy.Gen.u4Type));
|
---|
1430 | return iemRaiseTaskSwitchFaultBySelector(pVCpu, uNewSS);
|
---|
1431 | }
|
---|
1432 |
|
---|
1433 | if (!DescSS.Legacy.Gen.u1Present)
|
---|
1434 | {
|
---|
1435 | Log(("BranchCallGate New stack not present uSel=%04x -> #SS(NewSS)\n", uNewSS));
|
---|
1436 | return iemRaiseStackSelectorNotPresentBySelector(pVCpu, uNewSS);
|
---|
1437 | }
|
---|
1438 | if (pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_386_CALL_GATE)
|
---|
1439 | cbNewStack = (uint16_t)sizeof(uint32_t) * (4 + cbWords);
|
---|
1440 | else
|
---|
1441 | cbNewStack = (uint16_t)sizeof(uint16_t) * (4 + cbWords);
|
---|
1442 | }
|
---|
1443 | else
|
---|
1444 | {
|
---|
1445 | /* Just grab the new (NULL) SS descriptor. */
|
---|
1446 | /** @todo testcase: Check whether the zero GDT entry is actually loaded here
|
---|
1447 | * like we do... */
|
---|
1448 | rcStrict = iemMemFetchSelDesc(pVCpu, &DescSS, uNewSS, X86_XCPT_SS);
|
---|
1449 | if (rcStrict != VINF_SUCCESS)
|
---|
1450 | return rcStrict;
|
---|
1451 |
|
---|
1452 | cbNewStack = sizeof(uint64_t) * 4;
|
---|
1453 | }
|
---|
1454 |
|
---|
1455 | /** @todo According to Intel, new stack is checked for enough space first,
|
---|
1456 | * then switched. According to AMD, the stack is switched first and
|
---|
1457 | * then pushes might fault!
|
---|
1458 | * NB: OS/2 Warp 3/4 actively relies on the fact that possible
|
---|
1459 | * incoming stack \#PF happens before actual stack switch. AMD is
|
---|
1460 | * either lying or implicitly assumes that new state is committed
|
---|
1461 | * only if and when an instruction doesn't fault.
|
---|
1462 | */
|
---|
1463 |
|
---|
1464 | /** @todo According to AMD, CS is loaded first, then SS.
|
---|
1465 | * According to Intel, it's the other way around!?
|
---|
1466 | */
|
---|
1467 |
|
---|
1468 | /** @todo Intel and AMD disagree on when exactly the CPL changes! */
|
---|
1469 |
|
---|
1470 | /* Set the accessed bit before committing new SS. */
|
---|
1471 | if (!(DescSS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
1472 | {
|
---|
1473 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewSS);
|
---|
1474 | if (rcStrict != VINF_SUCCESS)
|
---|
1475 | return rcStrict;
|
---|
1476 | DescSS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
1477 | }
|
---|
1478 |
|
---|
1479 | /* Remember the old SS:rSP and their linear address. */
|
---|
1480 | uOldSS = pVCpu->cpum.GstCtx.ss.Sel;
|
---|
1481 | uOldRsp = pVCpu->cpum.GstCtx.ss.Attr.n.u1DefBig ? pVCpu->cpum.GstCtx.rsp : pVCpu->cpum.GstCtx.sp;
|
---|
1482 |
|
---|
1483 | GCPtrParmWds = pVCpu->cpum.GstCtx.ss.u64Base + uOldRsp;
|
---|
1484 |
|
---|
1485 | /* HACK ALERT! Probe if the write to the new stack will succeed. May #SS(NewSS)
|
---|
1486 | or #PF, the former is not implemented in this workaround. */
|
---|
1487 | /** @todo Proper fix callgate target stack exceptions. */
|
---|
1488 | /** @todo testcase: Cover callgates with partially or fully inaccessible
|
---|
1489 | * target stacks. */
|
---|
1490 | void *pvNewFrame;
|
---|
1491 | RTGCPTR GCPtrNewStack = X86DESC_BASE(&DescSS.Legacy) + uNewRsp - cbNewStack;
|
---|
1492 | rcStrict = iemMemMap(pVCpu, &pvNewFrame, cbNewStack, UINT8_MAX, GCPtrNewStack, IEM_ACCESS_SYS_RW, 0);
|
---|
1493 | if (rcStrict != VINF_SUCCESS)
|
---|
1494 | {
|
---|
1495 | Log(("BranchCallGate: Incoming stack (%04x:%08RX64) not accessible, rc=%Rrc\n", uNewSS, uNewRsp, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1496 | return rcStrict;
|
---|
1497 | }
|
---|
1498 | rcStrict = iemMemCommitAndUnmap(pVCpu, pvNewFrame, IEM_ACCESS_SYS_RW);
|
---|
1499 | if (rcStrict != VINF_SUCCESS)
|
---|
1500 | {
|
---|
1501 | Log(("BranchCallGate: New stack probe unmapping failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1502 | return rcStrict;
|
---|
1503 | }
|
---|
1504 |
|
---|
1505 | /* Commit new SS:rSP. */
|
---|
1506 | pVCpu->cpum.GstCtx.ss.Sel = uNewSS;
|
---|
1507 | pVCpu->cpum.GstCtx.ss.ValidSel = uNewSS;
|
---|
1508 | pVCpu->cpum.GstCtx.ss.Attr.u = X86DESC_GET_HID_ATTR(&DescSS.Legacy);
|
---|
1509 | pVCpu->cpum.GstCtx.ss.u32Limit = X86DESC_LIMIT_G(&DescSS.Legacy);
|
---|
1510 | pVCpu->cpum.GstCtx.ss.u64Base = X86DESC_BASE(&DescSS.Legacy);
|
---|
1511 | pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
1512 | pVCpu->cpum.GstCtx.rsp = uNewRsp;
|
---|
1513 | IEM_SET_CPL(pVCpu, uNewCSDpl); /** @todo Are the parameter words accessed using the new CPL or the old CPL? */
|
---|
1514 | Assert(CPUMSELREG_ARE_HIDDEN_PARTS_VALID(pVCpu, &pVCpu->cpum.GstCtx.ss));
|
---|
1515 | CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_HIDDEN_SEL_REGS);
|
---|
1516 |
|
---|
1517 | /* At this point the stack access must not fail because new state was already committed. */
|
---|
1518 | /** @todo this can still fail due to SS.LIMIT not check. */
|
---|
1519 | rcStrict = iemMemStackPushBeginSpecial(pVCpu, cbNewStack,
|
---|
1520 | IEM_IS_LONG_MODE(pVCpu) ? 7
|
---|
1521 | : pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_386_CALL_GATE ? 3 : 1,
|
---|
1522 | &uPtrRet.pv, &uNewRsp);
|
---|
1523 | AssertMsgReturn(rcStrict == VINF_SUCCESS, ("BranchCallGate: New stack mapping failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)),
|
---|
1524 | VERR_INTERNAL_ERROR_5);
|
---|
1525 |
|
---|
1526 | if (!IEM_IS_LONG_MODE(pVCpu))
|
---|
1527 | {
|
---|
1528 | if (pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_386_CALL_GATE)
|
---|
1529 | {
|
---|
1530 | if (cbWords)
|
---|
1531 | {
|
---|
1532 | /* Map the relevant chunk of the old stack. */
|
---|
1533 | rcStrict = iemMemMap(pVCpu, &uPtrParmWds.pv, cbWords * 4, UINT8_MAX, GCPtrParmWds,
|
---|
1534 | IEM_ACCESS_DATA_R, 0 /** @todo Can uNewCSDpl == 3? Then we need alignment mask here! */);
|
---|
1535 | if (rcStrict != VINF_SUCCESS)
|
---|
1536 | {
|
---|
1537 | Log(("BranchCallGate: Old stack mapping (32-bit) failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1538 | return rcStrict;
|
---|
1539 | }
|
---|
1540 |
|
---|
1541 | /* Copy the parameter (d)words. */
|
---|
1542 | for (int i = 0; i < cbWords; ++i)
|
---|
1543 | uPtrRet.pu32[2 + i] = uPtrParmWds.pu32[i];
|
---|
1544 |
|
---|
1545 | /* Unmap the old stack. */
|
---|
1546 | rcStrict = iemMemCommitAndUnmap(pVCpu, uPtrParmWds.pv, IEM_ACCESS_DATA_R);
|
---|
1547 | if (rcStrict != VINF_SUCCESS)
|
---|
1548 | {
|
---|
1549 | Log(("BranchCallGate: Old stack unmapping (32-bit) failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1550 | return rcStrict;
|
---|
1551 | }
|
---|
1552 | }
|
---|
1553 |
|
---|
1554 | /* Push the old CS:rIP. */
|
---|
1555 | uPtrRet.pu32[0] = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
1556 | uPtrRet.pu32[1] = pVCpu->cpum.GstCtx.cs.Sel; /** @todo Testcase: What is written to the high word when pushing CS? */
|
---|
1557 |
|
---|
1558 | /* Push the old SS:rSP. */
|
---|
1559 | uPtrRet.pu32[2 + cbWords + 0] = uOldRsp;
|
---|
1560 | uPtrRet.pu32[2 + cbWords + 1] = uOldSS;
|
---|
1561 | }
|
---|
1562 | else
|
---|
1563 | {
|
---|
1564 | Assert(pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_286_CALL_GATE);
|
---|
1565 |
|
---|
1566 | if (cbWords)
|
---|
1567 | {
|
---|
1568 | /* Map the relevant chunk of the old stack. */
|
---|
1569 | rcStrict = iemMemMap(pVCpu, &uPtrParmWds.pv, cbWords * 2, UINT8_MAX, GCPtrParmWds,
|
---|
1570 | IEM_ACCESS_DATA_R, 0 /** @todo Can uNewCSDpl == 3? Then we need alignment mask here! */);
|
---|
1571 | if (rcStrict != VINF_SUCCESS)
|
---|
1572 | {
|
---|
1573 | Log(("BranchCallGate: Old stack mapping (16-bit) failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1574 | return rcStrict;
|
---|
1575 | }
|
---|
1576 |
|
---|
1577 | /* Copy the parameter words. */
|
---|
1578 | for (int i = 0; i < cbWords; ++i)
|
---|
1579 | uPtrRet.pu16[2 + i] = uPtrParmWds.pu16[i];
|
---|
1580 |
|
---|
1581 | /* Unmap the old stack. */
|
---|
1582 | rcStrict = iemMemCommitAndUnmap(pVCpu, uPtrParmWds.pv, IEM_ACCESS_DATA_R);
|
---|
1583 | if (rcStrict != VINF_SUCCESS)
|
---|
1584 | {
|
---|
1585 | Log(("BranchCallGate: Old stack unmapping (32-bit) failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1586 | return rcStrict;
|
---|
1587 | }
|
---|
1588 | }
|
---|
1589 |
|
---|
1590 | /* Push the old CS:rIP. */
|
---|
1591 | uPtrRet.pu16[0] = pVCpu->cpum.GstCtx.ip + cbInstr;
|
---|
1592 | uPtrRet.pu16[1] = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
1593 |
|
---|
1594 | /* Push the old SS:rSP. */
|
---|
1595 | uPtrRet.pu16[2 + cbWords + 0] = uOldRsp;
|
---|
1596 | uPtrRet.pu16[2 + cbWords + 1] = uOldSS;
|
---|
1597 | }
|
---|
1598 | }
|
---|
1599 | else
|
---|
1600 | {
|
---|
1601 | Assert(pDesc->Legacy.Gate.u4Type == AMD64_SEL_TYPE_SYS_CALL_GATE);
|
---|
1602 |
|
---|
1603 | /* For 64-bit gates, no parameters are copied. Just push old SS:rSP and CS:rIP. */
|
---|
1604 | uPtrRet.pu64[0] = pVCpu->cpum.GstCtx.rip + cbInstr;
|
---|
1605 | uPtrRet.pu64[1] = pVCpu->cpum.GstCtx.cs.Sel; /** @todo Testcase: What is written to the high words when pushing CS? */
|
---|
1606 | uPtrRet.pu64[2] = uOldRsp;
|
---|
1607 | uPtrRet.pu64[3] = uOldSS; /** @todo Testcase: What is written to the high words when pushing SS? */
|
---|
1608 | }
|
---|
1609 |
|
---|
1610 | rcStrict = iemMemStackPushCommitSpecial(pVCpu, uPtrRet.pv, uNewRsp);
|
---|
1611 | if (rcStrict != VINF_SUCCESS)
|
---|
1612 | {
|
---|
1613 | Log(("BranchCallGate: New stack unmapping failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1614 | return rcStrict;
|
---|
1615 | }
|
---|
1616 |
|
---|
1617 | /* Chop the high bits off if 16-bit gate (Intel says so). */
|
---|
1618 | if (pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_286_CALL_GATE)
|
---|
1619 | uNewRip = (uint16_t)uNewRip;
|
---|
1620 |
|
---|
1621 | /* Limit / canonical check. */
|
---|
1622 | cbLimit = X86DESC_LIMIT_G(&DescCS.Legacy);
|
---|
1623 | if (!IEM_IS_LONG_MODE(pVCpu))
|
---|
1624 | {
|
---|
1625 | if (uNewRip > cbLimit)
|
---|
1626 | {
|
---|
1627 | Log(("BranchCallGate %04x:%08RX64 -> out of bounds (%#x)\n", uNewCS, uNewRip, cbLimit));
|
---|
1628 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, 0);
|
---|
1629 | }
|
---|
1630 | u64Base = X86DESC_BASE(&DescCS.Legacy);
|
---|
1631 | }
|
---|
1632 | else
|
---|
1633 | {
|
---|
1634 | Assert(pDesc->Legacy.Gate.u4Type == AMD64_SEL_TYPE_SYS_CALL_GATE);
|
---|
1635 | if (!IEM_IS_CANONICAL(uNewRip))
|
---|
1636 | {
|
---|
1637 | Log(("BranchCallGate call %04x:%016RX64 - not canonical -> #GP\n", uNewCS, uNewRip));
|
---|
1638 | return iemRaiseNotCanonical(pVCpu);
|
---|
1639 | }
|
---|
1640 | u64Base = 0;
|
---|
1641 | }
|
---|
1642 |
|
---|
1643 | /*
|
---|
1644 | * Now set the accessed bit before
|
---|
1645 | * writing the return address to the stack and committing the result into
|
---|
1646 | * CS, CSHID and RIP.
|
---|
1647 | */
|
---|
1648 | /** @todo Testcase: Need to check WHEN exactly the accessed bit is set. */
|
---|
1649 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
1650 | {
|
---|
1651 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewCS);
|
---|
1652 | if (rcStrict != VINF_SUCCESS)
|
---|
1653 | return rcStrict;
|
---|
1654 | /** @todo check what VT-x and AMD-V does. */
|
---|
1655 | DescCS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
1656 | }
|
---|
1657 |
|
---|
1658 | /* Commit new CS:rIP. */
|
---|
1659 | pVCpu->cpum.GstCtx.rip = uNewRip;
|
---|
1660 | pVCpu->cpum.GstCtx.cs.Sel = uNewCS & X86_SEL_MASK_OFF_RPL;
|
---|
1661 | pVCpu->cpum.GstCtx.cs.Sel |= IEM_GET_CPL(pVCpu);
|
---|
1662 | pVCpu->cpum.GstCtx.cs.ValidSel = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
1663 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
1664 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCS.Legacy);
|
---|
1665 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimit;
|
---|
1666 | pVCpu->cpum.GstCtx.cs.u64Base = u64Base;
|
---|
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 |= IEM_GET_CPL(pVCpu);
|
---|
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 | }
|
---|
1761 | }
|
---|
1762 | pVCpu->cpum.GstCtx.eflags.Bits.u1RF = 0;
|
---|
1763 |
|
---|
1764 | iemRecalcExecModeAndCplFlags(pVCpu);
|
---|
1765 |
|
---|
1766 | /** @todo single stepping */
|
---|
1767 |
|
---|
1768 | /* Flush the prefetch buffer. */
|
---|
1769 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr);
|
---|
1770 | return VINF_SUCCESS;
|
---|
1771 | #endif /* IEM_IMPLEMENTS_CALLGATE */
|
---|
1772 | }
|
---|
1773 |
|
---|
1774 |
|
---|
1775 | /**
|
---|
1776 | * Implements far jumps and calls thru system selectors.
|
---|
1777 | *
|
---|
1778 | * @returns VBox strict status code.
|
---|
1779 | * @param pVCpu The cross context virtual CPU structure of the
|
---|
1780 | * calling thread.
|
---|
1781 | * @param cbInstr The current instruction length.
|
---|
1782 | * @param uSel The selector.
|
---|
1783 | * @param enmBranch The kind of branching we're performing.
|
---|
1784 | * @param enmEffOpSize The effective operand size.
|
---|
1785 | * @param pDesc The descriptor corresponding to @a uSel.
|
---|
1786 | */
|
---|
1787 | static VBOXSTRICTRC iemCImpl_BranchSysSel(PVMCPUCC pVCpu, uint8_t cbInstr, uint16_t uSel, IEMBRANCH enmBranch,
|
---|
1788 | IEMMODE enmEffOpSize, PIEMSELDESC pDesc)
|
---|
1789 | {
|
---|
1790 | Assert(enmBranch == IEMBRANCH_JUMP || enmBranch == IEMBRANCH_CALL);
|
---|
1791 | Assert((uSel & X86_SEL_MASK_OFF_RPL));
|
---|
1792 | IEM_CTX_IMPORT_RET(pVCpu, IEM_CPUMCTX_EXTRN_XCPT_MASK);
|
---|
1793 |
|
---|
1794 | if (IEM_IS_LONG_MODE(pVCpu))
|
---|
1795 | switch (pDesc->Legacy.Gen.u4Type)
|
---|
1796 | {
|
---|
1797 | case AMD64_SEL_TYPE_SYS_CALL_GATE:
|
---|
1798 | return iemCImpl_BranchCallGate(pVCpu, cbInstr, uSel, enmBranch, enmEffOpSize, pDesc);
|
---|
1799 |
|
---|
1800 | default:
|
---|
1801 | case AMD64_SEL_TYPE_SYS_LDT:
|
---|
1802 | case AMD64_SEL_TYPE_SYS_TSS_BUSY:
|
---|
1803 | case AMD64_SEL_TYPE_SYS_TSS_AVAIL:
|
---|
1804 | case AMD64_SEL_TYPE_SYS_TRAP_GATE:
|
---|
1805 | case AMD64_SEL_TYPE_SYS_INT_GATE:
|
---|
1806 | Log(("branch %04x -> wrong sys selector (64-bit): %d\n", uSel, pDesc->Legacy.Gen.u4Type));
|
---|
1807 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1808 | }
|
---|
1809 |
|
---|
1810 | switch (pDesc->Legacy.Gen.u4Type)
|
---|
1811 | {
|
---|
1812 | case X86_SEL_TYPE_SYS_286_CALL_GATE:
|
---|
1813 | case X86_SEL_TYPE_SYS_386_CALL_GATE:
|
---|
1814 | return iemCImpl_BranchCallGate(pVCpu, cbInstr, uSel, enmBranch, enmEffOpSize, pDesc);
|
---|
1815 |
|
---|
1816 | case X86_SEL_TYPE_SYS_TASK_GATE:
|
---|
1817 | return iemCImpl_BranchTaskGate(pVCpu, cbInstr, uSel, enmBranch, enmEffOpSize, pDesc);
|
---|
1818 |
|
---|
1819 | case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
|
---|
1820 | case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
|
---|
1821 | return iemCImpl_BranchTaskSegment(pVCpu, cbInstr, uSel, enmBranch, enmEffOpSize, pDesc);
|
---|
1822 |
|
---|
1823 | case X86_SEL_TYPE_SYS_286_TSS_BUSY:
|
---|
1824 | Log(("branch %04x -> busy 286 TSS\n", uSel));
|
---|
1825 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1826 |
|
---|
1827 | case X86_SEL_TYPE_SYS_386_TSS_BUSY:
|
---|
1828 | Log(("branch %04x -> busy 386 TSS\n", uSel));
|
---|
1829 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1830 |
|
---|
1831 | default:
|
---|
1832 | case X86_SEL_TYPE_SYS_LDT:
|
---|
1833 | case X86_SEL_TYPE_SYS_286_INT_GATE:
|
---|
1834 | case X86_SEL_TYPE_SYS_286_TRAP_GATE:
|
---|
1835 | case X86_SEL_TYPE_SYS_386_INT_GATE:
|
---|
1836 | case X86_SEL_TYPE_SYS_386_TRAP_GATE:
|
---|
1837 | Log(("branch %04x -> wrong sys selector: %d\n", uSel, pDesc->Legacy.Gen.u4Type));
|
---|
1838 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1839 | }
|
---|
1840 | }
|
---|
1841 |
|
---|
1842 |
|
---|
1843 | /**
|
---|
1844 | * Implements far jumps.
|
---|
1845 | *
|
---|
1846 | * @param uSel The selector.
|
---|
1847 | * @param offSeg The segment offset.
|
---|
1848 | * @param enmEffOpSize The effective operand size.
|
---|
1849 | */
|
---|
1850 | IEM_CIMPL_DEF_3(iemCImpl_FarJmp, uint16_t, uSel, uint64_t, offSeg, IEMMODE, enmEffOpSize)
|
---|
1851 | {
|
---|
1852 | NOREF(cbInstr);
|
---|
1853 | Assert(offSeg <= UINT32_MAX || (!IEM_IS_GUEST_CPU_AMD(pVCpu) && IEM_IS_64BIT_CODE(pVCpu)));
|
---|
1854 |
|
---|
1855 | /*
|
---|
1856 | * Real mode and V8086 mode are easy. The only snag seems to be that
|
---|
1857 | * CS.limit doesn't change and the limit check is done against the current
|
---|
1858 | * limit.
|
---|
1859 | */
|
---|
1860 | /** @todo Robert Collins claims (The Segment Descriptor Cache, DDJ August
|
---|
1861 | * 1998) that up to and including the Intel 486, far control
|
---|
1862 | * transfers in real mode set default CS attributes (0x93) and also
|
---|
1863 | * set a 64K segment limit. Starting with the Pentium, the
|
---|
1864 | * attributes and limit are left alone but the access rights are
|
---|
1865 | * ignored. We only implement the Pentium+ behavior.
|
---|
1866 | * */
|
---|
1867 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
1868 | {
|
---|
1869 | Assert(enmEffOpSize == IEMMODE_16BIT || enmEffOpSize == IEMMODE_32BIT);
|
---|
1870 | if (offSeg > pVCpu->cpum.GstCtx.cs.u32Limit)
|
---|
1871 | {
|
---|
1872 | Log(("iemCImpl_FarJmp: 16-bit limit\n"));
|
---|
1873 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
1874 | }
|
---|
1875 |
|
---|
1876 | if (enmEffOpSize == IEMMODE_16BIT) /** @todo WRONG, must pass this. */
|
---|
1877 | pVCpu->cpum.GstCtx.rip = offSeg;
|
---|
1878 | else
|
---|
1879 | pVCpu->cpum.GstCtx.rip = offSeg & UINT16_MAX;
|
---|
1880 | pVCpu->cpum.GstCtx.cs.Sel = uSel;
|
---|
1881 | pVCpu->cpum.GstCtx.cs.ValidSel = uSel;
|
---|
1882 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
1883 | pVCpu->cpum.GstCtx.cs.u64Base = (uint32_t)uSel << 4;
|
---|
1884 |
|
---|
1885 | /* Update the FLAT 32-bit mode flag, if we're in 32-bit unreal mode (unlikely): */
|
---|
1886 | if (RT_LIKELY(!IEM_IS_32BIT_CODE(pVCpu)))
|
---|
1887 | { /* likely */ }
|
---|
1888 | else if (uSel != 0)
|
---|
1889 | pVCpu->iem.s.fExec &= ~IEM_F_MODE_X86_FLAT_OR_PRE_386_MASK;
|
---|
1890 | else
|
---|
1891 | pVCpu->iem.s.fExec = (pVCpu->iem.s.fExec & ~IEM_F_MODE_X86_FLAT_OR_PRE_386_MASK)
|
---|
1892 | | iemCalc32BitFlatIndicator(pVCpu);
|
---|
1893 |
|
---|
1894 | return iemRegFinishClearingRF(pVCpu);
|
---|
1895 | }
|
---|
1896 |
|
---|
1897 | /*
|
---|
1898 | * Protected mode. Need to parse the specified descriptor...
|
---|
1899 | */
|
---|
1900 | if (!(uSel & X86_SEL_MASK_OFF_RPL))
|
---|
1901 | {
|
---|
1902 | Log(("jmpf %04x:%08RX64 -> invalid selector, #GP(0)\n", uSel, offSeg));
|
---|
1903 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
1904 | }
|
---|
1905 |
|
---|
1906 | /* Fetch the descriptor. */
|
---|
1907 | IEMSELDESC Desc;
|
---|
1908 | VBOXSTRICTRC rcStrict = iemMemFetchSelDesc(pVCpu, &Desc, uSel, X86_XCPT_GP);
|
---|
1909 | if (rcStrict != VINF_SUCCESS)
|
---|
1910 | return rcStrict;
|
---|
1911 |
|
---|
1912 | /* Is it there? */
|
---|
1913 | if (!Desc.Legacy.Gen.u1Present) /** @todo this is probably checked too early. Testcase! */
|
---|
1914 | {
|
---|
1915 | Log(("jmpf %04x:%08RX64 -> segment not present\n", uSel, offSeg));
|
---|
1916 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uSel);
|
---|
1917 | }
|
---|
1918 |
|
---|
1919 | /*
|
---|
1920 | * Deal with it according to its type. We do the standard code selectors
|
---|
1921 | * here and dispatch the system selectors to worker functions.
|
---|
1922 | */
|
---|
1923 | if (!Desc.Legacy.Gen.u1DescType)
|
---|
1924 | return iemCImpl_BranchSysSel(pVCpu, cbInstr, uSel, IEMBRANCH_JUMP, enmEffOpSize, &Desc);
|
---|
1925 |
|
---|
1926 | /* Only code segments. */
|
---|
1927 | if (!(Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE))
|
---|
1928 | {
|
---|
1929 | Log(("jmpf %04x:%08RX64 -> not a code selector (u4Type=%#x).\n", uSel, offSeg, Desc.Legacy.Gen.u4Type));
|
---|
1930 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1931 | }
|
---|
1932 |
|
---|
1933 | /* L vs D. */
|
---|
1934 | if ( Desc.Legacy.Gen.u1Long
|
---|
1935 | && Desc.Legacy.Gen.u1DefBig
|
---|
1936 | && IEM_IS_LONG_MODE(pVCpu))
|
---|
1937 | {
|
---|
1938 | Log(("jmpf %04x:%08RX64 -> both L and D are set.\n", uSel, offSeg));
|
---|
1939 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1940 | }
|
---|
1941 |
|
---|
1942 | /* DPL/RPL/CPL check, where conforming segments makes a difference. */
|
---|
1943 | if (Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF)
|
---|
1944 | {
|
---|
1945 | if (IEM_GET_CPL(pVCpu) < Desc.Legacy.Gen.u2Dpl)
|
---|
1946 | {
|
---|
1947 | Log(("jmpf %04x:%08RX64 -> DPL violation (conforming); DPL=%d CPL=%u\n",
|
---|
1948 | uSel, offSeg, Desc.Legacy.Gen.u2Dpl, IEM_GET_CPL(pVCpu)));
|
---|
1949 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1950 | }
|
---|
1951 | }
|
---|
1952 | else
|
---|
1953 | {
|
---|
1954 | if (IEM_GET_CPL(pVCpu) != Desc.Legacy.Gen.u2Dpl)
|
---|
1955 | {
|
---|
1956 | Log(("jmpf %04x:%08RX64 -> CPL != DPL; DPL=%d CPL=%u\n", uSel, offSeg, Desc.Legacy.Gen.u2Dpl, IEM_GET_CPL(pVCpu)));
|
---|
1957 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1958 | }
|
---|
1959 | if ((uSel & X86_SEL_RPL) > IEM_GET_CPL(pVCpu))
|
---|
1960 | {
|
---|
1961 | Log(("jmpf %04x:%08RX64 -> RPL > DPL; RPL=%d CPL=%u\n", uSel, offSeg, (uSel & X86_SEL_RPL), IEM_GET_CPL(pVCpu)));
|
---|
1962 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1963 | }
|
---|
1964 | }
|
---|
1965 |
|
---|
1966 | /* Chop the high bits if 16-bit (Intel says so). */
|
---|
1967 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
1968 | offSeg &= UINT16_MAX;
|
---|
1969 |
|
---|
1970 | /* Limit check and get the base. */
|
---|
1971 | uint64_t u64Base;
|
---|
1972 | uint32_t cbLimit = X86DESC_LIMIT_G(&Desc.Legacy);
|
---|
1973 | if ( !Desc.Legacy.Gen.u1Long
|
---|
1974 | || !IEM_IS_LONG_MODE(pVCpu))
|
---|
1975 | {
|
---|
1976 | if (RT_LIKELY(offSeg <= cbLimit))
|
---|
1977 | u64Base = X86DESC_BASE(&Desc.Legacy);
|
---|
1978 | else
|
---|
1979 | {
|
---|
1980 | Log(("jmpf %04x:%08RX64 -> out of bounds (%#x)\n", uSel, offSeg, cbLimit));
|
---|
1981 | /** @todo Intel says this is \#GP(0)! */
|
---|
1982 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1983 | }
|
---|
1984 | }
|
---|
1985 | else
|
---|
1986 | u64Base = 0;
|
---|
1987 |
|
---|
1988 | /*
|
---|
1989 | * Ok, everything checked out fine. Now set the accessed bit before
|
---|
1990 | * committing the result into CS, CSHID and RIP.
|
---|
1991 | */
|
---|
1992 | if (!(Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
1993 | {
|
---|
1994 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uSel);
|
---|
1995 | if (rcStrict != VINF_SUCCESS)
|
---|
1996 | return rcStrict;
|
---|
1997 | /** @todo check what VT-x and AMD-V does. */
|
---|
1998 | Desc.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
1999 | }
|
---|
2000 |
|
---|
2001 | /* commit */
|
---|
2002 | pVCpu->cpum.GstCtx.rip = offSeg;
|
---|
2003 | pVCpu->cpum.GstCtx.cs.Sel = uSel & X86_SEL_MASK_OFF_RPL;
|
---|
2004 | pVCpu->cpum.GstCtx.cs.Sel |= IEM_GET_CPL(pVCpu); /** @todo is this right for conforming segs? or in general? */
|
---|
2005 | pVCpu->cpum.GstCtx.cs.ValidSel = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
2006 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
2007 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&Desc.Legacy);
|
---|
2008 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimit;
|
---|
2009 | pVCpu->cpum.GstCtx.cs.u64Base = u64Base;
|
---|
2010 |
|
---|
2011 | /** @todo check if the hidden bits are loaded correctly for 64-bit
|
---|
2012 | * mode. */
|
---|
2013 |
|
---|
2014 | iemRecalcExecModeAndCplFlags(pVCpu);
|
---|
2015 |
|
---|
2016 | /* Flush the prefetch buffer. */
|
---|
2017 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr);
|
---|
2018 |
|
---|
2019 | return iemRegFinishClearingRF(pVCpu);
|
---|
2020 | }
|
---|
2021 |
|
---|
2022 |
|
---|
2023 | /**
|
---|
2024 | * Implements far calls.
|
---|
2025 | *
|
---|
2026 | * This very similar to iemCImpl_FarJmp.
|
---|
2027 | *
|
---|
2028 | * @param uSel The selector.
|
---|
2029 | * @param offSeg The segment offset.
|
---|
2030 | * @param enmEffOpSize The operand size (in case we need it).
|
---|
2031 | */
|
---|
2032 | IEM_CIMPL_DEF_3(iemCImpl_callf, uint16_t, uSel, uint64_t, offSeg, IEMMODE, enmEffOpSize)
|
---|
2033 | {
|
---|
2034 | VBOXSTRICTRC rcStrict;
|
---|
2035 | uint64_t uNewRsp;
|
---|
2036 | RTPTRUNION uPtrRet;
|
---|
2037 |
|
---|
2038 | /*
|
---|
2039 | * Real mode and V8086 mode are easy. The only snag seems to be that
|
---|
2040 | * CS.limit doesn't change and the limit check is done against the current
|
---|
2041 | * limit.
|
---|
2042 | */
|
---|
2043 | /** @todo See comment for similar code in iemCImpl_FarJmp */
|
---|
2044 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
2045 | {
|
---|
2046 | Assert(enmEffOpSize == IEMMODE_16BIT || enmEffOpSize == IEMMODE_32BIT);
|
---|
2047 |
|
---|
2048 | /* Check stack first - may #SS(0). */
|
---|
2049 | rcStrict = iemMemStackPushBeginSpecial(pVCpu, enmEffOpSize == IEMMODE_32BIT ? 4+4 : 2+2,
|
---|
2050 | enmEffOpSize == IEMMODE_32BIT ? 3 : 1,
|
---|
2051 | &uPtrRet.pv, &uNewRsp);
|
---|
2052 | if (rcStrict != VINF_SUCCESS)
|
---|
2053 | return rcStrict;
|
---|
2054 |
|
---|
2055 | /* Check the target address range. */
|
---|
2056 | /** @todo this must be wrong! Write unreal mode tests! */
|
---|
2057 | if (offSeg > UINT32_MAX)
|
---|
2058 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
2059 |
|
---|
2060 | /* Everything is fine, push the return address. */
|
---|
2061 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
2062 | {
|
---|
2063 | uPtrRet.pu16[0] = pVCpu->cpum.GstCtx.ip + cbInstr;
|
---|
2064 | uPtrRet.pu16[1] = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
2065 | }
|
---|
2066 | else
|
---|
2067 | {
|
---|
2068 | uPtrRet.pu32[0] = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
2069 | uPtrRet.pu16[2] = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
2070 | }
|
---|
2071 | rcStrict = iemMemStackPushCommitSpecial(pVCpu, uPtrRet.pv, uNewRsp);
|
---|
2072 | if (rcStrict != VINF_SUCCESS)
|
---|
2073 | return rcStrict;
|
---|
2074 |
|
---|
2075 | /* Branch. */
|
---|
2076 | pVCpu->cpum.GstCtx.rip = offSeg;
|
---|
2077 | pVCpu->cpum.GstCtx.cs.Sel = uSel;
|
---|
2078 | pVCpu->cpum.GstCtx.cs.ValidSel = uSel;
|
---|
2079 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
2080 | pVCpu->cpum.GstCtx.cs.u64Base = (uint32_t)uSel << 4;
|
---|
2081 |
|
---|
2082 | return iemRegFinishClearingRF(pVCpu);
|
---|
2083 | }
|
---|
2084 |
|
---|
2085 | /*
|
---|
2086 | * Protected mode. Need to parse the specified descriptor...
|
---|
2087 | */
|
---|
2088 | if (!(uSel & X86_SEL_MASK_OFF_RPL))
|
---|
2089 | {
|
---|
2090 | Log(("callf %04x:%08RX64 -> invalid selector, #GP(0)\n", uSel, offSeg));
|
---|
2091 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
2092 | }
|
---|
2093 |
|
---|
2094 | /* Fetch the descriptor. */
|
---|
2095 | IEMSELDESC Desc;
|
---|
2096 | rcStrict = iemMemFetchSelDesc(pVCpu, &Desc, uSel, X86_XCPT_GP);
|
---|
2097 | if (rcStrict != VINF_SUCCESS)
|
---|
2098 | return rcStrict;
|
---|
2099 |
|
---|
2100 | /*
|
---|
2101 | * Deal with it according to its type. We do the standard code selectors
|
---|
2102 | * here and dispatch the system selectors to worker functions.
|
---|
2103 | */
|
---|
2104 | if (!Desc.Legacy.Gen.u1DescType)
|
---|
2105 | return iemCImpl_BranchSysSel(pVCpu, cbInstr, uSel, IEMBRANCH_CALL, enmEffOpSize, &Desc);
|
---|
2106 |
|
---|
2107 | /* Only code segments. */
|
---|
2108 | if (!(Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE))
|
---|
2109 | {
|
---|
2110 | Log(("callf %04x:%08RX64 -> not a code selector (u4Type=%#x).\n", uSel, offSeg, Desc.Legacy.Gen.u4Type));
|
---|
2111 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
2112 | }
|
---|
2113 |
|
---|
2114 | /* L vs D. */
|
---|
2115 | if ( Desc.Legacy.Gen.u1Long
|
---|
2116 | && Desc.Legacy.Gen.u1DefBig
|
---|
2117 | && IEM_IS_LONG_MODE(pVCpu))
|
---|
2118 | {
|
---|
2119 | Log(("callf %04x:%08RX64 -> both L and D are set.\n", uSel, offSeg));
|
---|
2120 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
2121 | }
|
---|
2122 |
|
---|
2123 | /* DPL/RPL/CPL check, where conforming segments makes a difference. */
|
---|
2124 | if (Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF)
|
---|
2125 | {
|
---|
2126 | if (IEM_GET_CPL(pVCpu) < Desc.Legacy.Gen.u2Dpl)
|
---|
2127 | {
|
---|
2128 | Log(("callf %04x:%08RX64 -> DPL violation (conforming); DPL=%d CPL=%u\n",
|
---|
2129 | uSel, offSeg, Desc.Legacy.Gen.u2Dpl, IEM_GET_CPL(pVCpu)));
|
---|
2130 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
2131 | }
|
---|
2132 | }
|
---|
2133 | else
|
---|
2134 | {
|
---|
2135 | if (IEM_GET_CPL(pVCpu) != Desc.Legacy.Gen.u2Dpl)
|
---|
2136 | {
|
---|
2137 | Log(("callf %04x:%08RX64 -> CPL != DPL; DPL=%d CPL=%u\n", uSel, offSeg, Desc.Legacy.Gen.u2Dpl, IEM_GET_CPL(pVCpu)));
|
---|
2138 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
2139 | }
|
---|
2140 | if ((uSel & X86_SEL_RPL) > IEM_GET_CPL(pVCpu))
|
---|
2141 | {
|
---|
2142 | Log(("callf %04x:%08RX64 -> RPL > DPL; RPL=%d CPL=%u\n", uSel, offSeg, (uSel & X86_SEL_RPL), IEM_GET_CPL(pVCpu)));
|
---|
2143 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
2144 | }
|
---|
2145 | }
|
---|
2146 |
|
---|
2147 | /* Is it there? */
|
---|
2148 | if (!Desc.Legacy.Gen.u1Present)
|
---|
2149 | {
|
---|
2150 | Log(("callf %04x:%08RX64 -> segment not present\n", uSel, offSeg));
|
---|
2151 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uSel);
|
---|
2152 | }
|
---|
2153 |
|
---|
2154 | /* Check stack first - may #SS(0). */
|
---|
2155 | /** @todo check how operand prefix affects pushing of CS! Does callf 16:32 in
|
---|
2156 | * 16-bit code cause a two or four byte CS to be pushed? */
|
---|
2157 | rcStrict = iemMemStackPushBeginSpecial(pVCpu,
|
---|
2158 | enmEffOpSize == IEMMODE_64BIT ? 8+8 : enmEffOpSize == IEMMODE_32BIT ? 4+4 : 2+2,
|
---|
2159 | enmEffOpSize == IEMMODE_64BIT ? 7 : enmEffOpSize == IEMMODE_32BIT ? 3 : 1,
|
---|
2160 | &uPtrRet.pv, &uNewRsp);
|
---|
2161 | if (rcStrict != VINF_SUCCESS)
|
---|
2162 | return rcStrict;
|
---|
2163 |
|
---|
2164 | /* Chop the high bits if 16-bit (Intel says so). */
|
---|
2165 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
2166 | offSeg &= UINT16_MAX;
|
---|
2167 |
|
---|
2168 | /* Limit / canonical check. */
|
---|
2169 | uint64_t u64Base;
|
---|
2170 | uint32_t cbLimit = X86DESC_LIMIT_G(&Desc.Legacy);
|
---|
2171 | if ( !Desc.Legacy.Gen.u1Long
|
---|
2172 | || !IEM_IS_LONG_MODE(pVCpu))
|
---|
2173 | {
|
---|
2174 | if (RT_LIKELY(offSeg <= cbLimit))
|
---|
2175 | u64Base = X86DESC_BASE(&Desc.Legacy);
|
---|
2176 | else
|
---|
2177 | {
|
---|
2178 | Log(("callf %04x:%08RX64 -> out of bounds (%#x)\n", uSel, offSeg, cbLimit));
|
---|
2179 | /** @todo Intel says this is \#GP(0)! */
|
---|
2180 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
2181 | }
|
---|
2182 | }
|
---|
2183 | else if (IEM_IS_CANONICAL(offSeg))
|
---|
2184 | u64Base = 0;
|
---|
2185 | else
|
---|
2186 | {
|
---|
2187 | Log(("callf %04x:%016RX64 - not canonical -> #GP\n", uSel, offSeg));
|
---|
2188 | return iemRaiseNotCanonical(pVCpu);
|
---|
2189 | }
|
---|
2190 |
|
---|
2191 | /*
|
---|
2192 | * Now set the accessed bit before
|
---|
2193 | * writing the return address to the stack and committing the result into
|
---|
2194 | * CS, CSHID and RIP.
|
---|
2195 | */
|
---|
2196 | /** @todo Testcase: Need to check WHEN exactly the accessed bit is set. */
|
---|
2197 | if (!(Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
2198 | {
|
---|
2199 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uSel);
|
---|
2200 | if (rcStrict != VINF_SUCCESS)
|
---|
2201 | return rcStrict;
|
---|
2202 | /** @todo check what VT-x and AMD-V does. */
|
---|
2203 | Desc.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
2204 | }
|
---|
2205 |
|
---|
2206 | /* stack */
|
---|
2207 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
2208 | {
|
---|
2209 | uPtrRet.pu16[0] = pVCpu->cpum.GstCtx.ip + cbInstr;
|
---|
2210 | uPtrRet.pu16[1] = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
2211 | }
|
---|
2212 | else if (enmEffOpSize == IEMMODE_32BIT)
|
---|
2213 | {
|
---|
2214 | uPtrRet.pu32[0] = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
2215 | uPtrRet.pu32[1] = pVCpu->cpum.GstCtx.cs.Sel; /** @todo Testcase: What is written to the high word when callf is pushing CS? */
|
---|
2216 | }
|
---|
2217 | else
|
---|
2218 | {
|
---|
2219 | uPtrRet.pu64[0] = pVCpu->cpum.GstCtx.rip + cbInstr;
|
---|
2220 | uPtrRet.pu64[1] = pVCpu->cpum.GstCtx.cs.Sel; /** @todo Testcase: What is written to the high words when callf is pushing CS? */
|
---|
2221 | }
|
---|
2222 | rcStrict = iemMemStackPushCommitSpecial(pVCpu, uPtrRet.pv, uNewRsp);
|
---|
2223 | if (rcStrict != VINF_SUCCESS)
|
---|
2224 | return rcStrict;
|
---|
2225 |
|
---|
2226 | /* commit */
|
---|
2227 | pVCpu->cpum.GstCtx.rip = offSeg;
|
---|
2228 | pVCpu->cpum.GstCtx.cs.Sel = uSel & X86_SEL_MASK_OFF_RPL;
|
---|
2229 | pVCpu->cpum.GstCtx.cs.Sel |= IEM_GET_CPL(pVCpu);
|
---|
2230 | pVCpu->cpum.GstCtx.cs.ValidSel = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
2231 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
2232 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&Desc.Legacy);
|
---|
2233 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimit;
|
---|
2234 | pVCpu->cpum.GstCtx.cs.u64Base = u64Base;
|
---|
2235 |
|
---|
2236 | /** @todo check if the hidden bits are loaded correctly for 64-bit
|
---|
2237 | * mode. */
|
---|
2238 |
|
---|
2239 | iemRecalcExecDbgFlags(pVCpu);
|
---|
2240 |
|
---|
2241 | /* Flush the prefetch buffer. */
|
---|
2242 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr);
|
---|
2243 |
|
---|
2244 | return iemRegFinishClearingRF(pVCpu);
|
---|
2245 | }
|
---|
2246 |
|
---|
2247 |
|
---|
2248 | /**
|
---|
2249 | * Implements retf.
|
---|
2250 | *
|
---|
2251 | * @param enmEffOpSize The effective operand size.
|
---|
2252 | * @param cbPop The amount of arguments to pop from the stack
|
---|
2253 | * (bytes).
|
---|
2254 | */
|
---|
2255 | IEM_CIMPL_DEF_2(iemCImpl_retf, IEMMODE, enmEffOpSize, uint16_t, cbPop)
|
---|
2256 | {
|
---|
2257 | VBOXSTRICTRC rcStrict;
|
---|
2258 | RTCPTRUNION uPtrFrame;
|
---|
2259 | RTUINT64U NewRsp;
|
---|
2260 | uint64_t uNewRip;
|
---|
2261 | uint16_t uNewCs;
|
---|
2262 | NOREF(cbInstr);
|
---|
2263 |
|
---|
2264 | /*
|
---|
2265 | * Read the stack values first.
|
---|
2266 | */
|
---|
2267 | uint32_t cbRetPtr = enmEffOpSize == IEMMODE_16BIT ? 2+2
|
---|
2268 | : enmEffOpSize == IEMMODE_32BIT ? 4+4 : 8+8;
|
---|
2269 | rcStrict = iemMemStackPopBeginSpecial(pVCpu, cbRetPtr,
|
---|
2270 | enmEffOpSize == IEMMODE_16BIT ? 1 : enmEffOpSize == IEMMODE_32BIT ? 3 : 7,
|
---|
2271 | &uPtrFrame.pv, &NewRsp.u);
|
---|
2272 | if (rcStrict != VINF_SUCCESS)
|
---|
2273 | return rcStrict;
|
---|
2274 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
2275 | {
|
---|
2276 | uNewRip = uPtrFrame.pu16[0];
|
---|
2277 | uNewCs = uPtrFrame.pu16[1];
|
---|
2278 | }
|
---|
2279 | else if (enmEffOpSize == IEMMODE_32BIT)
|
---|
2280 | {
|
---|
2281 | uNewRip = uPtrFrame.pu32[0];
|
---|
2282 | uNewCs = uPtrFrame.pu16[2];
|
---|
2283 | }
|
---|
2284 | else
|
---|
2285 | {
|
---|
2286 | uNewRip = uPtrFrame.pu64[0];
|
---|
2287 | uNewCs = uPtrFrame.pu16[4];
|
---|
2288 | }
|
---|
2289 | rcStrict = iemMemStackPopDoneSpecial(pVCpu, uPtrFrame.pv);
|
---|
2290 | if (RT_LIKELY(rcStrict == VINF_SUCCESS))
|
---|
2291 | { /* extremely likely */ }
|
---|
2292 | else
|
---|
2293 | return rcStrict;
|
---|
2294 |
|
---|
2295 | /*
|
---|
2296 | * Real mode and V8086 mode are easy.
|
---|
2297 | */
|
---|
2298 | /** @todo See comment for similar code in iemCImpl_FarJmp */
|
---|
2299 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
2300 | {
|
---|
2301 | Assert(enmEffOpSize == IEMMODE_32BIT || enmEffOpSize == IEMMODE_16BIT);
|
---|
2302 | /** @todo check how this is supposed to work if sp=0xfffe. */
|
---|
2303 |
|
---|
2304 | /* Check the limit of the new EIP. */
|
---|
2305 | /** @todo Intel pseudo code only does the limit check for 16-bit
|
---|
2306 | * operands, AMD does not make any distinction. What is right? */
|
---|
2307 | if (uNewRip > pVCpu->cpum.GstCtx.cs.u32Limit)
|
---|
2308 | return iemRaiseSelectorBounds(pVCpu, X86_SREG_CS, IEM_ACCESS_INSTRUCTION);
|
---|
2309 |
|
---|
2310 | /* commit the operation. */
|
---|
2311 | if (cbPop)
|
---|
2312 | iemRegAddToRspEx(pVCpu, &NewRsp, cbPop);
|
---|
2313 | pVCpu->cpum.GstCtx.rsp = NewRsp.u;
|
---|
2314 | pVCpu->cpum.GstCtx.rip = uNewRip;
|
---|
2315 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
|
---|
2316 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
|
---|
2317 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
2318 | pVCpu->cpum.GstCtx.cs.u64Base = (uint32_t)uNewCs << 4;
|
---|
2319 | return iemRegFinishClearingRF(pVCpu);
|
---|
2320 | }
|
---|
2321 |
|
---|
2322 | /*
|
---|
2323 | * Protected mode is complicated, of course.
|
---|
2324 | */
|
---|
2325 | if (!(uNewCs & X86_SEL_MASK_OFF_RPL))
|
---|
2326 | {
|
---|
2327 | Log(("retf %04x:%08RX64 -> invalid selector, #GP(0)\n", uNewCs, uNewRip));
|
---|
2328 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
2329 | }
|
---|
2330 |
|
---|
2331 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SREG_MASK | CPUMCTX_EXTRN_GDTR | CPUMCTX_EXTRN_LDTR);
|
---|
2332 |
|
---|
2333 | /* Fetch the descriptor. */
|
---|
2334 | IEMSELDESC DescCs;
|
---|
2335 | rcStrict = iemMemFetchSelDesc(pVCpu, &DescCs, uNewCs, X86_XCPT_GP);
|
---|
2336 | if (rcStrict != VINF_SUCCESS)
|
---|
2337 | return rcStrict;
|
---|
2338 |
|
---|
2339 | /* Can only return to a code selector. */
|
---|
2340 | if ( !DescCs.Legacy.Gen.u1DescType
|
---|
2341 | || !(DescCs.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE) )
|
---|
2342 | {
|
---|
2343 | Log(("retf %04x:%08RX64 -> not a code selector (u1DescType=%u u4Type=%#x).\n",
|
---|
2344 | uNewCs, uNewRip, DescCs.Legacy.Gen.u1DescType, DescCs.Legacy.Gen.u4Type));
|
---|
2345 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
2346 | }
|
---|
2347 |
|
---|
2348 | /* L vs D. */
|
---|
2349 | if ( DescCs.Legacy.Gen.u1Long /** @todo Testcase: far return to a selector with both L and D set. */
|
---|
2350 | && DescCs.Legacy.Gen.u1DefBig
|
---|
2351 | && IEM_IS_LONG_MODE(pVCpu))
|
---|
2352 | {
|
---|
2353 | Log(("retf %04x:%08RX64 -> both L & D set.\n", uNewCs, uNewRip));
|
---|
2354 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
2355 | }
|
---|
2356 |
|
---|
2357 | /* DPL/RPL/CPL checks. */
|
---|
2358 | if ((uNewCs & X86_SEL_RPL) < IEM_GET_CPL(pVCpu))
|
---|
2359 | {
|
---|
2360 | Log(("retf %04x:%08RX64 -> RPL < CPL(%d).\n", uNewCs, uNewRip, IEM_GET_CPL(pVCpu)));
|
---|
2361 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
2362 | }
|
---|
2363 |
|
---|
2364 | if (DescCs.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF)
|
---|
2365 | {
|
---|
2366 | if ((uNewCs & X86_SEL_RPL) < DescCs.Legacy.Gen.u2Dpl)
|
---|
2367 | {
|
---|
2368 | Log(("retf %04x:%08RX64 -> DPL violation (conforming); DPL=%u RPL=%u\n",
|
---|
2369 | uNewCs, uNewRip, DescCs.Legacy.Gen.u2Dpl, (uNewCs & X86_SEL_RPL)));
|
---|
2370 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
2371 | }
|
---|
2372 | }
|
---|
2373 | else
|
---|
2374 | {
|
---|
2375 | if ((uNewCs & X86_SEL_RPL) != DescCs.Legacy.Gen.u2Dpl)
|
---|
2376 | {
|
---|
2377 | Log(("retf %04x:%08RX64 -> RPL != DPL; DPL=%u RPL=%u\n",
|
---|
2378 | uNewCs, uNewRip, DescCs.Legacy.Gen.u2Dpl, (uNewCs & X86_SEL_RPL)));
|
---|
2379 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
2380 | }
|
---|
2381 | }
|
---|
2382 |
|
---|
2383 | /* Is it there? */
|
---|
2384 | if (!DescCs.Legacy.Gen.u1Present)
|
---|
2385 | {
|
---|
2386 | Log(("retf %04x:%08RX64 -> segment not present\n", uNewCs, uNewRip));
|
---|
2387 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uNewCs);
|
---|
2388 | }
|
---|
2389 |
|
---|
2390 | /*
|
---|
2391 | * Return to outer privilege? (We'll typically have entered via a call gate.)
|
---|
2392 | */
|
---|
2393 | if ((uNewCs & X86_SEL_RPL) != IEM_GET_CPL(pVCpu))
|
---|
2394 | {
|
---|
2395 | /* Read the outer stack pointer stored *after* the parameters. */
|
---|
2396 | rcStrict = iemMemStackPopContinueSpecial(pVCpu, cbPop /*off*/, cbRetPtr, &uPtrFrame.pv, NewRsp.u);
|
---|
2397 | if (rcStrict != VINF_SUCCESS)
|
---|
2398 | return rcStrict;
|
---|
2399 |
|
---|
2400 | uint16_t uNewOuterSs;
|
---|
2401 | RTUINT64U NewOuterRsp;
|
---|
2402 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
2403 | {
|
---|
2404 | NewOuterRsp.u = uPtrFrame.pu16[0];
|
---|
2405 | uNewOuterSs = uPtrFrame.pu16[1];
|
---|
2406 | }
|
---|
2407 | else if (enmEffOpSize == IEMMODE_32BIT)
|
---|
2408 | {
|
---|
2409 | NewOuterRsp.u = uPtrFrame.pu32[0];
|
---|
2410 | uNewOuterSs = uPtrFrame.pu16[2];
|
---|
2411 | }
|
---|
2412 | else
|
---|
2413 | {
|
---|
2414 | NewOuterRsp.u = uPtrFrame.pu64[0];
|
---|
2415 | uNewOuterSs = uPtrFrame.pu16[4];
|
---|
2416 | }
|
---|
2417 | rcStrict = iemMemStackPopDoneSpecial(pVCpu, uPtrFrame.pv);
|
---|
2418 | if (RT_LIKELY(rcStrict == VINF_SUCCESS))
|
---|
2419 | { /* extremely likely */ }
|
---|
2420 | else
|
---|
2421 | return rcStrict;
|
---|
2422 |
|
---|
2423 | /* Check for NULL stack selector (invalid in ring-3 and non-long mode)
|
---|
2424 | and read the selector. */
|
---|
2425 | IEMSELDESC DescSs;
|
---|
2426 | if (!(uNewOuterSs & X86_SEL_MASK_OFF_RPL))
|
---|
2427 | {
|
---|
2428 | if ( !DescCs.Legacy.Gen.u1Long
|
---|
2429 | || (uNewOuterSs & X86_SEL_RPL) == 3)
|
---|
2430 | {
|
---|
2431 | Log(("retf %04x:%08RX64 %04x:%08RX64 -> invalid stack selector, #GP\n",
|
---|
2432 | uNewCs, uNewRip, uNewOuterSs, NewOuterRsp.u));
|
---|
2433 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
2434 | }
|
---|
2435 | /** @todo Testcase: Return far to ring-1 or ring-2 with SS=0. */
|
---|
2436 | iemMemFakeStackSelDesc(&DescSs, (uNewOuterSs & X86_SEL_RPL));
|
---|
2437 | }
|
---|
2438 | else
|
---|
2439 | {
|
---|
2440 | /* Fetch the descriptor for the new stack segment. */
|
---|
2441 | rcStrict = iemMemFetchSelDesc(pVCpu, &DescSs, uNewOuterSs, X86_XCPT_GP);
|
---|
2442 | if (rcStrict != VINF_SUCCESS)
|
---|
2443 | return rcStrict;
|
---|
2444 | }
|
---|
2445 |
|
---|
2446 | /* Check that RPL of stack and code selectors match. */
|
---|
2447 | if ((uNewCs & X86_SEL_RPL) != (uNewOuterSs & X86_SEL_RPL))
|
---|
2448 | {
|
---|
2449 | Log(("retf %04x:%08RX64 %04x:%08RX64 - SS.RPL != CS.RPL -> #GP(SS)\n", uNewCs, uNewRip, uNewOuterSs, NewOuterRsp.u));
|
---|
2450 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewOuterSs);
|
---|
2451 | }
|
---|
2452 |
|
---|
2453 | /* Must be a writable data segment. */
|
---|
2454 | if ( !DescSs.Legacy.Gen.u1DescType
|
---|
2455 | || (DescSs.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE)
|
---|
2456 | || !(DescSs.Legacy.Gen.u4Type & X86_SEL_TYPE_WRITE) )
|
---|
2457 | {
|
---|
2458 | Log(("retf %04x:%08RX64 %04x:%08RX64 - SS not a writable data segment (u1DescType=%u u4Type=%#x) -> #GP(SS).\n",
|
---|
2459 | uNewCs, uNewRip, uNewOuterSs, NewOuterRsp.u, DescSs.Legacy.Gen.u1DescType, DescSs.Legacy.Gen.u4Type));
|
---|
2460 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewOuterSs);
|
---|
2461 | }
|
---|
2462 |
|
---|
2463 | /* L vs D. (Not mentioned by intel.) */
|
---|
2464 | if ( DescSs.Legacy.Gen.u1Long /** @todo Testcase: far return to a stack selector with both L and D set. */
|
---|
2465 | && DescSs.Legacy.Gen.u1DefBig
|
---|
2466 | && IEM_IS_LONG_MODE(pVCpu))
|
---|
2467 | {
|
---|
2468 | Log(("retf %04x:%08RX64 %04x:%08RX64 - SS has both L & D set -> #GP(SS).\n",
|
---|
2469 | uNewCs, uNewRip, uNewOuterSs, NewOuterRsp.u));
|
---|
2470 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewOuterSs);
|
---|
2471 | }
|
---|
2472 |
|
---|
2473 | /* DPL/RPL/CPL checks. */
|
---|
2474 | if (DescSs.Legacy.Gen.u2Dpl != (uNewCs & X86_SEL_RPL))
|
---|
2475 | {
|
---|
2476 | Log(("retf %04x:%08RX64 %04x:%08RX64 - SS.DPL(%u) != CS.RPL (%u) -> #GP(SS).\n",
|
---|
2477 | uNewCs, uNewRip, uNewOuterSs, NewOuterRsp.u, DescSs.Legacy.Gen.u2Dpl, uNewCs & X86_SEL_RPL));
|
---|
2478 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewOuterSs);
|
---|
2479 | }
|
---|
2480 |
|
---|
2481 | /* Is it there? */
|
---|
2482 | if (!DescSs.Legacy.Gen.u1Present)
|
---|
2483 | {
|
---|
2484 | Log(("retf %04x:%08RX64 %04x:%08RX64 - SS not present -> #NP(SS).\n", uNewCs, uNewRip, uNewOuterSs, NewOuterRsp.u));
|
---|
2485 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uNewCs);
|
---|
2486 | }
|
---|
2487 |
|
---|
2488 | /* Calc SS limit.*/
|
---|
2489 | uint64_t u64BaseSs;
|
---|
2490 | uint32_t cbLimitSs = X86DESC_LIMIT_G(&DescSs.Legacy);
|
---|
2491 |
|
---|
2492 | /* Is RIP canonical or within CS.limit? */
|
---|
2493 | uint64_t u64BaseCs;
|
---|
2494 | uint32_t cbLimitCs = X86DESC_LIMIT_G(&DescCs.Legacy);
|
---|
2495 |
|
---|
2496 | /** @todo Testcase: Is this correct? */
|
---|
2497 | if ( DescCs.Legacy.Gen.u1Long
|
---|
2498 | && IEM_IS_LONG_MODE(pVCpu) )
|
---|
2499 | {
|
---|
2500 | if (!IEM_IS_CANONICAL(uNewRip))
|
---|
2501 | {
|
---|
2502 | Log(("retf %04x:%08RX64 %04x:%08RX64 - not canonical -> #GP.\n", uNewCs, uNewRip, uNewOuterSs, NewOuterRsp.u));
|
---|
2503 | return iemRaiseNotCanonical(pVCpu);
|
---|
2504 | }
|
---|
2505 | u64BaseCs = 0;
|
---|
2506 | u64BaseSs = 0;
|
---|
2507 | }
|
---|
2508 | else
|
---|
2509 | {
|
---|
2510 | if (uNewRip > cbLimitCs)
|
---|
2511 | {
|
---|
2512 | Log(("retf %04x:%08RX64 %04x:%08RX64 - out of bounds (%#x)-> #GP(CS).\n",
|
---|
2513 | uNewCs, uNewRip, uNewOuterSs, NewOuterRsp.u, cbLimitCs));
|
---|
2514 | /** @todo Intel says this is \#GP(0)! */
|
---|
2515 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
2516 | }
|
---|
2517 | u64BaseCs = X86DESC_BASE(&DescCs.Legacy);
|
---|
2518 | u64BaseSs = X86DESC_BASE(&DescSs.Legacy);
|
---|
2519 | }
|
---|
2520 |
|
---|
2521 | /*
|
---|
2522 | * Now set the accessed bit before
|
---|
2523 | * writing the return address to the stack and committing the result into
|
---|
2524 | * CS, CSHID and RIP.
|
---|
2525 | */
|
---|
2526 | /** @todo Testcase: Need to check WHEN exactly the CS accessed bit is set. */
|
---|
2527 | if (!(DescCs.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
2528 | {
|
---|
2529 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewCs);
|
---|
2530 | if (rcStrict != VINF_SUCCESS)
|
---|
2531 | return rcStrict;
|
---|
2532 | /** @todo check what VT-x and AMD-V does. */
|
---|
2533 | DescCs.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
2534 | }
|
---|
2535 | /** @todo Testcase: Need to check WHEN exactly the SS accessed bit is set. */
|
---|
2536 | if (!(DescSs.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
2537 | {
|
---|
2538 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewOuterSs);
|
---|
2539 | if (rcStrict != VINF_SUCCESS)
|
---|
2540 | return rcStrict;
|
---|
2541 | /** @todo check what VT-x and AMD-V does. */
|
---|
2542 | DescSs.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
2543 | }
|
---|
2544 |
|
---|
2545 | /* commit */
|
---|
2546 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
2547 | 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... */
|
---|
2548 | else
|
---|
2549 | pVCpu->cpum.GstCtx.rip = uNewRip;
|
---|
2550 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
|
---|
2551 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
|
---|
2552 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
2553 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCs.Legacy);
|
---|
2554 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimitCs;
|
---|
2555 | pVCpu->cpum.GstCtx.cs.u64Base = u64BaseCs;
|
---|
2556 | pVCpu->cpum.GstCtx.ss.Sel = uNewOuterSs;
|
---|
2557 | pVCpu->cpum.GstCtx.ss.ValidSel = uNewOuterSs;
|
---|
2558 | pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
2559 | pVCpu->cpum.GstCtx.ss.Attr.u = X86DESC_GET_HID_ATTR(&DescSs.Legacy);
|
---|
2560 | pVCpu->cpum.GstCtx.ss.u32Limit = cbLimitSs;
|
---|
2561 | pVCpu->cpum.GstCtx.ss.u64Base = u64BaseSs;
|
---|
2562 |
|
---|
2563 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.ds);
|
---|
2564 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.es);
|
---|
2565 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.fs);
|
---|
2566 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.gs);
|
---|
2567 |
|
---|
2568 | iemRecalcExecModeAndCplFlags(pVCpu); /* Affects iemRegAddToRspEx and the setting of RSP/SP below. */
|
---|
2569 |
|
---|
2570 | if (cbPop)
|
---|
2571 | iemRegAddToRspEx(pVCpu, &NewOuterRsp, cbPop);
|
---|
2572 | if (IEM_IS_64BIT_CODE(pVCpu))
|
---|
2573 | pVCpu->cpum.GstCtx.rsp = NewOuterRsp.u;
|
---|
2574 | else if (pVCpu->cpum.GstCtx.ss.Attr.n.u1DefBig)
|
---|
2575 | pVCpu->cpum.GstCtx.rsp = (uint32_t)NewOuterRsp.u;
|
---|
2576 | else
|
---|
2577 | pVCpu->cpum.GstCtx.sp = (uint16_t)NewOuterRsp.u;
|
---|
2578 |
|
---|
2579 | iemRecalcExecModeAndCplFlags(pVCpu); /* Affects iemRegAddToRspEx and the setting of RSP/SP below. */
|
---|
2580 |
|
---|
2581 | /** @todo check if the hidden bits are loaded correctly for 64-bit
|
---|
2582 | * mode. */
|
---|
2583 | }
|
---|
2584 | /*
|
---|
2585 | * Return to the same privilege level
|
---|
2586 | */
|
---|
2587 | else
|
---|
2588 | {
|
---|
2589 | /* Limit / canonical check. */
|
---|
2590 | uint64_t u64Base;
|
---|
2591 | uint32_t cbLimitCs = X86DESC_LIMIT_G(&DescCs.Legacy);
|
---|
2592 |
|
---|
2593 | /** @todo Testcase: Is this correct? */
|
---|
2594 | bool f64BitCs = false;
|
---|
2595 | if ( DescCs.Legacy.Gen.u1Long
|
---|
2596 | && IEM_IS_LONG_MODE(pVCpu) )
|
---|
2597 | {
|
---|
2598 | if (!IEM_IS_CANONICAL(uNewRip))
|
---|
2599 | {
|
---|
2600 | Log(("retf %04x:%08RX64 - not canonical -> #GP\n", uNewCs, uNewRip));
|
---|
2601 | return iemRaiseNotCanonical(pVCpu);
|
---|
2602 | }
|
---|
2603 | u64Base = 0;
|
---|
2604 | f64BitCs = true;
|
---|
2605 | f64BitCs = true;
|
---|
2606 | }
|
---|
2607 | else
|
---|
2608 | {
|
---|
2609 | if (uNewRip > cbLimitCs)
|
---|
2610 | {
|
---|
2611 | Log(("retf %04x:%08RX64 -> out of bounds (%#x)\n", uNewCs, uNewRip, cbLimitCs));
|
---|
2612 | /** @todo Intel says this is \#GP(0)! */
|
---|
2613 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
2614 | }
|
---|
2615 | u64Base = X86DESC_BASE(&DescCs.Legacy);
|
---|
2616 | }
|
---|
2617 |
|
---|
2618 | /*
|
---|
2619 | * Now set the accessed bit before
|
---|
2620 | * writing the return address to the stack and committing the result into
|
---|
2621 | * CS, CSHID and RIP.
|
---|
2622 | */
|
---|
2623 | /** @todo Testcase: Need to check WHEN exactly the accessed bit is set. */
|
---|
2624 | if (!(DescCs.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
2625 | {
|
---|
2626 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewCs);
|
---|
2627 | if (rcStrict != VINF_SUCCESS)
|
---|
2628 | return rcStrict;
|
---|
2629 | /** @todo check what VT-x and AMD-V does. */
|
---|
2630 | DescCs.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
2631 | }
|
---|
2632 |
|
---|
2633 | /* commit */
|
---|
2634 | if (cbPop)
|
---|
2635 | /** @todo This cannot be right. We're using the old CS mode here, and iemRegAddToRspEx checks fExec. */
|
---|
2636 | iemRegAddToRspEx(pVCpu, &NewRsp, cbPop);
|
---|
2637 | if (pVCpu->cpum.GstCtx.ss.Attr.n.u1DefBig || f64BitCs)
|
---|
2638 | pVCpu->cpum.GstCtx.rsp = NewRsp.u;
|
---|
2639 | else
|
---|
2640 | pVCpu->cpum.GstCtx.sp = (uint16_t)NewRsp.u;
|
---|
2641 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
2642 | 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... */
|
---|
2643 | else
|
---|
2644 | pVCpu->cpum.GstCtx.rip = uNewRip;
|
---|
2645 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
|
---|
2646 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
|
---|
2647 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
2648 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCs.Legacy);
|
---|
2649 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimitCs;
|
---|
2650 | pVCpu->cpum.GstCtx.cs.u64Base = u64Base;
|
---|
2651 | /** @todo check if the hidden bits are loaded correctly for 64-bit
|
---|
2652 | * mode. */
|
---|
2653 |
|
---|
2654 | iemRecalcExecModeAndCplFlags(pVCpu);
|
---|
2655 | }
|
---|
2656 |
|
---|
2657 | /* Flush the prefetch buffer. */
|
---|
2658 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr); /** @todo use light flush for same privilege? */
|
---|
2659 |
|
---|
2660 | return iemRegFinishClearingRF(pVCpu);
|
---|
2661 | }
|
---|
2662 |
|
---|
2663 |
|
---|
2664 | /**
|
---|
2665 | * Implements retn and retn imm16.
|
---|
2666 | *
|
---|
2667 | * We're doing this in C because of the \#GP that might be raised if the popped
|
---|
2668 | * program counter is out of bounds.
|
---|
2669 | *
|
---|
2670 | * The hope with this forced inline worker function, is that the compiler will
|
---|
2671 | * be clever enough to eliminate unused code for the constant enmEffOpSize and
|
---|
2672 | * maybe cbPop parameters.
|
---|
2673 | *
|
---|
2674 | * @param pVCpu The cross context virtual CPU structure of the
|
---|
2675 | * calling thread.
|
---|
2676 | * @param cbInstr The current instruction length.
|
---|
2677 | * @param enmEffOpSize The effective operand size. This is constant.
|
---|
2678 | * @param cbPop The amount of arguments to pop from the stack
|
---|
2679 | * (bytes). This can be constant (zero).
|
---|
2680 | */
|
---|
2681 | DECL_FORCE_INLINE(VBOXSTRICTRC) iemCImpl_ReturnNearCommon(PVMCPUCC pVCpu, uint8_t cbInstr, IEMMODE enmEffOpSize, uint16_t cbPop)
|
---|
2682 | {
|
---|
2683 | /* Fetch the RSP from the stack. */
|
---|
2684 | VBOXSTRICTRC rcStrict;
|
---|
2685 | RTUINT64U NewRip;
|
---|
2686 | RTUINT64U NewRsp;
|
---|
2687 | NewRsp.u = pVCpu->cpum.GstCtx.rsp;
|
---|
2688 |
|
---|
2689 | switch (enmEffOpSize)
|
---|
2690 | {
|
---|
2691 | case IEMMODE_16BIT:
|
---|
2692 | NewRip.u = 0;
|
---|
2693 | rcStrict = iemMemStackPopU16Ex(pVCpu, &NewRip.Words.w0, &NewRsp);
|
---|
2694 | break;
|
---|
2695 | case IEMMODE_32BIT:
|
---|
2696 | NewRip.u = 0;
|
---|
2697 | rcStrict = iemMemStackPopU32Ex(pVCpu, &NewRip.DWords.dw0, &NewRsp);
|
---|
2698 | break;
|
---|
2699 | case IEMMODE_64BIT:
|
---|
2700 | rcStrict = iemMemStackPopU64Ex(pVCpu, &NewRip.u, &NewRsp);
|
---|
2701 | break;
|
---|
2702 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
2703 | }
|
---|
2704 | if (rcStrict != VINF_SUCCESS)
|
---|
2705 | return rcStrict;
|
---|
2706 |
|
---|
2707 | /* Check the new RSP before loading it. */
|
---|
2708 | /** @todo Should test this as the intel+amd pseudo code doesn't mention half
|
---|
2709 | * of it. The canonical test is performed here and for call. */
|
---|
2710 | if (enmEffOpSize != IEMMODE_64BIT)
|
---|
2711 | {
|
---|
2712 | if (RT_LIKELY(NewRip.DWords.dw0 <= pVCpu->cpum.GstCtx.cs.u32Limit))
|
---|
2713 | { /* likely */ }
|
---|
2714 | else
|
---|
2715 | {
|
---|
2716 | Log(("retn newrip=%llx - out of bounds (%x) -> #GP\n", NewRip.u, pVCpu->cpum.GstCtx.cs.u32Limit));
|
---|
2717 | return iemRaiseSelectorBounds(pVCpu, X86_SREG_CS, IEM_ACCESS_INSTRUCTION);
|
---|
2718 | }
|
---|
2719 | }
|
---|
2720 | else
|
---|
2721 | {
|
---|
2722 | if (RT_LIKELY(IEM_IS_CANONICAL(NewRip.u)))
|
---|
2723 | { /* likely */ }
|
---|
2724 | else
|
---|
2725 | {
|
---|
2726 | Log(("retn newrip=%llx - not canonical -> #GP\n", NewRip.u));
|
---|
2727 | return iemRaiseNotCanonical(pVCpu);
|
---|
2728 | }
|
---|
2729 | }
|
---|
2730 |
|
---|
2731 | /* Apply cbPop */
|
---|
2732 | if (cbPop)
|
---|
2733 | iemRegAddToRspEx(pVCpu, &NewRsp, cbPop);
|
---|
2734 |
|
---|
2735 | /* Commit it. */
|
---|
2736 | pVCpu->cpum.GstCtx.rip = NewRip.u;
|
---|
2737 | pVCpu->cpum.GstCtx.rsp = NewRsp.u;
|
---|
2738 |
|
---|
2739 | /* Flush the prefetch buffer. */
|
---|
2740 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr); /** @todo only need a light flush here, don't we? We don't really need any flushing... */
|
---|
2741 | RT_NOREF(cbInstr);
|
---|
2742 |
|
---|
2743 | return iemRegFinishClearingRF(pVCpu);
|
---|
2744 | }
|
---|
2745 |
|
---|
2746 |
|
---|
2747 | /**
|
---|
2748 | * Implements retn imm16 with 16-bit effective operand size.
|
---|
2749 | *
|
---|
2750 | * @param cbPop The amount of arguments to pop from the stack (bytes).
|
---|
2751 | */
|
---|
2752 | IEM_CIMPL_DEF_1(iemCImpl_retn_iw_16, uint16_t, cbPop)
|
---|
2753 | {
|
---|
2754 | return iemCImpl_ReturnNearCommon(pVCpu, cbInstr, IEMMODE_16BIT, cbPop);
|
---|
2755 | }
|
---|
2756 |
|
---|
2757 |
|
---|
2758 | /**
|
---|
2759 | * Implements retn imm16 with 32-bit effective operand size.
|
---|
2760 | *
|
---|
2761 | * @param cbPop The amount of arguments to pop from the stack (bytes).
|
---|
2762 | */
|
---|
2763 | IEM_CIMPL_DEF_1(iemCImpl_retn_iw_32, uint16_t, cbPop)
|
---|
2764 | {
|
---|
2765 | return iemCImpl_ReturnNearCommon(pVCpu, cbInstr, IEMMODE_32BIT, cbPop);
|
---|
2766 | }
|
---|
2767 |
|
---|
2768 |
|
---|
2769 | /**
|
---|
2770 | * Implements retn imm16 with 64-bit effective operand size.
|
---|
2771 | *
|
---|
2772 | * @param cbPop The amount of arguments to pop from the stack (bytes).
|
---|
2773 | */
|
---|
2774 | IEM_CIMPL_DEF_1(iemCImpl_retn_iw_64, uint16_t, cbPop)
|
---|
2775 | {
|
---|
2776 | return iemCImpl_ReturnNearCommon(pVCpu, cbInstr, IEMMODE_64BIT, cbPop);
|
---|
2777 | }
|
---|
2778 |
|
---|
2779 |
|
---|
2780 | /**
|
---|
2781 | * Implements retn with 16-bit effective operand size.
|
---|
2782 | */
|
---|
2783 | IEM_CIMPL_DEF_0(iemCImpl_retn_16)
|
---|
2784 | {
|
---|
2785 | return iemCImpl_ReturnNearCommon(pVCpu, cbInstr, IEMMODE_16BIT, 0);
|
---|
2786 | }
|
---|
2787 |
|
---|
2788 |
|
---|
2789 | /**
|
---|
2790 | * Implements retn with 32-bit effective operand size.
|
---|
2791 | */
|
---|
2792 | IEM_CIMPL_DEF_0(iemCImpl_retn_32)
|
---|
2793 | {
|
---|
2794 | return iemCImpl_ReturnNearCommon(pVCpu, cbInstr, IEMMODE_32BIT, 0);
|
---|
2795 | }
|
---|
2796 |
|
---|
2797 |
|
---|
2798 | /**
|
---|
2799 | * Implements retn with 64-bit effective operand size.
|
---|
2800 | */
|
---|
2801 | IEM_CIMPL_DEF_0(iemCImpl_retn_64)
|
---|
2802 | {
|
---|
2803 | return iemCImpl_ReturnNearCommon(pVCpu, cbInstr, IEMMODE_64BIT, 0);
|
---|
2804 | }
|
---|
2805 |
|
---|
2806 |
|
---|
2807 | /**
|
---|
2808 | * Implements enter.
|
---|
2809 | *
|
---|
2810 | * We're doing this in C because the instruction is insane, even for the
|
---|
2811 | * u8NestingLevel=0 case dealing with the stack is tedious.
|
---|
2812 | *
|
---|
2813 | * @param enmEffOpSize The effective operand size.
|
---|
2814 | * @param cbFrame Frame size.
|
---|
2815 | * @param cParameters Frame parameter count.
|
---|
2816 | */
|
---|
2817 | IEM_CIMPL_DEF_3(iemCImpl_enter, IEMMODE, enmEffOpSize, uint16_t, cbFrame, uint8_t, cParameters)
|
---|
2818 | {
|
---|
2819 | /* Push RBP, saving the old value in TmpRbp. */
|
---|
2820 | RTUINT64U NewRsp; NewRsp.u = pVCpu->cpum.GstCtx.rsp;
|
---|
2821 | RTUINT64U TmpRbp; TmpRbp.u = pVCpu->cpum.GstCtx.rbp;
|
---|
2822 | RTUINT64U NewRbp;
|
---|
2823 | VBOXSTRICTRC rcStrict;
|
---|
2824 | if (enmEffOpSize == IEMMODE_64BIT)
|
---|
2825 | {
|
---|
2826 | rcStrict = iemMemStackPushU64Ex(pVCpu, TmpRbp.u, &NewRsp);
|
---|
2827 | NewRbp = NewRsp;
|
---|
2828 | }
|
---|
2829 | else if (enmEffOpSize == IEMMODE_32BIT)
|
---|
2830 | {
|
---|
2831 | rcStrict = iemMemStackPushU32Ex(pVCpu, TmpRbp.DWords.dw0, &NewRsp);
|
---|
2832 | NewRbp = NewRsp;
|
---|
2833 | }
|
---|
2834 | else
|
---|
2835 | {
|
---|
2836 | rcStrict = iemMemStackPushU16Ex(pVCpu, TmpRbp.Words.w0, &NewRsp);
|
---|
2837 | NewRbp = TmpRbp;
|
---|
2838 | NewRbp.Words.w0 = NewRsp.Words.w0;
|
---|
2839 | }
|
---|
2840 | if (rcStrict != VINF_SUCCESS)
|
---|
2841 | return rcStrict;
|
---|
2842 |
|
---|
2843 | /* Copy the parameters (aka nesting levels by Intel). */
|
---|
2844 | cParameters &= 0x1f;
|
---|
2845 | if (cParameters > 0)
|
---|
2846 | {
|
---|
2847 | switch (enmEffOpSize)
|
---|
2848 | {
|
---|
2849 | case IEMMODE_16BIT:
|
---|
2850 | if (pVCpu->cpum.GstCtx.ss.Attr.n.u1DefBig)
|
---|
2851 | TmpRbp.DWords.dw0 -= 2;
|
---|
2852 | else
|
---|
2853 | TmpRbp.Words.w0 -= 2;
|
---|
2854 | do
|
---|
2855 | {
|
---|
2856 | uint16_t u16Tmp;
|
---|
2857 | rcStrict = iemMemStackPopU16Ex(pVCpu, &u16Tmp, &TmpRbp);
|
---|
2858 | if (rcStrict != VINF_SUCCESS)
|
---|
2859 | break;
|
---|
2860 | rcStrict = iemMemStackPushU16Ex(pVCpu, u16Tmp, &NewRsp);
|
---|
2861 | } while (--cParameters > 0 && rcStrict == VINF_SUCCESS);
|
---|
2862 | break;
|
---|
2863 |
|
---|
2864 | case IEMMODE_32BIT:
|
---|
2865 | if (pVCpu->cpum.GstCtx.ss.Attr.n.u1DefBig)
|
---|
2866 | TmpRbp.DWords.dw0 -= 4;
|
---|
2867 | else
|
---|
2868 | TmpRbp.Words.w0 -= 4;
|
---|
2869 | do
|
---|
2870 | {
|
---|
2871 | uint32_t u32Tmp;
|
---|
2872 | rcStrict = iemMemStackPopU32Ex(pVCpu, &u32Tmp, &TmpRbp);
|
---|
2873 | if (rcStrict != VINF_SUCCESS)
|
---|
2874 | break;
|
---|
2875 | rcStrict = iemMemStackPushU32Ex(pVCpu, u32Tmp, &NewRsp);
|
---|
2876 | } while (--cParameters > 0 && rcStrict == VINF_SUCCESS);
|
---|
2877 | break;
|
---|
2878 |
|
---|
2879 | case IEMMODE_64BIT:
|
---|
2880 | TmpRbp.u -= 8;
|
---|
2881 | do
|
---|
2882 | {
|
---|
2883 | uint64_t u64Tmp;
|
---|
2884 | rcStrict = iemMemStackPopU64Ex(pVCpu, &u64Tmp, &TmpRbp);
|
---|
2885 | if (rcStrict != VINF_SUCCESS)
|
---|
2886 | break;
|
---|
2887 | rcStrict = iemMemStackPushU64Ex(pVCpu, u64Tmp, &NewRsp);
|
---|
2888 | } while (--cParameters > 0 && rcStrict == VINF_SUCCESS);
|
---|
2889 | break;
|
---|
2890 |
|
---|
2891 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
2892 | }
|
---|
2893 | if (rcStrict != VINF_SUCCESS)
|
---|
2894 | return VINF_SUCCESS;
|
---|
2895 |
|
---|
2896 | /* Push the new RBP */
|
---|
2897 | if (enmEffOpSize == IEMMODE_64BIT)
|
---|
2898 | rcStrict = iemMemStackPushU64Ex(pVCpu, NewRbp.u, &NewRsp);
|
---|
2899 | else if (enmEffOpSize == IEMMODE_32BIT)
|
---|
2900 | rcStrict = iemMemStackPushU32Ex(pVCpu, NewRbp.DWords.dw0, &NewRsp);
|
---|
2901 | else
|
---|
2902 | rcStrict = iemMemStackPushU16Ex(pVCpu, NewRbp.Words.w0, &NewRsp);
|
---|
2903 | if (rcStrict != VINF_SUCCESS)
|
---|
2904 | return rcStrict;
|
---|
2905 |
|
---|
2906 | }
|
---|
2907 |
|
---|
2908 | /* Recalc RSP. */
|
---|
2909 | iemRegSubFromRspEx(pVCpu, &NewRsp, cbFrame);
|
---|
2910 |
|
---|
2911 | /** @todo Should probe write access at the new RSP according to AMD. */
|
---|
2912 | /** @todo Should handle accesses to the VMX APIC-access page. */
|
---|
2913 |
|
---|
2914 | /* Commit it. */
|
---|
2915 | pVCpu->cpum.GstCtx.rbp = NewRbp.u;
|
---|
2916 | pVCpu->cpum.GstCtx.rsp = NewRsp.u;
|
---|
2917 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
2918 | }
|
---|
2919 |
|
---|
2920 |
|
---|
2921 |
|
---|
2922 | /**
|
---|
2923 | * Implements leave.
|
---|
2924 | *
|
---|
2925 | * We're doing this in C because messing with the stack registers is annoying
|
---|
2926 | * since they depends on SS attributes.
|
---|
2927 | *
|
---|
2928 | * @param enmEffOpSize The effective operand size.
|
---|
2929 | */
|
---|
2930 | IEM_CIMPL_DEF_1(iemCImpl_leave, IEMMODE, enmEffOpSize)
|
---|
2931 | {
|
---|
2932 | /* Calculate the intermediate RSP from RBP and the stack attributes. */
|
---|
2933 | RTUINT64U NewRsp;
|
---|
2934 | if (IEM_IS_64BIT_CODE(pVCpu))
|
---|
2935 | NewRsp.u = pVCpu->cpum.GstCtx.rbp;
|
---|
2936 | else if (pVCpu->cpum.GstCtx.ss.Attr.n.u1DefBig)
|
---|
2937 | NewRsp.u = pVCpu->cpum.GstCtx.ebp;
|
---|
2938 | else
|
---|
2939 | {
|
---|
2940 | /** @todo Check that LEAVE actually preserve the high EBP bits. */
|
---|
2941 | NewRsp.u = pVCpu->cpum.GstCtx.rsp;
|
---|
2942 | NewRsp.Words.w0 = pVCpu->cpum.GstCtx.bp;
|
---|
2943 | }
|
---|
2944 |
|
---|
2945 | /* Pop RBP according to the operand size. */
|
---|
2946 | VBOXSTRICTRC rcStrict;
|
---|
2947 | RTUINT64U NewRbp;
|
---|
2948 | switch (enmEffOpSize)
|
---|
2949 | {
|
---|
2950 | case IEMMODE_16BIT:
|
---|
2951 | NewRbp.u = pVCpu->cpum.GstCtx.rbp;
|
---|
2952 | rcStrict = iemMemStackPopU16Ex(pVCpu, &NewRbp.Words.w0, &NewRsp);
|
---|
2953 | break;
|
---|
2954 | case IEMMODE_32BIT:
|
---|
2955 | NewRbp.u = 0;
|
---|
2956 | rcStrict = iemMemStackPopU32Ex(pVCpu, &NewRbp.DWords.dw0, &NewRsp);
|
---|
2957 | break;
|
---|
2958 | case IEMMODE_64BIT:
|
---|
2959 | rcStrict = iemMemStackPopU64Ex(pVCpu, &NewRbp.u, &NewRsp);
|
---|
2960 | break;
|
---|
2961 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
2962 | }
|
---|
2963 | if (rcStrict != VINF_SUCCESS)
|
---|
2964 | return rcStrict;
|
---|
2965 |
|
---|
2966 |
|
---|
2967 | /* Commit it. */
|
---|
2968 | pVCpu->cpum.GstCtx.rbp = NewRbp.u;
|
---|
2969 | pVCpu->cpum.GstCtx.rsp = NewRsp.u;
|
---|
2970 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
2971 | }
|
---|
2972 |
|
---|
2973 |
|
---|
2974 | /**
|
---|
2975 | * Implements int3 and int XX.
|
---|
2976 | *
|
---|
2977 | * @param u8Int The interrupt vector number.
|
---|
2978 | * @param enmInt The int instruction type.
|
---|
2979 | */
|
---|
2980 | IEM_CIMPL_DEF_2(iemCImpl_int, uint8_t, u8Int, IEMINT, enmInt)
|
---|
2981 | {
|
---|
2982 | Assert(pVCpu->iem.s.cXcptRecursions == 0);
|
---|
2983 |
|
---|
2984 | /*
|
---|
2985 | * We must check if this INT3 might belong to DBGF before raising a #BP.
|
---|
2986 | */
|
---|
2987 | if (u8Int == 3)
|
---|
2988 | {
|
---|
2989 | PVMCC pVM = pVCpu->CTX_SUFF(pVM);
|
---|
2990 | if (pVM->dbgf.ro.cEnabledInt3Breakpoints == 0)
|
---|
2991 | { /* likely: No vbox debugger breakpoints */ }
|
---|
2992 | else
|
---|
2993 | {
|
---|
2994 | VBOXSTRICTRC rcStrict = DBGFTrap03Handler(pVM, pVCpu, &pVCpu->cpum.GstCtx);
|
---|
2995 | Log(("iemCImpl_int: DBGFTrap03Handler -> %Rrc\n", VBOXSTRICTRC_VAL(rcStrict) ));
|
---|
2996 | if (rcStrict != VINF_EM_RAW_GUEST_TRAP)
|
---|
2997 | return iemSetPassUpStatus(pVCpu, rcStrict);
|
---|
2998 | }
|
---|
2999 | }
|
---|
3000 | /** @todo single stepping */
|
---|
3001 | return iemRaiseXcptOrInt(pVCpu,
|
---|
3002 | cbInstr,
|
---|
3003 | u8Int,
|
---|
3004 | IEM_XCPT_FLAGS_T_SOFT_INT | enmInt,
|
---|
3005 | 0,
|
---|
3006 | 0);
|
---|
3007 | }
|
---|
3008 |
|
---|
3009 |
|
---|
3010 | /**
|
---|
3011 | * Implements iret for real mode and V8086 mode.
|
---|
3012 | *
|
---|
3013 | * @param enmEffOpSize The effective operand size.
|
---|
3014 | */
|
---|
3015 | IEM_CIMPL_DEF_1(iemCImpl_iret_real_v8086, IEMMODE, enmEffOpSize)
|
---|
3016 | {
|
---|
3017 | X86EFLAGS Efl;
|
---|
3018 | Efl.u = IEMMISC_GET_EFL(pVCpu);
|
---|
3019 | NOREF(cbInstr);
|
---|
3020 |
|
---|
3021 | /*
|
---|
3022 | * iret throws an exception if VME isn't enabled.
|
---|
3023 | */
|
---|
3024 | if ( Efl.Bits.u1VM
|
---|
3025 | && Efl.Bits.u2IOPL != 3
|
---|
3026 | && !(pVCpu->cpum.GstCtx.cr4 & X86_CR4_VME))
|
---|
3027 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
3028 |
|
---|
3029 | /*
|
---|
3030 | * Do the stack bits, but don't commit RSP before everything checks
|
---|
3031 | * out right.
|
---|
3032 | */
|
---|
3033 | Assert(enmEffOpSize == IEMMODE_32BIT || enmEffOpSize == IEMMODE_16BIT);
|
---|
3034 | VBOXSTRICTRC rcStrict;
|
---|
3035 | RTCPTRUNION uFrame;
|
---|
3036 | uint16_t uNewCs;
|
---|
3037 | uint32_t uNewEip;
|
---|
3038 | uint32_t uNewFlags;
|
---|
3039 | uint64_t uNewRsp;
|
---|
3040 | if (enmEffOpSize == IEMMODE_32BIT)
|
---|
3041 | {
|
---|
3042 | rcStrict = iemMemStackPopBeginSpecial(pVCpu, 12, 1, &uFrame.pv, &uNewRsp);
|
---|
3043 | if (rcStrict != VINF_SUCCESS)
|
---|
3044 | return rcStrict;
|
---|
3045 | uNewEip = uFrame.pu32[0];
|
---|
3046 | if (uNewEip > UINT16_MAX)
|
---|
3047 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
3048 |
|
---|
3049 | uNewCs = (uint16_t)uFrame.pu32[1];
|
---|
3050 | uNewFlags = uFrame.pu32[2];
|
---|
3051 | uNewFlags &= X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF
|
---|
3052 | | X86_EFL_TF | X86_EFL_IF | X86_EFL_DF | X86_EFL_OF | X86_EFL_IOPL | X86_EFL_NT
|
---|
3053 | | X86_EFL_RF /*| X86_EFL_VM*/ | X86_EFL_AC /*|X86_EFL_VIF*/ /*|X86_EFL_VIP*/
|
---|
3054 | | X86_EFL_ID;
|
---|
3055 | if (IEM_GET_TARGET_CPU(pVCpu) <= IEMTARGETCPU_386)
|
---|
3056 | uNewFlags &= ~(X86_EFL_AC | X86_EFL_ID | X86_EFL_VIF | X86_EFL_VIP);
|
---|
3057 | uNewFlags |= Efl.u & (X86_EFL_VM | X86_EFL_VIF | X86_EFL_VIP | X86_EFL_1);
|
---|
3058 | }
|
---|
3059 | else
|
---|
3060 | {
|
---|
3061 | rcStrict = iemMemStackPopBeginSpecial(pVCpu, 6, 1, &uFrame.pv, &uNewRsp);
|
---|
3062 | if (rcStrict != VINF_SUCCESS)
|
---|
3063 | return rcStrict;
|
---|
3064 | uNewEip = uFrame.pu16[0];
|
---|
3065 | uNewCs = uFrame.pu16[1];
|
---|
3066 | uNewFlags = uFrame.pu16[2];
|
---|
3067 | uNewFlags &= X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF
|
---|
3068 | | X86_EFL_TF | X86_EFL_IF | X86_EFL_DF | X86_EFL_OF | X86_EFL_IOPL | X86_EFL_NT;
|
---|
3069 | uNewFlags |= Efl.u & ((UINT32_C(0xffff0000) | X86_EFL_1) & ~X86_EFL_RF);
|
---|
3070 | /** @todo The intel pseudo code does not indicate what happens to
|
---|
3071 | * reserved flags. We just ignore them. */
|
---|
3072 | /* Ancient CPU adjustments: See iemCImpl_popf. */
|
---|
3073 | if (IEM_GET_TARGET_CPU(pVCpu) == IEMTARGETCPU_286)
|
---|
3074 | uNewFlags &= ~(X86_EFL_NT | X86_EFL_IOPL);
|
---|
3075 | }
|
---|
3076 | rcStrict = iemMemStackPopDoneSpecial(pVCpu, uFrame.pv);
|
---|
3077 | if (RT_LIKELY(rcStrict == VINF_SUCCESS))
|
---|
3078 | { /* extremely likely */ }
|
---|
3079 | else
|
---|
3080 | return rcStrict;
|
---|
3081 |
|
---|
3082 | /** @todo Check how this is supposed to work if sp=0xfffe. */
|
---|
3083 | Log7(("iemCImpl_iret_real_v8086: uNewCs=%#06x uNewRip=%#010x uNewFlags=%#x uNewRsp=%#18llx\n",
|
---|
3084 | uNewCs, uNewEip, uNewFlags, uNewRsp));
|
---|
3085 |
|
---|
3086 | /*
|
---|
3087 | * Check the limit of the new EIP.
|
---|
3088 | */
|
---|
3089 | /** @todo Only the AMD pseudo code check the limit here, what's
|
---|
3090 | * right? */
|
---|
3091 | if (uNewEip > pVCpu->cpum.GstCtx.cs.u32Limit)
|
---|
3092 | return iemRaiseSelectorBounds(pVCpu, X86_SREG_CS, IEM_ACCESS_INSTRUCTION);
|
---|
3093 |
|
---|
3094 | /*
|
---|
3095 | * V8086 checks and flag adjustments
|
---|
3096 | */
|
---|
3097 | if (Efl.Bits.u1VM)
|
---|
3098 | {
|
---|
3099 | if (Efl.Bits.u2IOPL == 3)
|
---|
3100 | {
|
---|
3101 | /* Preserve IOPL and clear RF. */
|
---|
3102 | uNewFlags &= ~(X86_EFL_IOPL | X86_EFL_RF);
|
---|
3103 | uNewFlags |= Efl.u & (X86_EFL_IOPL);
|
---|
3104 | }
|
---|
3105 | else if ( enmEffOpSize == IEMMODE_16BIT
|
---|
3106 | && ( !(uNewFlags & X86_EFL_IF)
|
---|
3107 | || !Efl.Bits.u1VIP )
|
---|
3108 | && !(uNewFlags & X86_EFL_TF) )
|
---|
3109 | {
|
---|
3110 | /* Move IF to VIF, clear RF and preserve IF and IOPL.*/
|
---|
3111 | uNewFlags &= ~X86_EFL_VIF;
|
---|
3112 | uNewFlags |= (uNewFlags & X86_EFL_IF) << (19 - 9);
|
---|
3113 | uNewFlags &= ~(X86_EFL_IF | X86_EFL_IOPL | X86_EFL_RF);
|
---|
3114 | uNewFlags |= Efl.u & (X86_EFL_IF | X86_EFL_IOPL);
|
---|
3115 | }
|
---|
3116 | else
|
---|
3117 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
3118 | Log7(("iemCImpl_iret_real_v8086: u1VM=1: adjusted uNewFlags=%#x\n", uNewFlags));
|
---|
3119 | }
|
---|
3120 |
|
---|
3121 | /*
|
---|
3122 | * Commit the operation.
|
---|
3123 | */
|
---|
3124 | #ifdef DBGFTRACE_ENABLED
|
---|
3125 | RTTraceBufAddMsgF(pVCpu->CTX_SUFF(pVM)->CTX_SUFF(hTraceBuf), "iret/rm %04x:%04x -> %04x:%04x %x %04llx",
|
---|
3126 | pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.eip, uNewCs, uNewEip, uNewFlags, uNewRsp);
|
---|
3127 | #endif
|
---|
3128 | pVCpu->cpum.GstCtx.rsp = uNewRsp;
|
---|
3129 | pVCpu->cpum.GstCtx.rip = uNewEip;
|
---|
3130 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
|
---|
3131 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
|
---|
3132 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
3133 | pVCpu->cpum.GstCtx.cs.u64Base = (uint32_t)uNewCs << 4;
|
---|
3134 | /** @todo do we load attribs and limit as well? */
|
---|
3135 | Assert(uNewFlags & X86_EFL_1);
|
---|
3136 | IEMMISC_SET_EFL(pVCpu, uNewFlags);
|
---|
3137 |
|
---|
3138 | /* Flush the prefetch buffer. */
|
---|
3139 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr); /** @todo can do light flush in real mode at least */
|
---|
3140 |
|
---|
3141 | /** @todo single stepping */
|
---|
3142 | return VINF_SUCCESS;
|
---|
3143 | }
|
---|
3144 |
|
---|
3145 |
|
---|
3146 | /**
|
---|
3147 | * Loads a segment register when entering V8086 mode.
|
---|
3148 | *
|
---|
3149 | * @param pSReg The segment register.
|
---|
3150 | * @param uSeg The segment to load.
|
---|
3151 | */
|
---|
3152 | static void iemCImplCommonV8086LoadSeg(PCPUMSELREG pSReg, uint16_t uSeg)
|
---|
3153 | {
|
---|
3154 | pSReg->Sel = uSeg;
|
---|
3155 | pSReg->ValidSel = uSeg;
|
---|
3156 | pSReg->fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
3157 | pSReg->u64Base = (uint32_t)uSeg << 4;
|
---|
3158 | pSReg->u32Limit = 0xffff;
|
---|
3159 | pSReg->Attr.u = X86_SEL_TYPE_RW_ACC | RT_BIT(4) /*!sys*/ | RT_BIT(7) /*P*/ | (3 /*DPL*/ << 5); /* VT-x wants 0xf3 */
|
---|
3160 | /** @todo Testcase: Check if VT-x really needs this and what it does itself when
|
---|
3161 | * IRET'ing to V8086. */
|
---|
3162 | }
|
---|
3163 |
|
---|
3164 |
|
---|
3165 | /**
|
---|
3166 | * Implements iret for protected mode returning to V8086 mode.
|
---|
3167 | *
|
---|
3168 | * @param uNewEip The new EIP.
|
---|
3169 | * @param uNewCs The new CS.
|
---|
3170 | * @param uNewFlags The new EFLAGS.
|
---|
3171 | * @param uNewRsp The RSP after the initial IRET frame.
|
---|
3172 | *
|
---|
3173 | * @note This can only be a 32-bit iret du to the X86_EFL_VM position.
|
---|
3174 | */
|
---|
3175 | IEM_CIMPL_DEF_4(iemCImpl_iret_prot_v8086, uint32_t, uNewEip, uint16_t, uNewCs, uint32_t, uNewFlags, uint64_t, uNewRsp)
|
---|
3176 | {
|
---|
3177 | RT_NOREF_PV(cbInstr);
|
---|
3178 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SREG_MASK);
|
---|
3179 |
|
---|
3180 | /*
|
---|
3181 | * Pop the V8086 specific frame bits off the stack.
|
---|
3182 | */
|
---|
3183 | VBOXSTRICTRC rcStrict;
|
---|
3184 | RTCPTRUNION uFrame;
|
---|
3185 | rcStrict = iemMemStackPopContinueSpecial(pVCpu, 0 /*off*/, 24 /*cbMem*/, &uFrame.pv, uNewRsp);
|
---|
3186 | if (rcStrict != VINF_SUCCESS)
|
---|
3187 | return rcStrict;
|
---|
3188 | uint32_t uNewEsp = uFrame.pu32[0];
|
---|
3189 | uint16_t uNewSs = uFrame.pu32[1];
|
---|
3190 | uint16_t uNewEs = uFrame.pu32[2];
|
---|
3191 | uint16_t uNewDs = uFrame.pu32[3];
|
---|
3192 | uint16_t uNewFs = uFrame.pu32[4];
|
---|
3193 | uint16_t uNewGs = uFrame.pu32[5];
|
---|
3194 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)uFrame.pv, IEM_ACCESS_STACK_R); /* don't use iemMemStackPopCommitSpecial here. */
|
---|
3195 | if (rcStrict != VINF_SUCCESS)
|
---|
3196 | return rcStrict;
|
---|
3197 |
|
---|
3198 | /*
|
---|
3199 | * Commit the operation.
|
---|
3200 | */
|
---|
3201 | uNewFlags &= X86_EFL_LIVE_MASK;
|
---|
3202 | uNewFlags |= X86_EFL_RA1_MASK;
|
---|
3203 | #ifdef DBGFTRACE_ENABLED
|
---|
3204 | RTTraceBufAddMsgF(pVCpu->CTX_SUFF(pVM)->CTX_SUFF(hTraceBuf), "iret/p/v %04x:%08x -> %04x:%04x %x %04x:%04x",
|
---|
3205 | pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.eip, uNewCs, uNewEip, uNewFlags, uNewSs, uNewEsp);
|
---|
3206 | #endif
|
---|
3207 | 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));
|
---|
3208 |
|
---|
3209 | IEMMISC_SET_EFL(pVCpu, uNewFlags);
|
---|
3210 | iemCImplCommonV8086LoadSeg(&pVCpu->cpum.GstCtx.cs, uNewCs);
|
---|
3211 | iemCImplCommonV8086LoadSeg(&pVCpu->cpum.GstCtx.ss, uNewSs);
|
---|
3212 | iemCImplCommonV8086LoadSeg(&pVCpu->cpum.GstCtx.es, uNewEs);
|
---|
3213 | iemCImplCommonV8086LoadSeg(&pVCpu->cpum.GstCtx.ds, uNewDs);
|
---|
3214 | iemCImplCommonV8086LoadSeg(&pVCpu->cpum.GstCtx.fs, uNewFs);
|
---|
3215 | iemCImplCommonV8086LoadSeg(&pVCpu->cpum.GstCtx.gs, uNewGs);
|
---|
3216 | pVCpu->cpum.GstCtx.rip = (uint16_t)uNewEip;
|
---|
3217 | pVCpu->cpum.GstCtx.rsp = uNewEsp; /** @todo check this out! */
|
---|
3218 | pVCpu->iem.s.fExec = (pVCpu->iem.s.fExec & ~(IEM_F_MODE_MASK | IEM_F_X86_CPL_MASK))
|
---|
3219 | | (3 << IEM_F_X86_CPL_SHIFT)
|
---|
3220 | | IEM_F_MODE_X86_16BIT_PROT_V86;
|
---|
3221 |
|
---|
3222 | /* Flush the prefetch buffer. */
|
---|
3223 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr);
|
---|
3224 |
|
---|
3225 | /** @todo single stepping */
|
---|
3226 | return VINF_SUCCESS;
|
---|
3227 | }
|
---|
3228 |
|
---|
3229 |
|
---|
3230 | /**
|
---|
3231 | * Implements iret for protected mode returning via a nested task.
|
---|
3232 | *
|
---|
3233 | * @param enmEffOpSize The effective operand size.
|
---|
3234 | */
|
---|
3235 | IEM_CIMPL_DEF_1(iemCImpl_iret_prot_NestedTask, IEMMODE, enmEffOpSize)
|
---|
3236 | {
|
---|
3237 | Log7(("iemCImpl_iret_prot_NestedTask:\n"));
|
---|
3238 | #ifndef IEM_IMPLEMENTS_TASKSWITCH
|
---|
3239 | IEM_RETURN_ASPECT_NOT_IMPLEMENTED();
|
---|
3240 | #else
|
---|
3241 | RT_NOREF_PV(enmEffOpSize);
|
---|
3242 |
|
---|
3243 | /*
|
---|
3244 | * Read the segment selector in the link-field of the current TSS.
|
---|
3245 | */
|
---|
3246 | RTSEL uSelRet;
|
---|
3247 | VBOXSTRICTRC rcStrict = iemMemFetchSysU16(pVCpu, &uSelRet, UINT8_MAX, pVCpu->cpum.GstCtx.tr.u64Base);
|
---|
3248 | if (rcStrict != VINF_SUCCESS)
|
---|
3249 | return rcStrict;
|
---|
3250 |
|
---|
3251 | /*
|
---|
3252 | * Fetch the returning task's TSS descriptor from the GDT.
|
---|
3253 | */
|
---|
3254 | if (uSelRet & X86_SEL_LDT)
|
---|
3255 | {
|
---|
3256 | Log(("iret_prot_NestedTask TSS not in LDT. uSelRet=%04x -> #TS\n", uSelRet));
|
---|
3257 | return iemRaiseTaskSwitchFaultBySelector(pVCpu, uSelRet);
|
---|
3258 | }
|
---|
3259 |
|
---|
3260 | IEMSELDESC TssDesc;
|
---|
3261 | rcStrict = iemMemFetchSelDesc(pVCpu, &TssDesc, uSelRet, X86_XCPT_GP);
|
---|
3262 | if (rcStrict != VINF_SUCCESS)
|
---|
3263 | return rcStrict;
|
---|
3264 |
|
---|
3265 | if (TssDesc.Legacy.Gate.u1DescType)
|
---|
3266 | {
|
---|
3267 | Log(("iret_prot_NestedTask Invalid TSS type. uSelRet=%04x -> #TS\n", uSelRet));
|
---|
3268 | return iemRaiseTaskSwitchFaultBySelector(pVCpu, uSelRet & X86_SEL_MASK_OFF_RPL);
|
---|
3269 | }
|
---|
3270 |
|
---|
3271 | if ( TssDesc.Legacy.Gate.u4Type != X86_SEL_TYPE_SYS_286_TSS_BUSY
|
---|
3272 | && TssDesc.Legacy.Gate.u4Type != X86_SEL_TYPE_SYS_386_TSS_BUSY)
|
---|
3273 | {
|
---|
3274 | Log(("iret_prot_NestedTask TSS is not busy. uSelRet=%04x DescType=%#x -> #TS\n", uSelRet, TssDesc.Legacy.Gate.u4Type));
|
---|
3275 | return iemRaiseTaskSwitchFaultBySelector(pVCpu, uSelRet & X86_SEL_MASK_OFF_RPL);
|
---|
3276 | }
|
---|
3277 |
|
---|
3278 | if (!TssDesc.Legacy.Gate.u1Present)
|
---|
3279 | {
|
---|
3280 | Log(("iret_prot_NestedTask TSS is not present. uSelRet=%04x -> #NP\n", uSelRet));
|
---|
3281 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uSelRet & X86_SEL_MASK_OFF_RPL);
|
---|
3282 | }
|
---|
3283 |
|
---|
3284 | uint32_t uNextEip = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
3285 | return iemTaskSwitch(pVCpu, IEMTASKSWITCH_IRET, uNextEip, 0 /* fFlags */, 0 /* uErr */,
|
---|
3286 | 0 /* uCr2 */, uSelRet, &TssDesc);
|
---|
3287 | #endif
|
---|
3288 | }
|
---|
3289 |
|
---|
3290 |
|
---|
3291 | /**
|
---|
3292 | * Implements iret for protected mode
|
---|
3293 | *
|
---|
3294 | * @param enmEffOpSize The effective operand size.
|
---|
3295 | */
|
---|
3296 | IEM_CIMPL_DEF_1(iemCImpl_iret_prot, IEMMODE, enmEffOpSize)
|
---|
3297 | {
|
---|
3298 | NOREF(cbInstr);
|
---|
3299 | Assert(enmEffOpSize == IEMMODE_32BIT || enmEffOpSize == IEMMODE_16BIT);
|
---|
3300 |
|
---|
3301 | /*
|
---|
3302 | * Nested task return.
|
---|
3303 | */
|
---|
3304 | if (pVCpu->cpum.GstCtx.eflags.Bits.u1NT)
|
---|
3305 | return IEM_CIMPL_CALL_1(iemCImpl_iret_prot_NestedTask, enmEffOpSize);
|
---|
3306 |
|
---|
3307 | /*
|
---|
3308 | * Normal return.
|
---|
3309 | *
|
---|
3310 | * Do the stack bits, but don't commit RSP before everything checks
|
---|
3311 | * out right.
|
---|
3312 | */
|
---|
3313 | Assert(enmEffOpSize == IEMMODE_32BIT || enmEffOpSize == IEMMODE_16BIT);
|
---|
3314 | VBOXSTRICTRC rcStrict;
|
---|
3315 | RTCPTRUNION uFrame;
|
---|
3316 | uint16_t uNewCs;
|
---|
3317 | uint32_t uNewEip;
|
---|
3318 | uint32_t uNewFlags;
|
---|
3319 | uint64_t uNewRsp;
|
---|
3320 | if (enmEffOpSize == IEMMODE_32BIT)
|
---|
3321 | {
|
---|
3322 | rcStrict = iemMemStackPopBeginSpecial(pVCpu, 12, 3, &uFrame.pv, &uNewRsp);
|
---|
3323 | if (rcStrict != VINF_SUCCESS)
|
---|
3324 | return rcStrict;
|
---|
3325 | uNewEip = uFrame.pu32[0];
|
---|
3326 | uNewCs = (uint16_t)uFrame.pu32[1];
|
---|
3327 | uNewFlags = uFrame.pu32[2];
|
---|
3328 | }
|
---|
3329 | else
|
---|
3330 | {
|
---|
3331 | rcStrict = iemMemStackPopBeginSpecial(pVCpu, 6, 1, &uFrame.pv, &uNewRsp);
|
---|
3332 | if (rcStrict != VINF_SUCCESS)
|
---|
3333 | return rcStrict;
|
---|
3334 | uNewEip = uFrame.pu16[0];
|
---|
3335 | uNewCs = uFrame.pu16[1];
|
---|
3336 | uNewFlags = uFrame.pu16[2];
|
---|
3337 | }
|
---|
3338 | rcStrict = iemMemStackPopDoneSpecial(pVCpu, (void *)uFrame.pv); /* don't use iemMemStackPopCommitSpecial here. */
|
---|
3339 | if (RT_LIKELY(rcStrict == VINF_SUCCESS))
|
---|
3340 | { /* extremely likely */ }
|
---|
3341 | else
|
---|
3342 | return rcStrict;
|
---|
3343 | Log7(("iemCImpl_iret_prot: uNewCs=%#06x uNewEip=%#010x uNewFlags=%#x uNewRsp=%#18llx uCpl=%u\n", uNewCs, uNewEip, uNewFlags, uNewRsp, IEM_GET_CPL(pVCpu)));
|
---|
3344 |
|
---|
3345 | /*
|
---|
3346 | * We're hopefully not returning to V8086 mode...
|
---|
3347 | */
|
---|
3348 | if ( (uNewFlags & X86_EFL_VM)
|
---|
3349 | && IEM_GET_CPL(pVCpu) == 0)
|
---|
3350 | {
|
---|
3351 | Assert(enmEffOpSize == IEMMODE_32BIT);
|
---|
3352 | return IEM_CIMPL_CALL_4(iemCImpl_iret_prot_v8086, uNewEip, uNewCs, uNewFlags, uNewRsp);
|
---|
3353 | }
|
---|
3354 |
|
---|
3355 | /*
|
---|
3356 | * Protected mode.
|
---|
3357 | */
|
---|
3358 | /* Read the CS descriptor. */
|
---|
3359 | if (!(uNewCs & X86_SEL_MASK_OFF_RPL))
|
---|
3360 | {
|
---|
3361 | Log(("iret %04x:%08x -> invalid CS selector, #GP(0)\n", uNewCs, uNewEip));
|
---|
3362 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
3363 | }
|
---|
3364 |
|
---|
3365 | IEMSELDESC DescCS;
|
---|
3366 | rcStrict = iemMemFetchSelDesc(pVCpu, &DescCS, uNewCs, X86_XCPT_GP);
|
---|
3367 | if (rcStrict != VINF_SUCCESS)
|
---|
3368 | {
|
---|
3369 | Log(("iret %04x:%08x - rcStrict=%Rrc when fetching CS\n", uNewCs, uNewEip, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
3370 | return rcStrict;
|
---|
3371 | }
|
---|
3372 |
|
---|
3373 | /* Must be a code descriptor. */
|
---|
3374 | if (!DescCS.Legacy.Gen.u1DescType)
|
---|
3375 | {
|
---|
3376 | Log(("iret %04x:%08x - CS is system segment (%#x) -> #GP\n", uNewCs, uNewEip, DescCS.Legacy.Gen.u4Type));
|
---|
3377 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
3378 | }
|
---|
3379 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE))
|
---|
3380 | {
|
---|
3381 | Log(("iret %04x:%08x - not code segment (%#x) -> #GP\n", uNewCs, uNewEip, DescCS.Legacy.Gen.u4Type));
|
---|
3382 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
3383 | }
|
---|
3384 |
|
---|
3385 | /* Privilege checks. */
|
---|
3386 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF))
|
---|
3387 | {
|
---|
3388 | if ((uNewCs & X86_SEL_RPL) != DescCS.Legacy.Gen.u2Dpl)
|
---|
3389 | {
|
---|
3390 | Log(("iret %04x:%08x - RPL != DPL (%d) -> #GP\n", uNewCs, uNewEip, DescCS.Legacy.Gen.u2Dpl));
|
---|
3391 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
3392 | }
|
---|
3393 | }
|
---|
3394 | else if ((uNewCs & X86_SEL_RPL) < DescCS.Legacy.Gen.u2Dpl)
|
---|
3395 | {
|
---|
3396 | Log(("iret %04x:%08x - RPL < DPL (%d) -> #GP\n", uNewCs, uNewEip, DescCS.Legacy.Gen.u2Dpl));
|
---|
3397 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
3398 | }
|
---|
3399 | if ((uNewCs & X86_SEL_RPL) < IEM_GET_CPL(pVCpu))
|
---|
3400 | {
|
---|
3401 | Log(("iret %04x:%08x - RPL < CPL (%d) -> #GP\n", uNewCs, uNewEip, IEM_GET_CPL(pVCpu)));
|
---|
3402 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
3403 | }
|
---|
3404 |
|
---|
3405 | /* Present? */
|
---|
3406 | if (!DescCS.Legacy.Gen.u1Present)
|
---|
3407 | {
|
---|
3408 | Log(("iret %04x:%08x - CS not present -> #NP\n", uNewCs, uNewEip));
|
---|
3409 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uNewCs);
|
---|
3410 | }
|
---|
3411 |
|
---|
3412 | uint32_t cbLimitCS = X86DESC_LIMIT_G(&DescCS.Legacy);
|
---|
3413 |
|
---|
3414 | /*
|
---|
3415 | * Return to outer level?
|
---|
3416 | */
|
---|
3417 | if ((uNewCs & X86_SEL_RPL) != IEM_GET_CPL(pVCpu))
|
---|
3418 | {
|
---|
3419 | uint16_t uNewSS;
|
---|
3420 | uint32_t uNewESP;
|
---|
3421 | if (enmEffOpSize == IEMMODE_32BIT)
|
---|
3422 | {
|
---|
3423 | rcStrict = iemMemStackPopContinueSpecial(pVCpu, 0/*off*/, 8 /*cbMem*/, &uFrame.pv, uNewRsp);
|
---|
3424 | if (rcStrict != VINF_SUCCESS)
|
---|
3425 | return rcStrict;
|
---|
3426 | /** @todo We might be popping a 32-bit ESP from the IRET frame, but whether
|
---|
3427 | * 16-bit or 32-bit are being loaded into SP depends on the D/B
|
---|
3428 | * bit of the popped SS selector it turns out. */
|
---|
3429 | uNewESP = uFrame.pu32[0];
|
---|
3430 | uNewSS = (uint16_t)uFrame.pu32[1];
|
---|
3431 | }
|
---|
3432 | else
|
---|
3433 | {
|
---|
3434 | rcStrict = iemMemStackPopContinueSpecial(pVCpu, 0 /*off*/, 4 /*cbMem*/, &uFrame.pv, uNewRsp);
|
---|
3435 | if (rcStrict != VINF_SUCCESS)
|
---|
3436 | return rcStrict;
|
---|
3437 | uNewESP = uFrame.pu16[0];
|
---|
3438 | uNewSS = uFrame.pu16[1];
|
---|
3439 | }
|
---|
3440 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)uFrame.pv, IEM_ACCESS_STACK_R);
|
---|
3441 | if (rcStrict != VINF_SUCCESS)
|
---|
3442 | return rcStrict;
|
---|
3443 | Log7(("iemCImpl_iret_prot: uNewSS=%#06x uNewESP=%#010x\n", uNewSS, uNewESP));
|
---|
3444 |
|
---|
3445 | /* Read the SS descriptor. */
|
---|
3446 | if (!(uNewSS & X86_SEL_MASK_OFF_RPL))
|
---|
3447 | {
|
---|
3448 | Log(("iret %04x:%08x/%04x:%08x -> invalid SS selector, #GP(0)\n", uNewCs, uNewEip, uNewSS, uNewESP));
|
---|
3449 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
3450 | }
|
---|
3451 |
|
---|
3452 | IEMSELDESC DescSS;
|
---|
3453 | rcStrict = iemMemFetchSelDesc(pVCpu, &DescSS, uNewSS, X86_XCPT_GP); /** @todo Correct exception? */
|
---|
3454 | if (rcStrict != VINF_SUCCESS)
|
---|
3455 | {
|
---|
3456 | Log(("iret %04x:%08x/%04x:%08x - %Rrc when fetching SS\n",
|
---|
3457 | uNewCs, uNewEip, uNewSS, uNewESP, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
3458 | return rcStrict;
|
---|
3459 | }
|
---|
3460 |
|
---|
3461 | /* Privilege checks. */
|
---|
3462 | if ((uNewSS & X86_SEL_RPL) != (uNewCs & X86_SEL_RPL))
|
---|
3463 | {
|
---|
3464 | Log(("iret %04x:%08x/%04x:%08x -> SS.RPL != CS.RPL -> #GP\n", uNewCs, uNewEip, uNewSS, uNewESP));
|
---|
3465 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSS);
|
---|
3466 | }
|
---|
3467 | if (DescSS.Legacy.Gen.u2Dpl != (uNewCs & X86_SEL_RPL))
|
---|
3468 | {
|
---|
3469 | Log(("iret %04x:%08x/%04x:%08x -> SS.DPL (%d) != CS.RPL -> #GP\n",
|
---|
3470 | uNewCs, uNewEip, uNewSS, uNewESP, DescSS.Legacy.Gen.u2Dpl));
|
---|
3471 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSS);
|
---|
3472 | }
|
---|
3473 |
|
---|
3474 | /* Must be a writeable data segment descriptor. */
|
---|
3475 | if (!DescSS.Legacy.Gen.u1DescType)
|
---|
3476 | {
|
---|
3477 | Log(("iret %04x:%08x/%04x:%08x -> SS is system segment (%#x) -> #GP\n",
|
---|
3478 | uNewCs, uNewEip, uNewSS, uNewESP, DescSS.Legacy.Gen.u4Type));
|
---|
3479 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSS);
|
---|
3480 | }
|
---|
3481 | if ((DescSS.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_WRITE)) != X86_SEL_TYPE_WRITE)
|
---|
3482 | {
|
---|
3483 | Log(("iret %04x:%08x/%04x:%08x - not writable data segment (%#x) -> #GP\n",
|
---|
3484 | uNewCs, uNewEip, uNewSS, uNewESP, DescSS.Legacy.Gen.u4Type));
|
---|
3485 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSS);
|
---|
3486 | }
|
---|
3487 |
|
---|
3488 | /* Present? */
|
---|
3489 | if (!DescSS.Legacy.Gen.u1Present)
|
---|
3490 | {
|
---|
3491 | Log(("iret %04x:%08x/%04x:%08x -> SS not present -> #SS\n", uNewCs, uNewEip, uNewSS, uNewESP));
|
---|
3492 | return iemRaiseStackSelectorNotPresentBySelector(pVCpu, uNewSS);
|
---|
3493 | }
|
---|
3494 |
|
---|
3495 | uint32_t cbLimitSs = X86DESC_LIMIT_G(&DescSS.Legacy);
|
---|
3496 |
|
---|
3497 | /* Check EIP. */
|
---|
3498 | if (uNewEip > cbLimitCS)
|
---|
3499 | {
|
---|
3500 | Log(("iret %04x:%08x/%04x:%08x -> EIP is out of bounds (%#x) -> #GP(0)\n",
|
---|
3501 | uNewCs, uNewEip, uNewSS, uNewESP, cbLimitCS));
|
---|
3502 | /** @todo Which is it, \#GP(0) or \#GP(sel)? */
|
---|
3503 | return iemRaiseSelectorBoundsBySelector(pVCpu, uNewCs);
|
---|
3504 | }
|
---|
3505 |
|
---|
3506 | /*
|
---|
3507 | * Commit the changes, marking CS and SS accessed first since
|
---|
3508 | * that may fail.
|
---|
3509 | */
|
---|
3510 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
3511 | {
|
---|
3512 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewCs);
|
---|
3513 | if (rcStrict != VINF_SUCCESS)
|
---|
3514 | return rcStrict;
|
---|
3515 | DescCS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
3516 | }
|
---|
3517 | if (!(DescSS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
3518 | {
|
---|
3519 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewSS);
|
---|
3520 | if (rcStrict != VINF_SUCCESS)
|
---|
3521 | return rcStrict;
|
---|
3522 | DescSS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
3523 | }
|
---|
3524 |
|
---|
3525 | uint32_t fEFlagsMask = X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF
|
---|
3526 | | X86_EFL_TF | X86_EFL_DF | X86_EFL_OF | X86_EFL_NT;
|
---|
3527 | if (enmEffOpSize != IEMMODE_16BIT)
|
---|
3528 | fEFlagsMask |= X86_EFL_RF | X86_EFL_AC | X86_EFL_ID;
|
---|
3529 | if (IEM_GET_CPL(pVCpu) == 0)
|
---|
3530 | fEFlagsMask |= X86_EFL_IF | X86_EFL_IOPL | X86_EFL_VIF | X86_EFL_VIP; /* VM is 0 */
|
---|
3531 | else if (IEM_GET_CPL(pVCpu) <= pVCpu->cpum.GstCtx.eflags.Bits.u2IOPL)
|
---|
3532 | fEFlagsMask |= X86_EFL_IF;
|
---|
3533 | if (IEM_GET_TARGET_CPU(pVCpu) <= IEMTARGETCPU_386)
|
---|
3534 | fEFlagsMask &= ~(X86_EFL_AC | X86_EFL_ID | X86_EFL_VIF | X86_EFL_VIP);
|
---|
3535 | uint32_t fEFlagsNew = IEMMISC_GET_EFL(pVCpu);
|
---|
3536 | fEFlagsNew &= ~fEFlagsMask;
|
---|
3537 | fEFlagsNew |= uNewFlags & fEFlagsMask;
|
---|
3538 | #ifdef DBGFTRACE_ENABLED
|
---|
3539 | RTTraceBufAddMsgF(pVCpu->CTX_SUFF(pVM)->CTX_SUFF(hTraceBuf), "iret/%up%u %04x:%08x -> %04x:%04x %x %04x:%04x",
|
---|
3540 | IEM_GET_CPL(pVCpu), uNewCs & X86_SEL_RPL, pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.eip,
|
---|
3541 | uNewCs, uNewEip, uNewFlags, uNewSS, uNewESP);
|
---|
3542 | #endif
|
---|
3543 |
|
---|
3544 | IEMMISC_SET_EFL(pVCpu, fEFlagsNew);
|
---|
3545 | pVCpu->cpum.GstCtx.rip = uNewEip;
|
---|
3546 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
|
---|
3547 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
|
---|
3548 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
3549 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCS.Legacy);
|
---|
3550 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimitCS;
|
---|
3551 | pVCpu->cpum.GstCtx.cs.u64Base = X86DESC_BASE(&DescCS.Legacy);
|
---|
3552 |
|
---|
3553 | pVCpu->cpum.GstCtx.ss.Sel = uNewSS;
|
---|
3554 | pVCpu->cpum.GstCtx.ss.ValidSel = uNewSS;
|
---|
3555 | pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
3556 | pVCpu->cpum.GstCtx.ss.Attr.u = X86DESC_GET_HID_ATTR(&DescSS.Legacy);
|
---|
3557 | pVCpu->cpum.GstCtx.ss.u32Limit = cbLimitSs;
|
---|
3558 | pVCpu->cpum.GstCtx.ss.u64Base = X86DESC_BASE(&DescSS.Legacy);
|
---|
3559 | if (!pVCpu->cpum.GstCtx.ss.Attr.n.u1DefBig)
|
---|
3560 | pVCpu->cpum.GstCtx.sp = (uint16_t)uNewESP;
|
---|
3561 | else
|
---|
3562 | pVCpu->cpum.GstCtx.rsp = uNewESP;
|
---|
3563 |
|
---|
3564 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.ds);
|
---|
3565 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.es);
|
---|
3566 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.fs);
|
---|
3567 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.gs);
|
---|
3568 |
|
---|
3569 | iemRecalcExecModeAndCplFlags(pVCpu);
|
---|
3570 |
|
---|
3571 | /* Done! */
|
---|
3572 |
|
---|
3573 | }
|
---|
3574 | /*
|
---|
3575 | * Return to the same level.
|
---|
3576 | */
|
---|
3577 | else
|
---|
3578 | {
|
---|
3579 | /* Check EIP. */
|
---|
3580 | if (uNewEip > cbLimitCS)
|
---|
3581 | {
|
---|
3582 | Log(("iret %04x:%08x - EIP is out of bounds (%#x) -> #GP(0)\n", uNewCs, uNewEip, cbLimitCS));
|
---|
3583 | /** @todo Which is it, \#GP(0) or \#GP(sel)? */
|
---|
3584 | return iemRaiseSelectorBoundsBySelector(pVCpu, uNewCs);
|
---|
3585 | }
|
---|
3586 |
|
---|
3587 | /*
|
---|
3588 | * Commit the changes, marking CS first since it may fail.
|
---|
3589 | */
|
---|
3590 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
3591 | {
|
---|
3592 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewCs);
|
---|
3593 | if (rcStrict != VINF_SUCCESS)
|
---|
3594 | return rcStrict;
|
---|
3595 | DescCS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
3596 | }
|
---|
3597 |
|
---|
3598 | X86EFLAGS NewEfl;
|
---|
3599 | NewEfl.u = IEMMISC_GET_EFL(pVCpu);
|
---|
3600 | uint32_t fEFlagsMask = X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF
|
---|
3601 | | X86_EFL_TF | X86_EFL_DF | X86_EFL_OF | X86_EFL_NT;
|
---|
3602 | if (enmEffOpSize != IEMMODE_16BIT)
|
---|
3603 | fEFlagsMask |= X86_EFL_RF | X86_EFL_AC | X86_EFL_ID;
|
---|
3604 | if (IEM_GET_CPL(pVCpu) == 0)
|
---|
3605 | fEFlagsMask |= X86_EFL_IF | X86_EFL_IOPL | X86_EFL_VIF | X86_EFL_VIP; /* VM is 0 */
|
---|
3606 | else if (IEM_GET_CPL(pVCpu) <= NewEfl.Bits.u2IOPL)
|
---|
3607 | fEFlagsMask |= X86_EFL_IF;
|
---|
3608 | if (IEM_GET_TARGET_CPU(pVCpu) <= IEMTARGETCPU_386)
|
---|
3609 | fEFlagsMask &= ~(X86_EFL_AC | X86_EFL_ID | X86_EFL_VIF | X86_EFL_VIP);
|
---|
3610 | NewEfl.u &= ~fEFlagsMask;
|
---|
3611 | NewEfl.u |= fEFlagsMask & uNewFlags;
|
---|
3612 | #ifdef DBGFTRACE_ENABLED
|
---|
3613 | RTTraceBufAddMsgF(pVCpu->CTX_SUFF(pVM)->CTX_SUFF(hTraceBuf), "iret/%up %04x:%08x -> %04x:%04x %x %04x:%04llx",
|
---|
3614 | IEM_GET_CPL(pVCpu), pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.eip,
|
---|
3615 | uNewCs, uNewEip, uNewFlags, pVCpu->cpum.GstCtx.ss.Sel, uNewRsp);
|
---|
3616 | #endif
|
---|
3617 |
|
---|
3618 | IEMMISC_SET_EFL(pVCpu, NewEfl.u);
|
---|
3619 | pVCpu->cpum.GstCtx.rip = uNewEip;
|
---|
3620 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
|
---|
3621 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
|
---|
3622 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
3623 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCS.Legacy);
|
---|
3624 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimitCS;
|
---|
3625 | pVCpu->cpum.GstCtx.cs.u64Base = X86DESC_BASE(&DescCS.Legacy);
|
---|
3626 | if (!pVCpu->cpum.GstCtx.ss.Attr.n.u1DefBig)
|
---|
3627 | pVCpu->cpum.GstCtx.sp = (uint16_t)uNewRsp;
|
---|
3628 | else
|
---|
3629 | pVCpu->cpum.GstCtx.rsp = uNewRsp;
|
---|
3630 |
|
---|
3631 | iemRecalcExecModeAndCplFlags(pVCpu);
|
---|
3632 |
|
---|
3633 | /* Done! */
|
---|
3634 | }
|
---|
3635 |
|
---|
3636 | /* Flush the prefetch buffer. */
|
---|
3637 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr); /** @todo may light flush if same ring? */
|
---|
3638 |
|
---|
3639 | /** @todo single stepping */
|
---|
3640 | return VINF_SUCCESS;
|
---|
3641 | }
|
---|
3642 |
|
---|
3643 |
|
---|
3644 | /**
|
---|
3645 | * Implements iret for long mode
|
---|
3646 | *
|
---|
3647 | * @param enmEffOpSize The effective operand size.
|
---|
3648 | */
|
---|
3649 | IEM_CIMPL_DEF_1(iemCImpl_iret_64bit, IEMMODE, enmEffOpSize)
|
---|
3650 | {
|
---|
3651 | NOREF(cbInstr);
|
---|
3652 |
|
---|
3653 | /*
|
---|
3654 | * Nested task return is not supported in long mode.
|
---|
3655 | */
|
---|
3656 | if (pVCpu->cpum.GstCtx.eflags.Bits.u1NT)
|
---|
3657 | {
|
---|
3658 | Log(("iretq with NT=1 (eflags=%#x) -> #GP(0)\n", pVCpu->cpum.GstCtx.eflags.u));
|
---|
3659 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
3660 | }
|
---|
3661 |
|
---|
3662 | /*
|
---|
3663 | * Normal return.
|
---|
3664 | *
|
---|
3665 | * Do the stack bits, but don't commit RSP before everything checks
|
---|
3666 | * out right.
|
---|
3667 | */
|
---|
3668 | VBOXSTRICTRC rcStrict;
|
---|
3669 | RTCPTRUNION uFrame;
|
---|
3670 | uint64_t uNewRip;
|
---|
3671 | uint16_t uNewCs;
|
---|
3672 | uint16_t uNewSs;
|
---|
3673 | uint32_t uNewFlags;
|
---|
3674 | uint64_t uNewRsp;
|
---|
3675 | if (enmEffOpSize == IEMMODE_64BIT)
|
---|
3676 | {
|
---|
3677 | rcStrict = iemMemStackPopBeginSpecial(pVCpu, 5*8, 7, &uFrame.pv, &uNewRsp);
|
---|
3678 | if (rcStrict != VINF_SUCCESS)
|
---|
3679 | return rcStrict;
|
---|
3680 | uNewRip = uFrame.pu64[0];
|
---|
3681 | uNewCs = (uint16_t)uFrame.pu64[1];
|
---|
3682 | uNewFlags = (uint32_t)uFrame.pu64[2];
|
---|
3683 | uNewRsp = uFrame.pu64[3];
|
---|
3684 | uNewSs = (uint16_t)uFrame.pu64[4];
|
---|
3685 | }
|
---|
3686 | else if (enmEffOpSize == IEMMODE_32BIT)
|
---|
3687 | {
|
---|
3688 | rcStrict = iemMemStackPopBeginSpecial(pVCpu, 5*4, 3, &uFrame.pv, &uNewRsp);
|
---|
3689 | if (rcStrict != VINF_SUCCESS)
|
---|
3690 | return rcStrict;
|
---|
3691 | uNewRip = uFrame.pu32[0];
|
---|
3692 | uNewCs = (uint16_t)uFrame.pu32[1];
|
---|
3693 | uNewFlags = uFrame.pu32[2];
|
---|
3694 | uNewRsp = uFrame.pu32[3];
|
---|
3695 | uNewSs = (uint16_t)uFrame.pu32[4];
|
---|
3696 | }
|
---|
3697 | else
|
---|
3698 | {
|
---|
3699 | Assert(enmEffOpSize == IEMMODE_16BIT);
|
---|
3700 | rcStrict = iemMemStackPopBeginSpecial(pVCpu, 5*2, 1, &uFrame.pv, &uNewRsp);
|
---|
3701 | if (rcStrict != VINF_SUCCESS)
|
---|
3702 | return rcStrict;
|
---|
3703 | uNewRip = uFrame.pu16[0];
|
---|
3704 | uNewCs = uFrame.pu16[1];
|
---|
3705 | uNewFlags = uFrame.pu16[2];
|
---|
3706 | uNewRsp = uFrame.pu16[3];
|
---|
3707 | uNewSs = uFrame.pu16[4];
|
---|
3708 | }
|
---|
3709 | rcStrict = iemMemStackPopDoneSpecial(pVCpu, (void *)uFrame.pv); /* don't use iemMemStackPopCommitSpecial here. */
|
---|
3710 | if (RT_LIKELY(rcStrict == VINF_SUCCESS))
|
---|
3711 | { /* extremely like */ }
|
---|
3712 | else
|
---|
3713 | return rcStrict;
|
---|
3714 | Log7(("iretq stack: cs:rip=%04x:%016RX64 rflags=%016RX64 ss:rsp=%04x:%016RX64\n", uNewCs, uNewRip, uNewFlags, uNewSs, uNewRsp));
|
---|
3715 |
|
---|
3716 | /*
|
---|
3717 | * Check stuff.
|
---|
3718 | */
|
---|
3719 | /* Read the CS descriptor. */
|
---|
3720 | if (!(uNewCs & X86_SEL_MASK_OFF_RPL))
|
---|
3721 | {
|
---|
3722 | Log(("iret %04x:%016RX64/%04x:%016RX64 -> invalid CS selector, #GP(0)\n", uNewCs, uNewRip, uNewSs, uNewRsp));
|
---|
3723 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
3724 | }
|
---|
3725 |
|
---|
3726 | IEMSELDESC DescCS;
|
---|
3727 | rcStrict = iemMemFetchSelDesc(pVCpu, &DescCS, uNewCs, X86_XCPT_GP);
|
---|
3728 | if (rcStrict != VINF_SUCCESS)
|
---|
3729 | {
|
---|
3730 | Log(("iret %04x:%016RX64/%04x:%016RX64 - rcStrict=%Rrc when fetching CS\n",
|
---|
3731 | uNewCs, uNewRip, uNewSs, uNewRsp, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
3732 | return rcStrict;
|
---|
3733 | }
|
---|
3734 |
|
---|
3735 | /* Must be a code descriptor. */
|
---|
3736 | if ( !DescCS.Legacy.Gen.u1DescType
|
---|
3737 | || !(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE))
|
---|
3738 | {
|
---|
3739 | Log(("iret %04x:%016RX64/%04x:%016RX64 - CS is not a code segment T=%u T=%#xu -> #GP\n",
|
---|
3740 | uNewCs, uNewRip, uNewSs, uNewRsp, DescCS.Legacy.Gen.u1DescType, DescCS.Legacy.Gen.u4Type));
|
---|
3741 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
3742 | }
|
---|
3743 |
|
---|
3744 | /* Privilege checks. */
|
---|
3745 | uint8_t const uNewCpl = uNewCs & X86_SEL_RPL;
|
---|
3746 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF))
|
---|
3747 | {
|
---|
3748 | if ((uNewCs & X86_SEL_RPL) != DescCS.Legacy.Gen.u2Dpl)
|
---|
3749 | {
|
---|
3750 | Log(("iret %04x:%016RX64 - RPL != DPL (%d) -> #GP\n", uNewCs, uNewRip, DescCS.Legacy.Gen.u2Dpl));
|
---|
3751 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
3752 | }
|
---|
3753 | }
|
---|
3754 | else if ((uNewCs & X86_SEL_RPL) < DescCS.Legacy.Gen.u2Dpl)
|
---|
3755 | {
|
---|
3756 | Log(("iret %04x:%016RX64 - RPL < DPL (%d) -> #GP\n", uNewCs, uNewRip, DescCS.Legacy.Gen.u2Dpl));
|
---|
3757 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
3758 | }
|
---|
3759 | if ((uNewCs & X86_SEL_RPL) < IEM_GET_CPL(pVCpu))
|
---|
3760 | {
|
---|
3761 | Log(("iret %04x:%016RX64 - RPL < CPL (%d) -> #GP\n", uNewCs, uNewRip, IEM_GET_CPL(pVCpu)));
|
---|
3762 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
3763 | }
|
---|
3764 |
|
---|
3765 | /* Present? */
|
---|
3766 | if (!DescCS.Legacy.Gen.u1Present)
|
---|
3767 | {
|
---|
3768 | Log(("iret %04x:%016RX64/%04x:%016RX64 - CS not present -> #NP\n", uNewCs, uNewRip, uNewSs, uNewRsp));
|
---|
3769 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uNewCs);
|
---|
3770 | }
|
---|
3771 |
|
---|
3772 | uint32_t cbLimitCS = X86DESC_LIMIT_G(&DescCS.Legacy);
|
---|
3773 |
|
---|
3774 | /* Read the SS descriptor. */
|
---|
3775 | IEMSELDESC DescSS;
|
---|
3776 | if (!(uNewSs & X86_SEL_MASK_OFF_RPL))
|
---|
3777 | {
|
---|
3778 | if ( !DescCS.Legacy.Gen.u1Long
|
---|
3779 | || DescCS.Legacy.Gen.u1DefBig /** @todo exactly how does iret (and others) behave with u1Long=1 and u1DefBig=1? \#GP(sel)? */
|
---|
3780 | || uNewCpl > 2) /** @todo verify SS=0 impossible for ring-3. */
|
---|
3781 | {
|
---|
3782 | Log(("iret %04x:%016RX64/%04x:%016RX64 -> invalid SS selector, #GP(0)\n", uNewCs, uNewRip, uNewSs, uNewRsp));
|
---|
3783 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
3784 | }
|
---|
3785 | /* Make sure SS is sensible, marked as accessed etc. */
|
---|
3786 | iemMemFakeStackSelDesc(&DescSS, (uNewSs & X86_SEL_RPL));
|
---|
3787 | }
|
---|
3788 | else
|
---|
3789 | {
|
---|
3790 | rcStrict = iemMemFetchSelDesc(pVCpu, &DescSS, uNewSs, X86_XCPT_GP); /** @todo Correct exception? */
|
---|
3791 | if (rcStrict != VINF_SUCCESS)
|
---|
3792 | {
|
---|
3793 | Log(("iret %04x:%016RX64/%04x:%016RX64 - %Rrc when fetching SS\n",
|
---|
3794 | uNewCs, uNewRip, uNewSs, uNewRsp, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
3795 | return rcStrict;
|
---|
3796 | }
|
---|
3797 | }
|
---|
3798 |
|
---|
3799 | /* Privilege checks. */
|
---|
3800 | if ((uNewSs & X86_SEL_RPL) != (uNewCs & X86_SEL_RPL))
|
---|
3801 | {
|
---|
3802 | Log(("iret %04x:%016RX64/%04x:%016RX64 -> SS.RPL != CS.RPL -> #GP\n", uNewCs, uNewRip, uNewSs, uNewRsp));
|
---|
3803 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSs);
|
---|
3804 | }
|
---|
3805 |
|
---|
3806 | uint32_t cbLimitSs;
|
---|
3807 | if (!(uNewSs & X86_SEL_MASK_OFF_RPL))
|
---|
3808 | cbLimitSs = UINT32_MAX;
|
---|
3809 | else
|
---|
3810 | {
|
---|
3811 | if (DescSS.Legacy.Gen.u2Dpl != (uNewCs & X86_SEL_RPL))
|
---|
3812 | {
|
---|
3813 | Log(("iret %04x:%016RX64/%04x:%016RX64 -> SS.DPL (%d) != CS.RPL -> #GP\n",
|
---|
3814 | uNewCs, uNewRip, uNewSs, uNewRsp, DescSS.Legacy.Gen.u2Dpl));
|
---|
3815 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSs);
|
---|
3816 | }
|
---|
3817 |
|
---|
3818 | /* Must be a writeable data segment descriptor. */
|
---|
3819 | if (!DescSS.Legacy.Gen.u1DescType)
|
---|
3820 | {
|
---|
3821 | Log(("iret %04x:%016RX64/%04x:%016RX64 -> SS is system segment (%#x) -> #GP\n",
|
---|
3822 | uNewCs, uNewRip, uNewSs, uNewRsp, DescSS.Legacy.Gen.u4Type));
|
---|
3823 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSs);
|
---|
3824 | }
|
---|
3825 | if ((DescSS.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_WRITE)) != X86_SEL_TYPE_WRITE)
|
---|
3826 | {
|
---|
3827 | Log(("iret %04x:%016RX64/%04x:%016RX64 - not writable data segment (%#x) -> #GP\n",
|
---|
3828 | uNewCs, uNewRip, uNewSs, uNewRsp, DescSS.Legacy.Gen.u4Type));
|
---|
3829 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSs);
|
---|
3830 | }
|
---|
3831 |
|
---|
3832 | /* Present? */
|
---|
3833 | if (!DescSS.Legacy.Gen.u1Present)
|
---|
3834 | {
|
---|
3835 | Log(("iret %04x:%016RX64/%04x:%016RX64 -> SS not present -> #SS\n", uNewCs, uNewRip, uNewSs, uNewRsp));
|
---|
3836 | return iemRaiseStackSelectorNotPresentBySelector(pVCpu, uNewSs);
|
---|
3837 | }
|
---|
3838 | cbLimitSs = X86DESC_LIMIT_G(&DescSS.Legacy);
|
---|
3839 | }
|
---|
3840 |
|
---|
3841 | /* Check EIP. */
|
---|
3842 | if (DescCS.Legacy.Gen.u1Long)
|
---|
3843 | {
|
---|
3844 | if (!IEM_IS_CANONICAL(uNewRip))
|
---|
3845 | {
|
---|
3846 | Log(("iret %04x:%016RX64/%04x:%016RX64 -> RIP is not canonical -> #GP(0)\n",
|
---|
3847 | uNewCs, uNewRip, uNewSs, uNewRsp));
|
---|
3848 | return iemRaiseNotCanonical(pVCpu);
|
---|
3849 | }
|
---|
3850 | /** @todo check the location of this... Testcase. */
|
---|
3851 | if (RT_LIKELY(!DescCS.Legacy.Gen.u1DefBig))
|
---|
3852 | { /* likely */ }
|
---|
3853 | else
|
---|
3854 | {
|
---|
3855 | Log(("iret %04x:%016RX64/%04x:%016RX64 -> both L and D are set -> #GP(0)\n", uNewCs, uNewRip, uNewSs, uNewRsp));
|
---|
3856 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
3857 | }
|
---|
3858 | }
|
---|
3859 | else
|
---|
3860 | {
|
---|
3861 | if (uNewRip > cbLimitCS)
|
---|
3862 | {
|
---|
3863 | Log(("iret %04x:%016RX64/%04x:%016RX64 -> EIP is out of bounds (%#x) -> #GP(0)\n",
|
---|
3864 | uNewCs, uNewRip, uNewSs, uNewRsp, cbLimitCS));
|
---|
3865 | /** @todo Which is it, \#GP(0) or \#GP(sel)? */
|
---|
3866 | return iemRaiseSelectorBoundsBySelector(pVCpu, uNewCs);
|
---|
3867 | }
|
---|
3868 | }
|
---|
3869 |
|
---|
3870 | /*
|
---|
3871 | * Commit the changes, marking CS and SS accessed first since
|
---|
3872 | * that may fail.
|
---|
3873 | */
|
---|
3874 | /** @todo where exactly are these actually marked accessed by a real CPU? */
|
---|
3875 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
3876 | {
|
---|
3877 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewCs);
|
---|
3878 | if (rcStrict != VINF_SUCCESS)
|
---|
3879 | return rcStrict;
|
---|
3880 | DescCS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
3881 | }
|
---|
3882 | if (!(DescSS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
3883 | {
|
---|
3884 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewSs);
|
---|
3885 | if (rcStrict != VINF_SUCCESS)
|
---|
3886 | return rcStrict;
|
---|
3887 | DescSS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
3888 | }
|
---|
3889 |
|
---|
3890 | uint32_t fEFlagsMask = X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF
|
---|
3891 | | X86_EFL_TF | X86_EFL_DF | X86_EFL_OF | X86_EFL_NT;
|
---|
3892 | if (enmEffOpSize != IEMMODE_16BIT)
|
---|
3893 | fEFlagsMask |= X86_EFL_RF | X86_EFL_AC | X86_EFL_ID;
|
---|
3894 | if (IEM_GET_CPL(pVCpu) == 0)
|
---|
3895 | fEFlagsMask |= X86_EFL_IF | X86_EFL_IOPL | X86_EFL_VIF | X86_EFL_VIP; /* VM is ignored */
|
---|
3896 | else if (IEM_GET_CPL(pVCpu) <= pVCpu->cpum.GstCtx.eflags.Bits.u2IOPL)
|
---|
3897 | fEFlagsMask |= X86_EFL_IF;
|
---|
3898 | uint32_t fEFlagsNew = IEMMISC_GET_EFL(pVCpu);
|
---|
3899 | fEFlagsNew &= ~fEFlagsMask;
|
---|
3900 | fEFlagsNew |= uNewFlags & fEFlagsMask;
|
---|
3901 | #ifdef DBGFTRACE_ENABLED
|
---|
3902 | RTTraceBufAddMsgF(pVCpu->CTX_SUFF(pVM)->CTX_SUFF(hTraceBuf), "iret/%ul%u %08llx -> %04x:%04llx %llx %04x:%04llx",
|
---|
3903 | IEM_GET_CPL(pVCpu), uNewCpl, pVCpu->cpum.GstCtx.rip, uNewCs, uNewRip, uNewFlags, uNewSs, uNewRsp);
|
---|
3904 | #endif
|
---|
3905 |
|
---|
3906 | IEMMISC_SET_EFL(pVCpu, fEFlagsNew);
|
---|
3907 | pVCpu->cpum.GstCtx.rip = uNewRip;
|
---|
3908 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
|
---|
3909 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
|
---|
3910 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
3911 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCS.Legacy);
|
---|
3912 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimitCS;
|
---|
3913 | pVCpu->cpum.GstCtx.cs.u64Base = X86DESC_BASE(&DescCS.Legacy);
|
---|
3914 | if (pVCpu->cpum.GstCtx.cs.Attr.n.u1Long || pVCpu->cpum.GstCtx.cs.Attr.n.u1DefBig)
|
---|
3915 | pVCpu->cpum.GstCtx.rsp = uNewRsp;
|
---|
3916 | else
|
---|
3917 | pVCpu->cpum.GstCtx.sp = (uint16_t)uNewRsp;
|
---|
3918 | pVCpu->cpum.GstCtx.ss.Sel = uNewSs;
|
---|
3919 | pVCpu->cpum.GstCtx.ss.ValidSel = uNewSs;
|
---|
3920 | if (!(uNewSs & X86_SEL_MASK_OFF_RPL))
|
---|
3921 | {
|
---|
3922 | pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
3923 | pVCpu->cpum.GstCtx.ss.Attr.u = X86DESCATTR_UNUSABLE | (uNewCpl << X86DESCATTR_DPL_SHIFT);
|
---|
3924 | pVCpu->cpum.GstCtx.ss.u32Limit = UINT32_MAX;
|
---|
3925 | pVCpu->cpum.GstCtx.ss.u64Base = 0;
|
---|
3926 | Log2(("iretq new SS: NULL\n"));
|
---|
3927 | }
|
---|
3928 | else
|
---|
3929 | {
|
---|
3930 | pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
3931 | pVCpu->cpum.GstCtx.ss.Attr.u = X86DESC_GET_HID_ATTR(&DescSS.Legacy);
|
---|
3932 | pVCpu->cpum.GstCtx.ss.u32Limit = cbLimitSs;
|
---|
3933 | pVCpu->cpum.GstCtx.ss.u64Base = X86DESC_BASE(&DescSS.Legacy);
|
---|
3934 | 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));
|
---|
3935 | }
|
---|
3936 |
|
---|
3937 | if (IEM_GET_CPL(pVCpu) != uNewCpl)
|
---|
3938 | {
|
---|
3939 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCpl, &pVCpu->cpum.GstCtx.ds);
|
---|
3940 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCpl, &pVCpu->cpum.GstCtx.es);
|
---|
3941 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCpl, &pVCpu->cpum.GstCtx.fs);
|
---|
3942 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCpl, &pVCpu->cpum.GstCtx.gs);
|
---|
3943 | }
|
---|
3944 |
|
---|
3945 | iemRecalcExecModeAndCplFlags(pVCpu);
|
---|
3946 |
|
---|
3947 | /* Flush the prefetch buffer. */
|
---|
3948 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr); /** @todo may light flush if the ring + mode doesn't change */
|
---|
3949 |
|
---|
3950 | /** @todo single stepping */
|
---|
3951 | return VINF_SUCCESS;
|
---|
3952 | }
|
---|
3953 |
|
---|
3954 |
|
---|
3955 | /**
|
---|
3956 | * Implements iret.
|
---|
3957 | *
|
---|
3958 | * @param enmEffOpSize The effective operand size.
|
---|
3959 | */
|
---|
3960 | IEM_CIMPL_DEF_1(iemCImpl_iret, IEMMODE, enmEffOpSize)
|
---|
3961 | {
|
---|
3962 | bool fBlockingNmi = CPUMAreInterruptsInhibitedByNmi(&pVCpu->cpum.GstCtx);
|
---|
3963 |
|
---|
3964 | if (!IEM_IS_IN_GUEST(pVCpu))
|
---|
3965 | { /* probable */ }
|
---|
3966 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
3967 | else if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
3968 | {
|
---|
3969 | /*
|
---|
3970 | * Record whether NMI (or virtual-NMI) blocking is in effect during the execution
|
---|
3971 | * of this IRET instruction. We need to provide this information as part of some
|
---|
3972 | * VM-exits.
|
---|
3973 | *
|
---|
3974 | * See Intel spec. 27.2.2 "Information for VM Exits Due to Vectored Events".
|
---|
3975 | */
|
---|
3976 | if (IEM_VMX_IS_PINCTLS_SET(pVCpu, VMX_PIN_CTLS_VIRT_NMI))
|
---|
3977 | pVCpu->cpum.GstCtx.hwvirt.vmx.fNmiUnblockingIret = pVCpu->cpum.GstCtx.hwvirt.vmx.fVirtNmiBlocking;
|
---|
3978 | else
|
---|
3979 | pVCpu->cpum.GstCtx.hwvirt.vmx.fNmiUnblockingIret = fBlockingNmi;
|
---|
3980 |
|
---|
3981 | /*
|
---|
3982 | * If "NMI exiting" is set, IRET does not affect blocking of NMIs.
|
---|
3983 | * See Intel Spec. 25.3 "Changes To Instruction Behavior In VMX Non-root Operation".
|
---|
3984 | */
|
---|
3985 | if (IEM_VMX_IS_PINCTLS_SET(pVCpu, VMX_PIN_CTLS_NMI_EXIT))
|
---|
3986 | fBlockingNmi = false;
|
---|
3987 |
|
---|
3988 | /* Clear virtual-NMI blocking, if any, before causing any further exceptions. */
|
---|
3989 | pVCpu->cpum.GstCtx.hwvirt.vmx.fVirtNmiBlocking = false;
|
---|
3990 | }
|
---|
3991 | #endif
|
---|
3992 | /*
|
---|
3993 | * The SVM nested-guest intercept for IRET takes priority over all exceptions,
|
---|
3994 | * The NMI is still held pending (which I assume means blocking of further NMIs
|
---|
3995 | * is in effect).
|
---|
3996 | *
|
---|
3997 | * See AMD spec. 15.9 "Instruction Intercepts".
|
---|
3998 | * See AMD spec. 15.21.9 "NMI Support".
|
---|
3999 | */
|
---|
4000 | else if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_IRET))
|
---|
4001 | {
|
---|
4002 | Log(("iret: Guest intercept -> #VMEXIT\n"));
|
---|
4003 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
4004 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_IRET, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
4005 | }
|
---|
4006 |
|
---|
4007 | /*
|
---|
4008 | * Clear NMI blocking, if any, before causing any further exceptions.
|
---|
4009 | * See Intel spec. 6.7.1 "Handling Multiple NMIs".
|
---|
4010 | */
|
---|
4011 | if (fBlockingNmi)
|
---|
4012 | CPUMClearInterruptInhibitingByNmi(&pVCpu->cpum.GstCtx);
|
---|
4013 |
|
---|
4014 | /*
|
---|
4015 | * Call a mode specific worker.
|
---|
4016 | */
|
---|
4017 | VBOXSTRICTRC rcStrict;
|
---|
4018 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
4019 | rcStrict = IEM_CIMPL_CALL_1(iemCImpl_iret_real_v8086, enmEffOpSize);
|
---|
4020 | else
|
---|
4021 | {
|
---|
4022 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SREG_MASK | CPUMCTX_EXTRN_GDTR | CPUMCTX_EXTRN_LDTR);
|
---|
4023 | if (IEM_IS_64BIT_CODE(pVCpu))
|
---|
4024 | rcStrict = IEM_CIMPL_CALL_1(iemCImpl_iret_64bit, enmEffOpSize);
|
---|
4025 | else
|
---|
4026 | rcStrict = IEM_CIMPL_CALL_1(iemCImpl_iret_prot, enmEffOpSize);
|
---|
4027 | }
|
---|
4028 |
|
---|
4029 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
4030 | /*
|
---|
4031 | * Clear NMI unblocking IRET state with the completion of IRET.
|
---|
4032 | */
|
---|
4033 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
4034 | pVCpu->cpum.GstCtx.hwvirt.vmx.fNmiUnblockingIret = false;
|
---|
4035 | #endif
|
---|
4036 | return rcStrict;
|
---|
4037 | }
|
---|
4038 |
|
---|
4039 |
|
---|
4040 | static void iemLoadallSetSelector(PVMCPUCC pVCpu, uint8_t iSegReg, uint16_t uSel)
|
---|
4041 | {
|
---|
4042 | PCPUMSELREGHID pHid = iemSRegGetHid(pVCpu, iSegReg);
|
---|
4043 |
|
---|
4044 | pHid->Sel = uSel;
|
---|
4045 | pHid->ValidSel = uSel;
|
---|
4046 | pHid->fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4047 | }
|
---|
4048 |
|
---|
4049 |
|
---|
4050 | static void iemLoadall286SetDescCache(PVMCPUCC pVCpu, uint8_t iSegReg, uint8_t const *pbMem)
|
---|
4051 | {
|
---|
4052 | PCPUMSELREGHID pHid = iemSRegGetHid(pVCpu, iSegReg);
|
---|
4053 |
|
---|
4054 | /* The base is in the first three bytes. */
|
---|
4055 | pHid->u64Base = pbMem[0] + (pbMem[1] << 8) + (pbMem[2] << 16);
|
---|
4056 | /* The attributes are in the fourth byte. */
|
---|
4057 | pHid->Attr.u = pbMem[3];
|
---|
4058 | pHid->Attr.u &= ~(X86DESCATTR_L | X86DESCATTR_D); /* (just to be on the safe side) */
|
---|
4059 | /* The limit is in the last two bytes. */
|
---|
4060 | pHid->u32Limit = pbMem[4] + (pbMem[5] << 8);
|
---|
4061 | }
|
---|
4062 |
|
---|
4063 |
|
---|
4064 | /**
|
---|
4065 | * Implements 286 LOADALL (286 CPUs only).
|
---|
4066 | */
|
---|
4067 | IEM_CIMPL_DEF_0(iemCImpl_loadall286)
|
---|
4068 | {
|
---|
4069 | NOREF(cbInstr);
|
---|
4070 |
|
---|
4071 | /* Data is loaded from a buffer at 800h. No checks are done on the
|
---|
4072 | * validity of loaded state.
|
---|
4073 | *
|
---|
4074 | * LOADALL only loads the internal CPU state, it does not access any
|
---|
4075 | * GDT, LDT, or similar tables.
|
---|
4076 | */
|
---|
4077 |
|
---|
4078 | if (IEM_GET_CPL(pVCpu) != 0)
|
---|
4079 | {
|
---|
4080 | Log(("loadall286: CPL must be 0 not %u -> #GP(0)\n", IEM_GET_CPL(pVCpu)));
|
---|
4081 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4082 | }
|
---|
4083 |
|
---|
4084 | uint8_t const *pbMem = NULL;
|
---|
4085 | uint16_t const *pa16Mem;
|
---|
4086 | uint8_t const *pa8Mem;
|
---|
4087 | RTGCPHYS GCPtrStart = 0x800; /* Fixed table location. */
|
---|
4088 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, (void **)&pbMem, 0x66, UINT8_MAX, GCPtrStart, IEM_ACCESS_SYS_R, 0);
|
---|
4089 | if (rcStrict != VINF_SUCCESS)
|
---|
4090 | return rcStrict;
|
---|
4091 |
|
---|
4092 | /* The MSW is at offset 0x06. */
|
---|
4093 | pa16Mem = (uint16_t const *)(pbMem + 0x06);
|
---|
4094 | /* Even LOADALL can't clear the MSW.PE bit, though it can set it. */
|
---|
4095 | uint64_t uNewCr0 = pVCpu->cpum.GstCtx.cr0 & ~(X86_CR0_MP | X86_CR0_EM | X86_CR0_TS);
|
---|
4096 | uNewCr0 |= *pa16Mem & (X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS);
|
---|
4097 | uint64_t const uOldCr0 = pVCpu->cpum.GstCtx.cr0;
|
---|
4098 |
|
---|
4099 | CPUMSetGuestCR0(pVCpu, uNewCr0);
|
---|
4100 | Assert(pVCpu->cpum.GstCtx.cr0 == uNewCr0);
|
---|
4101 |
|
---|
4102 | /* Inform PGM if mode changed. */
|
---|
4103 | if ((uNewCr0 & X86_CR0_PE) != (uOldCr0 & X86_CR0_PE))
|
---|
4104 | {
|
---|
4105 | int rc = PGMFlushTLB(pVCpu, pVCpu->cpum.GstCtx.cr3, true /* global */);
|
---|
4106 | AssertRCReturn(rc, rc);
|
---|
4107 | /* ignore informational status codes */
|
---|
4108 | }
|
---|
4109 | rcStrict = PGMChangeMode(pVCpu, pVCpu->cpum.GstCtx.cr0, pVCpu->cpum.GstCtx.cr4, pVCpu->cpum.GstCtx.msrEFER,
|
---|
4110 | false /* fForce */);
|
---|
4111 |
|
---|
4112 | /* TR selector is at offset 0x16. */
|
---|
4113 | pa16Mem = (uint16_t const *)(pbMem + 0x16);
|
---|
4114 | pVCpu->cpum.GstCtx.tr.Sel = pa16Mem[0];
|
---|
4115 | pVCpu->cpum.GstCtx.tr.ValidSel = pa16Mem[0];
|
---|
4116 | pVCpu->cpum.GstCtx.tr.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4117 |
|
---|
4118 | /* Followed by FLAGS... */
|
---|
4119 | pVCpu->cpum.GstCtx.eflags.u = pa16Mem[1] | X86_EFL_1;
|
---|
4120 | pVCpu->cpum.GstCtx.ip = pa16Mem[2]; /* ...and IP. */
|
---|
4121 |
|
---|
4122 | /* LDT is at offset 0x1C. */
|
---|
4123 | pa16Mem = (uint16_t const *)(pbMem + 0x1C);
|
---|
4124 | pVCpu->cpum.GstCtx.ldtr.Sel = pa16Mem[0];
|
---|
4125 | pVCpu->cpum.GstCtx.ldtr.ValidSel = pa16Mem[0];
|
---|
4126 | pVCpu->cpum.GstCtx.ldtr.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4127 |
|
---|
4128 | /* Segment registers are at offset 0x1E. */
|
---|
4129 | pa16Mem = (uint16_t const *)(pbMem + 0x1E);
|
---|
4130 | iemLoadallSetSelector(pVCpu, X86_SREG_DS, pa16Mem[0]);
|
---|
4131 | iemLoadallSetSelector(pVCpu, X86_SREG_SS, pa16Mem[1]);
|
---|
4132 | iemLoadallSetSelector(pVCpu, X86_SREG_CS, pa16Mem[2]);
|
---|
4133 | iemLoadallSetSelector(pVCpu, X86_SREG_ES, pa16Mem[3]);
|
---|
4134 |
|
---|
4135 | /* GPRs are at offset 0x26. */
|
---|
4136 | pa16Mem = (uint16_t const *)(pbMem + 0x26);
|
---|
4137 | pVCpu->cpum.GstCtx.di = pa16Mem[0];
|
---|
4138 | pVCpu->cpum.GstCtx.si = pa16Mem[1];
|
---|
4139 | pVCpu->cpum.GstCtx.bp = pa16Mem[2];
|
---|
4140 | pVCpu->cpum.GstCtx.sp = pa16Mem[3];
|
---|
4141 | pVCpu->cpum.GstCtx.bx = pa16Mem[4];
|
---|
4142 | pVCpu->cpum.GstCtx.dx = pa16Mem[5];
|
---|
4143 | pVCpu->cpum.GstCtx.cx = pa16Mem[6];
|
---|
4144 | pVCpu->cpum.GstCtx.ax = pa16Mem[7];
|
---|
4145 |
|
---|
4146 | /* Descriptor caches are at offset 0x36, 6 bytes per entry. */
|
---|
4147 | iemLoadall286SetDescCache(pVCpu, X86_SREG_ES, pbMem + 0x36);
|
---|
4148 | iemLoadall286SetDescCache(pVCpu, X86_SREG_CS, pbMem + 0x3C);
|
---|
4149 | iemLoadall286SetDescCache(pVCpu, X86_SREG_SS, pbMem + 0x42);
|
---|
4150 | iemLoadall286SetDescCache(pVCpu, X86_SREG_DS, pbMem + 0x48);
|
---|
4151 |
|
---|
4152 | /* GDTR contents are at offset 0x4E, 6 bytes. */
|
---|
4153 | RTGCPHYS GCPtrBase;
|
---|
4154 | uint16_t cbLimit;
|
---|
4155 | pa8Mem = pbMem + 0x4E;
|
---|
4156 | /* NB: Fourth byte "should be zero"; we are ignoring it. */
|
---|
4157 | GCPtrBase = pa8Mem[0] + (pa8Mem[1] << 8) + (pa8Mem[2] << 16);
|
---|
4158 | cbLimit = pa8Mem[4] + (pa8Mem[5] << 8);
|
---|
4159 | CPUMSetGuestGDTR(pVCpu, GCPtrBase, cbLimit);
|
---|
4160 |
|
---|
4161 | /* IDTR contents are at offset 0x5A, 6 bytes. */
|
---|
4162 | pa8Mem = pbMem + 0x5A;
|
---|
4163 | GCPtrBase = pa8Mem[0] + (pa8Mem[1] << 8) + (pa8Mem[2] << 16);
|
---|
4164 | cbLimit = pa8Mem[4] + (pa8Mem[5] << 8);
|
---|
4165 | CPUMSetGuestIDTR(pVCpu, GCPtrBase, cbLimit);
|
---|
4166 |
|
---|
4167 | 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));
|
---|
4168 | 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));
|
---|
4169 | 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));
|
---|
4170 | 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));
|
---|
4171 | 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));
|
---|
4172 | 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));
|
---|
4173 |
|
---|
4174 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)pbMem, IEM_ACCESS_SYS_R);
|
---|
4175 | if (rcStrict != VINF_SUCCESS)
|
---|
4176 | return rcStrict;
|
---|
4177 |
|
---|
4178 | /*
|
---|
4179 | * The CPL may change and protected mode may change enabled. It is taken
|
---|
4180 | * from the "DPL fields of the SS and CS descriptor caches" but there is no
|
---|
4181 | * word as to what happens if those are not identical (probably bad things).
|
---|
4182 | */
|
---|
4183 | iemRecalcExecModeAndCplFlags(pVCpu);
|
---|
4184 | Assert(IEM_IS_16BIT_CODE(pVCpu));
|
---|
4185 |
|
---|
4186 | CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_HIDDEN_SEL_REGS | CPUM_CHANGED_IDTR | CPUM_CHANGED_GDTR | CPUM_CHANGED_TR | CPUM_CHANGED_LDTR);
|
---|
4187 |
|
---|
4188 | /* Flush the prefetch buffer. */
|
---|
4189 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr);
|
---|
4190 |
|
---|
4191 | /** @todo single stepping */
|
---|
4192 | return rcStrict;
|
---|
4193 | }
|
---|
4194 |
|
---|
4195 |
|
---|
4196 | /**
|
---|
4197 | * Implements SYSCALL (AMD and Intel64).
|
---|
4198 | */
|
---|
4199 | IEM_CIMPL_DEF_0(iemCImpl_syscall)
|
---|
4200 | {
|
---|
4201 | /** @todo hack, LOADALL should be decoded as such on a 286. */
|
---|
4202 | if (RT_UNLIKELY(pVCpu->iem.s.uTargetCpu == IEMTARGETCPU_286))
|
---|
4203 | return iemCImpl_loadall286(pVCpu, cbInstr);
|
---|
4204 |
|
---|
4205 | /*
|
---|
4206 | * Check preconditions.
|
---|
4207 | *
|
---|
4208 | * Note that CPUs described in the documentation may load a few odd values
|
---|
4209 | * into CS and SS than we allow here. This has yet to be checked on real
|
---|
4210 | * hardware.
|
---|
4211 | */
|
---|
4212 | if (!(pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_SCE))
|
---|
4213 | {
|
---|
4214 | Log(("syscall: Not enabled in EFER -> #UD\n"));
|
---|
4215 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4216 | }
|
---|
4217 | if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE))
|
---|
4218 | {
|
---|
4219 | Log(("syscall: Protected mode is required -> #GP(0)\n"));
|
---|
4220 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4221 | }
|
---|
4222 | if (IEM_IS_GUEST_CPU_INTEL(pVCpu) && !CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu)))
|
---|
4223 | {
|
---|
4224 | Log(("syscall: Only available in long mode on intel -> #UD\n"));
|
---|
4225 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4226 | }
|
---|
4227 |
|
---|
4228 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SYSCALL_MSRS);
|
---|
4229 |
|
---|
4230 | /** @todo verify RPL ignoring and CS=0xfff8 (i.e. SS == 0). */
|
---|
4231 | /** @todo what about LDT selectors? Shouldn't matter, really. */
|
---|
4232 | uint16_t uNewCs = (pVCpu->cpum.GstCtx.msrSTAR >> MSR_K6_STAR_SYSCALL_CS_SS_SHIFT) & X86_SEL_MASK_OFF_RPL;
|
---|
4233 | uint16_t uNewSs = uNewCs + 8;
|
---|
4234 | if (uNewCs == 0 || uNewSs == 0)
|
---|
4235 | {
|
---|
4236 | /** @todo Neither Intel nor AMD document this check. */
|
---|
4237 | Log(("syscall: msrSTAR.CS = 0 or SS = 0 -> #GP(0)\n"));
|
---|
4238 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4239 | }
|
---|
4240 |
|
---|
4241 | /* Long mode and legacy mode differs. */
|
---|
4242 | if (CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu)))
|
---|
4243 | {
|
---|
4244 | uint64_t uNewRip = IEM_IS_64BIT_CODE(pVCpu) ? pVCpu->cpum.GstCtx.msrLSTAR : pVCpu->cpum.GstCtx. msrCSTAR;
|
---|
4245 |
|
---|
4246 | /* This test isn't in the docs, but I'm not trusting the guys writing
|
---|
4247 | the MSRs to have validated the values as canonical like they should. */
|
---|
4248 | if (!IEM_IS_CANONICAL(uNewRip))
|
---|
4249 | {
|
---|
4250 | /** @todo Intel claims this can't happen because IA32_LSTAR MSR can't be written with non-canonical address. */
|
---|
4251 | Log(("syscall: New RIP not canonical -> #UD\n"));
|
---|
4252 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4253 | }
|
---|
4254 |
|
---|
4255 | /*
|
---|
4256 | * Commit it.
|
---|
4257 | */
|
---|
4258 | 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));
|
---|
4259 | pVCpu->cpum.GstCtx.rcx = pVCpu->cpum.GstCtx.rip + cbInstr;
|
---|
4260 | pVCpu->cpum.GstCtx.rip = uNewRip;
|
---|
4261 |
|
---|
4262 | pVCpu->cpum.GstCtx.rflags.u &= ~X86_EFL_RF;
|
---|
4263 | pVCpu->cpum.GstCtx.r11 = pVCpu->cpum.GstCtx.rflags.u;
|
---|
4264 | pVCpu->cpum.GstCtx.rflags.u &= ~pVCpu->cpum.GstCtx.msrSFMASK;
|
---|
4265 | pVCpu->cpum.GstCtx.rflags.u |= X86_EFL_RA1_MASK;
|
---|
4266 |
|
---|
4267 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_L | X86DESCATTR_DT | X86_SEL_TYPE_ER_ACC;
|
---|
4268 | pVCpu->cpum.GstCtx.ss.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_D | X86DESCATTR_DT | X86_SEL_TYPE_RW_ACC;
|
---|
4269 |
|
---|
4270 | pVCpu->iem.s.fExec = (pVCpu->iem.s.fExec & ~(IEM_F_MODE_MASK | IEM_F_X86_CPL_MASK))
|
---|
4271 | | IEM_F_MODE_X86_64BIT;
|
---|
4272 | }
|
---|
4273 | else
|
---|
4274 | {
|
---|
4275 | /*
|
---|
4276 | * Commit it.
|
---|
4277 | */
|
---|
4278 | 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)));
|
---|
4279 | pVCpu->cpum.GstCtx.rcx = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
4280 | pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.msrSTAR & MSR_K6_STAR_SYSCALL_EIP_MASK;
|
---|
4281 | pVCpu->cpum.GstCtx.rflags.u &= ~(X86_EFL_VM | X86_EFL_IF | X86_EFL_RF);
|
---|
4282 |
|
---|
4283 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_D | X86DESCATTR_DT | X86_SEL_TYPE_ER_ACC;
|
---|
4284 | pVCpu->cpum.GstCtx.ss.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_D | X86DESCATTR_DT | X86_SEL_TYPE_RW_ACC;
|
---|
4285 |
|
---|
4286 | pVCpu->iem.s.fExec = (pVCpu->iem.s.fExec & ~(IEM_F_MODE_MASK | IEM_F_X86_CPL_MASK))
|
---|
4287 | | IEM_F_MODE_X86_32BIT_PROT
|
---|
4288 | | iemCalc32BitFlatIndicatorEsDs(pVCpu);
|
---|
4289 | }
|
---|
4290 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
|
---|
4291 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
|
---|
4292 | pVCpu->cpum.GstCtx.cs.u64Base = 0;
|
---|
4293 | pVCpu->cpum.GstCtx.cs.u32Limit = UINT32_MAX;
|
---|
4294 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4295 |
|
---|
4296 | pVCpu->cpum.GstCtx.ss.Sel = uNewSs;
|
---|
4297 | pVCpu->cpum.GstCtx.ss.ValidSel = uNewSs;
|
---|
4298 | pVCpu->cpum.GstCtx.ss.u64Base = 0;
|
---|
4299 | pVCpu->cpum.GstCtx.ss.u32Limit = UINT32_MAX;
|
---|
4300 | pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4301 |
|
---|
4302 | /* Flush the prefetch buffer. */
|
---|
4303 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr);
|
---|
4304 |
|
---|
4305 | /** @todo single step */
|
---|
4306 | return VINF_SUCCESS;
|
---|
4307 | }
|
---|
4308 |
|
---|
4309 |
|
---|
4310 | /**
|
---|
4311 | * Implements SYSRET (AMD and Intel64).
|
---|
4312 | *
|
---|
4313 | * @param enmEffOpSize The effective operand size.
|
---|
4314 | */
|
---|
4315 | IEM_CIMPL_DEF_1(iemCImpl_sysret, IEMMODE, enmEffOpSize)
|
---|
4316 |
|
---|
4317 | {
|
---|
4318 | RT_NOREF_PV(cbInstr);
|
---|
4319 |
|
---|
4320 | /*
|
---|
4321 | * Check preconditions.
|
---|
4322 | *
|
---|
4323 | * Note that CPUs described in the documentation may load a few odd values
|
---|
4324 | * into CS and SS than we allow here. This has yet to be checked on real
|
---|
4325 | * hardware.
|
---|
4326 | */
|
---|
4327 | if (!(pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_SCE))
|
---|
4328 | {
|
---|
4329 | Log(("sysret: Not enabled in EFER -> #UD\n"));
|
---|
4330 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4331 | }
|
---|
4332 | if (IEM_IS_GUEST_CPU_INTEL(pVCpu) && !CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu)))
|
---|
4333 | {
|
---|
4334 | Log(("sysret: Only available in long mode on intel -> #UD\n"));
|
---|
4335 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4336 | }
|
---|
4337 | if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE))
|
---|
4338 | {
|
---|
4339 | Log(("sysret: Protected mode is required -> #GP(0)\n"));
|
---|
4340 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4341 | }
|
---|
4342 | if (IEM_GET_CPL(pVCpu) != 0)
|
---|
4343 | {
|
---|
4344 | Log(("sysret: CPL must be 0 not %u -> #GP(0)\n", IEM_GET_CPL(pVCpu)));
|
---|
4345 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4346 | }
|
---|
4347 |
|
---|
4348 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SYSCALL_MSRS);
|
---|
4349 |
|
---|
4350 | /** @todo Does SYSRET verify CS != 0 and SS != 0? Neither is valid in ring-3. */
|
---|
4351 | uint16_t uNewCs = (pVCpu->cpum.GstCtx.msrSTAR >> MSR_K6_STAR_SYSRET_CS_SS_SHIFT) & X86_SEL_MASK_OFF_RPL;
|
---|
4352 | uint16_t uNewSs = uNewCs + 8;
|
---|
4353 | if (enmEffOpSize == IEMMODE_64BIT)
|
---|
4354 | uNewCs += 16;
|
---|
4355 | if (uNewCs == 0 || uNewSs == 0)
|
---|
4356 | {
|
---|
4357 | Log(("sysret: msrSTAR.CS = 0 or SS = 0 -> #GP(0)\n"));
|
---|
4358 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4359 | }
|
---|
4360 |
|
---|
4361 | /*
|
---|
4362 | * Commit it.
|
---|
4363 | */
|
---|
4364 | bool f32Bit = true;
|
---|
4365 | if (CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu)))
|
---|
4366 | {
|
---|
4367 | if (enmEffOpSize == IEMMODE_64BIT)
|
---|
4368 | {
|
---|
4369 | 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));
|
---|
4370 | /* Note! We disregard intel manual regarding the RCX canonical
|
---|
4371 | check, ask intel+xen why AMD doesn't do it. */
|
---|
4372 | pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.rcx;
|
---|
4373 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_L | X86DESCATTR_DT | X86_SEL_TYPE_ER_ACC
|
---|
4374 | | (3 << X86DESCATTR_DPL_SHIFT);
|
---|
4375 | f32Bit = false;
|
---|
4376 | }
|
---|
4377 | else
|
---|
4378 | {
|
---|
4379 | 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));
|
---|
4380 | pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.ecx;
|
---|
4381 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_D | X86DESCATTR_DT | X86_SEL_TYPE_ER_ACC
|
---|
4382 | | (3 << X86DESCATTR_DPL_SHIFT);
|
---|
4383 | }
|
---|
4384 | /** @todo testcase: See what kind of flags we can make SYSRET restore and
|
---|
4385 | * what it really ignores. RF and VM are hinted at being zero, by AMD.
|
---|
4386 | * Intel says: RFLAGS := (R11 & 3C7FD7H) | 2; */
|
---|
4387 | pVCpu->cpum.GstCtx.rflags.u = pVCpu->cpum.GstCtx.r11 & (X86_EFL_POPF_BITS | X86_EFL_VIF | X86_EFL_VIP);
|
---|
4388 | pVCpu->cpum.GstCtx.rflags.u |= X86_EFL_RA1_MASK;
|
---|
4389 | }
|
---|
4390 | else
|
---|
4391 | {
|
---|
4392 | 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));
|
---|
4393 | pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.rcx;
|
---|
4394 | pVCpu->cpum.GstCtx.rflags.u |= X86_EFL_IF;
|
---|
4395 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_D | X86DESCATTR_DT | X86_SEL_TYPE_ER_ACC
|
---|
4396 | | (3 << X86DESCATTR_DPL_SHIFT);
|
---|
4397 | }
|
---|
4398 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs | 3;
|
---|
4399 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs | 3;
|
---|
4400 | pVCpu->cpum.GstCtx.cs.u64Base = 0;
|
---|
4401 | pVCpu->cpum.GstCtx.cs.u32Limit = UINT32_MAX;
|
---|
4402 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4403 |
|
---|
4404 | pVCpu->cpum.GstCtx.ss.Sel = uNewSs | 3;
|
---|
4405 | pVCpu->cpum.GstCtx.ss.ValidSel = uNewSs | 3;
|
---|
4406 | pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4407 | /* The SS hidden bits remains unchanged says AMD. To that I say "Yeah, right!". */
|
---|
4408 | pVCpu->cpum.GstCtx.ss.Attr.u |= (3 << X86DESCATTR_DPL_SHIFT);
|
---|
4409 | /** @todo Testcase: verify that SS.u1Long and SS.u1DefBig are left unchanged
|
---|
4410 | * on sysret. */
|
---|
4411 | /** @todo intel documents SS.BASE and SS.LIMIT as being set as well as the
|
---|
4412 | * TYPE, S, DPL, P, B and G flag bits. */
|
---|
4413 |
|
---|
4414 | if (!f32Bit)
|
---|
4415 | pVCpu->iem.s.fExec = (pVCpu->iem.s.fExec & ~(IEM_F_MODE_MASK | IEM_F_X86_CPL_MASK))
|
---|
4416 | | (3 << IEM_F_X86_CPL_SHIFT)
|
---|
4417 | | IEM_F_MODE_X86_64BIT;
|
---|
4418 | else
|
---|
4419 | pVCpu->iem.s.fExec = (pVCpu->iem.s.fExec & ~(IEM_F_MODE_MASK | IEM_F_X86_CPL_MASK))
|
---|
4420 | | (3 << IEM_F_X86_CPL_SHIFT)
|
---|
4421 | | IEM_F_MODE_X86_32BIT_PROT
|
---|
4422 | /** @todo sort out the SS.BASE/LIM/ATTR claim by AMD and maybe we can switch to
|
---|
4423 | * iemCalc32BitFlatIndicatorDsEs and move this up into the above branch. */
|
---|
4424 | | iemCalc32BitFlatIndicator(pVCpu);
|
---|
4425 |
|
---|
4426 | /* Flush the prefetch buffer. */
|
---|
4427 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr);
|
---|
4428 |
|
---|
4429 | /** @todo single step */
|
---|
4430 | return VINF_SUCCESS;
|
---|
4431 | }
|
---|
4432 |
|
---|
4433 |
|
---|
4434 | /**
|
---|
4435 | * Implements SYSENTER (Intel, 32-bit AMD).
|
---|
4436 | */
|
---|
4437 | IEM_CIMPL_DEF_0(iemCImpl_sysenter)
|
---|
4438 | {
|
---|
4439 | RT_NOREF(cbInstr);
|
---|
4440 |
|
---|
4441 | /*
|
---|
4442 | * Check preconditions.
|
---|
4443 | *
|
---|
4444 | * Note that CPUs described in the documentation may load a few odd values
|
---|
4445 | * into CS and SS than we allow here. This has yet to be checked on real
|
---|
4446 | * hardware.
|
---|
4447 | */
|
---|
4448 | if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fSysEnter)
|
---|
4449 | {
|
---|
4450 | Log(("sysenter: not supported -=> #UD\n"));
|
---|
4451 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4452 | }
|
---|
4453 | if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE))
|
---|
4454 | {
|
---|
4455 | Log(("sysenter: Protected or long mode is required -> #GP(0)\n"));
|
---|
4456 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4457 | }
|
---|
4458 | bool fIsLongMode = CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu));
|
---|
4459 | if (IEM_IS_GUEST_CPU_AMD(pVCpu) && fIsLongMode)
|
---|
4460 | {
|
---|
4461 | Log(("sysenter: Only available in protected mode on AMD -> #UD\n"));
|
---|
4462 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4463 | }
|
---|
4464 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SYSENTER_MSRS);
|
---|
4465 | uint16_t uNewCs = pVCpu->cpum.GstCtx.SysEnter.cs;
|
---|
4466 | if ((uNewCs & X86_SEL_MASK_OFF_RPL) == 0)
|
---|
4467 | {
|
---|
4468 | Log(("sysenter: SYSENTER_CS = %#x -> #GP(0)\n", uNewCs));
|
---|
4469 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4470 | }
|
---|
4471 |
|
---|
4472 | /* This test isn't in the docs, it's just a safeguard against missing
|
---|
4473 | canonical checks when writing the registers. */
|
---|
4474 | if (RT_LIKELY( !fIsLongMode
|
---|
4475 | || ( IEM_IS_CANONICAL(pVCpu->cpum.GstCtx.SysEnter.eip)
|
---|
4476 | && IEM_IS_CANONICAL(pVCpu->cpum.GstCtx.SysEnter.esp))))
|
---|
4477 | { /* likely */ }
|
---|
4478 | else
|
---|
4479 | {
|
---|
4480 | Log(("sysenter: SYSENTER_EIP = %#RX64 or/and SYSENTER_ESP = %#RX64 not canonical -> #GP(0)\n",
|
---|
4481 | pVCpu->cpum.GstCtx.SysEnter.eip, pVCpu->cpum.GstCtx.SysEnter.esp));
|
---|
4482 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4483 | }
|
---|
4484 |
|
---|
4485 | /** @todo Test: Sysenter from ring-0, ring-1 and ring-2. */
|
---|
4486 |
|
---|
4487 | /*
|
---|
4488 | * Update registers and commit.
|
---|
4489 | */
|
---|
4490 | if (fIsLongMode)
|
---|
4491 | {
|
---|
4492 | Log(("sysenter: %04x:%016RX64 [efl=%#llx] -> %04x:%016RX64\n", pVCpu->cpum.GstCtx.cs, pVCpu->cpum.GstCtx.rip,
|
---|
4493 | pVCpu->cpum.GstCtx.rflags.u, uNewCs & X86_SEL_MASK_OFF_RPL, pVCpu->cpum.GstCtx.SysEnter.eip));
|
---|
4494 | pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.SysEnter.eip;
|
---|
4495 | pVCpu->cpum.GstCtx.rsp = pVCpu->cpum.GstCtx.SysEnter.esp;
|
---|
4496 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_L | X86DESCATTR_G | X86DESCATTR_P | X86DESCATTR_DT
|
---|
4497 | | X86DESCATTR_LIMIT_HIGH | X86_SEL_TYPE_ER_ACC;
|
---|
4498 | pVCpu->iem.s.fExec = (pVCpu->iem.s.fExec & ~(IEM_F_MODE_MASK | IEM_F_X86_CPL_MASK))
|
---|
4499 | | IEM_F_MODE_X86_64BIT;
|
---|
4500 | }
|
---|
4501 | else
|
---|
4502 | {
|
---|
4503 | Log(("sysenter: %04x:%08RX32 [efl=%#llx] -> %04x:%08RX32\n", pVCpu->cpum.GstCtx.cs, (uint32_t)pVCpu->cpum.GstCtx.rip,
|
---|
4504 | pVCpu->cpum.GstCtx.rflags.u, uNewCs & X86_SEL_MASK_OFF_RPL, (uint32_t)pVCpu->cpum.GstCtx.SysEnter.eip));
|
---|
4505 | pVCpu->cpum.GstCtx.rip = (uint32_t)pVCpu->cpum.GstCtx.SysEnter.eip;
|
---|
4506 | pVCpu->cpum.GstCtx.rsp = (uint32_t)pVCpu->cpum.GstCtx.SysEnter.esp;
|
---|
4507 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_D | X86DESCATTR_G | X86DESCATTR_P | X86DESCATTR_DT
|
---|
4508 | | X86DESCATTR_LIMIT_HIGH | X86_SEL_TYPE_ER_ACC;
|
---|
4509 | pVCpu->iem.s.fExec = (pVCpu->iem.s.fExec & ~(IEM_F_MODE_MASK | IEM_F_X86_CPL_MASK))
|
---|
4510 | | IEM_F_MODE_X86_32BIT
|
---|
4511 | | iemCalc32BitFlatIndicatorEsDs(pVCpu);
|
---|
4512 | }
|
---|
4513 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs & X86_SEL_MASK_OFF_RPL;
|
---|
4514 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs & X86_SEL_MASK_OFF_RPL;
|
---|
4515 | pVCpu->cpum.GstCtx.cs.u64Base = 0;
|
---|
4516 | pVCpu->cpum.GstCtx.cs.u32Limit = UINT32_MAX;
|
---|
4517 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4518 |
|
---|
4519 | pVCpu->cpum.GstCtx.ss.Sel = (uNewCs & X86_SEL_MASK_OFF_RPL) + 8;
|
---|
4520 | pVCpu->cpum.GstCtx.ss.ValidSel = (uNewCs & X86_SEL_MASK_OFF_RPL) + 8;
|
---|
4521 | pVCpu->cpum.GstCtx.ss.u64Base = 0;
|
---|
4522 | pVCpu->cpum.GstCtx.ss.u32Limit = UINT32_MAX;
|
---|
4523 | pVCpu->cpum.GstCtx.ss.Attr.u = X86DESCATTR_D | X86DESCATTR_G | X86DESCATTR_P | X86DESCATTR_DT
|
---|
4524 | | X86DESCATTR_LIMIT_HIGH | X86_SEL_TYPE_RW_ACC;
|
---|
4525 | pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4526 |
|
---|
4527 | pVCpu->cpum.GstCtx.rflags.Bits.u1IF = 0;
|
---|
4528 | pVCpu->cpum.GstCtx.rflags.Bits.u1VM = 0;
|
---|
4529 | pVCpu->cpum.GstCtx.rflags.Bits.u1RF = 0;
|
---|
4530 |
|
---|
4531 | /* Flush the prefetch buffer. */
|
---|
4532 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr);
|
---|
4533 |
|
---|
4534 | /** @todo single stepping */
|
---|
4535 | return VINF_SUCCESS;
|
---|
4536 | }
|
---|
4537 |
|
---|
4538 |
|
---|
4539 | /**
|
---|
4540 | * Implements SYSEXIT (Intel, 32-bit AMD).
|
---|
4541 | *
|
---|
4542 | * @param enmEffOpSize The effective operand size.
|
---|
4543 | */
|
---|
4544 | IEM_CIMPL_DEF_1(iemCImpl_sysexit, IEMMODE, enmEffOpSize)
|
---|
4545 | {
|
---|
4546 | RT_NOREF(cbInstr);
|
---|
4547 |
|
---|
4548 | /*
|
---|
4549 | * Check preconditions.
|
---|
4550 | *
|
---|
4551 | * Note that CPUs described in the documentation may load a few odd values
|
---|
4552 | * into CS and SS than we allow here. This has yet to be checked on real
|
---|
4553 | * hardware.
|
---|
4554 | */
|
---|
4555 | if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fSysEnter)
|
---|
4556 | {
|
---|
4557 | Log(("sysexit: not supported -=> #UD\n"));
|
---|
4558 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4559 | }
|
---|
4560 | if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE))
|
---|
4561 | {
|
---|
4562 | Log(("sysexit: Protected or long mode is required -> #GP(0)\n"));
|
---|
4563 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4564 | }
|
---|
4565 | bool fIsLongMode = CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu));
|
---|
4566 | if (IEM_IS_GUEST_CPU_AMD(pVCpu) && fIsLongMode)
|
---|
4567 | {
|
---|
4568 | Log(("sysexit: Only available in protected mode on AMD -> #UD\n"));
|
---|
4569 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4570 | }
|
---|
4571 | if (IEM_GET_CPL(pVCpu) != 0)
|
---|
4572 | {
|
---|
4573 | Log(("sysexit: CPL(=%u) != 0 -> #GP(0)\n", IEM_GET_CPL(pVCpu)));
|
---|
4574 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4575 | }
|
---|
4576 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SYSENTER_MSRS);
|
---|
4577 | uint16_t uNewCs = pVCpu->cpum.GstCtx.SysEnter.cs;
|
---|
4578 | if ((uNewCs & X86_SEL_MASK_OFF_RPL) == 0)
|
---|
4579 | {
|
---|
4580 | Log(("sysexit: SYSENTER_CS = %#x -> #GP(0)\n", uNewCs));
|
---|
4581 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4582 | }
|
---|
4583 |
|
---|
4584 | /*
|
---|
4585 | * Update registers and commit.
|
---|
4586 | */
|
---|
4587 | if (enmEffOpSize == IEMMODE_64BIT)
|
---|
4588 | {
|
---|
4589 | Log(("sysexit: %04x:%016RX64 [efl=%#llx] -> %04x:%016RX64\n", pVCpu->cpum.GstCtx.cs, pVCpu->cpum.GstCtx.rip,
|
---|
4590 | pVCpu->cpum.GstCtx.rflags.u, (uNewCs | 3) + 32, pVCpu->cpum.GstCtx.rcx));
|
---|
4591 | pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.rdx;
|
---|
4592 | pVCpu->cpum.GstCtx.rsp = pVCpu->cpum.GstCtx.rcx;
|
---|
4593 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_L | X86DESCATTR_G | X86DESCATTR_P | X86DESCATTR_DT
|
---|
4594 | | X86DESCATTR_LIMIT_HIGH | X86_SEL_TYPE_ER_ACC | (3 << X86DESCATTR_DPL_SHIFT);
|
---|
4595 | pVCpu->cpum.GstCtx.cs.Sel = (uNewCs | 3) + 32;
|
---|
4596 | pVCpu->cpum.GstCtx.cs.ValidSel = (uNewCs | 3) + 32;
|
---|
4597 | pVCpu->cpum.GstCtx.ss.Sel = (uNewCs | 3) + 40;
|
---|
4598 | pVCpu->cpum.GstCtx.ss.ValidSel = (uNewCs | 3) + 40;
|
---|
4599 |
|
---|
4600 | pVCpu->iem.s.fExec = (pVCpu->iem.s.fExec & ~(IEM_F_MODE_MASK | IEM_F_X86_CPL_MASK))
|
---|
4601 | | (3 << IEM_F_X86_CPL_SHIFT)
|
---|
4602 | | IEM_F_MODE_X86_64BIT;
|
---|
4603 | }
|
---|
4604 | else
|
---|
4605 | {
|
---|
4606 | Log(("sysexit: %04x:%08RX64 [efl=%#llx] -> %04x:%08RX32\n", pVCpu->cpum.GstCtx.cs, pVCpu->cpum.GstCtx.rip,
|
---|
4607 | pVCpu->cpum.GstCtx.rflags.u, (uNewCs | 3) + 16, (uint32_t)pVCpu->cpum.GstCtx.edx));
|
---|
4608 | pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.edx;
|
---|
4609 | pVCpu->cpum.GstCtx.rsp = pVCpu->cpum.GstCtx.ecx;
|
---|
4610 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_D | X86DESCATTR_G | X86DESCATTR_P | X86DESCATTR_DT
|
---|
4611 | | X86DESCATTR_LIMIT_HIGH | X86_SEL_TYPE_ER_ACC | (3 << X86DESCATTR_DPL_SHIFT);
|
---|
4612 | pVCpu->cpum.GstCtx.cs.Sel = (uNewCs | 3) + 16;
|
---|
4613 | pVCpu->cpum.GstCtx.cs.ValidSel = (uNewCs | 3) + 16;
|
---|
4614 | pVCpu->cpum.GstCtx.ss.Sel = (uNewCs | 3) + 24;
|
---|
4615 | pVCpu->cpum.GstCtx.ss.ValidSel = (uNewCs | 3) + 24;
|
---|
4616 |
|
---|
4617 | pVCpu->iem.s.fExec = (pVCpu->iem.s.fExec & ~(IEM_F_MODE_MASK | IEM_F_X86_CPL_MASK))
|
---|
4618 | | (3 << IEM_F_X86_CPL_SHIFT)
|
---|
4619 | | IEM_F_MODE_X86_32BIT
|
---|
4620 | | iemCalc32BitFlatIndicatorEsDs(pVCpu);
|
---|
4621 | }
|
---|
4622 | pVCpu->cpum.GstCtx.cs.u64Base = 0;
|
---|
4623 | pVCpu->cpum.GstCtx.cs.u32Limit = UINT32_MAX;
|
---|
4624 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4625 |
|
---|
4626 | pVCpu->cpum.GstCtx.ss.u64Base = 0;
|
---|
4627 | pVCpu->cpum.GstCtx.ss.u32Limit = UINT32_MAX;
|
---|
4628 | pVCpu->cpum.GstCtx.ss.Attr.u = X86DESCATTR_D | X86DESCATTR_G | X86DESCATTR_P | X86DESCATTR_DT
|
---|
4629 | | X86DESCATTR_LIMIT_HIGH | X86_SEL_TYPE_RW_ACC | (3 << X86DESCATTR_DPL_SHIFT);
|
---|
4630 | pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4631 | pVCpu->cpum.GstCtx.rflags.Bits.u1RF = 0;
|
---|
4632 |
|
---|
4633 | /** @todo single stepping */
|
---|
4634 |
|
---|
4635 | /* Flush the prefetch buffer. */
|
---|
4636 | IEM_FLUSH_PREFETCH_HEAVY(pVCpu, cbInstr);
|
---|
4637 |
|
---|
4638 | return VINF_SUCCESS;
|
---|
4639 | }
|
---|
4640 |
|
---|
4641 |
|
---|
4642 | /**
|
---|
4643 | * Completes a MOV SReg,XXX or POP SReg instruction.
|
---|
4644 | *
|
---|
4645 | * When not modifying SS or when we're already in an interrupt shadow we
|
---|
4646 | * can update RIP and finish the instruction the normal way.
|
---|
4647 | *
|
---|
4648 | * Otherwise, the MOV/POP SS interrupt shadow that we now enable will block
|
---|
4649 | * both TF and DBx events. The TF will be ignored while the DBx ones will
|
---|
4650 | * be delayed till the next instruction boundrary. For more details see
|
---|
4651 | * @sdmv3{077,200,6.8.3,Masking Exceptions and Interrupts When Switching Stacks}.
|
---|
4652 | */
|
---|
4653 | DECLINLINE(VBOXSTRICTRC) iemCImpl_LoadSRegFinish(PVMCPUCC pVCpu, uint8_t cbInstr, uint8_t iSegReg)
|
---|
4654 | {
|
---|
4655 | if (iSegReg != X86_SREG_SS || CPUMIsInInterruptShadow(&pVCpu->cpum.GstCtx))
|
---|
4656 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
4657 |
|
---|
4658 | iemRegAddToRip(pVCpu, cbInstr);
|
---|
4659 | pVCpu->cpum.GstCtx.eflags.uBoth &= ~X86_EFL_RF; /* Shadow int isn't set and DRx is delayed, so only clear RF. */
|
---|
4660 | CPUMSetInInterruptShadowSs(&pVCpu->cpum.GstCtx);
|
---|
4661 |
|
---|
4662 | return VINF_SUCCESS;
|
---|
4663 | }
|
---|
4664 |
|
---|
4665 |
|
---|
4666 | /**
|
---|
4667 | * Common worker for 'pop SReg', 'mov SReg, GReg' and 'lXs GReg, reg/mem'.
|
---|
4668 | *
|
---|
4669 | * @param pVCpu The cross context virtual CPU structure of the calling
|
---|
4670 | * thread.
|
---|
4671 | * @param iSegReg The segment register number (valid).
|
---|
4672 | * @param uSel The new selector value.
|
---|
4673 | */
|
---|
4674 | static VBOXSTRICTRC iemCImpl_LoadSRegWorker(PVMCPUCC pVCpu, uint8_t iSegReg, uint16_t uSel)
|
---|
4675 | {
|
---|
4676 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SREG_FROM_IDX(iSegReg));
|
---|
4677 | uint16_t *pSel = iemSRegRef(pVCpu, iSegReg);
|
---|
4678 | PCPUMSELREGHID pHid = iemSRegGetHid(pVCpu, iSegReg);
|
---|
4679 |
|
---|
4680 | Assert(iSegReg <= X86_SREG_GS && iSegReg != X86_SREG_CS);
|
---|
4681 |
|
---|
4682 | /*
|
---|
4683 | * Real mode and V8086 mode are easy.
|
---|
4684 | */
|
---|
4685 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
4686 | {
|
---|
4687 | *pSel = uSel;
|
---|
4688 | pHid->u64Base = (uint32_t)uSel << 4;
|
---|
4689 | pHid->ValidSel = uSel;
|
---|
4690 | pHid->fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4691 | #if 0 /* AMD Volume 2, chapter 4.1 - "real mode segmentation" - states that limit and attributes are untouched. */
|
---|
4692 | /** @todo Does the CPU actually load limits and attributes in the
|
---|
4693 | * real/V8086 mode segment load case? It doesn't for CS in far
|
---|
4694 | * jumps... Affects unreal mode. */
|
---|
4695 | pHid->u32Limit = 0xffff;
|
---|
4696 | pHid->Attr.u = 0;
|
---|
4697 | pHid->Attr.n.u1Present = 1;
|
---|
4698 | pHid->Attr.n.u1DescType = 1;
|
---|
4699 | pHid->Attr.n.u4Type = iSegReg != X86_SREG_CS
|
---|
4700 | ? X86_SEL_TYPE_RW
|
---|
4701 | : X86_SEL_TYPE_READ | X86_SEL_TYPE_CODE;
|
---|
4702 | #endif
|
---|
4703 |
|
---|
4704 | /* Update the FLAT 32-bit mode flag, if we're in 32-bit unreal mode (unlikely): */
|
---|
4705 | if (RT_LIKELY(!IEM_IS_32BIT_CODE(pVCpu)))
|
---|
4706 | { /* likely */ }
|
---|
4707 | else if (uSel != 0)
|
---|
4708 | pVCpu->iem.s.fExec &= ~IEM_F_MODE_X86_FLAT_OR_PRE_386_MASK;
|
---|
4709 | else
|
---|
4710 | pVCpu->iem.s.fExec = (pVCpu->iem.s.fExec & ~IEM_F_MODE_X86_FLAT_OR_PRE_386_MASK)
|
---|
4711 | | iemCalc32BitFlatIndicator(pVCpu);
|
---|
4712 | }
|
---|
4713 | /*
|
---|
4714 | * Protected / long mode - null segment.
|
---|
4715 | *
|
---|
4716 | * Check if it's a null segment selector value first, that's OK for DS, ES,
|
---|
4717 | * FS and GS. If not null, then we have to load and parse the descriptor.
|
---|
4718 | */
|
---|
4719 | else if (!(uSel & X86_SEL_MASK_OFF_RPL))
|
---|
4720 | {
|
---|
4721 | Assert(iSegReg != X86_SREG_CS); /** @todo testcase for \#UD on MOV CS, ax! */
|
---|
4722 | if (iSegReg == X86_SREG_SS)
|
---|
4723 | {
|
---|
4724 | /* In 64-bit kernel mode, the stack can be 0 because of the way
|
---|
4725 | interrupts are dispatched. AMD seems to have a slighly more
|
---|
4726 | relaxed relationship to SS.RPL than intel does. */
|
---|
4727 | /** @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! */
|
---|
4728 | if ( !IEM_IS_64BIT_CODE(pVCpu)
|
---|
4729 | || IEM_GET_CPL(pVCpu) > 2
|
---|
4730 | || ( uSel != IEM_GET_CPL(pVCpu)
|
---|
4731 | && !IEM_IS_GUEST_CPU_AMD(pVCpu)) )
|
---|
4732 | {
|
---|
4733 | Log(("load sreg %#x -> invalid stack selector, #GP(0)\n", uSel));
|
---|
4734 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4735 | }
|
---|
4736 | }
|
---|
4737 |
|
---|
4738 | *pSel = uSel; /* Not RPL, remember :-) */
|
---|
4739 | iemHlpLoadNullDataSelectorProt(pVCpu, pHid, uSel);
|
---|
4740 | if (iSegReg == X86_SREG_SS)
|
---|
4741 | pHid->Attr.u |= IEM_GET_CPL(pVCpu) << X86DESCATTR_DPL_SHIFT;
|
---|
4742 |
|
---|
4743 | /* This will affect the FLAT 32-bit mode flag: */
|
---|
4744 | if ( iSegReg < X86_SREG_FS
|
---|
4745 | && IEM_IS_32BIT_CODE(pVCpu))
|
---|
4746 | pVCpu->iem.s.fExec &= ~IEM_F_MODE_X86_FLAT_OR_PRE_386_MASK;
|
---|
4747 | }
|
---|
4748 | /*
|
---|
4749 | * Protected / long mode.
|
---|
4750 | */
|
---|
4751 | else
|
---|
4752 | {
|
---|
4753 | /* Fetch the descriptor. */
|
---|
4754 | IEMSELDESC Desc;
|
---|
4755 | VBOXSTRICTRC rcStrict = iemMemFetchSelDesc(pVCpu, &Desc, uSel, X86_XCPT_GP); /** @todo Correct exception? */
|
---|
4756 | if (rcStrict != VINF_SUCCESS)
|
---|
4757 | return rcStrict;
|
---|
4758 |
|
---|
4759 | /* Check GPs first. */
|
---|
4760 | if (!Desc.Legacy.Gen.u1DescType)
|
---|
4761 | {
|
---|
4762 | Log(("load sreg %d (=%#x) - system selector (%#x) -> #GP\n", iSegReg, uSel, Desc.Legacy.Gen.u4Type));
|
---|
4763 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
4764 | }
|
---|
4765 | if (iSegReg == X86_SREG_SS) /* SS gets different treatment */
|
---|
4766 | {
|
---|
4767 | if ( (Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE)
|
---|
4768 | || !(Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_WRITE) )
|
---|
4769 | {
|
---|
4770 | Log(("load sreg SS, %#x - code or read only (%#x) -> #GP\n", uSel, Desc.Legacy.Gen.u4Type));
|
---|
4771 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
4772 | }
|
---|
4773 | if ((uSel & X86_SEL_RPL) != IEM_GET_CPL(pVCpu))
|
---|
4774 | {
|
---|
4775 | Log(("load sreg SS, %#x - RPL and CPL (%d) differs -> #GP\n", uSel, IEM_GET_CPL(pVCpu)));
|
---|
4776 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
4777 | }
|
---|
4778 | if (Desc.Legacy.Gen.u2Dpl != IEM_GET_CPL(pVCpu))
|
---|
4779 | {
|
---|
4780 | Log(("load sreg SS, %#x - DPL (%d) and CPL (%d) differs -> #GP\n", uSel, Desc.Legacy.Gen.u2Dpl, IEM_GET_CPL(pVCpu)));
|
---|
4781 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
4782 | }
|
---|
4783 | }
|
---|
4784 | else
|
---|
4785 | {
|
---|
4786 | if ((Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_READ)) == X86_SEL_TYPE_CODE)
|
---|
4787 | {
|
---|
4788 | Log(("load sreg%u, %#x - execute only segment -> #GP\n", iSegReg, uSel));
|
---|
4789 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
4790 | }
|
---|
4791 | if ( (Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
|
---|
4792 | != (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
|
---|
4793 | {
|
---|
4794 | #if 0 /* this is what intel says. */
|
---|
4795 | if ( (uSel & X86_SEL_RPL) > Desc.Legacy.Gen.u2Dpl
|
---|
4796 | && IEM_GET_CPL(pVCpu) > Desc.Legacy.Gen.u2Dpl)
|
---|
4797 | {
|
---|
4798 | Log(("load sreg%u, %#x - both RPL (%d) and CPL (%d) are greater than DPL (%d) -> #GP\n",
|
---|
4799 | iSegReg, uSel, (uSel & X86_SEL_RPL), IEM_GET_CPL(pVCpu), Desc.Legacy.Gen.u2Dpl));
|
---|
4800 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
4801 | }
|
---|
4802 | #else /* this is what makes more sense. */
|
---|
4803 | if ((unsigned)(uSel & X86_SEL_RPL) > Desc.Legacy.Gen.u2Dpl)
|
---|
4804 | {
|
---|
4805 | Log(("load sreg%u, %#x - RPL (%d) is greater than DPL (%d) -> #GP\n",
|
---|
4806 | iSegReg, uSel, (uSel & X86_SEL_RPL), Desc.Legacy.Gen.u2Dpl));
|
---|
4807 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
4808 | }
|
---|
4809 | if (IEM_GET_CPL(pVCpu) > Desc.Legacy.Gen.u2Dpl)
|
---|
4810 | {
|
---|
4811 | Log(("load sreg%u, %#x - CPL (%d) is greater than DPL (%d) -> #GP\n",
|
---|
4812 | iSegReg, uSel, IEM_GET_CPL(pVCpu), Desc.Legacy.Gen.u2Dpl));
|
---|
4813 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
4814 | }
|
---|
4815 | #endif
|
---|
4816 | }
|
---|
4817 | }
|
---|
4818 |
|
---|
4819 | /* Is it there? */
|
---|
4820 | if (!Desc.Legacy.Gen.u1Present)
|
---|
4821 | {
|
---|
4822 | Log(("load sreg%d,%#x - segment not present -> #NP\n", iSegReg, uSel));
|
---|
4823 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uSel);
|
---|
4824 | }
|
---|
4825 |
|
---|
4826 | /* The base and limit. */
|
---|
4827 | uint32_t cbLimit = X86DESC_LIMIT_G(&Desc.Legacy);
|
---|
4828 | uint64_t u64Base = X86DESC_BASE(&Desc.Legacy);
|
---|
4829 |
|
---|
4830 | /*
|
---|
4831 | * Ok, everything checked out fine. Now set the accessed bit before
|
---|
4832 | * committing the result into the registers.
|
---|
4833 | */
|
---|
4834 | if (!(Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
4835 | {
|
---|
4836 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uSel);
|
---|
4837 | if (rcStrict != VINF_SUCCESS)
|
---|
4838 | return rcStrict;
|
---|
4839 | Desc.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
4840 | }
|
---|
4841 |
|
---|
4842 | /* commit */
|
---|
4843 | *pSel = uSel;
|
---|
4844 | pHid->Attr.u = X86DESC_GET_HID_ATTR(&Desc.Legacy);
|
---|
4845 | pHid->u32Limit = cbLimit;
|
---|
4846 | pHid->u64Base = u64Base;
|
---|
4847 | pHid->ValidSel = uSel;
|
---|
4848 | pHid->fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4849 |
|
---|
4850 | /** @todo check if the hidden bits are loaded correctly for 64-bit
|
---|
4851 | * mode. */
|
---|
4852 |
|
---|
4853 | /* This will affect the FLAT 32-bit mode flag: */
|
---|
4854 | if ( iSegReg < X86_SREG_FS
|
---|
4855 | && IEM_IS_32BIT_CODE(pVCpu))
|
---|
4856 | pVCpu->iem.s.fExec = (pVCpu->iem.s.fExec & ~IEM_F_MODE_X86_FLAT_OR_PRE_386_MASK)
|
---|
4857 | | iemCalc32BitFlatIndicator(pVCpu);
|
---|
4858 | }
|
---|
4859 |
|
---|
4860 | Assert(CPUMSELREG_ARE_HIDDEN_PARTS_VALID(pVCpu, pHid));
|
---|
4861 | CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_HIDDEN_SEL_REGS);
|
---|
4862 | return VINF_SUCCESS;
|
---|
4863 | }
|
---|
4864 |
|
---|
4865 |
|
---|
4866 | /**
|
---|
4867 | * Implements 'mov SReg, r/m'.
|
---|
4868 | *
|
---|
4869 | * @param iSegReg The segment register number (valid).
|
---|
4870 | * @param uSel The new selector value.
|
---|
4871 | */
|
---|
4872 | IEM_CIMPL_DEF_2(iemCImpl_load_SReg, uint8_t, iSegReg, uint16_t, uSel)
|
---|
4873 | {
|
---|
4874 | VBOXSTRICTRC rcStrict = iemCImpl_LoadSRegWorker(pVCpu, iSegReg, uSel);
|
---|
4875 | if (rcStrict == VINF_SUCCESS)
|
---|
4876 | rcStrict = iemCImpl_LoadSRegFinish(pVCpu, cbInstr, iSegReg);
|
---|
4877 | return rcStrict;
|
---|
4878 | }
|
---|
4879 |
|
---|
4880 |
|
---|
4881 | /**
|
---|
4882 | * Implements 'pop SReg'.
|
---|
4883 | *
|
---|
4884 | * @param iSegReg The segment register number (valid).
|
---|
4885 | * @param enmEffOpSize The efficient operand size (valid).
|
---|
4886 | */
|
---|
4887 | IEM_CIMPL_DEF_2(iemCImpl_pop_Sreg, uint8_t, iSegReg, IEMMODE, enmEffOpSize)
|
---|
4888 | {
|
---|
4889 | VBOXSTRICTRC rcStrict;
|
---|
4890 |
|
---|
4891 | /*
|
---|
4892 | * Read the selector off the stack and join paths with mov ss, reg.
|
---|
4893 | */
|
---|
4894 | RTUINT64U TmpRsp;
|
---|
4895 | TmpRsp.u = pVCpu->cpum.GstCtx.rsp;
|
---|
4896 | switch (enmEffOpSize)
|
---|
4897 | {
|
---|
4898 | case IEMMODE_16BIT:
|
---|
4899 | {
|
---|
4900 | uint16_t uSel;
|
---|
4901 | rcStrict = iemMemStackPopU16Ex(pVCpu, &uSel, &TmpRsp);
|
---|
4902 | if (rcStrict == VINF_SUCCESS)
|
---|
4903 | rcStrict = iemCImpl_LoadSRegWorker(pVCpu, iSegReg, uSel);
|
---|
4904 | break;
|
---|
4905 | }
|
---|
4906 |
|
---|
4907 | case IEMMODE_32BIT:
|
---|
4908 | {
|
---|
4909 | uint32_t u32Value;
|
---|
4910 | rcStrict = iemMemStackPopU32Ex(pVCpu, &u32Value, &TmpRsp);
|
---|
4911 | if (rcStrict == VINF_SUCCESS)
|
---|
4912 | rcStrict = iemCImpl_LoadSRegWorker(pVCpu, iSegReg, (uint16_t)u32Value);
|
---|
4913 | break;
|
---|
4914 | }
|
---|
4915 |
|
---|
4916 | case IEMMODE_64BIT:
|
---|
4917 | {
|
---|
4918 | uint64_t u64Value;
|
---|
4919 | rcStrict = iemMemStackPopU64Ex(pVCpu, &u64Value, &TmpRsp);
|
---|
4920 | if (rcStrict == VINF_SUCCESS)
|
---|
4921 | rcStrict = iemCImpl_LoadSRegWorker(pVCpu, iSegReg, (uint16_t)u64Value);
|
---|
4922 | break;
|
---|
4923 | }
|
---|
4924 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
4925 | }
|
---|
4926 |
|
---|
4927 | /*
|
---|
4928 | * If the load succeeded, commit the stack change and finish the instruction.
|
---|
4929 | */
|
---|
4930 | if (rcStrict == VINF_SUCCESS)
|
---|
4931 | {
|
---|
4932 | pVCpu->cpum.GstCtx.rsp = TmpRsp.u;
|
---|
4933 | rcStrict = iemCImpl_LoadSRegFinish(pVCpu, cbInstr, iSegReg);
|
---|
4934 | }
|
---|
4935 |
|
---|
4936 | return rcStrict;
|
---|
4937 | }
|
---|
4938 |
|
---|
4939 |
|
---|
4940 | /**
|
---|
4941 | * Implements lgs, lfs, les, lds & lss.
|
---|
4942 | */
|
---|
4943 | IEM_CIMPL_DEF_5(iemCImpl_load_SReg_Greg, uint16_t, uSel, uint64_t, offSeg, uint8_t, iSegReg, uint8_t, iGReg, IEMMODE, enmEffOpSize)
|
---|
4944 | {
|
---|
4945 | /*
|
---|
4946 | * Use iemCImpl_LoadSRegWorker to do the tricky segment register loading.
|
---|
4947 | */
|
---|
4948 | /** @todo verify and test that mov, pop and lXs works the segment
|
---|
4949 | * register loading in the exact same way. */
|
---|
4950 | VBOXSTRICTRC rcStrict = iemCImpl_LoadSRegWorker(pVCpu, iSegReg, uSel);
|
---|
4951 | if (rcStrict == VINF_SUCCESS)
|
---|
4952 | {
|
---|
4953 | switch (enmEffOpSize)
|
---|
4954 | {
|
---|
4955 | case IEMMODE_16BIT:
|
---|
4956 | *(uint16_t *)iemGRegRef(pVCpu, iGReg) = offSeg;
|
---|
4957 | break;
|
---|
4958 | case IEMMODE_32BIT:
|
---|
4959 | case IEMMODE_64BIT:
|
---|
4960 | *(uint64_t *)iemGRegRef(pVCpu, iGReg) = offSeg;
|
---|
4961 | break;
|
---|
4962 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
4963 | }
|
---|
4964 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
4965 | }
|
---|
4966 | return rcStrict;
|
---|
4967 | }
|
---|
4968 |
|
---|
4969 |
|
---|
4970 | /**
|
---|
4971 | * Helper for VERR, VERW, LAR, and LSL and loads the descriptor into memory.
|
---|
4972 | *
|
---|
4973 | * @retval VINF_SUCCESS on success.
|
---|
4974 | * @retval VINF_IEM_SELECTOR_NOT_OK if the selector isn't ok.
|
---|
4975 | * @retval iemMemFetchSysU64 return value.
|
---|
4976 | *
|
---|
4977 | * @param pVCpu The cross context virtual CPU structure of the calling thread.
|
---|
4978 | * @param uSel The selector value.
|
---|
4979 | * @param fAllowSysDesc Whether system descriptors are OK or not.
|
---|
4980 | * @param pDesc Where to return the descriptor on success.
|
---|
4981 | */
|
---|
4982 | static VBOXSTRICTRC iemCImpl_LoadDescHelper(PVMCPUCC pVCpu, uint16_t uSel, bool fAllowSysDesc, PIEMSELDESC pDesc)
|
---|
4983 | {
|
---|
4984 | pDesc->Long.au64[0] = 0;
|
---|
4985 | pDesc->Long.au64[1] = 0;
|
---|
4986 |
|
---|
4987 | if (!(uSel & X86_SEL_MASK_OFF_RPL)) /** @todo test this on 64-bit. */
|
---|
4988 | return VINF_IEM_SELECTOR_NOT_OK;
|
---|
4989 |
|
---|
4990 | /* Within the table limits? */
|
---|
4991 | RTGCPTR GCPtrBase;
|
---|
4992 | if (uSel & X86_SEL_LDT)
|
---|
4993 | {
|
---|
4994 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_LDTR);
|
---|
4995 | if ( !pVCpu->cpum.GstCtx.ldtr.Attr.n.u1Present
|
---|
4996 | || (uSel | X86_SEL_RPL_LDT) > pVCpu->cpum.GstCtx.ldtr.u32Limit )
|
---|
4997 | return VINF_IEM_SELECTOR_NOT_OK;
|
---|
4998 | GCPtrBase = pVCpu->cpum.GstCtx.ldtr.u64Base;
|
---|
4999 | }
|
---|
5000 | else
|
---|
5001 | {
|
---|
5002 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_GDTR);
|
---|
5003 | if ((uSel | X86_SEL_RPL_LDT) > pVCpu->cpum.GstCtx.gdtr.cbGdt)
|
---|
5004 | return VINF_IEM_SELECTOR_NOT_OK;
|
---|
5005 | GCPtrBase = pVCpu->cpum.GstCtx.gdtr.pGdt;
|
---|
5006 | }
|
---|
5007 |
|
---|
5008 | /* Fetch the descriptor. */
|
---|
5009 | VBOXSTRICTRC rcStrict = iemMemFetchSysU64(pVCpu, &pDesc->Legacy.u, UINT8_MAX, GCPtrBase + (uSel & X86_SEL_MASK));
|
---|
5010 | if (rcStrict != VINF_SUCCESS)
|
---|
5011 | return rcStrict;
|
---|
5012 | if (!pDesc->Legacy.Gen.u1DescType)
|
---|
5013 | {
|
---|
5014 | if (!fAllowSysDesc)
|
---|
5015 | return VINF_IEM_SELECTOR_NOT_OK;
|
---|
5016 | if (CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu)))
|
---|
5017 | {
|
---|
5018 | rcStrict = iemMemFetchSysU64(pVCpu, &pDesc->Long.au64[1], UINT8_MAX, GCPtrBase + (uSel & X86_SEL_MASK) + 8);
|
---|
5019 | if (rcStrict != VINF_SUCCESS)
|
---|
5020 | return rcStrict;
|
---|
5021 | }
|
---|
5022 |
|
---|
5023 | }
|
---|
5024 |
|
---|
5025 | return VINF_SUCCESS;
|
---|
5026 | }
|
---|
5027 |
|
---|
5028 |
|
---|
5029 | /**
|
---|
5030 | * Implements verr (fWrite = false) and verw (fWrite = true).
|
---|
5031 | */
|
---|
5032 | IEM_CIMPL_DEF_2(iemCImpl_VerX, uint16_t, uSel, bool, fWrite)
|
---|
5033 | {
|
---|
5034 | Assert(!IEM_IS_REAL_OR_V86_MODE(pVCpu));
|
---|
5035 |
|
---|
5036 | /** @todo figure whether the accessed bit is set or not. */
|
---|
5037 |
|
---|
5038 | bool fAccessible = true;
|
---|
5039 | IEMSELDESC Desc;
|
---|
5040 | VBOXSTRICTRC rcStrict = iemCImpl_LoadDescHelper(pVCpu, uSel, false /*fAllowSysDesc*/, &Desc);
|
---|
5041 | if (rcStrict == VINF_SUCCESS)
|
---|
5042 | {
|
---|
5043 | /* Check the descriptor, order doesn't matter much here. */
|
---|
5044 | if ( !Desc.Legacy.Gen.u1DescType
|
---|
5045 | || !Desc.Legacy.Gen.u1Present)
|
---|
5046 | fAccessible = false;
|
---|
5047 | else
|
---|
5048 | {
|
---|
5049 | if ( fWrite
|
---|
5050 | ? (Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_WRITE)) != X86_SEL_TYPE_WRITE
|
---|
5051 | : (Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_READ)) == X86_SEL_TYPE_CODE)
|
---|
5052 | fAccessible = false;
|
---|
5053 |
|
---|
5054 | /** @todo testcase for the conforming behavior. */
|
---|
5055 | if ( (Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
|
---|
5056 | != (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
|
---|
5057 | {
|
---|
5058 | if ((unsigned)(uSel & X86_SEL_RPL) > Desc.Legacy.Gen.u2Dpl)
|
---|
5059 | fAccessible = false;
|
---|
5060 | else if (IEM_GET_CPL(pVCpu) > Desc.Legacy.Gen.u2Dpl)
|
---|
5061 | fAccessible = false;
|
---|
5062 | }
|
---|
5063 | }
|
---|
5064 |
|
---|
5065 | }
|
---|
5066 | else if (rcStrict == VINF_IEM_SELECTOR_NOT_OK)
|
---|
5067 | fAccessible = false;
|
---|
5068 | else
|
---|
5069 | return rcStrict;
|
---|
5070 |
|
---|
5071 | /* commit */
|
---|
5072 | pVCpu->cpum.GstCtx.eflags.Bits.u1ZF = fAccessible;
|
---|
5073 |
|
---|
5074 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5075 | }
|
---|
5076 |
|
---|
5077 |
|
---|
5078 | /**
|
---|
5079 | * Implements LAR and LSL with 64-bit operand size.
|
---|
5080 | *
|
---|
5081 | * @returns VINF_SUCCESS.
|
---|
5082 | * @param pu64Dst Pointer to the destination register.
|
---|
5083 | * @param uSel The selector to load details for.
|
---|
5084 | * @param fIsLar true = LAR, false = LSL.
|
---|
5085 | */
|
---|
5086 | IEM_CIMPL_DEF_3(iemCImpl_LarLsl_u64, uint64_t *, pu64Dst, uint16_t, uSel, bool, fIsLar)
|
---|
5087 | {
|
---|
5088 | Assert(!IEM_IS_REAL_OR_V86_MODE(pVCpu));
|
---|
5089 |
|
---|
5090 | /** @todo figure whether the accessed bit is set or not. */
|
---|
5091 |
|
---|
5092 | bool fDescOk = true;
|
---|
5093 | IEMSELDESC Desc;
|
---|
5094 | VBOXSTRICTRC rcStrict = iemCImpl_LoadDescHelper(pVCpu, uSel, true /*fAllowSysDesc*/, &Desc);
|
---|
5095 | if (rcStrict == VINF_SUCCESS)
|
---|
5096 | {
|
---|
5097 | /*
|
---|
5098 | * Check the descriptor type.
|
---|
5099 | */
|
---|
5100 | if (!Desc.Legacy.Gen.u1DescType)
|
---|
5101 | {
|
---|
5102 | if (CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu)))
|
---|
5103 | {
|
---|
5104 | if (Desc.Long.Gen.u5Zeros)
|
---|
5105 | fDescOk = false;
|
---|
5106 | else
|
---|
5107 | switch (Desc.Long.Gen.u4Type)
|
---|
5108 | {
|
---|
5109 | /** @todo Intel lists 0 as valid for LSL, verify whether that's correct */
|
---|
5110 | case AMD64_SEL_TYPE_SYS_TSS_AVAIL:
|
---|
5111 | case AMD64_SEL_TYPE_SYS_TSS_BUSY:
|
---|
5112 | case AMD64_SEL_TYPE_SYS_LDT: /** @todo Intel lists this as invalid for LAR, AMD and 32-bit does otherwise. */
|
---|
5113 | break;
|
---|
5114 | case AMD64_SEL_TYPE_SYS_CALL_GATE:
|
---|
5115 | fDescOk = fIsLar;
|
---|
5116 | break;
|
---|
5117 | default:
|
---|
5118 | fDescOk = false;
|
---|
5119 | break;
|
---|
5120 | }
|
---|
5121 | }
|
---|
5122 | else
|
---|
5123 | {
|
---|
5124 | switch (Desc.Long.Gen.u4Type)
|
---|
5125 | {
|
---|
5126 | case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
|
---|
5127 | case X86_SEL_TYPE_SYS_286_TSS_BUSY:
|
---|
5128 | case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
|
---|
5129 | case X86_SEL_TYPE_SYS_386_TSS_BUSY:
|
---|
5130 | case X86_SEL_TYPE_SYS_LDT:
|
---|
5131 | break;
|
---|
5132 | case X86_SEL_TYPE_SYS_286_CALL_GATE:
|
---|
5133 | case X86_SEL_TYPE_SYS_TASK_GATE:
|
---|
5134 | case X86_SEL_TYPE_SYS_386_CALL_GATE:
|
---|
5135 | fDescOk = fIsLar;
|
---|
5136 | break;
|
---|
5137 | default:
|
---|
5138 | fDescOk = false;
|
---|
5139 | break;
|
---|
5140 | }
|
---|
5141 | }
|
---|
5142 | }
|
---|
5143 | if (fDescOk)
|
---|
5144 | {
|
---|
5145 | /*
|
---|
5146 | * Check the RPL/DPL/CPL interaction..
|
---|
5147 | */
|
---|
5148 | /** @todo testcase for the conforming behavior. */
|
---|
5149 | if ( (Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF)) != (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF)
|
---|
5150 | || !Desc.Legacy.Gen.u1DescType)
|
---|
5151 | {
|
---|
5152 | if ((unsigned)(uSel & X86_SEL_RPL) > Desc.Legacy.Gen.u2Dpl)
|
---|
5153 | fDescOk = false;
|
---|
5154 | else if (IEM_GET_CPL(pVCpu) > Desc.Legacy.Gen.u2Dpl)
|
---|
5155 | fDescOk = false;
|
---|
5156 | }
|
---|
5157 | }
|
---|
5158 |
|
---|
5159 | if (fDescOk)
|
---|
5160 | {
|
---|
5161 | /*
|
---|
5162 | * All fine, start committing the result.
|
---|
5163 | */
|
---|
5164 | if (fIsLar)
|
---|
5165 | *pu64Dst = Desc.Legacy.au32[1] & UINT32_C(0x00ffff00);
|
---|
5166 | else
|
---|
5167 | *pu64Dst = X86DESC_LIMIT_G(&Desc.Legacy);
|
---|
5168 | }
|
---|
5169 |
|
---|
5170 | }
|
---|
5171 | else if (rcStrict == VINF_IEM_SELECTOR_NOT_OK)
|
---|
5172 | fDescOk = false;
|
---|
5173 | else
|
---|
5174 | return rcStrict;
|
---|
5175 |
|
---|
5176 | /* commit flags value and advance rip. */
|
---|
5177 | pVCpu->cpum.GstCtx.eflags.Bits.u1ZF = fDescOk;
|
---|
5178 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5179 | }
|
---|
5180 |
|
---|
5181 |
|
---|
5182 | /**
|
---|
5183 | * Implements LAR and LSL with 16-bit operand size.
|
---|
5184 | *
|
---|
5185 | * @returns VINF_SUCCESS.
|
---|
5186 | * @param pu16Dst Pointer to the destination register.
|
---|
5187 | * @param uSel The selector to load details for.
|
---|
5188 | * @param fIsLar true = LAR, false = LSL.
|
---|
5189 | */
|
---|
5190 | IEM_CIMPL_DEF_3(iemCImpl_LarLsl_u16, uint16_t *, pu16Dst, uint16_t, uSel, bool, fIsLar)
|
---|
5191 | {
|
---|
5192 | uint64_t u64TmpDst = *pu16Dst;
|
---|
5193 | IEM_CIMPL_CALL_3(iemCImpl_LarLsl_u64, &u64TmpDst, uSel, fIsLar);
|
---|
5194 | *pu16Dst = u64TmpDst;
|
---|
5195 | return VINF_SUCCESS;
|
---|
5196 | }
|
---|
5197 |
|
---|
5198 |
|
---|
5199 | /**
|
---|
5200 | * Implements lgdt.
|
---|
5201 | *
|
---|
5202 | * @param iEffSeg The segment of the new gdtr contents
|
---|
5203 | * @param GCPtrEffSrc The address of the new gdtr contents.
|
---|
5204 | * @param enmEffOpSize The effective operand size.
|
---|
5205 | */
|
---|
5206 | IEM_CIMPL_DEF_3(iemCImpl_lgdt, uint8_t, iEffSeg, RTGCPTR, GCPtrEffSrc, IEMMODE, enmEffOpSize)
|
---|
5207 | {
|
---|
5208 | if (IEM_GET_CPL(pVCpu) != 0)
|
---|
5209 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5210 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
5211 |
|
---|
5212 | if (!IEM_IS_IN_GUEST(pVCpu))
|
---|
5213 | { /* probable */ }
|
---|
5214 | else if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
5215 | && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
|
---|
5216 | {
|
---|
5217 | Log(("lgdt: Guest intercept -> VM-exit\n"));
|
---|
5218 | IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_GDTR_IDTR_ACCESS, VMXINSTRID_LGDT, cbInstr);
|
---|
5219 | }
|
---|
5220 | else if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_GDTR_WRITES))
|
---|
5221 | {
|
---|
5222 | Log(("lgdt: Guest intercept -> #VMEXIT\n"));
|
---|
5223 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
5224 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_GDTR_WRITE, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
5225 | }
|
---|
5226 |
|
---|
5227 | /*
|
---|
5228 | * Fetch the limit and base address.
|
---|
5229 | */
|
---|
5230 | uint16_t cbLimit;
|
---|
5231 | RTGCPTR GCPtrBase;
|
---|
5232 | VBOXSTRICTRC rcStrict = iemMemFetchDataXdtr(pVCpu, &cbLimit, &GCPtrBase, iEffSeg, GCPtrEffSrc, enmEffOpSize);
|
---|
5233 | if (rcStrict == VINF_SUCCESS)
|
---|
5234 | {
|
---|
5235 | if ( !IEM_IS_64BIT_CODE(pVCpu)
|
---|
5236 | || X86_IS_CANONICAL(GCPtrBase))
|
---|
5237 | {
|
---|
5238 | rcStrict = CPUMSetGuestGDTR(pVCpu, GCPtrBase, cbLimit);
|
---|
5239 | if (rcStrict == VINF_SUCCESS)
|
---|
5240 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5241 | }
|
---|
5242 | else
|
---|
5243 | {
|
---|
5244 | Log(("iemCImpl_lgdt: Non-canonical base %04x:%RGv\n", cbLimit, GCPtrBase));
|
---|
5245 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5246 | }
|
---|
5247 | }
|
---|
5248 | return rcStrict;
|
---|
5249 | }
|
---|
5250 |
|
---|
5251 |
|
---|
5252 | /**
|
---|
5253 | * Implements sgdt.
|
---|
5254 | *
|
---|
5255 | * @param iEffSeg The segment where to store the gdtr content.
|
---|
5256 | * @param GCPtrEffDst The address where to store the gdtr content.
|
---|
5257 | */
|
---|
5258 | IEM_CIMPL_DEF_2(iemCImpl_sgdt, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
|
---|
5259 | {
|
---|
5260 | /*
|
---|
5261 | * Join paths with sidt.
|
---|
5262 | * Note! No CPL or V8086 checks here, it's a really sad story, ask Intel if
|
---|
5263 | * you really must know.
|
---|
5264 | */
|
---|
5265 | if (!IEM_IS_IN_GUEST(pVCpu))
|
---|
5266 | { /* probable */ }
|
---|
5267 | else if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
5268 | && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
|
---|
5269 | {
|
---|
5270 | Log(("sgdt: Guest intercept -> VM-exit\n"));
|
---|
5271 | IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_GDTR_IDTR_ACCESS, VMXINSTRID_SGDT, cbInstr);
|
---|
5272 | }
|
---|
5273 | else if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_GDTR_READS))
|
---|
5274 | {
|
---|
5275 | Log(("sgdt: Guest intercept -> #VMEXIT\n"));
|
---|
5276 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
5277 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_GDTR_READ, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
5278 | }
|
---|
5279 |
|
---|
5280 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_GDTR);
|
---|
5281 | VBOXSTRICTRC rcStrict = iemMemStoreDataXdtr(pVCpu, pVCpu->cpum.GstCtx.gdtr.cbGdt, pVCpu->cpum.GstCtx.gdtr.pGdt, iEffSeg, GCPtrEffDst);
|
---|
5282 | if (rcStrict == VINF_SUCCESS)
|
---|
5283 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5284 | return rcStrict;
|
---|
5285 | }
|
---|
5286 |
|
---|
5287 |
|
---|
5288 | /**
|
---|
5289 | * Implements lidt.
|
---|
5290 | *
|
---|
5291 | * @param iEffSeg The segment of the new idtr contents
|
---|
5292 | * @param GCPtrEffSrc The address of the new idtr contents.
|
---|
5293 | * @param enmEffOpSize The effective operand size.
|
---|
5294 | */
|
---|
5295 | IEM_CIMPL_DEF_3(iemCImpl_lidt, uint8_t, iEffSeg, RTGCPTR, GCPtrEffSrc, IEMMODE, enmEffOpSize)
|
---|
5296 | {
|
---|
5297 | if (IEM_GET_CPL(pVCpu) != 0)
|
---|
5298 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5299 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
5300 |
|
---|
5301 | if (!IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_IDTR_WRITES))
|
---|
5302 | { /* probable */ }
|
---|
5303 | else
|
---|
5304 | {
|
---|
5305 | Log(("lidt: Guest intercept -> #VMEXIT\n"));
|
---|
5306 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
5307 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_IDTR_WRITE, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
5308 | }
|
---|
5309 |
|
---|
5310 | /*
|
---|
5311 | * Fetch the limit and base address.
|
---|
5312 | */
|
---|
5313 | uint16_t cbLimit;
|
---|
5314 | RTGCPTR GCPtrBase;
|
---|
5315 | VBOXSTRICTRC rcStrict = iemMemFetchDataXdtr(pVCpu, &cbLimit, &GCPtrBase, iEffSeg, GCPtrEffSrc, enmEffOpSize);
|
---|
5316 | if (rcStrict == VINF_SUCCESS)
|
---|
5317 | {
|
---|
5318 | if ( !IEM_IS_64BIT_CODE(pVCpu)
|
---|
5319 | || X86_IS_CANONICAL(GCPtrBase))
|
---|
5320 | {
|
---|
5321 | CPUMSetGuestIDTR(pVCpu, GCPtrBase, cbLimit);
|
---|
5322 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5323 | }
|
---|
5324 | else
|
---|
5325 | {
|
---|
5326 | Log(("iemCImpl_lidt: Non-canonical base %04x:%RGv\n", cbLimit, GCPtrBase));
|
---|
5327 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5328 | }
|
---|
5329 | }
|
---|
5330 | return rcStrict;
|
---|
5331 | }
|
---|
5332 |
|
---|
5333 |
|
---|
5334 | /**
|
---|
5335 | * Implements sidt.
|
---|
5336 | *
|
---|
5337 | * @param iEffSeg The segment where to store the idtr content.
|
---|
5338 | * @param GCPtrEffDst The address where to store the idtr content.
|
---|
5339 | */
|
---|
5340 | IEM_CIMPL_DEF_2(iemCImpl_sidt, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
|
---|
5341 | {
|
---|
5342 | /*
|
---|
5343 | * Join paths with sgdt.
|
---|
5344 | * Note! No CPL or V8086 checks here, it's a really sad story, ask Intel if
|
---|
5345 | * you really must know.
|
---|
5346 | */
|
---|
5347 | if (!IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_IDTR_READS))
|
---|
5348 | { /* probable */ }
|
---|
5349 | else
|
---|
5350 | {
|
---|
5351 | Log(("sidt: Guest intercept -> #VMEXIT\n"));
|
---|
5352 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
5353 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_IDTR_READ, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
5354 | }
|
---|
5355 |
|
---|
5356 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_IDTR);
|
---|
5357 | VBOXSTRICTRC rcStrict = iemMemStoreDataXdtr(pVCpu, pVCpu->cpum.GstCtx.idtr.cbIdt, pVCpu->cpum.GstCtx.idtr.pIdt, iEffSeg, GCPtrEffDst);
|
---|
5358 | if (rcStrict == VINF_SUCCESS)
|
---|
5359 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5360 | return rcStrict;
|
---|
5361 | }
|
---|
5362 |
|
---|
5363 |
|
---|
5364 | /**
|
---|
5365 | * Implements lldt.
|
---|
5366 | *
|
---|
5367 | * @param uNewLdt The new LDT selector value.
|
---|
5368 | */
|
---|
5369 | IEM_CIMPL_DEF_1(iemCImpl_lldt, uint16_t, uNewLdt)
|
---|
5370 | {
|
---|
5371 | /*
|
---|
5372 | * Check preconditions.
|
---|
5373 | */
|
---|
5374 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
5375 | {
|
---|
5376 | Log(("lldt %04x - real or v8086 mode -> #GP(0)\n", uNewLdt));
|
---|
5377 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
5378 | }
|
---|
5379 | if (IEM_GET_CPL(pVCpu) != 0)
|
---|
5380 | {
|
---|
5381 | Log(("lldt %04x - CPL is %d -> #GP(0)\n", uNewLdt, IEM_GET_CPL(pVCpu)));
|
---|
5382 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5383 | }
|
---|
5384 |
|
---|
5385 | /* Nested-guest VMX intercept (SVM is after all checks). */
|
---|
5386 | /** @todo testcase: exit vs check order. */
|
---|
5387 | if ( !IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
5388 | || !IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
|
---|
5389 | { /* probable */ }
|
---|
5390 | else
|
---|
5391 | {
|
---|
5392 | Log(("lldt: Guest intercept -> VM-exit\n"));
|
---|
5393 | IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_LDTR_TR_ACCESS, VMXINSTRID_LLDT, cbInstr);
|
---|
5394 | }
|
---|
5395 |
|
---|
5396 | if (uNewLdt & X86_SEL_LDT)
|
---|
5397 | {
|
---|
5398 | Log(("lldt %04x - LDT selector -> #GP\n", uNewLdt));
|
---|
5399 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewLdt);
|
---|
5400 | }
|
---|
5401 |
|
---|
5402 | /*
|
---|
5403 | * Now, loading a NULL selector is easy.
|
---|
5404 | */
|
---|
5405 | if (!(uNewLdt & X86_SEL_MASK_OFF_RPL))
|
---|
5406 | {
|
---|
5407 | /* Nested-guest SVM intercept. */
|
---|
5408 | if (!IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_LDTR_WRITES))
|
---|
5409 | { /* probable */ }
|
---|
5410 | else
|
---|
5411 | {
|
---|
5412 | Log(("lldt: Guest intercept -> #VMEXIT\n"));
|
---|
5413 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
5414 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_LDTR_WRITE, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
5415 | }
|
---|
5416 |
|
---|
5417 | Log(("lldt %04x: Loading NULL selector.\n", uNewLdt));
|
---|
5418 | pVCpu->cpum.GstCtx.fExtrn &= ~CPUMCTX_EXTRN_LDTR;
|
---|
5419 | CPUMSetGuestLDTR(pVCpu, uNewLdt);
|
---|
5420 | pVCpu->cpum.GstCtx.ldtr.ValidSel = uNewLdt;
|
---|
5421 | pVCpu->cpum.GstCtx.ldtr.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
5422 | if (IEM_IS_GUEST_CPU_AMD(pVCpu))
|
---|
5423 | {
|
---|
5424 | /* AMD-V seems to leave the base and limit alone. */
|
---|
5425 | pVCpu->cpum.GstCtx.ldtr.Attr.u = X86DESCATTR_UNUSABLE;
|
---|
5426 | }
|
---|
5427 | else
|
---|
5428 | {
|
---|
5429 | /* VT-x (Intel 3960x) seems to be doing the following. */
|
---|
5430 | pVCpu->cpum.GstCtx.ldtr.Attr.u = X86DESCATTR_UNUSABLE | X86DESCATTR_G | X86DESCATTR_D;
|
---|
5431 | pVCpu->cpum.GstCtx.ldtr.u64Base = 0;
|
---|
5432 | pVCpu->cpum.GstCtx.ldtr.u32Limit = UINT32_MAX;
|
---|
5433 | }
|
---|
5434 |
|
---|
5435 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5436 | }
|
---|
5437 |
|
---|
5438 | /*
|
---|
5439 | * Read the descriptor.
|
---|
5440 | */
|
---|
5441 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_LDTR | CPUMCTX_EXTRN_GDTR);
|
---|
5442 | IEMSELDESC Desc;
|
---|
5443 | VBOXSTRICTRC rcStrict = iemMemFetchSelDesc(pVCpu, &Desc, uNewLdt, X86_XCPT_GP); /** @todo Correct exception? */
|
---|
5444 | if (rcStrict != VINF_SUCCESS)
|
---|
5445 | return rcStrict;
|
---|
5446 |
|
---|
5447 | /* Check GPs first. */
|
---|
5448 | if (Desc.Legacy.Gen.u1DescType)
|
---|
5449 | {
|
---|
5450 | Log(("lldt %#x - not system selector (type %x) -> #GP\n", uNewLdt, Desc.Legacy.Gen.u4Type));
|
---|
5451 | return iemRaiseGeneralProtectionFault(pVCpu, uNewLdt & X86_SEL_MASK_OFF_RPL);
|
---|
5452 | }
|
---|
5453 | if (Desc.Legacy.Gen.u4Type != X86_SEL_TYPE_SYS_LDT)
|
---|
5454 | {
|
---|
5455 | Log(("lldt %#x - not LDT selector (type %x) -> #GP\n", uNewLdt, Desc.Legacy.Gen.u4Type));
|
---|
5456 | return iemRaiseGeneralProtectionFault(pVCpu, uNewLdt & X86_SEL_MASK_OFF_RPL);
|
---|
5457 | }
|
---|
5458 | uint64_t u64Base;
|
---|
5459 | if (!IEM_IS_LONG_MODE(pVCpu))
|
---|
5460 | u64Base = X86DESC_BASE(&Desc.Legacy);
|
---|
5461 | else
|
---|
5462 | {
|
---|
5463 | if (Desc.Long.Gen.u5Zeros)
|
---|
5464 | {
|
---|
5465 | Log(("lldt %#x - u5Zeros=%#x -> #GP\n", uNewLdt, Desc.Long.Gen.u5Zeros));
|
---|
5466 | return iemRaiseGeneralProtectionFault(pVCpu, uNewLdt & X86_SEL_MASK_OFF_RPL);
|
---|
5467 | }
|
---|
5468 |
|
---|
5469 | u64Base = X86DESC64_BASE(&Desc.Long);
|
---|
5470 | if (!IEM_IS_CANONICAL(u64Base))
|
---|
5471 | {
|
---|
5472 | Log(("lldt %#x - non-canonical base address %#llx -> #GP\n", uNewLdt, u64Base));
|
---|
5473 | return iemRaiseGeneralProtectionFault(pVCpu, uNewLdt & X86_SEL_MASK_OFF_RPL);
|
---|
5474 | }
|
---|
5475 | }
|
---|
5476 |
|
---|
5477 | /* NP */
|
---|
5478 | if (!Desc.Legacy.Gen.u1Present)
|
---|
5479 | {
|
---|
5480 | Log(("lldt %#x - segment not present -> #NP\n", uNewLdt));
|
---|
5481 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uNewLdt);
|
---|
5482 | }
|
---|
5483 |
|
---|
5484 | /* Nested-guest SVM intercept. */
|
---|
5485 | if (!IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_LDTR_WRITES))
|
---|
5486 | { /* probable */ }
|
---|
5487 | else
|
---|
5488 | {
|
---|
5489 | Log(("lldt: Guest intercept -> #VMEXIT\n"));
|
---|
5490 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
5491 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_LDTR_WRITE, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
5492 | }
|
---|
5493 |
|
---|
5494 | /*
|
---|
5495 | * It checks out alright, update the registers.
|
---|
5496 | */
|
---|
5497 | /** @todo check if the actual value is loaded or if the RPL is dropped */
|
---|
5498 | CPUMSetGuestLDTR(pVCpu, uNewLdt & X86_SEL_MASK_OFF_RPL);
|
---|
5499 | pVCpu->cpum.GstCtx.ldtr.ValidSel = uNewLdt & X86_SEL_MASK_OFF_RPL;
|
---|
5500 | pVCpu->cpum.GstCtx.ldtr.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
5501 | pVCpu->cpum.GstCtx.ldtr.Attr.u = X86DESC_GET_HID_ATTR(&Desc.Legacy);
|
---|
5502 | pVCpu->cpum.GstCtx.ldtr.u32Limit = X86DESC_LIMIT_G(&Desc.Legacy);
|
---|
5503 | pVCpu->cpum.GstCtx.ldtr.u64Base = u64Base;
|
---|
5504 |
|
---|
5505 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5506 | }
|
---|
5507 |
|
---|
5508 |
|
---|
5509 | /**
|
---|
5510 | * Implements sldt GReg
|
---|
5511 | *
|
---|
5512 | * @param iGReg The general register to store the CRx value in.
|
---|
5513 | * @param enmEffOpSize The operand size.
|
---|
5514 | */
|
---|
5515 | IEM_CIMPL_DEF_2(iemCImpl_sldt_reg, uint8_t, iGReg, uint8_t, enmEffOpSize)
|
---|
5516 | {
|
---|
5517 | if (!IEM_IS_IN_GUEST(pVCpu))
|
---|
5518 | { /* probable */ }
|
---|
5519 | else if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
5520 | && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
|
---|
5521 | {
|
---|
5522 | Log(("sldt: Guest intercept -> VM-exit\n"));
|
---|
5523 | IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_LDTR_TR_ACCESS, VMXINSTRID_SLDT, cbInstr);
|
---|
5524 | }
|
---|
5525 | else
|
---|
5526 | IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_LDTR_READS, SVM_EXIT_LDTR_READ, 0, 0, cbInstr);
|
---|
5527 |
|
---|
5528 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_LDTR);
|
---|
5529 | switch (enmEffOpSize)
|
---|
5530 | {
|
---|
5531 | case IEMMODE_16BIT: *(uint16_t *)iemGRegRef(pVCpu, iGReg) = pVCpu->cpum.GstCtx.ldtr.Sel; break;
|
---|
5532 | case IEMMODE_32BIT: *(uint64_t *)iemGRegRef(pVCpu, iGReg) = pVCpu->cpum.GstCtx.ldtr.Sel; break;
|
---|
5533 | case IEMMODE_64BIT: *(uint64_t *)iemGRegRef(pVCpu, iGReg) = pVCpu->cpum.GstCtx.ldtr.Sel; break;
|
---|
5534 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
5535 | }
|
---|
5536 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5537 | }
|
---|
5538 |
|
---|
5539 |
|
---|
5540 | /**
|
---|
5541 | * Implements sldt mem.
|
---|
5542 | *
|
---|
5543 | * @param iEffSeg The effective segment register to use with @a GCPtrMem.
|
---|
5544 | * @param GCPtrEffDst Where to store the 16-bit CR0 value.
|
---|
5545 | */
|
---|
5546 | IEM_CIMPL_DEF_2(iemCImpl_sldt_mem, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
|
---|
5547 | {
|
---|
5548 | IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_LDTR_READS, SVM_EXIT_LDTR_READ, 0, 0, cbInstr);
|
---|
5549 |
|
---|
5550 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_LDTR);
|
---|
5551 | VBOXSTRICTRC rcStrict = iemMemStoreDataU16(pVCpu, iEffSeg, GCPtrEffDst, pVCpu->cpum.GstCtx.ldtr.Sel);
|
---|
5552 | if (rcStrict == VINF_SUCCESS)
|
---|
5553 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5554 | return rcStrict;
|
---|
5555 | }
|
---|
5556 |
|
---|
5557 |
|
---|
5558 | /**
|
---|
5559 | * Implements ltr.
|
---|
5560 | *
|
---|
5561 | * @param uNewTr The new TSS selector value.
|
---|
5562 | */
|
---|
5563 | IEM_CIMPL_DEF_1(iemCImpl_ltr, uint16_t, uNewTr)
|
---|
5564 | {
|
---|
5565 | /*
|
---|
5566 | * Check preconditions.
|
---|
5567 | */
|
---|
5568 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
5569 | {
|
---|
5570 | Log(("ltr %04x - real or v8086 mode -> #GP(0)\n", uNewTr));
|
---|
5571 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
5572 | }
|
---|
5573 | if (IEM_GET_CPL(pVCpu) != 0)
|
---|
5574 | {
|
---|
5575 | Log(("ltr %04x - CPL is %d -> #GP(0)\n", uNewTr, IEM_GET_CPL(pVCpu)));
|
---|
5576 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5577 | }
|
---|
5578 | if ( !IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
5579 | || !IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
|
---|
5580 | { /* probable */ }
|
---|
5581 | else
|
---|
5582 | {
|
---|
5583 | Log(("ltr: Guest intercept -> VM-exit\n"));
|
---|
5584 | IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_LDTR_TR_ACCESS, VMXINSTRID_LTR, cbInstr);
|
---|
5585 | }
|
---|
5586 | if (uNewTr & X86_SEL_LDT)
|
---|
5587 | {
|
---|
5588 | Log(("ltr %04x - LDT selector -> #GP\n", uNewTr));
|
---|
5589 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewTr);
|
---|
5590 | }
|
---|
5591 | if (!(uNewTr & X86_SEL_MASK_OFF_RPL))
|
---|
5592 | {
|
---|
5593 | Log(("ltr %04x - NULL selector -> #GP(0)\n", uNewTr));
|
---|
5594 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5595 | }
|
---|
5596 | if (!IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_TR_WRITES))
|
---|
5597 | { /* probable */ }
|
---|
5598 | else
|
---|
5599 | {
|
---|
5600 | Log(("ltr: Guest intercept -> #VMEXIT\n"));
|
---|
5601 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
5602 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_TR_WRITE, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
5603 | }
|
---|
5604 |
|
---|
5605 | /*
|
---|
5606 | * Read the descriptor.
|
---|
5607 | */
|
---|
5608 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_LDTR | CPUMCTX_EXTRN_GDTR | CPUMCTX_EXTRN_TR);
|
---|
5609 | IEMSELDESC Desc;
|
---|
5610 | VBOXSTRICTRC rcStrict = iemMemFetchSelDesc(pVCpu, &Desc, uNewTr, X86_XCPT_GP); /** @todo Correct exception? */
|
---|
5611 | if (rcStrict != VINF_SUCCESS)
|
---|
5612 | return rcStrict;
|
---|
5613 |
|
---|
5614 | /* Check GPs first. */
|
---|
5615 | if (Desc.Legacy.Gen.u1DescType)
|
---|
5616 | {
|
---|
5617 | Log(("ltr %#x - not system selector (type %x) -> #GP\n", uNewTr, Desc.Legacy.Gen.u4Type));
|
---|
5618 | return iemRaiseGeneralProtectionFault(pVCpu, uNewTr & X86_SEL_MASK_OFF_RPL);
|
---|
5619 | }
|
---|
5620 | if ( Desc.Legacy.Gen.u4Type != X86_SEL_TYPE_SYS_386_TSS_AVAIL /* same as AMD64_SEL_TYPE_SYS_TSS_AVAIL */
|
---|
5621 | && ( Desc.Legacy.Gen.u4Type != X86_SEL_TYPE_SYS_286_TSS_AVAIL
|
---|
5622 | || IEM_IS_LONG_MODE(pVCpu)) )
|
---|
5623 | {
|
---|
5624 | Log(("ltr %#x - not an available TSS selector (type %x) -> #GP\n", uNewTr, Desc.Legacy.Gen.u4Type));
|
---|
5625 | return iemRaiseGeneralProtectionFault(pVCpu, uNewTr & X86_SEL_MASK_OFF_RPL);
|
---|
5626 | }
|
---|
5627 | uint64_t u64Base;
|
---|
5628 | if (!IEM_IS_LONG_MODE(pVCpu))
|
---|
5629 | u64Base = X86DESC_BASE(&Desc.Legacy);
|
---|
5630 | else
|
---|
5631 | {
|
---|
5632 | if (Desc.Long.Gen.u5Zeros)
|
---|
5633 | {
|
---|
5634 | Log(("ltr %#x - u5Zeros=%#x -> #GP\n", uNewTr, Desc.Long.Gen.u5Zeros));
|
---|
5635 | return iemRaiseGeneralProtectionFault(pVCpu, uNewTr & X86_SEL_MASK_OFF_RPL);
|
---|
5636 | }
|
---|
5637 |
|
---|
5638 | u64Base = X86DESC64_BASE(&Desc.Long);
|
---|
5639 | if (!IEM_IS_CANONICAL(u64Base))
|
---|
5640 | {
|
---|
5641 | Log(("ltr %#x - non-canonical base address %#llx -> #GP\n", uNewTr, u64Base));
|
---|
5642 | return iemRaiseGeneralProtectionFault(pVCpu, uNewTr & X86_SEL_MASK_OFF_RPL);
|
---|
5643 | }
|
---|
5644 | }
|
---|
5645 |
|
---|
5646 | /* NP */
|
---|
5647 | if (!Desc.Legacy.Gen.u1Present)
|
---|
5648 | {
|
---|
5649 | Log(("ltr %#x - segment not present -> #NP\n", uNewTr));
|
---|
5650 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uNewTr);
|
---|
5651 | }
|
---|
5652 |
|
---|
5653 | /*
|
---|
5654 | * Set it busy.
|
---|
5655 | * Note! Intel says this should lock down the whole descriptor, but we'll
|
---|
5656 | * restrict our selves to 32-bit for now due to lack of inline
|
---|
5657 | * assembly and such.
|
---|
5658 | */
|
---|
5659 | void *pvDesc;
|
---|
5660 | rcStrict = iemMemMap(pVCpu, &pvDesc, 8, UINT8_MAX, pVCpu->cpum.GstCtx.gdtr.pGdt + (uNewTr & X86_SEL_MASK_OFF_RPL),
|
---|
5661 | IEM_ACCESS_DATA_RW, 0);
|
---|
5662 | if (rcStrict != VINF_SUCCESS)
|
---|
5663 | return rcStrict;
|
---|
5664 | switch ((uintptr_t)pvDesc & 3)
|
---|
5665 | {
|
---|
5666 | case 0: ASMAtomicBitSet(pvDesc, 40 + 1); break;
|
---|
5667 | case 1: ASMAtomicBitSet((uint8_t *)pvDesc + 3, 40 + 1 - 24); break;
|
---|
5668 | case 2: ASMAtomicBitSet((uint8_t *)pvDesc + 2, 40 + 1 - 16); break;
|
---|
5669 | case 3: ASMAtomicBitSet((uint8_t *)pvDesc + 1, 40 + 1 - 8); break;
|
---|
5670 | }
|
---|
5671 | rcStrict = iemMemCommitAndUnmap(pVCpu, pvDesc, IEM_ACCESS_DATA_RW);
|
---|
5672 | if (rcStrict != VINF_SUCCESS)
|
---|
5673 | return rcStrict;
|
---|
5674 | Desc.Legacy.Gen.u4Type |= X86_SEL_TYPE_SYS_TSS_BUSY_MASK;
|
---|
5675 |
|
---|
5676 | /*
|
---|
5677 | * It checks out alright, update the registers.
|
---|
5678 | */
|
---|
5679 | /** @todo check if the actual value is loaded or if the RPL is dropped */
|
---|
5680 | CPUMSetGuestTR(pVCpu, uNewTr & X86_SEL_MASK_OFF_RPL);
|
---|
5681 | pVCpu->cpum.GstCtx.tr.ValidSel = uNewTr & X86_SEL_MASK_OFF_RPL;
|
---|
5682 | pVCpu->cpum.GstCtx.tr.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
5683 | pVCpu->cpum.GstCtx.tr.Attr.u = X86DESC_GET_HID_ATTR(&Desc.Legacy);
|
---|
5684 | pVCpu->cpum.GstCtx.tr.u32Limit = X86DESC_LIMIT_G(&Desc.Legacy);
|
---|
5685 | pVCpu->cpum.GstCtx.tr.u64Base = u64Base;
|
---|
5686 |
|
---|
5687 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5688 | }
|
---|
5689 |
|
---|
5690 |
|
---|
5691 | /**
|
---|
5692 | * Implements str GReg
|
---|
5693 | *
|
---|
5694 | * @param iGReg The general register to store the CRx value in.
|
---|
5695 | * @param enmEffOpSize The operand size.
|
---|
5696 | */
|
---|
5697 | IEM_CIMPL_DEF_2(iemCImpl_str_reg, uint8_t, iGReg, uint8_t, enmEffOpSize)
|
---|
5698 | {
|
---|
5699 | if (!IEM_IS_IN_GUEST(pVCpu))
|
---|
5700 | { /* probable */ }
|
---|
5701 | else if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
5702 | && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
|
---|
5703 | {
|
---|
5704 | Log(("str_reg: Guest intercept -> VM-exit\n"));
|
---|
5705 | IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_LDTR_TR_ACCESS, VMXINSTRID_STR, cbInstr);
|
---|
5706 | }
|
---|
5707 | else
|
---|
5708 | IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_TR_READS, SVM_EXIT_TR_READ, 0, 0, cbInstr);
|
---|
5709 |
|
---|
5710 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_TR);
|
---|
5711 | switch (enmEffOpSize)
|
---|
5712 | {
|
---|
5713 | case IEMMODE_16BIT: *(uint16_t *)iemGRegRef(pVCpu, iGReg) = pVCpu->cpum.GstCtx.tr.Sel; break;
|
---|
5714 | case IEMMODE_32BIT: *(uint64_t *)iemGRegRef(pVCpu, iGReg) = pVCpu->cpum.GstCtx.tr.Sel; break;
|
---|
5715 | case IEMMODE_64BIT: *(uint64_t *)iemGRegRef(pVCpu, iGReg) = pVCpu->cpum.GstCtx.tr.Sel; break;
|
---|
5716 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
5717 | }
|
---|
5718 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5719 | }
|
---|
5720 |
|
---|
5721 |
|
---|
5722 | /**
|
---|
5723 | * Implements str mem.
|
---|
5724 | *
|
---|
5725 | * @param iEffSeg The effective segment register to use with @a GCPtrMem.
|
---|
5726 | * @param GCPtrEffDst Where to store the 16-bit CR0 value.
|
---|
5727 | */
|
---|
5728 | IEM_CIMPL_DEF_2(iemCImpl_str_mem, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
|
---|
5729 | {
|
---|
5730 | if (!IEM_IS_IN_GUEST(pVCpu))
|
---|
5731 | { /* probable */ }
|
---|
5732 | else if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
5733 | && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
|
---|
5734 | {
|
---|
5735 | Log(("str_mem: Guest intercept -> VM-exit\n"));
|
---|
5736 | IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_LDTR_TR_ACCESS, VMXINSTRID_STR, cbInstr);
|
---|
5737 | }
|
---|
5738 | else
|
---|
5739 | IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_TR_READS, SVM_EXIT_TR_READ, 0, 0, cbInstr);
|
---|
5740 |
|
---|
5741 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_TR);
|
---|
5742 | VBOXSTRICTRC rcStrict = iemMemStoreDataU16(pVCpu, iEffSeg, GCPtrEffDst, pVCpu->cpum.GstCtx.tr.Sel);
|
---|
5743 | if (rcStrict == VINF_SUCCESS)
|
---|
5744 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5745 | return rcStrict;
|
---|
5746 | }
|
---|
5747 |
|
---|
5748 |
|
---|
5749 | /**
|
---|
5750 | * Implements mov GReg,CRx.
|
---|
5751 | *
|
---|
5752 | * @param iGReg The general register to store the CRx value in.
|
---|
5753 | * @param iCrReg The CRx register to read (valid).
|
---|
5754 | */
|
---|
5755 | IEM_CIMPL_DEF_2(iemCImpl_mov_Rd_Cd, uint8_t, iGReg, uint8_t, iCrReg)
|
---|
5756 | {
|
---|
5757 | if (IEM_GET_CPL(pVCpu) != 0)
|
---|
5758 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5759 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
5760 |
|
---|
5761 | if (!IEM_SVM_IS_READ_CR_INTERCEPT_SET(pVCpu, iCrReg))
|
---|
5762 | { /* probable */ }
|
---|
5763 | else
|
---|
5764 | {
|
---|
5765 | Log(("iemCImpl_mov_Rd_Cd: Guest intercept CR%u -> #VMEXIT\n", iCrReg));
|
---|
5766 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
5767 | IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_READ_CR0 + iCrReg, IEMACCESSCRX_MOV_CRX, iGReg);
|
---|
5768 | }
|
---|
5769 |
|
---|
5770 | /* Read it. */
|
---|
5771 | uint64_t crX;
|
---|
5772 | switch (iCrReg)
|
---|
5773 | {
|
---|
5774 | case 0:
|
---|
5775 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0);
|
---|
5776 | crX = pVCpu->cpum.GstCtx.cr0;
|
---|
5777 | if (IEM_GET_TARGET_CPU(pVCpu) <= IEMTARGETCPU_386)
|
---|
5778 | crX |= UINT32_C(0x7fffffe0); /* All reserved CR0 flags are set on a 386, just like MSW on 286. */
|
---|
5779 | break;
|
---|
5780 | case 2:
|
---|
5781 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_CR2);
|
---|
5782 | crX = pVCpu->cpum.GstCtx.cr2;
|
---|
5783 | break;
|
---|
5784 | case 3:
|
---|
5785 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR3);
|
---|
5786 | crX = pVCpu->cpum.GstCtx.cr3;
|
---|
5787 | break;
|
---|
5788 | case 4:
|
---|
5789 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR4);
|
---|
5790 | crX = pVCpu->cpum.GstCtx.cr4;
|
---|
5791 | break;
|
---|
5792 | case 8:
|
---|
5793 | {
|
---|
5794 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_APIC_TPR);
|
---|
5795 | if (!IEM_IS_IN_GUEST(pVCpu))
|
---|
5796 | { /* probable */ }
|
---|
5797 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
5798 | else if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
5799 | {
|
---|
5800 | VBOXSTRICTRC rcStrict = iemVmxVmexitInstrMovFromCr8(pVCpu, iGReg, cbInstr);
|
---|
5801 | if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
|
---|
5802 | return rcStrict;
|
---|
5803 |
|
---|
5804 | /*
|
---|
5805 | * If the Mov-from-CR8 doesn't cause a VM-exit, bits 7:4 of the VTPR is copied
|
---|
5806 | * to bits 0:3 of the destination operand. Bits 63:4 of the destination operand
|
---|
5807 | * are cleared.
|
---|
5808 | *
|
---|
5809 | * See Intel Spec. 29.3 "Virtualizing CR8-based TPR Accesses"
|
---|
5810 | */
|
---|
5811 | if (IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_USE_TPR_SHADOW))
|
---|
5812 | {
|
---|
5813 | uint32_t const uTpr = iemVmxVirtApicReadRaw32(pVCpu, XAPIC_OFF_TPR);
|
---|
5814 | crX = (uTpr >> 4) & 0xf;
|
---|
5815 | break;
|
---|
5816 | }
|
---|
5817 | }
|
---|
5818 | #endif
|
---|
5819 | #ifdef VBOX_WITH_NESTED_HWVIRT_SVM
|
---|
5820 | else if (pVCpu->iem.s.fExec & IEM_F_X86_CTX_SVM)
|
---|
5821 | {
|
---|
5822 | PCSVMVMCBCTRL pVmcbCtrl = &pVCpu->cpum.GstCtx.hwvirt.svm.Vmcb.ctrl;
|
---|
5823 | if (CPUMIsGuestSvmVirtIntrMasking(pVCpu, IEM_GET_CTX(pVCpu)))
|
---|
5824 | {
|
---|
5825 | crX = pVmcbCtrl->IntCtrl.n.u8VTPR & 0xf;
|
---|
5826 | break;
|
---|
5827 | }
|
---|
5828 | }
|
---|
5829 | #endif
|
---|
5830 | uint8_t uTpr;
|
---|
5831 | int rc = APICGetTpr(pVCpu, &uTpr, NULL, NULL);
|
---|
5832 | if (RT_SUCCESS(rc))
|
---|
5833 | crX = uTpr >> 4;
|
---|
5834 | else
|
---|
5835 | crX = 0;
|
---|
5836 | break;
|
---|
5837 | }
|
---|
5838 | IEM_NOT_REACHED_DEFAULT_CASE_RET(); /* call checks */
|
---|
5839 | }
|
---|
5840 |
|
---|
5841 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
5842 | if (!IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
5843 | { /* probable */ }
|
---|
5844 | else
|
---|
5845 | switch (iCrReg)
|
---|
5846 | {
|
---|
5847 | /* CR0/CR4 reads are subject to masking when in VMX non-root mode. */
|
---|
5848 | case 0: crX = CPUMGetGuestVmxMaskedCr0(&pVCpu->cpum.GstCtx, pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64Cr0Mask.u); break;
|
---|
5849 | case 4: crX = CPUMGetGuestVmxMaskedCr4(&pVCpu->cpum.GstCtx, pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64Cr4Mask.u); break;
|
---|
5850 | case 3:
|
---|
5851 | {
|
---|
5852 | VBOXSTRICTRC rcStrict = iemVmxVmexitInstrMovFromCr3(pVCpu, iGReg, cbInstr);
|
---|
5853 | if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
|
---|
5854 | return rcStrict;
|
---|
5855 | break;
|
---|
5856 | }
|
---|
5857 | }
|
---|
5858 | #endif
|
---|
5859 |
|
---|
5860 | /* Store it. */
|
---|
5861 | if (IEM_IS_64BIT_CODE(pVCpu))
|
---|
5862 | *(uint64_t *)iemGRegRef(pVCpu, iGReg) = crX;
|
---|
5863 | else
|
---|
5864 | *(uint64_t *)iemGRegRef(pVCpu, iGReg) = (uint32_t)crX;
|
---|
5865 |
|
---|
5866 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5867 | }
|
---|
5868 |
|
---|
5869 |
|
---|
5870 | /**
|
---|
5871 | * Implements smsw GReg.
|
---|
5872 | *
|
---|
5873 | * @param iGReg The general register to store the CRx value in.
|
---|
5874 | * @param enmEffOpSize The operand size.
|
---|
5875 | */
|
---|
5876 | IEM_CIMPL_DEF_2(iemCImpl_smsw_reg, uint8_t, iGReg, uint8_t, enmEffOpSize)
|
---|
5877 | {
|
---|
5878 | IEM_SVM_CHECK_READ_CR0_INTERCEPT(pVCpu, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */, cbInstr);
|
---|
5879 |
|
---|
5880 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
5881 | uint64_t u64MaskedCr0;
|
---|
5882 | if (!IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
5883 | u64MaskedCr0 = pVCpu->cpum.GstCtx.cr0;
|
---|
5884 | else
|
---|
5885 | u64MaskedCr0 = CPUMGetGuestVmxMaskedCr0(&pVCpu->cpum.GstCtx, pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64Cr0Mask.u);
|
---|
5886 | uint64_t const u64GuestCr0 = u64MaskedCr0;
|
---|
5887 | #else
|
---|
5888 | uint64_t const u64GuestCr0 = pVCpu->cpum.GstCtx.cr0;
|
---|
5889 | #endif
|
---|
5890 |
|
---|
5891 | switch (enmEffOpSize)
|
---|
5892 | {
|
---|
5893 | case IEMMODE_16BIT:
|
---|
5894 | if (IEM_GET_TARGET_CPU(pVCpu) > IEMTARGETCPU_386)
|
---|
5895 | *(uint16_t *)iemGRegRef(pVCpu, iGReg) = (uint16_t)u64GuestCr0;
|
---|
5896 | else if (IEM_GET_TARGET_CPU(pVCpu) >= IEMTARGETCPU_386)
|
---|
5897 | *(uint16_t *)iemGRegRef(pVCpu, iGReg) = (uint16_t)u64GuestCr0 | 0xffe0;
|
---|
5898 | else
|
---|
5899 | *(uint16_t *)iemGRegRef(pVCpu, iGReg) = (uint16_t)u64GuestCr0 | 0xfff0;
|
---|
5900 | break;
|
---|
5901 |
|
---|
5902 | case IEMMODE_32BIT:
|
---|
5903 | *(uint32_t *)iemGRegRef(pVCpu, iGReg) = (uint32_t)u64GuestCr0;
|
---|
5904 | break;
|
---|
5905 |
|
---|
5906 | case IEMMODE_64BIT:
|
---|
5907 | *(uint64_t *)iemGRegRef(pVCpu, iGReg) = u64GuestCr0;
|
---|
5908 | break;
|
---|
5909 |
|
---|
5910 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
5911 | }
|
---|
5912 |
|
---|
5913 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5914 | }
|
---|
5915 |
|
---|
5916 |
|
---|
5917 | /**
|
---|
5918 | * Implements smsw mem.
|
---|
5919 | *
|
---|
5920 | * @param iEffSeg The effective segment register to use with @a GCPtrMem.
|
---|
5921 | * @param GCPtrEffDst Where to store the 16-bit CR0 value.
|
---|
5922 | */
|
---|
5923 | IEM_CIMPL_DEF_2(iemCImpl_smsw_mem, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
|
---|
5924 | {
|
---|
5925 | uint64_t u64GuestCr0 = pVCpu->cpum.GstCtx.cr0;
|
---|
5926 | if (!IEM_IS_IN_GUEST(pVCpu))
|
---|
5927 | { /* probable */ }
|
---|
5928 | else if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
5929 | u64GuestCr0 = CPUMGetGuestVmxMaskedCr0(&pVCpu->cpum.GstCtx, pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64Cr0Mask.u);
|
---|
5930 | else
|
---|
5931 | IEM_SVM_CHECK_READ_CR0_INTERCEPT(pVCpu, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */, cbInstr);
|
---|
5932 |
|
---|
5933 | uint16_t u16Value;
|
---|
5934 | if (IEM_GET_TARGET_CPU(pVCpu) > IEMTARGETCPU_386)
|
---|
5935 | u16Value = (uint16_t)u64GuestCr0;
|
---|
5936 | else if (IEM_GET_TARGET_CPU(pVCpu) >= IEMTARGETCPU_386)
|
---|
5937 | u16Value = (uint16_t)u64GuestCr0 | 0xffe0;
|
---|
5938 | else
|
---|
5939 | u16Value = (uint16_t)u64GuestCr0 | 0xfff0;
|
---|
5940 |
|
---|
5941 | VBOXSTRICTRC rcStrict = iemMemStoreDataU16(pVCpu, iEffSeg, GCPtrEffDst, u16Value);
|
---|
5942 | if (rcStrict == VINF_SUCCESS)
|
---|
5943 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
5944 | return rcStrict;
|
---|
5945 | }
|
---|
5946 |
|
---|
5947 |
|
---|
5948 | /**
|
---|
5949 | * Helper for mapping CR3 and PAE PDPEs for 'mov CRx,GReg'.
|
---|
5950 | */
|
---|
5951 | #define IEM_MAP_PAE_PDPES_AT_CR3_RET(a_pVCpu, a_iCrReg, a_uCr3) \
|
---|
5952 | do \
|
---|
5953 | { \
|
---|
5954 | int const rcX = PGMGstMapPaePdpesAtCr3(a_pVCpu, a_uCr3); \
|
---|
5955 | if (RT_SUCCESS(rcX)) \
|
---|
5956 | { /* likely */ } \
|
---|
5957 | else \
|
---|
5958 | { \
|
---|
5959 | /* Either invalid PDPTEs or CR3 second-level translation failed. Raise #GP(0) either way. */ \
|
---|
5960 | Log(("iemCImpl_load_Cr%#x: Trying to load invalid PAE PDPEs\n", a_iCrReg)); \
|
---|
5961 | return iemRaiseGeneralProtectionFault0(a_pVCpu); \
|
---|
5962 | } \
|
---|
5963 | } while (0)
|
---|
5964 |
|
---|
5965 |
|
---|
5966 | /**
|
---|
5967 | * Used to implemented 'mov CRx,GReg' and 'lmsw r/m16'.
|
---|
5968 | *
|
---|
5969 | * @param iCrReg The CRx register to write (valid).
|
---|
5970 | * @param uNewCrX The new value.
|
---|
5971 | * @param enmAccessCrX The instruction that caused the CrX load.
|
---|
5972 | * @param iGReg The general register in case of a 'mov CRx,GReg'
|
---|
5973 | * instruction.
|
---|
5974 | */
|
---|
5975 | IEM_CIMPL_DEF_4(iemCImpl_load_CrX, uint8_t, iCrReg, uint64_t, uNewCrX, IEMACCESSCRX, enmAccessCrX, uint8_t, iGReg)
|
---|
5976 | {
|
---|
5977 | VBOXSTRICTRC rcStrict;
|
---|
5978 | int rc;
|
---|
5979 | #ifndef VBOX_WITH_NESTED_HWVIRT_SVM
|
---|
5980 | RT_NOREF2(iGReg, enmAccessCrX);
|
---|
5981 | #endif
|
---|
5982 |
|
---|
5983 | /*
|
---|
5984 | * Try store it.
|
---|
5985 | * Unfortunately, CPUM only does a tiny bit of the work.
|
---|
5986 | */
|
---|
5987 | switch (iCrReg)
|
---|
5988 | {
|
---|
5989 | case 0:
|
---|
5990 | {
|
---|
5991 | /*
|
---|
5992 | * Perform checks.
|
---|
5993 | */
|
---|
5994 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0);
|
---|
5995 |
|
---|
5996 | uint64_t const uOldCrX = pVCpu->cpum.GstCtx.cr0;
|
---|
5997 | uint32_t const fValid = CPUMGetGuestCR0ValidMask();
|
---|
5998 |
|
---|
5999 | /* ET is hardcoded on 486 and later. */
|
---|
6000 | if (IEM_GET_TARGET_CPU(pVCpu) > IEMTARGETCPU_486)
|
---|
6001 | uNewCrX |= X86_CR0_ET;
|
---|
6002 | /* The 386 and 486 didn't #GP(0) on attempting to set reserved CR0 bits. ET was settable on 386. */
|
---|
6003 | else if (IEM_GET_TARGET_CPU(pVCpu) == IEMTARGETCPU_486)
|
---|
6004 | {
|
---|
6005 | uNewCrX &= fValid;
|
---|
6006 | uNewCrX |= X86_CR0_ET;
|
---|
6007 | }
|
---|
6008 | else
|
---|
6009 | uNewCrX &= X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG | X86_CR0_ET;
|
---|
6010 |
|
---|
6011 | /* Check for reserved bits. */
|
---|
6012 | if (uNewCrX & ~(uint64_t)fValid)
|
---|
6013 | {
|
---|
6014 | Log(("Trying to set reserved CR0 bits: NewCR0=%#llx InvalidBits=%#llx\n", uNewCrX, uNewCrX & ~(uint64_t)fValid));
|
---|
6015 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6016 | }
|
---|
6017 |
|
---|
6018 | /* Check for invalid combinations. */
|
---|
6019 | if ( (uNewCrX & X86_CR0_PG)
|
---|
6020 | && !(uNewCrX & X86_CR0_PE) )
|
---|
6021 | {
|
---|
6022 | Log(("Trying to set CR0.PG without CR0.PE\n"));
|
---|
6023 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6024 | }
|
---|
6025 |
|
---|
6026 | if ( !(uNewCrX & X86_CR0_CD)
|
---|
6027 | && (uNewCrX & X86_CR0_NW) )
|
---|
6028 | {
|
---|
6029 | Log(("Trying to clear CR0.CD while leaving CR0.NW set\n"));
|
---|
6030 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6031 | }
|
---|
6032 |
|
---|
6033 | if ( !(uNewCrX & X86_CR0_PG)
|
---|
6034 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_PCIDE))
|
---|
6035 | {
|
---|
6036 | Log(("Trying to clear CR0.PG while leaving CR4.PCID set\n"));
|
---|
6037 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6038 | }
|
---|
6039 |
|
---|
6040 | /* Long mode consistency checks. */
|
---|
6041 | if ( (uNewCrX & X86_CR0_PG)
|
---|
6042 | && !(uOldCrX & X86_CR0_PG)
|
---|
6043 | && (pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_LME) )
|
---|
6044 | {
|
---|
6045 | if (!(pVCpu->cpum.GstCtx.cr4 & X86_CR4_PAE))
|
---|
6046 | {
|
---|
6047 | Log(("Trying to enabled long mode paging without CR4.PAE set\n"));
|
---|
6048 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6049 | }
|
---|
6050 | if (pVCpu->cpum.GstCtx.cs.Attr.n.u1Long)
|
---|
6051 | {
|
---|
6052 | Log(("Trying to enabled long mode paging with a long CS descriptor loaded.\n"));
|
---|
6053 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6054 | }
|
---|
6055 | }
|
---|
6056 |
|
---|
6057 | /** @todo testcase: what happens if we disable paging while in 64-bit code? */
|
---|
6058 |
|
---|
6059 | if (!IEM_IS_IN_GUEST(pVCpu))
|
---|
6060 | { /* probable */ }
|
---|
6061 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
6062 | /* Check for bits that must remain set or cleared in VMX operation,
|
---|
6063 | see Intel spec. 23.8 "Restrictions on VMX operation". */
|
---|
6064 | else if (IEM_VMX_IS_ROOT_MODE(pVCpu))
|
---|
6065 | {
|
---|
6066 | uint64_t const uCr0Fixed0 = iemVmxGetCr0Fixed0(pVCpu, IEM_VMX_IS_NON_ROOT_MODE(pVCpu));
|
---|
6067 | if ((uNewCrX & uCr0Fixed0) != uCr0Fixed0)
|
---|
6068 | {
|
---|
6069 | Log(("Trying to clear reserved CR0 bits in VMX operation: NewCr0=%#llx MB1=%#llx\n", uNewCrX, uCr0Fixed0));
|
---|
6070 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6071 | }
|
---|
6072 |
|
---|
6073 | uint64_t const uCr0Fixed1 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr0Fixed1;
|
---|
6074 | if (uNewCrX & ~uCr0Fixed1)
|
---|
6075 | {
|
---|
6076 | Log(("Trying to set reserved CR0 bits in VMX operation: NewCr0=%#llx MB0=%#llx\n", uNewCrX, uCr0Fixed1));
|
---|
6077 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6078 | }
|
---|
6079 | }
|
---|
6080 | #endif
|
---|
6081 | /*
|
---|
6082 | * SVM nested-guest CR0 write intercepts.
|
---|
6083 | */
|
---|
6084 | else if (IEM_SVM_IS_WRITE_CR_INTERCEPT_SET(pVCpu, iCrReg))
|
---|
6085 | {
|
---|
6086 | Log(("iemCImpl_load_Cr%#x: Guest intercept -> #VMEXIT\n", iCrReg));
|
---|
6087 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
6088 | IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_WRITE_CR0, enmAccessCrX, iGReg);
|
---|
6089 | }
|
---|
6090 | else if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_CR0_SEL_WRITE))
|
---|
6091 | {
|
---|
6092 | /* 'lmsw' intercepts regardless of whether the TS/MP bits are actually toggled. */
|
---|
6093 | if ( enmAccessCrX == IEMACCESSCRX_LMSW
|
---|
6094 | || (uNewCrX & ~(X86_CR0_TS | X86_CR0_MP)) != (uOldCrX & ~(X86_CR0_TS | X86_CR0_MP)))
|
---|
6095 | {
|
---|
6096 | Assert(enmAccessCrX != IEMACCESSCRX_CLTS);
|
---|
6097 | Log(("iemCImpl_load_Cr%#x: lmsw or bits other than TS/MP changed: Guest intercept -> #VMEXIT\n", iCrReg));
|
---|
6098 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
6099 | IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_CR0_SEL_WRITE, enmAccessCrX, iGReg);
|
---|
6100 | }
|
---|
6101 | }
|
---|
6102 |
|
---|
6103 | /*
|
---|
6104 | * Change EFER.LMA if entering or leaving long mode.
|
---|
6105 | */
|
---|
6106 | uint64_t NewEFER = pVCpu->cpum.GstCtx.msrEFER;
|
---|
6107 | if ( (uNewCrX & X86_CR0_PG) != (uOldCrX & X86_CR0_PG)
|
---|
6108 | && (pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_LME) )
|
---|
6109 | {
|
---|
6110 | if (uNewCrX & X86_CR0_PG)
|
---|
6111 | NewEFER |= MSR_K6_EFER_LMA;
|
---|
6112 | else
|
---|
6113 | NewEFER &= ~MSR_K6_EFER_LMA;
|
---|
6114 |
|
---|
6115 | CPUMSetGuestEFER(pVCpu, NewEFER);
|
---|
6116 | Assert(pVCpu->cpum.GstCtx.msrEFER == NewEFER);
|
---|
6117 | }
|
---|
6118 |
|
---|
6119 | /*
|
---|
6120 | * Inform PGM.
|
---|
6121 | */
|
---|
6122 | if ( (uNewCrX & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE | X86_CR0_CD | X86_CR0_NW))
|
---|
6123 | != (uOldCrX & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE | X86_CR0_CD | X86_CR0_NW)) )
|
---|
6124 | {
|
---|
6125 | if ( enmAccessCrX != IEMACCESSCRX_MOV_CRX
|
---|
6126 | || !CPUMIsPaePagingEnabled(uNewCrX, pVCpu->cpum.GstCtx.cr4, NewEFER)
|
---|
6127 | || CPUMIsGuestInSvmNestedHwVirtMode(IEM_GET_CTX(pVCpu)))
|
---|
6128 | { /* likely */ }
|
---|
6129 | else
|
---|
6130 | IEM_MAP_PAE_PDPES_AT_CR3_RET(pVCpu, iCrReg, pVCpu->cpum.GstCtx.cr3);
|
---|
6131 | rc = PGMFlushTLB(pVCpu, pVCpu->cpum.GstCtx.cr3, true /* global */);
|
---|
6132 | AssertRCReturn(rc, rc);
|
---|
6133 | /* ignore informational status codes */
|
---|
6134 | }
|
---|
6135 |
|
---|
6136 | /*
|
---|
6137 | * Change CR0.
|
---|
6138 | */
|
---|
6139 | CPUMSetGuestCR0(pVCpu, uNewCrX);
|
---|
6140 | Assert(pVCpu->cpum.GstCtx.cr0 == uNewCrX);
|
---|
6141 |
|
---|
6142 | /* Update the fExec flags if PE changed. */
|
---|
6143 | if ((uNewCrX ^ uOldCrX) & X86_CR0_PE)
|
---|
6144 | iemRecalcExecModeAndCplFlags(pVCpu);
|
---|
6145 |
|
---|
6146 | /*
|
---|
6147 | * Inform PGM some more...
|
---|
6148 | */
|
---|
6149 | rcStrict = PGMChangeMode(pVCpu, pVCpu->cpum.GstCtx.cr0, pVCpu->cpum.GstCtx.cr4, pVCpu->cpum.GstCtx.msrEFER,
|
---|
6150 | false /* fForce */);
|
---|
6151 | break;
|
---|
6152 | }
|
---|
6153 |
|
---|
6154 | /*
|
---|
6155 | * CR2 can be changed without any restrictions.
|
---|
6156 | */
|
---|
6157 | case 2:
|
---|
6158 | {
|
---|
6159 | if (!IEM_SVM_IS_WRITE_CR_INTERCEPT_SET(pVCpu, /*cr*/ 2))
|
---|
6160 | { /* probable */ }
|
---|
6161 | else
|
---|
6162 | {
|
---|
6163 | Log(("iemCImpl_load_Cr%#x: Guest intercept -> #VMEXIT\n", iCrReg));
|
---|
6164 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
6165 | IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_WRITE_CR2, enmAccessCrX, iGReg);
|
---|
6166 | }
|
---|
6167 | pVCpu->cpum.GstCtx.cr2 = uNewCrX;
|
---|
6168 | pVCpu->cpum.GstCtx.fExtrn &= ~CPUMCTX_EXTRN_CR2;
|
---|
6169 | rcStrict = VINF_SUCCESS;
|
---|
6170 | break;
|
---|
6171 | }
|
---|
6172 |
|
---|
6173 | /*
|
---|
6174 | * CR3 is relatively simple, although AMD and Intel have different
|
---|
6175 | * accounts of how setting reserved bits are handled. We take intel's
|
---|
6176 | * word for the lower bits and AMD's for the high bits (63:52). The
|
---|
6177 | * lower reserved bits are ignored and left alone; OpenBSD 5.8 relies
|
---|
6178 | * on this.
|
---|
6179 | */
|
---|
6180 | /** @todo Testcase: Setting reserved bits in CR3, especially before
|
---|
6181 | * enabling paging. */
|
---|
6182 | case 3:
|
---|
6183 | {
|
---|
6184 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR3);
|
---|
6185 |
|
---|
6186 | /* Bit 63 being clear in the source operand with PCIDE indicates no invalidations are required. */
|
---|
6187 | if ( (pVCpu->cpum.GstCtx.cr4 & X86_CR4_PCIDE)
|
---|
6188 | && (uNewCrX & RT_BIT_64(63)))
|
---|
6189 | {
|
---|
6190 | /** @todo r=ramshankar: avoiding a TLB flush altogether here causes Windows 10
|
---|
6191 | * SMP(w/o nested-paging) to hang during bootup on Skylake systems, see
|
---|
6192 | * Intel spec. 4.10.4.1 "Operations that Invalidate TLBs and
|
---|
6193 | * Paging-Structure Caches". */
|
---|
6194 | uNewCrX &= ~RT_BIT_64(63);
|
---|
6195 | }
|
---|
6196 |
|
---|
6197 | /* Check / mask the value. */
|
---|
6198 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX_EPT
|
---|
6199 | /* See Intel spec. 27.2.2 "EPT Translation Mechanism" footnote. */
|
---|
6200 | uint64_t const fInvPhysMask = !CPUMIsGuestVmxEptPagingEnabledEx(IEM_GET_CTX(pVCpu))
|
---|
6201 | ? (UINT64_MAX << IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cMaxPhysAddrWidth)
|
---|
6202 | : (~X86_CR3_EPT_PAGE_MASK & X86_PAGE_4K_BASE_MASK);
|
---|
6203 | #else
|
---|
6204 | uint64_t const fInvPhysMask = UINT64_C(0xfff0000000000000);
|
---|
6205 | #endif
|
---|
6206 | if (uNewCrX & fInvPhysMask)
|
---|
6207 | {
|
---|
6208 | /** @todo Should we raise this only for 64-bit mode like Intel claims? AMD is
|
---|
6209 | * very vague in this area. As mentioned above, need testcase on real
|
---|
6210 | * hardware... Sigh. */
|
---|
6211 | Log(("Trying to load CR3 with invalid high bits set: %#llx\n", uNewCrX));
|
---|
6212 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6213 | }
|
---|
6214 |
|
---|
6215 | uint64_t fValid;
|
---|
6216 | if ( (pVCpu->cpum.GstCtx.cr4 & X86_CR4_PAE)
|
---|
6217 | && (pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_LME))
|
---|
6218 | {
|
---|
6219 | /** @todo Redundant? This value has already been validated above. */
|
---|
6220 | fValid = UINT64_C(0x000fffffffffffff);
|
---|
6221 | }
|
---|
6222 | else
|
---|
6223 | fValid = UINT64_C(0xffffffff);
|
---|
6224 | if (uNewCrX & ~fValid)
|
---|
6225 | {
|
---|
6226 | Log(("Automatically clearing reserved MBZ bits in CR3 load: NewCR3=%#llx ClearedBits=%#llx\n",
|
---|
6227 | uNewCrX, uNewCrX & ~fValid));
|
---|
6228 | uNewCrX &= fValid;
|
---|
6229 | }
|
---|
6230 |
|
---|
6231 | if (!IEM_SVM_IS_WRITE_CR_INTERCEPT_SET(pVCpu, /*cr*/ 3))
|
---|
6232 | { /* probable */ }
|
---|
6233 | else
|
---|
6234 | {
|
---|
6235 | Log(("iemCImpl_load_Cr%#x: Guest intercept -> #VMEXIT\n", iCrReg));
|
---|
6236 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
6237 | IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_WRITE_CR3, enmAccessCrX, iGReg);
|
---|
6238 | }
|
---|
6239 |
|
---|
6240 | /* Inform PGM. */
|
---|
6241 | if (pVCpu->cpum.GstCtx.cr0 & X86_CR0_PG)
|
---|
6242 | {
|
---|
6243 | if ( !CPUMIsGuestInPAEModeEx(IEM_GET_CTX(pVCpu))
|
---|
6244 | || CPUMIsGuestInSvmNestedHwVirtMode(IEM_GET_CTX(pVCpu)))
|
---|
6245 | { /* likely */ }
|
---|
6246 | else
|
---|
6247 | {
|
---|
6248 | Assert(enmAccessCrX == IEMACCESSCRX_MOV_CRX);
|
---|
6249 | IEM_MAP_PAE_PDPES_AT_CR3_RET(pVCpu, iCrReg, uNewCrX);
|
---|
6250 | }
|
---|
6251 | rc = PGMFlushTLB(pVCpu, uNewCrX, !(pVCpu->cpum.GstCtx.cr4 & X86_CR4_PGE));
|
---|
6252 | AssertRCReturn(rc, rc);
|
---|
6253 | /* ignore informational status codes */
|
---|
6254 | }
|
---|
6255 |
|
---|
6256 | /* Make the change. */
|
---|
6257 | rc = CPUMSetGuestCR3(pVCpu, uNewCrX);
|
---|
6258 | AssertRCSuccessReturn(rc, rc);
|
---|
6259 |
|
---|
6260 | rcStrict = VINF_SUCCESS;
|
---|
6261 | break;
|
---|
6262 | }
|
---|
6263 |
|
---|
6264 | /*
|
---|
6265 | * CR4 is a bit more tedious as there are bits which cannot be cleared
|
---|
6266 | * under some circumstances and such.
|
---|
6267 | */
|
---|
6268 | case 4:
|
---|
6269 | {
|
---|
6270 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR4);
|
---|
6271 | uint64_t const uOldCrX = pVCpu->cpum.GstCtx.cr4;
|
---|
6272 |
|
---|
6273 | /* Reserved bits. */
|
---|
6274 | uint32_t const fValid = CPUMGetGuestCR4ValidMask(pVCpu->CTX_SUFF(pVM));
|
---|
6275 | if (uNewCrX & ~(uint64_t)fValid)
|
---|
6276 | {
|
---|
6277 | Log(("Trying to set reserved CR4 bits: NewCR4=%#llx InvalidBits=%#llx\n", uNewCrX, uNewCrX & ~(uint64_t)fValid));
|
---|
6278 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6279 | }
|
---|
6280 |
|
---|
6281 | bool const fPcide = !(uOldCrX & X86_CR4_PCIDE) && (uNewCrX & X86_CR4_PCIDE);
|
---|
6282 | bool const fLongMode = CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu));
|
---|
6283 |
|
---|
6284 | /* PCIDE check. */
|
---|
6285 | if ( fPcide
|
---|
6286 | && ( !fLongMode
|
---|
6287 | || (pVCpu->cpum.GstCtx.cr3 & UINT64_C(0xfff))))
|
---|
6288 | {
|
---|
6289 | Log(("Trying to set PCIDE with invalid PCID or outside long mode. Pcid=%#x\n", (pVCpu->cpum.GstCtx.cr3 & UINT64_C(0xfff))));
|
---|
6290 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6291 | }
|
---|
6292 |
|
---|
6293 | /* PAE check. */
|
---|
6294 | if ( fLongMode
|
---|
6295 | && (uOldCrX & X86_CR4_PAE)
|
---|
6296 | && !(uNewCrX & X86_CR4_PAE))
|
---|
6297 | {
|
---|
6298 | Log(("Trying to set clear CR4.PAE while long mode is active\n"));
|
---|
6299 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6300 | }
|
---|
6301 |
|
---|
6302 | if (!IEM_SVM_IS_WRITE_CR_INTERCEPT_SET(pVCpu, /*cr*/ 4))
|
---|
6303 | { /* probable */ }
|
---|
6304 | else
|
---|
6305 | {
|
---|
6306 | Log(("iemCImpl_load_Cr%#x: Guest intercept -> #VMEXIT\n", iCrReg));
|
---|
6307 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
6308 | IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_WRITE_CR4, enmAccessCrX, iGReg);
|
---|
6309 | }
|
---|
6310 |
|
---|
6311 | /* Check for bits that must remain set or cleared in VMX operation,
|
---|
6312 | see Intel spec. 23.8 "Restrictions on VMX operation". */
|
---|
6313 | if (!IEM_VMX_IS_ROOT_MODE(pVCpu))
|
---|
6314 | { /* probable */ }
|
---|
6315 | else
|
---|
6316 | {
|
---|
6317 | uint64_t const uCr4Fixed0 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr4Fixed0;
|
---|
6318 | if ((uNewCrX & uCr4Fixed0) != uCr4Fixed0)
|
---|
6319 | {
|
---|
6320 | Log(("Trying to clear reserved CR4 bits in VMX operation: NewCr4=%#llx MB1=%#llx\n", uNewCrX, uCr4Fixed0));
|
---|
6321 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6322 | }
|
---|
6323 |
|
---|
6324 | uint64_t const uCr4Fixed1 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr4Fixed1;
|
---|
6325 | if (uNewCrX & ~uCr4Fixed1)
|
---|
6326 | {
|
---|
6327 | Log(("Trying to set reserved CR4 bits in VMX operation: NewCr4=%#llx MB0=%#llx\n", uNewCrX, uCr4Fixed1));
|
---|
6328 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6329 | }
|
---|
6330 | }
|
---|
6331 |
|
---|
6332 | /*
|
---|
6333 | * Notify PGM.
|
---|
6334 | */
|
---|
6335 | if ((uNewCrX ^ uOldCrX) & (X86_CR4_PSE | X86_CR4_PAE | X86_CR4_PGE | X86_CR4_PCIDE /* | X86_CR4_SMEP */))
|
---|
6336 | {
|
---|
6337 | if ( !CPUMIsPaePagingEnabled(pVCpu->cpum.GstCtx.cr0, uNewCrX, pVCpu->cpum.GstCtx.msrEFER)
|
---|
6338 | || CPUMIsGuestInSvmNestedHwVirtMode(IEM_GET_CTX(pVCpu)))
|
---|
6339 | { /* likely */ }
|
---|
6340 | else
|
---|
6341 | {
|
---|
6342 | Assert(enmAccessCrX == IEMACCESSCRX_MOV_CRX);
|
---|
6343 | IEM_MAP_PAE_PDPES_AT_CR3_RET(pVCpu, iCrReg, pVCpu->cpum.GstCtx.cr3);
|
---|
6344 | }
|
---|
6345 | rc = PGMFlushTLB(pVCpu, pVCpu->cpum.GstCtx.cr3, true /* global */);
|
---|
6346 | AssertRCReturn(rc, rc);
|
---|
6347 | /* ignore informational status codes */
|
---|
6348 | }
|
---|
6349 |
|
---|
6350 | /*
|
---|
6351 | * Change it.
|
---|
6352 | */
|
---|
6353 | rc = CPUMSetGuestCR4(pVCpu, uNewCrX);
|
---|
6354 | AssertRCSuccessReturn(rc, rc);
|
---|
6355 | Assert(pVCpu->cpum.GstCtx.cr4 == uNewCrX);
|
---|
6356 |
|
---|
6357 | rcStrict = PGMChangeMode(pVCpu, pVCpu->cpum.GstCtx.cr0, pVCpu->cpum.GstCtx.cr4, pVCpu->cpum.GstCtx.msrEFER,
|
---|
6358 | false /* fForce */);
|
---|
6359 | break;
|
---|
6360 | }
|
---|
6361 |
|
---|
6362 | /*
|
---|
6363 | * CR8 maps to the APIC TPR.
|
---|
6364 | */
|
---|
6365 | case 8:
|
---|
6366 | {
|
---|
6367 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_APIC_TPR);
|
---|
6368 | if (uNewCrX & ~(uint64_t)0xf)
|
---|
6369 | {
|
---|
6370 | Log(("Trying to set reserved CR8 bits (%#RX64)\n", uNewCrX));
|
---|
6371 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6372 | }
|
---|
6373 |
|
---|
6374 | if (!IEM_IS_IN_GUEST(pVCpu))
|
---|
6375 | { /* probable */ }
|
---|
6376 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
6377 | else if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
6378 | && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_USE_TPR_SHADOW))
|
---|
6379 | {
|
---|
6380 | /*
|
---|
6381 | * If the Mov-to-CR8 doesn't cause a VM-exit, bits 0:3 of the source operand
|
---|
6382 | * is copied to bits 7:4 of the VTPR. Bits 0:3 and bits 31:8 of the VTPR are
|
---|
6383 | * cleared. Following this the processor performs TPR virtualization.
|
---|
6384 | *
|
---|
6385 | * However, we should not perform TPR virtualization immediately here but
|
---|
6386 | * after this instruction has completed.
|
---|
6387 | *
|
---|
6388 | * See Intel spec. 29.3 "Virtualizing CR8-based TPR Accesses"
|
---|
6389 | * See Intel spec. 27.1 "Architectural State Before A VM-exit"
|
---|
6390 | */
|
---|
6391 | uint32_t const uTpr = (uNewCrX & 0xf) << 4;
|
---|
6392 | Log(("iemCImpl_load_Cr%#x: Virtualizing TPR (%#x) write\n", iCrReg, uTpr));
|
---|
6393 | iemVmxVirtApicWriteRaw32(pVCpu, XAPIC_OFF_TPR, uTpr);
|
---|
6394 | iemVmxVirtApicSetPendingWrite(pVCpu, XAPIC_OFF_TPR);
|
---|
6395 | rcStrict = VINF_SUCCESS;
|
---|
6396 | break;
|
---|
6397 | }
|
---|
6398 | #endif
|
---|
6399 | #ifdef VBOX_WITH_NESTED_HWVIRT_SVM
|
---|
6400 | else if (pVCpu->iem.s.fExec & IEM_F_X86_CTX_SVM)
|
---|
6401 | {
|
---|
6402 | if (IEM_SVM_IS_WRITE_CR_INTERCEPT_SET(pVCpu, /*cr*/ 8))
|
---|
6403 | {
|
---|
6404 | Log(("iemCImpl_load_Cr%#x: Guest intercept -> #VMEXIT\n", iCrReg));
|
---|
6405 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
6406 | IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_WRITE_CR8, enmAccessCrX, iGReg);
|
---|
6407 | }
|
---|
6408 |
|
---|
6409 | pVCpu->cpum.GstCtx.hwvirt.svm.Vmcb.ctrl.IntCtrl.n.u8VTPR = uNewCrX;
|
---|
6410 | if (CPUMIsGuestSvmVirtIntrMasking(pVCpu, IEM_GET_CTX(pVCpu)))
|
---|
6411 | {
|
---|
6412 | rcStrict = VINF_SUCCESS;
|
---|
6413 | break;
|
---|
6414 | }
|
---|
6415 | }
|
---|
6416 | #endif
|
---|
6417 | uint8_t const u8Tpr = (uint8_t)uNewCrX << 4;
|
---|
6418 | APICSetTpr(pVCpu, u8Tpr);
|
---|
6419 | rcStrict = VINF_SUCCESS;
|
---|
6420 | break;
|
---|
6421 | }
|
---|
6422 |
|
---|
6423 | IEM_NOT_REACHED_DEFAULT_CASE_RET(); /* call checks */
|
---|
6424 | }
|
---|
6425 |
|
---|
6426 | /*
|
---|
6427 | * Advance the RIP on success.
|
---|
6428 | */
|
---|
6429 | if (RT_SUCCESS(rcStrict))
|
---|
6430 | {
|
---|
6431 | if (rcStrict != VINF_SUCCESS)
|
---|
6432 | iemSetPassUpStatus(pVCpu, rcStrict);
|
---|
6433 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
6434 | }
|
---|
6435 |
|
---|
6436 | return rcStrict;
|
---|
6437 | }
|
---|
6438 |
|
---|
6439 |
|
---|
6440 | /**
|
---|
6441 | * Implements mov CRx,GReg.
|
---|
6442 | *
|
---|
6443 | * @param iCrReg The CRx register to write (valid).
|
---|
6444 | * @param iGReg The general register to load the CRx value from.
|
---|
6445 | */
|
---|
6446 | IEM_CIMPL_DEF_2(iemCImpl_mov_Cd_Rd, uint8_t, iCrReg, uint8_t, iGReg)
|
---|
6447 | {
|
---|
6448 | if (IEM_GET_CPL(pVCpu) != 0)
|
---|
6449 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6450 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
6451 |
|
---|
6452 | /*
|
---|
6453 | * Read the new value from the source register and call common worker.
|
---|
6454 | */
|
---|
6455 | uint64_t uNewCrX;
|
---|
6456 | if (IEM_IS_64BIT_CODE(pVCpu))
|
---|
6457 | uNewCrX = iemGRegFetchU64(pVCpu, iGReg);
|
---|
6458 | else
|
---|
6459 | uNewCrX = iemGRegFetchU32(pVCpu, iGReg);
|
---|
6460 |
|
---|
6461 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
6462 | if (!IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
6463 | { /* probable */ }
|
---|
6464 | else
|
---|
6465 | {
|
---|
6466 | VBOXSTRICTRC rcStrict = VINF_VMX_INTERCEPT_NOT_ACTIVE;
|
---|
6467 | switch (iCrReg)
|
---|
6468 | {
|
---|
6469 | case 0:
|
---|
6470 | case 4: rcStrict = iemVmxVmexitInstrMovToCr0Cr4(pVCpu, iCrReg, &uNewCrX, iGReg, cbInstr); break;
|
---|
6471 | case 3: rcStrict = iemVmxVmexitInstrMovToCr3(pVCpu, uNewCrX, iGReg, cbInstr); break;
|
---|
6472 | case 8: rcStrict = iemVmxVmexitInstrMovToCr8(pVCpu, iGReg, cbInstr); break;
|
---|
6473 | }
|
---|
6474 | if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
|
---|
6475 | return rcStrict;
|
---|
6476 | }
|
---|
6477 | #endif
|
---|
6478 |
|
---|
6479 | return IEM_CIMPL_CALL_4(iemCImpl_load_CrX, iCrReg, uNewCrX, IEMACCESSCRX_MOV_CRX, iGReg);
|
---|
6480 | }
|
---|
6481 |
|
---|
6482 |
|
---|
6483 | /**
|
---|
6484 | * Implements 'LMSW r/m16'
|
---|
6485 | *
|
---|
6486 | * @param u16NewMsw The new value.
|
---|
6487 | * @param GCPtrEffDst The guest-linear address of the source operand in case
|
---|
6488 | * of a memory operand. For register operand, pass
|
---|
6489 | * NIL_RTGCPTR.
|
---|
6490 | */
|
---|
6491 | IEM_CIMPL_DEF_2(iemCImpl_lmsw, uint16_t, u16NewMsw, RTGCPTR, GCPtrEffDst)
|
---|
6492 | {
|
---|
6493 | if (IEM_GET_CPL(pVCpu) != 0)
|
---|
6494 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6495 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
6496 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0);
|
---|
6497 |
|
---|
6498 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
6499 | /* Check nested-guest VMX intercept and get updated MSW if there's no VM-exit. */
|
---|
6500 | if (!IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
6501 | { /* probable */ }
|
---|
6502 | else
|
---|
6503 | {
|
---|
6504 | VBOXSTRICTRC rcStrict = iemVmxVmexitInstrLmsw(pVCpu, pVCpu->cpum.GstCtx.cr0, &u16NewMsw, GCPtrEffDst, cbInstr);
|
---|
6505 | if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
|
---|
6506 | return rcStrict;
|
---|
6507 | }
|
---|
6508 | #else
|
---|
6509 | RT_NOREF_PV(GCPtrEffDst);
|
---|
6510 | #endif
|
---|
6511 |
|
---|
6512 | /*
|
---|
6513 | * Compose the new CR0 value and call common worker.
|
---|
6514 | */
|
---|
6515 | uint64_t uNewCr0 = pVCpu->cpum.GstCtx.cr0 & ~(X86_CR0_MP | X86_CR0_EM | X86_CR0_TS);
|
---|
6516 | uNewCr0 |= u16NewMsw & (X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS);
|
---|
6517 | return IEM_CIMPL_CALL_4(iemCImpl_load_CrX, /*cr*/ 0, uNewCr0, IEMACCESSCRX_LMSW, UINT8_MAX /* iGReg */);
|
---|
6518 | }
|
---|
6519 |
|
---|
6520 |
|
---|
6521 | /**
|
---|
6522 | * Implements 'CLTS'.
|
---|
6523 | */
|
---|
6524 | IEM_CIMPL_DEF_0(iemCImpl_clts)
|
---|
6525 | {
|
---|
6526 | if (IEM_GET_CPL(pVCpu) != 0)
|
---|
6527 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6528 |
|
---|
6529 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0);
|
---|
6530 | uint64_t uNewCr0 = pVCpu->cpum.GstCtx.cr0;
|
---|
6531 | uNewCr0 &= ~X86_CR0_TS;
|
---|
6532 |
|
---|
6533 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
6534 | if (!IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
6535 | { /* probable */ }
|
---|
6536 | else
|
---|
6537 | {
|
---|
6538 | VBOXSTRICTRC rcStrict = iemVmxVmexitInstrClts(pVCpu, cbInstr);
|
---|
6539 | if (rcStrict == VINF_VMX_MODIFIES_BEHAVIOR)
|
---|
6540 | uNewCr0 |= (pVCpu->cpum.GstCtx.cr0 & X86_CR0_TS);
|
---|
6541 | else if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
|
---|
6542 | return rcStrict;
|
---|
6543 | }
|
---|
6544 | #endif
|
---|
6545 |
|
---|
6546 | return IEM_CIMPL_CALL_4(iemCImpl_load_CrX, /*cr*/ 0, uNewCr0, IEMACCESSCRX_CLTS, UINT8_MAX /* iGReg */);
|
---|
6547 | }
|
---|
6548 |
|
---|
6549 |
|
---|
6550 | /**
|
---|
6551 | * Implements mov GReg,DRx.
|
---|
6552 | *
|
---|
6553 | * @param iGReg The general register to store the DRx value in.
|
---|
6554 | * @param iDrReg The DRx register to read (0-7).
|
---|
6555 | */
|
---|
6556 | IEM_CIMPL_DEF_2(iemCImpl_mov_Rd_Dd, uint8_t, iGReg, uint8_t, iDrReg)
|
---|
6557 | {
|
---|
6558 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
6559 | /*
|
---|
6560 | * Check nested-guest VMX intercept.
|
---|
6561 | * Unlike most other intercepts, the Mov DRx intercept takes preceedence
|
---|
6562 | * over CPL and CR4.DE and even DR4/DR5 checks.
|
---|
6563 | *
|
---|
6564 | * See Intel spec. 25.1.3 "Instructions That Cause VM Exits Conditionally".
|
---|
6565 | */
|
---|
6566 | if (!IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
6567 | { /* probable */ }
|
---|
6568 | else
|
---|
6569 | {
|
---|
6570 | VBOXSTRICTRC rcStrict = iemVmxVmexitInstrMovDrX(pVCpu, VMXINSTRID_MOV_FROM_DRX, iDrReg, iGReg, cbInstr);
|
---|
6571 | if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
|
---|
6572 | return rcStrict;
|
---|
6573 | }
|
---|
6574 | #endif
|
---|
6575 |
|
---|
6576 | /*
|
---|
6577 | * Check preconditions.
|
---|
6578 | */
|
---|
6579 | /* Raise GPs. */
|
---|
6580 | if (IEM_GET_CPL(pVCpu) != 0)
|
---|
6581 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6582 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
6583 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_DR7);
|
---|
6584 |
|
---|
6585 | /** @todo \#UD in outside ring-0 too? */
|
---|
6586 | if (iDrReg == 4 || iDrReg == 5)
|
---|
6587 | {
|
---|
6588 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_CR4);
|
---|
6589 | if (pVCpu->cpum.GstCtx.cr4 & X86_CR4_DE)
|
---|
6590 | {
|
---|
6591 | Log(("mov r%u,dr%u: CR4.DE=1 -> #GP(0)\n", iGReg, iDrReg));
|
---|
6592 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6593 | }
|
---|
6594 | iDrReg += 2;
|
---|
6595 | }
|
---|
6596 |
|
---|
6597 | /* Raise #DB if general access detect is enabled. */
|
---|
6598 | if (pVCpu->cpum.GstCtx.dr[7] & X86_DR7_GD)
|
---|
6599 | {
|
---|
6600 | Log(("mov r%u,dr%u: DR7.GD=1 -> #DB\n", iGReg, iDrReg));
|
---|
6601 | return iemRaiseDebugException(pVCpu);
|
---|
6602 | }
|
---|
6603 |
|
---|
6604 | /*
|
---|
6605 | * Read the debug register and store it in the specified general register.
|
---|
6606 | */
|
---|
6607 | uint64_t drX;
|
---|
6608 | switch (iDrReg)
|
---|
6609 | {
|
---|
6610 | case 0:
|
---|
6611 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3);
|
---|
6612 | drX = pVCpu->cpum.GstCtx.dr[0];
|
---|
6613 | break;
|
---|
6614 | case 1:
|
---|
6615 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3);
|
---|
6616 | drX = pVCpu->cpum.GstCtx.dr[1];
|
---|
6617 | break;
|
---|
6618 | case 2:
|
---|
6619 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3);
|
---|
6620 | drX = pVCpu->cpum.GstCtx.dr[2];
|
---|
6621 | break;
|
---|
6622 | case 3:
|
---|
6623 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3);
|
---|
6624 | drX = pVCpu->cpum.GstCtx.dr[3];
|
---|
6625 | break;
|
---|
6626 | case 6:
|
---|
6627 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR6);
|
---|
6628 | drX = pVCpu->cpum.GstCtx.dr[6];
|
---|
6629 | drX |= X86_DR6_RA1_MASK;
|
---|
6630 | drX &= ~X86_DR6_RAZ_MASK;
|
---|
6631 | break;
|
---|
6632 | case 7:
|
---|
6633 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_DR7);
|
---|
6634 | drX = pVCpu->cpum.GstCtx.dr[7];
|
---|
6635 | drX |=X86_DR7_RA1_MASK;
|
---|
6636 | drX &= ~X86_DR7_RAZ_MASK;
|
---|
6637 | break;
|
---|
6638 | IEM_NOT_REACHED_DEFAULT_CASE_RET(); /* caller checks */
|
---|
6639 | }
|
---|
6640 |
|
---|
6641 | /** @todo SVM nested-guest intercept for DR8-DR15? */
|
---|
6642 | /*
|
---|
6643 | * Check for any SVM nested-guest intercepts for the DRx read.
|
---|
6644 | */
|
---|
6645 | if (!IEM_SVM_IS_READ_DR_INTERCEPT_SET(pVCpu, iDrReg))
|
---|
6646 | { /* probable */ }
|
---|
6647 | else
|
---|
6648 | {
|
---|
6649 | Log(("mov r%u,dr%u: Guest intercept -> #VMEXIT\n", iGReg, iDrReg));
|
---|
6650 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
6651 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_READ_DR0 + (iDrReg & 0xf),
|
---|
6652 | IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fSvmDecodeAssists ? (iGReg & 7) : 0, 0 /* uExitInfo2 */);
|
---|
6653 | }
|
---|
6654 |
|
---|
6655 | if (IEM_IS_64BIT_CODE(pVCpu))
|
---|
6656 | *(uint64_t *)iemGRegRef(pVCpu, iGReg) = drX;
|
---|
6657 | else
|
---|
6658 | *(uint64_t *)iemGRegRef(pVCpu, iGReg) = (uint32_t)drX;
|
---|
6659 |
|
---|
6660 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
6661 | }
|
---|
6662 |
|
---|
6663 |
|
---|
6664 | /**
|
---|
6665 | * Implements mov DRx,GReg.
|
---|
6666 | *
|
---|
6667 | * @param iDrReg The DRx register to write (valid).
|
---|
6668 | * @param iGReg The general register to load the DRx value from.
|
---|
6669 | */
|
---|
6670 | IEM_CIMPL_DEF_2(iemCImpl_mov_Dd_Rd, uint8_t, iDrReg, uint8_t, iGReg)
|
---|
6671 | {
|
---|
6672 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
6673 | /*
|
---|
6674 | * Check nested-guest VMX intercept.
|
---|
6675 | * Unlike most other intercepts, the Mov DRx intercept takes preceedence
|
---|
6676 | * over CPL and CR4.DE and even DR4/DR5 checks.
|
---|
6677 | *
|
---|
6678 | * See Intel spec. 25.1.3 "Instructions That Cause VM Exits Conditionally".
|
---|
6679 | */
|
---|
6680 | if (!IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
6681 | { /* probable */ }
|
---|
6682 | else
|
---|
6683 | {
|
---|
6684 | VBOXSTRICTRC rcStrict = iemVmxVmexitInstrMovDrX(pVCpu, VMXINSTRID_MOV_TO_DRX, iDrReg, iGReg, cbInstr);
|
---|
6685 | if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
|
---|
6686 | return rcStrict;
|
---|
6687 | }
|
---|
6688 | #endif
|
---|
6689 |
|
---|
6690 | /*
|
---|
6691 | * Check preconditions.
|
---|
6692 | */
|
---|
6693 | if (IEM_GET_CPL(pVCpu) != 0)
|
---|
6694 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6695 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
6696 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_DR7);
|
---|
6697 |
|
---|
6698 | if (iDrReg == 4 || iDrReg == 5)
|
---|
6699 | {
|
---|
6700 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_CR4);
|
---|
6701 | if (pVCpu->cpum.GstCtx.cr4 & X86_CR4_DE)
|
---|
6702 | {
|
---|
6703 | Log(("mov dr%u,r%u: CR4.DE=1 -> #GP(0)\n", iDrReg, iGReg));
|
---|
6704 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6705 | }
|
---|
6706 | iDrReg += 2;
|
---|
6707 | }
|
---|
6708 |
|
---|
6709 | /* Raise #DB if general access detect is enabled. */
|
---|
6710 | /** @todo is \#DB/DR7.GD raised before any reserved high bits in DR7/DR6
|
---|
6711 | * \#GP? */
|
---|
6712 | if (pVCpu->cpum.GstCtx.dr[7] & X86_DR7_GD)
|
---|
6713 | {
|
---|
6714 | Log(("mov dr%u,r%u: DR7.GD=1 -> #DB\n", iDrReg, iGReg));
|
---|
6715 | return iemRaiseDebugException(pVCpu);
|
---|
6716 | }
|
---|
6717 |
|
---|
6718 | /*
|
---|
6719 | * Read the new value from the source register.
|
---|
6720 | */
|
---|
6721 | uint64_t uNewDrX;
|
---|
6722 | if (IEM_IS_64BIT_CODE(pVCpu))
|
---|
6723 | uNewDrX = iemGRegFetchU64(pVCpu, iGReg);
|
---|
6724 | else
|
---|
6725 | uNewDrX = iemGRegFetchU32(pVCpu, iGReg);
|
---|
6726 |
|
---|
6727 | /*
|
---|
6728 | * Adjust it.
|
---|
6729 | */
|
---|
6730 | switch (iDrReg)
|
---|
6731 | {
|
---|
6732 | case 0:
|
---|
6733 | case 1:
|
---|
6734 | case 2:
|
---|
6735 | case 3:
|
---|
6736 | /* nothing to adjust */
|
---|
6737 | break;
|
---|
6738 |
|
---|
6739 | case 6:
|
---|
6740 | if (uNewDrX & X86_DR6_MBZ_MASK)
|
---|
6741 | {
|
---|
6742 | Log(("mov dr%u,%#llx: DR6 high bits are not zero -> #GP(0)\n", iDrReg, uNewDrX));
|
---|
6743 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6744 | }
|
---|
6745 | uNewDrX |= X86_DR6_RA1_MASK;
|
---|
6746 | uNewDrX &= ~X86_DR6_RAZ_MASK;
|
---|
6747 | break;
|
---|
6748 |
|
---|
6749 | case 7:
|
---|
6750 | if (uNewDrX & X86_DR7_MBZ_MASK)
|
---|
6751 | {
|
---|
6752 | Log(("mov dr%u,%#llx: DR7 high bits are not zero -> #GP(0)\n", iDrReg, uNewDrX));
|
---|
6753 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6754 | }
|
---|
6755 | uNewDrX |= X86_DR7_RA1_MASK;
|
---|
6756 | uNewDrX &= ~X86_DR7_RAZ_MASK;
|
---|
6757 | break;
|
---|
6758 |
|
---|
6759 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
6760 | }
|
---|
6761 |
|
---|
6762 | /** @todo SVM nested-guest intercept for DR8-DR15? */
|
---|
6763 | /*
|
---|
6764 | * Check for any SVM nested-guest intercepts for the DRx write.
|
---|
6765 | */
|
---|
6766 | if (!IEM_SVM_IS_WRITE_DR_INTERCEPT_SET(pVCpu, iDrReg))
|
---|
6767 | { /* probable */ }
|
---|
6768 | else
|
---|
6769 | {
|
---|
6770 | Log2(("mov dr%u,r%u: Guest intercept -> #VMEXIT\n", iDrReg, iGReg));
|
---|
6771 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
6772 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_WRITE_DR0 + (iDrReg & 0xf),
|
---|
6773 | IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fSvmDecodeAssists ? (iGReg & 7) : 0, 0 /* uExitInfo2 */);
|
---|
6774 | }
|
---|
6775 |
|
---|
6776 | /*
|
---|
6777 | * Do the actual setting.
|
---|
6778 | */
|
---|
6779 | if (iDrReg < 4)
|
---|
6780 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3);
|
---|
6781 | else if (iDrReg == 6)
|
---|
6782 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR6);
|
---|
6783 |
|
---|
6784 | int rc = CPUMSetGuestDRx(pVCpu, iDrReg, uNewDrX);
|
---|
6785 | AssertRCSuccessReturn(rc, RT_SUCCESS_NP(rc) ? VERR_IEM_IPE_1 : rc);
|
---|
6786 |
|
---|
6787 | /*
|
---|
6788 | * Re-init hardware breakpoint summary if it was DR7 that got changed.
|
---|
6789 | */
|
---|
6790 | if (iDrReg == 7)
|
---|
6791 | iemRecalcExecDbgFlags(pVCpu);
|
---|
6792 |
|
---|
6793 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
6794 | }
|
---|
6795 |
|
---|
6796 |
|
---|
6797 | /**
|
---|
6798 | * Implements mov GReg,TRx.
|
---|
6799 | *
|
---|
6800 | * @param iGReg The general register to store the
|
---|
6801 | * TRx value in.
|
---|
6802 | * @param iTrReg The TRx register to read (6/7).
|
---|
6803 | */
|
---|
6804 | IEM_CIMPL_DEF_2(iemCImpl_mov_Rd_Td, uint8_t, iGReg, uint8_t, iTrReg)
|
---|
6805 | {
|
---|
6806 | /*
|
---|
6807 | * Check preconditions. NB: This instruction is 386/486 only.
|
---|
6808 | */
|
---|
6809 |
|
---|
6810 | /* Raise GPs. */
|
---|
6811 | if (IEM_GET_CPL(pVCpu) != 0)
|
---|
6812 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6813 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
6814 |
|
---|
6815 | if (iTrReg < 6 || iTrReg > 7)
|
---|
6816 | {
|
---|
6817 | /** @todo Do Intel CPUs reject this or are the TRs aliased? */
|
---|
6818 | Log(("mov r%u,tr%u: invalid register -> #GP(0)\n", iGReg, iTrReg));
|
---|
6819 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6820 | }
|
---|
6821 |
|
---|
6822 | /*
|
---|
6823 | * Read the test register and store it in the specified general register.
|
---|
6824 | * This is currently a dummy implementation that only exists to satisfy
|
---|
6825 | * old debuggers like WDEB386 or OS/2 KDB which unconditionally read the
|
---|
6826 | * TR6/TR7 registers. Software which actually depends on the TR values
|
---|
6827 | * (different on 386/486) is exceedingly rare.
|
---|
6828 | */
|
---|
6829 | uint64_t trX;
|
---|
6830 | switch (iTrReg)
|
---|
6831 | {
|
---|
6832 | case 6:
|
---|
6833 | trX = 0; /* Currently a dummy. */
|
---|
6834 | break;
|
---|
6835 | case 7:
|
---|
6836 | trX = 0; /* Currently a dummy. */
|
---|
6837 | break;
|
---|
6838 | IEM_NOT_REACHED_DEFAULT_CASE_RET(); /* call checks */
|
---|
6839 | }
|
---|
6840 |
|
---|
6841 | *(uint64_t *)iemGRegRef(pVCpu, iGReg) = (uint32_t)trX;
|
---|
6842 |
|
---|
6843 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
6844 | }
|
---|
6845 |
|
---|
6846 |
|
---|
6847 | /**
|
---|
6848 | * Implements mov TRx,GReg.
|
---|
6849 | *
|
---|
6850 | * @param iTrReg The TRx register to write (valid).
|
---|
6851 | * @param iGReg The general register to load the TRx
|
---|
6852 | * value from.
|
---|
6853 | */
|
---|
6854 | IEM_CIMPL_DEF_2(iemCImpl_mov_Td_Rd, uint8_t, iTrReg, uint8_t, iGReg)
|
---|
6855 | {
|
---|
6856 | /*
|
---|
6857 | * Check preconditions. NB: This instruction is 386/486 only.
|
---|
6858 | */
|
---|
6859 |
|
---|
6860 | /* Raise GPs. */
|
---|
6861 | if (IEM_GET_CPL(pVCpu) != 0)
|
---|
6862 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6863 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
6864 |
|
---|
6865 | if (iTrReg < 6 || iTrReg > 7)
|
---|
6866 | {
|
---|
6867 | /** @todo Do Intel CPUs reject this or are the TRs aliased? */
|
---|
6868 | Log(("mov r%u,tr%u: invalid register -> #GP(0)\n", iGReg, iTrReg));
|
---|
6869 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6870 | }
|
---|
6871 |
|
---|
6872 | /*
|
---|
6873 | * Read the new value from the source register.
|
---|
6874 | */
|
---|
6875 | uint64_t uNewTrX;
|
---|
6876 | if (IEM_IS_64BIT_CODE(pVCpu)) /** @todo err... 64-bit 386? */
|
---|
6877 | uNewTrX = iemGRegFetchU64(pVCpu, iGReg);
|
---|
6878 | else
|
---|
6879 | uNewTrX = iemGRegFetchU32(pVCpu, iGReg);
|
---|
6880 |
|
---|
6881 | /*
|
---|
6882 | * Here we would do the actual setting if this weren't a dummy implementation.
|
---|
6883 | * This is currently a dummy implementation that only exists to prevent
|
---|
6884 | * old debuggers like WDEB386 or OS/2 KDB from crashing.
|
---|
6885 | */
|
---|
6886 | RT_NOREF(uNewTrX);
|
---|
6887 |
|
---|
6888 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
6889 | }
|
---|
6890 |
|
---|
6891 |
|
---|
6892 | /**
|
---|
6893 | * Implements 'INVLPG m'.
|
---|
6894 | *
|
---|
6895 | * @param GCPtrPage The effective address of the page to invalidate.
|
---|
6896 | * @remarks Updates the RIP.
|
---|
6897 | */
|
---|
6898 | IEM_CIMPL_DEF_1(iemCImpl_invlpg, RTGCPTR, GCPtrPage)
|
---|
6899 | {
|
---|
6900 | /* ring-0 only. */
|
---|
6901 | if (IEM_GET_CPL(pVCpu) != 0)
|
---|
6902 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6903 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
6904 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_CR3 | CPUMCTX_EXTRN_CR4 | CPUMCTX_EXTRN_EFER);
|
---|
6905 |
|
---|
6906 | if (!IEM_IS_IN_GUEST(pVCpu))
|
---|
6907 | { /* probable */ }
|
---|
6908 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
6909 | else if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
6910 | && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_INVLPG_EXIT))
|
---|
6911 | {
|
---|
6912 | Log(("invlpg: Guest intercept (%RGp) -> VM-exit\n", GCPtrPage));
|
---|
6913 | return iemVmxVmexitInstrInvlpg(pVCpu, GCPtrPage, cbInstr);
|
---|
6914 | }
|
---|
6915 | #endif
|
---|
6916 | else if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_INVLPG))
|
---|
6917 | {
|
---|
6918 | Log(("invlpg: Guest intercept (%RGp) -> #VMEXIT\n", GCPtrPage));
|
---|
6919 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
6920 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_INVLPG,
|
---|
6921 | IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fSvmDecodeAssists ? GCPtrPage : 0, 0 /* uExitInfo2 */);
|
---|
6922 | }
|
---|
6923 |
|
---|
6924 | int rc = PGMInvalidatePage(pVCpu, GCPtrPage);
|
---|
6925 | if (rc == VINF_SUCCESS)
|
---|
6926 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
6927 | if (rc == VINF_PGM_SYNC_CR3)
|
---|
6928 | {
|
---|
6929 | iemSetPassUpStatus(pVCpu, rc);
|
---|
6930 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
6931 | }
|
---|
6932 |
|
---|
6933 | AssertMsg(RT_FAILURE_NP(rc), ("%Rrc\n", rc));
|
---|
6934 | Log(("PGMInvalidatePage(%RGv) -> %Rrc\n", GCPtrPage, rc));
|
---|
6935 | return rc;
|
---|
6936 | }
|
---|
6937 |
|
---|
6938 |
|
---|
6939 | /**
|
---|
6940 | * Implements INVPCID.
|
---|
6941 | *
|
---|
6942 | * @param iEffSeg The segment of the invpcid descriptor.
|
---|
6943 | * @param GCPtrInvpcidDesc The address of invpcid descriptor.
|
---|
6944 | * @param uInvpcidType The invalidation type.
|
---|
6945 | * @remarks Updates the RIP.
|
---|
6946 | */
|
---|
6947 | IEM_CIMPL_DEF_3(iemCImpl_invpcid, uint8_t, iEffSeg, RTGCPTR, GCPtrInvpcidDesc, uint64_t, uInvpcidType)
|
---|
6948 | {
|
---|
6949 | /*
|
---|
6950 | * Check preconditions.
|
---|
6951 | */
|
---|
6952 | if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fInvpcid)
|
---|
6953 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
6954 |
|
---|
6955 | /* When in VMX non-root mode and INVPCID is not enabled, it results in #UD. */
|
---|
6956 | if (RT_LIKELY( !IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
6957 | || IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_INVPCID)))
|
---|
6958 | { /* likely */ }
|
---|
6959 | else
|
---|
6960 | {
|
---|
6961 | Log(("invpcid: Not enabled for nested-guest execution -> #UD\n"));
|
---|
6962 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
6963 | }
|
---|
6964 |
|
---|
6965 | if (IEM_GET_CPL(pVCpu) != 0)
|
---|
6966 | {
|
---|
6967 | Log(("invpcid: CPL != 0 -> #GP(0)\n"));
|
---|
6968 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6969 | }
|
---|
6970 |
|
---|
6971 | if (IEM_IS_V86_MODE(pVCpu))
|
---|
6972 | {
|
---|
6973 | Log(("invpcid: v8086 mode -> #GP(0)\n"));
|
---|
6974 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6975 | }
|
---|
6976 |
|
---|
6977 | /*
|
---|
6978 | * Check nested-guest intercept.
|
---|
6979 | *
|
---|
6980 | * INVPCID causes a VM-exit if "enable INVPCID" and "INVLPG exiting" are
|
---|
6981 | * both set. We have already checked the former earlier in this function.
|
---|
6982 | *
|
---|
6983 | * CPL and virtual-8086 mode checks take priority over this VM-exit.
|
---|
6984 | * See Intel spec. "25.1.1 Relative Priority of Faults and VM Exits".
|
---|
6985 | */
|
---|
6986 | if ( !IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
6987 | || !IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_INVLPG_EXIT))
|
---|
6988 | { /* probable */ }
|
---|
6989 | else
|
---|
6990 | {
|
---|
6991 | Log(("invpcid: Guest intercept -> #VM-exit\n"));
|
---|
6992 | IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_INVPCID, VMXINSTRID_NONE, cbInstr);
|
---|
6993 | }
|
---|
6994 |
|
---|
6995 | if (uInvpcidType > X86_INVPCID_TYPE_MAX_VALID)
|
---|
6996 | {
|
---|
6997 | Log(("invpcid: invalid/unrecognized invpcid type %#RX64 -> #GP(0)\n", uInvpcidType));
|
---|
6998 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6999 | }
|
---|
7000 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_CR3 | CPUMCTX_EXTRN_CR4 | CPUMCTX_EXTRN_EFER);
|
---|
7001 |
|
---|
7002 | /*
|
---|
7003 | * Fetch the invpcid descriptor from guest memory.
|
---|
7004 | */
|
---|
7005 | RTUINT128U uDesc;
|
---|
7006 | VBOXSTRICTRC rcStrict = iemMemFetchDataU128(pVCpu, &uDesc, iEffSeg, GCPtrInvpcidDesc);
|
---|
7007 | if (rcStrict == VINF_SUCCESS)
|
---|
7008 | {
|
---|
7009 | /*
|
---|
7010 | * Validate the descriptor.
|
---|
7011 | */
|
---|
7012 | if (uDesc.s.Lo > 0xfff)
|
---|
7013 | {
|
---|
7014 | Log(("invpcid: reserved bits set in invpcid descriptor %#RX64 -> #GP(0)\n", uDesc.s.Lo));
|
---|
7015 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7016 | }
|
---|
7017 |
|
---|
7018 | RTGCUINTPTR64 const GCPtrInvAddr = uDesc.s.Hi;
|
---|
7019 | uint8_t const uPcid = uDesc.s.Lo & UINT64_C(0xfff);
|
---|
7020 | uint32_t const uCr4 = pVCpu->cpum.GstCtx.cr4;
|
---|
7021 | uint64_t const uCr3 = pVCpu->cpum.GstCtx.cr3;
|
---|
7022 | switch (uInvpcidType)
|
---|
7023 | {
|
---|
7024 | case X86_INVPCID_TYPE_INDV_ADDR:
|
---|
7025 | {
|
---|
7026 | if (!IEM_IS_CANONICAL(GCPtrInvAddr))
|
---|
7027 | {
|
---|
7028 | Log(("invpcid: invalidation address %#RGP is not canonical -> #GP(0)\n", GCPtrInvAddr));
|
---|
7029 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7030 | }
|
---|
7031 | if ( !(uCr4 & X86_CR4_PCIDE)
|
---|
7032 | && uPcid != 0)
|
---|
7033 | {
|
---|
7034 | Log(("invpcid: invalid pcid %#x\n", uPcid));
|
---|
7035 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7036 | }
|
---|
7037 |
|
---|
7038 | /* Invalidate mappings for the linear address tagged with PCID except global translations. */
|
---|
7039 | PGMFlushTLB(pVCpu, uCr3, false /* fGlobal */);
|
---|
7040 | break;
|
---|
7041 | }
|
---|
7042 |
|
---|
7043 | case X86_INVPCID_TYPE_SINGLE_CONTEXT:
|
---|
7044 | {
|
---|
7045 | if ( !(uCr4 & X86_CR4_PCIDE)
|
---|
7046 | && uPcid != 0)
|
---|
7047 | {
|
---|
7048 | Log(("invpcid: invalid pcid %#x\n", uPcid));
|
---|
7049 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7050 | }
|
---|
7051 | /* Invalidate all mappings associated with PCID except global translations. */
|
---|
7052 | PGMFlushTLB(pVCpu, uCr3, false /* fGlobal */);
|
---|
7053 | break;
|
---|
7054 | }
|
---|
7055 |
|
---|
7056 | case X86_INVPCID_TYPE_ALL_CONTEXT_INCL_GLOBAL:
|
---|
7057 | {
|
---|
7058 | PGMFlushTLB(pVCpu, uCr3, true /* fGlobal */);
|
---|
7059 | break;
|
---|
7060 | }
|
---|
7061 |
|
---|
7062 | case X86_INVPCID_TYPE_ALL_CONTEXT_EXCL_GLOBAL:
|
---|
7063 | {
|
---|
7064 | PGMFlushTLB(pVCpu, uCr3, false /* fGlobal */);
|
---|
7065 | break;
|
---|
7066 | }
|
---|
7067 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
7068 | }
|
---|
7069 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7070 | }
|
---|
7071 | return rcStrict;
|
---|
7072 | }
|
---|
7073 |
|
---|
7074 |
|
---|
7075 | /**
|
---|
7076 | * Implements INVD.
|
---|
7077 | */
|
---|
7078 | IEM_CIMPL_DEF_0(iemCImpl_invd)
|
---|
7079 | {
|
---|
7080 | if (IEM_GET_CPL(pVCpu) != 0)
|
---|
7081 | {
|
---|
7082 | Log(("invd: CPL != 0 -> #GP(0)\n"));
|
---|
7083 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7084 | }
|
---|
7085 |
|
---|
7086 | if (!IEM_IS_IN_GUEST(pVCpu))
|
---|
7087 | { /* probable */ }
|
---|
7088 | else if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
7089 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_INVD, cbInstr);
|
---|
7090 | else
|
---|
7091 | IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_INVD, SVM_EXIT_INVD, 0, 0, cbInstr);
|
---|
7092 |
|
---|
7093 | /* We currently take no action here. */
|
---|
7094 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7095 | }
|
---|
7096 |
|
---|
7097 |
|
---|
7098 | /**
|
---|
7099 | * Implements WBINVD.
|
---|
7100 | */
|
---|
7101 | IEM_CIMPL_DEF_0(iemCImpl_wbinvd)
|
---|
7102 | {
|
---|
7103 | if (IEM_GET_CPL(pVCpu) != 0)
|
---|
7104 | {
|
---|
7105 | Log(("wbinvd: CPL != 0 -> #GP(0)\n"));
|
---|
7106 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7107 | }
|
---|
7108 |
|
---|
7109 | if (!IEM_IS_IN_GUEST(pVCpu))
|
---|
7110 | { /* probable */ }
|
---|
7111 | else if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
7112 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_WBINVD, cbInstr);
|
---|
7113 | else
|
---|
7114 | IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_WBINVD, SVM_EXIT_WBINVD, 0, 0, cbInstr);
|
---|
7115 |
|
---|
7116 | /* We currently take no action here. */
|
---|
7117 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7118 | }
|
---|
7119 |
|
---|
7120 |
|
---|
7121 | /** Opcode 0x0f 0xaa. */
|
---|
7122 | IEM_CIMPL_DEF_0(iemCImpl_rsm)
|
---|
7123 | {
|
---|
7124 | IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_RSM, SVM_EXIT_RSM, 0, 0, cbInstr);
|
---|
7125 | NOREF(cbInstr);
|
---|
7126 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
7127 | }
|
---|
7128 |
|
---|
7129 |
|
---|
7130 | /**
|
---|
7131 | * Implements RDTSC.
|
---|
7132 | */
|
---|
7133 | IEM_CIMPL_DEF_0(iemCImpl_rdtsc)
|
---|
7134 | {
|
---|
7135 | /*
|
---|
7136 | * Check preconditions.
|
---|
7137 | */
|
---|
7138 | if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fTsc)
|
---|
7139 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
7140 |
|
---|
7141 | if (IEM_GET_CPL(pVCpu) != 0)
|
---|
7142 | {
|
---|
7143 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR4);
|
---|
7144 | if (pVCpu->cpum.GstCtx.cr4 & X86_CR4_TSD)
|
---|
7145 | {
|
---|
7146 | Log(("rdtsc: CR4.TSD and CPL=%u -> #GP(0)\n", IEM_GET_CPL(pVCpu)));
|
---|
7147 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7148 | }
|
---|
7149 | }
|
---|
7150 |
|
---|
7151 | if (!IEM_IS_IN_GUEST(pVCpu))
|
---|
7152 | { /* probable */ }
|
---|
7153 | else if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
7154 | && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_RDTSC_EXIT))
|
---|
7155 | {
|
---|
7156 | Log(("rdtsc: Guest intercept -> VM-exit\n"));
|
---|
7157 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_RDTSC, cbInstr);
|
---|
7158 | }
|
---|
7159 | else if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_RDTSC))
|
---|
7160 | {
|
---|
7161 | Log(("rdtsc: Guest intercept -> #VMEXIT\n"));
|
---|
7162 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
7163 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_RDTSC, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
7164 | }
|
---|
7165 |
|
---|
7166 | /*
|
---|
7167 | * Do the job.
|
---|
7168 | */
|
---|
7169 | uint64_t uTicks = TMCpuTickGet(pVCpu);
|
---|
7170 | #if defined(VBOX_WITH_NESTED_HWVIRT_SVM) || defined(VBOX_WITH_NESTED_HWVIRT_VMX)
|
---|
7171 | uTicks = CPUMApplyNestedGuestTscOffset(pVCpu, uTicks);
|
---|
7172 | #endif
|
---|
7173 | pVCpu->cpum.GstCtx.rax = RT_LO_U32(uTicks);
|
---|
7174 | pVCpu->cpum.GstCtx.rdx = RT_HI_U32(uTicks);
|
---|
7175 | pVCpu->cpum.GstCtx.fExtrn &= ~(CPUMCTX_EXTRN_RAX | CPUMCTX_EXTRN_RDX); /* For IEMExecDecodedRdtsc. */
|
---|
7176 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7177 | }
|
---|
7178 |
|
---|
7179 |
|
---|
7180 | /**
|
---|
7181 | * Implements RDTSC.
|
---|
7182 | */
|
---|
7183 | IEM_CIMPL_DEF_0(iemCImpl_rdtscp)
|
---|
7184 | {
|
---|
7185 | /*
|
---|
7186 | * Check preconditions.
|
---|
7187 | */
|
---|
7188 | if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fRdTscP)
|
---|
7189 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
7190 |
|
---|
7191 | if (RT_LIKELY( !IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
7192 | || IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_RDTSCP)))
|
---|
7193 | { /* likely */ }
|
---|
7194 | else
|
---|
7195 | {
|
---|
7196 | Log(("rdtscp: Not enabled for VMX non-root mode -> #UD\n"));
|
---|
7197 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
7198 | }
|
---|
7199 |
|
---|
7200 | if (IEM_GET_CPL(pVCpu) != 0)
|
---|
7201 | {
|
---|
7202 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR4);
|
---|
7203 | if (pVCpu->cpum.GstCtx.cr4 & X86_CR4_TSD)
|
---|
7204 | {
|
---|
7205 | Log(("rdtscp: CR4.TSD and CPL=%u -> #GP(0)\n", IEM_GET_CPL(pVCpu)));
|
---|
7206 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7207 | }
|
---|
7208 | }
|
---|
7209 |
|
---|
7210 | if (!IEM_IS_IN_GUEST(pVCpu))
|
---|
7211 | { /* probable */ }
|
---|
7212 | else if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
7213 | && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_RDTSC_EXIT))
|
---|
7214 | {
|
---|
7215 | Log(("rdtscp: Guest intercept -> VM-exit\n"));
|
---|
7216 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_RDTSCP, cbInstr);
|
---|
7217 | }
|
---|
7218 | else if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_RDTSCP))
|
---|
7219 | {
|
---|
7220 | Log(("rdtscp: Guest intercept -> #VMEXIT\n"));
|
---|
7221 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
7222 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_RDTSCP, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
7223 | }
|
---|
7224 |
|
---|
7225 | /*
|
---|
7226 | * Do the job.
|
---|
7227 | * Query the MSR first in case of trips to ring-3.
|
---|
7228 | */
|
---|
7229 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_TSC_AUX);
|
---|
7230 | VBOXSTRICTRC rcStrict = CPUMQueryGuestMsr(pVCpu, MSR_K8_TSC_AUX, &pVCpu->cpum.GstCtx.rcx);
|
---|
7231 | if (rcStrict == VINF_SUCCESS)
|
---|
7232 | {
|
---|
7233 | /* Low dword of the TSC_AUX msr only. */
|
---|
7234 | pVCpu->cpum.GstCtx.rcx &= UINT32_C(0xffffffff);
|
---|
7235 |
|
---|
7236 | uint64_t uTicks = TMCpuTickGet(pVCpu);
|
---|
7237 | #if defined(VBOX_WITH_NESTED_HWVIRT_SVM) || defined(VBOX_WITH_NESTED_HWVIRT_VMX)
|
---|
7238 | uTicks = CPUMApplyNestedGuestTscOffset(pVCpu, uTicks);
|
---|
7239 | #endif
|
---|
7240 | pVCpu->cpum.GstCtx.rax = RT_LO_U32(uTicks);
|
---|
7241 | pVCpu->cpum.GstCtx.rdx = RT_HI_U32(uTicks);
|
---|
7242 | pVCpu->cpum.GstCtx.fExtrn &= ~(CPUMCTX_EXTRN_RAX | CPUMCTX_EXTRN_RDX | CPUMCTX_EXTRN_RCX); /* For IEMExecDecodedRdtscp. */
|
---|
7243 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7244 | }
|
---|
7245 | return rcStrict;
|
---|
7246 | }
|
---|
7247 |
|
---|
7248 |
|
---|
7249 | /**
|
---|
7250 | * Implements RDPMC.
|
---|
7251 | */
|
---|
7252 | IEM_CIMPL_DEF_0(iemCImpl_rdpmc)
|
---|
7253 | {
|
---|
7254 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR4);
|
---|
7255 |
|
---|
7256 | if ( IEM_GET_CPL(pVCpu) != 0
|
---|
7257 | && !(pVCpu->cpum.GstCtx.cr4 & X86_CR4_PCE))
|
---|
7258 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7259 |
|
---|
7260 | if (!IEM_IS_IN_GUEST(pVCpu))
|
---|
7261 | { /* probable */ }
|
---|
7262 | else if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
7263 | && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_RDPMC_EXIT))
|
---|
7264 | {
|
---|
7265 | Log(("rdpmc: Guest intercept -> VM-exit\n"));
|
---|
7266 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_RDPMC, cbInstr);
|
---|
7267 | }
|
---|
7268 | else if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_RDPMC))
|
---|
7269 | {
|
---|
7270 | Log(("rdpmc: Guest intercept -> #VMEXIT\n"));
|
---|
7271 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
7272 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_RDPMC, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
7273 | }
|
---|
7274 |
|
---|
7275 | /** @todo Emulate performance counters, for now just return 0. */
|
---|
7276 | pVCpu->cpum.GstCtx.rax = 0;
|
---|
7277 | pVCpu->cpum.GstCtx.rdx = 0;
|
---|
7278 | pVCpu->cpum.GstCtx.fExtrn &= ~(CPUMCTX_EXTRN_RAX | CPUMCTX_EXTRN_RDX);
|
---|
7279 | /** @todo We should trigger a \#GP here if the CPU doesn't support the index in
|
---|
7280 | * ecx but see @bugref{3472}! */
|
---|
7281 |
|
---|
7282 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7283 | }
|
---|
7284 |
|
---|
7285 |
|
---|
7286 | /**
|
---|
7287 | * Implements RDMSR.
|
---|
7288 | */
|
---|
7289 | IEM_CIMPL_DEF_0(iemCImpl_rdmsr)
|
---|
7290 | {
|
---|
7291 | /*
|
---|
7292 | * Check preconditions.
|
---|
7293 | */
|
---|
7294 | if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fMsr)
|
---|
7295 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
7296 | if (IEM_GET_CPL(pVCpu) != 0)
|
---|
7297 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7298 |
|
---|
7299 | /*
|
---|
7300 | * Check nested-guest intercepts.
|
---|
7301 | */
|
---|
7302 | if (!IEM_IS_IN_GUEST(pVCpu))
|
---|
7303 | { /* probable */ }
|
---|
7304 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
7305 | else if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
7306 | {
|
---|
7307 | if (iemVmxIsRdmsrWrmsrInterceptSet(pVCpu, VMX_EXIT_RDMSR, pVCpu->cpum.GstCtx.ecx))
|
---|
7308 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_RDMSR, cbInstr);
|
---|
7309 | }
|
---|
7310 | #endif
|
---|
7311 | #ifdef VBOX_WITH_NESTED_HWVIRT_SVM
|
---|
7312 | else if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_MSR_PROT))
|
---|
7313 | {
|
---|
7314 | VBOXSTRICTRC rcStrict = iemSvmHandleMsrIntercept(pVCpu, pVCpu->cpum.GstCtx.ecx, false /* fWrite */, cbInstr);
|
---|
7315 | if (rcStrict == VINF_SVM_VMEXIT)
|
---|
7316 | return VINF_SUCCESS;
|
---|
7317 | if (rcStrict != VINF_SVM_INTERCEPT_NOT_ACTIVE)
|
---|
7318 | {
|
---|
7319 | Log(("IEM: SVM intercepted rdmsr(%#x) failed. rc=%Rrc\n", pVCpu->cpum.GstCtx.ecx, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
7320 | return rcStrict;
|
---|
7321 | }
|
---|
7322 | }
|
---|
7323 | #endif
|
---|
7324 |
|
---|
7325 | /*
|
---|
7326 | * Do the job.
|
---|
7327 | */
|
---|
7328 | RTUINT64U uValue;
|
---|
7329 | /** @todo make CPUMAllMsrs.cpp import the necessary MSR state. */
|
---|
7330 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_ALL_MSRS);
|
---|
7331 |
|
---|
7332 | VBOXSTRICTRC rcStrict = CPUMQueryGuestMsr(pVCpu, pVCpu->cpum.GstCtx.ecx, &uValue.u);
|
---|
7333 | if (rcStrict == VINF_SUCCESS)
|
---|
7334 | {
|
---|
7335 | pVCpu->cpum.GstCtx.rax = uValue.s.Lo;
|
---|
7336 | pVCpu->cpum.GstCtx.rdx = uValue.s.Hi;
|
---|
7337 | pVCpu->cpum.GstCtx.fExtrn &= ~(CPUMCTX_EXTRN_RAX | CPUMCTX_EXTRN_RDX);
|
---|
7338 |
|
---|
7339 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7340 | }
|
---|
7341 |
|
---|
7342 | #ifndef IN_RING3
|
---|
7343 | /* Deferred to ring-3. */
|
---|
7344 | if (rcStrict == VINF_CPUM_R3_MSR_READ)
|
---|
7345 | {
|
---|
7346 | Log(("IEM: rdmsr(%#x) -> ring-3\n", pVCpu->cpum.GstCtx.ecx));
|
---|
7347 | return rcStrict;
|
---|
7348 | }
|
---|
7349 | #endif
|
---|
7350 |
|
---|
7351 | /* Often a unimplemented MSR or MSR bit, so worth logging. */
|
---|
7352 | if (pVCpu->iem.s.cLogRelRdMsr < 32)
|
---|
7353 | {
|
---|
7354 | pVCpu->iem.s.cLogRelRdMsr++;
|
---|
7355 | LogRel(("IEM: rdmsr(%#x) -> #GP(0)\n", pVCpu->cpum.GstCtx.ecx));
|
---|
7356 | }
|
---|
7357 | else
|
---|
7358 | Log(( "IEM: rdmsr(%#x) -> #GP(0)\n", pVCpu->cpum.GstCtx.ecx));
|
---|
7359 | AssertMsgReturn(rcStrict == VERR_CPUM_RAISE_GP_0, ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)), VERR_IPE_UNEXPECTED_STATUS);
|
---|
7360 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7361 | }
|
---|
7362 |
|
---|
7363 |
|
---|
7364 | /**
|
---|
7365 | * Implements WRMSR.
|
---|
7366 | */
|
---|
7367 | IEM_CIMPL_DEF_0(iemCImpl_wrmsr)
|
---|
7368 | {
|
---|
7369 | /*
|
---|
7370 | * Check preconditions.
|
---|
7371 | */
|
---|
7372 | if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fMsr)
|
---|
7373 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
7374 | if (IEM_GET_CPL(pVCpu) != 0)
|
---|
7375 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7376 |
|
---|
7377 | RTUINT64U uValue;
|
---|
7378 | uValue.s.Lo = pVCpu->cpum.GstCtx.eax;
|
---|
7379 | uValue.s.Hi = pVCpu->cpum.GstCtx.edx;
|
---|
7380 |
|
---|
7381 | uint32_t const idMsr = pVCpu->cpum.GstCtx.ecx;
|
---|
7382 |
|
---|
7383 | /** @todo make CPUMAllMsrs.cpp import the necessary MSR state. */
|
---|
7384 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_ALL_MSRS);
|
---|
7385 |
|
---|
7386 | /*
|
---|
7387 | * Check nested-guest intercepts.
|
---|
7388 | */
|
---|
7389 | if (!IEM_IS_IN_GUEST(pVCpu))
|
---|
7390 | { /* probable */ }
|
---|
7391 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
7392 | else if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
7393 | {
|
---|
7394 | if (iemVmxIsRdmsrWrmsrInterceptSet(pVCpu, VMX_EXIT_WRMSR, idMsr))
|
---|
7395 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_WRMSR, cbInstr);
|
---|
7396 | }
|
---|
7397 | #endif
|
---|
7398 | #ifdef VBOX_WITH_NESTED_HWVIRT_SVM
|
---|
7399 | else if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_MSR_PROT))
|
---|
7400 | {
|
---|
7401 | VBOXSTRICTRC rcStrict = iemSvmHandleMsrIntercept(pVCpu, idMsr, true /* fWrite */, cbInstr);
|
---|
7402 | if (rcStrict == VINF_SVM_VMEXIT)
|
---|
7403 | return VINF_SUCCESS;
|
---|
7404 | if (rcStrict != VINF_SVM_INTERCEPT_NOT_ACTIVE)
|
---|
7405 | {
|
---|
7406 | Log(("IEM: SVM intercepted rdmsr(%#x) failed. rc=%Rrc\n", idMsr, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
7407 | return rcStrict;
|
---|
7408 | }
|
---|
7409 | }
|
---|
7410 | #endif
|
---|
7411 |
|
---|
7412 | /*
|
---|
7413 | * Do the job.
|
---|
7414 | */
|
---|
7415 | VBOXSTRICTRC rcStrict = CPUMSetGuestMsr(pVCpu, idMsr, uValue.u);
|
---|
7416 | if (rcStrict == VINF_SUCCESS)
|
---|
7417 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7418 |
|
---|
7419 | #ifndef IN_RING3
|
---|
7420 | /* Deferred to ring-3. */
|
---|
7421 | if (rcStrict == VINF_CPUM_R3_MSR_WRITE)
|
---|
7422 | {
|
---|
7423 | Log(("IEM: wrmsr(%#x) -> ring-3\n", idMsr));
|
---|
7424 | return rcStrict;
|
---|
7425 | }
|
---|
7426 | #endif
|
---|
7427 |
|
---|
7428 | /* Often a unimplemented MSR or MSR bit, so worth logging. */
|
---|
7429 | if (pVCpu->iem.s.cLogRelWrMsr < 32)
|
---|
7430 | {
|
---|
7431 | pVCpu->iem.s.cLogRelWrMsr++;
|
---|
7432 | LogRel(("IEM: wrmsr(%#x,%#x`%08x) -> #GP(0)\n", idMsr, uValue.s.Hi, uValue.s.Lo));
|
---|
7433 | }
|
---|
7434 | else
|
---|
7435 | Log(( "IEM: wrmsr(%#x,%#x`%08x) -> #GP(0)\n", idMsr, uValue.s.Hi, uValue.s.Lo));
|
---|
7436 | AssertMsgReturn(rcStrict == VERR_CPUM_RAISE_GP_0, ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)), VERR_IPE_UNEXPECTED_STATUS);
|
---|
7437 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7438 | }
|
---|
7439 |
|
---|
7440 |
|
---|
7441 | /**
|
---|
7442 | * Implements 'IN eAX, port'.
|
---|
7443 | *
|
---|
7444 | * @param u16Port The source port.
|
---|
7445 | * @param cbReg The register size.
|
---|
7446 | * @param bImmAndEffAddrMode Bit 7: Whether the port was specified through an
|
---|
7447 | * immediate operand or the implicit DX register.
|
---|
7448 | * Bits 3-0: Effective address mode.
|
---|
7449 | */
|
---|
7450 | IEM_CIMPL_DEF_3(iemCImpl_in, uint16_t, u16Port, uint8_t, cbReg, uint8_t, bImmAndEffAddrMode)
|
---|
7451 | {
|
---|
7452 | /*
|
---|
7453 | * CPL check
|
---|
7454 | */
|
---|
7455 | VBOXSTRICTRC rcStrict = iemHlpCheckPortIOPermission(pVCpu, u16Port, cbReg);
|
---|
7456 | if (rcStrict != VINF_SUCCESS)
|
---|
7457 | return rcStrict;
|
---|
7458 |
|
---|
7459 | if (!IEM_IS_IN_GUEST(pVCpu))
|
---|
7460 | { /* probable */ }
|
---|
7461 |
|
---|
7462 | /*
|
---|
7463 | * Check VMX nested-guest IO intercept.
|
---|
7464 | */
|
---|
7465 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
7466 | else if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
7467 | {
|
---|
7468 | rcStrict = iemVmxVmexitInstrIo(pVCpu, VMXINSTRID_IO_IN, u16Port, RT_BOOL(bImmAndEffAddrMode & 0x80), cbReg, cbInstr);
|
---|
7469 | if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
|
---|
7470 | return rcStrict;
|
---|
7471 | }
|
---|
7472 | #endif
|
---|
7473 |
|
---|
7474 | /*
|
---|
7475 | * Check SVM nested-guest IO intercept.
|
---|
7476 | */
|
---|
7477 | #ifdef VBOX_WITH_NESTED_HWVIRT_SVM
|
---|
7478 | else if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_IOIO_PROT))
|
---|
7479 | {
|
---|
7480 | uint8_t cAddrSizeBits;
|
---|
7481 | switch (bImmAndEffAddrMode & 0xf)
|
---|
7482 | {
|
---|
7483 | case IEMMODE_16BIT: cAddrSizeBits = 16; break;
|
---|
7484 | case IEMMODE_32BIT: cAddrSizeBits = 32; break;
|
---|
7485 | case IEMMODE_64BIT: cAddrSizeBits = 64; break;
|
---|
7486 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
7487 | }
|
---|
7488 | rcStrict = iemSvmHandleIOIntercept(pVCpu, u16Port, SVMIOIOTYPE_IN, cbReg, cAddrSizeBits, 0 /* N/A - iEffSeg */,
|
---|
7489 | false /* fRep */, false /* fStrIo */, cbInstr);
|
---|
7490 | if (rcStrict == VINF_SVM_VMEXIT)
|
---|
7491 | return VINF_SUCCESS;
|
---|
7492 | if (rcStrict != VINF_SVM_INTERCEPT_NOT_ACTIVE)
|
---|
7493 | {
|
---|
7494 | Log(("iemCImpl_in: iemSvmHandleIOIntercept failed (u16Port=%#x, cbReg=%u) rc=%Rrc\n", u16Port, cbReg,
|
---|
7495 | VBOXSTRICTRC_VAL(rcStrict)));
|
---|
7496 | return rcStrict;
|
---|
7497 | }
|
---|
7498 | }
|
---|
7499 | #endif
|
---|
7500 | #if defined(VBOX_WITH_NESTED_HWVIRT_VMX) || defined(VBOX_WITH_NESTED_HWVIRT_SVM)
|
---|
7501 | RT_NOREF(bImmAndEffAddrMode);
|
---|
7502 | #endif
|
---|
7503 |
|
---|
7504 | /*
|
---|
7505 | * Perform the I/O.
|
---|
7506 | */
|
---|
7507 | PVMCC const pVM = pVCpu->CTX_SUFF(pVM);
|
---|
7508 | uint32_t u32Value = 0;
|
---|
7509 | rcStrict = IOMIOPortRead(pVM, pVCpu, u16Port, &u32Value, cbReg);
|
---|
7510 | if (IOM_SUCCESS(rcStrict))
|
---|
7511 | {
|
---|
7512 | switch (cbReg)
|
---|
7513 | {
|
---|
7514 | case 1: pVCpu->cpum.GstCtx.al = (uint8_t)u32Value; break;
|
---|
7515 | case 2: pVCpu->cpum.GstCtx.ax = (uint16_t)u32Value; break;
|
---|
7516 | case 4: pVCpu->cpum.GstCtx.rax = u32Value; break;
|
---|
7517 | default: AssertFailedReturn(VERR_IEM_IPE_3);
|
---|
7518 | }
|
---|
7519 |
|
---|
7520 | pVCpu->iem.s.cPotentialExits++;
|
---|
7521 | if (rcStrict != VINF_SUCCESS)
|
---|
7522 | iemSetPassUpStatus(pVCpu, rcStrict);
|
---|
7523 |
|
---|
7524 | /*
|
---|
7525 | * Check for I/O breakpoints before we complete the instruction.
|
---|
7526 | */
|
---|
7527 | uint32_t const fDr7 = pVCpu->cpum.GstCtx.dr[7];
|
---|
7528 | if (RT_UNLIKELY( ( ( (fDr7 & X86_DR7_ENABLED_MASK)
|
---|
7529 | && X86_DR7_ANY_RW_IO(fDr7)
|
---|
7530 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_DE))
|
---|
7531 | || pVM->dbgf.ro.cEnabledHwIoBreakpoints > 0)
|
---|
7532 | && rcStrict == VINF_SUCCESS))
|
---|
7533 | {
|
---|
7534 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3 | CPUMCTX_EXTRN_DR6);
|
---|
7535 | pVCpu->cpum.GstCtx.eflags.uBoth |= DBGFBpCheckIo2(pVM, pVCpu, u16Port, cbReg);
|
---|
7536 | }
|
---|
7537 |
|
---|
7538 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7539 | }
|
---|
7540 |
|
---|
7541 | return rcStrict;
|
---|
7542 | }
|
---|
7543 |
|
---|
7544 |
|
---|
7545 | /**
|
---|
7546 | * Implements 'IN eAX, DX'.
|
---|
7547 | *
|
---|
7548 | * @param cbReg The register size.
|
---|
7549 | * @param enmEffAddrMode Effective address mode.
|
---|
7550 | */
|
---|
7551 | IEM_CIMPL_DEF_2(iemCImpl_in_eAX_DX, uint8_t, cbReg, IEMMODE, enmEffAddrMode)
|
---|
7552 | {
|
---|
7553 | return IEM_CIMPL_CALL_3(iemCImpl_in, pVCpu->cpum.GstCtx.dx, cbReg, 0 /* fImm */ | enmEffAddrMode);
|
---|
7554 | }
|
---|
7555 |
|
---|
7556 |
|
---|
7557 | /**
|
---|
7558 | * Implements 'OUT port, eAX'.
|
---|
7559 | *
|
---|
7560 | * @param u16Port The destination port.
|
---|
7561 | * @param cbReg The register size.
|
---|
7562 | * @param bImmAndEffAddrMode Bit 7: Whether the port was specified through an
|
---|
7563 | * immediate operand or the implicit DX register.
|
---|
7564 | * Bits 3-0: Effective address mode.
|
---|
7565 | */
|
---|
7566 | IEM_CIMPL_DEF_3(iemCImpl_out, uint16_t, u16Port, uint8_t, cbReg, uint8_t, bImmAndEffAddrMode)
|
---|
7567 | {
|
---|
7568 | /*
|
---|
7569 | * CPL check
|
---|
7570 | */
|
---|
7571 | VBOXSTRICTRC rcStrict = iemHlpCheckPortIOPermission(pVCpu, u16Port, cbReg);
|
---|
7572 | if (rcStrict != VINF_SUCCESS)
|
---|
7573 | return rcStrict;
|
---|
7574 |
|
---|
7575 | if (!IEM_IS_IN_GUEST(pVCpu))
|
---|
7576 | { /* probable */ }
|
---|
7577 |
|
---|
7578 | /*
|
---|
7579 | * Check VMX nested-guest I/O intercept.
|
---|
7580 | */
|
---|
7581 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
7582 | else if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
7583 | {
|
---|
7584 | rcStrict = iemVmxVmexitInstrIo(pVCpu, VMXINSTRID_IO_OUT, u16Port, RT_BOOL(bImmAndEffAddrMode & 0x80), cbReg, cbInstr);
|
---|
7585 | if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
|
---|
7586 | return rcStrict;
|
---|
7587 | }
|
---|
7588 | #endif
|
---|
7589 |
|
---|
7590 | /*
|
---|
7591 | * Check SVM nested-guest I/O intercept.
|
---|
7592 | */
|
---|
7593 | #ifdef VBOX_WITH_NESTED_HWVIRT_SVM
|
---|
7594 | else if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_IOIO_PROT))
|
---|
7595 | {
|
---|
7596 | uint8_t cAddrSizeBits;
|
---|
7597 | switch (bImmAndEffAddrMode & 0xf)
|
---|
7598 | {
|
---|
7599 | case IEMMODE_16BIT: cAddrSizeBits = 16; break;
|
---|
7600 | case IEMMODE_32BIT: cAddrSizeBits = 32; break;
|
---|
7601 | case IEMMODE_64BIT: cAddrSizeBits = 64; break;
|
---|
7602 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
7603 | }
|
---|
7604 | rcStrict = iemSvmHandleIOIntercept(pVCpu, u16Port, SVMIOIOTYPE_OUT, cbReg, cAddrSizeBits, 0 /* N/A - iEffSeg */,
|
---|
7605 | false /* fRep */, false /* fStrIo */, cbInstr);
|
---|
7606 | if (rcStrict == VINF_SVM_VMEXIT)
|
---|
7607 | return VINF_SUCCESS;
|
---|
7608 | if (rcStrict != VINF_SVM_INTERCEPT_NOT_ACTIVE)
|
---|
7609 | {
|
---|
7610 | Log(("iemCImpl_out: iemSvmHandleIOIntercept failed (u16Port=%#x, cbReg=%u) rc=%Rrc\n", u16Port, cbReg,
|
---|
7611 | VBOXSTRICTRC_VAL(rcStrict)));
|
---|
7612 | return rcStrict;
|
---|
7613 | }
|
---|
7614 | }
|
---|
7615 | #endif
|
---|
7616 | #if defined(VBOX_WITH_NESTED_HWVIRT_VMX) || defined(VBOX_WITH_NESTED_HWVIRT_SVM)
|
---|
7617 | RT_NOREF(bImmAndEffAddrMode);
|
---|
7618 | #endif
|
---|
7619 |
|
---|
7620 | /*
|
---|
7621 | * Perform the I/O.
|
---|
7622 | */
|
---|
7623 | PVMCC const pVM = pVCpu->CTX_SUFF(pVM);
|
---|
7624 | uint32_t u32Value;
|
---|
7625 | switch (cbReg)
|
---|
7626 | {
|
---|
7627 | case 1: u32Value = pVCpu->cpum.GstCtx.al; break;
|
---|
7628 | case 2: u32Value = pVCpu->cpum.GstCtx.ax; break;
|
---|
7629 | case 4: u32Value = pVCpu->cpum.GstCtx.eax; break;
|
---|
7630 | default: AssertFailedReturn(VERR_IEM_IPE_4);
|
---|
7631 | }
|
---|
7632 | rcStrict = IOMIOPortWrite(pVM, pVCpu, u16Port, u32Value, cbReg);
|
---|
7633 | if (IOM_SUCCESS(rcStrict))
|
---|
7634 | {
|
---|
7635 | pVCpu->iem.s.cPotentialExits++;
|
---|
7636 | if (rcStrict != VINF_SUCCESS)
|
---|
7637 | iemSetPassUpStatus(pVCpu, rcStrict);
|
---|
7638 |
|
---|
7639 | /*
|
---|
7640 | * Check for I/O breakpoints before we complete the instruction.
|
---|
7641 | */
|
---|
7642 | uint32_t const fDr7 = pVCpu->cpum.GstCtx.dr[7];
|
---|
7643 | if (RT_UNLIKELY( ( ( (fDr7 & X86_DR7_ENABLED_MASK)
|
---|
7644 | && X86_DR7_ANY_RW_IO(fDr7)
|
---|
7645 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_DE))
|
---|
7646 | || pVM->dbgf.ro.cEnabledHwIoBreakpoints > 0)
|
---|
7647 | && rcStrict == VINF_SUCCESS))
|
---|
7648 | {
|
---|
7649 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3 | CPUMCTX_EXTRN_DR6);
|
---|
7650 | pVCpu->cpum.GstCtx.eflags.uBoth |= DBGFBpCheckIo2(pVM, pVCpu, u16Port, cbReg);
|
---|
7651 | }
|
---|
7652 |
|
---|
7653 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7654 | }
|
---|
7655 | return rcStrict;
|
---|
7656 | }
|
---|
7657 |
|
---|
7658 |
|
---|
7659 | /**
|
---|
7660 | * Implements 'OUT DX, eAX'.
|
---|
7661 | *
|
---|
7662 | * @param cbReg The register size.
|
---|
7663 | * @param enmEffAddrMode Effective address mode.
|
---|
7664 | */
|
---|
7665 | IEM_CIMPL_DEF_2(iemCImpl_out_DX_eAX, uint8_t, cbReg, IEMMODE, enmEffAddrMode)
|
---|
7666 | {
|
---|
7667 | return IEM_CIMPL_CALL_3(iemCImpl_out, pVCpu->cpum.GstCtx.dx, cbReg, 0 /* fImm */ | enmEffAddrMode);
|
---|
7668 | }
|
---|
7669 |
|
---|
7670 |
|
---|
7671 | /**
|
---|
7672 | * Implements 'CLI'.
|
---|
7673 | */
|
---|
7674 | IEM_CIMPL_DEF_0(iemCImpl_cli)
|
---|
7675 | {
|
---|
7676 | uint32_t fEfl = IEMMISC_GET_EFL(pVCpu);
|
---|
7677 | #ifdef LOG_ENABLED
|
---|
7678 | uint32_t const fEflOld = fEfl;
|
---|
7679 | #endif
|
---|
7680 |
|
---|
7681 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_CR4);
|
---|
7682 | if (pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE)
|
---|
7683 | {
|
---|
7684 | uint8_t const uIopl = X86_EFL_GET_IOPL(fEfl);
|
---|
7685 | if (!(fEfl & X86_EFL_VM))
|
---|
7686 | {
|
---|
7687 | if (IEM_GET_CPL(pVCpu) <= uIopl)
|
---|
7688 | fEfl &= ~X86_EFL_IF;
|
---|
7689 | else if ( IEM_GET_CPL(pVCpu) == 3
|
---|
7690 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_PVI) )
|
---|
7691 | fEfl &= ~X86_EFL_VIF;
|
---|
7692 | else
|
---|
7693 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7694 | }
|
---|
7695 | /* V8086 */
|
---|
7696 | else if (uIopl == 3)
|
---|
7697 | fEfl &= ~X86_EFL_IF;
|
---|
7698 | else if ( uIopl < 3
|
---|
7699 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_VME) )
|
---|
7700 | fEfl &= ~X86_EFL_VIF;
|
---|
7701 | else
|
---|
7702 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7703 | }
|
---|
7704 | /* real mode */
|
---|
7705 | else
|
---|
7706 | fEfl &= ~X86_EFL_IF;
|
---|
7707 |
|
---|
7708 | /* Commit. */
|
---|
7709 | IEMMISC_SET_EFL(pVCpu, fEfl);
|
---|
7710 | VBOXSTRICTRC const rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7711 | Log2(("CLI: %#x -> %#x\n", fEflOld, fEfl));
|
---|
7712 | return rcStrict;
|
---|
7713 | }
|
---|
7714 |
|
---|
7715 |
|
---|
7716 | /**
|
---|
7717 | * Implements 'STI'.
|
---|
7718 | */
|
---|
7719 | IEM_CIMPL_DEF_0(iemCImpl_sti)
|
---|
7720 | {
|
---|
7721 | uint32_t fEfl = IEMMISC_GET_EFL(pVCpu);
|
---|
7722 | uint32_t const fEflOld = fEfl;
|
---|
7723 |
|
---|
7724 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_CR4);
|
---|
7725 | if (pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE)
|
---|
7726 | {
|
---|
7727 | uint8_t const uIopl = X86_EFL_GET_IOPL(fEfl);
|
---|
7728 | if (!(fEfl & X86_EFL_VM))
|
---|
7729 | {
|
---|
7730 | if (IEM_GET_CPL(pVCpu) <= uIopl)
|
---|
7731 | fEfl |= X86_EFL_IF;
|
---|
7732 | else if ( IEM_GET_CPL(pVCpu) == 3
|
---|
7733 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_PVI)
|
---|
7734 | && !(fEfl & X86_EFL_VIP) )
|
---|
7735 | fEfl |= X86_EFL_VIF;
|
---|
7736 | else
|
---|
7737 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7738 | }
|
---|
7739 | /* V8086 */
|
---|
7740 | else if (uIopl == 3)
|
---|
7741 | fEfl |= X86_EFL_IF;
|
---|
7742 | else if ( uIopl < 3
|
---|
7743 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_VME)
|
---|
7744 | && !(fEfl & X86_EFL_VIP) )
|
---|
7745 | fEfl |= X86_EFL_VIF;
|
---|
7746 | else
|
---|
7747 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7748 | }
|
---|
7749 | /* real mode */
|
---|
7750 | else
|
---|
7751 | fEfl |= X86_EFL_IF;
|
---|
7752 |
|
---|
7753 | /*
|
---|
7754 | * Commit.
|
---|
7755 | *
|
---|
7756 | * Note! Setting the shadow interrupt flag must be done after RIP updating.
|
---|
7757 | */
|
---|
7758 | IEMMISC_SET_EFL(pVCpu, fEfl);
|
---|
7759 | VBOXSTRICTRC const rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7760 | if (!(fEflOld & X86_EFL_IF) && (fEfl & X86_EFL_IF))
|
---|
7761 | {
|
---|
7762 | /** @todo only set it the shadow flag if it was clear before? */
|
---|
7763 | CPUMSetInInterruptShadowSti(&pVCpu->cpum.GstCtx);
|
---|
7764 | }
|
---|
7765 | Log2(("STI: %#x -> %#x\n", fEflOld, fEfl));
|
---|
7766 | return rcStrict;
|
---|
7767 | }
|
---|
7768 |
|
---|
7769 |
|
---|
7770 | /**
|
---|
7771 | * Implements 'HLT'.
|
---|
7772 | */
|
---|
7773 | IEM_CIMPL_DEF_0(iemCImpl_hlt)
|
---|
7774 | {
|
---|
7775 | if (IEM_GET_CPL(pVCpu) != 0)
|
---|
7776 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7777 |
|
---|
7778 | if (!IEM_IS_IN_GUEST(pVCpu))
|
---|
7779 | { /* probable */ }
|
---|
7780 | else if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
7781 | && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_HLT_EXIT))
|
---|
7782 | {
|
---|
7783 | Log2(("hlt: Guest intercept -> VM-exit\n"));
|
---|
7784 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_HLT, cbInstr);
|
---|
7785 | }
|
---|
7786 | else if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_HLT))
|
---|
7787 | {
|
---|
7788 | Log2(("hlt: Guest intercept -> #VMEXIT\n"));
|
---|
7789 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
7790 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_HLT, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
7791 | }
|
---|
7792 |
|
---|
7793 | /** @todo finish: This ASSUMES that iemRegAddToRipAndFinishingClearingRF won't
|
---|
7794 | * be returning any status codes relating to non-guest events being raised, as
|
---|
7795 | * we'll mess up the guest HALT otherwise. */
|
---|
7796 | VBOXSTRICTRC rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7797 | if (rcStrict == VINF_SUCCESS)
|
---|
7798 | rcStrict = VINF_EM_HALT;
|
---|
7799 | return rcStrict;
|
---|
7800 | }
|
---|
7801 |
|
---|
7802 |
|
---|
7803 | /**
|
---|
7804 | * Implements 'MONITOR'.
|
---|
7805 | */
|
---|
7806 | IEM_CIMPL_DEF_1(iemCImpl_monitor, uint8_t, iEffSeg)
|
---|
7807 | {
|
---|
7808 | /*
|
---|
7809 | * Permission checks.
|
---|
7810 | */
|
---|
7811 | if (IEM_GET_CPL(pVCpu) != 0)
|
---|
7812 | {
|
---|
7813 | Log2(("monitor: CPL != 0\n"));
|
---|
7814 | return iemRaiseUndefinedOpcode(pVCpu); /** @todo MSR[0xC0010015].MonMwaitUserEn if we care. */
|
---|
7815 | }
|
---|
7816 | if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fMonitorMWait)
|
---|
7817 | {
|
---|
7818 | Log2(("monitor: Not in CPUID\n"));
|
---|
7819 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
7820 | }
|
---|
7821 |
|
---|
7822 | /*
|
---|
7823 | * Check VMX guest-intercept.
|
---|
7824 | * This should be considered a fault-like VM-exit.
|
---|
7825 | * See Intel spec. 25.1.1 "Relative Priority of Faults and VM Exits".
|
---|
7826 | */
|
---|
7827 | if ( !IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
7828 | || !IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_MONITOR_EXIT))
|
---|
7829 | { /* probable */ }
|
---|
7830 | else
|
---|
7831 | {
|
---|
7832 | Log2(("monitor: Guest intercept -> #VMEXIT\n"));
|
---|
7833 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_MONITOR, cbInstr);
|
---|
7834 | }
|
---|
7835 |
|
---|
7836 | /*
|
---|
7837 | * Gather the operands and validate them.
|
---|
7838 | */
|
---|
7839 | RTGCPTR GCPtrMem = IEM_IS_64BIT_CODE(pVCpu) ? pVCpu->cpum.GstCtx.rax : pVCpu->cpum.GstCtx.eax;
|
---|
7840 | uint32_t uEcx = pVCpu->cpum.GstCtx.ecx;
|
---|
7841 | uint32_t uEdx = pVCpu->cpum.GstCtx.edx;
|
---|
7842 | /** @todo Test whether EAX or ECX is processed first, i.e. do we get \#PF or
|
---|
7843 | * \#GP first. */
|
---|
7844 | if (uEcx != 0)
|
---|
7845 | {
|
---|
7846 | Log2(("monitor rax=%RX64, ecx=%RX32, edx=%RX32; ECX != 0 -> #GP(0)\n", GCPtrMem, uEcx, uEdx)); NOREF(uEdx);
|
---|
7847 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7848 | }
|
---|
7849 |
|
---|
7850 | VBOXSTRICTRC rcStrict = iemMemApplySegment(pVCpu, IEM_ACCESS_TYPE_READ | IEM_ACCESS_WHAT_DATA, iEffSeg, 1, &GCPtrMem);
|
---|
7851 | if (rcStrict != VINF_SUCCESS)
|
---|
7852 | return rcStrict;
|
---|
7853 |
|
---|
7854 | RTGCPHYS GCPhysMem;
|
---|
7855 | /** @todo access size */
|
---|
7856 | rcStrict = iemMemPageTranslateAndCheckAccess(pVCpu, GCPtrMem, 1, IEM_ACCESS_TYPE_READ | IEM_ACCESS_WHAT_DATA, &GCPhysMem);
|
---|
7857 | if (rcStrict != VINF_SUCCESS)
|
---|
7858 | return rcStrict;
|
---|
7859 |
|
---|
7860 | if (!IEM_IS_IN_GUEST(pVCpu))
|
---|
7861 | { /* probable */ }
|
---|
7862 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
7863 | else if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
7864 | && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_VIRT_APIC_ACCESS))
|
---|
7865 | {
|
---|
7866 | /*
|
---|
7867 | * MONITOR does not access the memory, just monitors the address. However,
|
---|
7868 | * if the address falls in the APIC-access page, the address monitored must
|
---|
7869 | * instead be the corresponding address in the virtual-APIC page.
|
---|
7870 | *
|
---|
7871 | * See Intel spec. 29.4.4 "Instruction-Specific Considerations".
|
---|
7872 | */
|
---|
7873 | rcStrict = iemVmxVirtApicAccessUnused(pVCpu, &GCPhysMem, 1, IEM_ACCESS_TYPE_READ | IEM_ACCESS_WHAT_DATA);
|
---|
7874 | if ( rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE
|
---|
7875 | && rcStrict != VINF_VMX_MODIFIES_BEHAVIOR)
|
---|
7876 | return rcStrict;
|
---|
7877 | }
|
---|
7878 | #endif
|
---|
7879 | else if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_MONITOR))
|
---|
7880 | {
|
---|
7881 | Log2(("monitor: Guest intercept -> #VMEXIT\n"));
|
---|
7882 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
7883 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_MONITOR, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
7884 | }
|
---|
7885 |
|
---|
7886 | /*
|
---|
7887 | * Call EM to prepare the monitor/wait.
|
---|
7888 | */
|
---|
7889 | rcStrict = EMMonitorWaitPrepare(pVCpu, pVCpu->cpum.GstCtx.rax, pVCpu->cpum.GstCtx.rcx, pVCpu->cpum.GstCtx.rdx, GCPhysMem);
|
---|
7890 | Assert(rcStrict == VINF_SUCCESS);
|
---|
7891 | if (rcStrict == VINF_SUCCESS)
|
---|
7892 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7893 | return rcStrict;
|
---|
7894 | }
|
---|
7895 |
|
---|
7896 |
|
---|
7897 | /**
|
---|
7898 | * Implements 'MWAIT'.
|
---|
7899 | */
|
---|
7900 | IEM_CIMPL_DEF_0(iemCImpl_mwait)
|
---|
7901 | {
|
---|
7902 | /*
|
---|
7903 | * Permission checks.
|
---|
7904 | */
|
---|
7905 | if (IEM_GET_CPL(pVCpu) != 0)
|
---|
7906 | {
|
---|
7907 | Log2(("mwait: CPL != 0\n"));
|
---|
7908 | /** @todo MSR[0xC0010015].MonMwaitUserEn if we care. (Remember to check
|
---|
7909 | * EFLAGS.VM then.) */
|
---|
7910 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
7911 | }
|
---|
7912 | if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fMonitorMWait)
|
---|
7913 | {
|
---|
7914 | Log2(("mwait: Not in CPUID\n"));
|
---|
7915 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
7916 | }
|
---|
7917 |
|
---|
7918 | /* Check VMX nested-guest intercept. */
|
---|
7919 | if ( !IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
7920 | || !IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_MWAIT_EXIT))
|
---|
7921 | { /* probable */ }
|
---|
7922 | else
|
---|
7923 | IEM_VMX_VMEXIT_MWAIT_RET(pVCpu, EMMonitorIsArmed(pVCpu), cbInstr);
|
---|
7924 |
|
---|
7925 | /*
|
---|
7926 | * Gather the operands and validate them.
|
---|
7927 | */
|
---|
7928 | uint32_t const uEax = pVCpu->cpum.GstCtx.eax;
|
---|
7929 | uint32_t const uEcx = pVCpu->cpum.GstCtx.ecx;
|
---|
7930 | if (uEcx != 0)
|
---|
7931 | {
|
---|
7932 | /* Only supported extension is break on IRQ when IF=0. */
|
---|
7933 | if (uEcx > 1)
|
---|
7934 | {
|
---|
7935 | Log2(("mwait eax=%RX32, ecx=%RX32; ECX > 1 -> #GP(0)\n", uEax, uEcx));
|
---|
7936 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7937 | }
|
---|
7938 | uint32_t fMWaitFeatures = 0;
|
---|
7939 | uint32_t uIgnore = 0;
|
---|
7940 | CPUMGetGuestCpuId(pVCpu, 5, 0, -1 /*f64BitMode*/, &uIgnore, &uIgnore, &fMWaitFeatures, &uIgnore);
|
---|
7941 | if ( (fMWaitFeatures & (X86_CPUID_MWAIT_ECX_EXT | X86_CPUID_MWAIT_ECX_BREAKIRQIF0))
|
---|
7942 | != (X86_CPUID_MWAIT_ECX_EXT | X86_CPUID_MWAIT_ECX_BREAKIRQIF0))
|
---|
7943 | {
|
---|
7944 | Log2(("mwait eax=%RX32, ecx=%RX32; break-on-IRQ-IF=0 extension not enabled -> #GP(0)\n", uEax, uEcx));
|
---|
7945 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7946 | }
|
---|
7947 |
|
---|
7948 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
7949 | /*
|
---|
7950 | * If the interrupt-window exiting control is set or a virtual-interrupt is pending
|
---|
7951 | * for delivery; and interrupts are disabled the processor does not enter its
|
---|
7952 | * mwait state but rather passes control to the next instruction.
|
---|
7953 | *
|
---|
7954 | * See Intel spec. 25.3 "Changes to Instruction Behavior In VMX Non-root Operation".
|
---|
7955 | */
|
---|
7956 | if ( !IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
7957 | || pVCpu->cpum.GstCtx.eflags.Bits.u1IF)
|
---|
7958 | { /* probable */ }
|
---|
7959 | else if ( IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_INT_WINDOW_EXIT)
|
---|
7960 | || VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_NESTED_GUEST))
|
---|
7961 | /** @todo finish: check up this out after we move int window stuff out of the
|
---|
7962 | * run loop and into the instruction finishing logic here. */
|
---|
7963 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7964 | #endif
|
---|
7965 | }
|
---|
7966 |
|
---|
7967 | /*
|
---|
7968 | * Check SVM nested-guest mwait intercepts.
|
---|
7969 | */
|
---|
7970 | if (!IEM_IS_IN_GUEST(pVCpu))
|
---|
7971 | { /* probable */ }
|
---|
7972 | else if ( IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_MWAIT_ARMED)
|
---|
7973 | && EMMonitorIsArmed(pVCpu))
|
---|
7974 | {
|
---|
7975 | Log2(("mwait: Guest intercept (monitor hardware armed) -> #VMEXIT\n"));
|
---|
7976 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
7977 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_MWAIT_ARMED, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
7978 | }
|
---|
7979 | else if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_MWAIT))
|
---|
7980 | {
|
---|
7981 | Log2(("mwait: Guest intercept -> #VMEXIT\n"));
|
---|
7982 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
7983 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_MWAIT, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
7984 | }
|
---|
7985 |
|
---|
7986 | /*
|
---|
7987 | * Call EM to prepare the monitor/wait.
|
---|
7988 | *
|
---|
7989 | * This will return VINF_EM_HALT. If there the trap flag is set, we may
|
---|
7990 | * override it when executing iemRegAddToRipAndFinishingClearingRF ASSUMING
|
---|
7991 | * that will only return guest related events.
|
---|
7992 | */
|
---|
7993 | VBOXSTRICTRC rcStrict = EMMonitorWaitPerform(pVCpu, uEax, uEcx);
|
---|
7994 |
|
---|
7995 | /** @todo finish: This needs more thinking as we should suppress internal
|
---|
7996 | * debugger events here, or we'll bugger up the guest state even more than we
|
---|
7997 | * alread do around VINF_EM_HALT. */
|
---|
7998 | VBOXSTRICTRC rcStrict2 = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
7999 | if (rcStrict2 != VINF_SUCCESS)
|
---|
8000 | {
|
---|
8001 | Log2(("mwait: %Rrc (perform) -> %Rrc (finish)!\n", VBOXSTRICTRC_VAL(rcStrict), VBOXSTRICTRC_VAL(rcStrict2) ));
|
---|
8002 | rcStrict = rcStrict2;
|
---|
8003 | }
|
---|
8004 |
|
---|
8005 | return rcStrict;
|
---|
8006 | }
|
---|
8007 |
|
---|
8008 |
|
---|
8009 | /**
|
---|
8010 | * Implements 'SWAPGS'.
|
---|
8011 | */
|
---|
8012 | IEM_CIMPL_DEF_0(iemCImpl_swapgs)
|
---|
8013 | {
|
---|
8014 | Assert(IEM_IS_64BIT_CODE(pVCpu)); /* Caller checks this. */
|
---|
8015 |
|
---|
8016 | /*
|
---|
8017 | * Permission checks.
|
---|
8018 | */
|
---|
8019 | if (IEM_GET_CPL(pVCpu) != 0)
|
---|
8020 | {
|
---|
8021 | Log2(("swapgs: CPL != 0\n"));
|
---|
8022 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
8023 | }
|
---|
8024 |
|
---|
8025 | /*
|
---|
8026 | * Do the job.
|
---|
8027 | */
|
---|
8028 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_KERNEL_GS_BASE | CPUMCTX_EXTRN_GS);
|
---|
8029 | uint64_t uOtherGsBase = pVCpu->cpum.GstCtx.msrKERNELGSBASE;
|
---|
8030 | pVCpu->cpum.GstCtx.msrKERNELGSBASE = pVCpu->cpum.GstCtx.gs.u64Base;
|
---|
8031 | pVCpu->cpum.GstCtx.gs.u64Base = uOtherGsBase;
|
---|
8032 |
|
---|
8033 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8034 | }
|
---|
8035 |
|
---|
8036 |
|
---|
8037 | #ifndef VBOX_WITHOUT_CPUID_HOST_CALL
|
---|
8038 | /**
|
---|
8039 | * Handles a CPUID call.
|
---|
8040 | */
|
---|
8041 | static VBOXSTRICTRC iemCpuIdVBoxCall(PVMCPUCC pVCpu, uint32_t iFunction,
|
---|
8042 | uint32_t *pEax, uint32_t *pEbx, uint32_t *pEcx, uint32_t *pEdx)
|
---|
8043 | {
|
---|
8044 | switch (iFunction)
|
---|
8045 | {
|
---|
8046 | case VBOX_CPUID_FN_ID:
|
---|
8047 | LogFlow(("iemCpuIdVBoxCall: VBOX_CPUID_FN_ID\n"));
|
---|
8048 | *pEax = VBOX_CPUID_RESP_ID_EAX;
|
---|
8049 | *pEbx = VBOX_CPUID_RESP_ID_EBX;
|
---|
8050 | *pEcx = VBOX_CPUID_RESP_ID_ECX;
|
---|
8051 | *pEdx = VBOX_CPUID_RESP_ID_EDX;
|
---|
8052 | break;
|
---|
8053 |
|
---|
8054 | case VBOX_CPUID_FN_LOG:
|
---|
8055 | {
|
---|
8056 | CPUM_IMPORT_EXTRN_RET(pVCpu, CPUMCTX_EXTRN_RDX | CPUMCTX_EXTRN_RBX | CPUMCTX_EXTRN_RSI
|
---|
8057 | | IEM_CPUMCTX_EXTRN_EXEC_DECODED_MEM_MASK);
|
---|
8058 |
|
---|
8059 | /* Validate input. */
|
---|
8060 | uint32_t cchToLog = *pEdx;
|
---|
8061 | if (cchToLog <= _2M)
|
---|
8062 | {
|
---|
8063 | uint32_t const uLogPicker = *pEbx;
|
---|
8064 | if (uLogPicker <= 1)
|
---|
8065 | {
|
---|
8066 | /* Resolve the logger. */
|
---|
8067 | PRTLOGGER const pLogger = !uLogPicker
|
---|
8068 | ? RTLogDefaultInstanceEx(UINT32_MAX) : RTLogRelGetDefaultInstanceEx(UINT32_MAX);
|
---|
8069 | if (pLogger)
|
---|
8070 | {
|
---|
8071 | /* Copy over the data: */
|
---|
8072 | RTGCPTR GCPtrSrc = pVCpu->cpum.GstCtx.rsi;
|
---|
8073 | while (cchToLog > 0)
|
---|
8074 | {
|
---|
8075 | uint32_t cbToMap = GUEST_PAGE_SIZE - (GCPtrSrc & GUEST_PAGE_OFFSET_MASK);
|
---|
8076 | if (cbToMap > cchToLog)
|
---|
8077 | cbToMap = cchToLog;
|
---|
8078 | /** @todo Extend iemMemMap to allowing page size accessing and avoid 7
|
---|
8079 | * unnecessary calls & iterations per pages. */
|
---|
8080 | if (cbToMap > 512)
|
---|
8081 | cbToMap = 512;
|
---|
8082 | void *pvSrc = NULL;
|
---|
8083 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, &pvSrc, cbToMap, UINT8_MAX, GCPtrSrc, IEM_ACCESS_DATA_R, 0);
|
---|
8084 | if (rcStrict == VINF_SUCCESS)
|
---|
8085 | {
|
---|
8086 | RTLogBulkNestedWrite(pLogger, (const char *)pvSrc, cbToMap, "Gst:");
|
---|
8087 | rcStrict = iemMemCommitAndUnmap(pVCpu, pvSrc, IEM_ACCESS_DATA_R);
|
---|
8088 | AssertRCSuccessReturn(VBOXSTRICTRC_VAL(rcStrict), rcStrict);
|
---|
8089 | }
|
---|
8090 | else
|
---|
8091 | {
|
---|
8092 | Log(("iemCpuIdVBoxCall: %Rrc at %RGp LB %#x\n", VBOXSTRICTRC_VAL(rcStrict), GCPtrSrc, cbToMap));
|
---|
8093 | return rcStrict;
|
---|
8094 | }
|
---|
8095 |
|
---|
8096 | /* Advance. */
|
---|
8097 | pVCpu->cpum.GstCtx.rsi = GCPtrSrc += cbToMap;
|
---|
8098 | *pEdx = cchToLog -= cbToMap;
|
---|
8099 | }
|
---|
8100 | *pEax = VINF_SUCCESS;
|
---|
8101 | }
|
---|
8102 | else
|
---|
8103 | *pEax = (uint32_t)VERR_NOT_FOUND;
|
---|
8104 | }
|
---|
8105 | else
|
---|
8106 | *pEax = (uint32_t)VERR_NOT_FOUND;
|
---|
8107 | }
|
---|
8108 | else
|
---|
8109 | *pEax = (uint32_t)VERR_TOO_MUCH_DATA;
|
---|
8110 | *pEdx = VBOX_CPUID_RESP_GEN_EDX;
|
---|
8111 | *pEcx = VBOX_CPUID_RESP_GEN_ECX;
|
---|
8112 | *pEbx = VBOX_CPUID_RESP_GEN_EBX;
|
---|
8113 | break;
|
---|
8114 | }
|
---|
8115 |
|
---|
8116 | default:
|
---|
8117 | LogFlow(("iemCpuIdVBoxCall: Invalid function %#x (%#x, %#x)\n", iFunction, *pEbx, *pEdx));
|
---|
8118 | *pEax = (uint32_t)VERR_INVALID_FUNCTION;
|
---|
8119 | *pEbx = (uint32_t)VERR_INVALID_FUNCTION;
|
---|
8120 | *pEcx = (uint32_t)VERR_INVALID_FUNCTION;
|
---|
8121 | *pEdx = (uint32_t)VERR_INVALID_FUNCTION;
|
---|
8122 | break;
|
---|
8123 | }
|
---|
8124 | return VINF_SUCCESS;
|
---|
8125 | }
|
---|
8126 | #endif /* VBOX_WITHOUT_CPUID_HOST_CALL */
|
---|
8127 |
|
---|
8128 | /**
|
---|
8129 | * Implements 'CPUID'.
|
---|
8130 | */
|
---|
8131 | IEM_CIMPL_DEF_0(iemCImpl_cpuid)
|
---|
8132 | {
|
---|
8133 | if (!IEM_IS_IN_GUEST(pVCpu))
|
---|
8134 | { /* probable */ }
|
---|
8135 | else if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
8136 | {
|
---|
8137 | Log2(("cpuid: Guest intercept -> VM-exit\n"));
|
---|
8138 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_CPUID, cbInstr);
|
---|
8139 | }
|
---|
8140 | else if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_CPUID))
|
---|
8141 | {
|
---|
8142 | Log2(("cpuid: Guest intercept -> #VMEXIT\n"));
|
---|
8143 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
8144 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_CPUID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
8145 | }
|
---|
8146 |
|
---|
8147 |
|
---|
8148 | uint32_t const uEax = pVCpu->cpum.GstCtx.eax;
|
---|
8149 | uint32_t const uEcx = pVCpu->cpum.GstCtx.ecx;
|
---|
8150 |
|
---|
8151 | #ifndef VBOX_WITHOUT_CPUID_HOST_CALL
|
---|
8152 | /*
|
---|
8153 | * CPUID host call backdoor.
|
---|
8154 | */
|
---|
8155 | if ( uEax == VBOX_CPUID_REQ_EAX_FIXED
|
---|
8156 | && (uEcx & VBOX_CPUID_REQ_ECX_FIXED_MASK) == VBOX_CPUID_REQ_ECX_FIXED
|
---|
8157 | && pVCpu->CTX_SUFF(pVM)->iem.s.fCpuIdHostCall)
|
---|
8158 | {
|
---|
8159 | VBOXSTRICTRC rcStrict = iemCpuIdVBoxCall(pVCpu, uEcx & VBOX_CPUID_REQ_ECX_FN_MASK,
|
---|
8160 | &pVCpu->cpum.GstCtx.eax, &pVCpu->cpum.GstCtx.ebx,
|
---|
8161 | &pVCpu->cpum.GstCtx.ecx, &pVCpu->cpum.GstCtx.edx);
|
---|
8162 | if (rcStrict != VINF_SUCCESS)
|
---|
8163 | return rcStrict;
|
---|
8164 | }
|
---|
8165 | /*
|
---|
8166 | * Regular CPUID.
|
---|
8167 | */
|
---|
8168 | else
|
---|
8169 | #endif
|
---|
8170 | CPUMGetGuestCpuId(pVCpu, uEax, uEcx, pVCpu->cpum.GstCtx.cs.Attr.n.u1Long,
|
---|
8171 | &pVCpu->cpum.GstCtx.eax, &pVCpu->cpum.GstCtx.ebx, &pVCpu->cpum.GstCtx.ecx, &pVCpu->cpum.GstCtx.edx);
|
---|
8172 |
|
---|
8173 | pVCpu->cpum.GstCtx.rax &= UINT32_C(0xffffffff);
|
---|
8174 | pVCpu->cpum.GstCtx.rbx &= UINT32_C(0xffffffff);
|
---|
8175 | pVCpu->cpum.GstCtx.rcx &= UINT32_C(0xffffffff);
|
---|
8176 | pVCpu->cpum.GstCtx.rdx &= UINT32_C(0xffffffff);
|
---|
8177 | pVCpu->cpum.GstCtx.fExtrn &= ~(CPUMCTX_EXTRN_RAX | CPUMCTX_EXTRN_RCX | CPUMCTX_EXTRN_RDX | CPUMCTX_EXTRN_RBX);
|
---|
8178 |
|
---|
8179 | pVCpu->iem.s.cPotentialExits++;
|
---|
8180 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8181 | }
|
---|
8182 |
|
---|
8183 |
|
---|
8184 | /**
|
---|
8185 | * Implements 'AAD'.
|
---|
8186 | *
|
---|
8187 | * @param bImm The immediate operand.
|
---|
8188 | */
|
---|
8189 | IEM_CIMPL_DEF_1(iemCImpl_aad, uint8_t, bImm)
|
---|
8190 | {
|
---|
8191 | uint16_t const ax = pVCpu->cpum.GstCtx.ax;
|
---|
8192 | uint8_t const al = (uint8_t)ax + (uint8_t)(ax >> 8) * bImm;
|
---|
8193 | pVCpu->cpum.GstCtx.ax = al;
|
---|
8194 | iemHlpUpdateArithEFlagsU8(pVCpu, al,
|
---|
8195 | X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF,
|
---|
8196 | X86_EFL_OF | X86_EFL_AF | X86_EFL_CF);
|
---|
8197 |
|
---|
8198 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8199 | }
|
---|
8200 |
|
---|
8201 |
|
---|
8202 | /**
|
---|
8203 | * Implements 'AAM'.
|
---|
8204 | *
|
---|
8205 | * @param bImm The immediate operand. Cannot be 0.
|
---|
8206 | */
|
---|
8207 | IEM_CIMPL_DEF_1(iemCImpl_aam, uint8_t, bImm)
|
---|
8208 | {
|
---|
8209 | Assert(bImm != 0); /* #DE on 0 is handled in the decoder. */
|
---|
8210 |
|
---|
8211 | uint16_t const ax = pVCpu->cpum.GstCtx.ax;
|
---|
8212 | uint8_t const al = (uint8_t)ax % bImm;
|
---|
8213 | uint8_t const ah = (uint8_t)ax / bImm;
|
---|
8214 | pVCpu->cpum.GstCtx.ax = (ah << 8) + al;
|
---|
8215 | iemHlpUpdateArithEFlagsU8(pVCpu, al,
|
---|
8216 | X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF,
|
---|
8217 | X86_EFL_OF | X86_EFL_AF | X86_EFL_CF);
|
---|
8218 |
|
---|
8219 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8220 | }
|
---|
8221 |
|
---|
8222 |
|
---|
8223 | /**
|
---|
8224 | * Implements 'DAA'.
|
---|
8225 | */
|
---|
8226 | IEM_CIMPL_DEF_0(iemCImpl_daa)
|
---|
8227 | {
|
---|
8228 | uint8_t const al = pVCpu->cpum.GstCtx.al;
|
---|
8229 | bool const fCarry = pVCpu->cpum.GstCtx.eflags.Bits.u1CF;
|
---|
8230 |
|
---|
8231 | if ( pVCpu->cpum.GstCtx.eflags.Bits.u1AF
|
---|
8232 | || (al & 0xf) >= 10)
|
---|
8233 | {
|
---|
8234 | pVCpu->cpum.GstCtx.al = al + 6;
|
---|
8235 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 1;
|
---|
8236 | }
|
---|
8237 | else
|
---|
8238 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 0;
|
---|
8239 |
|
---|
8240 | if (al >= 0x9a || fCarry)
|
---|
8241 | {
|
---|
8242 | pVCpu->cpum.GstCtx.al += 0x60;
|
---|
8243 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
|
---|
8244 | }
|
---|
8245 | else
|
---|
8246 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 0;
|
---|
8247 |
|
---|
8248 | iemHlpUpdateArithEFlagsU8(pVCpu, pVCpu->cpum.GstCtx.al, X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF, X86_EFL_OF);
|
---|
8249 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8250 | }
|
---|
8251 |
|
---|
8252 |
|
---|
8253 | /**
|
---|
8254 | * Implements 'DAS'.
|
---|
8255 | */
|
---|
8256 | IEM_CIMPL_DEF_0(iemCImpl_das)
|
---|
8257 | {
|
---|
8258 | uint8_t const uInputAL = pVCpu->cpum.GstCtx.al;
|
---|
8259 | bool const fCarry = pVCpu->cpum.GstCtx.eflags.Bits.u1CF;
|
---|
8260 |
|
---|
8261 | if ( pVCpu->cpum.GstCtx.eflags.Bits.u1AF
|
---|
8262 | || (uInputAL & 0xf) >= 10)
|
---|
8263 | {
|
---|
8264 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 1;
|
---|
8265 | if (uInputAL < 6)
|
---|
8266 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
|
---|
8267 | pVCpu->cpum.GstCtx.al = uInputAL - 6;
|
---|
8268 | }
|
---|
8269 | else
|
---|
8270 | {
|
---|
8271 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 0;
|
---|
8272 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 0;
|
---|
8273 | }
|
---|
8274 |
|
---|
8275 | if (uInputAL >= 0x9a || fCarry)
|
---|
8276 | {
|
---|
8277 | pVCpu->cpum.GstCtx.al -= 0x60;
|
---|
8278 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
|
---|
8279 | }
|
---|
8280 |
|
---|
8281 | iemHlpUpdateArithEFlagsU8(pVCpu, pVCpu->cpum.GstCtx.al, X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF, X86_EFL_OF);
|
---|
8282 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8283 | }
|
---|
8284 |
|
---|
8285 |
|
---|
8286 | /**
|
---|
8287 | * Implements 'AAA'.
|
---|
8288 | */
|
---|
8289 | IEM_CIMPL_DEF_0(iemCImpl_aaa)
|
---|
8290 | {
|
---|
8291 | if (IEM_IS_GUEST_CPU_AMD(pVCpu))
|
---|
8292 | {
|
---|
8293 | if ( pVCpu->cpum.GstCtx.eflags.Bits.u1AF
|
---|
8294 | || (pVCpu->cpum.GstCtx.ax & 0xf) >= 10)
|
---|
8295 | {
|
---|
8296 | iemAImpl_add_u16(&pVCpu->cpum.GstCtx.ax, 0x106, &pVCpu->cpum.GstCtx.eflags.uBoth);
|
---|
8297 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 1;
|
---|
8298 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
|
---|
8299 | }
|
---|
8300 | else
|
---|
8301 | {
|
---|
8302 | iemHlpUpdateArithEFlagsU16(pVCpu, pVCpu->cpum.GstCtx.ax, X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF, X86_EFL_OF);
|
---|
8303 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 0;
|
---|
8304 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 0;
|
---|
8305 | }
|
---|
8306 | pVCpu->cpum.GstCtx.ax &= UINT16_C(0xff0f);
|
---|
8307 | }
|
---|
8308 | else
|
---|
8309 | {
|
---|
8310 | if ( pVCpu->cpum.GstCtx.eflags.Bits.u1AF
|
---|
8311 | || (pVCpu->cpum.GstCtx.ax & 0xf) >= 10)
|
---|
8312 | {
|
---|
8313 | pVCpu->cpum.GstCtx.ax += UINT16_C(0x106);
|
---|
8314 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 1;
|
---|
8315 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
|
---|
8316 | }
|
---|
8317 | else
|
---|
8318 | {
|
---|
8319 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 0;
|
---|
8320 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 0;
|
---|
8321 | }
|
---|
8322 | pVCpu->cpum.GstCtx.ax &= UINT16_C(0xff0f);
|
---|
8323 | iemHlpUpdateArithEFlagsU8(pVCpu, pVCpu->cpum.GstCtx.al, X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF, X86_EFL_OF);
|
---|
8324 | }
|
---|
8325 |
|
---|
8326 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8327 | }
|
---|
8328 |
|
---|
8329 |
|
---|
8330 | /**
|
---|
8331 | * Implements 'AAS'.
|
---|
8332 | */
|
---|
8333 | IEM_CIMPL_DEF_0(iemCImpl_aas)
|
---|
8334 | {
|
---|
8335 | if (IEM_IS_GUEST_CPU_AMD(pVCpu))
|
---|
8336 | {
|
---|
8337 | if ( pVCpu->cpum.GstCtx.eflags.Bits.u1AF
|
---|
8338 | || (pVCpu->cpum.GstCtx.ax & 0xf) >= 10)
|
---|
8339 | {
|
---|
8340 | iemAImpl_sub_u16(&pVCpu->cpum.GstCtx.ax, 0x106, &pVCpu->cpum.GstCtx.eflags.uBoth);
|
---|
8341 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 1;
|
---|
8342 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
|
---|
8343 | }
|
---|
8344 | else
|
---|
8345 | {
|
---|
8346 | iemHlpUpdateArithEFlagsU16(pVCpu, pVCpu->cpum.GstCtx.ax, X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF, X86_EFL_OF);
|
---|
8347 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 0;
|
---|
8348 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 0;
|
---|
8349 | }
|
---|
8350 | pVCpu->cpum.GstCtx.ax &= UINT16_C(0xff0f);
|
---|
8351 | }
|
---|
8352 | else
|
---|
8353 | {
|
---|
8354 | if ( pVCpu->cpum.GstCtx.eflags.Bits.u1AF
|
---|
8355 | || (pVCpu->cpum.GstCtx.ax & 0xf) >= 10)
|
---|
8356 | {
|
---|
8357 | pVCpu->cpum.GstCtx.ax -= UINT16_C(0x106);
|
---|
8358 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 1;
|
---|
8359 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
|
---|
8360 | }
|
---|
8361 | else
|
---|
8362 | {
|
---|
8363 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 0;
|
---|
8364 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 0;
|
---|
8365 | }
|
---|
8366 | pVCpu->cpum.GstCtx.ax &= UINT16_C(0xff0f);
|
---|
8367 | iemHlpUpdateArithEFlagsU8(pVCpu, pVCpu->cpum.GstCtx.al, X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF, X86_EFL_OF);
|
---|
8368 | }
|
---|
8369 |
|
---|
8370 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8371 | }
|
---|
8372 |
|
---|
8373 |
|
---|
8374 | /**
|
---|
8375 | * Implements the 16-bit version of 'BOUND'.
|
---|
8376 | *
|
---|
8377 | * @note We have separate 16-bit and 32-bit variants of this function due to
|
---|
8378 | * the decoder using unsigned parameters, whereas we want signed one to
|
---|
8379 | * do the job. This is significant for a recompiler.
|
---|
8380 | */
|
---|
8381 | IEM_CIMPL_DEF_3(iemCImpl_bound_16, int16_t, idxArray, int16_t, idxLowerBound, int16_t, idxUpperBound)
|
---|
8382 | {
|
---|
8383 | /*
|
---|
8384 | * Check if the index is inside the bounds, otherwise raise #BR.
|
---|
8385 | */
|
---|
8386 | if ( idxArray >= idxLowerBound
|
---|
8387 | && idxArray <= idxUpperBound)
|
---|
8388 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8389 | return iemRaiseBoundRangeExceeded(pVCpu);
|
---|
8390 | }
|
---|
8391 |
|
---|
8392 |
|
---|
8393 | /**
|
---|
8394 | * Implements the 32-bit version of 'BOUND'.
|
---|
8395 | */
|
---|
8396 | IEM_CIMPL_DEF_3(iemCImpl_bound_32, int32_t, idxArray, int32_t, idxLowerBound, int32_t, idxUpperBound)
|
---|
8397 | {
|
---|
8398 | /*
|
---|
8399 | * Check if the index is inside the bounds, otherwise raise #BR.
|
---|
8400 | */
|
---|
8401 | if ( idxArray >= idxLowerBound
|
---|
8402 | && idxArray <= idxUpperBound)
|
---|
8403 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8404 | return iemRaiseBoundRangeExceeded(pVCpu);
|
---|
8405 | }
|
---|
8406 |
|
---|
8407 |
|
---|
8408 |
|
---|
8409 | /*
|
---|
8410 | * Instantiate the various string operation combinations.
|
---|
8411 | */
|
---|
8412 | #define OP_SIZE 8
|
---|
8413 | #define ADDR_SIZE 16
|
---|
8414 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
8415 | #define OP_SIZE 8
|
---|
8416 | #define ADDR_SIZE 32
|
---|
8417 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
8418 | #define OP_SIZE 8
|
---|
8419 | #define ADDR_SIZE 64
|
---|
8420 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
8421 |
|
---|
8422 | #define OP_SIZE 16
|
---|
8423 | #define ADDR_SIZE 16
|
---|
8424 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
8425 | #define OP_SIZE 16
|
---|
8426 | #define ADDR_SIZE 32
|
---|
8427 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
8428 | #define OP_SIZE 16
|
---|
8429 | #define ADDR_SIZE 64
|
---|
8430 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
8431 |
|
---|
8432 | #define OP_SIZE 32
|
---|
8433 | #define ADDR_SIZE 16
|
---|
8434 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
8435 | #define OP_SIZE 32
|
---|
8436 | #define ADDR_SIZE 32
|
---|
8437 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
8438 | #define OP_SIZE 32
|
---|
8439 | #define ADDR_SIZE 64
|
---|
8440 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
8441 |
|
---|
8442 | #define OP_SIZE 64
|
---|
8443 | #define ADDR_SIZE 32
|
---|
8444 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
8445 | #define OP_SIZE 64
|
---|
8446 | #define ADDR_SIZE 64
|
---|
8447 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
8448 |
|
---|
8449 |
|
---|
8450 | /**
|
---|
8451 | * Implements 'XGETBV'.
|
---|
8452 | */
|
---|
8453 | IEM_CIMPL_DEF_0(iemCImpl_xgetbv)
|
---|
8454 | {
|
---|
8455 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR4);
|
---|
8456 | if (pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSXSAVE)
|
---|
8457 | {
|
---|
8458 | uint32_t uEcx = pVCpu->cpum.GstCtx.ecx;
|
---|
8459 | switch (uEcx)
|
---|
8460 | {
|
---|
8461 | case 0:
|
---|
8462 | break;
|
---|
8463 |
|
---|
8464 | case 1: /** @todo Implement XCR1 support. */
|
---|
8465 | default:
|
---|
8466 | Log(("xgetbv ecx=%RX32 -> #GP(0)\n", uEcx));
|
---|
8467 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
8468 |
|
---|
8469 | }
|
---|
8470 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_XCRx);
|
---|
8471 | pVCpu->cpum.GstCtx.rax = RT_LO_U32(pVCpu->cpum.GstCtx.aXcr[uEcx]);
|
---|
8472 | pVCpu->cpum.GstCtx.rdx = RT_HI_U32(pVCpu->cpum.GstCtx.aXcr[uEcx]);
|
---|
8473 |
|
---|
8474 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8475 | }
|
---|
8476 | Log(("xgetbv CR4.OSXSAVE=0 -> UD\n"));
|
---|
8477 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
8478 | }
|
---|
8479 |
|
---|
8480 |
|
---|
8481 | /**
|
---|
8482 | * Implements 'XSETBV'.
|
---|
8483 | */
|
---|
8484 | IEM_CIMPL_DEF_0(iemCImpl_xsetbv)
|
---|
8485 | {
|
---|
8486 | if (pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSXSAVE)
|
---|
8487 | {
|
---|
8488 | /** @todo explain why this happens before the CPL check. */
|
---|
8489 | if (!IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_XSETBV))
|
---|
8490 | { /* probable */ }
|
---|
8491 | else
|
---|
8492 | {
|
---|
8493 | Log2(("xsetbv: Guest intercept -> #VMEXIT\n"));
|
---|
8494 | IEM_SVM_UPDATE_NRIP(pVCpu, cbInstr);
|
---|
8495 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_XSETBV, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
8496 | }
|
---|
8497 |
|
---|
8498 | if (IEM_GET_CPL(pVCpu) == 0)
|
---|
8499 | {
|
---|
8500 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_XCRx);
|
---|
8501 |
|
---|
8502 | if (!IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
8503 | { /* probable */ }
|
---|
8504 | else
|
---|
8505 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_XSETBV, cbInstr);
|
---|
8506 |
|
---|
8507 | uint32_t uEcx = pVCpu->cpum.GstCtx.ecx;
|
---|
8508 | uint64_t uNewValue = RT_MAKE_U64(pVCpu->cpum.GstCtx.eax, pVCpu->cpum.GstCtx.edx);
|
---|
8509 | switch (uEcx)
|
---|
8510 | {
|
---|
8511 | case 0:
|
---|
8512 | {
|
---|
8513 | int rc = CPUMSetGuestXcr0(pVCpu, uNewValue);
|
---|
8514 | if (rc == VINF_SUCCESS)
|
---|
8515 | break;
|
---|
8516 | Assert(rc == VERR_CPUM_RAISE_GP_0);
|
---|
8517 | Log(("xsetbv ecx=%RX32 (newvalue=%RX64) -> #GP(0)\n", uEcx, uNewValue));
|
---|
8518 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
8519 | }
|
---|
8520 |
|
---|
8521 | case 1: /** @todo Implement XCR1 support. */
|
---|
8522 | default:
|
---|
8523 | Log(("xsetbv ecx=%RX32 (newvalue=%RX64) -> #GP(0)\n", uEcx, uNewValue));
|
---|
8524 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
8525 |
|
---|
8526 | }
|
---|
8527 |
|
---|
8528 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8529 | }
|
---|
8530 |
|
---|
8531 | Log(("xsetbv cpl=%u -> GP(0)\n", IEM_GET_CPL(pVCpu)));
|
---|
8532 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
8533 | }
|
---|
8534 | Log(("xsetbv CR4.OSXSAVE=0 -> UD\n"));
|
---|
8535 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
8536 | }
|
---|
8537 |
|
---|
8538 | #ifndef RT_ARCH_ARM64
|
---|
8539 | # ifdef IN_RING3
|
---|
8540 |
|
---|
8541 | /** Argument package for iemCImpl_cmpxchg16b_fallback_rendezvous_callback. */
|
---|
8542 | struct IEMCIMPLCX16ARGS
|
---|
8543 | {
|
---|
8544 | PRTUINT128U pu128Dst;
|
---|
8545 | PRTUINT128U pu128RaxRdx;
|
---|
8546 | PRTUINT128U pu128RbxRcx;
|
---|
8547 | uint32_t *pEFlags;
|
---|
8548 | # ifdef VBOX_STRICT
|
---|
8549 | uint32_t cCalls;
|
---|
8550 | # endif
|
---|
8551 | };
|
---|
8552 |
|
---|
8553 | /**
|
---|
8554 | * @callback_method_impl{FNVMMEMTRENDEZVOUS,
|
---|
8555 | * Worker for iemCImpl_cmpxchg16b_fallback_rendezvous}
|
---|
8556 | */
|
---|
8557 | static DECLCALLBACK(VBOXSTRICTRC) iemCImpl_cmpxchg16b_fallback_rendezvous_callback(PVM pVM, PVMCPUCC pVCpu, void *pvUser)
|
---|
8558 | {
|
---|
8559 | RT_NOREF(pVM, pVCpu);
|
---|
8560 | struct IEMCIMPLCX16ARGS *pArgs = (struct IEMCIMPLCX16ARGS *)pvUser;
|
---|
8561 | # ifdef VBOX_STRICT
|
---|
8562 | Assert(pArgs->cCalls == 0);
|
---|
8563 | pArgs->cCalls++;
|
---|
8564 | # endif
|
---|
8565 |
|
---|
8566 | iemAImpl_cmpxchg16b_fallback(pArgs->pu128Dst, pArgs->pu128RaxRdx, pArgs->pu128RbxRcx, pArgs->pEFlags);
|
---|
8567 | return VINF_SUCCESS;
|
---|
8568 | }
|
---|
8569 |
|
---|
8570 | # endif /* IN_RING3 */
|
---|
8571 |
|
---|
8572 | /**
|
---|
8573 | * Implements 'CMPXCHG16B' fallback using rendezvous.
|
---|
8574 | */
|
---|
8575 | IEM_CIMPL_DEF_4(iemCImpl_cmpxchg16b_fallback_rendezvous, PRTUINT128U, pu128Dst, PRTUINT128U, pu128RaxRdx,
|
---|
8576 | PRTUINT128U, pu128RbxRcx, uint32_t *, pEFlags)
|
---|
8577 | {
|
---|
8578 | # ifdef IN_RING3
|
---|
8579 | struct IEMCIMPLCX16ARGS Args;
|
---|
8580 | Args.pu128Dst = pu128Dst;
|
---|
8581 | Args.pu128RaxRdx = pu128RaxRdx;
|
---|
8582 | Args.pu128RbxRcx = pu128RbxRcx;
|
---|
8583 | Args.pEFlags = pEFlags;
|
---|
8584 | # ifdef VBOX_STRICT
|
---|
8585 | Args.cCalls = 0;
|
---|
8586 | # endif
|
---|
8587 | VBOXSTRICTRC rcStrict = VMMR3EmtRendezvous(pVCpu->CTX_SUFF(pVM), VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE,
|
---|
8588 | iemCImpl_cmpxchg16b_fallback_rendezvous_callback, &Args);
|
---|
8589 | Assert(Args.cCalls == 1);
|
---|
8590 | if (rcStrict == VINF_SUCCESS)
|
---|
8591 | {
|
---|
8592 | /* Duplicated tail code. */
|
---|
8593 | rcStrict = iemMemCommitAndUnmap(pVCpu, pu128Dst, IEM_ACCESS_DATA_RW);
|
---|
8594 | if (rcStrict == VINF_SUCCESS)
|
---|
8595 | {
|
---|
8596 | pVCpu->cpum.GstCtx.eflags.u = *pEFlags; /* IEM_MC_COMMIT_EFLAGS */
|
---|
8597 | if (!(*pEFlags & X86_EFL_ZF))
|
---|
8598 | {
|
---|
8599 | pVCpu->cpum.GstCtx.rax = pu128RaxRdx->s.Lo;
|
---|
8600 | pVCpu->cpum.GstCtx.rdx = pu128RaxRdx->s.Hi;
|
---|
8601 | }
|
---|
8602 | rcStrict = iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8603 | }
|
---|
8604 | }
|
---|
8605 | return rcStrict;
|
---|
8606 | # else
|
---|
8607 | RT_NOREF(pVCpu, cbInstr, pu128Dst, pu128RaxRdx, pu128RbxRcx, pEFlags);
|
---|
8608 | return VERR_IEM_ASPECT_NOT_IMPLEMENTED; /* This should get us to ring-3 for now. Should perhaps be replaced later. */
|
---|
8609 | # endif
|
---|
8610 | }
|
---|
8611 |
|
---|
8612 | #endif /* RT_ARCH_ARM64 */
|
---|
8613 |
|
---|
8614 | /**
|
---|
8615 | * Implements 'CLFLUSH' and 'CLFLUSHOPT'.
|
---|
8616 | *
|
---|
8617 | * This is implemented in C because it triggers a load like behaviour without
|
---|
8618 | * actually reading anything. Since that's not so common, it's implemented
|
---|
8619 | * here.
|
---|
8620 | *
|
---|
8621 | * @param iEffSeg The effective segment.
|
---|
8622 | * @param GCPtrEff The address of the image.
|
---|
8623 | */
|
---|
8624 | IEM_CIMPL_DEF_2(iemCImpl_clflush_clflushopt, uint8_t, iEffSeg, RTGCPTR, GCPtrEff)
|
---|
8625 | {
|
---|
8626 | /*
|
---|
8627 | * Pretend to do a load w/o reading (see also iemCImpl_monitor and iemMemMap).
|
---|
8628 | */
|
---|
8629 | VBOXSTRICTRC rcStrict = iemMemApplySegment(pVCpu, IEM_ACCESS_TYPE_READ | IEM_ACCESS_WHAT_DATA, iEffSeg, 1, &GCPtrEff);
|
---|
8630 | if (rcStrict == VINF_SUCCESS)
|
---|
8631 | {
|
---|
8632 | RTGCPHYS GCPhysMem;
|
---|
8633 | /** @todo access size. */
|
---|
8634 | rcStrict = iemMemPageTranslateAndCheckAccess(pVCpu, GCPtrEff, 1, IEM_ACCESS_TYPE_READ | IEM_ACCESS_WHAT_DATA, &GCPhysMem);
|
---|
8635 | if (rcStrict == VINF_SUCCESS)
|
---|
8636 | {
|
---|
8637 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
8638 | if ( !IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
8639 | || !IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_VIRT_APIC_ACCESS))
|
---|
8640 | { /* probable */ }
|
---|
8641 | else
|
---|
8642 | {
|
---|
8643 | /*
|
---|
8644 | * CLFLUSH/CLFLUSHOPT does not access the memory, but flushes the cache-line
|
---|
8645 | * that contains the address. However, if the address falls in the APIC-access
|
---|
8646 | * page, the address flushed must instead be the corresponding address in the
|
---|
8647 | * virtual-APIC page.
|
---|
8648 | *
|
---|
8649 | * See Intel spec. 29.4.4 "Instruction-Specific Considerations".
|
---|
8650 | */
|
---|
8651 | rcStrict = iemVmxVirtApicAccessUnused(pVCpu, &GCPhysMem, 1, IEM_ACCESS_TYPE_READ | IEM_ACCESS_WHAT_DATA);
|
---|
8652 | if ( rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE
|
---|
8653 | && rcStrict != VINF_VMX_MODIFIES_BEHAVIOR)
|
---|
8654 | return rcStrict;
|
---|
8655 | }
|
---|
8656 | #endif
|
---|
8657 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8658 | }
|
---|
8659 | }
|
---|
8660 |
|
---|
8661 | return rcStrict;
|
---|
8662 | }
|
---|
8663 |
|
---|
8664 |
|
---|
8665 | /**
|
---|
8666 | * Implements 'FINIT' and 'FNINIT'.
|
---|
8667 | *
|
---|
8668 | * @param fCheckXcpts Whether to check for umasked pending exceptions or
|
---|
8669 | * not.
|
---|
8670 | */
|
---|
8671 | IEM_CIMPL_DEF_1(iemCImpl_finit, bool, fCheckXcpts)
|
---|
8672 | {
|
---|
8673 | /*
|
---|
8674 | * Exceptions.
|
---|
8675 | */
|
---|
8676 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0);
|
---|
8677 | if (pVCpu->cpum.GstCtx.cr0 & (X86_CR0_EM | X86_CR0_TS))
|
---|
8678 | return iemRaiseDeviceNotAvailable(pVCpu);
|
---|
8679 |
|
---|
8680 | iemFpuActualizeStateForChange(pVCpu);
|
---|
8681 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_X87);
|
---|
8682 |
|
---|
8683 | /* FINIT: Raise #MF on pending exception(s): */
|
---|
8684 | if (fCheckXcpts && (pVCpu->cpum.GstCtx.XState.x87.FSW & X86_FSW_ES))
|
---|
8685 | return iemRaiseMathFault(pVCpu);
|
---|
8686 |
|
---|
8687 | /*
|
---|
8688 | * Reset the state.
|
---|
8689 | */
|
---|
8690 | PX86XSAVEAREA pXState = &pVCpu->cpum.GstCtx.XState;
|
---|
8691 |
|
---|
8692 | /* Rotate the stack to account for changed TOS. */
|
---|
8693 | iemFpuRotateStackSetTop(&pXState->x87, 0);
|
---|
8694 |
|
---|
8695 | pXState->x87.FCW = 0x37f;
|
---|
8696 | pXState->x87.FSW = 0;
|
---|
8697 | pXState->x87.FTW = 0x00; /* 0 - empty. */
|
---|
8698 | /** @todo Intel says the instruction and data pointers are not cleared on
|
---|
8699 | * 387, presume that 8087 and 287 doesn't do so either. */
|
---|
8700 | /** @todo test this stuff. */
|
---|
8701 | if (IEM_GET_TARGET_CPU(pVCpu) > IEMTARGETCPU_386)
|
---|
8702 | {
|
---|
8703 | pXState->x87.FPUDP = 0;
|
---|
8704 | pXState->x87.DS = 0; //??
|
---|
8705 | pXState->x87.Rsrvd2 = 0;
|
---|
8706 | pXState->x87.FPUIP = 0;
|
---|
8707 | pXState->x87.CS = 0; //??
|
---|
8708 | pXState->x87.Rsrvd1 = 0;
|
---|
8709 | }
|
---|
8710 | pXState->x87.FOP = 0;
|
---|
8711 |
|
---|
8712 | iemHlpUsedFpu(pVCpu);
|
---|
8713 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8714 | }
|
---|
8715 |
|
---|
8716 |
|
---|
8717 | /**
|
---|
8718 | * Implements 'FXSAVE'.
|
---|
8719 | *
|
---|
8720 | * @param iEffSeg The effective segment.
|
---|
8721 | * @param GCPtrEff The address of the image.
|
---|
8722 | * @param enmEffOpSize The operand size (only REX.W really matters).
|
---|
8723 | */
|
---|
8724 | IEM_CIMPL_DEF_3(iemCImpl_fxsave, uint8_t, iEffSeg, RTGCPTR, GCPtrEff, IEMMODE, enmEffOpSize)
|
---|
8725 | {
|
---|
8726 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX);
|
---|
8727 |
|
---|
8728 | /** @todo check out bugref{1529} and AMD behaviour */
|
---|
8729 |
|
---|
8730 | /*
|
---|
8731 | * Raise exceptions.
|
---|
8732 | */
|
---|
8733 | if (pVCpu->cpum.GstCtx.cr0 & (X86_CR0_TS | X86_CR0_EM))
|
---|
8734 | return iemRaiseDeviceNotAvailable(pVCpu);
|
---|
8735 |
|
---|
8736 | /*
|
---|
8737 | * Access the memory.
|
---|
8738 | */
|
---|
8739 | void *pvMem512;
|
---|
8740 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, &pvMem512, 512, iEffSeg, GCPtrEff, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE,
|
---|
8741 | 15 | IEM_MEMMAP_F_ALIGN_GP | IEM_MEMMAP_F_ALIGN_GP_OR_AC);
|
---|
8742 | if (rcStrict != VINF_SUCCESS)
|
---|
8743 | return rcStrict;
|
---|
8744 | PX86FXSTATE pDst = (PX86FXSTATE)pvMem512;
|
---|
8745 | PCX86FXSTATE pSrc = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
8746 |
|
---|
8747 | /*
|
---|
8748 | * Store the registers.
|
---|
8749 | */
|
---|
8750 | /** @todo CPU/VM detection possible! If CR4.OSFXSR=0 MXCSR it's
|
---|
8751 | * implementation specific whether MXCSR and XMM0-XMM7 are saved. */
|
---|
8752 |
|
---|
8753 | /* common for all formats */
|
---|
8754 | pDst->FCW = pSrc->FCW;
|
---|
8755 | pDst->FSW = pSrc->FSW;
|
---|
8756 | pDst->FTW = pSrc->FTW & UINT16_C(0xff);
|
---|
8757 | pDst->FOP = pSrc->FOP;
|
---|
8758 | pDst->MXCSR = pSrc->MXCSR;
|
---|
8759 | pDst->MXCSR_MASK = CPUMGetGuestMxCsrMask(pVCpu->CTX_SUFF(pVM));
|
---|
8760 | for (uint32_t i = 0; i < RT_ELEMENTS(pDst->aRegs); i++)
|
---|
8761 | {
|
---|
8762 | /** @todo Testcase: What actually happens to the 6 reserved bytes? I'm clearing
|
---|
8763 | * them for now... */
|
---|
8764 | pDst->aRegs[i].au32[0] = pSrc->aRegs[i].au32[0];
|
---|
8765 | pDst->aRegs[i].au32[1] = pSrc->aRegs[i].au32[1];
|
---|
8766 | pDst->aRegs[i].au32[2] = pSrc->aRegs[i].au32[2] & UINT32_C(0xffff);
|
---|
8767 | pDst->aRegs[i].au32[3] = 0;
|
---|
8768 | }
|
---|
8769 |
|
---|
8770 | /* FPU IP, CS, DP and DS. */
|
---|
8771 | pDst->FPUIP = pSrc->FPUIP;
|
---|
8772 | pDst->CS = pSrc->CS;
|
---|
8773 | pDst->FPUDP = pSrc->FPUDP;
|
---|
8774 | pDst->DS = pSrc->DS;
|
---|
8775 | if (enmEffOpSize == IEMMODE_64BIT)
|
---|
8776 | {
|
---|
8777 | /* Save upper 16-bits of FPUIP (IP:CS:Rsvd1) and FPUDP (DP:DS:Rsvd2). */
|
---|
8778 | pDst->Rsrvd1 = pSrc->Rsrvd1;
|
---|
8779 | pDst->Rsrvd2 = pSrc->Rsrvd2;
|
---|
8780 | }
|
---|
8781 | else
|
---|
8782 | {
|
---|
8783 | pDst->Rsrvd1 = 0;
|
---|
8784 | pDst->Rsrvd2 = 0;
|
---|
8785 | }
|
---|
8786 |
|
---|
8787 | /* XMM registers. Skipped in 64-bit CPL0 if EFER.FFXSR (AMD only) is set. */
|
---|
8788 | if ( !(pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_FFXSR)
|
---|
8789 | || !IEM_IS_64BIT_CODE(pVCpu)
|
---|
8790 | || IEM_GET_CPL(pVCpu) != 0)
|
---|
8791 | {
|
---|
8792 | uint32_t cXmmRegs = IEM_IS_64BIT_CODE(pVCpu) ? 16 : 8;
|
---|
8793 | for (uint32_t i = 0; i < cXmmRegs; i++)
|
---|
8794 | pDst->aXMM[i] = pSrc->aXMM[i];
|
---|
8795 | /** @todo Testcase: What happens to the reserved XMM registers? Untouched,
|
---|
8796 | * right? */
|
---|
8797 | }
|
---|
8798 |
|
---|
8799 | /*
|
---|
8800 | * Commit the memory.
|
---|
8801 | */
|
---|
8802 | rcStrict = iemMemCommitAndUnmap(pVCpu, pvMem512, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
|
---|
8803 | if (rcStrict != VINF_SUCCESS)
|
---|
8804 | return rcStrict;
|
---|
8805 |
|
---|
8806 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8807 | }
|
---|
8808 |
|
---|
8809 |
|
---|
8810 | /**
|
---|
8811 | * Implements 'FXRSTOR'.
|
---|
8812 | *
|
---|
8813 | * @param iEffSeg The effective segment register for @a GCPtrEff.
|
---|
8814 | * @param GCPtrEff The address of the image.
|
---|
8815 | * @param enmEffOpSize The operand size (only REX.W really matters).
|
---|
8816 | */
|
---|
8817 | IEM_CIMPL_DEF_3(iemCImpl_fxrstor, uint8_t, iEffSeg, RTGCPTR, GCPtrEff, IEMMODE, enmEffOpSize)
|
---|
8818 | {
|
---|
8819 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX);
|
---|
8820 |
|
---|
8821 | /** @todo check out bugref{1529} and AMD behaviour */
|
---|
8822 |
|
---|
8823 | /*
|
---|
8824 | * Raise exceptions.
|
---|
8825 | */
|
---|
8826 | if (pVCpu->cpum.GstCtx.cr0 & (X86_CR0_TS | X86_CR0_EM))
|
---|
8827 | return iemRaiseDeviceNotAvailable(pVCpu);
|
---|
8828 |
|
---|
8829 | /*
|
---|
8830 | * Access the memory.
|
---|
8831 | */
|
---|
8832 | void *pvMem512;
|
---|
8833 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, &pvMem512, 512, iEffSeg, GCPtrEff, IEM_ACCESS_DATA_R,
|
---|
8834 | 15 | IEM_MEMMAP_F_ALIGN_GP | IEM_MEMMAP_F_ALIGN_GP_OR_AC);
|
---|
8835 | if (rcStrict != VINF_SUCCESS)
|
---|
8836 | return rcStrict;
|
---|
8837 | PCX86FXSTATE pSrc = (PCX86FXSTATE)pvMem512;
|
---|
8838 | PX86FXSTATE pDst = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
8839 |
|
---|
8840 | /*
|
---|
8841 | * Check the state for stuff which will #GP(0).
|
---|
8842 | */
|
---|
8843 | uint32_t const fMXCSR = pSrc->MXCSR;
|
---|
8844 | uint32_t const fMXCSR_MASK = CPUMGetGuestMxCsrMask(pVCpu->CTX_SUFF(pVM));
|
---|
8845 | if (fMXCSR & ~fMXCSR_MASK)
|
---|
8846 | {
|
---|
8847 | Log(("fxrstor: MXCSR=%#x (MXCSR_MASK=%#x) -> #GP(0)\n", fMXCSR, fMXCSR_MASK));
|
---|
8848 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
8849 | }
|
---|
8850 |
|
---|
8851 | /*
|
---|
8852 | * Load the registers.
|
---|
8853 | */
|
---|
8854 | /** @todo CPU/VM detection possible! If CR4.OSFXSR=0 MXCSR it's
|
---|
8855 | * implementation specific whether MXCSR and XMM0-XMM7 are
|
---|
8856 | * restored according to Intel.
|
---|
8857 | * AMD says MXCSR and XMM registers are never loaded if
|
---|
8858 | * CR4.OSFXSR=0.
|
---|
8859 | */
|
---|
8860 |
|
---|
8861 | /* common for all formats */
|
---|
8862 | pDst->FCW = pSrc->FCW;
|
---|
8863 | pDst->FSW = pSrc->FSW;
|
---|
8864 | pDst->FTW = pSrc->FTW & UINT16_C(0xff);
|
---|
8865 | pDst->FOP = pSrc->FOP;
|
---|
8866 | pDst->MXCSR = fMXCSR;
|
---|
8867 | /* (MXCSR_MASK is read-only) */
|
---|
8868 | for (uint32_t i = 0; i < RT_ELEMENTS(pSrc->aRegs); i++)
|
---|
8869 | {
|
---|
8870 | pDst->aRegs[i].au32[0] = pSrc->aRegs[i].au32[0];
|
---|
8871 | pDst->aRegs[i].au32[1] = pSrc->aRegs[i].au32[1];
|
---|
8872 | pDst->aRegs[i].au32[2] = pSrc->aRegs[i].au32[2] & UINT32_C(0xffff);
|
---|
8873 | pDst->aRegs[i].au32[3] = 0;
|
---|
8874 | }
|
---|
8875 |
|
---|
8876 | /* FPU IP, CS, DP and DS. */
|
---|
8877 | /** @todo AMD says this is only done if FSW.ES is set after loading. */
|
---|
8878 | if (enmEffOpSize == IEMMODE_64BIT)
|
---|
8879 | {
|
---|
8880 | pDst->FPUIP = pSrc->FPUIP;
|
---|
8881 | pDst->CS = pSrc->CS;
|
---|
8882 | pDst->Rsrvd1 = pSrc->Rsrvd1;
|
---|
8883 | pDst->FPUDP = pSrc->FPUDP;
|
---|
8884 | pDst->DS = pSrc->DS;
|
---|
8885 | pDst->Rsrvd2 = pSrc->Rsrvd2;
|
---|
8886 | }
|
---|
8887 | else
|
---|
8888 | {
|
---|
8889 | pDst->FPUIP = pSrc->FPUIP;
|
---|
8890 | pDst->CS = pSrc->CS;
|
---|
8891 | pDst->Rsrvd1 = 0;
|
---|
8892 | pDst->FPUDP = pSrc->FPUDP;
|
---|
8893 | pDst->DS = pSrc->DS;
|
---|
8894 | pDst->Rsrvd2 = 0;
|
---|
8895 | }
|
---|
8896 |
|
---|
8897 | /* XMM registers. Skipped in 64-bit CPL0 if EFER.FFXSR (AMD only) is set.
|
---|
8898 | * Does not affect MXCSR, only registers.
|
---|
8899 | */
|
---|
8900 | if ( !(pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_FFXSR)
|
---|
8901 | || !IEM_IS_64BIT_CODE(pVCpu)
|
---|
8902 | || IEM_GET_CPL(pVCpu) != 0)
|
---|
8903 | {
|
---|
8904 | uint32_t cXmmRegs = IEM_IS_64BIT_CODE(pVCpu) ? 16 : 8;
|
---|
8905 | for (uint32_t i = 0; i < cXmmRegs; i++)
|
---|
8906 | pDst->aXMM[i] = pSrc->aXMM[i];
|
---|
8907 | }
|
---|
8908 |
|
---|
8909 | pDst->FCW &= ~X86_FCW_ZERO_MASK | X86_FCW_IC_MASK; /* Intel 10980xe allows setting the IC bit. Win 3.11 CALC.EXE sets it. */
|
---|
8910 | iemFpuRecalcExceptionStatus(pDst);
|
---|
8911 |
|
---|
8912 | if (pDst->FSW & X86_FSW_ES)
|
---|
8913 | Log11(("fxrstor: %04x:%08RX64: loading state with pending FPU exception (FSW=%#x)\n",
|
---|
8914 | pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip, pSrc->FSW));
|
---|
8915 |
|
---|
8916 | /*
|
---|
8917 | * Unmap the memory.
|
---|
8918 | */
|
---|
8919 | rcStrict = iemMemCommitAndUnmap(pVCpu, pvMem512, IEM_ACCESS_DATA_R);
|
---|
8920 | if (rcStrict != VINF_SUCCESS)
|
---|
8921 | return rcStrict;
|
---|
8922 |
|
---|
8923 | iemHlpUsedFpu(pVCpu);
|
---|
8924 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
8925 | }
|
---|
8926 |
|
---|
8927 |
|
---|
8928 | /**
|
---|
8929 | * Implements 'XSAVE'.
|
---|
8930 | *
|
---|
8931 | * @param iEffSeg The effective segment.
|
---|
8932 | * @param GCPtrEff The address of the image.
|
---|
8933 | * @param enmEffOpSize The operand size (only REX.W really matters).
|
---|
8934 | */
|
---|
8935 | IEM_CIMPL_DEF_3(iemCImpl_xsave, uint8_t, iEffSeg, RTGCPTR, GCPtrEff, IEMMODE, enmEffOpSize)
|
---|
8936 | {
|
---|
8937 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX | CPUMCTX_EXTRN_OTHER_XSAVE | CPUMCTX_EXTRN_XCRx);
|
---|
8938 |
|
---|
8939 | /*
|
---|
8940 | * Raise exceptions.
|
---|
8941 | */
|
---|
8942 | if (!(pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSXSAVE))
|
---|
8943 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
8944 | /* When in VMX non-root mode and XSAVE/XRSTOR is not enabled, it results in #UD. */
|
---|
8945 | if (RT_LIKELY( !IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
8946 | || IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_XSAVES_XRSTORS)))
|
---|
8947 | { /* likely */ }
|
---|
8948 | else
|
---|
8949 | {
|
---|
8950 | Log(("xrstor: Not enabled for nested-guest execution -> #UD\n"));
|
---|
8951 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
8952 | }
|
---|
8953 | if (pVCpu->cpum.GstCtx.cr0 & X86_CR0_TS)
|
---|
8954 | return iemRaiseDeviceNotAvailable(pVCpu);
|
---|
8955 |
|
---|
8956 | /*
|
---|
8957 | * Calc the requested mask.
|
---|
8958 | */
|
---|
8959 | uint64_t const fReqComponents = RT_MAKE_U64(pVCpu->cpum.GstCtx.eax, pVCpu->cpum.GstCtx.edx) & pVCpu->cpum.GstCtx.aXcr[0];
|
---|
8960 | AssertLogRelReturn(!(fReqComponents & ~(XSAVE_C_X87 | XSAVE_C_SSE | XSAVE_C_YMM)), VERR_IEM_ASPECT_NOT_IMPLEMENTED);
|
---|
8961 | uint64_t const fXInUse = pVCpu->cpum.GstCtx.aXcr[0];
|
---|
8962 |
|
---|
8963 | /** @todo figure out the exact protocol for the memory access. Currently we
|
---|
8964 | * just need this crap to work halfways to make it possible to test
|
---|
8965 | * AVX instructions. */
|
---|
8966 | /** @todo figure out the XINUSE and XMODIFIED */
|
---|
8967 |
|
---|
8968 | /*
|
---|
8969 | * Access the x87 memory state.
|
---|
8970 | */
|
---|
8971 | /* The x87+SSE state. */
|
---|
8972 | void *pvMem512;
|
---|
8973 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, &pvMem512, 512, iEffSeg, GCPtrEff, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE,
|
---|
8974 | 63 | IEM_MEMMAP_F_ALIGN_GP | IEM_MEMMAP_F_ALIGN_GP_OR_AC);
|
---|
8975 | if (rcStrict != VINF_SUCCESS)
|
---|
8976 | return rcStrict;
|
---|
8977 | PX86FXSTATE pDst = (PX86FXSTATE)pvMem512;
|
---|
8978 | PCX86FXSTATE pSrc = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
8979 |
|
---|
8980 | /* The header. */
|
---|
8981 | PX86XSAVEHDR pHdr;
|
---|
8982 | rcStrict = iemMemMap(pVCpu, (void **)&pHdr, sizeof(&pHdr), iEffSeg, GCPtrEff + 512, IEM_ACCESS_DATA_RW, 0 /* checked above */);
|
---|
8983 | if (rcStrict != VINF_SUCCESS)
|
---|
8984 | return rcStrict;
|
---|
8985 |
|
---|
8986 | /*
|
---|
8987 | * Store the X87 state.
|
---|
8988 | */
|
---|
8989 | if (fReqComponents & XSAVE_C_X87)
|
---|
8990 | {
|
---|
8991 | /* common for all formats */
|
---|
8992 | pDst->FCW = pSrc->FCW;
|
---|
8993 | pDst->FSW = pSrc->FSW;
|
---|
8994 | pDst->FTW = pSrc->FTW & UINT16_C(0xff);
|
---|
8995 | pDst->FOP = pSrc->FOP;
|
---|
8996 | pDst->FPUIP = pSrc->FPUIP;
|
---|
8997 | pDst->CS = pSrc->CS;
|
---|
8998 | pDst->FPUDP = pSrc->FPUDP;
|
---|
8999 | pDst->DS = pSrc->DS;
|
---|
9000 | if (enmEffOpSize == IEMMODE_64BIT)
|
---|
9001 | {
|
---|
9002 | /* Save upper 16-bits of FPUIP (IP:CS:Rsvd1) and FPUDP (DP:DS:Rsvd2). */
|
---|
9003 | pDst->Rsrvd1 = pSrc->Rsrvd1;
|
---|
9004 | pDst->Rsrvd2 = pSrc->Rsrvd2;
|
---|
9005 | }
|
---|
9006 | else
|
---|
9007 | {
|
---|
9008 | pDst->Rsrvd1 = 0;
|
---|
9009 | pDst->Rsrvd2 = 0;
|
---|
9010 | }
|
---|
9011 | for (uint32_t i = 0; i < RT_ELEMENTS(pDst->aRegs); i++)
|
---|
9012 | {
|
---|
9013 | /** @todo Testcase: What actually happens to the 6 reserved bytes? I'm clearing
|
---|
9014 | * them for now... */
|
---|
9015 | pDst->aRegs[i].au32[0] = pSrc->aRegs[i].au32[0];
|
---|
9016 | pDst->aRegs[i].au32[1] = pSrc->aRegs[i].au32[1];
|
---|
9017 | pDst->aRegs[i].au32[2] = pSrc->aRegs[i].au32[2] & UINT32_C(0xffff);
|
---|
9018 | pDst->aRegs[i].au32[3] = 0;
|
---|
9019 | }
|
---|
9020 |
|
---|
9021 | }
|
---|
9022 |
|
---|
9023 | if (fReqComponents & (XSAVE_C_SSE | XSAVE_C_YMM))
|
---|
9024 | {
|
---|
9025 | pDst->MXCSR = pSrc->MXCSR;
|
---|
9026 | pDst->MXCSR_MASK = CPUMGetGuestMxCsrMask(pVCpu->CTX_SUFF(pVM));
|
---|
9027 | }
|
---|
9028 |
|
---|
9029 | if (fReqComponents & XSAVE_C_SSE)
|
---|
9030 | {
|
---|
9031 | /* XMM registers. */
|
---|
9032 | uint32_t cXmmRegs = enmEffOpSize == IEMMODE_64BIT ? 16 : 8;
|
---|
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 |
|
---|
9039 | /* Commit the x87 state bits. (probably wrong) */
|
---|
9040 | rcStrict = iemMemCommitAndUnmap(pVCpu, pvMem512, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
|
---|
9041 | if (rcStrict != VINF_SUCCESS)
|
---|
9042 | return rcStrict;
|
---|
9043 |
|
---|
9044 | /*
|
---|
9045 | * Store AVX state.
|
---|
9046 | */
|
---|
9047 | if (fReqComponents & XSAVE_C_YMM)
|
---|
9048 | {
|
---|
9049 | /** @todo testcase: xsave64 vs xsave32 wrt XSAVE_C_YMM. */
|
---|
9050 | AssertLogRelReturn(pVCpu->cpum.GstCtx.aoffXState[XSAVE_C_YMM_BIT] != UINT16_MAX, VERR_IEM_IPE_9);
|
---|
9051 | PCX86XSAVEYMMHI pCompSrc = CPUMCTX_XSAVE_C_PTR(IEM_GET_CTX(pVCpu), XSAVE_C_YMM_BIT, PCX86XSAVEYMMHI);
|
---|
9052 | PX86XSAVEYMMHI pCompDst;
|
---|
9053 | rcStrict = iemMemMap(pVCpu, (void **)&pCompDst, sizeof(*pCompDst), iEffSeg, GCPtrEff + pVCpu->cpum.GstCtx.aoffXState[XSAVE_C_YMM_BIT],
|
---|
9054 | IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE, 0 /* checked above */);
|
---|
9055 | if (rcStrict != VINF_SUCCESS)
|
---|
9056 | return rcStrict;
|
---|
9057 |
|
---|
9058 | uint32_t cXmmRegs = enmEffOpSize == IEMMODE_64BIT ? 16 : 8;
|
---|
9059 | for (uint32_t i = 0; i < cXmmRegs; i++)
|
---|
9060 | pCompDst->aYmmHi[i] = pCompSrc->aYmmHi[i];
|
---|
9061 |
|
---|
9062 | rcStrict = iemMemCommitAndUnmap(pVCpu, pCompDst, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
|
---|
9063 | if (rcStrict != VINF_SUCCESS)
|
---|
9064 | return rcStrict;
|
---|
9065 | }
|
---|
9066 |
|
---|
9067 | /*
|
---|
9068 | * Update the header.
|
---|
9069 | */
|
---|
9070 | pHdr->bmXState = (pHdr->bmXState & ~fReqComponents)
|
---|
9071 | | (fReqComponents & fXInUse);
|
---|
9072 |
|
---|
9073 | rcStrict = iemMemCommitAndUnmap(pVCpu, pHdr, IEM_ACCESS_DATA_RW);
|
---|
9074 | if (rcStrict != VINF_SUCCESS)
|
---|
9075 | return rcStrict;
|
---|
9076 |
|
---|
9077 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
9078 | }
|
---|
9079 |
|
---|
9080 |
|
---|
9081 | /**
|
---|
9082 | * Implements 'XRSTOR'.
|
---|
9083 | *
|
---|
9084 | * @param iEffSeg The effective segment.
|
---|
9085 | * @param GCPtrEff The address of the image.
|
---|
9086 | * @param enmEffOpSize The operand size (only REX.W really matters).
|
---|
9087 | */
|
---|
9088 | IEM_CIMPL_DEF_3(iemCImpl_xrstor, uint8_t, iEffSeg, RTGCPTR, GCPtrEff, IEMMODE, enmEffOpSize)
|
---|
9089 | {
|
---|
9090 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX | CPUMCTX_EXTRN_OTHER_XSAVE | CPUMCTX_EXTRN_XCRx);
|
---|
9091 |
|
---|
9092 | /*
|
---|
9093 | * Raise exceptions.
|
---|
9094 | */
|
---|
9095 | if (!(pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSXSAVE))
|
---|
9096 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
9097 | /* When in VMX non-root mode and XSAVE/XRSTOR is not enabled, it results in #UD. */
|
---|
9098 | if (RT_LIKELY( !IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
9099 | || IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_XSAVES_XRSTORS)))
|
---|
9100 | { /* likely */ }
|
---|
9101 | else
|
---|
9102 | {
|
---|
9103 | Log(("xrstor: Not enabled for nested-guest execution -> #UD\n"));
|
---|
9104 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
9105 | }
|
---|
9106 | if (pVCpu->cpum.GstCtx.cr0 & X86_CR0_TS)
|
---|
9107 | return iemRaiseDeviceNotAvailable(pVCpu);
|
---|
9108 | if (GCPtrEff & 63)
|
---|
9109 | {
|
---|
9110 | /** @todo CPU/VM detection possible! \#AC might not be signal for
|
---|
9111 | * all/any misalignment sizes, intel says its an implementation detail. */
|
---|
9112 | if ( (pVCpu->cpum.GstCtx.cr0 & X86_CR0_AM)
|
---|
9113 | && pVCpu->cpum.GstCtx.eflags.Bits.u1AC
|
---|
9114 | && IEM_GET_CPL(pVCpu) == 3)
|
---|
9115 | return iemRaiseAlignmentCheckException(pVCpu);
|
---|
9116 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
9117 | }
|
---|
9118 |
|
---|
9119 | /** @todo figure out the exact protocol for the memory access. Currently we
|
---|
9120 | * just need this crap to work halfways to make it possible to test
|
---|
9121 | * AVX instructions. */
|
---|
9122 | /** @todo figure out the XINUSE and XMODIFIED */
|
---|
9123 |
|
---|
9124 | /*
|
---|
9125 | * Access the x87 memory state.
|
---|
9126 | */
|
---|
9127 | /* The x87+SSE state. */
|
---|
9128 | void *pvMem512;
|
---|
9129 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, &pvMem512, 512, iEffSeg, GCPtrEff, IEM_ACCESS_DATA_R,
|
---|
9130 | 63 | IEM_MEMMAP_F_ALIGN_GP | IEM_MEMMAP_F_ALIGN_GP_OR_AC);
|
---|
9131 | if (rcStrict != VINF_SUCCESS)
|
---|
9132 | return rcStrict;
|
---|
9133 | PCX86FXSTATE pSrc = (PCX86FXSTATE)pvMem512;
|
---|
9134 | PX86FXSTATE pDst = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
9135 |
|
---|
9136 | /*
|
---|
9137 | * Calc the requested mask
|
---|
9138 | */
|
---|
9139 | PX86XSAVEHDR pHdrDst = &pVCpu->cpum.GstCtx.XState.Hdr;
|
---|
9140 | PCX86XSAVEHDR pHdrSrc;
|
---|
9141 | rcStrict = iemMemMap(pVCpu, (void **)&pHdrSrc, sizeof(&pHdrSrc), iEffSeg, GCPtrEff + 512,
|
---|
9142 | IEM_ACCESS_DATA_R, 0 /* checked above */);
|
---|
9143 | if (rcStrict != VINF_SUCCESS)
|
---|
9144 | return rcStrict;
|
---|
9145 |
|
---|
9146 | uint64_t const fReqComponents = RT_MAKE_U64(pVCpu->cpum.GstCtx.eax, pVCpu->cpum.GstCtx.edx) & pVCpu->cpum.GstCtx.aXcr[0];
|
---|
9147 | AssertLogRelReturn(!(fReqComponents & ~(XSAVE_C_X87 | XSAVE_C_SSE | XSAVE_C_YMM)), VERR_IEM_ASPECT_NOT_IMPLEMENTED);
|
---|
9148 | //uint64_t const fXInUse = pVCpu->cpum.GstCtx.aXcr[0];
|
---|
9149 | uint64_t const fRstorMask = pHdrSrc->bmXState;
|
---|
9150 | uint64_t const fCompMask = pHdrSrc->bmXComp;
|
---|
9151 |
|
---|
9152 | AssertLogRelReturn(!(fCompMask & XSAVE_C_X), VERR_IEM_ASPECT_NOT_IMPLEMENTED);
|
---|
9153 |
|
---|
9154 | uint32_t const cXmmRegs = enmEffOpSize == IEMMODE_64BIT ? 16 : 8;
|
---|
9155 |
|
---|
9156 | /* We won't need this any longer. */
|
---|
9157 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)pHdrSrc, IEM_ACCESS_DATA_R);
|
---|
9158 | if (rcStrict != VINF_SUCCESS)
|
---|
9159 | return rcStrict;
|
---|
9160 |
|
---|
9161 | /*
|
---|
9162 | * Load the X87 state.
|
---|
9163 | */
|
---|
9164 | if (fReqComponents & XSAVE_C_X87)
|
---|
9165 | {
|
---|
9166 | if (fRstorMask & XSAVE_C_X87)
|
---|
9167 | {
|
---|
9168 | pDst->FCW = pSrc->FCW;
|
---|
9169 | pDst->FSW = pSrc->FSW;
|
---|
9170 | pDst->FTW = pSrc->FTW & UINT16_C(0xff);
|
---|
9171 | pDst->FOP = pSrc->FOP;
|
---|
9172 | pDst->FPUIP = pSrc->FPUIP;
|
---|
9173 | pDst->CS = pSrc->CS;
|
---|
9174 | pDst->FPUDP = pSrc->FPUDP;
|
---|
9175 | pDst->DS = pSrc->DS;
|
---|
9176 | if (enmEffOpSize == IEMMODE_64BIT)
|
---|
9177 | {
|
---|
9178 | /* Load upper 16-bits of FPUIP (IP:CS:Rsvd1) and FPUDP (DP:DS:Rsvd2). */
|
---|
9179 | pDst->Rsrvd1 = pSrc->Rsrvd1;
|
---|
9180 | pDst->Rsrvd2 = pSrc->Rsrvd2;
|
---|
9181 | }
|
---|
9182 | else
|
---|
9183 | {
|
---|
9184 | pDst->Rsrvd1 = 0;
|
---|
9185 | pDst->Rsrvd2 = 0;
|
---|
9186 | }
|
---|
9187 | for (uint32_t i = 0; i < RT_ELEMENTS(pDst->aRegs); i++)
|
---|
9188 | {
|
---|
9189 | pDst->aRegs[i].au32[0] = pSrc->aRegs[i].au32[0];
|
---|
9190 | pDst->aRegs[i].au32[1] = pSrc->aRegs[i].au32[1];
|
---|
9191 | pDst->aRegs[i].au32[2] = pSrc->aRegs[i].au32[2] & UINT32_C(0xffff);
|
---|
9192 | pDst->aRegs[i].au32[3] = 0;
|
---|
9193 | }
|
---|
9194 |
|
---|
9195 | pDst->FCW &= ~X86_FCW_ZERO_MASK | X86_FCW_IC_MASK; /* Intel 10980xe allows setting the IC bit. Win 3.11 CALC.EXE sets it. */
|
---|
9196 | iemFpuRecalcExceptionStatus(pDst);
|
---|
9197 |
|
---|
9198 | if (pDst->FSW & X86_FSW_ES)
|
---|
9199 | Log11(("xrstor: %04x:%08RX64: loading state with pending FPU exception (FSW=%#x)\n",
|
---|
9200 | pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip, pSrc->FSW));
|
---|
9201 | }
|
---|
9202 | else
|
---|
9203 | {
|
---|
9204 | pDst->FCW = 0x37f;
|
---|
9205 | pDst->FSW = 0;
|
---|
9206 | pDst->FTW = 0x00; /* 0 - empty. */
|
---|
9207 | pDst->FPUDP = 0;
|
---|
9208 | pDst->DS = 0; //??
|
---|
9209 | pDst->Rsrvd2= 0;
|
---|
9210 | pDst->FPUIP = 0;
|
---|
9211 | pDst->CS = 0; //??
|
---|
9212 | pDst->Rsrvd1= 0;
|
---|
9213 | pDst->FOP = 0;
|
---|
9214 | for (uint32_t i = 0; i < RT_ELEMENTS(pSrc->aRegs); i++)
|
---|
9215 | {
|
---|
9216 | pDst->aRegs[i].au32[0] = 0;
|
---|
9217 | pDst->aRegs[i].au32[1] = 0;
|
---|
9218 | pDst->aRegs[i].au32[2] = 0;
|
---|
9219 | pDst->aRegs[i].au32[3] = 0;
|
---|
9220 | }
|
---|
9221 | }
|
---|
9222 | pHdrDst->bmXState |= XSAVE_C_X87; /* playing safe for now */
|
---|
9223 | }
|
---|
9224 |
|
---|
9225 | /* MXCSR */
|
---|
9226 | if (fReqComponents & (XSAVE_C_SSE | XSAVE_C_YMM))
|
---|
9227 | {
|
---|
9228 | if (fRstorMask & (XSAVE_C_SSE | XSAVE_C_YMM))
|
---|
9229 | pDst->MXCSR = pSrc->MXCSR;
|
---|
9230 | else
|
---|
9231 | pDst->MXCSR = 0x1f80;
|
---|
9232 | }
|
---|
9233 |
|
---|
9234 | /* XMM registers. */
|
---|
9235 | if (fReqComponents & XSAVE_C_SSE)
|
---|
9236 | {
|
---|
9237 | if (fRstorMask & XSAVE_C_SSE)
|
---|
9238 | {
|
---|
9239 | for (uint32_t i = 0; i < cXmmRegs; i++)
|
---|
9240 | pDst->aXMM[i] = pSrc->aXMM[i];
|
---|
9241 | /** @todo Testcase: What happens to the reserved XMM registers? Untouched,
|
---|
9242 | * right? */
|
---|
9243 | }
|
---|
9244 | else
|
---|
9245 | {
|
---|
9246 | for (uint32_t i = 0; i < cXmmRegs; i++)
|
---|
9247 | {
|
---|
9248 | pDst->aXMM[i].au64[0] = 0;
|
---|
9249 | pDst->aXMM[i].au64[1] = 0;
|
---|
9250 | }
|
---|
9251 | }
|
---|
9252 | pHdrDst->bmXState |= XSAVE_C_SSE; /* playing safe for now */
|
---|
9253 | }
|
---|
9254 |
|
---|
9255 | /* Unmap the x87 state bits (so we've don't run out of mapping). */
|
---|
9256 | rcStrict = iemMemCommitAndUnmap(pVCpu, pvMem512, IEM_ACCESS_DATA_R);
|
---|
9257 | if (rcStrict != VINF_SUCCESS)
|
---|
9258 | return rcStrict;
|
---|
9259 |
|
---|
9260 | /*
|
---|
9261 | * Restore AVX state.
|
---|
9262 | */
|
---|
9263 | if (fReqComponents & XSAVE_C_YMM)
|
---|
9264 | {
|
---|
9265 | AssertLogRelReturn(pVCpu->cpum.GstCtx.aoffXState[XSAVE_C_YMM_BIT] != UINT16_MAX, VERR_IEM_IPE_9);
|
---|
9266 | PX86XSAVEYMMHI pCompDst = CPUMCTX_XSAVE_C_PTR(IEM_GET_CTX(pVCpu), XSAVE_C_YMM_BIT, PX86XSAVEYMMHI);
|
---|
9267 |
|
---|
9268 | if (fRstorMask & XSAVE_C_YMM)
|
---|
9269 | {
|
---|
9270 | /** @todo testcase: xsave64 vs xsave32 wrt XSAVE_C_YMM. */
|
---|
9271 | PCX86XSAVEYMMHI pCompSrc;
|
---|
9272 | rcStrict = iemMemMap(pVCpu, (void **)&pCompSrc, sizeof(*pCompDst),
|
---|
9273 | iEffSeg, GCPtrEff + pVCpu->cpum.GstCtx.aoffXState[XSAVE_C_YMM_BIT],
|
---|
9274 | IEM_ACCESS_DATA_R, 0 /* checked above */);
|
---|
9275 | if (rcStrict != VINF_SUCCESS)
|
---|
9276 | return rcStrict;
|
---|
9277 |
|
---|
9278 | for (uint32_t i = 0; i < cXmmRegs; i++)
|
---|
9279 | {
|
---|
9280 | pCompDst->aYmmHi[i].au64[0] = pCompSrc->aYmmHi[i].au64[0];
|
---|
9281 | pCompDst->aYmmHi[i].au64[1] = pCompSrc->aYmmHi[i].au64[1];
|
---|
9282 | }
|
---|
9283 |
|
---|
9284 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)pCompSrc, IEM_ACCESS_DATA_R);
|
---|
9285 | if (rcStrict != VINF_SUCCESS)
|
---|
9286 | return rcStrict;
|
---|
9287 | }
|
---|
9288 | else
|
---|
9289 | {
|
---|
9290 | for (uint32_t i = 0; i < cXmmRegs; i++)
|
---|
9291 | {
|
---|
9292 | pCompDst->aYmmHi[i].au64[0] = 0;
|
---|
9293 | pCompDst->aYmmHi[i].au64[1] = 0;
|
---|
9294 | }
|
---|
9295 | }
|
---|
9296 | pHdrDst->bmXState |= XSAVE_C_YMM; /* playing safe for now */
|
---|
9297 | }
|
---|
9298 |
|
---|
9299 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
9300 | }
|
---|
9301 |
|
---|
9302 |
|
---|
9303 |
|
---|
9304 |
|
---|
9305 | /**
|
---|
9306 | * Implements 'STMXCSR'.
|
---|
9307 | *
|
---|
9308 | * @param iEffSeg The effective segment register for @a GCPtrEff.
|
---|
9309 | * @param GCPtrEff The address of the image.
|
---|
9310 | */
|
---|
9311 | IEM_CIMPL_DEF_2(iemCImpl_stmxcsr, uint8_t, iEffSeg, RTGCPTR, GCPtrEff)
|
---|
9312 | {
|
---|
9313 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX);
|
---|
9314 |
|
---|
9315 | /*
|
---|
9316 | * Raise exceptions.
|
---|
9317 | */
|
---|
9318 | if ( !(pVCpu->cpum.GstCtx.cr0 & X86_CR0_EM)
|
---|
9319 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSFXSR))
|
---|
9320 | {
|
---|
9321 | if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_TS))
|
---|
9322 | {
|
---|
9323 | /*
|
---|
9324 | * Do the job.
|
---|
9325 | */
|
---|
9326 | VBOXSTRICTRC rcStrict = iemMemStoreDataU32(pVCpu, iEffSeg, GCPtrEff, pVCpu->cpum.GstCtx.XState.x87.MXCSR);
|
---|
9327 | if (rcStrict == VINF_SUCCESS)
|
---|
9328 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
9329 | return rcStrict;
|
---|
9330 | }
|
---|
9331 | return iemRaiseDeviceNotAvailable(pVCpu);
|
---|
9332 | }
|
---|
9333 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
9334 | }
|
---|
9335 |
|
---|
9336 |
|
---|
9337 | /**
|
---|
9338 | * Implements 'VSTMXCSR'.
|
---|
9339 | *
|
---|
9340 | * @param iEffSeg The effective segment register for @a GCPtrEff.
|
---|
9341 | * @param GCPtrEff The address of the image.
|
---|
9342 | */
|
---|
9343 | IEM_CIMPL_DEF_2(iemCImpl_vstmxcsr, uint8_t, iEffSeg, RTGCPTR, GCPtrEff)
|
---|
9344 | {
|
---|
9345 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX | CPUMCTX_EXTRN_XCRx);
|
---|
9346 |
|
---|
9347 | /*
|
---|
9348 | * Raise exceptions.
|
---|
9349 | */
|
---|
9350 | if ( ( !IEM_IS_GUEST_CPU_AMD(pVCpu)
|
---|
9351 | ? (pVCpu->cpum.GstCtx.aXcr[0] & (XSAVE_C_SSE | XSAVE_C_YMM)) == (XSAVE_C_SSE | XSAVE_C_YMM)
|
---|
9352 | : !(pVCpu->cpum.GstCtx.cr0 & X86_CR0_EM)) /* AMD Jaguar CPU (f0x16,m0,s1) behaviour */
|
---|
9353 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSXSAVE))
|
---|
9354 | {
|
---|
9355 | if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_TS))
|
---|
9356 | {
|
---|
9357 | /*
|
---|
9358 | * Do the job.
|
---|
9359 | */
|
---|
9360 | VBOXSTRICTRC rcStrict = iemMemStoreDataU32(pVCpu, iEffSeg, GCPtrEff, pVCpu->cpum.GstCtx.XState.x87.MXCSR);
|
---|
9361 | if (rcStrict == VINF_SUCCESS)
|
---|
9362 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
9363 | return rcStrict;
|
---|
9364 | }
|
---|
9365 | return iemRaiseDeviceNotAvailable(pVCpu);
|
---|
9366 | }
|
---|
9367 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
9368 | }
|
---|
9369 |
|
---|
9370 |
|
---|
9371 | /**
|
---|
9372 | * Implements 'LDMXCSR'.
|
---|
9373 | *
|
---|
9374 | * @param iEffSeg The effective segment register for @a GCPtrEff.
|
---|
9375 | * @param GCPtrEff The address of the image.
|
---|
9376 | */
|
---|
9377 | IEM_CIMPL_DEF_2(iemCImpl_ldmxcsr, uint8_t, iEffSeg, RTGCPTR, GCPtrEff)
|
---|
9378 | {
|
---|
9379 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX);
|
---|
9380 |
|
---|
9381 | /*
|
---|
9382 | * Raise exceptions.
|
---|
9383 | */
|
---|
9384 | /** @todo testcase - order of LDMXCSR faults. Does \#PF, \#GP and \#SS
|
---|
9385 | * happen after or before \#UD and \#EM? */
|
---|
9386 | if ( !(pVCpu->cpum.GstCtx.cr0 & X86_CR0_EM)
|
---|
9387 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSFXSR))
|
---|
9388 | {
|
---|
9389 | if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_TS))
|
---|
9390 | {
|
---|
9391 | /*
|
---|
9392 | * Do the job.
|
---|
9393 | */
|
---|
9394 | uint32_t fNewMxCsr;
|
---|
9395 | VBOXSTRICTRC rcStrict = iemMemFetchDataU32(pVCpu, &fNewMxCsr, iEffSeg, GCPtrEff);
|
---|
9396 | if (rcStrict == VINF_SUCCESS)
|
---|
9397 | {
|
---|
9398 | uint32_t const fMxCsrMask = CPUMGetGuestMxCsrMask(pVCpu->CTX_SUFF(pVM));
|
---|
9399 | if (!(fNewMxCsr & ~fMxCsrMask))
|
---|
9400 | {
|
---|
9401 | pVCpu->cpum.GstCtx.XState.x87.MXCSR = fNewMxCsr;
|
---|
9402 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
9403 | }
|
---|
9404 | Log(("ldmxcsr: New MXCSR=%#RX32 & ~MASK=%#RX32 = %#RX32 -> #GP(0)\n",
|
---|
9405 | fNewMxCsr, fMxCsrMask, fNewMxCsr & ~fMxCsrMask));
|
---|
9406 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
9407 | }
|
---|
9408 | return rcStrict;
|
---|
9409 | }
|
---|
9410 | return iemRaiseDeviceNotAvailable(pVCpu);
|
---|
9411 | }
|
---|
9412 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
9413 | }
|
---|
9414 |
|
---|
9415 |
|
---|
9416 | /**
|
---|
9417 | * Commmon routine for fnstenv and fnsave.
|
---|
9418 | *
|
---|
9419 | * @param pVCpu The cross context virtual CPU structure of the calling thread.
|
---|
9420 | * @param enmEffOpSize The effective operand size.
|
---|
9421 | * @param uPtr Where to store the state.
|
---|
9422 | */
|
---|
9423 | static void iemCImplCommonFpuStoreEnv(PVMCPUCC pVCpu, IEMMODE enmEffOpSize, RTPTRUNION uPtr)
|
---|
9424 | {
|
---|
9425 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87);
|
---|
9426 | PCX86FXSTATE pSrcX87 = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
9427 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
9428 | {
|
---|
9429 | uPtr.pu16[0] = pSrcX87->FCW;
|
---|
9430 | uPtr.pu16[1] = pSrcX87->FSW;
|
---|
9431 | uPtr.pu16[2] = iemFpuCalcFullFtw(pSrcX87);
|
---|
9432 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
9433 | {
|
---|
9434 | /** @todo Testcase: How does this work when the FPUIP/CS was saved in
|
---|
9435 | * protected mode or long mode and we save it in real mode? And vice
|
---|
9436 | * versa? And with 32-bit operand size? I think CPU is storing the
|
---|
9437 | * effective address ((CS << 4) + IP) in the offset register and not
|
---|
9438 | * doing any address calculations here. */
|
---|
9439 | uPtr.pu16[3] = (uint16_t)pSrcX87->FPUIP;
|
---|
9440 | uPtr.pu16[4] = ((pSrcX87->FPUIP >> 4) & UINT16_C(0xf000)) | pSrcX87->FOP;
|
---|
9441 | uPtr.pu16[5] = (uint16_t)pSrcX87->FPUDP;
|
---|
9442 | uPtr.pu16[6] = (pSrcX87->FPUDP >> 4) & UINT16_C(0xf000);
|
---|
9443 | }
|
---|
9444 | else
|
---|
9445 | {
|
---|
9446 | uPtr.pu16[3] = pSrcX87->FPUIP;
|
---|
9447 | uPtr.pu16[4] = pSrcX87->CS;
|
---|
9448 | uPtr.pu16[5] = pSrcX87->FPUDP;
|
---|
9449 | uPtr.pu16[6] = pSrcX87->DS;
|
---|
9450 | }
|
---|
9451 | }
|
---|
9452 | else
|
---|
9453 | {
|
---|
9454 | /** @todo Testcase: what is stored in the "gray" areas? (figure 8-9 and 8-10) */
|
---|
9455 | uPtr.pu16[0*2] = pSrcX87->FCW;
|
---|
9456 | uPtr.pu16[0*2+1] = 0xffff; /* (0xffff observed on intel skylake.) */
|
---|
9457 | uPtr.pu16[1*2] = pSrcX87->FSW;
|
---|
9458 | uPtr.pu16[1*2+1] = 0xffff;
|
---|
9459 | uPtr.pu16[2*2] = iemFpuCalcFullFtw(pSrcX87);
|
---|
9460 | uPtr.pu16[2*2+1] = 0xffff;
|
---|
9461 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
9462 | {
|
---|
9463 | uPtr.pu16[3*2] = (uint16_t)pSrcX87->FPUIP;
|
---|
9464 | uPtr.pu32[4] = ((pSrcX87->FPUIP & UINT32_C(0xffff0000)) >> 4) | pSrcX87->FOP;
|
---|
9465 | uPtr.pu16[5*2] = (uint16_t)pSrcX87->FPUDP;
|
---|
9466 | uPtr.pu32[6] = (pSrcX87->FPUDP & UINT32_C(0xffff0000)) >> 4;
|
---|
9467 | }
|
---|
9468 | else
|
---|
9469 | {
|
---|
9470 | uPtr.pu32[3] = pSrcX87->FPUIP;
|
---|
9471 | uPtr.pu16[4*2] = pSrcX87->CS;
|
---|
9472 | uPtr.pu16[4*2+1] = pSrcX87->FOP;
|
---|
9473 | uPtr.pu32[5] = pSrcX87->FPUDP;
|
---|
9474 | uPtr.pu16[6*2] = pSrcX87->DS;
|
---|
9475 | uPtr.pu16[6*2+1] = 0xffff;
|
---|
9476 | }
|
---|
9477 | }
|
---|
9478 | }
|
---|
9479 |
|
---|
9480 |
|
---|
9481 | /**
|
---|
9482 | * Commmon routine for fldenv and frstor
|
---|
9483 | *
|
---|
9484 | * @param pVCpu The cross context virtual CPU structure of the calling thread.
|
---|
9485 | * @param enmEffOpSize The effective operand size.
|
---|
9486 | * @param uPtr Where to store the state.
|
---|
9487 | */
|
---|
9488 | static void iemCImplCommonFpuRestoreEnv(PVMCPUCC pVCpu, IEMMODE enmEffOpSize, RTCPTRUNION uPtr)
|
---|
9489 | {
|
---|
9490 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87);
|
---|
9491 | PX86FXSTATE pDstX87 = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
9492 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
9493 | {
|
---|
9494 | pDstX87->FCW = uPtr.pu16[0];
|
---|
9495 | pDstX87->FSW = uPtr.pu16[1];
|
---|
9496 | pDstX87->FTW = uPtr.pu16[2];
|
---|
9497 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
9498 | {
|
---|
9499 | pDstX87->FPUIP = uPtr.pu16[3] | ((uint32_t)(uPtr.pu16[4] & UINT16_C(0xf000)) << 4);
|
---|
9500 | pDstX87->FPUDP = uPtr.pu16[5] | ((uint32_t)(uPtr.pu16[6] & UINT16_C(0xf000)) << 4);
|
---|
9501 | pDstX87->FOP = uPtr.pu16[4] & UINT16_C(0x07ff);
|
---|
9502 | pDstX87->CS = 0;
|
---|
9503 | pDstX87->Rsrvd1= 0;
|
---|
9504 | pDstX87->DS = 0;
|
---|
9505 | pDstX87->Rsrvd2= 0;
|
---|
9506 | }
|
---|
9507 | else
|
---|
9508 | {
|
---|
9509 | pDstX87->FPUIP = uPtr.pu16[3];
|
---|
9510 | pDstX87->CS = uPtr.pu16[4];
|
---|
9511 | pDstX87->Rsrvd1= 0;
|
---|
9512 | pDstX87->FPUDP = uPtr.pu16[5];
|
---|
9513 | pDstX87->DS = uPtr.pu16[6];
|
---|
9514 | pDstX87->Rsrvd2= 0;
|
---|
9515 | /** @todo Testcase: Is FOP cleared when doing 16-bit protected mode fldenv? */
|
---|
9516 | }
|
---|
9517 | }
|
---|
9518 | else
|
---|
9519 | {
|
---|
9520 | pDstX87->FCW = uPtr.pu16[0*2];
|
---|
9521 | pDstX87->FSW = uPtr.pu16[1*2];
|
---|
9522 | pDstX87->FTW = uPtr.pu16[2*2];
|
---|
9523 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
9524 | {
|
---|
9525 | pDstX87->FPUIP = uPtr.pu16[3*2] | ((uPtr.pu32[4] & UINT32_C(0x0ffff000)) << 4);
|
---|
9526 | pDstX87->FOP = uPtr.pu32[4] & UINT16_C(0x07ff);
|
---|
9527 | pDstX87->FPUDP = uPtr.pu16[5*2] | ((uPtr.pu32[6] & UINT32_C(0x0ffff000)) << 4);
|
---|
9528 | pDstX87->CS = 0;
|
---|
9529 | pDstX87->Rsrvd1= 0;
|
---|
9530 | pDstX87->DS = 0;
|
---|
9531 | pDstX87->Rsrvd2= 0;
|
---|
9532 | }
|
---|
9533 | else
|
---|
9534 | {
|
---|
9535 | pDstX87->FPUIP = uPtr.pu32[3];
|
---|
9536 | pDstX87->CS = uPtr.pu16[4*2];
|
---|
9537 | pDstX87->Rsrvd1= 0;
|
---|
9538 | pDstX87->FOP = uPtr.pu16[4*2+1];
|
---|
9539 | pDstX87->FPUDP = uPtr.pu32[5];
|
---|
9540 | pDstX87->DS = uPtr.pu16[6*2];
|
---|
9541 | pDstX87->Rsrvd2= 0;
|
---|
9542 | }
|
---|
9543 | }
|
---|
9544 |
|
---|
9545 | /* Make adjustments. */
|
---|
9546 | pDstX87->FTW = iemFpuCompressFtw(pDstX87->FTW);
|
---|
9547 | #ifdef LOG_ENABLED
|
---|
9548 | uint16_t const fOldFsw = pDstX87->FSW;
|
---|
9549 | #endif
|
---|
9550 | pDstX87->FCW &= ~X86_FCW_ZERO_MASK | X86_FCW_IC_MASK; /* Intel 10980xe allows setting the IC bit. Win 3.11 CALC.EXE sets it. */
|
---|
9551 | iemFpuRecalcExceptionStatus(pDstX87);
|
---|
9552 | #ifdef LOG_ENABLED
|
---|
9553 | if ((pDstX87->FSW & X86_FSW_ES) ^ (fOldFsw & X86_FSW_ES))
|
---|
9554 | Log11(("iemCImplCommonFpuRestoreEnv: %04x:%08RX64: %s FPU exception (FCW=%#x FSW=%#x -> %#x)\n",
|
---|
9555 | pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip, fOldFsw & X86_FSW_ES ? "Supressed" : "Raised",
|
---|
9556 | pDstX87->FCW, fOldFsw, pDstX87->FSW));
|
---|
9557 | #endif
|
---|
9558 |
|
---|
9559 | /** @todo Testcase: Check if ES and/or B are automatically cleared if no
|
---|
9560 | * exceptions are pending after loading the saved state? */
|
---|
9561 | }
|
---|
9562 |
|
---|
9563 |
|
---|
9564 | /**
|
---|
9565 | * Implements 'FNSTENV'.
|
---|
9566 | *
|
---|
9567 | * @param enmEffOpSize The operand size (only REX.W really matters).
|
---|
9568 | * @param iEffSeg The effective segment register for @a GCPtrEffDst.
|
---|
9569 | * @param GCPtrEffDst The address of the image.
|
---|
9570 | */
|
---|
9571 | IEM_CIMPL_DEF_3(iemCImpl_fnstenv, IEMMODE, enmEffOpSize, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
|
---|
9572 | {
|
---|
9573 | RTPTRUNION uPtr;
|
---|
9574 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, &uPtr.pv, enmEffOpSize == IEMMODE_16BIT ? 14 : 28,
|
---|
9575 | iEffSeg, GCPtrEffDst, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE,
|
---|
9576 | enmEffOpSize == IEMMODE_16BIT ? 1 : 3 /** @todo ? */);
|
---|
9577 | if (rcStrict != VINF_SUCCESS)
|
---|
9578 | return rcStrict;
|
---|
9579 |
|
---|
9580 | iemCImplCommonFpuStoreEnv(pVCpu, enmEffOpSize, uPtr);
|
---|
9581 |
|
---|
9582 | rcStrict = iemMemCommitAndUnmap(pVCpu, uPtr.pv, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
|
---|
9583 | if (rcStrict != VINF_SUCCESS)
|
---|
9584 | return rcStrict;
|
---|
9585 |
|
---|
9586 | /* Mask all math exceptions. Any possibly pending exceptions will be cleared. */
|
---|
9587 | PX86FXSTATE pFpuCtx = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
9588 | pFpuCtx->FCW |= X86_FCW_XCPT_MASK;
|
---|
9589 | #ifdef LOG_ENABLED
|
---|
9590 | uint16_t fOldFsw = pFpuCtx->FSW;
|
---|
9591 | #endif
|
---|
9592 | iemFpuRecalcExceptionStatus(pFpuCtx);
|
---|
9593 | #ifdef LOG_ENABLED
|
---|
9594 | if ((pFpuCtx->FSW & X86_FSW_ES) ^ (fOldFsw & X86_FSW_ES))
|
---|
9595 | Log11(("fnstenv: %04x:%08RX64: %s FPU exception (FCW=%#x, FSW %#x -> %#x)\n", pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip,
|
---|
9596 | fOldFsw & X86_FSW_ES ? "Supressed" : "Raised", pFpuCtx->FCW, fOldFsw, pFpuCtx->FSW));
|
---|
9597 | #endif
|
---|
9598 |
|
---|
9599 | iemHlpUsedFpu(pVCpu);
|
---|
9600 |
|
---|
9601 | /* Note: C0, C1, C2 and C3 are documented as undefined, we leave them untouched! */
|
---|
9602 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
9603 | }
|
---|
9604 |
|
---|
9605 |
|
---|
9606 | /**
|
---|
9607 | * Implements 'FNSAVE'.
|
---|
9608 | *
|
---|
9609 | * @param enmEffOpSize The operand size.
|
---|
9610 | * @param iEffSeg The effective segment register for @a GCPtrEffDst.
|
---|
9611 | * @param GCPtrEffDst The address of the image.
|
---|
9612 | */
|
---|
9613 | IEM_CIMPL_DEF_3(iemCImpl_fnsave, IEMMODE, enmEffOpSize, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
|
---|
9614 | {
|
---|
9615 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87);
|
---|
9616 |
|
---|
9617 | RTPTRUNION uPtr;
|
---|
9618 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, &uPtr.pv, enmEffOpSize == IEMMODE_16BIT ? 94 : 108,
|
---|
9619 | iEffSeg, GCPtrEffDst, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE, 3 /** @todo ? */);
|
---|
9620 | if (rcStrict != VINF_SUCCESS)
|
---|
9621 | return rcStrict;
|
---|
9622 |
|
---|
9623 | PX86FXSTATE pFpuCtx = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
9624 | iemCImplCommonFpuStoreEnv(pVCpu, enmEffOpSize, uPtr);
|
---|
9625 | PRTFLOAT80U paRegs = (PRTFLOAT80U)(uPtr.pu8 + (enmEffOpSize == IEMMODE_16BIT ? 14 : 28));
|
---|
9626 | for (uint32_t i = 0; i < RT_ELEMENTS(pFpuCtx->aRegs); i++)
|
---|
9627 | {
|
---|
9628 | paRegs[i].au32[0] = pFpuCtx->aRegs[i].au32[0];
|
---|
9629 | paRegs[i].au32[1] = pFpuCtx->aRegs[i].au32[1];
|
---|
9630 | paRegs[i].au16[4] = pFpuCtx->aRegs[i].au16[4];
|
---|
9631 | }
|
---|
9632 |
|
---|
9633 | rcStrict = iemMemCommitAndUnmap(pVCpu, uPtr.pv, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
|
---|
9634 | if (rcStrict != VINF_SUCCESS)
|
---|
9635 | return rcStrict;
|
---|
9636 |
|
---|
9637 | /* Rotate the stack to account for changed TOS. */
|
---|
9638 | iemFpuRotateStackSetTop(pFpuCtx, 0);
|
---|
9639 |
|
---|
9640 | /*
|
---|
9641 | * Re-initialize the FPU context.
|
---|
9642 | */
|
---|
9643 | pFpuCtx->FCW = 0x37f;
|
---|
9644 | pFpuCtx->FSW = 0;
|
---|
9645 | pFpuCtx->FTW = 0x00; /* 0 - empty */
|
---|
9646 | pFpuCtx->FPUDP = 0;
|
---|
9647 | pFpuCtx->DS = 0;
|
---|
9648 | pFpuCtx->Rsrvd2= 0;
|
---|
9649 | pFpuCtx->FPUIP = 0;
|
---|
9650 | pFpuCtx->CS = 0;
|
---|
9651 | pFpuCtx->Rsrvd1= 0;
|
---|
9652 | pFpuCtx->FOP = 0;
|
---|
9653 |
|
---|
9654 | iemHlpUsedFpu(pVCpu);
|
---|
9655 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
9656 | }
|
---|
9657 |
|
---|
9658 |
|
---|
9659 |
|
---|
9660 | /**
|
---|
9661 | * Implements 'FLDENV'.
|
---|
9662 | *
|
---|
9663 | * @param enmEffOpSize The operand size (only REX.W really matters).
|
---|
9664 | * @param iEffSeg The effective segment register for @a GCPtrEffSrc.
|
---|
9665 | * @param GCPtrEffSrc The address of the image.
|
---|
9666 | */
|
---|
9667 | IEM_CIMPL_DEF_3(iemCImpl_fldenv, IEMMODE, enmEffOpSize, uint8_t, iEffSeg, RTGCPTR, GCPtrEffSrc)
|
---|
9668 | {
|
---|
9669 | RTCPTRUNION uPtr;
|
---|
9670 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, (void **)&uPtr.pv, enmEffOpSize == IEMMODE_16BIT ? 14 : 28,
|
---|
9671 | iEffSeg, GCPtrEffSrc, IEM_ACCESS_DATA_R,
|
---|
9672 | enmEffOpSize == IEMMODE_16BIT ? 1 : 3 /** @todo ?*/);
|
---|
9673 | if (rcStrict != VINF_SUCCESS)
|
---|
9674 | return rcStrict;
|
---|
9675 |
|
---|
9676 | iemCImplCommonFpuRestoreEnv(pVCpu, enmEffOpSize, uPtr);
|
---|
9677 |
|
---|
9678 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)uPtr.pv, IEM_ACCESS_DATA_R);
|
---|
9679 | if (rcStrict != VINF_SUCCESS)
|
---|
9680 | return rcStrict;
|
---|
9681 |
|
---|
9682 | iemHlpUsedFpu(pVCpu);
|
---|
9683 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
9684 | }
|
---|
9685 |
|
---|
9686 |
|
---|
9687 | /**
|
---|
9688 | * Implements 'FRSTOR'.
|
---|
9689 | *
|
---|
9690 | * @param enmEffOpSize The operand size.
|
---|
9691 | * @param iEffSeg The effective segment register for @a GCPtrEffSrc.
|
---|
9692 | * @param GCPtrEffSrc The address of the image.
|
---|
9693 | */
|
---|
9694 | IEM_CIMPL_DEF_3(iemCImpl_frstor, IEMMODE, enmEffOpSize, uint8_t, iEffSeg, RTGCPTR, GCPtrEffSrc)
|
---|
9695 | {
|
---|
9696 | RTCPTRUNION uPtr;
|
---|
9697 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, (void **)&uPtr.pv, enmEffOpSize == IEMMODE_16BIT ? 94 : 108,
|
---|
9698 | iEffSeg, GCPtrEffSrc, IEM_ACCESS_DATA_R, 3 /** @todo ?*/ );
|
---|
9699 | if (rcStrict != VINF_SUCCESS)
|
---|
9700 | return rcStrict;
|
---|
9701 |
|
---|
9702 | PX86FXSTATE pFpuCtx = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
9703 | iemCImplCommonFpuRestoreEnv(pVCpu, enmEffOpSize, uPtr);
|
---|
9704 | PCRTFLOAT80U paRegs = (PCRTFLOAT80U)(uPtr.pu8 + (enmEffOpSize == IEMMODE_16BIT ? 14 : 28));
|
---|
9705 | for (uint32_t i = 0; i < RT_ELEMENTS(pFpuCtx->aRegs); i++)
|
---|
9706 | {
|
---|
9707 | pFpuCtx->aRegs[i].au32[0] = paRegs[i].au32[0];
|
---|
9708 | pFpuCtx->aRegs[i].au32[1] = paRegs[i].au32[1];
|
---|
9709 | pFpuCtx->aRegs[i].au32[2] = paRegs[i].au16[4];
|
---|
9710 | pFpuCtx->aRegs[i].au32[3] = 0;
|
---|
9711 | }
|
---|
9712 |
|
---|
9713 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)uPtr.pv, IEM_ACCESS_DATA_R);
|
---|
9714 | if (rcStrict != VINF_SUCCESS)
|
---|
9715 | return rcStrict;
|
---|
9716 |
|
---|
9717 | iemHlpUsedFpu(pVCpu);
|
---|
9718 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
9719 | }
|
---|
9720 |
|
---|
9721 |
|
---|
9722 | /**
|
---|
9723 | * Implements 'FLDCW'.
|
---|
9724 | *
|
---|
9725 | * @param u16Fcw The new FCW.
|
---|
9726 | */
|
---|
9727 | IEM_CIMPL_DEF_1(iemCImpl_fldcw, uint16_t, u16Fcw)
|
---|
9728 | {
|
---|
9729 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87);
|
---|
9730 |
|
---|
9731 | /** @todo Testcase: Check what happens when trying to load X86_FCW_PC_RSVD. */
|
---|
9732 | /** @todo Testcase: Try see what happens when trying to set undefined bits
|
---|
9733 | * (other than 6 and 7). Currently ignoring them. */
|
---|
9734 | /** @todo Testcase: Test that it raises and loweres the FPU exception bits
|
---|
9735 | * according to FSW. (This is what is currently implemented.) */
|
---|
9736 | PX86FXSTATE pFpuCtx = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
9737 | pFpuCtx->FCW = u16Fcw & (~X86_FCW_ZERO_MASK | X86_FCW_IC_MASK); /* Intel 10980xe allows setting the IC bit. Win 3.11 CALC.EXE sets it. */
|
---|
9738 | #ifdef LOG_ENABLED
|
---|
9739 | uint16_t fOldFsw = pFpuCtx->FSW;
|
---|
9740 | #endif
|
---|
9741 | iemFpuRecalcExceptionStatus(pFpuCtx);
|
---|
9742 | #ifdef LOG_ENABLED
|
---|
9743 | if ((pFpuCtx->FSW & X86_FSW_ES) ^ (fOldFsw & X86_FSW_ES))
|
---|
9744 | Log11(("fldcw: %04x:%08RX64: %s FPU exception (FCW=%#x, FSW %#x -> %#x)\n", pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip,
|
---|
9745 | fOldFsw & X86_FSW_ES ? "Supressed" : "Raised", pFpuCtx->FCW, fOldFsw, pFpuCtx->FSW));
|
---|
9746 | #endif
|
---|
9747 |
|
---|
9748 | /* Note: C0, C1, C2 and C3 are documented as undefined, we leave them untouched! */
|
---|
9749 | iemHlpUsedFpu(pVCpu);
|
---|
9750 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
9751 | }
|
---|
9752 |
|
---|
9753 |
|
---|
9754 |
|
---|
9755 | /**
|
---|
9756 | * Implements the underflow case of fxch.
|
---|
9757 | *
|
---|
9758 | * @param iStReg The other stack register.
|
---|
9759 | * @param uFpuOpcode The FPU opcode (for simplicity).
|
---|
9760 | */
|
---|
9761 | IEM_CIMPL_DEF_2(iemCImpl_fxch_underflow, uint8_t, iStReg, uint16_t, uFpuOpcode)
|
---|
9762 | {
|
---|
9763 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87);
|
---|
9764 |
|
---|
9765 | PX86FXSTATE pFpuCtx = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
9766 | unsigned const iReg1 = X86_FSW_TOP_GET(pFpuCtx->FSW);
|
---|
9767 | unsigned const iReg2 = (iReg1 + iStReg) & X86_FSW_TOP_SMASK;
|
---|
9768 | Assert(!(RT_BIT(iReg1) & pFpuCtx->FTW) || !(RT_BIT(iReg2) & pFpuCtx->FTW));
|
---|
9769 |
|
---|
9770 | /** @todo Testcase: fxch underflow. Making assumptions that underflowed
|
---|
9771 | * registers are read as QNaN and then exchanged. This could be
|
---|
9772 | * wrong... */
|
---|
9773 | if (pFpuCtx->FCW & X86_FCW_IM)
|
---|
9774 | {
|
---|
9775 | if (RT_BIT(iReg1) & pFpuCtx->FTW)
|
---|
9776 | {
|
---|
9777 | if (RT_BIT(iReg2) & pFpuCtx->FTW)
|
---|
9778 | iemFpuStoreQNan(&pFpuCtx->aRegs[0].r80);
|
---|
9779 | else
|
---|
9780 | pFpuCtx->aRegs[0].r80 = pFpuCtx->aRegs[iStReg].r80;
|
---|
9781 | iemFpuStoreQNan(&pFpuCtx->aRegs[iStReg].r80);
|
---|
9782 | }
|
---|
9783 | else
|
---|
9784 | {
|
---|
9785 | pFpuCtx->aRegs[iStReg].r80 = pFpuCtx->aRegs[0].r80;
|
---|
9786 | iemFpuStoreQNan(&pFpuCtx->aRegs[0].r80);
|
---|
9787 | }
|
---|
9788 | pFpuCtx->FSW &= ~X86_FSW_C_MASK;
|
---|
9789 | pFpuCtx->FSW |= X86_FSW_C1 | X86_FSW_IE | X86_FSW_SF;
|
---|
9790 | }
|
---|
9791 | else
|
---|
9792 | {
|
---|
9793 | /* raise underflow exception, don't change anything. */
|
---|
9794 | pFpuCtx->FSW &= ~(X86_FSW_TOP_MASK | X86_FSW_XCPT_MASK);
|
---|
9795 | pFpuCtx->FSW |= X86_FSW_C1 | X86_FSW_IE | X86_FSW_SF | X86_FSW_ES | X86_FSW_B;
|
---|
9796 | Log11(("fxch: %04x:%08RX64: Underflow exception (FSW=%#x)\n",
|
---|
9797 | pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip, pFpuCtx->FSW));
|
---|
9798 | }
|
---|
9799 |
|
---|
9800 | iemFpuUpdateOpcodeAndIpWorkerEx(pVCpu, pFpuCtx, uFpuOpcode);
|
---|
9801 | iemHlpUsedFpu(pVCpu);
|
---|
9802 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
9803 | }
|
---|
9804 |
|
---|
9805 |
|
---|
9806 | /**
|
---|
9807 | * Implements 'FCOMI', 'FCOMIP', 'FUCOMI', and 'FUCOMIP'.
|
---|
9808 | *
|
---|
9809 | * @param iStReg The other stack register.
|
---|
9810 | * @param pfnAImpl The assembly comparison implementation.
|
---|
9811 | * @param uPopAndFpuOpcode Bits 15-0: The FPU opcode.
|
---|
9812 | * Bit 31: Whether we should pop the stack when
|
---|
9813 | * done or not.
|
---|
9814 | */
|
---|
9815 | IEM_CIMPL_DEF_3(iemCImpl_fcomi_fucomi, uint8_t, iStReg, PFNIEMAIMPLFPUR80EFL, pfnAImpl, uint32_t, uPopAndFpuOpcode)
|
---|
9816 | {
|
---|
9817 | Assert(iStReg < 8);
|
---|
9818 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87);
|
---|
9819 |
|
---|
9820 | /*
|
---|
9821 | * Raise exceptions.
|
---|
9822 | */
|
---|
9823 | if (pVCpu->cpum.GstCtx.cr0 & (X86_CR0_EM | X86_CR0_TS))
|
---|
9824 | return iemRaiseDeviceNotAvailable(pVCpu);
|
---|
9825 |
|
---|
9826 | PX86FXSTATE pFpuCtx = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
9827 | uint16_t u16Fsw = pFpuCtx->FSW;
|
---|
9828 | if (u16Fsw & X86_FSW_ES)
|
---|
9829 | return iemRaiseMathFault(pVCpu);
|
---|
9830 |
|
---|
9831 | /*
|
---|
9832 | * Check if any of the register accesses causes #SF + #IA.
|
---|
9833 | */
|
---|
9834 | bool fPop = RT_BOOL(uPopAndFpuOpcode & RT_BIT_32(31));
|
---|
9835 | unsigned const iReg1 = X86_FSW_TOP_GET(u16Fsw);
|
---|
9836 | unsigned const iReg2 = (iReg1 + iStReg) & X86_FSW_TOP_SMASK;
|
---|
9837 | if ((pFpuCtx->FTW & (RT_BIT(iReg1) | RT_BIT(iReg2))) == (RT_BIT(iReg1) | RT_BIT(iReg2)))
|
---|
9838 | {
|
---|
9839 | uint32_t u32Eflags = pfnAImpl(pFpuCtx, &u16Fsw, &pFpuCtx->aRegs[0].r80, &pFpuCtx->aRegs[iStReg].r80);
|
---|
9840 |
|
---|
9841 | pFpuCtx->FSW &= ~X86_FSW_C1;
|
---|
9842 | pFpuCtx->FSW |= u16Fsw & ~X86_FSW_TOP_MASK;
|
---|
9843 | if ( !(u16Fsw & X86_FSW_IE)
|
---|
9844 | || (pFpuCtx->FCW & X86_FCW_IM) )
|
---|
9845 | {
|
---|
9846 | pVCpu->cpum.GstCtx.eflags.u &= ~(X86_EFL_OF | X86_EFL_SF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_PF | X86_EFL_CF);
|
---|
9847 | pVCpu->cpum.GstCtx.eflags.u |= u32Eflags & (X86_EFL_ZF | X86_EFL_PF | X86_EFL_CF);
|
---|
9848 | }
|
---|
9849 | }
|
---|
9850 | else if (pFpuCtx->FCW & X86_FCW_IM)
|
---|
9851 | {
|
---|
9852 | /* Masked underflow. */
|
---|
9853 | pFpuCtx->FSW &= ~X86_FSW_C1;
|
---|
9854 | pFpuCtx->FSW |= X86_FSW_IE | X86_FSW_SF;
|
---|
9855 | pVCpu->cpum.GstCtx.eflags.u &= ~(X86_EFL_OF | X86_EFL_SF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_PF | X86_EFL_CF);
|
---|
9856 | pVCpu->cpum.GstCtx.eflags.u |= X86_EFL_ZF | X86_EFL_PF | X86_EFL_CF;
|
---|
9857 | }
|
---|
9858 | else
|
---|
9859 | {
|
---|
9860 | /* Raise underflow - don't touch EFLAGS or TOP. */
|
---|
9861 | pFpuCtx->FSW &= ~X86_FSW_C1;
|
---|
9862 | pFpuCtx->FSW |= X86_FSW_IE | X86_FSW_SF | X86_FSW_ES | X86_FSW_B;
|
---|
9863 | Log11(("fxch: %04x:%08RX64: Raising IE+SF exception (FSW=%#x)\n",
|
---|
9864 | pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip, pFpuCtx->FSW));
|
---|
9865 | fPop = false;
|
---|
9866 | }
|
---|
9867 |
|
---|
9868 | /*
|
---|
9869 | * Pop if necessary.
|
---|
9870 | */
|
---|
9871 | if (fPop)
|
---|
9872 | {
|
---|
9873 | pFpuCtx->FTW &= ~RT_BIT(iReg1);
|
---|
9874 | iemFpuStackIncTop(pVCpu);
|
---|
9875 | }
|
---|
9876 |
|
---|
9877 | iemFpuUpdateOpcodeAndIpWorkerEx(pVCpu, pFpuCtx, (uint16_t)uPopAndFpuOpcode);
|
---|
9878 | iemHlpUsedFpu(pVCpu);
|
---|
9879 | return iemRegAddToRipAndFinishingClearingRF(pVCpu, cbInstr);
|
---|
9880 | }
|
---|
9881 |
|
---|
9882 | /** @} */
|
---|
9883 |
|
---|