1 | /* $Id: IEMAllCImpl.cpp.h 92545 2021-11-22 12:12:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IEM - Instruction Implementation in C/C++ (code include).
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include "IEMAllCImplSvmInstr.cpp.h"
|
---|
19 | #include "IEMAllCImplVmxInstr.cpp.h"
|
---|
20 |
|
---|
21 |
|
---|
22 | /** @name Misc Helpers
|
---|
23 | * @{
|
---|
24 | */
|
---|
25 |
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Worker function for iemHlpCheckPortIOPermission, don't call directly.
|
---|
29 | *
|
---|
30 | * @returns Strict VBox status code.
|
---|
31 | *
|
---|
32 | * @param pVCpu The cross context virtual CPU structure of the calling thread.
|
---|
33 | * @param u16Port The port number.
|
---|
34 | * @param cbOperand The operand size.
|
---|
35 | */
|
---|
36 | static VBOXSTRICTRC iemHlpCheckPortIOPermissionBitmap(PVMCPUCC pVCpu, uint16_t u16Port, uint8_t cbOperand)
|
---|
37 | {
|
---|
38 | /* The TSS bits we're interested in are the same on 386 and AMD64. */
|
---|
39 | AssertCompile(AMD64_SEL_TYPE_SYS_TSS_BUSY == X86_SEL_TYPE_SYS_386_TSS_BUSY);
|
---|
40 | AssertCompile(AMD64_SEL_TYPE_SYS_TSS_AVAIL == X86_SEL_TYPE_SYS_386_TSS_AVAIL);
|
---|
41 | AssertCompileMembersAtSameOffset(X86TSS32, offIoBitmap, X86TSS64, offIoBitmap);
|
---|
42 | AssertCompile(sizeof(X86TSS32) == sizeof(X86TSS64));
|
---|
43 |
|
---|
44 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_TR);
|
---|
45 |
|
---|
46 | /*
|
---|
47 | * Check the TSS type, 16-bit TSSes doesn't have any I/O permission bitmap.
|
---|
48 | */
|
---|
49 | Assert(!pVCpu->cpum.GstCtx.tr.Attr.n.u1DescType);
|
---|
50 | if (RT_UNLIKELY( pVCpu->cpum.GstCtx.tr.Attr.n.u4Type != AMD64_SEL_TYPE_SYS_TSS_BUSY
|
---|
51 | && pVCpu->cpum.GstCtx.tr.Attr.n.u4Type != AMD64_SEL_TYPE_SYS_TSS_AVAIL))
|
---|
52 | {
|
---|
53 | Log(("iemHlpCheckPortIOPermissionBitmap: Port=%#x cb=%d - TSS type %#x (attr=%#x) has no I/O bitmap -> #GP(0)\n",
|
---|
54 | u16Port, cbOperand, pVCpu->cpum.GstCtx.tr.Attr.n.u4Type, pVCpu->cpum.GstCtx.tr.Attr.u));
|
---|
55 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
56 | }
|
---|
57 |
|
---|
58 | /*
|
---|
59 | * Read the bitmap offset (may #PF).
|
---|
60 | */
|
---|
61 | uint16_t offBitmap;
|
---|
62 | VBOXSTRICTRC rcStrict = iemMemFetchSysU16(pVCpu, &offBitmap, UINT8_MAX,
|
---|
63 | pVCpu->cpum.GstCtx.tr.u64Base + RT_UOFFSETOF(X86TSS64, offIoBitmap));
|
---|
64 | if (rcStrict != VINF_SUCCESS)
|
---|
65 | {
|
---|
66 | Log(("iemHlpCheckPortIOPermissionBitmap: Error reading offIoBitmap (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
67 | return rcStrict;
|
---|
68 | }
|
---|
69 |
|
---|
70 | /*
|
---|
71 | * The bit range from u16Port to (u16Port + cbOperand - 1), however intel
|
---|
72 | * describes the CPU actually reading two bytes regardless of whether the
|
---|
73 | * bit range crosses a byte boundrary. Thus the + 1 in the test below.
|
---|
74 | */
|
---|
75 | uint32_t offFirstBit = (uint32_t)u16Port / 8 + offBitmap;
|
---|
76 | /** @todo check if real CPUs ensures that offBitmap has a minimum value of
|
---|
77 | * for instance sizeof(X86TSS32). */
|
---|
78 | if (offFirstBit + 1 > pVCpu->cpum.GstCtx.tr.u32Limit) /* the limit is inclusive */
|
---|
79 | {
|
---|
80 | Log(("iemHlpCheckPortIOPermissionBitmap: offFirstBit=%#x + 1 is beyond u32Limit=%#x -> #GP(0)\n",
|
---|
81 | offFirstBit, pVCpu->cpum.GstCtx.tr.u32Limit));
|
---|
82 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
83 | }
|
---|
84 |
|
---|
85 | /*
|
---|
86 | * Read the necessary bits.
|
---|
87 | */
|
---|
88 | /** @todo Test the assertion in the intel manual that the CPU reads two
|
---|
89 | * bytes. The question is how this works wrt to #PF and #GP on the
|
---|
90 | * 2nd byte when it's not required. */
|
---|
91 | uint16_t bmBytes = UINT16_MAX;
|
---|
92 | rcStrict = iemMemFetchSysU16(pVCpu, &bmBytes, UINT8_MAX, pVCpu->cpum.GstCtx.tr.u64Base + offFirstBit);
|
---|
93 | if (rcStrict != VINF_SUCCESS)
|
---|
94 | {
|
---|
95 | Log(("iemHlpCheckPortIOPermissionBitmap: Error reading I/O bitmap @%#x (%Rrc)\n", offFirstBit, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
96 | return rcStrict;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /*
|
---|
100 | * Perform the check.
|
---|
101 | */
|
---|
102 | uint16_t fPortMask = (1 << cbOperand) - 1;
|
---|
103 | bmBytes >>= (u16Port & 7);
|
---|
104 | if (bmBytes & fPortMask)
|
---|
105 | {
|
---|
106 | Log(("iemHlpCheckPortIOPermissionBitmap: u16Port=%#x LB %u - access denied (bm=%#x mask=%#x) -> #GP(0)\n",
|
---|
107 | u16Port, cbOperand, bmBytes, fPortMask));
|
---|
108 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
109 | }
|
---|
110 |
|
---|
111 | return VINF_SUCCESS;
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Checks if we are allowed to access the given I/O port, raising the
|
---|
117 | * appropriate exceptions if we aren't (or if the I/O bitmap is not
|
---|
118 | * accessible).
|
---|
119 | *
|
---|
120 | * @returns Strict VBox status code.
|
---|
121 | *
|
---|
122 | * @param pVCpu The cross context virtual CPU structure of the calling thread.
|
---|
123 | * @param u16Port The port number.
|
---|
124 | * @param cbOperand The operand size.
|
---|
125 | */
|
---|
126 | DECLINLINE(VBOXSTRICTRC) iemHlpCheckPortIOPermission(PVMCPUCC pVCpu, uint16_t u16Port, uint8_t cbOperand)
|
---|
127 | {
|
---|
128 | X86EFLAGS Efl;
|
---|
129 | Efl.u = IEMMISC_GET_EFL(pVCpu);
|
---|
130 | if ( (pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE)
|
---|
131 | && ( pVCpu->iem.s.uCpl > Efl.Bits.u2IOPL
|
---|
132 | || Efl.Bits.u1VM) )
|
---|
133 | return iemHlpCheckPortIOPermissionBitmap(pVCpu, u16Port, cbOperand);
|
---|
134 | return VINF_SUCCESS;
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | #if 0
|
---|
139 | /**
|
---|
140 | * Calculates the parity bit.
|
---|
141 | *
|
---|
142 | * @returns true if the bit is set, false if not.
|
---|
143 | * @param u8Result The least significant byte of the result.
|
---|
144 | */
|
---|
145 | static bool iemHlpCalcParityFlag(uint8_t u8Result)
|
---|
146 | {
|
---|
147 | /*
|
---|
148 | * Parity is set if the number of bits in the least significant byte of
|
---|
149 | * the result is even.
|
---|
150 | */
|
---|
151 | uint8_t cBits;
|
---|
152 | cBits = u8Result & 1; /* 0 */
|
---|
153 | u8Result >>= 1;
|
---|
154 | cBits += u8Result & 1;
|
---|
155 | u8Result >>= 1;
|
---|
156 | cBits += u8Result & 1;
|
---|
157 | u8Result >>= 1;
|
---|
158 | cBits += u8Result & 1;
|
---|
159 | u8Result >>= 1;
|
---|
160 | cBits += u8Result & 1; /* 4 */
|
---|
161 | u8Result >>= 1;
|
---|
162 | cBits += u8Result & 1;
|
---|
163 | u8Result >>= 1;
|
---|
164 | cBits += u8Result & 1;
|
---|
165 | u8Result >>= 1;
|
---|
166 | cBits += u8Result & 1;
|
---|
167 | return !(cBits & 1);
|
---|
168 | }
|
---|
169 | #endif /* not used */
|
---|
170 |
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Updates the specified flags according to a 8-bit result.
|
---|
174 | *
|
---|
175 | * @param pVCpu The cross context virtual CPU structure of the calling thread.
|
---|
176 | * @param u8Result The result to set the flags according to.
|
---|
177 | * @param fToUpdate The flags to update.
|
---|
178 | * @param fUndefined The flags that are specified as undefined.
|
---|
179 | */
|
---|
180 | static void iemHlpUpdateArithEFlagsU8(PVMCPUCC pVCpu, uint8_t u8Result, uint32_t fToUpdate, uint32_t fUndefined)
|
---|
181 | {
|
---|
182 | uint32_t fEFlags = pVCpu->cpum.GstCtx.eflags.u;
|
---|
183 | iemAImpl_test_u8(&u8Result, u8Result, &fEFlags);
|
---|
184 | pVCpu->cpum.GstCtx.eflags.u &= ~(fToUpdate | fUndefined);
|
---|
185 | pVCpu->cpum.GstCtx.eflags.u |= (fToUpdate | fUndefined) & fEFlags;
|
---|
186 | }
|
---|
187 |
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Updates the specified flags according to a 16-bit result.
|
---|
191 | *
|
---|
192 | * @param pVCpu The cross context virtual CPU structure of the calling thread.
|
---|
193 | * @param u16Result The result to set the flags according to.
|
---|
194 | * @param fToUpdate The flags to update.
|
---|
195 | * @param fUndefined The flags that are specified as undefined.
|
---|
196 | */
|
---|
197 | static void iemHlpUpdateArithEFlagsU16(PVMCPUCC pVCpu, uint16_t u16Result, uint32_t fToUpdate, uint32_t fUndefined)
|
---|
198 | {
|
---|
199 | uint32_t fEFlags = pVCpu->cpum.GstCtx.eflags.u;
|
---|
200 | iemAImpl_test_u16(&u16Result, u16Result, &fEFlags);
|
---|
201 | pVCpu->cpum.GstCtx.eflags.u &= ~(fToUpdate | fUndefined);
|
---|
202 | pVCpu->cpum.GstCtx.eflags.u |= (fToUpdate | fUndefined) & fEFlags;
|
---|
203 | }
|
---|
204 |
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Helper used by iret.
|
---|
208 | *
|
---|
209 | * @param pVCpu The cross context virtual CPU structure of the calling thread.
|
---|
210 | * @param uCpl The new CPL.
|
---|
211 | * @param pSReg Pointer to the segment register.
|
---|
212 | */
|
---|
213 | static void iemHlpAdjustSelectorForNewCpl(PVMCPUCC pVCpu, uint8_t uCpl, PCPUMSELREG pSReg)
|
---|
214 | {
|
---|
215 | Assert(CPUMSELREG_ARE_HIDDEN_PARTS_VALID(pVCpu, pSReg));
|
---|
216 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_SREG_MASK);
|
---|
217 |
|
---|
218 | if ( uCpl > pSReg->Attr.n.u2Dpl
|
---|
219 | && pSReg->Attr.n.u1DescType /* code or data, not system */
|
---|
220 | && (pSReg->Attr.n.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
|
---|
221 | != (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF)) /* not conforming code */
|
---|
222 | iemHlpLoadNullDataSelectorProt(pVCpu, pSReg, 0);
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * Indicates that we have modified the FPU state.
|
---|
228 | *
|
---|
229 | * @param pVCpu The cross context virtual CPU structure of the calling thread.
|
---|
230 | */
|
---|
231 | DECLINLINE(void) iemHlpUsedFpu(PVMCPUCC pVCpu)
|
---|
232 | {
|
---|
233 | CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_FPU_REM);
|
---|
234 | }
|
---|
235 |
|
---|
236 | /** @} */
|
---|
237 |
|
---|
238 | /** @name C Implementations
|
---|
239 | * @{
|
---|
240 | */
|
---|
241 |
|
---|
242 | /**
|
---|
243 | * Implements a 16-bit popa.
|
---|
244 | */
|
---|
245 | IEM_CIMPL_DEF_0(iemCImpl_popa_16)
|
---|
246 | {
|
---|
247 | RTGCPTR GCPtrStart = iemRegGetEffRsp(pVCpu);
|
---|
248 | RTGCPTR GCPtrLast = GCPtrStart + 15;
|
---|
249 | VBOXSTRICTRC rcStrict;
|
---|
250 |
|
---|
251 | /*
|
---|
252 | * The docs are a bit hard to comprehend here, but it looks like we wrap
|
---|
253 | * around in real mode as long as none of the individual "popa" crosses the
|
---|
254 | * end of the stack segment. In protected mode we check the whole access
|
---|
255 | * in one go. For efficiency, only do the word-by-word thing if we're in
|
---|
256 | * danger of wrapping around.
|
---|
257 | */
|
---|
258 | /** @todo do popa boundary / wrap-around checks. */
|
---|
259 | if (RT_UNLIKELY( IEM_IS_REAL_OR_V86_MODE(pVCpu)
|
---|
260 | && (pVCpu->cpum.GstCtx.cs.u32Limit < GCPtrLast)) ) /* ASSUMES 64-bit RTGCPTR */
|
---|
261 | {
|
---|
262 | /* word-by-word */
|
---|
263 | RTUINT64U TmpRsp;
|
---|
264 | TmpRsp.u = pVCpu->cpum.GstCtx.rsp;
|
---|
265 | rcStrict = iemMemStackPopU16Ex(pVCpu, &pVCpu->cpum.GstCtx.di, &TmpRsp);
|
---|
266 | if (rcStrict == VINF_SUCCESS)
|
---|
267 | rcStrict = iemMemStackPopU16Ex(pVCpu, &pVCpu->cpum.GstCtx.si, &TmpRsp);
|
---|
268 | if (rcStrict == VINF_SUCCESS)
|
---|
269 | rcStrict = iemMemStackPopU16Ex(pVCpu, &pVCpu->cpum.GstCtx.bp, &TmpRsp);
|
---|
270 | if (rcStrict == VINF_SUCCESS)
|
---|
271 | {
|
---|
272 | iemRegAddToRspEx(pVCpu, &TmpRsp, 2); /* sp */
|
---|
273 | rcStrict = iemMemStackPopU16Ex(pVCpu, &pVCpu->cpum.GstCtx.bx, &TmpRsp);
|
---|
274 | }
|
---|
275 | if (rcStrict == VINF_SUCCESS)
|
---|
276 | rcStrict = iemMemStackPopU16Ex(pVCpu, &pVCpu->cpum.GstCtx.dx, &TmpRsp);
|
---|
277 | if (rcStrict == VINF_SUCCESS)
|
---|
278 | rcStrict = iemMemStackPopU16Ex(pVCpu, &pVCpu->cpum.GstCtx.cx, &TmpRsp);
|
---|
279 | if (rcStrict == VINF_SUCCESS)
|
---|
280 | rcStrict = iemMemStackPopU16Ex(pVCpu, &pVCpu->cpum.GstCtx.ax, &TmpRsp);
|
---|
281 | if (rcStrict == VINF_SUCCESS)
|
---|
282 | {
|
---|
283 | pVCpu->cpum.GstCtx.rsp = TmpRsp.u;
|
---|
284 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
285 | }
|
---|
286 | }
|
---|
287 | else
|
---|
288 | {
|
---|
289 | uint16_t const *pa16Mem = NULL;
|
---|
290 | rcStrict = iemMemMap(pVCpu, (void **)&pa16Mem, 16, X86_SREG_SS, GCPtrStart, IEM_ACCESS_STACK_R);
|
---|
291 | if (rcStrict == VINF_SUCCESS)
|
---|
292 | {
|
---|
293 | pVCpu->cpum.GstCtx.di = pa16Mem[7 - X86_GREG_xDI];
|
---|
294 | pVCpu->cpum.GstCtx.si = pa16Mem[7 - X86_GREG_xSI];
|
---|
295 | pVCpu->cpum.GstCtx.bp = pa16Mem[7 - X86_GREG_xBP];
|
---|
296 | /* skip sp */
|
---|
297 | pVCpu->cpum.GstCtx.bx = pa16Mem[7 - X86_GREG_xBX];
|
---|
298 | pVCpu->cpum.GstCtx.dx = pa16Mem[7 - X86_GREG_xDX];
|
---|
299 | pVCpu->cpum.GstCtx.cx = pa16Mem[7 - X86_GREG_xCX];
|
---|
300 | pVCpu->cpum.GstCtx.ax = pa16Mem[7 - X86_GREG_xAX];
|
---|
301 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)pa16Mem, IEM_ACCESS_STACK_R);
|
---|
302 | if (rcStrict == VINF_SUCCESS)
|
---|
303 | {
|
---|
304 | iemRegAddToRsp(pVCpu, 16);
|
---|
305 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
306 | }
|
---|
307 | }
|
---|
308 | }
|
---|
309 | return rcStrict;
|
---|
310 | }
|
---|
311 |
|
---|
312 |
|
---|
313 | /**
|
---|
314 | * Implements a 32-bit popa.
|
---|
315 | */
|
---|
316 | IEM_CIMPL_DEF_0(iemCImpl_popa_32)
|
---|
317 | {
|
---|
318 | RTGCPTR GCPtrStart = iemRegGetEffRsp(pVCpu);
|
---|
319 | RTGCPTR GCPtrLast = GCPtrStart + 31;
|
---|
320 | VBOXSTRICTRC rcStrict;
|
---|
321 |
|
---|
322 | /*
|
---|
323 | * The docs are a bit hard to comprehend here, but it looks like we wrap
|
---|
324 | * around in real mode as long as none of the individual "popa" crosses the
|
---|
325 | * end of the stack segment. In protected mode we check the whole access
|
---|
326 | * in one go. For efficiency, only do the word-by-word thing if we're in
|
---|
327 | * danger of wrapping around.
|
---|
328 | */
|
---|
329 | /** @todo do popa boundary / wrap-around checks. */
|
---|
330 | if (RT_UNLIKELY( IEM_IS_REAL_OR_V86_MODE(pVCpu)
|
---|
331 | && (pVCpu->cpum.GstCtx.cs.u32Limit < GCPtrLast)) ) /* ASSUMES 64-bit RTGCPTR */
|
---|
332 | {
|
---|
333 | /* word-by-word */
|
---|
334 | RTUINT64U TmpRsp;
|
---|
335 | TmpRsp.u = pVCpu->cpum.GstCtx.rsp;
|
---|
336 | rcStrict = iemMemStackPopU32Ex(pVCpu, &pVCpu->cpum.GstCtx.edi, &TmpRsp);
|
---|
337 | if (rcStrict == VINF_SUCCESS)
|
---|
338 | rcStrict = iemMemStackPopU32Ex(pVCpu, &pVCpu->cpum.GstCtx.esi, &TmpRsp);
|
---|
339 | if (rcStrict == VINF_SUCCESS)
|
---|
340 | rcStrict = iemMemStackPopU32Ex(pVCpu, &pVCpu->cpum.GstCtx.ebp, &TmpRsp);
|
---|
341 | if (rcStrict == VINF_SUCCESS)
|
---|
342 | {
|
---|
343 | iemRegAddToRspEx(pVCpu, &TmpRsp, 2); /* sp */
|
---|
344 | rcStrict = iemMemStackPopU32Ex(pVCpu, &pVCpu->cpum.GstCtx.ebx, &TmpRsp);
|
---|
345 | }
|
---|
346 | if (rcStrict == VINF_SUCCESS)
|
---|
347 | rcStrict = iemMemStackPopU32Ex(pVCpu, &pVCpu->cpum.GstCtx.edx, &TmpRsp);
|
---|
348 | if (rcStrict == VINF_SUCCESS)
|
---|
349 | rcStrict = iemMemStackPopU32Ex(pVCpu, &pVCpu->cpum.GstCtx.ecx, &TmpRsp);
|
---|
350 | if (rcStrict == VINF_SUCCESS)
|
---|
351 | rcStrict = iemMemStackPopU32Ex(pVCpu, &pVCpu->cpum.GstCtx.eax, &TmpRsp);
|
---|
352 | if (rcStrict == VINF_SUCCESS)
|
---|
353 | {
|
---|
354 | #if 1 /** @todo what actually happens with the high bits when we're in 16-bit mode? */
|
---|
355 | pVCpu->cpum.GstCtx.rdi &= UINT32_MAX;
|
---|
356 | pVCpu->cpum.GstCtx.rsi &= UINT32_MAX;
|
---|
357 | pVCpu->cpum.GstCtx.rbp &= UINT32_MAX;
|
---|
358 | pVCpu->cpum.GstCtx.rbx &= UINT32_MAX;
|
---|
359 | pVCpu->cpum.GstCtx.rdx &= UINT32_MAX;
|
---|
360 | pVCpu->cpum.GstCtx.rcx &= UINT32_MAX;
|
---|
361 | pVCpu->cpum.GstCtx.rax &= UINT32_MAX;
|
---|
362 | #endif
|
---|
363 | pVCpu->cpum.GstCtx.rsp = TmpRsp.u;
|
---|
364 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
365 | }
|
---|
366 | }
|
---|
367 | else
|
---|
368 | {
|
---|
369 | uint32_t const *pa32Mem;
|
---|
370 | rcStrict = iemMemMap(pVCpu, (void **)&pa32Mem, 32, X86_SREG_SS, GCPtrStart, IEM_ACCESS_STACK_R);
|
---|
371 | if (rcStrict == VINF_SUCCESS)
|
---|
372 | {
|
---|
373 | pVCpu->cpum.GstCtx.rdi = pa32Mem[7 - X86_GREG_xDI];
|
---|
374 | pVCpu->cpum.GstCtx.rsi = pa32Mem[7 - X86_GREG_xSI];
|
---|
375 | pVCpu->cpum.GstCtx.rbp = pa32Mem[7 - X86_GREG_xBP];
|
---|
376 | /* skip esp */
|
---|
377 | pVCpu->cpum.GstCtx.rbx = pa32Mem[7 - X86_GREG_xBX];
|
---|
378 | pVCpu->cpum.GstCtx.rdx = pa32Mem[7 - X86_GREG_xDX];
|
---|
379 | pVCpu->cpum.GstCtx.rcx = pa32Mem[7 - X86_GREG_xCX];
|
---|
380 | pVCpu->cpum.GstCtx.rax = pa32Mem[7 - X86_GREG_xAX];
|
---|
381 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)pa32Mem, IEM_ACCESS_STACK_R);
|
---|
382 | if (rcStrict == VINF_SUCCESS)
|
---|
383 | {
|
---|
384 | iemRegAddToRsp(pVCpu, 32);
|
---|
385 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
386 | }
|
---|
387 | }
|
---|
388 | }
|
---|
389 | return rcStrict;
|
---|
390 | }
|
---|
391 |
|
---|
392 |
|
---|
393 | /**
|
---|
394 | * Implements a 16-bit pusha.
|
---|
395 | */
|
---|
396 | IEM_CIMPL_DEF_0(iemCImpl_pusha_16)
|
---|
397 | {
|
---|
398 | RTGCPTR GCPtrTop = iemRegGetEffRsp(pVCpu);
|
---|
399 | RTGCPTR GCPtrBottom = GCPtrTop - 15;
|
---|
400 | VBOXSTRICTRC rcStrict;
|
---|
401 |
|
---|
402 | /*
|
---|
403 | * The docs are a bit hard to comprehend here, but it looks like we wrap
|
---|
404 | * around in real mode as long as none of the individual "pushd" crosses the
|
---|
405 | * end of the stack segment. In protected mode we check the whole access
|
---|
406 | * in one go. For efficiency, only do the word-by-word thing if we're in
|
---|
407 | * danger of wrapping around.
|
---|
408 | */
|
---|
409 | /** @todo do pusha boundary / wrap-around checks. */
|
---|
410 | if (RT_UNLIKELY( GCPtrBottom > GCPtrTop
|
---|
411 | && IEM_IS_REAL_OR_V86_MODE(pVCpu) ) )
|
---|
412 | {
|
---|
413 | /* word-by-word */
|
---|
414 | RTUINT64U TmpRsp;
|
---|
415 | TmpRsp.u = pVCpu->cpum.GstCtx.rsp;
|
---|
416 | rcStrict = iemMemStackPushU16Ex(pVCpu, pVCpu->cpum.GstCtx.ax, &TmpRsp);
|
---|
417 | if (rcStrict == VINF_SUCCESS)
|
---|
418 | rcStrict = iemMemStackPushU16Ex(pVCpu, pVCpu->cpum.GstCtx.cx, &TmpRsp);
|
---|
419 | if (rcStrict == VINF_SUCCESS)
|
---|
420 | rcStrict = iemMemStackPushU16Ex(pVCpu, pVCpu->cpum.GstCtx.dx, &TmpRsp);
|
---|
421 | if (rcStrict == VINF_SUCCESS)
|
---|
422 | rcStrict = iemMemStackPushU16Ex(pVCpu, pVCpu->cpum.GstCtx.bx, &TmpRsp);
|
---|
423 | if (rcStrict == VINF_SUCCESS)
|
---|
424 | rcStrict = iemMemStackPushU16Ex(pVCpu, pVCpu->cpum.GstCtx.sp, &TmpRsp);
|
---|
425 | if (rcStrict == VINF_SUCCESS)
|
---|
426 | rcStrict = iemMemStackPushU16Ex(pVCpu, pVCpu->cpum.GstCtx.bp, &TmpRsp);
|
---|
427 | if (rcStrict == VINF_SUCCESS)
|
---|
428 | rcStrict = iemMemStackPushU16Ex(pVCpu, pVCpu->cpum.GstCtx.si, &TmpRsp);
|
---|
429 | if (rcStrict == VINF_SUCCESS)
|
---|
430 | rcStrict = iemMemStackPushU16Ex(pVCpu, pVCpu->cpum.GstCtx.di, &TmpRsp);
|
---|
431 | if (rcStrict == VINF_SUCCESS)
|
---|
432 | {
|
---|
433 | pVCpu->cpum.GstCtx.rsp = TmpRsp.u;
|
---|
434 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
435 | }
|
---|
436 | }
|
---|
437 | else
|
---|
438 | {
|
---|
439 | GCPtrBottom--;
|
---|
440 | uint16_t *pa16Mem = NULL;
|
---|
441 | rcStrict = iemMemMap(pVCpu, (void **)&pa16Mem, 16, X86_SREG_SS, GCPtrBottom, IEM_ACCESS_STACK_W);
|
---|
442 | if (rcStrict == VINF_SUCCESS)
|
---|
443 | {
|
---|
444 | pa16Mem[7 - X86_GREG_xDI] = pVCpu->cpum.GstCtx.di;
|
---|
445 | pa16Mem[7 - X86_GREG_xSI] = pVCpu->cpum.GstCtx.si;
|
---|
446 | pa16Mem[7 - X86_GREG_xBP] = pVCpu->cpum.GstCtx.bp;
|
---|
447 | pa16Mem[7 - X86_GREG_xSP] = pVCpu->cpum.GstCtx.sp;
|
---|
448 | pa16Mem[7 - X86_GREG_xBX] = pVCpu->cpum.GstCtx.bx;
|
---|
449 | pa16Mem[7 - X86_GREG_xDX] = pVCpu->cpum.GstCtx.dx;
|
---|
450 | pa16Mem[7 - X86_GREG_xCX] = pVCpu->cpum.GstCtx.cx;
|
---|
451 | pa16Mem[7 - X86_GREG_xAX] = pVCpu->cpum.GstCtx.ax;
|
---|
452 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)pa16Mem, IEM_ACCESS_STACK_W);
|
---|
453 | if (rcStrict == VINF_SUCCESS)
|
---|
454 | {
|
---|
455 | iemRegSubFromRsp(pVCpu, 16);
|
---|
456 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
457 | }
|
---|
458 | }
|
---|
459 | }
|
---|
460 | return rcStrict;
|
---|
461 | }
|
---|
462 |
|
---|
463 |
|
---|
464 | /**
|
---|
465 | * Implements a 32-bit pusha.
|
---|
466 | */
|
---|
467 | IEM_CIMPL_DEF_0(iemCImpl_pusha_32)
|
---|
468 | {
|
---|
469 | RTGCPTR GCPtrTop = iemRegGetEffRsp(pVCpu);
|
---|
470 | RTGCPTR GCPtrBottom = GCPtrTop - 31;
|
---|
471 | VBOXSTRICTRC rcStrict;
|
---|
472 |
|
---|
473 | /*
|
---|
474 | * The docs are a bit hard to comprehend here, but it looks like we wrap
|
---|
475 | * around in real mode as long as none of the individual "pusha" crosses the
|
---|
476 | * end of the stack segment. In protected mode we check the whole access
|
---|
477 | * in one go. For efficiency, only do the word-by-word thing if we're in
|
---|
478 | * danger of wrapping around.
|
---|
479 | */
|
---|
480 | /** @todo do pusha boundary / wrap-around checks. */
|
---|
481 | if (RT_UNLIKELY( GCPtrBottom > GCPtrTop
|
---|
482 | && IEM_IS_REAL_OR_V86_MODE(pVCpu) ) )
|
---|
483 | {
|
---|
484 | /* word-by-word */
|
---|
485 | RTUINT64U TmpRsp;
|
---|
486 | TmpRsp.u = pVCpu->cpum.GstCtx.rsp;
|
---|
487 | rcStrict = iemMemStackPushU32Ex(pVCpu, pVCpu->cpum.GstCtx.eax, &TmpRsp);
|
---|
488 | if (rcStrict == VINF_SUCCESS)
|
---|
489 | rcStrict = iemMemStackPushU32Ex(pVCpu, pVCpu->cpum.GstCtx.ecx, &TmpRsp);
|
---|
490 | if (rcStrict == VINF_SUCCESS)
|
---|
491 | rcStrict = iemMemStackPushU32Ex(pVCpu, pVCpu->cpum.GstCtx.edx, &TmpRsp);
|
---|
492 | if (rcStrict == VINF_SUCCESS)
|
---|
493 | rcStrict = iemMemStackPushU32Ex(pVCpu, pVCpu->cpum.GstCtx.ebx, &TmpRsp);
|
---|
494 | if (rcStrict == VINF_SUCCESS)
|
---|
495 | rcStrict = iemMemStackPushU32Ex(pVCpu, pVCpu->cpum.GstCtx.esp, &TmpRsp);
|
---|
496 | if (rcStrict == VINF_SUCCESS)
|
---|
497 | rcStrict = iemMemStackPushU32Ex(pVCpu, pVCpu->cpum.GstCtx.ebp, &TmpRsp);
|
---|
498 | if (rcStrict == VINF_SUCCESS)
|
---|
499 | rcStrict = iemMemStackPushU32Ex(pVCpu, pVCpu->cpum.GstCtx.esi, &TmpRsp);
|
---|
500 | if (rcStrict == VINF_SUCCESS)
|
---|
501 | rcStrict = iemMemStackPushU32Ex(pVCpu, pVCpu->cpum.GstCtx.edi, &TmpRsp);
|
---|
502 | if (rcStrict == VINF_SUCCESS)
|
---|
503 | {
|
---|
504 | pVCpu->cpum.GstCtx.rsp = TmpRsp.u;
|
---|
505 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
506 | }
|
---|
507 | }
|
---|
508 | else
|
---|
509 | {
|
---|
510 | GCPtrBottom--;
|
---|
511 | uint32_t *pa32Mem;
|
---|
512 | rcStrict = iemMemMap(pVCpu, (void **)&pa32Mem, 32, X86_SREG_SS, GCPtrBottom, IEM_ACCESS_STACK_W);
|
---|
513 | if (rcStrict == VINF_SUCCESS)
|
---|
514 | {
|
---|
515 | pa32Mem[7 - X86_GREG_xDI] = pVCpu->cpum.GstCtx.edi;
|
---|
516 | pa32Mem[7 - X86_GREG_xSI] = pVCpu->cpum.GstCtx.esi;
|
---|
517 | pa32Mem[7 - X86_GREG_xBP] = pVCpu->cpum.GstCtx.ebp;
|
---|
518 | pa32Mem[7 - X86_GREG_xSP] = pVCpu->cpum.GstCtx.esp;
|
---|
519 | pa32Mem[7 - X86_GREG_xBX] = pVCpu->cpum.GstCtx.ebx;
|
---|
520 | pa32Mem[7 - X86_GREG_xDX] = pVCpu->cpum.GstCtx.edx;
|
---|
521 | pa32Mem[7 - X86_GREG_xCX] = pVCpu->cpum.GstCtx.ecx;
|
---|
522 | pa32Mem[7 - X86_GREG_xAX] = pVCpu->cpum.GstCtx.eax;
|
---|
523 | rcStrict = iemMemCommitAndUnmap(pVCpu, pa32Mem, IEM_ACCESS_STACK_W);
|
---|
524 | if (rcStrict == VINF_SUCCESS)
|
---|
525 | {
|
---|
526 | iemRegSubFromRsp(pVCpu, 32);
|
---|
527 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
528 | }
|
---|
529 | }
|
---|
530 | }
|
---|
531 | return rcStrict;
|
---|
532 | }
|
---|
533 |
|
---|
534 |
|
---|
535 | /**
|
---|
536 | * Implements pushf.
|
---|
537 | *
|
---|
538 | *
|
---|
539 | * @param enmEffOpSize The effective operand size.
|
---|
540 | */
|
---|
541 | IEM_CIMPL_DEF_1(iemCImpl_pushf, IEMMODE, enmEffOpSize)
|
---|
542 | {
|
---|
543 | VBOXSTRICTRC rcStrict;
|
---|
544 |
|
---|
545 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_PUSHF))
|
---|
546 | {
|
---|
547 | Log2(("pushf: Guest intercept -> #VMEXIT\n"));
|
---|
548 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
549 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_PUSHF, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
550 | }
|
---|
551 |
|
---|
552 | /*
|
---|
553 | * If we're in V8086 mode some care is required (which is why we're in
|
---|
554 | * doing this in a C implementation).
|
---|
555 | */
|
---|
556 | uint32_t fEfl = IEMMISC_GET_EFL(pVCpu);
|
---|
557 | if ( (fEfl & X86_EFL_VM)
|
---|
558 | && X86_EFL_GET_IOPL(fEfl) != 3 )
|
---|
559 | {
|
---|
560 | Assert(pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE);
|
---|
561 | if ( enmEffOpSize != IEMMODE_16BIT
|
---|
562 | || !(pVCpu->cpum.GstCtx.cr4 & X86_CR4_VME))
|
---|
563 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
564 | fEfl &= ~X86_EFL_IF; /* (RF and VM are out of range) */
|
---|
565 | fEfl |= (fEfl & X86_EFL_VIF) >> (19 - 9);
|
---|
566 | rcStrict = iemMemStackPushU16(pVCpu, (uint16_t)fEfl);
|
---|
567 | }
|
---|
568 | else
|
---|
569 | {
|
---|
570 |
|
---|
571 | /*
|
---|
572 | * Ok, clear RF and VM, adjust for ancient CPUs, and push the flags.
|
---|
573 | */
|
---|
574 | fEfl &= ~(X86_EFL_RF | X86_EFL_VM);
|
---|
575 |
|
---|
576 | switch (enmEffOpSize)
|
---|
577 | {
|
---|
578 | case IEMMODE_16BIT:
|
---|
579 | AssertCompile(IEMTARGETCPU_8086 <= IEMTARGETCPU_186 && IEMTARGETCPU_V20 <= IEMTARGETCPU_186 && IEMTARGETCPU_286 > IEMTARGETCPU_186);
|
---|
580 | if (IEM_GET_TARGET_CPU(pVCpu) <= IEMTARGETCPU_186)
|
---|
581 | fEfl |= UINT16_C(0xf000);
|
---|
582 | rcStrict = iemMemStackPushU16(pVCpu, (uint16_t)fEfl);
|
---|
583 | break;
|
---|
584 | case IEMMODE_32BIT:
|
---|
585 | rcStrict = iemMemStackPushU32(pVCpu, fEfl);
|
---|
586 | break;
|
---|
587 | case IEMMODE_64BIT:
|
---|
588 | rcStrict = iemMemStackPushU64(pVCpu, fEfl);
|
---|
589 | break;
|
---|
590 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
591 | }
|
---|
592 | }
|
---|
593 | if (rcStrict != VINF_SUCCESS)
|
---|
594 | return rcStrict;
|
---|
595 |
|
---|
596 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
597 | return VINF_SUCCESS;
|
---|
598 | }
|
---|
599 |
|
---|
600 |
|
---|
601 | /**
|
---|
602 | * Implements popf.
|
---|
603 | *
|
---|
604 | * @param enmEffOpSize The effective operand size.
|
---|
605 | */
|
---|
606 | IEM_CIMPL_DEF_1(iemCImpl_popf, IEMMODE, enmEffOpSize)
|
---|
607 | {
|
---|
608 | uint32_t const fEflOld = IEMMISC_GET_EFL(pVCpu);
|
---|
609 | VBOXSTRICTRC rcStrict;
|
---|
610 | uint32_t fEflNew;
|
---|
611 |
|
---|
612 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_POPF))
|
---|
613 | {
|
---|
614 | Log2(("popf: Guest intercept -> #VMEXIT\n"));
|
---|
615 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
616 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_POPF, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
617 | }
|
---|
618 |
|
---|
619 | /*
|
---|
620 | * V8086 is special as usual.
|
---|
621 | */
|
---|
622 | if (fEflOld & X86_EFL_VM)
|
---|
623 | {
|
---|
624 | /*
|
---|
625 | * Almost anything goes if IOPL is 3.
|
---|
626 | */
|
---|
627 | if (X86_EFL_GET_IOPL(fEflOld) == 3)
|
---|
628 | {
|
---|
629 | switch (enmEffOpSize)
|
---|
630 | {
|
---|
631 | case IEMMODE_16BIT:
|
---|
632 | {
|
---|
633 | uint16_t u16Value;
|
---|
634 | rcStrict = iemMemStackPopU16(pVCpu, &u16Value);
|
---|
635 | if (rcStrict != VINF_SUCCESS)
|
---|
636 | return rcStrict;
|
---|
637 | fEflNew = u16Value | (fEflOld & UINT32_C(0xffff0000));
|
---|
638 | break;
|
---|
639 | }
|
---|
640 | case IEMMODE_32BIT:
|
---|
641 | rcStrict = iemMemStackPopU32(pVCpu, &fEflNew);
|
---|
642 | if (rcStrict != VINF_SUCCESS)
|
---|
643 | return rcStrict;
|
---|
644 | break;
|
---|
645 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
646 | }
|
---|
647 |
|
---|
648 | const uint32_t fPopfBits = pVCpu->CTX_SUFF(pVM)->cpum.ro.GuestFeatures.enmMicroarch != kCpumMicroarch_Intel_80386
|
---|
649 | ? X86_EFL_POPF_BITS : X86_EFL_POPF_BITS_386;
|
---|
650 | fEflNew &= fPopfBits & ~(X86_EFL_IOPL);
|
---|
651 | fEflNew |= ~(fPopfBits & ~(X86_EFL_IOPL)) & fEflOld;
|
---|
652 | }
|
---|
653 | /*
|
---|
654 | * Interrupt flag virtualization with CR4.VME=1.
|
---|
655 | */
|
---|
656 | else if ( enmEffOpSize == IEMMODE_16BIT
|
---|
657 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_VME) )
|
---|
658 | {
|
---|
659 | uint16_t u16Value;
|
---|
660 | RTUINT64U TmpRsp;
|
---|
661 | TmpRsp.u = pVCpu->cpum.GstCtx.rsp;
|
---|
662 | rcStrict = iemMemStackPopU16Ex(pVCpu, &u16Value, &TmpRsp);
|
---|
663 | if (rcStrict != VINF_SUCCESS)
|
---|
664 | return rcStrict;
|
---|
665 |
|
---|
666 | /** @todo Is the popf VME #GP(0) delivered after updating RSP+RIP
|
---|
667 | * or before? */
|
---|
668 | if ( ( (u16Value & X86_EFL_IF)
|
---|
669 | && (fEflOld & X86_EFL_VIP))
|
---|
670 | || (u16Value & X86_EFL_TF) )
|
---|
671 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
672 |
|
---|
673 | fEflNew = u16Value | (fEflOld & UINT32_C(0xffff0000) & ~X86_EFL_VIF);
|
---|
674 | fEflNew |= (fEflNew & X86_EFL_IF) << (19 - 9);
|
---|
675 | fEflNew &= X86_EFL_POPF_BITS & ~(X86_EFL_IOPL | X86_EFL_IF);
|
---|
676 | fEflNew |= ~(X86_EFL_POPF_BITS & ~(X86_EFL_IOPL | X86_EFL_IF)) & fEflOld;
|
---|
677 |
|
---|
678 | pVCpu->cpum.GstCtx.rsp = TmpRsp.u;
|
---|
679 | }
|
---|
680 | else
|
---|
681 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
682 |
|
---|
683 | }
|
---|
684 | /*
|
---|
685 | * Not in V8086 mode.
|
---|
686 | */
|
---|
687 | else
|
---|
688 | {
|
---|
689 | /* Pop the flags. */
|
---|
690 | switch (enmEffOpSize)
|
---|
691 | {
|
---|
692 | case IEMMODE_16BIT:
|
---|
693 | {
|
---|
694 | uint16_t u16Value;
|
---|
695 | rcStrict = iemMemStackPopU16(pVCpu, &u16Value);
|
---|
696 | if (rcStrict != VINF_SUCCESS)
|
---|
697 | return rcStrict;
|
---|
698 | fEflNew = u16Value | (fEflOld & UINT32_C(0xffff0000));
|
---|
699 |
|
---|
700 | /*
|
---|
701 | * Ancient CPU adjustments:
|
---|
702 | * - 8086, 80186, V20/30:
|
---|
703 | * Fixed bits 15:12 bits are not kept correctly internally, mostly for
|
---|
704 | * practical reasons (masking below). We add them when pushing flags.
|
---|
705 | * - 80286:
|
---|
706 | * The NT and IOPL flags cannot be popped from real mode and are
|
---|
707 | * therefore always zero (since a 286 can never exit from PM and
|
---|
708 | * their initial value is zero). This changed on a 386 and can
|
---|
709 | * therefore be used to detect 286 or 386 CPU in real mode.
|
---|
710 | */
|
---|
711 | if ( IEM_GET_TARGET_CPU(pVCpu) == IEMTARGETCPU_286
|
---|
712 | && !(pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE) )
|
---|
713 | fEflNew &= ~(X86_EFL_NT | X86_EFL_IOPL);
|
---|
714 | break;
|
---|
715 | }
|
---|
716 | case IEMMODE_32BIT:
|
---|
717 | rcStrict = iemMemStackPopU32(pVCpu, &fEflNew);
|
---|
718 | if (rcStrict != VINF_SUCCESS)
|
---|
719 | return rcStrict;
|
---|
720 | break;
|
---|
721 | case IEMMODE_64BIT:
|
---|
722 | {
|
---|
723 | uint64_t u64Value;
|
---|
724 | rcStrict = iemMemStackPopU64(pVCpu, &u64Value);
|
---|
725 | if (rcStrict != VINF_SUCCESS)
|
---|
726 | return rcStrict;
|
---|
727 | fEflNew = u64Value; /** @todo testcase: Check exactly what happens if high bits are set. */
|
---|
728 | break;
|
---|
729 | }
|
---|
730 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
731 | }
|
---|
732 |
|
---|
733 | /* Merge them with the current flags. */
|
---|
734 | const uint32_t fPopfBits = pVCpu->CTX_SUFF(pVM)->cpum.ro.GuestFeatures.enmMicroarch != kCpumMicroarch_Intel_80386
|
---|
735 | ? X86_EFL_POPF_BITS : X86_EFL_POPF_BITS_386;
|
---|
736 | if ( (fEflNew & (X86_EFL_IOPL | X86_EFL_IF)) == (fEflOld & (X86_EFL_IOPL | X86_EFL_IF))
|
---|
737 | || pVCpu->iem.s.uCpl == 0)
|
---|
738 | {
|
---|
739 | fEflNew &= fPopfBits;
|
---|
740 | fEflNew |= ~fPopfBits & fEflOld;
|
---|
741 | }
|
---|
742 | else if (pVCpu->iem.s.uCpl <= X86_EFL_GET_IOPL(fEflOld))
|
---|
743 | {
|
---|
744 | fEflNew &= fPopfBits & ~(X86_EFL_IOPL);
|
---|
745 | fEflNew |= ~(fPopfBits & ~(X86_EFL_IOPL)) & fEflOld;
|
---|
746 | }
|
---|
747 | else
|
---|
748 | {
|
---|
749 | fEflNew &= fPopfBits & ~(X86_EFL_IOPL | X86_EFL_IF);
|
---|
750 | fEflNew |= ~(fPopfBits & ~(X86_EFL_IOPL | X86_EFL_IF)) & fEflOld;
|
---|
751 | }
|
---|
752 | }
|
---|
753 |
|
---|
754 | /*
|
---|
755 | * Commit the flags.
|
---|
756 | */
|
---|
757 | Assert(fEflNew & RT_BIT_32(1));
|
---|
758 | IEMMISC_SET_EFL(pVCpu, fEflNew);
|
---|
759 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
760 |
|
---|
761 | return VINF_SUCCESS;
|
---|
762 | }
|
---|
763 |
|
---|
764 |
|
---|
765 | /**
|
---|
766 | * Implements an indirect call.
|
---|
767 | *
|
---|
768 | * @param uNewPC The new program counter (RIP) value (loaded from the
|
---|
769 | * operand).
|
---|
770 | * @param enmEffOpSize The effective operand size.
|
---|
771 | */
|
---|
772 | IEM_CIMPL_DEF_1(iemCImpl_call_16, uint16_t, uNewPC)
|
---|
773 | {
|
---|
774 | uint16_t uOldPC = pVCpu->cpum.GstCtx.ip + cbInstr;
|
---|
775 | if (uNewPC > pVCpu->cpum.GstCtx.cs.u32Limit)
|
---|
776 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
777 |
|
---|
778 | VBOXSTRICTRC rcStrict = iemMemStackPushU16(pVCpu, uOldPC);
|
---|
779 | if (rcStrict != VINF_SUCCESS)
|
---|
780 | return rcStrict;
|
---|
781 |
|
---|
782 | pVCpu->cpum.GstCtx.rip = uNewPC;
|
---|
783 | pVCpu->cpum.GstCtx.eflags.Bits.u1RF = 0;
|
---|
784 |
|
---|
785 | #ifndef IEM_WITH_CODE_TLB
|
---|
786 | /* Flush the prefetch buffer. */
|
---|
787 | pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
|
---|
788 | #endif
|
---|
789 | return VINF_SUCCESS;
|
---|
790 | }
|
---|
791 |
|
---|
792 |
|
---|
793 | /**
|
---|
794 | * Implements a 16-bit relative call.
|
---|
795 | *
|
---|
796 | * @param offDisp The displacment offset.
|
---|
797 | */
|
---|
798 | IEM_CIMPL_DEF_1(iemCImpl_call_rel_16, int16_t, offDisp)
|
---|
799 | {
|
---|
800 | uint16_t uOldPC = pVCpu->cpum.GstCtx.ip + cbInstr;
|
---|
801 | uint16_t uNewPC = uOldPC + offDisp;
|
---|
802 | if (uNewPC > pVCpu->cpum.GstCtx.cs.u32Limit)
|
---|
803 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
804 |
|
---|
805 | VBOXSTRICTRC rcStrict = iemMemStackPushU16(pVCpu, uOldPC);
|
---|
806 | if (rcStrict != VINF_SUCCESS)
|
---|
807 | return rcStrict;
|
---|
808 |
|
---|
809 | pVCpu->cpum.GstCtx.rip = uNewPC;
|
---|
810 | pVCpu->cpum.GstCtx.eflags.Bits.u1RF = 0;
|
---|
811 |
|
---|
812 | #ifndef IEM_WITH_CODE_TLB
|
---|
813 | /* Flush the prefetch buffer. */
|
---|
814 | pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
|
---|
815 | #endif
|
---|
816 | return VINF_SUCCESS;
|
---|
817 | }
|
---|
818 |
|
---|
819 |
|
---|
820 | /**
|
---|
821 | * Implements a 32-bit indirect call.
|
---|
822 | *
|
---|
823 | * @param uNewPC The new program counter (RIP) value (loaded from the
|
---|
824 | * operand).
|
---|
825 | * @param enmEffOpSize The effective operand size.
|
---|
826 | */
|
---|
827 | IEM_CIMPL_DEF_1(iemCImpl_call_32, uint32_t, uNewPC)
|
---|
828 | {
|
---|
829 | uint32_t uOldPC = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
830 | if (uNewPC > pVCpu->cpum.GstCtx.cs.u32Limit)
|
---|
831 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
832 |
|
---|
833 | VBOXSTRICTRC rcStrict = iemMemStackPushU32(pVCpu, uOldPC);
|
---|
834 | if (rcStrict != VINF_SUCCESS)
|
---|
835 | return rcStrict;
|
---|
836 |
|
---|
837 | pVCpu->cpum.GstCtx.rip = uNewPC;
|
---|
838 | pVCpu->cpum.GstCtx.eflags.Bits.u1RF = 0;
|
---|
839 |
|
---|
840 | #ifndef IEM_WITH_CODE_TLB
|
---|
841 | /* Flush the prefetch buffer. */
|
---|
842 | pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
|
---|
843 | #endif
|
---|
844 | return VINF_SUCCESS;
|
---|
845 | }
|
---|
846 |
|
---|
847 |
|
---|
848 | /**
|
---|
849 | * Implements a 32-bit relative call.
|
---|
850 | *
|
---|
851 | * @param offDisp The displacment offset.
|
---|
852 | */
|
---|
853 | IEM_CIMPL_DEF_1(iemCImpl_call_rel_32, int32_t, offDisp)
|
---|
854 | {
|
---|
855 | uint32_t uOldPC = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
856 | uint32_t uNewPC = uOldPC + offDisp;
|
---|
857 | if (uNewPC > pVCpu->cpum.GstCtx.cs.u32Limit)
|
---|
858 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
859 |
|
---|
860 | VBOXSTRICTRC rcStrict = iemMemStackPushU32(pVCpu, uOldPC);
|
---|
861 | if (rcStrict != VINF_SUCCESS)
|
---|
862 | return rcStrict;
|
---|
863 |
|
---|
864 | pVCpu->cpum.GstCtx.rip = uNewPC;
|
---|
865 | pVCpu->cpum.GstCtx.eflags.Bits.u1RF = 0;
|
---|
866 |
|
---|
867 | #ifndef IEM_WITH_CODE_TLB
|
---|
868 | /* Flush the prefetch buffer. */
|
---|
869 | pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
|
---|
870 | #endif
|
---|
871 | return VINF_SUCCESS;
|
---|
872 | }
|
---|
873 |
|
---|
874 |
|
---|
875 | /**
|
---|
876 | * Implements a 64-bit indirect call.
|
---|
877 | *
|
---|
878 | * @param uNewPC The new program counter (RIP) value (loaded from the
|
---|
879 | * operand).
|
---|
880 | * @param enmEffOpSize The effective operand size.
|
---|
881 | */
|
---|
882 | IEM_CIMPL_DEF_1(iemCImpl_call_64, uint64_t, uNewPC)
|
---|
883 | {
|
---|
884 | uint64_t uOldPC = pVCpu->cpum.GstCtx.rip + cbInstr;
|
---|
885 | if (!IEM_IS_CANONICAL(uNewPC))
|
---|
886 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
887 |
|
---|
888 | VBOXSTRICTRC rcStrict = iemMemStackPushU64(pVCpu, uOldPC);
|
---|
889 | if (rcStrict != VINF_SUCCESS)
|
---|
890 | return rcStrict;
|
---|
891 |
|
---|
892 | pVCpu->cpum.GstCtx.rip = uNewPC;
|
---|
893 | pVCpu->cpum.GstCtx.eflags.Bits.u1RF = 0;
|
---|
894 |
|
---|
895 | #ifndef IEM_WITH_CODE_TLB
|
---|
896 | /* Flush the prefetch buffer. */
|
---|
897 | pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
|
---|
898 | #endif
|
---|
899 | return VINF_SUCCESS;
|
---|
900 | }
|
---|
901 |
|
---|
902 |
|
---|
903 | /**
|
---|
904 | * Implements a 64-bit relative call.
|
---|
905 | *
|
---|
906 | * @param offDisp The displacment offset.
|
---|
907 | */
|
---|
908 | IEM_CIMPL_DEF_1(iemCImpl_call_rel_64, int64_t, offDisp)
|
---|
909 | {
|
---|
910 | uint64_t uOldPC = pVCpu->cpum.GstCtx.rip + cbInstr;
|
---|
911 | uint64_t uNewPC = uOldPC + offDisp;
|
---|
912 | if (!IEM_IS_CANONICAL(uNewPC))
|
---|
913 | return iemRaiseNotCanonical(pVCpu);
|
---|
914 |
|
---|
915 | VBOXSTRICTRC rcStrict = iemMemStackPushU64(pVCpu, uOldPC);
|
---|
916 | if (rcStrict != VINF_SUCCESS)
|
---|
917 | return rcStrict;
|
---|
918 |
|
---|
919 | pVCpu->cpum.GstCtx.rip = uNewPC;
|
---|
920 | pVCpu->cpum.GstCtx.eflags.Bits.u1RF = 0;
|
---|
921 |
|
---|
922 | #ifndef IEM_WITH_CODE_TLB
|
---|
923 | /* Flush the prefetch buffer. */
|
---|
924 | pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
|
---|
925 | #endif
|
---|
926 |
|
---|
927 | return VINF_SUCCESS;
|
---|
928 | }
|
---|
929 |
|
---|
930 |
|
---|
931 | /**
|
---|
932 | * Implements far jumps and calls thru task segments (TSS).
|
---|
933 | *
|
---|
934 | * @param uSel The selector.
|
---|
935 | * @param enmBranch The kind of branching we're performing.
|
---|
936 | * @param enmEffOpSize The effective operand size.
|
---|
937 | * @param pDesc The descriptor corresponding to @a uSel. The type is
|
---|
938 | * task gate.
|
---|
939 | */
|
---|
940 | IEM_CIMPL_DEF_4(iemCImpl_BranchTaskSegment, uint16_t, uSel, IEMBRANCH, enmBranch, IEMMODE, enmEffOpSize, PIEMSELDESC, pDesc)
|
---|
941 | {
|
---|
942 | #ifndef IEM_IMPLEMENTS_TASKSWITCH
|
---|
943 | IEM_RETURN_ASPECT_NOT_IMPLEMENTED();
|
---|
944 | #else
|
---|
945 | Assert(enmBranch == IEMBRANCH_JUMP || enmBranch == IEMBRANCH_CALL);
|
---|
946 | Assert( pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_286_TSS_AVAIL
|
---|
947 | || pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_386_TSS_AVAIL);
|
---|
948 | RT_NOREF_PV(enmEffOpSize);
|
---|
949 | IEM_CTX_ASSERT(pVCpu, IEM_CPUMCTX_EXTRN_XCPT_MASK);
|
---|
950 |
|
---|
951 | if ( pDesc->Legacy.Gate.u2Dpl < pVCpu->iem.s.uCpl
|
---|
952 | || pDesc->Legacy.Gate.u2Dpl < (uSel & X86_SEL_RPL))
|
---|
953 | {
|
---|
954 | Log(("BranchTaskSegment invalid priv. uSel=%04x TSS DPL=%d CPL=%u Sel RPL=%u -> #GP\n", uSel, pDesc->Legacy.Gate.u2Dpl,
|
---|
955 | pVCpu->iem.s.uCpl, (uSel & X86_SEL_RPL)));
|
---|
956 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel & X86_SEL_MASK_OFF_RPL);
|
---|
957 | }
|
---|
958 |
|
---|
959 | /** @todo This is checked earlier for far jumps (see iemCImpl_FarJmp) but not
|
---|
960 | * far calls (see iemCImpl_callf). Most likely in both cases it should be
|
---|
961 | * checked here, need testcases. */
|
---|
962 | if (!pDesc->Legacy.Gen.u1Present)
|
---|
963 | {
|
---|
964 | Log(("BranchTaskSegment TSS not present uSel=%04x -> #NP\n", uSel));
|
---|
965 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uSel & X86_SEL_MASK_OFF_RPL);
|
---|
966 | }
|
---|
967 |
|
---|
968 | uint32_t uNextEip = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
969 | return iemTaskSwitch(pVCpu, enmBranch == IEMBRANCH_JUMP ? IEMTASKSWITCH_JUMP : IEMTASKSWITCH_CALL,
|
---|
970 | uNextEip, 0 /* fFlags */, 0 /* uErr */, 0 /* uCr2 */, uSel, pDesc);
|
---|
971 | #endif
|
---|
972 | }
|
---|
973 |
|
---|
974 |
|
---|
975 | /**
|
---|
976 | * Implements far jumps and calls thru task gates.
|
---|
977 | *
|
---|
978 | * @param uSel The selector.
|
---|
979 | * @param enmBranch The kind of branching we're performing.
|
---|
980 | * @param enmEffOpSize The effective operand size.
|
---|
981 | * @param pDesc The descriptor corresponding to @a uSel. The type is
|
---|
982 | * task gate.
|
---|
983 | */
|
---|
984 | IEM_CIMPL_DEF_4(iemCImpl_BranchTaskGate, uint16_t, uSel, IEMBRANCH, enmBranch, IEMMODE, enmEffOpSize, PIEMSELDESC, pDesc)
|
---|
985 | {
|
---|
986 | #ifndef IEM_IMPLEMENTS_TASKSWITCH
|
---|
987 | IEM_RETURN_ASPECT_NOT_IMPLEMENTED();
|
---|
988 | #else
|
---|
989 | Assert(enmBranch == IEMBRANCH_JUMP || enmBranch == IEMBRANCH_CALL);
|
---|
990 | RT_NOREF_PV(enmEffOpSize);
|
---|
991 | IEM_CTX_ASSERT(pVCpu, IEM_CPUMCTX_EXTRN_XCPT_MASK);
|
---|
992 |
|
---|
993 | if ( pDesc->Legacy.Gate.u2Dpl < pVCpu->iem.s.uCpl
|
---|
994 | || pDesc->Legacy.Gate.u2Dpl < (uSel & X86_SEL_RPL))
|
---|
995 | {
|
---|
996 | Log(("BranchTaskGate invalid priv. uSel=%04x TSS DPL=%d CPL=%u Sel RPL=%u -> #GP\n", uSel, pDesc->Legacy.Gate.u2Dpl,
|
---|
997 | pVCpu->iem.s.uCpl, (uSel & X86_SEL_RPL)));
|
---|
998 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel & X86_SEL_MASK_OFF_RPL);
|
---|
999 | }
|
---|
1000 |
|
---|
1001 | /** @todo This is checked earlier for far jumps (see iemCImpl_FarJmp) but not
|
---|
1002 | * far calls (see iemCImpl_callf). Most likely in both cases it should be
|
---|
1003 | * checked here, need testcases. */
|
---|
1004 | if (!pDesc->Legacy.Gen.u1Present)
|
---|
1005 | {
|
---|
1006 | Log(("BranchTaskSegment segment not present uSel=%04x -> #NP\n", uSel));
|
---|
1007 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uSel & X86_SEL_MASK_OFF_RPL);
|
---|
1008 | }
|
---|
1009 |
|
---|
1010 | /*
|
---|
1011 | * Fetch the new TSS descriptor from the GDT.
|
---|
1012 | */
|
---|
1013 | RTSEL uSelTss = pDesc->Legacy.Gate.u16Sel;
|
---|
1014 | if (uSelTss & X86_SEL_LDT)
|
---|
1015 | {
|
---|
1016 | Log(("BranchTaskGate TSS is in LDT. uSel=%04x uSelTss=%04x -> #GP\n", uSel, uSelTss));
|
---|
1017 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel & X86_SEL_MASK_OFF_RPL);
|
---|
1018 | }
|
---|
1019 |
|
---|
1020 | IEMSELDESC TssDesc;
|
---|
1021 | VBOXSTRICTRC rcStrict = iemMemFetchSelDesc(pVCpu, &TssDesc, uSelTss, X86_XCPT_GP);
|
---|
1022 | if (rcStrict != VINF_SUCCESS)
|
---|
1023 | return rcStrict;
|
---|
1024 |
|
---|
1025 | if (TssDesc.Legacy.Gate.u4Type & X86_SEL_TYPE_SYS_TSS_BUSY_MASK)
|
---|
1026 | {
|
---|
1027 | Log(("BranchTaskGate TSS is busy. uSel=%04x uSelTss=%04x DescType=%#x -> #GP\n", uSel, uSelTss,
|
---|
1028 | TssDesc.Legacy.Gate.u4Type));
|
---|
1029 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel & X86_SEL_MASK_OFF_RPL);
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 | if (!TssDesc.Legacy.Gate.u1Present)
|
---|
1033 | {
|
---|
1034 | Log(("BranchTaskGate TSS is not present. uSel=%04x uSelTss=%04x -> #NP\n", uSel, uSelTss));
|
---|
1035 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uSelTss & X86_SEL_MASK_OFF_RPL);
|
---|
1036 | }
|
---|
1037 |
|
---|
1038 | uint32_t uNextEip = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
1039 | return iemTaskSwitch(pVCpu, enmBranch == IEMBRANCH_JUMP ? IEMTASKSWITCH_JUMP : IEMTASKSWITCH_CALL,
|
---|
1040 | uNextEip, 0 /* fFlags */, 0 /* uErr */, 0 /* uCr2 */, uSelTss, &TssDesc);
|
---|
1041 | #endif
|
---|
1042 | }
|
---|
1043 |
|
---|
1044 |
|
---|
1045 | /**
|
---|
1046 | * Implements far jumps and calls thru call gates.
|
---|
1047 | *
|
---|
1048 | * @param uSel The selector.
|
---|
1049 | * @param enmBranch The kind of branching we're performing.
|
---|
1050 | * @param enmEffOpSize The effective operand size.
|
---|
1051 | * @param pDesc The descriptor corresponding to @a uSel. The type is
|
---|
1052 | * call gate.
|
---|
1053 | */
|
---|
1054 | IEM_CIMPL_DEF_4(iemCImpl_BranchCallGate, uint16_t, uSel, IEMBRANCH, enmBranch, IEMMODE, enmEffOpSize, PIEMSELDESC, pDesc)
|
---|
1055 | {
|
---|
1056 | #define IEM_IMPLEMENTS_CALLGATE
|
---|
1057 | #ifndef IEM_IMPLEMENTS_CALLGATE
|
---|
1058 | IEM_RETURN_ASPECT_NOT_IMPLEMENTED();
|
---|
1059 | #else
|
---|
1060 | RT_NOREF_PV(enmEffOpSize);
|
---|
1061 | IEM_CTX_ASSERT(pVCpu, IEM_CPUMCTX_EXTRN_XCPT_MASK);
|
---|
1062 |
|
---|
1063 | /* NB: Far jumps can only do intra-privilege transfers. Far calls support
|
---|
1064 | * inter-privilege calls and are much more complex.
|
---|
1065 | *
|
---|
1066 | * NB: 64-bit call gate has the same type as a 32-bit call gate! If
|
---|
1067 | * EFER.LMA=1, the gate must be 64-bit. Conversely if EFER.LMA=0, the gate
|
---|
1068 | * must be 16-bit or 32-bit.
|
---|
1069 | */
|
---|
1070 | /** @todo: effective operand size is probably irrelevant here, only the
|
---|
1071 | * call gate bitness matters??
|
---|
1072 | */
|
---|
1073 | VBOXSTRICTRC rcStrict;
|
---|
1074 | RTPTRUNION uPtrRet;
|
---|
1075 | uint64_t uNewRsp;
|
---|
1076 | uint64_t uNewRip;
|
---|
1077 | uint64_t u64Base;
|
---|
1078 | uint32_t cbLimit;
|
---|
1079 | RTSEL uNewCS;
|
---|
1080 | IEMSELDESC DescCS;
|
---|
1081 |
|
---|
1082 | AssertCompile(X86_SEL_TYPE_SYS_386_CALL_GATE == AMD64_SEL_TYPE_SYS_CALL_GATE);
|
---|
1083 | Assert(enmBranch == IEMBRANCH_JUMP || enmBranch == IEMBRANCH_CALL);
|
---|
1084 | Assert( pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_286_CALL_GATE
|
---|
1085 | || pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_386_CALL_GATE);
|
---|
1086 |
|
---|
1087 | /* Determine the new instruction pointer from the gate descriptor. */
|
---|
1088 | uNewRip = pDesc->Legacy.Gate.u16OffsetLow
|
---|
1089 | | ((uint32_t)pDesc->Legacy.Gate.u16OffsetHigh << 16)
|
---|
1090 | | ((uint64_t)pDesc->Long.Gate.u32OffsetTop << 32);
|
---|
1091 |
|
---|
1092 | /* Perform DPL checks on the gate descriptor. */
|
---|
1093 | if ( pDesc->Legacy.Gate.u2Dpl < pVCpu->iem.s.uCpl
|
---|
1094 | || pDesc->Legacy.Gate.u2Dpl < (uSel & X86_SEL_RPL))
|
---|
1095 | {
|
---|
1096 | Log(("BranchCallGate invalid priv. uSel=%04x Gate DPL=%d CPL=%u Sel RPL=%u -> #GP\n", uSel, pDesc->Legacy.Gate.u2Dpl,
|
---|
1097 | pVCpu->iem.s.uCpl, (uSel & X86_SEL_RPL)));
|
---|
1098 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 | /** @todo does this catch NULL selectors, too? */
|
---|
1102 | if (!pDesc->Legacy.Gen.u1Present)
|
---|
1103 | {
|
---|
1104 | Log(("BranchCallGate Gate not present uSel=%04x -> #NP\n", uSel));
|
---|
1105 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uSel);
|
---|
1106 | }
|
---|
1107 |
|
---|
1108 | /*
|
---|
1109 | * Fetch the target CS descriptor from the GDT or LDT.
|
---|
1110 | */
|
---|
1111 | uNewCS = pDesc->Legacy.Gate.u16Sel;
|
---|
1112 | rcStrict = iemMemFetchSelDesc(pVCpu, &DescCS, uNewCS, X86_XCPT_GP);
|
---|
1113 | if (rcStrict != VINF_SUCCESS)
|
---|
1114 | return rcStrict;
|
---|
1115 |
|
---|
1116 | /* Target CS must be a code selector. */
|
---|
1117 | if ( !DescCS.Legacy.Gen.u1DescType
|
---|
1118 | || !(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE) )
|
---|
1119 | {
|
---|
1120 | Log(("BranchCallGate %04x:%08RX64 -> not a code selector (u1DescType=%u u4Type=%#x).\n",
|
---|
1121 | uNewCS, uNewRip, DescCS.Legacy.Gen.u1DescType, DescCS.Legacy.Gen.u4Type));
|
---|
1122 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCS);
|
---|
1123 | }
|
---|
1124 |
|
---|
1125 | /* Privilege checks on target CS. */
|
---|
1126 | if (enmBranch == IEMBRANCH_JUMP)
|
---|
1127 | {
|
---|
1128 | if (DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF)
|
---|
1129 | {
|
---|
1130 | if (DescCS.Legacy.Gen.u2Dpl > pVCpu->iem.s.uCpl)
|
---|
1131 | {
|
---|
1132 | Log(("BranchCallGate jump (conforming) bad DPL uNewCS=%04x Gate DPL=%d CPL=%u -> #GP\n",
|
---|
1133 | uNewCS, DescCS.Legacy.Gen.u2Dpl, pVCpu->iem.s.uCpl));
|
---|
1134 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCS);
|
---|
1135 | }
|
---|
1136 | }
|
---|
1137 | else
|
---|
1138 | {
|
---|
1139 | if (DescCS.Legacy.Gen.u2Dpl != pVCpu->iem.s.uCpl)
|
---|
1140 | {
|
---|
1141 | Log(("BranchCallGate jump (non-conforming) bad DPL uNewCS=%04x Gate DPL=%d CPL=%u -> #GP\n",
|
---|
1142 | uNewCS, DescCS.Legacy.Gen.u2Dpl, pVCpu->iem.s.uCpl));
|
---|
1143 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCS);
|
---|
1144 | }
|
---|
1145 | }
|
---|
1146 | }
|
---|
1147 | else
|
---|
1148 | {
|
---|
1149 | Assert(enmBranch == IEMBRANCH_CALL);
|
---|
1150 | if (DescCS.Legacy.Gen.u2Dpl > pVCpu->iem.s.uCpl)
|
---|
1151 | {
|
---|
1152 | Log(("BranchCallGate call invalid priv. uNewCS=%04x Gate DPL=%d CPL=%u -> #GP\n",
|
---|
1153 | uNewCS, DescCS.Legacy.Gen.u2Dpl, pVCpu->iem.s.uCpl));
|
---|
1154 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCS & X86_SEL_MASK_OFF_RPL);
|
---|
1155 | }
|
---|
1156 | }
|
---|
1157 |
|
---|
1158 | /* Additional long mode checks. */
|
---|
1159 | if (IEM_IS_LONG_MODE(pVCpu))
|
---|
1160 | {
|
---|
1161 | if (!DescCS.Legacy.Gen.u1Long)
|
---|
1162 | {
|
---|
1163 | Log(("BranchCallGate uNewCS %04x -> not a 64-bit code segment.\n", uNewCS));
|
---|
1164 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCS);
|
---|
1165 | }
|
---|
1166 |
|
---|
1167 | /* L vs D. */
|
---|
1168 | if ( DescCS.Legacy.Gen.u1Long
|
---|
1169 | && DescCS.Legacy.Gen.u1DefBig)
|
---|
1170 | {
|
---|
1171 | Log(("BranchCallGate uNewCS %04x -> both L and D are set.\n", uNewCS));
|
---|
1172 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCS);
|
---|
1173 | }
|
---|
1174 | }
|
---|
1175 |
|
---|
1176 | if (!DescCS.Legacy.Gate.u1Present)
|
---|
1177 | {
|
---|
1178 | Log(("BranchCallGate target CS is not present. uSel=%04x uNewCS=%04x -> #NP(CS)\n", uSel, uNewCS));
|
---|
1179 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uNewCS);
|
---|
1180 | }
|
---|
1181 |
|
---|
1182 | if (enmBranch == IEMBRANCH_JUMP)
|
---|
1183 | {
|
---|
1184 | /** @todo: This is very similar to regular far jumps; merge! */
|
---|
1185 | /* Jumps are fairly simple... */
|
---|
1186 |
|
---|
1187 | /* Chop the high bits off if 16-bit gate (Intel says so). */
|
---|
1188 | if (pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_286_CALL_GATE)
|
---|
1189 | uNewRip = (uint16_t)uNewRip;
|
---|
1190 |
|
---|
1191 | /* Limit check for non-long segments. */
|
---|
1192 | cbLimit = X86DESC_LIMIT_G(&DescCS.Legacy);
|
---|
1193 | if (DescCS.Legacy.Gen.u1Long)
|
---|
1194 | u64Base = 0;
|
---|
1195 | else
|
---|
1196 | {
|
---|
1197 | if (uNewRip > cbLimit)
|
---|
1198 | {
|
---|
1199 | Log(("BranchCallGate jump %04x:%08RX64 -> out of bounds (%#x) -> #GP(0)\n", uNewCS, uNewRip, cbLimit));
|
---|
1200 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, 0);
|
---|
1201 | }
|
---|
1202 | u64Base = X86DESC_BASE(&DescCS.Legacy);
|
---|
1203 | }
|
---|
1204 |
|
---|
1205 | /* Canonical address check. */
|
---|
1206 | if (!IEM_IS_CANONICAL(uNewRip))
|
---|
1207 | {
|
---|
1208 | Log(("BranchCallGate jump %04x:%016RX64 - not canonical -> #GP\n", uNewCS, uNewRip));
|
---|
1209 | return iemRaiseNotCanonical(pVCpu);
|
---|
1210 | }
|
---|
1211 |
|
---|
1212 | /*
|
---|
1213 | * Ok, everything checked out fine. Now set the accessed bit before
|
---|
1214 | * committing the result into CS, CSHID and RIP.
|
---|
1215 | */
|
---|
1216 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
1217 | {
|
---|
1218 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewCS);
|
---|
1219 | if (rcStrict != VINF_SUCCESS)
|
---|
1220 | return rcStrict;
|
---|
1221 | /** @todo check what VT-x and AMD-V does. */
|
---|
1222 | DescCS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
1223 | }
|
---|
1224 |
|
---|
1225 | /* commit */
|
---|
1226 | pVCpu->cpum.GstCtx.rip = uNewRip;
|
---|
1227 | pVCpu->cpum.GstCtx.cs.Sel = uNewCS & X86_SEL_MASK_OFF_RPL;
|
---|
1228 | pVCpu->cpum.GstCtx.cs.Sel |= pVCpu->iem.s.uCpl; /** @todo is this right for conforming segs? or in general? */
|
---|
1229 | pVCpu->cpum.GstCtx.cs.ValidSel = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
1230 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
1231 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCS.Legacy);
|
---|
1232 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimit;
|
---|
1233 | pVCpu->cpum.GstCtx.cs.u64Base = u64Base;
|
---|
1234 | pVCpu->iem.s.enmCpuMode = iemCalcCpuMode(pVCpu);
|
---|
1235 | }
|
---|
1236 | else
|
---|
1237 | {
|
---|
1238 | Assert(enmBranch == IEMBRANCH_CALL);
|
---|
1239 | /* Calls are much more complicated. */
|
---|
1240 |
|
---|
1241 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF) && (DescCS.Legacy.Gen.u2Dpl < pVCpu->iem.s.uCpl))
|
---|
1242 | {
|
---|
1243 | uint16_t offNewStack; /* Offset of new stack in TSS. */
|
---|
1244 | uint16_t cbNewStack; /* Number of bytes the stack information takes up in TSS. */
|
---|
1245 | uint8_t uNewCSDpl;
|
---|
1246 | uint8_t cbWords;
|
---|
1247 | RTSEL uNewSS;
|
---|
1248 | RTSEL uOldSS;
|
---|
1249 | uint64_t uOldRsp;
|
---|
1250 | IEMSELDESC DescSS;
|
---|
1251 | RTPTRUNION uPtrTSS;
|
---|
1252 | RTGCPTR GCPtrTSS;
|
---|
1253 | RTPTRUNION uPtrParmWds;
|
---|
1254 | RTGCPTR GCPtrParmWds;
|
---|
1255 |
|
---|
1256 | /* More privilege. This is the fun part. */
|
---|
1257 | Assert(!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF)); /* Filtered out above. */
|
---|
1258 |
|
---|
1259 | /*
|
---|
1260 | * Determine new SS:rSP from the TSS.
|
---|
1261 | */
|
---|
1262 | Assert(!pVCpu->cpum.GstCtx.tr.Attr.n.u1DescType);
|
---|
1263 |
|
---|
1264 | /* Figure out where the new stack pointer is stored in the TSS. */
|
---|
1265 | uNewCSDpl = DescCS.Legacy.Gen.u2Dpl;
|
---|
1266 | if (!IEM_IS_LONG_MODE(pVCpu))
|
---|
1267 | {
|
---|
1268 | if (pVCpu->cpum.GstCtx.tr.Attr.n.u4Type == X86_SEL_TYPE_SYS_386_TSS_BUSY)
|
---|
1269 | {
|
---|
1270 | offNewStack = RT_UOFFSETOF(X86TSS32, esp0) + uNewCSDpl * 8;
|
---|
1271 | cbNewStack = RT_SIZEOFMEMB(X86TSS32, esp0) + RT_SIZEOFMEMB(X86TSS32, ss0);
|
---|
1272 | }
|
---|
1273 | else
|
---|
1274 | {
|
---|
1275 | Assert(pVCpu->cpum.GstCtx.tr.Attr.n.u4Type == X86_SEL_TYPE_SYS_286_TSS_BUSY);
|
---|
1276 | offNewStack = RT_UOFFSETOF(X86TSS16, sp0) + uNewCSDpl * 4;
|
---|
1277 | cbNewStack = RT_SIZEOFMEMB(X86TSS16, sp0) + RT_SIZEOFMEMB(X86TSS16, ss0);
|
---|
1278 | }
|
---|
1279 | }
|
---|
1280 | else
|
---|
1281 | {
|
---|
1282 | Assert(pVCpu->cpum.GstCtx.tr.Attr.n.u4Type == AMD64_SEL_TYPE_SYS_TSS_BUSY);
|
---|
1283 | offNewStack = RT_UOFFSETOF(X86TSS64, rsp0) + uNewCSDpl * RT_SIZEOFMEMB(X86TSS64, rsp0);
|
---|
1284 | cbNewStack = RT_SIZEOFMEMB(X86TSS64, rsp0);
|
---|
1285 | }
|
---|
1286 |
|
---|
1287 | /* Check against TSS limit. */
|
---|
1288 | if ((uint16_t)(offNewStack + cbNewStack - 1) > pVCpu->cpum.GstCtx.tr.u32Limit)
|
---|
1289 | {
|
---|
1290 | Log(("BranchCallGate inner stack past TSS limit - %u > %u -> #TS(TSS)\n", offNewStack + cbNewStack - 1, pVCpu->cpum.GstCtx.tr.u32Limit));
|
---|
1291 | return iemRaiseTaskSwitchFaultBySelector(pVCpu, pVCpu->cpum.GstCtx.tr.Sel);
|
---|
1292 | }
|
---|
1293 |
|
---|
1294 | GCPtrTSS = pVCpu->cpum.GstCtx.tr.u64Base + offNewStack;
|
---|
1295 | rcStrict = iemMemMap(pVCpu, &uPtrTSS.pv, cbNewStack, UINT8_MAX, GCPtrTSS, IEM_ACCESS_SYS_R);
|
---|
1296 | if (rcStrict != VINF_SUCCESS)
|
---|
1297 | {
|
---|
1298 | Log(("BranchCallGate: TSS mapping failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1299 | return rcStrict;
|
---|
1300 | }
|
---|
1301 |
|
---|
1302 | if (!IEM_IS_LONG_MODE(pVCpu))
|
---|
1303 | {
|
---|
1304 | if (pVCpu->cpum.GstCtx.tr.Attr.n.u4Type == X86_SEL_TYPE_SYS_386_TSS_BUSY)
|
---|
1305 | {
|
---|
1306 | uNewRsp = uPtrTSS.pu32[0];
|
---|
1307 | uNewSS = uPtrTSS.pu16[2];
|
---|
1308 | }
|
---|
1309 | else
|
---|
1310 | {
|
---|
1311 | Assert(pVCpu->cpum.GstCtx.tr.Attr.n.u4Type == X86_SEL_TYPE_SYS_286_TSS_BUSY);
|
---|
1312 | uNewRsp = uPtrTSS.pu16[0];
|
---|
1313 | uNewSS = uPtrTSS.pu16[1];
|
---|
1314 | }
|
---|
1315 | }
|
---|
1316 | else
|
---|
1317 | {
|
---|
1318 | Assert(pVCpu->cpum.GstCtx.tr.Attr.n.u4Type == AMD64_SEL_TYPE_SYS_TSS_BUSY);
|
---|
1319 | /* SS will be a NULL selector, but that's valid. */
|
---|
1320 | uNewRsp = uPtrTSS.pu64[0];
|
---|
1321 | uNewSS = uNewCSDpl;
|
---|
1322 | }
|
---|
1323 |
|
---|
1324 | /* Done with the TSS now. */
|
---|
1325 | rcStrict = iemMemCommitAndUnmap(pVCpu, uPtrTSS.pv, IEM_ACCESS_SYS_R);
|
---|
1326 | if (rcStrict != VINF_SUCCESS)
|
---|
1327 | {
|
---|
1328 | Log(("BranchCallGate: TSS unmapping failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1329 | return rcStrict;
|
---|
1330 | }
|
---|
1331 |
|
---|
1332 | /* Only used outside of long mode. */
|
---|
1333 | cbWords = pDesc->Legacy.Gate.u5ParmCount;
|
---|
1334 |
|
---|
1335 | /* If EFER.LMA is 0, there's extra work to do. */
|
---|
1336 | if (!IEM_IS_LONG_MODE(pVCpu))
|
---|
1337 | {
|
---|
1338 | if ((uNewSS & X86_SEL_MASK_OFF_RPL) == 0)
|
---|
1339 | {
|
---|
1340 | Log(("BranchCallGate new SS NULL -> #TS(NewSS)\n"));
|
---|
1341 | return iemRaiseTaskSwitchFaultBySelector(pVCpu, uNewSS);
|
---|
1342 | }
|
---|
1343 |
|
---|
1344 | /* Grab the new SS descriptor. */
|
---|
1345 | rcStrict = iemMemFetchSelDesc(pVCpu, &DescSS, uNewSS, X86_XCPT_SS);
|
---|
1346 | if (rcStrict != VINF_SUCCESS)
|
---|
1347 | return rcStrict;
|
---|
1348 |
|
---|
1349 | /* Ensure that CS.DPL == SS.RPL == SS.DPL. */
|
---|
1350 | if ( (DescCS.Legacy.Gen.u2Dpl != (uNewSS & X86_SEL_RPL))
|
---|
1351 | || (DescCS.Legacy.Gen.u2Dpl != DescSS.Legacy.Gen.u2Dpl))
|
---|
1352 | {
|
---|
1353 | Log(("BranchCallGate call bad RPL/DPL uNewSS=%04x SS DPL=%d CS DPL=%u -> #TS(NewSS)\n",
|
---|
1354 | uNewSS, DescCS.Legacy.Gen.u2Dpl, DescCS.Legacy.Gen.u2Dpl));
|
---|
1355 | return iemRaiseTaskSwitchFaultBySelector(pVCpu, uNewSS);
|
---|
1356 | }
|
---|
1357 |
|
---|
1358 | /* Ensure new SS is a writable data segment. */
|
---|
1359 | if ((DescSS.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_WRITE)) != X86_SEL_TYPE_WRITE)
|
---|
1360 | {
|
---|
1361 | Log(("BranchCallGate call new SS -> not a writable data selector (u4Type=%#x)\n", DescSS.Legacy.Gen.u4Type));
|
---|
1362 | return iemRaiseTaskSwitchFaultBySelector(pVCpu, uNewSS);
|
---|
1363 | }
|
---|
1364 |
|
---|
1365 | if (!DescSS.Legacy.Gen.u1Present)
|
---|
1366 | {
|
---|
1367 | Log(("BranchCallGate New stack not present uSel=%04x -> #SS(NewSS)\n", uNewSS));
|
---|
1368 | return iemRaiseStackSelectorNotPresentBySelector(pVCpu, uNewSS);
|
---|
1369 | }
|
---|
1370 | if (pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_386_CALL_GATE)
|
---|
1371 | cbNewStack = (uint16_t)sizeof(uint32_t) * (4 + cbWords);
|
---|
1372 | else
|
---|
1373 | cbNewStack = (uint16_t)sizeof(uint16_t) * (4 + cbWords);
|
---|
1374 | }
|
---|
1375 | else
|
---|
1376 | {
|
---|
1377 | /* Just grab the new (NULL) SS descriptor. */
|
---|
1378 | /** @todo testcase: Check whether the zero GDT entry is actually loaded here
|
---|
1379 | * like we do... */
|
---|
1380 | rcStrict = iemMemFetchSelDesc(pVCpu, &DescSS, uNewSS, X86_XCPT_SS);
|
---|
1381 | if (rcStrict != VINF_SUCCESS)
|
---|
1382 | return rcStrict;
|
---|
1383 |
|
---|
1384 | cbNewStack = sizeof(uint64_t) * 4;
|
---|
1385 | }
|
---|
1386 |
|
---|
1387 | /** @todo: According to Intel, new stack is checked for enough space first,
|
---|
1388 | * then switched. According to AMD, the stack is switched first and
|
---|
1389 | * then pushes might fault!
|
---|
1390 | * NB: OS/2 Warp 3/4 actively relies on the fact that possible
|
---|
1391 | * incoming stack #PF happens before actual stack switch. AMD is
|
---|
1392 | * either lying or implicitly assumes that new state is committed
|
---|
1393 | * only if and when an instruction doesn't fault.
|
---|
1394 | */
|
---|
1395 |
|
---|
1396 | /** @todo: According to AMD, CS is loaded first, then SS.
|
---|
1397 | * According to Intel, it's the other way around!?
|
---|
1398 | */
|
---|
1399 |
|
---|
1400 | /** @todo: Intel and AMD disagree on when exactly the CPL changes! */
|
---|
1401 |
|
---|
1402 | /* Set the accessed bit before committing new SS. */
|
---|
1403 | if (!(DescSS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
1404 | {
|
---|
1405 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewSS);
|
---|
1406 | if (rcStrict != VINF_SUCCESS)
|
---|
1407 | return rcStrict;
|
---|
1408 | DescSS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
1409 | }
|
---|
1410 |
|
---|
1411 | /* Remember the old SS:rSP and their linear address. */
|
---|
1412 | uOldSS = pVCpu->cpum.GstCtx.ss.Sel;
|
---|
1413 | uOldRsp = pVCpu->cpum.GstCtx.ss.Attr.n.u1DefBig ? pVCpu->cpum.GstCtx.rsp : pVCpu->cpum.GstCtx.sp;
|
---|
1414 |
|
---|
1415 | GCPtrParmWds = pVCpu->cpum.GstCtx.ss.u64Base + uOldRsp;
|
---|
1416 |
|
---|
1417 | /* HACK ALERT! Probe if the write to the new stack will succeed. May #SS(NewSS)
|
---|
1418 | or #PF, the former is not implemented in this workaround. */
|
---|
1419 | /** @todo Proper fix callgate target stack exceptions. */
|
---|
1420 | /** @todo testcase: Cover callgates with partially or fully inaccessible
|
---|
1421 | * target stacks. */
|
---|
1422 | void *pvNewFrame;
|
---|
1423 | RTGCPTR GCPtrNewStack = X86DESC_BASE(&DescSS.Legacy) + uNewRsp - cbNewStack;
|
---|
1424 | rcStrict = iemMemMap(pVCpu, &pvNewFrame, cbNewStack, UINT8_MAX, GCPtrNewStack, IEM_ACCESS_SYS_RW);
|
---|
1425 | if (rcStrict != VINF_SUCCESS)
|
---|
1426 | {
|
---|
1427 | Log(("BranchCallGate: Incoming stack (%04x:%08RX64) not accessible, rc=%Rrc\n", uNewSS, uNewRsp, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1428 | return rcStrict;
|
---|
1429 | }
|
---|
1430 | rcStrict = iemMemCommitAndUnmap(pVCpu, pvNewFrame, IEM_ACCESS_SYS_RW);
|
---|
1431 | if (rcStrict != VINF_SUCCESS)
|
---|
1432 | {
|
---|
1433 | Log(("BranchCallGate: New stack probe unmapping failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1434 | return rcStrict;
|
---|
1435 | }
|
---|
1436 |
|
---|
1437 | /* Commit new SS:rSP. */
|
---|
1438 | pVCpu->cpum.GstCtx.ss.Sel = uNewSS;
|
---|
1439 | pVCpu->cpum.GstCtx.ss.ValidSel = uNewSS;
|
---|
1440 | pVCpu->cpum.GstCtx.ss.Attr.u = X86DESC_GET_HID_ATTR(&DescSS.Legacy);
|
---|
1441 | pVCpu->cpum.GstCtx.ss.u32Limit = X86DESC_LIMIT_G(&DescSS.Legacy);
|
---|
1442 | pVCpu->cpum.GstCtx.ss.u64Base = X86DESC_BASE(&DescSS.Legacy);
|
---|
1443 | pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
1444 | pVCpu->cpum.GstCtx.rsp = uNewRsp;
|
---|
1445 | pVCpu->iem.s.uCpl = uNewCSDpl;
|
---|
1446 | Assert(CPUMSELREG_ARE_HIDDEN_PARTS_VALID(pVCpu, &pVCpu->cpum.GstCtx.ss));
|
---|
1447 | CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_HIDDEN_SEL_REGS);
|
---|
1448 |
|
---|
1449 | /* At this point the stack access must not fail because new state was already committed. */
|
---|
1450 | /** @todo this can still fail due to SS.LIMIT not check. */
|
---|
1451 | rcStrict = iemMemStackPushBeginSpecial(pVCpu, cbNewStack,
|
---|
1452 | &uPtrRet.pv, &uNewRsp);
|
---|
1453 | AssertMsgReturn(rcStrict == VINF_SUCCESS, ("BranchCallGate: New stack mapping failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)),
|
---|
1454 | VERR_INTERNAL_ERROR_5);
|
---|
1455 |
|
---|
1456 | if (!IEM_IS_LONG_MODE(pVCpu))
|
---|
1457 | {
|
---|
1458 | if (pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_386_CALL_GATE)
|
---|
1459 | {
|
---|
1460 | /* Push the old CS:rIP. */
|
---|
1461 | uPtrRet.pu32[0] = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
1462 | uPtrRet.pu32[1] = pVCpu->cpum.GstCtx.cs.Sel; /** @todo Testcase: What is written to the high word when pushing CS? */
|
---|
1463 |
|
---|
1464 | if (cbWords)
|
---|
1465 | {
|
---|
1466 | /* Map the relevant chunk of the old stack. */
|
---|
1467 | rcStrict = iemMemMap(pVCpu, &uPtrParmWds.pv, cbWords * 4, UINT8_MAX, GCPtrParmWds, IEM_ACCESS_DATA_R);
|
---|
1468 | if (rcStrict != VINF_SUCCESS)
|
---|
1469 | {
|
---|
1470 | Log(("BranchCallGate: Old stack mapping (32-bit) failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1471 | return rcStrict;
|
---|
1472 | }
|
---|
1473 |
|
---|
1474 | /* Copy the parameter (d)words. */
|
---|
1475 | for (int i = 0; i < cbWords; ++i)
|
---|
1476 | uPtrRet.pu32[2 + i] = uPtrParmWds.pu32[i];
|
---|
1477 |
|
---|
1478 | /* Unmap the old stack. */
|
---|
1479 | rcStrict = iemMemCommitAndUnmap(pVCpu, uPtrParmWds.pv, IEM_ACCESS_DATA_R);
|
---|
1480 | if (rcStrict != VINF_SUCCESS)
|
---|
1481 | {
|
---|
1482 | Log(("BranchCallGate: Old stack unmapping (32-bit) failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1483 | return rcStrict;
|
---|
1484 | }
|
---|
1485 | }
|
---|
1486 |
|
---|
1487 | /* Push the old SS:rSP. */
|
---|
1488 | uPtrRet.pu32[2 + cbWords + 0] = uOldRsp;
|
---|
1489 | uPtrRet.pu32[2 + cbWords + 1] = uOldSS;
|
---|
1490 | }
|
---|
1491 | else
|
---|
1492 | {
|
---|
1493 | Assert(pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_286_CALL_GATE);
|
---|
1494 |
|
---|
1495 | /* Push the old CS:rIP. */
|
---|
1496 | uPtrRet.pu16[0] = pVCpu->cpum.GstCtx.ip + cbInstr;
|
---|
1497 | uPtrRet.pu16[1] = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
1498 |
|
---|
1499 | if (cbWords)
|
---|
1500 | {
|
---|
1501 | /* Map the relevant chunk of the old stack. */
|
---|
1502 | rcStrict = iemMemMap(pVCpu, &uPtrParmWds.pv, cbWords * 2, UINT8_MAX, GCPtrParmWds, IEM_ACCESS_DATA_R);
|
---|
1503 | if (rcStrict != VINF_SUCCESS)
|
---|
1504 | {
|
---|
1505 | Log(("BranchCallGate: Old stack mapping (16-bit) failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1506 | return rcStrict;
|
---|
1507 | }
|
---|
1508 |
|
---|
1509 | /* Copy the parameter words. */
|
---|
1510 | for (int i = 0; i < cbWords; ++i)
|
---|
1511 | uPtrRet.pu16[2 + i] = uPtrParmWds.pu16[i];
|
---|
1512 |
|
---|
1513 | /* Unmap the old stack. */
|
---|
1514 | rcStrict = iemMemCommitAndUnmap(pVCpu, uPtrParmWds.pv, IEM_ACCESS_DATA_R);
|
---|
1515 | if (rcStrict != VINF_SUCCESS)
|
---|
1516 | {
|
---|
1517 | Log(("BranchCallGate: Old stack unmapping (32-bit) failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1518 | return rcStrict;
|
---|
1519 | }
|
---|
1520 | }
|
---|
1521 |
|
---|
1522 | /* Push the old SS:rSP. */
|
---|
1523 | uPtrRet.pu16[2 + cbWords + 0] = uOldRsp;
|
---|
1524 | uPtrRet.pu16[2 + cbWords + 1] = uOldSS;
|
---|
1525 | }
|
---|
1526 | }
|
---|
1527 | else
|
---|
1528 | {
|
---|
1529 | Assert(pDesc->Legacy.Gate.u4Type == AMD64_SEL_TYPE_SYS_CALL_GATE);
|
---|
1530 |
|
---|
1531 | /* For 64-bit gates, no parameters are copied. Just push old SS:rSP and CS:rIP. */
|
---|
1532 | uPtrRet.pu64[0] = pVCpu->cpum.GstCtx.rip + cbInstr;
|
---|
1533 | uPtrRet.pu64[1] = pVCpu->cpum.GstCtx.cs.Sel; /** @todo Testcase: What is written to the high words when pushing CS? */
|
---|
1534 | uPtrRet.pu64[2] = uOldRsp;
|
---|
1535 | uPtrRet.pu64[3] = uOldSS; /** @todo Testcase: What is written to the high words when pushing SS? */
|
---|
1536 | }
|
---|
1537 |
|
---|
1538 | rcStrict = iemMemStackPushCommitSpecial(pVCpu, uPtrRet.pv, uNewRsp);
|
---|
1539 | if (rcStrict != VINF_SUCCESS)
|
---|
1540 | {
|
---|
1541 | Log(("BranchCallGate: New stack unmapping failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1542 | return rcStrict;
|
---|
1543 | }
|
---|
1544 |
|
---|
1545 | /* Chop the high bits off if 16-bit gate (Intel says so). */
|
---|
1546 | if (pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_286_CALL_GATE)
|
---|
1547 | uNewRip = (uint16_t)uNewRip;
|
---|
1548 |
|
---|
1549 | /* Limit / canonical check. */
|
---|
1550 | cbLimit = X86DESC_LIMIT_G(&DescCS.Legacy);
|
---|
1551 | if (!IEM_IS_LONG_MODE(pVCpu))
|
---|
1552 | {
|
---|
1553 | if (uNewRip > cbLimit)
|
---|
1554 | {
|
---|
1555 | Log(("BranchCallGate %04x:%08RX64 -> out of bounds (%#x)\n", uNewCS, uNewRip, cbLimit));
|
---|
1556 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, 0);
|
---|
1557 | }
|
---|
1558 | u64Base = X86DESC_BASE(&DescCS.Legacy);
|
---|
1559 | }
|
---|
1560 | else
|
---|
1561 | {
|
---|
1562 | Assert(pDesc->Legacy.Gate.u4Type == AMD64_SEL_TYPE_SYS_CALL_GATE);
|
---|
1563 | if (!IEM_IS_CANONICAL(uNewRip))
|
---|
1564 | {
|
---|
1565 | Log(("BranchCallGate call %04x:%016RX64 - not canonical -> #GP\n", uNewCS, uNewRip));
|
---|
1566 | return iemRaiseNotCanonical(pVCpu);
|
---|
1567 | }
|
---|
1568 | u64Base = 0;
|
---|
1569 | }
|
---|
1570 |
|
---|
1571 | /*
|
---|
1572 | * Now set the accessed bit before
|
---|
1573 | * writing the return address to the stack and committing the result into
|
---|
1574 | * CS, CSHID and RIP.
|
---|
1575 | */
|
---|
1576 | /** @todo Testcase: Need to check WHEN exactly the accessed bit is set. */
|
---|
1577 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
1578 | {
|
---|
1579 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewCS);
|
---|
1580 | if (rcStrict != VINF_SUCCESS)
|
---|
1581 | return rcStrict;
|
---|
1582 | /** @todo check what VT-x and AMD-V does. */
|
---|
1583 | DescCS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
1584 | }
|
---|
1585 |
|
---|
1586 | /* Commit new CS:rIP. */
|
---|
1587 | pVCpu->cpum.GstCtx.rip = uNewRip;
|
---|
1588 | pVCpu->cpum.GstCtx.cs.Sel = uNewCS & X86_SEL_MASK_OFF_RPL;
|
---|
1589 | pVCpu->cpum.GstCtx.cs.Sel |= pVCpu->iem.s.uCpl;
|
---|
1590 | pVCpu->cpum.GstCtx.cs.ValidSel = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
1591 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
1592 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCS.Legacy);
|
---|
1593 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimit;
|
---|
1594 | pVCpu->cpum.GstCtx.cs.u64Base = u64Base;
|
---|
1595 | pVCpu->iem.s.enmCpuMode = iemCalcCpuMode(pVCpu);
|
---|
1596 | }
|
---|
1597 | else
|
---|
1598 | {
|
---|
1599 | /* Same privilege. */
|
---|
1600 | /** @todo: This is very similar to regular far calls; merge! */
|
---|
1601 |
|
---|
1602 | /* Check stack first - may #SS(0). */
|
---|
1603 | /** @todo check how gate size affects pushing of CS! Does callf 16:32 in
|
---|
1604 | * 16-bit code cause a two or four byte CS to be pushed? */
|
---|
1605 | rcStrict = iemMemStackPushBeginSpecial(pVCpu,
|
---|
1606 | IEM_IS_LONG_MODE(pVCpu) ? 8+8
|
---|
1607 | : pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_386_CALL_GATE ? 4+4 : 2+2,
|
---|
1608 | &uPtrRet.pv, &uNewRsp);
|
---|
1609 | if (rcStrict != VINF_SUCCESS)
|
---|
1610 | return rcStrict;
|
---|
1611 |
|
---|
1612 | /* Chop the high bits off if 16-bit gate (Intel says so). */
|
---|
1613 | if (pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_286_CALL_GATE)
|
---|
1614 | uNewRip = (uint16_t)uNewRip;
|
---|
1615 |
|
---|
1616 | /* Limit / canonical check. */
|
---|
1617 | cbLimit = X86DESC_LIMIT_G(&DescCS.Legacy);
|
---|
1618 | if (!IEM_IS_LONG_MODE(pVCpu))
|
---|
1619 | {
|
---|
1620 | if (uNewRip > cbLimit)
|
---|
1621 | {
|
---|
1622 | Log(("BranchCallGate %04x:%08RX64 -> out of bounds (%#x)\n", uNewCS, uNewRip, cbLimit));
|
---|
1623 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, 0);
|
---|
1624 | }
|
---|
1625 | u64Base = X86DESC_BASE(&DescCS.Legacy);
|
---|
1626 | }
|
---|
1627 | else
|
---|
1628 | {
|
---|
1629 | if (!IEM_IS_CANONICAL(uNewRip))
|
---|
1630 | {
|
---|
1631 | Log(("BranchCallGate call %04x:%016RX64 - not canonical -> #GP\n", uNewCS, uNewRip));
|
---|
1632 | return iemRaiseNotCanonical(pVCpu);
|
---|
1633 | }
|
---|
1634 | u64Base = 0;
|
---|
1635 | }
|
---|
1636 |
|
---|
1637 | /*
|
---|
1638 | * Now set the accessed bit before
|
---|
1639 | * writing the return address to the stack and committing the result into
|
---|
1640 | * CS, CSHID and RIP.
|
---|
1641 | */
|
---|
1642 | /** @todo Testcase: Need to check WHEN exactly the accessed bit is set. */
|
---|
1643 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
1644 | {
|
---|
1645 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewCS);
|
---|
1646 | if (rcStrict != VINF_SUCCESS)
|
---|
1647 | return rcStrict;
|
---|
1648 | /** @todo check what VT-x and AMD-V does. */
|
---|
1649 | DescCS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
1650 | }
|
---|
1651 |
|
---|
1652 | /* stack */
|
---|
1653 | if (!IEM_IS_LONG_MODE(pVCpu))
|
---|
1654 | {
|
---|
1655 | if (pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_386_CALL_GATE)
|
---|
1656 | {
|
---|
1657 | uPtrRet.pu32[0] = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
1658 | uPtrRet.pu32[1] = pVCpu->cpum.GstCtx.cs.Sel; /** @todo Testcase: What is written to the high word when pushing CS? */
|
---|
1659 | }
|
---|
1660 | else
|
---|
1661 | {
|
---|
1662 | Assert(pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_286_CALL_GATE);
|
---|
1663 | uPtrRet.pu16[0] = pVCpu->cpum.GstCtx.ip + cbInstr;
|
---|
1664 | uPtrRet.pu16[1] = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
1665 | }
|
---|
1666 | }
|
---|
1667 | else
|
---|
1668 | {
|
---|
1669 | Assert(pDesc->Legacy.Gate.u4Type == AMD64_SEL_TYPE_SYS_CALL_GATE);
|
---|
1670 | uPtrRet.pu64[0] = pVCpu->cpum.GstCtx.rip + cbInstr;
|
---|
1671 | uPtrRet.pu64[1] = pVCpu->cpum.GstCtx.cs.Sel; /** @todo Testcase: What is written to the high words when pushing CS? */
|
---|
1672 | }
|
---|
1673 |
|
---|
1674 | rcStrict = iemMemStackPushCommitSpecial(pVCpu, uPtrRet.pv, uNewRsp);
|
---|
1675 | if (rcStrict != VINF_SUCCESS)
|
---|
1676 | return rcStrict;
|
---|
1677 |
|
---|
1678 | /* commit */
|
---|
1679 | pVCpu->cpum.GstCtx.rip = uNewRip;
|
---|
1680 | pVCpu->cpum.GstCtx.cs.Sel = uNewCS & X86_SEL_MASK_OFF_RPL;
|
---|
1681 | pVCpu->cpum.GstCtx.cs.Sel |= pVCpu->iem.s.uCpl;
|
---|
1682 | pVCpu->cpum.GstCtx.cs.ValidSel = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
1683 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
1684 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCS.Legacy);
|
---|
1685 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimit;
|
---|
1686 | pVCpu->cpum.GstCtx.cs.u64Base = u64Base;
|
---|
1687 | pVCpu->iem.s.enmCpuMode = iemCalcCpuMode(pVCpu);
|
---|
1688 | }
|
---|
1689 | }
|
---|
1690 | pVCpu->cpum.GstCtx.eflags.Bits.u1RF = 0;
|
---|
1691 |
|
---|
1692 | /* Flush the prefetch buffer. */
|
---|
1693 | # ifdef IEM_WITH_CODE_TLB
|
---|
1694 | pVCpu->iem.s.pbInstrBuf = NULL;
|
---|
1695 | # else
|
---|
1696 | pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
|
---|
1697 | # endif
|
---|
1698 | return VINF_SUCCESS;
|
---|
1699 | #endif
|
---|
1700 | }
|
---|
1701 |
|
---|
1702 |
|
---|
1703 | /**
|
---|
1704 | * Implements far jumps and calls thru system selectors.
|
---|
1705 | *
|
---|
1706 | * @param uSel The selector.
|
---|
1707 | * @param enmBranch The kind of branching we're performing.
|
---|
1708 | * @param enmEffOpSize The effective operand size.
|
---|
1709 | * @param pDesc The descriptor corresponding to @a uSel.
|
---|
1710 | */
|
---|
1711 | IEM_CIMPL_DEF_4(iemCImpl_BranchSysSel, uint16_t, uSel, IEMBRANCH, enmBranch, IEMMODE, enmEffOpSize, PIEMSELDESC, pDesc)
|
---|
1712 | {
|
---|
1713 | Assert(enmBranch == IEMBRANCH_JUMP || enmBranch == IEMBRANCH_CALL);
|
---|
1714 | Assert((uSel & X86_SEL_MASK_OFF_RPL));
|
---|
1715 | IEM_CTX_IMPORT_RET(pVCpu, IEM_CPUMCTX_EXTRN_XCPT_MASK);
|
---|
1716 |
|
---|
1717 | if (IEM_IS_LONG_MODE(pVCpu))
|
---|
1718 | switch (pDesc->Legacy.Gen.u4Type)
|
---|
1719 | {
|
---|
1720 | case AMD64_SEL_TYPE_SYS_CALL_GATE:
|
---|
1721 | return IEM_CIMPL_CALL_4(iemCImpl_BranchCallGate, uSel, enmBranch, enmEffOpSize, pDesc);
|
---|
1722 |
|
---|
1723 | default:
|
---|
1724 | case AMD64_SEL_TYPE_SYS_LDT:
|
---|
1725 | case AMD64_SEL_TYPE_SYS_TSS_BUSY:
|
---|
1726 | case AMD64_SEL_TYPE_SYS_TSS_AVAIL:
|
---|
1727 | case AMD64_SEL_TYPE_SYS_TRAP_GATE:
|
---|
1728 | case AMD64_SEL_TYPE_SYS_INT_GATE:
|
---|
1729 | Log(("branch %04x -> wrong sys selector (64-bit): %d\n", uSel, pDesc->Legacy.Gen.u4Type));
|
---|
1730 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1731 | }
|
---|
1732 |
|
---|
1733 | switch (pDesc->Legacy.Gen.u4Type)
|
---|
1734 | {
|
---|
1735 | case X86_SEL_TYPE_SYS_286_CALL_GATE:
|
---|
1736 | case X86_SEL_TYPE_SYS_386_CALL_GATE:
|
---|
1737 | return IEM_CIMPL_CALL_4(iemCImpl_BranchCallGate, uSel, enmBranch, enmEffOpSize, pDesc);
|
---|
1738 |
|
---|
1739 | case X86_SEL_TYPE_SYS_TASK_GATE:
|
---|
1740 | return IEM_CIMPL_CALL_4(iemCImpl_BranchTaskGate, uSel, enmBranch, enmEffOpSize, pDesc);
|
---|
1741 |
|
---|
1742 | case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
|
---|
1743 | case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
|
---|
1744 | return IEM_CIMPL_CALL_4(iemCImpl_BranchTaskSegment, uSel, enmBranch, enmEffOpSize, pDesc);
|
---|
1745 |
|
---|
1746 | case X86_SEL_TYPE_SYS_286_TSS_BUSY:
|
---|
1747 | Log(("branch %04x -> busy 286 TSS\n", uSel));
|
---|
1748 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1749 |
|
---|
1750 | case X86_SEL_TYPE_SYS_386_TSS_BUSY:
|
---|
1751 | Log(("branch %04x -> busy 386 TSS\n", uSel));
|
---|
1752 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1753 |
|
---|
1754 | default:
|
---|
1755 | case X86_SEL_TYPE_SYS_LDT:
|
---|
1756 | case X86_SEL_TYPE_SYS_286_INT_GATE:
|
---|
1757 | case X86_SEL_TYPE_SYS_286_TRAP_GATE:
|
---|
1758 | case X86_SEL_TYPE_SYS_386_INT_GATE:
|
---|
1759 | case X86_SEL_TYPE_SYS_386_TRAP_GATE:
|
---|
1760 | Log(("branch %04x -> wrong sys selector: %d\n", uSel, pDesc->Legacy.Gen.u4Type));
|
---|
1761 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1762 | }
|
---|
1763 | }
|
---|
1764 |
|
---|
1765 |
|
---|
1766 | /**
|
---|
1767 | * Implements far jumps.
|
---|
1768 | *
|
---|
1769 | * @param uSel The selector.
|
---|
1770 | * @param offSeg The segment offset.
|
---|
1771 | * @param enmEffOpSize The effective operand size.
|
---|
1772 | */
|
---|
1773 | IEM_CIMPL_DEF_3(iemCImpl_FarJmp, uint16_t, uSel, uint64_t, offSeg, IEMMODE, enmEffOpSize)
|
---|
1774 | {
|
---|
1775 | NOREF(cbInstr);
|
---|
1776 | Assert(offSeg <= UINT32_MAX);
|
---|
1777 |
|
---|
1778 | /*
|
---|
1779 | * Real mode and V8086 mode are easy. The only snag seems to be that
|
---|
1780 | * CS.limit doesn't change and the limit check is done against the current
|
---|
1781 | * limit.
|
---|
1782 | */
|
---|
1783 | /** @todo Robert Collins claims (The Segment Descriptor Cache, DDJ August
|
---|
1784 | * 1998) that up to and including the Intel 486, far control
|
---|
1785 | * transfers in real mode set default CS attributes (0x93) and also
|
---|
1786 | * set a 64K segment limit. Starting with the Pentium, the
|
---|
1787 | * attributes and limit are left alone but the access rights are
|
---|
1788 | * ignored. We only implement the Pentium+ behavior.
|
---|
1789 | * */
|
---|
1790 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
1791 | {
|
---|
1792 | Assert(enmEffOpSize == IEMMODE_16BIT || enmEffOpSize == IEMMODE_32BIT);
|
---|
1793 | if (offSeg > pVCpu->cpum.GstCtx.cs.u32Limit)
|
---|
1794 | {
|
---|
1795 | Log(("iemCImpl_FarJmp: 16-bit limit\n"));
|
---|
1796 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
1797 | }
|
---|
1798 |
|
---|
1799 | if (enmEffOpSize == IEMMODE_16BIT) /** @todo WRONG, must pass this. */
|
---|
1800 | pVCpu->cpum.GstCtx.rip = offSeg;
|
---|
1801 | else
|
---|
1802 | pVCpu->cpum.GstCtx.rip = offSeg & UINT16_MAX;
|
---|
1803 | pVCpu->cpum.GstCtx.cs.Sel = uSel;
|
---|
1804 | pVCpu->cpum.GstCtx.cs.ValidSel = uSel;
|
---|
1805 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
1806 | pVCpu->cpum.GstCtx.cs.u64Base = (uint32_t)uSel << 4;
|
---|
1807 | pVCpu->cpum.GstCtx.eflags.Bits.u1RF = 0;
|
---|
1808 | return VINF_SUCCESS;
|
---|
1809 | }
|
---|
1810 |
|
---|
1811 | /*
|
---|
1812 | * Protected mode. Need to parse the specified descriptor...
|
---|
1813 | */
|
---|
1814 | if (!(uSel & X86_SEL_MASK_OFF_RPL))
|
---|
1815 | {
|
---|
1816 | Log(("jmpf %04x:%08RX64 -> invalid selector, #GP(0)\n", uSel, offSeg));
|
---|
1817 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
1818 | }
|
---|
1819 |
|
---|
1820 | /* Fetch the descriptor. */
|
---|
1821 | IEMSELDESC Desc;
|
---|
1822 | VBOXSTRICTRC rcStrict = iemMemFetchSelDesc(pVCpu, &Desc, uSel, X86_XCPT_GP);
|
---|
1823 | if (rcStrict != VINF_SUCCESS)
|
---|
1824 | return rcStrict;
|
---|
1825 |
|
---|
1826 | /* Is it there? */
|
---|
1827 | if (!Desc.Legacy.Gen.u1Present) /** @todo this is probably checked too early. Testcase! */
|
---|
1828 | {
|
---|
1829 | Log(("jmpf %04x:%08RX64 -> segment not present\n", uSel, offSeg));
|
---|
1830 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uSel);
|
---|
1831 | }
|
---|
1832 |
|
---|
1833 | /*
|
---|
1834 | * Deal with it according to its type. We do the standard code selectors
|
---|
1835 | * here and dispatch the system selectors to worker functions.
|
---|
1836 | */
|
---|
1837 | if (!Desc.Legacy.Gen.u1DescType)
|
---|
1838 | return IEM_CIMPL_CALL_4(iemCImpl_BranchSysSel, uSel, IEMBRANCH_JUMP, enmEffOpSize, &Desc);
|
---|
1839 |
|
---|
1840 | /* Only code segments. */
|
---|
1841 | if (!(Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE))
|
---|
1842 | {
|
---|
1843 | Log(("jmpf %04x:%08RX64 -> not a code selector (u4Type=%#x).\n", uSel, offSeg, Desc.Legacy.Gen.u4Type));
|
---|
1844 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1845 | }
|
---|
1846 |
|
---|
1847 | /* L vs D. */
|
---|
1848 | if ( Desc.Legacy.Gen.u1Long
|
---|
1849 | && Desc.Legacy.Gen.u1DefBig
|
---|
1850 | && IEM_IS_LONG_MODE(pVCpu))
|
---|
1851 | {
|
---|
1852 | Log(("jmpf %04x:%08RX64 -> both L and D are set.\n", uSel, offSeg));
|
---|
1853 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1854 | }
|
---|
1855 |
|
---|
1856 | /* DPL/RPL/CPL check, where conforming segments makes a difference. */
|
---|
1857 | if (Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF)
|
---|
1858 | {
|
---|
1859 | if (pVCpu->iem.s.uCpl < Desc.Legacy.Gen.u2Dpl)
|
---|
1860 | {
|
---|
1861 | Log(("jmpf %04x:%08RX64 -> DPL violation (conforming); DPL=%d CPL=%u\n",
|
---|
1862 | uSel, offSeg, Desc.Legacy.Gen.u2Dpl, pVCpu->iem.s.uCpl));
|
---|
1863 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1864 | }
|
---|
1865 | }
|
---|
1866 | else
|
---|
1867 | {
|
---|
1868 | if (pVCpu->iem.s.uCpl != Desc.Legacy.Gen.u2Dpl)
|
---|
1869 | {
|
---|
1870 | Log(("jmpf %04x:%08RX64 -> CPL != DPL; DPL=%d CPL=%u\n", uSel, offSeg, Desc.Legacy.Gen.u2Dpl, pVCpu->iem.s.uCpl));
|
---|
1871 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1872 | }
|
---|
1873 | if ((uSel & X86_SEL_RPL) > pVCpu->iem.s.uCpl)
|
---|
1874 | {
|
---|
1875 | Log(("jmpf %04x:%08RX64 -> RPL > DPL; RPL=%d CPL=%u\n", uSel, offSeg, (uSel & X86_SEL_RPL), pVCpu->iem.s.uCpl));
|
---|
1876 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1877 | }
|
---|
1878 | }
|
---|
1879 |
|
---|
1880 | /* Chop the high bits if 16-bit (Intel says so). */
|
---|
1881 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
1882 | offSeg &= UINT16_MAX;
|
---|
1883 |
|
---|
1884 | /* Limit check. (Should alternatively check for non-canonical addresses
|
---|
1885 | here, but that is ruled out by offSeg being 32-bit, right?) */
|
---|
1886 | uint64_t u64Base;
|
---|
1887 | uint32_t cbLimit = X86DESC_LIMIT_G(&Desc.Legacy);
|
---|
1888 | if (Desc.Legacy.Gen.u1Long)
|
---|
1889 | u64Base = 0;
|
---|
1890 | else
|
---|
1891 | {
|
---|
1892 | if (offSeg > cbLimit)
|
---|
1893 | {
|
---|
1894 | Log(("jmpf %04x:%08RX64 -> out of bounds (%#x)\n", uSel, offSeg, cbLimit));
|
---|
1895 | /** @todo: Intel says this is #GP(0)! */
|
---|
1896 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
1897 | }
|
---|
1898 | u64Base = X86DESC_BASE(&Desc.Legacy);
|
---|
1899 | }
|
---|
1900 |
|
---|
1901 | /*
|
---|
1902 | * Ok, everything checked out fine. Now set the accessed bit before
|
---|
1903 | * committing the result into CS, CSHID and RIP.
|
---|
1904 | */
|
---|
1905 | if (!(Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
1906 | {
|
---|
1907 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uSel);
|
---|
1908 | if (rcStrict != VINF_SUCCESS)
|
---|
1909 | return rcStrict;
|
---|
1910 | /** @todo check what VT-x and AMD-V does. */
|
---|
1911 | Desc.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
1912 | }
|
---|
1913 |
|
---|
1914 | /* commit */
|
---|
1915 | pVCpu->cpum.GstCtx.rip = offSeg;
|
---|
1916 | pVCpu->cpum.GstCtx.cs.Sel = uSel & X86_SEL_MASK_OFF_RPL;
|
---|
1917 | pVCpu->cpum.GstCtx.cs.Sel |= pVCpu->iem.s.uCpl; /** @todo is this right for conforming segs? or in general? */
|
---|
1918 | pVCpu->cpum.GstCtx.cs.ValidSel = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
1919 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
1920 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&Desc.Legacy);
|
---|
1921 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimit;
|
---|
1922 | pVCpu->cpum.GstCtx.cs.u64Base = u64Base;
|
---|
1923 | pVCpu->iem.s.enmCpuMode = iemCalcCpuMode(pVCpu);
|
---|
1924 | pVCpu->cpum.GstCtx.eflags.Bits.u1RF = 0;
|
---|
1925 | /** @todo check if the hidden bits are loaded correctly for 64-bit
|
---|
1926 | * mode. */
|
---|
1927 |
|
---|
1928 | /* Flush the prefetch buffer. */
|
---|
1929 | #ifdef IEM_WITH_CODE_TLB
|
---|
1930 | pVCpu->iem.s.pbInstrBuf = NULL;
|
---|
1931 | #else
|
---|
1932 | pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
|
---|
1933 | #endif
|
---|
1934 |
|
---|
1935 | return VINF_SUCCESS;
|
---|
1936 | }
|
---|
1937 |
|
---|
1938 |
|
---|
1939 | /**
|
---|
1940 | * Implements far calls.
|
---|
1941 | *
|
---|
1942 | * This very similar to iemCImpl_FarJmp.
|
---|
1943 | *
|
---|
1944 | * @param uSel The selector.
|
---|
1945 | * @param offSeg The segment offset.
|
---|
1946 | * @param enmEffOpSize The operand size (in case we need it).
|
---|
1947 | */
|
---|
1948 | IEM_CIMPL_DEF_3(iemCImpl_callf, uint16_t, uSel, uint64_t, offSeg, IEMMODE, enmEffOpSize)
|
---|
1949 | {
|
---|
1950 | VBOXSTRICTRC rcStrict;
|
---|
1951 | uint64_t uNewRsp;
|
---|
1952 | RTPTRUNION uPtrRet;
|
---|
1953 |
|
---|
1954 | /*
|
---|
1955 | * Real mode and V8086 mode are easy. The only snag seems to be that
|
---|
1956 | * CS.limit doesn't change and the limit check is done against the current
|
---|
1957 | * limit.
|
---|
1958 | */
|
---|
1959 | /** @todo See comment for similar code in iemCImpl_FarJmp */
|
---|
1960 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
1961 | {
|
---|
1962 | Assert(enmEffOpSize == IEMMODE_16BIT || enmEffOpSize == IEMMODE_32BIT);
|
---|
1963 |
|
---|
1964 | /* Check stack first - may #SS(0). */
|
---|
1965 | rcStrict = iemMemStackPushBeginSpecial(pVCpu, enmEffOpSize == IEMMODE_32BIT ? 4+4 : 2+2,
|
---|
1966 | &uPtrRet.pv, &uNewRsp);
|
---|
1967 | if (rcStrict != VINF_SUCCESS)
|
---|
1968 | return rcStrict;
|
---|
1969 |
|
---|
1970 | /* Check the target address range. */
|
---|
1971 | if (offSeg > UINT32_MAX)
|
---|
1972 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
1973 |
|
---|
1974 | /* Everything is fine, push the return address. */
|
---|
1975 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
1976 | {
|
---|
1977 | uPtrRet.pu16[0] = pVCpu->cpum.GstCtx.ip + cbInstr;
|
---|
1978 | uPtrRet.pu16[1] = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
1979 | }
|
---|
1980 | else
|
---|
1981 | {
|
---|
1982 | uPtrRet.pu32[0] = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
1983 | uPtrRet.pu16[2] = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
1984 | }
|
---|
1985 | rcStrict = iemMemStackPushCommitSpecial(pVCpu, uPtrRet.pv, uNewRsp);
|
---|
1986 | if (rcStrict != VINF_SUCCESS)
|
---|
1987 | return rcStrict;
|
---|
1988 |
|
---|
1989 | /* Branch. */
|
---|
1990 | pVCpu->cpum.GstCtx.rip = offSeg;
|
---|
1991 | pVCpu->cpum.GstCtx.cs.Sel = uSel;
|
---|
1992 | pVCpu->cpum.GstCtx.cs.ValidSel = uSel;
|
---|
1993 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
1994 | pVCpu->cpum.GstCtx.cs.u64Base = (uint32_t)uSel << 4;
|
---|
1995 | pVCpu->cpum.GstCtx.eflags.Bits.u1RF = 0;
|
---|
1996 | return VINF_SUCCESS;
|
---|
1997 | }
|
---|
1998 |
|
---|
1999 | /*
|
---|
2000 | * Protected mode. Need to parse the specified descriptor...
|
---|
2001 | */
|
---|
2002 | if (!(uSel & X86_SEL_MASK_OFF_RPL))
|
---|
2003 | {
|
---|
2004 | Log(("callf %04x:%08RX64 -> invalid selector, #GP(0)\n", uSel, offSeg));
|
---|
2005 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
2006 | }
|
---|
2007 |
|
---|
2008 | /* Fetch the descriptor. */
|
---|
2009 | IEMSELDESC Desc;
|
---|
2010 | rcStrict = iemMemFetchSelDesc(pVCpu, &Desc, uSel, X86_XCPT_GP);
|
---|
2011 | if (rcStrict != VINF_SUCCESS)
|
---|
2012 | return rcStrict;
|
---|
2013 |
|
---|
2014 | /*
|
---|
2015 | * Deal with it according to its type. We do the standard code selectors
|
---|
2016 | * here and dispatch the system selectors to worker functions.
|
---|
2017 | */
|
---|
2018 | if (!Desc.Legacy.Gen.u1DescType)
|
---|
2019 | return IEM_CIMPL_CALL_4(iemCImpl_BranchSysSel, uSel, IEMBRANCH_CALL, enmEffOpSize, &Desc);
|
---|
2020 |
|
---|
2021 | /* Only code segments. */
|
---|
2022 | if (!(Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE))
|
---|
2023 | {
|
---|
2024 | Log(("callf %04x:%08RX64 -> not a code selector (u4Type=%#x).\n", uSel, offSeg, Desc.Legacy.Gen.u4Type));
|
---|
2025 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
2026 | }
|
---|
2027 |
|
---|
2028 | /* L vs D. */
|
---|
2029 | if ( Desc.Legacy.Gen.u1Long
|
---|
2030 | && Desc.Legacy.Gen.u1DefBig
|
---|
2031 | && IEM_IS_LONG_MODE(pVCpu))
|
---|
2032 | {
|
---|
2033 | Log(("callf %04x:%08RX64 -> both L and D are set.\n", uSel, offSeg));
|
---|
2034 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
2035 | }
|
---|
2036 |
|
---|
2037 | /* DPL/RPL/CPL check, where conforming segments makes a difference. */
|
---|
2038 | if (Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF)
|
---|
2039 | {
|
---|
2040 | if (pVCpu->iem.s.uCpl < Desc.Legacy.Gen.u2Dpl)
|
---|
2041 | {
|
---|
2042 | Log(("callf %04x:%08RX64 -> DPL violation (conforming); DPL=%d CPL=%u\n",
|
---|
2043 | uSel, offSeg, Desc.Legacy.Gen.u2Dpl, pVCpu->iem.s.uCpl));
|
---|
2044 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
2045 | }
|
---|
2046 | }
|
---|
2047 | else
|
---|
2048 | {
|
---|
2049 | if (pVCpu->iem.s.uCpl != Desc.Legacy.Gen.u2Dpl)
|
---|
2050 | {
|
---|
2051 | Log(("callf %04x:%08RX64 -> CPL != DPL; DPL=%d CPL=%u\n", uSel, offSeg, Desc.Legacy.Gen.u2Dpl, pVCpu->iem.s.uCpl));
|
---|
2052 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
2053 | }
|
---|
2054 | if ((uSel & X86_SEL_RPL) > pVCpu->iem.s.uCpl)
|
---|
2055 | {
|
---|
2056 | Log(("callf %04x:%08RX64 -> RPL > DPL; RPL=%d CPL=%u\n", uSel, offSeg, (uSel & X86_SEL_RPL), pVCpu->iem.s.uCpl));
|
---|
2057 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
2058 | }
|
---|
2059 | }
|
---|
2060 |
|
---|
2061 | /* Is it there? */
|
---|
2062 | if (!Desc.Legacy.Gen.u1Present)
|
---|
2063 | {
|
---|
2064 | Log(("callf %04x:%08RX64 -> segment not present\n", uSel, offSeg));
|
---|
2065 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uSel);
|
---|
2066 | }
|
---|
2067 |
|
---|
2068 | /* Check stack first - may #SS(0). */
|
---|
2069 | /** @todo check how operand prefix affects pushing of CS! Does callf 16:32 in
|
---|
2070 | * 16-bit code cause a two or four byte CS to be pushed? */
|
---|
2071 | rcStrict = iemMemStackPushBeginSpecial(pVCpu,
|
---|
2072 | enmEffOpSize == IEMMODE_64BIT ? 8+8
|
---|
2073 | : enmEffOpSize == IEMMODE_32BIT ? 4+4 : 2+2,
|
---|
2074 | &uPtrRet.pv, &uNewRsp);
|
---|
2075 | if (rcStrict != VINF_SUCCESS)
|
---|
2076 | return rcStrict;
|
---|
2077 |
|
---|
2078 | /* Chop the high bits if 16-bit (Intel says so). */
|
---|
2079 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
2080 | offSeg &= UINT16_MAX;
|
---|
2081 |
|
---|
2082 | /* Limit / canonical check. */
|
---|
2083 | uint64_t u64Base;
|
---|
2084 | uint32_t cbLimit = X86DESC_LIMIT_G(&Desc.Legacy);
|
---|
2085 | if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
|
---|
2086 | {
|
---|
2087 | if (!IEM_IS_CANONICAL(offSeg))
|
---|
2088 | {
|
---|
2089 | Log(("callf %04x:%016RX64 - not canonical -> #GP\n", uSel, offSeg));
|
---|
2090 | return iemRaiseNotCanonical(pVCpu);
|
---|
2091 | }
|
---|
2092 | u64Base = 0;
|
---|
2093 | }
|
---|
2094 | else
|
---|
2095 | {
|
---|
2096 | if (offSeg > cbLimit)
|
---|
2097 | {
|
---|
2098 | Log(("callf %04x:%08RX64 -> out of bounds (%#x)\n", uSel, offSeg, cbLimit));
|
---|
2099 | /** @todo: Intel says this is #GP(0)! */
|
---|
2100 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
2101 | }
|
---|
2102 | u64Base = X86DESC_BASE(&Desc.Legacy);
|
---|
2103 | }
|
---|
2104 |
|
---|
2105 | /*
|
---|
2106 | * Now set the accessed bit before
|
---|
2107 | * writing the return address to the stack and committing the result into
|
---|
2108 | * CS, CSHID and RIP.
|
---|
2109 | */
|
---|
2110 | /** @todo Testcase: Need to check WHEN exactly the accessed bit is set. */
|
---|
2111 | if (!(Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
2112 | {
|
---|
2113 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uSel);
|
---|
2114 | if (rcStrict != VINF_SUCCESS)
|
---|
2115 | return rcStrict;
|
---|
2116 | /** @todo check what VT-x and AMD-V does. */
|
---|
2117 | Desc.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
2118 | }
|
---|
2119 |
|
---|
2120 | /* stack */
|
---|
2121 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
2122 | {
|
---|
2123 | uPtrRet.pu16[0] = pVCpu->cpum.GstCtx.ip + cbInstr;
|
---|
2124 | uPtrRet.pu16[1] = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
2125 | }
|
---|
2126 | else if (enmEffOpSize == IEMMODE_32BIT)
|
---|
2127 | {
|
---|
2128 | uPtrRet.pu32[0] = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
2129 | uPtrRet.pu32[1] = pVCpu->cpum.GstCtx.cs.Sel; /** @todo Testcase: What is written to the high word when callf is pushing CS? */
|
---|
2130 | }
|
---|
2131 | else
|
---|
2132 | {
|
---|
2133 | uPtrRet.pu64[0] = pVCpu->cpum.GstCtx.rip + cbInstr;
|
---|
2134 | uPtrRet.pu64[1] = pVCpu->cpum.GstCtx.cs.Sel; /** @todo Testcase: What is written to the high words when callf is pushing CS? */
|
---|
2135 | }
|
---|
2136 | rcStrict = iemMemStackPushCommitSpecial(pVCpu, uPtrRet.pv, uNewRsp);
|
---|
2137 | if (rcStrict != VINF_SUCCESS)
|
---|
2138 | return rcStrict;
|
---|
2139 |
|
---|
2140 | /* commit */
|
---|
2141 | pVCpu->cpum.GstCtx.rip = offSeg;
|
---|
2142 | pVCpu->cpum.GstCtx.cs.Sel = uSel & X86_SEL_MASK_OFF_RPL;
|
---|
2143 | pVCpu->cpum.GstCtx.cs.Sel |= pVCpu->iem.s.uCpl;
|
---|
2144 | pVCpu->cpum.GstCtx.cs.ValidSel = pVCpu->cpum.GstCtx.cs.Sel;
|
---|
2145 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
2146 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&Desc.Legacy);
|
---|
2147 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimit;
|
---|
2148 | pVCpu->cpum.GstCtx.cs.u64Base = u64Base;
|
---|
2149 | pVCpu->iem.s.enmCpuMode = iemCalcCpuMode(pVCpu);
|
---|
2150 | pVCpu->cpum.GstCtx.eflags.Bits.u1RF = 0;
|
---|
2151 | /** @todo check if the hidden bits are loaded correctly for 64-bit
|
---|
2152 | * mode. */
|
---|
2153 |
|
---|
2154 | /* Flush the prefetch buffer. */
|
---|
2155 | #ifdef IEM_WITH_CODE_TLB
|
---|
2156 | pVCpu->iem.s.pbInstrBuf = NULL;
|
---|
2157 | #else
|
---|
2158 | pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
|
---|
2159 | #endif
|
---|
2160 | return VINF_SUCCESS;
|
---|
2161 | }
|
---|
2162 |
|
---|
2163 |
|
---|
2164 | /**
|
---|
2165 | * Implements retf.
|
---|
2166 | *
|
---|
2167 | * @param enmEffOpSize The effective operand size.
|
---|
2168 | * @param cbPop The amount of arguments to pop from the stack
|
---|
2169 | * (bytes).
|
---|
2170 | */
|
---|
2171 | IEM_CIMPL_DEF_2(iemCImpl_retf, IEMMODE, enmEffOpSize, uint16_t, cbPop)
|
---|
2172 | {
|
---|
2173 | VBOXSTRICTRC rcStrict;
|
---|
2174 | RTCPTRUNION uPtrFrame;
|
---|
2175 | uint64_t uNewRsp;
|
---|
2176 | uint64_t uNewRip;
|
---|
2177 | uint16_t uNewCs;
|
---|
2178 | NOREF(cbInstr);
|
---|
2179 |
|
---|
2180 | /*
|
---|
2181 | * Read the stack values first.
|
---|
2182 | */
|
---|
2183 | uint32_t cbRetPtr = enmEffOpSize == IEMMODE_16BIT ? 2+2
|
---|
2184 | : enmEffOpSize == IEMMODE_32BIT ? 4+4 : 8+8;
|
---|
2185 | rcStrict = iemMemStackPopBeginSpecial(pVCpu, cbRetPtr, &uPtrFrame.pv, &uNewRsp);
|
---|
2186 | if (rcStrict != VINF_SUCCESS)
|
---|
2187 | return rcStrict;
|
---|
2188 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
2189 | {
|
---|
2190 | uNewRip = uPtrFrame.pu16[0];
|
---|
2191 | uNewCs = uPtrFrame.pu16[1];
|
---|
2192 | }
|
---|
2193 | else if (enmEffOpSize == IEMMODE_32BIT)
|
---|
2194 | {
|
---|
2195 | uNewRip = uPtrFrame.pu32[0];
|
---|
2196 | uNewCs = uPtrFrame.pu16[2];
|
---|
2197 | }
|
---|
2198 | else
|
---|
2199 | {
|
---|
2200 | uNewRip = uPtrFrame.pu64[0];
|
---|
2201 | uNewCs = uPtrFrame.pu16[4];
|
---|
2202 | }
|
---|
2203 | rcStrict = iemMemStackPopDoneSpecial(pVCpu, uPtrFrame.pv);
|
---|
2204 | if (RT_LIKELY(rcStrict == VINF_SUCCESS))
|
---|
2205 | { /* extremely likely */ }
|
---|
2206 | else
|
---|
2207 | return rcStrict;
|
---|
2208 |
|
---|
2209 | /*
|
---|
2210 | * Real mode and V8086 mode are easy.
|
---|
2211 | */
|
---|
2212 | /** @todo See comment for similar code in iemCImpl_FarJmp */
|
---|
2213 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
2214 | {
|
---|
2215 | Assert(enmEffOpSize == IEMMODE_32BIT || enmEffOpSize == IEMMODE_16BIT);
|
---|
2216 | /** @todo check how this is supposed to work if sp=0xfffe. */
|
---|
2217 |
|
---|
2218 | /* Check the limit of the new EIP. */
|
---|
2219 | /** @todo Intel pseudo code only does the limit check for 16-bit
|
---|
2220 | * operands, AMD does not make any distinction. What is right? */
|
---|
2221 | if (uNewRip > pVCpu->cpum.GstCtx.cs.u32Limit)
|
---|
2222 | return iemRaiseSelectorBounds(pVCpu, X86_SREG_CS, IEM_ACCESS_INSTRUCTION);
|
---|
2223 |
|
---|
2224 | /* commit the operation. */
|
---|
2225 | pVCpu->cpum.GstCtx.rsp = uNewRsp;
|
---|
2226 | pVCpu->cpum.GstCtx.rip = uNewRip;
|
---|
2227 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
|
---|
2228 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
|
---|
2229 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
2230 | pVCpu->cpum.GstCtx.cs.u64Base = (uint32_t)uNewCs << 4;
|
---|
2231 | pVCpu->cpum.GstCtx.eflags.Bits.u1RF = 0;
|
---|
2232 | if (cbPop)
|
---|
2233 | iemRegAddToRsp(pVCpu, cbPop);
|
---|
2234 | return VINF_SUCCESS;
|
---|
2235 | }
|
---|
2236 |
|
---|
2237 | /*
|
---|
2238 | * Protected mode is complicated, of course.
|
---|
2239 | */
|
---|
2240 | if (!(uNewCs & X86_SEL_MASK_OFF_RPL))
|
---|
2241 | {
|
---|
2242 | Log(("retf %04x:%08RX64 -> invalid selector, #GP(0)\n", uNewCs, uNewRip));
|
---|
2243 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
2244 | }
|
---|
2245 |
|
---|
2246 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SREG_MASK | CPUMCTX_EXTRN_GDTR | CPUMCTX_EXTRN_LDTR);
|
---|
2247 |
|
---|
2248 | /* Fetch the descriptor. */
|
---|
2249 | IEMSELDESC DescCs;
|
---|
2250 | rcStrict = iemMemFetchSelDesc(pVCpu, &DescCs, uNewCs, X86_XCPT_GP);
|
---|
2251 | if (rcStrict != VINF_SUCCESS)
|
---|
2252 | return rcStrict;
|
---|
2253 |
|
---|
2254 | /* Can only return to a code selector. */
|
---|
2255 | if ( !DescCs.Legacy.Gen.u1DescType
|
---|
2256 | || !(DescCs.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE) )
|
---|
2257 | {
|
---|
2258 | Log(("retf %04x:%08RX64 -> not a code selector (u1DescType=%u u4Type=%#x).\n",
|
---|
2259 | uNewCs, uNewRip, DescCs.Legacy.Gen.u1DescType, DescCs.Legacy.Gen.u4Type));
|
---|
2260 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
2261 | }
|
---|
2262 |
|
---|
2263 | /* L vs D. */
|
---|
2264 | if ( DescCs.Legacy.Gen.u1Long /** @todo Testcase: far return to a selector with both L and D set. */
|
---|
2265 | && DescCs.Legacy.Gen.u1DefBig
|
---|
2266 | && IEM_IS_LONG_MODE(pVCpu))
|
---|
2267 | {
|
---|
2268 | Log(("retf %04x:%08RX64 -> both L & D set.\n", uNewCs, uNewRip));
|
---|
2269 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
2270 | }
|
---|
2271 |
|
---|
2272 | /* DPL/RPL/CPL checks. */
|
---|
2273 | if ((uNewCs & X86_SEL_RPL) < pVCpu->iem.s.uCpl)
|
---|
2274 | {
|
---|
2275 | Log(("retf %04x:%08RX64 -> RPL < CPL(%d).\n", uNewCs, uNewRip, pVCpu->iem.s.uCpl));
|
---|
2276 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
2277 | }
|
---|
2278 |
|
---|
2279 | if (DescCs.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF)
|
---|
2280 | {
|
---|
2281 | if ((uNewCs & X86_SEL_RPL) < DescCs.Legacy.Gen.u2Dpl)
|
---|
2282 | {
|
---|
2283 | Log(("retf %04x:%08RX64 -> DPL violation (conforming); DPL=%u RPL=%u\n",
|
---|
2284 | uNewCs, uNewRip, DescCs.Legacy.Gen.u2Dpl, (uNewCs & X86_SEL_RPL)));
|
---|
2285 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
2286 | }
|
---|
2287 | }
|
---|
2288 | else
|
---|
2289 | {
|
---|
2290 | if ((uNewCs & X86_SEL_RPL) != DescCs.Legacy.Gen.u2Dpl)
|
---|
2291 | {
|
---|
2292 | Log(("retf %04x:%08RX64 -> RPL != DPL; DPL=%u RPL=%u\n",
|
---|
2293 | uNewCs, uNewRip, DescCs.Legacy.Gen.u2Dpl, (uNewCs & X86_SEL_RPL)));
|
---|
2294 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
2295 | }
|
---|
2296 | }
|
---|
2297 |
|
---|
2298 | /* Is it there? */
|
---|
2299 | if (!DescCs.Legacy.Gen.u1Present)
|
---|
2300 | {
|
---|
2301 | Log(("retf %04x:%08RX64 -> segment not present\n", uNewCs, uNewRip));
|
---|
2302 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uNewCs);
|
---|
2303 | }
|
---|
2304 |
|
---|
2305 | /*
|
---|
2306 | * Return to outer privilege? (We'll typically have entered via a call gate.)
|
---|
2307 | */
|
---|
2308 | if ((uNewCs & X86_SEL_RPL) != pVCpu->iem.s.uCpl)
|
---|
2309 | {
|
---|
2310 | /* Read the outer stack pointer stored *after* the parameters. */
|
---|
2311 | rcStrict = iemMemStackPopContinueSpecial(pVCpu, cbPop + cbRetPtr, &uPtrFrame.pv, &uNewRsp);
|
---|
2312 | if (rcStrict != VINF_SUCCESS)
|
---|
2313 | return rcStrict;
|
---|
2314 |
|
---|
2315 | uPtrFrame.pu8 += cbPop; /* Skip the parameters. */
|
---|
2316 |
|
---|
2317 | uint16_t uNewOuterSs;
|
---|
2318 | uint64_t uNewOuterRsp;
|
---|
2319 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
2320 | {
|
---|
2321 | uNewOuterRsp = uPtrFrame.pu16[0];
|
---|
2322 | uNewOuterSs = uPtrFrame.pu16[1];
|
---|
2323 | }
|
---|
2324 | else if (enmEffOpSize == IEMMODE_32BIT)
|
---|
2325 | {
|
---|
2326 | uNewOuterRsp = uPtrFrame.pu32[0];
|
---|
2327 | uNewOuterSs = uPtrFrame.pu16[2];
|
---|
2328 | }
|
---|
2329 | else
|
---|
2330 | {
|
---|
2331 | uNewOuterRsp = uPtrFrame.pu64[0];
|
---|
2332 | uNewOuterSs = uPtrFrame.pu16[4];
|
---|
2333 | }
|
---|
2334 | uPtrFrame.pu8 -= cbPop; /* Put uPtrFrame back the way it was. */
|
---|
2335 | rcStrict = iemMemStackPopDoneSpecial(pVCpu, uPtrFrame.pv);
|
---|
2336 | if (RT_LIKELY(rcStrict == VINF_SUCCESS))
|
---|
2337 | { /* extremely likely */ }
|
---|
2338 | else
|
---|
2339 | return rcStrict;
|
---|
2340 |
|
---|
2341 | /* Check for NULL stack selector (invalid in ring-3 and non-long mode)
|
---|
2342 | and read the selector. */
|
---|
2343 | IEMSELDESC DescSs;
|
---|
2344 | if (!(uNewOuterSs & X86_SEL_MASK_OFF_RPL))
|
---|
2345 | {
|
---|
2346 | if ( !DescCs.Legacy.Gen.u1Long
|
---|
2347 | || (uNewOuterSs & X86_SEL_RPL) == 3)
|
---|
2348 | {
|
---|
2349 | Log(("retf %04x:%08RX64 %04x:%08RX64 -> invalid stack selector, #GP\n",
|
---|
2350 | uNewCs, uNewRip, uNewOuterSs, uNewOuterRsp));
|
---|
2351 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
2352 | }
|
---|
2353 | /** @todo Testcase: Return far to ring-1 or ring-2 with SS=0. */
|
---|
2354 | iemMemFakeStackSelDesc(&DescSs, (uNewOuterSs & X86_SEL_RPL));
|
---|
2355 | }
|
---|
2356 | else
|
---|
2357 | {
|
---|
2358 | /* Fetch the descriptor for the new stack segment. */
|
---|
2359 | rcStrict = iemMemFetchSelDesc(pVCpu, &DescSs, uNewOuterSs, X86_XCPT_GP);
|
---|
2360 | if (rcStrict != VINF_SUCCESS)
|
---|
2361 | return rcStrict;
|
---|
2362 | }
|
---|
2363 |
|
---|
2364 | /* Check that RPL of stack and code selectors match. */
|
---|
2365 | if ((uNewCs & X86_SEL_RPL) != (uNewOuterSs & X86_SEL_RPL))
|
---|
2366 | {
|
---|
2367 | Log(("retf %04x:%08RX64 %04x:%08RX64 - SS.RPL != CS.RPL -> #GP(SS)\n", uNewCs, uNewRip, uNewOuterSs, uNewOuterRsp));
|
---|
2368 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewOuterSs);
|
---|
2369 | }
|
---|
2370 |
|
---|
2371 | /* Must be a writable data segment. */
|
---|
2372 | if ( !DescSs.Legacy.Gen.u1DescType
|
---|
2373 | || (DescSs.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE)
|
---|
2374 | || !(DescSs.Legacy.Gen.u4Type & X86_SEL_TYPE_WRITE) )
|
---|
2375 | {
|
---|
2376 | Log(("retf %04x:%08RX64 %04x:%08RX64 - SS not a writable data segment (u1DescType=%u u4Type=%#x) -> #GP(SS).\n",
|
---|
2377 | uNewCs, uNewRip, uNewOuterSs, uNewOuterRsp, DescSs.Legacy.Gen.u1DescType, DescSs.Legacy.Gen.u4Type));
|
---|
2378 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewOuterSs);
|
---|
2379 | }
|
---|
2380 |
|
---|
2381 | /* L vs D. (Not mentioned by intel.) */
|
---|
2382 | if ( DescSs.Legacy.Gen.u1Long /** @todo Testcase: far return to a stack selector with both L and D set. */
|
---|
2383 | && DescSs.Legacy.Gen.u1DefBig
|
---|
2384 | && IEM_IS_LONG_MODE(pVCpu))
|
---|
2385 | {
|
---|
2386 | Log(("retf %04x:%08RX64 %04x:%08RX64 - SS has both L & D set -> #GP(SS).\n",
|
---|
2387 | uNewCs, uNewRip, uNewOuterSs, uNewOuterRsp));
|
---|
2388 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewOuterSs);
|
---|
2389 | }
|
---|
2390 |
|
---|
2391 | /* DPL/RPL/CPL checks. */
|
---|
2392 | if (DescSs.Legacy.Gen.u2Dpl != (uNewCs & X86_SEL_RPL))
|
---|
2393 | {
|
---|
2394 | Log(("retf %04x:%08RX64 %04x:%08RX64 - SS.DPL(%u) != CS.RPL (%u) -> #GP(SS).\n",
|
---|
2395 | uNewCs, uNewRip, uNewOuterSs, uNewOuterRsp, DescSs.Legacy.Gen.u2Dpl, uNewCs & X86_SEL_RPL));
|
---|
2396 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewOuterSs);
|
---|
2397 | }
|
---|
2398 |
|
---|
2399 | /* Is it there? */
|
---|
2400 | if (!DescSs.Legacy.Gen.u1Present)
|
---|
2401 | {
|
---|
2402 | Log(("retf %04x:%08RX64 %04x:%08RX64 - SS not present -> #NP(SS).\n", uNewCs, uNewRip, uNewOuterSs, uNewOuterRsp));
|
---|
2403 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uNewCs);
|
---|
2404 | }
|
---|
2405 |
|
---|
2406 | /* Calc SS limit.*/
|
---|
2407 | uint32_t cbLimitSs = X86DESC_LIMIT_G(&DescSs.Legacy);
|
---|
2408 |
|
---|
2409 | /* Is RIP canonical or within CS.limit? */
|
---|
2410 | uint64_t u64Base;
|
---|
2411 | uint32_t cbLimitCs = X86DESC_LIMIT_G(&DescCs.Legacy);
|
---|
2412 |
|
---|
2413 | /** @todo Testcase: Is this correct? */
|
---|
2414 | if ( DescCs.Legacy.Gen.u1Long
|
---|
2415 | && IEM_IS_LONG_MODE(pVCpu) )
|
---|
2416 | {
|
---|
2417 | if (!IEM_IS_CANONICAL(uNewRip))
|
---|
2418 | {
|
---|
2419 | Log(("retf %04x:%08RX64 %04x:%08RX64 - not canonical -> #GP.\n", uNewCs, uNewRip, uNewOuterSs, uNewOuterRsp));
|
---|
2420 | return iemRaiseNotCanonical(pVCpu);
|
---|
2421 | }
|
---|
2422 | u64Base = 0;
|
---|
2423 | }
|
---|
2424 | else
|
---|
2425 | {
|
---|
2426 | if (uNewRip > cbLimitCs)
|
---|
2427 | {
|
---|
2428 | Log(("retf %04x:%08RX64 %04x:%08RX64 - out of bounds (%#x)-> #GP(CS).\n",
|
---|
2429 | uNewCs, uNewRip, uNewOuterSs, uNewOuterRsp, cbLimitCs));
|
---|
2430 | /** @todo: Intel says this is #GP(0)! */
|
---|
2431 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
2432 | }
|
---|
2433 | u64Base = X86DESC_BASE(&DescCs.Legacy);
|
---|
2434 | }
|
---|
2435 |
|
---|
2436 | /*
|
---|
2437 | * Now set the accessed bit before
|
---|
2438 | * writing the return address to the stack and committing the result into
|
---|
2439 | * CS, CSHID and RIP.
|
---|
2440 | */
|
---|
2441 | /** @todo Testcase: Need to check WHEN exactly the CS accessed bit is set. */
|
---|
2442 | if (!(DescCs.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
2443 | {
|
---|
2444 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewCs);
|
---|
2445 | if (rcStrict != VINF_SUCCESS)
|
---|
2446 | return rcStrict;
|
---|
2447 | /** @todo check what VT-x and AMD-V does. */
|
---|
2448 | DescCs.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
2449 | }
|
---|
2450 | /** @todo Testcase: Need to check WHEN exactly the SS accessed bit is set. */
|
---|
2451 | if (!(DescSs.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
2452 | {
|
---|
2453 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewOuterSs);
|
---|
2454 | if (rcStrict != VINF_SUCCESS)
|
---|
2455 | return rcStrict;
|
---|
2456 | /** @todo check what VT-x and AMD-V does. */
|
---|
2457 | DescSs.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
2458 | }
|
---|
2459 |
|
---|
2460 | /* commit */
|
---|
2461 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
2462 | 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... */
|
---|
2463 | else
|
---|
2464 | pVCpu->cpum.GstCtx.rip = uNewRip;
|
---|
2465 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
|
---|
2466 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
|
---|
2467 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
2468 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCs.Legacy);
|
---|
2469 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimitCs;
|
---|
2470 | pVCpu->cpum.GstCtx.cs.u64Base = u64Base;
|
---|
2471 | pVCpu->iem.s.enmCpuMode = iemCalcCpuMode(pVCpu);
|
---|
2472 | pVCpu->cpum.GstCtx.ss.Sel = uNewOuterSs;
|
---|
2473 | pVCpu->cpum.GstCtx.ss.ValidSel = uNewOuterSs;
|
---|
2474 | pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
2475 | pVCpu->cpum.GstCtx.ss.Attr.u = X86DESC_GET_HID_ATTR(&DescSs.Legacy);
|
---|
2476 | pVCpu->cpum.GstCtx.ss.u32Limit = cbLimitSs;
|
---|
2477 | if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
|
---|
2478 | pVCpu->cpum.GstCtx.ss.u64Base = 0;
|
---|
2479 | else
|
---|
2480 | pVCpu->cpum.GstCtx.ss.u64Base = X86DESC_BASE(&DescSs.Legacy);
|
---|
2481 | if (!pVCpu->cpum.GstCtx.ss.Attr.n.u1DefBig)
|
---|
2482 | pVCpu->cpum.GstCtx.sp = (uint16_t)uNewOuterRsp;
|
---|
2483 | else
|
---|
2484 | pVCpu->cpum.GstCtx.rsp = uNewOuterRsp;
|
---|
2485 |
|
---|
2486 | pVCpu->iem.s.uCpl = (uNewCs & X86_SEL_RPL);
|
---|
2487 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.ds);
|
---|
2488 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.es);
|
---|
2489 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.fs);
|
---|
2490 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.gs);
|
---|
2491 |
|
---|
2492 | /** @todo check if the hidden bits are loaded correctly for 64-bit
|
---|
2493 | * mode. */
|
---|
2494 |
|
---|
2495 | if (cbPop)
|
---|
2496 | iemRegAddToRsp(pVCpu, cbPop);
|
---|
2497 | pVCpu->cpum.GstCtx.eflags.Bits.u1RF = 0;
|
---|
2498 |
|
---|
2499 | /* Done! */
|
---|
2500 | }
|
---|
2501 | /*
|
---|
2502 | * Return to the same privilege level
|
---|
2503 | */
|
---|
2504 | else
|
---|
2505 | {
|
---|
2506 | /* Limit / canonical check. */
|
---|
2507 | uint64_t u64Base;
|
---|
2508 | uint32_t cbLimitCs = X86DESC_LIMIT_G(&DescCs.Legacy);
|
---|
2509 |
|
---|
2510 | /** @todo Testcase: Is this correct? */
|
---|
2511 | if ( DescCs.Legacy.Gen.u1Long
|
---|
2512 | && IEM_IS_LONG_MODE(pVCpu) )
|
---|
2513 | {
|
---|
2514 | if (!IEM_IS_CANONICAL(uNewRip))
|
---|
2515 | {
|
---|
2516 | Log(("retf %04x:%08RX64 - not canonical -> #GP\n", uNewCs, uNewRip));
|
---|
2517 | return iemRaiseNotCanonical(pVCpu);
|
---|
2518 | }
|
---|
2519 | u64Base = 0;
|
---|
2520 | }
|
---|
2521 | else
|
---|
2522 | {
|
---|
2523 | if (uNewRip > cbLimitCs)
|
---|
2524 | {
|
---|
2525 | Log(("retf %04x:%08RX64 -> out of bounds (%#x)\n", uNewCs, uNewRip, cbLimitCs));
|
---|
2526 | /** @todo: Intel says this is #GP(0)! */
|
---|
2527 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
2528 | }
|
---|
2529 | u64Base = X86DESC_BASE(&DescCs.Legacy);
|
---|
2530 | }
|
---|
2531 |
|
---|
2532 | /*
|
---|
2533 | * Now set the accessed bit before
|
---|
2534 | * writing the return address to the stack and committing the result into
|
---|
2535 | * CS, CSHID and RIP.
|
---|
2536 | */
|
---|
2537 | /** @todo Testcase: Need to check WHEN exactly the accessed bit is set. */
|
---|
2538 | if (!(DescCs.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
2539 | {
|
---|
2540 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewCs);
|
---|
2541 | if (rcStrict != VINF_SUCCESS)
|
---|
2542 | return rcStrict;
|
---|
2543 | /** @todo check what VT-x and AMD-V does. */
|
---|
2544 | DescCs.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
2545 | }
|
---|
2546 |
|
---|
2547 | /* commit */
|
---|
2548 | if (!pVCpu->cpum.GstCtx.ss.Attr.n.u1DefBig)
|
---|
2549 | pVCpu->cpum.GstCtx.sp = (uint16_t)uNewRsp;
|
---|
2550 | else
|
---|
2551 | pVCpu->cpum.GstCtx.rsp = uNewRsp;
|
---|
2552 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
2553 | 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... */
|
---|
2554 | else
|
---|
2555 | pVCpu->cpum.GstCtx.rip = uNewRip;
|
---|
2556 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
|
---|
2557 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
|
---|
2558 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
2559 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCs.Legacy);
|
---|
2560 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimitCs;
|
---|
2561 | pVCpu->cpum.GstCtx.cs.u64Base = u64Base;
|
---|
2562 | /** @todo check if the hidden bits are loaded correctly for 64-bit
|
---|
2563 | * mode. */
|
---|
2564 | pVCpu->iem.s.enmCpuMode = iemCalcCpuMode(pVCpu);
|
---|
2565 | if (cbPop)
|
---|
2566 | iemRegAddToRsp(pVCpu, cbPop);
|
---|
2567 | pVCpu->cpum.GstCtx.eflags.Bits.u1RF = 0;
|
---|
2568 | }
|
---|
2569 |
|
---|
2570 | /* Flush the prefetch buffer. */
|
---|
2571 | #ifdef IEM_WITH_CODE_TLB
|
---|
2572 | pVCpu->iem.s.pbInstrBuf = NULL;
|
---|
2573 | #else
|
---|
2574 | pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
|
---|
2575 | #endif
|
---|
2576 | return VINF_SUCCESS;
|
---|
2577 | }
|
---|
2578 |
|
---|
2579 |
|
---|
2580 | /**
|
---|
2581 | * Implements retn.
|
---|
2582 | *
|
---|
2583 | * We're doing this in C because of the \#GP that might be raised if the popped
|
---|
2584 | * program counter is out of bounds.
|
---|
2585 | *
|
---|
2586 | * @param enmEffOpSize The effective operand size.
|
---|
2587 | * @param cbPop The amount of arguments to pop from the stack
|
---|
2588 | * (bytes).
|
---|
2589 | */
|
---|
2590 | IEM_CIMPL_DEF_2(iemCImpl_retn, IEMMODE, enmEffOpSize, uint16_t, cbPop)
|
---|
2591 | {
|
---|
2592 | NOREF(cbInstr);
|
---|
2593 |
|
---|
2594 | /* Fetch the RSP from the stack. */
|
---|
2595 | VBOXSTRICTRC rcStrict;
|
---|
2596 | RTUINT64U NewRip;
|
---|
2597 | RTUINT64U NewRsp;
|
---|
2598 | NewRsp.u = pVCpu->cpum.GstCtx.rsp;
|
---|
2599 |
|
---|
2600 | switch (enmEffOpSize)
|
---|
2601 | {
|
---|
2602 | case IEMMODE_16BIT:
|
---|
2603 | NewRip.u = 0;
|
---|
2604 | rcStrict = iemMemStackPopU16Ex(pVCpu, &NewRip.Words.w0, &NewRsp);
|
---|
2605 | break;
|
---|
2606 | case IEMMODE_32BIT:
|
---|
2607 | NewRip.u = 0;
|
---|
2608 | rcStrict = iemMemStackPopU32Ex(pVCpu, &NewRip.DWords.dw0, &NewRsp);
|
---|
2609 | break;
|
---|
2610 | case IEMMODE_64BIT:
|
---|
2611 | rcStrict = iemMemStackPopU64Ex(pVCpu, &NewRip.u, &NewRsp);
|
---|
2612 | break;
|
---|
2613 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
2614 | }
|
---|
2615 | if (rcStrict != VINF_SUCCESS)
|
---|
2616 | return rcStrict;
|
---|
2617 |
|
---|
2618 | /* Check the new RSP before loading it. */
|
---|
2619 | /** @todo Should test this as the intel+amd pseudo code doesn't mention half
|
---|
2620 | * of it. The canonical test is performed here and for call. */
|
---|
2621 | if (enmEffOpSize != IEMMODE_64BIT)
|
---|
2622 | {
|
---|
2623 | if (NewRip.DWords.dw0 > pVCpu->cpum.GstCtx.cs.u32Limit)
|
---|
2624 | {
|
---|
2625 | Log(("retn newrip=%llx - out of bounds (%x) -> #GP\n", NewRip.u, pVCpu->cpum.GstCtx.cs.u32Limit));
|
---|
2626 | return iemRaiseSelectorBounds(pVCpu, X86_SREG_CS, IEM_ACCESS_INSTRUCTION);
|
---|
2627 | }
|
---|
2628 | }
|
---|
2629 | else
|
---|
2630 | {
|
---|
2631 | if (!IEM_IS_CANONICAL(NewRip.u))
|
---|
2632 | {
|
---|
2633 | Log(("retn newrip=%llx - not canonical -> #GP\n", NewRip.u));
|
---|
2634 | return iemRaiseNotCanonical(pVCpu);
|
---|
2635 | }
|
---|
2636 | }
|
---|
2637 |
|
---|
2638 | /* Apply cbPop */
|
---|
2639 | if (cbPop)
|
---|
2640 | iemRegAddToRspEx(pVCpu, &NewRsp, cbPop);
|
---|
2641 |
|
---|
2642 | /* Commit it. */
|
---|
2643 | pVCpu->cpum.GstCtx.rip = NewRip.u;
|
---|
2644 | pVCpu->cpum.GstCtx.rsp = NewRsp.u;
|
---|
2645 | pVCpu->cpum.GstCtx.eflags.Bits.u1RF = 0;
|
---|
2646 |
|
---|
2647 | /* Flush the prefetch buffer. */
|
---|
2648 | #ifndef IEM_WITH_CODE_TLB
|
---|
2649 | pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
|
---|
2650 | #endif
|
---|
2651 |
|
---|
2652 | return VINF_SUCCESS;
|
---|
2653 | }
|
---|
2654 |
|
---|
2655 |
|
---|
2656 | /**
|
---|
2657 | * Implements enter.
|
---|
2658 | *
|
---|
2659 | * We're doing this in C because the instruction is insane, even for the
|
---|
2660 | * u8NestingLevel=0 case dealing with the stack is tedious.
|
---|
2661 | *
|
---|
2662 | * @param enmEffOpSize The effective operand size.
|
---|
2663 | */
|
---|
2664 | IEM_CIMPL_DEF_3(iemCImpl_enter, IEMMODE, enmEffOpSize, uint16_t, cbFrame, uint8_t, cParameters)
|
---|
2665 | {
|
---|
2666 | /* Push RBP, saving the old value in TmpRbp. */
|
---|
2667 | RTUINT64U NewRsp; NewRsp.u = pVCpu->cpum.GstCtx.rsp;
|
---|
2668 | RTUINT64U TmpRbp; TmpRbp.u = pVCpu->cpum.GstCtx.rbp;
|
---|
2669 | RTUINT64U NewRbp;
|
---|
2670 | VBOXSTRICTRC rcStrict;
|
---|
2671 | if (enmEffOpSize == IEMMODE_64BIT)
|
---|
2672 | {
|
---|
2673 | rcStrict = iemMemStackPushU64Ex(pVCpu, TmpRbp.u, &NewRsp);
|
---|
2674 | NewRbp = NewRsp;
|
---|
2675 | }
|
---|
2676 | else if (enmEffOpSize == IEMMODE_32BIT)
|
---|
2677 | {
|
---|
2678 | rcStrict = iemMemStackPushU32Ex(pVCpu, TmpRbp.DWords.dw0, &NewRsp);
|
---|
2679 | NewRbp = NewRsp;
|
---|
2680 | }
|
---|
2681 | else
|
---|
2682 | {
|
---|
2683 | rcStrict = iemMemStackPushU16Ex(pVCpu, TmpRbp.Words.w0, &NewRsp);
|
---|
2684 | NewRbp = TmpRbp;
|
---|
2685 | NewRbp.Words.w0 = NewRsp.Words.w0;
|
---|
2686 | }
|
---|
2687 | if (rcStrict != VINF_SUCCESS)
|
---|
2688 | return rcStrict;
|
---|
2689 |
|
---|
2690 | /* Copy the parameters (aka nesting levels by Intel). */
|
---|
2691 | cParameters &= 0x1f;
|
---|
2692 | if (cParameters > 0)
|
---|
2693 | {
|
---|
2694 | switch (enmEffOpSize)
|
---|
2695 | {
|
---|
2696 | case IEMMODE_16BIT:
|
---|
2697 | if (pVCpu->cpum.GstCtx.ss.Attr.n.u1DefBig)
|
---|
2698 | TmpRbp.DWords.dw0 -= 2;
|
---|
2699 | else
|
---|
2700 | TmpRbp.Words.w0 -= 2;
|
---|
2701 | do
|
---|
2702 | {
|
---|
2703 | uint16_t u16Tmp;
|
---|
2704 | rcStrict = iemMemStackPopU16Ex(pVCpu, &u16Tmp, &TmpRbp);
|
---|
2705 | if (rcStrict != VINF_SUCCESS)
|
---|
2706 | break;
|
---|
2707 | rcStrict = iemMemStackPushU16Ex(pVCpu, u16Tmp, &NewRsp);
|
---|
2708 | } while (--cParameters > 0 && rcStrict == VINF_SUCCESS);
|
---|
2709 | break;
|
---|
2710 |
|
---|
2711 | case IEMMODE_32BIT:
|
---|
2712 | if (pVCpu->cpum.GstCtx.ss.Attr.n.u1DefBig)
|
---|
2713 | TmpRbp.DWords.dw0 -= 4;
|
---|
2714 | else
|
---|
2715 | TmpRbp.Words.w0 -= 4;
|
---|
2716 | do
|
---|
2717 | {
|
---|
2718 | uint32_t u32Tmp;
|
---|
2719 | rcStrict = iemMemStackPopU32Ex(pVCpu, &u32Tmp, &TmpRbp);
|
---|
2720 | if (rcStrict != VINF_SUCCESS)
|
---|
2721 | break;
|
---|
2722 | rcStrict = iemMemStackPushU32Ex(pVCpu, u32Tmp, &NewRsp);
|
---|
2723 | } while (--cParameters > 0 && rcStrict == VINF_SUCCESS);
|
---|
2724 | break;
|
---|
2725 |
|
---|
2726 | case IEMMODE_64BIT:
|
---|
2727 | TmpRbp.u -= 8;
|
---|
2728 | do
|
---|
2729 | {
|
---|
2730 | uint64_t u64Tmp;
|
---|
2731 | rcStrict = iemMemStackPopU64Ex(pVCpu, &u64Tmp, &TmpRbp);
|
---|
2732 | if (rcStrict != VINF_SUCCESS)
|
---|
2733 | break;
|
---|
2734 | rcStrict = iemMemStackPushU64Ex(pVCpu, u64Tmp, &NewRsp);
|
---|
2735 | } while (--cParameters > 0 && rcStrict == VINF_SUCCESS);
|
---|
2736 | break;
|
---|
2737 |
|
---|
2738 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
2739 | }
|
---|
2740 | if (rcStrict != VINF_SUCCESS)
|
---|
2741 | return VINF_SUCCESS;
|
---|
2742 |
|
---|
2743 | /* Push the new RBP */
|
---|
2744 | if (enmEffOpSize == IEMMODE_64BIT)
|
---|
2745 | rcStrict = iemMemStackPushU64Ex(pVCpu, NewRbp.u, &NewRsp);
|
---|
2746 | else if (enmEffOpSize == IEMMODE_32BIT)
|
---|
2747 | rcStrict = iemMemStackPushU32Ex(pVCpu, NewRbp.DWords.dw0, &NewRsp);
|
---|
2748 | else
|
---|
2749 | rcStrict = iemMemStackPushU16Ex(pVCpu, NewRbp.Words.w0, &NewRsp);
|
---|
2750 | if (rcStrict != VINF_SUCCESS)
|
---|
2751 | return rcStrict;
|
---|
2752 |
|
---|
2753 | }
|
---|
2754 |
|
---|
2755 | /* Recalc RSP. */
|
---|
2756 | iemRegSubFromRspEx(pVCpu, &NewRsp, cbFrame);
|
---|
2757 |
|
---|
2758 | /** @todo Should probe write access at the new RSP according to AMD. */
|
---|
2759 |
|
---|
2760 | /* Commit it. */
|
---|
2761 | pVCpu->cpum.GstCtx.rbp = NewRbp.u;
|
---|
2762 | pVCpu->cpum.GstCtx.rsp = NewRsp.u;
|
---|
2763 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
2764 |
|
---|
2765 | return VINF_SUCCESS;
|
---|
2766 | }
|
---|
2767 |
|
---|
2768 |
|
---|
2769 |
|
---|
2770 | /**
|
---|
2771 | * Implements leave.
|
---|
2772 | *
|
---|
2773 | * We're doing this in C because messing with the stack registers is annoying
|
---|
2774 | * since they depends on SS attributes.
|
---|
2775 | *
|
---|
2776 | * @param enmEffOpSize The effective operand size.
|
---|
2777 | */
|
---|
2778 | IEM_CIMPL_DEF_1(iemCImpl_leave, IEMMODE, enmEffOpSize)
|
---|
2779 | {
|
---|
2780 | /* Calculate the intermediate RSP from RBP and the stack attributes. */
|
---|
2781 | RTUINT64U NewRsp;
|
---|
2782 | if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
|
---|
2783 | NewRsp.u = pVCpu->cpum.GstCtx.rbp;
|
---|
2784 | else if (pVCpu->cpum.GstCtx.ss.Attr.n.u1DefBig)
|
---|
2785 | NewRsp.u = pVCpu->cpum.GstCtx.ebp;
|
---|
2786 | else
|
---|
2787 | {
|
---|
2788 | /** @todo Check that LEAVE actually preserve the high EBP bits. */
|
---|
2789 | NewRsp.u = pVCpu->cpum.GstCtx.rsp;
|
---|
2790 | NewRsp.Words.w0 = pVCpu->cpum.GstCtx.bp;
|
---|
2791 | }
|
---|
2792 |
|
---|
2793 | /* Pop RBP according to the operand size. */
|
---|
2794 | VBOXSTRICTRC rcStrict;
|
---|
2795 | RTUINT64U NewRbp;
|
---|
2796 | switch (enmEffOpSize)
|
---|
2797 | {
|
---|
2798 | case IEMMODE_16BIT:
|
---|
2799 | NewRbp.u = pVCpu->cpum.GstCtx.rbp;
|
---|
2800 | rcStrict = iemMemStackPopU16Ex(pVCpu, &NewRbp.Words.w0, &NewRsp);
|
---|
2801 | break;
|
---|
2802 | case IEMMODE_32BIT:
|
---|
2803 | NewRbp.u = 0;
|
---|
2804 | rcStrict = iemMemStackPopU32Ex(pVCpu, &NewRbp.DWords.dw0, &NewRsp);
|
---|
2805 | break;
|
---|
2806 | case IEMMODE_64BIT:
|
---|
2807 | rcStrict = iemMemStackPopU64Ex(pVCpu, &NewRbp.u, &NewRsp);
|
---|
2808 | break;
|
---|
2809 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
2810 | }
|
---|
2811 | if (rcStrict != VINF_SUCCESS)
|
---|
2812 | return rcStrict;
|
---|
2813 |
|
---|
2814 |
|
---|
2815 | /* Commit it. */
|
---|
2816 | pVCpu->cpum.GstCtx.rbp = NewRbp.u;
|
---|
2817 | pVCpu->cpum.GstCtx.rsp = NewRsp.u;
|
---|
2818 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
2819 |
|
---|
2820 | return VINF_SUCCESS;
|
---|
2821 | }
|
---|
2822 |
|
---|
2823 |
|
---|
2824 | /**
|
---|
2825 | * Implements int3 and int XX.
|
---|
2826 | *
|
---|
2827 | * @param u8Int The interrupt vector number.
|
---|
2828 | * @param enmInt The int instruction type.
|
---|
2829 | */
|
---|
2830 | IEM_CIMPL_DEF_2(iemCImpl_int, uint8_t, u8Int, IEMINT, enmInt)
|
---|
2831 | {
|
---|
2832 | Assert(pVCpu->iem.s.cXcptRecursions == 0);
|
---|
2833 | return iemRaiseXcptOrInt(pVCpu,
|
---|
2834 | cbInstr,
|
---|
2835 | u8Int,
|
---|
2836 | IEM_XCPT_FLAGS_T_SOFT_INT | enmInt,
|
---|
2837 | 0,
|
---|
2838 | 0);
|
---|
2839 | }
|
---|
2840 |
|
---|
2841 |
|
---|
2842 | /**
|
---|
2843 | * Implements iret for real mode and V8086 mode.
|
---|
2844 | *
|
---|
2845 | * @param enmEffOpSize The effective operand size.
|
---|
2846 | */
|
---|
2847 | IEM_CIMPL_DEF_1(iemCImpl_iret_real_v8086, IEMMODE, enmEffOpSize)
|
---|
2848 | {
|
---|
2849 | X86EFLAGS Efl;
|
---|
2850 | Efl.u = IEMMISC_GET_EFL(pVCpu);
|
---|
2851 | NOREF(cbInstr);
|
---|
2852 |
|
---|
2853 | /*
|
---|
2854 | * iret throws an exception if VME isn't enabled.
|
---|
2855 | */
|
---|
2856 | if ( Efl.Bits.u1VM
|
---|
2857 | && Efl.Bits.u2IOPL != 3
|
---|
2858 | && !(pVCpu->cpum.GstCtx.cr4 & X86_CR4_VME))
|
---|
2859 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
2860 |
|
---|
2861 | /*
|
---|
2862 | * Do the stack bits, but don't commit RSP before everything checks
|
---|
2863 | * out right.
|
---|
2864 | */
|
---|
2865 | Assert(enmEffOpSize == IEMMODE_32BIT || enmEffOpSize == IEMMODE_16BIT);
|
---|
2866 | VBOXSTRICTRC rcStrict;
|
---|
2867 | RTCPTRUNION uFrame;
|
---|
2868 | uint16_t uNewCs;
|
---|
2869 | uint32_t uNewEip;
|
---|
2870 | uint32_t uNewFlags;
|
---|
2871 | uint64_t uNewRsp;
|
---|
2872 | if (enmEffOpSize == IEMMODE_32BIT)
|
---|
2873 | {
|
---|
2874 | rcStrict = iemMemStackPopBeginSpecial(pVCpu, 12, &uFrame.pv, &uNewRsp);
|
---|
2875 | if (rcStrict != VINF_SUCCESS)
|
---|
2876 | return rcStrict;
|
---|
2877 | uNewEip = uFrame.pu32[0];
|
---|
2878 | if (uNewEip > UINT16_MAX)
|
---|
2879 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
2880 |
|
---|
2881 | uNewCs = (uint16_t)uFrame.pu32[1];
|
---|
2882 | uNewFlags = uFrame.pu32[2];
|
---|
2883 | uNewFlags &= X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF
|
---|
2884 | | X86_EFL_TF | X86_EFL_IF | X86_EFL_DF | X86_EFL_OF | X86_EFL_IOPL | X86_EFL_NT
|
---|
2885 | | X86_EFL_RF /*| X86_EFL_VM*/ | X86_EFL_AC /*|X86_EFL_VIF*/ /*|X86_EFL_VIP*/
|
---|
2886 | | X86_EFL_ID;
|
---|
2887 | if (IEM_GET_TARGET_CPU(pVCpu) <= IEMTARGETCPU_386)
|
---|
2888 | uNewFlags &= ~(X86_EFL_AC | X86_EFL_ID | X86_EFL_VIF | X86_EFL_VIP);
|
---|
2889 | uNewFlags |= Efl.u & (X86_EFL_VM | X86_EFL_VIF | X86_EFL_VIP | X86_EFL_1);
|
---|
2890 | }
|
---|
2891 | else
|
---|
2892 | {
|
---|
2893 | rcStrict = iemMemStackPopBeginSpecial(pVCpu, 6, &uFrame.pv, &uNewRsp);
|
---|
2894 | if (rcStrict != VINF_SUCCESS)
|
---|
2895 | return rcStrict;
|
---|
2896 | uNewEip = uFrame.pu16[0];
|
---|
2897 | uNewCs = uFrame.pu16[1];
|
---|
2898 | uNewFlags = uFrame.pu16[2];
|
---|
2899 | uNewFlags &= X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF
|
---|
2900 | | X86_EFL_TF | X86_EFL_IF | X86_EFL_DF | X86_EFL_OF | X86_EFL_IOPL | X86_EFL_NT;
|
---|
2901 | uNewFlags |= Efl.u & ((UINT32_C(0xffff0000) | X86_EFL_1) & ~X86_EFL_RF);
|
---|
2902 | /** @todo The intel pseudo code does not indicate what happens to
|
---|
2903 | * reserved flags. We just ignore them. */
|
---|
2904 | /* Ancient CPU adjustments: See iemCImpl_popf. */
|
---|
2905 | if (IEM_GET_TARGET_CPU(pVCpu) == IEMTARGETCPU_286)
|
---|
2906 | uNewFlags &= ~(X86_EFL_NT | X86_EFL_IOPL);
|
---|
2907 | }
|
---|
2908 | rcStrict = iemMemStackPopDoneSpecial(pVCpu, uFrame.pv);
|
---|
2909 | if (RT_LIKELY(rcStrict == VINF_SUCCESS))
|
---|
2910 | { /* extremely likely */ }
|
---|
2911 | else
|
---|
2912 | return rcStrict;
|
---|
2913 |
|
---|
2914 | /** @todo Check how this is supposed to work if sp=0xfffe. */
|
---|
2915 | Log7(("iemCImpl_iret_real_v8086: uNewCs=%#06x uNewRip=%#010x uNewFlags=%#x uNewRsp=%#18llx\n",
|
---|
2916 | uNewCs, uNewEip, uNewFlags, uNewRsp));
|
---|
2917 |
|
---|
2918 | /*
|
---|
2919 | * Check the limit of the new EIP.
|
---|
2920 | */
|
---|
2921 | /** @todo Only the AMD pseudo code check the limit here, what's
|
---|
2922 | * right? */
|
---|
2923 | if (uNewEip > pVCpu->cpum.GstCtx.cs.u32Limit)
|
---|
2924 | return iemRaiseSelectorBounds(pVCpu, X86_SREG_CS, IEM_ACCESS_INSTRUCTION);
|
---|
2925 |
|
---|
2926 | /*
|
---|
2927 | * V8086 checks and flag adjustments
|
---|
2928 | */
|
---|
2929 | if (Efl.Bits.u1VM)
|
---|
2930 | {
|
---|
2931 | if (Efl.Bits.u2IOPL == 3)
|
---|
2932 | {
|
---|
2933 | /* Preserve IOPL and clear RF. */
|
---|
2934 | uNewFlags &= ~(X86_EFL_IOPL | X86_EFL_RF);
|
---|
2935 | uNewFlags |= Efl.u & (X86_EFL_IOPL);
|
---|
2936 | }
|
---|
2937 | else if ( enmEffOpSize == IEMMODE_16BIT
|
---|
2938 | && ( !(uNewFlags & X86_EFL_IF)
|
---|
2939 | || !Efl.Bits.u1VIP )
|
---|
2940 | && !(uNewFlags & X86_EFL_TF) )
|
---|
2941 | {
|
---|
2942 | /* Move IF to VIF, clear RF and preserve IF and IOPL.*/
|
---|
2943 | uNewFlags &= ~X86_EFL_VIF;
|
---|
2944 | uNewFlags |= (uNewFlags & X86_EFL_IF) << (19 - 9);
|
---|
2945 | uNewFlags &= ~(X86_EFL_IF | X86_EFL_IOPL | X86_EFL_RF);
|
---|
2946 | uNewFlags |= Efl.u & (X86_EFL_IF | X86_EFL_IOPL);
|
---|
2947 | }
|
---|
2948 | else
|
---|
2949 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
2950 | Log7(("iemCImpl_iret_real_v8086: u1VM=1: adjusted uNewFlags=%#x\n", uNewFlags));
|
---|
2951 | }
|
---|
2952 |
|
---|
2953 | /*
|
---|
2954 | * Commit the operation.
|
---|
2955 | */
|
---|
2956 | #ifdef DBGFTRACE_ENABLED
|
---|
2957 | RTTraceBufAddMsgF(pVCpu->CTX_SUFF(pVM)->CTX_SUFF(hTraceBuf), "iret/rm %04x:%04x -> %04x:%04x %x %04llx",
|
---|
2958 | pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.eip, uNewCs, uNewEip, uNewFlags, uNewRsp);
|
---|
2959 | #endif
|
---|
2960 | pVCpu->cpum.GstCtx.rsp = uNewRsp;
|
---|
2961 | pVCpu->cpum.GstCtx.rip = uNewEip;
|
---|
2962 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
|
---|
2963 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
|
---|
2964 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
2965 | pVCpu->cpum.GstCtx.cs.u64Base = (uint32_t)uNewCs << 4;
|
---|
2966 | /** @todo do we load attribs and limit as well? */
|
---|
2967 | Assert(uNewFlags & X86_EFL_1);
|
---|
2968 | IEMMISC_SET_EFL(pVCpu, uNewFlags);
|
---|
2969 |
|
---|
2970 | /* Flush the prefetch buffer. */
|
---|
2971 | #ifdef IEM_WITH_CODE_TLB
|
---|
2972 | pVCpu->iem.s.pbInstrBuf = NULL;
|
---|
2973 | #else
|
---|
2974 | pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
|
---|
2975 | #endif
|
---|
2976 |
|
---|
2977 | return VINF_SUCCESS;
|
---|
2978 | }
|
---|
2979 |
|
---|
2980 |
|
---|
2981 | /**
|
---|
2982 | * Loads a segment register when entering V8086 mode.
|
---|
2983 | *
|
---|
2984 | * @param pSReg The segment register.
|
---|
2985 | * @param uSeg The segment to load.
|
---|
2986 | */
|
---|
2987 | static void iemCImplCommonV8086LoadSeg(PCPUMSELREG pSReg, uint16_t uSeg)
|
---|
2988 | {
|
---|
2989 | pSReg->Sel = uSeg;
|
---|
2990 | pSReg->ValidSel = uSeg;
|
---|
2991 | pSReg->fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
2992 | pSReg->u64Base = (uint32_t)uSeg << 4;
|
---|
2993 | pSReg->u32Limit = 0xffff;
|
---|
2994 | pSReg->Attr.u = X86_SEL_TYPE_RW_ACC | RT_BIT(4) /*!sys*/ | RT_BIT(7) /*P*/ | (3 /*DPL*/ << 5); /* VT-x wants 0xf3 */
|
---|
2995 | /** @todo Testcase: Check if VT-x really needs this and what it does itself when
|
---|
2996 | * IRET'ing to V8086. */
|
---|
2997 | }
|
---|
2998 |
|
---|
2999 |
|
---|
3000 | /**
|
---|
3001 | * Implements iret for protected mode returning to V8086 mode.
|
---|
3002 | *
|
---|
3003 | * @param uNewEip The new EIP.
|
---|
3004 | * @param uNewCs The new CS.
|
---|
3005 | * @param uNewFlags The new EFLAGS.
|
---|
3006 | * @param uNewRsp The RSP after the initial IRET frame.
|
---|
3007 | *
|
---|
3008 | * @note This can only be a 32-bit iret du to the X86_EFL_VM position.
|
---|
3009 | */
|
---|
3010 | IEM_CIMPL_DEF_4(iemCImpl_iret_prot_v8086, uint32_t, uNewEip, uint16_t, uNewCs, uint32_t, uNewFlags, uint64_t, uNewRsp)
|
---|
3011 | {
|
---|
3012 | RT_NOREF_PV(cbInstr);
|
---|
3013 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SREG_MASK);
|
---|
3014 |
|
---|
3015 | /*
|
---|
3016 | * Pop the V8086 specific frame bits off the stack.
|
---|
3017 | */
|
---|
3018 | VBOXSTRICTRC rcStrict;
|
---|
3019 | RTCPTRUNION uFrame;
|
---|
3020 | rcStrict = iemMemStackPopContinueSpecial(pVCpu, 24, &uFrame.pv, &uNewRsp);
|
---|
3021 | if (rcStrict != VINF_SUCCESS)
|
---|
3022 | return rcStrict;
|
---|
3023 | uint32_t uNewEsp = uFrame.pu32[0];
|
---|
3024 | uint16_t uNewSs = uFrame.pu32[1];
|
---|
3025 | uint16_t uNewEs = uFrame.pu32[2];
|
---|
3026 | uint16_t uNewDs = uFrame.pu32[3];
|
---|
3027 | uint16_t uNewFs = uFrame.pu32[4];
|
---|
3028 | uint16_t uNewGs = uFrame.pu32[5];
|
---|
3029 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)uFrame.pv, IEM_ACCESS_STACK_R); /* don't use iemMemStackPopCommitSpecial here. */
|
---|
3030 | if (rcStrict != VINF_SUCCESS)
|
---|
3031 | return rcStrict;
|
---|
3032 |
|
---|
3033 | /*
|
---|
3034 | * Commit the operation.
|
---|
3035 | */
|
---|
3036 | uNewFlags &= X86_EFL_LIVE_MASK;
|
---|
3037 | uNewFlags |= X86_EFL_RA1_MASK;
|
---|
3038 | #ifdef DBGFTRACE_ENABLED
|
---|
3039 | RTTraceBufAddMsgF(pVCpu->CTX_SUFF(pVM)->CTX_SUFF(hTraceBuf), "iret/p/v %04x:%08x -> %04x:%04x %x %04x:%04x",
|
---|
3040 | pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.eip, uNewCs, uNewEip, uNewFlags, uNewSs, uNewEsp);
|
---|
3041 | #endif
|
---|
3042 | 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));
|
---|
3043 |
|
---|
3044 | IEMMISC_SET_EFL(pVCpu, uNewFlags);
|
---|
3045 | iemCImplCommonV8086LoadSeg(&pVCpu->cpum.GstCtx.cs, uNewCs);
|
---|
3046 | iemCImplCommonV8086LoadSeg(&pVCpu->cpum.GstCtx.ss, uNewSs);
|
---|
3047 | iemCImplCommonV8086LoadSeg(&pVCpu->cpum.GstCtx.es, uNewEs);
|
---|
3048 | iemCImplCommonV8086LoadSeg(&pVCpu->cpum.GstCtx.ds, uNewDs);
|
---|
3049 | iemCImplCommonV8086LoadSeg(&pVCpu->cpum.GstCtx.fs, uNewFs);
|
---|
3050 | iemCImplCommonV8086LoadSeg(&pVCpu->cpum.GstCtx.gs, uNewGs);
|
---|
3051 | pVCpu->cpum.GstCtx.rip = (uint16_t)uNewEip;
|
---|
3052 | pVCpu->cpum.GstCtx.rsp = uNewEsp; /** @todo check this out! */
|
---|
3053 | pVCpu->iem.s.uCpl = 3;
|
---|
3054 |
|
---|
3055 | /* Flush the prefetch buffer. */
|
---|
3056 | #ifdef IEM_WITH_CODE_TLB
|
---|
3057 | pVCpu->iem.s.pbInstrBuf = NULL;
|
---|
3058 | #else
|
---|
3059 | pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
|
---|
3060 | #endif
|
---|
3061 |
|
---|
3062 | return VINF_SUCCESS;
|
---|
3063 | }
|
---|
3064 |
|
---|
3065 |
|
---|
3066 | /**
|
---|
3067 | * Implements iret for protected mode returning via a nested task.
|
---|
3068 | *
|
---|
3069 | * @param enmEffOpSize The effective operand size.
|
---|
3070 | */
|
---|
3071 | IEM_CIMPL_DEF_1(iemCImpl_iret_prot_NestedTask, IEMMODE, enmEffOpSize)
|
---|
3072 | {
|
---|
3073 | Log7(("iemCImpl_iret_prot_NestedTask:\n"));
|
---|
3074 | #ifndef IEM_IMPLEMENTS_TASKSWITCH
|
---|
3075 | IEM_RETURN_ASPECT_NOT_IMPLEMENTED();
|
---|
3076 | #else
|
---|
3077 | RT_NOREF_PV(enmEffOpSize);
|
---|
3078 |
|
---|
3079 | /*
|
---|
3080 | * Read the segment selector in the link-field of the current TSS.
|
---|
3081 | */
|
---|
3082 | RTSEL uSelRet;
|
---|
3083 | VBOXSTRICTRC rcStrict = iemMemFetchSysU16(pVCpu, &uSelRet, UINT8_MAX, pVCpu->cpum.GstCtx.tr.u64Base);
|
---|
3084 | if (rcStrict != VINF_SUCCESS)
|
---|
3085 | return rcStrict;
|
---|
3086 |
|
---|
3087 | /*
|
---|
3088 | * Fetch the returning task's TSS descriptor from the GDT.
|
---|
3089 | */
|
---|
3090 | if (uSelRet & X86_SEL_LDT)
|
---|
3091 | {
|
---|
3092 | Log(("iret_prot_NestedTask TSS not in LDT. uSelRet=%04x -> #TS\n", uSelRet));
|
---|
3093 | return iemRaiseTaskSwitchFaultBySelector(pVCpu, uSelRet);
|
---|
3094 | }
|
---|
3095 |
|
---|
3096 | IEMSELDESC TssDesc;
|
---|
3097 | rcStrict = iemMemFetchSelDesc(pVCpu, &TssDesc, uSelRet, X86_XCPT_GP);
|
---|
3098 | if (rcStrict != VINF_SUCCESS)
|
---|
3099 | return rcStrict;
|
---|
3100 |
|
---|
3101 | if (TssDesc.Legacy.Gate.u1DescType)
|
---|
3102 | {
|
---|
3103 | Log(("iret_prot_NestedTask Invalid TSS type. uSelRet=%04x -> #TS\n", uSelRet));
|
---|
3104 | return iemRaiseTaskSwitchFaultBySelector(pVCpu, uSelRet & X86_SEL_MASK_OFF_RPL);
|
---|
3105 | }
|
---|
3106 |
|
---|
3107 | if ( TssDesc.Legacy.Gate.u4Type != X86_SEL_TYPE_SYS_286_TSS_BUSY
|
---|
3108 | && TssDesc.Legacy.Gate.u4Type != X86_SEL_TYPE_SYS_386_TSS_BUSY)
|
---|
3109 | {
|
---|
3110 | Log(("iret_prot_NestedTask TSS is not busy. uSelRet=%04x DescType=%#x -> #TS\n", uSelRet, TssDesc.Legacy.Gate.u4Type));
|
---|
3111 | return iemRaiseTaskSwitchFaultBySelector(pVCpu, uSelRet & X86_SEL_MASK_OFF_RPL);
|
---|
3112 | }
|
---|
3113 |
|
---|
3114 | if (!TssDesc.Legacy.Gate.u1Present)
|
---|
3115 | {
|
---|
3116 | Log(("iret_prot_NestedTask TSS is not present. uSelRet=%04x -> #NP\n", uSelRet));
|
---|
3117 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uSelRet & X86_SEL_MASK_OFF_RPL);
|
---|
3118 | }
|
---|
3119 |
|
---|
3120 | uint32_t uNextEip = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
3121 | return iemTaskSwitch(pVCpu, IEMTASKSWITCH_IRET, uNextEip, 0 /* fFlags */, 0 /* uErr */,
|
---|
3122 | 0 /* uCr2 */, uSelRet, &TssDesc);
|
---|
3123 | #endif
|
---|
3124 | }
|
---|
3125 |
|
---|
3126 |
|
---|
3127 | /**
|
---|
3128 | * Implements iret for protected mode
|
---|
3129 | *
|
---|
3130 | * @param enmEffOpSize The effective operand size.
|
---|
3131 | */
|
---|
3132 | IEM_CIMPL_DEF_1(iemCImpl_iret_prot, IEMMODE, enmEffOpSize)
|
---|
3133 | {
|
---|
3134 | NOREF(cbInstr);
|
---|
3135 | Assert(enmEffOpSize == IEMMODE_32BIT || enmEffOpSize == IEMMODE_16BIT);
|
---|
3136 |
|
---|
3137 | /*
|
---|
3138 | * Nested task return.
|
---|
3139 | */
|
---|
3140 | if (pVCpu->cpum.GstCtx.eflags.Bits.u1NT)
|
---|
3141 | return IEM_CIMPL_CALL_1(iemCImpl_iret_prot_NestedTask, enmEffOpSize);
|
---|
3142 |
|
---|
3143 | /*
|
---|
3144 | * Normal return.
|
---|
3145 | *
|
---|
3146 | * Do the stack bits, but don't commit RSP before everything checks
|
---|
3147 | * out right.
|
---|
3148 | */
|
---|
3149 | Assert(enmEffOpSize == IEMMODE_32BIT || enmEffOpSize == IEMMODE_16BIT);
|
---|
3150 | VBOXSTRICTRC rcStrict;
|
---|
3151 | RTCPTRUNION uFrame;
|
---|
3152 | uint16_t uNewCs;
|
---|
3153 | uint32_t uNewEip;
|
---|
3154 | uint32_t uNewFlags;
|
---|
3155 | uint64_t uNewRsp;
|
---|
3156 | if (enmEffOpSize == IEMMODE_32BIT)
|
---|
3157 | {
|
---|
3158 | rcStrict = iemMemStackPopBeginSpecial(pVCpu, 12, &uFrame.pv, &uNewRsp);
|
---|
3159 | if (rcStrict != VINF_SUCCESS)
|
---|
3160 | return rcStrict;
|
---|
3161 | uNewEip = uFrame.pu32[0];
|
---|
3162 | uNewCs = (uint16_t)uFrame.pu32[1];
|
---|
3163 | uNewFlags = uFrame.pu32[2];
|
---|
3164 | }
|
---|
3165 | else
|
---|
3166 | {
|
---|
3167 | rcStrict = iemMemStackPopBeginSpecial(pVCpu, 6, &uFrame.pv, &uNewRsp);
|
---|
3168 | if (rcStrict != VINF_SUCCESS)
|
---|
3169 | return rcStrict;
|
---|
3170 | uNewEip = uFrame.pu16[0];
|
---|
3171 | uNewCs = uFrame.pu16[1];
|
---|
3172 | uNewFlags = uFrame.pu16[2];
|
---|
3173 | }
|
---|
3174 | rcStrict = iemMemStackPopDoneSpecial(pVCpu, (void *)uFrame.pv); /* don't use iemMemStackPopCommitSpecial here. */
|
---|
3175 | if (RT_LIKELY(rcStrict == VINF_SUCCESS))
|
---|
3176 | { /* extremely likely */ }
|
---|
3177 | else
|
---|
3178 | return rcStrict;
|
---|
3179 | Log7(("iemCImpl_iret_prot: uNewCs=%#06x uNewEip=%#010x uNewFlags=%#x uNewRsp=%#18llx uCpl=%u\n", uNewCs, uNewEip, uNewFlags, uNewRsp, pVCpu->iem.s.uCpl));
|
---|
3180 |
|
---|
3181 | /*
|
---|
3182 | * We're hopefully not returning to V8086 mode...
|
---|
3183 | */
|
---|
3184 | if ( (uNewFlags & X86_EFL_VM)
|
---|
3185 | && pVCpu->iem.s.uCpl == 0)
|
---|
3186 | {
|
---|
3187 | Assert(enmEffOpSize == IEMMODE_32BIT);
|
---|
3188 | return IEM_CIMPL_CALL_4(iemCImpl_iret_prot_v8086, uNewEip, uNewCs, uNewFlags, uNewRsp);
|
---|
3189 | }
|
---|
3190 |
|
---|
3191 | /*
|
---|
3192 | * Protected mode.
|
---|
3193 | */
|
---|
3194 | /* Read the CS descriptor. */
|
---|
3195 | if (!(uNewCs & X86_SEL_MASK_OFF_RPL))
|
---|
3196 | {
|
---|
3197 | Log(("iret %04x:%08x -> invalid CS selector, #GP(0)\n", uNewCs, uNewEip));
|
---|
3198 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
3199 | }
|
---|
3200 |
|
---|
3201 | IEMSELDESC DescCS;
|
---|
3202 | rcStrict = iemMemFetchSelDesc(pVCpu, &DescCS, uNewCs, X86_XCPT_GP);
|
---|
3203 | if (rcStrict != VINF_SUCCESS)
|
---|
3204 | {
|
---|
3205 | Log(("iret %04x:%08x - rcStrict=%Rrc when fetching CS\n", uNewCs, uNewEip, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
3206 | return rcStrict;
|
---|
3207 | }
|
---|
3208 |
|
---|
3209 | /* Must be a code descriptor. */
|
---|
3210 | if (!DescCS.Legacy.Gen.u1DescType)
|
---|
3211 | {
|
---|
3212 | Log(("iret %04x:%08x - CS is system segment (%#x) -> #GP\n", uNewCs, uNewEip, DescCS.Legacy.Gen.u4Type));
|
---|
3213 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
3214 | }
|
---|
3215 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE))
|
---|
3216 | {
|
---|
3217 | Log(("iret %04x:%08x - not code segment (%#x) -> #GP\n", uNewCs, uNewEip, DescCS.Legacy.Gen.u4Type));
|
---|
3218 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
3219 | }
|
---|
3220 |
|
---|
3221 | /* Privilege checks. */
|
---|
3222 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF))
|
---|
3223 | {
|
---|
3224 | if ((uNewCs & X86_SEL_RPL) != DescCS.Legacy.Gen.u2Dpl)
|
---|
3225 | {
|
---|
3226 | Log(("iret %04x:%08x - RPL != DPL (%d) -> #GP\n", uNewCs, uNewEip, DescCS.Legacy.Gen.u2Dpl));
|
---|
3227 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
3228 | }
|
---|
3229 | }
|
---|
3230 | else if ((uNewCs & X86_SEL_RPL) < DescCS.Legacy.Gen.u2Dpl)
|
---|
3231 | {
|
---|
3232 | Log(("iret %04x:%08x - RPL < DPL (%d) -> #GP\n", uNewCs, uNewEip, DescCS.Legacy.Gen.u2Dpl));
|
---|
3233 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
3234 | }
|
---|
3235 | if ((uNewCs & X86_SEL_RPL) < pVCpu->iem.s.uCpl)
|
---|
3236 | {
|
---|
3237 | Log(("iret %04x:%08x - RPL < CPL (%d) -> #GP\n", uNewCs, uNewEip, pVCpu->iem.s.uCpl));
|
---|
3238 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
3239 | }
|
---|
3240 |
|
---|
3241 | /* Present? */
|
---|
3242 | if (!DescCS.Legacy.Gen.u1Present)
|
---|
3243 | {
|
---|
3244 | Log(("iret %04x:%08x - CS not present -> #NP\n", uNewCs, uNewEip));
|
---|
3245 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uNewCs);
|
---|
3246 | }
|
---|
3247 |
|
---|
3248 | uint32_t cbLimitCS = X86DESC_LIMIT_G(&DescCS.Legacy);
|
---|
3249 |
|
---|
3250 | /*
|
---|
3251 | * Return to outer level?
|
---|
3252 | */
|
---|
3253 | if ((uNewCs & X86_SEL_RPL) != pVCpu->iem.s.uCpl)
|
---|
3254 | {
|
---|
3255 | uint16_t uNewSS;
|
---|
3256 | uint32_t uNewESP;
|
---|
3257 | if (enmEffOpSize == IEMMODE_32BIT)
|
---|
3258 | {
|
---|
3259 | rcStrict = iemMemStackPopContinueSpecial(pVCpu, 8, &uFrame.pv, &uNewRsp);
|
---|
3260 | if (rcStrict != VINF_SUCCESS)
|
---|
3261 | return rcStrict;
|
---|
3262 | /** @todo We might be popping a 32-bit ESP from the IRET frame, but whether
|
---|
3263 | * 16-bit or 32-bit are being loaded into SP depends on the D/B
|
---|
3264 | * bit of the popped SS selector it turns out. */
|
---|
3265 | uNewESP = uFrame.pu32[0];
|
---|
3266 | uNewSS = (uint16_t)uFrame.pu32[1];
|
---|
3267 | }
|
---|
3268 | else
|
---|
3269 | {
|
---|
3270 | rcStrict = iemMemStackPopContinueSpecial(pVCpu, 4, &uFrame.pv, &uNewRsp);
|
---|
3271 | if (rcStrict != VINF_SUCCESS)
|
---|
3272 | return rcStrict;
|
---|
3273 | uNewESP = uFrame.pu16[0];
|
---|
3274 | uNewSS = uFrame.pu16[1];
|
---|
3275 | }
|
---|
3276 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)uFrame.pv, IEM_ACCESS_STACK_R);
|
---|
3277 | if (rcStrict != VINF_SUCCESS)
|
---|
3278 | return rcStrict;
|
---|
3279 | Log7(("iemCImpl_iret_prot: uNewSS=%#06x uNewESP=%#010x\n", uNewSS, uNewESP));
|
---|
3280 |
|
---|
3281 | /* Read the SS descriptor. */
|
---|
3282 | if (!(uNewSS & X86_SEL_MASK_OFF_RPL))
|
---|
3283 | {
|
---|
3284 | Log(("iret %04x:%08x/%04x:%08x -> invalid SS selector, #GP(0)\n", uNewCs, uNewEip, uNewSS, uNewESP));
|
---|
3285 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
3286 | }
|
---|
3287 |
|
---|
3288 | IEMSELDESC DescSS;
|
---|
3289 | rcStrict = iemMemFetchSelDesc(pVCpu, &DescSS, uNewSS, X86_XCPT_GP); /** @todo Correct exception? */
|
---|
3290 | if (rcStrict != VINF_SUCCESS)
|
---|
3291 | {
|
---|
3292 | Log(("iret %04x:%08x/%04x:%08x - %Rrc when fetching SS\n",
|
---|
3293 | uNewCs, uNewEip, uNewSS, uNewESP, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
3294 | return rcStrict;
|
---|
3295 | }
|
---|
3296 |
|
---|
3297 | /* Privilege checks. */
|
---|
3298 | if ((uNewSS & X86_SEL_RPL) != (uNewCs & X86_SEL_RPL))
|
---|
3299 | {
|
---|
3300 | Log(("iret %04x:%08x/%04x:%08x -> SS.RPL != CS.RPL -> #GP\n", uNewCs, uNewEip, uNewSS, uNewESP));
|
---|
3301 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSS);
|
---|
3302 | }
|
---|
3303 | if (DescSS.Legacy.Gen.u2Dpl != (uNewCs & X86_SEL_RPL))
|
---|
3304 | {
|
---|
3305 | Log(("iret %04x:%08x/%04x:%08x -> SS.DPL (%d) != CS.RPL -> #GP\n",
|
---|
3306 | uNewCs, uNewEip, uNewSS, uNewESP, DescSS.Legacy.Gen.u2Dpl));
|
---|
3307 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSS);
|
---|
3308 | }
|
---|
3309 |
|
---|
3310 | /* Must be a writeable data segment descriptor. */
|
---|
3311 | if (!DescSS.Legacy.Gen.u1DescType)
|
---|
3312 | {
|
---|
3313 | Log(("iret %04x:%08x/%04x:%08x -> SS is system segment (%#x) -> #GP\n",
|
---|
3314 | uNewCs, uNewEip, uNewSS, uNewESP, DescSS.Legacy.Gen.u4Type));
|
---|
3315 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSS);
|
---|
3316 | }
|
---|
3317 | if ((DescSS.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_WRITE)) != X86_SEL_TYPE_WRITE)
|
---|
3318 | {
|
---|
3319 | Log(("iret %04x:%08x/%04x:%08x - not writable data segment (%#x) -> #GP\n",
|
---|
3320 | uNewCs, uNewEip, uNewSS, uNewESP, DescSS.Legacy.Gen.u4Type));
|
---|
3321 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSS);
|
---|
3322 | }
|
---|
3323 |
|
---|
3324 | /* Present? */
|
---|
3325 | if (!DescSS.Legacy.Gen.u1Present)
|
---|
3326 | {
|
---|
3327 | Log(("iret %04x:%08x/%04x:%08x -> SS not present -> #SS\n", uNewCs, uNewEip, uNewSS, uNewESP));
|
---|
3328 | return iemRaiseStackSelectorNotPresentBySelector(pVCpu, uNewSS);
|
---|
3329 | }
|
---|
3330 |
|
---|
3331 | uint32_t cbLimitSs = X86DESC_LIMIT_G(&DescSS.Legacy);
|
---|
3332 |
|
---|
3333 | /* Check EIP. */
|
---|
3334 | if (uNewEip > cbLimitCS)
|
---|
3335 | {
|
---|
3336 | Log(("iret %04x:%08x/%04x:%08x -> EIP is out of bounds (%#x) -> #GP(0)\n",
|
---|
3337 | uNewCs, uNewEip, uNewSS, uNewESP, cbLimitCS));
|
---|
3338 | /** @todo: Which is it, #GP(0) or #GP(sel)? */
|
---|
3339 | return iemRaiseSelectorBoundsBySelector(pVCpu, uNewCs);
|
---|
3340 | }
|
---|
3341 |
|
---|
3342 | /*
|
---|
3343 | * Commit the changes, marking CS and SS accessed first since
|
---|
3344 | * that may fail.
|
---|
3345 | */
|
---|
3346 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
3347 | {
|
---|
3348 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewCs);
|
---|
3349 | if (rcStrict != VINF_SUCCESS)
|
---|
3350 | return rcStrict;
|
---|
3351 | DescCS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
3352 | }
|
---|
3353 | if (!(DescSS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
3354 | {
|
---|
3355 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewSS);
|
---|
3356 | if (rcStrict != VINF_SUCCESS)
|
---|
3357 | return rcStrict;
|
---|
3358 | DescSS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
3359 | }
|
---|
3360 |
|
---|
3361 | uint32_t fEFlagsMask = X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF
|
---|
3362 | | X86_EFL_TF | X86_EFL_DF | X86_EFL_OF | X86_EFL_NT;
|
---|
3363 | if (enmEffOpSize != IEMMODE_16BIT)
|
---|
3364 | fEFlagsMask |= X86_EFL_RF | X86_EFL_AC | X86_EFL_ID;
|
---|
3365 | if (pVCpu->iem.s.uCpl == 0)
|
---|
3366 | fEFlagsMask |= X86_EFL_IF | X86_EFL_IOPL | X86_EFL_VIF | X86_EFL_VIP; /* VM is 0 */
|
---|
3367 | else if (pVCpu->iem.s.uCpl <= pVCpu->cpum.GstCtx.eflags.Bits.u2IOPL)
|
---|
3368 | fEFlagsMask |= X86_EFL_IF;
|
---|
3369 | if (IEM_GET_TARGET_CPU(pVCpu) <= IEMTARGETCPU_386)
|
---|
3370 | fEFlagsMask &= ~(X86_EFL_AC | X86_EFL_ID | X86_EFL_VIF | X86_EFL_VIP);
|
---|
3371 | uint32_t fEFlagsNew = IEMMISC_GET_EFL(pVCpu);
|
---|
3372 | fEFlagsNew &= ~fEFlagsMask;
|
---|
3373 | fEFlagsNew |= uNewFlags & fEFlagsMask;
|
---|
3374 | #ifdef DBGFTRACE_ENABLED
|
---|
3375 | RTTraceBufAddMsgF(pVCpu->CTX_SUFF(pVM)->CTX_SUFF(hTraceBuf), "iret/%up%u %04x:%08x -> %04x:%04x %x %04x:%04x",
|
---|
3376 | pVCpu->iem.s.uCpl, uNewCs & X86_SEL_RPL, pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.eip,
|
---|
3377 | uNewCs, uNewEip, uNewFlags, uNewSS, uNewESP);
|
---|
3378 | #endif
|
---|
3379 |
|
---|
3380 | IEMMISC_SET_EFL(pVCpu, fEFlagsNew);
|
---|
3381 | pVCpu->cpum.GstCtx.rip = uNewEip;
|
---|
3382 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
|
---|
3383 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
|
---|
3384 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
3385 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCS.Legacy);
|
---|
3386 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimitCS;
|
---|
3387 | pVCpu->cpum.GstCtx.cs.u64Base = X86DESC_BASE(&DescCS.Legacy);
|
---|
3388 | pVCpu->iem.s.enmCpuMode = iemCalcCpuMode(pVCpu);
|
---|
3389 |
|
---|
3390 | pVCpu->cpum.GstCtx.ss.Sel = uNewSS;
|
---|
3391 | pVCpu->cpum.GstCtx.ss.ValidSel = uNewSS;
|
---|
3392 | pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
3393 | pVCpu->cpum.GstCtx.ss.Attr.u = X86DESC_GET_HID_ATTR(&DescSS.Legacy);
|
---|
3394 | pVCpu->cpum.GstCtx.ss.u32Limit = cbLimitSs;
|
---|
3395 | pVCpu->cpum.GstCtx.ss.u64Base = X86DESC_BASE(&DescSS.Legacy);
|
---|
3396 | if (!pVCpu->cpum.GstCtx.ss.Attr.n.u1DefBig)
|
---|
3397 | pVCpu->cpum.GstCtx.sp = (uint16_t)uNewESP;
|
---|
3398 | else
|
---|
3399 | pVCpu->cpum.GstCtx.rsp = uNewESP;
|
---|
3400 |
|
---|
3401 | pVCpu->iem.s.uCpl = uNewCs & X86_SEL_RPL;
|
---|
3402 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.ds);
|
---|
3403 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.es);
|
---|
3404 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.fs);
|
---|
3405 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.gs);
|
---|
3406 |
|
---|
3407 | /* Done! */
|
---|
3408 |
|
---|
3409 | }
|
---|
3410 | /*
|
---|
3411 | * Return to the same level.
|
---|
3412 | */
|
---|
3413 | else
|
---|
3414 | {
|
---|
3415 | /* Check EIP. */
|
---|
3416 | if (uNewEip > cbLimitCS)
|
---|
3417 | {
|
---|
3418 | Log(("iret %04x:%08x - EIP is out of bounds (%#x) -> #GP(0)\n", uNewCs, uNewEip, cbLimitCS));
|
---|
3419 | /** @todo: Which is it, #GP(0) or #GP(sel)? */
|
---|
3420 | return iemRaiseSelectorBoundsBySelector(pVCpu, uNewCs);
|
---|
3421 | }
|
---|
3422 |
|
---|
3423 | /*
|
---|
3424 | * Commit the changes, marking CS first since it may fail.
|
---|
3425 | */
|
---|
3426 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
3427 | {
|
---|
3428 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewCs);
|
---|
3429 | if (rcStrict != VINF_SUCCESS)
|
---|
3430 | return rcStrict;
|
---|
3431 | DescCS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
3432 | }
|
---|
3433 |
|
---|
3434 | X86EFLAGS NewEfl;
|
---|
3435 | NewEfl.u = IEMMISC_GET_EFL(pVCpu);
|
---|
3436 | uint32_t fEFlagsMask = X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF
|
---|
3437 | | X86_EFL_TF | X86_EFL_DF | X86_EFL_OF | X86_EFL_NT;
|
---|
3438 | if (enmEffOpSize != IEMMODE_16BIT)
|
---|
3439 | fEFlagsMask |= X86_EFL_RF | X86_EFL_AC | X86_EFL_ID;
|
---|
3440 | if (pVCpu->iem.s.uCpl == 0)
|
---|
3441 | fEFlagsMask |= X86_EFL_IF | X86_EFL_IOPL | X86_EFL_VIF | X86_EFL_VIP; /* VM is 0 */
|
---|
3442 | else if (pVCpu->iem.s.uCpl <= NewEfl.Bits.u2IOPL)
|
---|
3443 | fEFlagsMask |= X86_EFL_IF;
|
---|
3444 | if (IEM_GET_TARGET_CPU(pVCpu) <= IEMTARGETCPU_386)
|
---|
3445 | fEFlagsMask &= ~(X86_EFL_AC | X86_EFL_ID | X86_EFL_VIF | X86_EFL_VIP);
|
---|
3446 | NewEfl.u &= ~fEFlagsMask;
|
---|
3447 | NewEfl.u |= fEFlagsMask & uNewFlags;
|
---|
3448 | #ifdef DBGFTRACE_ENABLED
|
---|
3449 | RTTraceBufAddMsgF(pVCpu->CTX_SUFF(pVM)->CTX_SUFF(hTraceBuf), "iret/%up %04x:%08x -> %04x:%04x %x %04x:%04llx",
|
---|
3450 | pVCpu->iem.s.uCpl, pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.eip,
|
---|
3451 | uNewCs, uNewEip, uNewFlags, pVCpu->cpum.GstCtx.ss.Sel, uNewRsp);
|
---|
3452 | #endif
|
---|
3453 |
|
---|
3454 | IEMMISC_SET_EFL(pVCpu, NewEfl.u);
|
---|
3455 | pVCpu->cpum.GstCtx.rip = uNewEip;
|
---|
3456 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
|
---|
3457 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
|
---|
3458 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
3459 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCS.Legacy);
|
---|
3460 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimitCS;
|
---|
3461 | pVCpu->cpum.GstCtx.cs.u64Base = X86DESC_BASE(&DescCS.Legacy);
|
---|
3462 | pVCpu->iem.s.enmCpuMode = iemCalcCpuMode(pVCpu);
|
---|
3463 | if (!pVCpu->cpum.GstCtx.ss.Attr.n.u1DefBig)
|
---|
3464 | pVCpu->cpum.GstCtx.sp = (uint16_t)uNewRsp;
|
---|
3465 | else
|
---|
3466 | pVCpu->cpum.GstCtx.rsp = uNewRsp;
|
---|
3467 | /* Done! */
|
---|
3468 | }
|
---|
3469 |
|
---|
3470 | /* Flush the prefetch buffer. */
|
---|
3471 | #ifdef IEM_WITH_CODE_TLB
|
---|
3472 | pVCpu->iem.s.pbInstrBuf = NULL;
|
---|
3473 | #else
|
---|
3474 | pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
|
---|
3475 | #endif
|
---|
3476 |
|
---|
3477 | return VINF_SUCCESS;
|
---|
3478 | }
|
---|
3479 |
|
---|
3480 |
|
---|
3481 | /**
|
---|
3482 | * Implements iret for long mode
|
---|
3483 | *
|
---|
3484 | * @param enmEffOpSize The effective operand size.
|
---|
3485 | */
|
---|
3486 | IEM_CIMPL_DEF_1(iemCImpl_iret_64bit, IEMMODE, enmEffOpSize)
|
---|
3487 | {
|
---|
3488 | NOREF(cbInstr);
|
---|
3489 |
|
---|
3490 | /*
|
---|
3491 | * Nested task return is not supported in long mode.
|
---|
3492 | */
|
---|
3493 | if (pVCpu->cpum.GstCtx.eflags.Bits.u1NT)
|
---|
3494 | {
|
---|
3495 | Log(("iretq with NT=1 (eflags=%#x) -> #GP(0)\n", pVCpu->cpum.GstCtx.eflags.u));
|
---|
3496 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
3497 | }
|
---|
3498 |
|
---|
3499 | /*
|
---|
3500 | * Normal return.
|
---|
3501 | *
|
---|
3502 | * Do the stack bits, but don't commit RSP before everything checks
|
---|
3503 | * out right.
|
---|
3504 | */
|
---|
3505 | VBOXSTRICTRC rcStrict;
|
---|
3506 | RTCPTRUNION uFrame;
|
---|
3507 | uint64_t uNewRip;
|
---|
3508 | uint16_t uNewCs;
|
---|
3509 | uint16_t uNewSs;
|
---|
3510 | uint32_t uNewFlags;
|
---|
3511 | uint64_t uNewRsp;
|
---|
3512 | if (enmEffOpSize == IEMMODE_64BIT)
|
---|
3513 | {
|
---|
3514 | rcStrict = iemMemStackPopBeginSpecial(pVCpu, 5*8, &uFrame.pv, &uNewRsp);
|
---|
3515 | if (rcStrict != VINF_SUCCESS)
|
---|
3516 | return rcStrict;
|
---|
3517 | uNewRip = uFrame.pu64[0];
|
---|
3518 | uNewCs = (uint16_t)uFrame.pu64[1];
|
---|
3519 | uNewFlags = (uint32_t)uFrame.pu64[2];
|
---|
3520 | uNewRsp = uFrame.pu64[3];
|
---|
3521 | uNewSs = (uint16_t)uFrame.pu64[4];
|
---|
3522 | }
|
---|
3523 | else if (enmEffOpSize == IEMMODE_32BIT)
|
---|
3524 | {
|
---|
3525 | rcStrict = iemMemStackPopBeginSpecial(pVCpu, 5*4, &uFrame.pv, &uNewRsp);
|
---|
3526 | if (rcStrict != VINF_SUCCESS)
|
---|
3527 | return rcStrict;
|
---|
3528 | uNewRip = uFrame.pu32[0];
|
---|
3529 | uNewCs = (uint16_t)uFrame.pu32[1];
|
---|
3530 | uNewFlags = uFrame.pu32[2];
|
---|
3531 | uNewRsp = uFrame.pu32[3];
|
---|
3532 | uNewSs = (uint16_t)uFrame.pu32[4];
|
---|
3533 | }
|
---|
3534 | else
|
---|
3535 | {
|
---|
3536 | Assert(enmEffOpSize == IEMMODE_16BIT);
|
---|
3537 | rcStrict = iemMemStackPopBeginSpecial(pVCpu, 5*2, &uFrame.pv, &uNewRsp);
|
---|
3538 | if (rcStrict != VINF_SUCCESS)
|
---|
3539 | return rcStrict;
|
---|
3540 | uNewRip = uFrame.pu16[0];
|
---|
3541 | uNewCs = uFrame.pu16[1];
|
---|
3542 | uNewFlags = uFrame.pu16[2];
|
---|
3543 | uNewRsp = uFrame.pu16[3];
|
---|
3544 | uNewSs = uFrame.pu16[4];
|
---|
3545 | }
|
---|
3546 | rcStrict = iemMemStackPopDoneSpecial(pVCpu, (void *)uFrame.pv); /* don't use iemMemStackPopCommitSpecial here. */
|
---|
3547 | if (RT_LIKELY(rcStrict == VINF_SUCCESS))
|
---|
3548 | { /* extremely like */ }
|
---|
3549 | else
|
---|
3550 | return rcStrict;
|
---|
3551 | Log7(("iretq stack: cs:rip=%04x:%016RX64 rflags=%016RX64 ss:rsp=%04x:%016RX64\n", uNewCs, uNewRip, uNewFlags, uNewSs, uNewRsp));
|
---|
3552 |
|
---|
3553 | /*
|
---|
3554 | * Check stuff.
|
---|
3555 | */
|
---|
3556 | /* Read the CS descriptor. */
|
---|
3557 | if (!(uNewCs & X86_SEL_MASK_OFF_RPL))
|
---|
3558 | {
|
---|
3559 | Log(("iret %04x:%016RX64/%04x:%016RX64 -> invalid CS selector, #GP(0)\n", uNewCs, uNewRip, uNewSs, uNewRsp));
|
---|
3560 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
3561 | }
|
---|
3562 |
|
---|
3563 | IEMSELDESC DescCS;
|
---|
3564 | rcStrict = iemMemFetchSelDesc(pVCpu, &DescCS, uNewCs, X86_XCPT_GP);
|
---|
3565 | if (rcStrict != VINF_SUCCESS)
|
---|
3566 | {
|
---|
3567 | Log(("iret %04x:%016RX64/%04x:%016RX64 - rcStrict=%Rrc when fetching CS\n",
|
---|
3568 | uNewCs, uNewRip, uNewSs, uNewRsp, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
3569 | return rcStrict;
|
---|
3570 | }
|
---|
3571 |
|
---|
3572 | /* Must be a code descriptor. */
|
---|
3573 | if ( !DescCS.Legacy.Gen.u1DescType
|
---|
3574 | || !(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE))
|
---|
3575 | {
|
---|
3576 | Log(("iret %04x:%016RX64/%04x:%016RX64 - CS is not a code segment T=%u T=%#xu -> #GP\n",
|
---|
3577 | uNewCs, uNewRip, uNewSs, uNewRsp, DescCS.Legacy.Gen.u1DescType, DescCS.Legacy.Gen.u4Type));
|
---|
3578 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
3579 | }
|
---|
3580 |
|
---|
3581 | /* Privilege checks. */
|
---|
3582 | uint8_t const uNewCpl = uNewCs & X86_SEL_RPL;
|
---|
3583 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF))
|
---|
3584 | {
|
---|
3585 | if ((uNewCs & X86_SEL_RPL) != DescCS.Legacy.Gen.u2Dpl)
|
---|
3586 | {
|
---|
3587 | Log(("iret %04x:%016RX64 - RPL != DPL (%d) -> #GP\n", uNewCs, uNewRip, DescCS.Legacy.Gen.u2Dpl));
|
---|
3588 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
3589 | }
|
---|
3590 | }
|
---|
3591 | else if ((uNewCs & X86_SEL_RPL) < DescCS.Legacy.Gen.u2Dpl)
|
---|
3592 | {
|
---|
3593 | Log(("iret %04x:%016RX64 - RPL < DPL (%d) -> #GP\n", uNewCs, uNewRip, DescCS.Legacy.Gen.u2Dpl));
|
---|
3594 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
3595 | }
|
---|
3596 | if ((uNewCs & X86_SEL_RPL) < pVCpu->iem.s.uCpl)
|
---|
3597 | {
|
---|
3598 | Log(("iret %04x:%016RX64 - RPL < CPL (%d) -> #GP\n", uNewCs, uNewRip, pVCpu->iem.s.uCpl));
|
---|
3599 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
|
---|
3600 | }
|
---|
3601 |
|
---|
3602 | /* Present? */
|
---|
3603 | if (!DescCS.Legacy.Gen.u1Present)
|
---|
3604 | {
|
---|
3605 | Log(("iret %04x:%016RX64/%04x:%016RX64 - CS not present -> #NP\n", uNewCs, uNewRip, uNewSs, uNewRsp));
|
---|
3606 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uNewCs);
|
---|
3607 | }
|
---|
3608 |
|
---|
3609 | uint32_t cbLimitCS = X86DESC_LIMIT_G(&DescCS.Legacy);
|
---|
3610 |
|
---|
3611 | /* Read the SS descriptor. */
|
---|
3612 | IEMSELDESC DescSS;
|
---|
3613 | if (!(uNewSs & X86_SEL_MASK_OFF_RPL))
|
---|
3614 | {
|
---|
3615 | if ( !DescCS.Legacy.Gen.u1Long
|
---|
3616 | || DescCS.Legacy.Gen.u1DefBig /** @todo exactly how does iret (and others) behave with u1Long=1 and u1DefBig=1? \#GP(sel)? */
|
---|
3617 | || uNewCpl > 2) /** @todo verify SS=0 impossible for ring-3. */
|
---|
3618 | {
|
---|
3619 | Log(("iret %04x:%016RX64/%04x:%016RX64 -> invalid SS selector, #GP(0)\n", uNewCs, uNewRip, uNewSs, uNewRsp));
|
---|
3620 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
3621 | }
|
---|
3622 | DescSS.Legacy.u = 0;
|
---|
3623 | }
|
---|
3624 | else
|
---|
3625 | {
|
---|
3626 | rcStrict = iemMemFetchSelDesc(pVCpu, &DescSS, uNewSs, X86_XCPT_GP); /** @todo Correct exception? */
|
---|
3627 | if (rcStrict != VINF_SUCCESS)
|
---|
3628 | {
|
---|
3629 | Log(("iret %04x:%016RX64/%04x:%016RX64 - %Rrc when fetching SS\n",
|
---|
3630 | uNewCs, uNewRip, uNewSs, uNewRsp, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
3631 | return rcStrict;
|
---|
3632 | }
|
---|
3633 | }
|
---|
3634 |
|
---|
3635 | /* Privilege checks. */
|
---|
3636 | if ((uNewSs & X86_SEL_RPL) != (uNewCs & X86_SEL_RPL))
|
---|
3637 | {
|
---|
3638 | Log(("iret %04x:%016RX64/%04x:%016RX64 -> SS.RPL != CS.RPL -> #GP\n", uNewCs, uNewRip, uNewSs, uNewRsp));
|
---|
3639 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSs);
|
---|
3640 | }
|
---|
3641 |
|
---|
3642 | uint32_t cbLimitSs;
|
---|
3643 | if (!(uNewSs & X86_SEL_MASK_OFF_RPL))
|
---|
3644 | cbLimitSs = UINT32_MAX;
|
---|
3645 | else
|
---|
3646 | {
|
---|
3647 | if (DescSS.Legacy.Gen.u2Dpl != (uNewCs & X86_SEL_RPL))
|
---|
3648 | {
|
---|
3649 | Log(("iret %04x:%016RX64/%04x:%016RX64 -> SS.DPL (%d) != CS.RPL -> #GP\n",
|
---|
3650 | uNewCs, uNewRip, uNewSs, uNewRsp, DescSS.Legacy.Gen.u2Dpl));
|
---|
3651 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSs);
|
---|
3652 | }
|
---|
3653 |
|
---|
3654 | /* Must be a writeable data segment descriptor. */
|
---|
3655 | if (!DescSS.Legacy.Gen.u1DescType)
|
---|
3656 | {
|
---|
3657 | Log(("iret %04x:%016RX64/%04x:%016RX64 -> SS is system segment (%#x) -> #GP\n",
|
---|
3658 | uNewCs, uNewRip, uNewSs, uNewRsp, DescSS.Legacy.Gen.u4Type));
|
---|
3659 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSs);
|
---|
3660 | }
|
---|
3661 | if ((DescSS.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_WRITE)) != X86_SEL_TYPE_WRITE)
|
---|
3662 | {
|
---|
3663 | Log(("iret %04x:%016RX64/%04x:%016RX64 - not writable data segment (%#x) -> #GP\n",
|
---|
3664 | uNewCs, uNewRip, uNewSs, uNewRsp, DescSS.Legacy.Gen.u4Type));
|
---|
3665 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSs);
|
---|
3666 | }
|
---|
3667 |
|
---|
3668 | /* Present? */
|
---|
3669 | if (!DescSS.Legacy.Gen.u1Present)
|
---|
3670 | {
|
---|
3671 | Log(("iret %04x:%016RX64/%04x:%016RX64 -> SS not present -> #SS\n", uNewCs, uNewRip, uNewSs, uNewRsp));
|
---|
3672 | return iemRaiseStackSelectorNotPresentBySelector(pVCpu, uNewSs);
|
---|
3673 | }
|
---|
3674 | cbLimitSs = X86DESC_LIMIT_G(&DescSS.Legacy);
|
---|
3675 | }
|
---|
3676 |
|
---|
3677 | /* Check EIP. */
|
---|
3678 | if (DescCS.Legacy.Gen.u1Long)
|
---|
3679 | {
|
---|
3680 | if (!IEM_IS_CANONICAL(uNewRip))
|
---|
3681 | {
|
---|
3682 | Log(("iret %04x:%016RX64/%04x:%016RX64 -> RIP is not canonical -> #GP(0)\n",
|
---|
3683 | uNewCs, uNewRip, uNewSs, uNewRsp));
|
---|
3684 | return iemRaiseSelectorBoundsBySelector(pVCpu, uNewCs);
|
---|
3685 | }
|
---|
3686 | }
|
---|
3687 | else
|
---|
3688 | {
|
---|
3689 | if (uNewRip > cbLimitCS)
|
---|
3690 | {
|
---|
3691 | Log(("iret %04x:%016RX64/%04x:%016RX64 -> EIP is out of bounds (%#x) -> #GP(0)\n",
|
---|
3692 | uNewCs, uNewRip, uNewSs, uNewRsp, cbLimitCS));
|
---|
3693 | /** @todo: Which is it, #GP(0) or #GP(sel)? */
|
---|
3694 | return iemRaiseSelectorBoundsBySelector(pVCpu, uNewCs);
|
---|
3695 | }
|
---|
3696 | }
|
---|
3697 |
|
---|
3698 | /*
|
---|
3699 | * Commit the changes, marking CS and SS accessed first since
|
---|
3700 | * that may fail.
|
---|
3701 | */
|
---|
3702 | /** @todo where exactly are these actually marked accessed by a real CPU? */
|
---|
3703 | if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
3704 | {
|
---|
3705 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewCs);
|
---|
3706 | if (rcStrict != VINF_SUCCESS)
|
---|
3707 | return rcStrict;
|
---|
3708 | DescCS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
3709 | }
|
---|
3710 | if (!(DescSS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
3711 | {
|
---|
3712 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewSs);
|
---|
3713 | if (rcStrict != VINF_SUCCESS)
|
---|
3714 | return rcStrict;
|
---|
3715 | DescSS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
3716 | }
|
---|
3717 |
|
---|
3718 | uint32_t fEFlagsMask = X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF
|
---|
3719 | | X86_EFL_TF | X86_EFL_DF | X86_EFL_OF | X86_EFL_NT;
|
---|
3720 | if (enmEffOpSize != IEMMODE_16BIT)
|
---|
3721 | fEFlagsMask |= X86_EFL_RF | X86_EFL_AC | X86_EFL_ID;
|
---|
3722 | if (pVCpu->iem.s.uCpl == 0)
|
---|
3723 | fEFlagsMask |= X86_EFL_IF | X86_EFL_IOPL | X86_EFL_VIF | X86_EFL_VIP; /* VM is ignored */
|
---|
3724 | else if (pVCpu->iem.s.uCpl <= pVCpu->cpum.GstCtx.eflags.Bits.u2IOPL)
|
---|
3725 | fEFlagsMask |= X86_EFL_IF;
|
---|
3726 | uint32_t fEFlagsNew = IEMMISC_GET_EFL(pVCpu);
|
---|
3727 | fEFlagsNew &= ~fEFlagsMask;
|
---|
3728 | fEFlagsNew |= uNewFlags & fEFlagsMask;
|
---|
3729 | #ifdef DBGFTRACE_ENABLED
|
---|
3730 | RTTraceBufAddMsgF(pVCpu->CTX_SUFF(pVM)->CTX_SUFF(hTraceBuf), "iret/%ul%u %08llx -> %04x:%04llx %llx %04x:%04llx",
|
---|
3731 | pVCpu->iem.s.uCpl, uNewCpl, pVCpu->cpum.GstCtx.rip, uNewCs, uNewRip, uNewFlags, uNewSs, uNewRsp);
|
---|
3732 | #endif
|
---|
3733 |
|
---|
3734 | IEMMISC_SET_EFL(pVCpu, fEFlagsNew);
|
---|
3735 | pVCpu->cpum.GstCtx.rip = uNewRip;
|
---|
3736 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
|
---|
3737 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
|
---|
3738 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
3739 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCS.Legacy);
|
---|
3740 | pVCpu->cpum.GstCtx.cs.u32Limit = cbLimitCS;
|
---|
3741 | pVCpu->cpum.GstCtx.cs.u64Base = X86DESC_BASE(&DescCS.Legacy);
|
---|
3742 | pVCpu->iem.s.enmCpuMode = iemCalcCpuMode(pVCpu);
|
---|
3743 | if (pVCpu->cpum.GstCtx.cs.Attr.n.u1Long || pVCpu->cpum.GstCtx.cs.Attr.n.u1DefBig)
|
---|
3744 | pVCpu->cpum.GstCtx.rsp = uNewRsp;
|
---|
3745 | else
|
---|
3746 | pVCpu->cpum.GstCtx.sp = (uint16_t)uNewRsp;
|
---|
3747 | pVCpu->cpum.GstCtx.ss.Sel = uNewSs;
|
---|
3748 | pVCpu->cpum.GstCtx.ss.ValidSel = uNewSs;
|
---|
3749 | if (!(uNewSs & X86_SEL_MASK_OFF_RPL))
|
---|
3750 | {
|
---|
3751 | pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
3752 | pVCpu->cpum.GstCtx.ss.Attr.u = X86DESCATTR_UNUSABLE | (uNewCpl << X86DESCATTR_DPL_SHIFT);
|
---|
3753 | pVCpu->cpum.GstCtx.ss.u32Limit = UINT32_MAX;
|
---|
3754 | pVCpu->cpum.GstCtx.ss.u64Base = 0;
|
---|
3755 | Log2(("iretq new SS: NULL\n"));
|
---|
3756 | }
|
---|
3757 | else
|
---|
3758 | {
|
---|
3759 | pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
3760 | pVCpu->cpum.GstCtx.ss.Attr.u = X86DESC_GET_HID_ATTR(&DescSS.Legacy);
|
---|
3761 | pVCpu->cpum.GstCtx.ss.u32Limit = cbLimitSs;
|
---|
3762 | pVCpu->cpum.GstCtx.ss.u64Base = X86DESC_BASE(&DescSS.Legacy);
|
---|
3763 | 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));
|
---|
3764 | }
|
---|
3765 |
|
---|
3766 | if (pVCpu->iem.s.uCpl != uNewCpl)
|
---|
3767 | {
|
---|
3768 | pVCpu->iem.s.uCpl = uNewCpl;
|
---|
3769 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCpl, &pVCpu->cpum.GstCtx.ds);
|
---|
3770 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCpl, &pVCpu->cpum.GstCtx.es);
|
---|
3771 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCpl, &pVCpu->cpum.GstCtx.fs);
|
---|
3772 | iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCpl, &pVCpu->cpum.GstCtx.gs);
|
---|
3773 | }
|
---|
3774 |
|
---|
3775 | /* Flush the prefetch buffer. */
|
---|
3776 | #ifdef IEM_WITH_CODE_TLB
|
---|
3777 | pVCpu->iem.s.pbInstrBuf = NULL;
|
---|
3778 | #else
|
---|
3779 | pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
|
---|
3780 | #endif
|
---|
3781 |
|
---|
3782 | return VINF_SUCCESS;
|
---|
3783 | }
|
---|
3784 |
|
---|
3785 |
|
---|
3786 | /**
|
---|
3787 | * Implements iret.
|
---|
3788 | *
|
---|
3789 | * @param enmEffOpSize The effective operand size.
|
---|
3790 | */
|
---|
3791 | IEM_CIMPL_DEF_1(iemCImpl_iret, IEMMODE, enmEffOpSize)
|
---|
3792 | {
|
---|
3793 | bool fBlockingNmi = VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_BLOCK_NMIS);
|
---|
3794 |
|
---|
3795 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
3796 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
3797 | {
|
---|
3798 | /*
|
---|
3799 | * Record whether NMI (or virtual-NMI) blocking is in effect during the execution
|
---|
3800 | * of this IRET instruction. We need to provide this information as part of some
|
---|
3801 | * VM-exits.
|
---|
3802 | *
|
---|
3803 | * See Intel spec. 27.2.2 "Information for VM Exits Due to Vectored Events".
|
---|
3804 | */
|
---|
3805 | if (IEM_VMX_IS_PINCTLS_SET(pVCpu, VMX_PIN_CTLS_VIRT_NMI))
|
---|
3806 | pVCpu->cpum.GstCtx.hwvirt.vmx.fNmiUnblockingIret = pVCpu->cpum.GstCtx.hwvirt.vmx.fVirtNmiBlocking;
|
---|
3807 | else
|
---|
3808 | pVCpu->cpum.GstCtx.hwvirt.vmx.fNmiUnblockingIret = fBlockingNmi;
|
---|
3809 |
|
---|
3810 | /*
|
---|
3811 | * If "NMI exiting" is set, IRET does not affect blocking of NMIs.
|
---|
3812 | * See Intel Spec. 25.3 "Changes To Instruction Behavior In VMX Non-root Operation".
|
---|
3813 | */
|
---|
3814 | if (IEM_VMX_IS_PINCTLS_SET(pVCpu, VMX_PIN_CTLS_NMI_EXIT))
|
---|
3815 | fBlockingNmi = false;
|
---|
3816 |
|
---|
3817 | /* Clear virtual-NMI blocking, if any, before causing any further exceptions. */
|
---|
3818 | pVCpu->cpum.GstCtx.hwvirt.vmx.fVirtNmiBlocking = false;
|
---|
3819 | }
|
---|
3820 | #endif
|
---|
3821 |
|
---|
3822 | /*
|
---|
3823 | * The SVM nested-guest intercept for IRET takes priority over all exceptions,
|
---|
3824 | * The NMI is still held pending (which I assume means blocking of further NMIs
|
---|
3825 | * is in effect).
|
---|
3826 | *
|
---|
3827 | * See AMD spec. 15.9 "Instruction Intercepts".
|
---|
3828 | * See AMD spec. 15.21.9 "NMI Support".
|
---|
3829 | */
|
---|
3830 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_IRET))
|
---|
3831 | {
|
---|
3832 | Log(("iret: Guest intercept -> #VMEXIT\n"));
|
---|
3833 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
3834 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_IRET, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
3835 | }
|
---|
3836 |
|
---|
3837 | /*
|
---|
3838 | * Clear NMI blocking, if any, before causing any further exceptions.
|
---|
3839 | * See Intel spec. 6.7.1 "Handling Multiple NMIs".
|
---|
3840 | */
|
---|
3841 | if (fBlockingNmi)
|
---|
3842 | VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_BLOCK_NMIS);
|
---|
3843 |
|
---|
3844 | /*
|
---|
3845 | * Call a mode specific worker.
|
---|
3846 | */
|
---|
3847 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
3848 | return IEM_CIMPL_CALL_1(iemCImpl_iret_real_v8086, enmEffOpSize);
|
---|
3849 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SREG_MASK | CPUMCTX_EXTRN_GDTR | CPUMCTX_EXTRN_LDTR);
|
---|
3850 | if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
|
---|
3851 | return IEM_CIMPL_CALL_1(iemCImpl_iret_64bit, enmEffOpSize);
|
---|
3852 | return IEM_CIMPL_CALL_1(iemCImpl_iret_prot, enmEffOpSize);
|
---|
3853 | }
|
---|
3854 |
|
---|
3855 |
|
---|
3856 | static void iemLoadallSetSelector(PVMCPUCC pVCpu, uint8_t iSegReg, uint16_t uSel)
|
---|
3857 | {
|
---|
3858 | PCPUMSELREGHID pHid = iemSRegGetHid(pVCpu, iSegReg);
|
---|
3859 |
|
---|
3860 | pHid->Sel = uSel;
|
---|
3861 | pHid->ValidSel = uSel;
|
---|
3862 | pHid->fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
3863 | }
|
---|
3864 |
|
---|
3865 |
|
---|
3866 | static void iemLoadall286SetDescCache(PVMCPUCC pVCpu, uint8_t iSegReg, uint8_t const *pbMem)
|
---|
3867 | {
|
---|
3868 | PCPUMSELREGHID pHid = iemSRegGetHid(pVCpu, iSegReg);
|
---|
3869 |
|
---|
3870 | /* The base is in the first three bytes. */
|
---|
3871 | pHid->u64Base = pbMem[0] + (pbMem[1] << 8) + (pbMem[2] << 16);
|
---|
3872 | /* The attributes are in the fourth byte. */
|
---|
3873 | pHid->Attr.u = pbMem[3];
|
---|
3874 | /* The limit is in the last two bytes. */
|
---|
3875 | pHid->u32Limit = pbMem[4] + (pbMem[5] << 8);
|
---|
3876 | }
|
---|
3877 |
|
---|
3878 |
|
---|
3879 | /**
|
---|
3880 | * Implements 286 LOADALL (286 CPUs only).
|
---|
3881 | */
|
---|
3882 | IEM_CIMPL_DEF_0(iemCImpl_loadall286)
|
---|
3883 | {
|
---|
3884 | NOREF(cbInstr);
|
---|
3885 |
|
---|
3886 | /* Data is loaded from a buffer at 800h. No checks are done on the
|
---|
3887 | * validity of loaded state.
|
---|
3888 | *
|
---|
3889 | * LOADALL only loads the internal CPU state, it does not access any
|
---|
3890 | * GDT, LDT, or similar tables.
|
---|
3891 | */
|
---|
3892 |
|
---|
3893 | if (pVCpu->iem.s.uCpl != 0)
|
---|
3894 | {
|
---|
3895 | Log(("loadall286: CPL must be 0 not %u -> #GP(0)\n", pVCpu->iem.s.uCpl));
|
---|
3896 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
3897 | }
|
---|
3898 |
|
---|
3899 | uint8_t const *pbMem = NULL;
|
---|
3900 | uint16_t const *pa16Mem;
|
---|
3901 | uint8_t const *pa8Mem;
|
---|
3902 | RTGCPHYS GCPtrStart = 0x800; /* Fixed table location. */
|
---|
3903 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, (void **)&pbMem, 0x66, UINT8_MAX, GCPtrStart, IEM_ACCESS_SYS_R);
|
---|
3904 | if (rcStrict != VINF_SUCCESS)
|
---|
3905 | return rcStrict;
|
---|
3906 |
|
---|
3907 | /* The MSW is at offset 0x06. */
|
---|
3908 | pa16Mem = (uint16_t const *)(pbMem + 0x06);
|
---|
3909 | /* Even LOADALL can't clear the MSW.PE bit, though it can set it. */
|
---|
3910 | uint64_t uNewCr0 = pVCpu->cpum.GstCtx.cr0 & ~(X86_CR0_MP | X86_CR0_EM | X86_CR0_TS);
|
---|
3911 | uNewCr0 |= *pa16Mem & (X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS);
|
---|
3912 | uint64_t const uOldCr0 = pVCpu->cpum.GstCtx.cr0;
|
---|
3913 |
|
---|
3914 | CPUMSetGuestCR0(pVCpu, uNewCr0);
|
---|
3915 | Assert(pVCpu->cpum.GstCtx.cr0 == uNewCr0);
|
---|
3916 |
|
---|
3917 | /* Inform PGM if mode changed. */
|
---|
3918 | if ((uNewCr0 & X86_CR0_PE) != (uOldCr0 & X86_CR0_PE))
|
---|
3919 | {
|
---|
3920 | int rc = PGMFlushTLB(pVCpu, pVCpu->cpum.GstCtx.cr3, true /* global */, false /* fPdpesMapped */);
|
---|
3921 | AssertRCReturn(rc, rc);
|
---|
3922 | /* ignore informational status codes */
|
---|
3923 | }
|
---|
3924 | rcStrict = PGMChangeMode(pVCpu, pVCpu->cpum.GstCtx.cr0, pVCpu->cpum.GstCtx.cr4, pVCpu->cpum.GstCtx.msrEFER,
|
---|
3925 | false /* fForce */);
|
---|
3926 |
|
---|
3927 | /* TR selector is at offset 0x16. */
|
---|
3928 | pa16Mem = (uint16_t const *)(pbMem + 0x16);
|
---|
3929 | pVCpu->cpum.GstCtx.tr.Sel = pa16Mem[0];
|
---|
3930 | pVCpu->cpum.GstCtx.tr.ValidSel = pa16Mem[0];
|
---|
3931 | pVCpu->cpum.GstCtx.tr.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
3932 |
|
---|
3933 | /* Followed by FLAGS... */
|
---|
3934 | pVCpu->cpum.GstCtx.eflags.u = pa16Mem[1] | X86_EFL_1;
|
---|
3935 | pVCpu->cpum.GstCtx.ip = pa16Mem[2]; /* ...and IP. */
|
---|
3936 |
|
---|
3937 | /* LDT is at offset 0x1C. */
|
---|
3938 | pa16Mem = (uint16_t const *)(pbMem + 0x1C);
|
---|
3939 | pVCpu->cpum.GstCtx.ldtr.Sel = pa16Mem[0];
|
---|
3940 | pVCpu->cpum.GstCtx.ldtr.ValidSel = pa16Mem[0];
|
---|
3941 | pVCpu->cpum.GstCtx.ldtr.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
3942 |
|
---|
3943 | /* Segment registers are at offset 0x1E. */
|
---|
3944 | pa16Mem = (uint16_t const *)(pbMem + 0x1E);
|
---|
3945 | iemLoadallSetSelector(pVCpu, X86_SREG_DS, pa16Mem[0]);
|
---|
3946 | iemLoadallSetSelector(pVCpu, X86_SREG_SS, pa16Mem[1]);
|
---|
3947 | iemLoadallSetSelector(pVCpu, X86_SREG_CS, pa16Mem[2]);
|
---|
3948 | iemLoadallSetSelector(pVCpu, X86_SREG_ES, pa16Mem[3]);
|
---|
3949 |
|
---|
3950 | /* GPRs are at offset 0x26. */
|
---|
3951 | pa16Mem = (uint16_t const *)(pbMem + 0x26);
|
---|
3952 | pVCpu->cpum.GstCtx.di = pa16Mem[0];
|
---|
3953 | pVCpu->cpum.GstCtx.si = pa16Mem[1];
|
---|
3954 | pVCpu->cpum.GstCtx.bp = pa16Mem[2];
|
---|
3955 | pVCpu->cpum.GstCtx.sp = pa16Mem[3];
|
---|
3956 | pVCpu->cpum.GstCtx.bx = pa16Mem[4];
|
---|
3957 | pVCpu->cpum.GstCtx.dx = pa16Mem[5];
|
---|
3958 | pVCpu->cpum.GstCtx.cx = pa16Mem[6];
|
---|
3959 | pVCpu->cpum.GstCtx.ax = pa16Mem[7];
|
---|
3960 |
|
---|
3961 | /* Descriptor caches are at offset 0x36, 6 bytes per entry. */
|
---|
3962 | iemLoadall286SetDescCache(pVCpu, X86_SREG_ES, pbMem + 0x36);
|
---|
3963 | iemLoadall286SetDescCache(pVCpu, X86_SREG_CS, pbMem + 0x3C);
|
---|
3964 | iemLoadall286SetDescCache(pVCpu, X86_SREG_SS, pbMem + 0x42);
|
---|
3965 | iemLoadall286SetDescCache(pVCpu, X86_SREG_DS, pbMem + 0x48);
|
---|
3966 |
|
---|
3967 | /* GDTR contents are at offset 0x4E, 6 bytes. */
|
---|
3968 | RTGCPHYS GCPtrBase;
|
---|
3969 | uint16_t cbLimit;
|
---|
3970 | pa8Mem = pbMem + 0x4E;
|
---|
3971 | /* NB: Fourth byte "should be zero"; we are ignoring it. */
|
---|
3972 | GCPtrBase = pa8Mem[0] + (pa8Mem[1] << 8) + (pa8Mem[2] << 16);
|
---|
3973 | cbLimit = pa8Mem[4] + (pa8Mem[5] << 8);
|
---|
3974 | CPUMSetGuestGDTR(pVCpu, GCPtrBase, cbLimit);
|
---|
3975 |
|
---|
3976 | /* IDTR contents are at offset 0x5A, 6 bytes. */
|
---|
3977 | pa8Mem = pbMem + 0x5A;
|
---|
3978 | GCPtrBase = pa8Mem[0] + (pa8Mem[1] << 8) + (pa8Mem[2] << 16);
|
---|
3979 | cbLimit = pa8Mem[4] + (pa8Mem[5] << 8);
|
---|
3980 | CPUMSetGuestIDTR(pVCpu, GCPtrBase, cbLimit);
|
---|
3981 |
|
---|
3982 | 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));
|
---|
3983 | 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));
|
---|
3984 | 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));
|
---|
3985 | 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));
|
---|
3986 | 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));
|
---|
3987 | 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));
|
---|
3988 |
|
---|
3989 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)pbMem, IEM_ACCESS_SYS_R);
|
---|
3990 | if (rcStrict != VINF_SUCCESS)
|
---|
3991 | return rcStrict;
|
---|
3992 |
|
---|
3993 | /* The CPL may change. It is taken from the "DPL fields of the SS and CS
|
---|
3994 | * descriptor caches" but there is no word as to what happens if those are
|
---|
3995 | * not identical (probably bad things).
|
---|
3996 | */
|
---|
3997 | pVCpu->iem.s.uCpl = pVCpu->cpum.GstCtx.cs.Attr.n.u2Dpl;
|
---|
3998 |
|
---|
3999 | CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_HIDDEN_SEL_REGS | CPUM_CHANGED_IDTR | CPUM_CHANGED_GDTR | CPUM_CHANGED_TR | CPUM_CHANGED_LDTR);
|
---|
4000 |
|
---|
4001 | /* Flush the prefetch buffer. */
|
---|
4002 | #ifdef IEM_WITH_CODE_TLB
|
---|
4003 | pVCpu->iem.s.pbInstrBuf = NULL;
|
---|
4004 | #else
|
---|
4005 | pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
|
---|
4006 | #endif
|
---|
4007 | return rcStrict;
|
---|
4008 | }
|
---|
4009 |
|
---|
4010 |
|
---|
4011 | /**
|
---|
4012 | * Implements SYSCALL (AMD and Intel64).
|
---|
4013 | *
|
---|
4014 | * @param enmEffOpSize The effective operand size.
|
---|
4015 | */
|
---|
4016 | IEM_CIMPL_DEF_0(iemCImpl_syscall)
|
---|
4017 | {
|
---|
4018 | /** @todo hack, LOADALL should be decoded as such on a 286. */
|
---|
4019 | if (RT_UNLIKELY(pVCpu->iem.s.uTargetCpu == IEMTARGETCPU_286))
|
---|
4020 | return iemCImpl_loadall286(pVCpu, cbInstr);
|
---|
4021 |
|
---|
4022 | /*
|
---|
4023 | * Check preconditions.
|
---|
4024 | *
|
---|
4025 | * Note that CPUs described in the documentation may load a few odd values
|
---|
4026 | * into CS and SS than we allow here. This has yet to be checked on real
|
---|
4027 | * hardware.
|
---|
4028 | */
|
---|
4029 | if (!(pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_SCE))
|
---|
4030 | {
|
---|
4031 | Log(("syscall: Not enabled in EFER -> #UD\n"));
|
---|
4032 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4033 | }
|
---|
4034 | if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE))
|
---|
4035 | {
|
---|
4036 | Log(("syscall: Protected mode is required -> #GP(0)\n"));
|
---|
4037 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4038 | }
|
---|
4039 | if (IEM_IS_GUEST_CPU_INTEL(pVCpu) && !CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu)))
|
---|
4040 | {
|
---|
4041 | Log(("syscall: Only available in long mode on intel -> #UD\n"));
|
---|
4042 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4043 | }
|
---|
4044 |
|
---|
4045 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SYSCALL_MSRS);
|
---|
4046 |
|
---|
4047 | /** @todo verify RPL ignoring and CS=0xfff8 (i.e. SS == 0). */
|
---|
4048 | /** @todo what about LDT selectors? Shouldn't matter, really. */
|
---|
4049 | uint16_t uNewCs = (pVCpu->cpum.GstCtx.msrSTAR >> MSR_K6_STAR_SYSCALL_CS_SS_SHIFT) & X86_SEL_MASK_OFF_RPL;
|
---|
4050 | uint16_t uNewSs = uNewCs + 8;
|
---|
4051 | if (uNewCs == 0 || uNewSs == 0)
|
---|
4052 | {
|
---|
4053 | Log(("syscall: msrSTAR.CS = 0 or SS = 0 -> #GP(0)\n"));
|
---|
4054 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4055 | }
|
---|
4056 |
|
---|
4057 | /* Long mode and legacy mode differs. */
|
---|
4058 | if (CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu)))
|
---|
4059 | {
|
---|
4060 | uint64_t uNewRip = pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT ? pVCpu->cpum.GstCtx.msrLSTAR : pVCpu->cpum.GstCtx. msrCSTAR;
|
---|
4061 |
|
---|
4062 | /* This test isn't in the docs, but I'm not trusting the guys writing
|
---|
4063 | the MSRs to have validated the values as canonical like they should. */
|
---|
4064 | if (!IEM_IS_CANONICAL(uNewRip))
|
---|
4065 | {
|
---|
4066 | Log(("syscall: Only available in long mode on intel -> #UD\n"));
|
---|
4067 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4068 | }
|
---|
4069 |
|
---|
4070 | /*
|
---|
4071 | * Commit it.
|
---|
4072 | */
|
---|
4073 | Log(("syscall: %04x:%016RX64 [efl=%#llx] -> %04x:%016RX64\n", pVCpu->cpum.GstCtx.cs, pVCpu->cpum.GstCtx.rip, pVCpu->cpum.GstCtx.rflags.u, uNewCs, uNewRip));
|
---|
4074 | pVCpu->cpum.GstCtx.rcx = pVCpu->cpum.GstCtx.rip + cbInstr;
|
---|
4075 | pVCpu->cpum.GstCtx.rip = uNewRip;
|
---|
4076 |
|
---|
4077 | pVCpu->cpum.GstCtx.rflags.u &= ~X86_EFL_RF;
|
---|
4078 | pVCpu->cpum.GstCtx.r11 = pVCpu->cpum.GstCtx.rflags.u;
|
---|
4079 | pVCpu->cpum.GstCtx.rflags.u &= ~pVCpu->cpum.GstCtx.msrSFMASK;
|
---|
4080 | pVCpu->cpum.GstCtx.rflags.u |= X86_EFL_1;
|
---|
4081 |
|
---|
4082 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_L | X86DESCATTR_DT | X86_SEL_TYPE_ER_ACC;
|
---|
4083 | pVCpu->cpum.GstCtx.ss.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_L | X86DESCATTR_DT | X86_SEL_TYPE_RW_ACC;
|
---|
4084 | }
|
---|
4085 | else
|
---|
4086 | {
|
---|
4087 | /*
|
---|
4088 | * Commit it.
|
---|
4089 | */
|
---|
4090 | Log(("syscall: %04x:%08RX32 [efl=%#x] -> %04x:%08RX32\n",
|
---|
4091 | pVCpu->cpum.GstCtx.cs, pVCpu->cpum.GstCtx.eip, pVCpu->cpum.GstCtx.eflags.u, uNewCs, (uint32_t)(pVCpu->cpum.GstCtx.msrSTAR & MSR_K6_STAR_SYSCALL_EIP_MASK)));
|
---|
4092 | pVCpu->cpum.GstCtx.rcx = pVCpu->cpum.GstCtx.eip + cbInstr;
|
---|
4093 | pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.msrSTAR & MSR_K6_STAR_SYSCALL_EIP_MASK;
|
---|
4094 | pVCpu->cpum.GstCtx.rflags.u &= ~(X86_EFL_VM | X86_EFL_IF | X86_EFL_RF);
|
---|
4095 |
|
---|
4096 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_D | X86DESCATTR_DT | X86_SEL_TYPE_ER_ACC;
|
---|
4097 | pVCpu->cpum.GstCtx.ss.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_D | X86DESCATTR_DT | X86_SEL_TYPE_RW_ACC;
|
---|
4098 | }
|
---|
4099 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
|
---|
4100 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
|
---|
4101 | pVCpu->cpum.GstCtx.cs.u64Base = 0;
|
---|
4102 | pVCpu->cpum.GstCtx.cs.u32Limit = UINT32_MAX;
|
---|
4103 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4104 |
|
---|
4105 | pVCpu->cpum.GstCtx.ss.Sel = uNewSs;
|
---|
4106 | pVCpu->cpum.GstCtx.ss.ValidSel = uNewSs;
|
---|
4107 | pVCpu->cpum.GstCtx.ss.u64Base = 0;
|
---|
4108 | pVCpu->cpum.GstCtx.ss.u32Limit = UINT32_MAX;
|
---|
4109 | pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4110 |
|
---|
4111 | /* Flush the prefetch buffer. */
|
---|
4112 | #ifdef IEM_WITH_CODE_TLB
|
---|
4113 | pVCpu->iem.s.pbInstrBuf = NULL;
|
---|
4114 | #else
|
---|
4115 | pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
|
---|
4116 | #endif
|
---|
4117 |
|
---|
4118 | return VINF_SUCCESS;
|
---|
4119 | }
|
---|
4120 |
|
---|
4121 |
|
---|
4122 | /**
|
---|
4123 | * Implements SYSRET (AMD and Intel64).
|
---|
4124 | */
|
---|
4125 | IEM_CIMPL_DEF_0(iemCImpl_sysret)
|
---|
4126 |
|
---|
4127 | {
|
---|
4128 | RT_NOREF_PV(cbInstr);
|
---|
4129 |
|
---|
4130 | /*
|
---|
4131 | * Check preconditions.
|
---|
4132 | *
|
---|
4133 | * Note that CPUs described in the documentation may load a few odd values
|
---|
4134 | * into CS and SS than we allow here. This has yet to be checked on real
|
---|
4135 | * hardware.
|
---|
4136 | */
|
---|
4137 | if (!(pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_SCE))
|
---|
4138 | {
|
---|
4139 | Log(("sysret: Not enabled in EFER -> #UD\n"));
|
---|
4140 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4141 | }
|
---|
4142 | if (IEM_IS_GUEST_CPU_INTEL(pVCpu) && !CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu)))
|
---|
4143 | {
|
---|
4144 | Log(("sysret: Only available in long mode on intel -> #UD\n"));
|
---|
4145 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4146 | }
|
---|
4147 | if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE))
|
---|
4148 | {
|
---|
4149 | Log(("sysret: Protected mode is required -> #GP(0)\n"));
|
---|
4150 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4151 | }
|
---|
4152 | if (pVCpu->iem.s.uCpl != 0)
|
---|
4153 | {
|
---|
4154 | Log(("sysret: CPL must be 0 not %u -> #GP(0)\n", pVCpu->iem.s.uCpl));
|
---|
4155 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4156 | }
|
---|
4157 |
|
---|
4158 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SYSCALL_MSRS);
|
---|
4159 |
|
---|
4160 | /** @todo Does SYSRET verify CS != 0 and SS != 0? Neither is valid in ring-3. */
|
---|
4161 | uint16_t uNewCs = (pVCpu->cpum.GstCtx.msrSTAR >> MSR_K6_STAR_SYSRET_CS_SS_SHIFT) & X86_SEL_MASK_OFF_RPL;
|
---|
4162 | uint16_t uNewSs = uNewCs + 8;
|
---|
4163 | if (pVCpu->iem.s.enmEffOpSize == IEMMODE_64BIT)
|
---|
4164 | uNewCs += 16;
|
---|
4165 | if (uNewCs == 0 || uNewSs == 0)
|
---|
4166 | {
|
---|
4167 | Log(("sysret: msrSTAR.CS = 0 or SS = 0 -> #GP(0)\n"));
|
---|
4168 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4169 | }
|
---|
4170 |
|
---|
4171 | /*
|
---|
4172 | * Commit it.
|
---|
4173 | */
|
---|
4174 | if (CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu)))
|
---|
4175 | {
|
---|
4176 | if (pVCpu->iem.s.enmEffOpSize == IEMMODE_64BIT)
|
---|
4177 | {
|
---|
4178 | Log(("sysret: %04x:%016RX64 [efl=%#llx] -> %04x:%016RX64 [r11=%#llx]\n",
|
---|
4179 | pVCpu->cpum.GstCtx.cs, pVCpu->cpum.GstCtx.rip, pVCpu->cpum.GstCtx.rflags.u, uNewCs, pVCpu->cpum.GstCtx.rcx, pVCpu->cpum.GstCtx.r11));
|
---|
4180 | /* Note! We disregard intel manual regarding the RCX cananonical
|
---|
4181 | check, ask intel+xen why AMD doesn't do it. */
|
---|
4182 | pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.rcx;
|
---|
4183 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_L | X86DESCATTR_DT | X86_SEL_TYPE_ER_ACC
|
---|
4184 | | (3 << X86DESCATTR_DPL_SHIFT);
|
---|
4185 | }
|
---|
4186 | else
|
---|
4187 | {
|
---|
4188 | Log(("sysret: %04x:%016RX64 [efl=%#llx] -> %04x:%08RX32 [r11=%#llx]\n",
|
---|
4189 | pVCpu->cpum.GstCtx.cs, pVCpu->cpum.GstCtx.rip, pVCpu->cpum.GstCtx.rflags.u, uNewCs, pVCpu->cpum.GstCtx.ecx, pVCpu->cpum.GstCtx.r11));
|
---|
4190 | pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.ecx;
|
---|
4191 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_D | X86DESCATTR_DT | X86_SEL_TYPE_ER_ACC
|
---|
4192 | | (3 << X86DESCATTR_DPL_SHIFT);
|
---|
4193 | }
|
---|
4194 | /** @todo testcase: See what kind of flags we can make SYSRET restore and
|
---|
4195 | * what it really ignores. RF and VM are hinted at being zero, by AMD. */
|
---|
4196 | pVCpu->cpum.GstCtx.rflags.u = pVCpu->cpum.GstCtx.r11 & (X86_EFL_POPF_BITS | X86_EFL_VIF | X86_EFL_VIP);
|
---|
4197 | pVCpu->cpum.GstCtx.rflags.u |= X86_EFL_1;
|
---|
4198 | }
|
---|
4199 | else
|
---|
4200 | {
|
---|
4201 | Log(("sysret: %04x:%08RX32 [efl=%#x] -> %04x:%08RX32\n", pVCpu->cpum.GstCtx.cs, pVCpu->cpum.GstCtx.eip, pVCpu->cpum.GstCtx.eflags.u, uNewCs, pVCpu->cpum.GstCtx.ecx));
|
---|
4202 | pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.rcx;
|
---|
4203 | pVCpu->cpum.GstCtx.rflags.u |= X86_EFL_IF;
|
---|
4204 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_D | X86DESCATTR_DT | X86_SEL_TYPE_ER_ACC
|
---|
4205 | | (3 << X86DESCATTR_DPL_SHIFT);
|
---|
4206 | }
|
---|
4207 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs | 3;
|
---|
4208 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs | 3;
|
---|
4209 | pVCpu->cpum.GstCtx.cs.u64Base = 0;
|
---|
4210 | pVCpu->cpum.GstCtx.cs.u32Limit = UINT32_MAX;
|
---|
4211 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4212 |
|
---|
4213 | pVCpu->cpum.GstCtx.ss.Sel = uNewSs | 3;
|
---|
4214 | pVCpu->cpum.GstCtx.ss.ValidSel = uNewSs | 3;
|
---|
4215 | pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4216 | /* The SS hidden bits remains unchanged says AMD. To that I say "Yeah, right!". */
|
---|
4217 | pVCpu->cpum.GstCtx.ss.Attr.u |= (3 << X86DESCATTR_DPL_SHIFT);
|
---|
4218 | /** @todo Testcase: verify that SS.u1Long and SS.u1DefBig are left unchanged
|
---|
4219 | * on sysret. */
|
---|
4220 |
|
---|
4221 | /* Flush the prefetch buffer. */
|
---|
4222 | #ifdef IEM_WITH_CODE_TLB
|
---|
4223 | pVCpu->iem.s.pbInstrBuf = NULL;
|
---|
4224 | #else
|
---|
4225 | pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
|
---|
4226 | #endif
|
---|
4227 |
|
---|
4228 | return VINF_SUCCESS;
|
---|
4229 | }
|
---|
4230 |
|
---|
4231 |
|
---|
4232 | /**
|
---|
4233 | * Implements SYSENTER (Intel, 32-bit AMD).
|
---|
4234 | */
|
---|
4235 | IEM_CIMPL_DEF_0(iemCImpl_sysenter)
|
---|
4236 | {
|
---|
4237 | RT_NOREF(cbInstr);
|
---|
4238 |
|
---|
4239 | /*
|
---|
4240 | * Check preconditions.
|
---|
4241 | *
|
---|
4242 | * Note that CPUs described in the documentation may load a few odd values
|
---|
4243 | * into CS and SS than we allow here. This has yet to be checked on real
|
---|
4244 | * hardware.
|
---|
4245 | */
|
---|
4246 | if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fSysEnter)
|
---|
4247 | {
|
---|
4248 | Log(("sysenter: not supported -=> #UD\n"));
|
---|
4249 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4250 | }
|
---|
4251 | if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE))
|
---|
4252 | {
|
---|
4253 | Log(("sysenter: Protected or long mode is required -> #GP(0)\n"));
|
---|
4254 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4255 | }
|
---|
4256 | bool fIsLongMode = CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu));
|
---|
4257 | if (IEM_IS_GUEST_CPU_AMD(pVCpu) && !fIsLongMode)
|
---|
4258 | {
|
---|
4259 | Log(("sysenter: Only available in protected mode on AMD -> #UD\n"));
|
---|
4260 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4261 | }
|
---|
4262 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SYSENTER_MSRS);
|
---|
4263 | uint16_t uNewCs = pVCpu->cpum.GstCtx.SysEnter.cs;
|
---|
4264 | if ((uNewCs & X86_SEL_MASK_OFF_RPL) == 0)
|
---|
4265 | {
|
---|
4266 | Log(("sysenter: SYSENTER_CS = %#x -> #GP(0)\n", uNewCs));
|
---|
4267 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4268 | }
|
---|
4269 |
|
---|
4270 | /* This test isn't in the docs, it's just a safeguard against missing
|
---|
4271 | canonical checks when writing the registers. */
|
---|
4272 | if (RT_LIKELY( !fIsLongMode
|
---|
4273 | || ( IEM_IS_CANONICAL(pVCpu->cpum.GstCtx.SysEnter.eip)
|
---|
4274 | && IEM_IS_CANONICAL(pVCpu->cpum.GstCtx.SysEnter.esp))))
|
---|
4275 | { /* likely */ }
|
---|
4276 | else
|
---|
4277 | {
|
---|
4278 | Log(("sysenter: SYSENTER_EIP = %#RX64 or/and SYSENTER_ESP = %#RX64 not canonical -> #GP(0)\n",
|
---|
4279 | pVCpu->cpum.GstCtx.SysEnter.eip, pVCpu->cpum.GstCtx.SysEnter.esp));
|
---|
4280 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4281 | }
|
---|
4282 |
|
---|
4283 | /** @todo Test: Sysenter from ring-0, ring-1 and ring-2. */
|
---|
4284 |
|
---|
4285 | /*
|
---|
4286 | * Update registers and commit.
|
---|
4287 | */
|
---|
4288 | if (fIsLongMode)
|
---|
4289 | {
|
---|
4290 | Log(("sysenter: %04x:%016RX64 [efl=%#llx] -> %04x:%016RX64\n", pVCpu->cpum.GstCtx.cs, pVCpu->cpum.GstCtx.rip,
|
---|
4291 | pVCpu->cpum.GstCtx.rflags.u, uNewCs & X86_SEL_MASK_OFF_RPL, pVCpu->cpum.GstCtx.SysEnter.eip));
|
---|
4292 | pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.SysEnter.eip;
|
---|
4293 | pVCpu->cpum.GstCtx.rsp = pVCpu->cpum.GstCtx.SysEnter.esp;
|
---|
4294 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_L | X86DESCATTR_G | X86DESCATTR_P | X86DESCATTR_DT
|
---|
4295 | | X86DESCATTR_LIMIT_HIGH | X86_SEL_TYPE_ER_ACC;
|
---|
4296 | }
|
---|
4297 | else
|
---|
4298 | {
|
---|
4299 | Log(("sysenter: %04x:%08RX32 [efl=%#llx] -> %04x:%08RX32\n", pVCpu->cpum.GstCtx.cs, (uint32_t)pVCpu->cpum.GstCtx.rip,
|
---|
4300 | pVCpu->cpum.GstCtx.rflags.u, uNewCs & X86_SEL_MASK_OFF_RPL, (uint32_t)pVCpu->cpum.GstCtx.SysEnter.eip));
|
---|
4301 | pVCpu->cpum.GstCtx.rip = (uint32_t)pVCpu->cpum.GstCtx.SysEnter.eip;
|
---|
4302 | pVCpu->cpum.GstCtx.rsp = (uint32_t)pVCpu->cpum.GstCtx.SysEnter.esp;
|
---|
4303 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_D | X86DESCATTR_G | X86DESCATTR_P | X86DESCATTR_DT
|
---|
4304 | | X86DESCATTR_LIMIT_HIGH | X86_SEL_TYPE_ER_ACC;
|
---|
4305 | }
|
---|
4306 | pVCpu->cpum.GstCtx.cs.Sel = uNewCs & X86_SEL_MASK_OFF_RPL;
|
---|
4307 | pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs & X86_SEL_MASK_OFF_RPL;
|
---|
4308 | pVCpu->cpum.GstCtx.cs.u64Base = 0;
|
---|
4309 | pVCpu->cpum.GstCtx.cs.u32Limit = UINT32_MAX;
|
---|
4310 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4311 |
|
---|
4312 | pVCpu->cpum.GstCtx.ss.Sel = (uNewCs & X86_SEL_MASK_OFF_RPL) + 8;
|
---|
4313 | pVCpu->cpum.GstCtx.ss.ValidSel = (uNewCs & X86_SEL_MASK_OFF_RPL) + 8;
|
---|
4314 | pVCpu->cpum.GstCtx.ss.u64Base = 0;
|
---|
4315 | pVCpu->cpum.GstCtx.ss.u32Limit = UINT32_MAX;
|
---|
4316 | pVCpu->cpum.GstCtx.ss.Attr.u = X86DESCATTR_D | X86DESCATTR_G | X86DESCATTR_P | X86DESCATTR_DT
|
---|
4317 | | X86DESCATTR_LIMIT_HIGH | X86_SEL_TYPE_RW_ACC;
|
---|
4318 | pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4319 |
|
---|
4320 | pVCpu->cpum.GstCtx.rflags.Bits.u1IF = 0;
|
---|
4321 | pVCpu->cpum.GstCtx.rflags.Bits.u1VM = 0;
|
---|
4322 | pVCpu->cpum.GstCtx.rflags.Bits.u1RF = 0;
|
---|
4323 |
|
---|
4324 | pVCpu->iem.s.uCpl = 0;
|
---|
4325 |
|
---|
4326 | /* Flush the prefetch buffer. */
|
---|
4327 | #ifdef IEM_WITH_CODE_TLB
|
---|
4328 | pVCpu->iem.s.pbInstrBuf = NULL;
|
---|
4329 | #else
|
---|
4330 | pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
|
---|
4331 | #endif
|
---|
4332 |
|
---|
4333 | return VINF_SUCCESS;
|
---|
4334 | }
|
---|
4335 |
|
---|
4336 |
|
---|
4337 | /**
|
---|
4338 | * Implements SYSEXIT (Intel, 32-bit AMD).
|
---|
4339 | *
|
---|
4340 | * @param enmEffOpSize The effective operand size.
|
---|
4341 | */
|
---|
4342 | IEM_CIMPL_DEF_1(iemCImpl_sysexit, IEMMODE, enmEffOpSize)
|
---|
4343 | {
|
---|
4344 | RT_NOREF(cbInstr);
|
---|
4345 |
|
---|
4346 | /*
|
---|
4347 | * Check preconditions.
|
---|
4348 | *
|
---|
4349 | * Note that CPUs described in the documentation may load a few odd values
|
---|
4350 | * into CS and SS than we allow here. This has yet to be checked on real
|
---|
4351 | * hardware.
|
---|
4352 | */
|
---|
4353 | if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fSysEnter)
|
---|
4354 | {
|
---|
4355 | Log(("sysexit: not supported -=> #UD\n"));
|
---|
4356 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4357 | }
|
---|
4358 | if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE))
|
---|
4359 | {
|
---|
4360 | Log(("sysexit: Protected or long mode is required -> #GP(0)\n"));
|
---|
4361 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4362 | }
|
---|
4363 | bool fIsLongMode = CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu));
|
---|
4364 | if (IEM_IS_GUEST_CPU_AMD(pVCpu) && !fIsLongMode)
|
---|
4365 | {
|
---|
4366 | Log(("sysexit: Only available in protected mode on AMD -> #UD\n"));
|
---|
4367 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
4368 | }
|
---|
4369 | if (pVCpu->iem.s.uCpl != 0)
|
---|
4370 | {
|
---|
4371 | Log(("sysexit: CPL(=%u) != 0 -> #GP(0)\n", pVCpu->iem.s.uCpl));
|
---|
4372 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4373 | }
|
---|
4374 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SYSENTER_MSRS);
|
---|
4375 | uint16_t uNewCs = pVCpu->cpum.GstCtx.SysEnter.cs;
|
---|
4376 | if ((uNewCs & X86_SEL_MASK_OFF_RPL) == 0)
|
---|
4377 | {
|
---|
4378 | Log(("sysexit: SYSENTER_CS = %#x -> #GP(0)\n", uNewCs));
|
---|
4379 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4380 | }
|
---|
4381 |
|
---|
4382 | /*
|
---|
4383 | * Update registers and commit.
|
---|
4384 | */
|
---|
4385 | if (enmEffOpSize == IEMMODE_64BIT)
|
---|
4386 | {
|
---|
4387 | Log(("sysexit: %04x:%016RX64 [efl=%#llx] -> %04x:%016RX64\n", pVCpu->cpum.GstCtx.cs, pVCpu->cpum.GstCtx.rip,
|
---|
4388 | pVCpu->cpum.GstCtx.rflags.u, (uNewCs | 3) + 32, pVCpu->cpum.GstCtx.rcx));
|
---|
4389 | pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.rdx;
|
---|
4390 | pVCpu->cpum.GstCtx.rsp = pVCpu->cpum.GstCtx.rcx;
|
---|
4391 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_L | X86DESCATTR_G | X86DESCATTR_P | X86DESCATTR_DT
|
---|
4392 | | X86DESCATTR_LIMIT_HIGH | X86_SEL_TYPE_ER_ACC | (3 << X86DESCATTR_DPL_SHIFT);
|
---|
4393 | pVCpu->cpum.GstCtx.cs.Sel = (uNewCs | 3) + 32;
|
---|
4394 | pVCpu->cpum.GstCtx.cs.ValidSel = (uNewCs | 3) + 32;
|
---|
4395 | pVCpu->cpum.GstCtx.ss.Sel = (uNewCs | 3) + 40;
|
---|
4396 | pVCpu->cpum.GstCtx.ss.ValidSel = (uNewCs | 3) + 40;
|
---|
4397 | }
|
---|
4398 | else
|
---|
4399 | {
|
---|
4400 | Log(("sysexit: %04x:%08RX64 [efl=%#llx] -> %04x:%08RX32\n", pVCpu->cpum.GstCtx.cs, pVCpu->cpum.GstCtx.rip,
|
---|
4401 | pVCpu->cpum.GstCtx.rflags.u, (uNewCs | 3) + 16, (uint32_t)pVCpu->cpum.GstCtx.edx));
|
---|
4402 | pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.edx;
|
---|
4403 | pVCpu->cpum.GstCtx.rsp = pVCpu->cpum.GstCtx.ecx;
|
---|
4404 | pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_D | X86DESCATTR_G | X86DESCATTR_P | X86DESCATTR_DT
|
---|
4405 | | X86DESCATTR_LIMIT_HIGH | X86_SEL_TYPE_ER_ACC | (3 << X86DESCATTR_DPL_SHIFT);
|
---|
4406 | pVCpu->cpum.GstCtx.cs.Sel = (uNewCs | 3) + 16;
|
---|
4407 | pVCpu->cpum.GstCtx.cs.ValidSel = (uNewCs | 3) + 16;
|
---|
4408 | pVCpu->cpum.GstCtx.ss.Sel = (uNewCs | 3) + 24;
|
---|
4409 | pVCpu->cpum.GstCtx.ss.ValidSel = (uNewCs | 3) + 24;
|
---|
4410 | }
|
---|
4411 | pVCpu->cpum.GstCtx.cs.u64Base = 0;
|
---|
4412 | pVCpu->cpum.GstCtx.cs.u32Limit = UINT32_MAX;
|
---|
4413 | pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4414 |
|
---|
4415 | pVCpu->cpum.GstCtx.ss.u64Base = 0;
|
---|
4416 | pVCpu->cpum.GstCtx.ss.u32Limit = UINT32_MAX;
|
---|
4417 | pVCpu->cpum.GstCtx.ss.Attr.u = X86DESCATTR_D | X86DESCATTR_G | X86DESCATTR_P | X86DESCATTR_DT
|
---|
4418 | | X86DESCATTR_LIMIT_HIGH | X86_SEL_TYPE_RW_ACC | (3 << X86DESCATTR_DPL_SHIFT);
|
---|
4419 | pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4420 | pVCpu->cpum.GstCtx.rflags.Bits.u1RF = 0;
|
---|
4421 |
|
---|
4422 | pVCpu->iem.s.uCpl = 3;
|
---|
4423 |
|
---|
4424 | /* Flush the prefetch buffer. */
|
---|
4425 | #ifdef IEM_WITH_CODE_TLB
|
---|
4426 | pVCpu->iem.s.pbInstrBuf = NULL;
|
---|
4427 | #else
|
---|
4428 | pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
|
---|
4429 | #endif
|
---|
4430 |
|
---|
4431 | return VINF_SUCCESS;
|
---|
4432 | }
|
---|
4433 |
|
---|
4434 |
|
---|
4435 | /**
|
---|
4436 | * Common worker for 'pop SReg', 'mov SReg, GReg' and 'lXs GReg, reg/mem'.
|
---|
4437 | *
|
---|
4438 | * @param iSegReg The segment register number (valid).
|
---|
4439 | * @param uSel The new selector value.
|
---|
4440 | */
|
---|
4441 | IEM_CIMPL_DEF_2(iemCImpl_LoadSReg, uint8_t, iSegReg, uint16_t, uSel)
|
---|
4442 | {
|
---|
4443 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SREG_FROM_IDX(iSegReg));
|
---|
4444 | uint16_t *pSel = iemSRegRef(pVCpu, iSegReg);
|
---|
4445 | PCPUMSELREGHID pHid = iemSRegGetHid(pVCpu, iSegReg);
|
---|
4446 |
|
---|
4447 | Assert(iSegReg <= X86_SREG_GS && iSegReg != X86_SREG_CS);
|
---|
4448 |
|
---|
4449 | /*
|
---|
4450 | * Real mode and V8086 mode are easy.
|
---|
4451 | */
|
---|
4452 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
4453 | {
|
---|
4454 | *pSel = uSel;
|
---|
4455 | pHid->u64Base = (uint32_t)uSel << 4;
|
---|
4456 | pHid->ValidSel = uSel;
|
---|
4457 | pHid->fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4458 | #if 0 /* AMD Volume 2, chapter 4.1 - "real mode segmentation" - states that limit and attributes are untouched. */
|
---|
4459 | /** @todo Does the CPU actually load limits and attributes in the
|
---|
4460 | * real/V8086 mode segment load case? It doesn't for CS in far
|
---|
4461 | * jumps... Affects unreal mode. */
|
---|
4462 | pHid->u32Limit = 0xffff;
|
---|
4463 | pHid->Attr.u = 0;
|
---|
4464 | pHid->Attr.n.u1Present = 1;
|
---|
4465 | pHid->Attr.n.u1DescType = 1;
|
---|
4466 | pHid->Attr.n.u4Type = iSegReg != X86_SREG_CS
|
---|
4467 | ? X86_SEL_TYPE_RW
|
---|
4468 | : X86_SEL_TYPE_READ | X86_SEL_TYPE_CODE;
|
---|
4469 | #endif
|
---|
4470 | CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_HIDDEN_SEL_REGS);
|
---|
4471 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
4472 | return VINF_SUCCESS;
|
---|
4473 | }
|
---|
4474 |
|
---|
4475 | /*
|
---|
4476 | * Protected mode.
|
---|
4477 | *
|
---|
4478 | * Check if it's a null segment selector value first, that's OK for DS, ES,
|
---|
4479 | * FS and GS. If not null, then we have to load and parse the descriptor.
|
---|
4480 | */
|
---|
4481 | if (!(uSel & X86_SEL_MASK_OFF_RPL))
|
---|
4482 | {
|
---|
4483 | Assert(iSegReg != X86_SREG_CS); /** @todo testcase for \#UD on MOV CS, ax! */
|
---|
4484 | if (iSegReg == X86_SREG_SS)
|
---|
4485 | {
|
---|
4486 | /* In 64-bit kernel mode, the stack can be 0 because of the way
|
---|
4487 | interrupts are dispatched. AMD seems to have a slighly more
|
---|
4488 | relaxed relationship to SS.RPL than intel does. */
|
---|
4489 | /** @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! */
|
---|
4490 | if ( pVCpu->iem.s.enmCpuMode != IEMMODE_64BIT
|
---|
4491 | || pVCpu->iem.s.uCpl > 2
|
---|
4492 | || ( uSel != pVCpu->iem.s.uCpl
|
---|
4493 | && !IEM_IS_GUEST_CPU_AMD(pVCpu)) )
|
---|
4494 | {
|
---|
4495 | Log(("load sreg %#x -> invalid stack selector, #GP(0)\n", uSel));
|
---|
4496 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4497 | }
|
---|
4498 | }
|
---|
4499 |
|
---|
4500 | *pSel = uSel; /* Not RPL, remember :-) */
|
---|
4501 | iemHlpLoadNullDataSelectorProt(pVCpu, pHid, uSel);
|
---|
4502 | if (iSegReg == X86_SREG_SS)
|
---|
4503 | pHid->Attr.u |= pVCpu->iem.s.uCpl << X86DESCATTR_DPL_SHIFT;
|
---|
4504 |
|
---|
4505 | Assert(CPUMSELREG_ARE_HIDDEN_PARTS_VALID(pVCpu, pHid));
|
---|
4506 | CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_HIDDEN_SEL_REGS);
|
---|
4507 |
|
---|
4508 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
4509 | return VINF_SUCCESS;
|
---|
4510 | }
|
---|
4511 |
|
---|
4512 | /* Fetch the descriptor. */
|
---|
4513 | IEMSELDESC Desc;
|
---|
4514 | VBOXSTRICTRC rcStrict = iemMemFetchSelDesc(pVCpu, &Desc, uSel, X86_XCPT_GP); /** @todo Correct exception? */
|
---|
4515 | if (rcStrict != VINF_SUCCESS)
|
---|
4516 | return rcStrict;
|
---|
4517 |
|
---|
4518 | /* Check GPs first. */
|
---|
4519 | if (!Desc.Legacy.Gen.u1DescType)
|
---|
4520 | {
|
---|
4521 | Log(("load sreg %d (=%#x) - system selector (%#x) -> #GP\n", iSegReg, uSel, Desc.Legacy.Gen.u4Type));
|
---|
4522 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
4523 | }
|
---|
4524 | if (iSegReg == X86_SREG_SS) /* SS gets different treatment */
|
---|
4525 | {
|
---|
4526 | if ( (Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE)
|
---|
4527 | || !(Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_WRITE) )
|
---|
4528 | {
|
---|
4529 | Log(("load sreg SS, %#x - code or read only (%#x) -> #GP\n", uSel, Desc.Legacy.Gen.u4Type));
|
---|
4530 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
4531 | }
|
---|
4532 | if ((uSel & X86_SEL_RPL) != pVCpu->iem.s.uCpl)
|
---|
4533 | {
|
---|
4534 | Log(("load sreg SS, %#x - RPL and CPL (%d) differs -> #GP\n", uSel, pVCpu->iem.s.uCpl));
|
---|
4535 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
4536 | }
|
---|
4537 | if (Desc.Legacy.Gen.u2Dpl != pVCpu->iem.s.uCpl)
|
---|
4538 | {
|
---|
4539 | Log(("load sreg SS, %#x - DPL (%d) and CPL (%d) differs -> #GP\n", uSel, Desc.Legacy.Gen.u2Dpl, pVCpu->iem.s.uCpl));
|
---|
4540 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
4541 | }
|
---|
4542 | }
|
---|
4543 | else
|
---|
4544 | {
|
---|
4545 | if ((Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_READ)) == X86_SEL_TYPE_CODE)
|
---|
4546 | {
|
---|
4547 | Log(("load sreg%u, %#x - execute only segment -> #GP\n", iSegReg, uSel));
|
---|
4548 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
4549 | }
|
---|
4550 | if ( (Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
|
---|
4551 | != (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
|
---|
4552 | {
|
---|
4553 | #if 0 /* this is what intel says. */
|
---|
4554 | if ( (uSel & X86_SEL_RPL) > Desc.Legacy.Gen.u2Dpl
|
---|
4555 | && pVCpu->iem.s.uCpl > Desc.Legacy.Gen.u2Dpl)
|
---|
4556 | {
|
---|
4557 | Log(("load sreg%u, %#x - both RPL (%d) and CPL (%d) are greater than DPL (%d) -> #GP\n",
|
---|
4558 | iSegReg, uSel, (uSel & X86_SEL_RPL), pVCpu->iem.s.uCpl, Desc.Legacy.Gen.u2Dpl));
|
---|
4559 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
4560 | }
|
---|
4561 | #else /* this is what makes more sense. */
|
---|
4562 | if ((unsigned)(uSel & X86_SEL_RPL) > Desc.Legacy.Gen.u2Dpl)
|
---|
4563 | {
|
---|
4564 | Log(("load sreg%u, %#x - RPL (%d) is greater than DPL (%d) -> #GP\n",
|
---|
4565 | iSegReg, uSel, (uSel & X86_SEL_RPL), Desc.Legacy.Gen.u2Dpl));
|
---|
4566 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
4567 | }
|
---|
4568 | if (pVCpu->iem.s.uCpl > Desc.Legacy.Gen.u2Dpl)
|
---|
4569 | {
|
---|
4570 | Log(("load sreg%u, %#x - CPL (%d) is greater than DPL (%d) -> #GP\n",
|
---|
4571 | iSegReg, uSel, pVCpu->iem.s.uCpl, Desc.Legacy.Gen.u2Dpl));
|
---|
4572 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
|
---|
4573 | }
|
---|
4574 | #endif
|
---|
4575 | }
|
---|
4576 | }
|
---|
4577 |
|
---|
4578 | /* Is it there? */
|
---|
4579 | if (!Desc.Legacy.Gen.u1Present)
|
---|
4580 | {
|
---|
4581 | Log(("load sreg%d,%#x - segment not present -> #NP\n", iSegReg, uSel));
|
---|
4582 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uSel);
|
---|
4583 | }
|
---|
4584 |
|
---|
4585 | /* The base and limit. */
|
---|
4586 | uint32_t cbLimit = X86DESC_LIMIT_G(&Desc.Legacy);
|
---|
4587 | uint64_t u64Base = X86DESC_BASE(&Desc.Legacy);
|
---|
4588 |
|
---|
4589 | /*
|
---|
4590 | * Ok, everything checked out fine. Now set the accessed bit before
|
---|
4591 | * committing the result into the registers.
|
---|
4592 | */
|
---|
4593 | if (!(Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
|
---|
4594 | {
|
---|
4595 | rcStrict = iemMemMarkSelDescAccessed(pVCpu, uSel);
|
---|
4596 | if (rcStrict != VINF_SUCCESS)
|
---|
4597 | return rcStrict;
|
---|
4598 | Desc.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
|
---|
4599 | }
|
---|
4600 |
|
---|
4601 | /* commit */
|
---|
4602 | *pSel = uSel;
|
---|
4603 | pHid->Attr.u = X86DESC_GET_HID_ATTR(&Desc.Legacy);
|
---|
4604 | pHid->u32Limit = cbLimit;
|
---|
4605 | pHid->u64Base = u64Base;
|
---|
4606 | pHid->ValidSel = uSel;
|
---|
4607 | pHid->fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
4608 |
|
---|
4609 | /** @todo check if the hidden bits are loaded correctly for 64-bit
|
---|
4610 | * mode. */
|
---|
4611 | Assert(CPUMSELREG_ARE_HIDDEN_PARTS_VALID(pVCpu, pHid));
|
---|
4612 |
|
---|
4613 | CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_HIDDEN_SEL_REGS);
|
---|
4614 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
4615 | return VINF_SUCCESS;
|
---|
4616 | }
|
---|
4617 |
|
---|
4618 |
|
---|
4619 | /**
|
---|
4620 | * Implements 'mov SReg, r/m'.
|
---|
4621 | *
|
---|
4622 | * @param iSegReg The segment register number (valid).
|
---|
4623 | * @param uSel The new selector value.
|
---|
4624 | */
|
---|
4625 | IEM_CIMPL_DEF_2(iemCImpl_load_SReg, uint8_t, iSegReg, uint16_t, uSel)
|
---|
4626 | {
|
---|
4627 | VBOXSTRICTRC rcStrict = IEM_CIMPL_CALL_2(iemCImpl_LoadSReg, iSegReg, uSel);
|
---|
4628 | if (rcStrict == VINF_SUCCESS)
|
---|
4629 | {
|
---|
4630 | if (iSegReg == X86_SREG_SS)
|
---|
4631 | EMSetInhibitInterruptsPC(pVCpu, pVCpu->cpum.GstCtx.rip);
|
---|
4632 | }
|
---|
4633 | return rcStrict;
|
---|
4634 | }
|
---|
4635 |
|
---|
4636 |
|
---|
4637 | /**
|
---|
4638 | * Implements 'pop SReg'.
|
---|
4639 | *
|
---|
4640 | * @param iSegReg The segment register number (valid).
|
---|
4641 | * @param enmEffOpSize The efficient operand size (valid).
|
---|
4642 | */
|
---|
4643 | IEM_CIMPL_DEF_2(iemCImpl_pop_Sreg, uint8_t, iSegReg, IEMMODE, enmEffOpSize)
|
---|
4644 | {
|
---|
4645 | VBOXSTRICTRC rcStrict;
|
---|
4646 |
|
---|
4647 | /*
|
---|
4648 | * Read the selector off the stack and join paths with mov ss, reg.
|
---|
4649 | */
|
---|
4650 | RTUINT64U TmpRsp;
|
---|
4651 | TmpRsp.u = pVCpu->cpum.GstCtx.rsp;
|
---|
4652 | switch (enmEffOpSize)
|
---|
4653 | {
|
---|
4654 | case IEMMODE_16BIT:
|
---|
4655 | {
|
---|
4656 | uint16_t uSel;
|
---|
4657 | rcStrict = iemMemStackPopU16Ex(pVCpu, &uSel, &TmpRsp);
|
---|
4658 | if (rcStrict == VINF_SUCCESS)
|
---|
4659 | rcStrict = IEM_CIMPL_CALL_2(iemCImpl_LoadSReg, iSegReg, uSel);
|
---|
4660 | break;
|
---|
4661 | }
|
---|
4662 |
|
---|
4663 | case IEMMODE_32BIT:
|
---|
4664 | {
|
---|
4665 | uint32_t u32Value;
|
---|
4666 | rcStrict = iemMemStackPopU32Ex(pVCpu, &u32Value, &TmpRsp);
|
---|
4667 | if (rcStrict == VINF_SUCCESS)
|
---|
4668 | rcStrict = IEM_CIMPL_CALL_2(iemCImpl_LoadSReg, iSegReg, (uint16_t)u32Value);
|
---|
4669 | break;
|
---|
4670 | }
|
---|
4671 |
|
---|
4672 | case IEMMODE_64BIT:
|
---|
4673 | {
|
---|
4674 | uint64_t u64Value;
|
---|
4675 | rcStrict = iemMemStackPopU64Ex(pVCpu, &u64Value, &TmpRsp);
|
---|
4676 | if (rcStrict == VINF_SUCCESS)
|
---|
4677 | rcStrict = IEM_CIMPL_CALL_2(iemCImpl_LoadSReg, iSegReg, (uint16_t)u64Value);
|
---|
4678 | break;
|
---|
4679 | }
|
---|
4680 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
4681 | }
|
---|
4682 |
|
---|
4683 | /*
|
---|
4684 | * Commit the stack on success.
|
---|
4685 | */
|
---|
4686 | if (rcStrict == VINF_SUCCESS)
|
---|
4687 | {
|
---|
4688 | pVCpu->cpum.GstCtx.rsp = TmpRsp.u;
|
---|
4689 | if (iSegReg == X86_SREG_SS)
|
---|
4690 | EMSetInhibitInterruptsPC(pVCpu, pVCpu->cpum.GstCtx.rip);
|
---|
4691 | }
|
---|
4692 | return rcStrict;
|
---|
4693 | }
|
---|
4694 |
|
---|
4695 |
|
---|
4696 | /**
|
---|
4697 | * Implements lgs, lfs, les, lds & lss.
|
---|
4698 | */
|
---|
4699 | IEM_CIMPL_DEF_5(iemCImpl_load_SReg_Greg,
|
---|
4700 | uint16_t, uSel,
|
---|
4701 | uint64_t, offSeg,
|
---|
4702 | uint8_t, iSegReg,
|
---|
4703 | uint8_t, iGReg,
|
---|
4704 | IEMMODE, enmEffOpSize)
|
---|
4705 | {
|
---|
4706 | /*
|
---|
4707 | * Use iemCImpl_LoadSReg to do the tricky segment register loading.
|
---|
4708 | */
|
---|
4709 | /** @todo verify and test that mov, pop and lXs works the segment
|
---|
4710 | * register loading in the exact same way. */
|
---|
4711 | VBOXSTRICTRC rcStrict = IEM_CIMPL_CALL_2(iemCImpl_LoadSReg, iSegReg, uSel);
|
---|
4712 | if (rcStrict == VINF_SUCCESS)
|
---|
4713 | {
|
---|
4714 | switch (enmEffOpSize)
|
---|
4715 | {
|
---|
4716 | case IEMMODE_16BIT:
|
---|
4717 | *(uint16_t *)iemGRegRef(pVCpu, iGReg) = offSeg;
|
---|
4718 | break;
|
---|
4719 | case IEMMODE_32BIT:
|
---|
4720 | *(uint64_t *)iemGRegRef(pVCpu, iGReg) = offSeg;
|
---|
4721 | break;
|
---|
4722 | case IEMMODE_64BIT:
|
---|
4723 | *(uint64_t *)iemGRegRef(pVCpu, iGReg) = offSeg;
|
---|
4724 | break;
|
---|
4725 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
4726 | }
|
---|
4727 | }
|
---|
4728 |
|
---|
4729 | return rcStrict;
|
---|
4730 | }
|
---|
4731 |
|
---|
4732 |
|
---|
4733 | /**
|
---|
4734 | * Helper for VERR, VERW, LAR, and LSL and loads the descriptor into memory.
|
---|
4735 | *
|
---|
4736 | * @retval VINF_SUCCESS on success.
|
---|
4737 | * @retval VINF_IEM_SELECTOR_NOT_OK if the selector isn't ok.
|
---|
4738 | * @retval iemMemFetchSysU64 return value.
|
---|
4739 | *
|
---|
4740 | * @param pVCpu The cross context virtual CPU structure of the calling thread.
|
---|
4741 | * @param uSel The selector value.
|
---|
4742 | * @param fAllowSysDesc Whether system descriptors are OK or not.
|
---|
4743 | * @param pDesc Where to return the descriptor on success.
|
---|
4744 | */
|
---|
4745 | static VBOXSTRICTRC iemCImpl_LoadDescHelper(PVMCPUCC pVCpu, uint16_t uSel, bool fAllowSysDesc, PIEMSELDESC pDesc)
|
---|
4746 | {
|
---|
4747 | pDesc->Long.au64[0] = 0;
|
---|
4748 | pDesc->Long.au64[1] = 0;
|
---|
4749 |
|
---|
4750 | if (!(uSel & X86_SEL_MASK_OFF_RPL)) /** @todo test this on 64-bit. */
|
---|
4751 | return VINF_IEM_SELECTOR_NOT_OK;
|
---|
4752 |
|
---|
4753 | /* Within the table limits? */
|
---|
4754 | RTGCPTR GCPtrBase;
|
---|
4755 | if (uSel & X86_SEL_LDT)
|
---|
4756 | {
|
---|
4757 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_LDTR);
|
---|
4758 | if ( !pVCpu->cpum.GstCtx.ldtr.Attr.n.u1Present
|
---|
4759 | || (uSel | X86_SEL_RPL_LDT) > pVCpu->cpum.GstCtx.ldtr.u32Limit )
|
---|
4760 | return VINF_IEM_SELECTOR_NOT_OK;
|
---|
4761 | GCPtrBase = pVCpu->cpum.GstCtx.ldtr.u64Base;
|
---|
4762 | }
|
---|
4763 | else
|
---|
4764 | {
|
---|
4765 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_GDTR);
|
---|
4766 | if ((uSel | X86_SEL_RPL_LDT) > pVCpu->cpum.GstCtx.gdtr.cbGdt)
|
---|
4767 | return VINF_IEM_SELECTOR_NOT_OK;
|
---|
4768 | GCPtrBase = pVCpu->cpum.GstCtx.gdtr.pGdt;
|
---|
4769 | }
|
---|
4770 |
|
---|
4771 | /* Fetch the descriptor. */
|
---|
4772 | VBOXSTRICTRC rcStrict = iemMemFetchSysU64(pVCpu, &pDesc->Legacy.u, UINT8_MAX, GCPtrBase + (uSel & X86_SEL_MASK));
|
---|
4773 | if (rcStrict != VINF_SUCCESS)
|
---|
4774 | return rcStrict;
|
---|
4775 | if (!pDesc->Legacy.Gen.u1DescType)
|
---|
4776 | {
|
---|
4777 | if (!fAllowSysDesc)
|
---|
4778 | return VINF_IEM_SELECTOR_NOT_OK;
|
---|
4779 | if (CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu)))
|
---|
4780 | {
|
---|
4781 | rcStrict = iemMemFetchSysU64(pVCpu, &pDesc->Long.au64[1], UINT8_MAX, GCPtrBase + (uSel & X86_SEL_MASK) + 8);
|
---|
4782 | if (rcStrict != VINF_SUCCESS)
|
---|
4783 | return rcStrict;
|
---|
4784 | }
|
---|
4785 |
|
---|
4786 | }
|
---|
4787 |
|
---|
4788 | return VINF_SUCCESS;
|
---|
4789 | }
|
---|
4790 |
|
---|
4791 |
|
---|
4792 | /**
|
---|
4793 | * Implements verr (fWrite = false) and verw (fWrite = true).
|
---|
4794 | */
|
---|
4795 | IEM_CIMPL_DEF_2(iemCImpl_VerX, uint16_t, uSel, bool, fWrite)
|
---|
4796 | {
|
---|
4797 | Assert(!IEM_IS_REAL_OR_V86_MODE(pVCpu));
|
---|
4798 |
|
---|
4799 | /** @todo figure whether the accessed bit is set or not. */
|
---|
4800 |
|
---|
4801 | bool fAccessible = true;
|
---|
4802 | IEMSELDESC Desc;
|
---|
4803 | VBOXSTRICTRC rcStrict = iemCImpl_LoadDescHelper(pVCpu, uSel, false /*fAllowSysDesc*/, &Desc);
|
---|
4804 | if (rcStrict == VINF_SUCCESS)
|
---|
4805 | {
|
---|
4806 | /* Check the descriptor, order doesn't matter much here. */
|
---|
4807 | if ( !Desc.Legacy.Gen.u1DescType
|
---|
4808 | || !Desc.Legacy.Gen.u1Present)
|
---|
4809 | fAccessible = false;
|
---|
4810 | else
|
---|
4811 | {
|
---|
4812 | if ( fWrite
|
---|
4813 | ? (Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_WRITE)) != X86_SEL_TYPE_WRITE
|
---|
4814 | : (Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_READ)) == X86_SEL_TYPE_CODE)
|
---|
4815 | fAccessible = false;
|
---|
4816 |
|
---|
4817 | /** @todo testcase for the conforming behavior. */
|
---|
4818 | if ( (Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
|
---|
4819 | != (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
|
---|
4820 | {
|
---|
4821 | if ((unsigned)(uSel & X86_SEL_RPL) > Desc.Legacy.Gen.u2Dpl)
|
---|
4822 | fAccessible = false;
|
---|
4823 | else if (pVCpu->iem.s.uCpl > Desc.Legacy.Gen.u2Dpl)
|
---|
4824 | fAccessible = false;
|
---|
4825 | }
|
---|
4826 | }
|
---|
4827 |
|
---|
4828 | }
|
---|
4829 | else if (rcStrict == VINF_IEM_SELECTOR_NOT_OK)
|
---|
4830 | fAccessible = false;
|
---|
4831 | else
|
---|
4832 | return rcStrict;
|
---|
4833 |
|
---|
4834 | /* commit */
|
---|
4835 | pVCpu->cpum.GstCtx.eflags.Bits.u1ZF = fAccessible;
|
---|
4836 |
|
---|
4837 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
4838 | return VINF_SUCCESS;
|
---|
4839 | }
|
---|
4840 |
|
---|
4841 |
|
---|
4842 | /**
|
---|
4843 | * Implements LAR and LSL with 64-bit operand size.
|
---|
4844 | *
|
---|
4845 | * @returns VINF_SUCCESS.
|
---|
4846 | * @param pu16Dst Pointer to the destination register.
|
---|
4847 | * @param uSel The selector to load details for.
|
---|
4848 | * @param fIsLar true = LAR, false = LSL.
|
---|
4849 | */
|
---|
4850 | IEM_CIMPL_DEF_3(iemCImpl_LarLsl_u64, uint64_t *, pu64Dst, uint16_t, uSel, bool, fIsLar)
|
---|
4851 | {
|
---|
4852 | Assert(!IEM_IS_REAL_OR_V86_MODE(pVCpu));
|
---|
4853 |
|
---|
4854 | /** @todo figure whether the accessed bit is set or not. */
|
---|
4855 |
|
---|
4856 | bool fDescOk = true;
|
---|
4857 | IEMSELDESC Desc;
|
---|
4858 | VBOXSTRICTRC rcStrict = iemCImpl_LoadDescHelper(pVCpu, uSel, true /*fAllowSysDesc*/, &Desc);
|
---|
4859 | if (rcStrict == VINF_SUCCESS)
|
---|
4860 | {
|
---|
4861 | /*
|
---|
4862 | * Check the descriptor type.
|
---|
4863 | */
|
---|
4864 | if (!Desc.Legacy.Gen.u1DescType)
|
---|
4865 | {
|
---|
4866 | if (CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu)))
|
---|
4867 | {
|
---|
4868 | if (Desc.Long.Gen.u5Zeros)
|
---|
4869 | fDescOk = false;
|
---|
4870 | else
|
---|
4871 | switch (Desc.Long.Gen.u4Type)
|
---|
4872 | {
|
---|
4873 | /** @todo Intel lists 0 as valid for LSL, verify whether that's correct */
|
---|
4874 | case AMD64_SEL_TYPE_SYS_TSS_AVAIL:
|
---|
4875 | case AMD64_SEL_TYPE_SYS_TSS_BUSY:
|
---|
4876 | case AMD64_SEL_TYPE_SYS_LDT: /** @todo Intel lists this as invalid for LAR, AMD and 32-bit does otherwise. */
|
---|
4877 | break;
|
---|
4878 | case AMD64_SEL_TYPE_SYS_CALL_GATE:
|
---|
4879 | fDescOk = fIsLar;
|
---|
4880 | break;
|
---|
4881 | default:
|
---|
4882 | fDescOk = false;
|
---|
4883 | break;
|
---|
4884 | }
|
---|
4885 | }
|
---|
4886 | else
|
---|
4887 | {
|
---|
4888 | switch (Desc.Long.Gen.u4Type)
|
---|
4889 | {
|
---|
4890 | case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
|
---|
4891 | case X86_SEL_TYPE_SYS_286_TSS_BUSY:
|
---|
4892 | case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
|
---|
4893 | case X86_SEL_TYPE_SYS_386_TSS_BUSY:
|
---|
4894 | case X86_SEL_TYPE_SYS_LDT:
|
---|
4895 | break;
|
---|
4896 | case X86_SEL_TYPE_SYS_286_CALL_GATE:
|
---|
4897 | case X86_SEL_TYPE_SYS_TASK_GATE:
|
---|
4898 | case X86_SEL_TYPE_SYS_386_CALL_GATE:
|
---|
4899 | fDescOk = fIsLar;
|
---|
4900 | break;
|
---|
4901 | default:
|
---|
4902 | fDescOk = false;
|
---|
4903 | break;
|
---|
4904 | }
|
---|
4905 | }
|
---|
4906 | }
|
---|
4907 | if (fDescOk)
|
---|
4908 | {
|
---|
4909 | /*
|
---|
4910 | * Check the RPL/DPL/CPL interaction..
|
---|
4911 | */
|
---|
4912 | /** @todo testcase for the conforming behavior. */
|
---|
4913 | if ( (Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF)) != (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF)
|
---|
4914 | || !Desc.Legacy.Gen.u1DescType)
|
---|
4915 | {
|
---|
4916 | if ((unsigned)(uSel & X86_SEL_RPL) > Desc.Legacy.Gen.u2Dpl)
|
---|
4917 | fDescOk = false;
|
---|
4918 | else if (pVCpu->iem.s.uCpl > Desc.Legacy.Gen.u2Dpl)
|
---|
4919 | fDescOk = false;
|
---|
4920 | }
|
---|
4921 | }
|
---|
4922 |
|
---|
4923 | if (fDescOk)
|
---|
4924 | {
|
---|
4925 | /*
|
---|
4926 | * All fine, start committing the result.
|
---|
4927 | */
|
---|
4928 | if (fIsLar)
|
---|
4929 | *pu64Dst = Desc.Legacy.au32[1] & UINT32_C(0x00ffff00);
|
---|
4930 | else
|
---|
4931 | *pu64Dst = X86DESC_LIMIT_G(&Desc.Legacy);
|
---|
4932 | }
|
---|
4933 |
|
---|
4934 | }
|
---|
4935 | else if (rcStrict == VINF_IEM_SELECTOR_NOT_OK)
|
---|
4936 | fDescOk = false;
|
---|
4937 | else
|
---|
4938 | return rcStrict;
|
---|
4939 |
|
---|
4940 | /* commit flags value and advance rip. */
|
---|
4941 | pVCpu->cpum.GstCtx.eflags.Bits.u1ZF = fDescOk;
|
---|
4942 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
4943 |
|
---|
4944 | return VINF_SUCCESS;
|
---|
4945 | }
|
---|
4946 |
|
---|
4947 |
|
---|
4948 | /**
|
---|
4949 | * Implements LAR and LSL with 16-bit operand size.
|
---|
4950 | *
|
---|
4951 | * @returns VINF_SUCCESS.
|
---|
4952 | * @param pu16Dst Pointer to the destination register.
|
---|
4953 | * @param u16Sel The selector to load details for.
|
---|
4954 | * @param fIsLar true = LAR, false = LSL.
|
---|
4955 | */
|
---|
4956 | IEM_CIMPL_DEF_3(iemCImpl_LarLsl_u16, uint16_t *, pu16Dst, uint16_t, uSel, bool, fIsLar)
|
---|
4957 | {
|
---|
4958 | uint64_t u64TmpDst = *pu16Dst;
|
---|
4959 | IEM_CIMPL_CALL_3(iemCImpl_LarLsl_u64, &u64TmpDst, uSel, fIsLar);
|
---|
4960 | *pu16Dst = u64TmpDst;
|
---|
4961 | return VINF_SUCCESS;
|
---|
4962 | }
|
---|
4963 |
|
---|
4964 |
|
---|
4965 | /**
|
---|
4966 | * Implements lgdt.
|
---|
4967 | *
|
---|
4968 | * @param iEffSeg The segment of the new gdtr contents
|
---|
4969 | * @param GCPtrEffSrc The address of the new gdtr contents.
|
---|
4970 | * @param enmEffOpSize The effective operand size.
|
---|
4971 | */
|
---|
4972 | IEM_CIMPL_DEF_3(iemCImpl_lgdt, uint8_t, iEffSeg, RTGCPTR, GCPtrEffSrc, IEMMODE, enmEffOpSize)
|
---|
4973 | {
|
---|
4974 | if (pVCpu->iem.s.uCpl != 0)
|
---|
4975 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
4976 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
4977 |
|
---|
4978 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
4979 | && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
|
---|
4980 | {
|
---|
4981 | Log(("lgdt: Guest intercept -> VM-exit\n"));
|
---|
4982 | IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_GDTR_IDTR_ACCESS, VMXINSTRID_LGDT, cbInstr);
|
---|
4983 | }
|
---|
4984 |
|
---|
4985 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_GDTR_WRITES))
|
---|
4986 | {
|
---|
4987 | Log(("lgdt: Guest intercept -> #VMEXIT\n"));
|
---|
4988 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
4989 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_GDTR_WRITE, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
4990 | }
|
---|
4991 |
|
---|
4992 | /*
|
---|
4993 | * Fetch the limit and base address.
|
---|
4994 | */
|
---|
4995 | uint16_t cbLimit;
|
---|
4996 | RTGCPTR GCPtrBase;
|
---|
4997 | VBOXSTRICTRC rcStrict = iemMemFetchDataXdtr(pVCpu, &cbLimit, &GCPtrBase, iEffSeg, GCPtrEffSrc, enmEffOpSize);
|
---|
4998 | if (rcStrict == VINF_SUCCESS)
|
---|
4999 | {
|
---|
5000 | if ( pVCpu->iem.s.enmCpuMode != IEMMODE_64BIT
|
---|
5001 | || X86_IS_CANONICAL(GCPtrBase))
|
---|
5002 | {
|
---|
5003 | rcStrict = CPUMSetGuestGDTR(pVCpu, GCPtrBase, cbLimit);
|
---|
5004 | if (rcStrict == VINF_SUCCESS)
|
---|
5005 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
5006 | }
|
---|
5007 | else
|
---|
5008 | {
|
---|
5009 | Log(("iemCImpl_lgdt: Non-canonical base %04x:%RGv\n", cbLimit, GCPtrBase));
|
---|
5010 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5011 | }
|
---|
5012 | }
|
---|
5013 | return rcStrict;
|
---|
5014 | }
|
---|
5015 |
|
---|
5016 |
|
---|
5017 | /**
|
---|
5018 | * Implements sgdt.
|
---|
5019 | *
|
---|
5020 | * @param iEffSeg The segment where to store the gdtr content.
|
---|
5021 | * @param GCPtrEffDst The address where to store the gdtr content.
|
---|
5022 | */
|
---|
5023 | IEM_CIMPL_DEF_2(iemCImpl_sgdt, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
|
---|
5024 | {
|
---|
5025 | /*
|
---|
5026 | * Join paths with sidt.
|
---|
5027 | * Note! No CPL or V8086 checks here, it's a really sad story, ask Intel if
|
---|
5028 | * you really must know.
|
---|
5029 | */
|
---|
5030 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
5031 | && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
|
---|
5032 | {
|
---|
5033 | Log(("sgdt: Guest intercept -> VM-exit\n"));
|
---|
5034 | IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_GDTR_IDTR_ACCESS, VMXINSTRID_SGDT, cbInstr);
|
---|
5035 | }
|
---|
5036 |
|
---|
5037 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_GDTR_READS))
|
---|
5038 | {
|
---|
5039 | Log(("sgdt: Guest intercept -> #VMEXIT\n"));
|
---|
5040 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
5041 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_GDTR_READ, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
5042 | }
|
---|
5043 |
|
---|
5044 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_GDTR);
|
---|
5045 | VBOXSTRICTRC rcStrict = iemMemStoreDataXdtr(pVCpu, pVCpu->cpum.GstCtx.gdtr.cbGdt, pVCpu->cpum.GstCtx.gdtr.pGdt, iEffSeg, GCPtrEffDst);
|
---|
5046 | if (rcStrict == VINF_SUCCESS)
|
---|
5047 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
5048 | return rcStrict;
|
---|
5049 | }
|
---|
5050 |
|
---|
5051 |
|
---|
5052 | /**
|
---|
5053 | * Implements lidt.
|
---|
5054 | *
|
---|
5055 | * @param iEffSeg The segment of the new idtr contents
|
---|
5056 | * @param GCPtrEffSrc The address of the new idtr contents.
|
---|
5057 | * @param enmEffOpSize The effective operand size.
|
---|
5058 | */
|
---|
5059 | IEM_CIMPL_DEF_3(iemCImpl_lidt, uint8_t, iEffSeg, RTGCPTR, GCPtrEffSrc, IEMMODE, enmEffOpSize)
|
---|
5060 | {
|
---|
5061 | if (pVCpu->iem.s.uCpl != 0)
|
---|
5062 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5063 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
5064 |
|
---|
5065 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_IDTR_WRITES))
|
---|
5066 | {
|
---|
5067 | Log(("lidt: Guest intercept -> #VMEXIT\n"));
|
---|
5068 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
5069 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_IDTR_WRITE, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
5070 | }
|
---|
5071 |
|
---|
5072 | /*
|
---|
5073 | * Fetch the limit and base address.
|
---|
5074 | */
|
---|
5075 | uint16_t cbLimit;
|
---|
5076 | RTGCPTR GCPtrBase;
|
---|
5077 | VBOXSTRICTRC rcStrict = iemMemFetchDataXdtr(pVCpu, &cbLimit, &GCPtrBase, iEffSeg, GCPtrEffSrc, enmEffOpSize);
|
---|
5078 | if (rcStrict == VINF_SUCCESS)
|
---|
5079 | {
|
---|
5080 | if ( pVCpu->iem.s.enmCpuMode != IEMMODE_64BIT
|
---|
5081 | || X86_IS_CANONICAL(GCPtrBase))
|
---|
5082 | {
|
---|
5083 | CPUMSetGuestIDTR(pVCpu, GCPtrBase, cbLimit);
|
---|
5084 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
5085 | }
|
---|
5086 | else
|
---|
5087 | {
|
---|
5088 | Log(("iemCImpl_lidt: Non-canonical base %04x:%RGv\n", cbLimit, GCPtrBase));
|
---|
5089 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5090 | }
|
---|
5091 | }
|
---|
5092 | return rcStrict;
|
---|
5093 | }
|
---|
5094 |
|
---|
5095 |
|
---|
5096 | /**
|
---|
5097 | * Implements sidt.
|
---|
5098 | *
|
---|
5099 | * @param iEffSeg The segment where to store the idtr content.
|
---|
5100 | * @param GCPtrEffDst The address where to store the idtr content.
|
---|
5101 | */
|
---|
5102 | IEM_CIMPL_DEF_2(iemCImpl_sidt, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
|
---|
5103 | {
|
---|
5104 | /*
|
---|
5105 | * Join paths with sgdt.
|
---|
5106 | * Note! No CPL or V8086 checks here, it's a really sad story, ask Intel if
|
---|
5107 | * you really must know.
|
---|
5108 | */
|
---|
5109 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_IDTR_READS))
|
---|
5110 | {
|
---|
5111 | Log(("sidt: Guest intercept -> #VMEXIT\n"));
|
---|
5112 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
5113 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_IDTR_READ, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
5114 | }
|
---|
5115 |
|
---|
5116 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_IDTR);
|
---|
5117 | VBOXSTRICTRC rcStrict = iemMemStoreDataXdtr(pVCpu, pVCpu->cpum.GstCtx.idtr.cbIdt, pVCpu->cpum.GstCtx.idtr.pIdt, iEffSeg, GCPtrEffDst);
|
---|
5118 | if (rcStrict == VINF_SUCCESS)
|
---|
5119 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
5120 | return rcStrict;
|
---|
5121 | }
|
---|
5122 |
|
---|
5123 |
|
---|
5124 | /**
|
---|
5125 | * Implements lldt.
|
---|
5126 | *
|
---|
5127 | * @param uNewLdt The new LDT selector value.
|
---|
5128 | */
|
---|
5129 | IEM_CIMPL_DEF_1(iemCImpl_lldt, uint16_t, uNewLdt)
|
---|
5130 | {
|
---|
5131 | /*
|
---|
5132 | * Check preconditions.
|
---|
5133 | */
|
---|
5134 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
5135 | {
|
---|
5136 | Log(("lldt %04x - real or v8086 mode -> #GP(0)\n", uNewLdt));
|
---|
5137 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
5138 | }
|
---|
5139 | if (pVCpu->iem.s.uCpl != 0)
|
---|
5140 | {
|
---|
5141 | Log(("lldt %04x - CPL is %d -> #GP(0)\n", uNewLdt, pVCpu->iem.s.uCpl));
|
---|
5142 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5143 | }
|
---|
5144 | /* Nested-guest VMX intercept. */
|
---|
5145 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
5146 | && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
|
---|
5147 | {
|
---|
5148 | Log(("lldt: Guest intercept -> VM-exit\n"));
|
---|
5149 | IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_LDTR_TR_ACCESS, VMXINSTRID_LLDT, cbInstr);
|
---|
5150 | }
|
---|
5151 | if (uNewLdt & X86_SEL_LDT)
|
---|
5152 | {
|
---|
5153 | Log(("lldt %04x - LDT selector -> #GP\n", uNewLdt));
|
---|
5154 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewLdt);
|
---|
5155 | }
|
---|
5156 |
|
---|
5157 | /*
|
---|
5158 | * Now, loading a NULL selector is easy.
|
---|
5159 | */
|
---|
5160 | if (!(uNewLdt & X86_SEL_MASK_OFF_RPL))
|
---|
5161 | {
|
---|
5162 | /* Nested-guest SVM intercept. */
|
---|
5163 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_LDTR_WRITES))
|
---|
5164 | {
|
---|
5165 | Log(("lldt: Guest intercept -> #VMEXIT\n"));
|
---|
5166 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
5167 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_LDTR_WRITE, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
5168 | }
|
---|
5169 |
|
---|
5170 | Log(("lldt %04x: Loading NULL selector.\n", uNewLdt));
|
---|
5171 | pVCpu->cpum.GstCtx.fExtrn &= ~CPUMCTX_EXTRN_LDTR;
|
---|
5172 | CPUMSetGuestLDTR(pVCpu, uNewLdt);
|
---|
5173 | pVCpu->cpum.GstCtx.ldtr.ValidSel = uNewLdt;
|
---|
5174 | pVCpu->cpum.GstCtx.ldtr.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
5175 | if (IEM_IS_GUEST_CPU_AMD(pVCpu))
|
---|
5176 | {
|
---|
5177 | /* AMD-V seems to leave the base and limit alone. */
|
---|
5178 | pVCpu->cpum.GstCtx.ldtr.Attr.u = X86DESCATTR_UNUSABLE;
|
---|
5179 | }
|
---|
5180 | else
|
---|
5181 | {
|
---|
5182 | /* VT-x (Intel 3960x) seems to be doing the following. */
|
---|
5183 | pVCpu->cpum.GstCtx.ldtr.Attr.u = X86DESCATTR_UNUSABLE | X86DESCATTR_G | X86DESCATTR_D;
|
---|
5184 | pVCpu->cpum.GstCtx.ldtr.u64Base = 0;
|
---|
5185 | pVCpu->cpum.GstCtx.ldtr.u32Limit = UINT32_MAX;
|
---|
5186 | }
|
---|
5187 |
|
---|
5188 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
5189 | return VINF_SUCCESS;
|
---|
5190 | }
|
---|
5191 |
|
---|
5192 | /*
|
---|
5193 | * Read the descriptor.
|
---|
5194 | */
|
---|
5195 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_LDTR | CPUMCTX_EXTRN_GDTR);
|
---|
5196 | IEMSELDESC Desc;
|
---|
5197 | VBOXSTRICTRC rcStrict = iemMemFetchSelDesc(pVCpu, &Desc, uNewLdt, X86_XCPT_GP); /** @todo Correct exception? */
|
---|
5198 | if (rcStrict != VINF_SUCCESS)
|
---|
5199 | return rcStrict;
|
---|
5200 |
|
---|
5201 | /* Check GPs first. */
|
---|
5202 | if (Desc.Legacy.Gen.u1DescType)
|
---|
5203 | {
|
---|
5204 | Log(("lldt %#x - not system selector (type %x) -> #GP\n", uNewLdt, Desc.Legacy.Gen.u4Type));
|
---|
5205 | return iemRaiseGeneralProtectionFault(pVCpu, uNewLdt & X86_SEL_MASK_OFF_RPL);
|
---|
5206 | }
|
---|
5207 | if (Desc.Legacy.Gen.u4Type != X86_SEL_TYPE_SYS_LDT)
|
---|
5208 | {
|
---|
5209 | Log(("lldt %#x - not LDT selector (type %x) -> #GP\n", uNewLdt, Desc.Legacy.Gen.u4Type));
|
---|
5210 | return iemRaiseGeneralProtectionFault(pVCpu, uNewLdt & X86_SEL_MASK_OFF_RPL);
|
---|
5211 | }
|
---|
5212 | uint64_t u64Base;
|
---|
5213 | if (!IEM_IS_LONG_MODE(pVCpu))
|
---|
5214 | u64Base = X86DESC_BASE(&Desc.Legacy);
|
---|
5215 | else
|
---|
5216 | {
|
---|
5217 | if (Desc.Long.Gen.u5Zeros)
|
---|
5218 | {
|
---|
5219 | Log(("lldt %#x - u5Zeros=%#x -> #GP\n", uNewLdt, Desc.Long.Gen.u5Zeros));
|
---|
5220 | return iemRaiseGeneralProtectionFault(pVCpu, uNewLdt & X86_SEL_MASK_OFF_RPL);
|
---|
5221 | }
|
---|
5222 |
|
---|
5223 | u64Base = X86DESC64_BASE(&Desc.Long);
|
---|
5224 | if (!IEM_IS_CANONICAL(u64Base))
|
---|
5225 | {
|
---|
5226 | Log(("lldt %#x - non-canonical base address %#llx -> #GP\n", uNewLdt, u64Base));
|
---|
5227 | return iemRaiseGeneralProtectionFault(pVCpu, uNewLdt & X86_SEL_MASK_OFF_RPL);
|
---|
5228 | }
|
---|
5229 | }
|
---|
5230 |
|
---|
5231 | /* NP */
|
---|
5232 | if (!Desc.Legacy.Gen.u1Present)
|
---|
5233 | {
|
---|
5234 | Log(("lldt %#x - segment not present -> #NP\n", uNewLdt));
|
---|
5235 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uNewLdt);
|
---|
5236 | }
|
---|
5237 |
|
---|
5238 | /* Nested-guest SVM intercept. */
|
---|
5239 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_LDTR_WRITES))
|
---|
5240 | {
|
---|
5241 | Log(("lldt: Guest intercept -> #VMEXIT\n"));
|
---|
5242 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
5243 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_LDTR_WRITE, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
5244 | }
|
---|
5245 |
|
---|
5246 | /*
|
---|
5247 | * It checks out alright, update the registers.
|
---|
5248 | */
|
---|
5249 | /** @todo check if the actual value is loaded or if the RPL is dropped */
|
---|
5250 | CPUMSetGuestLDTR(pVCpu, uNewLdt & X86_SEL_MASK_OFF_RPL);
|
---|
5251 | pVCpu->cpum.GstCtx.ldtr.ValidSel = uNewLdt & X86_SEL_MASK_OFF_RPL;
|
---|
5252 | pVCpu->cpum.GstCtx.ldtr.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
5253 | pVCpu->cpum.GstCtx.ldtr.Attr.u = X86DESC_GET_HID_ATTR(&Desc.Legacy);
|
---|
5254 | pVCpu->cpum.GstCtx.ldtr.u32Limit = X86DESC_LIMIT_G(&Desc.Legacy);
|
---|
5255 | pVCpu->cpum.GstCtx.ldtr.u64Base = u64Base;
|
---|
5256 |
|
---|
5257 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
5258 | return VINF_SUCCESS;
|
---|
5259 | }
|
---|
5260 |
|
---|
5261 |
|
---|
5262 | /**
|
---|
5263 | * Implements sldt GReg
|
---|
5264 | *
|
---|
5265 | * @param iGReg The general register to store the CRx value in.
|
---|
5266 | * @param enmEffOpSize The operand size.
|
---|
5267 | */
|
---|
5268 | IEM_CIMPL_DEF_2(iemCImpl_sldt_reg, uint8_t, iGReg, uint8_t, enmEffOpSize)
|
---|
5269 | {
|
---|
5270 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
5271 | && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
|
---|
5272 | {
|
---|
5273 | Log(("sldt: Guest intercept -> VM-exit\n"));
|
---|
5274 | IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_LDTR_TR_ACCESS, VMXINSTRID_SLDT, cbInstr);
|
---|
5275 | }
|
---|
5276 |
|
---|
5277 | IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_LDTR_READS, SVM_EXIT_LDTR_READ, 0, 0);
|
---|
5278 |
|
---|
5279 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_LDTR);
|
---|
5280 | switch (enmEffOpSize)
|
---|
5281 | {
|
---|
5282 | case IEMMODE_16BIT: *(uint16_t *)iemGRegRef(pVCpu, iGReg) = pVCpu->cpum.GstCtx.ldtr.Sel; break;
|
---|
5283 | case IEMMODE_32BIT: *(uint64_t *)iemGRegRef(pVCpu, iGReg) = pVCpu->cpum.GstCtx.ldtr.Sel; break;
|
---|
5284 | case IEMMODE_64BIT: *(uint64_t *)iemGRegRef(pVCpu, iGReg) = pVCpu->cpum.GstCtx.ldtr.Sel; break;
|
---|
5285 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
5286 | }
|
---|
5287 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
5288 | return VINF_SUCCESS;
|
---|
5289 | }
|
---|
5290 |
|
---|
5291 |
|
---|
5292 | /**
|
---|
5293 | * Implements sldt mem.
|
---|
5294 | *
|
---|
5295 | * @param iGReg The general register to store the CRx value in.
|
---|
5296 | * @param iEffSeg The effective segment register to use with @a GCPtrMem.
|
---|
5297 | * @param GCPtrEffDst Where to store the 16-bit CR0 value.
|
---|
5298 | */
|
---|
5299 | IEM_CIMPL_DEF_2(iemCImpl_sldt_mem, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
|
---|
5300 | {
|
---|
5301 | IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_LDTR_READS, SVM_EXIT_LDTR_READ, 0, 0);
|
---|
5302 |
|
---|
5303 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_LDTR);
|
---|
5304 | VBOXSTRICTRC rcStrict = iemMemStoreDataU16(pVCpu, iEffSeg, GCPtrEffDst, pVCpu->cpum.GstCtx.ldtr.Sel);
|
---|
5305 | if (rcStrict == VINF_SUCCESS)
|
---|
5306 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
5307 | return rcStrict;
|
---|
5308 | }
|
---|
5309 |
|
---|
5310 |
|
---|
5311 | /**
|
---|
5312 | * Implements ltr.
|
---|
5313 | *
|
---|
5314 | * @param uNewTr The new TSS selector value.
|
---|
5315 | */
|
---|
5316 | IEM_CIMPL_DEF_1(iemCImpl_ltr, uint16_t, uNewTr)
|
---|
5317 | {
|
---|
5318 | /*
|
---|
5319 | * Check preconditions.
|
---|
5320 | */
|
---|
5321 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
5322 | {
|
---|
5323 | Log(("ltr %04x - real or v8086 mode -> #GP(0)\n", uNewTr));
|
---|
5324 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
5325 | }
|
---|
5326 | if (pVCpu->iem.s.uCpl != 0)
|
---|
5327 | {
|
---|
5328 | Log(("ltr %04x - CPL is %d -> #GP(0)\n", uNewTr, pVCpu->iem.s.uCpl));
|
---|
5329 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5330 | }
|
---|
5331 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
5332 | && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
|
---|
5333 | {
|
---|
5334 | Log(("ltr: Guest intercept -> VM-exit\n"));
|
---|
5335 | IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_LDTR_TR_ACCESS, VMXINSTRID_LTR, cbInstr);
|
---|
5336 | }
|
---|
5337 | if (uNewTr & X86_SEL_LDT)
|
---|
5338 | {
|
---|
5339 | Log(("ltr %04x - LDT selector -> #GP\n", uNewTr));
|
---|
5340 | return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewTr);
|
---|
5341 | }
|
---|
5342 | if (!(uNewTr & X86_SEL_MASK_OFF_RPL))
|
---|
5343 | {
|
---|
5344 | Log(("ltr %04x - NULL selector -> #GP(0)\n", uNewTr));
|
---|
5345 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5346 | }
|
---|
5347 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_TR_WRITES))
|
---|
5348 | {
|
---|
5349 | Log(("ltr: Guest intercept -> #VMEXIT\n"));
|
---|
5350 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
5351 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_TR_WRITE, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
5352 | }
|
---|
5353 |
|
---|
5354 | /*
|
---|
5355 | * Read the descriptor.
|
---|
5356 | */
|
---|
5357 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_LDTR | CPUMCTX_EXTRN_GDTR | CPUMCTX_EXTRN_TR);
|
---|
5358 | IEMSELDESC Desc;
|
---|
5359 | VBOXSTRICTRC rcStrict = iemMemFetchSelDesc(pVCpu, &Desc, uNewTr, X86_XCPT_GP); /** @todo Correct exception? */
|
---|
5360 | if (rcStrict != VINF_SUCCESS)
|
---|
5361 | return rcStrict;
|
---|
5362 |
|
---|
5363 | /* Check GPs first. */
|
---|
5364 | if (Desc.Legacy.Gen.u1DescType)
|
---|
5365 | {
|
---|
5366 | Log(("ltr %#x - not system selector (type %x) -> #GP\n", uNewTr, Desc.Legacy.Gen.u4Type));
|
---|
5367 | return iemRaiseGeneralProtectionFault(pVCpu, uNewTr & X86_SEL_MASK_OFF_RPL);
|
---|
5368 | }
|
---|
5369 | if ( Desc.Legacy.Gen.u4Type != X86_SEL_TYPE_SYS_386_TSS_AVAIL /* same as AMD64_SEL_TYPE_SYS_TSS_AVAIL */
|
---|
5370 | && ( Desc.Legacy.Gen.u4Type != X86_SEL_TYPE_SYS_286_TSS_AVAIL
|
---|
5371 | || IEM_IS_LONG_MODE(pVCpu)) )
|
---|
5372 | {
|
---|
5373 | Log(("ltr %#x - not an available TSS selector (type %x) -> #GP\n", uNewTr, Desc.Legacy.Gen.u4Type));
|
---|
5374 | return iemRaiseGeneralProtectionFault(pVCpu, uNewTr & X86_SEL_MASK_OFF_RPL);
|
---|
5375 | }
|
---|
5376 | uint64_t u64Base;
|
---|
5377 | if (!IEM_IS_LONG_MODE(pVCpu))
|
---|
5378 | u64Base = X86DESC_BASE(&Desc.Legacy);
|
---|
5379 | else
|
---|
5380 | {
|
---|
5381 | if (Desc.Long.Gen.u5Zeros)
|
---|
5382 | {
|
---|
5383 | Log(("ltr %#x - u5Zeros=%#x -> #GP\n", uNewTr, Desc.Long.Gen.u5Zeros));
|
---|
5384 | return iemRaiseGeneralProtectionFault(pVCpu, uNewTr & X86_SEL_MASK_OFF_RPL);
|
---|
5385 | }
|
---|
5386 |
|
---|
5387 | u64Base = X86DESC64_BASE(&Desc.Long);
|
---|
5388 | if (!IEM_IS_CANONICAL(u64Base))
|
---|
5389 | {
|
---|
5390 | Log(("ltr %#x - non-canonical base address %#llx -> #GP\n", uNewTr, u64Base));
|
---|
5391 | return iemRaiseGeneralProtectionFault(pVCpu, uNewTr & X86_SEL_MASK_OFF_RPL);
|
---|
5392 | }
|
---|
5393 | }
|
---|
5394 |
|
---|
5395 | /* NP */
|
---|
5396 | if (!Desc.Legacy.Gen.u1Present)
|
---|
5397 | {
|
---|
5398 | Log(("ltr %#x - segment not present -> #NP\n", uNewTr));
|
---|
5399 | return iemRaiseSelectorNotPresentBySelector(pVCpu, uNewTr);
|
---|
5400 | }
|
---|
5401 |
|
---|
5402 | /*
|
---|
5403 | * Set it busy.
|
---|
5404 | * Note! Intel says this should lock down the whole descriptor, but we'll
|
---|
5405 | * restrict our selves to 32-bit for now due to lack of inline
|
---|
5406 | * assembly and such.
|
---|
5407 | */
|
---|
5408 | void *pvDesc;
|
---|
5409 | rcStrict = iemMemMap(pVCpu, &pvDesc, 8, UINT8_MAX, pVCpu->cpum.GstCtx.gdtr.pGdt + (uNewTr & X86_SEL_MASK_OFF_RPL), IEM_ACCESS_DATA_RW);
|
---|
5410 | if (rcStrict != VINF_SUCCESS)
|
---|
5411 | return rcStrict;
|
---|
5412 | switch ((uintptr_t)pvDesc & 3)
|
---|
5413 | {
|
---|
5414 | case 0: ASMAtomicBitSet(pvDesc, 40 + 1); break;
|
---|
5415 | case 1: ASMAtomicBitSet((uint8_t *)pvDesc + 3, 40 + 1 - 24); break;
|
---|
5416 | case 2: ASMAtomicBitSet((uint8_t *)pvDesc + 2, 40 + 1 - 16); break;
|
---|
5417 | case 3: ASMAtomicBitSet((uint8_t *)pvDesc + 1, 40 + 1 - 8); break;
|
---|
5418 | }
|
---|
5419 | rcStrict = iemMemCommitAndUnmap(pVCpu, pvDesc, IEM_ACCESS_DATA_RW);
|
---|
5420 | if (rcStrict != VINF_SUCCESS)
|
---|
5421 | return rcStrict;
|
---|
5422 | Desc.Legacy.Gen.u4Type |= X86_SEL_TYPE_SYS_TSS_BUSY_MASK;
|
---|
5423 |
|
---|
5424 | /*
|
---|
5425 | * It checks out alright, update the registers.
|
---|
5426 | */
|
---|
5427 | /** @todo check if the actual value is loaded or if the RPL is dropped */
|
---|
5428 | CPUMSetGuestTR(pVCpu, uNewTr & X86_SEL_MASK_OFF_RPL);
|
---|
5429 | pVCpu->cpum.GstCtx.tr.ValidSel = uNewTr & X86_SEL_MASK_OFF_RPL;
|
---|
5430 | pVCpu->cpum.GstCtx.tr.fFlags = CPUMSELREG_FLAGS_VALID;
|
---|
5431 | pVCpu->cpum.GstCtx.tr.Attr.u = X86DESC_GET_HID_ATTR(&Desc.Legacy);
|
---|
5432 | pVCpu->cpum.GstCtx.tr.u32Limit = X86DESC_LIMIT_G(&Desc.Legacy);
|
---|
5433 | pVCpu->cpum.GstCtx.tr.u64Base = u64Base;
|
---|
5434 |
|
---|
5435 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
5436 | return VINF_SUCCESS;
|
---|
5437 | }
|
---|
5438 |
|
---|
5439 |
|
---|
5440 | /**
|
---|
5441 | * Implements str GReg
|
---|
5442 | *
|
---|
5443 | * @param iGReg The general register to store the CRx value in.
|
---|
5444 | * @param enmEffOpSize The operand size.
|
---|
5445 | */
|
---|
5446 | IEM_CIMPL_DEF_2(iemCImpl_str_reg, uint8_t, iGReg, uint8_t, enmEffOpSize)
|
---|
5447 | {
|
---|
5448 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
5449 | && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
|
---|
5450 | {
|
---|
5451 | Log(("str_reg: Guest intercept -> VM-exit\n"));
|
---|
5452 | IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_LDTR_TR_ACCESS, VMXINSTRID_STR, cbInstr);
|
---|
5453 | }
|
---|
5454 |
|
---|
5455 | IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_TR_READS, SVM_EXIT_TR_READ, 0, 0);
|
---|
5456 |
|
---|
5457 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_TR);
|
---|
5458 | switch (enmEffOpSize)
|
---|
5459 | {
|
---|
5460 | case IEMMODE_16BIT: *(uint16_t *)iemGRegRef(pVCpu, iGReg) = pVCpu->cpum.GstCtx.tr.Sel; break;
|
---|
5461 | case IEMMODE_32BIT: *(uint64_t *)iemGRegRef(pVCpu, iGReg) = pVCpu->cpum.GstCtx.tr.Sel; break;
|
---|
5462 | case IEMMODE_64BIT: *(uint64_t *)iemGRegRef(pVCpu, iGReg) = pVCpu->cpum.GstCtx.tr.Sel; break;
|
---|
5463 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
5464 | }
|
---|
5465 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
5466 | return VINF_SUCCESS;
|
---|
5467 | }
|
---|
5468 |
|
---|
5469 |
|
---|
5470 | /**
|
---|
5471 | * Implements str mem.
|
---|
5472 | *
|
---|
5473 | * @param iGReg The general register to store the CRx value in.
|
---|
5474 | * @param iEffSeg The effective segment register to use with @a GCPtrMem.
|
---|
5475 | * @param GCPtrEffDst Where to store the 16-bit CR0 value.
|
---|
5476 | */
|
---|
5477 | IEM_CIMPL_DEF_2(iemCImpl_str_mem, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
|
---|
5478 | {
|
---|
5479 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
5480 | && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
|
---|
5481 | {
|
---|
5482 | Log(("str_mem: Guest intercept -> VM-exit\n"));
|
---|
5483 | IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_LDTR_TR_ACCESS, VMXINSTRID_STR, cbInstr);
|
---|
5484 | }
|
---|
5485 |
|
---|
5486 | IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_TR_READS, SVM_EXIT_TR_READ, 0, 0);
|
---|
5487 |
|
---|
5488 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_TR);
|
---|
5489 | VBOXSTRICTRC rcStrict = iemMemStoreDataU16(pVCpu, iEffSeg, GCPtrEffDst, pVCpu->cpum.GstCtx.tr.Sel);
|
---|
5490 | if (rcStrict == VINF_SUCCESS)
|
---|
5491 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
5492 | return rcStrict;
|
---|
5493 | }
|
---|
5494 |
|
---|
5495 |
|
---|
5496 | /**
|
---|
5497 | * Implements mov GReg,CRx.
|
---|
5498 | *
|
---|
5499 | * @param iGReg The general register to store the CRx value in.
|
---|
5500 | * @param iCrReg The CRx register to read (valid).
|
---|
5501 | */
|
---|
5502 | IEM_CIMPL_DEF_2(iemCImpl_mov_Rd_Cd, uint8_t, iGReg, uint8_t, iCrReg)
|
---|
5503 | {
|
---|
5504 | if (pVCpu->iem.s.uCpl != 0)
|
---|
5505 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5506 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
5507 |
|
---|
5508 | if (IEM_SVM_IS_READ_CR_INTERCEPT_SET(pVCpu, iCrReg))
|
---|
5509 | {
|
---|
5510 | Log(("iemCImpl_mov_Rd_Cd: Guest intercept CR%u -> #VMEXIT\n", iCrReg));
|
---|
5511 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
5512 | IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_READ_CR0 + iCrReg, IEMACCESSCRX_MOV_CRX, iGReg);
|
---|
5513 | }
|
---|
5514 |
|
---|
5515 | /* Read it. */
|
---|
5516 | uint64_t crX;
|
---|
5517 | switch (iCrReg)
|
---|
5518 | {
|
---|
5519 | case 0:
|
---|
5520 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0);
|
---|
5521 | crX = pVCpu->cpum.GstCtx.cr0;
|
---|
5522 | if (IEM_GET_TARGET_CPU(pVCpu) <= IEMTARGETCPU_386)
|
---|
5523 | crX |= UINT32_C(0x7fffffe0); /* All reserved CR0 flags are set on a 386, just like MSW on 286. */
|
---|
5524 | break;
|
---|
5525 | case 2:
|
---|
5526 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_CR2);
|
---|
5527 | crX = pVCpu->cpum.GstCtx.cr2;
|
---|
5528 | break;
|
---|
5529 | case 3:
|
---|
5530 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR3);
|
---|
5531 | crX = pVCpu->cpum.GstCtx.cr3;
|
---|
5532 | break;
|
---|
5533 | case 4:
|
---|
5534 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR4);
|
---|
5535 | crX = pVCpu->cpum.GstCtx.cr4;
|
---|
5536 | break;
|
---|
5537 | case 8:
|
---|
5538 | {
|
---|
5539 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_APIC_TPR);
|
---|
5540 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
5541 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
5542 | {
|
---|
5543 | VBOXSTRICTRC rcStrict = iemVmxVmexitInstrMovFromCr8(pVCpu, iGReg, cbInstr);
|
---|
5544 | if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
|
---|
5545 | return rcStrict;
|
---|
5546 |
|
---|
5547 | /*
|
---|
5548 | * If the Mov-from-CR8 doesn't cause a VM-exit, bits 7:4 of the VTPR is copied
|
---|
5549 | * to bits 0:3 of the destination operand. Bits 63:4 of the destination operand
|
---|
5550 | * are cleared.
|
---|
5551 | *
|
---|
5552 | * See Intel Spec. 29.3 "Virtualizing CR8-based TPR Accesses"
|
---|
5553 | */
|
---|
5554 | if (IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_USE_TPR_SHADOW))
|
---|
5555 | {
|
---|
5556 | uint32_t const uTpr = iemVmxVirtApicReadRaw32(pVCpu, XAPIC_OFF_TPR);
|
---|
5557 | crX = (uTpr >> 4) & 0xf;
|
---|
5558 | break;
|
---|
5559 | }
|
---|
5560 | }
|
---|
5561 | #endif
|
---|
5562 | #ifdef VBOX_WITH_NESTED_HWVIRT_SVM
|
---|
5563 | if (CPUMIsGuestInSvmNestedHwVirtMode(IEM_GET_CTX(pVCpu)))
|
---|
5564 | {
|
---|
5565 | PCSVMVMCBCTRL pVmcbCtrl = &pVCpu->cpum.GstCtx.hwvirt.svm.Vmcb.ctrl;
|
---|
5566 | if (CPUMIsGuestSvmVirtIntrMasking(pVCpu, IEM_GET_CTX(pVCpu)))
|
---|
5567 | {
|
---|
5568 | crX = pVmcbCtrl->IntCtrl.n.u8VTPR & 0xf;
|
---|
5569 | break;
|
---|
5570 | }
|
---|
5571 | }
|
---|
5572 | #endif
|
---|
5573 | uint8_t uTpr;
|
---|
5574 | int rc = APICGetTpr(pVCpu, &uTpr, NULL, NULL);
|
---|
5575 | if (RT_SUCCESS(rc))
|
---|
5576 | crX = uTpr >> 4;
|
---|
5577 | else
|
---|
5578 | crX = 0;
|
---|
5579 | break;
|
---|
5580 | }
|
---|
5581 | IEM_NOT_REACHED_DEFAULT_CASE_RET(); /* call checks */
|
---|
5582 | }
|
---|
5583 |
|
---|
5584 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
5585 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
5586 | {
|
---|
5587 | switch (iCrReg)
|
---|
5588 | {
|
---|
5589 | /* CR0/CR4 reads are subject to masking when in VMX non-root mode. */
|
---|
5590 | case 0: crX = CPUMGetGuestVmxMaskedCr0(&pVCpu->cpum.GstCtx, pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64Cr0Mask.u); break;
|
---|
5591 | case 4: crX = CPUMGetGuestVmxMaskedCr4(&pVCpu->cpum.GstCtx, pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64Cr4Mask.u); break;
|
---|
5592 |
|
---|
5593 | case 3:
|
---|
5594 | {
|
---|
5595 | VBOXSTRICTRC rcStrict = iemVmxVmexitInstrMovFromCr3(pVCpu, iGReg, cbInstr);
|
---|
5596 | if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
|
---|
5597 | return rcStrict;
|
---|
5598 | break;
|
---|
5599 | }
|
---|
5600 | }
|
---|
5601 | }
|
---|
5602 | #endif
|
---|
5603 |
|
---|
5604 | /* Store it. */
|
---|
5605 | if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
|
---|
5606 | *(uint64_t *)iemGRegRef(pVCpu, iGReg) = crX;
|
---|
5607 | else
|
---|
5608 | *(uint64_t *)iemGRegRef(pVCpu, iGReg) = (uint32_t)crX;
|
---|
5609 |
|
---|
5610 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
5611 | return VINF_SUCCESS;
|
---|
5612 | }
|
---|
5613 |
|
---|
5614 |
|
---|
5615 | /**
|
---|
5616 | * Implements smsw GReg.
|
---|
5617 | *
|
---|
5618 | * @param iGReg The general register to store the CRx value in.
|
---|
5619 | * @param enmEffOpSize The operand size.
|
---|
5620 | */
|
---|
5621 | IEM_CIMPL_DEF_2(iemCImpl_smsw_reg, uint8_t, iGReg, uint8_t, enmEffOpSize)
|
---|
5622 | {
|
---|
5623 | IEM_SVM_CHECK_READ_CR0_INTERCEPT(pVCpu, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
5624 |
|
---|
5625 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
5626 | uint64_t u64MaskedCr0;
|
---|
5627 | if (!IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
5628 | u64MaskedCr0 = pVCpu->cpum.GstCtx.cr0;
|
---|
5629 | else
|
---|
5630 | u64MaskedCr0 = CPUMGetGuestVmxMaskedCr0(&pVCpu->cpum.GstCtx, pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64Cr0Mask.u);
|
---|
5631 | uint64_t const u64GuestCr0 = u64MaskedCr0;
|
---|
5632 | #else
|
---|
5633 | uint64_t const u64GuestCr0 = pVCpu->cpum.GstCtx.cr0;
|
---|
5634 | #endif
|
---|
5635 |
|
---|
5636 | switch (enmEffOpSize)
|
---|
5637 | {
|
---|
5638 | case IEMMODE_16BIT:
|
---|
5639 | if (IEM_GET_TARGET_CPU(pVCpu) > IEMTARGETCPU_386)
|
---|
5640 | *(uint16_t *)iemGRegRef(pVCpu, iGReg) = (uint16_t)u64GuestCr0;
|
---|
5641 | else if (IEM_GET_TARGET_CPU(pVCpu) >= IEMTARGETCPU_386)
|
---|
5642 | *(uint16_t *)iemGRegRef(pVCpu, iGReg) = (uint16_t)u64GuestCr0 | 0xffe0;
|
---|
5643 | else
|
---|
5644 | *(uint16_t *)iemGRegRef(pVCpu, iGReg) = (uint16_t)u64GuestCr0 | 0xfff0;
|
---|
5645 | break;
|
---|
5646 |
|
---|
5647 | case IEMMODE_32BIT:
|
---|
5648 | *(uint32_t *)iemGRegRef(pVCpu, iGReg) = (uint32_t)u64GuestCr0;
|
---|
5649 | break;
|
---|
5650 |
|
---|
5651 | case IEMMODE_64BIT:
|
---|
5652 | *(uint64_t *)iemGRegRef(pVCpu, iGReg) = u64GuestCr0;
|
---|
5653 | break;
|
---|
5654 |
|
---|
5655 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
5656 | }
|
---|
5657 |
|
---|
5658 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
5659 | return VINF_SUCCESS;
|
---|
5660 | }
|
---|
5661 |
|
---|
5662 |
|
---|
5663 | /**
|
---|
5664 | * Implements smsw mem.
|
---|
5665 | *
|
---|
5666 | * @param iGReg The general register to store the CR0 value in.
|
---|
5667 | * @param iEffSeg The effective segment register to use with @a GCPtrMem.
|
---|
5668 | * @param GCPtrEffDst Where to store the 16-bit CR0 value.
|
---|
5669 | */
|
---|
5670 | IEM_CIMPL_DEF_2(iemCImpl_smsw_mem, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
|
---|
5671 | {
|
---|
5672 | IEM_SVM_CHECK_READ_CR0_INTERCEPT(pVCpu, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
5673 |
|
---|
5674 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
5675 | uint64_t u64MaskedCr0;
|
---|
5676 | if (!IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
5677 | u64MaskedCr0 = pVCpu->cpum.GstCtx.cr0;
|
---|
5678 | else
|
---|
5679 | u64MaskedCr0 = CPUMGetGuestVmxMaskedCr0(&pVCpu->cpum.GstCtx, pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64Cr0Mask.u);
|
---|
5680 | uint64_t const u64GuestCr0 = u64MaskedCr0;
|
---|
5681 | #else
|
---|
5682 | uint64_t const u64GuestCr0 = pVCpu->cpum.GstCtx.cr0;
|
---|
5683 | #endif
|
---|
5684 |
|
---|
5685 | uint16_t u16Value;
|
---|
5686 | if (IEM_GET_TARGET_CPU(pVCpu) > IEMTARGETCPU_386)
|
---|
5687 | u16Value = (uint16_t)u64GuestCr0;
|
---|
5688 | else if (IEM_GET_TARGET_CPU(pVCpu) >= IEMTARGETCPU_386)
|
---|
5689 | u16Value = (uint16_t)u64GuestCr0 | 0xffe0;
|
---|
5690 | else
|
---|
5691 | u16Value = (uint16_t)u64GuestCr0 | 0xfff0;
|
---|
5692 |
|
---|
5693 | VBOXSTRICTRC rcStrict = iemMemStoreDataU16(pVCpu, iEffSeg, GCPtrEffDst, u16Value);
|
---|
5694 | if (rcStrict == VINF_SUCCESS)
|
---|
5695 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
5696 | return rcStrict;
|
---|
5697 | }
|
---|
5698 |
|
---|
5699 |
|
---|
5700 | /**
|
---|
5701 | * Helper for mapping CR3 and PAE PDPEs for 'mov CRx,GReg'.
|
---|
5702 | */
|
---|
5703 | #define IEM_MAP_PAE_PDPES_AT_CR3_RET(a_pVCpu, a_iCrReg, a_uCr3) \
|
---|
5704 | do \
|
---|
5705 | { \
|
---|
5706 | int const rcX = PGMGstMapPaePdpesAtCr3(a_pVCpu, a_uCr3); \
|
---|
5707 | if (RT_SUCCESS(rcX)) \
|
---|
5708 | { /* likely */ } \
|
---|
5709 | else \
|
---|
5710 | { \
|
---|
5711 | Log(("iemCImpl_load_Cr%#x: Trying to load invalid PAE PDPEs\n", a_iCrReg)); \
|
---|
5712 | return iemRaiseGeneralProtectionFault0(a_pVCpu); \
|
---|
5713 | } \
|
---|
5714 | } while (0)
|
---|
5715 |
|
---|
5716 |
|
---|
5717 | /**
|
---|
5718 | * Used to implemented 'mov CRx,GReg' and 'lmsw r/m16'.
|
---|
5719 | *
|
---|
5720 | * @param iCrReg The CRx register to write (valid).
|
---|
5721 | * @param uNewCrX The new value.
|
---|
5722 | * @param enmAccessCrx The instruction that caused the CrX load.
|
---|
5723 | * @param iGReg The general register in case of a 'mov CRx,GReg'
|
---|
5724 | * instruction.
|
---|
5725 | */
|
---|
5726 | IEM_CIMPL_DEF_4(iemCImpl_load_CrX, uint8_t, iCrReg, uint64_t, uNewCrX, IEMACCESSCRX, enmAccessCrX, uint8_t, iGReg)
|
---|
5727 | {
|
---|
5728 | VBOXSTRICTRC rcStrict;
|
---|
5729 | int rc;
|
---|
5730 | #ifndef VBOX_WITH_NESTED_HWVIRT_SVM
|
---|
5731 | RT_NOREF2(iGReg, enmAccessCrX);
|
---|
5732 | #endif
|
---|
5733 |
|
---|
5734 | /*
|
---|
5735 | * Try store it.
|
---|
5736 | * Unfortunately, CPUM only does a tiny bit of the work.
|
---|
5737 | */
|
---|
5738 | switch (iCrReg)
|
---|
5739 | {
|
---|
5740 | case 0:
|
---|
5741 | {
|
---|
5742 | /*
|
---|
5743 | * Perform checks.
|
---|
5744 | */
|
---|
5745 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0);
|
---|
5746 |
|
---|
5747 | uint64_t const uOldCrX = pVCpu->cpum.GstCtx.cr0;
|
---|
5748 | uint32_t const fValid = CPUMGetGuestCR0ValidMask();
|
---|
5749 |
|
---|
5750 | /* ET is hardcoded on 486 and later. */
|
---|
5751 | if (IEM_GET_TARGET_CPU(pVCpu) > IEMTARGETCPU_486)
|
---|
5752 | uNewCrX |= X86_CR0_ET;
|
---|
5753 | /* The 386 and 486 didn't #GP(0) on attempting to set reserved CR0 bits. ET was settable on 386. */
|
---|
5754 | else if (IEM_GET_TARGET_CPU(pVCpu) == IEMTARGETCPU_486)
|
---|
5755 | {
|
---|
5756 | uNewCrX &= fValid;
|
---|
5757 | uNewCrX |= X86_CR0_ET;
|
---|
5758 | }
|
---|
5759 | else
|
---|
5760 | uNewCrX &= X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG | X86_CR0_ET;
|
---|
5761 |
|
---|
5762 | /* Check for reserved bits. */
|
---|
5763 | if (uNewCrX & ~(uint64_t)fValid)
|
---|
5764 | {
|
---|
5765 | Log(("Trying to set reserved CR0 bits: NewCR0=%#llx InvalidBits=%#llx\n", uNewCrX, uNewCrX & ~(uint64_t)fValid));
|
---|
5766 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5767 | }
|
---|
5768 |
|
---|
5769 | /* Check for invalid combinations. */
|
---|
5770 | if ( (uNewCrX & X86_CR0_PG)
|
---|
5771 | && !(uNewCrX & X86_CR0_PE) )
|
---|
5772 | {
|
---|
5773 | Log(("Trying to set CR0.PG without CR0.PE\n"));
|
---|
5774 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5775 | }
|
---|
5776 |
|
---|
5777 | if ( !(uNewCrX & X86_CR0_CD)
|
---|
5778 | && (uNewCrX & X86_CR0_NW) )
|
---|
5779 | {
|
---|
5780 | Log(("Trying to clear CR0.CD while leaving CR0.NW set\n"));
|
---|
5781 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5782 | }
|
---|
5783 |
|
---|
5784 | if ( !(uNewCrX & X86_CR0_PG)
|
---|
5785 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_PCIDE))
|
---|
5786 | {
|
---|
5787 | Log(("Trying to clear CR0.PG while leaving CR4.PCID set\n"));
|
---|
5788 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5789 | }
|
---|
5790 |
|
---|
5791 | /* Long mode consistency checks. */
|
---|
5792 | if ( (uNewCrX & X86_CR0_PG)
|
---|
5793 | && !(uOldCrX & X86_CR0_PG)
|
---|
5794 | && (pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_LME) )
|
---|
5795 | {
|
---|
5796 | if (!(pVCpu->cpum.GstCtx.cr4 & X86_CR4_PAE))
|
---|
5797 | {
|
---|
5798 | Log(("Trying to enabled long mode paging without CR4.PAE set\n"));
|
---|
5799 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5800 | }
|
---|
5801 | if (pVCpu->cpum.GstCtx.cs.Attr.n.u1Long)
|
---|
5802 | {
|
---|
5803 | Log(("Trying to enabled long mode paging with a long CS descriptor loaded.\n"));
|
---|
5804 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5805 | }
|
---|
5806 | }
|
---|
5807 |
|
---|
5808 | /* Check for bits that must remain set or cleared in VMX operation,
|
---|
5809 | see Intel spec. 23.8 "Restrictions on VMX operation". */
|
---|
5810 | if (IEM_VMX_IS_ROOT_MODE(pVCpu))
|
---|
5811 | {
|
---|
5812 | uint64_t const uCr0Fixed0 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr0Fixed0;
|
---|
5813 | if ((uNewCrX & uCr0Fixed0) != uCr0Fixed0)
|
---|
5814 | {
|
---|
5815 | Log(("Trying to clear reserved CR0 bits in VMX operation: NewCr0=%#llx MB1=%#llx\n", uNewCrX, uCr0Fixed0));
|
---|
5816 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5817 | }
|
---|
5818 |
|
---|
5819 | uint64_t const uCr0Fixed1 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr0Fixed1;
|
---|
5820 | if (uNewCrX & ~uCr0Fixed1)
|
---|
5821 | {
|
---|
5822 | Log(("Trying to set reserved CR0 bits in VMX operation: NewCr0=%#llx MB0=%#llx\n", uNewCrX, uCr0Fixed1));
|
---|
5823 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5824 | }
|
---|
5825 | }
|
---|
5826 |
|
---|
5827 | /*
|
---|
5828 | * SVM nested-guest CR0 write intercepts.
|
---|
5829 | */
|
---|
5830 | if (IEM_SVM_IS_WRITE_CR_INTERCEPT_SET(pVCpu, iCrReg))
|
---|
5831 | {
|
---|
5832 | Log(("iemCImpl_load_Cr%#x: Guest intercept -> #VMEXIT\n", iCrReg));
|
---|
5833 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
5834 | IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_WRITE_CR0, enmAccessCrX, iGReg);
|
---|
5835 | }
|
---|
5836 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_CR0_SEL_WRITE))
|
---|
5837 | {
|
---|
5838 | /* 'lmsw' intercepts regardless of whether the TS/MP bits are actually toggled. */
|
---|
5839 | if ( enmAccessCrX == IEMACCESSCRX_LMSW
|
---|
5840 | || (uNewCrX & ~(X86_CR0_TS | X86_CR0_MP)) != (uOldCrX & ~(X86_CR0_TS | X86_CR0_MP)))
|
---|
5841 | {
|
---|
5842 | Assert(enmAccessCrX != IEMACCESSCRX_CLTS);
|
---|
5843 | Log(("iemCImpl_load_Cr%#x: lmsw or bits other than TS/MP changed: Guest intercept -> #VMEXIT\n", iCrReg));
|
---|
5844 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
5845 | IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_CR0_SEL_WRITE, enmAccessCrX, iGReg);
|
---|
5846 | }
|
---|
5847 | }
|
---|
5848 |
|
---|
5849 | /*
|
---|
5850 | * Change EFER.LMA if entering or leaving long mode.
|
---|
5851 | */
|
---|
5852 | uint64_t NewEFER = pVCpu->cpum.GstCtx.msrEFER;
|
---|
5853 | if ( (uNewCrX & X86_CR0_PG) != (uOldCrX & X86_CR0_PG)
|
---|
5854 | && (pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_LME) )
|
---|
5855 | {
|
---|
5856 | if (uNewCrX & X86_CR0_PG)
|
---|
5857 | NewEFER |= MSR_K6_EFER_LMA;
|
---|
5858 | else
|
---|
5859 | NewEFER &= ~MSR_K6_EFER_LMA;
|
---|
5860 |
|
---|
5861 | CPUMSetGuestEFER(pVCpu, NewEFER);
|
---|
5862 | Assert(pVCpu->cpum.GstCtx.msrEFER == NewEFER);
|
---|
5863 | }
|
---|
5864 |
|
---|
5865 | /*
|
---|
5866 | * Inform PGM.
|
---|
5867 | */
|
---|
5868 | if ( (uNewCrX & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE | X86_CR0_CD | X86_CR0_NW))
|
---|
5869 | != (uOldCrX & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE | X86_CR0_CD | X86_CR0_NW)) )
|
---|
5870 | {
|
---|
5871 | bool fPdpesMapped;
|
---|
5872 | if ( enmAccessCrX != IEMACCESSCRX_MOV_CRX
|
---|
5873 | || !CPUMIsPaePagingEnabled(uNewCrX, pVCpu->cpum.GstCtx.cr4, NewEFER)
|
---|
5874 | || CPUMIsGuestInSvmNestedHwVirtMode(IEM_GET_CTX(pVCpu)))
|
---|
5875 | fPdpesMapped = false;
|
---|
5876 | else
|
---|
5877 | {
|
---|
5878 | IEM_MAP_PAE_PDPES_AT_CR3_RET(pVCpu, iCrReg, pVCpu->cpum.GstCtx.cr3);
|
---|
5879 | fPdpesMapped = true;
|
---|
5880 | }
|
---|
5881 | rc = PGMFlushTLB(pVCpu, pVCpu->cpum.GstCtx.cr3, true /* global */, fPdpesMapped);
|
---|
5882 | AssertRCReturn(rc, rc);
|
---|
5883 | /* ignore informational status codes */
|
---|
5884 | }
|
---|
5885 |
|
---|
5886 | /*
|
---|
5887 | * Change CR0.
|
---|
5888 | */
|
---|
5889 | CPUMSetGuestCR0(pVCpu, uNewCrX);
|
---|
5890 | Assert(pVCpu->cpum.GstCtx.cr0 == uNewCrX);
|
---|
5891 |
|
---|
5892 | rcStrict = PGMChangeMode(pVCpu, pVCpu->cpum.GstCtx.cr0, pVCpu->cpum.GstCtx.cr4, pVCpu->cpum.GstCtx.msrEFER,
|
---|
5893 | false /* fForce */);
|
---|
5894 | break;
|
---|
5895 | }
|
---|
5896 |
|
---|
5897 | /*
|
---|
5898 | * CR2 can be changed without any restrictions.
|
---|
5899 | */
|
---|
5900 | case 2:
|
---|
5901 | {
|
---|
5902 | if (IEM_SVM_IS_WRITE_CR_INTERCEPT_SET(pVCpu, /*cr*/ 2))
|
---|
5903 | {
|
---|
5904 | Log(("iemCImpl_load_Cr%#x: Guest intercept -> #VMEXIT\n", iCrReg));
|
---|
5905 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
5906 | IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_WRITE_CR2, enmAccessCrX, iGReg);
|
---|
5907 | }
|
---|
5908 | pVCpu->cpum.GstCtx.cr2 = uNewCrX;
|
---|
5909 | pVCpu->cpum.GstCtx.fExtrn &= ~CPUMCTX_EXTRN_CR2;
|
---|
5910 | rcStrict = VINF_SUCCESS;
|
---|
5911 | break;
|
---|
5912 | }
|
---|
5913 |
|
---|
5914 | /*
|
---|
5915 | * CR3 is relatively simple, although AMD and Intel have different
|
---|
5916 | * accounts of how setting reserved bits are handled. We take intel's
|
---|
5917 | * word for the lower bits and AMD's for the high bits (63:52). The
|
---|
5918 | * lower reserved bits are ignored and left alone; OpenBSD 5.8 relies
|
---|
5919 | * on this.
|
---|
5920 | */
|
---|
5921 | /** @todo Testcase: Setting reserved bits in CR3, especially before
|
---|
5922 | * enabling paging. */
|
---|
5923 | case 3:
|
---|
5924 | {
|
---|
5925 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR3);
|
---|
5926 |
|
---|
5927 | /* Bit 63 being clear in the source operand with PCIDE indicates no invalidations are required. */
|
---|
5928 | if ( (pVCpu->cpum.GstCtx.cr4 & X86_CR4_PCIDE)
|
---|
5929 | && (uNewCrX & RT_BIT_64(63)))
|
---|
5930 | {
|
---|
5931 | /** @todo r=ramshankar: avoiding a TLB flush altogether here causes Windows 10
|
---|
5932 | * SMP(w/o nested-paging) to hang during bootup on Skylake systems, see
|
---|
5933 | * Intel spec. 4.10.4.1 "Operations that Invalidate TLBs and
|
---|
5934 | * Paging-Structure Caches". */
|
---|
5935 | uNewCrX &= ~RT_BIT_64(63);
|
---|
5936 | }
|
---|
5937 |
|
---|
5938 | /* Check / mask the value. */
|
---|
5939 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX_EPT
|
---|
5940 | /* See Intel spec. 27.2.2 "EPT Translation Mechanism" footnote. */
|
---|
5941 | uint64_t const fInvPhysMask = !CPUMIsGuestVmxEptPagingEnabledEx(IEM_GET_CTX(pVCpu))
|
---|
5942 | ? (UINT64_MAX << IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cMaxPhysAddrWidth)
|
---|
5943 | : (~X86_CR3_EPT_PAGE_MASK & X86_PAGE_4K_BASE_MASK);
|
---|
5944 | #else
|
---|
5945 | uint64_t const fInvPhysMask = UINT64_C(0xfff0000000000000);
|
---|
5946 | #endif
|
---|
5947 | if (uNewCrX & fInvPhysMask)
|
---|
5948 | {
|
---|
5949 | /** @todo Should we raise this only for 64-bit mode like Intel claims? AMD is
|
---|
5950 | * very vague in this area. As mentioned above, need testcase on real
|
---|
5951 | * hardware... Sigh. */
|
---|
5952 | Log(("Trying to load CR3 with invalid high bits set: %#llx\n", uNewCrX));
|
---|
5953 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
5954 | }
|
---|
5955 |
|
---|
5956 | uint64_t fValid;
|
---|
5957 | if ( (pVCpu->cpum.GstCtx.cr4 & X86_CR4_PAE)
|
---|
5958 | && (pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_LME))
|
---|
5959 | {
|
---|
5960 | /** @todo Redundant? This value has already been validated above. */
|
---|
5961 | fValid = UINT64_C(0x000fffffffffffff);
|
---|
5962 | }
|
---|
5963 | else
|
---|
5964 | fValid = UINT64_C(0xffffffff);
|
---|
5965 | if (uNewCrX & ~fValid)
|
---|
5966 | {
|
---|
5967 | Log(("Automatically clearing reserved MBZ bits in CR3 load: NewCR3=%#llx ClearedBits=%#llx\n",
|
---|
5968 | uNewCrX, uNewCrX & ~fValid));
|
---|
5969 | uNewCrX &= fValid;
|
---|
5970 | }
|
---|
5971 |
|
---|
5972 | if (IEM_SVM_IS_WRITE_CR_INTERCEPT_SET(pVCpu, /*cr*/ 3))
|
---|
5973 | {
|
---|
5974 | Log(("iemCImpl_load_Cr%#x: Guest intercept -> #VMEXIT\n", iCrReg));
|
---|
5975 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
5976 | IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_WRITE_CR3, enmAccessCrX, iGReg);
|
---|
5977 | }
|
---|
5978 |
|
---|
5979 | /* Inform PGM. */
|
---|
5980 | if (pVCpu->cpum.GstCtx.cr0 & X86_CR0_PG)
|
---|
5981 | {
|
---|
5982 | bool fPdpesMapped;
|
---|
5983 | if ( !CPUMIsGuestInPAEModeEx(IEM_GET_CTX(pVCpu))
|
---|
5984 | || CPUMIsGuestInSvmNestedHwVirtMode(IEM_GET_CTX(pVCpu)))
|
---|
5985 | fPdpesMapped = false;
|
---|
5986 | else
|
---|
5987 | {
|
---|
5988 | Assert(enmAccessCrX == IEMACCESSCRX_MOV_CRX);
|
---|
5989 | IEM_MAP_PAE_PDPES_AT_CR3_RET(pVCpu, iCrReg, uNewCrX);
|
---|
5990 | fPdpesMapped = true;
|
---|
5991 | }
|
---|
5992 | rc = PGMFlushTLB(pVCpu, uNewCrX, !(pVCpu->cpum.GstCtx.cr4 & X86_CR4_PGE), fPdpesMapped);
|
---|
5993 | AssertRCReturn(rc, rc);
|
---|
5994 | /* ignore informational status codes */
|
---|
5995 | }
|
---|
5996 |
|
---|
5997 | /* Make the change. */
|
---|
5998 | rc = CPUMSetGuestCR3(pVCpu, uNewCrX);
|
---|
5999 | AssertRCSuccessReturn(rc, rc);
|
---|
6000 |
|
---|
6001 | rcStrict = VINF_SUCCESS;
|
---|
6002 | break;
|
---|
6003 | }
|
---|
6004 |
|
---|
6005 | /*
|
---|
6006 | * CR4 is a bit more tedious as there are bits which cannot be cleared
|
---|
6007 | * under some circumstances and such.
|
---|
6008 | */
|
---|
6009 | case 4:
|
---|
6010 | {
|
---|
6011 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR4);
|
---|
6012 | uint64_t const uOldCrX = pVCpu->cpum.GstCtx.cr4;
|
---|
6013 |
|
---|
6014 | /* Reserved bits. */
|
---|
6015 | uint32_t const fValid = CPUMGetGuestCR4ValidMask(pVCpu->CTX_SUFF(pVM));
|
---|
6016 | if (uNewCrX & ~(uint64_t)fValid)
|
---|
6017 | {
|
---|
6018 | Log(("Trying to set reserved CR4 bits: NewCR4=%#llx InvalidBits=%#llx\n", uNewCrX, uNewCrX & ~(uint64_t)fValid));
|
---|
6019 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6020 | }
|
---|
6021 |
|
---|
6022 | bool const fPcide = !(uOldCrX & X86_CR4_PCIDE) && (uNewCrX & X86_CR4_PCIDE);
|
---|
6023 | bool const fLongMode = CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu));
|
---|
6024 |
|
---|
6025 | /* PCIDE check. */
|
---|
6026 | if ( fPcide
|
---|
6027 | && ( !fLongMode
|
---|
6028 | || (pVCpu->cpum.GstCtx.cr3 & UINT64_C(0xfff))))
|
---|
6029 | {
|
---|
6030 | Log(("Trying to set PCIDE with invalid PCID or outside long mode. Pcid=%#x\n", (pVCpu->cpum.GstCtx.cr3 & UINT64_C(0xfff))));
|
---|
6031 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6032 | }
|
---|
6033 |
|
---|
6034 | /* PAE check. */
|
---|
6035 | if ( fLongMode
|
---|
6036 | && (uOldCrX & X86_CR4_PAE)
|
---|
6037 | && !(uNewCrX & X86_CR4_PAE))
|
---|
6038 | {
|
---|
6039 | Log(("Trying to set clear CR4.PAE while long mode is active\n"));
|
---|
6040 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6041 | }
|
---|
6042 |
|
---|
6043 | if (IEM_SVM_IS_WRITE_CR_INTERCEPT_SET(pVCpu, /*cr*/ 4))
|
---|
6044 | {
|
---|
6045 | Log(("iemCImpl_load_Cr%#x: Guest intercept -> #VMEXIT\n", iCrReg));
|
---|
6046 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
6047 | IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_WRITE_CR4, enmAccessCrX, iGReg);
|
---|
6048 | }
|
---|
6049 |
|
---|
6050 | /* Check for bits that must remain set or cleared in VMX operation,
|
---|
6051 | see Intel spec. 23.8 "Restrictions on VMX operation". */
|
---|
6052 | if (IEM_VMX_IS_ROOT_MODE(pVCpu))
|
---|
6053 | {
|
---|
6054 | uint64_t const uCr4Fixed0 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr4Fixed0;
|
---|
6055 | if ((uNewCrX & uCr4Fixed0) != uCr4Fixed0)
|
---|
6056 | {
|
---|
6057 | Log(("Trying to clear reserved CR4 bits in VMX operation: NewCr4=%#llx MB1=%#llx\n", uNewCrX, uCr4Fixed0));
|
---|
6058 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6059 | }
|
---|
6060 |
|
---|
6061 | uint64_t const uCr4Fixed1 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr4Fixed1;
|
---|
6062 | if (uNewCrX & ~uCr4Fixed1)
|
---|
6063 | {
|
---|
6064 | Log(("Trying to set reserved CR4 bits in VMX operation: NewCr4=%#llx MB0=%#llx\n", uNewCrX, uCr4Fixed1));
|
---|
6065 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6066 | }
|
---|
6067 | }
|
---|
6068 |
|
---|
6069 | /*
|
---|
6070 | * Notify PGM.
|
---|
6071 | */
|
---|
6072 | if ((uNewCrX ^ uOldCrX) & (X86_CR4_PSE | X86_CR4_PAE | X86_CR4_PGE | X86_CR4_PCIDE /* | X86_CR4_SMEP */))
|
---|
6073 | {
|
---|
6074 | bool fPdpesMapped;
|
---|
6075 | if ( !CPUMIsPaePagingEnabled(pVCpu->cpum.GstCtx.cr0, uNewCrX, pVCpu->cpum.GstCtx.msrEFER)
|
---|
6076 | || CPUMIsGuestInSvmNestedHwVirtMode(IEM_GET_CTX(pVCpu)))
|
---|
6077 | fPdpesMapped = false;
|
---|
6078 | else
|
---|
6079 | {
|
---|
6080 | Assert(enmAccessCrX == IEMACCESSCRX_MOV_CRX);
|
---|
6081 | IEM_MAP_PAE_PDPES_AT_CR3_RET(pVCpu, iCrReg, pVCpu->cpum.GstCtx.cr3);
|
---|
6082 | fPdpesMapped = true;
|
---|
6083 | }
|
---|
6084 | rc = PGMFlushTLB(pVCpu, pVCpu->cpum.GstCtx.cr3, true /* global */, fPdpesMapped);
|
---|
6085 | AssertRCReturn(rc, rc);
|
---|
6086 | /* ignore informational status codes */
|
---|
6087 | }
|
---|
6088 |
|
---|
6089 | /*
|
---|
6090 | * Change it.
|
---|
6091 | */
|
---|
6092 | rc = CPUMSetGuestCR4(pVCpu, uNewCrX);
|
---|
6093 | AssertRCSuccessReturn(rc, rc);
|
---|
6094 | Assert(pVCpu->cpum.GstCtx.cr4 == uNewCrX);
|
---|
6095 |
|
---|
6096 | rcStrict = PGMChangeMode(pVCpu, pVCpu->cpum.GstCtx.cr0, pVCpu->cpum.GstCtx.cr4, pVCpu->cpum.GstCtx.msrEFER,
|
---|
6097 | false /* fForce */);
|
---|
6098 | break;
|
---|
6099 | }
|
---|
6100 |
|
---|
6101 | /*
|
---|
6102 | * CR8 maps to the APIC TPR.
|
---|
6103 | */
|
---|
6104 | case 8:
|
---|
6105 | {
|
---|
6106 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_APIC_TPR);
|
---|
6107 | if (uNewCrX & ~(uint64_t)0xf)
|
---|
6108 | {
|
---|
6109 | Log(("Trying to set reserved CR8 bits (%#RX64)\n", uNewCrX));
|
---|
6110 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6111 | }
|
---|
6112 |
|
---|
6113 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
6114 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
6115 | && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_USE_TPR_SHADOW))
|
---|
6116 | {
|
---|
6117 | /*
|
---|
6118 | * If the Mov-to-CR8 doesn't cause a VM-exit, bits 0:3 of the source operand
|
---|
6119 | * is copied to bits 7:4 of the VTPR. Bits 0:3 and bits 31:8 of the VTPR are
|
---|
6120 | * cleared. Following this the processor performs TPR virtualization.
|
---|
6121 | *
|
---|
6122 | * However, we should not perform TPR virtualization immediately here but
|
---|
6123 | * after this instruction has completed.
|
---|
6124 | *
|
---|
6125 | * See Intel spec. 29.3 "Virtualizing CR8-based TPR Accesses"
|
---|
6126 | * See Intel spec. 27.1 "Architectural State Before A VM-exit"
|
---|
6127 | */
|
---|
6128 | uint32_t const uTpr = (uNewCrX & 0xf) << 4;
|
---|
6129 | Log(("iemCImpl_load_Cr%#x: Virtualizing TPR (%#x) write\n", iCrReg, uTpr));
|
---|
6130 | iemVmxVirtApicWriteRaw32(pVCpu, XAPIC_OFF_TPR, uTpr);
|
---|
6131 | iemVmxVirtApicSetPendingWrite(pVCpu, XAPIC_OFF_TPR);
|
---|
6132 | rcStrict = VINF_SUCCESS;
|
---|
6133 | break;
|
---|
6134 | }
|
---|
6135 | #endif
|
---|
6136 |
|
---|
6137 | #ifdef VBOX_WITH_NESTED_HWVIRT_SVM
|
---|
6138 | if (CPUMIsGuestInSvmNestedHwVirtMode(IEM_GET_CTX(pVCpu)))
|
---|
6139 | {
|
---|
6140 | if (IEM_SVM_IS_WRITE_CR_INTERCEPT_SET(pVCpu, /*cr*/ 8))
|
---|
6141 | {
|
---|
6142 | Log(("iemCImpl_load_Cr%#x: Guest intercept -> #VMEXIT\n", iCrReg));
|
---|
6143 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
6144 | IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_WRITE_CR8, enmAccessCrX, iGReg);
|
---|
6145 | }
|
---|
6146 |
|
---|
6147 | pVCpu->cpum.GstCtx.hwvirt.svm.Vmcb.ctrl.IntCtrl.n.u8VTPR = uNewCrX;
|
---|
6148 | if (CPUMIsGuestSvmVirtIntrMasking(pVCpu, IEM_GET_CTX(pVCpu)))
|
---|
6149 | {
|
---|
6150 | rcStrict = VINF_SUCCESS;
|
---|
6151 | break;
|
---|
6152 | }
|
---|
6153 | }
|
---|
6154 | #endif
|
---|
6155 | uint8_t const u8Tpr = (uint8_t)uNewCrX << 4;
|
---|
6156 | APICSetTpr(pVCpu, u8Tpr);
|
---|
6157 | rcStrict = VINF_SUCCESS;
|
---|
6158 | break;
|
---|
6159 | }
|
---|
6160 |
|
---|
6161 | IEM_NOT_REACHED_DEFAULT_CASE_RET(); /* call checks */
|
---|
6162 | }
|
---|
6163 |
|
---|
6164 | /*
|
---|
6165 | * Advance the RIP on success.
|
---|
6166 | */
|
---|
6167 | if (RT_SUCCESS(rcStrict))
|
---|
6168 | {
|
---|
6169 | if (rcStrict != VINF_SUCCESS)
|
---|
6170 | rcStrict = iemSetPassUpStatus(pVCpu, rcStrict);
|
---|
6171 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
6172 | }
|
---|
6173 |
|
---|
6174 | return rcStrict;
|
---|
6175 | }
|
---|
6176 |
|
---|
6177 |
|
---|
6178 | /**
|
---|
6179 | * Implements mov CRx,GReg.
|
---|
6180 | *
|
---|
6181 | * @param iCrReg The CRx register to write (valid).
|
---|
6182 | * @param iGReg The general register to load the CRx value from.
|
---|
6183 | */
|
---|
6184 | IEM_CIMPL_DEF_2(iemCImpl_mov_Cd_Rd, uint8_t, iCrReg, uint8_t, iGReg)
|
---|
6185 | {
|
---|
6186 | if (pVCpu->iem.s.uCpl != 0)
|
---|
6187 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6188 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
6189 |
|
---|
6190 | /*
|
---|
6191 | * Read the new value from the source register and call common worker.
|
---|
6192 | */
|
---|
6193 | uint64_t uNewCrX;
|
---|
6194 | if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
|
---|
6195 | uNewCrX = iemGRegFetchU64(pVCpu, iGReg);
|
---|
6196 | else
|
---|
6197 | uNewCrX = iemGRegFetchU32(pVCpu, iGReg);
|
---|
6198 |
|
---|
6199 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
6200 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
6201 | {
|
---|
6202 | VBOXSTRICTRC rcStrict = VINF_VMX_INTERCEPT_NOT_ACTIVE;
|
---|
6203 | switch (iCrReg)
|
---|
6204 | {
|
---|
6205 | case 0:
|
---|
6206 | case 4: rcStrict = iemVmxVmexitInstrMovToCr0Cr4(pVCpu, iCrReg, &uNewCrX, iGReg, cbInstr); break;
|
---|
6207 | case 3: rcStrict = iemVmxVmexitInstrMovToCr3(pVCpu, uNewCrX, iGReg, cbInstr); break;
|
---|
6208 | case 8: rcStrict = iemVmxVmexitInstrMovToCr8(pVCpu, iGReg, cbInstr); break;
|
---|
6209 | }
|
---|
6210 | if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
|
---|
6211 | return rcStrict;
|
---|
6212 | }
|
---|
6213 | #endif
|
---|
6214 |
|
---|
6215 | return IEM_CIMPL_CALL_4(iemCImpl_load_CrX, iCrReg, uNewCrX, IEMACCESSCRX_MOV_CRX, iGReg);
|
---|
6216 | }
|
---|
6217 |
|
---|
6218 |
|
---|
6219 | /**
|
---|
6220 | * Implements 'LMSW r/m16'
|
---|
6221 | *
|
---|
6222 | * @param u16NewMsw The new value.
|
---|
6223 | * @param GCPtrEffDst The guest-linear address of the source operand in case
|
---|
6224 | * of a memory operand. For register operand, pass
|
---|
6225 | * NIL_RTGCPTR.
|
---|
6226 | */
|
---|
6227 | IEM_CIMPL_DEF_2(iemCImpl_lmsw, uint16_t, u16NewMsw, RTGCPTR, GCPtrEffDst)
|
---|
6228 | {
|
---|
6229 | if (pVCpu->iem.s.uCpl != 0)
|
---|
6230 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6231 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
6232 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0);
|
---|
6233 |
|
---|
6234 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
6235 | /* Check nested-guest VMX intercept and get updated MSW if there's no VM-exit. */
|
---|
6236 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
6237 | {
|
---|
6238 | VBOXSTRICTRC rcStrict = iemVmxVmexitInstrLmsw(pVCpu, pVCpu->cpum.GstCtx.cr0, &u16NewMsw, GCPtrEffDst, cbInstr);
|
---|
6239 | if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
|
---|
6240 | return rcStrict;
|
---|
6241 | }
|
---|
6242 | #else
|
---|
6243 | RT_NOREF_PV(GCPtrEffDst);
|
---|
6244 | #endif
|
---|
6245 |
|
---|
6246 | /*
|
---|
6247 | * Compose the new CR0 value and call common worker.
|
---|
6248 | */
|
---|
6249 | uint64_t uNewCr0 = pVCpu->cpum.GstCtx.cr0 & ~(X86_CR0_MP | X86_CR0_EM | X86_CR0_TS);
|
---|
6250 | uNewCr0 |= u16NewMsw & (X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS);
|
---|
6251 | return IEM_CIMPL_CALL_4(iemCImpl_load_CrX, /*cr*/ 0, uNewCr0, IEMACCESSCRX_LMSW, UINT8_MAX /* iGReg */);
|
---|
6252 | }
|
---|
6253 |
|
---|
6254 |
|
---|
6255 | /**
|
---|
6256 | * Implements 'CLTS'.
|
---|
6257 | */
|
---|
6258 | IEM_CIMPL_DEF_0(iemCImpl_clts)
|
---|
6259 | {
|
---|
6260 | if (pVCpu->iem.s.uCpl != 0)
|
---|
6261 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6262 |
|
---|
6263 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0);
|
---|
6264 | uint64_t uNewCr0 = pVCpu->cpum.GstCtx.cr0;
|
---|
6265 | uNewCr0 &= ~X86_CR0_TS;
|
---|
6266 |
|
---|
6267 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
6268 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
6269 | {
|
---|
6270 | VBOXSTRICTRC rcStrict = iemVmxVmexitInstrClts(pVCpu, cbInstr);
|
---|
6271 | if (rcStrict == VINF_VMX_MODIFIES_BEHAVIOR)
|
---|
6272 | uNewCr0 |= (pVCpu->cpum.GstCtx.cr0 & X86_CR0_TS);
|
---|
6273 | else if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
|
---|
6274 | return rcStrict;
|
---|
6275 | }
|
---|
6276 | #endif
|
---|
6277 |
|
---|
6278 | return IEM_CIMPL_CALL_4(iemCImpl_load_CrX, /*cr*/ 0, uNewCr0, IEMACCESSCRX_CLTS, UINT8_MAX /* iGReg */);
|
---|
6279 | }
|
---|
6280 |
|
---|
6281 |
|
---|
6282 | /**
|
---|
6283 | * Implements mov GReg,DRx.
|
---|
6284 | *
|
---|
6285 | * @param iGReg The general register to store the DRx value in.
|
---|
6286 | * @param iDrReg The DRx register to read (0-7).
|
---|
6287 | */
|
---|
6288 | IEM_CIMPL_DEF_2(iemCImpl_mov_Rd_Dd, uint8_t, iGReg, uint8_t, iDrReg)
|
---|
6289 | {
|
---|
6290 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
6291 | /*
|
---|
6292 | * Check nested-guest VMX intercept.
|
---|
6293 | * Unlike most other intercepts, the Mov DRx intercept takes preceedence
|
---|
6294 | * over CPL and CR4.DE and even DR4/DR5 checks.
|
---|
6295 | *
|
---|
6296 | * See Intel spec. 25.1.3 "Instructions That Cause VM Exits Conditionally".
|
---|
6297 | */
|
---|
6298 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
6299 | {
|
---|
6300 | VBOXSTRICTRC rcStrict = iemVmxVmexitInstrMovDrX(pVCpu, VMXINSTRID_MOV_FROM_DRX, iDrReg, iGReg, cbInstr);
|
---|
6301 | if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
|
---|
6302 | return rcStrict;
|
---|
6303 | }
|
---|
6304 | #endif
|
---|
6305 |
|
---|
6306 | /*
|
---|
6307 | * Check preconditions.
|
---|
6308 | */
|
---|
6309 | /* Raise GPs. */
|
---|
6310 | if (pVCpu->iem.s.uCpl != 0)
|
---|
6311 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6312 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
6313 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_DR7 | CPUMCTX_EXTRN_CR0);
|
---|
6314 |
|
---|
6315 | if ( (iDrReg == 4 || iDrReg == 5)
|
---|
6316 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_DE) )
|
---|
6317 | {
|
---|
6318 | Log(("mov r%u,dr%u: CR4.DE=1 -> #GP(0)\n", iGReg, iDrReg));
|
---|
6319 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6320 | }
|
---|
6321 |
|
---|
6322 | /* Raise #DB if general access detect is enabled. */
|
---|
6323 | if (pVCpu->cpum.GstCtx.dr[7] & X86_DR7_GD)
|
---|
6324 | {
|
---|
6325 | Log(("mov r%u,dr%u: DR7.GD=1 -> #DB\n", iGReg, iDrReg));
|
---|
6326 | return iemRaiseDebugException(pVCpu);
|
---|
6327 | }
|
---|
6328 |
|
---|
6329 | /*
|
---|
6330 | * Read the debug register and store it in the specified general register.
|
---|
6331 | */
|
---|
6332 | uint64_t drX;
|
---|
6333 | switch (iDrReg)
|
---|
6334 | {
|
---|
6335 | case 0:
|
---|
6336 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3);
|
---|
6337 | drX = pVCpu->cpum.GstCtx.dr[0];
|
---|
6338 | break;
|
---|
6339 | case 1:
|
---|
6340 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3);
|
---|
6341 | drX = pVCpu->cpum.GstCtx.dr[1];
|
---|
6342 | break;
|
---|
6343 | case 2:
|
---|
6344 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3);
|
---|
6345 | drX = pVCpu->cpum.GstCtx.dr[2];
|
---|
6346 | break;
|
---|
6347 | case 3:
|
---|
6348 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3);
|
---|
6349 | drX = pVCpu->cpum.GstCtx.dr[3];
|
---|
6350 | break;
|
---|
6351 | case 6:
|
---|
6352 | case 4:
|
---|
6353 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR6);
|
---|
6354 | drX = pVCpu->cpum.GstCtx.dr[6];
|
---|
6355 | drX |= X86_DR6_RA1_MASK;
|
---|
6356 | drX &= ~X86_DR6_RAZ_MASK;
|
---|
6357 | break;
|
---|
6358 | case 7:
|
---|
6359 | case 5:
|
---|
6360 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_DR7);
|
---|
6361 | drX = pVCpu->cpum.GstCtx.dr[7];
|
---|
6362 | drX |=X86_DR7_RA1_MASK;
|
---|
6363 | drX &= ~X86_DR7_RAZ_MASK;
|
---|
6364 | break;
|
---|
6365 | IEM_NOT_REACHED_DEFAULT_CASE_RET(); /* call checks */
|
---|
6366 | }
|
---|
6367 |
|
---|
6368 | /** @todo SVM nested-guest intercept for DR8-DR15? */
|
---|
6369 | /*
|
---|
6370 | * Check for any SVM nested-guest intercepts for the DRx read.
|
---|
6371 | */
|
---|
6372 | if (IEM_SVM_IS_READ_DR_INTERCEPT_SET(pVCpu, iDrReg))
|
---|
6373 | {
|
---|
6374 | Log(("mov r%u,dr%u: Guest intercept -> #VMEXIT\n", iGReg, iDrReg));
|
---|
6375 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
6376 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_READ_DR0 + (iDrReg & 0xf),
|
---|
6377 | IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fSvmDecodeAssists ? (iGReg & 7) : 0, 0 /* uExitInfo2 */);
|
---|
6378 | }
|
---|
6379 |
|
---|
6380 | if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
|
---|
6381 | *(uint64_t *)iemGRegRef(pVCpu, iGReg) = drX;
|
---|
6382 | else
|
---|
6383 | *(uint64_t *)iemGRegRef(pVCpu, iGReg) = (uint32_t)drX;
|
---|
6384 |
|
---|
6385 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
6386 | return VINF_SUCCESS;
|
---|
6387 | }
|
---|
6388 |
|
---|
6389 |
|
---|
6390 | /**
|
---|
6391 | * Implements mov DRx,GReg.
|
---|
6392 | *
|
---|
6393 | * @param iDrReg The DRx register to write (valid).
|
---|
6394 | * @param iGReg The general register to load the DRx value from.
|
---|
6395 | */
|
---|
6396 | IEM_CIMPL_DEF_2(iemCImpl_mov_Dd_Rd, uint8_t, iDrReg, uint8_t, iGReg)
|
---|
6397 | {
|
---|
6398 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
6399 | /*
|
---|
6400 | * Check nested-guest VMX intercept.
|
---|
6401 | * Unlike most other intercepts, the Mov DRx intercept takes preceedence
|
---|
6402 | * over CPL and CR4.DE and even DR4/DR5 checks.
|
---|
6403 | *
|
---|
6404 | * See Intel spec. 25.1.3 "Instructions That Cause VM Exits Conditionally".
|
---|
6405 | */
|
---|
6406 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
6407 | {
|
---|
6408 | VBOXSTRICTRC rcStrict = iemVmxVmexitInstrMovDrX(pVCpu, VMXINSTRID_MOV_TO_DRX, iDrReg, iGReg, cbInstr);
|
---|
6409 | if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
|
---|
6410 | return rcStrict;
|
---|
6411 | }
|
---|
6412 | #endif
|
---|
6413 |
|
---|
6414 | /*
|
---|
6415 | * Check preconditions.
|
---|
6416 | */
|
---|
6417 | if (pVCpu->iem.s.uCpl != 0)
|
---|
6418 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6419 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
6420 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_DR7 | CPUMCTX_EXTRN_CR4);
|
---|
6421 |
|
---|
6422 | if (iDrReg == 4 || iDrReg == 5)
|
---|
6423 | {
|
---|
6424 | if (pVCpu->cpum.GstCtx.cr4 & X86_CR4_DE)
|
---|
6425 | {
|
---|
6426 | Log(("mov dr%u,r%u: CR4.DE=1 -> #GP(0)\n", iDrReg, iGReg));
|
---|
6427 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6428 | }
|
---|
6429 | iDrReg += 2;
|
---|
6430 | }
|
---|
6431 |
|
---|
6432 | /* Raise #DB if general access detect is enabled. */
|
---|
6433 | /** @todo is \#DB/DR7.GD raised before any reserved high bits in DR7/DR6
|
---|
6434 | * \#GP? */
|
---|
6435 | if (pVCpu->cpum.GstCtx.dr[7] & X86_DR7_GD)
|
---|
6436 | {
|
---|
6437 | Log(("mov dr%u,r%u: DR7.GD=1 -> #DB\n", iDrReg, iGReg));
|
---|
6438 | return iemRaiseDebugException(pVCpu);
|
---|
6439 | }
|
---|
6440 |
|
---|
6441 | /*
|
---|
6442 | * Read the new value from the source register.
|
---|
6443 | */
|
---|
6444 | uint64_t uNewDrX;
|
---|
6445 | if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
|
---|
6446 | uNewDrX = iemGRegFetchU64(pVCpu, iGReg);
|
---|
6447 | else
|
---|
6448 | uNewDrX = iemGRegFetchU32(pVCpu, iGReg);
|
---|
6449 |
|
---|
6450 | /*
|
---|
6451 | * Adjust it.
|
---|
6452 | */
|
---|
6453 | switch (iDrReg)
|
---|
6454 | {
|
---|
6455 | case 0:
|
---|
6456 | case 1:
|
---|
6457 | case 2:
|
---|
6458 | case 3:
|
---|
6459 | /* nothing to adjust */
|
---|
6460 | break;
|
---|
6461 |
|
---|
6462 | case 6:
|
---|
6463 | if (uNewDrX & X86_DR6_MBZ_MASK)
|
---|
6464 | {
|
---|
6465 | Log(("mov dr%u,%#llx: DR6 high bits are not zero -> #GP(0)\n", iDrReg, uNewDrX));
|
---|
6466 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6467 | }
|
---|
6468 | uNewDrX |= X86_DR6_RA1_MASK;
|
---|
6469 | uNewDrX &= ~X86_DR6_RAZ_MASK;
|
---|
6470 | break;
|
---|
6471 |
|
---|
6472 | case 7:
|
---|
6473 | if (uNewDrX & X86_DR7_MBZ_MASK)
|
---|
6474 | {
|
---|
6475 | Log(("mov dr%u,%#llx: DR7 high bits are not zero -> #GP(0)\n", iDrReg, uNewDrX));
|
---|
6476 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6477 | }
|
---|
6478 | uNewDrX |= X86_DR7_RA1_MASK;
|
---|
6479 | uNewDrX &= ~X86_DR7_RAZ_MASK;
|
---|
6480 | break;
|
---|
6481 |
|
---|
6482 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
6483 | }
|
---|
6484 |
|
---|
6485 | /** @todo SVM nested-guest intercept for DR8-DR15? */
|
---|
6486 | /*
|
---|
6487 | * Check for any SVM nested-guest intercepts for the DRx write.
|
---|
6488 | */
|
---|
6489 | if (IEM_SVM_IS_WRITE_DR_INTERCEPT_SET(pVCpu, iDrReg))
|
---|
6490 | {
|
---|
6491 | Log2(("mov dr%u,r%u: Guest intercept -> #VMEXIT\n", iDrReg, iGReg));
|
---|
6492 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
6493 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_WRITE_DR0 + (iDrReg & 0xf),
|
---|
6494 | IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fSvmDecodeAssists ? (iGReg & 7) : 0, 0 /* uExitInfo2 */);
|
---|
6495 | }
|
---|
6496 |
|
---|
6497 | /*
|
---|
6498 | * Do the actual setting.
|
---|
6499 | */
|
---|
6500 | if (iDrReg < 4)
|
---|
6501 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3);
|
---|
6502 | else if (iDrReg == 6)
|
---|
6503 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR6);
|
---|
6504 |
|
---|
6505 | int rc = CPUMSetGuestDRx(pVCpu, iDrReg, uNewDrX);
|
---|
6506 | AssertRCSuccessReturn(rc, RT_SUCCESS_NP(rc) ? VERR_IEM_IPE_1 : rc);
|
---|
6507 |
|
---|
6508 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
6509 | return VINF_SUCCESS;
|
---|
6510 | }
|
---|
6511 |
|
---|
6512 |
|
---|
6513 | /**
|
---|
6514 | * Implements mov GReg,TRx.
|
---|
6515 | *
|
---|
6516 | * @param iGReg The general register to store the
|
---|
6517 | * TRx value in.
|
---|
6518 | * @param iTrReg The TRx register to read (6/7).
|
---|
6519 | */
|
---|
6520 | IEM_CIMPL_DEF_2(iemCImpl_mov_Rd_Td, uint8_t, iGReg, uint8_t, iTrReg)
|
---|
6521 | {
|
---|
6522 | /*
|
---|
6523 | * Check preconditions. NB: This instruction is 386/486 only.
|
---|
6524 | */
|
---|
6525 |
|
---|
6526 | /* Raise GPs. */
|
---|
6527 | if (pVCpu->iem.s.uCpl != 0)
|
---|
6528 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6529 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
6530 |
|
---|
6531 | if (iTrReg < 6 || iTrReg > 7)
|
---|
6532 | {
|
---|
6533 | /** @todo Do Intel CPUs reject this or are the TRs aliased? */
|
---|
6534 | Log(("mov r%u,tr%u: invalid register -> #GP(0)\n", iGReg, iTrReg));
|
---|
6535 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6536 | }
|
---|
6537 |
|
---|
6538 | /*
|
---|
6539 | * Read the test register and store it in the specified general register.
|
---|
6540 | * This is currently a dummy implementation that only exists to satisfy
|
---|
6541 | * old debuggers like WDEB386 or OS/2 KDB which unconditionally read the
|
---|
6542 | * TR6/TR7 registers. Software which actually depends on the TR values
|
---|
6543 | * (different on 386/486) is exceedingly rare.
|
---|
6544 | */
|
---|
6545 | uint64_t trX;
|
---|
6546 | switch (iTrReg)
|
---|
6547 | {
|
---|
6548 | case 6:
|
---|
6549 | trX = 0; /* Currently a dummy. */
|
---|
6550 | break;
|
---|
6551 | case 7:
|
---|
6552 | trX = 0; /* Currently a dummy. */
|
---|
6553 | break;
|
---|
6554 | IEM_NOT_REACHED_DEFAULT_CASE_RET(); /* call checks */
|
---|
6555 | }
|
---|
6556 |
|
---|
6557 | *(uint64_t *)iemGRegRef(pVCpu, iGReg) = (uint32_t)trX;
|
---|
6558 |
|
---|
6559 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
6560 | return VINF_SUCCESS;
|
---|
6561 | }
|
---|
6562 |
|
---|
6563 |
|
---|
6564 | /**
|
---|
6565 | * Implements mov TRx,GReg.
|
---|
6566 | *
|
---|
6567 | * @param iTrReg The TRx register to write (valid).
|
---|
6568 | * @param iGReg The general register to load the TRx
|
---|
6569 | * value from.
|
---|
6570 | */
|
---|
6571 | IEM_CIMPL_DEF_2(iemCImpl_mov_Td_Rd, uint8_t, iTrReg, uint8_t, iGReg)
|
---|
6572 | {
|
---|
6573 | /*
|
---|
6574 | * Check preconditions. NB: This instruction is 386/486 only.
|
---|
6575 | */
|
---|
6576 |
|
---|
6577 | /* Raise GPs. */
|
---|
6578 | if (pVCpu->iem.s.uCpl != 0)
|
---|
6579 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6580 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
6581 |
|
---|
6582 | if (iTrReg < 6 || iTrReg > 7)
|
---|
6583 | {
|
---|
6584 | /** @todo Do Intel CPUs reject this or are the TRs aliased? */
|
---|
6585 | Log(("mov r%u,tr%u: invalid register -> #GP(0)\n", iGReg, iTrReg));
|
---|
6586 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6587 | }
|
---|
6588 |
|
---|
6589 | /*
|
---|
6590 | * Read the new value from the source register.
|
---|
6591 | */
|
---|
6592 | uint64_t uNewTrX;
|
---|
6593 | if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
|
---|
6594 | uNewTrX = iemGRegFetchU64(pVCpu, iGReg);
|
---|
6595 | else
|
---|
6596 | uNewTrX = iemGRegFetchU32(pVCpu, iGReg);
|
---|
6597 |
|
---|
6598 | /*
|
---|
6599 | * Here we would do the actual setting if this weren't a dummy implementation.
|
---|
6600 | * This is currently a dummy implementation that only exists to prevent
|
---|
6601 | * old debuggers like WDEB386 or OS/2 KDB from crashing.
|
---|
6602 | */
|
---|
6603 | RT_NOREF(uNewTrX);
|
---|
6604 |
|
---|
6605 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
6606 | return VINF_SUCCESS;
|
---|
6607 | }
|
---|
6608 |
|
---|
6609 |
|
---|
6610 | /**
|
---|
6611 | * Implements 'INVLPG m'.
|
---|
6612 | *
|
---|
6613 | * @param GCPtrPage The effective address of the page to invalidate.
|
---|
6614 | * @remarks Updates the RIP.
|
---|
6615 | */
|
---|
6616 | IEM_CIMPL_DEF_1(iemCImpl_invlpg, RTGCPTR, GCPtrPage)
|
---|
6617 | {
|
---|
6618 | /* ring-0 only. */
|
---|
6619 | if (pVCpu->iem.s.uCpl != 0)
|
---|
6620 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6621 | Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
|
---|
6622 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_CR3 | CPUMCTX_EXTRN_CR4 | CPUMCTX_EXTRN_EFER);
|
---|
6623 |
|
---|
6624 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
6625 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
6626 | && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_INVLPG_EXIT))
|
---|
6627 | {
|
---|
6628 | Log(("invlpg: Guest intercept (%RGp) -> VM-exit\n", GCPtrPage));
|
---|
6629 | return iemVmxVmexitInstrInvlpg(pVCpu, GCPtrPage, cbInstr);
|
---|
6630 | }
|
---|
6631 | #endif
|
---|
6632 |
|
---|
6633 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_INVLPG))
|
---|
6634 | {
|
---|
6635 | Log(("invlpg: Guest intercept (%RGp) -> #VMEXIT\n", GCPtrPage));
|
---|
6636 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
6637 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_INVLPG,
|
---|
6638 | IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fSvmDecodeAssists ? GCPtrPage : 0, 0 /* uExitInfo2 */);
|
---|
6639 | }
|
---|
6640 |
|
---|
6641 | int rc = PGMInvalidatePage(pVCpu, GCPtrPage);
|
---|
6642 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
6643 |
|
---|
6644 | if (rc == VINF_SUCCESS)
|
---|
6645 | return VINF_SUCCESS;
|
---|
6646 | if (rc == VINF_PGM_SYNC_CR3)
|
---|
6647 | return iemSetPassUpStatus(pVCpu, rc);
|
---|
6648 |
|
---|
6649 | AssertMsg(rc == VINF_EM_RAW_EMULATE_INSTR || RT_FAILURE_NP(rc), ("%Rrc\n", rc));
|
---|
6650 | Log(("PGMInvalidatePage(%RGv) -> %Rrc\n", GCPtrPage, rc));
|
---|
6651 | return rc;
|
---|
6652 | }
|
---|
6653 |
|
---|
6654 |
|
---|
6655 | /**
|
---|
6656 | * Implements INVPCID.
|
---|
6657 | *
|
---|
6658 | * @param iEffSeg The segment of the invpcid descriptor.
|
---|
6659 | * @param GCPtrInvpcidDesc The address of invpcid descriptor.
|
---|
6660 | * @param uInvpcidType The invalidation type.
|
---|
6661 | * @remarks Updates the RIP.
|
---|
6662 | */
|
---|
6663 | IEM_CIMPL_DEF_3(iemCImpl_invpcid, uint8_t, iEffSeg, RTGCPTR, GCPtrInvpcidDesc, uint64_t, uInvpcidType)
|
---|
6664 | {
|
---|
6665 | /*
|
---|
6666 | * Check preconditions.
|
---|
6667 | */
|
---|
6668 | if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fInvpcid)
|
---|
6669 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
6670 |
|
---|
6671 | /* When in VMX non-root mode and INVPCID is not enabled, it results in #UD. */
|
---|
6672 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
6673 | && !IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_INVPCID))
|
---|
6674 | {
|
---|
6675 | Log(("invpcid: Not enabled for nested-guest execution -> #UD\n"));
|
---|
6676 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
6677 | }
|
---|
6678 |
|
---|
6679 | if (pVCpu->iem.s.uCpl != 0)
|
---|
6680 | {
|
---|
6681 | Log(("invpcid: CPL != 0 -> #GP(0)\n"));
|
---|
6682 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6683 | }
|
---|
6684 |
|
---|
6685 | if (IEM_IS_V86_MODE(pVCpu))
|
---|
6686 | {
|
---|
6687 | Log(("invpcid: v8086 mode -> #GP(0)\n"));
|
---|
6688 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6689 | }
|
---|
6690 |
|
---|
6691 | /*
|
---|
6692 | * Check nested-guest intercept.
|
---|
6693 | *
|
---|
6694 | * INVPCID causes a VM-exit if "enable INVPCID" and "INVLPG exiting" are
|
---|
6695 | * both set. We have already checked the former earlier in this function.
|
---|
6696 | *
|
---|
6697 | * CPL and virtual-8086 mode checks take priority over this VM-exit.
|
---|
6698 | * See Intel spec. "25.1.1 Relative Priority of Faults and VM Exits".
|
---|
6699 | */
|
---|
6700 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
6701 | && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_INVLPG_EXIT))
|
---|
6702 | {
|
---|
6703 | Log(("invpcid: Guest intercept -> #VM-exit\n"));
|
---|
6704 | IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_INVPCID, VMXINSTRID_NONE, cbInstr);
|
---|
6705 | }
|
---|
6706 |
|
---|
6707 | if (uInvpcidType > X86_INVPCID_TYPE_MAX_VALID)
|
---|
6708 | {
|
---|
6709 | Log(("invpcid: invalid/unrecognized invpcid type %#RX64 -> #GP(0)\n", uInvpcidType));
|
---|
6710 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6711 | }
|
---|
6712 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_CR3 | CPUMCTX_EXTRN_CR4 | CPUMCTX_EXTRN_EFER);
|
---|
6713 |
|
---|
6714 | /*
|
---|
6715 | * Fetch the invpcid descriptor from guest memory.
|
---|
6716 | */
|
---|
6717 | RTUINT128U uDesc;
|
---|
6718 | VBOXSTRICTRC rcStrict = iemMemFetchDataU128(pVCpu, &uDesc, iEffSeg, GCPtrInvpcidDesc);
|
---|
6719 | if (rcStrict == VINF_SUCCESS)
|
---|
6720 | {
|
---|
6721 | /*
|
---|
6722 | * Validate the descriptor.
|
---|
6723 | */
|
---|
6724 | if (uDesc.s.Lo > 0xfff)
|
---|
6725 | {
|
---|
6726 | Log(("invpcid: reserved bits set in invpcid descriptor %#RX64 -> #GP(0)\n", uDesc.s.Lo));
|
---|
6727 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6728 | }
|
---|
6729 |
|
---|
6730 | RTGCUINTPTR64 const GCPtrInvAddr = uDesc.s.Hi;
|
---|
6731 | uint8_t const uPcid = uDesc.s.Lo & UINT64_C(0xfff);
|
---|
6732 | uint32_t const uCr4 = pVCpu->cpum.GstCtx.cr4;
|
---|
6733 | uint64_t const uCr3 = pVCpu->cpum.GstCtx.cr3;
|
---|
6734 | switch (uInvpcidType)
|
---|
6735 | {
|
---|
6736 | case X86_INVPCID_TYPE_INDV_ADDR:
|
---|
6737 | {
|
---|
6738 | if (!IEM_IS_CANONICAL(GCPtrInvAddr))
|
---|
6739 | {
|
---|
6740 | Log(("invpcid: invalidation address %#RGP is not canonical -> #GP(0)\n", GCPtrInvAddr));
|
---|
6741 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6742 | }
|
---|
6743 | if ( !(uCr4 & X86_CR4_PCIDE)
|
---|
6744 | && uPcid != 0)
|
---|
6745 | {
|
---|
6746 | Log(("invpcid: invalid pcid %#x\n", uPcid));
|
---|
6747 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6748 | }
|
---|
6749 |
|
---|
6750 | /* Invalidate mappings for the linear address tagged with PCID except global translations. */
|
---|
6751 | PGMFlushTLB(pVCpu, uCr3, false /* fGlobal */, false /* fPdpesMapped */);
|
---|
6752 | break;
|
---|
6753 | }
|
---|
6754 |
|
---|
6755 | case X86_INVPCID_TYPE_SINGLE_CONTEXT:
|
---|
6756 | {
|
---|
6757 | if ( !(uCr4 & X86_CR4_PCIDE)
|
---|
6758 | && uPcid != 0)
|
---|
6759 | {
|
---|
6760 | Log(("invpcid: invalid pcid %#x\n", uPcid));
|
---|
6761 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6762 | }
|
---|
6763 | /* Invalidate all mappings associated with PCID except global translations. */
|
---|
6764 | PGMFlushTLB(pVCpu, uCr3, false /* fGlobal */, false /* fPdpesMapped */);
|
---|
6765 | break;
|
---|
6766 | }
|
---|
6767 |
|
---|
6768 | case X86_INVPCID_TYPE_ALL_CONTEXT_INCL_GLOBAL:
|
---|
6769 | {
|
---|
6770 | PGMFlushTLB(pVCpu, uCr3, true /* fGlobal */, false /* fPdpesMapped */);
|
---|
6771 | break;
|
---|
6772 | }
|
---|
6773 |
|
---|
6774 | case X86_INVPCID_TYPE_ALL_CONTEXT_EXCL_GLOBAL:
|
---|
6775 | {
|
---|
6776 | PGMFlushTLB(pVCpu, uCr3, false /* fGlobal */, false /* fPdpesMapped */);
|
---|
6777 | break;
|
---|
6778 | }
|
---|
6779 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
6780 | }
|
---|
6781 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
6782 | }
|
---|
6783 | return rcStrict;
|
---|
6784 | }
|
---|
6785 |
|
---|
6786 |
|
---|
6787 | /**
|
---|
6788 | * Implements INVD.
|
---|
6789 | */
|
---|
6790 | IEM_CIMPL_DEF_0(iemCImpl_invd)
|
---|
6791 | {
|
---|
6792 | if (pVCpu->iem.s.uCpl != 0)
|
---|
6793 | {
|
---|
6794 | Log(("invd: CPL != 0 -> #GP(0)\n"));
|
---|
6795 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6796 | }
|
---|
6797 |
|
---|
6798 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
6799 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_INVD, cbInstr);
|
---|
6800 |
|
---|
6801 | IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_INVD, SVM_EXIT_INVD, 0, 0);
|
---|
6802 |
|
---|
6803 | /* We currently take no action here. */
|
---|
6804 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
6805 | return VINF_SUCCESS;
|
---|
6806 | }
|
---|
6807 |
|
---|
6808 |
|
---|
6809 | /**
|
---|
6810 | * Implements WBINVD.
|
---|
6811 | */
|
---|
6812 | IEM_CIMPL_DEF_0(iemCImpl_wbinvd)
|
---|
6813 | {
|
---|
6814 | if (pVCpu->iem.s.uCpl != 0)
|
---|
6815 | {
|
---|
6816 | Log(("wbinvd: CPL != 0 -> #GP(0)\n"));
|
---|
6817 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6818 | }
|
---|
6819 |
|
---|
6820 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
6821 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_WBINVD, cbInstr);
|
---|
6822 |
|
---|
6823 | IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_WBINVD, SVM_EXIT_WBINVD, 0, 0);
|
---|
6824 |
|
---|
6825 | /* We currently take no action here. */
|
---|
6826 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
6827 | return VINF_SUCCESS;
|
---|
6828 | }
|
---|
6829 |
|
---|
6830 |
|
---|
6831 | /** Opcode 0x0f 0xaa. */
|
---|
6832 | IEM_CIMPL_DEF_0(iemCImpl_rsm)
|
---|
6833 | {
|
---|
6834 | IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_RSM, SVM_EXIT_RSM, 0, 0);
|
---|
6835 | NOREF(cbInstr);
|
---|
6836 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
6837 | }
|
---|
6838 |
|
---|
6839 |
|
---|
6840 | /**
|
---|
6841 | * Implements RDTSC.
|
---|
6842 | */
|
---|
6843 | IEM_CIMPL_DEF_0(iemCImpl_rdtsc)
|
---|
6844 | {
|
---|
6845 | /*
|
---|
6846 | * Check preconditions.
|
---|
6847 | */
|
---|
6848 | if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fTsc)
|
---|
6849 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
6850 |
|
---|
6851 | if (pVCpu->iem.s.uCpl != 0)
|
---|
6852 | {
|
---|
6853 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR4);
|
---|
6854 | if (pVCpu->cpum.GstCtx.cr4 & X86_CR4_TSD)
|
---|
6855 | {
|
---|
6856 | Log(("rdtsc: CR4.TSD and CPL=%u -> #GP(0)\n", pVCpu->iem.s.uCpl));
|
---|
6857 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6858 | }
|
---|
6859 | }
|
---|
6860 |
|
---|
6861 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
6862 | && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_RDTSC_EXIT))
|
---|
6863 | {
|
---|
6864 | Log(("rdtsc: Guest intercept -> VM-exit\n"));
|
---|
6865 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_RDTSC, cbInstr);
|
---|
6866 | }
|
---|
6867 |
|
---|
6868 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_RDTSC))
|
---|
6869 | {
|
---|
6870 | Log(("rdtsc: Guest intercept -> #VMEXIT\n"));
|
---|
6871 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
6872 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_RDTSC, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
6873 | }
|
---|
6874 |
|
---|
6875 | /*
|
---|
6876 | * Do the job.
|
---|
6877 | */
|
---|
6878 | uint64_t uTicks = TMCpuTickGet(pVCpu);
|
---|
6879 | #if defined(VBOX_WITH_NESTED_HWVIRT_SVM) || defined(VBOX_WITH_NESTED_HWVIRT_VMX)
|
---|
6880 | uTicks = CPUMApplyNestedGuestTscOffset(pVCpu, uTicks);
|
---|
6881 | #endif
|
---|
6882 | pVCpu->cpum.GstCtx.rax = RT_LO_U32(uTicks);
|
---|
6883 | pVCpu->cpum.GstCtx.rdx = RT_HI_U32(uTicks);
|
---|
6884 | pVCpu->cpum.GstCtx.fExtrn &= ~(CPUMCTX_EXTRN_RAX | CPUMCTX_EXTRN_RDX); /* For IEMExecDecodedRdtsc. */
|
---|
6885 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
6886 | return VINF_SUCCESS;
|
---|
6887 | }
|
---|
6888 |
|
---|
6889 |
|
---|
6890 | /**
|
---|
6891 | * Implements RDTSC.
|
---|
6892 | */
|
---|
6893 | IEM_CIMPL_DEF_0(iemCImpl_rdtscp)
|
---|
6894 | {
|
---|
6895 | /*
|
---|
6896 | * Check preconditions.
|
---|
6897 | */
|
---|
6898 | if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fRdTscP)
|
---|
6899 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
6900 |
|
---|
6901 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
6902 | && !IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_RDTSCP))
|
---|
6903 | {
|
---|
6904 | Log(("rdtscp: Not enabled for VMX non-root mode -> #UD\n"));
|
---|
6905 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
6906 | }
|
---|
6907 |
|
---|
6908 | if (pVCpu->iem.s.uCpl != 0)
|
---|
6909 | {
|
---|
6910 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR4);
|
---|
6911 | if (pVCpu->cpum.GstCtx.cr4 & X86_CR4_TSD)
|
---|
6912 | {
|
---|
6913 | Log(("rdtscp: CR4.TSD and CPL=%u -> #GP(0)\n", pVCpu->iem.s.uCpl));
|
---|
6914 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6915 | }
|
---|
6916 | }
|
---|
6917 |
|
---|
6918 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
6919 | && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_RDTSC_EXIT))
|
---|
6920 | {
|
---|
6921 | Log(("rdtscp: Guest intercept -> VM-exit\n"));
|
---|
6922 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_RDTSCP, cbInstr);
|
---|
6923 | }
|
---|
6924 | else if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_RDTSCP))
|
---|
6925 | {
|
---|
6926 | Log(("rdtscp: Guest intercept -> #VMEXIT\n"));
|
---|
6927 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
6928 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_RDTSCP, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
6929 | }
|
---|
6930 |
|
---|
6931 | /*
|
---|
6932 | * Do the job.
|
---|
6933 | * Query the MSR first in case of trips to ring-3.
|
---|
6934 | */
|
---|
6935 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_TSC_AUX);
|
---|
6936 | VBOXSTRICTRC rcStrict = CPUMQueryGuestMsr(pVCpu, MSR_K8_TSC_AUX, &pVCpu->cpum.GstCtx.rcx);
|
---|
6937 | if (rcStrict == VINF_SUCCESS)
|
---|
6938 | {
|
---|
6939 | /* Low dword of the TSC_AUX msr only. */
|
---|
6940 | pVCpu->cpum.GstCtx.rcx &= UINT32_C(0xffffffff);
|
---|
6941 |
|
---|
6942 | uint64_t uTicks = TMCpuTickGet(pVCpu);
|
---|
6943 | #if defined(VBOX_WITH_NESTED_HWVIRT_SVM) || defined(VBOX_WITH_NESTED_HWVIRT_VMX)
|
---|
6944 | uTicks = CPUMApplyNestedGuestTscOffset(pVCpu, uTicks);
|
---|
6945 | #endif
|
---|
6946 | pVCpu->cpum.GstCtx.rax = RT_LO_U32(uTicks);
|
---|
6947 | pVCpu->cpum.GstCtx.rdx = RT_HI_U32(uTicks);
|
---|
6948 | pVCpu->cpum.GstCtx.fExtrn &= ~(CPUMCTX_EXTRN_RAX | CPUMCTX_EXTRN_RDX | CPUMCTX_EXTRN_RCX); /* For IEMExecDecodedRdtscp. */
|
---|
6949 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
6950 | }
|
---|
6951 | return rcStrict;
|
---|
6952 | }
|
---|
6953 |
|
---|
6954 |
|
---|
6955 | /**
|
---|
6956 | * Implements RDPMC.
|
---|
6957 | */
|
---|
6958 | IEM_CIMPL_DEF_0(iemCImpl_rdpmc)
|
---|
6959 | {
|
---|
6960 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR4);
|
---|
6961 |
|
---|
6962 | if ( pVCpu->iem.s.uCpl != 0
|
---|
6963 | && !(pVCpu->cpum.GstCtx.cr4 & X86_CR4_PCE))
|
---|
6964 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
6965 |
|
---|
6966 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
6967 | && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_RDPMC_EXIT))
|
---|
6968 | {
|
---|
6969 | Log(("rdpmc: Guest intercept -> VM-exit\n"));
|
---|
6970 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_RDPMC, cbInstr);
|
---|
6971 | }
|
---|
6972 |
|
---|
6973 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_RDPMC))
|
---|
6974 | {
|
---|
6975 | Log(("rdpmc: Guest intercept -> #VMEXIT\n"));
|
---|
6976 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
6977 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_RDPMC, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
6978 | }
|
---|
6979 |
|
---|
6980 | /** @todo Emulate performance counters, for now just return 0. */
|
---|
6981 | pVCpu->cpum.GstCtx.rax = 0;
|
---|
6982 | pVCpu->cpum.GstCtx.rdx = 0;
|
---|
6983 | pVCpu->cpum.GstCtx.fExtrn &= ~(CPUMCTX_EXTRN_RAX | CPUMCTX_EXTRN_RDX);
|
---|
6984 | /** @todo We should trigger a \#GP here if the CPU doesn't support the index in
|
---|
6985 | * ecx but see @bugref{3472}! */
|
---|
6986 |
|
---|
6987 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
6988 | return VINF_SUCCESS;
|
---|
6989 | }
|
---|
6990 |
|
---|
6991 |
|
---|
6992 | /**
|
---|
6993 | * Implements RDMSR.
|
---|
6994 | */
|
---|
6995 | IEM_CIMPL_DEF_0(iemCImpl_rdmsr)
|
---|
6996 | {
|
---|
6997 | /*
|
---|
6998 | * Check preconditions.
|
---|
6999 | */
|
---|
7000 | if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fMsr)
|
---|
7001 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
7002 | if (pVCpu->iem.s.uCpl != 0)
|
---|
7003 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7004 |
|
---|
7005 | /*
|
---|
7006 | * Check nested-guest intercepts.
|
---|
7007 | */
|
---|
7008 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
7009 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
7010 | {
|
---|
7011 | if (iemVmxIsRdmsrWrmsrInterceptSet(pVCpu, VMX_EXIT_RDMSR, pVCpu->cpum.GstCtx.ecx))
|
---|
7012 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_RDMSR, cbInstr);
|
---|
7013 | }
|
---|
7014 | #endif
|
---|
7015 |
|
---|
7016 | #ifdef VBOX_WITH_NESTED_HWVIRT_SVM
|
---|
7017 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_MSR_PROT))
|
---|
7018 | {
|
---|
7019 | VBOXSTRICTRC rcStrict = iemSvmHandleMsrIntercept(pVCpu, pVCpu->cpum.GstCtx.ecx, false /* fWrite */);
|
---|
7020 | if (rcStrict == VINF_SVM_VMEXIT)
|
---|
7021 | return VINF_SUCCESS;
|
---|
7022 | if (rcStrict != VINF_SVM_INTERCEPT_NOT_ACTIVE)
|
---|
7023 | {
|
---|
7024 | Log(("IEM: SVM intercepted rdmsr(%#x) failed. rc=%Rrc\n", pVCpu->cpum.GstCtx.ecx, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
7025 | return rcStrict;
|
---|
7026 | }
|
---|
7027 | }
|
---|
7028 | #endif
|
---|
7029 |
|
---|
7030 | /*
|
---|
7031 | * Do the job.
|
---|
7032 | */
|
---|
7033 | RTUINT64U uValue;
|
---|
7034 | /** @todo make CPUMAllMsrs.cpp import the necessary MSR state. */
|
---|
7035 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_ALL_MSRS);
|
---|
7036 |
|
---|
7037 | VBOXSTRICTRC rcStrict = CPUMQueryGuestMsr(pVCpu, pVCpu->cpum.GstCtx.ecx, &uValue.u);
|
---|
7038 | if (rcStrict == VINF_SUCCESS)
|
---|
7039 | {
|
---|
7040 | pVCpu->cpum.GstCtx.rax = uValue.s.Lo;
|
---|
7041 | pVCpu->cpum.GstCtx.rdx = uValue.s.Hi;
|
---|
7042 | pVCpu->cpum.GstCtx.fExtrn &= ~(CPUMCTX_EXTRN_RAX | CPUMCTX_EXTRN_RDX);
|
---|
7043 |
|
---|
7044 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
7045 | return VINF_SUCCESS;
|
---|
7046 | }
|
---|
7047 |
|
---|
7048 | #ifndef IN_RING3
|
---|
7049 | /* Deferred to ring-3. */
|
---|
7050 | if (rcStrict == VINF_CPUM_R3_MSR_READ)
|
---|
7051 | {
|
---|
7052 | Log(("IEM: rdmsr(%#x) -> ring-3\n", pVCpu->cpum.GstCtx.ecx));
|
---|
7053 | return rcStrict;
|
---|
7054 | }
|
---|
7055 | #endif
|
---|
7056 |
|
---|
7057 | /* Often a unimplemented MSR or MSR bit, so worth logging. */
|
---|
7058 | if (pVCpu->iem.s.cLogRelRdMsr < 32)
|
---|
7059 | {
|
---|
7060 | pVCpu->iem.s.cLogRelRdMsr++;
|
---|
7061 | LogRel(("IEM: rdmsr(%#x) -> #GP(0)\n", pVCpu->cpum.GstCtx.ecx));
|
---|
7062 | }
|
---|
7063 | else
|
---|
7064 | Log(( "IEM: rdmsr(%#x) -> #GP(0)\n", pVCpu->cpum.GstCtx.ecx));
|
---|
7065 | AssertMsgReturn(rcStrict == VERR_CPUM_RAISE_GP_0, ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)), VERR_IPE_UNEXPECTED_STATUS);
|
---|
7066 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7067 | }
|
---|
7068 |
|
---|
7069 |
|
---|
7070 | /**
|
---|
7071 | * Implements WRMSR.
|
---|
7072 | */
|
---|
7073 | IEM_CIMPL_DEF_0(iemCImpl_wrmsr)
|
---|
7074 | {
|
---|
7075 | /*
|
---|
7076 | * Check preconditions.
|
---|
7077 | */
|
---|
7078 | if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fMsr)
|
---|
7079 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
7080 | if (pVCpu->iem.s.uCpl != 0)
|
---|
7081 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7082 |
|
---|
7083 | RTUINT64U uValue;
|
---|
7084 | uValue.s.Lo = pVCpu->cpum.GstCtx.eax;
|
---|
7085 | uValue.s.Hi = pVCpu->cpum.GstCtx.edx;
|
---|
7086 |
|
---|
7087 | uint32_t const idMsr = pVCpu->cpum.GstCtx.ecx;
|
---|
7088 |
|
---|
7089 | /** @todo make CPUMAllMsrs.cpp import the necessary MSR state. */
|
---|
7090 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_ALL_MSRS);
|
---|
7091 |
|
---|
7092 | /*
|
---|
7093 | * Check nested-guest intercepts.
|
---|
7094 | */
|
---|
7095 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
7096 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
7097 | {
|
---|
7098 | if (iemVmxIsRdmsrWrmsrInterceptSet(pVCpu, VMX_EXIT_WRMSR, idMsr))
|
---|
7099 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_WRMSR, cbInstr);
|
---|
7100 | }
|
---|
7101 | #endif
|
---|
7102 |
|
---|
7103 | #ifdef VBOX_WITH_NESTED_HWVIRT_SVM
|
---|
7104 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_MSR_PROT))
|
---|
7105 | {
|
---|
7106 | VBOXSTRICTRC rcStrict = iemSvmHandleMsrIntercept(pVCpu, idMsr, true /* fWrite */);
|
---|
7107 | if (rcStrict == VINF_SVM_VMEXIT)
|
---|
7108 | return VINF_SUCCESS;
|
---|
7109 | if (rcStrict != VINF_SVM_INTERCEPT_NOT_ACTIVE)
|
---|
7110 | {
|
---|
7111 | Log(("IEM: SVM intercepted rdmsr(%#x) failed. rc=%Rrc\n", idMsr, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
7112 | return rcStrict;
|
---|
7113 | }
|
---|
7114 | }
|
---|
7115 | #endif
|
---|
7116 |
|
---|
7117 | /*
|
---|
7118 | * Do the job.
|
---|
7119 | */
|
---|
7120 | VBOXSTRICTRC rcStrict = CPUMSetGuestMsr(pVCpu, idMsr, uValue.u);
|
---|
7121 | if (rcStrict == VINF_SUCCESS)
|
---|
7122 | {
|
---|
7123 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
7124 | return VINF_SUCCESS;
|
---|
7125 | }
|
---|
7126 |
|
---|
7127 | #ifndef IN_RING3
|
---|
7128 | /* Deferred to ring-3. */
|
---|
7129 | if (rcStrict == VINF_CPUM_R3_MSR_WRITE)
|
---|
7130 | {
|
---|
7131 | Log(("IEM: wrmsr(%#x) -> ring-3\n", idMsr));
|
---|
7132 | return rcStrict;
|
---|
7133 | }
|
---|
7134 | #endif
|
---|
7135 |
|
---|
7136 | /* Often a unimplemented MSR or MSR bit, so worth logging. */
|
---|
7137 | if (pVCpu->iem.s.cLogRelWrMsr < 32)
|
---|
7138 | {
|
---|
7139 | pVCpu->iem.s.cLogRelWrMsr++;
|
---|
7140 | LogRel(("IEM: wrmsr(%#x,%#x`%08x) -> #GP(0)\n", idMsr, uValue.s.Hi, uValue.s.Lo));
|
---|
7141 | }
|
---|
7142 | else
|
---|
7143 | Log(( "IEM: wrmsr(%#x,%#x`%08x) -> #GP(0)\n", idMsr, uValue.s.Hi, uValue.s.Lo));
|
---|
7144 | AssertMsgReturn(rcStrict == VERR_CPUM_RAISE_GP_0, ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)), VERR_IPE_UNEXPECTED_STATUS);
|
---|
7145 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7146 | }
|
---|
7147 |
|
---|
7148 |
|
---|
7149 | /**
|
---|
7150 | * Implements 'IN eAX, port'.
|
---|
7151 | *
|
---|
7152 | * @param u16Port The source port.
|
---|
7153 | * @param fImm Whether the port was specified through an immediate operand
|
---|
7154 | * or the implicit DX register.
|
---|
7155 | * @param cbReg The register size.
|
---|
7156 | */
|
---|
7157 | IEM_CIMPL_DEF_3(iemCImpl_in, uint16_t, u16Port, bool, fImm, uint8_t, cbReg)
|
---|
7158 | {
|
---|
7159 | /*
|
---|
7160 | * CPL check
|
---|
7161 | */
|
---|
7162 | VBOXSTRICTRC rcStrict = iemHlpCheckPortIOPermission(pVCpu, u16Port, cbReg);
|
---|
7163 | if (rcStrict != VINF_SUCCESS)
|
---|
7164 | return rcStrict;
|
---|
7165 |
|
---|
7166 | /*
|
---|
7167 | * Check VMX nested-guest IO intercept.
|
---|
7168 | */
|
---|
7169 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
7170 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
7171 | {
|
---|
7172 | rcStrict = iemVmxVmexitInstrIo(pVCpu, VMXINSTRID_IO_IN, u16Port, fImm, cbReg, cbInstr);
|
---|
7173 | if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
|
---|
7174 | return rcStrict;
|
---|
7175 | }
|
---|
7176 | #else
|
---|
7177 | RT_NOREF(fImm);
|
---|
7178 | #endif
|
---|
7179 |
|
---|
7180 | /*
|
---|
7181 | * Check SVM nested-guest IO intercept.
|
---|
7182 | */
|
---|
7183 | #ifdef VBOX_WITH_NESTED_HWVIRT_SVM
|
---|
7184 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_IOIO_PROT))
|
---|
7185 | {
|
---|
7186 | uint8_t cAddrSizeBits;
|
---|
7187 | switch (pVCpu->iem.s.enmEffAddrMode)
|
---|
7188 | {
|
---|
7189 | case IEMMODE_16BIT: cAddrSizeBits = 16; break;
|
---|
7190 | case IEMMODE_32BIT: cAddrSizeBits = 32; break;
|
---|
7191 | case IEMMODE_64BIT: cAddrSizeBits = 64; break;
|
---|
7192 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
7193 | }
|
---|
7194 | rcStrict = iemSvmHandleIOIntercept(pVCpu, u16Port, SVMIOIOTYPE_IN, cbReg, cAddrSizeBits, 0 /* N/A - iEffSeg */,
|
---|
7195 | false /* fRep */, false /* fStrIo */, cbInstr);
|
---|
7196 | if (rcStrict == VINF_SVM_VMEXIT)
|
---|
7197 | return VINF_SUCCESS;
|
---|
7198 | if (rcStrict != VINF_SVM_INTERCEPT_NOT_ACTIVE)
|
---|
7199 | {
|
---|
7200 | Log(("iemCImpl_in: iemSvmHandleIOIntercept failed (u16Port=%#x, cbReg=%u) rc=%Rrc\n", u16Port, cbReg,
|
---|
7201 | VBOXSTRICTRC_VAL(rcStrict)));
|
---|
7202 | return rcStrict;
|
---|
7203 | }
|
---|
7204 | }
|
---|
7205 | #endif
|
---|
7206 |
|
---|
7207 | /*
|
---|
7208 | * Perform the I/O.
|
---|
7209 | */
|
---|
7210 | uint32_t u32Value = 0;
|
---|
7211 | rcStrict = IOMIOPortRead(pVCpu->CTX_SUFF(pVM), pVCpu, u16Port, &u32Value, cbReg);
|
---|
7212 | if (IOM_SUCCESS(rcStrict))
|
---|
7213 | {
|
---|
7214 | switch (cbReg)
|
---|
7215 | {
|
---|
7216 | case 1: pVCpu->cpum.GstCtx.al = (uint8_t)u32Value; break;
|
---|
7217 | case 2: pVCpu->cpum.GstCtx.ax = (uint16_t)u32Value; break;
|
---|
7218 | case 4: pVCpu->cpum.GstCtx.rax = u32Value; break;
|
---|
7219 | default: AssertFailedReturn(VERR_IEM_IPE_3);
|
---|
7220 | }
|
---|
7221 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
7222 | pVCpu->iem.s.cPotentialExits++;
|
---|
7223 | if (rcStrict != VINF_SUCCESS)
|
---|
7224 | rcStrict = iemSetPassUpStatus(pVCpu, rcStrict);
|
---|
7225 | Assert(rcStrict == VINF_SUCCESS); /* assumed below */
|
---|
7226 |
|
---|
7227 | /*
|
---|
7228 | * Check for I/O breakpoints.
|
---|
7229 | */
|
---|
7230 | uint32_t const uDr7 = pVCpu->cpum.GstCtx.dr[7];
|
---|
7231 | if (RT_UNLIKELY( ( (uDr7 & X86_DR7_ENABLED_MASK)
|
---|
7232 | && X86_DR7_ANY_RW_IO(uDr7)
|
---|
7233 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_DE))
|
---|
7234 | || DBGFBpIsHwIoArmed(pVCpu->CTX_SUFF(pVM))))
|
---|
7235 | {
|
---|
7236 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3 | CPUMCTX_EXTRN_DR6);
|
---|
7237 | rcStrict = DBGFBpCheckIo(pVCpu->CTX_SUFF(pVM), pVCpu, IEM_GET_CTX(pVCpu), u16Port, cbReg);
|
---|
7238 | if (rcStrict == VINF_EM_RAW_GUEST_TRAP)
|
---|
7239 | rcStrict = iemRaiseDebugException(pVCpu);
|
---|
7240 | }
|
---|
7241 | }
|
---|
7242 |
|
---|
7243 | return rcStrict;
|
---|
7244 | }
|
---|
7245 |
|
---|
7246 |
|
---|
7247 | /**
|
---|
7248 | * Implements 'IN eAX, DX'.
|
---|
7249 | *
|
---|
7250 | * @param cbReg The register size.
|
---|
7251 | */
|
---|
7252 | IEM_CIMPL_DEF_1(iemCImpl_in_eAX_DX, uint8_t, cbReg)
|
---|
7253 | {
|
---|
7254 | return IEM_CIMPL_CALL_3(iemCImpl_in, pVCpu->cpum.GstCtx.dx, false /* fImm */, cbReg);
|
---|
7255 | }
|
---|
7256 |
|
---|
7257 |
|
---|
7258 | /**
|
---|
7259 | * Implements 'OUT port, eAX'.
|
---|
7260 | *
|
---|
7261 | * @param u16Port The destination port.
|
---|
7262 | * @param fImm Whether the port was specified through an immediate operand
|
---|
7263 | * or the implicit DX register.
|
---|
7264 | * @param cbReg The register size.
|
---|
7265 | */
|
---|
7266 | IEM_CIMPL_DEF_3(iemCImpl_out, uint16_t, u16Port, bool, fImm, uint8_t, cbReg)
|
---|
7267 | {
|
---|
7268 | /*
|
---|
7269 | * CPL check
|
---|
7270 | */
|
---|
7271 | VBOXSTRICTRC rcStrict = iemHlpCheckPortIOPermission(pVCpu, u16Port, cbReg);
|
---|
7272 | if (rcStrict != VINF_SUCCESS)
|
---|
7273 | return rcStrict;
|
---|
7274 |
|
---|
7275 | /*
|
---|
7276 | * Check VMX nested-guest I/O intercept.
|
---|
7277 | */
|
---|
7278 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
7279 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
7280 | {
|
---|
7281 | rcStrict = iemVmxVmexitInstrIo(pVCpu, VMXINSTRID_IO_OUT, u16Port, fImm, cbReg, cbInstr);
|
---|
7282 | if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
|
---|
7283 | return rcStrict;
|
---|
7284 | }
|
---|
7285 | #else
|
---|
7286 | RT_NOREF(fImm);
|
---|
7287 | #endif
|
---|
7288 |
|
---|
7289 | /*
|
---|
7290 | * Check SVM nested-guest I/O intercept.
|
---|
7291 | */
|
---|
7292 | #ifdef VBOX_WITH_NESTED_HWVIRT_SVM
|
---|
7293 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_IOIO_PROT))
|
---|
7294 | {
|
---|
7295 | uint8_t cAddrSizeBits;
|
---|
7296 | switch (pVCpu->iem.s.enmEffAddrMode)
|
---|
7297 | {
|
---|
7298 | case IEMMODE_16BIT: cAddrSizeBits = 16; break;
|
---|
7299 | case IEMMODE_32BIT: cAddrSizeBits = 32; break;
|
---|
7300 | case IEMMODE_64BIT: cAddrSizeBits = 64; break;
|
---|
7301 | IEM_NOT_REACHED_DEFAULT_CASE_RET();
|
---|
7302 | }
|
---|
7303 | rcStrict = iemSvmHandleIOIntercept(pVCpu, u16Port, SVMIOIOTYPE_OUT, cbReg, cAddrSizeBits, 0 /* N/A - iEffSeg */,
|
---|
7304 | false /* fRep */, false /* fStrIo */, cbInstr);
|
---|
7305 | if (rcStrict == VINF_SVM_VMEXIT)
|
---|
7306 | return VINF_SUCCESS;
|
---|
7307 | if (rcStrict != VINF_SVM_INTERCEPT_NOT_ACTIVE)
|
---|
7308 | {
|
---|
7309 | Log(("iemCImpl_out: iemSvmHandleIOIntercept failed (u16Port=%#x, cbReg=%u) rc=%Rrc\n", u16Port, cbReg,
|
---|
7310 | VBOXSTRICTRC_VAL(rcStrict)));
|
---|
7311 | return rcStrict;
|
---|
7312 | }
|
---|
7313 | }
|
---|
7314 | #endif
|
---|
7315 |
|
---|
7316 | /*
|
---|
7317 | * Perform the I/O.
|
---|
7318 | */
|
---|
7319 | uint32_t u32Value;
|
---|
7320 | switch (cbReg)
|
---|
7321 | {
|
---|
7322 | case 1: u32Value = pVCpu->cpum.GstCtx.al; break;
|
---|
7323 | case 2: u32Value = pVCpu->cpum.GstCtx.ax; break;
|
---|
7324 | case 4: u32Value = pVCpu->cpum.GstCtx.eax; break;
|
---|
7325 | default: AssertFailedReturn(VERR_IEM_IPE_4);
|
---|
7326 | }
|
---|
7327 | rcStrict = IOMIOPortWrite(pVCpu->CTX_SUFF(pVM), pVCpu, u16Port, u32Value, cbReg);
|
---|
7328 | if (IOM_SUCCESS(rcStrict))
|
---|
7329 | {
|
---|
7330 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
7331 | pVCpu->iem.s.cPotentialExits++;
|
---|
7332 | if (rcStrict != VINF_SUCCESS)
|
---|
7333 | rcStrict = iemSetPassUpStatus(pVCpu, rcStrict);
|
---|
7334 | Assert(rcStrict == VINF_SUCCESS); /* assumed below */
|
---|
7335 |
|
---|
7336 | /*
|
---|
7337 | * Check for I/O breakpoints.
|
---|
7338 | */
|
---|
7339 | uint32_t const uDr7 = pVCpu->cpum.GstCtx.dr[7];
|
---|
7340 | if (RT_UNLIKELY( ( (uDr7 & X86_DR7_ENABLED_MASK)
|
---|
7341 | && X86_DR7_ANY_RW_IO(uDr7)
|
---|
7342 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_DE))
|
---|
7343 | || DBGFBpIsHwIoArmed(pVCpu->CTX_SUFF(pVM))))
|
---|
7344 | {
|
---|
7345 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3 | CPUMCTX_EXTRN_DR6);
|
---|
7346 | rcStrict = DBGFBpCheckIo(pVCpu->CTX_SUFF(pVM), pVCpu, IEM_GET_CTX(pVCpu), u16Port, cbReg);
|
---|
7347 | if (rcStrict == VINF_EM_RAW_GUEST_TRAP)
|
---|
7348 | rcStrict = iemRaiseDebugException(pVCpu);
|
---|
7349 | }
|
---|
7350 | }
|
---|
7351 | return rcStrict;
|
---|
7352 | }
|
---|
7353 |
|
---|
7354 |
|
---|
7355 | /**
|
---|
7356 | * Implements 'OUT DX, eAX'.
|
---|
7357 | *
|
---|
7358 | * @param cbReg The register size.
|
---|
7359 | */
|
---|
7360 | IEM_CIMPL_DEF_1(iemCImpl_out_DX_eAX, uint8_t, cbReg)
|
---|
7361 | {
|
---|
7362 | return IEM_CIMPL_CALL_3(iemCImpl_out, pVCpu->cpum.GstCtx.dx, false /* fImm */, cbReg);
|
---|
7363 | }
|
---|
7364 |
|
---|
7365 |
|
---|
7366 | /**
|
---|
7367 | * Implements 'CLI'.
|
---|
7368 | */
|
---|
7369 | IEM_CIMPL_DEF_0(iemCImpl_cli)
|
---|
7370 | {
|
---|
7371 | uint32_t fEfl = IEMMISC_GET_EFL(pVCpu);
|
---|
7372 | uint32_t const fEflOld = fEfl;
|
---|
7373 |
|
---|
7374 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_CR4);
|
---|
7375 | if (pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE)
|
---|
7376 | {
|
---|
7377 | uint8_t const uIopl = X86_EFL_GET_IOPL(fEfl);
|
---|
7378 | if (!(fEfl & X86_EFL_VM))
|
---|
7379 | {
|
---|
7380 | if (pVCpu->iem.s.uCpl <= uIopl)
|
---|
7381 | fEfl &= ~X86_EFL_IF;
|
---|
7382 | else if ( pVCpu->iem.s.uCpl == 3
|
---|
7383 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_PVI) )
|
---|
7384 | fEfl &= ~X86_EFL_VIF;
|
---|
7385 | else
|
---|
7386 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7387 | }
|
---|
7388 | /* V8086 */
|
---|
7389 | else if (uIopl == 3)
|
---|
7390 | fEfl &= ~X86_EFL_IF;
|
---|
7391 | else if ( uIopl < 3
|
---|
7392 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_VME) )
|
---|
7393 | fEfl &= ~X86_EFL_VIF;
|
---|
7394 | else
|
---|
7395 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7396 | }
|
---|
7397 | /* real mode */
|
---|
7398 | else
|
---|
7399 | fEfl &= ~X86_EFL_IF;
|
---|
7400 |
|
---|
7401 | /* Commit. */
|
---|
7402 | IEMMISC_SET_EFL(pVCpu, fEfl);
|
---|
7403 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
7404 | Log2(("CLI: %#x -> %#x\n", fEflOld, fEfl)); NOREF(fEflOld);
|
---|
7405 | return VINF_SUCCESS;
|
---|
7406 | }
|
---|
7407 |
|
---|
7408 |
|
---|
7409 | /**
|
---|
7410 | * Implements 'STI'.
|
---|
7411 | */
|
---|
7412 | IEM_CIMPL_DEF_0(iemCImpl_sti)
|
---|
7413 | {
|
---|
7414 | uint32_t fEfl = IEMMISC_GET_EFL(pVCpu);
|
---|
7415 | uint32_t const fEflOld = fEfl;
|
---|
7416 |
|
---|
7417 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_CR4);
|
---|
7418 | if (pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE)
|
---|
7419 | {
|
---|
7420 | uint8_t const uIopl = X86_EFL_GET_IOPL(fEfl);
|
---|
7421 | if (!(fEfl & X86_EFL_VM))
|
---|
7422 | {
|
---|
7423 | if (pVCpu->iem.s.uCpl <= uIopl)
|
---|
7424 | fEfl |= X86_EFL_IF;
|
---|
7425 | else if ( pVCpu->iem.s.uCpl == 3
|
---|
7426 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_PVI)
|
---|
7427 | && !(fEfl & X86_EFL_VIP) )
|
---|
7428 | fEfl |= X86_EFL_VIF;
|
---|
7429 | else
|
---|
7430 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7431 | }
|
---|
7432 | /* V8086 */
|
---|
7433 | else if (uIopl == 3)
|
---|
7434 | fEfl |= X86_EFL_IF;
|
---|
7435 | else if ( uIopl < 3
|
---|
7436 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_VME)
|
---|
7437 | && !(fEfl & X86_EFL_VIP) )
|
---|
7438 | fEfl |= X86_EFL_VIF;
|
---|
7439 | else
|
---|
7440 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7441 | }
|
---|
7442 | /* real mode */
|
---|
7443 | else
|
---|
7444 | fEfl |= X86_EFL_IF;
|
---|
7445 |
|
---|
7446 | /* Commit. */
|
---|
7447 | IEMMISC_SET_EFL(pVCpu, fEfl);
|
---|
7448 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
7449 | if (!(fEflOld & X86_EFL_IF) && (fEfl & X86_EFL_IF))
|
---|
7450 | EMSetInhibitInterruptsPC(pVCpu, pVCpu->cpum.GstCtx.rip);
|
---|
7451 | Log2(("STI: %#x -> %#x\n", fEflOld, fEfl));
|
---|
7452 | return VINF_SUCCESS;
|
---|
7453 | }
|
---|
7454 |
|
---|
7455 |
|
---|
7456 | /**
|
---|
7457 | * Implements 'HLT'.
|
---|
7458 | */
|
---|
7459 | IEM_CIMPL_DEF_0(iemCImpl_hlt)
|
---|
7460 | {
|
---|
7461 | if (pVCpu->iem.s.uCpl != 0)
|
---|
7462 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7463 |
|
---|
7464 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
7465 | && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_HLT_EXIT))
|
---|
7466 | {
|
---|
7467 | Log2(("hlt: Guest intercept -> VM-exit\n"));
|
---|
7468 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_HLT, cbInstr);
|
---|
7469 | }
|
---|
7470 |
|
---|
7471 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_HLT))
|
---|
7472 | {
|
---|
7473 | Log2(("hlt: Guest intercept -> #VMEXIT\n"));
|
---|
7474 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
7475 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_HLT, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
7476 | }
|
---|
7477 |
|
---|
7478 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
7479 | return VINF_EM_HALT;
|
---|
7480 | }
|
---|
7481 |
|
---|
7482 |
|
---|
7483 | /**
|
---|
7484 | * Implements 'MONITOR'.
|
---|
7485 | */
|
---|
7486 | IEM_CIMPL_DEF_1(iemCImpl_monitor, uint8_t, iEffSeg)
|
---|
7487 | {
|
---|
7488 | /*
|
---|
7489 | * Permission checks.
|
---|
7490 | */
|
---|
7491 | if (pVCpu->iem.s.uCpl != 0)
|
---|
7492 | {
|
---|
7493 | Log2(("monitor: CPL != 0\n"));
|
---|
7494 | return iemRaiseUndefinedOpcode(pVCpu); /** @todo MSR[0xC0010015].MonMwaitUserEn if we care. */
|
---|
7495 | }
|
---|
7496 | if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fMonitorMWait)
|
---|
7497 | {
|
---|
7498 | Log2(("monitor: Not in CPUID\n"));
|
---|
7499 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
7500 | }
|
---|
7501 |
|
---|
7502 | /*
|
---|
7503 | * Check VMX guest-intercept.
|
---|
7504 | * This should be considered a fault-like VM-exit.
|
---|
7505 | * See Intel spec. 25.1.1 "Relative Priority of Faults and VM Exits".
|
---|
7506 | */
|
---|
7507 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
7508 | && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_MONITOR_EXIT))
|
---|
7509 | {
|
---|
7510 | Log2(("monitor: Guest intercept -> #VMEXIT\n"));
|
---|
7511 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_MONITOR, cbInstr);
|
---|
7512 | }
|
---|
7513 |
|
---|
7514 | /*
|
---|
7515 | * Gather the operands and validate them.
|
---|
7516 | */
|
---|
7517 | RTGCPTR GCPtrMem = pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT ? pVCpu->cpum.GstCtx.rax : pVCpu->cpum.GstCtx.eax;
|
---|
7518 | uint32_t uEcx = pVCpu->cpum.GstCtx.ecx;
|
---|
7519 | uint32_t uEdx = pVCpu->cpum.GstCtx.edx;
|
---|
7520 | /** @todo Test whether EAX or ECX is processed first, i.e. do we get \#PF or
|
---|
7521 | * \#GP first. */
|
---|
7522 | if (uEcx != 0)
|
---|
7523 | {
|
---|
7524 | Log2(("monitor rax=%RX64, ecx=%RX32, edx=%RX32; ECX != 0 -> #GP(0)\n", GCPtrMem, uEcx, uEdx)); NOREF(uEdx);
|
---|
7525 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7526 | }
|
---|
7527 |
|
---|
7528 | VBOXSTRICTRC rcStrict = iemMemApplySegment(pVCpu, IEM_ACCESS_TYPE_READ | IEM_ACCESS_WHAT_DATA, iEffSeg, 1, &GCPtrMem);
|
---|
7529 | if (rcStrict != VINF_SUCCESS)
|
---|
7530 | return rcStrict;
|
---|
7531 |
|
---|
7532 | RTGCPHYS GCPhysMem;
|
---|
7533 | rcStrict = iemMemPageTranslateAndCheckAccess(pVCpu, GCPtrMem, IEM_ACCESS_TYPE_READ | IEM_ACCESS_WHAT_DATA, &GCPhysMem);
|
---|
7534 | if (rcStrict != VINF_SUCCESS)
|
---|
7535 | return rcStrict;
|
---|
7536 |
|
---|
7537 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
7538 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
7539 | && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_VIRT_APIC_ACCESS))
|
---|
7540 | {
|
---|
7541 | /*
|
---|
7542 | * MONITOR does not access the memory, just monitors the address. However,
|
---|
7543 | * if the address falls in the APIC-access page, the address monitored must
|
---|
7544 | * instead be the corresponding address in the virtual-APIC page.
|
---|
7545 | *
|
---|
7546 | * See Intel spec. 29.4.4 "Instruction-Specific Considerations".
|
---|
7547 | */
|
---|
7548 | rcStrict = iemVmxVirtApicAccessUnused(pVCpu, &GCPhysMem);
|
---|
7549 | if ( rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE
|
---|
7550 | && rcStrict != VINF_VMX_MODIFIES_BEHAVIOR)
|
---|
7551 | return rcStrict;
|
---|
7552 | }
|
---|
7553 | #endif
|
---|
7554 |
|
---|
7555 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_MONITOR))
|
---|
7556 | {
|
---|
7557 | Log2(("monitor: Guest intercept -> #VMEXIT\n"));
|
---|
7558 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
7559 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_MONITOR, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
7560 | }
|
---|
7561 |
|
---|
7562 | /*
|
---|
7563 | * Call EM to prepare the monitor/wait.
|
---|
7564 | */
|
---|
7565 | rcStrict = EMMonitorWaitPrepare(pVCpu, pVCpu->cpum.GstCtx.rax, pVCpu->cpum.GstCtx.rcx, pVCpu->cpum.GstCtx.rdx, GCPhysMem);
|
---|
7566 | Assert(rcStrict == VINF_SUCCESS);
|
---|
7567 |
|
---|
7568 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
7569 | return rcStrict;
|
---|
7570 | }
|
---|
7571 |
|
---|
7572 |
|
---|
7573 | /**
|
---|
7574 | * Implements 'MWAIT'.
|
---|
7575 | */
|
---|
7576 | IEM_CIMPL_DEF_0(iemCImpl_mwait)
|
---|
7577 | {
|
---|
7578 | /*
|
---|
7579 | * Permission checks.
|
---|
7580 | */
|
---|
7581 | if (pVCpu->iem.s.uCpl != 0)
|
---|
7582 | {
|
---|
7583 | Log2(("mwait: CPL != 0\n"));
|
---|
7584 | /** @todo MSR[0xC0010015].MonMwaitUserEn if we care. (Remember to check
|
---|
7585 | * EFLAGS.VM then.) */
|
---|
7586 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
7587 | }
|
---|
7588 | if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fMonitorMWait)
|
---|
7589 | {
|
---|
7590 | Log2(("mwait: Not in CPUID\n"));
|
---|
7591 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
7592 | }
|
---|
7593 |
|
---|
7594 | /* Check VMX nested-guest intercept. */
|
---|
7595 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
7596 | && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_MWAIT_EXIT))
|
---|
7597 | IEM_VMX_VMEXIT_MWAIT_RET(pVCpu, EMMonitorIsArmed(pVCpu), cbInstr);
|
---|
7598 |
|
---|
7599 | /*
|
---|
7600 | * Gather the operands and validate them.
|
---|
7601 | */
|
---|
7602 | uint32_t const uEax = pVCpu->cpum.GstCtx.eax;
|
---|
7603 | uint32_t const uEcx = pVCpu->cpum.GstCtx.ecx;
|
---|
7604 | if (uEcx != 0)
|
---|
7605 | {
|
---|
7606 | /* Only supported extension is break on IRQ when IF=0. */
|
---|
7607 | if (uEcx > 1)
|
---|
7608 | {
|
---|
7609 | Log2(("mwait eax=%RX32, ecx=%RX32; ECX > 1 -> #GP(0)\n", uEax, uEcx));
|
---|
7610 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7611 | }
|
---|
7612 | uint32_t fMWaitFeatures = 0;
|
---|
7613 | uint32_t uIgnore = 0;
|
---|
7614 | CPUMGetGuestCpuId(pVCpu, 5, 0, &uIgnore, &uIgnore, &fMWaitFeatures, &uIgnore);
|
---|
7615 | if ( (fMWaitFeatures & (X86_CPUID_MWAIT_ECX_EXT | X86_CPUID_MWAIT_ECX_BREAKIRQIF0))
|
---|
7616 | != (X86_CPUID_MWAIT_ECX_EXT | X86_CPUID_MWAIT_ECX_BREAKIRQIF0))
|
---|
7617 | {
|
---|
7618 | Log2(("mwait eax=%RX32, ecx=%RX32; break-on-IRQ-IF=0 extension not enabled -> #GP(0)\n", uEax, uEcx));
|
---|
7619 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
7620 | }
|
---|
7621 |
|
---|
7622 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
7623 | /*
|
---|
7624 | * If the interrupt-window exiting control is set or a virtual-interrupt is pending
|
---|
7625 | * for delivery; and interrupts are disabled the processor does not enter its
|
---|
7626 | * mwait state but rather passes control to the next instruction.
|
---|
7627 | *
|
---|
7628 | * See Intel spec. 25.3 "Changes to Instruction Behavior In VMX Non-root Operation".
|
---|
7629 | */
|
---|
7630 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
7631 | && !pVCpu->cpum.GstCtx.eflags.Bits.u1IF)
|
---|
7632 | {
|
---|
7633 | if ( IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_INT_WINDOW_EXIT)
|
---|
7634 | || VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_NESTED_GUEST))
|
---|
7635 | {
|
---|
7636 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
7637 | return VINF_SUCCESS;
|
---|
7638 | }
|
---|
7639 | }
|
---|
7640 | #endif
|
---|
7641 | }
|
---|
7642 |
|
---|
7643 | /*
|
---|
7644 | * Check SVM nested-guest mwait intercepts.
|
---|
7645 | */
|
---|
7646 | if ( IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_MWAIT_ARMED)
|
---|
7647 | && EMMonitorIsArmed(pVCpu))
|
---|
7648 | {
|
---|
7649 | Log2(("mwait: Guest intercept (monitor hardware armed) -> #VMEXIT\n"));
|
---|
7650 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
7651 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_MWAIT_ARMED, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
7652 | }
|
---|
7653 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_MWAIT))
|
---|
7654 | {
|
---|
7655 | Log2(("mwait: Guest intercept -> #VMEXIT\n"));
|
---|
7656 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
7657 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_MWAIT, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
7658 | }
|
---|
7659 |
|
---|
7660 | /*
|
---|
7661 | * Call EM to prepare the monitor/wait.
|
---|
7662 | */
|
---|
7663 | VBOXSTRICTRC rcStrict = EMMonitorWaitPerform(pVCpu, uEax, uEcx);
|
---|
7664 |
|
---|
7665 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
7666 | return rcStrict;
|
---|
7667 | }
|
---|
7668 |
|
---|
7669 |
|
---|
7670 | /**
|
---|
7671 | * Implements 'SWAPGS'.
|
---|
7672 | */
|
---|
7673 | IEM_CIMPL_DEF_0(iemCImpl_swapgs)
|
---|
7674 | {
|
---|
7675 | Assert(pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT); /* Caller checks this. */
|
---|
7676 |
|
---|
7677 | /*
|
---|
7678 | * Permission checks.
|
---|
7679 | */
|
---|
7680 | if (pVCpu->iem.s.uCpl != 0)
|
---|
7681 | {
|
---|
7682 | Log2(("swapgs: CPL != 0\n"));
|
---|
7683 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
7684 | }
|
---|
7685 |
|
---|
7686 | /*
|
---|
7687 | * Do the job.
|
---|
7688 | */
|
---|
7689 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_KERNEL_GS_BASE | CPUMCTX_EXTRN_GS);
|
---|
7690 | uint64_t uOtherGsBase = pVCpu->cpum.GstCtx.msrKERNELGSBASE;
|
---|
7691 | pVCpu->cpum.GstCtx.msrKERNELGSBASE = pVCpu->cpum.GstCtx.gs.u64Base;
|
---|
7692 | pVCpu->cpum.GstCtx.gs.u64Base = uOtherGsBase;
|
---|
7693 |
|
---|
7694 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
7695 | return VINF_SUCCESS;
|
---|
7696 | }
|
---|
7697 |
|
---|
7698 |
|
---|
7699 | /**
|
---|
7700 | * Implements 'CPUID'.
|
---|
7701 | */
|
---|
7702 | IEM_CIMPL_DEF_0(iemCImpl_cpuid)
|
---|
7703 | {
|
---|
7704 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
7705 | {
|
---|
7706 | Log2(("cpuid: Guest intercept -> VM-exit\n"));
|
---|
7707 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_CPUID, cbInstr);
|
---|
7708 | }
|
---|
7709 |
|
---|
7710 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_CPUID))
|
---|
7711 | {
|
---|
7712 | Log2(("cpuid: Guest intercept -> #VMEXIT\n"));
|
---|
7713 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
7714 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_CPUID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
7715 | }
|
---|
7716 |
|
---|
7717 | CPUMGetGuestCpuId(pVCpu, pVCpu->cpum.GstCtx.eax, pVCpu->cpum.GstCtx.ecx,
|
---|
7718 | &pVCpu->cpum.GstCtx.eax, &pVCpu->cpum.GstCtx.ebx, &pVCpu->cpum.GstCtx.ecx, &pVCpu->cpum.GstCtx.edx);
|
---|
7719 | pVCpu->cpum.GstCtx.rax &= UINT32_C(0xffffffff);
|
---|
7720 | pVCpu->cpum.GstCtx.rbx &= UINT32_C(0xffffffff);
|
---|
7721 | pVCpu->cpum.GstCtx.rcx &= UINT32_C(0xffffffff);
|
---|
7722 | pVCpu->cpum.GstCtx.rdx &= UINT32_C(0xffffffff);
|
---|
7723 | pVCpu->cpum.GstCtx.fExtrn &= ~(CPUMCTX_EXTRN_RAX | CPUMCTX_EXTRN_RCX | CPUMCTX_EXTRN_RDX | CPUMCTX_EXTRN_RBX);
|
---|
7724 |
|
---|
7725 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
7726 | pVCpu->iem.s.cPotentialExits++;
|
---|
7727 | return VINF_SUCCESS;
|
---|
7728 | }
|
---|
7729 |
|
---|
7730 |
|
---|
7731 | /**
|
---|
7732 | * Implements 'AAD'.
|
---|
7733 | *
|
---|
7734 | * @param bImm The immediate operand.
|
---|
7735 | */
|
---|
7736 | IEM_CIMPL_DEF_1(iemCImpl_aad, uint8_t, bImm)
|
---|
7737 | {
|
---|
7738 | uint16_t const ax = pVCpu->cpum.GstCtx.ax;
|
---|
7739 | uint8_t const al = (uint8_t)ax + (uint8_t)(ax >> 8) * bImm;
|
---|
7740 | pVCpu->cpum.GstCtx.ax = al;
|
---|
7741 | iemHlpUpdateArithEFlagsU8(pVCpu, al,
|
---|
7742 | X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF,
|
---|
7743 | X86_EFL_OF | X86_EFL_AF | X86_EFL_CF);
|
---|
7744 |
|
---|
7745 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
7746 | return VINF_SUCCESS;
|
---|
7747 | }
|
---|
7748 |
|
---|
7749 |
|
---|
7750 | /**
|
---|
7751 | * Implements 'AAM'.
|
---|
7752 | *
|
---|
7753 | * @param bImm The immediate operand. Cannot be 0.
|
---|
7754 | */
|
---|
7755 | IEM_CIMPL_DEF_1(iemCImpl_aam, uint8_t, bImm)
|
---|
7756 | {
|
---|
7757 | Assert(bImm != 0); /* #DE on 0 is handled in the decoder. */
|
---|
7758 |
|
---|
7759 | uint16_t const ax = pVCpu->cpum.GstCtx.ax;
|
---|
7760 | uint8_t const al = (uint8_t)ax % bImm;
|
---|
7761 | uint8_t const ah = (uint8_t)ax / bImm;
|
---|
7762 | pVCpu->cpum.GstCtx.ax = (ah << 8) + al;
|
---|
7763 | iemHlpUpdateArithEFlagsU8(pVCpu, al,
|
---|
7764 | X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF,
|
---|
7765 | X86_EFL_OF | X86_EFL_AF | X86_EFL_CF);
|
---|
7766 |
|
---|
7767 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
7768 | return VINF_SUCCESS;
|
---|
7769 | }
|
---|
7770 |
|
---|
7771 |
|
---|
7772 | /**
|
---|
7773 | * Implements 'DAA'.
|
---|
7774 | */
|
---|
7775 | IEM_CIMPL_DEF_0(iemCImpl_daa)
|
---|
7776 | {
|
---|
7777 | uint8_t const al = pVCpu->cpum.GstCtx.al;
|
---|
7778 | bool const fCarry = pVCpu->cpum.GstCtx.eflags.Bits.u1CF;
|
---|
7779 |
|
---|
7780 | if ( pVCpu->cpum.GstCtx.eflags.Bits.u1AF
|
---|
7781 | || (al & 0xf) >= 10)
|
---|
7782 | {
|
---|
7783 | pVCpu->cpum.GstCtx.al = al + 6;
|
---|
7784 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 1;
|
---|
7785 | }
|
---|
7786 | else
|
---|
7787 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 0;
|
---|
7788 |
|
---|
7789 | if (al >= 0x9a || fCarry)
|
---|
7790 | {
|
---|
7791 | pVCpu->cpum.GstCtx.al += 0x60;
|
---|
7792 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
|
---|
7793 | }
|
---|
7794 | else
|
---|
7795 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 0;
|
---|
7796 |
|
---|
7797 | iemHlpUpdateArithEFlagsU8(pVCpu, pVCpu->cpum.GstCtx.al, X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF, X86_EFL_OF);
|
---|
7798 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
7799 | return VINF_SUCCESS;
|
---|
7800 | }
|
---|
7801 |
|
---|
7802 |
|
---|
7803 | /**
|
---|
7804 | * Implements 'DAS'.
|
---|
7805 | */
|
---|
7806 | IEM_CIMPL_DEF_0(iemCImpl_das)
|
---|
7807 | {
|
---|
7808 | uint8_t const uInputAL = pVCpu->cpum.GstCtx.al;
|
---|
7809 | bool const fCarry = pVCpu->cpum.GstCtx.eflags.Bits.u1CF;
|
---|
7810 |
|
---|
7811 | if ( pVCpu->cpum.GstCtx.eflags.Bits.u1AF
|
---|
7812 | || (uInputAL & 0xf) >= 10)
|
---|
7813 | {
|
---|
7814 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 1;
|
---|
7815 | if (uInputAL < 6)
|
---|
7816 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
|
---|
7817 | pVCpu->cpum.GstCtx.al = uInputAL - 6;
|
---|
7818 | }
|
---|
7819 | else
|
---|
7820 | {
|
---|
7821 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 0;
|
---|
7822 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 0;
|
---|
7823 | }
|
---|
7824 |
|
---|
7825 | if (uInputAL >= 0x9a || fCarry)
|
---|
7826 | {
|
---|
7827 | pVCpu->cpum.GstCtx.al -= 0x60;
|
---|
7828 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
|
---|
7829 | }
|
---|
7830 |
|
---|
7831 | iemHlpUpdateArithEFlagsU8(pVCpu, pVCpu->cpum.GstCtx.al, X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF, X86_EFL_OF);
|
---|
7832 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
7833 | return VINF_SUCCESS;
|
---|
7834 | }
|
---|
7835 |
|
---|
7836 |
|
---|
7837 | /**
|
---|
7838 | * Implements 'AAA'.
|
---|
7839 | */
|
---|
7840 | IEM_CIMPL_DEF_0(iemCImpl_aaa)
|
---|
7841 | {
|
---|
7842 | if (IEM_IS_GUEST_CPU_AMD(pVCpu))
|
---|
7843 | {
|
---|
7844 | if ( pVCpu->cpum.GstCtx.eflags.Bits.u1AF
|
---|
7845 | || (pVCpu->cpum.GstCtx.ax & 0xf) >= 10)
|
---|
7846 | {
|
---|
7847 | iemAImpl_add_u16(&pVCpu->cpum.GstCtx.ax, 0x106, &pVCpu->cpum.GstCtx.eflags.u32);
|
---|
7848 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 1;
|
---|
7849 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
|
---|
7850 | }
|
---|
7851 | else
|
---|
7852 | {
|
---|
7853 | iemHlpUpdateArithEFlagsU16(pVCpu, pVCpu->cpum.GstCtx.ax, X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF, X86_EFL_OF);
|
---|
7854 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 0;
|
---|
7855 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 0;
|
---|
7856 | }
|
---|
7857 | pVCpu->cpum.GstCtx.ax &= UINT16_C(0xff0f);
|
---|
7858 | }
|
---|
7859 | else
|
---|
7860 | {
|
---|
7861 | if ( pVCpu->cpum.GstCtx.eflags.Bits.u1AF
|
---|
7862 | || (pVCpu->cpum.GstCtx.ax & 0xf) >= 10)
|
---|
7863 | {
|
---|
7864 | pVCpu->cpum.GstCtx.ax += UINT16_C(0x106);
|
---|
7865 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 1;
|
---|
7866 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
|
---|
7867 | }
|
---|
7868 | else
|
---|
7869 | {
|
---|
7870 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 0;
|
---|
7871 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 0;
|
---|
7872 | }
|
---|
7873 | pVCpu->cpum.GstCtx.ax &= UINT16_C(0xff0f);
|
---|
7874 | iemHlpUpdateArithEFlagsU8(pVCpu, pVCpu->cpum.GstCtx.al, X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF, X86_EFL_OF);
|
---|
7875 | }
|
---|
7876 |
|
---|
7877 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
7878 | return VINF_SUCCESS;
|
---|
7879 | }
|
---|
7880 |
|
---|
7881 |
|
---|
7882 | /**
|
---|
7883 | * Implements 'AAS'.
|
---|
7884 | */
|
---|
7885 | IEM_CIMPL_DEF_0(iemCImpl_aas)
|
---|
7886 | {
|
---|
7887 | if (IEM_IS_GUEST_CPU_AMD(pVCpu))
|
---|
7888 | {
|
---|
7889 | if ( pVCpu->cpum.GstCtx.eflags.Bits.u1AF
|
---|
7890 | || (pVCpu->cpum.GstCtx.ax & 0xf) >= 10)
|
---|
7891 | {
|
---|
7892 | iemAImpl_sub_u16(&pVCpu->cpum.GstCtx.ax, 0x106, &pVCpu->cpum.GstCtx.eflags.u32);
|
---|
7893 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 1;
|
---|
7894 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
|
---|
7895 | }
|
---|
7896 | else
|
---|
7897 | {
|
---|
7898 | iemHlpUpdateArithEFlagsU16(pVCpu, pVCpu->cpum.GstCtx.ax, X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF, X86_EFL_OF);
|
---|
7899 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 0;
|
---|
7900 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 0;
|
---|
7901 | }
|
---|
7902 | pVCpu->cpum.GstCtx.ax &= UINT16_C(0xff0f);
|
---|
7903 | }
|
---|
7904 | else
|
---|
7905 | {
|
---|
7906 | if ( pVCpu->cpum.GstCtx.eflags.Bits.u1AF
|
---|
7907 | || (pVCpu->cpum.GstCtx.ax & 0xf) >= 10)
|
---|
7908 | {
|
---|
7909 | pVCpu->cpum.GstCtx.ax -= UINT16_C(0x106);
|
---|
7910 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 1;
|
---|
7911 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
|
---|
7912 | }
|
---|
7913 | else
|
---|
7914 | {
|
---|
7915 | pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 0;
|
---|
7916 | pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 0;
|
---|
7917 | }
|
---|
7918 | pVCpu->cpum.GstCtx.ax &= UINT16_C(0xff0f);
|
---|
7919 | iemHlpUpdateArithEFlagsU8(pVCpu, pVCpu->cpum.GstCtx.al, X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF, X86_EFL_OF);
|
---|
7920 | }
|
---|
7921 |
|
---|
7922 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
7923 | return VINF_SUCCESS;
|
---|
7924 | }
|
---|
7925 |
|
---|
7926 |
|
---|
7927 | /**
|
---|
7928 | * Implements the 16-bit version of 'BOUND'.
|
---|
7929 | *
|
---|
7930 | * @note We have separate 16-bit and 32-bit variants of this function due to
|
---|
7931 | * the decoder using unsigned parameters, whereas we want signed one to
|
---|
7932 | * do the job. This is significant for a recompiler.
|
---|
7933 | */
|
---|
7934 | IEM_CIMPL_DEF_3(iemCImpl_bound_16, int16_t, idxArray, int16_t, idxLowerBound, int16_t, idxUpperBound)
|
---|
7935 | {
|
---|
7936 | /*
|
---|
7937 | * Check if the index is inside the bounds, otherwise raise #BR.
|
---|
7938 | */
|
---|
7939 | if ( idxArray >= idxLowerBound
|
---|
7940 | && idxArray <= idxUpperBound)
|
---|
7941 | {
|
---|
7942 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
7943 | return VINF_SUCCESS;
|
---|
7944 | }
|
---|
7945 |
|
---|
7946 | return iemRaiseBoundRangeExceeded(pVCpu);
|
---|
7947 | }
|
---|
7948 |
|
---|
7949 |
|
---|
7950 | /**
|
---|
7951 | * Implements the 32-bit version of 'BOUND'.
|
---|
7952 | */
|
---|
7953 | IEM_CIMPL_DEF_3(iemCImpl_bound_32, int32_t, idxArray, int32_t, idxLowerBound, int32_t, idxUpperBound)
|
---|
7954 | {
|
---|
7955 | /*
|
---|
7956 | * Check if the index is inside the bounds, otherwise raise #BR.
|
---|
7957 | */
|
---|
7958 | if ( idxArray >= idxLowerBound
|
---|
7959 | && idxArray <= idxUpperBound)
|
---|
7960 | {
|
---|
7961 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
7962 | return VINF_SUCCESS;
|
---|
7963 | }
|
---|
7964 |
|
---|
7965 | return iemRaiseBoundRangeExceeded(pVCpu);
|
---|
7966 | }
|
---|
7967 |
|
---|
7968 |
|
---|
7969 |
|
---|
7970 | /*
|
---|
7971 | * Instantiate the various string operation combinations.
|
---|
7972 | */
|
---|
7973 | #define OP_SIZE 8
|
---|
7974 | #define ADDR_SIZE 16
|
---|
7975 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
7976 | #define OP_SIZE 8
|
---|
7977 | #define ADDR_SIZE 32
|
---|
7978 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
7979 | #define OP_SIZE 8
|
---|
7980 | #define ADDR_SIZE 64
|
---|
7981 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
7982 |
|
---|
7983 | #define OP_SIZE 16
|
---|
7984 | #define ADDR_SIZE 16
|
---|
7985 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
7986 | #define OP_SIZE 16
|
---|
7987 | #define ADDR_SIZE 32
|
---|
7988 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
7989 | #define OP_SIZE 16
|
---|
7990 | #define ADDR_SIZE 64
|
---|
7991 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
7992 |
|
---|
7993 | #define OP_SIZE 32
|
---|
7994 | #define ADDR_SIZE 16
|
---|
7995 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
7996 | #define OP_SIZE 32
|
---|
7997 | #define ADDR_SIZE 32
|
---|
7998 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
7999 | #define OP_SIZE 32
|
---|
8000 | #define ADDR_SIZE 64
|
---|
8001 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
8002 |
|
---|
8003 | #define OP_SIZE 64
|
---|
8004 | #define ADDR_SIZE 32
|
---|
8005 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
8006 | #define OP_SIZE 64
|
---|
8007 | #define ADDR_SIZE 64
|
---|
8008 | #include "IEMAllCImplStrInstr.cpp.h"
|
---|
8009 |
|
---|
8010 |
|
---|
8011 | /**
|
---|
8012 | * Implements 'XGETBV'.
|
---|
8013 | */
|
---|
8014 | IEM_CIMPL_DEF_0(iemCImpl_xgetbv)
|
---|
8015 | {
|
---|
8016 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR4);
|
---|
8017 | if (pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSXSAVE)
|
---|
8018 | {
|
---|
8019 | uint32_t uEcx = pVCpu->cpum.GstCtx.ecx;
|
---|
8020 | switch (uEcx)
|
---|
8021 | {
|
---|
8022 | case 0:
|
---|
8023 | break;
|
---|
8024 |
|
---|
8025 | case 1: /** @todo Implement XCR1 support. */
|
---|
8026 | default:
|
---|
8027 | Log(("xgetbv ecx=%RX32 -> #GP(0)\n", uEcx));
|
---|
8028 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
8029 |
|
---|
8030 | }
|
---|
8031 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_XCRx);
|
---|
8032 | pVCpu->cpum.GstCtx.rax = RT_LO_U32(pVCpu->cpum.GstCtx.aXcr[uEcx]);
|
---|
8033 | pVCpu->cpum.GstCtx.rdx = RT_HI_U32(pVCpu->cpum.GstCtx.aXcr[uEcx]);
|
---|
8034 |
|
---|
8035 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
8036 | return VINF_SUCCESS;
|
---|
8037 | }
|
---|
8038 | Log(("xgetbv CR4.OSXSAVE=0 -> UD\n"));
|
---|
8039 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
8040 | }
|
---|
8041 |
|
---|
8042 |
|
---|
8043 | /**
|
---|
8044 | * Implements 'XSETBV'.
|
---|
8045 | */
|
---|
8046 | IEM_CIMPL_DEF_0(iemCImpl_xsetbv)
|
---|
8047 | {
|
---|
8048 | if (pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSXSAVE)
|
---|
8049 | {
|
---|
8050 | if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_XSETBV))
|
---|
8051 | {
|
---|
8052 | Log2(("xsetbv: Guest intercept -> #VMEXIT\n"));
|
---|
8053 | IEM_SVM_UPDATE_NRIP(pVCpu);
|
---|
8054 | IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_XSETBV, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
|
---|
8055 | }
|
---|
8056 |
|
---|
8057 | if (pVCpu->iem.s.uCpl == 0)
|
---|
8058 | {
|
---|
8059 | IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_XCRx);
|
---|
8060 |
|
---|
8061 | if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
|
---|
8062 | IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_XSETBV, cbInstr);
|
---|
8063 |
|
---|
8064 | uint32_t uEcx = pVCpu->cpum.GstCtx.ecx;
|
---|
8065 | uint64_t uNewValue = RT_MAKE_U64(pVCpu->cpum.GstCtx.eax, pVCpu->cpum.GstCtx.edx);
|
---|
8066 | switch (uEcx)
|
---|
8067 | {
|
---|
8068 | case 0:
|
---|
8069 | {
|
---|
8070 | int rc = CPUMSetGuestXcr0(pVCpu, uNewValue);
|
---|
8071 | if (rc == VINF_SUCCESS)
|
---|
8072 | break;
|
---|
8073 | Assert(rc == VERR_CPUM_RAISE_GP_0);
|
---|
8074 | Log(("xsetbv ecx=%RX32 (newvalue=%RX64) -> #GP(0)\n", uEcx, uNewValue));
|
---|
8075 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
8076 | }
|
---|
8077 |
|
---|
8078 | case 1: /** @todo Implement XCR1 support. */
|
---|
8079 | default:
|
---|
8080 | Log(("xsetbv ecx=%RX32 (newvalue=%RX64) -> #GP(0)\n", uEcx, uNewValue));
|
---|
8081 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
8082 |
|
---|
8083 | }
|
---|
8084 |
|
---|
8085 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
8086 | return VINF_SUCCESS;
|
---|
8087 | }
|
---|
8088 |
|
---|
8089 | Log(("xsetbv cpl=%u -> GP(0)\n", pVCpu->iem.s.uCpl));
|
---|
8090 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
8091 | }
|
---|
8092 | Log(("xsetbv CR4.OSXSAVE=0 -> UD\n"));
|
---|
8093 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
8094 | }
|
---|
8095 |
|
---|
8096 | #ifdef IN_RING3
|
---|
8097 |
|
---|
8098 | /** Argument package for iemCImpl_cmpxchg16b_fallback_rendezvous_callback. */
|
---|
8099 | struct IEMCIMPLCX16ARGS
|
---|
8100 | {
|
---|
8101 | PRTUINT128U pu128Dst;
|
---|
8102 | PRTUINT128U pu128RaxRdx;
|
---|
8103 | PRTUINT128U pu128RbxRcx;
|
---|
8104 | uint32_t *pEFlags;
|
---|
8105 | # ifdef VBOX_STRICT
|
---|
8106 | uint32_t cCalls;
|
---|
8107 | # endif
|
---|
8108 | };
|
---|
8109 |
|
---|
8110 | /**
|
---|
8111 | * @callback_method_impl{FNVMMEMTRENDEZVOUS,
|
---|
8112 | * Worker for iemCImpl_cmpxchg16b_fallback_rendezvous}
|
---|
8113 | */
|
---|
8114 | static DECLCALLBACK(VBOXSTRICTRC) iemCImpl_cmpxchg16b_fallback_rendezvous_callback(PVM pVM, PVMCPUCC pVCpu, void *pvUser)
|
---|
8115 | {
|
---|
8116 | RT_NOREF(pVM, pVCpu);
|
---|
8117 | struct IEMCIMPLCX16ARGS *pArgs = (struct IEMCIMPLCX16ARGS *)pvUser;
|
---|
8118 | # ifdef VBOX_STRICT
|
---|
8119 | Assert(pArgs->cCalls == 0);
|
---|
8120 | pArgs->cCalls++;
|
---|
8121 | # endif
|
---|
8122 |
|
---|
8123 | iemAImpl_cmpxchg16b_fallback(pArgs->pu128Dst, pArgs->pu128RaxRdx, pArgs->pu128RbxRcx, pArgs->pEFlags);
|
---|
8124 | return VINF_SUCCESS;
|
---|
8125 | }
|
---|
8126 |
|
---|
8127 | #endif /* IN_RING3 */
|
---|
8128 |
|
---|
8129 | /**
|
---|
8130 | * Implements 'CMPXCHG16B' fallback using rendezvous.
|
---|
8131 | */
|
---|
8132 | IEM_CIMPL_DEF_4(iemCImpl_cmpxchg16b_fallback_rendezvous, PRTUINT128U, pu128Dst, PRTUINT128U, pu128RaxRdx,
|
---|
8133 | PRTUINT128U, pu128RbxRcx, uint32_t *, pEFlags)
|
---|
8134 | {
|
---|
8135 | #ifdef IN_RING3
|
---|
8136 | struct IEMCIMPLCX16ARGS Args;
|
---|
8137 | Args.pu128Dst = pu128Dst;
|
---|
8138 | Args.pu128RaxRdx = pu128RaxRdx;
|
---|
8139 | Args.pu128RbxRcx = pu128RbxRcx;
|
---|
8140 | Args.pEFlags = pEFlags;
|
---|
8141 | # ifdef VBOX_STRICT
|
---|
8142 | Args.cCalls = 0;
|
---|
8143 | # endif
|
---|
8144 | VBOXSTRICTRC rcStrict = VMMR3EmtRendezvous(pVCpu->CTX_SUFF(pVM), VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE,
|
---|
8145 | iemCImpl_cmpxchg16b_fallback_rendezvous_callback, &Args);
|
---|
8146 | Assert(Args.cCalls == 1);
|
---|
8147 | if (rcStrict == VINF_SUCCESS)
|
---|
8148 | {
|
---|
8149 | /* Duplicated tail code. */
|
---|
8150 | rcStrict = iemMemCommitAndUnmap(pVCpu, pu128Dst, IEM_ACCESS_DATA_RW);
|
---|
8151 | if (rcStrict == VINF_SUCCESS)
|
---|
8152 | {
|
---|
8153 | pVCpu->cpum.GstCtx.eflags.u = *pEFlags; /* IEM_MC_COMMIT_EFLAGS */
|
---|
8154 | if (!(*pEFlags & X86_EFL_ZF))
|
---|
8155 | {
|
---|
8156 | pVCpu->cpum.GstCtx.rax = pu128RaxRdx->s.Lo;
|
---|
8157 | pVCpu->cpum.GstCtx.rdx = pu128RaxRdx->s.Hi;
|
---|
8158 | }
|
---|
8159 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
8160 | }
|
---|
8161 | }
|
---|
8162 | return rcStrict;
|
---|
8163 | #else
|
---|
8164 | RT_NOREF(pVCpu, cbInstr, pu128Dst, pu128RaxRdx, pu128RbxRcx, pEFlags);
|
---|
8165 | return VERR_IEM_ASPECT_NOT_IMPLEMENTED; /* This should get us to ring-3 for now. Should perhaps be replaced later. */
|
---|
8166 | #endif
|
---|
8167 | }
|
---|
8168 |
|
---|
8169 |
|
---|
8170 | /**
|
---|
8171 | * Implements 'CLFLUSH' and 'CLFLUSHOPT'.
|
---|
8172 | *
|
---|
8173 | * This is implemented in C because it triggers a load like behaviour without
|
---|
8174 | * actually reading anything. Since that's not so common, it's implemented
|
---|
8175 | * here.
|
---|
8176 | *
|
---|
8177 | * @param iEffSeg The effective segment.
|
---|
8178 | * @param GCPtrEff The address of the image.
|
---|
8179 | */
|
---|
8180 | IEM_CIMPL_DEF_2(iemCImpl_clflush_clflushopt, uint8_t, iEffSeg, RTGCPTR, GCPtrEff)
|
---|
8181 | {
|
---|
8182 | /*
|
---|
8183 | * Pretend to do a load w/o reading (see also iemCImpl_monitor and iemMemMap).
|
---|
8184 | */
|
---|
8185 | VBOXSTRICTRC rcStrict = iemMemApplySegment(pVCpu, IEM_ACCESS_TYPE_READ | IEM_ACCESS_WHAT_DATA, iEffSeg, 1, &GCPtrEff);
|
---|
8186 | if (rcStrict == VINF_SUCCESS)
|
---|
8187 | {
|
---|
8188 | RTGCPHYS GCPhysMem;
|
---|
8189 | rcStrict = iemMemPageTranslateAndCheckAccess(pVCpu, GCPtrEff, IEM_ACCESS_TYPE_READ | IEM_ACCESS_WHAT_DATA, &GCPhysMem);
|
---|
8190 | if (rcStrict == VINF_SUCCESS)
|
---|
8191 | {
|
---|
8192 | #ifdef VBOX_WITH_NESTED_HWVIRT_VMX
|
---|
8193 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
8194 | && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_VIRT_APIC_ACCESS))
|
---|
8195 | {
|
---|
8196 | /*
|
---|
8197 | * CLFLUSH/CLFLUSHOPT does not access the memory, but flushes the cache-line
|
---|
8198 | * that contains the address. However, if the address falls in the APIC-access
|
---|
8199 | * page, the address flushed must instead be the corresponding address in the
|
---|
8200 | * virtual-APIC page.
|
---|
8201 | *
|
---|
8202 | * See Intel spec. 29.4.4 "Instruction-Specific Considerations".
|
---|
8203 | */
|
---|
8204 | rcStrict = iemVmxVirtApicAccessUnused(pVCpu, &GCPhysMem);
|
---|
8205 | if ( rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE
|
---|
8206 | && rcStrict != VINF_VMX_MODIFIES_BEHAVIOR)
|
---|
8207 | return rcStrict;
|
---|
8208 | }
|
---|
8209 | #endif
|
---|
8210 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
8211 | return VINF_SUCCESS;
|
---|
8212 | }
|
---|
8213 | }
|
---|
8214 |
|
---|
8215 | return rcStrict;
|
---|
8216 | }
|
---|
8217 |
|
---|
8218 |
|
---|
8219 | /**
|
---|
8220 | * Implements 'FINIT' and 'FNINIT'.
|
---|
8221 | *
|
---|
8222 | * @param fCheckXcpts Whether to check for umasked pending exceptions or
|
---|
8223 | * not.
|
---|
8224 | */
|
---|
8225 | IEM_CIMPL_DEF_1(iemCImpl_finit, bool, fCheckXcpts)
|
---|
8226 | {
|
---|
8227 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0);
|
---|
8228 | if (pVCpu->cpum.GstCtx.cr0 & (X86_CR0_EM | X86_CR0_TS))
|
---|
8229 | return iemRaiseDeviceNotAvailable(pVCpu);
|
---|
8230 |
|
---|
8231 | iemFpuActualizeStateForChange(pVCpu);
|
---|
8232 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_X87);
|
---|
8233 |
|
---|
8234 | NOREF(fCheckXcpts); /** @todo trigger pending exceptions:
|
---|
8235 | if (fCheckXcpts && TODO )
|
---|
8236 | return iemRaiseMathFault(pVCpu);
|
---|
8237 | */
|
---|
8238 |
|
---|
8239 | PX86XSAVEAREA pXState = &pVCpu->cpum.GstCtx.XState;
|
---|
8240 | pXState->x87.FCW = 0x37f;
|
---|
8241 | pXState->x87.FSW = 0;
|
---|
8242 | pXState->x87.FTW = 0x00; /* 0 - empty. */
|
---|
8243 | pXState->x87.FPUDP = 0;
|
---|
8244 | pXState->x87.DS = 0; //??
|
---|
8245 | pXState->x87.Rsrvd2= 0;
|
---|
8246 | pXState->x87.FPUIP = 0;
|
---|
8247 | pXState->x87.CS = 0; //??
|
---|
8248 | pXState->x87.Rsrvd1= 0;
|
---|
8249 | pXState->x87.FOP = 0;
|
---|
8250 |
|
---|
8251 | iemHlpUsedFpu(pVCpu);
|
---|
8252 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
8253 | return VINF_SUCCESS;
|
---|
8254 | }
|
---|
8255 |
|
---|
8256 |
|
---|
8257 | /**
|
---|
8258 | * Implements 'FXSAVE'.
|
---|
8259 | *
|
---|
8260 | * @param iEffSeg The effective segment.
|
---|
8261 | * @param GCPtrEff The address of the image.
|
---|
8262 | * @param enmEffOpSize The operand size (only REX.W really matters).
|
---|
8263 | */
|
---|
8264 | IEM_CIMPL_DEF_3(iemCImpl_fxsave, uint8_t, iEffSeg, RTGCPTR, GCPtrEff, IEMMODE, enmEffOpSize)
|
---|
8265 | {
|
---|
8266 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX);
|
---|
8267 |
|
---|
8268 | /*
|
---|
8269 | * Raise exceptions.
|
---|
8270 | */
|
---|
8271 | if (pVCpu->cpum.GstCtx.cr0 & X86_CR0_EM)
|
---|
8272 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
8273 | if (pVCpu->cpum.GstCtx.cr0 & (X86_CR0_TS | X86_CR0_EM))
|
---|
8274 | return iemRaiseDeviceNotAvailable(pVCpu);
|
---|
8275 | if (GCPtrEff & 15)
|
---|
8276 | {
|
---|
8277 | /** @todo CPU/VM detection possible! \#AC might not be signal for
|
---|
8278 | * all/any misalignment sizes, intel says its an implementation detail. */
|
---|
8279 | if ( (pVCpu->cpum.GstCtx.cr0 & X86_CR0_AM)
|
---|
8280 | && pVCpu->cpum.GstCtx.eflags.Bits.u1AC
|
---|
8281 | && pVCpu->iem.s.uCpl == 3)
|
---|
8282 | return iemRaiseAlignmentCheckException(pVCpu);
|
---|
8283 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
8284 | }
|
---|
8285 |
|
---|
8286 | /*
|
---|
8287 | * Access the memory.
|
---|
8288 | */
|
---|
8289 | void *pvMem512;
|
---|
8290 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, &pvMem512, 512, iEffSeg, GCPtrEff, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
|
---|
8291 | if (rcStrict != VINF_SUCCESS)
|
---|
8292 | return rcStrict;
|
---|
8293 | PX86FXSTATE pDst = (PX86FXSTATE)pvMem512;
|
---|
8294 | PCX86FXSTATE pSrc = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
8295 |
|
---|
8296 | /*
|
---|
8297 | * Store the registers.
|
---|
8298 | */
|
---|
8299 | /** @todo CPU/VM detection possible! If CR4.OSFXSR=0 MXCSR it's
|
---|
8300 | * implementation specific whether MXCSR and XMM0-XMM7 are saved. */
|
---|
8301 |
|
---|
8302 | /* common for all formats */
|
---|
8303 | pDst->FCW = pSrc->FCW;
|
---|
8304 | pDst->FSW = pSrc->FSW;
|
---|
8305 | pDst->FTW = pSrc->FTW & UINT16_C(0xff);
|
---|
8306 | pDst->FOP = pSrc->FOP;
|
---|
8307 | pDst->MXCSR = pSrc->MXCSR;
|
---|
8308 | pDst->MXCSR_MASK = CPUMGetGuestMxCsrMask(pVCpu->CTX_SUFF(pVM));
|
---|
8309 | for (uint32_t i = 0; i < RT_ELEMENTS(pDst->aRegs); i++)
|
---|
8310 | {
|
---|
8311 | /** @todo Testcase: What actually happens to the 6 reserved bytes? I'm clearing
|
---|
8312 | * them for now... */
|
---|
8313 | pDst->aRegs[i].au32[0] = pSrc->aRegs[i].au32[0];
|
---|
8314 | pDst->aRegs[i].au32[1] = pSrc->aRegs[i].au32[1];
|
---|
8315 | pDst->aRegs[i].au32[2] = pSrc->aRegs[i].au32[2] & UINT32_C(0xffff);
|
---|
8316 | pDst->aRegs[i].au32[3] = 0;
|
---|
8317 | }
|
---|
8318 |
|
---|
8319 | /* FPU IP, CS, DP and DS. */
|
---|
8320 | pDst->FPUIP = pSrc->FPUIP;
|
---|
8321 | pDst->CS = pSrc->CS;
|
---|
8322 | pDst->FPUDP = pSrc->FPUDP;
|
---|
8323 | pDst->DS = pSrc->DS;
|
---|
8324 | if (enmEffOpSize == IEMMODE_64BIT)
|
---|
8325 | {
|
---|
8326 | /* Save upper 16-bits of FPUIP (IP:CS:Rsvd1) and FPUDP (DP:DS:Rsvd2). */
|
---|
8327 | pDst->Rsrvd1 = pSrc->Rsrvd1;
|
---|
8328 | pDst->Rsrvd2 = pSrc->Rsrvd2;
|
---|
8329 | pDst->au32RsrvdForSoftware[0] = 0;
|
---|
8330 | }
|
---|
8331 | else
|
---|
8332 | {
|
---|
8333 | pDst->Rsrvd1 = 0;
|
---|
8334 | pDst->Rsrvd2 = 0;
|
---|
8335 | pDst->au32RsrvdForSoftware[0] = X86_FXSTATE_RSVD_32BIT_MAGIC;
|
---|
8336 | }
|
---|
8337 |
|
---|
8338 | /* XMM registers. */
|
---|
8339 | if ( !(pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_FFXSR)
|
---|
8340 | || pVCpu->iem.s.enmCpuMode != IEMMODE_64BIT
|
---|
8341 | || pVCpu->iem.s.uCpl != 0)
|
---|
8342 | {
|
---|
8343 | uint32_t cXmmRegs = enmEffOpSize == IEMMODE_64BIT ? 16 : 8;
|
---|
8344 | for (uint32_t i = 0; i < cXmmRegs; i++)
|
---|
8345 | pDst->aXMM[i] = pSrc->aXMM[i];
|
---|
8346 | /** @todo Testcase: What happens to the reserved XMM registers? Untouched,
|
---|
8347 | * right? */
|
---|
8348 | }
|
---|
8349 |
|
---|
8350 | /*
|
---|
8351 | * Commit the memory.
|
---|
8352 | */
|
---|
8353 | rcStrict = iemMemCommitAndUnmap(pVCpu, pvMem512, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
|
---|
8354 | if (rcStrict != VINF_SUCCESS)
|
---|
8355 | return rcStrict;
|
---|
8356 |
|
---|
8357 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
8358 | return VINF_SUCCESS;
|
---|
8359 | }
|
---|
8360 |
|
---|
8361 |
|
---|
8362 | /**
|
---|
8363 | * Implements 'FXRSTOR'.
|
---|
8364 | *
|
---|
8365 | * @param GCPtrEff The address of the image.
|
---|
8366 | * @param enmEffOpSize The operand size (only REX.W really matters).
|
---|
8367 | */
|
---|
8368 | IEM_CIMPL_DEF_3(iemCImpl_fxrstor, uint8_t, iEffSeg, RTGCPTR, GCPtrEff, IEMMODE, enmEffOpSize)
|
---|
8369 | {
|
---|
8370 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX);
|
---|
8371 |
|
---|
8372 | /*
|
---|
8373 | * Raise exceptions.
|
---|
8374 | */
|
---|
8375 | if (pVCpu->cpum.GstCtx.cr0 & X86_CR0_EM)
|
---|
8376 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
8377 | if (pVCpu->cpum.GstCtx.cr0 & (X86_CR0_TS | X86_CR0_EM))
|
---|
8378 | return iemRaiseDeviceNotAvailable(pVCpu);
|
---|
8379 | if (GCPtrEff & 15)
|
---|
8380 | {
|
---|
8381 | /** @todo CPU/VM detection possible! \#AC might not be signal for
|
---|
8382 | * all/any misalignment sizes, intel says its an implementation detail. */
|
---|
8383 | if ( (pVCpu->cpum.GstCtx.cr0 & X86_CR0_AM)
|
---|
8384 | && pVCpu->cpum.GstCtx.eflags.Bits.u1AC
|
---|
8385 | && pVCpu->iem.s.uCpl == 3)
|
---|
8386 | return iemRaiseAlignmentCheckException(pVCpu);
|
---|
8387 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
8388 | }
|
---|
8389 |
|
---|
8390 | /*
|
---|
8391 | * Access the memory.
|
---|
8392 | */
|
---|
8393 | void *pvMem512;
|
---|
8394 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, &pvMem512, 512, iEffSeg, GCPtrEff, IEM_ACCESS_DATA_R);
|
---|
8395 | if (rcStrict != VINF_SUCCESS)
|
---|
8396 | return rcStrict;
|
---|
8397 | PCX86FXSTATE pSrc = (PCX86FXSTATE)pvMem512;
|
---|
8398 | PX86FXSTATE pDst = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
8399 |
|
---|
8400 | /*
|
---|
8401 | * Check the state for stuff which will #GP(0).
|
---|
8402 | */
|
---|
8403 | uint32_t const fMXCSR = pSrc->MXCSR;
|
---|
8404 | uint32_t const fMXCSR_MASK = CPUMGetGuestMxCsrMask(pVCpu->CTX_SUFF(pVM));
|
---|
8405 | if (fMXCSR & ~fMXCSR_MASK)
|
---|
8406 | {
|
---|
8407 | Log(("fxrstor: MXCSR=%#x (MXCSR_MASK=%#x) -> #GP(0)\n", fMXCSR, fMXCSR_MASK));
|
---|
8408 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
8409 | }
|
---|
8410 |
|
---|
8411 | /*
|
---|
8412 | * Load the registers.
|
---|
8413 | */
|
---|
8414 | /** @todo CPU/VM detection possible! If CR4.OSFXSR=0 MXCSR it's
|
---|
8415 | * implementation specific whether MXCSR and XMM0-XMM7 are restored. */
|
---|
8416 |
|
---|
8417 | /* common for all formats */
|
---|
8418 | pDst->FCW = pSrc->FCW;
|
---|
8419 | pDst->FSW = pSrc->FSW;
|
---|
8420 | pDst->FTW = pSrc->FTW & UINT16_C(0xff);
|
---|
8421 | pDst->FOP = pSrc->FOP;
|
---|
8422 | pDst->MXCSR = fMXCSR;
|
---|
8423 | /* (MXCSR_MASK is read-only) */
|
---|
8424 | for (uint32_t i = 0; i < RT_ELEMENTS(pSrc->aRegs); i++)
|
---|
8425 | {
|
---|
8426 | pDst->aRegs[i].au32[0] = pSrc->aRegs[i].au32[0];
|
---|
8427 | pDst->aRegs[i].au32[1] = pSrc->aRegs[i].au32[1];
|
---|
8428 | pDst->aRegs[i].au32[2] = pSrc->aRegs[i].au32[2] & UINT32_C(0xffff);
|
---|
8429 | pDst->aRegs[i].au32[3] = 0;
|
---|
8430 | }
|
---|
8431 |
|
---|
8432 | /* FPU IP, CS, DP and DS. */
|
---|
8433 | if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
|
---|
8434 | {
|
---|
8435 | pDst->FPUIP = pSrc->FPUIP;
|
---|
8436 | pDst->CS = pSrc->CS;
|
---|
8437 | pDst->Rsrvd1 = pSrc->Rsrvd1;
|
---|
8438 | pDst->FPUDP = pSrc->FPUDP;
|
---|
8439 | pDst->DS = pSrc->DS;
|
---|
8440 | pDst->Rsrvd2 = pSrc->Rsrvd2;
|
---|
8441 | }
|
---|
8442 | else
|
---|
8443 | {
|
---|
8444 | pDst->FPUIP = pSrc->FPUIP;
|
---|
8445 | pDst->CS = pSrc->CS;
|
---|
8446 | pDst->Rsrvd1 = 0;
|
---|
8447 | pDst->FPUDP = pSrc->FPUDP;
|
---|
8448 | pDst->DS = pSrc->DS;
|
---|
8449 | pDst->Rsrvd2 = 0;
|
---|
8450 | }
|
---|
8451 |
|
---|
8452 | /* XMM registers. */
|
---|
8453 | if ( !(pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_FFXSR)
|
---|
8454 | || pVCpu->iem.s.enmCpuMode != IEMMODE_64BIT
|
---|
8455 | || pVCpu->iem.s.uCpl != 0)
|
---|
8456 | {
|
---|
8457 | uint32_t cXmmRegs = enmEffOpSize == IEMMODE_64BIT ? 16 : 8;
|
---|
8458 | for (uint32_t i = 0; i < cXmmRegs; i++)
|
---|
8459 | pDst->aXMM[i] = pSrc->aXMM[i];
|
---|
8460 | }
|
---|
8461 |
|
---|
8462 | /*
|
---|
8463 | * Commit the memory.
|
---|
8464 | */
|
---|
8465 | rcStrict = iemMemCommitAndUnmap(pVCpu, pvMem512, IEM_ACCESS_DATA_R);
|
---|
8466 | if (rcStrict != VINF_SUCCESS)
|
---|
8467 | return rcStrict;
|
---|
8468 |
|
---|
8469 | iemHlpUsedFpu(pVCpu);
|
---|
8470 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
8471 | return VINF_SUCCESS;
|
---|
8472 | }
|
---|
8473 |
|
---|
8474 |
|
---|
8475 | /**
|
---|
8476 | * Implements 'XSAVE'.
|
---|
8477 | *
|
---|
8478 | * @param iEffSeg The effective segment.
|
---|
8479 | * @param GCPtrEff The address of the image.
|
---|
8480 | * @param enmEffOpSize The operand size (only REX.W really matters).
|
---|
8481 | */
|
---|
8482 | IEM_CIMPL_DEF_3(iemCImpl_xsave, uint8_t, iEffSeg, RTGCPTR, GCPtrEff, IEMMODE, enmEffOpSize)
|
---|
8483 | {
|
---|
8484 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX | CPUMCTX_EXTRN_OTHER_XSAVE | CPUMCTX_EXTRN_XCRx);
|
---|
8485 |
|
---|
8486 | /*
|
---|
8487 | * Raise exceptions.
|
---|
8488 | */
|
---|
8489 | if (!(pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSXSAVE))
|
---|
8490 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
8491 | /* When in VMX non-root mode and XSAVE/XRSTOR is not enabled, it results in #UD. */
|
---|
8492 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
8493 | && !IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_XSAVES_XRSTORS))
|
---|
8494 | {
|
---|
8495 | Log(("xrstor: Not enabled for nested-guest execution -> #UD\n"));
|
---|
8496 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
8497 | }
|
---|
8498 | if (pVCpu->cpum.GstCtx.cr0 & X86_CR0_TS)
|
---|
8499 | return iemRaiseDeviceNotAvailable(pVCpu);
|
---|
8500 | if (GCPtrEff & 63)
|
---|
8501 | {
|
---|
8502 | /** @todo CPU/VM detection possible! \#AC might not be signal for
|
---|
8503 | * all/any misalignment sizes, intel says its an implementation detail. */
|
---|
8504 | if ( (pVCpu->cpum.GstCtx.cr0 & X86_CR0_AM)
|
---|
8505 | && pVCpu->cpum.GstCtx.eflags.Bits.u1AC
|
---|
8506 | && pVCpu->iem.s.uCpl == 3)
|
---|
8507 | return iemRaiseAlignmentCheckException(pVCpu);
|
---|
8508 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
8509 | }
|
---|
8510 |
|
---|
8511 | /*
|
---|
8512 | * Calc the requested mask.
|
---|
8513 | */
|
---|
8514 | uint64_t const fReqComponents = RT_MAKE_U64(pVCpu->cpum.GstCtx.eax, pVCpu->cpum.GstCtx.edx) & pVCpu->cpum.GstCtx.aXcr[0];
|
---|
8515 | AssertLogRelReturn(!(fReqComponents & ~(XSAVE_C_X87 | XSAVE_C_SSE | XSAVE_C_YMM)), VERR_IEM_ASPECT_NOT_IMPLEMENTED);
|
---|
8516 | uint64_t const fXInUse = pVCpu->cpum.GstCtx.aXcr[0];
|
---|
8517 |
|
---|
8518 | /** @todo figure out the exact protocol for the memory access. Currently we
|
---|
8519 | * just need this crap to work halfways to make it possible to test
|
---|
8520 | * AVX instructions. */
|
---|
8521 | /** @todo figure out the XINUSE and XMODIFIED */
|
---|
8522 |
|
---|
8523 | /*
|
---|
8524 | * Access the x87 memory state.
|
---|
8525 | */
|
---|
8526 | /* The x87+SSE state. */
|
---|
8527 | void *pvMem512;
|
---|
8528 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, &pvMem512, 512, iEffSeg, GCPtrEff, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
|
---|
8529 | if (rcStrict != VINF_SUCCESS)
|
---|
8530 | return rcStrict;
|
---|
8531 | PX86FXSTATE pDst = (PX86FXSTATE)pvMem512;
|
---|
8532 | PCX86FXSTATE pSrc = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
8533 |
|
---|
8534 | /* The header. */
|
---|
8535 | PX86XSAVEHDR pHdr;
|
---|
8536 | rcStrict = iemMemMap(pVCpu, (void **)&pHdr, sizeof(&pHdr), iEffSeg, GCPtrEff + 512, IEM_ACCESS_DATA_RW);
|
---|
8537 | if (rcStrict != VINF_SUCCESS)
|
---|
8538 | return rcStrict;
|
---|
8539 |
|
---|
8540 | /*
|
---|
8541 | * Store the X87 state.
|
---|
8542 | */
|
---|
8543 | if (fReqComponents & XSAVE_C_X87)
|
---|
8544 | {
|
---|
8545 | /* common for all formats */
|
---|
8546 | pDst->FCW = pSrc->FCW;
|
---|
8547 | pDst->FSW = pSrc->FSW;
|
---|
8548 | pDst->FTW = pSrc->FTW & UINT16_C(0xff);
|
---|
8549 | pDst->FOP = pSrc->FOP;
|
---|
8550 | pDst->FPUIP = pSrc->FPUIP;
|
---|
8551 | pDst->CS = pSrc->CS;
|
---|
8552 | pDst->FPUDP = pSrc->FPUDP;
|
---|
8553 | pDst->DS = pSrc->DS;
|
---|
8554 | if (enmEffOpSize == IEMMODE_64BIT)
|
---|
8555 | {
|
---|
8556 | /* Save upper 16-bits of FPUIP (IP:CS:Rsvd1) and FPUDP (DP:DS:Rsvd2). */
|
---|
8557 | pDst->Rsrvd1 = pSrc->Rsrvd1;
|
---|
8558 | pDst->Rsrvd2 = pSrc->Rsrvd2;
|
---|
8559 | pDst->au32RsrvdForSoftware[0] = 0;
|
---|
8560 | }
|
---|
8561 | else
|
---|
8562 | {
|
---|
8563 | pDst->Rsrvd1 = 0;
|
---|
8564 | pDst->Rsrvd2 = 0;
|
---|
8565 | pDst->au32RsrvdForSoftware[0] = X86_FXSTATE_RSVD_32BIT_MAGIC;
|
---|
8566 | }
|
---|
8567 | for (uint32_t i = 0; i < RT_ELEMENTS(pDst->aRegs); i++)
|
---|
8568 | {
|
---|
8569 | /** @todo Testcase: What actually happens to the 6 reserved bytes? I'm clearing
|
---|
8570 | * them for now... */
|
---|
8571 | pDst->aRegs[i].au32[0] = pSrc->aRegs[i].au32[0];
|
---|
8572 | pDst->aRegs[i].au32[1] = pSrc->aRegs[i].au32[1];
|
---|
8573 | pDst->aRegs[i].au32[2] = pSrc->aRegs[i].au32[2] & UINT32_C(0xffff);
|
---|
8574 | pDst->aRegs[i].au32[3] = 0;
|
---|
8575 | }
|
---|
8576 |
|
---|
8577 | }
|
---|
8578 |
|
---|
8579 | if (fReqComponents & (XSAVE_C_SSE | XSAVE_C_YMM))
|
---|
8580 | {
|
---|
8581 | pDst->MXCSR = pSrc->MXCSR;
|
---|
8582 | pDst->MXCSR_MASK = CPUMGetGuestMxCsrMask(pVCpu->CTX_SUFF(pVM));
|
---|
8583 | }
|
---|
8584 |
|
---|
8585 | if (fReqComponents & XSAVE_C_SSE)
|
---|
8586 | {
|
---|
8587 | /* XMM registers. */
|
---|
8588 | uint32_t cXmmRegs = enmEffOpSize == IEMMODE_64BIT ? 16 : 8;
|
---|
8589 | for (uint32_t i = 0; i < cXmmRegs; i++)
|
---|
8590 | pDst->aXMM[i] = pSrc->aXMM[i];
|
---|
8591 | /** @todo Testcase: What happens to the reserved XMM registers? Untouched,
|
---|
8592 | * right? */
|
---|
8593 | }
|
---|
8594 |
|
---|
8595 | /* Commit the x87 state bits. (probably wrong) */
|
---|
8596 | rcStrict = iemMemCommitAndUnmap(pVCpu, pvMem512, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
|
---|
8597 | if (rcStrict != VINF_SUCCESS)
|
---|
8598 | return rcStrict;
|
---|
8599 |
|
---|
8600 | /*
|
---|
8601 | * Store AVX state.
|
---|
8602 | */
|
---|
8603 | if (fReqComponents & XSAVE_C_YMM)
|
---|
8604 | {
|
---|
8605 | /** @todo testcase: xsave64 vs xsave32 wrt XSAVE_C_YMM. */
|
---|
8606 | AssertLogRelReturn(pVCpu->cpum.GstCtx.aoffXState[XSAVE_C_YMM_BIT] != UINT16_MAX, VERR_IEM_IPE_9);
|
---|
8607 | PCX86XSAVEYMMHI pCompSrc = CPUMCTX_XSAVE_C_PTR(IEM_GET_CTX(pVCpu), XSAVE_C_YMM_BIT, PCX86XSAVEYMMHI);
|
---|
8608 | PX86XSAVEYMMHI pCompDst;
|
---|
8609 | rcStrict = iemMemMap(pVCpu, (void **)&pCompDst, sizeof(*pCompDst), iEffSeg, GCPtrEff + pVCpu->cpum.GstCtx.aoffXState[XSAVE_C_YMM_BIT],
|
---|
8610 | IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
|
---|
8611 | if (rcStrict != VINF_SUCCESS)
|
---|
8612 | return rcStrict;
|
---|
8613 |
|
---|
8614 | uint32_t cXmmRegs = enmEffOpSize == IEMMODE_64BIT ? 16 : 8;
|
---|
8615 | for (uint32_t i = 0; i < cXmmRegs; i++)
|
---|
8616 | pCompDst->aYmmHi[i] = pCompSrc->aYmmHi[i];
|
---|
8617 |
|
---|
8618 | rcStrict = iemMemCommitAndUnmap(pVCpu, pCompDst, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
|
---|
8619 | if (rcStrict != VINF_SUCCESS)
|
---|
8620 | return rcStrict;
|
---|
8621 | }
|
---|
8622 |
|
---|
8623 | /*
|
---|
8624 | * Update the header.
|
---|
8625 | */
|
---|
8626 | pHdr->bmXState = (pHdr->bmXState & ~fReqComponents)
|
---|
8627 | | (fReqComponents & fXInUse);
|
---|
8628 |
|
---|
8629 | rcStrict = iemMemCommitAndUnmap(pVCpu, pHdr, IEM_ACCESS_DATA_RW);
|
---|
8630 | if (rcStrict != VINF_SUCCESS)
|
---|
8631 | return rcStrict;
|
---|
8632 |
|
---|
8633 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
8634 | return VINF_SUCCESS;
|
---|
8635 | }
|
---|
8636 |
|
---|
8637 |
|
---|
8638 | /**
|
---|
8639 | * Implements 'XRSTOR'.
|
---|
8640 | *
|
---|
8641 | * @param iEffSeg The effective segment.
|
---|
8642 | * @param GCPtrEff The address of the image.
|
---|
8643 | * @param enmEffOpSize The operand size (only REX.W really matters).
|
---|
8644 | */
|
---|
8645 | IEM_CIMPL_DEF_3(iemCImpl_xrstor, uint8_t, iEffSeg, RTGCPTR, GCPtrEff, IEMMODE, enmEffOpSize)
|
---|
8646 | {
|
---|
8647 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX | CPUMCTX_EXTRN_OTHER_XSAVE | CPUMCTX_EXTRN_XCRx);
|
---|
8648 |
|
---|
8649 | /*
|
---|
8650 | * Raise exceptions.
|
---|
8651 | */
|
---|
8652 | if (!(pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSXSAVE))
|
---|
8653 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
8654 | /* When in VMX non-root mode and XSAVE/XRSTOR is not enabled, it results in #UD. */
|
---|
8655 | if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
|
---|
8656 | && !IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_XSAVES_XRSTORS))
|
---|
8657 | {
|
---|
8658 | Log(("xrstor: Not enabled for nested-guest execution -> #UD\n"));
|
---|
8659 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
8660 | }
|
---|
8661 | if (pVCpu->cpum.GstCtx.cr0 & X86_CR0_TS)
|
---|
8662 | return iemRaiseDeviceNotAvailable(pVCpu);
|
---|
8663 | if (GCPtrEff & 63)
|
---|
8664 | {
|
---|
8665 | /** @todo CPU/VM detection possible! \#AC might not be signal for
|
---|
8666 | * all/any misalignment sizes, intel says its an implementation detail. */
|
---|
8667 | if ( (pVCpu->cpum.GstCtx.cr0 & X86_CR0_AM)
|
---|
8668 | && pVCpu->cpum.GstCtx.eflags.Bits.u1AC
|
---|
8669 | && pVCpu->iem.s.uCpl == 3)
|
---|
8670 | return iemRaiseAlignmentCheckException(pVCpu);
|
---|
8671 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
8672 | }
|
---|
8673 |
|
---|
8674 | /** @todo figure out the exact protocol for the memory access. Currently we
|
---|
8675 | * just need this crap to work halfways to make it possible to test
|
---|
8676 | * AVX instructions. */
|
---|
8677 | /** @todo figure out the XINUSE and XMODIFIED */
|
---|
8678 |
|
---|
8679 | /*
|
---|
8680 | * Access the x87 memory state.
|
---|
8681 | */
|
---|
8682 | /* The x87+SSE state. */
|
---|
8683 | void *pvMem512;
|
---|
8684 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, &pvMem512, 512, iEffSeg, GCPtrEff, IEM_ACCESS_DATA_R);
|
---|
8685 | if (rcStrict != VINF_SUCCESS)
|
---|
8686 | return rcStrict;
|
---|
8687 | PCX86FXSTATE pSrc = (PCX86FXSTATE)pvMem512;
|
---|
8688 | PX86FXSTATE pDst = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
8689 |
|
---|
8690 | /*
|
---|
8691 | * Calc the requested mask
|
---|
8692 | */
|
---|
8693 | PX86XSAVEHDR pHdrDst = &pVCpu->cpum.GstCtx.XState.Hdr;
|
---|
8694 | PCX86XSAVEHDR pHdrSrc;
|
---|
8695 | rcStrict = iemMemMap(pVCpu, (void **)&pHdrSrc, sizeof(&pHdrSrc), iEffSeg, GCPtrEff + 512, IEM_ACCESS_DATA_R);
|
---|
8696 | if (rcStrict != VINF_SUCCESS)
|
---|
8697 | return rcStrict;
|
---|
8698 |
|
---|
8699 | uint64_t const fReqComponents = RT_MAKE_U64(pVCpu->cpum.GstCtx.eax, pVCpu->cpum.GstCtx.edx) & pVCpu->cpum.GstCtx.aXcr[0];
|
---|
8700 | AssertLogRelReturn(!(fReqComponents & ~(XSAVE_C_X87 | XSAVE_C_SSE | XSAVE_C_YMM)), VERR_IEM_ASPECT_NOT_IMPLEMENTED);
|
---|
8701 | //uint64_t const fXInUse = pVCpu->cpum.GstCtx.aXcr[0];
|
---|
8702 | uint64_t const fRstorMask = pHdrSrc->bmXState;
|
---|
8703 | uint64_t const fCompMask = pHdrSrc->bmXComp;
|
---|
8704 |
|
---|
8705 | AssertLogRelReturn(!(fCompMask & XSAVE_C_X), VERR_IEM_ASPECT_NOT_IMPLEMENTED);
|
---|
8706 |
|
---|
8707 | uint32_t const cXmmRegs = enmEffOpSize == IEMMODE_64BIT ? 16 : 8;
|
---|
8708 |
|
---|
8709 | /* We won't need this any longer. */
|
---|
8710 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)pHdrSrc, IEM_ACCESS_DATA_R);
|
---|
8711 | if (rcStrict != VINF_SUCCESS)
|
---|
8712 | return rcStrict;
|
---|
8713 |
|
---|
8714 | /*
|
---|
8715 | * Store the X87 state.
|
---|
8716 | */
|
---|
8717 | if (fReqComponents & XSAVE_C_X87)
|
---|
8718 | {
|
---|
8719 | if (fRstorMask & XSAVE_C_X87)
|
---|
8720 | {
|
---|
8721 | pDst->FCW = pSrc->FCW;
|
---|
8722 | pDst->FSW = pSrc->FSW;
|
---|
8723 | pDst->FTW = pSrc->FTW & UINT16_C(0xff);
|
---|
8724 | pDst->FOP = pSrc->FOP;
|
---|
8725 | pDst->FPUIP = pSrc->FPUIP;
|
---|
8726 | pDst->CS = pSrc->CS;
|
---|
8727 | pDst->FPUDP = pSrc->FPUDP;
|
---|
8728 | pDst->DS = pSrc->DS;
|
---|
8729 | if (enmEffOpSize == IEMMODE_64BIT)
|
---|
8730 | {
|
---|
8731 | /* Save upper 16-bits of FPUIP (IP:CS:Rsvd1) and FPUDP (DP:DS:Rsvd2). */
|
---|
8732 | pDst->Rsrvd1 = pSrc->Rsrvd1;
|
---|
8733 | pDst->Rsrvd2 = pSrc->Rsrvd2;
|
---|
8734 | }
|
---|
8735 | else
|
---|
8736 | {
|
---|
8737 | pDst->Rsrvd1 = 0;
|
---|
8738 | pDst->Rsrvd2 = 0;
|
---|
8739 | }
|
---|
8740 | for (uint32_t i = 0; i < RT_ELEMENTS(pDst->aRegs); i++)
|
---|
8741 | {
|
---|
8742 | pDst->aRegs[i].au32[0] = pSrc->aRegs[i].au32[0];
|
---|
8743 | pDst->aRegs[i].au32[1] = pSrc->aRegs[i].au32[1];
|
---|
8744 | pDst->aRegs[i].au32[2] = pSrc->aRegs[i].au32[2] & UINT32_C(0xffff);
|
---|
8745 | pDst->aRegs[i].au32[3] = 0;
|
---|
8746 | }
|
---|
8747 | }
|
---|
8748 | else
|
---|
8749 | {
|
---|
8750 | pDst->FCW = 0x37f;
|
---|
8751 | pDst->FSW = 0;
|
---|
8752 | pDst->FTW = 0x00; /* 0 - empty. */
|
---|
8753 | pDst->FPUDP = 0;
|
---|
8754 | pDst->DS = 0; //??
|
---|
8755 | pDst->Rsrvd2= 0;
|
---|
8756 | pDst->FPUIP = 0;
|
---|
8757 | pDst->CS = 0; //??
|
---|
8758 | pDst->Rsrvd1= 0;
|
---|
8759 | pDst->FOP = 0;
|
---|
8760 | for (uint32_t i = 0; i < RT_ELEMENTS(pSrc->aRegs); i++)
|
---|
8761 | {
|
---|
8762 | pDst->aRegs[i].au32[0] = 0;
|
---|
8763 | pDst->aRegs[i].au32[1] = 0;
|
---|
8764 | pDst->aRegs[i].au32[2] = 0;
|
---|
8765 | pDst->aRegs[i].au32[3] = 0;
|
---|
8766 | }
|
---|
8767 | }
|
---|
8768 | pHdrDst->bmXState |= XSAVE_C_X87; /* playing safe for now */
|
---|
8769 | }
|
---|
8770 |
|
---|
8771 | /* MXCSR */
|
---|
8772 | if (fReqComponents & (XSAVE_C_SSE | XSAVE_C_YMM))
|
---|
8773 | {
|
---|
8774 | if (fRstorMask & (XSAVE_C_SSE | XSAVE_C_YMM))
|
---|
8775 | pDst->MXCSR = pSrc->MXCSR;
|
---|
8776 | else
|
---|
8777 | pDst->MXCSR = 0x1f80;
|
---|
8778 | }
|
---|
8779 |
|
---|
8780 | /* XMM registers. */
|
---|
8781 | if (fReqComponents & XSAVE_C_SSE)
|
---|
8782 | {
|
---|
8783 | if (fRstorMask & XSAVE_C_SSE)
|
---|
8784 | {
|
---|
8785 | for (uint32_t i = 0; i < cXmmRegs; i++)
|
---|
8786 | pDst->aXMM[i] = pSrc->aXMM[i];
|
---|
8787 | /** @todo Testcase: What happens to the reserved XMM registers? Untouched,
|
---|
8788 | * right? */
|
---|
8789 | }
|
---|
8790 | else
|
---|
8791 | {
|
---|
8792 | for (uint32_t i = 0; i < cXmmRegs; i++)
|
---|
8793 | {
|
---|
8794 | pDst->aXMM[i].au64[0] = 0;
|
---|
8795 | pDst->aXMM[i].au64[1] = 0;
|
---|
8796 | }
|
---|
8797 | }
|
---|
8798 | pHdrDst->bmXState |= XSAVE_C_SSE; /* playing safe for now */
|
---|
8799 | }
|
---|
8800 |
|
---|
8801 | /* Unmap the x87 state bits (so we've don't run out of mapping). */
|
---|
8802 | rcStrict = iemMemCommitAndUnmap(pVCpu, pvMem512, IEM_ACCESS_DATA_R);
|
---|
8803 | if (rcStrict != VINF_SUCCESS)
|
---|
8804 | return rcStrict;
|
---|
8805 |
|
---|
8806 | /*
|
---|
8807 | * Restore AVX state.
|
---|
8808 | */
|
---|
8809 | if (fReqComponents & XSAVE_C_YMM)
|
---|
8810 | {
|
---|
8811 | AssertLogRelReturn(pVCpu->cpum.GstCtx.aoffXState[XSAVE_C_YMM_BIT] != UINT16_MAX, VERR_IEM_IPE_9);
|
---|
8812 | PX86XSAVEYMMHI pCompDst = CPUMCTX_XSAVE_C_PTR(IEM_GET_CTX(pVCpu), XSAVE_C_YMM_BIT, PX86XSAVEYMMHI);
|
---|
8813 |
|
---|
8814 | if (fRstorMask & XSAVE_C_YMM)
|
---|
8815 | {
|
---|
8816 | /** @todo testcase: xsave64 vs xsave32 wrt XSAVE_C_YMM. */
|
---|
8817 | PCX86XSAVEYMMHI pCompSrc;
|
---|
8818 | rcStrict = iemMemMap(pVCpu, (void **)&pCompSrc, sizeof(*pCompDst),
|
---|
8819 | iEffSeg, GCPtrEff + pVCpu->cpum.GstCtx.aoffXState[XSAVE_C_YMM_BIT], IEM_ACCESS_DATA_R);
|
---|
8820 | if (rcStrict != VINF_SUCCESS)
|
---|
8821 | return rcStrict;
|
---|
8822 |
|
---|
8823 | for (uint32_t i = 0; i < cXmmRegs; i++)
|
---|
8824 | {
|
---|
8825 | pCompDst->aYmmHi[i].au64[0] = pCompSrc->aYmmHi[i].au64[0];
|
---|
8826 | pCompDst->aYmmHi[i].au64[1] = pCompSrc->aYmmHi[i].au64[1];
|
---|
8827 | }
|
---|
8828 |
|
---|
8829 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)pCompSrc, IEM_ACCESS_DATA_R);
|
---|
8830 | if (rcStrict != VINF_SUCCESS)
|
---|
8831 | return rcStrict;
|
---|
8832 | }
|
---|
8833 | else
|
---|
8834 | {
|
---|
8835 | for (uint32_t i = 0; i < cXmmRegs; i++)
|
---|
8836 | {
|
---|
8837 | pCompDst->aYmmHi[i].au64[0] = 0;
|
---|
8838 | pCompDst->aYmmHi[i].au64[1] = 0;
|
---|
8839 | }
|
---|
8840 | }
|
---|
8841 | pHdrDst->bmXState |= XSAVE_C_YMM; /* playing safe for now */
|
---|
8842 | }
|
---|
8843 |
|
---|
8844 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
8845 | return VINF_SUCCESS;
|
---|
8846 | }
|
---|
8847 |
|
---|
8848 |
|
---|
8849 |
|
---|
8850 |
|
---|
8851 | /**
|
---|
8852 | * Implements 'STMXCSR'.
|
---|
8853 | *
|
---|
8854 | * @param GCPtrEff The address of the image.
|
---|
8855 | */
|
---|
8856 | IEM_CIMPL_DEF_2(iemCImpl_stmxcsr, uint8_t, iEffSeg, RTGCPTR, GCPtrEff)
|
---|
8857 | {
|
---|
8858 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX);
|
---|
8859 |
|
---|
8860 | /*
|
---|
8861 | * Raise exceptions.
|
---|
8862 | */
|
---|
8863 | if ( !(pVCpu->cpum.GstCtx.cr0 & X86_CR0_EM)
|
---|
8864 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSFXSR))
|
---|
8865 | {
|
---|
8866 | if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_TS))
|
---|
8867 | {
|
---|
8868 | /*
|
---|
8869 | * Do the job.
|
---|
8870 | */
|
---|
8871 | VBOXSTRICTRC rcStrict = iemMemStoreDataU32(pVCpu, iEffSeg, GCPtrEff, pVCpu->cpum.GstCtx.XState.x87.MXCSR);
|
---|
8872 | if (rcStrict == VINF_SUCCESS)
|
---|
8873 | {
|
---|
8874 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
8875 | return VINF_SUCCESS;
|
---|
8876 | }
|
---|
8877 | return rcStrict;
|
---|
8878 | }
|
---|
8879 | return iemRaiseDeviceNotAvailable(pVCpu);
|
---|
8880 | }
|
---|
8881 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
8882 | }
|
---|
8883 |
|
---|
8884 |
|
---|
8885 | /**
|
---|
8886 | * Implements 'VSTMXCSR'.
|
---|
8887 | *
|
---|
8888 | * @param GCPtrEff The address of the image.
|
---|
8889 | */
|
---|
8890 | IEM_CIMPL_DEF_2(iemCImpl_vstmxcsr, uint8_t, iEffSeg, RTGCPTR, GCPtrEff)
|
---|
8891 | {
|
---|
8892 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX | CPUMCTX_EXTRN_XCRx);
|
---|
8893 |
|
---|
8894 | /*
|
---|
8895 | * Raise exceptions.
|
---|
8896 | */
|
---|
8897 | if ( ( !IEM_IS_GUEST_CPU_AMD(pVCpu)
|
---|
8898 | ? (pVCpu->cpum.GstCtx.aXcr[0] & (XSAVE_C_SSE | XSAVE_C_YMM)) == (XSAVE_C_SSE | XSAVE_C_YMM)
|
---|
8899 | : !(pVCpu->cpum.GstCtx.cr0 & X86_CR0_EM)) /* AMD Jaguar CPU (f0x16,m0,s1) behaviour */
|
---|
8900 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSXSAVE))
|
---|
8901 | {
|
---|
8902 | if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_TS))
|
---|
8903 | {
|
---|
8904 | /*
|
---|
8905 | * Do the job.
|
---|
8906 | */
|
---|
8907 | VBOXSTRICTRC rcStrict = iemMemStoreDataU32(pVCpu, iEffSeg, GCPtrEff, pVCpu->cpum.GstCtx.XState.x87.MXCSR);
|
---|
8908 | if (rcStrict == VINF_SUCCESS)
|
---|
8909 | {
|
---|
8910 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
8911 | return VINF_SUCCESS;
|
---|
8912 | }
|
---|
8913 | return rcStrict;
|
---|
8914 | }
|
---|
8915 | return iemRaiseDeviceNotAvailable(pVCpu);
|
---|
8916 | }
|
---|
8917 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
8918 | }
|
---|
8919 |
|
---|
8920 |
|
---|
8921 | /**
|
---|
8922 | * Implements 'LDMXCSR'.
|
---|
8923 | *
|
---|
8924 | * @param GCPtrEff The address of the image.
|
---|
8925 | */
|
---|
8926 | IEM_CIMPL_DEF_2(iemCImpl_ldmxcsr, uint8_t, iEffSeg, RTGCPTR, GCPtrEff)
|
---|
8927 | {
|
---|
8928 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX);
|
---|
8929 |
|
---|
8930 | /*
|
---|
8931 | * Raise exceptions.
|
---|
8932 | */
|
---|
8933 | /** @todo testcase - order of LDMXCSR faults. Does \#PF, \#GP and \#SS
|
---|
8934 | * happen after or before \#UD and \#EM? */
|
---|
8935 | if ( !(pVCpu->cpum.GstCtx.cr0 & X86_CR0_EM)
|
---|
8936 | && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSFXSR))
|
---|
8937 | {
|
---|
8938 | if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_TS))
|
---|
8939 | {
|
---|
8940 | /*
|
---|
8941 | * Do the job.
|
---|
8942 | */
|
---|
8943 | uint32_t fNewMxCsr;
|
---|
8944 | VBOXSTRICTRC rcStrict = iemMemFetchDataU32(pVCpu, &fNewMxCsr, iEffSeg, GCPtrEff);
|
---|
8945 | if (rcStrict == VINF_SUCCESS)
|
---|
8946 | {
|
---|
8947 | uint32_t const fMxCsrMask = CPUMGetGuestMxCsrMask(pVCpu->CTX_SUFF(pVM));
|
---|
8948 | if (!(fNewMxCsr & ~fMxCsrMask))
|
---|
8949 | {
|
---|
8950 | pVCpu->cpum.GstCtx.XState.x87.MXCSR = fNewMxCsr;
|
---|
8951 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
8952 | return VINF_SUCCESS;
|
---|
8953 | }
|
---|
8954 | Log(("lddmxcsr: New MXCSR=%#RX32 & ~MASK=%#RX32 = %#RX32 -> #GP(0)\n",
|
---|
8955 | fNewMxCsr, fMxCsrMask, fNewMxCsr & ~fMxCsrMask));
|
---|
8956 | return iemRaiseGeneralProtectionFault0(pVCpu);
|
---|
8957 | }
|
---|
8958 | return rcStrict;
|
---|
8959 | }
|
---|
8960 | return iemRaiseDeviceNotAvailable(pVCpu);
|
---|
8961 | }
|
---|
8962 | return iemRaiseUndefinedOpcode(pVCpu);
|
---|
8963 | }
|
---|
8964 |
|
---|
8965 |
|
---|
8966 | /**
|
---|
8967 | * Commmon routine for fnstenv and fnsave.
|
---|
8968 | *
|
---|
8969 | * @param pVCpu The cross context virtual CPU structure of the calling thread.
|
---|
8970 | * @param enmEffOpSize The effective operand size.
|
---|
8971 | * @param uPtr Where to store the state.
|
---|
8972 | */
|
---|
8973 | static void iemCImplCommonFpuStoreEnv(PVMCPUCC pVCpu, IEMMODE enmEffOpSize, RTPTRUNION uPtr)
|
---|
8974 | {
|
---|
8975 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87);
|
---|
8976 | PCX86FXSTATE pSrcX87 = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
8977 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
8978 | {
|
---|
8979 | uPtr.pu16[0] = pSrcX87->FCW;
|
---|
8980 | uPtr.pu16[1] = pSrcX87->FSW;
|
---|
8981 | uPtr.pu16[2] = iemFpuCalcFullFtw(pSrcX87);
|
---|
8982 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
8983 | {
|
---|
8984 | /** @todo Testcase: How does this work when the FPUIP/CS was saved in
|
---|
8985 | * protected mode or long mode and we save it in real mode? And vice
|
---|
8986 | * versa? And with 32-bit operand size? I think CPU is storing the
|
---|
8987 | * effective address ((CS << 4) + IP) in the offset register and not
|
---|
8988 | * doing any address calculations here. */
|
---|
8989 | uPtr.pu16[3] = (uint16_t)pSrcX87->FPUIP;
|
---|
8990 | uPtr.pu16[4] = ((pSrcX87->FPUIP >> 4) & UINT16_C(0xf000)) | pSrcX87->FOP;
|
---|
8991 | uPtr.pu16[5] = (uint16_t)pSrcX87->FPUDP;
|
---|
8992 | uPtr.pu16[6] = (pSrcX87->FPUDP >> 4) & UINT16_C(0xf000);
|
---|
8993 | }
|
---|
8994 | else
|
---|
8995 | {
|
---|
8996 | uPtr.pu16[3] = pSrcX87->FPUIP;
|
---|
8997 | uPtr.pu16[4] = pSrcX87->CS;
|
---|
8998 | uPtr.pu16[5] = pSrcX87->FPUDP;
|
---|
8999 | uPtr.pu16[6] = pSrcX87->DS;
|
---|
9000 | }
|
---|
9001 | }
|
---|
9002 | else
|
---|
9003 | {
|
---|
9004 | /** @todo Testcase: what is stored in the "gray" areas? (figure 8-9 and 8-10) */
|
---|
9005 | uPtr.pu16[0*2] = pSrcX87->FCW;
|
---|
9006 | uPtr.pu16[0*2+1] = 0xffff; /* (0xffff observed on intel skylake.) */
|
---|
9007 | uPtr.pu16[1*2] = pSrcX87->FSW;
|
---|
9008 | uPtr.pu16[1*2+1] = 0xffff;
|
---|
9009 | uPtr.pu16[2*2] = iemFpuCalcFullFtw(pSrcX87);
|
---|
9010 | uPtr.pu16[2*2+1] = 0xffff;
|
---|
9011 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
9012 | {
|
---|
9013 | uPtr.pu16[3*2] = (uint16_t)pSrcX87->FPUIP;
|
---|
9014 | uPtr.pu32[4] = ((pSrcX87->FPUIP & UINT32_C(0xffff0000)) >> 4) | pSrcX87->FOP;
|
---|
9015 | uPtr.pu16[5*2] = (uint16_t)pSrcX87->FPUDP;
|
---|
9016 | uPtr.pu32[6] = (pSrcX87->FPUDP & UINT32_C(0xffff0000)) >> 4;
|
---|
9017 | }
|
---|
9018 | else
|
---|
9019 | {
|
---|
9020 | uPtr.pu32[3] = pSrcX87->FPUIP;
|
---|
9021 | uPtr.pu16[4*2] = pSrcX87->CS;
|
---|
9022 | uPtr.pu16[4*2+1] = pSrcX87->FOP;
|
---|
9023 | uPtr.pu32[5] = pSrcX87->FPUDP;
|
---|
9024 | uPtr.pu16[6*2] = pSrcX87->DS;
|
---|
9025 | uPtr.pu16[6*2+1] = 0xffff;
|
---|
9026 | }
|
---|
9027 | }
|
---|
9028 | }
|
---|
9029 |
|
---|
9030 |
|
---|
9031 | /**
|
---|
9032 | * Commmon routine for fldenv and frstor
|
---|
9033 | *
|
---|
9034 | * @param pVCpu The cross context virtual CPU structure of the calling thread.
|
---|
9035 | * @param enmEffOpSize The effective operand size.
|
---|
9036 | * @param uPtr Where to store the state.
|
---|
9037 | */
|
---|
9038 | static void iemCImplCommonFpuRestoreEnv(PVMCPUCC pVCpu, IEMMODE enmEffOpSize, RTCPTRUNION uPtr)
|
---|
9039 | {
|
---|
9040 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87);
|
---|
9041 | PX86FXSTATE pDstX87 = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
9042 | if (enmEffOpSize == IEMMODE_16BIT)
|
---|
9043 | {
|
---|
9044 | pDstX87->FCW = uPtr.pu16[0];
|
---|
9045 | pDstX87->FSW = uPtr.pu16[1];
|
---|
9046 | pDstX87->FTW = uPtr.pu16[2];
|
---|
9047 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
9048 | {
|
---|
9049 | pDstX87->FPUIP = uPtr.pu16[3] | ((uint32_t)(uPtr.pu16[4] & UINT16_C(0xf000)) << 4);
|
---|
9050 | pDstX87->FPUDP = uPtr.pu16[5] | ((uint32_t)(uPtr.pu16[6] & UINT16_C(0xf000)) << 4);
|
---|
9051 | pDstX87->FOP = uPtr.pu16[4] & UINT16_C(0x07ff);
|
---|
9052 | pDstX87->CS = 0;
|
---|
9053 | pDstX87->Rsrvd1= 0;
|
---|
9054 | pDstX87->DS = 0;
|
---|
9055 | pDstX87->Rsrvd2= 0;
|
---|
9056 | }
|
---|
9057 | else
|
---|
9058 | {
|
---|
9059 | pDstX87->FPUIP = uPtr.pu16[3];
|
---|
9060 | pDstX87->CS = uPtr.pu16[4];
|
---|
9061 | pDstX87->Rsrvd1= 0;
|
---|
9062 | pDstX87->FPUDP = uPtr.pu16[5];
|
---|
9063 | pDstX87->DS = uPtr.pu16[6];
|
---|
9064 | pDstX87->Rsrvd2= 0;
|
---|
9065 | /** @todo Testcase: Is FOP cleared when doing 16-bit protected mode fldenv? */
|
---|
9066 | }
|
---|
9067 | }
|
---|
9068 | else
|
---|
9069 | {
|
---|
9070 | pDstX87->FCW = uPtr.pu16[0*2];
|
---|
9071 | pDstX87->FSW = uPtr.pu16[1*2];
|
---|
9072 | pDstX87->FTW = uPtr.pu16[2*2];
|
---|
9073 | if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
|
---|
9074 | {
|
---|
9075 | pDstX87->FPUIP = uPtr.pu16[3*2] | ((uPtr.pu32[4] & UINT32_C(0x0ffff000)) << 4);
|
---|
9076 | pDstX87->FOP = uPtr.pu32[4] & UINT16_C(0x07ff);
|
---|
9077 | pDstX87->FPUDP = uPtr.pu16[5*2] | ((uPtr.pu32[6] & UINT32_C(0x0ffff000)) << 4);
|
---|
9078 | pDstX87->CS = 0;
|
---|
9079 | pDstX87->Rsrvd1= 0;
|
---|
9080 | pDstX87->DS = 0;
|
---|
9081 | pDstX87->Rsrvd2= 0;
|
---|
9082 | }
|
---|
9083 | else
|
---|
9084 | {
|
---|
9085 | pDstX87->FPUIP = uPtr.pu32[3];
|
---|
9086 | pDstX87->CS = uPtr.pu16[4*2];
|
---|
9087 | pDstX87->Rsrvd1= 0;
|
---|
9088 | pDstX87->FOP = uPtr.pu16[4*2+1];
|
---|
9089 | pDstX87->FPUDP = uPtr.pu32[5];
|
---|
9090 | pDstX87->DS = uPtr.pu16[6*2];
|
---|
9091 | pDstX87->Rsrvd2= 0;
|
---|
9092 | }
|
---|
9093 | }
|
---|
9094 |
|
---|
9095 | /* Make adjustments. */
|
---|
9096 | pDstX87->FTW = iemFpuCompressFtw(pDstX87->FTW);
|
---|
9097 | pDstX87->FCW &= ~X86_FCW_ZERO_MASK;
|
---|
9098 | iemFpuRecalcExceptionStatus(pDstX87);
|
---|
9099 | /** @todo Testcase: Check if ES and/or B are automatically cleared if no
|
---|
9100 | * exceptions are pending after loading the saved state? */
|
---|
9101 | }
|
---|
9102 |
|
---|
9103 |
|
---|
9104 | /**
|
---|
9105 | * Implements 'FNSTENV'.
|
---|
9106 | *
|
---|
9107 | * @param enmEffOpSize The operand size (only REX.W really matters).
|
---|
9108 | * @param iEffSeg The effective segment register for @a GCPtrEff.
|
---|
9109 | * @param GCPtrEffDst The address of the image.
|
---|
9110 | */
|
---|
9111 | IEM_CIMPL_DEF_3(iemCImpl_fnstenv, IEMMODE, enmEffOpSize, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
|
---|
9112 | {
|
---|
9113 | RTPTRUNION uPtr;
|
---|
9114 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, &uPtr.pv, enmEffOpSize == IEMMODE_16BIT ? 14 : 28,
|
---|
9115 | iEffSeg, GCPtrEffDst, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
|
---|
9116 | if (rcStrict != VINF_SUCCESS)
|
---|
9117 | return rcStrict;
|
---|
9118 |
|
---|
9119 | iemCImplCommonFpuStoreEnv(pVCpu, enmEffOpSize, uPtr);
|
---|
9120 |
|
---|
9121 | rcStrict = iemMemCommitAndUnmap(pVCpu, uPtr.pv, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
|
---|
9122 | if (rcStrict != VINF_SUCCESS)
|
---|
9123 | return rcStrict;
|
---|
9124 |
|
---|
9125 | /* Note: C0, C1, C2 and C3 are documented as undefined, we leave them untouched! */
|
---|
9126 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
9127 | return VINF_SUCCESS;
|
---|
9128 | }
|
---|
9129 |
|
---|
9130 |
|
---|
9131 | /**
|
---|
9132 | * Implements 'FNSAVE'.
|
---|
9133 | *
|
---|
9134 | * @param GCPtrEffDst The address of the image.
|
---|
9135 | * @param enmEffOpSize The operand size.
|
---|
9136 | */
|
---|
9137 | IEM_CIMPL_DEF_3(iemCImpl_fnsave, IEMMODE, enmEffOpSize, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
|
---|
9138 | {
|
---|
9139 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87);
|
---|
9140 |
|
---|
9141 | RTPTRUNION uPtr;
|
---|
9142 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, &uPtr.pv, enmEffOpSize == IEMMODE_16BIT ? 94 : 108,
|
---|
9143 | iEffSeg, GCPtrEffDst, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
|
---|
9144 | if (rcStrict != VINF_SUCCESS)
|
---|
9145 | return rcStrict;
|
---|
9146 |
|
---|
9147 | PX86FXSTATE pFpuCtx = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
9148 | iemCImplCommonFpuStoreEnv(pVCpu, enmEffOpSize, uPtr);
|
---|
9149 | PRTFLOAT80U paRegs = (PRTFLOAT80U)(uPtr.pu8 + (enmEffOpSize == IEMMODE_16BIT ? 14 : 28));
|
---|
9150 | for (uint32_t i = 0; i < RT_ELEMENTS(pFpuCtx->aRegs); i++)
|
---|
9151 | {
|
---|
9152 | paRegs[i].au32[0] = pFpuCtx->aRegs[i].au32[0];
|
---|
9153 | paRegs[i].au32[1] = pFpuCtx->aRegs[i].au32[1];
|
---|
9154 | paRegs[i].au16[4] = pFpuCtx->aRegs[i].au16[4];
|
---|
9155 | }
|
---|
9156 |
|
---|
9157 | rcStrict = iemMemCommitAndUnmap(pVCpu, uPtr.pv, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
|
---|
9158 | if (rcStrict != VINF_SUCCESS)
|
---|
9159 | return rcStrict;
|
---|
9160 |
|
---|
9161 | /*
|
---|
9162 | * Re-initialize the FPU context.
|
---|
9163 | */
|
---|
9164 | pFpuCtx->FCW = 0x37f;
|
---|
9165 | pFpuCtx->FSW = 0;
|
---|
9166 | pFpuCtx->FTW = 0x00; /* 0 - empty */
|
---|
9167 | pFpuCtx->FPUDP = 0;
|
---|
9168 | pFpuCtx->DS = 0;
|
---|
9169 | pFpuCtx->Rsrvd2= 0;
|
---|
9170 | pFpuCtx->FPUIP = 0;
|
---|
9171 | pFpuCtx->CS = 0;
|
---|
9172 | pFpuCtx->Rsrvd1= 0;
|
---|
9173 | pFpuCtx->FOP = 0;
|
---|
9174 |
|
---|
9175 | iemHlpUsedFpu(pVCpu);
|
---|
9176 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
9177 | return VINF_SUCCESS;
|
---|
9178 | }
|
---|
9179 |
|
---|
9180 |
|
---|
9181 |
|
---|
9182 | /**
|
---|
9183 | * Implements 'FLDENV'.
|
---|
9184 | *
|
---|
9185 | * @param enmEffOpSize The operand size (only REX.W really matters).
|
---|
9186 | * @param iEffSeg The effective segment register for @a GCPtrEff.
|
---|
9187 | * @param GCPtrEffSrc The address of the image.
|
---|
9188 | */
|
---|
9189 | IEM_CIMPL_DEF_3(iemCImpl_fldenv, IEMMODE, enmEffOpSize, uint8_t, iEffSeg, RTGCPTR, GCPtrEffSrc)
|
---|
9190 | {
|
---|
9191 | RTCPTRUNION uPtr;
|
---|
9192 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, (void **)&uPtr.pv, enmEffOpSize == IEMMODE_16BIT ? 14 : 28,
|
---|
9193 | iEffSeg, GCPtrEffSrc, IEM_ACCESS_DATA_R);
|
---|
9194 | if (rcStrict != VINF_SUCCESS)
|
---|
9195 | return rcStrict;
|
---|
9196 |
|
---|
9197 | iemCImplCommonFpuRestoreEnv(pVCpu, enmEffOpSize, uPtr);
|
---|
9198 |
|
---|
9199 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)uPtr.pv, IEM_ACCESS_DATA_R);
|
---|
9200 | if (rcStrict != VINF_SUCCESS)
|
---|
9201 | return rcStrict;
|
---|
9202 |
|
---|
9203 | iemHlpUsedFpu(pVCpu);
|
---|
9204 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
9205 | return VINF_SUCCESS;
|
---|
9206 | }
|
---|
9207 |
|
---|
9208 |
|
---|
9209 | /**
|
---|
9210 | * Implements 'FRSTOR'.
|
---|
9211 | *
|
---|
9212 | * @param GCPtrEffSrc The address of the image.
|
---|
9213 | * @param enmEffOpSize The operand size.
|
---|
9214 | */
|
---|
9215 | IEM_CIMPL_DEF_3(iemCImpl_frstor, IEMMODE, enmEffOpSize, uint8_t, iEffSeg, RTGCPTR, GCPtrEffSrc)
|
---|
9216 | {
|
---|
9217 | RTCPTRUNION uPtr;
|
---|
9218 | VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, (void **)&uPtr.pv, enmEffOpSize == IEMMODE_16BIT ? 94 : 108,
|
---|
9219 | iEffSeg, GCPtrEffSrc, IEM_ACCESS_DATA_R);
|
---|
9220 | if (rcStrict != VINF_SUCCESS)
|
---|
9221 | return rcStrict;
|
---|
9222 |
|
---|
9223 | PX86FXSTATE pFpuCtx = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
9224 | iemCImplCommonFpuRestoreEnv(pVCpu, enmEffOpSize, uPtr);
|
---|
9225 | PCRTFLOAT80U paRegs = (PCRTFLOAT80U)(uPtr.pu8 + (enmEffOpSize == IEMMODE_16BIT ? 14 : 28));
|
---|
9226 | for (uint32_t i = 0; i < RT_ELEMENTS(pFpuCtx->aRegs); i++)
|
---|
9227 | {
|
---|
9228 | pFpuCtx->aRegs[i].au32[0] = paRegs[i].au32[0];
|
---|
9229 | pFpuCtx->aRegs[i].au32[1] = paRegs[i].au32[1];
|
---|
9230 | pFpuCtx->aRegs[i].au32[2] = paRegs[i].au16[4];
|
---|
9231 | pFpuCtx->aRegs[i].au32[3] = 0;
|
---|
9232 | }
|
---|
9233 |
|
---|
9234 | rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)uPtr.pv, IEM_ACCESS_DATA_R);
|
---|
9235 | if (rcStrict != VINF_SUCCESS)
|
---|
9236 | return rcStrict;
|
---|
9237 |
|
---|
9238 | iemHlpUsedFpu(pVCpu);
|
---|
9239 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
9240 | return VINF_SUCCESS;
|
---|
9241 | }
|
---|
9242 |
|
---|
9243 |
|
---|
9244 | /**
|
---|
9245 | * Implements 'FLDCW'.
|
---|
9246 | *
|
---|
9247 | * @param u16Fcw The new FCW.
|
---|
9248 | */
|
---|
9249 | IEM_CIMPL_DEF_1(iemCImpl_fldcw, uint16_t, u16Fcw)
|
---|
9250 | {
|
---|
9251 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87);
|
---|
9252 |
|
---|
9253 | /** @todo Testcase: Check what happens when trying to load X86_FCW_PC_RSVD. */
|
---|
9254 | /** @todo Testcase: Try see what happens when trying to set undefined bits
|
---|
9255 | * (other than 6 and 7). Currently ignoring them. */
|
---|
9256 | /** @todo Testcase: Test that it raises and loweres the FPU exception bits
|
---|
9257 | * according to FSW. (This is was is currently implemented.) */
|
---|
9258 | PX86FXSTATE pFpuCtx = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
9259 | pFpuCtx->FCW = u16Fcw & ~X86_FCW_ZERO_MASK;
|
---|
9260 | iemFpuRecalcExceptionStatus(pFpuCtx);
|
---|
9261 |
|
---|
9262 | /* Note: C0, C1, C2 and C3 are documented as undefined, we leave them untouched! */
|
---|
9263 | iemHlpUsedFpu(pVCpu);
|
---|
9264 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
9265 | return VINF_SUCCESS;
|
---|
9266 | }
|
---|
9267 |
|
---|
9268 |
|
---|
9269 |
|
---|
9270 | /**
|
---|
9271 | * Implements the underflow case of fxch.
|
---|
9272 | *
|
---|
9273 | * @param iStReg The other stack register.
|
---|
9274 | */
|
---|
9275 | IEM_CIMPL_DEF_1(iemCImpl_fxch_underflow, uint8_t, iStReg)
|
---|
9276 | {
|
---|
9277 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87);
|
---|
9278 |
|
---|
9279 | PX86FXSTATE pFpuCtx = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
9280 | unsigned const iReg1 = X86_FSW_TOP_GET(pFpuCtx->FSW);
|
---|
9281 | unsigned const iReg2 = (iReg1 + iStReg) & X86_FSW_TOP_SMASK;
|
---|
9282 | Assert(!(RT_BIT(iReg1) & pFpuCtx->FTW) || !(RT_BIT(iReg2) & pFpuCtx->FTW));
|
---|
9283 |
|
---|
9284 | /** @todo Testcase: fxch underflow. Making assumptions that underflowed
|
---|
9285 | * registers are read as QNaN and then exchanged. This could be
|
---|
9286 | * wrong... */
|
---|
9287 | if (pFpuCtx->FCW & X86_FCW_IM)
|
---|
9288 | {
|
---|
9289 | if (RT_BIT(iReg1) & pFpuCtx->FTW)
|
---|
9290 | {
|
---|
9291 | if (RT_BIT(iReg2) & pFpuCtx->FTW)
|
---|
9292 | iemFpuStoreQNan(&pFpuCtx->aRegs[0].r80);
|
---|
9293 | else
|
---|
9294 | pFpuCtx->aRegs[0].r80 = pFpuCtx->aRegs[iStReg].r80;
|
---|
9295 | iemFpuStoreQNan(&pFpuCtx->aRegs[iStReg].r80);
|
---|
9296 | }
|
---|
9297 | else
|
---|
9298 | {
|
---|
9299 | pFpuCtx->aRegs[iStReg].r80 = pFpuCtx->aRegs[0].r80;
|
---|
9300 | iemFpuStoreQNan(&pFpuCtx->aRegs[0].r80);
|
---|
9301 | }
|
---|
9302 | pFpuCtx->FSW &= ~X86_FSW_C_MASK;
|
---|
9303 | pFpuCtx->FSW |= X86_FSW_C1 | X86_FSW_IE | X86_FSW_SF;
|
---|
9304 | }
|
---|
9305 | else
|
---|
9306 | {
|
---|
9307 | /* raise underflow exception, don't change anything. */
|
---|
9308 | pFpuCtx->FSW &= ~(X86_FSW_TOP_MASK | X86_FSW_XCPT_MASK);
|
---|
9309 | pFpuCtx->FSW |= X86_FSW_C1 | X86_FSW_IE | X86_FSW_SF | X86_FSW_ES | X86_FSW_B;
|
---|
9310 | }
|
---|
9311 |
|
---|
9312 | iemFpuUpdateOpcodeAndIpWorker(pVCpu, pFpuCtx);
|
---|
9313 | iemHlpUsedFpu(pVCpu);
|
---|
9314 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
9315 | return VINF_SUCCESS;
|
---|
9316 | }
|
---|
9317 |
|
---|
9318 |
|
---|
9319 | /**
|
---|
9320 | * Implements 'FCOMI', 'FCOMIP', 'FUCOMI', and 'FUCOMIP'.
|
---|
9321 | *
|
---|
9322 | * @param cToAdd 1 or 7.
|
---|
9323 | */
|
---|
9324 | IEM_CIMPL_DEF_3(iemCImpl_fcomi_fucomi, uint8_t, iStReg, PFNIEMAIMPLFPUR80EFL, pfnAImpl, bool, fPop)
|
---|
9325 | {
|
---|
9326 | Assert(iStReg < 8);
|
---|
9327 | IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87);
|
---|
9328 |
|
---|
9329 | /*
|
---|
9330 | * Raise exceptions.
|
---|
9331 | */
|
---|
9332 | if (pVCpu->cpum.GstCtx.cr0 & (X86_CR0_EM | X86_CR0_TS))
|
---|
9333 | return iemRaiseDeviceNotAvailable(pVCpu);
|
---|
9334 |
|
---|
9335 | PX86FXSTATE pFpuCtx = &pVCpu->cpum.GstCtx.XState.x87;
|
---|
9336 | uint16_t u16Fsw = pFpuCtx->FSW;
|
---|
9337 | if (u16Fsw & X86_FSW_ES)
|
---|
9338 | return iemRaiseMathFault(pVCpu);
|
---|
9339 |
|
---|
9340 | /*
|
---|
9341 | * Check if any of the register accesses causes #SF + #IA.
|
---|
9342 | */
|
---|
9343 | unsigned const iReg1 = X86_FSW_TOP_GET(u16Fsw);
|
---|
9344 | unsigned const iReg2 = (iReg1 + iStReg) & X86_FSW_TOP_SMASK;
|
---|
9345 | if ((pFpuCtx->FTW & (RT_BIT(iReg1) | RT_BIT(iReg2))) == (RT_BIT(iReg1) | RT_BIT(iReg2)))
|
---|
9346 | {
|
---|
9347 | uint32_t u32Eflags = pfnAImpl(pFpuCtx, &u16Fsw, &pFpuCtx->aRegs[0].r80, &pFpuCtx->aRegs[iStReg].r80);
|
---|
9348 | NOREF(u32Eflags);
|
---|
9349 |
|
---|
9350 | pFpuCtx->FSW &= ~X86_FSW_C1;
|
---|
9351 | pFpuCtx->FSW |= u16Fsw & ~X86_FSW_TOP_MASK;
|
---|
9352 | if ( !(u16Fsw & X86_FSW_IE)
|
---|
9353 | || (pFpuCtx->FCW & X86_FCW_IM) )
|
---|
9354 | {
|
---|
9355 | pVCpu->cpum.GstCtx.eflags.u &= ~(X86_EFL_OF | X86_EFL_SF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_PF | X86_EFL_CF);
|
---|
9356 | pVCpu->cpum.GstCtx.eflags.u |= pVCpu->cpum.GstCtx.eflags.u & (X86_EFL_ZF | X86_EFL_PF | X86_EFL_CF);
|
---|
9357 | }
|
---|
9358 | }
|
---|
9359 | else if (pFpuCtx->FCW & X86_FCW_IM)
|
---|
9360 | {
|
---|
9361 | /* Masked underflow. */
|
---|
9362 | pFpuCtx->FSW &= ~X86_FSW_C1;
|
---|
9363 | pFpuCtx->FSW |= X86_FSW_IE | X86_FSW_SF;
|
---|
9364 | pVCpu->cpum.GstCtx.eflags.u &= ~(X86_EFL_OF | X86_EFL_SF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_PF | X86_EFL_CF);
|
---|
9365 | pVCpu->cpum.GstCtx.eflags.u |= X86_EFL_ZF | X86_EFL_PF | X86_EFL_CF;
|
---|
9366 | }
|
---|
9367 | else
|
---|
9368 | {
|
---|
9369 | /* Raise underflow - don't touch EFLAGS or TOP. */
|
---|
9370 | pFpuCtx->FSW &= ~X86_FSW_C1;
|
---|
9371 | pFpuCtx->FSW |= X86_FSW_IE | X86_FSW_SF | X86_FSW_ES | X86_FSW_B;
|
---|
9372 | fPop = false;
|
---|
9373 | }
|
---|
9374 |
|
---|
9375 | /*
|
---|
9376 | * Pop if necessary.
|
---|
9377 | */
|
---|
9378 | if (fPop)
|
---|
9379 | {
|
---|
9380 | pFpuCtx->FTW &= ~RT_BIT(iReg1);
|
---|
9381 | pFpuCtx->FSW &= X86_FSW_TOP_MASK;
|
---|
9382 | pFpuCtx->FSW |= ((iReg1 + 7) & X86_FSW_TOP_SMASK) << X86_FSW_TOP_SHIFT;
|
---|
9383 | }
|
---|
9384 |
|
---|
9385 | iemFpuUpdateOpcodeAndIpWorker(pVCpu, pFpuCtx);
|
---|
9386 | iemHlpUsedFpu(pVCpu);
|
---|
9387 | iemRegAddToRipAndClearRF(pVCpu, cbInstr);
|
---|
9388 | return VINF_SUCCESS;
|
---|
9389 | }
|
---|
9390 |
|
---|
9391 | /** @} */
|
---|
9392 |
|
---|