VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/IEMAllCImpl.cpp.h@ 92780

Last change on this file since 92780 was 92745, checked in by vboxsync, 3 years ago

VMM/IEM,DBGF: Hook INT3 emulation IEM up with DBGF to make breakpoints work a little bit.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 336.2 KB
Line 
1/* $Id: IEMAllCImpl.cpp.h 92745 2021-12-03 20:38:46Z 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 */
36static 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 */
126DECLINLINE(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 */
145static 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 */
180static 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 */
197static 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 */
213static 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 */
231DECLINLINE(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 */
245IEM_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 */
316IEM_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 */
396IEM_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 */
467IEM_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 */
541IEM_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 */
606IEM_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 */
772IEM_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 */
798IEM_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 */
827IEM_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 */
853IEM_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 */
882IEM_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 */
908IEM_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 */
940IEM_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 */
984IEM_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 */
1054IEM_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 */
1711IEM_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 */
1773IEM_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 */
1948IEM_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 */
2171IEM_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 */
2590IEM_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 */
2664IEM_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 */
2778IEM_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 */
2830IEM_CIMPL_DEF_2(iemCImpl_int, uint8_t, u8Int, IEMINT, enmInt)
2831{
2832 Assert(pVCpu->iem.s.cXcptRecursions == 0);
2833
2834 /*
2835 * We must check if this INT3 might belong to DBGF before raising a #BP.
2836 */
2837 if (u8Int == 3)
2838 {
2839 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
2840 if (pVM->dbgf.ro.cEnabledInt3Breakpoints == 0)
2841 { /* likely: No vbox debugger breakpoints */ }
2842 else
2843 {
2844 VBOXSTRICTRC rcStrict = DBGFTrap03Handler(pVM, pVCpu, CPUMCTX2CORE(&pVCpu->cpum.GstCtx));
2845 Log(("iemCImpl_int: DBGFTrap03Handler -> %Rrc\n", VBOXSTRICTRC_VAL(rcStrict) ));
2846 if (rcStrict != VINF_EM_RAW_GUEST_TRAP)
2847 return iemSetPassUpStatus(pVCpu, rcStrict);
2848 }
2849 }
2850 return iemRaiseXcptOrInt(pVCpu,
2851 cbInstr,
2852 u8Int,
2853 IEM_XCPT_FLAGS_T_SOFT_INT | enmInt,
2854 0,
2855 0);
2856}
2857
2858
2859/**
2860 * Implements iret for real mode and V8086 mode.
2861 *
2862 * @param enmEffOpSize The effective operand size.
2863 */
2864IEM_CIMPL_DEF_1(iemCImpl_iret_real_v8086, IEMMODE, enmEffOpSize)
2865{
2866 X86EFLAGS Efl;
2867 Efl.u = IEMMISC_GET_EFL(pVCpu);
2868 NOREF(cbInstr);
2869
2870 /*
2871 * iret throws an exception if VME isn't enabled.
2872 */
2873 if ( Efl.Bits.u1VM
2874 && Efl.Bits.u2IOPL != 3
2875 && !(pVCpu->cpum.GstCtx.cr4 & X86_CR4_VME))
2876 return iemRaiseGeneralProtectionFault0(pVCpu);
2877
2878 /*
2879 * Do the stack bits, but don't commit RSP before everything checks
2880 * out right.
2881 */
2882 Assert(enmEffOpSize == IEMMODE_32BIT || enmEffOpSize == IEMMODE_16BIT);
2883 VBOXSTRICTRC rcStrict;
2884 RTCPTRUNION uFrame;
2885 uint16_t uNewCs;
2886 uint32_t uNewEip;
2887 uint32_t uNewFlags;
2888 uint64_t uNewRsp;
2889 if (enmEffOpSize == IEMMODE_32BIT)
2890 {
2891 rcStrict = iemMemStackPopBeginSpecial(pVCpu, 12, &uFrame.pv, &uNewRsp);
2892 if (rcStrict != VINF_SUCCESS)
2893 return rcStrict;
2894 uNewEip = uFrame.pu32[0];
2895 if (uNewEip > UINT16_MAX)
2896 return iemRaiseGeneralProtectionFault0(pVCpu);
2897
2898 uNewCs = (uint16_t)uFrame.pu32[1];
2899 uNewFlags = uFrame.pu32[2];
2900 uNewFlags &= X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF
2901 | X86_EFL_TF | X86_EFL_IF | X86_EFL_DF | X86_EFL_OF | X86_EFL_IOPL | X86_EFL_NT
2902 | X86_EFL_RF /*| X86_EFL_VM*/ | X86_EFL_AC /*|X86_EFL_VIF*/ /*|X86_EFL_VIP*/
2903 | X86_EFL_ID;
2904 if (IEM_GET_TARGET_CPU(pVCpu) <= IEMTARGETCPU_386)
2905 uNewFlags &= ~(X86_EFL_AC | X86_EFL_ID | X86_EFL_VIF | X86_EFL_VIP);
2906 uNewFlags |= Efl.u & (X86_EFL_VM | X86_EFL_VIF | X86_EFL_VIP | X86_EFL_1);
2907 }
2908 else
2909 {
2910 rcStrict = iemMemStackPopBeginSpecial(pVCpu, 6, &uFrame.pv, &uNewRsp);
2911 if (rcStrict != VINF_SUCCESS)
2912 return rcStrict;
2913 uNewEip = uFrame.pu16[0];
2914 uNewCs = uFrame.pu16[1];
2915 uNewFlags = uFrame.pu16[2];
2916 uNewFlags &= X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF
2917 | X86_EFL_TF | X86_EFL_IF | X86_EFL_DF | X86_EFL_OF | X86_EFL_IOPL | X86_EFL_NT;
2918 uNewFlags |= Efl.u & ((UINT32_C(0xffff0000) | X86_EFL_1) & ~X86_EFL_RF);
2919 /** @todo The intel pseudo code does not indicate what happens to
2920 * reserved flags. We just ignore them. */
2921 /* Ancient CPU adjustments: See iemCImpl_popf. */
2922 if (IEM_GET_TARGET_CPU(pVCpu) == IEMTARGETCPU_286)
2923 uNewFlags &= ~(X86_EFL_NT | X86_EFL_IOPL);
2924 }
2925 rcStrict = iemMemStackPopDoneSpecial(pVCpu, uFrame.pv);
2926 if (RT_LIKELY(rcStrict == VINF_SUCCESS))
2927 { /* extremely likely */ }
2928 else
2929 return rcStrict;
2930
2931 /** @todo Check how this is supposed to work if sp=0xfffe. */
2932 Log7(("iemCImpl_iret_real_v8086: uNewCs=%#06x uNewRip=%#010x uNewFlags=%#x uNewRsp=%#18llx\n",
2933 uNewCs, uNewEip, uNewFlags, uNewRsp));
2934
2935 /*
2936 * Check the limit of the new EIP.
2937 */
2938 /** @todo Only the AMD pseudo code check the limit here, what's
2939 * right? */
2940 if (uNewEip > pVCpu->cpum.GstCtx.cs.u32Limit)
2941 return iemRaiseSelectorBounds(pVCpu, X86_SREG_CS, IEM_ACCESS_INSTRUCTION);
2942
2943 /*
2944 * V8086 checks and flag adjustments
2945 */
2946 if (Efl.Bits.u1VM)
2947 {
2948 if (Efl.Bits.u2IOPL == 3)
2949 {
2950 /* Preserve IOPL and clear RF. */
2951 uNewFlags &= ~(X86_EFL_IOPL | X86_EFL_RF);
2952 uNewFlags |= Efl.u & (X86_EFL_IOPL);
2953 }
2954 else if ( enmEffOpSize == IEMMODE_16BIT
2955 && ( !(uNewFlags & X86_EFL_IF)
2956 || !Efl.Bits.u1VIP )
2957 && !(uNewFlags & X86_EFL_TF) )
2958 {
2959 /* Move IF to VIF, clear RF and preserve IF and IOPL.*/
2960 uNewFlags &= ~X86_EFL_VIF;
2961 uNewFlags |= (uNewFlags & X86_EFL_IF) << (19 - 9);
2962 uNewFlags &= ~(X86_EFL_IF | X86_EFL_IOPL | X86_EFL_RF);
2963 uNewFlags |= Efl.u & (X86_EFL_IF | X86_EFL_IOPL);
2964 }
2965 else
2966 return iemRaiseGeneralProtectionFault0(pVCpu);
2967 Log7(("iemCImpl_iret_real_v8086: u1VM=1: adjusted uNewFlags=%#x\n", uNewFlags));
2968 }
2969
2970 /*
2971 * Commit the operation.
2972 */
2973#ifdef DBGFTRACE_ENABLED
2974 RTTraceBufAddMsgF(pVCpu->CTX_SUFF(pVM)->CTX_SUFF(hTraceBuf), "iret/rm %04x:%04x -> %04x:%04x %x %04llx",
2975 pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.eip, uNewCs, uNewEip, uNewFlags, uNewRsp);
2976#endif
2977 pVCpu->cpum.GstCtx.rsp = uNewRsp;
2978 pVCpu->cpum.GstCtx.rip = uNewEip;
2979 pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
2980 pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
2981 pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
2982 pVCpu->cpum.GstCtx.cs.u64Base = (uint32_t)uNewCs << 4;
2983 /** @todo do we load attribs and limit as well? */
2984 Assert(uNewFlags & X86_EFL_1);
2985 IEMMISC_SET_EFL(pVCpu, uNewFlags);
2986
2987 /* Flush the prefetch buffer. */
2988#ifdef IEM_WITH_CODE_TLB
2989 pVCpu->iem.s.pbInstrBuf = NULL;
2990#else
2991 pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
2992#endif
2993
2994 return VINF_SUCCESS;
2995}
2996
2997
2998/**
2999 * Loads a segment register when entering V8086 mode.
3000 *
3001 * @param pSReg The segment register.
3002 * @param uSeg The segment to load.
3003 */
3004static void iemCImplCommonV8086LoadSeg(PCPUMSELREG pSReg, uint16_t uSeg)
3005{
3006 pSReg->Sel = uSeg;
3007 pSReg->ValidSel = uSeg;
3008 pSReg->fFlags = CPUMSELREG_FLAGS_VALID;
3009 pSReg->u64Base = (uint32_t)uSeg << 4;
3010 pSReg->u32Limit = 0xffff;
3011 pSReg->Attr.u = X86_SEL_TYPE_RW_ACC | RT_BIT(4) /*!sys*/ | RT_BIT(7) /*P*/ | (3 /*DPL*/ << 5); /* VT-x wants 0xf3 */
3012 /** @todo Testcase: Check if VT-x really needs this and what it does itself when
3013 * IRET'ing to V8086. */
3014}
3015
3016
3017/**
3018 * Implements iret for protected mode returning to V8086 mode.
3019 *
3020 * @param uNewEip The new EIP.
3021 * @param uNewCs The new CS.
3022 * @param uNewFlags The new EFLAGS.
3023 * @param uNewRsp The RSP after the initial IRET frame.
3024 *
3025 * @note This can only be a 32-bit iret du to the X86_EFL_VM position.
3026 */
3027IEM_CIMPL_DEF_4(iemCImpl_iret_prot_v8086, uint32_t, uNewEip, uint16_t, uNewCs, uint32_t, uNewFlags, uint64_t, uNewRsp)
3028{
3029 RT_NOREF_PV(cbInstr);
3030 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SREG_MASK);
3031
3032 /*
3033 * Pop the V8086 specific frame bits off the stack.
3034 */
3035 VBOXSTRICTRC rcStrict;
3036 RTCPTRUNION uFrame;
3037 rcStrict = iemMemStackPopContinueSpecial(pVCpu, 24, &uFrame.pv, &uNewRsp);
3038 if (rcStrict != VINF_SUCCESS)
3039 return rcStrict;
3040 uint32_t uNewEsp = uFrame.pu32[0];
3041 uint16_t uNewSs = uFrame.pu32[1];
3042 uint16_t uNewEs = uFrame.pu32[2];
3043 uint16_t uNewDs = uFrame.pu32[3];
3044 uint16_t uNewFs = uFrame.pu32[4];
3045 uint16_t uNewGs = uFrame.pu32[5];
3046 rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)uFrame.pv, IEM_ACCESS_STACK_R); /* don't use iemMemStackPopCommitSpecial here. */
3047 if (rcStrict != VINF_SUCCESS)
3048 return rcStrict;
3049
3050 /*
3051 * Commit the operation.
3052 */
3053 uNewFlags &= X86_EFL_LIVE_MASK;
3054 uNewFlags |= X86_EFL_RA1_MASK;
3055#ifdef DBGFTRACE_ENABLED
3056 RTTraceBufAddMsgF(pVCpu->CTX_SUFF(pVM)->CTX_SUFF(hTraceBuf), "iret/p/v %04x:%08x -> %04x:%04x %x %04x:%04x",
3057 pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.eip, uNewCs, uNewEip, uNewFlags, uNewSs, uNewEsp);
3058#endif
3059 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));
3060
3061 IEMMISC_SET_EFL(pVCpu, uNewFlags);
3062 iemCImplCommonV8086LoadSeg(&pVCpu->cpum.GstCtx.cs, uNewCs);
3063 iemCImplCommonV8086LoadSeg(&pVCpu->cpum.GstCtx.ss, uNewSs);
3064 iemCImplCommonV8086LoadSeg(&pVCpu->cpum.GstCtx.es, uNewEs);
3065 iemCImplCommonV8086LoadSeg(&pVCpu->cpum.GstCtx.ds, uNewDs);
3066 iemCImplCommonV8086LoadSeg(&pVCpu->cpum.GstCtx.fs, uNewFs);
3067 iemCImplCommonV8086LoadSeg(&pVCpu->cpum.GstCtx.gs, uNewGs);
3068 pVCpu->cpum.GstCtx.rip = (uint16_t)uNewEip;
3069 pVCpu->cpum.GstCtx.rsp = uNewEsp; /** @todo check this out! */
3070 pVCpu->iem.s.uCpl = 3;
3071
3072 /* Flush the prefetch buffer. */
3073#ifdef IEM_WITH_CODE_TLB
3074 pVCpu->iem.s.pbInstrBuf = NULL;
3075#else
3076 pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
3077#endif
3078
3079 return VINF_SUCCESS;
3080}
3081
3082
3083/**
3084 * Implements iret for protected mode returning via a nested task.
3085 *
3086 * @param enmEffOpSize The effective operand size.
3087 */
3088IEM_CIMPL_DEF_1(iemCImpl_iret_prot_NestedTask, IEMMODE, enmEffOpSize)
3089{
3090 Log7(("iemCImpl_iret_prot_NestedTask:\n"));
3091#ifndef IEM_IMPLEMENTS_TASKSWITCH
3092 IEM_RETURN_ASPECT_NOT_IMPLEMENTED();
3093#else
3094 RT_NOREF_PV(enmEffOpSize);
3095
3096 /*
3097 * Read the segment selector in the link-field of the current TSS.
3098 */
3099 RTSEL uSelRet;
3100 VBOXSTRICTRC rcStrict = iemMemFetchSysU16(pVCpu, &uSelRet, UINT8_MAX, pVCpu->cpum.GstCtx.tr.u64Base);
3101 if (rcStrict != VINF_SUCCESS)
3102 return rcStrict;
3103
3104 /*
3105 * Fetch the returning task's TSS descriptor from the GDT.
3106 */
3107 if (uSelRet & X86_SEL_LDT)
3108 {
3109 Log(("iret_prot_NestedTask TSS not in LDT. uSelRet=%04x -> #TS\n", uSelRet));
3110 return iemRaiseTaskSwitchFaultBySelector(pVCpu, uSelRet);
3111 }
3112
3113 IEMSELDESC TssDesc;
3114 rcStrict = iemMemFetchSelDesc(pVCpu, &TssDesc, uSelRet, X86_XCPT_GP);
3115 if (rcStrict != VINF_SUCCESS)
3116 return rcStrict;
3117
3118 if (TssDesc.Legacy.Gate.u1DescType)
3119 {
3120 Log(("iret_prot_NestedTask Invalid TSS type. uSelRet=%04x -> #TS\n", uSelRet));
3121 return iemRaiseTaskSwitchFaultBySelector(pVCpu, uSelRet & X86_SEL_MASK_OFF_RPL);
3122 }
3123
3124 if ( TssDesc.Legacy.Gate.u4Type != X86_SEL_TYPE_SYS_286_TSS_BUSY
3125 && TssDesc.Legacy.Gate.u4Type != X86_SEL_TYPE_SYS_386_TSS_BUSY)
3126 {
3127 Log(("iret_prot_NestedTask TSS is not busy. uSelRet=%04x DescType=%#x -> #TS\n", uSelRet, TssDesc.Legacy.Gate.u4Type));
3128 return iemRaiseTaskSwitchFaultBySelector(pVCpu, uSelRet & X86_SEL_MASK_OFF_RPL);
3129 }
3130
3131 if (!TssDesc.Legacy.Gate.u1Present)
3132 {
3133 Log(("iret_prot_NestedTask TSS is not present. uSelRet=%04x -> #NP\n", uSelRet));
3134 return iemRaiseSelectorNotPresentBySelector(pVCpu, uSelRet & X86_SEL_MASK_OFF_RPL);
3135 }
3136
3137 uint32_t uNextEip = pVCpu->cpum.GstCtx.eip + cbInstr;
3138 return iemTaskSwitch(pVCpu, IEMTASKSWITCH_IRET, uNextEip, 0 /* fFlags */, 0 /* uErr */,
3139 0 /* uCr2 */, uSelRet, &TssDesc);
3140#endif
3141}
3142
3143
3144/**
3145 * Implements iret for protected mode
3146 *
3147 * @param enmEffOpSize The effective operand size.
3148 */
3149IEM_CIMPL_DEF_1(iemCImpl_iret_prot, IEMMODE, enmEffOpSize)
3150{
3151 NOREF(cbInstr);
3152 Assert(enmEffOpSize == IEMMODE_32BIT || enmEffOpSize == IEMMODE_16BIT);
3153
3154 /*
3155 * Nested task return.
3156 */
3157 if (pVCpu->cpum.GstCtx.eflags.Bits.u1NT)
3158 return IEM_CIMPL_CALL_1(iemCImpl_iret_prot_NestedTask, enmEffOpSize);
3159
3160 /*
3161 * Normal return.
3162 *
3163 * Do the stack bits, but don't commit RSP before everything checks
3164 * out right.
3165 */
3166 Assert(enmEffOpSize == IEMMODE_32BIT || enmEffOpSize == IEMMODE_16BIT);
3167 VBOXSTRICTRC rcStrict;
3168 RTCPTRUNION uFrame;
3169 uint16_t uNewCs;
3170 uint32_t uNewEip;
3171 uint32_t uNewFlags;
3172 uint64_t uNewRsp;
3173 if (enmEffOpSize == IEMMODE_32BIT)
3174 {
3175 rcStrict = iemMemStackPopBeginSpecial(pVCpu, 12, &uFrame.pv, &uNewRsp);
3176 if (rcStrict != VINF_SUCCESS)
3177 return rcStrict;
3178 uNewEip = uFrame.pu32[0];
3179 uNewCs = (uint16_t)uFrame.pu32[1];
3180 uNewFlags = uFrame.pu32[2];
3181 }
3182 else
3183 {
3184 rcStrict = iemMemStackPopBeginSpecial(pVCpu, 6, &uFrame.pv, &uNewRsp);
3185 if (rcStrict != VINF_SUCCESS)
3186 return rcStrict;
3187 uNewEip = uFrame.pu16[0];
3188 uNewCs = uFrame.pu16[1];
3189 uNewFlags = uFrame.pu16[2];
3190 }
3191 rcStrict = iemMemStackPopDoneSpecial(pVCpu, (void *)uFrame.pv); /* don't use iemMemStackPopCommitSpecial here. */
3192 if (RT_LIKELY(rcStrict == VINF_SUCCESS))
3193 { /* extremely likely */ }
3194 else
3195 return rcStrict;
3196 Log7(("iemCImpl_iret_prot: uNewCs=%#06x uNewEip=%#010x uNewFlags=%#x uNewRsp=%#18llx uCpl=%u\n", uNewCs, uNewEip, uNewFlags, uNewRsp, pVCpu->iem.s.uCpl));
3197
3198 /*
3199 * We're hopefully not returning to V8086 mode...
3200 */
3201 if ( (uNewFlags & X86_EFL_VM)
3202 && pVCpu->iem.s.uCpl == 0)
3203 {
3204 Assert(enmEffOpSize == IEMMODE_32BIT);
3205 return IEM_CIMPL_CALL_4(iemCImpl_iret_prot_v8086, uNewEip, uNewCs, uNewFlags, uNewRsp);
3206 }
3207
3208 /*
3209 * Protected mode.
3210 */
3211 /* Read the CS descriptor. */
3212 if (!(uNewCs & X86_SEL_MASK_OFF_RPL))
3213 {
3214 Log(("iret %04x:%08x -> invalid CS selector, #GP(0)\n", uNewCs, uNewEip));
3215 return iemRaiseGeneralProtectionFault0(pVCpu);
3216 }
3217
3218 IEMSELDESC DescCS;
3219 rcStrict = iemMemFetchSelDesc(pVCpu, &DescCS, uNewCs, X86_XCPT_GP);
3220 if (rcStrict != VINF_SUCCESS)
3221 {
3222 Log(("iret %04x:%08x - rcStrict=%Rrc when fetching CS\n", uNewCs, uNewEip, VBOXSTRICTRC_VAL(rcStrict)));
3223 return rcStrict;
3224 }
3225
3226 /* Must be a code descriptor. */
3227 if (!DescCS.Legacy.Gen.u1DescType)
3228 {
3229 Log(("iret %04x:%08x - CS is system segment (%#x) -> #GP\n", uNewCs, uNewEip, DescCS.Legacy.Gen.u4Type));
3230 return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
3231 }
3232 if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE))
3233 {
3234 Log(("iret %04x:%08x - not code segment (%#x) -> #GP\n", uNewCs, uNewEip, DescCS.Legacy.Gen.u4Type));
3235 return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
3236 }
3237
3238 /* Privilege checks. */
3239 if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF))
3240 {
3241 if ((uNewCs & X86_SEL_RPL) != DescCS.Legacy.Gen.u2Dpl)
3242 {
3243 Log(("iret %04x:%08x - RPL != DPL (%d) -> #GP\n", uNewCs, uNewEip, DescCS.Legacy.Gen.u2Dpl));
3244 return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
3245 }
3246 }
3247 else if ((uNewCs & X86_SEL_RPL) < DescCS.Legacy.Gen.u2Dpl)
3248 {
3249 Log(("iret %04x:%08x - RPL < DPL (%d) -> #GP\n", uNewCs, uNewEip, DescCS.Legacy.Gen.u2Dpl));
3250 return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
3251 }
3252 if ((uNewCs & X86_SEL_RPL) < pVCpu->iem.s.uCpl)
3253 {
3254 Log(("iret %04x:%08x - RPL < CPL (%d) -> #GP\n", uNewCs, uNewEip, pVCpu->iem.s.uCpl));
3255 return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
3256 }
3257
3258 /* Present? */
3259 if (!DescCS.Legacy.Gen.u1Present)
3260 {
3261 Log(("iret %04x:%08x - CS not present -> #NP\n", uNewCs, uNewEip));
3262 return iemRaiseSelectorNotPresentBySelector(pVCpu, uNewCs);
3263 }
3264
3265 uint32_t cbLimitCS = X86DESC_LIMIT_G(&DescCS.Legacy);
3266
3267 /*
3268 * Return to outer level?
3269 */
3270 if ((uNewCs & X86_SEL_RPL) != pVCpu->iem.s.uCpl)
3271 {
3272 uint16_t uNewSS;
3273 uint32_t uNewESP;
3274 if (enmEffOpSize == IEMMODE_32BIT)
3275 {
3276 rcStrict = iemMemStackPopContinueSpecial(pVCpu, 8, &uFrame.pv, &uNewRsp);
3277 if (rcStrict != VINF_SUCCESS)
3278 return rcStrict;
3279/** @todo We might be popping a 32-bit ESP from the IRET frame, but whether
3280 * 16-bit or 32-bit are being loaded into SP depends on the D/B
3281 * bit of the popped SS selector it turns out. */
3282 uNewESP = uFrame.pu32[0];
3283 uNewSS = (uint16_t)uFrame.pu32[1];
3284 }
3285 else
3286 {
3287 rcStrict = iemMemStackPopContinueSpecial(pVCpu, 4, &uFrame.pv, &uNewRsp);
3288 if (rcStrict != VINF_SUCCESS)
3289 return rcStrict;
3290 uNewESP = uFrame.pu16[0];
3291 uNewSS = uFrame.pu16[1];
3292 }
3293 rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)uFrame.pv, IEM_ACCESS_STACK_R);
3294 if (rcStrict != VINF_SUCCESS)
3295 return rcStrict;
3296 Log7(("iemCImpl_iret_prot: uNewSS=%#06x uNewESP=%#010x\n", uNewSS, uNewESP));
3297
3298 /* Read the SS descriptor. */
3299 if (!(uNewSS & X86_SEL_MASK_OFF_RPL))
3300 {
3301 Log(("iret %04x:%08x/%04x:%08x -> invalid SS selector, #GP(0)\n", uNewCs, uNewEip, uNewSS, uNewESP));
3302 return iemRaiseGeneralProtectionFault0(pVCpu);
3303 }
3304
3305 IEMSELDESC DescSS;
3306 rcStrict = iemMemFetchSelDesc(pVCpu, &DescSS, uNewSS, X86_XCPT_GP); /** @todo Correct exception? */
3307 if (rcStrict != VINF_SUCCESS)
3308 {
3309 Log(("iret %04x:%08x/%04x:%08x - %Rrc when fetching SS\n",
3310 uNewCs, uNewEip, uNewSS, uNewESP, VBOXSTRICTRC_VAL(rcStrict)));
3311 return rcStrict;
3312 }
3313
3314 /* Privilege checks. */
3315 if ((uNewSS & X86_SEL_RPL) != (uNewCs & X86_SEL_RPL))
3316 {
3317 Log(("iret %04x:%08x/%04x:%08x -> SS.RPL != CS.RPL -> #GP\n", uNewCs, uNewEip, uNewSS, uNewESP));
3318 return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSS);
3319 }
3320 if (DescSS.Legacy.Gen.u2Dpl != (uNewCs & X86_SEL_RPL))
3321 {
3322 Log(("iret %04x:%08x/%04x:%08x -> SS.DPL (%d) != CS.RPL -> #GP\n",
3323 uNewCs, uNewEip, uNewSS, uNewESP, DescSS.Legacy.Gen.u2Dpl));
3324 return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSS);
3325 }
3326
3327 /* Must be a writeable data segment descriptor. */
3328 if (!DescSS.Legacy.Gen.u1DescType)
3329 {
3330 Log(("iret %04x:%08x/%04x:%08x -> SS is system segment (%#x) -> #GP\n",
3331 uNewCs, uNewEip, uNewSS, uNewESP, DescSS.Legacy.Gen.u4Type));
3332 return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSS);
3333 }
3334 if ((DescSS.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_WRITE)) != X86_SEL_TYPE_WRITE)
3335 {
3336 Log(("iret %04x:%08x/%04x:%08x - not writable data segment (%#x) -> #GP\n",
3337 uNewCs, uNewEip, uNewSS, uNewESP, DescSS.Legacy.Gen.u4Type));
3338 return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSS);
3339 }
3340
3341 /* Present? */
3342 if (!DescSS.Legacy.Gen.u1Present)
3343 {
3344 Log(("iret %04x:%08x/%04x:%08x -> SS not present -> #SS\n", uNewCs, uNewEip, uNewSS, uNewESP));
3345 return iemRaiseStackSelectorNotPresentBySelector(pVCpu, uNewSS);
3346 }
3347
3348 uint32_t cbLimitSs = X86DESC_LIMIT_G(&DescSS.Legacy);
3349
3350 /* Check EIP. */
3351 if (uNewEip > cbLimitCS)
3352 {
3353 Log(("iret %04x:%08x/%04x:%08x -> EIP is out of bounds (%#x) -> #GP(0)\n",
3354 uNewCs, uNewEip, uNewSS, uNewESP, cbLimitCS));
3355 /** @todo: Which is it, #GP(0) or #GP(sel)? */
3356 return iemRaiseSelectorBoundsBySelector(pVCpu, uNewCs);
3357 }
3358
3359 /*
3360 * Commit the changes, marking CS and SS accessed first since
3361 * that may fail.
3362 */
3363 if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
3364 {
3365 rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewCs);
3366 if (rcStrict != VINF_SUCCESS)
3367 return rcStrict;
3368 DescCS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
3369 }
3370 if (!(DescSS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
3371 {
3372 rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewSS);
3373 if (rcStrict != VINF_SUCCESS)
3374 return rcStrict;
3375 DescSS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
3376 }
3377
3378 uint32_t fEFlagsMask = X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF
3379 | X86_EFL_TF | X86_EFL_DF | X86_EFL_OF | X86_EFL_NT;
3380 if (enmEffOpSize != IEMMODE_16BIT)
3381 fEFlagsMask |= X86_EFL_RF | X86_EFL_AC | X86_EFL_ID;
3382 if (pVCpu->iem.s.uCpl == 0)
3383 fEFlagsMask |= X86_EFL_IF | X86_EFL_IOPL | X86_EFL_VIF | X86_EFL_VIP; /* VM is 0 */
3384 else if (pVCpu->iem.s.uCpl <= pVCpu->cpum.GstCtx.eflags.Bits.u2IOPL)
3385 fEFlagsMask |= X86_EFL_IF;
3386 if (IEM_GET_TARGET_CPU(pVCpu) <= IEMTARGETCPU_386)
3387 fEFlagsMask &= ~(X86_EFL_AC | X86_EFL_ID | X86_EFL_VIF | X86_EFL_VIP);
3388 uint32_t fEFlagsNew = IEMMISC_GET_EFL(pVCpu);
3389 fEFlagsNew &= ~fEFlagsMask;
3390 fEFlagsNew |= uNewFlags & fEFlagsMask;
3391#ifdef DBGFTRACE_ENABLED
3392 RTTraceBufAddMsgF(pVCpu->CTX_SUFF(pVM)->CTX_SUFF(hTraceBuf), "iret/%up%u %04x:%08x -> %04x:%04x %x %04x:%04x",
3393 pVCpu->iem.s.uCpl, uNewCs & X86_SEL_RPL, pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.eip,
3394 uNewCs, uNewEip, uNewFlags, uNewSS, uNewESP);
3395#endif
3396
3397 IEMMISC_SET_EFL(pVCpu, fEFlagsNew);
3398 pVCpu->cpum.GstCtx.rip = uNewEip;
3399 pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
3400 pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
3401 pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
3402 pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCS.Legacy);
3403 pVCpu->cpum.GstCtx.cs.u32Limit = cbLimitCS;
3404 pVCpu->cpum.GstCtx.cs.u64Base = X86DESC_BASE(&DescCS.Legacy);
3405 pVCpu->iem.s.enmCpuMode = iemCalcCpuMode(pVCpu);
3406
3407 pVCpu->cpum.GstCtx.ss.Sel = uNewSS;
3408 pVCpu->cpum.GstCtx.ss.ValidSel = uNewSS;
3409 pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
3410 pVCpu->cpum.GstCtx.ss.Attr.u = X86DESC_GET_HID_ATTR(&DescSS.Legacy);
3411 pVCpu->cpum.GstCtx.ss.u32Limit = cbLimitSs;
3412 pVCpu->cpum.GstCtx.ss.u64Base = X86DESC_BASE(&DescSS.Legacy);
3413 if (!pVCpu->cpum.GstCtx.ss.Attr.n.u1DefBig)
3414 pVCpu->cpum.GstCtx.sp = (uint16_t)uNewESP;
3415 else
3416 pVCpu->cpum.GstCtx.rsp = uNewESP;
3417
3418 pVCpu->iem.s.uCpl = uNewCs & X86_SEL_RPL;
3419 iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.ds);
3420 iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.es);
3421 iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.fs);
3422 iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCs & X86_SEL_RPL, &pVCpu->cpum.GstCtx.gs);
3423
3424 /* Done! */
3425
3426 }
3427 /*
3428 * Return to the same level.
3429 */
3430 else
3431 {
3432 /* Check EIP. */
3433 if (uNewEip > cbLimitCS)
3434 {
3435 Log(("iret %04x:%08x - EIP is out of bounds (%#x) -> #GP(0)\n", uNewCs, uNewEip, cbLimitCS));
3436 /** @todo: Which is it, #GP(0) or #GP(sel)? */
3437 return iemRaiseSelectorBoundsBySelector(pVCpu, uNewCs);
3438 }
3439
3440 /*
3441 * Commit the changes, marking CS first since it may fail.
3442 */
3443 if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
3444 {
3445 rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewCs);
3446 if (rcStrict != VINF_SUCCESS)
3447 return rcStrict;
3448 DescCS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
3449 }
3450
3451 X86EFLAGS NewEfl;
3452 NewEfl.u = IEMMISC_GET_EFL(pVCpu);
3453 uint32_t fEFlagsMask = X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF
3454 | X86_EFL_TF | X86_EFL_DF | X86_EFL_OF | X86_EFL_NT;
3455 if (enmEffOpSize != IEMMODE_16BIT)
3456 fEFlagsMask |= X86_EFL_RF | X86_EFL_AC | X86_EFL_ID;
3457 if (pVCpu->iem.s.uCpl == 0)
3458 fEFlagsMask |= X86_EFL_IF | X86_EFL_IOPL | X86_EFL_VIF | X86_EFL_VIP; /* VM is 0 */
3459 else if (pVCpu->iem.s.uCpl <= NewEfl.Bits.u2IOPL)
3460 fEFlagsMask |= X86_EFL_IF;
3461 if (IEM_GET_TARGET_CPU(pVCpu) <= IEMTARGETCPU_386)
3462 fEFlagsMask &= ~(X86_EFL_AC | X86_EFL_ID | X86_EFL_VIF | X86_EFL_VIP);
3463 NewEfl.u &= ~fEFlagsMask;
3464 NewEfl.u |= fEFlagsMask & uNewFlags;
3465#ifdef DBGFTRACE_ENABLED
3466 RTTraceBufAddMsgF(pVCpu->CTX_SUFF(pVM)->CTX_SUFF(hTraceBuf), "iret/%up %04x:%08x -> %04x:%04x %x %04x:%04llx",
3467 pVCpu->iem.s.uCpl, pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.eip,
3468 uNewCs, uNewEip, uNewFlags, pVCpu->cpum.GstCtx.ss.Sel, uNewRsp);
3469#endif
3470
3471 IEMMISC_SET_EFL(pVCpu, NewEfl.u);
3472 pVCpu->cpum.GstCtx.rip = uNewEip;
3473 pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
3474 pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
3475 pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
3476 pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCS.Legacy);
3477 pVCpu->cpum.GstCtx.cs.u32Limit = cbLimitCS;
3478 pVCpu->cpum.GstCtx.cs.u64Base = X86DESC_BASE(&DescCS.Legacy);
3479 pVCpu->iem.s.enmCpuMode = iemCalcCpuMode(pVCpu);
3480 if (!pVCpu->cpum.GstCtx.ss.Attr.n.u1DefBig)
3481 pVCpu->cpum.GstCtx.sp = (uint16_t)uNewRsp;
3482 else
3483 pVCpu->cpum.GstCtx.rsp = uNewRsp;
3484 /* Done! */
3485 }
3486
3487 /* Flush the prefetch buffer. */
3488#ifdef IEM_WITH_CODE_TLB
3489 pVCpu->iem.s.pbInstrBuf = NULL;
3490#else
3491 pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
3492#endif
3493
3494 return VINF_SUCCESS;
3495}
3496
3497
3498/**
3499 * Implements iret for long mode
3500 *
3501 * @param enmEffOpSize The effective operand size.
3502 */
3503IEM_CIMPL_DEF_1(iemCImpl_iret_64bit, IEMMODE, enmEffOpSize)
3504{
3505 NOREF(cbInstr);
3506
3507 /*
3508 * Nested task return is not supported in long mode.
3509 */
3510 if (pVCpu->cpum.GstCtx.eflags.Bits.u1NT)
3511 {
3512 Log(("iretq with NT=1 (eflags=%#x) -> #GP(0)\n", pVCpu->cpum.GstCtx.eflags.u));
3513 return iemRaiseGeneralProtectionFault0(pVCpu);
3514 }
3515
3516 /*
3517 * Normal return.
3518 *
3519 * Do the stack bits, but don't commit RSP before everything checks
3520 * out right.
3521 */
3522 VBOXSTRICTRC rcStrict;
3523 RTCPTRUNION uFrame;
3524 uint64_t uNewRip;
3525 uint16_t uNewCs;
3526 uint16_t uNewSs;
3527 uint32_t uNewFlags;
3528 uint64_t uNewRsp;
3529 if (enmEffOpSize == IEMMODE_64BIT)
3530 {
3531 rcStrict = iemMemStackPopBeginSpecial(pVCpu, 5*8, &uFrame.pv, &uNewRsp);
3532 if (rcStrict != VINF_SUCCESS)
3533 return rcStrict;
3534 uNewRip = uFrame.pu64[0];
3535 uNewCs = (uint16_t)uFrame.pu64[1];
3536 uNewFlags = (uint32_t)uFrame.pu64[2];
3537 uNewRsp = uFrame.pu64[3];
3538 uNewSs = (uint16_t)uFrame.pu64[4];
3539 }
3540 else if (enmEffOpSize == IEMMODE_32BIT)
3541 {
3542 rcStrict = iemMemStackPopBeginSpecial(pVCpu, 5*4, &uFrame.pv, &uNewRsp);
3543 if (rcStrict != VINF_SUCCESS)
3544 return rcStrict;
3545 uNewRip = uFrame.pu32[0];
3546 uNewCs = (uint16_t)uFrame.pu32[1];
3547 uNewFlags = uFrame.pu32[2];
3548 uNewRsp = uFrame.pu32[3];
3549 uNewSs = (uint16_t)uFrame.pu32[4];
3550 }
3551 else
3552 {
3553 Assert(enmEffOpSize == IEMMODE_16BIT);
3554 rcStrict = iemMemStackPopBeginSpecial(pVCpu, 5*2, &uFrame.pv, &uNewRsp);
3555 if (rcStrict != VINF_SUCCESS)
3556 return rcStrict;
3557 uNewRip = uFrame.pu16[0];
3558 uNewCs = uFrame.pu16[1];
3559 uNewFlags = uFrame.pu16[2];
3560 uNewRsp = uFrame.pu16[3];
3561 uNewSs = uFrame.pu16[4];
3562 }
3563 rcStrict = iemMemStackPopDoneSpecial(pVCpu, (void *)uFrame.pv); /* don't use iemMemStackPopCommitSpecial here. */
3564 if (RT_LIKELY(rcStrict == VINF_SUCCESS))
3565 { /* extremely like */ }
3566 else
3567 return rcStrict;
3568 Log7(("iretq stack: cs:rip=%04x:%016RX64 rflags=%016RX64 ss:rsp=%04x:%016RX64\n", uNewCs, uNewRip, uNewFlags, uNewSs, uNewRsp));
3569
3570 /*
3571 * Check stuff.
3572 */
3573 /* Read the CS descriptor. */
3574 if (!(uNewCs & X86_SEL_MASK_OFF_RPL))
3575 {
3576 Log(("iret %04x:%016RX64/%04x:%016RX64 -> invalid CS selector, #GP(0)\n", uNewCs, uNewRip, uNewSs, uNewRsp));
3577 return iemRaiseGeneralProtectionFault0(pVCpu);
3578 }
3579
3580 IEMSELDESC DescCS;
3581 rcStrict = iemMemFetchSelDesc(pVCpu, &DescCS, uNewCs, X86_XCPT_GP);
3582 if (rcStrict != VINF_SUCCESS)
3583 {
3584 Log(("iret %04x:%016RX64/%04x:%016RX64 - rcStrict=%Rrc when fetching CS\n",
3585 uNewCs, uNewRip, uNewSs, uNewRsp, VBOXSTRICTRC_VAL(rcStrict)));
3586 return rcStrict;
3587 }
3588
3589 /* Must be a code descriptor. */
3590 if ( !DescCS.Legacy.Gen.u1DescType
3591 || !(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE))
3592 {
3593 Log(("iret %04x:%016RX64/%04x:%016RX64 - CS is not a code segment T=%u T=%#xu -> #GP\n",
3594 uNewCs, uNewRip, uNewSs, uNewRsp, DescCS.Legacy.Gen.u1DescType, DescCS.Legacy.Gen.u4Type));
3595 return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
3596 }
3597
3598 /* Privilege checks. */
3599 uint8_t const uNewCpl = uNewCs & X86_SEL_RPL;
3600 if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF))
3601 {
3602 if ((uNewCs & X86_SEL_RPL) != DescCS.Legacy.Gen.u2Dpl)
3603 {
3604 Log(("iret %04x:%016RX64 - RPL != DPL (%d) -> #GP\n", uNewCs, uNewRip, DescCS.Legacy.Gen.u2Dpl));
3605 return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
3606 }
3607 }
3608 else if ((uNewCs & X86_SEL_RPL) < DescCS.Legacy.Gen.u2Dpl)
3609 {
3610 Log(("iret %04x:%016RX64 - RPL < DPL (%d) -> #GP\n", uNewCs, uNewRip, DescCS.Legacy.Gen.u2Dpl));
3611 return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
3612 }
3613 if ((uNewCs & X86_SEL_RPL) < pVCpu->iem.s.uCpl)
3614 {
3615 Log(("iret %04x:%016RX64 - RPL < CPL (%d) -> #GP\n", uNewCs, uNewRip, pVCpu->iem.s.uCpl));
3616 return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewCs);
3617 }
3618
3619 /* Present? */
3620 if (!DescCS.Legacy.Gen.u1Present)
3621 {
3622 Log(("iret %04x:%016RX64/%04x:%016RX64 - CS not present -> #NP\n", uNewCs, uNewRip, uNewSs, uNewRsp));
3623 return iemRaiseSelectorNotPresentBySelector(pVCpu, uNewCs);
3624 }
3625
3626 uint32_t cbLimitCS = X86DESC_LIMIT_G(&DescCS.Legacy);
3627
3628 /* Read the SS descriptor. */
3629 IEMSELDESC DescSS;
3630 if (!(uNewSs & X86_SEL_MASK_OFF_RPL))
3631 {
3632 if ( !DescCS.Legacy.Gen.u1Long
3633 || DescCS.Legacy.Gen.u1DefBig /** @todo exactly how does iret (and others) behave with u1Long=1 and u1DefBig=1? \#GP(sel)? */
3634 || uNewCpl > 2) /** @todo verify SS=0 impossible for ring-3. */
3635 {
3636 Log(("iret %04x:%016RX64/%04x:%016RX64 -> invalid SS selector, #GP(0)\n", uNewCs, uNewRip, uNewSs, uNewRsp));
3637 return iemRaiseGeneralProtectionFault0(pVCpu);
3638 }
3639 DescSS.Legacy.u = 0;
3640 }
3641 else
3642 {
3643 rcStrict = iemMemFetchSelDesc(pVCpu, &DescSS, uNewSs, X86_XCPT_GP); /** @todo Correct exception? */
3644 if (rcStrict != VINF_SUCCESS)
3645 {
3646 Log(("iret %04x:%016RX64/%04x:%016RX64 - %Rrc when fetching SS\n",
3647 uNewCs, uNewRip, uNewSs, uNewRsp, VBOXSTRICTRC_VAL(rcStrict)));
3648 return rcStrict;
3649 }
3650 }
3651
3652 /* Privilege checks. */
3653 if ((uNewSs & X86_SEL_RPL) != (uNewCs & X86_SEL_RPL))
3654 {
3655 Log(("iret %04x:%016RX64/%04x:%016RX64 -> SS.RPL != CS.RPL -> #GP\n", uNewCs, uNewRip, uNewSs, uNewRsp));
3656 return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSs);
3657 }
3658
3659 uint32_t cbLimitSs;
3660 if (!(uNewSs & X86_SEL_MASK_OFF_RPL))
3661 cbLimitSs = UINT32_MAX;
3662 else
3663 {
3664 if (DescSS.Legacy.Gen.u2Dpl != (uNewCs & X86_SEL_RPL))
3665 {
3666 Log(("iret %04x:%016RX64/%04x:%016RX64 -> SS.DPL (%d) != CS.RPL -> #GP\n",
3667 uNewCs, uNewRip, uNewSs, uNewRsp, DescSS.Legacy.Gen.u2Dpl));
3668 return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSs);
3669 }
3670
3671 /* Must be a writeable data segment descriptor. */
3672 if (!DescSS.Legacy.Gen.u1DescType)
3673 {
3674 Log(("iret %04x:%016RX64/%04x:%016RX64 -> SS is system segment (%#x) -> #GP\n",
3675 uNewCs, uNewRip, uNewSs, uNewRsp, DescSS.Legacy.Gen.u4Type));
3676 return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSs);
3677 }
3678 if ((DescSS.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_WRITE)) != X86_SEL_TYPE_WRITE)
3679 {
3680 Log(("iret %04x:%016RX64/%04x:%016RX64 - not writable data segment (%#x) -> #GP\n",
3681 uNewCs, uNewRip, uNewSs, uNewRsp, DescSS.Legacy.Gen.u4Type));
3682 return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewSs);
3683 }
3684
3685 /* Present? */
3686 if (!DescSS.Legacy.Gen.u1Present)
3687 {
3688 Log(("iret %04x:%016RX64/%04x:%016RX64 -> SS not present -> #SS\n", uNewCs, uNewRip, uNewSs, uNewRsp));
3689 return iemRaiseStackSelectorNotPresentBySelector(pVCpu, uNewSs);
3690 }
3691 cbLimitSs = X86DESC_LIMIT_G(&DescSS.Legacy);
3692 }
3693
3694 /* Check EIP. */
3695 if (DescCS.Legacy.Gen.u1Long)
3696 {
3697 if (!IEM_IS_CANONICAL(uNewRip))
3698 {
3699 Log(("iret %04x:%016RX64/%04x:%016RX64 -> RIP is not canonical -> #GP(0)\n",
3700 uNewCs, uNewRip, uNewSs, uNewRsp));
3701 return iemRaiseSelectorBoundsBySelector(pVCpu, uNewCs);
3702 }
3703 }
3704 else
3705 {
3706 if (uNewRip > cbLimitCS)
3707 {
3708 Log(("iret %04x:%016RX64/%04x:%016RX64 -> EIP is out of bounds (%#x) -> #GP(0)\n",
3709 uNewCs, uNewRip, uNewSs, uNewRsp, cbLimitCS));
3710 /** @todo: Which is it, #GP(0) or #GP(sel)? */
3711 return iemRaiseSelectorBoundsBySelector(pVCpu, uNewCs);
3712 }
3713 }
3714
3715 /*
3716 * Commit the changes, marking CS and SS accessed first since
3717 * that may fail.
3718 */
3719 /** @todo where exactly are these actually marked accessed by a real CPU? */
3720 if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
3721 {
3722 rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewCs);
3723 if (rcStrict != VINF_SUCCESS)
3724 return rcStrict;
3725 DescCS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
3726 }
3727 if (!(DescSS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
3728 {
3729 rcStrict = iemMemMarkSelDescAccessed(pVCpu, uNewSs);
3730 if (rcStrict != VINF_SUCCESS)
3731 return rcStrict;
3732 DescSS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
3733 }
3734
3735 uint32_t fEFlagsMask = X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF
3736 | X86_EFL_TF | X86_EFL_DF | X86_EFL_OF | X86_EFL_NT;
3737 if (enmEffOpSize != IEMMODE_16BIT)
3738 fEFlagsMask |= X86_EFL_RF | X86_EFL_AC | X86_EFL_ID;
3739 if (pVCpu->iem.s.uCpl == 0)
3740 fEFlagsMask |= X86_EFL_IF | X86_EFL_IOPL | X86_EFL_VIF | X86_EFL_VIP; /* VM is ignored */
3741 else if (pVCpu->iem.s.uCpl <= pVCpu->cpum.GstCtx.eflags.Bits.u2IOPL)
3742 fEFlagsMask |= X86_EFL_IF;
3743 uint32_t fEFlagsNew = IEMMISC_GET_EFL(pVCpu);
3744 fEFlagsNew &= ~fEFlagsMask;
3745 fEFlagsNew |= uNewFlags & fEFlagsMask;
3746#ifdef DBGFTRACE_ENABLED
3747 RTTraceBufAddMsgF(pVCpu->CTX_SUFF(pVM)->CTX_SUFF(hTraceBuf), "iret/%ul%u %08llx -> %04x:%04llx %llx %04x:%04llx",
3748 pVCpu->iem.s.uCpl, uNewCpl, pVCpu->cpum.GstCtx.rip, uNewCs, uNewRip, uNewFlags, uNewSs, uNewRsp);
3749#endif
3750
3751 IEMMISC_SET_EFL(pVCpu, fEFlagsNew);
3752 pVCpu->cpum.GstCtx.rip = uNewRip;
3753 pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
3754 pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
3755 pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
3756 pVCpu->cpum.GstCtx.cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCS.Legacy);
3757 pVCpu->cpum.GstCtx.cs.u32Limit = cbLimitCS;
3758 pVCpu->cpum.GstCtx.cs.u64Base = X86DESC_BASE(&DescCS.Legacy);
3759 pVCpu->iem.s.enmCpuMode = iemCalcCpuMode(pVCpu);
3760 if (pVCpu->cpum.GstCtx.cs.Attr.n.u1Long || pVCpu->cpum.GstCtx.cs.Attr.n.u1DefBig)
3761 pVCpu->cpum.GstCtx.rsp = uNewRsp;
3762 else
3763 pVCpu->cpum.GstCtx.sp = (uint16_t)uNewRsp;
3764 pVCpu->cpum.GstCtx.ss.Sel = uNewSs;
3765 pVCpu->cpum.GstCtx.ss.ValidSel = uNewSs;
3766 if (!(uNewSs & X86_SEL_MASK_OFF_RPL))
3767 {
3768 pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
3769 pVCpu->cpum.GstCtx.ss.Attr.u = X86DESCATTR_UNUSABLE | (uNewCpl << X86DESCATTR_DPL_SHIFT);
3770 pVCpu->cpum.GstCtx.ss.u32Limit = UINT32_MAX;
3771 pVCpu->cpum.GstCtx.ss.u64Base = 0;
3772 Log2(("iretq new SS: NULL\n"));
3773 }
3774 else
3775 {
3776 pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
3777 pVCpu->cpum.GstCtx.ss.Attr.u = X86DESC_GET_HID_ATTR(&DescSS.Legacy);
3778 pVCpu->cpum.GstCtx.ss.u32Limit = cbLimitSs;
3779 pVCpu->cpum.GstCtx.ss.u64Base = X86DESC_BASE(&DescSS.Legacy);
3780 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));
3781 }
3782
3783 if (pVCpu->iem.s.uCpl != uNewCpl)
3784 {
3785 pVCpu->iem.s.uCpl = uNewCpl;
3786 iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCpl, &pVCpu->cpum.GstCtx.ds);
3787 iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCpl, &pVCpu->cpum.GstCtx.es);
3788 iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCpl, &pVCpu->cpum.GstCtx.fs);
3789 iemHlpAdjustSelectorForNewCpl(pVCpu, uNewCpl, &pVCpu->cpum.GstCtx.gs);
3790 }
3791
3792 /* Flush the prefetch buffer. */
3793#ifdef IEM_WITH_CODE_TLB
3794 pVCpu->iem.s.pbInstrBuf = NULL;
3795#else
3796 pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
3797#endif
3798
3799 return VINF_SUCCESS;
3800}
3801
3802
3803/**
3804 * Implements iret.
3805 *
3806 * @param enmEffOpSize The effective operand size.
3807 */
3808IEM_CIMPL_DEF_1(iemCImpl_iret, IEMMODE, enmEffOpSize)
3809{
3810 bool fBlockingNmi = VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_BLOCK_NMIS);
3811
3812#ifdef VBOX_WITH_NESTED_HWVIRT_VMX
3813 if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
3814 {
3815 /*
3816 * Record whether NMI (or virtual-NMI) blocking is in effect during the execution
3817 * of this IRET instruction. We need to provide this information as part of some
3818 * VM-exits.
3819 *
3820 * See Intel spec. 27.2.2 "Information for VM Exits Due to Vectored Events".
3821 */
3822 if (IEM_VMX_IS_PINCTLS_SET(pVCpu, VMX_PIN_CTLS_VIRT_NMI))
3823 pVCpu->cpum.GstCtx.hwvirt.vmx.fNmiUnblockingIret = pVCpu->cpum.GstCtx.hwvirt.vmx.fVirtNmiBlocking;
3824 else
3825 pVCpu->cpum.GstCtx.hwvirt.vmx.fNmiUnblockingIret = fBlockingNmi;
3826
3827 /*
3828 * If "NMI exiting" is set, IRET does not affect blocking of NMIs.
3829 * See Intel Spec. 25.3 "Changes To Instruction Behavior In VMX Non-root Operation".
3830 */
3831 if (IEM_VMX_IS_PINCTLS_SET(pVCpu, VMX_PIN_CTLS_NMI_EXIT))
3832 fBlockingNmi = false;
3833
3834 /* Clear virtual-NMI blocking, if any, before causing any further exceptions. */
3835 pVCpu->cpum.GstCtx.hwvirt.vmx.fVirtNmiBlocking = false;
3836 }
3837#endif
3838
3839 /*
3840 * The SVM nested-guest intercept for IRET takes priority over all exceptions,
3841 * The NMI is still held pending (which I assume means blocking of further NMIs
3842 * is in effect).
3843 *
3844 * See AMD spec. 15.9 "Instruction Intercepts".
3845 * See AMD spec. 15.21.9 "NMI Support".
3846 */
3847 if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_IRET))
3848 {
3849 Log(("iret: Guest intercept -> #VMEXIT\n"));
3850 IEM_SVM_UPDATE_NRIP(pVCpu);
3851 IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_IRET, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
3852 }
3853
3854 /*
3855 * Clear NMI blocking, if any, before causing any further exceptions.
3856 * See Intel spec. 6.7.1 "Handling Multiple NMIs".
3857 */
3858 if (fBlockingNmi)
3859 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_BLOCK_NMIS);
3860
3861 /*
3862 * Call a mode specific worker.
3863 */
3864 if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
3865 return IEM_CIMPL_CALL_1(iemCImpl_iret_real_v8086, enmEffOpSize);
3866 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SREG_MASK | CPUMCTX_EXTRN_GDTR | CPUMCTX_EXTRN_LDTR);
3867 if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
3868 return IEM_CIMPL_CALL_1(iemCImpl_iret_64bit, enmEffOpSize);
3869 return IEM_CIMPL_CALL_1(iemCImpl_iret_prot, enmEffOpSize);
3870}
3871
3872
3873static void iemLoadallSetSelector(PVMCPUCC pVCpu, uint8_t iSegReg, uint16_t uSel)
3874{
3875 PCPUMSELREGHID pHid = iemSRegGetHid(pVCpu, iSegReg);
3876
3877 pHid->Sel = uSel;
3878 pHid->ValidSel = uSel;
3879 pHid->fFlags = CPUMSELREG_FLAGS_VALID;
3880}
3881
3882
3883static void iemLoadall286SetDescCache(PVMCPUCC pVCpu, uint8_t iSegReg, uint8_t const *pbMem)
3884{
3885 PCPUMSELREGHID pHid = iemSRegGetHid(pVCpu, iSegReg);
3886
3887 /* The base is in the first three bytes. */
3888 pHid->u64Base = pbMem[0] + (pbMem[1] << 8) + (pbMem[2] << 16);
3889 /* The attributes are in the fourth byte. */
3890 pHid->Attr.u = pbMem[3];
3891 /* The limit is in the last two bytes. */
3892 pHid->u32Limit = pbMem[4] + (pbMem[5] << 8);
3893}
3894
3895
3896/**
3897 * Implements 286 LOADALL (286 CPUs only).
3898 */
3899IEM_CIMPL_DEF_0(iemCImpl_loadall286)
3900{
3901 NOREF(cbInstr);
3902
3903 /* Data is loaded from a buffer at 800h. No checks are done on the
3904 * validity of loaded state.
3905 *
3906 * LOADALL only loads the internal CPU state, it does not access any
3907 * GDT, LDT, or similar tables.
3908 */
3909
3910 if (pVCpu->iem.s.uCpl != 0)
3911 {
3912 Log(("loadall286: CPL must be 0 not %u -> #GP(0)\n", pVCpu->iem.s.uCpl));
3913 return iemRaiseGeneralProtectionFault0(pVCpu);
3914 }
3915
3916 uint8_t const *pbMem = NULL;
3917 uint16_t const *pa16Mem;
3918 uint8_t const *pa8Mem;
3919 RTGCPHYS GCPtrStart = 0x800; /* Fixed table location. */
3920 VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, (void **)&pbMem, 0x66, UINT8_MAX, GCPtrStart, IEM_ACCESS_SYS_R);
3921 if (rcStrict != VINF_SUCCESS)
3922 return rcStrict;
3923
3924 /* The MSW is at offset 0x06. */
3925 pa16Mem = (uint16_t const *)(pbMem + 0x06);
3926 /* Even LOADALL can't clear the MSW.PE bit, though it can set it. */
3927 uint64_t uNewCr0 = pVCpu->cpum.GstCtx.cr0 & ~(X86_CR0_MP | X86_CR0_EM | X86_CR0_TS);
3928 uNewCr0 |= *pa16Mem & (X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS);
3929 uint64_t const uOldCr0 = pVCpu->cpum.GstCtx.cr0;
3930
3931 CPUMSetGuestCR0(pVCpu, uNewCr0);
3932 Assert(pVCpu->cpum.GstCtx.cr0 == uNewCr0);
3933
3934 /* Inform PGM if mode changed. */
3935 if ((uNewCr0 & X86_CR0_PE) != (uOldCr0 & X86_CR0_PE))
3936 {
3937 int rc = PGMFlushTLB(pVCpu, pVCpu->cpum.GstCtx.cr3, true /* global */);
3938 AssertRCReturn(rc, rc);
3939 /* ignore informational status codes */
3940 }
3941 rcStrict = PGMChangeMode(pVCpu, pVCpu->cpum.GstCtx.cr0, pVCpu->cpum.GstCtx.cr4, pVCpu->cpum.GstCtx.msrEFER,
3942 false /* fForce */);
3943
3944 /* TR selector is at offset 0x16. */
3945 pa16Mem = (uint16_t const *)(pbMem + 0x16);
3946 pVCpu->cpum.GstCtx.tr.Sel = pa16Mem[0];
3947 pVCpu->cpum.GstCtx.tr.ValidSel = pa16Mem[0];
3948 pVCpu->cpum.GstCtx.tr.fFlags = CPUMSELREG_FLAGS_VALID;
3949
3950 /* Followed by FLAGS... */
3951 pVCpu->cpum.GstCtx.eflags.u = pa16Mem[1] | X86_EFL_1;
3952 pVCpu->cpum.GstCtx.ip = pa16Mem[2]; /* ...and IP. */
3953
3954 /* LDT is at offset 0x1C. */
3955 pa16Mem = (uint16_t const *)(pbMem + 0x1C);
3956 pVCpu->cpum.GstCtx.ldtr.Sel = pa16Mem[0];
3957 pVCpu->cpum.GstCtx.ldtr.ValidSel = pa16Mem[0];
3958 pVCpu->cpum.GstCtx.ldtr.fFlags = CPUMSELREG_FLAGS_VALID;
3959
3960 /* Segment registers are at offset 0x1E. */
3961 pa16Mem = (uint16_t const *)(pbMem + 0x1E);
3962 iemLoadallSetSelector(pVCpu, X86_SREG_DS, pa16Mem[0]);
3963 iemLoadallSetSelector(pVCpu, X86_SREG_SS, pa16Mem[1]);
3964 iemLoadallSetSelector(pVCpu, X86_SREG_CS, pa16Mem[2]);
3965 iemLoadallSetSelector(pVCpu, X86_SREG_ES, pa16Mem[3]);
3966
3967 /* GPRs are at offset 0x26. */
3968 pa16Mem = (uint16_t const *)(pbMem + 0x26);
3969 pVCpu->cpum.GstCtx.di = pa16Mem[0];
3970 pVCpu->cpum.GstCtx.si = pa16Mem[1];
3971 pVCpu->cpum.GstCtx.bp = pa16Mem[2];
3972 pVCpu->cpum.GstCtx.sp = pa16Mem[3];
3973 pVCpu->cpum.GstCtx.bx = pa16Mem[4];
3974 pVCpu->cpum.GstCtx.dx = pa16Mem[5];
3975 pVCpu->cpum.GstCtx.cx = pa16Mem[6];
3976 pVCpu->cpum.GstCtx.ax = pa16Mem[7];
3977
3978 /* Descriptor caches are at offset 0x36, 6 bytes per entry. */
3979 iemLoadall286SetDescCache(pVCpu, X86_SREG_ES, pbMem + 0x36);
3980 iemLoadall286SetDescCache(pVCpu, X86_SREG_CS, pbMem + 0x3C);
3981 iemLoadall286SetDescCache(pVCpu, X86_SREG_SS, pbMem + 0x42);
3982 iemLoadall286SetDescCache(pVCpu, X86_SREG_DS, pbMem + 0x48);
3983
3984 /* GDTR contents are at offset 0x4E, 6 bytes. */
3985 RTGCPHYS GCPtrBase;
3986 uint16_t cbLimit;
3987 pa8Mem = pbMem + 0x4E;
3988 /* NB: Fourth byte "should be zero"; we are ignoring it. */
3989 GCPtrBase = pa8Mem[0] + (pa8Mem[1] << 8) + (pa8Mem[2] << 16);
3990 cbLimit = pa8Mem[4] + (pa8Mem[5] << 8);
3991 CPUMSetGuestGDTR(pVCpu, GCPtrBase, cbLimit);
3992
3993 /* IDTR contents are at offset 0x5A, 6 bytes. */
3994 pa8Mem = pbMem + 0x5A;
3995 GCPtrBase = pa8Mem[0] + (pa8Mem[1] << 8) + (pa8Mem[2] << 16);
3996 cbLimit = pa8Mem[4] + (pa8Mem[5] << 8);
3997 CPUMSetGuestIDTR(pVCpu, GCPtrBase, cbLimit);
3998
3999 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));
4000 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));
4001 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));
4002 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));
4003 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));
4004 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));
4005
4006 rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)pbMem, IEM_ACCESS_SYS_R);
4007 if (rcStrict != VINF_SUCCESS)
4008 return rcStrict;
4009
4010 /* The CPL may change. It is taken from the "DPL fields of the SS and CS
4011 * descriptor caches" but there is no word as to what happens if those are
4012 * not identical (probably bad things).
4013 */
4014 pVCpu->iem.s.uCpl = pVCpu->cpum.GstCtx.cs.Attr.n.u2Dpl;
4015
4016 CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_HIDDEN_SEL_REGS | CPUM_CHANGED_IDTR | CPUM_CHANGED_GDTR | CPUM_CHANGED_TR | CPUM_CHANGED_LDTR);
4017
4018 /* Flush the prefetch buffer. */
4019#ifdef IEM_WITH_CODE_TLB
4020 pVCpu->iem.s.pbInstrBuf = NULL;
4021#else
4022 pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
4023#endif
4024 return rcStrict;
4025}
4026
4027
4028/**
4029 * Implements SYSCALL (AMD and Intel64).
4030 *
4031 * @param enmEffOpSize The effective operand size.
4032 */
4033IEM_CIMPL_DEF_0(iemCImpl_syscall)
4034{
4035 /** @todo hack, LOADALL should be decoded as such on a 286. */
4036 if (RT_UNLIKELY(pVCpu->iem.s.uTargetCpu == IEMTARGETCPU_286))
4037 return iemCImpl_loadall286(pVCpu, cbInstr);
4038
4039 /*
4040 * Check preconditions.
4041 *
4042 * Note that CPUs described in the documentation may load a few odd values
4043 * into CS and SS than we allow here. This has yet to be checked on real
4044 * hardware.
4045 */
4046 if (!(pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_SCE))
4047 {
4048 Log(("syscall: Not enabled in EFER -> #UD\n"));
4049 return iemRaiseUndefinedOpcode(pVCpu);
4050 }
4051 if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE))
4052 {
4053 Log(("syscall: Protected mode is required -> #GP(0)\n"));
4054 return iemRaiseGeneralProtectionFault0(pVCpu);
4055 }
4056 if (IEM_IS_GUEST_CPU_INTEL(pVCpu) && !CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu)))
4057 {
4058 Log(("syscall: Only available in long mode on intel -> #UD\n"));
4059 return iemRaiseUndefinedOpcode(pVCpu);
4060 }
4061
4062 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SYSCALL_MSRS);
4063
4064 /** @todo verify RPL ignoring and CS=0xfff8 (i.e. SS == 0). */
4065 /** @todo what about LDT selectors? Shouldn't matter, really. */
4066 uint16_t uNewCs = (pVCpu->cpum.GstCtx.msrSTAR >> MSR_K6_STAR_SYSCALL_CS_SS_SHIFT) & X86_SEL_MASK_OFF_RPL;
4067 uint16_t uNewSs = uNewCs + 8;
4068 if (uNewCs == 0 || uNewSs == 0)
4069 {
4070 Log(("syscall: msrSTAR.CS = 0 or SS = 0 -> #GP(0)\n"));
4071 return iemRaiseGeneralProtectionFault0(pVCpu);
4072 }
4073
4074 /* Long mode and legacy mode differs. */
4075 if (CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu)))
4076 {
4077 uint64_t uNewRip = pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT ? pVCpu->cpum.GstCtx.msrLSTAR : pVCpu->cpum.GstCtx. msrCSTAR;
4078
4079 /* This test isn't in the docs, but I'm not trusting the guys writing
4080 the MSRs to have validated the values as canonical like they should. */
4081 if (!IEM_IS_CANONICAL(uNewRip))
4082 {
4083 Log(("syscall: Only available in long mode on intel -> #UD\n"));
4084 return iemRaiseUndefinedOpcode(pVCpu);
4085 }
4086
4087 /*
4088 * Commit it.
4089 */
4090 Log(("syscall: %04x:%016RX64 [efl=%#llx] -> %04x:%016RX64\n", pVCpu->cpum.GstCtx.cs, pVCpu->cpum.GstCtx.rip, pVCpu->cpum.GstCtx.rflags.u, uNewCs, uNewRip));
4091 pVCpu->cpum.GstCtx.rcx = pVCpu->cpum.GstCtx.rip + cbInstr;
4092 pVCpu->cpum.GstCtx.rip = uNewRip;
4093
4094 pVCpu->cpum.GstCtx.rflags.u &= ~X86_EFL_RF;
4095 pVCpu->cpum.GstCtx.r11 = pVCpu->cpum.GstCtx.rflags.u;
4096 pVCpu->cpum.GstCtx.rflags.u &= ~pVCpu->cpum.GstCtx.msrSFMASK;
4097 pVCpu->cpum.GstCtx.rflags.u |= X86_EFL_1;
4098
4099 pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_L | X86DESCATTR_DT | X86_SEL_TYPE_ER_ACC;
4100 pVCpu->cpum.GstCtx.ss.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_L | X86DESCATTR_DT | X86_SEL_TYPE_RW_ACC;
4101 }
4102 else
4103 {
4104 /*
4105 * Commit it.
4106 */
4107 Log(("syscall: %04x:%08RX32 [efl=%#x] -> %04x:%08RX32\n",
4108 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)));
4109 pVCpu->cpum.GstCtx.rcx = pVCpu->cpum.GstCtx.eip + cbInstr;
4110 pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.msrSTAR & MSR_K6_STAR_SYSCALL_EIP_MASK;
4111 pVCpu->cpum.GstCtx.rflags.u &= ~(X86_EFL_VM | X86_EFL_IF | X86_EFL_RF);
4112
4113 pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_D | X86DESCATTR_DT | X86_SEL_TYPE_ER_ACC;
4114 pVCpu->cpum.GstCtx.ss.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_D | X86DESCATTR_DT | X86_SEL_TYPE_RW_ACC;
4115 }
4116 pVCpu->cpum.GstCtx.cs.Sel = uNewCs;
4117 pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs;
4118 pVCpu->cpum.GstCtx.cs.u64Base = 0;
4119 pVCpu->cpum.GstCtx.cs.u32Limit = UINT32_MAX;
4120 pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
4121
4122 pVCpu->cpum.GstCtx.ss.Sel = uNewSs;
4123 pVCpu->cpum.GstCtx.ss.ValidSel = uNewSs;
4124 pVCpu->cpum.GstCtx.ss.u64Base = 0;
4125 pVCpu->cpum.GstCtx.ss.u32Limit = UINT32_MAX;
4126 pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
4127
4128 /* Flush the prefetch buffer. */
4129#ifdef IEM_WITH_CODE_TLB
4130 pVCpu->iem.s.pbInstrBuf = NULL;
4131#else
4132 pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
4133#endif
4134
4135 return VINF_SUCCESS;
4136}
4137
4138
4139/**
4140 * Implements SYSRET (AMD and Intel64).
4141 */
4142IEM_CIMPL_DEF_0(iemCImpl_sysret)
4143
4144{
4145 RT_NOREF_PV(cbInstr);
4146
4147 /*
4148 * Check preconditions.
4149 *
4150 * Note that CPUs described in the documentation may load a few odd values
4151 * into CS and SS than we allow here. This has yet to be checked on real
4152 * hardware.
4153 */
4154 if (!(pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_SCE))
4155 {
4156 Log(("sysret: Not enabled in EFER -> #UD\n"));
4157 return iemRaiseUndefinedOpcode(pVCpu);
4158 }
4159 if (IEM_IS_GUEST_CPU_INTEL(pVCpu) && !CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu)))
4160 {
4161 Log(("sysret: Only available in long mode on intel -> #UD\n"));
4162 return iemRaiseUndefinedOpcode(pVCpu);
4163 }
4164 if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE))
4165 {
4166 Log(("sysret: Protected mode is required -> #GP(0)\n"));
4167 return iemRaiseGeneralProtectionFault0(pVCpu);
4168 }
4169 if (pVCpu->iem.s.uCpl != 0)
4170 {
4171 Log(("sysret: CPL must be 0 not %u -> #GP(0)\n", pVCpu->iem.s.uCpl));
4172 return iemRaiseGeneralProtectionFault0(pVCpu);
4173 }
4174
4175 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SYSCALL_MSRS);
4176
4177 /** @todo Does SYSRET verify CS != 0 and SS != 0? Neither is valid in ring-3. */
4178 uint16_t uNewCs = (pVCpu->cpum.GstCtx.msrSTAR >> MSR_K6_STAR_SYSRET_CS_SS_SHIFT) & X86_SEL_MASK_OFF_RPL;
4179 uint16_t uNewSs = uNewCs + 8;
4180 if (pVCpu->iem.s.enmEffOpSize == IEMMODE_64BIT)
4181 uNewCs += 16;
4182 if (uNewCs == 0 || uNewSs == 0)
4183 {
4184 Log(("sysret: msrSTAR.CS = 0 or SS = 0 -> #GP(0)\n"));
4185 return iemRaiseGeneralProtectionFault0(pVCpu);
4186 }
4187
4188 /*
4189 * Commit it.
4190 */
4191 if (CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu)))
4192 {
4193 if (pVCpu->iem.s.enmEffOpSize == IEMMODE_64BIT)
4194 {
4195 Log(("sysret: %04x:%016RX64 [efl=%#llx] -> %04x:%016RX64 [r11=%#llx]\n",
4196 pVCpu->cpum.GstCtx.cs, pVCpu->cpum.GstCtx.rip, pVCpu->cpum.GstCtx.rflags.u, uNewCs, pVCpu->cpum.GstCtx.rcx, pVCpu->cpum.GstCtx.r11));
4197 /* Note! We disregard intel manual regarding the RCX cananonical
4198 check, ask intel+xen why AMD doesn't do it. */
4199 pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.rcx;
4200 pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_L | X86DESCATTR_DT | X86_SEL_TYPE_ER_ACC
4201 | (3 << X86DESCATTR_DPL_SHIFT);
4202 }
4203 else
4204 {
4205 Log(("sysret: %04x:%016RX64 [efl=%#llx] -> %04x:%08RX32 [r11=%#llx]\n",
4206 pVCpu->cpum.GstCtx.cs, pVCpu->cpum.GstCtx.rip, pVCpu->cpum.GstCtx.rflags.u, uNewCs, pVCpu->cpum.GstCtx.ecx, pVCpu->cpum.GstCtx.r11));
4207 pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.ecx;
4208 pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_D | X86DESCATTR_DT | X86_SEL_TYPE_ER_ACC
4209 | (3 << X86DESCATTR_DPL_SHIFT);
4210 }
4211 /** @todo testcase: See what kind of flags we can make SYSRET restore and
4212 * what it really ignores. RF and VM are hinted at being zero, by AMD. */
4213 pVCpu->cpum.GstCtx.rflags.u = pVCpu->cpum.GstCtx.r11 & (X86_EFL_POPF_BITS | X86_EFL_VIF | X86_EFL_VIP);
4214 pVCpu->cpum.GstCtx.rflags.u |= X86_EFL_1;
4215 }
4216 else
4217 {
4218 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));
4219 pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.rcx;
4220 pVCpu->cpum.GstCtx.rflags.u |= X86_EFL_IF;
4221 pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_D | X86DESCATTR_DT | X86_SEL_TYPE_ER_ACC
4222 | (3 << X86DESCATTR_DPL_SHIFT);
4223 }
4224 pVCpu->cpum.GstCtx.cs.Sel = uNewCs | 3;
4225 pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs | 3;
4226 pVCpu->cpum.GstCtx.cs.u64Base = 0;
4227 pVCpu->cpum.GstCtx.cs.u32Limit = UINT32_MAX;
4228 pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
4229
4230 pVCpu->cpum.GstCtx.ss.Sel = uNewSs | 3;
4231 pVCpu->cpum.GstCtx.ss.ValidSel = uNewSs | 3;
4232 pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
4233 /* The SS hidden bits remains unchanged says AMD. To that I say "Yeah, right!". */
4234 pVCpu->cpum.GstCtx.ss.Attr.u |= (3 << X86DESCATTR_DPL_SHIFT);
4235 /** @todo Testcase: verify that SS.u1Long and SS.u1DefBig are left unchanged
4236 * on sysret. */
4237
4238 /* Flush the prefetch buffer. */
4239#ifdef IEM_WITH_CODE_TLB
4240 pVCpu->iem.s.pbInstrBuf = NULL;
4241#else
4242 pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
4243#endif
4244
4245 return VINF_SUCCESS;
4246}
4247
4248
4249/**
4250 * Implements SYSENTER (Intel, 32-bit AMD).
4251 */
4252IEM_CIMPL_DEF_0(iemCImpl_sysenter)
4253{
4254 RT_NOREF(cbInstr);
4255
4256 /*
4257 * Check preconditions.
4258 *
4259 * Note that CPUs described in the documentation may load a few odd values
4260 * into CS and SS than we allow here. This has yet to be checked on real
4261 * hardware.
4262 */
4263 if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fSysEnter)
4264 {
4265 Log(("sysenter: not supported -=> #UD\n"));
4266 return iemRaiseUndefinedOpcode(pVCpu);
4267 }
4268 if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE))
4269 {
4270 Log(("sysenter: Protected or long mode is required -> #GP(0)\n"));
4271 return iemRaiseGeneralProtectionFault0(pVCpu);
4272 }
4273 bool fIsLongMode = CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu));
4274 if (IEM_IS_GUEST_CPU_AMD(pVCpu) && !fIsLongMode)
4275 {
4276 Log(("sysenter: Only available in protected mode on AMD -> #UD\n"));
4277 return iemRaiseUndefinedOpcode(pVCpu);
4278 }
4279 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SYSENTER_MSRS);
4280 uint16_t uNewCs = pVCpu->cpum.GstCtx.SysEnter.cs;
4281 if ((uNewCs & X86_SEL_MASK_OFF_RPL) == 0)
4282 {
4283 Log(("sysenter: SYSENTER_CS = %#x -> #GP(0)\n", uNewCs));
4284 return iemRaiseGeneralProtectionFault0(pVCpu);
4285 }
4286
4287 /* This test isn't in the docs, it's just a safeguard against missing
4288 canonical checks when writing the registers. */
4289 if (RT_LIKELY( !fIsLongMode
4290 || ( IEM_IS_CANONICAL(pVCpu->cpum.GstCtx.SysEnter.eip)
4291 && IEM_IS_CANONICAL(pVCpu->cpum.GstCtx.SysEnter.esp))))
4292 { /* likely */ }
4293 else
4294 {
4295 Log(("sysenter: SYSENTER_EIP = %#RX64 or/and SYSENTER_ESP = %#RX64 not canonical -> #GP(0)\n",
4296 pVCpu->cpum.GstCtx.SysEnter.eip, pVCpu->cpum.GstCtx.SysEnter.esp));
4297 return iemRaiseUndefinedOpcode(pVCpu);
4298 }
4299
4300/** @todo Test: Sysenter from ring-0, ring-1 and ring-2. */
4301
4302 /*
4303 * Update registers and commit.
4304 */
4305 if (fIsLongMode)
4306 {
4307 Log(("sysenter: %04x:%016RX64 [efl=%#llx] -> %04x:%016RX64\n", pVCpu->cpum.GstCtx.cs, pVCpu->cpum.GstCtx.rip,
4308 pVCpu->cpum.GstCtx.rflags.u, uNewCs & X86_SEL_MASK_OFF_RPL, pVCpu->cpum.GstCtx.SysEnter.eip));
4309 pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.SysEnter.eip;
4310 pVCpu->cpum.GstCtx.rsp = pVCpu->cpum.GstCtx.SysEnter.esp;
4311 pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_L | X86DESCATTR_G | X86DESCATTR_P | X86DESCATTR_DT
4312 | X86DESCATTR_LIMIT_HIGH | X86_SEL_TYPE_ER_ACC;
4313 }
4314 else
4315 {
4316 Log(("sysenter: %04x:%08RX32 [efl=%#llx] -> %04x:%08RX32\n", pVCpu->cpum.GstCtx.cs, (uint32_t)pVCpu->cpum.GstCtx.rip,
4317 pVCpu->cpum.GstCtx.rflags.u, uNewCs & X86_SEL_MASK_OFF_RPL, (uint32_t)pVCpu->cpum.GstCtx.SysEnter.eip));
4318 pVCpu->cpum.GstCtx.rip = (uint32_t)pVCpu->cpum.GstCtx.SysEnter.eip;
4319 pVCpu->cpum.GstCtx.rsp = (uint32_t)pVCpu->cpum.GstCtx.SysEnter.esp;
4320 pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_D | X86DESCATTR_G | X86DESCATTR_P | X86DESCATTR_DT
4321 | X86DESCATTR_LIMIT_HIGH | X86_SEL_TYPE_ER_ACC;
4322 }
4323 pVCpu->cpum.GstCtx.cs.Sel = uNewCs & X86_SEL_MASK_OFF_RPL;
4324 pVCpu->cpum.GstCtx.cs.ValidSel = uNewCs & X86_SEL_MASK_OFF_RPL;
4325 pVCpu->cpum.GstCtx.cs.u64Base = 0;
4326 pVCpu->cpum.GstCtx.cs.u32Limit = UINT32_MAX;
4327 pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
4328
4329 pVCpu->cpum.GstCtx.ss.Sel = (uNewCs & X86_SEL_MASK_OFF_RPL) + 8;
4330 pVCpu->cpum.GstCtx.ss.ValidSel = (uNewCs & X86_SEL_MASK_OFF_RPL) + 8;
4331 pVCpu->cpum.GstCtx.ss.u64Base = 0;
4332 pVCpu->cpum.GstCtx.ss.u32Limit = UINT32_MAX;
4333 pVCpu->cpum.GstCtx.ss.Attr.u = X86DESCATTR_D | X86DESCATTR_G | X86DESCATTR_P | X86DESCATTR_DT
4334 | X86DESCATTR_LIMIT_HIGH | X86_SEL_TYPE_RW_ACC;
4335 pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
4336
4337 pVCpu->cpum.GstCtx.rflags.Bits.u1IF = 0;
4338 pVCpu->cpum.GstCtx.rflags.Bits.u1VM = 0;
4339 pVCpu->cpum.GstCtx.rflags.Bits.u1RF = 0;
4340
4341 pVCpu->iem.s.uCpl = 0;
4342
4343 /* Flush the prefetch buffer. */
4344#ifdef IEM_WITH_CODE_TLB
4345 pVCpu->iem.s.pbInstrBuf = NULL;
4346#else
4347 pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
4348#endif
4349
4350 return VINF_SUCCESS;
4351}
4352
4353
4354/**
4355 * Implements SYSEXIT (Intel, 32-bit AMD).
4356 *
4357 * @param enmEffOpSize The effective operand size.
4358 */
4359IEM_CIMPL_DEF_1(iemCImpl_sysexit, IEMMODE, enmEffOpSize)
4360{
4361 RT_NOREF(cbInstr);
4362
4363 /*
4364 * Check preconditions.
4365 *
4366 * Note that CPUs described in the documentation may load a few odd values
4367 * into CS and SS than we allow here. This has yet to be checked on real
4368 * hardware.
4369 */
4370 if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fSysEnter)
4371 {
4372 Log(("sysexit: not supported -=> #UD\n"));
4373 return iemRaiseUndefinedOpcode(pVCpu);
4374 }
4375 if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE))
4376 {
4377 Log(("sysexit: Protected or long mode is required -> #GP(0)\n"));
4378 return iemRaiseGeneralProtectionFault0(pVCpu);
4379 }
4380 bool fIsLongMode = CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu));
4381 if (IEM_IS_GUEST_CPU_AMD(pVCpu) && !fIsLongMode)
4382 {
4383 Log(("sysexit: Only available in protected mode on AMD -> #UD\n"));
4384 return iemRaiseUndefinedOpcode(pVCpu);
4385 }
4386 if (pVCpu->iem.s.uCpl != 0)
4387 {
4388 Log(("sysexit: CPL(=%u) != 0 -> #GP(0)\n", pVCpu->iem.s.uCpl));
4389 return iemRaiseGeneralProtectionFault0(pVCpu);
4390 }
4391 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SYSENTER_MSRS);
4392 uint16_t uNewCs = pVCpu->cpum.GstCtx.SysEnter.cs;
4393 if ((uNewCs & X86_SEL_MASK_OFF_RPL) == 0)
4394 {
4395 Log(("sysexit: SYSENTER_CS = %#x -> #GP(0)\n", uNewCs));
4396 return iemRaiseGeneralProtectionFault0(pVCpu);
4397 }
4398
4399 /*
4400 * Update registers and commit.
4401 */
4402 if (enmEffOpSize == IEMMODE_64BIT)
4403 {
4404 Log(("sysexit: %04x:%016RX64 [efl=%#llx] -> %04x:%016RX64\n", pVCpu->cpum.GstCtx.cs, pVCpu->cpum.GstCtx.rip,
4405 pVCpu->cpum.GstCtx.rflags.u, (uNewCs | 3) + 32, pVCpu->cpum.GstCtx.rcx));
4406 pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.rdx;
4407 pVCpu->cpum.GstCtx.rsp = pVCpu->cpum.GstCtx.rcx;
4408 pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_L | X86DESCATTR_G | X86DESCATTR_P | X86DESCATTR_DT
4409 | X86DESCATTR_LIMIT_HIGH | X86_SEL_TYPE_ER_ACC | (3 << X86DESCATTR_DPL_SHIFT);
4410 pVCpu->cpum.GstCtx.cs.Sel = (uNewCs | 3) + 32;
4411 pVCpu->cpum.GstCtx.cs.ValidSel = (uNewCs | 3) + 32;
4412 pVCpu->cpum.GstCtx.ss.Sel = (uNewCs | 3) + 40;
4413 pVCpu->cpum.GstCtx.ss.ValidSel = (uNewCs | 3) + 40;
4414 }
4415 else
4416 {
4417 Log(("sysexit: %04x:%08RX64 [efl=%#llx] -> %04x:%08RX32\n", pVCpu->cpum.GstCtx.cs, pVCpu->cpum.GstCtx.rip,
4418 pVCpu->cpum.GstCtx.rflags.u, (uNewCs | 3) + 16, (uint32_t)pVCpu->cpum.GstCtx.edx));
4419 pVCpu->cpum.GstCtx.rip = pVCpu->cpum.GstCtx.edx;
4420 pVCpu->cpum.GstCtx.rsp = pVCpu->cpum.GstCtx.ecx;
4421 pVCpu->cpum.GstCtx.cs.Attr.u = X86DESCATTR_D | X86DESCATTR_G | X86DESCATTR_P | X86DESCATTR_DT
4422 | X86DESCATTR_LIMIT_HIGH | X86_SEL_TYPE_ER_ACC | (3 << X86DESCATTR_DPL_SHIFT);
4423 pVCpu->cpum.GstCtx.cs.Sel = (uNewCs | 3) + 16;
4424 pVCpu->cpum.GstCtx.cs.ValidSel = (uNewCs | 3) + 16;
4425 pVCpu->cpum.GstCtx.ss.Sel = (uNewCs | 3) + 24;
4426 pVCpu->cpum.GstCtx.ss.ValidSel = (uNewCs | 3) + 24;
4427 }
4428 pVCpu->cpum.GstCtx.cs.u64Base = 0;
4429 pVCpu->cpum.GstCtx.cs.u32Limit = UINT32_MAX;
4430 pVCpu->cpum.GstCtx.cs.fFlags = CPUMSELREG_FLAGS_VALID;
4431
4432 pVCpu->cpum.GstCtx.ss.u64Base = 0;
4433 pVCpu->cpum.GstCtx.ss.u32Limit = UINT32_MAX;
4434 pVCpu->cpum.GstCtx.ss.Attr.u = X86DESCATTR_D | X86DESCATTR_G | X86DESCATTR_P | X86DESCATTR_DT
4435 | X86DESCATTR_LIMIT_HIGH | X86_SEL_TYPE_RW_ACC | (3 << X86DESCATTR_DPL_SHIFT);
4436 pVCpu->cpum.GstCtx.ss.fFlags = CPUMSELREG_FLAGS_VALID;
4437 pVCpu->cpum.GstCtx.rflags.Bits.u1RF = 0;
4438
4439 pVCpu->iem.s.uCpl = 3;
4440
4441 /* Flush the prefetch buffer. */
4442#ifdef IEM_WITH_CODE_TLB
4443 pVCpu->iem.s.pbInstrBuf = NULL;
4444#else
4445 pVCpu->iem.s.cbOpcode = pVCpu->iem.s.offOpcode;
4446#endif
4447
4448 return VINF_SUCCESS;
4449}
4450
4451
4452/**
4453 * Common worker for 'pop SReg', 'mov SReg, GReg' and 'lXs GReg, reg/mem'.
4454 *
4455 * @param iSegReg The segment register number (valid).
4456 * @param uSel The new selector value.
4457 */
4458IEM_CIMPL_DEF_2(iemCImpl_LoadSReg, uint8_t, iSegReg, uint16_t, uSel)
4459{
4460 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_SREG_FROM_IDX(iSegReg));
4461 uint16_t *pSel = iemSRegRef(pVCpu, iSegReg);
4462 PCPUMSELREGHID pHid = iemSRegGetHid(pVCpu, iSegReg);
4463
4464 Assert(iSegReg <= X86_SREG_GS && iSegReg != X86_SREG_CS);
4465
4466 /*
4467 * Real mode and V8086 mode are easy.
4468 */
4469 if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
4470 {
4471 *pSel = uSel;
4472 pHid->u64Base = (uint32_t)uSel << 4;
4473 pHid->ValidSel = uSel;
4474 pHid->fFlags = CPUMSELREG_FLAGS_VALID;
4475#if 0 /* AMD Volume 2, chapter 4.1 - "real mode segmentation" - states that limit and attributes are untouched. */
4476 /** @todo Does the CPU actually load limits and attributes in the
4477 * real/V8086 mode segment load case? It doesn't for CS in far
4478 * jumps... Affects unreal mode. */
4479 pHid->u32Limit = 0xffff;
4480 pHid->Attr.u = 0;
4481 pHid->Attr.n.u1Present = 1;
4482 pHid->Attr.n.u1DescType = 1;
4483 pHid->Attr.n.u4Type = iSegReg != X86_SREG_CS
4484 ? X86_SEL_TYPE_RW
4485 : X86_SEL_TYPE_READ | X86_SEL_TYPE_CODE;
4486#endif
4487 CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_HIDDEN_SEL_REGS);
4488 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
4489 return VINF_SUCCESS;
4490 }
4491
4492 /*
4493 * Protected mode.
4494 *
4495 * Check if it's a null segment selector value first, that's OK for DS, ES,
4496 * FS and GS. If not null, then we have to load and parse the descriptor.
4497 */
4498 if (!(uSel & X86_SEL_MASK_OFF_RPL))
4499 {
4500 Assert(iSegReg != X86_SREG_CS); /** @todo testcase for \#UD on MOV CS, ax! */
4501 if (iSegReg == X86_SREG_SS)
4502 {
4503 /* In 64-bit kernel mode, the stack can be 0 because of the way
4504 interrupts are dispatched. AMD seems to have a slighly more
4505 relaxed relationship to SS.RPL than intel does. */
4506 /** @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! */
4507 if ( pVCpu->iem.s.enmCpuMode != IEMMODE_64BIT
4508 || pVCpu->iem.s.uCpl > 2
4509 || ( uSel != pVCpu->iem.s.uCpl
4510 && !IEM_IS_GUEST_CPU_AMD(pVCpu)) )
4511 {
4512 Log(("load sreg %#x -> invalid stack selector, #GP(0)\n", uSel));
4513 return iemRaiseGeneralProtectionFault0(pVCpu);
4514 }
4515 }
4516
4517 *pSel = uSel; /* Not RPL, remember :-) */
4518 iemHlpLoadNullDataSelectorProt(pVCpu, pHid, uSel);
4519 if (iSegReg == X86_SREG_SS)
4520 pHid->Attr.u |= pVCpu->iem.s.uCpl << X86DESCATTR_DPL_SHIFT;
4521
4522 Assert(CPUMSELREG_ARE_HIDDEN_PARTS_VALID(pVCpu, pHid));
4523 CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_HIDDEN_SEL_REGS);
4524
4525 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
4526 return VINF_SUCCESS;
4527 }
4528
4529 /* Fetch the descriptor. */
4530 IEMSELDESC Desc;
4531 VBOXSTRICTRC rcStrict = iemMemFetchSelDesc(pVCpu, &Desc, uSel, X86_XCPT_GP); /** @todo Correct exception? */
4532 if (rcStrict != VINF_SUCCESS)
4533 return rcStrict;
4534
4535 /* Check GPs first. */
4536 if (!Desc.Legacy.Gen.u1DescType)
4537 {
4538 Log(("load sreg %d (=%#x) - system selector (%#x) -> #GP\n", iSegReg, uSel, Desc.Legacy.Gen.u4Type));
4539 return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
4540 }
4541 if (iSegReg == X86_SREG_SS) /* SS gets different treatment */
4542 {
4543 if ( (Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE)
4544 || !(Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_WRITE) )
4545 {
4546 Log(("load sreg SS, %#x - code or read only (%#x) -> #GP\n", uSel, Desc.Legacy.Gen.u4Type));
4547 return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
4548 }
4549 if ((uSel & X86_SEL_RPL) != pVCpu->iem.s.uCpl)
4550 {
4551 Log(("load sreg SS, %#x - RPL and CPL (%d) differs -> #GP\n", uSel, pVCpu->iem.s.uCpl));
4552 return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
4553 }
4554 if (Desc.Legacy.Gen.u2Dpl != pVCpu->iem.s.uCpl)
4555 {
4556 Log(("load sreg SS, %#x - DPL (%d) and CPL (%d) differs -> #GP\n", uSel, Desc.Legacy.Gen.u2Dpl, pVCpu->iem.s.uCpl));
4557 return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
4558 }
4559 }
4560 else
4561 {
4562 if ((Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_READ)) == X86_SEL_TYPE_CODE)
4563 {
4564 Log(("load sreg%u, %#x - execute only segment -> #GP\n", iSegReg, uSel));
4565 return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
4566 }
4567 if ( (Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
4568 != (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
4569 {
4570#if 0 /* this is what intel says. */
4571 if ( (uSel & X86_SEL_RPL) > Desc.Legacy.Gen.u2Dpl
4572 && pVCpu->iem.s.uCpl > Desc.Legacy.Gen.u2Dpl)
4573 {
4574 Log(("load sreg%u, %#x - both RPL (%d) and CPL (%d) are greater than DPL (%d) -> #GP\n",
4575 iSegReg, uSel, (uSel & X86_SEL_RPL), pVCpu->iem.s.uCpl, Desc.Legacy.Gen.u2Dpl));
4576 return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
4577 }
4578#else /* this is what makes more sense. */
4579 if ((unsigned)(uSel & X86_SEL_RPL) > Desc.Legacy.Gen.u2Dpl)
4580 {
4581 Log(("load sreg%u, %#x - RPL (%d) is greater than DPL (%d) -> #GP\n",
4582 iSegReg, uSel, (uSel & X86_SEL_RPL), Desc.Legacy.Gen.u2Dpl));
4583 return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
4584 }
4585 if (pVCpu->iem.s.uCpl > Desc.Legacy.Gen.u2Dpl)
4586 {
4587 Log(("load sreg%u, %#x - CPL (%d) is greater than DPL (%d) -> #GP\n",
4588 iSegReg, uSel, pVCpu->iem.s.uCpl, Desc.Legacy.Gen.u2Dpl));
4589 return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uSel);
4590 }
4591#endif
4592 }
4593 }
4594
4595 /* Is it there? */
4596 if (!Desc.Legacy.Gen.u1Present)
4597 {
4598 Log(("load sreg%d,%#x - segment not present -> #NP\n", iSegReg, uSel));
4599 return iemRaiseSelectorNotPresentBySelector(pVCpu, uSel);
4600 }
4601
4602 /* The base and limit. */
4603 uint32_t cbLimit = X86DESC_LIMIT_G(&Desc.Legacy);
4604 uint64_t u64Base = X86DESC_BASE(&Desc.Legacy);
4605
4606 /*
4607 * Ok, everything checked out fine. Now set the accessed bit before
4608 * committing the result into the registers.
4609 */
4610 if (!(Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
4611 {
4612 rcStrict = iemMemMarkSelDescAccessed(pVCpu, uSel);
4613 if (rcStrict != VINF_SUCCESS)
4614 return rcStrict;
4615 Desc.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
4616 }
4617
4618 /* commit */
4619 *pSel = uSel;
4620 pHid->Attr.u = X86DESC_GET_HID_ATTR(&Desc.Legacy);
4621 pHid->u32Limit = cbLimit;
4622 pHid->u64Base = u64Base;
4623 pHid->ValidSel = uSel;
4624 pHid->fFlags = CPUMSELREG_FLAGS_VALID;
4625
4626 /** @todo check if the hidden bits are loaded correctly for 64-bit
4627 * mode. */
4628 Assert(CPUMSELREG_ARE_HIDDEN_PARTS_VALID(pVCpu, pHid));
4629
4630 CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_HIDDEN_SEL_REGS);
4631 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
4632 return VINF_SUCCESS;
4633}
4634
4635
4636/**
4637 * Implements 'mov SReg, r/m'.
4638 *
4639 * @param iSegReg The segment register number (valid).
4640 * @param uSel The new selector value.
4641 */
4642IEM_CIMPL_DEF_2(iemCImpl_load_SReg, uint8_t, iSegReg, uint16_t, uSel)
4643{
4644 VBOXSTRICTRC rcStrict = IEM_CIMPL_CALL_2(iemCImpl_LoadSReg, iSegReg, uSel);
4645 if (rcStrict == VINF_SUCCESS)
4646 {
4647 if (iSegReg == X86_SREG_SS)
4648 EMSetInhibitInterruptsPC(pVCpu, pVCpu->cpum.GstCtx.rip);
4649 }
4650 return rcStrict;
4651}
4652
4653
4654/**
4655 * Implements 'pop SReg'.
4656 *
4657 * @param iSegReg The segment register number (valid).
4658 * @param enmEffOpSize The efficient operand size (valid).
4659 */
4660IEM_CIMPL_DEF_2(iemCImpl_pop_Sreg, uint8_t, iSegReg, IEMMODE, enmEffOpSize)
4661{
4662 VBOXSTRICTRC rcStrict;
4663
4664 /*
4665 * Read the selector off the stack and join paths with mov ss, reg.
4666 */
4667 RTUINT64U TmpRsp;
4668 TmpRsp.u = pVCpu->cpum.GstCtx.rsp;
4669 switch (enmEffOpSize)
4670 {
4671 case IEMMODE_16BIT:
4672 {
4673 uint16_t uSel;
4674 rcStrict = iemMemStackPopU16Ex(pVCpu, &uSel, &TmpRsp);
4675 if (rcStrict == VINF_SUCCESS)
4676 rcStrict = IEM_CIMPL_CALL_2(iemCImpl_LoadSReg, iSegReg, uSel);
4677 break;
4678 }
4679
4680 case IEMMODE_32BIT:
4681 {
4682 uint32_t u32Value;
4683 rcStrict = iemMemStackPopU32Ex(pVCpu, &u32Value, &TmpRsp);
4684 if (rcStrict == VINF_SUCCESS)
4685 rcStrict = IEM_CIMPL_CALL_2(iemCImpl_LoadSReg, iSegReg, (uint16_t)u32Value);
4686 break;
4687 }
4688
4689 case IEMMODE_64BIT:
4690 {
4691 uint64_t u64Value;
4692 rcStrict = iemMemStackPopU64Ex(pVCpu, &u64Value, &TmpRsp);
4693 if (rcStrict == VINF_SUCCESS)
4694 rcStrict = IEM_CIMPL_CALL_2(iemCImpl_LoadSReg, iSegReg, (uint16_t)u64Value);
4695 break;
4696 }
4697 IEM_NOT_REACHED_DEFAULT_CASE_RET();
4698 }
4699
4700 /*
4701 * Commit the stack on success.
4702 */
4703 if (rcStrict == VINF_SUCCESS)
4704 {
4705 pVCpu->cpum.GstCtx.rsp = TmpRsp.u;
4706 if (iSegReg == X86_SREG_SS)
4707 EMSetInhibitInterruptsPC(pVCpu, pVCpu->cpum.GstCtx.rip);
4708 }
4709 return rcStrict;
4710}
4711
4712
4713/**
4714 * Implements lgs, lfs, les, lds & lss.
4715 */
4716IEM_CIMPL_DEF_5(iemCImpl_load_SReg_Greg,
4717 uint16_t, uSel,
4718 uint64_t, offSeg,
4719 uint8_t, iSegReg,
4720 uint8_t, iGReg,
4721 IEMMODE, enmEffOpSize)
4722{
4723 /*
4724 * Use iemCImpl_LoadSReg to do the tricky segment register loading.
4725 */
4726 /** @todo verify and test that mov, pop and lXs works the segment
4727 * register loading in the exact same way. */
4728 VBOXSTRICTRC rcStrict = IEM_CIMPL_CALL_2(iemCImpl_LoadSReg, iSegReg, uSel);
4729 if (rcStrict == VINF_SUCCESS)
4730 {
4731 switch (enmEffOpSize)
4732 {
4733 case IEMMODE_16BIT:
4734 *(uint16_t *)iemGRegRef(pVCpu, iGReg) = offSeg;
4735 break;
4736 case IEMMODE_32BIT:
4737 *(uint64_t *)iemGRegRef(pVCpu, iGReg) = offSeg;
4738 break;
4739 case IEMMODE_64BIT:
4740 *(uint64_t *)iemGRegRef(pVCpu, iGReg) = offSeg;
4741 break;
4742 IEM_NOT_REACHED_DEFAULT_CASE_RET();
4743 }
4744 }
4745
4746 return rcStrict;
4747}
4748
4749
4750/**
4751 * Helper for VERR, VERW, LAR, and LSL and loads the descriptor into memory.
4752 *
4753 * @retval VINF_SUCCESS on success.
4754 * @retval VINF_IEM_SELECTOR_NOT_OK if the selector isn't ok.
4755 * @retval iemMemFetchSysU64 return value.
4756 *
4757 * @param pVCpu The cross context virtual CPU structure of the calling thread.
4758 * @param uSel The selector value.
4759 * @param fAllowSysDesc Whether system descriptors are OK or not.
4760 * @param pDesc Where to return the descriptor on success.
4761 */
4762static VBOXSTRICTRC iemCImpl_LoadDescHelper(PVMCPUCC pVCpu, uint16_t uSel, bool fAllowSysDesc, PIEMSELDESC pDesc)
4763{
4764 pDesc->Long.au64[0] = 0;
4765 pDesc->Long.au64[1] = 0;
4766
4767 if (!(uSel & X86_SEL_MASK_OFF_RPL)) /** @todo test this on 64-bit. */
4768 return VINF_IEM_SELECTOR_NOT_OK;
4769
4770 /* Within the table limits? */
4771 RTGCPTR GCPtrBase;
4772 if (uSel & X86_SEL_LDT)
4773 {
4774 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_LDTR);
4775 if ( !pVCpu->cpum.GstCtx.ldtr.Attr.n.u1Present
4776 || (uSel | X86_SEL_RPL_LDT) > pVCpu->cpum.GstCtx.ldtr.u32Limit )
4777 return VINF_IEM_SELECTOR_NOT_OK;
4778 GCPtrBase = pVCpu->cpum.GstCtx.ldtr.u64Base;
4779 }
4780 else
4781 {
4782 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_GDTR);
4783 if ((uSel | X86_SEL_RPL_LDT) > pVCpu->cpum.GstCtx.gdtr.cbGdt)
4784 return VINF_IEM_SELECTOR_NOT_OK;
4785 GCPtrBase = pVCpu->cpum.GstCtx.gdtr.pGdt;
4786 }
4787
4788 /* Fetch the descriptor. */
4789 VBOXSTRICTRC rcStrict = iemMemFetchSysU64(pVCpu, &pDesc->Legacy.u, UINT8_MAX, GCPtrBase + (uSel & X86_SEL_MASK));
4790 if (rcStrict != VINF_SUCCESS)
4791 return rcStrict;
4792 if (!pDesc->Legacy.Gen.u1DescType)
4793 {
4794 if (!fAllowSysDesc)
4795 return VINF_IEM_SELECTOR_NOT_OK;
4796 if (CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu)))
4797 {
4798 rcStrict = iemMemFetchSysU64(pVCpu, &pDesc->Long.au64[1], UINT8_MAX, GCPtrBase + (uSel & X86_SEL_MASK) + 8);
4799 if (rcStrict != VINF_SUCCESS)
4800 return rcStrict;
4801 }
4802
4803 }
4804
4805 return VINF_SUCCESS;
4806}
4807
4808
4809/**
4810 * Implements verr (fWrite = false) and verw (fWrite = true).
4811 */
4812IEM_CIMPL_DEF_2(iemCImpl_VerX, uint16_t, uSel, bool, fWrite)
4813{
4814 Assert(!IEM_IS_REAL_OR_V86_MODE(pVCpu));
4815
4816 /** @todo figure whether the accessed bit is set or not. */
4817
4818 bool fAccessible = true;
4819 IEMSELDESC Desc;
4820 VBOXSTRICTRC rcStrict = iemCImpl_LoadDescHelper(pVCpu, uSel, false /*fAllowSysDesc*/, &Desc);
4821 if (rcStrict == VINF_SUCCESS)
4822 {
4823 /* Check the descriptor, order doesn't matter much here. */
4824 if ( !Desc.Legacy.Gen.u1DescType
4825 || !Desc.Legacy.Gen.u1Present)
4826 fAccessible = false;
4827 else
4828 {
4829 if ( fWrite
4830 ? (Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_WRITE)) != X86_SEL_TYPE_WRITE
4831 : (Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_READ)) == X86_SEL_TYPE_CODE)
4832 fAccessible = false;
4833
4834 /** @todo testcase for the conforming behavior. */
4835 if ( (Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
4836 != (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
4837 {
4838 if ((unsigned)(uSel & X86_SEL_RPL) > Desc.Legacy.Gen.u2Dpl)
4839 fAccessible = false;
4840 else if (pVCpu->iem.s.uCpl > Desc.Legacy.Gen.u2Dpl)
4841 fAccessible = false;
4842 }
4843 }
4844
4845 }
4846 else if (rcStrict == VINF_IEM_SELECTOR_NOT_OK)
4847 fAccessible = false;
4848 else
4849 return rcStrict;
4850
4851 /* commit */
4852 pVCpu->cpum.GstCtx.eflags.Bits.u1ZF = fAccessible;
4853
4854 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
4855 return VINF_SUCCESS;
4856}
4857
4858
4859/**
4860 * Implements LAR and LSL with 64-bit operand size.
4861 *
4862 * @returns VINF_SUCCESS.
4863 * @param pu16Dst Pointer to the destination register.
4864 * @param uSel The selector to load details for.
4865 * @param fIsLar true = LAR, false = LSL.
4866 */
4867IEM_CIMPL_DEF_3(iemCImpl_LarLsl_u64, uint64_t *, pu64Dst, uint16_t, uSel, bool, fIsLar)
4868{
4869 Assert(!IEM_IS_REAL_OR_V86_MODE(pVCpu));
4870
4871 /** @todo figure whether the accessed bit is set or not. */
4872
4873 bool fDescOk = true;
4874 IEMSELDESC Desc;
4875 VBOXSTRICTRC rcStrict = iemCImpl_LoadDescHelper(pVCpu, uSel, true /*fAllowSysDesc*/, &Desc);
4876 if (rcStrict == VINF_SUCCESS)
4877 {
4878 /*
4879 * Check the descriptor type.
4880 */
4881 if (!Desc.Legacy.Gen.u1DescType)
4882 {
4883 if (CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu)))
4884 {
4885 if (Desc.Long.Gen.u5Zeros)
4886 fDescOk = false;
4887 else
4888 switch (Desc.Long.Gen.u4Type)
4889 {
4890 /** @todo Intel lists 0 as valid for LSL, verify whether that's correct */
4891 case AMD64_SEL_TYPE_SYS_TSS_AVAIL:
4892 case AMD64_SEL_TYPE_SYS_TSS_BUSY:
4893 case AMD64_SEL_TYPE_SYS_LDT: /** @todo Intel lists this as invalid for LAR, AMD and 32-bit does otherwise. */
4894 break;
4895 case AMD64_SEL_TYPE_SYS_CALL_GATE:
4896 fDescOk = fIsLar;
4897 break;
4898 default:
4899 fDescOk = false;
4900 break;
4901 }
4902 }
4903 else
4904 {
4905 switch (Desc.Long.Gen.u4Type)
4906 {
4907 case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
4908 case X86_SEL_TYPE_SYS_286_TSS_BUSY:
4909 case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
4910 case X86_SEL_TYPE_SYS_386_TSS_BUSY:
4911 case X86_SEL_TYPE_SYS_LDT:
4912 break;
4913 case X86_SEL_TYPE_SYS_286_CALL_GATE:
4914 case X86_SEL_TYPE_SYS_TASK_GATE:
4915 case X86_SEL_TYPE_SYS_386_CALL_GATE:
4916 fDescOk = fIsLar;
4917 break;
4918 default:
4919 fDescOk = false;
4920 break;
4921 }
4922 }
4923 }
4924 if (fDescOk)
4925 {
4926 /*
4927 * Check the RPL/DPL/CPL interaction..
4928 */
4929 /** @todo testcase for the conforming behavior. */
4930 if ( (Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF)) != (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF)
4931 || !Desc.Legacy.Gen.u1DescType)
4932 {
4933 if ((unsigned)(uSel & X86_SEL_RPL) > Desc.Legacy.Gen.u2Dpl)
4934 fDescOk = false;
4935 else if (pVCpu->iem.s.uCpl > Desc.Legacy.Gen.u2Dpl)
4936 fDescOk = false;
4937 }
4938 }
4939
4940 if (fDescOk)
4941 {
4942 /*
4943 * All fine, start committing the result.
4944 */
4945 if (fIsLar)
4946 *pu64Dst = Desc.Legacy.au32[1] & UINT32_C(0x00ffff00);
4947 else
4948 *pu64Dst = X86DESC_LIMIT_G(&Desc.Legacy);
4949 }
4950
4951 }
4952 else if (rcStrict == VINF_IEM_SELECTOR_NOT_OK)
4953 fDescOk = false;
4954 else
4955 return rcStrict;
4956
4957 /* commit flags value and advance rip. */
4958 pVCpu->cpum.GstCtx.eflags.Bits.u1ZF = fDescOk;
4959 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
4960
4961 return VINF_SUCCESS;
4962}
4963
4964
4965/**
4966 * Implements LAR and LSL with 16-bit operand size.
4967 *
4968 * @returns VINF_SUCCESS.
4969 * @param pu16Dst Pointer to the destination register.
4970 * @param u16Sel The selector to load details for.
4971 * @param fIsLar true = LAR, false = LSL.
4972 */
4973IEM_CIMPL_DEF_3(iemCImpl_LarLsl_u16, uint16_t *, pu16Dst, uint16_t, uSel, bool, fIsLar)
4974{
4975 uint64_t u64TmpDst = *pu16Dst;
4976 IEM_CIMPL_CALL_3(iemCImpl_LarLsl_u64, &u64TmpDst, uSel, fIsLar);
4977 *pu16Dst = u64TmpDst;
4978 return VINF_SUCCESS;
4979}
4980
4981
4982/**
4983 * Implements lgdt.
4984 *
4985 * @param iEffSeg The segment of the new gdtr contents
4986 * @param GCPtrEffSrc The address of the new gdtr contents.
4987 * @param enmEffOpSize The effective operand size.
4988 */
4989IEM_CIMPL_DEF_3(iemCImpl_lgdt, uint8_t, iEffSeg, RTGCPTR, GCPtrEffSrc, IEMMODE, enmEffOpSize)
4990{
4991 if (pVCpu->iem.s.uCpl != 0)
4992 return iemRaiseGeneralProtectionFault0(pVCpu);
4993 Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
4994
4995 if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
4996 && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
4997 {
4998 Log(("lgdt: Guest intercept -> VM-exit\n"));
4999 IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_GDTR_IDTR_ACCESS, VMXINSTRID_LGDT, cbInstr);
5000 }
5001
5002 if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_GDTR_WRITES))
5003 {
5004 Log(("lgdt: Guest intercept -> #VMEXIT\n"));
5005 IEM_SVM_UPDATE_NRIP(pVCpu);
5006 IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_GDTR_WRITE, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
5007 }
5008
5009 /*
5010 * Fetch the limit and base address.
5011 */
5012 uint16_t cbLimit;
5013 RTGCPTR GCPtrBase;
5014 VBOXSTRICTRC rcStrict = iemMemFetchDataXdtr(pVCpu, &cbLimit, &GCPtrBase, iEffSeg, GCPtrEffSrc, enmEffOpSize);
5015 if (rcStrict == VINF_SUCCESS)
5016 {
5017 if ( pVCpu->iem.s.enmCpuMode != IEMMODE_64BIT
5018 || X86_IS_CANONICAL(GCPtrBase))
5019 {
5020 rcStrict = CPUMSetGuestGDTR(pVCpu, GCPtrBase, cbLimit);
5021 if (rcStrict == VINF_SUCCESS)
5022 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
5023 }
5024 else
5025 {
5026 Log(("iemCImpl_lgdt: Non-canonical base %04x:%RGv\n", cbLimit, GCPtrBase));
5027 return iemRaiseGeneralProtectionFault0(pVCpu);
5028 }
5029 }
5030 return rcStrict;
5031}
5032
5033
5034/**
5035 * Implements sgdt.
5036 *
5037 * @param iEffSeg The segment where to store the gdtr content.
5038 * @param GCPtrEffDst The address where to store the gdtr content.
5039 */
5040IEM_CIMPL_DEF_2(iemCImpl_sgdt, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
5041{
5042 /*
5043 * Join paths with sidt.
5044 * Note! No CPL or V8086 checks here, it's a really sad story, ask Intel if
5045 * you really must know.
5046 */
5047 if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
5048 && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
5049 {
5050 Log(("sgdt: Guest intercept -> VM-exit\n"));
5051 IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_GDTR_IDTR_ACCESS, VMXINSTRID_SGDT, cbInstr);
5052 }
5053
5054 if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_GDTR_READS))
5055 {
5056 Log(("sgdt: Guest intercept -> #VMEXIT\n"));
5057 IEM_SVM_UPDATE_NRIP(pVCpu);
5058 IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_GDTR_READ, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
5059 }
5060
5061 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_GDTR);
5062 VBOXSTRICTRC rcStrict = iemMemStoreDataXdtr(pVCpu, pVCpu->cpum.GstCtx.gdtr.cbGdt, pVCpu->cpum.GstCtx.gdtr.pGdt, iEffSeg, GCPtrEffDst);
5063 if (rcStrict == VINF_SUCCESS)
5064 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
5065 return rcStrict;
5066}
5067
5068
5069/**
5070 * Implements lidt.
5071 *
5072 * @param iEffSeg The segment of the new idtr contents
5073 * @param GCPtrEffSrc The address of the new idtr contents.
5074 * @param enmEffOpSize The effective operand size.
5075 */
5076IEM_CIMPL_DEF_3(iemCImpl_lidt, uint8_t, iEffSeg, RTGCPTR, GCPtrEffSrc, IEMMODE, enmEffOpSize)
5077{
5078 if (pVCpu->iem.s.uCpl != 0)
5079 return iemRaiseGeneralProtectionFault0(pVCpu);
5080 Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
5081
5082 if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_IDTR_WRITES))
5083 {
5084 Log(("lidt: Guest intercept -> #VMEXIT\n"));
5085 IEM_SVM_UPDATE_NRIP(pVCpu);
5086 IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_IDTR_WRITE, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
5087 }
5088
5089 /*
5090 * Fetch the limit and base address.
5091 */
5092 uint16_t cbLimit;
5093 RTGCPTR GCPtrBase;
5094 VBOXSTRICTRC rcStrict = iemMemFetchDataXdtr(pVCpu, &cbLimit, &GCPtrBase, iEffSeg, GCPtrEffSrc, enmEffOpSize);
5095 if (rcStrict == VINF_SUCCESS)
5096 {
5097 if ( pVCpu->iem.s.enmCpuMode != IEMMODE_64BIT
5098 || X86_IS_CANONICAL(GCPtrBase))
5099 {
5100 CPUMSetGuestIDTR(pVCpu, GCPtrBase, cbLimit);
5101 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
5102 }
5103 else
5104 {
5105 Log(("iemCImpl_lidt: Non-canonical base %04x:%RGv\n", cbLimit, GCPtrBase));
5106 return iemRaiseGeneralProtectionFault0(pVCpu);
5107 }
5108 }
5109 return rcStrict;
5110}
5111
5112
5113/**
5114 * Implements sidt.
5115 *
5116 * @param iEffSeg The segment where to store the idtr content.
5117 * @param GCPtrEffDst The address where to store the idtr content.
5118 */
5119IEM_CIMPL_DEF_2(iemCImpl_sidt, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
5120{
5121 /*
5122 * Join paths with sgdt.
5123 * Note! No CPL or V8086 checks here, it's a really sad story, ask Intel if
5124 * you really must know.
5125 */
5126 if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_IDTR_READS))
5127 {
5128 Log(("sidt: Guest intercept -> #VMEXIT\n"));
5129 IEM_SVM_UPDATE_NRIP(pVCpu);
5130 IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_IDTR_READ, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
5131 }
5132
5133 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_IDTR);
5134 VBOXSTRICTRC rcStrict = iemMemStoreDataXdtr(pVCpu, pVCpu->cpum.GstCtx.idtr.cbIdt, pVCpu->cpum.GstCtx.idtr.pIdt, iEffSeg, GCPtrEffDst);
5135 if (rcStrict == VINF_SUCCESS)
5136 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
5137 return rcStrict;
5138}
5139
5140
5141/**
5142 * Implements lldt.
5143 *
5144 * @param uNewLdt The new LDT selector value.
5145 */
5146IEM_CIMPL_DEF_1(iemCImpl_lldt, uint16_t, uNewLdt)
5147{
5148 /*
5149 * Check preconditions.
5150 */
5151 if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
5152 {
5153 Log(("lldt %04x - real or v8086 mode -> #GP(0)\n", uNewLdt));
5154 return iemRaiseUndefinedOpcode(pVCpu);
5155 }
5156 if (pVCpu->iem.s.uCpl != 0)
5157 {
5158 Log(("lldt %04x - CPL is %d -> #GP(0)\n", uNewLdt, pVCpu->iem.s.uCpl));
5159 return iemRaiseGeneralProtectionFault0(pVCpu);
5160 }
5161 /* Nested-guest VMX intercept. */
5162 if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
5163 && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
5164 {
5165 Log(("lldt: Guest intercept -> VM-exit\n"));
5166 IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_LDTR_TR_ACCESS, VMXINSTRID_LLDT, cbInstr);
5167 }
5168 if (uNewLdt & X86_SEL_LDT)
5169 {
5170 Log(("lldt %04x - LDT selector -> #GP\n", uNewLdt));
5171 return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewLdt);
5172 }
5173
5174 /*
5175 * Now, loading a NULL selector is easy.
5176 */
5177 if (!(uNewLdt & X86_SEL_MASK_OFF_RPL))
5178 {
5179 /* Nested-guest SVM intercept. */
5180 if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_LDTR_WRITES))
5181 {
5182 Log(("lldt: Guest intercept -> #VMEXIT\n"));
5183 IEM_SVM_UPDATE_NRIP(pVCpu);
5184 IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_LDTR_WRITE, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
5185 }
5186
5187 Log(("lldt %04x: Loading NULL selector.\n", uNewLdt));
5188 pVCpu->cpum.GstCtx.fExtrn &= ~CPUMCTX_EXTRN_LDTR;
5189 CPUMSetGuestLDTR(pVCpu, uNewLdt);
5190 pVCpu->cpum.GstCtx.ldtr.ValidSel = uNewLdt;
5191 pVCpu->cpum.GstCtx.ldtr.fFlags = CPUMSELREG_FLAGS_VALID;
5192 if (IEM_IS_GUEST_CPU_AMD(pVCpu))
5193 {
5194 /* AMD-V seems to leave the base and limit alone. */
5195 pVCpu->cpum.GstCtx.ldtr.Attr.u = X86DESCATTR_UNUSABLE;
5196 }
5197 else
5198 {
5199 /* VT-x (Intel 3960x) seems to be doing the following. */
5200 pVCpu->cpum.GstCtx.ldtr.Attr.u = X86DESCATTR_UNUSABLE | X86DESCATTR_G | X86DESCATTR_D;
5201 pVCpu->cpum.GstCtx.ldtr.u64Base = 0;
5202 pVCpu->cpum.GstCtx.ldtr.u32Limit = UINT32_MAX;
5203 }
5204
5205 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
5206 return VINF_SUCCESS;
5207 }
5208
5209 /*
5210 * Read the descriptor.
5211 */
5212 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_LDTR | CPUMCTX_EXTRN_GDTR);
5213 IEMSELDESC Desc;
5214 VBOXSTRICTRC rcStrict = iemMemFetchSelDesc(pVCpu, &Desc, uNewLdt, X86_XCPT_GP); /** @todo Correct exception? */
5215 if (rcStrict != VINF_SUCCESS)
5216 return rcStrict;
5217
5218 /* Check GPs first. */
5219 if (Desc.Legacy.Gen.u1DescType)
5220 {
5221 Log(("lldt %#x - not system selector (type %x) -> #GP\n", uNewLdt, Desc.Legacy.Gen.u4Type));
5222 return iemRaiseGeneralProtectionFault(pVCpu, uNewLdt & X86_SEL_MASK_OFF_RPL);
5223 }
5224 if (Desc.Legacy.Gen.u4Type != X86_SEL_TYPE_SYS_LDT)
5225 {
5226 Log(("lldt %#x - not LDT selector (type %x) -> #GP\n", uNewLdt, Desc.Legacy.Gen.u4Type));
5227 return iemRaiseGeneralProtectionFault(pVCpu, uNewLdt & X86_SEL_MASK_OFF_RPL);
5228 }
5229 uint64_t u64Base;
5230 if (!IEM_IS_LONG_MODE(pVCpu))
5231 u64Base = X86DESC_BASE(&Desc.Legacy);
5232 else
5233 {
5234 if (Desc.Long.Gen.u5Zeros)
5235 {
5236 Log(("lldt %#x - u5Zeros=%#x -> #GP\n", uNewLdt, Desc.Long.Gen.u5Zeros));
5237 return iemRaiseGeneralProtectionFault(pVCpu, uNewLdt & X86_SEL_MASK_OFF_RPL);
5238 }
5239
5240 u64Base = X86DESC64_BASE(&Desc.Long);
5241 if (!IEM_IS_CANONICAL(u64Base))
5242 {
5243 Log(("lldt %#x - non-canonical base address %#llx -> #GP\n", uNewLdt, u64Base));
5244 return iemRaiseGeneralProtectionFault(pVCpu, uNewLdt & X86_SEL_MASK_OFF_RPL);
5245 }
5246 }
5247
5248 /* NP */
5249 if (!Desc.Legacy.Gen.u1Present)
5250 {
5251 Log(("lldt %#x - segment not present -> #NP\n", uNewLdt));
5252 return iemRaiseSelectorNotPresentBySelector(pVCpu, uNewLdt);
5253 }
5254
5255 /* Nested-guest SVM intercept. */
5256 if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_LDTR_WRITES))
5257 {
5258 Log(("lldt: Guest intercept -> #VMEXIT\n"));
5259 IEM_SVM_UPDATE_NRIP(pVCpu);
5260 IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_LDTR_WRITE, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
5261 }
5262
5263 /*
5264 * It checks out alright, update the registers.
5265 */
5266/** @todo check if the actual value is loaded or if the RPL is dropped */
5267 CPUMSetGuestLDTR(pVCpu, uNewLdt & X86_SEL_MASK_OFF_RPL);
5268 pVCpu->cpum.GstCtx.ldtr.ValidSel = uNewLdt & X86_SEL_MASK_OFF_RPL;
5269 pVCpu->cpum.GstCtx.ldtr.fFlags = CPUMSELREG_FLAGS_VALID;
5270 pVCpu->cpum.GstCtx.ldtr.Attr.u = X86DESC_GET_HID_ATTR(&Desc.Legacy);
5271 pVCpu->cpum.GstCtx.ldtr.u32Limit = X86DESC_LIMIT_G(&Desc.Legacy);
5272 pVCpu->cpum.GstCtx.ldtr.u64Base = u64Base;
5273
5274 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
5275 return VINF_SUCCESS;
5276}
5277
5278
5279/**
5280 * Implements sldt GReg
5281 *
5282 * @param iGReg The general register to store the CRx value in.
5283 * @param enmEffOpSize The operand size.
5284 */
5285IEM_CIMPL_DEF_2(iemCImpl_sldt_reg, uint8_t, iGReg, uint8_t, enmEffOpSize)
5286{
5287 if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
5288 && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
5289 {
5290 Log(("sldt: Guest intercept -> VM-exit\n"));
5291 IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_LDTR_TR_ACCESS, VMXINSTRID_SLDT, cbInstr);
5292 }
5293
5294 IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_LDTR_READS, SVM_EXIT_LDTR_READ, 0, 0);
5295
5296 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_LDTR);
5297 switch (enmEffOpSize)
5298 {
5299 case IEMMODE_16BIT: *(uint16_t *)iemGRegRef(pVCpu, iGReg) = pVCpu->cpum.GstCtx.ldtr.Sel; break;
5300 case IEMMODE_32BIT: *(uint64_t *)iemGRegRef(pVCpu, iGReg) = pVCpu->cpum.GstCtx.ldtr.Sel; break;
5301 case IEMMODE_64BIT: *(uint64_t *)iemGRegRef(pVCpu, iGReg) = pVCpu->cpum.GstCtx.ldtr.Sel; break;
5302 IEM_NOT_REACHED_DEFAULT_CASE_RET();
5303 }
5304 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
5305 return VINF_SUCCESS;
5306}
5307
5308
5309/**
5310 * Implements sldt mem.
5311 *
5312 * @param iGReg The general register to store the CRx value in.
5313 * @param iEffSeg The effective segment register to use with @a GCPtrMem.
5314 * @param GCPtrEffDst Where to store the 16-bit CR0 value.
5315 */
5316IEM_CIMPL_DEF_2(iemCImpl_sldt_mem, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
5317{
5318 IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_LDTR_READS, SVM_EXIT_LDTR_READ, 0, 0);
5319
5320 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_LDTR);
5321 VBOXSTRICTRC rcStrict = iemMemStoreDataU16(pVCpu, iEffSeg, GCPtrEffDst, pVCpu->cpum.GstCtx.ldtr.Sel);
5322 if (rcStrict == VINF_SUCCESS)
5323 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
5324 return rcStrict;
5325}
5326
5327
5328/**
5329 * Implements ltr.
5330 *
5331 * @param uNewTr The new TSS selector value.
5332 */
5333IEM_CIMPL_DEF_1(iemCImpl_ltr, uint16_t, uNewTr)
5334{
5335 /*
5336 * Check preconditions.
5337 */
5338 if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
5339 {
5340 Log(("ltr %04x - real or v8086 mode -> #GP(0)\n", uNewTr));
5341 return iemRaiseUndefinedOpcode(pVCpu);
5342 }
5343 if (pVCpu->iem.s.uCpl != 0)
5344 {
5345 Log(("ltr %04x - CPL is %d -> #GP(0)\n", uNewTr, pVCpu->iem.s.uCpl));
5346 return iemRaiseGeneralProtectionFault0(pVCpu);
5347 }
5348 if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
5349 && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
5350 {
5351 Log(("ltr: Guest intercept -> VM-exit\n"));
5352 IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_LDTR_TR_ACCESS, VMXINSTRID_LTR, cbInstr);
5353 }
5354 if (uNewTr & X86_SEL_LDT)
5355 {
5356 Log(("ltr %04x - LDT selector -> #GP\n", uNewTr));
5357 return iemRaiseGeneralProtectionFaultBySelector(pVCpu, uNewTr);
5358 }
5359 if (!(uNewTr & X86_SEL_MASK_OFF_RPL))
5360 {
5361 Log(("ltr %04x - NULL selector -> #GP(0)\n", uNewTr));
5362 return iemRaiseGeneralProtectionFault0(pVCpu);
5363 }
5364 if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_TR_WRITES))
5365 {
5366 Log(("ltr: Guest intercept -> #VMEXIT\n"));
5367 IEM_SVM_UPDATE_NRIP(pVCpu);
5368 IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_TR_WRITE, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
5369 }
5370
5371 /*
5372 * Read the descriptor.
5373 */
5374 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_LDTR | CPUMCTX_EXTRN_GDTR | CPUMCTX_EXTRN_TR);
5375 IEMSELDESC Desc;
5376 VBOXSTRICTRC rcStrict = iemMemFetchSelDesc(pVCpu, &Desc, uNewTr, X86_XCPT_GP); /** @todo Correct exception? */
5377 if (rcStrict != VINF_SUCCESS)
5378 return rcStrict;
5379
5380 /* Check GPs first. */
5381 if (Desc.Legacy.Gen.u1DescType)
5382 {
5383 Log(("ltr %#x - not system selector (type %x) -> #GP\n", uNewTr, Desc.Legacy.Gen.u4Type));
5384 return iemRaiseGeneralProtectionFault(pVCpu, uNewTr & X86_SEL_MASK_OFF_RPL);
5385 }
5386 if ( Desc.Legacy.Gen.u4Type != X86_SEL_TYPE_SYS_386_TSS_AVAIL /* same as AMD64_SEL_TYPE_SYS_TSS_AVAIL */
5387 && ( Desc.Legacy.Gen.u4Type != X86_SEL_TYPE_SYS_286_TSS_AVAIL
5388 || IEM_IS_LONG_MODE(pVCpu)) )
5389 {
5390 Log(("ltr %#x - not an available TSS selector (type %x) -> #GP\n", uNewTr, Desc.Legacy.Gen.u4Type));
5391 return iemRaiseGeneralProtectionFault(pVCpu, uNewTr & X86_SEL_MASK_OFF_RPL);
5392 }
5393 uint64_t u64Base;
5394 if (!IEM_IS_LONG_MODE(pVCpu))
5395 u64Base = X86DESC_BASE(&Desc.Legacy);
5396 else
5397 {
5398 if (Desc.Long.Gen.u5Zeros)
5399 {
5400 Log(("ltr %#x - u5Zeros=%#x -> #GP\n", uNewTr, Desc.Long.Gen.u5Zeros));
5401 return iemRaiseGeneralProtectionFault(pVCpu, uNewTr & X86_SEL_MASK_OFF_RPL);
5402 }
5403
5404 u64Base = X86DESC64_BASE(&Desc.Long);
5405 if (!IEM_IS_CANONICAL(u64Base))
5406 {
5407 Log(("ltr %#x - non-canonical base address %#llx -> #GP\n", uNewTr, u64Base));
5408 return iemRaiseGeneralProtectionFault(pVCpu, uNewTr & X86_SEL_MASK_OFF_RPL);
5409 }
5410 }
5411
5412 /* NP */
5413 if (!Desc.Legacy.Gen.u1Present)
5414 {
5415 Log(("ltr %#x - segment not present -> #NP\n", uNewTr));
5416 return iemRaiseSelectorNotPresentBySelector(pVCpu, uNewTr);
5417 }
5418
5419 /*
5420 * Set it busy.
5421 * Note! Intel says this should lock down the whole descriptor, but we'll
5422 * restrict our selves to 32-bit for now due to lack of inline
5423 * assembly and such.
5424 */
5425 void *pvDesc;
5426 rcStrict = iemMemMap(pVCpu, &pvDesc, 8, UINT8_MAX, pVCpu->cpum.GstCtx.gdtr.pGdt + (uNewTr & X86_SEL_MASK_OFF_RPL), IEM_ACCESS_DATA_RW);
5427 if (rcStrict != VINF_SUCCESS)
5428 return rcStrict;
5429 switch ((uintptr_t)pvDesc & 3)
5430 {
5431 case 0: ASMAtomicBitSet(pvDesc, 40 + 1); break;
5432 case 1: ASMAtomicBitSet((uint8_t *)pvDesc + 3, 40 + 1 - 24); break;
5433 case 2: ASMAtomicBitSet((uint8_t *)pvDesc + 2, 40 + 1 - 16); break;
5434 case 3: ASMAtomicBitSet((uint8_t *)pvDesc + 1, 40 + 1 - 8); break;
5435 }
5436 rcStrict = iemMemCommitAndUnmap(pVCpu, pvDesc, IEM_ACCESS_DATA_RW);
5437 if (rcStrict != VINF_SUCCESS)
5438 return rcStrict;
5439 Desc.Legacy.Gen.u4Type |= X86_SEL_TYPE_SYS_TSS_BUSY_MASK;
5440
5441 /*
5442 * It checks out alright, update the registers.
5443 */
5444/** @todo check if the actual value is loaded or if the RPL is dropped */
5445 CPUMSetGuestTR(pVCpu, uNewTr & X86_SEL_MASK_OFF_RPL);
5446 pVCpu->cpum.GstCtx.tr.ValidSel = uNewTr & X86_SEL_MASK_OFF_RPL;
5447 pVCpu->cpum.GstCtx.tr.fFlags = CPUMSELREG_FLAGS_VALID;
5448 pVCpu->cpum.GstCtx.tr.Attr.u = X86DESC_GET_HID_ATTR(&Desc.Legacy);
5449 pVCpu->cpum.GstCtx.tr.u32Limit = X86DESC_LIMIT_G(&Desc.Legacy);
5450 pVCpu->cpum.GstCtx.tr.u64Base = u64Base;
5451
5452 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
5453 return VINF_SUCCESS;
5454}
5455
5456
5457/**
5458 * Implements str GReg
5459 *
5460 * @param iGReg The general register to store the CRx value in.
5461 * @param enmEffOpSize The operand size.
5462 */
5463IEM_CIMPL_DEF_2(iemCImpl_str_reg, uint8_t, iGReg, uint8_t, enmEffOpSize)
5464{
5465 if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
5466 && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
5467 {
5468 Log(("str_reg: Guest intercept -> VM-exit\n"));
5469 IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_LDTR_TR_ACCESS, VMXINSTRID_STR, cbInstr);
5470 }
5471
5472 IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_TR_READS, SVM_EXIT_TR_READ, 0, 0);
5473
5474 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_TR);
5475 switch (enmEffOpSize)
5476 {
5477 case IEMMODE_16BIT: *(uint16_t *)iemGRegRef(pVCpu, iGReg) = pVCpu->cpum.GstCtx.tr.Sel; break;
5478 case IEMMODE_32BIT: *(uint64_t *)iemGRegRef(pVCpu, iGReg) = pVCpu->cpum.GstCtx.tr.Sel; break;
5479 case IEMMODE_64BIT: *(uint64_t *)iemGRegRef(pVCpu, iGReg) = pVCpu->cpum.GstCtx.tr.Sel; break;
5480 IEM_NOT_REACHED_DEFAULT_CASE_RET();
5481 }
5482 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
5483 return VINF_SUCCESS;
5484}
5485
5486
5487/**
5488 * Implements str mem.
5489 *
5490 * @param iGReg The general register to store the CRx value in.
5491 * @param iEffSeg The effective segment register to use with @a GCPtrMem.
5492 * @param GCPtrEffDst Where to store the 16-bit CR0 value.
5493 */
5494IEM_CIMPL_DEF_2(iemCImpl_str_mem, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
5495{
5496 if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
5497 && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_DESC_TABLE_EXIT))
5498 {
5499 Log(("str_mem: Guest intercept -> VM-exit\n"));
5500 IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_LDTR_TR_ACCESS, VMXINSTRID_STR, cbInstr);
5501 }
5502
5503 IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_TR_READS, SVM_EXIT_TR_READ, 0, 0);
5504
5505 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_TR);
5506 VBOXSTRICTRC rcStrict = iemMemStoreDataU16(pVCpu, iEffSeg, GCPtrEffDst, pVCpu->cpum.GstCtx.tr.Sel);
5507 if (rcStrict == VINF_SUCCESS)
5508 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
5509 return rcStrict;
5510}
5511
5512
5513/**
5514 * Implements mov GReg,CRx.
5515 *
5516 * @param iGReg The general register to store the CRx value in.
5517 * @param iCrReg The CRx register to read (valid).
5518 */
5519IEM_CIMPL_DEF_2(iemCImpl_mov_Rd_Cd, uint8_t, iGReg, uint8_t, iCrReg)
5520{
5521 if (pVCpu->iem.s.uCpl != 0)
5522 return iemRaiseGeneralProtectionFault0(pVCpu);
5523 Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
5524
5525 if (IEM_SVM_IS_READ_CR_INTERCEPT_SET(pVCpu, iCrReg))
5526 {
5527 Log(("iemCImpl_mov_Rd_Cd: Guest intercept CR%u -> #VMEXIT\n", iCrReg));
5528 IEM_SVM_UPDATE_NRIP(pVCpu);
5529 IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_READ_CR0 + iCrReg, IEMACCESSCRX_MOV_CRX, iGReg);
5530 }
5531
5532 /* Read it. */
5533 uint64_t crX;
5534 switch (iCrReg)
5535 {
5536 case 0:
5537 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0);
5538 crX = pVCpu->cpum.GstCtx.cr0;
5539 if (IEM_GET_TARGET_CPU(pVCpu) <= IEMTARGETCPU_386)
5540 crX |= UINT32_C(0x7fffffe0); /* All reserved CR0 flags are set on a 386, just like MSW on 286. */
5541 break;
5542 case 2:
5543 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_CR2);
5544 crX = pVCpu->cpum.GstCtx.cr2;
5545 break;
5546 case 3:
5547 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR3);
5548 crX = pVCpu->cpum.GstCtx.cr3;
5549 break;
5550 case 4:
5551 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR4);
5552 crX = pVCpu->cpum.GstCtx.cr4;
5553 break;
5554 case 8:
5555 {
5556 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_APIC_TPR);
5557#ifdef VBOX_WITH_NESTED_HWVIRT_VMX
5558 if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
5559 {
5560 VBOXSTRICTRC rcStrict = iemVmxVmexitInstrMovFromCr8(pVCpu, iGReg, cbInstr);
5561 if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
5562 return rcStrict;
5563
5564 /*
5565 * If the Mov-from-CR8 doesn't cause a VM-exit, bits 7:4 of the VTPR is copied
5566 * to bits 0:3 of the destination operand. Bits 63:4 of the destination operand
5567 * are cleared.
5568 *
5569 * See Intel Spec. 29.3 "Virtualizing CR8-based TPR Accesses"
5570 */
5571 if (IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_USE_TPR_SHADOW))
5572 {
5573 uint32_t const uTpr = iemVmxVirtApicReadRaw32(pVCpu, XAPIC_OFF_TPR);
5574 crX = (uTpr >> 4) & 0xf;
5575 break;
5576 }
5577 }
5578#endif
5579#ifdef VBOX_WITH_NESTED_HWVIRT_SVM
5580 if (CPUMIsGuestInSvmNestedHwVirtMode(IEM_GET_CTX(pVCpu)))
5581 {
5582 PCSVMVMCBCTRL pVmcbCtrl = &pVCpu->cpum.GstCtx.hwvirt.svm.Vmcb.ctrl;
5583 if (CPUMIsGuestSvmVirtIntrMasking(pVCpu, IEM_GET_CTX(pVCpu)))
5584 {
5585 crX = pVmcbCtrl->IntCtrl.n.u8VTPR & 0xf;
5586 break;
5587 }
5588 }
5589#endif
5590 uint8_t uTpr;
5591 int rc = APICGetTpr(pVCpu, &uTpr, NULL, NULL);
5592 if (RT_SUCCESS(rc))
5593 crX = uTpr >> 4;
5594 else
5595 crX = 0;
5596 break;
5597 }
5598 IEM_NOT_REACHED_DEFAULT_CASE_RET(); /* call checks */
5599 }
5600
5601#ifdef VBOX_WITH_NESTED_HWVIRT_VMX
5602 if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
5603 {
5604 switch (iCrReg)
5605 {
5606 /* CR0/CR4 reads are subject to masking when in VMX non-root mode. */
5607 case 0: crX = CPUMGetGuestVmxMaskedCr0(&pVCpu->cpum.GstCtx, pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64Cr0Mask.u); break;
5608 case 4: crX = CPUMGetGuestVmxMaskedCr4(&pVCpu->cpum.GstCtx, pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64Cr4Mask.u); break;
5609
5610 case 3:
5611 {
5612 VBOXSTRICTRC rcStrict = iemVmxVmexitInstrMovFromCr3(pVCpu, iGReg, cbInstr);
5613 if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
5614 return rcStrict;
5615 break;
5616 }
5617 }
5618 }
5619#endif
5620
5621 /* Store it. */
5622 if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
5623 *(uint64_t *)iemGRegRef(pVCpu, iGReg) = crX;
5624 else
5625 *(uint64_t *)iemGRegRef(pVCpu, iGReg) = (uint32_t)crX;
5626
5627 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
5628 return VINF_SUCCESS;
5629}
5630
5631
5632/**
5633 * Implements smsw GReg.
5634 *
5635 * @param iGReg The general register to store the CRx value in.
5636 * @param enmEffOpSize The operand size.
5637 */
5638IEM_CIMPL_DEF_2(iemCImpl_smsw_reg, uint8_t, iGReg, uint8_t, enmEffOpSize)
5639{
5640 IEM_SVM_CHECK_READ_CR0_INTERCEPT(pVCpu, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
5641
5642#ifdef VBOX_WITH_NESTED_HWVIRT_VMX
5643 uint64_t u64MaskedCr0;
5644 if (!IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
5645 u64MaskedCr0 = pVCpu->cpum.GstCtx.cr0;
5646 else
5647 u64MaskedCr0 = CPUMGetGuestVmxMaskedCr0(&pVCpu->cpum.GstCtx, pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64Cr0Mask.u);
5648 uint64_t const u64GuestCr0 = u64MaskedCr0;
5649#else
5650 uint64_t const u64GuestCr0 = pVCpu->cpum.GstCtx.cr0;
5651#endif
5652
5653 switch (enmEffOpSize)
5654 {
5655 case IEMMODE_16BIT:
5656 if (IEM_GET_TARGET_CPU(pVCpu) > IEMTARGETCPU_386)
5657 *(uint16_t *)iemGRegRef(pVCpu, iGReg) = (uint16_t)u64GuestCr0;
5658 else if (IEM_GET_TARGET_CPU(pVCpu) >= IEMTARGETCPU_386)
5659 *(uint16_t *)iemGRegRef(pVCpu, iGReg) = (uint16_t)u64GuestCr0 | 0xffe0;
5660 else
5661 *(uint16_t *)iemGRegRef(pVCpu, iGReg) = (uint16_t)u64GuestCr0 | 0xfff0;
5662 break;
5663
5664 case IEMMODE_32BIT:
5665 *(uint32_t *)iemGRegRef(pVCpu, iGReg) = (uint32_t)u64GuestCr0;
5666 break;
5667
5668 case IEMMODE_64BIT:
5669 *(uint64_t *)iemGRegRef(pVCpu, iGReg) = u64GuestCr0;
5670 break;
5671
5672 IEM_NOT_REACHED_DEFAULT_CASE_RET();
5673 }
5674
5675 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
5676 return VINF_SUCCESS;
5677}
5678
5679
5680/**
5681 * Implements smsw mem.
5682 *
5683 * @param iGReg The general register to store the CR0 value in.
5684 * @param iEffSeg The effective segment register to use with @a GCPtrMem.
5685 * @param GCPtrEffDst Where to store the 16-bit CR0 value.
5686 */
5687IEM_CIMPL_DEF_2(iemCImpl_smsw_mem, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
5688{
5689 IEM_SVM_CHECK_READ_CR0_INTERCEPT(pVCpu, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
5690
5691#ifdef VBOX_WITH_NESTED_HWVIRT_VMX
5692 uint64_t u64MaskedCr0;
5693 if (!IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
5694 u64MaskedCr0 = pVCpu->cpum.GstCtx.cr0;
5695 else
5696 u64MaskedCr0 = CPUMGetGuestVmxMaskedCr0(&pVCpu->cpum.GstCtx, pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64Cr0Mask.u);
5697 uint64_t const u64GuestCr0 = u64MaskedCr0;
5698#else
5699 uint64_t const u64GuestCr0 = pVCpu->cpum.GstCtx.cr0;
5700#endif
5701
5702 uint16_t u16Value;
5703 if (IEM_GET_TARGET_CPU(pVCpu) > IEMTARGETCPU_386)
5704 u16Value = (uint16_t)u64GuestCr0;
5705 else if (IEM_GET_TARGET_CPU(pVCpu) >= IEMTARGETCPU_386)
5706 u16Value = (uint16_t)u64GuestCr0 | 0xffe0;
5707 else
5708 u16Value = (uint16_t)u64GuestCr0 | 0xfff0;
5709
5710 VBOXSTRICTRC rcStrict = iemMemStoreDataU16(pVCpu, iEffSeg, GCPtrEffDst, u16Value);
5711 if (rcStrict == VINF_SUCCESS)
5712 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
5713 return rcStrict;
5714}
5715
5716
5717/**
5718 * Helper for mapping CR3 and PAE PDPEs for 'mov CRx,GReg'.
5719 */
5720#define IEM_MAP_PAE_PDPES_AT_CR3_RET(a_pVCpu, a_iCrReg, a_uCr3) \
5721 do \
5722 { \
5723 int const rcX = PGMGstMapPaePdpesAtCr3(a_pVCpu, a_uCr3); \
5724 if (RT_SUCCESS(rcX)) \
5725 { /* likely */ } \
5726 else \
5727 { \
5728 /* Either invalid PDPTEs or CR3 second-level translation failed. Raise #GP(0) either way. */ \
5729 Log(("iemCImpl_load_Cr%#x: Trying to load invalid PAE PDPEs\n", a_iCrReg)); \
5730 return iemRaiseGeneralProtectionFault0(a_pVCpu); \
5731 } \
5732 } while (0)
5733
5734
5735/**
5736 * Used to implemented 'mov CRx,GReg' and 'lmsw r/m16'.
5737 *
5738 * @param iCrReg The CRx register to write (valid).
5739 * @param uNewCrX The new value.
5740 * @param enmAccessCrx The instruction that caused the CrX load.
5741 * @param iGReg The general register in case of a 'mov CRx,GReg'
5742 * instruction.
5743 */
5744IEM_CIMPL_DEF_4(iemCImpl_load_CrX, uint8_t, iCrReg, uint64_t, uNewCrX, IEMACCESSCRX, enmAccessCrX, uint8_t, iGReg)
5745{
5746 VBOXSTRICTRC rcStrict;
5747 int rc;
5748#ifndef VBOX_WITH_NESTED_HWVIRT_SVM
5749 RT_NOREF2(iGReg, enmAccessCrX);
5750#endif
5751
5752 /*
5753 * Try store it.
5754 * Unfortunately, CPUM only does a tiny bit of the work.
5755 */
5756 switch (iCrReg)
5757 {
5758 case 0:
5759 {
5760 /*
5761 * Perform checks.
5762 */
5763 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0);
5764
5765 uint64_t const uOldCrX = pVCpu->cpum.GstCtx.cr0;
5766 uint32_t const fValid = CPUMGetGuestCR0ValidMask();
5767
5768 /* ET is hardcoded on 486 and later. */
5769 if (IEM_GET_TARGET_CPU(pVCpu) > IEMTARGETCPU_486)
5770 uNewCrX |= X86_CR0_ET;
5771 /* The 386 and 486 didn't #GP(0) on attempting to set reserved CR0 bits. ET was settable on 386. */
5772 else if (IEM_GET_TARGET_CPU(pVCpu) == IEMTARGETCPU_486)
5773 {
5774 uNewCrX &= fValid;
5775 uNewCrX |= X86_CR0_ET;
5776 }
5777 else
5778 uNewCrX &= X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG | X86_CR0_ET;
5779
5780 /* Check for reserved bits. */
5781 if (uNewCrX & ~(uint64_t)fValid)
5782 {
5783 Log(("Trying to set reserved CR0 bits: NewCR0=%#llx InvalidBits=%#llx\n", uNewCrX, uNewCrX & ~(uint64_t)fValid));
5784 return iemRaiseGeneralProtectionFault0(pVCpu);
5785 }
5786
5787 /* Check for invalid combinations. */
5788 if ( (uNewCrX & X86_CR0_PG)
5789 && !(uNewCrX & X86_CR0_PE) )
5790 {
5791 Log(("Trying to set CR0.PG without CR0.PE\n"));
5792 return iemRaiseGeneralProtectionFault0(pVCpu);
5793 }
5794
5795 if ( !(uNewCrX & X86_CR0_CD)
5796 && (uNewCrX & X86_CR0_NW) )
5797 {
5798 Log(("Trying to clear CR0.CD while leaving CR0.NW set\n"));
5799 return iemRaiseGeneralProtectionFault0(pVCpu);
5800 }
5801
5802 if ( !(uNewCrX & X86_CR0_PG)
5803 && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_PCIDE))
5804 {
5805 Log(("Trying to clear CR0.PG while leaving CR4.PCID set\n"));
5806 return iemRaiseGeneralProtectionFault0(pVCpu);
5807 }
5808
5809 /* Long mode consistency checks. */
5810 if ( (uNewCrX & X86_CR0_PG)
5811 && !(uOldCrX & X86_CR0_PG)
5812 && (pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_LME) )
5813 {
5814 if (!(pVCpu->cpum.GstCtx.cr4 & X86_CR4_PAE))
5815 {
5816 Log(("Trying to enabled long mode paging without CR4.PAE set\n"));
5817 return iemRaiseGeneralProtectionFault0(pVCpu);
5818 }
5819 if (pVCpu->cpum.GstCtx.cs.Attr.n.u1Long)
5820 {
5821 Log(("Trying to enabled long mode paging with a long CS descriptor loaded.\n"));
5822 return iemRaiseGeneralProtectionFault0(pVCpu);
5823 }
5824 }
5825
5826 /* Check for bits that must remain set or cleared in VMX operation,
5827 see Intel spec. 23.8 "Restrictions on VMX operation". */
5828 if (IEM_VMX_IS_ROOT_MODE(pVCpu))
5829 {
5830 uint64_t const uCr0Fixed0 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr0Fixed0;
5831 if ((uNewCrX & uCr0Fixed0) != uCr0Fixed0)
5832 {
5833 Log(("Trying to clear reserved CR0 bits in VMX operation: NewCr0=%#llx MB1=%#llx\n", uNewCrX, uCr0Fixed0));
5834 return iemRaiseGeneralProtectionFault0(pVCpu);
5835 }
5836
5837 uint64_t const uCr0Fixed1 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr0Fixed1;
5838 if (uNewCrX & ~uCr0Fixed1)
5839 {
5840 Log(("Trying to set reserved CR0 bits in VMX operation: NewCr0=%#llx MB0=%#llx\n", uNewCrX, uCr0Fixed1));
5841 return iemRaiseGeneralProtectionFault0(pVCpu);
5842 }
5843 }
5844
5845 /*
5846 * SVM nested-guest CR0 write intercepts.
5847 */
5848 if (IEM_SVM_IS_WRITE_CR_INTERCEPT_SET(pVCpu, iCrReg))
5849 {
5850 Log(("iemCImpl_load_Cr%#x: Guest intercept -> #VMEXIT\n", iCrReg));
5851 IEM_SVM_UPDATE_NRIP(pVCpu);
5852 IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_WRITE_CR0, enmAccessCrX, iGReg);
5853 }
5854 if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_CR0_SEL_WRITE))
5855 {
5856 /* 'lmsw' intercepts regardless of whether the TS/MP bits are actually toggled. */
5857 if ( enmAccessCrX == IEMACCESSCRX_LMSW
5858 || (uNewCrX & ~(X86_CR0_TS | X86_CR0_MP)) != (uOldCrX & ~(X86_CR0_TS | X86_CR0_MP)))
5859 {
5860 Assert(enmAccessCrX != IEMACCESSCRX_CLTS);
5861 Log(("iemCImpl_load_Cr%#x: lmsw or bits other than TS/MP changed: Guest intercept -> #VMEXIT\n", iCrReg));
5862 IEM_SVM_UPDATE_NRIP(pVCpu);
5863 IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_CR0_SEL_WRITE, enmAccessCrX, iGReg);
5864 }
5865 }
5866
5867 /*
5868 * Change EFER.LMA if entering or leaving long mode.
5869 */
5870 uint64_t NewEFER = pVCpu->cpum.GstCtx.msrEFER;
5871 if ( (uNewCrX & X86_CR0_PG) != (uOldCrX & X86_CR0_PG)
5872 && (pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_LME) )
5873 {
5874 if (uNewCrX & X86_CR0_PG)
5875 NewEFER |= MSR_K6_EFER_LMA;
5876 else
5877 NewEFER &= ~MSR_K6_EFER_LMA;
5878
5879 CPUMSetGuestEFER(pVCpu, NewEFER);
5880 Assert(pVCpu->cpum.GstCtx.msrEFER == NewEFER);
5881 }
5882
5883 /*
5884 * Inform PGM.
5885 */
5886 if ( (uNewCrX & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE | X86_CR0_CD | X86_CR0_NW))
5887 != (uOldCrX & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE | X86_CR0_CD | X86_CR0_NW)) )
5888 {
5889 if ( enmAccessCrX != IEMACCESSCRX_MOV_CRX
5890 || !CPUMIsPaePagingEnabled(uNewCrX, pVCpu->cpum.GstCtx.cr4, NewEFER)
5891 || CPUMIsGuestInSvmNestedHwVirtMode(IEM_GET_CTX(pVCpu)))
5892 { /* likely */ }
5893 else
5894 IEM_MAP_PAE_PDPES_AT_CR3_RET(pVCpu, iCrReg, pVCpu->cpum.GstCtx.cr3);
5895 rc = PGMFlushTLB(pVCpu, pVCpu->cpum.GstCtx.cr3, true /* global */);
5896 AssertRCReturn(rc, rc);
5897 /* ignore informational status codes */
5898 }
5899
5900 /*
5901 * Change CR0.
5902 */
5903 CPUMSetGuestCR0(pVCpu, uNewCrX);
5904 Assert(pVCpu->cpum.GstCtx.cr0 == uNewCrX);
5905
5906 rcStrict = PGMChangeMode(pVCpu, pVCpu->cpum.GstCtx.cr0, pVCpu->cpum.GstCtx.cr4, pVCpu->cpum.GstCtx.msrEFER,
5907 false /* fForce */);
5908 break;
5909 }
5910
5911 /*
5912 * CR2 can be changed without any restrictions.
5913 */
5914 case 2:
5915 {
5916 if (IEM_SVM_IS_WRITE_CR_INTERCEPT_SET(pVCpu, /*cr*/ 2))
5917 {
5918 Log(("iemCImpl_load_Cr%#x: Guest intercept -> #VMEXIT\n", iCrReg));
5919 IEM_SVM_UPDATE_NRIP(pVCpu);
5920 IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_WRITE_CR2, enmAccessCrX, iGReg);
5921 }
5922 pVCpu->cpum.GstCtx.cr2 = uNewCrX;
5923 pVCpu->cpum.GstCtx.fExtrn &= ~CPUMCTX_EXTRN_CR2;
5924 rcStrict = VINF_SUCCESS;
5925 break;
5926 }
5927
5928 /*
5929 * CR3 is relatively simple, although AMD and Intel have different
5930 * accounts of how setting reserved bits are handled. We take intel's
5931 * word for the lower bits and AMD's for the high bits (63:52). The
5932 * lower reserved bits are ignored and left alone; OpenBSD 5.8 relies
5933 * on this.
5934 */
5935 /** @todo Testcase: Setting reserved bits in CR3, especially before
5936 * enabling paging. */
5937 case 3:
5938 {
5939 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR3);
5940
5941 /* Bit 63 being clear in the source operand with PCIDE indicates no invalidations are required. */
5942 if ( (pVCpu->cpum.GstCtx.cr4 & X86_CR4_PCIDE)
5943 && (uNewCrX & RT_BIT_64(63)))
5944 {
5945 /** @todo r=ramshankar: avoiding a TLB flush altogether here causes Windows 10
5946 * SMP(w/o nested-paging) to hang during bootup on Skylake systems, see
5947 * Intel spec. 4.10.4.1 "Operations that Invalidate TLBs and
5948 * Paging-Structure Caches". */
5949 uNewCrX &= ~RT_BIT_64(63);
5950 }
5951
5952 /* Check / mask the value. */
5953#ifdef VBOX_WITH_NESTED_HWVIRT_VMX_EPT
5954 /* See Intel spec. 27.2.2 "EPT Translation Mechanism" footnote. */
5955 uint64_t const fInvPhysMask = !CPUMIsGuestVmxEptPagingEnabledEx(IEM_GET_CTX(pVCpu))
5956 ? (UINT64_MAX << IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cMaxPhysAddrWidth)
5957 : (~X86_CR3_EPT_PAGE_MASK & X86_PAGE_4K_BASE_MASK);
5958#else
5959 uint64_t const fInvPhysMask = UINT64_C(0xfff0000000000000);
5960#endif
5961 if (uNewCrX & fInvPhysMask)
5962 {
5963 /** @todo Should we raise this only for 64-bit mode like Intel claims? AMD is
5964 * very vague in this area. As mentioned above, need testcase on real
5965 * hardware... Sigh. */
5966 Log(("Trying to load CR3 with invalid high bits set: %#llx\n", uNewCrX));
5967 return iemRaiseGeneralProtectionFault0(pVCpu);
5968 }
5969
5970 uint64_t fValid;
5971 if ( (pVCpu->cpum.GstCtx.cr4 & X86_CR4_PAE)
5972 && (pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_LME))
5973 {
5974 /** @todo Redundant? This value has already been validated above. */
5975 fValid = UINT64_C(0x000fffffffffffff);
5976 }
5977 else
5978 fValid = UINT64_C(0xffffffff);
5979 if (uNewCrX & ~fValid)
5980 {
5981 Log(("Automatically clearing reserved MBZ bits in CR3 load: NewCR3=%#llx ClearedBits=%#llx\n",
5982 uNewCrX, uNewCrX & ~fValid));
5983 uNewCrX &= fValid;
5984 }
5985
5986 if (IEM_SVM_IS_WRITE_CR_INTERCEPT_SET(pVCpu, /*cr*/ 3))
5987 {
5988 Log(("iemCImpl_load_Cr%#x: Guest intercept -> #VMEXIT\n", iCrReg));
5989 IEM_SVM_UPDATE_NRIP(pVCpu);
5990 IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_WRITE_CR3, enmAccessCrX, iGReg);
5991 }
5992
5993 /* Inform PGM. */
5994 if (pVCpu->cpum.GstCtx.cr0 & X86_CR0_PG)
5995 {
5996 if ( !CPUMIsGuestInPAEModeEx(IEM_GET_CTX(pVCpu))
5997 || CPUMIsGuestInSvmNestedHwVirtMode(IEM_GET_CTX(pVCpu)))
5998 { /* likely */ }
5999 else
6000 {
6001 Assert(enmAccessCrX == IEMACCESSCRX_MOV_CRX);
6002 IEM_MAP_PAE_PDPES_AT_CR3_RET(pVCpu, iCrReg, uNewCrX);
6003 }
6004 rc = PGMFlushTLB(pVCpu, uNewCrX, !(pVCpu->cpum.GstCtx.cr4 & X86_CR4_PGE));
6005 AssertRCReturn(rc, rc);
6006 /* ignore informational status codes */
6007 }
6008
6009 /* Make the change. */
6010 rc = CPUMSetGuestCR3(pVCpu, uNewCrX);
6011 AssertRCSuccessReturn(rc, rc);
6012
6013 rcStrict = VINF_SUCCESS;
6014 break;
6015 }
6016
6017 /*
6018 * CR4 is a bit more tedious as there are bits which cannot be cleared
6019 * under some circumstances and such.
6020 */
6021 case 4:
6022 {
6023 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR4);
6024 uint64_t const uOldCrX = pVCpu->cpum.GstCtx.cr4;
6025
6026 /* Reserved bits. */
6027 uint32_t const fValid = CPUMGetGuestCR4ValidMask(pVCpu->CTX_SUFF(pVM));
6028 if (uNewCrX & ~(uint64_t)fValid)
6029 {
6030 Log(("Trying to set reserved CR4 bits: NewCR4=%#llx InvalidBits=%#llx\n", uNewCrX, uNewCrX & ~(uint64_t)fValid));
6031 return iemRaiseGeneralProtectionFault0(pVCpu);
6032 }
6033
6034 bool const fPcide = !(uOldCrX & X86_CR4_PCIDE) && (uNewCrX & X86_CR4_PCIDE);
6035 bool const fLongMode = CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu));
6036
6037 /* PCIDE check. */
6038 if ( fPcide
6039 && ( !fLongMode
6040 || (pVCpu->cpum.GstCtx.cr3 & UINT64_C(0xfff))))
6041 {
6042 Log(("Trying to set PCIDE with invalid PCID or outside long mode. Pcid=%#x\n", (pVCpu->cpum.GstCtx.cr3 & UINT64_C(0xfff))));
6043 return iemRaiseGeneralProtectionFault0(pVCpu);
6044 }
6045
6046 /* PAE check. */
6047 if ( fLongMode
6048 && (uOldCrX & X86_CR4_PAE)
6049 && !(uNewCrX & X86_CR4_PAE))
6050 {
6051 Log(("Trying to set clear CR4.PAE while long mode is active\n"));
6052 return iemRaiseGeneralProtectionFault0(pVCpu);
6053 }
6054
6055 if (IEM_SVM_IS_WRITE_CR_INTERCEPT_SET(pVCpu, /*cr*/ 4))
6056 {
6057 Log(("iemCImpl_load_Cr%#x: Guest intercept -> #VMEXIT\n", iCrReg));
6058 IEM_SVM_UPDATE_NRIP(pVCpu);
6059 IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_WRITE_CR4, enmAccessCrX, iGReg);
6060 }
6061
6062 /* Check for bits that must remain set or cleared in VMX operation,
6063 see Intel spec. 23.8 "Restrictions on VMX operation". */
6064 if (IEM_VMX_IS_ROOT_MODE(pVCpu))
6065 {
6066 uint64_t const uCr4Fixed0 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr4Fixed0;
6067 if ((uNewCrX & uCr4Fixed0) != uCr4Fixed0)
6068 {
6069 Log(("Trying to clear reserved CR4 bits in VMX operation: NewCr4=%#llx MB1=%#llx\n", uNewCrX, uCr4Fixed0));
6070 return iemRaiseGeneralProtectionFault0(pVCpu);
6071 }
6072
6073 uint64_t const uCr4Fixed1 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr4Fixed1;
6074 if (uNewCrX & ~uCr4Fixed1)
6075 {
6076 Log(("Trying to set reserved CR4 bits in VMX operation: NewCr4=%#llx MB0=%#llx\n", uNewCrX, uCr4Fixed1));
6077 return iemRaiseGeneralProtectionFault0(pVCpu);
6078 }
6079 }
6080
6081 /*
6082 * Notify PGM.
6083 */
6084 if ((uNewCrX ^ uOldCrX) & (X86_CR4_PSE | X86_CR4_PAE | X86_CR4_PGE | X86_CR4_PCIDE /* | X86_CR4_SMEP */))
6085 {
6086 if ( !CPUMIsPaePagingEnabled(pVCpu->cpum.GstCtx.cr0, uNewCrX, pVCpu->cpum.GstCtx.msrEFER)
6087 || CPUMIsGuestInSvmNestedHwVirtMode(IEM_GET_CTX(pVCpu)))
6088 { /* likely */ }
6089 else
6090 {
6091 Assert(enmAccessCrX == IEMACCESSCRX_MOV_CRX);
6092 IEM_MAP_PAE_PDPES_AT_CR3_RET(pVCpu, iCrReg, pVCpu->cpum.GstCtx.cr3);
6093 }
6094 rc = PGMFlushTLB(pVCpu, pVCpu->cpum.GstCtx.cr3, true /* global */);
6095 AssertRCReturn(rc, rc);
6096 /* ignore informational status codes */
6097 }
6098
6099 /*
6100 * Change it.
6101 */
6102 rc = CPUMSetGuestCR4(pVCpu, uNewCrX);
6103 AssertRCSuccessReturn(rc, rc);
6104 Assert(pVCpu->cpum.GstCtx.cr4 == uNewCrX);
6105
6106 rcStrict = PGMChangeMode(pVCpu, pVCpu->cpum.GstCtx.cr0, pVCpu->cpum.GstCtx.cr4, pVCpu->cpum.GstCtx.msrEFER,
6107 false /* fForce */);
6108 break;
6109 }
6110
6111 /*
6112 * CR8 maps to the APIC TPR.
6113 */
6114 case 8:
6115 {
6116 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_APIC_TPR);
6117 if (uNewCrX & ~(uint64_t)0xf)
6118 {
6119 Log(("Trying to set reserved CR8 bits (%#RX64)\n", uNewCrX));
6120 return iemRaiseGeneralProtectionFault0(pVCpu);
6121 }
6122
6123#ifdef VBOX_WITH_NESTED_HWVIRT_VMX
6124 if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
6125 && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_USE_TPR_SHADOW))
6126 {
6127 /*
6128 * If the Mov-to-CR8 doesn't cause a VM-exit, bits 0:3 of the source operand
6129 * is copied to bits 7:4 of the VTPR. Bits 0:3 and bits 31:8 of the VTPR are
6130 * cleared. Following this the processor performs TPR virtualization.
6131 *
6132 * However, we should not perform TPR virtualization immediately here but
6133 * after this instruction has completed.
6134 *
6135 * See Intel spec. 29.3 "Virtualizing CR8-based TPR Accesses"
6136 * See Intel spec. 27.1 "Architectural State Before A VM-exit"
6137 */
6138 uint32_t const uTpr = (uNewCrX & 0xf) << 4;
6139 Log(("iemCImpl_load_Cr%#x: Virtualizing TPR (%#x) write\n", iCrReg, uTpr));
6140 iemVmxVirtApicWriteRaw32(pVCpu, XAPIC_OFF_TPR, uTpr);
6141 iemVmxVirtApicSetPendingWrite(pVCpu, XAPIC_OFF_TPR);
6142 rcStrict = VINF_SUCCESS;
6143 break;
6144 }
6145#endif
6146
6147#ifdef VBOX_WITH_NESTED_HWVIRT_SVM
6148 if (CPUMIsGuestInSvmNestedHwVirtMode(IEM_GET_CTX(pVCpu)))
6149 {
6150 if (IEM_SVM_IS_WRITE_CR_INTERCEPT_SET(pVCpu, /*cr*/ 8))
6151 {
6152 Log(("iemCImpl_load_Cr%#x: Guest intercept -> #VMEXIT\n", iCrReg));
6153 IEM_SVM_UPDATE_NRIP(pVCpu);
6154 IEM_SVM_CRX_VMEXIT_RET(pVCpu, SVM_EXIT_WRITE_CR8, enmAccessCrX, iGReg);
6155 }
6156
6157 pVCpu->cpum.GstCtx.hwvirt.svm.Vmcb.ctrl.IntCtrl.n.u8VTPR = uNewCrX;
6158 if (CPUMIsGuestSvmVirtIntrMasking(pVCpu, IEM_GET_CTX(pVCpu)))
6159 {
6160 rcStrict = VINF_SUCCESS;
6161 break;
6162 }
6163 }
6164#endif
6165 uint8_t const u8Tpr = (uint8_t)uNewCrX << 4;
6166 APICSetTpr(pVCpu, u8Tpr);
6167 rcStrict = VINF_SUCCESS;
6168 break;
6169 }
6170
6171 IEM_NOT_REACHED_DEFAULT_CASE_RET(); /* call checks */
6172 }
6173
6174 /*
6175 * Advance the RIP on success.
6176 */
6177 if (RT_SUCCESS(rcStrict))
6178 {
6179 if (rcStrict != VINF_SUCCESS)
6180 rcStrict = iemSetPassUpStatus(pVCpu, rcStrict);
6181 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
6182 }
6183
6184 return rcStrict;
6185}
6186
6187
6188/**
6189 * Implements mov CRx,GReg.
6190 *
6191 * @param iCrReg The CRx register to write (valid).
6192 * @param iGReg The general register to load the CRx value from.
6193 */
6194IEM_CIMPL_DEF_2(iemCImpl_mov_Cd_Rd, uint8_t, iCrReg, uint8_t, iGReg)
6195{
6196 if (pVCpu->iem.s.uCpl != 0)
6197 return iemRaiseGeneralProtectionFault0(pVCpu);
6198 Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
6199
6200 /*
6201 * Read the new value from the source register and call common worker.
6202 */
6203 uint64_t uNewCrX;
6204 if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
6205 uNewCrX = iemGRegFetchU64(pVCpu, iGReg);
6206 else
6207 uNewCrX = iemGRegFetchU32(pVCpu, iGReg);
6208
6209#ifdef VBOX_WITH_NESTED_HWVIRT_VMX
6210 if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
6211 {
6212 VBOXSTRICTRC rcStrict = VINF_VMX_INTERCEPT_NOT_ACTIVE;
6213 switch (iCrReg)
6214 {
6215 case 0:
6216 case 4: rcStrict = iemVmxVmexitInstrMovToCr0Cr4(pVCpu, iCrReg, &uNewCrX, iGReg, cbInstr); break;
6217 case 3: rcStrict = iemVmxVmexitInstrMovToCr3(pVCpu, uNewCrX, iGReg, cbInstr); break;
6218 case 8: rcStrict = iemVmxVmexitInstrMovToCr8(pVCpu, iGReg, cbInstr); break;
6219 }
6220 if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
6221 return rcStrict;
6222 }
6223#endif
6224
6225 return IEM_CIMPL_CALL_4(iemCImpl_load_CrX, iCrReg, uNewCrX, IEMACCESSCRX_MOV_CRX, iGReg);
6226}
6227
6228
6229/**
6230 * Implements 'LMSW r/m16'
6231 *
6232 * @param u16NewMsw The new value.
6233 * @param GCPtrEffDst The guest-linear address of the source operand in case
6234 * of a memory operand. For register operand, pass
6235 * NIL_RTGCPTR.
6236 */
6237IEM_CIMPL_DEF_2(iemCImpl_lmsw, uint16_t, u16NewMsw, RTGCPTR, GCPtrEffDst)
6238{
6239 if (pVCpu->iem.s.uCpl != 0)
6240 return iemRaiseGeneralProtectionFault0(pVCpu);
6241 Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
6242 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0);
6243
6244#ifdef VBOX_WITH_NESTED_HWVIRT_VMX
6245 /* Check nested-guest VMX intercept and get updated MSW if there's no VM-exit. */
6246 if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
6247 {
6248 VBOXSTRICTRC rcStrict = iemVmxVmexitInstrLmsw(pVCpu, pVCpu->cpum.GstCtx.cr0, &u16NewMsw, GCPtrEffDst, cbInstr);
6249 if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
6250 return rcStrict;
6251 }
6252#else
6253 RT_NOREF_PV(GCPtrEffDst);
6254#endif
6255
6256 /*
6257 * Compose the new CR0 value and call common worker.
6258 */
6259 uint64_t uNewCr0 = pVCpu->cpum.GstCtx.cr0 & ~(X86_CR0_MP | X86_CR0_EM | X86_CR0_TS);
6260 uNewCr0 |= u16NewMsw & (X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS);
6261 return IEM_CIMPL_CALL_4(iemCImpl_load_CrX, /*cr*/ 0, uNewCr0, IEMACCESSCRX_LMSW, UINT8_MAX /* iGReg */);
6262}
6263
6264
6265/**
6266 * Implements 'CLTS'.
6267 */
6268IEM_CIMPL_DEF_0(iemCImpl_clts)
6269{
6270 if (pVCpu->iem.s.uCpl != 0)
6271 return iemRaiseGeneralProtectionFault0(pVCpu);
6272
6273 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0);
6274 uint64_t uNewCr0 = pVCpu->cpum.GstCtx.cr0;
6275 uNewCr0 &= ~X86_CR0_TS;
6276
6277#ifdef VBOX_WITH_NESTED_HWVIRT_VMX
6278 if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
6279 {
6280 VBOXSTRICTRC rcStrict = iemVmxVmexitInstrClts(pVCpu, cbInstr);
6281 if (rcStrict == VINF_VMX_MODIFIES_BEHAVIOR)
6282 uNewCr0 |= (pVCpu->cpum.GstCtx.cr0 & X86_CR0_TS);
6283 else if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
6284 return rcStrict;
6285 }
6286#endif
6287
6288 return IEM_CIMPL_CALL_4(iemCImpl_load_CrX, /*cr*/ 0, uNewCr0, IEMACCESSCRX_CLTS, UINT8_MAX /* iGReg */);
6289}
6290
6291
6292/**
6293 * Implements mov GReg,DRx.
6294 *
6295 * @param iGReg The general register to store the DRx value in.
6296 * @param iDrReg The DRx register to read (0-7).
6297 */
6298IEM_CIMPL_DEF_2(iemCImpl_mov_Rd_Dd, uint8_t, iGReg, uint8_t, iDrReg)
6299{
6300#ifdef VBOX_WITH_NESTED_HWVIRT_VMX
6301 /*
6302 * Check nested-guest VMX intercept.
6303 * Unlike most other intercepts, the Mov DRx intercept takes preceedence
6304 * over CPL and CR4.DE and even DR4/DR5 checks.
6305 *
6306 * See Intel spec. 25.1.3 "Instructions That Cause VM Exits Conditionally".
6307 */
6308 if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
6309 {
6310 VBOXSTRICTRC rcStrict = iemVmxVmexitInstrMovDrX(pVCpu, VMXINSTRID_MOV_FROM_DRX, iDrReg, iGReg, cbInstr);
6311 if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
6312 return rcStrict;
6313 }
6314#endif
6315
6316 /*
6317 * Check preconditions.
6318 */
6319 /* Raise GPs. */
6320 if (pVCpu->iem.s.uCpl != 0)
6321 return iemRaiseGeneralProtectionFault0(pVCpu);
6322 Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
6323 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_DR7 | CPUMCTX_EXTRN_CR0);
6324
6325 if ( (iDrReg == 4 || iDrReg == 5)
6326 && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_DE) )
6327 {
6328 Log(("mov r%u,dr%u: CR4.DE=1 -> #GP(0)\n", iGReg, iDrReg));
6329 return iemRaiseGeneralProtectionFault0(pVCpu);
6330 }
6331
6332 /* Raise #DB if general access detect is enabled. */
6333 if (pVCpu->cpum.GstCtx.dr[7] & X86_DR7_GD)
6334 {
6335 Log(("mov r%u,dr%u: DR7.GD=1 -> #DB\n", iGReg, iDrReg));
6336 return iemRaiseDebugException(pVCpu);
6337 }
6338
6339 /*
6340 * Read the debug register and store it in the specified general register.
6341 */
6342 uint64_t drX;
6343 switch (iDrReg)
6344 {
6345 case 0:
6346 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3);
6347 drX = pVCpu->cpum.GstCtx.dr[0];
6348 break;
6349 case 1:
6350 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3);
6351 drX = pVCpu->cpum.GstCtx.dr[1];
6352 break;
6353 case 2:
6354 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3);
6355 drX = pVCpu->cpum.GstCtx.dr[2];
6356 break;
6357 case 3:
6358 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3);
6359 drX = pVCpu->cpum.GstCtx.dr[3];
6360 break;
6361 case 6:
6362 case 4:
6363 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR6);
6364 drX = pVCpu->cpum.GstCtx.dr[6];
6365 drX |= X86_DR6_RA1_MASK;
6366 drX &= ~X86_DR6_RAZ_MASK;
6367 break;
6368 case 7:
6369 case 5:
6370 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_DR7);
6371 drX = pVCpu->cpum.GstCtx.dr[7];
6372 drX |=X86_DR7_RA1_MASK;
6373 drX &= ~X86_DR7_RAZ_MASK;
6374 break;
6375 IEM_NOT_REACHED_DEFAULT_CASE_RET(); /* call checks */
6376 }
6377
6378 /** @todo SVM nested-guest intercept for DR8-DR15? */
6379 /*
6380 * Check for any SVM nested-guest intercepts for the DRx read.
6381 */
6382 if (IEM_SVM_IS_READ_DR_INTERCEPT_SET(pVCpu, iDrReg))
6383 {
6384 Log(("mov r%u,dr%u: Guest intercept -> #VMEXIT\n", iGReg, iDrReg));
6385 IEM_SVM_UPDATE_NRIP(pVCpu);
6386 IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_READ_DR0 + (iDrReg & 0xf),
6387 IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fSvmDecodeAssists ? (iGReg & 7) : 0, 0 /* uExitInfo2 */);
6388 }
6389
6390 if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
6391 *(uint64_t *)iemGRegRef(pVCpu, iGReg) = drX;
6392 else
6393 *(uint64_t *)iemGRegRef(pVCpu, iGReg) = (uint32_t)drX;
6394
6395 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
6396 return VINF_SUCCESS;
6397}
6398
6399
6400/**
6401 * Implements mov DRx,GReg.
6402 *
6403 * @param iDrReg The DRx register to write (valid).
6404 * @param iGReg The general register to load the DRx value from.
6405 */
6406IEM_CIMPL_DEF_2(iemCImpl_mov_Dd_Rd, uint8_t, iDrReg, uint8_t, iGReg)
6407{
6408#ifdef VBOX_WITH_NESTED_HWVIRT_VMX
6409 /*
6410 * Check nested-guest VMX intercept.
6411 * Unlike most other intercepts, the Mov DRx intercept takes preceedence
6412 * over CPL and CR4.DE and even DR4/DR5 checks.
6413 *
6414 * See Intel spec. 25.1.3 "Instructions That Cause VM Exits Conditionally".
6415 */
6416 if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
6417 {
6418 VBOXSTRICTRC rcStrict = iemVmxVmexitInstrMovDrX(pVCpu, VMXINSTRID_MOV_TO_DRX, iDrReg, iGReg, cbInstr);
6419 if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
6420 return rcStrict;
6421 }
6422#endif
6423
6424 /*
6425 * Check preconditions.
6426 */
6427 if (pVCpu->iem.s.uCpl != 0)
6428 return iemRaiseGeneralProtectionFault0(pVCpu);
6429 Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
6430 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_DR7 | CPUMCTX_EXTRN_CR4);
6431
6432 if (iDrReg == 4 || iDrReg == 5)
6433 {
6434 if (pVCpu->cpum.GstCtx.cr4 & X86_CR4_DE)
6435 {
6436 Log(("mov dr%u,r%u: CR4.DE=1 -> #GP(0)\n", iDrReg, iGReg));
6437 return iemRaiseGeneralProtectionFault0(pVCpu);
6438 }
6439 iDrReg += 2;
6440 }
6441
6442 /* Raise #DB if general access detect is enabled. */
6443 /** @todo is \#DB/DR7.GD raised before any reserved high bits in DR7/DR6
6444 * \#GP? */
6445 if (pVCpu->cpum.GstCtx.dr[7] & X86_DR7_GD)
6446 {
6447 Log(("mov dr%u,r%u: DR7.GD=1 -> #DB\n", iDrReg, iGReg));
6448 return iemRaiseDebugException(pVCpu);
6449 }
6450
6451 /*
6452 * Read the new value from the source register.
6453 */
6454 uint64_t uNewDrX;
6455 if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
6456 uNewDrX = iemGRegFetchU64(pVCpu, iGReg);
6457 else
6458 uNewDrX = iemGRegFetchU32(pVCpu, iGReg);
6459
6460 /*
6461 * Adjust it.
6462 */
6463 switch (iDrReg)
6464 {
6465 case 0:
6466 case 1:
6467 case 2:
6468 case 3:
6469 /* nothing to adjust */
6470 break;
6471
6472 case 6:
6473 if (uNewDrX & X86_DR6_MBZ_MASK)
6474 {
6475 Log(("mov dr%u,%#llx: DR6 high bits are not zero -> #GP(0)\n", iDrReg, uNewDrX));
6476 return iemRaiseGeneralProtectionFault0(pVCpu);
6477 }
6478 uNewDrX |= X86_DR6_RA1_MASK;
6479 uNewDrX &= ~X86_DR6_RAZ_MASK;
6480 break;
6481
6482 case 7:
6483 if (uNewDrX & X86_DR7_MBZ_MASK)
6484 {
6485 Log(("mov dr%u,%#llx: DR7 high bits are not zero -> #GP(0)\n", iDrReg, uNewDrX));
6486 return iemRaiseGeneralProtectionFault0(pVCpu);
6487 }
6488 uNewDrX |= X86_DR7_RA1_MASK;
6489 uNewDrX &= ~X86_DR7_RAZ_MASK;
6490 break;
6491
6492 IEM_NOT_REACHED_DEFAULT_CASE_RET();
6493 }
6494
6495 /** @todo SVM nested-guest intercept for DR8-DR15? */
6496 /*
6497 * Check for any SVM nested-guest intercepts for the DRx write.
6498 */
6499 if (IEM_SVM_IS_WRITE_DR_INTERCEPT_SET(pVCpu, iDrReg))
6500 {
6501 Log2(("mov dr%u,r%u: Guest intercept -> #VMEXIT\n", iDrReg, iGReg));
6502 IEM_SVM_UPDATE_NRIP(pVCpu);
6503 IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_WRITE_DR0 + (iDrReg & 0xf),
6504 IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fSvmDecodeAssists ? (iGReg & 7) : 0, 0 /* uExitInfo2 */);
6505 }
6506
6507 /*
6508 * Do the actual setting.
6509 */
6510 if (iDrReg < 4)
6511 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3);
6512 else if (iDrReg == 6)
6513 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR6);
6514
6515 int rc = CPUMSetGuestDRx(pVCpu, iDrReg, uNewDrX);
6516 AssertRCSuccessReturn(rc, RT_SUCCESS_NP(rc) ? VERR_IEM_IPE_1 : rc);
6517
6518 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
6519 return VINF_SUCCESS;
6520}
6521
6522
6523/**
6524 * Implements mov GReg,TRx.
6525 *
6526 * @param iGReg The general register to store the
6527 * TRx value in.
6528 * @param iTrReg The TRx register to read (6/7).
6529 */
6530IEM_CIMPL_DEF_2(iemCImpl_mov_Rd_Td, uint8_t, iGReg, uint8_t, iTrReg)
6531{
6532 /*
6533 * Check preconditions. NB: This instruction is 386/486 only.
6534 */
6535
6536 /* Raise GPs. */
6537 if (pVCpu->iem.s.uCpl != 0)
6538 return iemRaiseGeneralProtectionFault0(pVCpu);
6539 Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
6540
6541 if (iTrReg < 6 || iTrReg > 7)
6542 {
6543 /** @todo Do Intel CPUs reject this or are the TRs aliased? */
6544 Log(("mov r%u,tr%u: invalid register -> #GP(0)\n", iGReg, iTrReg));
6545 return iemRaiseGeneralProtectionFault0(pVCpu);
6546 }
6547
6548 /*
6549 * Read the test register and store it in the specified general register.
6550 * This is currently a dummy implementation that only exists to satisfy
6551 * old debuggers like WDEB386 or OS/2 KDB which unconditionally read the
6552 * TR6/TR7 registers. Software which actually depends on the TR values
6553 * (different on 386/486) is exceedingly rare.
6554 */
6555 uint64_t trX;
6556 switch (iTrReg)
6557 {
6558 case 6:
6559 trX = 0; /* Currently a dummy. */
6560 break;
6561 case 7:
6562 trX = 0; /* Currently a dummy. */
6563 break;
6564 IEM_NOT_REACHED_DEFAULT_CASE_RET(); /* call checks */
6565 }
6566
6567 *(uint64_t *)iemGRegRef(pVCpu, iGReg) = (uint32_t)trX;
6568
6569 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
6570 return VINF_SUCCESS;
6571}
6572
6573
6574/**
6575 * Implements mov TRx,GReg.
6576 *
6577 * @param iTrReg The TRx register to write (valid).
6578 * @param iGReg The general register to load the TRx
6579 * value from.
6580 */
6581IEM_CIMPL_DEF_2(iemCImpl_mov_Td_Rd, uint8_t, iTrReg, uint8_t, iGReg)
6582{
6583 /*
6584 * Check preconditions. NB: This instruction is 386/486 only.
6585 */
6586
6587 /* Raise GPs. */
6588 if (pVCpu->iem.s.uCpl != 0)
6589 return iemRaiseGeneralProtectionFault0(pVCpu);
6590 Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
6591
6592 if (iTrReg < 6 || iTrReg > 7)
6593 {
6594 /** @todo Do Intel CPUs reject this or are the TRs aliased? */
6595 Log(("mov r%u,tr%u: invalid register -> #GP(0)\n", iGReg, iTrReg));
6596 return iemRaiseGeneralProtectionFault0(pVCpu);
6597 }
6598
6599 /*
6600 * Read the new value from the source register.
6601 */
6602 uint64_t uNewTrX;
6603 if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
6604 uNewTrX = iemGRegFetchU64(pVCpu, iGReg);
6605 else
6606 uNewTrX = iemGRegFetchU32(pVCpu, iGReg);
6607
6608 /*
6609 * Here we would do the actual setting if this weren't a dummy implementation.
6610 * This is currently a dummy implementation that only exists to prevent
6611 * old debuggers like WDEB386 or OS/2 KDB from crashing.
6612 */
6613 RT_NOREF(uNewTrX);
6614
6615 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
6616 return VINF_SUCCESS;
6617}
6618
6619
6620/**
6621 * Implements 'INVLPG m'.
6622 *
6623 * @param GCPtrPage The effective address of the page to invalidate.
6624 * @remarks Updates the RIP.
6625 */
6626IEM_CIMPL_DEF_1(iemCImpl_invlpg, RTGCPTR, GCPtrPage)
6627{
6628 /* ring-0 only. */
6629 if (pVCpu->iem.s.uCpl != 0)
6630 return iemRaiseGeneralProtectionFault0(pVCpu);
6631 Assert(!pVCpu->cpum.GstCtx.eflags.Bits.u1VM);
6632 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_CR3 | CPUMCTX_EXTRN_CR4 | CPUMCTX_EXTRN_EFER);
6633
6634#ifdef VBOX_WITH_NESTED_HWVIRT_VMX
6635 if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
6636 && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_INVLPG_EXIT))
6637 {
6638 Log(("invlpg: Guest intercept (%RGp) -> VM-exit\n", GCPtrPage));
6639 return iemVmxVmexitInstrInvlpg(pVCpu, GCPtrPage, cbInstr);
6640 }
6641#endif
6642
6643 if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_INVLPG))
6644 {
6645 Log(("invlpg: Guest intercept (%RGp) -> #VMEXIT\n", GCPtrPage));
6646 IEM_SVM_UPDATE_NRIP(pVCpu);
6647 IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_INVLPG,
6648 IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fSvmDecodeAssists ? GCPtrPage : 0, 0 /* uExitInfo2 */);
6649 }
6650
6651 int rc = PGMInvalidatePage(pVCpu, GCPtrPage);
6652 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
6653
6654 if (rc == VINF_SUCCESS)
6655 return VINF_SUCCESS;
6656 if (rc == VINF_PGM_SYNC_CR3)
6657 return iemSetPassUpStatus(pVCpu, rc);
6658
6659 AssertMsg(rc == VINF_EM_RAW_EMULATE_INSTR || RT_FAILURE_NP(rc), ("%Rrc\n", rc));
6660 Log(("PGMInvalidatePage(%RGv) -> %Rrc\n", GCPtrPage, rc));
6661 return rc;
6662}
6663
6664
6665/**
6666 * Implements INVPCID.
6667 *
6668 * @param iEffSeg The segment of the invpcid descriptor.
6669 * @param GCPtrInvpcidDesc The address of invpcid descriptor.
6670 * @param uInvpcidType The invalidation type.
6671 * @remarks Updates the RIP.
6672 */
6673IEM_CIMPL_DEF_3(iemCImpl_invpcid, uint8_t, iEffSeg, RTGCPTR, GCPtrInvpcidDesc, uint64_t, uInvpcidType)
6674{
6675 /*
6676 * Check preconditions.
6677 */
6678 if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fInvpcid)
6679 return iemRaiseUndefinedOpcode(pVCpu);
6680
6681 /* When in VMX non-root mode and INVPCID is not enabled, it results in #UD. */
6682 if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
6683 && !IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_INVPCID))
6684 {
6685 Log(("invpcid: Not enabled for nested-guest execution -> #UD\n"));
6686 return iemRaiseUndefinedOpcode(pVCpu);
6687 }
6688
6689 if (pVCpu->iem.s.uCpl != 0)
6690 {
6691 Log(("invpcid: CPL != 0 -> #GP(0)\n"));
6692 return iemRaiseGeneralProtectionFault0(pVCpu);
6693 }
6694
6695 if (IEM_IS_V86_MODE(pVCpu))
6696 {
6697 Log(("invpcid: v8086 mode -> #GP(0)\n"));
6698 return iemRaiseGeneralProtectionFault0(pVCpu);
6699 }
6700
6701 /*
6702 * Check nested-guest intercept.
6703 *
6704 * INVPCID causes a VM-exit if "enable INVPCID" and "INVLPG exiting" are
6705 * both set. We have already checked the former earlier in this function.
6706 *
6707 * CPL and virtual-8086 mode checks take priority over this VM-exit.
6708 * See Intel spec. "25.1.1 Relative Priority of Faults and VM Exits".
6709 */
6710 if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
6711 && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_INVLPG_EXIT))
6712 {
6713 Log(("invpcid: Guest intercept -> #VM-exit\n"));
6714 IEM_VMX_VMEXIT_INSTR_NEEDS_INFO_RET(pVCpu, VMX_EXIT_INVPCID, VMXINSTRID_NONE, cbInstr);
6715 }
6716
6717 if (uInvpcidType > X86_INVPCID_TYPE_MAX_VALID)
6718 {
6719 Log(("invpcid: invalid/unrecognized invpcid type %#RX64 -> #GP(0)\n", uInvpcidType));
6720 return iemRaiseGeneralProtectionFault0(pVCpu);
6721 }
6722 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_CR3 | CPUMCTX_EXTRN_CR4 | CPUMCTX_EXTRN_EFER);
6723
6724 /*
6725 * Fetch the invpcid descriptor from guest memory.
6726 */
6727 RTUINT128U uDesc;
6728 VBOXSTRICTRC rcStrict = iemMemFetchDataU128(pVCpu, &uDesc, iEffSeg, GCPtrInvpcidDesc);
6729 if (rcStrict == VINF_SUCCESS)
6730 {
6731 /*
6732 * Validate the descriptor.
6733 */
6734 if (uDesc.s.Lo > 0xfff)
6735 {
6736 Log(("invpcid: reserved bits set in invpcid descriptor %#RX64 -> #GP(0)\n", uDesc.s.Lo));
6737 return iemRaiseGeneralProtectionFault0(pVCpu);
6738 }
6739
6740 RTGCUINTPTR64 const GCPtrInvAddr = uDesc.s.Hi;
6741 uint8_t const uPcid = uDesc.s.Lo & UINT64_C(0xfff);
6742 uint32_t const uCr4 = pVCpu->cpum.GstCtx.cr4;
6743 uint64_t const uCr3 = pVCpu->cpum.GstCtx.cr3;
6744 switch (uInvpcidType)
6745 {
6746 case X86_INVPCID_TYPE_INDV_ADDR:
6747 {
6748 if (!IEM_IS_CANONICAL(GCPtrInvAddr))
6749 {
6750 Log(("invpcid: invalidation address %#RGP is not canonical -> #GP(0)\n", GCPtrInvAddr));
6751 return iemRaiseGeneralProtectionFault0(pVCpu);
6752 }
6753 if ( !(uCr4 & X86_CR4_PCIDE)
6754 && uPcid != 0)
6755 {
6756 Log(("invpcid: invalid pcid %#x\n", uPcid));
6757 return iemRaiseGeneralProtectionFault0(pVCpu);
6758 }
6759
6760 /* Invalidate mappings for the linear address tagged with PCID except global translations. */
6761 PGMFlushTLB(pVCpu, uCr3, false /* fGlobal */);
6762 break;
6763 }
6764
6765 case X86_INVPCID_TYPE_SINGLE_CONTEXT:
6766 {
6767 if ( !(uCr4 & X86_CR4_PCIDE)
6768 && uPcid != 0)
6769 {
6770 Log(("invpcid: invalid pcid %#x\n", uPcid));
6771 return iemRaiseGeneralProtectionFault0(pVCpu);
6772 }
6773 /* Invalidate all mappings associated with PCID except global translations. */
6774 PGMFlushTLB(pVCpu, uCr3, false /* fGlobal */);
6775 break;
6776 }
6777
6778 case X86_INVPCID_TYPE_ALL_CONTEXT_INCL_GLOBAL:
6779 {
6780 PGMFlushTLB(pVCpu, uCr3, true /* fGlobal */);
6781 break;
6782 }
6783
6784 case X86_INVPCID_TYPE_ALL_CONTEXT_EXCL_GLOBAL:
6785 {
6786 PGMFlushTLB(pVCpu, uCr3, false /* fGlobal */);
6787 break;
6788 }
6789 IEM_NOT_REACHED_DEFAULT_CASE_RET();
6790 }
6791 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
6792 }
6793 return rcStrict;
6794}
6795
6796
6797/**
6798 * Implements INVD.
6799 */
6800IEM_CIMPL_DEF_0(iemCImpl_invd)
6801{
6802 if (pVCpu->iem.s.uCpl != 0)
6803 {
6804 Log(("invd: CPL != 0 -> #GP(0)\n"));
6805 return iemRaiseGeneralProtectionFault0(pVCpu);
6806 }
6807
6808 if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
6809 IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_INVD, cbInstr);
6810
6811 IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_INVD, SVM_EXIT_INVD, 0, 0);
6812
6813 /* We currently take no action here. */
6814 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
6815 return VINF_SUCCESS;
6816}
6817
6818
6819/**
6820 * Implements WBINVD.
6821 */
6822IEM_CIMPL_DEF_0(iemCImpl_wbinvd)
6823{
6824 if (pVCpu->iem.s.uCpl != 0)
6825 {
6826 Log(("wbinvd: CPL != 0 -> #GP(0)\n"));
6827 return iemRaiseGeneralProtectionFault0(pVCpu);
6828 }
6829
6830 if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
6831 IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_WBINVD, cbInstr);
6832
6833 IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_WBINVD, SVM_EXIT_WBINVD, 0, 0);
6834
6835 /* We currently take no action here. */
6836 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
6837 return VINF_SUCCESS;
6838}
6839
6840
6841/** Opcode 0x0f 0xaa. */
6842IEM_CIMPL_DEF_0(iemCImpl_rsm)
6843{
6844 IEM_SVM_CHECK_INSTR_INTERCEPT(pVCpu, SVM_CTRL_INTERCEPT_RSM, SVM_EXIT_RSM, 0, 0);
6845 NOREF(cbInstr);
6846 return iemRaiseUndefinedOpcode(pVCpu);
6847}
6848
6849
6850/**
6851 * Implements RDTSC.
6852 */
6853IEM_CIMPL_DEF_0(iemCImpl_rdtsc)
6854{
6855 /*
6856 * Check preconditions.
6857 */
6858 if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fTsc)
6859 return iemRaiseUndefinedOpcode(pVCpu);
6860
6861 if (pVCpu->iem.s.uCpl != 0)
6862 {
6863 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR4);
6864 if (pVCpu->cpum.GstCtx.cr4 & X86_CR4_TSD)
6865 {
6866 Log(("rdtsc: CR4.TSD and CPL=%u -> #GP(0)\n", pVCpu->iem.s.uCpl));
6867 return iemRaiseGeneralProtectionFault0(pVCpu);
6868 }
6869 }
6870
6871 if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
6872 && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_RDTSC_EXIT))
6873 {
6874 Log(("rdtsc: Guest intercept -> VM-exit\n"));
6875 IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_RDTSC, cbInstr);
6876 }
6877
6878 if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_RDTSC))
6879 {
6880 Log(("rdtsc: Guest intercept -> #VMEXIT\n"));
6881 IEM_SVM_UPDATE_NRIP(pVCpu);
6882 IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_RDTSC, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
6883 }
6884
6885 /*
6886 * Do the job.
6887 */
6888 uint64_t uTicks = TMCpuTickGet(pVCpu);
6889#if defined(VBOX_WITH_NESTED_HWVIRT_SVM) || defined(VBOX_WITH_NESTED_HWVIRT_VMX)
6890 uTicks = CPUMApplyNestedGuestTscOffset(pVCpu, uTicks);
6891#endif
6892 pVCpu->cpum.GstCtx.rax = RT_LO_U32(uTicks);
6893 pVCpu->cpum.GstCtx.rdx = RT_HI_U32(uTicks);
6894 pVCpu->cpum.GstCtx.fExtrn &= ~(CPUMCTX_EXTRN_RAX | CPUMCTX_EXTRN_RDX); /* For IEMExecDecodedRdtsc. */
6895 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
6896 return VINF_SUCCESS;
6897}
6898
6899
6900/**
6901 * Implements RDTSC.
6902 */
6903IEM_CIMPL_DEF_0(iemCImpl_rdtscp)
6904{
6905 /*
6906 * Check preconditions.
6907 */
6908 if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fRdTscP)
6909 return iemRaiseUndefinedOpcode(pVCpu);
6910
6911 if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
6912 && !IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_RDTSCP))
6913 {
6914 Log(("rdtscp: Not enabled for VMX non-root mode -> #UD\n"));
6915 return iemRaiseUndefinedOpcode(pVCpu);
6916 }
6917
6918 if (pVCpu->iem.s.uCpl != 0)
6919 {
6920 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR4);
6921 if (pVCpu->cpum.GstCtx.cr4 & X86_CR4_TSD)
6922 {
6923 Log(("rdtscp: CR4.TSD and CPL=%u -> #GP(0)\n", pVCpu->iem.s.uCpl));
6924 return iemRaiseGeneralProtectionFault0(pVCpu);
6925 }
6926 }
6927
6928 if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
6929 && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_RDTSC_EXIT))
6930 {
6931 Log(("rdtscp: Guest intercept -> VM-exit\n"));
6932 IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_RDTSCP, cbInstr);
6933 }
6934 else if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_RDTSCP))
6935 {
6936 Log(("rdtscp: Guest intercept -> #VMEXIT\n"));
6937 IEM_SVM_UPDATE_NRIP(pVCpu);
6938 IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_RDTSCP, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
6939 }
6940
6941 /*
6942 * Do the job.
6943 * Query the MSR first in case of trips to ring-3.
6944 */
6945 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_TSC_AUX);
6946 VBOXSTRICTRC rcStrict = CPUMQueryGuestMsr(pVCpu, MSR_K8_TSC_AUX, &pVCpu->cpum.GstCtx.rcx);
6947 if (rcStrict == VINF_SUCCESS)
6948 {
6949 /* Low dword of the TSC_AUX msr only. */
6950 pVCpu->cpum.GstCtx.rcx &= UINT32_C(0xffffffff);
6951
6952 uint64_t uTicks = TMCpuTickGet(pVCpu);
6953#if defined(VBOX_WITH_NESTED_HWVIRT_SVM) || defined(VBOX_WITH_NESTED_HWVIRT_VMX)
6954 uTicks = CPUMApplyNestedGuestTscOffset(pVCpu, uTicks);
6955#endif
6956 pVCpu->cpum.GstCtx.rax = RT_LO_U32(uTicks);
6957 pVCpu->cpum.GstCtx.rdx = RT_HI_U32(uTicks);
6958 pVCpu->cpum.GstCtx.fExtrn &= ~(CPUMCTX_EXTRN_RAX | CPUMCTX_EXTRN_RDX | CPUMCTX_EXTRN_RCX); /* For IEMExecDecodedRdtscp. */
6959 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
6960 }
6961 return rcStrict;
6962}
6963
6964
6965/**
6966 * Implements RDPMC.
6967 */
6968IEM_CIMPL_DEF_0(iemCImpl_rdpmc)
6969{
6970 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR4);
6971
6972 if ( pVCpu->iem.s.uCpl != 0
6973 && !(pVCpu->cpum.GstCtx.cr4 & X86_CR4_PCE))
6974 return iemRaiseGeneralProtectionFault0(pVCpu);
6975
6976 if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
6977 && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_RDPMC_EXIT))
6978 {
6979 Log(("rdpmc: Guest intercept -> VM-exit\n"));
6980 IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_RDPMC, cbInstr);
6981 }
6982
6983 if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_RDPMC))
6984 {
6985 Log(("rdpmc: Guest intercept -> #VMEXIT\n"));
6986 IEM_SVM_UPDATE_NRIP(pVCpu);
6987 IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_RDPMC, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
6988 }
6989
6990 /** @todo Emulate performance counters, for now just return 0. */
6991 pVCpu->cpum.GstCtx.rax = 0;
6992 pVCpu->cpum.GstCtx.rdx = 0;
6993 pVCpu->cpum.GstCtx.fExtrn &= ~(CPUMCTX_EXTRN_RAX | CPUMCTX_EXTRN_RDX);
6994 /** @todo We should trigger a \#GP here if the CPU doesn't support the index in
6995 * ecx but see @bugref{3472}! */
6996
6997 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
6998 return VINF_SUCCESS;
6999}
7000
7001
7002/**
7003 * Implements RDMSR.
7004 */
7005IEM_CIMPL_DEF_0(iemCImpl_rdmsr)
7006{
7007 /*
7008 * Check preconditions.
7009 */
7010 if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fMsr)
7011 return iemRaiseUndefinedOpcode(pVCpu);
7012 if (pVCpu->iem.s.uCpl != 0)
7013 return iemRaiseGeneralProtectionFault0(pVCpu);
7014
7015 /*
7016 * Check nested-guest intercepts.
7017 */
7018#ifdef VBOX_WITH_NESTED_HWVIRT_VMX
7019 if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
7020 {
7021 if (iemVmxIsRdmsrWrmsrInterceptSet(pVCpu, VMX_EXIT_RDMSR, pVCpu->cpum.GstCtx.ecx))
7022 IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_RDMSR, cbInstr);
7023 }
7024#endif
7025
7026#ifdef VBOX_WITH_NESTED_HWVIRT_SVM
7027 if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_MSR_PROT))
7028 {
7029 VBOXSTRICTRC rcStrict = iemSvmHandleMsrIntercept(pVCpu, pVCpu->cpum.GstCtx.ecx, false /* fWrite */);
7030 if (rcStrict == VINF_SVM_VMEXIT)
7031 return VINF_SUCCESS;
7032 if (rcStrict != VINF_SVM_INTERCEPT_NOT_ACTIVE)
7033 {
7034 Log(("IEM: SVM intercepted rdmsr(%#x) failed. rc=%Rrc\n", pVCpu->cpum.GstCtx.ecx, VBOXSTRICTRC_VAL(rcStrict)));
7035 return rcStrict;
7036 }
7037 }
7038#endif
7039
7040 /*
7041 * Do the job.
7042 */
7043 RTUINT64U uValue;
7044 /** @todo make CPUMAllMsrs.cpp import the necessary MSR state. */
7045 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_ALL_MSRS);
7046
7047 VBOXSTRICTRC rcStrict = CPUMQueryGuestMsr(pVCpu, pVCpu->cpum.GstCtx.ecx, &uValue.u);
7048 if (rcStrict == VINF_SUCCESS)
7049 {
7050 pVCpu->cpum.GstCtx.rax = uValue.s.Lo;
7051 pVCpu->cpum.GstCtx.rdx = uValue.s.Hi;
7052 pVCpu->cpum.GstCtx.fExtrn &= ~(CPUMCTX_EXTRN_RAX | CPUMCTX_EXTRN_RDX);
7053
7054 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7055 return VINF_SUCCESS;
7056 }
7057
7058#ifndef IN_RING3
7059 /* Deferred to ring-3. */
7060 if (rcStrict == VINF_CPUM_R3_MSR_READ)
7061 {
7062 Log(("IEM: rdmsr(%#x) -> ring-3\n", pVCpu->cpum.GstCtx.ecx));
7063 return rcStrict;
7064 }
7065#endif
7066
7067 /* Often a unimplemented MSR or MSR bit, so worth logging. */
7068 if (pVCpu->iem.s.cLogRelRdMsr < 32)
7069 {
7070 pVCpu->iem.s.cLogRelRdMsr++;
7071 LogRel(("IEM: rdmsr(%#x) -> #GP(0)\n", pVCpu->cpum.GstCtx.ecx));
7072 }
7073 else
7074 Log(( "IEM: rdmsr(%#x) -> #GP(0)\n", pVCpu->cpum.GstCtx.ecx));
7075 AssertMsgReturn(rcStrict == VERR_CPUM_RAISE_GP_0, ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)), VERR_IPE_UNEXPECTED_STATUS);
7076 return iemRaiseGeneralProtectionFault0(pVCpu);
7077}
7078
7079
7080/**
7081 * Implements WRMSR.
7082 */
7083IEM_CIMPL_DEF_0(iemCImpl_wrmsr)
7084{
7085 /*
7086 * Check preconditions.
7087 */
7088 if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fMsr)
7089 return iemRaiseUndefinedOpcode(pVCpu);
7090 if (pVCpu->iem.s.uCpl != 0)
7091 return iemRaiseGeneralProtectionFault0(pVCpu);
7092
7093 RTUINT64U uValue;
7094 uValue.s.Lo = pVCpu->cpum.GstCtx.eax;
7095 uValue.s.Hi = pVCpu->cpum.GstCtx.edx;
7096
7097 uint32_t const idMsr = pVCpu->cpum.GstCtx.ecx;
7098
7099 /** @todo make CPUMAllMsrs.cpp import the necessary MSR state. */
7100 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_ALL_MSRS);
7101
7102 /*
7103 * Check nested-guest intercepts.
7104 */
7105#ifdef VBOX_WITH_NESTED_HWVIRT_VMX
7106 if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
7107 {
7108 if (iemVmxIsRdmsrWrmsrInterceptSet(pVCpu, VMX_EXIT_WRMSR, idMsr))
7109 IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_WRMSR, cbInstr);
7110 }
7111#endif
7112
7113#ifdef VBOX_WITH_NESTED_HWVIRT_SVM
7114 if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_MSR_PROT))
7115 {
7116 VBOXSTRICTRC rcStrict = iemSvmHandleMsrIntercept(pVCpu, idMsr, true /* fWrite */);
7117 if (rcStrict == VINF_SVM_VMEXIT)
7118 return VINF_SUCCESS;
7119 if (rcStrict != VINF_SVM_INTERCEPT_NOT_ACTIVE)
7120 {
7121 Log(("IEM: SVM intercepted rdmsr(%#x) failed. rc=%Rrc\n", idMsr, VBOXSTRICTRC_VAL(rcStrict)));
7122 return rcStrict;
7123 }
7124 }
7125#endif
7126
7127 /*
7128 * Do the job.
7129 */
7130 VBOXSTRICTRC rcStrict = CPUMSetGuestMsr(pVCpu, idMsr, uValue.u);
7131 if (rcStrict == VINF_SUCCESS)
7132 {
7133 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7134 return VINF_SUCCESS;
7135 }
7136
7137#ifndef IN_RING3
7138 /* Deferred to ring-3. */
7139 if (rcStrict == VINF_CPUM_R3_MSR_WRITE)
7140 {
7141 Log(("IEM: wrmsr(%#x) -> ring-3\n", idMsr));
7142 return rcStrict;
7143 }
7144#endif
7145
7146 /* Often a unimplemented MSR or MSR bit, so worth logging. */
7147 if (pVCpu->iem.s.cLogRelWrMsr < 32)
7148 {
7149 pVCpu->iem.s.cLogRelWrMsr++;
7150 LogRel(("IEM: wrmsr(%#x,%#x`%08x) -> #GP(0)\n", idMsr, uValue.s.Hi, uValue.s.Lo));
7151 }
7152 else
7153 Log(( "IEM: wrmsr(%#x,%#x`%08x) -> #GP(0)\n", idMsr, uValue.s.Hi, uValue.s.Lo));
7154 AssertMsgReturn(rcStrict == VERR_CPUM_RAISE_GP_0, ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)), VERR_IPE_UNEXPECTED_STATUS);
7155 return iemRaiseGeneralProtectionFault0(pVCpu);
7156}
7157
7158
7159/**
7160 * Implements 'IN eAX, port'.
7161 *
7162 * @param u16Port The source port.
7163 * @param fImm Whether the port was specified through an immediate operand
7164 * or the implicit DX register.
7165 * @param cbReg The register size.
7166 */
7167IEM_CIMPL_DEF_3(iemCImpl_in, uint16_t, u16Port, bool, fImm, uint8_t, cbReg)
7168{
7169 /*
7170 * CPL check
7171 */
7172 VBOXSTRICTRC rcStrict = iemHlpCheckPortIOPermission(pVCpu, u16Port, cbReg);
7173 if (rcStrict != VINF_SUCCESS)
7174 return rcStrict;
7175
7176 /*
7177 * Check VMX nested-guest IO intercept.
7178 */
7179#ifdef VBOX_WITH_NESTED_HWVIRT_VMX
7180 if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
7181 {
7182 rcStrict = iemVmxVmexitInstrIo(pVCpu, VMXINSTRID_IO_IN, u16Port, fImm, cbReg, cbInstr);
7183 if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
7184 return rcStrict;
7185 }
7186#else
7187 RT_NOREF(fImm);
7188#endif
7189
7190 /*
7191 * Check SVM nested-guest IO intercept.
7192 */
7193#ifdef VBOX_WITH_NESTED_HWVIRT_SVM
7194 if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_IOIO_PROT))
7195 {
7196 uint8_t cAddrSizeBits;
7197 switch (pVCpu->iem.s.enmEffAddrMode)
7198 {
7199 case IEMMODE_16BIT: cAddrSizeBits = 16; break;
7200 case IEMMODE_32BIT: cAddrSizeBits = 32; break;
7201 case IEMMODE_64BIT: cAddrSizeBits = 64; break;
7202 IEM_NOT_REACHED_DEFAULT_CASE_RET();
7203 }
7204 rcStrict = iemSvmHandleIOIntercept(pVCpu, u16Port, SVMIOIOTYPE_IN, cbReg, cAddrSizeBits, 0 /* N/A - iEffSeg */,
7205 false /* fRep */, false /* fStrIo */, cbInstr);
7206 if (rcStrict == VINF_SVM_VMEXIT)
7207 return VINF_SUCCESS;
7208 if (rcStrict != VINF_SVM_INTERCEPT_NOT_ACTIVE)
7209 {
7210 Log(("iemCImpl_in: iemSvmHandleIOIntercept failed (u16Port=%#x, cbReg=%u) rc=%Rrc\n", u16Port, cbReg,
7211 VBOXSTRICTRC_VAL(rcStrict)));
7212 return rcStrict;
7213 }
7214 }
7215#endif
7216
7217 /*
7218 * Perform the I/O.
7219 */
7220 uint32_t u32Value = 0;
7221 rcStrict = IOMIOPortRead(pVCpu->CTX_SUFF(pVM), pVCpu, u16Port, &u32Value, cbReg);
7222 if (IOM_SUCCESS(rcStrict))
7223 {
7224 switch (cbReg)
7225 {
7226 case 1: pVCpu->cpum.GstCtx.al = (uint8_t)u32Value; break;
7227 case 2: pVCpu->cpum.GstCtx.ax = (uint16_t)u32Value; break;
7228 case 4: pVCpu->cpum.GstCtx.rax = u32Value; break;
7229 default: AssertFailedReturn(VERR_IEM_IPE_3);
7230 }
7231 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7232 pVCpu->iem.s.cPotentialExits++;
7233 if (rcStrict != VINF_SUCCESS)
7234 rcStrict = iemSetPassUpStatus(pVCpu, rcStrict);
7235 Assert(rcStrict == VINF_SUCCESS); /* assumed below */
7236
7237 /*
7238 * Check for I/O breakpoints.
7239 */
7240 uint32_t const uDr7 = pVCpu->cpum.GstCtx.dr[7];
7241 if (RT_UNLIKELY( ( (uDr7 & X86_DR7_ENABLED_MASK)
7242 && X86_DR7_ANY_RW_IO(uDr7)
7243 && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_DE))
7244 || DBGFBpIsHwIoArmed(pVCpu->CTX_SUFF(pVM))))
7245 {
7246 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3 | CPUMCTX_EXTRN_DR6);
7247 rcStrict = DBGFBpCheckIo(pVCpu->CTX_SUFF(pVM), pVCpu, IEM_GET_CTX(pVCpu), u16Port, cbReg);
7248 if (rcStrict == VINF_EM_RAW_GUEST_TRAP)
7249 rcStrict = iemRaiseDebugException(pVCpu);
7250 }
7251 }
7252
7253 return rcStrict;
7254}
7255
7256
7257/**
7258 * Implements 'IN eAX, DX'.
7259 *
7260 * @param cbReg The register size.
7261 */
7262IEM_CIMPL_DEF_1(iemCImpl_in_eAX_DX, uint8_t, cbReg)
7263{
7264 return IEM_CIMPL_CALL_3(iemCImpl_in, pVCpu->cpum.GstCtx.dx, false /* fImm */, cbReg);
7265}
7266
7267
7268/**
7269 * Implements 'OUT port, eAX'.
7270 *
7271 * @param u16Port The destination port.
7272 * @param fImm Whether the port was specified through an immediate operand
7273 * or the implicit DX register.
7274 * @param cbReg The register size.
7275 */
7276IEM_CIMPL_DEF_3(iemCImpl_out, uint16_t, u16Port, bool, fImm, uint8_t, cbReg)
7277{
7278 /*
7279 * CPL check
7280 */
7281 VBOXSTRICTRC rcStrict = iemHlpCheckPortIOPermission(pVCpu, u16Port, cbReg);
7282 if (rcStrict != VINF_SUCCESS)
7283 return rcStrict;
7284
7285 /*
7286 * Check VMX nested-guest I/O intercept.
7287 */
7288#ifdef VBOX_WITH_NESTED_HWVIRT_VMX
7289 if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
7290 {
7291 rcStrict = iemVmxVmexitInstrIo(pVCpu, VMXINSTRID_IO_OUT, u16Port, fImm, cbReg, cbInstr);
7292 if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
7293 return rcStrict;
7294 }
7295#else
7296 RT_NOREF(fImm);
7297#endif
7298
7299 /*
7300 * Check SVM nested-guest I/O intercept.
7301 */
7302#ifdef VBOX_WITH_NESTED_HWVIRT_SVM
7303 if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_IOIO_PROT))
7304 {
7305 uint8_t cAddrSizeBits;
7306 switch (pVCpu->iem.s.enmEffAddrMode)
7307 {
7308 case IEMMODE_16BIT: cAddrSizeBits = 16; break;
7309 case IEMMODE_32BIT: cAddrSizeBits = 32; break;
7310 case IEMMODE_64BIT: cAddrSizeBits = 64; break;
7311 IEM_NOT_REACHED_DEFAULT_CASE_RET();
7312 }
7313 rcStrict = iemSvmHandleIOIntercept(pVCpu, u16Port, SVMIOIOTYPE_OUT, cbReg, cAddrSizeBits, 0 /* N/A - iEffSeg */,
7314 false /* fRep */, false /* fStrIo */, cbInstr);
7315 if (rcStrict == VINF_SVM_VMEXIT)
7316 return VINF_SUCCESS;
7317 if (rcStrict != VINF_SVM_INTERCEPT_NOT_ACTIVE)
7318 {
7319 Log(("iemCImpl_out: iemSvmHandleIOIntercept failed (u16Port=%#x, cbReg=%u) rc=%Rrc\n", u16Port, cbReg,
7320 VBOXSTRICTRC_VAL(rcStrict)));
7321 return rcStrict;
7322 }
7323 }
7324#endif
7325
7326 /*
7327 * Perform the I/O.
7328 */
7329 uint32_t u32Value;
7330 switch (cbReg)
7331 {
7332 case 1: u32Value = pVCpu->cpum.GstCtx.al; break;
7333 case 2: u32Value = pVCpu->cpum.GstCtx.ax; break;
7334 case 4: u32Value = pVCpu->cpum.GstCtx.eax; break;
7335 default: AssertFailedReturn(VERR_IEM_IPE_4);
7336 }
7337 rcStrict = IOMIOPortWrite(pVCpu->CTX_SUFF(pVM), pVCpu, u16Port, u32Value, cbReg);
7338 if (IOM_SUCCESS(rcStrict))
7339 {
7340 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7341 pVCpu->iem.s.cPotentialExits++;
7342 if (rcStrict != VINF_SUCCESS)
7343 rcStrict = iemSetPassUpStatus(pVCpu, rcStrict);
7344 Assert(rcStrict == VINF_SUCCESS); /* assumed below */
7345
7346 /*
7347 * Check for I/O breakpoints.
7348 */
7349 uint32_t const uDr7 = pVCpu->cpum.GstCtx.dr[7];
7350 if (RT_UNLIKELY( ( (uDr7 & X86_DR7_ENABLED_MASK)
7351 && X86_DR7_ANY_RW_IO(uDr7)
7352 && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_DE))
7353 || DBGFBpIsHwIoArmed(pVCpu->CTX_SUFF(pVM))))
7354 {
7355 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR0_DR3 | CPUMCTX_EXTRN_DR6);
7356 rcStrict = DBGFBpCheckIo(pVCpu->CTX_SUFF(pVM), pVCpu, IEM_GET_CTX(pVCpu), u16Port, cbReg);
7357 if (rcStrict == VINF_EM_RAW_GUEST_TRAP)
7358 rcStrict = iemRaiseDebugException(pVCpu);
7359 }
7360 }
7361 return rcStrict;
7362}
7363
7364
7365/**
7366 * Implements 'OUT DX, eAX'.
7367 *
7368 * @param cbReg The register size.
7369 */
7370IEM_CIMPL_DEF_1(iemCImpl_out_DX_eAX, uint8_t, cbReg)
7371{
7372 return IEM_CIMPL_CALL_3(iemCImpl_out, pVCpu->cpum.GstCtx.dx, false /* fImm */, cbReg);
7373}
7374
7375
7376/**
7377 * Implements 'CLI'.
7378 */
7379IEM_CIMPL_DEF_0(iemCImpl_cli)
7380{
7381 uint32_t fEfl = IEMMISC_GET_EFL(pVCpu);
7382 uint32_t const fEflOld = fEfl;
7383
7384 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_CR4);
7385 if (pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE)
7386 {
7387 uint8_t const uIopl = X86_EFL_GET_IOPL(fEfl);
7388 if (!(fEfl & X86_EFL_VM))
7389 {
7390 if (pVCpu->iem.s.uCpl <= uIopl)
7391 fEfl &= ~X86_EFL_IF;
7392 else if ( pVCpu->iem.s.uCpl == 3
7393 && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_PVI) )
7394 fEfl &= ~X86_EFL_VIF;
7395 else
7396 return iemRaiseGeneralProtectionFault0(pVCpu);
7397 }
7398 /* V8086 */
7399 else if (uIopl == 3)
7400 fEfl &= ~X86_EFL_IF;
7401 else if ( uIopl < 3
7402 && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_VME) )
7403 fEfl &= ~X86_EFL_VIF;
7404 else
7405 return iemRaiseGeneralProtectionFault0(pVCpu);
7406 }
7407 /* real mode */
7408 else
7409 fEfl &= ~X86_EFL_IF;
7410
7411 /* Commit. */
7412 IEMMISC_SET_EFL(pVCpu, fEfl);
7413 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7414 Log2(("CLI: %#x -> %#x\n", fEflOld, fEfl)); NOREF(fEflOld);
7415 return VINF_SUCCESS;
7416}
7417
7418
7419/**
7420 * Implements 'STI'.
7421 */
7422IEM_CIMPL_DEF_0(iemCImpl_sti)
7423{
7424 uint32_t fEfl = IEMMISC_GET_EFL(pVCpu);
7425 uint32_t const fEflOld = fEfl;
7426
7427 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_CR4);
7428 if (pVCpu->cpum.GstCtx.cr0 & X86_CR0_PE)
7429 {
7430 uint8_t const uIopl = X86_EFL_GET_IOPL(fEfl);
7431 if (!(fEfl & X86_EFL_VM))
7432 {
7433 if (pVCpu->iem.s.uCpl <= uIopl)
7434 fEfl |= X86_EFL_IF;
7435 else if ( pVCpu->iem.s.uCpl == 3
7436 && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_PVI)
7437 && !(fEfl & X86_EFL_VIP) )
7438 fEfl |= X86_EFL_VIF;
7439 else
7440 return iemRaiseGeneralProtectionFault0(pVCpu);
7441 }
7442 /* V8086 */
7443 else if (uIopl == 3)
7444 fEfl |= X86_EFL_IF;
7445 else if ( uIopl < 3
7446 && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_VME)
7447 && !(fEfl & X86_EFL_VIP) )
7448 fEfl |= X86_EFL_VIF;
7449 else
7450 return iemRaiseGeneralProtectionFault0(pVCpu);
7451 }
7452 /* real mode */
7453 else
7454 fEfl |= X86_EFL_IF;
7455
7456 /* Commit. */
7457 IEMMISC_SET_EFL(pVCpu, fEfl);
7458 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7459 if (!(fEflOld & X86_EFL_IF) && (fEfl & X86_EFL_IF))
7460 EMSetInhibitInterruptsPC(pVCpu, pVCpu->cpum.GstCtx.rip);
7461 Log2(("STI: %#x -> %#x\n", fEflOld, fEfl));
7462 return VINF_SUCCESS;
7463}
7464
7465
7466/**
7467 * Implements 'HLT'.
7468 */
7469IEM_CIMPL_DEF_0(iemCImpl_hlt)
7470{
7471 if (pVCpu->iem.s.uCpl != 0)
7472 return iemRaiseGeneralProtectionFault0(pVCpu);
7473
7474 if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
7475 && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_HLT_EXIT))
7476 {
7477 Log2(("hlt: Guest intercept -> VM-exit\n"));
7478 IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_HLT, cbInstr);
7479 }
7480
7481 if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_HLT))
7482 {
7483 Log2(("hlt: Guest intercept -> #VMEXIT\n"));
7484 IEM_SVM_UPDATE_NRIP(pVCpu);
7485 IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_HLT, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
7486 }
7487
7488 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7489 return VINF_EM_HALT;
7490}
7491
7492
7493/**
7494 * Implements 'MONITOR'.
7495 */
7496IEM_CIMPL_DEF_1(iemCImpl_monitor, uint8_t, iEffSeg)
7497{
7498 /*
7499 * Permission checks.
7500 */
7501 if (pVCpu->iem.s.uCpl != 0)
7502 {
7503 Log2(("monitor: CPL != 0\n"));
7504 return iemRaiseUndefinedOpcode(pVCpu); /** @todo MSR[0xC0010015].MonMwaitUserEn if we care. */
7505 }
7506 if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fMonitorMWait)
7507 {
7508 Log2(("monitor: Not in CPUID\n"));
7509 return iemRaiseUndefinedOpcode(pVCpu);
7510 }
7511
7512 /*
7513 * Check VMX guest-intercept.
7514 * This should be considered a fault-like VM-exit.
7515 * See Intel spec. 25.1.1 "Relative Priority of Faults and VM Exits".
7516 */
7517 if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
7518 && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_MONITOR_EXIT))
7519 {
7520 Log2(("monitor: Guest intercept -> #VMEXIT\n"));
7521 IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_MONITOR, cbInstr);
7522 }
7523
7524 /*
7525 * Gather the operands and validate them.
7526 */
7527 RTGCPTR GCPtrMem = pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT ? pVCpu->cpum.GstCtx.rax : pVCpu->cpum.GstCtx.eax;
7528 uint32_t uEcx = pVCpu->cpum.GstCtx.ecx;
7529 uint32_t uEdx = pVCpu->cpum.GstCtx.edx;
7530/** @todo Test whether EAX or ECX is processed first, i.e. do we get \#PF or
7531 * \#GP first. */
7532 if (uEcx != 0)
7533 {
7534 Log2(("monitor rax=%RX64, ecx=%RX32, edx=%RX32; ECX != 0 -> #GP(0)\n", GCPtrMem, uEcx, uEdx)); NOREF(uEdx);
7535 return iemRaiseGeneralProtectionFault0(pVCpu);
7536 }
7537
7538 VBOXSTRICTRC rcStrict = iemMemApplySegment(pVCpu, IEM_ACCESS_TYPE_READ | IEM_ACCESS_WHAT_DATA, iEffSeg, 1, &GCPtrMem);
7539 if (rcStrict != VINF_SUCCESS)
7540 return rcStrict;
7541
7542 RTGCPHYS GCPhysMem;
7543 rcStrict = iemMemPageTranslateAndCheckAccess(pVCpu, GCPtrMem, IEM_ACCESS_TYPE_READ | IEM_ACCESS_WHAT_DATA, &GCPhysMem);
7544 if (rcStrict != VINF_SUCCESS)
7545 return rcStrict;
7546
7547#ifdef VBOX_WITH_NESTED_HWVIRT_VMX
7548 if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
7549 && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_VIRT_APIC_ACCESS))
7550 {
7551 /*
7552 * MONITOR does not access the memory, just monitors the address. However,
7553 * if the address falls in the APIC-access page, the address monitored must
7554 * instead be the corresponding address in the virtual-APIC page.
7555 *
7556 * See Intel spec. 29.4.4 "Instruction-Specific Considerations".
7557 */
7558 rcStrict = iemVmxVirtApicAccessUnused(pVCpu, &GCPhysMem);
7559 if ( rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE
7560 && rcStrict != VINF_VMX_MODIFIES_BEHAVIOR)
7561 return rcStrict;
7562 }
7563#endif
7564
7565 if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_MONITOR))
7566 {
7567 Log2(("monitor: Guest intercept -> #VMEXIT\n"));
7568 IEM_SVM_UPDATE_NRIP(pVCpu);
7569 IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_MONITOR, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
7570 }
7571
7572 /*
7573 * Call EM to prepare the monitor/wait.
7574 */
7575 rcStrict = EMMonitorWaitPrepare(pVCpu, pVCpu->cpum.GstCtx.rax, pVCpu->cpum.GstCtx.rcx, pVCpu->cpum.GstCtx.rdx, GCPhysMem);
7576 Assert(rcStrict == VINF_SUCCESS);
7577
7578 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7579 return rcStrict;
7580}
7581
7582
7583/**
7584 * Implements 'MWAIT'.
7585 */
7586IEM_CIMPL_DEF_0(iemCImpl_mwait)
7587{
7588 /*
7589 * Permission checks.
7590 */
7591 if (pVCpu->iem.s.uCpl != 0)
7592 {
7593 Log2(("mwait: CPL != 0\n"));
7594 /** @todo MSR[0xC0010015].MonMwaitUserEn if we care. (Remember to check
7595 * EFLAGS.VM then.) */
7596 return iemRaiseUndefinedOpcode(pVCpu);
7597 }
7598 if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fMonitorMWait)
7599 {
7600 Log2(("mwait: Not in CPUID\n"));
7601 return iemRaiseUndefinedOpcode(pVCpu);
7602 }
7603
7604 /* Check VMX nested-guest intercept. */
7605 if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
7606 && IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_MWAIT_EXIT))
7607 IEM_VMX_VMEXIT_MWAIT_RET(pVCpu, EMMonitorIsArmed(pVCpu), cbInstr);
7608
7609 /*
7610 * Gather the operands and validate them.
7611 */
7612 uint32_t const uEax = pVCpu->cpum.GstCtx.eax;
7613 uint32_t const uEcx = pVCpu->cpum.GstCtx.ecx;
7614 if (uEcx != 0)
7615 {
7616 /* Only supported extension is break on IRQ when IF=0. */
7617 if (uEcx > 1)
7618 {
7619 Log2(("mwait eax=%RX32, ecx=%RX32; ECX > 1 -> #GP(0)\n", uEax, uEcx));
7620 return iemRaiseGeneralProtectionFault0(pVCpu);
7621 }
7622 uint32_t fMWaitFeatures = 0;
7623 uint32_t uIgnore = 0;
7624 CPUMGetGuestCpuId(pVCpu, 5, 0, &uIgnore, &uIgnore, &fMWaitFeatures, &uIgnore);
7625 if ( (fMWaitFeatures & (X86_CPUID_MWAIT_ECX_EXT | X86_CPUID_MWAIT_ECX_BREAKIRQIF0))
7626 != (X86_CPUID_MWAIT_ECX_EXT | X86_CPUID_MWAIT_ECX_BREAKIRQIF0))
7627 {
7628 Log2(("mwait eax=%RX32, ecx=%RX32; break-on-IRQ-IF=0 extension not enabled -> #GP(0)\n", uEax, uEcx));
7629 return iemRaiseGeneralProtectionFault0(pVCpu);
7630 }
7631
7632#ifdef VBOX_WITH_NESTED_HWVIRT_VMX
7633 /*
7634 * If the interrupt-window exiting control is set or a virtual-interrupt is pending
7635 * for delivery; and interrupts are disabled the processor does not enter its
7636 * mwait state but rather passes control to the next instruction.
7637 *
7638 * See Intel spec. 25.3 "Changes to Instruction Behavior In VMX Non-root Operation".
7639 */
7640 if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
7641 && !pVCpu->cpum.GstCtx.eflags.Bits.u1IF)
7642 {
7643 if ( IEM_VMX_IS_PROCCTLS_SET(pVCpu, VMX_PROC_CTLS_INT_WINDOW_EXIT)
7644 || VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_NESTED_GUEST))
7645 {
7646 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7647 return VINF_SUCCESS;
7648 }
7649 }
7650#endif
7651 }
7652
7653 /*
7654 * Check SVM nested-guest mwait intercepts.
7655 */
7656 if ( IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_MWAIT_ARMED)
7657 && EMMonitorIsArmed(pVCpu))
7658 {
7659 Log2(("mwait: Guest intercept (monitor hardware armed) -> #VMEXIT\n"));
7660 IEM_SVM_UPDATE_NRIP(pVCpu);
7661 IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_MWAIT_ARMED, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
7662 }
7663 if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_MWAIT))
7664 {
7665 Log2(("mwait: Guest intercept -> #VMEXIT\n"));
7666 IEM_SVM_UPDATE_NRIP(pVCpu);
7667 IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_MWAIT, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
7668 }
7669
7670 /*
7671 * Call EM to prepare the monitor/wait.
7672 */
7673 VBOXSTRICTRC rcStrict = EMMonitorWaitPerform(pVCpu, uEax, uEcx);
7674
7675 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7676 return rcStrict;
7677}
7678
7679
7680/**
7681 * Implements 'SWAPGS'.
7682 */
7683IEM_CIMPL_DEF_0(iemCImpl_swapgs)
7684{
7685 Assert(pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT); /* Caller checks this. */
7686
7687 /*
7688 * Permission checks.
7689 */
7690 if (pVCpu->iem.s.uCpl != 0)
7691 {
7692 Log2(("swapgs: CPL != 0\n"));
7693 return iemRaiseUndefinedOpcode(pVCpu);
7694 }
7695
7696 /*
7697 * Do the job.
7698 */
7699 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_KERNEL_GS_BASE | CPUMCTX_EXTRN_GS);
7700 uint64_t uOtherGsBase = pVCpu->cpum.GstCtx.msrKERNELGSBASE;
7701 pVCpu->cpum.GstCtx.msrKERNELGSBASE = pVCpu->cpum.GstCtx.gs.u64Base;
7702 pVCpu->cpum.GstCtx.gs.u64Base = uOtherGsBase;
7703
7704 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7705 return VINF_SUCCESS;
7706}
7707
7708
7709/**
7710 * Implements 'CPUID'.
7711 */
7712IEM_CIMPL_DEF_0(iemCImpl_cpuid)
7713{
7714 if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
7715 {
7716 Log2(("cpuid: Guest intercept -> VM-exit\n"));
7717 IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_CPUID, cbInstr);
7718 }
7719
7720 if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_CPUID))
7721 {
7722 Log2(("cpuid: Guest intercept -> #VMEXIT\n"));
7723 IEM_SVM_UPDATE_NRIP(pVCpu);
7724 IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_CPUID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
7725 }
7726
7727 CPUMGetGuestCpuId(pVCpu, pVCpu->cpum.GstCtx.eax, pVCpu->cpum.GstCtx.ecx,
7728 &pVCpu->cpum.GstCtx.eax, &pVCpu->cpum.GstCtx.ebx, &pVCpu->cpum.GstCtx.ecx, &pVCpu->cpum.GstCtx.edx);
7729 pVCpu->cpum.GstCtx.rax &= UINT32_C(0xffffffff);
7730 pVCpu->cpum.GstCtx.rbx &= UINT32_C(0xffffffff);
7731 pVCpu->cpum.GstCtx.rcx &= UINT32_C(0xffffffff);
7732 pVCpu->cpum.GstCtx.rdx &= UINT32_C(0xffffffff);
7733 pVCpu->cpum.GstCtx.fExtrn &= ~(CPUMCTX_EXTRN_RAX | CPUMCTX_EXTRN_RCX | CPUMCTX_EXTRN_RDX | CPUMCTX_EXTRN_RBX);
7734
7735 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7736 pVCpu->iem.s.cPotentialExits++;
7737 return VINF_SUCCESS;
7738}
7739
7740
7741/**
7742 * Implements 'AAD'.
7743 *
7744 * @param bImm The immediate operand.
7745 */
7746IEM_CIMPL_DEF_1(iemCImpl_aad, uint8_t, bImm)
7747{
7748 uint16_t const ax = pVCpu->cpum.GstCtx.ax;
7749 uint8_t const al = (uint8_t)ax + (uint8_t)(ax >> 8) * bImm;
7750 pVCpu->cpum.GstCtx.ax = al;
7751 iemHlpUpdateArithEFlagsU8(pVCpu, al,
7752 X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF,
7753 X86_EFL_OF | X86_EFL_AF | X86_EFL_CF);
7754
7755 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7756 return VINF_SUCCESS;
7757}
7758
7759
7760/**
7761 * Implements 'AAM'.
7762 *
7763 * @param bImm The immediate operand. Cannot be 0.
7764 */
7765IEM_CIMPL_DEF_1(iemCImpl_aam, uint8_t, bImm)
7766{
7767 Assert(bImm != 0); /* #DE on 0 is handled in the decoder. */
7768
7769 uint16_t const ax = pVCpu->cpum.GstCtx.ax;
7770 uint8_t const al = (uint8_t)ax % bImm;
7771 uint8_t const ah = (uint8_t)ax / bImm;
7772 pVCpu->cpum.GstCtx.ax = (ah << 8) + al;
7773 iemHlpUpdateArithEFlagsU8(pVCpu, al,
7774 X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF,
7775 X86_EFL_OF | X86_EFL_AF | X86_EFL_CF);
7776
7777 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7778 return VINF_SUCCESS;
7779}
7780
7781
7782/**
7783 * Implements 'DAA'.
7784 */
7785IEM_CIMPL_DEF_0(iemCImpl_daa)
7786{
7787 uint8_t const al = pVCpu->cpum.GstCtx.al;
7788 bool const fCarry = pVCpu->cpum.GstCtx.eflags.Bits.u1CF;
7789
7790 if ( pVCpu->cpum.GstCtx.eflags.Bits.u1AF
7791 || (al & 0xf) >= 10)
7792 {
7793 pVCpu->cpum.GstCtx.al = al + 6;
7794 pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 1;
7795 }
7796 else
7797 pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 0;
7798
7799 if (al >= 0x9a || fCarry)
7800 {
7801 pVCpu->cpum.GstCtx.al += 0x60;
7802 pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
7803 }
7804 else
7805 pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 0;
7806
7807 iemHlpUpdateArithEFlagsU8(pVCpu, pVCpu->cpum.GstCtx.al, X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF, X86_EFL_OF);
7808 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7809 return VINF_SUCCESS;
7810}
7811
7812
7813/**
7814 * Implements 'DAS'.
7815 */
7816IEM_CIMPL_DEF_0(iemCImpl_das)
7817{
7818 uint8_t const uInputAL = pVCpu->cpum.GstCtx.al;
7819 bool const fCarry = pVCpu->cpum.GstCtx.eflags.Bits.u1CF;
7820
7821 if ( pVCpu->cpum.GstCtx.eflags.Bits.u1AF
7822 || (uInputAL & 0xf) >= 10)
7823 {
7824 pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 1;
7825 if (uInputAL < 6)
7826 pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
7827 pVCpu->cpum.GstCtx.al = uInputAL - 6;
7828 }
7829 else
7830 {
7831 pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 0;
7832 pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 0;
7833 }
7834
7835 if (uInputAL >= 0x9a || fCarry)
7836 {
7837 pVCpu->cpum.GstCtx.al -= 0x60;
7838 pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
7839 }
7840
7841 iemHlpUpdateArithEFlagsU8(pVCpu, pVCpu->cpum.GstCtx.al, X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF, X86_EFL_OF);
7842 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7843 return VINF_SUCCESS;
7844}
7845
7846
7847/**
7848 * Implements 'AAA'.
7849 */
7850IEM_CIMPL_DEF_0(iemCImpl_aaa)
7851{
7852 if (IEM_IS_GUEST_CPU_AMD(pVCpu))
7853 {
7854 if ( pVCpu->cpum.GstCtx.eflags.Bits.u1AF
7855 || (pVCpu->cpum.GstCtx.ax & 0xf) >= 10)
7856 {
7857 iemAImpl_add_u16(&pVCpu->cpum.GstCtx.ax, 0x106, &pVCpu->cpum.GstCtx.eflags.u32);
7858 pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 1;
7859 pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
7860 }
7861 else
7862 {
7863 iemHlpUpdateArithEFlagsU16(pVCpu, pVCpu->cpum.GstCtx.ax, X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF, X86_EFL_OF);
7864 pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 0;
7865 pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 0;
7866 }
7867 pVCpu->cpum.GstCtx.ax &= UINT16_C(0xff0f);
7868 }
7869 else
7870 {
7871 if ( pVCpu->cpum.GstCtx.eflags.Bits.u1AF
7872 || (pVCpu->cpum.GstCtx.ax & 0xf) >= 10)
7873 {
7874 pVCpu->cpum.GstCtx.ax += UINT16_C(0x106);
7875 pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 1;
7876 pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
7877 }
7878 else
7879 {
7880 pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 0;
7881 pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 0;
7882 }
7883 pVCpu->cpum.GstCtx.ax &= UINT16_C(0xff0f);
7884 iemHlpUpdateArithEFlagsU8(pVCpu, pVCpu->cpum.GstCtx.al, X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF, X86_EFL_OF);
7885 }
7886
7887 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7888 return VINF_SUCCESS;
7889}
7890
7891
7892/**
7893 * Implements 'AAS'.
7894 */
7895IEM_CIMPL_DEF_0(iemCImpl_aas)
7896{
7897 if (IEM_IS_GUEST_CPU_AMD(pVCpu))
7898 {
7899 if ( pVCpu->cpum.GstCtx.eflags.Bits.u1AF
7900 || (pVCpu->cpum.GstCtx.ax & 0xf) >= 10)
7901 {
7902 iemAImpl_sub_u16(&pVCpu->cpum.GstCtx.ax, 0x106, &pVCpu->cpum.GstCtx.eflags.u32);
7903 pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 1;
7904 pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
7905 }
7906 else
7907 {
7908 iemHlpUpdateArithEFlagsU16(pVCpu, pVCpu->cpum.GstCtx.ax, X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF, X86_EFL_OF);
7909 pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 0;
7910 pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 0;
7911 }
7912 pVCpu->cpum.GstCtx.ax &= UINT16_C(0xff0f);
7913 }
7914 else
7915 {
7916 if ( pVCpu->cpum.GstCtx.eflags.Bits.u1AF
7917 || (pVCpu->cpum.GstCtx.ax & 0xf) >= 10)
7918 {
7919 pVCpu->cpum.GstCtx.ax -= UINT16_C(0x106);
7920 pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 1;
7921 pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 1;
7922 }
7923 else
7924 {
7925 pVCpu->cpum.GstCtx.eflags.Bits.u1AF = 0;
7926 pVCpu->cpum.GstCtx.eflags.Bits.u1CF = 0;
7927 }
7928 pVCpu->cpum.GstCtx.ax &= UINT16_C(0xff0f);
7929 iemHlpUpdateArithEFlagsU8(pVCpu, pVCpu->cpum.GstCtx.al, X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF, X86_EFL_OF);
7930 }
7931
7932 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7933 return VINF_SUCCESS;
7934}
7935
7936
7937/**
7938 * Implements the 16-bit version of 'BOUND'.
7939 *
7940 * @note We have separate 16-bit and 32-bit variants of this function due to
7941 * the decoder using unsigned parameters, whereas we want signed one to
7942 * do the job. This is significant for a recompiler.
7943 */
7944IEM_CIMPL_DEF_3(iemCImpl_bound_16, int16_t, idxArray, int16_t, idxLowerBound, int16_t, idxUpperBound)
7945{
7946 /*
7947 * Check if the index is inside the bounds, otherwise raise #BR.
7948 */
7949 if ( idxArray >= idxLowerBound
7950 && idxArray <= idxUpperBound)
7951 {
7952 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7953 return VINF_SUCCESS;
7954 }
7955
7956 return iemRaiseBoundRangeExceeded(pVCpu);
7957}
7958
7959
7960/**
7961 * Implements the 32-bit version of 'BOUND'.
7962 */
7963IEM_CIMPL_DEF_3(iemCImpl_bound_32, int32_t, idxArray, int32_t, idxLowerBound, int32_t, idxUpperBound)
7964{
7965 /*
7966 * Check if the index is inside the bounds, otherwise raise #BR.
7967 */
7968 if ( idxArray >= idxLowerBound
7969 && idxArray <= idxUpperBound)
7970 {
7971 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7972 return VINF_SUCCESS;
7973 }
7974
7975 return iemRaiseBoundRangeExceeded(pVCpu);
7976}
7977
7978
7979
7980/*
7981 * Instantiate the various string operation combinations.
7982 */
7983#define OP_SIZE 8
7984#define ADDR_SIZE 16
7985#include "IEMAllCImplStrInstr.cpp.h"
7986#define OP_SIZE 8
7987#define ADDR_SIZE 32
7988#include "IEMAllCImplStrInstr.cpp.h"
7989#define OP_SIZE 8
7990#define ADDR_SIZE 64
7991#include "IEMAllCImplStrInstr.cpp.h"
7992
7993#define OP_SIZE 16
7994#define ADDR_SIZE 16
7995#include "IEMAllCImplStrInstr.cpp.h"
7996#define OP_SIZE 16
7997#define ADDR_SIZE 32
7998#include "IEMAllCImplStrInstr.cpp.h"
7999#define OP_SIZE 16
8000#define ADDR_SIZE 64
8001#include "IEMAllCImplStrInstr.cpp.h"
8002
8003#define OP_SIZE 32
8004#define ADDR_SIZE 16
8005#include "IEMAllCImplStrInstr.cpp.h"
8006#define OP_SIZE 32
8007#define ADDR_SIZE 32
8008#include "IEMAllCImplStrInstr.cpp.h"
8009#define OP_SIZE 32
8010#define ADDR_SIZE 64
8011#include "IEMAllCImplStrInstr.cpp.h"
8012
8013#define OP_SIZE 64
8014#define ADDR_SIZE 32
8015#include "IEMAllCImplStrInstr.cpp.h"
8016#define OP_SIZE 64
8017#define ADDR_SIZE 64
8018#include "IEMAllCImplStrInstr.cpp.h"
8019
8020
8021/**
8022 * Implements 'XGETBV'.
8023 */
8024IEM_CIMPL_DEF_0(iemCImpl_xgetbv)
8025{
8026 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR4);
8027 if (pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSXSAVE)
8028 {
8029 uint32_t uEcx = pVCpu->cpum.GstCtx.ecx;
8030 switch (uEcx)
8031 {
8032 case 0:
8033 break;
8034
8035 case 1: /** @todo Implement XCR1 support. */
8036 default:
8037 Log(("xgetbv ecx=%RX32 -> #GP(0)\n", uEcx));
8038 return iemRaiseGeneralProtectionFault0(pVCpu);
8039
8040 }
8041 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_XCRx);
8042 pVCpu->cpum.GstCtx.rax = RT_LO_U32(pVCpu->cpum.GstCtx.aXcr[uEcx]);
8043 pVCpu->cpum.GstCtx.rdx = RT_HI_U32(pVCpu->cpum.GstCtx.aXcr[uEcx]);
8044
8045 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8046 return VINF_SUCCESS;
8047 }
8048 Log(("xgetbv CR4.OSXSAVE=0 -> UD\n"));
8049 return iemRaiseUndefinedOpcode(pVCpu);
8050}
8051
8052
8053/**
8054 * Implements 'XSETBV'.
8055 */
8056IEM_CIMPL_DEF_0(iemCImpl_xsetbv)
8057{
8058 if (pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSXSAVE)
8059 {
8060 if (IEM_SVM_IS_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_XSETBV))
8061 {
8062 Log2(("xsetbv: Guest intercept -> #VMEXIT\n"));
8063 IEM_SVM_UPDATE_NRIP(pVCpu);
8064 IEM_SVM_VMEXIT_RET(pVCpu, SVM_EXIT_XSETBV, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
8065 }
8066
8067 if (pVCpu->iem.s.uCpl == 0)
8068 {
8069 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_XCRx);
8070
8071 if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
8072 IEM_VMX_VMEXIT_INSTR_RET(pVCpu, VMX_EXIT_XSETBV, cbInstr);
8073
8074 uint32_t uEcx = pVCpu->cpum.GstCtx.ecx;
8075 uint64_t uNewValue = RT_MAKE_U64(pVCpu->cpum.GstCtx.eax, pVCpu->cpum.GstCtx.edx);
8076 switch (uEcx)
8077 {
8078 case 0:
8079 {
8080 int rc = CPUMSetGuestXcr0(pVCpu, uNewValue);
8081 if (rc == VINF_SUCCESS)
8082 break;
8083 Assert(rc == VERR_CPUM_RAISE_GP_0);
8084 Log(("xsetbv ecx=%RX32 (newvalue=%RX64) -> #GP(0)\n", uEcx, uNewValue));
8085 return iemRaiseGeneralProtectionFault0(pVCpu);
8086 }
8087
8088 case 1: /** @todo Implement XCR1 support. */
8089 default:
8090 Log(("xsetbv ecx=%RX32 (newvalue=%RX64) -> #GP(0)\n", uEcx, uNewValue));
8091 return iemRaiseGeneralProtectionFault0(pVCpu);
8092
8093 }
8094
8095 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8096 return VINF_SUCCESS;
8097 }
8098
8099 Log(("xsetbv cpl=%u -> GP(0)\n", pVCpu->iem.s.uCpl));
8100 return iemRaiseGeneralProtectionFault0(pVCpu);
8101 }
8102 Log(("xsetbv CR4.OSXSAVE=0 -> UD\n"));
8103 return iemRaiseUndefinedOpcode(pVCpu);
8104}
8105
8106#ifdef IN_RING3
8107
8108/** Argument package for iemCImpl_cmpxchg16b_fallback_rendezvous_callback. */
8109struct IEMCIMPLCX16ARGS
8110{
8111 PRTUINT128U pu128Dst;
8112 PRTUINT128U pu128RaxRdx;
8113 PRTUINT128U pu128RbxRcx;
8114 uint32_t *pEFlags;
8115# ifdef VBOX_STRICT
8116 uint32_t cCalls;
8117# endif
8118};
8119
8120/**
8121 * @callback_method_impl{FNVMMEMTRENDEZVOUS,
8122 * Worker for iemCImpl_cmpxchg16b_fallback_rendezvous}
8123 */
8124static DECLCALLBACK(VBOXSTRICTRC) iemCImpl_cmpxchg16b_fallback_rendezvous_callback(PVM pVM, PVMCPUCC pVCpu, void *pvUser)
8125{
8126 RT_NOREF(pVM, pVCpu);
8127 struct IEMCIMPLCX16ARGS *pArgs = (struct IEMCIMPLCX16ARGS *)pvUser;
8128# ifdef VBOX_STRICT
8129 Assert(pArgs->cCalls == 0);
8130 pArgs->cCalls++;
8131# endif
8132
8133 iemAImpl_cmpxchg16b_fallback(pArgs->pu128Dst, pArgs->pu128RaxRdx, pArgs->pu128RbxRcx, pArgs->pEFlags);
8134 return VINF_SUCCESS;
8135}
8136
8137#endif /* IN_RING3 */
8138
8139/**
8140 * Implements 'CMPXCHG16B' fallback using rendezvous.
8141 */
8142IEM_CIMPL_DEF_4(iemCImpl_cmpxchg16b_fallback_rendezvous, PRTUINT128U, pu128Dst, PRTUINT128U, pu128RaxRdx,
8143 PRTUINT128U, pu128RbxRcx, uint32_t *, pEFlags)
8144{
8145#ifdef IN_RING3
8146 struct IEMCIMPLCX16ARGS Args;
8147 Args.pu128Dst = pu128Dst;
8148 Args.pu128RaxRdx = pu128RaxRdx;
8149 Args.pu128RbxRcx = pu128RbxRcx;
8150 Args.pEFlags = pEFlags;
8151# ifdef VBOX_STRICT
8152 Args.cCalls = 0;
8153# endif
8154 VBOXSTRICTRC rcStrict = VMMR3EmtRendezvous(pVCpu->CTX_SUFF(pVM), VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE,
8155 iemCImpl_cmpxchg16b_fallback_rendezvous_callback, &Args);
8156 Assert(Args.cCalls == 1);
8157 if (rcStrict == VINF_SUCCESS)
8158 {
8159 /* Duplicated tail code. */
8160 rcStrict = iemMemCommitAndUnmap(pVCpu, pu128Dst, IEM_ACCESS_DATA_RW);
8161 if (rcStrict == VINF_SUCCESS)
8162 {
8163 pVCpu->cpum.GstCtx.eflags.u = *pEFlags; /* IEM_MC_COMMIT_EFLAGS */
8164 if (!(*pEFlags & X86_EFL_ZF))
8165 {
8166 pVCpu->cpum.GstCtx.rax = pu128RaxRdx->s.Lo;
8167 pVCpu->cpum.GstCtx.rdx = pu128RaxRdx->s.Hi;
8168 }
8169 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8170 }
8171 }
8172 return rcStrict;
8173#else
8174 RT_NOREF(pVCpu, cbInstr, pu128Dst, pu128RaxRdx, pu128RbxRcx, pEFlags);
8175 return VERR_IEM_ASPECT_NOT_IMPLEMENTED; /* This should get us to ring-3 for now. Should perhaps be replaced later. */
8176#endif
8177}
8178
8179
8180/**
8181 * Implements 'CLFLUSH' and 'CLFLUSHOPT'.
8182 *
8183 * This is implemented in C because it triggers a load like behaviour without
8184 * actually reading anything. Since that's not so common, it's implemented
8185 * here.
8186 *
8187 * @param iEffSeg The effective segment.
8188 * @param GCPtrEff The address of the image.
8189 */
8190IEM_CIMPL_DEF_2(iemCImpl_clflush_clflushopt, uint8_t, iEffSeg, RTGCPTR, GCPtrEff)
8191{
8192 /*
8193 * Pretend to do a load w/o reading (see also iemCImpl_monitor and iemMemMap).
8194 */
8195 VBOXSTRICTRC rcStrict = iemMemApplySegment(pVCpu, IEM_ACCESS_TYPE_READ | IEM_ACCESS_WHAT_DATA, iEffSeg, 1, &GCPtrEff);
8196 if (rcStrict == VINF_SUCCESS)
8197 {
8198 RTGCPHYS GCPhysMem;
8199 rcStrict = iemMemPageTranslateAndCheckAccess(pVCpu, GCPtrEff, IEM_ACCESS_TYPE_READ | IEM_ACCESS_WHAT_DATA, &GCPhysMem);
8200 if (rcStrict == VINF_SUCCESS)
8201 {
8202#ifdef VBOX_WITH_NESTED_HWVIRT_VMX
8203 if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
8204 && IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_VIRT_APIC_ACCESS))
8205 {
8206 /*
8207 * CLFLUSH/CLFLUSHOPT does not access the memory, but flushes the cache-line
8208 * that contains the address. However, if the address falls in the APIC-access
8209 * page, the address flushed must instead be the corresponding address in the
8210 * virtual-APIC page.
8211 *
8212 * See Intel spec. 29.4.4 "Instruction-Specific Considerations".
8213 */
8214 rcStrict = iemVmxVirtApicAccessUnused(pVCpu, &GCPhysMem);
8215 if ( rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE
8216 && rcStrict != VINF_VMX_MODIFIES_BEHAVIOR)
8217 return rcStrict;
8218 }
8219#endif
8220 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8221 return VINF_SUCCESS;
8222 }
8223 }
8224
8225 return rcStrict;
8226}
8227
8228
8229/**
8230 * Implements 'FINIT' and 'FNINIT'.
8231 *
8232 * @param fCheckXcpts Whether to check for umasked pending exceptions or
8233 * not.
8234 */
8235IEM_CIMPL_DEF_1(iemCImpl_finit, bool, fCheckXcpts)
8236{
8237 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0);
8238 if (pVCpu->cpum.GstCtx.cr0 & (X86_CR0_EM | X86_CR0_TS))
8239 return iemRaiseDeviceNotAvailable(pVCpu);
8240
8241 iemFpuActualizeStateForChange(pVCpu);
8242 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_X87);
8243
8244 NOREF(fCheckXcpts); /** @todo trigger pending exceptions:
8245 if (fCheckXcpts && TODO )
8246 return iemRaiseMathFault(pVCpu);
8247 */
8248
8249 PX86XSAVEAREA pXState = &pVCpu->cpum.GstCtx.XState;
8250 pXState->x87.FCW = 0x37f;
8251 pXState->x87.FSW = 0;
8252 pXState->x87.FTW = 0x00; /* 0 - empty. */
8253 pXState->x87.FPUDP = 0;
8254 pXState->x87.DS = 0; //??
8255 pXState->x87.Rsrvd2= 0;
8256 pXState->x87.FPUIP = 0;
8257 pXState->x87.CS = 0; //??
8258 pXState->x87.Rsrvd1= 0;
8259 pXState->x87.FOP = 0;
8260
8261 iemHlpUsedFpu(pVCpu);
8262 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8263 return VINF_SUCCESS;
8264}
8265
8266
8267/**
8268 * Implements 'FXSAVE'.
8269 *
8270 * @param iEffSeg The effective segment.
8271 * @param GCPtrEff The address of the image.
8272 * @param enmEffOpSize The operand size (only REX.W really matters).
8273 */
8274IEM_CIMPL_DEF_3(iemCImpl_fxsave, uint8_t, iEffSeg, RTGCPTR, GCPtrEff, IEMMODE, enmEffOpSize)
8275{
8276 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX);
8277
8278 /*
8279 * Raise exceptions.
8280 */
8281 if (pVCpu->cpum.GstCtx.cr0 & X86_CR0_EM)
8282 return iemRaiseUndefinedOpcode(pVCpu);
8283 if (pVCpu->cpum.GstCtx.cr0 & (X86_CR0_TS | X86_CR0_EM))
8284 return iemRaiseDeviceNotAvailable(pVCpu);
8285 if (GCPtrEff & 15)
8286 {
8287 /** @todo CPU/VM detection possible! \#AC might not be signal for
8288 * all/any misalignment sizes, intel says its an implementation detail. */
8289 if ( (pVCpu->cpum.GstCtx.cr0 & X86_CR0_AM)
8290 && pVCpu->cpum.GstCtx.eflags.Bits.u1AC
8291 && pVCpu->iem.s.uCpl == 3)
8292 return iemRaiseAlignmentCheckException(pVCpu);
8293 return iemRaiseGeneralProtectionFault0(pVCpu);
8294 }
8295
8296 /*
8297 * Access the memory.
8298 */
8299 void *pvMem512;
8300 VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, &pvMem512, 512, iEffSeg, GCPtrEff, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
8301 if (rcStrict != VINF_SUCCESS)
8302 return rcStrict;
8303 PX86FXSTATE pDst = (PX86FXSTATE)pvMem512;
8304 PCX86FXSTATE pSrc = &pVCpu->cpum.GstCtx.XState.x87;
8305
8306 /*
8307 * Store the registers.
8308 */
8309 /** @todo CPU/VM detection possible! If CR4.OSFXSR=0 MXCSR it's
8310 * implementation specific whether MXCSR and XMM0-XMM7 are saved. */
8311
8312 /* common for all formats */
8313 pDst->FCW = pSrc->FCW;
8314 pDst->FSW = pSrc->FSW;
8315 pDst->FTW = pSrc->FTW & UINT16_C(0xff);
8316 pDst->FOP = pSrc->FOP;
8317 pDst->MXCSR = pSrc->MXCSR;
8318 pDst->MXCSR_MASK = CPUMGetGuestMxCsrMask(pVCpu->CTX_SUFF(pVM));
8319 for (uint32_t i = 0; i < RT_ELEMENTS(pDst->aRegs); i++)
8320 {
8321 /** @todo Testcase: What actually happens to the 6 reserved bytes? I'm clearing
8322 * them for now... */
8323 pDst->aRegs[i].au32[0] = pSrc->aRegs[i].au32[0];
8324 pDst->aRegs[i].au32[1] = pSrc->aRegs[i].au32[1];
8325 pDst->aRegs[i].au32[2] = pSrc->aRegs[i].au32[2] & UINT32_C(0xffff);
8326 pDst->aRegs[i].au32[3] = 0;
8327 }
8328
8329 /* FPU IP, CS, DP and DS. */
8330 pDst->FPUIP = pSrc->FPUIP;
8331 pDst->CS = pSrc->CS;
8332 pDst->FPUDP = pSrc->FPUDP;
8333 pDst->DS = pSrc->DS;
8334 if (enmEffOpSize == IEMMODE_64BIT)
8335 {
8336 /* Save upper 16-bits of FPUIP (IP:CS:Rsvd1) and FPUDP (DP:DS:Rsvd2). */
8337 pDst->Rsrvd1 = pSrc->Rsrvd1;
8338 pDst->Rsrvd2 = pSrc->Rsrvd2;
8339 pDst->au32RsrvdForSoftware[0] = 0;
8340 }
8341 else
8342 {
8343 pDst->Rsrvd1 = 0;
8344 pDst->Rsrvd2 = 0;
8345 pDst->au32RsrvdForSoftware[0] = X86_FXSTATE_RSVD_32BIT_MAGIC;
8346 }
8347
8348 /* XMM registers. */
8349 if ( !(pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_FFXSR)
8350 || pVCpu->iem.s.enmCpuMode != IEMMODE_64BIT
8351 || pVCpu->iem.s.uCpl != 0)
8352 {
8353 uint32_t cXmmRegs = enmEffOpSize == IEMMODE_64BIT ? 16 : 8;
8354 for (uint32_t i = 0; i < cXmmRegs; i++)
8355 pDst->aXMM[i] = pSrc->aXMM[i];
8356 /** @todo Testcase: What happens to the reserved XMM registers? Untouched,
8357 * right? */
8358 }
8359
8360 /*
8361 * Commit the memory.
8362 */
8363 rcStrict = iemMemCommitAndUnmap(pVCpu, pvMem512, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
8364 if (rcStrict != VINF_SUCCESS)
8365 return rcStrict;
8366
8367 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8368 return VINF_SUCCESS;
8369}
8370
8371
8372/**
8373 * Implements 'FXRSTOR'.
8374 *
8375 * @param GCPtrEff The address of the image.
8376 * @param enmEffOpSize The operand size (only REX.W really matters).
8377 */
8378IEM_CIMPL_DEF_3(iemCImpl_fxrstor, uint8_t, iEffSeg, RTGCPTR, GCPtrEff, IEMMODE, enmEffOpSize)
8379{
8380 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX);
8381
8382 /*
8383 * Raise exceptions.
8384 */
8385 if (pVCpu->cpum.GstCtx.cr0 & X86_CR0_EM)
8386 return iemRaiseUndefinedOpcode(pVCpu);
8387 if (pVCpu->cpum.GstCtx.cr0 & (X86_CR0_TS | X86_CR0_EM))
8388 return iemRaiseDeviceNotAvailable(pVCpu);
8389 if (GCPtrEff & 15)
8390 {
8391 /** @todo CPU/VM detection possible! \#AC might not be signal for
8392 * all/any misalignment sizes, intel says its an implementation detail. */
8393 if ( (pVCpu->cpum.GstCtx.cr0 & X86_CR0_AM)
8394 && pVCpu->cpum.GstCtx.eflags.Bits.u1AC
8395 && pVCpu->iem.s.uCpl == 3)
8396 return iemRaiseAlignmentCheckException(pVCpu);
8397 return iemRaiseGeneralProtectionFault0(pVCpu);
8398 }
8399
8400 /*
8401 * Access the memory.
8402 */
8403 void *pvMem512;
8404 VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, &pvMem512, 512, iEffSeg, GCPtrEff, IEM_ACCESS_DATA_R);
8405 if (rcStrict != VINF_SUCCESS)
8406 return rcStrict;
8407 PCX86FXSTATE pSrc = (PCX86FXSTATE)pvMem512;
8408 PX86FXSTATE pDst = &pVCpu->cpum.GstCtx.XState.x87;
8409
8410 /*
8411 * Check the state for stuff which will #GP(0).
8412 */
8413 uint32_t const fMXCSR = pSrc->MXCSR;
8414 uint32_t const fMXCSR_MASK = CPUMGetGuestMxCsrMask(pVCpu->CTX_SUFF(pVM));
8415 if (fMXCSR & ~fMXCSR_MASK)
8416 {
8417 Log(("fxrstor: MXCSR=%#x (MXCSR_MASK=%#x) -> #GP(0)\n", fMXCSR, fMXCSR_MASK));
8418 return iemRaiseGeneralProtectionFault0(pVCpu);
8419 }
8420
8421 /*
8422 * Load the registers.
8423 */
8424 /** @todo CPU/VM detection possible! If CR4.OSFXSR=0 MXCSR it's
8425 * implementation specific whether MXCSR and XMM0-XMM7 are restored. */
8426
8427 /* common for all formats */
8428 pDst->FCW = pSrc->FCW;
8429 pDst->FSW = pSrc->FSW;
8430 pDst->FTW = pSrc->FTW & UINT16_C(0xff);
8431 pDst->FOP = pSrc->FOP;
8432 pDst->MXCSR = fMXCSR;
8433 /* (MXCSR_MASK is read-only) */
8434 for (uint32_t i = 0; i < RT_ELEMENTS(pSrc->aRegs); i++)
8435 {
8436 pDst->aRegs[i].au32[0] = pSrc->aRegs[i].au32[0];
8437 pDst->aRegs[i].au32[1] = pSrc->aRegs[i].au32[1];
8438 pDst->aRegs[i].au32[2] = pSrc->aRegs[i].au32[2] & UINT32_C(0xffff);
8439 pDst->aRegs[i].au32[3] = 0;
8440 }
8441
8442 /* FPU IP, CS, DP and DS. */
8443 if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
8444 {
8445 pDst->FPUIP = pSrc->FPUIP;
8446 pDst->CS = pSrc->CS;
8447 pDst->Rsrvd1 = pSrc->Rsrvd1;
8448 pDst->FPUDP = pSrc->FPUDP;
8449 pDst->DS = pSrc->DS;
8450 pDst->Rsrvd2 = pSrc->Rsrvd2;
8451 }
8452 else
8453 {
8454 pDst->FPUIP = pSrc->FPUIP;
8455 pDst->CS = pSrc->CS;
8456 pDst->Rsrvd1 = 0;
8457 pDst->FPUDP = pSrc->FPUDP;
8458 pDst->DS = pSrc->DS;
8459 pDst->Rsrvd2 = 0;
8460 }
8461
8462 /* XMM registers. */
8463 if ( !(pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_FFXSR)
8464 || pVCpu->iem.s.enmCpuMode != IEMMODE_64BIT
8465 || pVCpu->iem.s.uCpl != 0)
8466 {
8467 uint32_t cXmmRegs = enmEffOpSize == IEMMODE_64BIT ? 16 : 8;
8468 for (uint32_t i = 0; i < cXmmRegs; i++)
8469 pDst->aXMM[i] = pSrc->aXMM[i];
8470 }
8471
8472 /*
8473 * Commit the memory.
8474 */
8475 rcStrict = iemMemCommitAndUnmap(pVCpu, pvMem512, IEM_ACCESS_DATA_R);
8476 if (rcStrict != VINF_SUCCESS)
8477 return rcStrict;
8478
8479 iemHlpUsedFpu(pVCpu);
8480 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8481 return VINF_SUCCESS;
8482}
8483
8484
8485/**
8486 * Implements 'XSAVE'.
8487 *
8488 * @param iEffSeg The effective segment.
8489 * @param GCPtrEff The address of the image.
8490 * @param enmEffOpSize The operand size (only REX.W really matters).
8491 */
8492IEM_CIMPL_DEF_3(iemCImpl_xsave, uint8_t, iEffSeg, RTGCPTR, GCPtrEff, IEMMODE, enmEffOpSize)
8493{
8494 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX | CPUMCTX_EXTRN_OTHER_XSAVE | CPUMCTX_EXTRN_XCRx);
8495
8496 /*
8497 * Raise exceptions.
8498 */
8499 if (!(pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSXSAVE))
8500 return iemRaiseUndefinedOpcode(pVCpu);
8501 /* When in VMX non-root mode and XSAVE/XRSTOR is not enabled, it results in #UD. */
8502 if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
8503 && !IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_XSAVES_XRSTORS))
8504 {
8505 Log(("xrstor: Not enabled for nested-guest execution -> #UD\n"));
8506 return iemRaiseUndefinedOpcode(pVCpu);
8507 }
8508 if (pVCpu->cpum.GstCtx.cr0 & X86_CR0_TS)
8509 return iemRaiseDeviceNotAvailable(pVCpu);
8510 if (GCPtrEff & 63)
8511 {
8512 /** @todo CPU/VM detection possible! \#AC might not be signal for
8513 * all/any misalignment sizes, intel says its an implementation detail. */
8514 if ( (pVCpu->cpum.GstCtx.cr0 & X86_CR0_AM)
8515 && pVCpu->cpum.GstCtx.eflags.Bits.u1AC
8516 && pVCpu->iem.s.uCpl == 3)
8517 return iemRaiseAlignmentCheckException(pVCpu);
8518 return iemRaiseGeneralProtectionFault0(pVCpu);
8519 }
8520
8521 /*
8522 * Calc the requested mask.
8523 */
8524 uint64_t const fReqComponents = RT_MAKE_U64(pVCpu->cpum.GstCtx.eax, pVCpu->cpum.GstCtx.edx) & pVCpu->cpum.GstCtx.aXcr[0];
8525 AssertLogRelReturn(!(fReqComponents & ~(XSAVE_C_X87 | XSAVE_C_SSE | XSAVE_C_YMM)), VERR_IEM_ASPECT_NOT_IMPLEMENTED);
8526 uint64_t const fXInUse = pVCpu->cpum.GstCtx.aXcr[0];
8527
8528/** @todo figure out the exact protocol for the memory access. Currently we
8529 * just need this crap to work halfways to make it possible to test
8530 * AVX instructions. */
8531/** @todo figure out the XINUSE and XMODIFIED */
8532
8533 /*
8534 * Access the x87 memory state.
8535 */
8536 /* The x87+SSE state. */
8537 void *pvMem512;
8538 VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, &pvMem512, 512, iEffSeg, GCPtrEff, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
8539 if (rcStrict != VINF_SUCCESS)
8540 return rcStrict;
8541 PX86FXSTATE pDst = (PX86FXSTATE)pvMem512;
8542 PCX86FXSTATE pSrc = &pVCpu->cpum.GstCtx.XState.x87;
8543
8544 /* The header. */
8545 PX86XSAVEHDR pHdr;
8546 rcStrict = iemMemMap(pVCpu, (void **)&pHdr, sizeof(&pHdr), iEffSeg, GCPtrEff + 512, IEM_ACCESS_DATA_RW);
8547 if (rcStrict != VINF_SUCCESS)
8548 return rcStrict;
8549
8550 /*
8551 * Store the X87 state.
8552 */
8553 if (fReqComponents & XSAVE_C_X87)
8554 {
8555 /* common for all formats */
8556 pDst->FCW = pSrc->FCW;
8557 pDst->FSW = pSrc->FSW;
8558 pDst->FTW = pSrc->FTW & UINT16_C(0xff);
8559 pDst->FOP = pSrc->FOP;
8560 pDst->FPUIP = pSrc->FPUIP;
8561 pDst->CS = pSrc->CS;
8562 pDst->FPUDP = pSrc->FPUDP;
8563 pDst->DS = pSrc->DS;
8564 if (enmEffOpSize == IEMMODE_64BIT)
8565 {
8566 /* Save upper 16-bits of FPUIP (IP:CS:Rsvd1) and FPUDP (DP:DS:Rsvd2). */
8567 pDst->Rsrvd1 = pSrc->Rsrvd1;
8568 pDst->Rsrvd2 = pSrc->Rsrvd2;
8569 pDst->au32RsrvdForSoftware[0] = 0;
8570 }
8571 else
8572 {
8573 pDst->Rsrvd1 = 0;
8574 pDst->Rsrvd2 = 0;
8575 pDst->au32RsrvdForSoftware[0] = X86_FXSTATE_RSVD_32BIT_MAGIC;
8576 }
8577 for (uint32_t i = 0; i < RT_ELEMENTS(pDst->aRegs); i++)
8578 {
8579 /** @todo Testcase: What actually happens to the 6 reserved bytes? I'm clearing
8580 * them for now... */
8581 pDst->aRegs[i].au32[0] = pSrc->aRegs[i].au32[0];
8582 pDst->aRegs[i].au32[1] = pSrc->aRegs[i].au32[1];
8583 pDst->aRegs[i].au32[2] = pSrc->aRegs[i].au32[2] & UINT32_C(0xffff);
8584 pDst->aRegs[i].au32[3] = 0;
8585 }
8586
8587 }
8588
8589 if (fReqComponents & (XSAVE_C_SSE | XSAVE_C_YMM))
8590 {
8591 pDst->MXCSR = pSrc->MXCSR;
8592 pDst->MXCSR_MASK = CPUMGetGuestMxCsrMask(pVCpu->CTX_SUFF(pVM));
8593 }
8594
8595 if (fReqComponents & XSAVE_C_SSE)
8596 {
8597 /* XMM registers. */
8598 uint32_t cXmmRegs = enmEffOpSize == IEMMODE_64BIT ? 16 : 8;
8599 for (uint32_t i = 0; i < cXmmRegs; i++)
8600 pDst->aXMM[i] = pSrc->aXMM[i];
8601 /** @todo Testcase: What happens to the reserved XMM registers? Untouched,
8602 * right? */
8603 }
8604
8605 /* Commit the x87 state bits. (probably wrong) */
8606 rcStrict = iemMemCommitAndUnmap(pVCpu, pvMem512, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
8607 if (rcStrict != VINF_SUCCESS)
8608 return rcStrict;
8609
8610 /*
8611 * Store AVX state.
8612 */
8613 if (fReqComponents & XSAVE_C_YMM)
8614 {
8615 /** @todo testcase: xsave64 vs xsave32 wrt XSAVE_C_YMM. */
8616 AssertLogRelReturn(pVCpu->cpum.GstCtx.aoffXState[XSAVE_C_YMM_BIT] != UINT16_MAX, VERR_IEM_IPE_9);
8617 PCX86XSAVEYMMHI pCompSrc = CPUMCTX_XSAVE_C_PTR(IEM_GET_CTX(pVCpu), XSAVE_C_YMM_BIT, PCX86XSAVEYMMHI);
8618 PX86XSAVEYMMHI pCompDst;
8619 rcStrict = iemMemMap(pVCpu, (void **)&pCompDst, sizeof(*pCompDst), iEffSeg, GCPtrEff + pVCpu->cpum.GstCtx.aoffXState[XSAVE_C_YMM_BIT],
8620 IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
8621 if (rcStrict != VINF_SUCCESS)
8622 return rcStrict;
8623
8624 uint32_t cXmmRegs = enmEffOpSize == IEMMODE_64BIT ? 16 : 8;
8625 for (uint32_t i = 0; i < cXmmRegs; i++)
8626 pCompDst->aYmmHi[i] = pCompSrc->aYmmHi[i];
8627
8628 rcStrict = iemMemCommitAndUnmap(pVCpu, pCompDst, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
8629 if (rcStrict != VINF_SUCCESS)
8630 return rcStrict;
8631 }
8632
8633 /*
8634 * Update the header.
8635 */
8636 pHdr->bmXState = (pHdr->bmXState & ~fReqComponents)
8637 | (fReqComponents & fXInUse);
8638
8639 rcStrict = iemMemCommitAndUnmap(pVCpu, pHdr, IEM_ACCESS_DATA_RW);
8640 if (rcStrict != VINF_SUCCESS)
8641 return rcStrict;
8642
8643 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8644 return VINF_SUCCESS;
8645}
8646
8647
8648/**
8649 * Implements 'XRSTOR'.
8650 *
8651 * @param iEffSeg The effective segment.
8652 * @param GCPtrEff The address of the image.
8653 * @param enmEffOpSize The operand size (only REX.W really matters).
8654 */
8655IEM_CIMPL_DEF_3(iemCImpl_xrstor, uint8_t, iEffSeg, RTGCPTR, GCPtrEff, IEMMODE, enmEffOpSize)
8656{
8657 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX | CPUMCTX_EXTRN_OTHER_XSAVE | CPUMCTX_EXTRN_XCRx);
8658
8659 /*
8660 * Raise exceptions.
8661 */
8662 if (!(pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSXSAVE))
8663 return iemRaiseUndefinedOpcode(pVCpu);
8664 /* When in VMX non-root mode and XSAVE/XRSTOR is not enabled, it results in #UD. */
8665 if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
8666 && !IEM_VMX_IS_PROCCTLS2_SET(pVCpu, VMX_PROC_CTLS2_XSAVES_XRSTORS))
8667 {
8668 Log(("xrstor: Not enabled for nested-guest execution -> #UD\n"));
8669 return iemRaiseUndefinedOpcode(pVCpu);
8670 }
8671 if (pVCpu->cpum.GstCtx.cr0 & X86_CR0_TS)
8672 return iemRaiseDeviceNotAvailable(pVCpu);
8673 if (GCPtrEff & 63)
8674 {
8675 /** @todo CPU/VM detection possible! \#AC might not be signal for
8676 * all/any misalignment sizes, intel says its an implementation detail. */
8677 if ( (pVCpu->cpum.GstCtx.cr0 & X86_CR0_AM)
8678 && pVCpu->cpum.GstCtx.eflags.Bits.u1AC
8679 && pVCpu->iem.s.uCpl == 3)
8680 return iemRaiseAlignmentCheckException(pVCpu);
8681 return iemRaiseGeneralProtectionFault0(pVCpu);
8682 }
8683
8684/** @todo figure out the exact protocol for the memory access. Currently we
8685 * just need this crap to work halfways to make it possible to test
8686 * AVX instructions. */
8687/** @todo figure out the XINUSE and XMODIFIED */
8688
8689 /*
8690 * Access the x87 memory state.
8691 */
8692 /* The x87+SSE state. */
8693 void *pvMem512;
8694 VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, &pvMem512, 512, iEffSeg, GCPtrEff, IEM_ACCESS_DATA_R);
8695 if (rcStrict != VINF_SUCCESS)
8696 return rcStrict;
8697 PCX86FXSTATE pSrc = (PCX86FXSTATE)pvMem512;
8698 PX86FXSTATE pDst = &pVCpu->cpum.GstCtx.XState.x87;
8699
8700 /*
8701 * Calc the requested mask
8702 */
8703 PX86XSAVEHDR pHdrDst = &pVCpu->cpum.GstCtx.XState.Hdr;
8704 PCX86XSAVEHDR pHdrSrc;
8705 rcStrict = iemMemMap(pVCpu, (void **)&pHdrSrc, sizeof(&pHdrSrc), iEffSeg, GCPtrEff + 512, IEM_ACCESS_DATA_R);
8706 if (rcStrict != VINF_SUCCESS)
8707 return rcStrict;
8708
8709 uint64_t const fReqComponents = RT_MAKE_U64(pVCpu->cpum.GstCtx.eax, pVCpu->cpum.GstCtx.edx) & pVCpu->cpum.GstCtx.aXcr[0];
8710 AssertLogRelReturn(!(fReqComponents & ~(XSAVE_C_X87 | XSAVE_C_SSE | XSAVE_C_YMM)), VERR_IEM_ASPECT_NOT_IMPLEMENTED);
8711 //uint64_t const fXInUse = pVCpu->cpum.GstCtx.aXcr[0];
8712 uint64_t const fRstorMask = pHdrSrc->bmXState;
8713 uint64_t const fCompMask = pHdrSrc->bmXComp;
8714
8715 AssertLogRelReturn(!(fCompMask & XSAVE_C_X), VERR_IEM_ASPECT_NOT_IMPLEMENTED);
8716
8717 uint32_t const cXmmRegs = enmEffOpSize == IEMMODE_64BIT ? 16 : 8;
8718
8719 /* We won't need this any longer. */
8720 rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)pHdrSrc, IEM_ACCESS_DATA_R);
8721 if (rcStrict != VINF_SUCCESS)
8722 return rcStrict;
8723
8724 /*
8725 * Store the X87 state.
8726 */
8727 if (fReqComponents & XSAVE_C_X87)
8728 {
8729 if (fRstorMask & XSAVE_C_X87)
8730 {
8731 pDst->FCW = pSrc->FCW;
8732 pDst->FSW = pSrc->FSW;
8733 pDst->FTW = pSrc->FTW & UINT16_C(0xff);
8734 pDst->FOP = pSrc->FOP;
8735 pDst->FPUIP = pSrc->FPUIP;
8736 pDst->CS = pSrc->CS;
8737 pDst->FPUDP = pSrc->FPUDP;
8738 pDst->DS = pSrc->DS;
8739 if (enmEffOpSize == IEMMODE_64BIT)
8740 {
8741 /* Save upper 16-bits of FPUIP (IP:CS:Rsvd1) and FPUDP (DP:DS:Rsvd2). */
8742 pDst->Rsrvd1 = pSrc->Rsrvd1;
8743 pDst->Rsrvd2 = pSrc->Rsrvd2;
8744 }
8745 else
8746 {
8747 pDst->Rsrvd1 = 0;
8748 pDst->Rsrvd2 = 0;
8749 }
8750 for (uint32_t i = 0; i < RT_ELEMENTS(pDst->aRegs); i++)
8751 {
8752 pDst->aRegs[i].au32[0] = pSrc->aRegs[i].au32[0];
8753 pDst->aRegs[i].au32[1] = pSrc->aRegs[i].au32[1];
8754 pDst->aRegs[i].au32[2] = pSrc->aRegs[i].au32[2] & UINT32_C(0xffff);
8755 pDst->aRegs[i].au32[3] = 0;
8756 }
8757 }
8758 else
8759 {
8760 pDst->FCW = 0x37f;
8761 pDst->FSW = 0;
8762 pDst->FTW = 0x00; /* 0 - empty. */
8763 pDst->FPUDP = 0;
8764 pDst->DS = 0; //??
8765 pDst->Rsrvd2= 0;
8766 pDst->FPUIP = 0;
8767 pDst->CS = 0; //??
8768 pDst->Rsrvd1= 0;
8769 pDst->FOP = 0;
8770 for (uint32_t i = 0; i < RT_ELEMENTS(pSrc->aRegs); i++)
8771 {
8772 pDst->aRegs[i].au32[0] = 0;
8773 pDst->aRegs[i].au32[1] = 0;
8774 pDst->aRegs[i].au32[2] = 0;
8775 pDst->aRegs[i].au32[3] = 0;
8776 }
8777 }
8778 pHdrDst->bmXState |= XSAVE_C_X87; /* playing safe for now */
8779 }
8780
8781 /* MXCSR */
8782 if (fReqComponents & (XSAVE_C_SSE | XSAVE_C_YMM))
8783 {
8784 if (fRstorMask & (XSAVE_C_SSE | XSAVE_C_YMM))
8785 pDst->MXCSR = pSrc->MXCSR;
8786 else
8787 pDst->MXCSR = 0x1f80;
8788 }
8789
8790 /* XMM registers. */
8791 if (fReqComponents & XSAVE_C_SSE)
8792 {
8793 if (fRstorMask & XSAVE_C_SSE)
8794 {
8795 for (uint32_t i = 0; i < cXmmRegs; i++)
8796 pDst->aXMM[i] = pSrc->aXMM[i];
8797 /** @todo Testcase: What happens to the reserved XMM registers? Untouched,
8798 * right? */
8799 }
8800 else
8801 {
8802 for (uint32_t i = 0; i < cXmmRegs; i++)
8803 {
8804 pDst->aXMM[i].au64[0] = 0;
8805 pDst->aXMM[i].au64[1] = 0;
8806 }
8807 }
8808 pHdrDst->bmXState |= XSAVE_C_SSE; /* playing safe for now */
8809 }
8810
8811 /* Unmap the x87 state bits (so we've don't run out of mapping). */
8812 rcStrict = iemMemCommitAndUnmap(pVCpu, pvMem512, IEM_ACCESS_DATA_R);
8813 if (rcStrict != VINF_SUCCESS)
8814 return rcStrict;
8815
8816 /*
8817 * Restore AVX state.
8818 */
8819 if (fReqComponents & XSAVE_C_YMM)
8820 {
8821 AssertLogRelReturn(pVCpu->cpum.GstCtx.aoffXState[XSAVE_C_YMM_BIT] != UINT16_MAX, VERR_IEM_IPE_9);
8822 PX86XSAVEYMMHI pCompDst = CPUMCTX_XSAVE_C_PTR(IEM_GET_CTX(pVCpu), XSAVE_C_YMM_BIT, PX86XSAVEYMMHI);
8823
8824 if (fRstorMask & XSAVE_C_YMM)
8825 {
8826 /** @todo testcase: xsave64 vs xsave32 wrt XSAVE_C_YMM. */
8827 PCX86XSAVEYMMHI pCompSrc;
8828 rcStrict = iemMemMap(pVCpu, (void **)&pCompSrc, sizeof(*pCompDst),
8829 iEffSeg, GCPtrEff + pVCpu->cpum.GstCtx.aoffXState[XSAVE_C_YMM_BIT], IEM_ACCESS_DATA_R);
8830 if (rcStrict != VINF_SUCCESS)
8831 return rcStrict;
8832
8833 for (uint32_t i = 0; i < cXmmRegs; i++)
8834 {
8835 pCompDst->aYmmHi[i].au64[0] = pCompSrc->aYmmHi[i].au64[0];
8836 pCompDst->aYmmHi[i].au64[1] = pCompSrc->aYmmHi[i].au64[1];
8837 }
8838
8839 rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)pCompSrc, IEM_ACCESS_DATA_R);
8840 if (rcStrict != VINF_SUCCESS)
8841 return rcStrict;
8842 }
8843 else
8844 {
8845 for (uint32_t i = 0; i < cXmmRegs; i++)
8846 {
8847 pCompDst->aYmmHi[i].au64[0] = 0;
8848 pCompDst->aYmmHi[i].au64[1] = 0;
8849 }
8850 }
8851 pHdrDst->bmXState |= XSAVE_C_YMM; /* playing safe for now */
8852 }
8853
8854 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8855 return VINF_SUCCESS;
8856}
8857
8858
8859
8860
8861/**
8862 * Implements 'STMXCSR'.
8863 *
8864 * @param GCPtrEff The address of the image.
8865 */
8866IEM_CIMPL_DEF_2(iemCImpl_stmxcsr, uint8_t, iEffSeg, RTGCPTR, GCPtrEff)
8867{
8868 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX);
8869
8870 /*
8871 * Raise exceptions.
8872 */
8873 if ( !(pVCpu->cpum.GstCtx.cr0 & X86_CR0_EM)
8874 && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSFXSR))
8875 {
8876 if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_TS))
8877 {
8878 /*
8879 * Do the job.
8880 */
8881 VBOXSTRICTRC rcStrict = iemMemStoreDataU32(pVCpu, iEffSeg, GCPtrEff, pVCpu->cpum.GstCtx.XState.x87.MXCSR);
8882 if (rcStrict == VINF_SUCCESS)
8883 {
8884 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8885 return VINF_SUCCESS;
8886 }
8887 return rcStrict;
8888 }
8889 return iemRaiseDeviceNotAvailable(pVCpu);
8890 }
8891 return iemRaiseUndefinedOpcode(pVCpu);
8892}
8893
8894
8895/**
8896 * Implements 'VSTMXCSR'.
8897 *
8898 * @param GCPtrEff The address of the image.
8899 */
8900IEM_CIMPL_DEF_2(iemCImpl_vstmxcsr, uint8_t, iEffSeg, RTGCPTR, GCPtrEff)
8901{
8902 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX | CPUMCTX_EXTRN_XCRx);
8903
8904 /*
8905 * Raise exceptions.
8906 */
8907 if ( ( !IEM_IS_GUEST_CPU_AMD(pVCpu)
8908 ? (pVCpu->cpum.GstCtx.aXcr[0] & (XSAVE_C_SSE | XSAVE_C_YMM)) == (XSAVE_C_SSE | XSAVE_C_YMM)
8909 : !(pVCpu->cpum.GstCtx.cr0 & X86_CR0_EM)) /* AMD Jaguar CPU (f0x16,m0,s1) behaviour */
8910 && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSXSAVE))
8911 {
8912 if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_TS))
8913 {
8914 /*
8915 * Do the job.
8916 */
8917 VBOXSTRICTRC rcStrict = iemMemStoreDataU32(pVCpu, iEffSeg, GCPtrEff, pVCpu->cpum.GstCtx.XState.x87.MXCSR);
8918 if (rcStrict == VINF_SUCCESS)
8919 {
8920 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8921 return VINF_SUCCESS;
8922 }
8923 return rcStrict;
8924 }
8925 return iemRaiseDeviceNotAvailable(pVCpu);
8926 }
8927 return iemRaiseUndefinedOpcode(pVCpu);
8928}
8929
8930
8931/**
8932 * Implements 'LDMXCSR'.
8933 *
8934 * @param GCPtrEff The address of the image.
8935 */
8936IEM_CIMPL_DEF_2(iemCImpl_ldmxcsr, uint8_t, iEffSeg, RTGCPTR, GCPtrEff)
8937{
8938 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX);
8939
8940 /*
8941 * Raise exceptions.
8942 */
8943 /** @todo testcase - order of LDMXCSR faults. Does \#PF, \#GP and \#SS
8944 * happen after or before \#UD and \#EM? */
8945 if ( !(pVCpu->cpum.GstCtx.cr0 & X86_CR0_EM)
8946 && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_OSFXSR))
8947 {
8948 if (!(pVCpu->cpum.GstCtx.cr0 & X86_CR0_TS))
8949 {
8950 /*
8951 * Do the job.
8952 */
8953 uint32_t fNewMxCsr;
8954 VBOXSTRICTRC rcStrict = iemMemFetchDataU32(pVCpu, &fNewMxCsr, iEffSeg, GCPtrEff);
8955 if (rcStrict == VINF_SUCCESS)
8956 {
8957 uint32_t const fMxCsrMask = CPUMGetGuestMxCsrMask(pVCpu->CTX_SUFF(pVM));
8958 if (!(fNewMxCsr & ~fMxCsrMask))
8959 {
8960 pVCpu->cpum.GstCtx.XState.x87.MXCSR = fNewMxCsr;
8961 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8962 return VINF_SUCCESS;
8963 }
8964 Log(("lddmxcsr: New MXCSR=%#RX32 & ~MASK=%#RX32 = %#RX32 -> #GP(0)\n",
8965 fNewMxCsr, fMxCsrMask, fNewMxCsr & ~fMxCsrMask));
8966 return iemRaiseGeneralProtectionFault0(pVCpu);
8967 }
8968 return rcStrict;
8969 }
8970 return iemRaiseDeviceNotAvailable(pVCpu);
8971 }
8972 return iemRaiseUndefinedOpcode(pVCpu);
8973}
8974
8975
8976/**
8977 * Commmon routine for fnstenv and fnsave.
8978 *
8979 * @param pVCpu The cross context virtual CPU structure of the calling thread.
8980 * @param enmEffOpSize The effective operand size.
8981 * @param uPtr Where to store the state.
8982 */
8983static void iemCImplCommonFpuStoreEnv(PVMCPUCC pVCpu, IEMMODE enmEffOpSize, RTPTRUNION uPtr)
8984{
8985 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87);
8986 PCX86FXSTATE pSrcX87 = &pVCpu->cpum.GstCtx.XState.x87;
8987 if (enmEffOpSize == IEMMODE_16BIT)
8988 {
8989 uPtr.pu16[0] = pSrcX87->FCW;
8990 uPtr.pu16[1] = pSrcX87->FSW;
8991 uPtr.pu16[2] = iemFpuCalcFullFtw(pSrcX87);
8992 if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
8993 {
8994 /** @todo Testcase: How does this work when the FPUIP/CS was saved in
8995 * protected mode or long mode and we save it in real mode? And vice
8996 * versa? And with 32-bit operand size? I think CPU is storing the
8997 * effective address ((CS << 4) + IP) in the offset register and not
8998 * doing any address calculations here. */
8999 uPtr.pu16[3] = (uint16_t)pSrcX87->FPUIP;
9000 uPtr.pu16[4] = ((pSrcX87->FPUIP >> 4) & UINT16_C(0xf000)) | pSrcX87->FOP;
9001 uPtr.pu16[5] = (uint16_t)pSrcX87->FPUDP;
9002 uPtr.pu16[6] = (pSrcX87->FPUDP >> 4) & UINT16_C(0xf000);
9003 }
9004 else
9005 {
9006 uPtr.pu16[3] = pSrcX87->FPUIP;
9007 uPtr.pu16[4] = pSrcX87->CS;
9008 uPtr.pu16[5] = pSrcX87->FPUDP;
9009 uPtr.pu16[6] = pSrcX87->DS;
9010 }
9011 }
9012 else
9013 {
9014 /** @todo Testcase: what is stored in the "gray" areas? (figure 8-9 and 8-10) */
9015 uPtr.pu16[0*2] = pSrcX87->FCW;
9016 uPtr.pu16[0*2+1] = 0xffff; /* (0xffff observed on intel skylake.) */
9017 uPtr.pu16[1*2] = pSrcX87->FSW;
9018 uPtr.pu16[1*2+1] = 0xffff;
9019 uPtr.pu16[2*2] = iemFpuCalcFullFtw(pSrcX87);
9020 uPtr.pu16[2*2+1] = 0xffff;
9021 if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
9022 {
9023 uPtr.pu16[3*2] = (uint16_t)pSrcX87->FPUIP;
9024 uPtr.pu32[4] = ((pSrcX87->FPUIP & UINT32_C(0xffff0000)) >> 4) | pSrcX87->FOP;
9025 uPtr.pu16[5*2] = (uint16_t)pSrcX87->FPUDP;
9026 uPtr.pu32[6] = (pSrcX87->FPUDP & UINT32_C(0xffff0000)) >> 4;
9027 }
9028 else
9029 {
9030 uPtr.pu32[3] = pSrcX87->FPUIP;
9031 uPtr.pu16[4*2] = pSrcX87->CS;
9032 uPtr.pu16[4*2+1] = pSrcX87->FOP;
9033 uPtr.pu32[5] = pSrcX87->FPUDP;
9034 uPtr.pu16[6*2] = pSrcX87->DS;
9035 uPtr.pu16[6*2+1] = 0xffff;
9036 }
9037 }
9038}
9039
9040
9041/**
9042 * Commmon routine for fldenv and frstor
9043 *
9044 * @param pVCpu The cross context virtual CPU structure of the calling thread.
9045 * @param enmEffOpSize The effective operand size.
9046 * @param uPtr Where to store the state.
9047 */
9048static void iemCImplCommonFpuRestoreEnv(PVMCPUCC pVCpu, IEMMODE enmEffOpSize, RTCPTRUNION uPtr)
9049{
9050 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87);
9051 PX86FXSTATE pDstX87 = &pVCpu->cpum.GstCtx.XState.x87;
9052 if (enmEffOpSize == IEMMODE_16BIT)
9053 {
9054 pDstX87->FCW = uPtr.pu16[0];
9055 pDstX87->FSW = uPtr.pu16[1];
9056 pDstX87->FTW = uPtr.pu16[2];
9057 if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
9058 {
9059 pDstX87->FPUIP = uPtr.pu16[3] | ((uint32_t)(uPtr.pu16[4] & UINT16_C(0xf000)) << 4);
9060 pDstX87->FPUDP = uPtr.pu16[5] | ((uint32_t)(uPtr.pu16[6] & UINT16_C(0xf000)) << 4);
9061 pDstX87->FOP = uPtr.pu16[4] & UINT16_C(0x07ff);
9062 pDstX87->CS = 0;
9063 pDstX87->Rsrvd1= 0;
9064 pDstX87->DS = 0;
9065 pDstX87->Rsrvd2= 0;
9066 }
9067 else
9068 {
9069 pDstX87->FPUIP = uPtr.pu16[3];
9070 pDstX87->CS = uPtr.pu16[4];
9071 pDstX87->Rsrvd1= 0;
9072 pDstX87->FPUDP = uPtr.pu16[5];
9073 pDstX87->DS = uPtr.pu16[6];
9074 pDstX87->Rsrvd2= 0;
9075 /** @todo Testcase: Is FOP cleared when doing 16-bit protected mode fldenv? */
9076 }
9077 }
9078 else
9079 {
9080 pDstX87->FCW = uPtr.pu16[0*2];
9081 pDstX87->FSW = uPtr.pu16[1*2];
9082 pDstX87->FTW = uPtr.pu16[2*2];
9083 if (IEM_IS_REAL_OR_V86_MODE(pVCpu))
9084 {
9085 pDstX87->FPUIP = uPtr.pu16[3*2] | ((uPtr.pu32[4] & UINT32_C(0x0ffff000)) << 4);
9086 pDstX87->FOP = uPtr.pu32[4] & UINT16_C(0x07ff);
9087 pDstX87->FPUDP = uPtr.pu16[5*2] | ((uPtr.pu32[6] & UINT32_C(0x0ffff000)) << 4);
9088 pDstX87->CS = 0;
9089 pDstX87->Rsrvd1= 0;
9090 pDstX87->DS = 0;
9091 pDstX87->Rsrvd2= 0;
9092 }
9093 else
9094 {
9095 pDstX87->FPUIP = uPtr.pu32[3];
9096 pDstX87->CS = uPtr.pu16[4*2];
9097 pDstX87->Rsrvd1= 0;
9098 pDstX87->FOP = uPtr.pu16[4*2+1];
9099 pDstX87->FPUDP = uPtr.pu32[5];
9100 pDstX87->DS = uPtr.pu16[6*2];
9101 pDstX87->Rsrvd2= 0;
9102 }
9103 }
9104
9105 /* Make adjustments. */
9106 pDstX87->FTW = iemFpuCompressFtw(pDstX87->FTW);
9107 pDstX87->FCW &= ~X86_FCW_ZERO_MASK;
9108 iemFpuRecalcExceptionStatus(pDstX87);
9109 /** @todo Testcase: Check if ES and/or B are automatically cleared if no
9110 * exceptions are pending after loading the saved state? */
9111}
9112
9113
9114/**
9115 * Implements 'FNSTENV'.
9116 *
9117 * @param enmEffOpSize The operand size (only REX.W really matters).
9118 * @param iEffSeg The effective segment register for @a GCPtrEff.
9119 * @param GCPtrEffDst The address of the image.
9120 */
9121IEM_CIMPL_DEF_3(iemCImpl_fnstenv, IEMMODE, enmEffOpSize, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
9122{
9123 RTPTRUNION uPtr;
9124 VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, &uPtr.pv, enmEffOpSize == IEMMODE_16BIT ? 14 : 28,
9125 iEffSeg, GCPtrEffDst, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
9126 if (rcStrict != VINF_SUCCESS)
9127 return rcStrict;
9128
9129 iemCImplCommonFpuStoreEnv(pVCpu, enmEffOpSize, uPtr);
9130
9131 rcStrict = iemMemCommitAndUnmap(pVCpu, uPtr.pv, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
9132 if (rcStrict != VINF_SUCCESS)
9133 return rcStrict;
9134
9135 /* Note: C0, C1, C2 and C3 are documented as undefined, we leave them untouched! */
9136 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
9137 return VINF_SUCCESS;
9138}
9139
9140
9141/**
9142 * Implements 'FNSAVE'.
9143 *
9144 * @param GCPtrEffDst The address of the image.
9145 * @param enmEffOpSize The operand size.
9146 */
9147IEM_CIMPL_DEF_3(iemCImpl_fnsave, IEMMODE, enmEffOpSize, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
9148{
9149 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87);
9150
9151 RTPTRUNION uPtr;
9152 VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, &uPtr.pv, enmEffOpSize == IEMMODE_16BIT ? 94 : 108,
9153 iEffSeg, GCPtrEffDst, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
9154 if (rcStrict != VINF_SUCCESS)
9155 return rcStrict;
9156
9157 PX86FXSTATE pFpuCtx = &pVCpu->cpum.GstCtx.XState.x87;
9158 iemCImplCommonFpuStoreEnv(pVCpu, enmEffOpSize, uPtr);
9159 PRTFLOAT80U paRegs = (PRTFLOAT80U)(uPtr.pu8 + (enmEffOpSize == IEMMODE_16BIT ? 14 : 28));
9160 for (uint32_t i = 0; i < RT_ELEMENTS(pFpuCtx->aRegs); i++)
9161 {
9162 paRegs[i].au32[0] = pFpuCtx->aRegs[i].au32[0];
9163 paRegs[i].au32[1] = pFpuCtx->aRegs[i].au32[1];
9164 paRegs[i].au16[4] = pFpuCtx->aRegs[i].au16[4];
9165 }
9166
9167 rcStrict = iemMemCommitAndUnmap(pVCpu, uPtr.pv, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
9168 if (rcStrict != VINF_SUCCESS)
9169 return rcStrict;
9170
9171 /*
9172 * Re-initialize the FPU context.
9173 */
9174 pFpuCtx->FCW = 0x37f;
9175 pFpuCtx->FSW = 0;
9176 pFpuCtx->FTW = 0x00; /* 0 - empty */
9177 pFpuCtx->FPUDP = 0;
9178 pFpuCtx->DS = 0;
9179 pFpuCtx->Rsrvd2= 0;
9180 pFpuCtx->FPUIP = 0;
9181 pFpuCtx->CS = 0;
9182 pFpuCtx->Rsrvd1= 0;
9183 pFpuCtx->FOP = 0;
9184
9185 iemHlpUsedFpu(pVCpu);
9186 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
9187 return VINF_SUCCESS;
9188}
9189
9190
9191
9192/**
9193 * Implements 'FLDENV'.
9194 *
9195 * @param enmEffOpSize The operand size (only REX.W really matters).
9196 * @param iEffSeg The effective segment register for @a GCPtrEff.
9197 * @param GCPtrEffSrc The address of the image.
9198 */
9199IEM_CIMPL_DEF_3(iemCImpl_fldenv, IEMMODE, enmEffOpSize, uint8_t, iEffSeg, RTGCPTR, GCPtrEffSrc)
9200{
9201 RTCPTRUNION uPtr;
9202 VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, (void **)&uPtr.pv, enmEffOpSize == IEMMODE_16BIT ? 14 : 28,
9203 iEffSeg, GCPtrEffSrc, IEM_ACCESS_DATA_R);
9204 if (rcStrict != VINF_SUCCESS)
9205 return rcStrict;
9206
9207 iemCImplCommonFpuRestoreEnv(pVCpu, enmEffOpSize, uPtr);
9208
9209 rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)uPtr.pv, IEM_ACCESS_DATA_R);
9210 if (rcStrict != VINF_SUCCESS)
9211 return rcStrict;
9212
9213 iemHlpUsedFpu(pVCpu);
9214 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
9215 return VINF_SUCCESS;
9216}
9217
9218
9219/**
9220 * Implements 'FRSTOR'.
9221 *
9222 * @param GCPtrEffSrc The address of the image.
9223 * @param enmEffOpSize The operand size.
9224 */
9225IEM_CIMPL_DEF_3(iemCImpl_frstor, IEMMODE, enmEffOpSize, uint8_t, iEffSeg, RTGCPTR, GCPtrEffSrc)
9226{
9227 RTCPTRUNION uPtr;
9228 VBOXSTRICTRC rcStrict = iemMemMap(pVCpu, (void **)&uPtr.pv, enmEffOpSize == IEMMODE_16BIT ? 94 : 108,
9229 iEffSeg, GCPtrEffSrc, IEM_ACCESS_DATA_R);
9230 if (rcStrict != VINF_SUCCESS)
9231 return rcStrict;
9232
9233 PX86FXSTATE pFpuCtx = &pVCpu->cpum.GstCtx.XState.x87;
9234 iemCImplCommonFpuRestoreEnv(pVCpu, enmEffOpSize, uPtr);
9235 PCRTFLOAT80U paRegs = (PCRTFLOAT80U)(uPtr.pu8 + (enmEffOpSize == IEMMODE_16BIT ? 14 : 28));
9236 for (uint32_t i = 0; i < RT_ELEMENTS(pFpuCtx->aRegs); i++)
9237 {
9238 pFpuCtx->aRegs[i].au32[0] = paRegs[i].au32[0];
9239 pFpuCtx->aRegs[i].au32[1] = paRegs[i].au32[1];
9240 pFpuCtx->aRegs[i].au32[2] = paRegs[i].au16[4];
9241 pFpuCtx->aRegs[i].au32[3] = 0;
9242 }
9243
9244 rcStrict = iemMemCommitAndUnmap(pVCpu, (void *)uPtr.pv, IEM_ACCESS_DATA_R);
9245 if (rcStrict != VINF_SUCCESS)
9246 return rcStrict;
9247
9248 iemHlpUsedFpu(pVCpu);
9249 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
9250 return VINF_SUCCESS;
9251}
9252
9253
9254/**
9255 * Implements 'FLDCW'.
9256 *
9257 * @param u16Fcw The new FCW.
9258 */
9259IEM_CIMPL_DEF_1(iemCImpl_fldcw, uint16_t, u16Fcw)
9260{
9261 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87);
9262
9263 /** @todo Testcase: Check what happens when trying to load X86_FCW_PC_RSVD. */
9264 /** @todo Testcase: Try see what happens when trying to set undefined bits
9265 * (other than 6 and 7). Currently ignoring them. */
9266 /** @todo Testcase: Test that it raises and loweres the FPU exception bits
9267 * according to FSW. (This is was is currently implemented.) */
9268 PX86FXSTATE pFpuCtx = &pVCpu->cpum.GstCtx.XState.x87;
9269 pFpuCtx->FCW = u16Fcw & ~X86_FCW_ZERO_MASK;
9270 iemFpuRecalcExceptionStatus(pFpuCtx);
9271
9272 /* Note: C0, C1, C2 and C3 are documented as undefined, we leave them untouched! */
9273 iemHlpUsedFpu(pVCpu);
9274 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
9275 return VINF_SUCCESS;
9276}
9277
9278
9279
9280/**
9281 * Implements the underflow case of fxch.
9282 *
9283 * @param iStReg The other stack register.
9284 */
9285IEM_CIMPL_DEF_1(iemCImpl_fxch_underflow, uint8_t, iStReg)
9286{
9287 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87);
9288
9289 PX86FXSTATE pFpuCtx = &pVCpu->cpum.GstCtx.XState.x87;
9290 unsigned const iReg1 = X86_FSW_TOP_GET(pFpuCtx->FSW);
9291 unsigned const iReg2 = (iReg1 + iStReg) & X86_FSW_TOP_SMASK;
9292 Assert(!(RT_BIT(iReg1) & pFpuCtx->FTW) || !(RT_BIT(iReg2) & pFpuCtx->FTW));
9293
9294 /** @todo Testcase: fxch underflow. Making assumptions that underflowed
9295 * registers are read as QNaN and then exchanged. This could be
9296 * wrong... */
9297 if (pFpuCtx->FCW & X86_FCW_IM)
9298 {
9299 if (RT_BIT(iReg1) & pFpuCtx->FTW)
9300 {
9301 if (RT_BIT(iReg2) & pFpuCtx->FTW)
9302 iemFpuStoreQNan(&pFpuCtx->aRegs[0].r80);
9303 else
9304 pFpuCtx->aRegs[0].r80 = pFpuCtx->aRegs[iStReg].r80;
9305 iemFpuStoreQNan(&pFpuCtx->aRegs[iStReg].r80);
9306 }
9307 else
9308 {
9309 pFpuCtx->aRegs[iStReg].r80 = pFpuCtx->aRegs[0].r80;
9310 iemFpuStoreQNan(&pFpuCtx->aRegs[0].r80);
9311 }
9312 pFpuCtx->FSW &= ~X86_FSW_C_MASK;
9313 pFpuCtx->FSW |= X86_FSW_C1 | X86_FSW_IE | X86_FSW_SF;
9314 }
9315 else
9316 {
9317 /* raise underflow exception, don't change anything. */
9318 pFpuCtx->FSW &= ~(X86_FSW_TOP_MASK | X86_FSW_XCPT_MASK);
9319 pFpuCtx->FSW |= X86_FSW_C1 | X86_FSW_IE | X86_FSW_SF | X86_FSW_ES | X86_FSW_B;
9320 }
9321
9322 iemFpuUpdateOpcodeAndIpWorker(pVCpu, pFpuCtx);
9323 iemHlpUsedFpu(pVCpu);
9324 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
9325 return VINF_SUCCESS;
9326}
9327
9328
9329/**
9330 * Implements 'FCOMI', 'FCOMIP', 'FUCOMI', and 'FUCOMIP'.
9331 *
9332 * @param cToAdd 1 or 7.
9333 */
9334IEM_CIMPL_DEF_3(iemCImpl_fcomi_fucomi, uint8_t, iStReg, PFNIEMAIMPLFPUR80EFL, pfnAImpl, bool, fPop)
9335{
9336 Assert(iStReg < 8);
9337 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_X87);
9338
9339 /*
9340 * Raise exceptions.
9341 */
9342 if (pVCpu->cpum.GstCtx.cr0 & (X86_CR0_EM | X86_CR0_TS))
9343 return iemRaiseDeviceNotAvailable(pVCpu);
9344
9345 PX86FXSTATE pFpuCtx = &pVCpu->cpum.GstCtx.XState.x87;
9346 uint16_t u16Fsw = pFpuCtx->FSW;
9347 if (u16Fsw & X86_FSW_ES)
9348 return iemRaiseMathFault(pVCpu);
9349
9350 /*
9351 * Check if any of the register accesses causes #SF + #IA.
9352 */
9353 unsigned const iReg1 = X86_FSW_TOP_GET(u16Fsw);
9354 unsigned const iReg2 = (iReg1 + iStReg) & X86_FSW_TOP_SMASK;
9355 if ((pFpuCtx->FTW & (RT_BIT(iReg1) | RT_BIT(iReg2))) == (RT_BIT(iReg1) | RT_BIT(iReg2)))
9356 {
9357 uint32_t u32Eflags = pfnAImpl(pFpuCtx, &u16Fsw, &pFpuCtx->aRegs[0].r80, &pFpuCtx->aRegs[iStReg].r80);
9358 NOREF(u32Eflags);
9359
9360 pFpuCtx->FSW &= ~X86_FSW_C1;
9361 pFpuCtx->FSW |= u16Fsw & ~X86_FSW_TOP_MASK;
9362 if ( !(u16Fsw & X86_FSW_IE)
9363 || (pFpuCtx->FCW & X86_FCW_IM) )
9364 {
9365 pVCpu->cpum.GstCtx.eflags.u &= ~(X86_EFL_OF | X86_EFL_SF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_PF | X86_EFL_CF);
9366 pVCpu->cpum.GstCtx.eflags.u |= pVCpu->cpum.GstCtx.eflags.u & (X86_EFL_ZF | X86_EFL_PF | X86_EFL_CF);
9367 }
9368 }
9369 else if (pFpuCtx->FCW & X86_FCW_IM)
9370 {
9371 /* Masked underflow. */
9372 pFpuCtx->FSW &= ~X86_FSW_C1;
9373 pFpuCtx->FSW |= X86_FSW_IE | X86_FSW_SF;
9374 pVCpu->cpum.GstCtx.eflags.u &= ~(X86_EFL_OF | X86_EFL_SF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_PF | X86_EFL_CF);
9375 pVCpu->cpum.GstCtx.eflags.u |= X86_EFL_ZF | X86_EFL_PF | X86_EFL_CF;
9376 }
9377 else
9378 {
9379 /* Raise underflow - don't touch EFLAGS or TOP. */
9380 pFpuCtx->FSW &= ~X86_FSW_C1;
9381 pFpuCtx->FSW |= X86_FSW_IE | X86_FSW_SF | X86_FSW_ES | X86_FSW_B;
9382 fPop = false;
9383 }
9384
9385 /*
9386 * Pop if necessary.
9387 */
9388 if (fPop)
9389 {
9390 pFpuCtx->FTW &= ~RT_BIT(iReg1);
9391 pFpuCtx->FSW &= X86_FSW_TOP_MASK;
9392 pFpuCtx->FSW |= ((iReg1 + 7) & X86_FSW_TOP_SMASK) << X86_FSW_TOP_SHIFT;
9393 }
9394
9395 iemFpuUpdateOpcodeAndIpWorker(pVCpu, pFpuCtx);
9396 iemHlpUsedFpu(pVCpu);
9397 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
9398 return VINF_SUCCESS;
9399}
9400
9401/** @} */
9402
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette