VirtualBox

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

Last change on this file since 58935 was 58935, checked in by vboxsync, 9 years ago

IEM: More iret logging.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 232.1 KB
Line 
1/* $Id: IEMAllCImpl.cpp.h 58935 2015-12-01 10:53:10Z vboxsync $ */
2/** @file
3 * IEM - Instruction Implementation in C/C++ (code include).
4 */
5
6/*
7 * Copyright (C) 2011-2015 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
19/** @name Misc Helpers
20 * @{
21 */
22
23
24/**
25 * Worker function for iemHlpCheckPortIOPermission, don't call directly.
26 *
27 * @returns Strict VBox status code.
28 *
29 * @param pIemCpu The IEM per CPU data.
30 * @param pCtx The register context.
31 * @param u16Port The port number.
32 * @param cbOperand The operand size.
33 */
34static VBOXSTRICTRC iemHlpCheckPortIOPermissionBitmap(PIEMCPU pIemCpu, PCCPUMCTX pCtx, uint16_t u16Port, uint8_t cbOperand)
35{
36 /* The TSS bits we're interested in are the same on 386 and AMD64. */
37 AssertCompile(AMD64_SEL_TYPE_SYS_TSS_BUSY == X86_SEL_TYPE_SYS_386_TSS_BUSY);
38 AssertCompile(AMD64_SEL_TYPE_SYS_TSS_AVAIL == X86_SEL_TYPE_SYS_386_TSS_AVAIL);
39 AssertCompileMembersAtSameOffset(X86TSS32, offIoBitmap, X86TSS64, offIoBitmap);
40 AssertCompile(sizeof(X86TSS32) == sizeof(X86TSS64));
41
42 /*
43 * Check the TSS type, 16-bit TSSes doesn't have any I/O permission bitmap.
44 */
45 Assert(!pCtx->tr.Attr.n.u1DescType);
46 if (RT_UNLIKELY( pCtx->tr.Attr.n.u4Type != AMD64_SEL_TYPE_SYS_TSS_BUSY
47 && pCtx->tr.Attr.n.u4Type != AMD64_SEL_TYPE_SYS_TSS_AVAIL))
48 {
49 Log(("iemHlpCheckPortIOPermissionBitmap: Port=%#x cb=%d - TSS type %#x (attr=%#x) has no I/O bitmap -> #GP(0)\n",
50 u16Port, cbOperand, pCtx->tr.Attr.n.u4Type, pCtx->tr.Attr.u));
51 return iemRaiseGeneralProtectionFault0(pIemCpu);
52 }
53
54 /*
55 * Read the bitmap offset (may #PF).
56 */
57 uint16_t offBitmap;
58 VBOXSTRICTRC rcStrict = iemMemFetchSysU16(pIemCpu, &offBitmap, UINT8_MAX,
59 pCtx->tr.u64Base + RT_OFFSETOF(X86TSS64, offIoBitmap));
60 if (rcStrict != VINF_SUCCESS)
61 {
62 Log(("iemHlpCheckPortIOPermissionBitmap: Error reading offIoBitmap (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
63 return rcStrict;
64 }
65
66 /*
67 * The bit range from u16Port to (u16Port + cbOperand - 1), however intel
68 * describes the CPU actually reading two bytes regardless of whether the
69 * bit range crosses a byte boundrary. Thus the + 1 in the test below.
70 */
71 uint32_t offFirstBit = (uint32_t)u16Port / 8 + offBitmap;
72 /** @todo check if real CPUs ensures that offBitmap has a minimum value of
73 * for instance sizeof(X86TSS32). */
74 if (offFirstBit + 1 > pCtx->tr.u32Limit) /* the limit is inclusive */
75 {
76 Log(("iemHlpCheckPortIOPermissionBitmap: offFirstBit=%#x + 1 is beyond u32Limit=%#x -> #GP(0)\n",
77 offFirstBit, pCtx->tr.u32Limit));
78 return iemRaiseGeneralProtectionFault0(pIemCpu);
79 }
80
81 /*
82 * Read the necessary bits.
83 */
84 /** @todo Test the assertion in the intel manual that the CPU reads two
85 * bytes. The question is how this works wrt to #PF and #GP on the
86 * 2nd byte when it's not required. */
87 uint16_t bmBytes = UINT16_MAX;
88 rcStrict = iemMemFetchSysU16(pIemCpu, &bmBytes, UINT8_MAX, pCtx->tr.u64Base + offFirstBit);
89 if (rcStrict != VINF_SUCCESS)
90 {
91 Log(("iemHlpCheckPortIOPermissionBitmap: Error reading I/O bitmap @%#x (%Rrc)\n", offFirstBit, VBOXSTRICTRC_VAL(rcStrict)));
92 return rcStrict;
93 }
94
95 /*
96 * Perform the check.
97 */
98 uint16_t fPortMask = (1 << cbOperand) - 1;
99 bmBytes >>= (u16Port & 7);
100 if (bmBytes & fPortMask)
101 {
102 Log(("iemHlpCheckPortIOPermissionBitmap: u16Port=%#x LB %u - access denied (bm=%#x mask=%#x) -> #GP(0)\n",
103 u16Port, cbOperand, bmBytes, fPortMask));
104 return iemRaiseGeneralProtectionFault0(pIemCpu);
105 }
106
107 return VINF_SUCCESS;
108}
109
110
111/**
112 * Checks if we are allowed to access the given I/O port, raising the
113 * appropriate exceptions if we aren't (or if the I/O bitmap is not
114 * accessible).
115 *
116 * @returns Strict VBox status code.
117 *
118 * @param pIemCpu The IEM per CPU data.
119 * @param pCtx The register context.
120 * @param u16Port The port number.
121 * @param cbOperand The operand size.
122 */
123DECLINLINE(VBOXSTRICTRC) iemHlpCheckPortIOPermission(PIEMCPU pIemCpu, PCCPUMCTX pCtx, uint16_t u16Port, uint8_t cbOperand)
124{
125 X86EFLAGS Efl;
126 Efl.u = IEMMISC_GET_EFL(pIemCpu, pCtx);
127 if ( (pCtx->cr0 & X86_CR0_PE)
128 && ( pIemCpu->uCpl > Efl.Bits.u2IOPL
129 || Efl.Bits.u1VM) )
130 return iemHlpCheckPortIOPermissionBitmap(pIemCpu, pCtx, u16Port, cbOperand);
131 return VINF_SUCCESS;
132}
133
134
135#if 0
136/**
137 * Calculates the parity bit.
138 *
139 * @returns true if the bit is set, false if not.
140 * @param u8Result The least significant byte of the result.
141 */
142static bool iemHlpCalcParityFlag(uint8_t u8Result)
143{
144 /*
145 * Parity is set if the number of bits in the least significant byte of
146 * the result is even.
147 */
148 uint8_t cBits;
149 cBits = u8Result & 1; /* 0 */
150 u8Result >>= 1;
151 cBits += u8Result & 1;
152 u8Result >>= 1;
153 cBits += u8Result & 1;
154 u8Result >>= 1;
155 cBits += u8Result & 1;
156 u8Result >>= 1;
157 cBits += u8Result & 1; /* 4 */
158 u8Result >>= 1;
159 cBits += u8Result & 1;
160 u8Result >>= 1;
161 cBits += u8Result & 1;
162 u8Result >>= 1;
163 cBits += u8Result & 1;
164 return !(cBits & 1);
165}
166#endif /* not used */
167
168
169/**
170 * Updates the specified flags according to a 8-bit result.
171 *
172 * @param pIemCpu The IEM state of the calling EMT.
173 * @param u8Result The result to set the flags according to.
174 * @param fToUpdate The flags to update.
175 * @param fUndefined The flags that are specified as undefined.
176 */
177static void iemHlpUpdateArithEFlagsU8(PIEMCPU pIemCpu, uint8_t u8Result, uint32_t fToUpdate, uint32_t fUndefined)
178{
179 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
180
181 uint32_t fEFlags = pCtx->eflags.u;
182 iemAImpl_test_u8(&u8Result, u8Result, &fEFlags);
183 pCtx->eflags.u &= ~(fToUpdate | fUndefined);
184 pCtx->eflags.u |= (fToUpdate | fUndefined) & fEFlags;
185#ifdef IEM_VERIFICATION_MODE_FULL
186 pIemCpu->fUndefinedEFlags |= fUndefined;
187#endif
188}
189
190
191/**
192 * Helper used by iret.
193 *
194 * @param uCpl The new CPL.
195 * @param pSReg Pointer to the segment register.
196 */
197static void iemHlpAdjustSelectorForNewCpl(PIEMCPU pIemCpu, uint8_t uCpl, PCPUMSELREG pSReg)
198{
199#ifdef VBOX_WITH_RAW_MODE_NOT_R0
200 if (!CPUMSELREG_ARE_HIDDEN_PARTS_VALID(IEMCPU_TO_VMCPU(pIemCpu), pSReg))
201 CPUMGuestLazyLoadHiddenSelectorReg(IEMCPU_TO_VMCPU(pIemCpu), pSReg);
202#else
203 Assert(CPUMSELREG_ARE_HIDDEN_PARTS_VALID(IEMCPU_TO_VMCPU(pIemCpu), pSReg));
204#endif
205
206 if ( uCpl > pSReg->Attr.n.u2Dpl
207 && pSReg->Attr.n.u1DescType /* code or data, not system */
208 && (pSReg->Attr.n.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
209 != (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF)) /* not conforming code */
210 iemHlpLoadNullDataSelectorProt(pIemCpu, pSReg, 0);
211}
212
213
214/**
215 * Indicates that we have modified the FPU state.
216 *
217 * @param pIemCpu The IEM state of the calling EMT.
218 */
219DECLINLINE(void) iemHlpUsedFpu(PIEMCPU pIemCpu)
220{
221 CPUMSetChangedFlags(IEMCPU_TO_VMCPU(pIemCpu), CPUM_CHANGED_FPU_REM);
222}
223
224/** @} */
225
226/** @name C Implementations
227 * @{
228 */
229
230/**
231 * Implements a 16-bit popa.
232 */
233IEM_CIMPL_DEF_0(iemCImpl_popa_16)
234{
235 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
236 RTGCPTR GCPtrStart = iemRegGetEffRsp(pIemCpu, pCtx);
237 RTGCPTR GCPtrLast = GCPtrStart + 15;
238 VBOXSTRICTRC rcStrict;
239
240 /*
241 * The docs are a bit hard to comprehend here, but it looks like we wrap
242 * around in real mode as long as none of the individual "popa" crosses the
243 * end of the stack segment. In protected mode we check the whole access
244 * in one go. For efficiency, only do the word-by-word thing if we're in
245 * danger of wrapping around.
246 */
247 /** @todo do popa boundary / wrap-around checks. */
248 if (RT_UNLIKELY( IEM_IS_REAL_OR_V86_MODE(pIemCpu)
249 && (pCtx->cs.u32Limit < GCPtrLast)) ) /* ASSUMES 64-bit RTGCPTR */
250 {
251 /* word-by-word */
252 RTUINT64U TmpRsp;
253 TmpRsp.u = pCtx->rsp;
254 rcStrict = iemMemStackPopU16Ex(pIemCpu, &pCtx->di, &TmpRsp);
255 if (rcStrict == VINF_SUCCESS)
256 rcStrict = iemMemStackPopU16Ex(pIemCpu, &pCtx->si, &TmpRsp);
257 if (rcStrict == VINF_SUCCESS)
258 rcStrict = iemMemStackPopU16Ex(pIemCpu, &pCtx->bp, &TmpRsp);
259 if (rcStrict == VINF_SUCCESS)
260 {
261 iemRegAddToRspEx(pIemCpu, pCtx, &TmpRsp, 2); /* sp */
262 rcStrict = iemMemStackPopU16Ex(pIemCpu, &pCtx->bx, &TmpRsp);
263 }
264 if (rcStrict == VINF_SUCCESS)
265 rcStrict = iemMemStackPopU16Ex(pIemCpu, &pCtx->dx, &TmpRsp);
266 if (rcStrict == VINF_SUCCESS)
267 rcStrict = iemMemStackPopU16Ex(pIemCpu, &pCtx->cx, &TmpRsp);
268 if (rcStrict == VINF_SUCCESS)
269 rcStrict = iemMemStackPopU16Ex(pIemCpu, &pCtx->ax, &TmpRsp);
270 if (rcStrict == VINF_SUCCESS)
271 {
272 pCtx->rsp = TmpRsp.u;
273 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
274 }
275 }
276 else
277 {
278 uint16_t const *pa16Mem = NULL;
279 rcStrict = iemMemMap(pIemCpu, (void **)&pa16Mem, 16, X86_SREG_SS, GCPtrStart, IEM_ACCESS_STACK_R);
280 if (rcStrict == VINF_SUCCESS)
281 {
282 pCtx->di = pa16Mem[7 - X86_GREG_xDI];
283 pCtx->si = pa16Mem[7 - X86_GREG_xSI];
284 pCtx->bp = pa16Mem[7 - X86_GREG_xBP];
285 /* skip sp */
286 pCtx->bx = pa16Mem[7 - X86_GREG_xBX];
287 pCtx->dx = pa16Mem[7 - X86_GREG_xDX];
288 pCtx->cx = pa16Mem[7 - X86_GREG_xCX];
289 pCtx->ax = pa16Mem[7 - X86_GREG_xAX];
290 rcStrict = iemMemCommitAndUnmap(pIemCpu, (void *)pa16Mem, IEM_ACCESS_STACK_R);
291 if (rcStrict == VINF_SUCCESS)
292 {
293 iemRegAddToRsp(pIemCpu, pCtx, 16);
294 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
295 }
296 }
297 }
298 return rcStrict;
299}
300
301
302/**
303 * Implements a 32-bit popa.
304 */
305IEM_CIMPL_DEF_0(iemCImpl_popa_32)
306{
307 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
308 RTGCPTR GCPtrStart = iemRegGetEffRsp(pIemCpu, pCtx);
309 RTGCPTR GCPtrLast = GCPtrStart + 31;
310 VBOXSTRICTRC rcStrict;
311
312 /*
313 * The docs are a bit hard to comprehend here, but it looks like we wrap
314 * around in real mode as long as none of the individual "popa" crosses the
315 * end of the stack segment. In protected mode we check the whole access
316 * in one go. For efficiency, only do the word-by-word thing if we're in
317 * danger of wrapping around.
318 */
319 /** @todo do popa boundary / wrap-around checks. */
320 if (RT_UNLIKELY( IEM_IS_REAL_OR_V86_MODE(pIemCpu)
321 && (pCtx->cs.u32Limit < GCPtrLast)) ) /* ASSUMES 64-bit RTGCPTR */
322 {
323 /* word-by-word */
324 RTUINT64U TmpRsp;
325 TmpRsp.u = pCtx->rsp;
326 rcStrict = iemMemStackPopU32Ex(pIemCpu, &pCtx->edi, &TmpRsp);
327 if (rcStrict == VINF_SUCCESS)
328 rcStrict = iemMemStackPopU32Ex(pIemCpu, &pCtx->esi, &TmpRsp);
329 if (rcStrict == VINF_SUCCESS)
330 rcStrict = iemMemStackPopU32Ex(pIemCpu, &pCtx->ebp, &TmpRsp);
331 if (rcStrict == VINF_SUCCESS)
332 {
333 iemRegAddToRspEx(pIemCpu, pCtx, &TmpRsp, 2); /* sp */
334 rcStrict = iemMemStackPopU32Ex(pIemCpu, &pCtx->ebx, &TmpRsp);
335 }
336 if (rcStrict == VINF_SUCCESS)
337 rcStrict = iemMemStackPopU32Ex(pIemCpu, &pCtx->edx, &TmpRsp);
338 if (rcStrict == VINF_SUCCESS)
339 rcStrict = iemMemStackPopU32Ex(pIemCpu, &pCtx->ecx, &TmpRsp);
340 if (rcStrict == VINF_SUCCESS)
341 rcStrict = iemMemStackPopU32Ex(pIemCpu, &pCtx->eax, &TmpRsp);
342 if (rcStrict == VINF_SUCCESS)
343 {
344#if 1 /** @todo what actually happens with the high bits when we're in 16-bit mode? */
345 pCtx->rdi &= UINT32_MAX;
346 pCtx->rsi &= UINT32_MAX;
347 pCtx->rbp &= UINT32_MAX;
348 pCtx->rbx &= UINT32_MAX;
349 pCtx->rdx &= UINT32_MAX;
350 pCtx->rcx &= UINT32_MAX;
351 pCtx->rax &= UINT32_MAX;
352#endif
353 pCtx->rsp = TmpRsp.u;
354 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
355 }
356 }
357 else
358 {
359 uint32_t const *pa32Mem;
360 rcStrict = iemMemMap(pIemCpu, (void **)&pa32Mem, 32, X86_SREG_SS, GCPtrStart, IEM_ACCESS_STACK_R);
361 if (rcStrict == VINF_SUCCESS)
362 {
363 pCtx->rdi = pa32Mem[7 - X86_GREG_xDI];
364 pCtx->rsi = pa32Mem[7 - X86_GREG_xSI];
365 pCtx->rbp = pa32Mem[7 - X86_GREG_xBP];
366 /* skip esp */
367 pCtx->rbx = pa32Mem[7 - X86_GREG_xBX];
368 pCtx->rdx = pa32Mem[7 - X86_GREG_xDX];
369 pCtx->rcx = pa32Mem[7 - X86_GREG_xCX];
370 pCtx->rax = pa32Mem[7 - X86_GREG_xAX];
371 rcStrict = iemMemCommitAndUnmap(pIemCpu, (void *)pa32Mem, IEM_ACCESS_STACK_R);
372 if (rcStrict == VINF_SUCCESS)
373 {
374 iemRegAddToRsp(pIemCpu, pCtx, 32);
375 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
376 }
377 }
378 }
379 return rcStrict;
380}
381
382
383/**
384 * Implements a 16-bit pusha.
385 */
386IEM_CIMPL_DEF_0(iemCImpl_pusha_16)
387{
388 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
389 RTGCPTR GCPtrTop = iemRegGetEffRsp(pIemCpu, pCtx);
390 RTGCPTR GCPtrBottom = GCPtrTop - 15;
391 VBOXSTRICTRC rcStrict;
392
393 /*
394 * The docs are a bit hard to comprehend here, but it looks like we wrap
395 * around in real mode as long as none of the individual "pushd" crosses the
396 * end of the stack segment. In protected mode we check the whole access
397 * in one go. For efficiency, only do the word-by-word thing if we're in
398 * danger of wrapping around.
399 */
400 /** @todo do pusha boundary / wrap-around checks. */
401 if (RT_UNLIKELY( GCPtrBottom > GCPtrTop
402 && IEM_IS_REAL_OR_V86_MODE(pIemCpu) ) )
403 {
404 /* word-by-word */
405 RTUINT64U TmpRsp;
406 TmpRsp.u = pCtx->rsp;
407 rcStrict = iemMemStackPushU16Ex(pIemCpu, pCtx->ax, &TmpRsp);
408 if (rcStrict == VINF_SUCCESS)
409 rcStrict = iemMemStackPushU16Ex(pIemCpu, pCtx->cx, &TmpRsp);
410 if (rcStrict == VINF_SUCCESS)
411 rcStrict = iemMemStackPushU16Ex(pIemCpu, pCtx->dx, &TmpRsp);
412 if (rcStrict == VINF_SUCCESS)
413 rcStrict = iemMemStackPushU16Ex(pIemCpu, pCtx->bx, &TmpRsp);
414 if (rcStrict == VINF_SUCCESS)
415 rcStrict = iemMemStackPushU16Ex(pIemCpu, pCtx->sp, &TmpRsp);
416 if (rcStrict == VINF_SUCCESS)
417 rcStrict = iemMemStackPushU16Ex(pIemCpu, pCtx->bp, &TmpRsp);
418 if (rcStrict == VINF_SUCCESS)
419 rcStrict = iemMemStackPushU16Ex(pIemCpu, pCtx->si, &TmpRsp);
420 if (rcStrict == VINF_SUCCESS)
421 rcStrict = iemMemStackPushU16Ex(pIemCpu, pCtx->di, &TmpRsp);
422 if (rcStrict == VINF_SUCCESS)
423 {
424 pCtx->rsp = TmpRsp.u;
425 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
426 }
427 }
428 else
429 {
430 GCPtrBottom--;
431 uint16_t *pa16Mem = NULL;
432 rcStrict = iemMemMap(pIemCpu, (void **)&pa16Mem, 16, X86_SREG_SS, GCPtrBottom, IEM_ACCESS_STACK_W);
433 if (rcStrict == VINF_SUCCESS)
434 {
435 pa16Mem[7 - X86_GREG_xDI] = pCtx->di;
436 pa16Mem[7 - X86_GREG_xSI] = pCtx->si;
437 pa16Mem[7 - X86_GREG_xBP] = pCtx->bp;
438 pa16Mem[7 - X86_GREG_xSP] = pCtx->sp;
439 pa16Mem[7 - X86_GREG_xBX] = pCtx->bx;
440 pa16Mem[7 - X86_GREG_xDX] = pCtx->dx;
441 pa16Mem[7 - X86_GREG_xCX] = pCtx->cx;
442 pa16Mem[7 - X86_GREG_xAX] = pCtx->ax;
443 rcStrict = iemMemCommitAndUnmap(pIemCpu, (void *)pa16Mem, IEM_ACCESS_STACK_W);
444 if (rcStrict == VINF_SUCCESS)
445 {
446 iemRegSubFromRsp(pIemCpu, pCtx, 16);
447 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
448 }
449 }
450 }
451 return rcStrict;
452}
453
454
455/**
456 * Implements a 32-bit pusha.
457 */
458IEM_CIMPL_DEF_0(iemCImpl_pusha_32)
459{
460 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
461 RTGCPTR GCPtrTop = iemRegGetEffRsp(pIemCpu, pCtx);
462 RTGCPTR GCPtrBottom = GCPtrTop - 31;
463 VBOXSTRICTRC rcStrict;
464
465 /*
466 * The docs are a bit hard to comprehend here, but it looks like we wrap
467 * around in real mode as long as none of the individual "pusha" crosses the
468 * end of the stack segment. In protected mode we check the whole access
469 * in one go. For efficiency, only do the word-by-word thing if we're in
470 * danger of wrapping around.
471 */
472 /** @todo do pusha boundary / wrap-around checks. */
473 if (RT_UNLIKELY( GCPtrBottom > GCPtrTop
474 && IEM_IS_REAL_OR_V86_MODE(pIemCpu) ) )
475 {
476 /* word-by-word */
477 RTUINT64U TmpRsp;
478 TmpRsp.u = pCtx->rsp;
479 rcStrict = iemMemStackPushU32Ex(pIemCpu, pCtx->eax, &TmpRsp);
480 if (rcStrict == VINF_SUCCESS)
481 rcStrict = iemMemStackPushU32Ex(pIemCpu, pCtx->ecx, &TmpRsp);
482 if (rcStrict == VINF_SUCCESS)
483 rcStrict = iemMemStackPushU32Ex(pIemCpu, pCtx->edx, &TmpRsp);
484 if (rcStrict == VINF_SUCCESS)
485 rcStrict = iemMemStackPushU32Ex(pIemCpu, pCtx->ebx, &TmpRsp);
486 if (rcStrict == VINF_SUCCESS)
487 rcStrict = iemMemStackPushU32Ex(pIemCpu, pCtx->esp, &TmpRsp);
488 if (rcStrict == VINF_SUCCESS)
489 rcStrict = iemMemStackPushU32Ex(pIemCpu, pCtx->ebp, &TmpRsp);
490 if (rcStrict == VINF_SUCCESS)
491 rcStrict = iemMemStackPushU32Ex(pIemCpu, pCtx->esi, &TmpRsp);
492 if (rcStrict == VINF_SUCCESS)
493 rcStrict = iemMemStackPushU32Ex(pIemCpu, pCtx->edi, &TmpRsp);
494 if (rcStrict == VINF_SUCCESS)
495 {
496 pCtx->rsp = TmpRsp.u;
497 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
498 }
499 }
500 else
501 {
502 GCPtrBottom--;
503 uint32_t *pa32Mem;
504 rcStrict = iemMemMap(pIemCpu, (void **)&pa32Mem, 32, X86_SREG_SS, GCPtrBottom, IEM_ACCESS_STACK_W);
505 if (rcStrict == VINF_SUCCESS)
506 {
507 pa32Mem[7 - X86_GREG_xDI] = pCtx->edi;
508 pa32Mem[7 - X86_GREG_xSI] = pCtx->esi;
509 pa32Mem[7 - X86_GREG_xBP] = pCtx->ebp;
510 pa32Mem[7 - X86_GREG_xSP] = pCtx->esp;
511 pa32Mem[7 - X86_GREG_xBX] = pCtx->ebx;
512 pa32Mem[7 - X86_GREG_xDX] = pCtx->edx;
513 pa32Mem[7 - X86_GREG_xCX] = pCtx->ecx;
514 pa32Mem[7 - X86_GREG_xAX] = pCtx->eax;
515 rcStrict = iemMemCommitAndUnmap(pIemCpu, pa32Mem, IEM_ACCESS_STACK_W);
516 if (rcStrict == VINF_SUCCESS)
517 {
518 iemRegSubFromRsp(pIemCpu, pCtx, 32);
519 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
520 }
521 }
522 }
523 return rcStrict;
524}
525
526
527/**
528 * Implements pushf.
529 *
530 *
531 * @param enmEffOpSize The effective operand size.
532 */
533IEM_CIMPL_DEF_1(iemCImpl_pushf, IEMMODE, enmEffOpSize)
534{
535 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
536
537 /*
538 * If we're in V8086 mode some care is required (which is why we're in
539 * doing this in a C implementation).
540 */
541 uint32_t fEfl = IEMMISC_GET_EFL(pIemCpu, pCtx);
542 if ( (fEfl & X86_EFL_VM)
543 && X86_EFL_GET_IOPL(fEfl) != 3 )
544 {
545 Assert(pCtx->cr0 & X86_CR0_PE);
546 if ( enmEffOpSize != IEMMODE_16BIT
547 || !(pCtx->cr4 & X86_CR4_VME))
548 return iemRaiseGeneralProtectionFault0(pIemCpu);
549 fEfl &= ~X86_EFL_IF; /* (RF and VM are out of range) */
550 fEfl |= (fEfl & X86_EFL_VIF) >> (19 - 9);
551 return iemMemStackPushU16(pIemCpu, (uint16_t)fEfl);
552 }
553
554 /*
555 * Ok, clear RF and VM and push the flags.
556 */
557 fEfl &= ~(X86_EFL_RF | X86_EFL_VM);
558
559 VBOXSTRICTRC rcStrict;
560 switch (enmEffOpSize)
561 {
562 case IEMMODE_16BIT:
563 rcStrict = iemMemStackPushU16(pIemCpu, (uint16_t)fEfl);
564 break;
565 case IEMMODE_32BIT:
566 rcStrict = iemMemStackPushU32(pIemCpu, fEfl);
567 break;
568 case IEMMODE_64BIT:
569 rcStrict = iemMemStackPushU64(pIemCpu, fEfl);
570 break;
571 IEM_NOT_REACHED_DEFAULT_CASE_RET();
572 }
573 if (rcStrict != VINF_SUCCESS)
574 return rcStrict;
575
576 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
577 return VINF_SUCCESS;
578}
579
580
581/**
582 * Implements popf.
583 *
584 * @param enmEffOpSize The effective operand size.
585 */
586IEM_CIMPL_DEF_1(iemCImpl_popf, IEMMODE, enmEffOpSize)
587{
588 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
589 uint32_t const fEflOld = IEMMISC_GET_EFL(pIemCpu, pCtx);
590 VBOXSTRICTRC rcStrict;
591 uint32_t fEflNew;
592
593 /*
594 * V8086 is special as usual.
595 */
596 if (fEflOld & X86_EFL_VM)
597 {
598 /*
599 * Almost anything goes if IOPL is 3.
600 */
601 if (X86_EFL_GET_IOPL(fEflOld) == 3)
602 {
603 switch (enmEffOpSize)
604 {
605 case IEMMODE_16BIT:
606 {
607 uint16_t u16Value;
608 rcStrict = iemMemStackPopU16(pIemCpu, &u16Value);
609 if (rcStrict != VINF_SUCCESS)
610 return rcStrict;
611 fEflNew = u16Value | (fEflOld & UINT32_C(0xffff0000));
612 break;
613 }
614 case IEMMODE_32BIT:
615 rcStrict = iemMemStackPopU32(pIemCpu, &fEflNew);
616 if (rcStrict != VINF_SUCCESS)
617 return rcStrict;
618 break;
619 IEM_NOT_REACHED_DEFAULT_CASE_RET();
620 }
621
622 fEflNew &= X86_EFL_POPF_BITS & ~(X86_EFL_IOPL);
623 fEflNew |= ~(X86_EFL_POPF_BITS & ~(X86_EFL_IOPL)) & fEflOld;
624 }
625 /*
626 * Interrupt flag virtualization with CR4.VME=1.
627 */
628 else if ( enmEffOpSize == IEMMODE_16BIT
629 && (pCtx->cr4 & X86_CR4_VME) )
630 {
631 uint16_t u16Value;
632 RTUINT64U TmpRsp;
633 TmpRsp.u = pCtx->rsp;
634 rcStrict = iemMemStackPopU16Ex(pIemCpu, &u16Value, &TmpRsp);
635 if (rcStrict != VINF_SUCCESS)
636 return rcStrict;
637
638 /** @todo Is the popf VME #GP(0) delivered after updating RSP+RIP
639 * or before? */
640 if ( ( (u16Value & X86_EFL_IF)
641 && (fEflOld & X86_EFL_VIP))
642 || (u16Value & X86_EFL_TF) )
643 return iemRaiseGeneralProtectionFault0(pIemCpu);
644
645 fEflNew = u16Value | (fEflOld & UINT32_C(0xffff0000) & ~X86_EFL_VIF);
646 fEflNew |= (fEflNew & X86_EFL_IF) << (19 - 9);
647 fEflNew &= X86_EFL_POPF_BITS & ~(X86_EFL_IOPL | X86_EFL_IF);
648 fEflNew |= ~(X86_EFL_POPF_BITS & ~(X86_EFL_IOPL | X86_EFL_IF)) & fEflOld;
649
650 pCtx->rsp = TmpRsp.u;
651 }
652 else
653 return iemRaiseGeneralProtectionFault0(pIemCpu);
654
655 }
656 /*
657 * Not in V8086 mode.
658 */
659 else
660 {
661 /* Pop the flags. */
662 switch (enmEffOpSize)
663 {
664 case IEMMODE_16BIT:
665 {
666 uint16_t u16Value;
667 rcStrict = iemMemStackPopU16(pIemCpu, &u16Value);
668 if (rcStrict != VINF_SUCCESS)
669 return rcStrict;
670 fEflNew = u16Value | (fEflOld & UINT32_C(0xffff0000));
671 break;
672 }
673 case IEMMODE_32BIT:
674 rcStrict = iemMemStackPopU32(pIemCpu, &fEflNew);
675 if (rcStrict != VINF_SUCCESS)
676 return rcStrict;
677 break;
678 case IEMMODE_64BIT:
679 {
680 uint64_t u64Value;
681 rcStrict = iemMemStackPopU64(pIemCpu, &u64Value);
682 if (rcStrict != VINF_SUCCESS)
683 return rcStrict;
684 fEflNew = u64Value; /** @todo testcase: Check exactly what happens if high bits are set. */
685 break;
686 }
687 IEM_NOT_REACHED_DEFAULT_CASE_RET();
688 }
689
690 /* Merge them with the current flags. */
691 if ( (fEflNew & (X86_EFL_IOPL | X86_EFL_IF)) == (fEflOld & (X86_EFL_IOPL | X86_EFL_IF))
692 || pIemCpu->uCpl == 0)
693 {
694 fEflNew &= X86_EFL_POPF_BITS;
695 fEflNew |= ~X86_EFL_POPF_BITS & fEflOld;
696 }
697 else if (pIemCpu->uCpl <= X86_EFL_GET_IOPL(fEflOld))
698 {
699 fEflNew &= X86_EFL_POPF_BITS & ~(X86_EFL_IOPL);
700 fEflNew |= ~(X86_EFL_POPF_BITS & ~(X86_EFL_IOPL)) & fEflOld;
701 }
702 else
703 {
704 fEflNew &= X86_EFL_POPF_BITS & ~(X86_EFL_IOPL | X86_EFL_IF);
705 fEflNew |= ~(X86_EFL_POPF_BITS & ~(X86_EFL_IOPL | X86_EFL_IF)) & fEflOld;
706 }
707 }
708
709 /*
710 * Commit the flags.
711 */
712 Assert(fEflNew & RT_BIT_32(1));
713 IEMMISC_SET_EFL(pIemCpu, pCtx, fEflNew);
714 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
715
716 return VINF_SUCCESS;
717}
718
719
720/**
721 * Implements an indirect call.
722 *
723 * @param uNewPC The new program counter (RIP) value (loaded from the
724 * operand).
725 * @param enmEffOpSize The effective operand size.
726 */
727IEM_CIMPL_DEF_1(iemCImpl_call_16, uint16_t, uNewPC)
728{
729 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
730 uint16_t uOldPC = pCtx->ip + cbInstr;
731 if (uNewPC > pCtx->cs.u32Limit)
732 return iemRaiseGeneralProtectionFault0(pIemCpu);
733
734 VBOXSTRICTRC rcStrict = iemMemStackPushU16(pIemCpu, uOldPC);
735 if (rcStrict != VINF_SUCCESS)
736 return rcStrict;
737
738 pCtx->rip = uNewPC;
739 pCtx->eflags.Bits.u1RF = 0;
740 return VINF_SUCCESS;
741}
742
743
744/**
745 * Implements a 16-bit relative call.
746 *
747 * @param offDisp The displacment offset.
748 */
749IEM_CIMPL_DEF_1(iemCImpl_call_rel_16, int16_t, offDisp)
750{
751 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
752 uint16_t uOldPC = pCtx->ip + cbInstr;
753 uint16_t uNewPC = uOldPC + offDisp;
754 if (uNewPC > pCtx->cs.u32Limit)
755 return iemRaiseGeneralProtectionFault0(pIemCpu);
756
757 VBOXSTRICTRC rcStrict = iemMemStackPushU16(pIemCpu, uOldPC);
758 if (rcStrict != VINF_SUCCESS)
759 return rcStrict;
760
761 pCtx->rip = uNewPC;
762 pCtx->eflags.Bits.u1RF = 0;
763 return VINF_SUCCESS;
764}
765
766
767/**
768 * Implements a 32-bit indirect call.
769 *
770 * @param uNewPC The new program counter (RIP) value (loaded from the
771 * operand).
772 * @param enmEffOpSize The effective operand size.
773 */
774IEM_CIMPL_DEF_1(iemCImpl_call_32, uint32_t, uNewPC)
775{
776 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
777 uint32_t uOldPC = pCtx->eip + cbInstr;
778 if (uNewPC > pCtx->cs.u32Limit)
779 return iemRaiseGeneralProtectionFault0(pIemCpu);
780
781 VBOXSTRICTRC rcStrict = iemMemStackPushU32(pIemCpu, uOldPC);
782 if (rcStrict != VINF_SUCCESS)
783 return rcStrict;
784
785#if defined(IN_RING3) && defined(VBOX_WITH_RAW_MODE) && defined(VBOX_WITH_CALL_RECORD)
786 /*
787 * CASM hook for recording interesting indirect calls.
788 */
789 if ( !pCtx->eflags.Bits.u1IF
790 && (pCtx->cr0 & X86_CR0_PG)
791 && !CSAMIsEnabled(IEMCPU_TO_VM(pIemCpu))
792 && pIemCpu->uCpl == 0)
793 {
794 EMSTATE enmState = EMGetState(IEMCPU_TO_VMCPU(pIemCpu));
795 if ( enmState == EMSTATE_IEM_THEN_REM
796 || enmState == EMSTATE_IEM
797 || enmState == EMSTATE_REM)
798 CSAMR3RecordCallAddress(IEMCPU_TO_VM(pIemCpu), pCtx->eip);
799 }
800#endif
801
802 pCtx->rip = uNewPC;
803 pCtx->eflags.Bits.u1RF = 0;
804 return VINF_SUCCESS;
805}
806
807
808/**
809 * Implements a 32-bit relative call.
810 *
811 * @param offDisp The displacment offset.
812 */
813IEM_CIMPL_DEF_1(iemCImpl_call_rel_32, int32_t, offDisp)
814{
815 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
816 uint32_t uOldPC = pCtx->eip + cbInstr;
817 uint32_t uNewPC = uOldPC + offDisp;
818 if (uNewPC > pCtx->cs.u32Limit)
819 return iemRaiseGeneralProtectionFault0(pIemCpu);
820
821 VBOXSTRICTRC rcStrict = iemMemStackPushU32(pIemCpu, uOldPC);
822 if (rcStrict != VINF_SUCCESS)
823 return rcStrict;
824
825 pCtx->rip = uNewPC;
826 pCtx->eflags.Bits.u1RF = 0;
827 return VINF_SUCCESS;
828}
829
830
831/**
832 * Implements a 64-bit indirect call.
833 *
834 * @param uNewPC The new program counter (RIP) value (loaded from the
835 * operand).
836 * @param enmEffOpSize The effective operand size.
837 */
838IEM_CIMPL_DEF_1(iemCImpl_call_64, uint64_t, uNewPC)
839{
840 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
841 uint64_t uOldPC = pCtx->rip + cbInstr;
842 if (!IEM_IS_CANONICAL(uNewPC))
843 return iemRaiseGeneralProtectionFault0(pIemCpu);
844
845 VBOXSTRICTRC rcStrict = iemMemStackPushU64(pIemCpu, uOldPC);
846 if (rcStrict != VINF_SUCCESS)
847 return rcStrict;
848
849 pCtx->rip = uNewPC;
850 pCtx->eflags.Bits.u1RF = 0;
851 return VINF_SUCCESS;
852}
853
854
855/**
856 * Implements a 64-bit relative call.
857 *
858 * @param offDisp The displacment offset.
859 */
860IEM_CIMPL_DEF_1(iemCImpl_call_rel_64, int64_t, offDisp)
861{
862 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
863 uint64_t uOldPC = pCtx->rip + cbInstr;
864 uint64_t uNewPC = uOldPC + offDisp;
865 if (!IEM_IS_CANONICAL(uNewPC))
866 return iemRaiseNotCanonical(pIemCpu);
867
868 VBOXSTRICTRC rcStrict = iemMemStackPushU64(pIemCpu, uOldPC);
869 if (rcStrict != VINF_SUCCESS)
870 return rcStrict;
871
872 pCtx->rip = uNewPC;
873 pCtx->eflags.Bits.u1RF = 0;
874 return VINF_SUCCESS;
875}
876
877
878/**
879 * Implements far jumps and calls thru task segments (TSS).
880 *
881 * @param uSel The selector.
882 * @param enmBranch The kind of branching we're performing.
883 * @param enmEffOpSize The effective operand size.
884 * @param pDesc The descriptor corresponding to @a uSel. The type is
885 * task gate.
886 */
887IEM_CIMPL_DEF_4(iemCImpl_BranchTaskSegment, uint16_t, uSel, IEMBRANCH, enmBranch, IEMMODE, enmEffOpSize, PIEMSELDESC, pDesc)
888{
889#ifndef IEM_IMPLEMENTS_TASKSWITCH
890 IEM_RETURN_ASPECT_NOT_IMPLEMENTED();
891#else
892 Assert(enmBranch == IEMBRANCH_JUMP || enmBranch == IEMBRANCH_CALL);
893 Assert( pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_286_TSS_AVAIL
894 || pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_386_TSS_AVAIL);
895
896 if ( pDesc->Legacy.Gate.u2Dpl < pIemCpu->uCpl
897 || pDesc->Legacy.Gate.u2Dpl < (uSel & X86_SEL_RPL))
898 {
899 Log(("BranchTaskSegment invalid priv. uSel=%04x TSS DPL=%d CPL=%u Sel RPL=%u -> #GP\n", uSel, pDesc->Legacy.Gate.u2Dpl,
900 pIemCpu->uCpl, (uSel & X86_SEL_RPL)));
901 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel & X86_SEL_MASK_OFF_RPL);
902 }
903
904 /** @todo This is checked earlier for far jumps (see iemCImpl_FarJmp) but not
905 * far calls (see iemCImpl_callf). Most likely in both cases it should be
906 * checked here, need testcases. */
907 if (!pDesc->Legacy.Gen.u1Present)
908 {
909 Log(("BranchTaskSegment TSS not present uSel=%04x -> #NP\n", uSel));
910 return iemRaiseSelectorNotPresentBySelector(pIemCpu, uSel & X86_SEL_MASK_OFF_RPL);
911 }
912
913 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
914 uint32_t uNextEip = pCtx->eip + cbInstr;
915 return iemTaskSwitch(pIemCpu, pIemCpu->CTX_SUFF(pCtx), enmBranch == IEMBRANCH_JUMP ? IEMTASKSWITCH_JUMP : IEMTASKSWITCH_CALL,
916 uNextEip, 0 /* fFlags */, 0 /* uErr */, 0 /* uCr2 */, uSel, pDesc);
917#endif
918}
919
920
921/**
922 * Implements far jumps and calls thru task gates.
923 *
924 * @param uSel The selector.
925 * @param enmBranch The kind of branching we're performing.
926 * @param enmEffOpSize The effective operand size.
927 * @param pDesc The descriptor corresponding to @a uSel. The type is
928 * task gate.
929 */
930IEM_CIMPL_DEF_4(iemCImpl_BranchTaskGate, uint16_t, uSel, IEMBRANCH, enmBranch, IEMMODE, enmEffOpSize, PIEMSELDESC, pDesc)
931{
932#ifndef IEM_IMPLEMENTS_TASKSWITCH
933 IEM_RETURN_ASPECT_NOT_IMPLEMENTED();
934#else
935 Assert(enmBranch == IEMBRANCH_JUMP || enmBranch == IEMBRANCH_CALL);
936
937 if ( pDesc->Legacy.Gate.u2Dpl < pIemCpu->uCpl
938 || pDesc->Legacy.Gate.u2Dpl < (uSel & X86_SEL_RPL))
939 {
940 Log(("BranchTaskGate invalid priv. uSel=%04x TSS DPL=%d CPL=%u Sel RPL=%u -> #GP\n", uSel, pDesc->Legacy.Gate.u2Dpl,
941 pIemCpu->uCpl, (uSel & X86_SEL_RPL)));
942 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel & X86_SEL_MASK_OFF_RPL);
943 }
944
945 /** @todo This is checked earlier for far jumps (see iemCImpl_FarJmp) but not
946 * far calls (see iemCImpl_callf). Most likely in both cases it should be
947 * checked here, need testcases. */
948 if (!pDesc->Legacy.Gen.u1Present)
949 {
950 Log(("BranchTaskSegment segment not present uSel=%04x -> #NP\n", uSel));
951 return iemRaiseSelectorNotPresentBySelector(pIemCpu, uSel & X86_SEL_MASK_OFF_RPL);
952 }
953
954 /*
955 * Fetch the new TSS descriptor from the GDT.
956 */
957 RTSEL uSelTss = pDesc->Legacy.Gate.u16Sel;
958 if (uSelTss & X86_SEL_LDT)
959 {
960 Log(("BranchTaskGate TSS is in LDT. uSel=%04x uSelTss=%04x -> #GP\n", uSel, uSelTss));
961 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel & X86_SEL_MASK_OFF_RPL);
962 }
963
964 IEMSELDESC TssDesc;
965 VBOXSTRICTRC rcStrict = iemMemFetchSelDesc(pIemCpu, &TssDesc, uSelTss, X86_XCPT_GP);
966 if (rcStrict != VINF_SUCCESS)
967 return rcStrict;
968
969 if (TssDesc.Legacy.Gate.u4Type & X86_SEL_TYPE_SYS_TSS_BUSY_MASK)
970 {
971 Log(("BranchTaskGate TSS is busy. uSel=%04x uSelTss=%04x DescType=%#x -> #GP\n", uSel, uSelTss,
972 TssDesc.Legacy.Gate.u4Type));
973 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel & X86_SEL_MASK_OFF_RPL);
974 }
975
976 if (!TssDesc.Legacy.Gate.u1Present)
977 {
978 Log(("BranchTaskGate TSS is not present. uSel=%04x uSelTss=%04x -> #NP\n", uSel, uSelTss));
979 return iemRaiseSelectorNotPresentBySelector(pIemCpu, uSelTss & X86_SEL_MASK_OFF_RPL);
980 }
981
982 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
983 uint32_t uNextEip = pCtx->eip + cbInstr;
984 return iemTaskSwitch(pIemCpu, pIemCpu->CTX_SUFF(pCtx), enmBranch == IEMBRANCH_JUMP ? IEMTASKSWITCH_JUMP : IEMTASKSWITCH_CALL,
985 uNextEip, 0 /* fFlags */, 0 /* uErr */, 0 /* uCr2 */, uSelTss, &TssDesc);
986#endif
987}
988
989
990/**
991 * Implements far jumps and calls thru call gates.
992 *
993 * @param uSel The selector.
994 * @param enmBranch The kind of branching we're performing.
995 * @param enmEffOpSize The effective operand size.
996 * @param pDesc The descriptor corresponding to @a uSel. The type is
997 * call gate.
998 */
999IEM_CIMPL_DEF_4(iemCImpl_BranchCallGate, uint16_t, uSel, IEMBRANCH, enmBranch, IEMMODE, enmEffOpSize, PIEMSELDESC, pDesc)
1000{
1001#ifndef IEM_IMPLEMENTS_CALLGATE
1002 IEM_RETURN_ASPECT_NOT_IMPLEMENTED();
1003#else
1004 /* NB: Far jumps can only do intra-privilege transfers. Far calls support
1005 * inter-privilege calls and are much more complex.
1006 *
1007 * NB: 64-bit call gate has the same type as a 32-bit call gate! If
1008 * EFER.LMA=1, the gate must be 64-bit. Conversely if EFER.LMA=0, the gate
1009 * must be 16-bit or 32-bit.
1010 */
1011 /** @todo: effective operand size is probably irrelevant here, only the
1012 * call gate bitness matters??
1013 */
1014 VBOXSTRICTRC rcStrict;
1015 RTPTRUNION uPtrRet;
1016 uint64_t uNewRsp;
1017 uint64_t uNewRip;
1018 uint64_t u64Base;
1019 uint32_t cbLimit;
1020 RTSEL uNewCS;
1021 IEMSELDESC DescCS;
1022 PCPUMCTX pCtx;
1023
1024 AssertCompile(X86_SEL_TYPE_SYS_386_CALL_GATE == AMD64_SEL_TYPE_SYS_CALL_GATE);
1025 Assert(enmBranch == IEMBRANCH_JUMP || enmBranch == IEMBRANCH_CALL);
1026 Assert( pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_286_CALL_GATE
1027 || pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_386_CALL_GATE);
1028
1029 /* Determine the new instruction pointer from the gate descriptor. */
1030 uNewRip = pDesc->Legacy.Gate.u16OffsetLow
1031 | ((uint32_t)pDesc->Legacy.Gate.u16OffsetHigh << 16)
1032 | ((uint64_t)pDesc->Long.Gate.u32OffsetTop << 32);
1033
1034 /* Perform DPL checks on the gate descriptor. */
1035 if ( pDesc->Legacy.Gate.u2Dpl < pIemCpu->uCpl
1036 || pDesc->Legacy.Gate.u2Dpl < (uSel & X86_SEL_RPL))
1037 {
1038 Log(("BranchCallGate invalid priv. uSel=%04x Gate DPL=%d CPL=%u Sel RPL=%u -> #GP\n", uSel, pDesc->Legacy.Gate.u2Dpl,
1039 pIemCpu->uCpl, (uSel & X86_SEL_RPL)));
1040 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel);
1041 }
1042
1043 /** @todo does this catch NULL selectors, too? */
1044 if (!pDesc->Legacy.Gen.u1Present)
1045 {
1046 Log(("BranchCallGate Gate not present uSel=%04x -> #NP\n", uSel));
1047 return iemRaiseSelectorNotPresentBySelector(pIemCpu, uSel);
1048 }
1049
1050 /*
1051 * Fetch the target CS descriptor from the GDT or LDT.
1052 */
1053 uNewCS = pDesc->Legacy.Gate.u16Sel;
1054 rcStrict = iemMemFetchSelDesc(pIemCpu, &DescCS, uNewCS, X86_XCPT_GP);
1055 if (rcStrict != VINF_SUCCESS)
1056 return rcStrict;
1057
1058 /* Target CS must be a code selector. */
1059 if ( !DescCS.Legacy.Gen.u1DescType
1060 || !(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE) )
1061 {
1062 Log(("BranchCallGate %04x:%08RX64 -> not a code selector (u1DescType=%u u4Type=%#x).\n",
1063 uNewCS, uNewRip, DescCS.Legacy.Gen.u1DescType, DescCS.Legacy.Gen.u4Type));
1064 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewCS);
1065 }
1066
1067 /* Privilege checks on target CS. */
1068 if (enmBranch == IEMBRANCH_JUMP)
1069 {
1070 if (DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF)
1071 {
1072 if (DescCS.Legacy.Gen.u2Dpl > pIemCpu->uCpl)
1073 {
1074 Log(("BranchCallGate jump (conforming) bad DPL uNewCS=%04x Gate DPL=%d CPL=%u -> #GP\n",
1075 uNewCS, DescCS.Legacy.Gen.u2Dpl, pIemCpu->uCpl));
1076 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewCS);
1077 }
1078 }
1079 else
1080 {
1081 if (DescCS.Legacy.Gen.u2Dpl != pIemCpu->uCpl)
1082 {
1083 Log(("BranchCallGate jump (non-conforming) bad DPL uNewCS=%04x Gate DPL=%d CPL=%u -> #GP\n",
1084 uNewCS, DescCS.Legacy.Gen.u2Dpl, pIemCpu->uCpl));
1085 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewCS);
1086 }
1087 }
1088 }
1089 else
1090 {
1091 Assert(enmBranch == IEMBRANCH_CALL);
1092 if (DescCS.Legacy.Gen.u2Dpl > pIemCpu->uCpl)
1093 {
1094 Log(("BranchCallGate call invalid priv. uNewCS=%04x Gate DPL=%d CPL=%u -> #GP\n",
1095 uNewCS, DescCS.Legacy.Gen.u2Dpl, pIemCpu->uCpl));
1096 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewCS & X86_SEL_MASK_OFF_RPL);
1097 }
1098 }
1099
1100 /* Additional long mode checks. */
1101 if (IEM_IS_LONG_MODE(pIemCpu))
1102 {
1103 if (!DescCS.Legacy.Gen.u1Long)
1104 {
1105 Log(("BranchCallGate uNewCS %04x -> not a 64-bit code segment.\n", uNewCS));
1106 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewCS);
1107 }
1108
1109 /* L vs D. */
1110 if ( DescCS.Legacy.Gen.u1Long
1111 && DescCS.Legacy.Gen.u1DefBig)
1112 {
1113 Log(("BranchCallGate uNewCS %04x -> both L and D are set.\n", uNewCS));
1114 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewCS);
1115 }
1116 }
1117
1118 if (!DescCS.Legacy.Gate.u1Present)
1119 {
1120 Log(("BranchCallGate target CS is not present. uSel=%04x uNewCS=%04x -> #NP(CS)\n", uSel, uNewCS));
1121 return iemRaiseSelectorNotPresentBySelector(pIemCpu, uNewCS);
1122 }
1123
1124 pCtx = pIemCpu->CTX_SUFF(pCtx);
1125
1126 if (enmBranch == IEMBRANCH_JUMP)
1127 {
1128 /** @todo: This is very similar to regular far jumps; merge! */
1129 /* Jumps are fairly simple... */
1130
1131 /* Chop the high bits off if 16-bit gate (Intel says so). */
1132 if (pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_286_CALL_GATE)
1133 uNewRip = (uint16_t)uNewRip;
1134
1135 /* Limit check for non-long segments. */
1136 cbLimit = X86DESC_LIMIT_G(&DescCS.Legacy);
1137 if (DescCS.Legacy.Gen.u1Long)
1138 u64Base = 0;
1139 else
1140 {
1141 if (uNewRip > cbLimit)
1142 {
1143 Log(("BranchCallGate jump %04x:%08RX64 -> out of bounds (%#x) -> #GP(0)\n", uNewCS, uNewRip, cbLimit));
1144 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, 0);
1145 }
1146 u64Base = X86DESC_BASE(&DescCS.Legacy);
1147 }
1148
1149 /* Canonical address check. */
1150 if (!IEM_IS_CANONICAL(uNewRip))
1151 {
1152 Log(("BranchCallGate jump %04x:%016RX64 - not canonical -> #GP\n", uNewCS, uNewRip));
1153 return iemRaiseNotCanonical(pIemCpu);
1154 }
1155
1156 /*
1157 * Ok, everything checked out fine. Now set the accessed bit before
1158 * committing the result into CS, CSHID and RIP.
1159 */
1160 if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
1161 {
1162 rcStrict = iemMemMarkSelDescAccessed(pIemCpu, uNewCS);
1163 if (rcStrict != VINF_SUCCESS)
1164 return rcStrict;
1165 /** @todo check what VT-x and AMD-V does. */
1166 DescCS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
1167 }
1168
1169 /* commit */
1170 pCtx->rip = uNewRip;
1171 pCtx->cs.Sel = uNewCS & X86_SEL_MASK_OFF_RPL;
1172 pCtx->cs.Sel |= pIemCpu->uCpl; /** @todo is this right for conforming segs? or in general? */
1173 pCtx->cs.ValidSel = pCtx->cs.Sel;
1174 pCtx->cs.fFlags = CPUMSELREG_FLAGS_VALID;
1175 pCtx->cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCS.Legacy);
1176 pCtx->cs.u32Limit = cbLimit;
1177 pCtx->cs.u64Base = u64Base;
1178 }
1179 else
1180 {
1181 Assert(enmBranch == IEMBRANCH_CALL);
1182 /* Calls are much more complicated. */
1183
1184 if (DescCS.Legacy.Gen.u2Dpl < pIemCpu->uCpl)
1185 {
1186 uint16_t offNewStack; /* Offset of new stack in TSS. */
1187 uint16_t cbNewStack; /* Number of bytes the stack information takes up in TSS. */
1188 uint8_t uNewCSDpl;
1189 uint8_t cbWords;
1190 RTSEL uNewSS;
1191 RTSEL uOldSS;
1192 uint64_t uOldRsp;
1193 IEMSELDESC DescSS;
1194 RTPTRUNION uPtrTSS;
1195 RTGCPTR GCPtrTSS;
1196 RTPTRUNION uPtrParmWds;
1197 RTGCPTR GCPtrParmWds;
1198
1199 /* More privilege. This is the fun part. */
1200 Assert(!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF)); /* Filtered out above. */
1201
1202 /*
1203 * Determine new SS:rSP from the TSS.
1204 */
1205 Assert(!pCtx->tr.Attr.n.u1DescType);
1206
1207 /* Figure out where the new stack pointer is stored in the TSS. */
1208 uNewCSDpl = uNewCS & X86_SEL_RPL;
1209 if (!IEM_IS_LONG_MODE(pIemCpu))
1210 {
1211 if (pCtx->tr.Attr.n.u4Type == X86_SEL_TYPE_SYS_386_TSS_BUSY)
1212 {
1213 offNewStack = RT_OFFSETOF(X86TSS32, esp0) + uNewCSDpl * 8;
1214 cbNewStack = RT_SIZEOFMEMB(X86TSS32, esp0) + RT_SIZEOFMEMB(X86TSS32, ss0);
1215 }
1216 else
1217 {
1218 Assert(pCtx->tr.Attr.n.u4Type == X86_SEL_TYPE_SYS_286_TSS_BUSY);
1219 offNewStack = RT_OFFSETOF(X86TSS16, sp0) + uNewCSDpl * 4;
1220 cbNewStack = RT_SIZEOFMEMB(X86TSS16, sp0) + RT_SIZEOFMEMB(X86TSS16, ss0);
1221 }
1222 }
1223 else
1224 {
1225 Assert(pCtx->tr.Attr.n.u4Type == AMD64_SEL_TYPE_SYS_TSS_BUSY);
1226 offNewStack = RT_OFFSETOF(X86TSS64, rsp0) + uNewCSDpl * RT_SIZEOFMEMB(X86TSS64, rsp0);
1227 cbNewStack = RT_SIZEOFMEMB(X86TSS64, rsp0);
1228 }
1229
1230 /* Check against TSS limit. */
1231 if ((uint16_t)(offNewStack + cbNewStack - 1) > pCtx->tr.u32Limit)
1232 {
1233 Log(("BranchCallGate inner stack past TSS limit - %u > %u -> #TS(TSS)\n", offNewStack + cbNewStack - 1, pCtx->tr.u32Limit));
1234 return iemRaiseTaskSwitchFaultBySelector(pIemCpu, pCtx->tr.Sel);
1235 }
1236
1237 GCPtrTSS = pCtx->tr.u64Base + offNewStack;
1238 rcStrict = iemMemMap(pIemCpu, &uPtrTSS.pv, cbNewStack, UINT8_MAX, GCPtrTSS, IEM_ACCESS_SYS_R);
1239 if (rcStrict != VINF_SUCCESS)
1240 {
1241 Log(("BranchCallGate: TSS mapping failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
1242 return rcStrict;
1243 }
1244
1245 if (!IEM_IS_LONG_MODE(pIemCpu))
1246 {
1247 if (pCtx->tr.Attr.n.u4Type == X86_SEL_TYPE_SYS_386_TSS_BUSY)
1248 {
1249 uNewRsp = uPtrTSS.pu32[0];
1250 uNewSS = uPtrTSS.pu16[2];
1251 }
1252 else
1253 {
1254 Assert(pCtx->tr.Attr.n.u4Type == X86_SEL_TYPE_SYS_286_TSS_BUSY);
1255 uNewRsp = uPtrTSS.pu16[0];
1256 uNewSS = uPtrTSS.pu16[1];
1257 }
1258 }
1259 else
1260 {
1261 Assert(pCtx->tr.Attr.n.u4Type == AMD64_SEL_TYPE_SYS_TSS_BUSY);
1262 /* SS will be a NULL selector, but that's valid. */
1263 uNewRsp = uPtrTSS.pu64[0];
1264 uNewSS = uNewCSDpl;
1265 }
1266
1267 /* Done with the TSS now. */
1268 rcStrict = iemMemCommitAndUnmap(pIemCpu, uPtrTSS.pv, IEM_ACCESS_SYS_R);
1269 if (rcStrict != VINF_SUCCESS)
1270 {
1271 Log(("BranchCallGate: TSS unmapping failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
1272 return rcStrict;
1273 }
1274
1275 /* Only used outside of long mode. */
1276 cbWords = pDesc->Legacy.Gate.u4ParmCount;
1277
1278 /* If EFER.LMA is 0, there's extra work to do. */
1279 if (!IEM_IS_LONG_MODE(pIemCpu))
1280 {
1281 if ((uNewSS & X86_SEL_MASK_OFF_RPL) == 0)
1282 {
1283 Log(("BranchCallGate new SS NULL -> #TS(NewSS)\n"));
1284 return iemRaiseTaskSwitchFaultBySelector(pIemCpu, uNewSS);
1285 }
1286
1287 /* Grab the new SS descriptor. */
1288 rcStrict = iemMemFetchSelDesc(pIemCpu, &DescSS, uNewSS, X86_XCPT_SS);
1289 if (rcStrict != VINF_SUCCESS)
1290 return rcStrict;
1291
1292 /* Ensure that CS.DPL == SS.RPL == SS.DPL. */
1293 if ( (DescCS.Legacy.Gen.u2Dpl != (uNewSS & X86_SEL_RPL))
1294 || (DescCS.Legacy.Gen.u2Dpl != DescSS.Legacy.Gen.u2Dpl))
1295 {
1296 Log(("BranchCallGate call bad RPL/DPL uNewSS=%04x SS DPL=%d CS DPL=%u -> #TS(NewSS)\n",
1297 uNewSS, DescCS.Legacy.Gen.u2Dpl, DescCS.Legacy.Gen.u2Dpl));
1298 return iemRaiseTaskSwitchFaultBySelector(pIemCpu, uNewSS);
1299 }
1300
1301 /* Ensure new SS is a writable data segment. */
1302 if ((DescSS.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_WRITE)) != X86_SEL_TYPE_WRITE)
1303 {
1304 Log(("BranchCallGate call new SS -> not a writable data selector (u4Type=%#x)\n", DescSS.Legacy.Gen.u4Type));
1305 return iemRaiseTaskSwitchFaultBySelector(pIemCpu, uNewSS);
1306 }
1307
1308 if (!DescSS.Legacy.Gen.u1Present)
1309 {
1310 Log(("BranchCallGate New stack not present uSel=%04x -> #SS(NewSS)\n", uNewSS));
1311 return iemRaiseStackSelectorNotPresentBySelector(pIemCpu, uNewSS);
1312 }
1313 if (pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_386_CALL_GATE)
1314 cbNewStack = (uint16_t)sizeof(uint32_t) * (4 + cbWords);
1315 else
1316 cbNewStack = (uint16_t)sizeof(uint16_t) * (4 + cbWords);
1317 }
1318 else
1319 {
1320 /* Just grab the new (NULL) SS descriptor. */
1321 rcStrict = iemMemFetchSelDesc(pIemCpu, &DescSS, uNewSS, X86_XCPT_SS);
1322 if (rcStrict != VINF_SUCCESS)
1323 return rcStrict;
1324
1325 cbNewStack = sizeof(uint64_t) * 4;
1326 }
1327
1328 /** @todo: According to Intel, new stack is checked for enough space first,
1329 * then switched. According to AMD, the stack is switched first and
1330 * then pushes might fault!
1331 */
1332
1333 /** @todo: According to AMD, CS is loaded first, then SS.
1334 * According to Intel, it's the other way around!?
1335 */
1336
1337 /** @todo: Intel and AMD disagree on when exactly the CPL changes! */
1338
1339 /* Set the accessed bit before committing new SS. */
1340 if (!(DescSS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
1341 {
1342 rcStrict = iemMemMarkSelDescAccessed(pIemCpu, uNewSS);
1343 if (rcStrict != VINF_SUCCESS)
1344 return rcStrict;
1345 DescSS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
1346 }
1347
1348 /* Remember the old SS:rSP and their linear address. */
1349 uOldSS = pCtx->ss.Sel;
1350 uOldRsp = pCtx->rsp;
1351
1352 GCPtrParmWds = pCtx->ss.u64Base + pCtx->rsp;
1353
1354 /* Commit new SS:rSP. */
1355 pCtx->ss.Sel = uNewSS;
1356 pCtx->ss.ValidSel = uNewSS;
1357 pCtx->ss.Attr.u = X86DESC_GET_HID_ATTR(&DescSS.Legacy);
1358 pCtx->ss.u32Limit = X86DESC_LIMIT_G(&DescSS.Legacy);
1359 pCtx->ss.u64Base = X86DESC_BASE(&DescSS.Legacy);
1360 pCtx->ss.fFlags = CPUMSELREG_FLAGS_VALID;
1361 pCtx->rsp = uNewRsp;
1362 pIemCpu->uCpl = uNewCSDpl;
1363 Assert(CPUMSELREG_ARE_HIDDEN_PARTS_VALID(IEMCPU_TO_VMCPU(pIemCpu), &pCtx->ss));
1364 CPUMSetChangedFlags(IEMCPU_TO_VMCPU(pIemCpu), CPUM_CHANGED_HIDDEN_SEL_REGS);
1365
1366 /* Check new stack - may #SS(NewSS). */
1367 rcStrict = iemMemStackPushBeginSpecial(pIemCpu, cbNewStack,
1368 &uPtrRet.pv, &uNewRsp);
1369 if (rcStrict != VINF_SUCCESS)
1370 {
1371 Log(("BranchCallGate: New stack mapping failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
1372 return rcStrict;
1373 }
1374
1375 if (!IEM_IS_LONG_MODE(pIemCpu))
1376 {
1377 if (pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_386_CALL_GATE)
1378 {
1379 /* Push the old CS:rIP. */
1380 uPtrRet.pu32[0] = pCtx->eip + cbInstr;
1381 uPtrRet.pu32[1] = pCtx->cs.Sel; /** @todo Testcase: What is written to the high word when pushing CS? */
1382
1383 /* Map the relevant chunk of the old stack. */
1384 rcStrict = iemMemMap(pIemCpu, &uPtrParmWds.pv, cbWords * 4, UINT8_MAX, GCPtrParmWds, IEM_ACCESS_DATA_R);
1385 if (rcStrict != VINF_SUCCESS)
1386 {
1387 Log(("BranchCallGate: Old stack mapping (32-bit) failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
1388 return rcStrict;
1389 }
1390
1391 /* Copy the parameter (d)words. */
1392 for (int i = 0; i < cbWords; ++i)
1393 uPtrRet.pu32[2 + i] = uPtrParmWds.pu32[i];
1394
1395 /* Unmap the old stack. */
1396 rcStrict = iemMemCommitAndUnmap(pIemCpu, uPtrParmWds.pv, IEM_ACCESS_DATA_R);
1397 if (rcStrict != VINF_SUCCESS)
1398 {
1399 Log(("BranchCallGate: Old stack unmapping (32-bit) failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
1400 return rcStrict;
1401 }
1402
1403 /* Push the old SS:rSP. */
1404 uPtrRet.pu32[2 + cbWords + 0] = uOldRsp;
1405 uPtrRet.pu32[2 + cbWords + 1] = uOldSS;
1406 }
1407 else
1408 {
1409 Assert(pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_286_CALL_GATE);
1410
1411 /* Push the old CS:rIP. */
1412 uPtrRet.pu16[0] = pCtx->ip + cbInstr;
1413 uPtrRet.pu16[1] = pCtx->cs.Sel;
1414
1415 /* Map the relevant chunk of the old stack. */
1416 rcStrict = iemMemMap(pIemCpu, &uPtrParmWds.pv, cbWords * 2, UINT8_MAX, GCPtrParmWds, IEM_ACCESS_DATA_R);
1417 if (rcStrict != VINF_SUCCESS)
1418 {
1419 Log(("BranchCallGate: Old stack mapping (16-bit) failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
1420 return rcStrict;
1421 }
1422
1423 /* Copy the parameter words. */
1424 for (int i = 0; i < cbWords; ++i)
1425 uPtrRet.pu16[2 + i] = uPtrParmWds.pu16[i];
1426
1427 /* Unmap the old stack. */
1428 rcStrict = iemMemCommitAndUnmap(pIemCpu, uPtrParmWds.pv, IEM_ACCESS_DATA_R);
1429 if (rcStrict != VINF_SUCCESS)
1430 {
1431 Log(("BranchCallGate: Old stack unmapping (32-bit) failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
1432 return rcStrict;
1433 }
1434
1435 /* Push the old SS:rSP. */
1436 uPtrRet.pu16[2 + cbWords + 0] = uOldRsp;
1437 uPtrRet.pu16[2 + cbWords + 1] = uOldSS;
1438 }
1439 }
1440 else
1441 {
1442 Assert(pDesc->Legacy.Gate.u4Type == AMD64_SEL_TYPE_SYS_CALL_GATE);
1443
1444 /* For 64-bit gates, no parameters are copied. Just push old SS:rSP and CS:rIP. */
1445 uPtrRet.pu64[0] = pCtx->rip + cbInstr;
1446 uPtrRet.pu64[1] = pCtx->cs.Sel; /** @todo Testcase: What is written to the high words when pushing CS? */
1447 uPtrRet.pu64[2] = uOldRsp;
1448 uPtrRet.pu64[3] = uOldSS; /** @todo Testcase: What is written to the high words when pushing SS? */
1449 }
1450
1451 rcStrict = iemMemStackPushCommitSpecial(pIemCpu, uPtrRet.pv, uNewRsp);
1452 if (rcStrict != VINF_SUCCESS)
1453 {
1454 Log(("BranchCallGate: New stack unmapping failed (%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
1455 return rcStrict;
1456 }
1457
1458 /* Chop the high bits off if 16-bit gate (Intel says so). */
1459 if (pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_286_CALL_GATE)
1460 uNewRip = (uint16_t)uNewRip;
1461
1462 /* Limit / canonical check. */
1463 cbLimit = X86DESC_LIMIT_G(&DescCS.Legacy);
1464 if (!IEM_IS_LONG_MODE(pIemCpu))
1465 {
1466 if (uNewRip > cbLimit)
1467 {
1468 Log(("BranchCallGate %04x:%08RX64 -> out of bounds (%#x)\n", uNewCS, uNewRip, cbLimit));
1469 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, 0);
1470 }
1471 u64Base = X86DESC_BASE(&DescCS.Legacy);
1472 }
1473 else
1474 {
1475 Assert(pDesc->Legacy.Gate.u4Type == AMD64_SEL_TYPE_SYS_CALL_GATE);
1476 if (!IEM_IS_CANONICAL(uNewRip))
1477 {
1478 Log(("BranchCallGate call %04x:%016RX64 - not canonical -> #GP\n", uNewCS, uNewRip));
1479 return iemRaiseNotCanonical(pIemCpu);
1480 }
1481 u64Base = 0;
1482 }
1483
1484 /*
1485 * Now set the accessed bit before
1486 * writing the return address to the stack and committing the result into
1487 * CS, CSHID and RIP.
1488 */
1489 /** @todo Testcase: Need to check WHEN exactly the accessed bit is set. */
1490 if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
1491 {
1492 rcStrict = iemMemMarkSelDescAccessed(pIemCpu, uNewCS);
1493 if (rcStrict != VINF_SUCCESS)
1494 return rcStrict;
1495 /** @todo check what VT-x and AMD-V does. */
1496 DescCS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
1497 }
1498
1499 /* Commit new CS:rIP. */
1500 pCtx->rip = uNewRip;
1501 pCtx->cs.Sel = uNewCS & X86_SEL_MASK_OFF_RPL;
1502 pCtx->cs.Sel |= pIemCpu->uCpl;
1503 pCtx->cs.ValidSel = pCtx->cs.Sel;
1504 pCtx->cs.fFlags = CPUMSELREG_FLAGS_VALID;
1505 pCtx->cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCS.Legacy);
1506 pCtx->cs.u32Limit = cbLimit;
1507 pCtx->cs.u64Base = u64Base;
1508 }
1509 else
1510 {
1511 /* Same privilege. */
1512 /** @todo: This is very similar to regular far calls; merge! */
1513
1514 /* Check stack first - may #SS(0). */
1515 /** @todo check how gate size affects pushing of CS! Does callf 16:32 in
1516 * 16-bit code cause a two or four byte CS to be pushed? */
1517 rcStrict = iemMemStackPushBeginSpecial(pIemCpu,
1518 IEM_IS_LONG_MODE(pIemCpu) ? 8+8
1519 : pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_386_CALL_GATE ? 4+4 : 2+2,
1520 &uPtrRet.pv, &uNewRsp);
1521 if (rcStrict != VINF_SUCCESS)
1522 return rcStrict;
1523
1524 /* Chop the high bits off if 16-bit gate (Intel says so). */
1525 if (pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_286_CALL_GATE)
1526 uNewRip = (uint16_t)uNewRip;
1527
1528 /* Limit / canonical check. */
1529 cbLimit = X86DESC_LIMIT_G(&DescCS.Legacy);
1530 if (!IEM_IS_LONG_MODE(pIemCpu))
1531 {
1532 if (uNewRip > cbLimit)
1533 {
1534 Log(("BranchCallGate %04x:%08RX64 -> out of bounds (%#x)\n", uNewCS, uNewRip, cbLimit));
1535 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, 0);
1536 }
1537 u64Base = X86DESC_BASE(&DescCS.Legacy);
1538 }
1539 else
1540 {
1541 if (!IEM_IS_CANONICAL(uNewRip))
1542 {
1543 Log(("BranchCallGate call %04x:%016RX64 - not canonical -> #GP\n", uNewCS, uNewRip));
1544 return iemRaiseNotCanonical(pIemCpu);
1545 }
1546 u64Base = 0;
1547 }
1548
1549 /*
1550 * Now set the accessed bit before
1551 * writing the return address to the stack and committing the result into
1552 * CS, CSHID and RIP.
1553 */
1554 /** @todo Testcase: Need to check WHEN exactly the accessed bit is set. */
1555 if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
1556 {
1557 rcStrict = iemMemMarkSelDescAccessed(pIemCpu, uNewCS);
1558 if (rcStrict != VINF_SUCCESS)
1559 return rcStrict;
1560 /** @todo check what VT-x and AMD-V does. */
1561 DescCS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
1562 }
1563
1564 /* stack */
1565 if (!IEM_IS_LONG_MODE(pIemCpu))
1566 {
1567 if (pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_386_CALL_GATE)
1568 {
1569 uPtrRet.pu32[0] = pCtx->eip + cbInstr;
1570 uPtrRet.pu32[1] = pCtx->cs.Sel; /** @todo Testcase: What is written to the high word when pushing CS? */
1571 }
1572 else
1573 {
1574 Assert(pDesc->Legacy.Gate.u4Type == X86_SEL_TYPE_SYS_286_CALL_GATE);
1575 uPtrRet.pu16[0] = pCtx->ip + cbInstr;
1576 uPtrRet.pu16[1] = pCtx->cs.Sel;
1577 }
1578 }
1579 else
1580 {
1581 Assert(pDesc->Legacy.Gate.u4Type == AMD64_SEL_TYPE_SYS_CALL_GATE);
1582 uPtrRet.pu64[0] = pCtx->rip + cbInstr;
1583 uPtrRet.pu64[1] = pCtx->cs.Sel; /** @todo Testcase: What is written to the high words when pushing CS? */
1584 }
1585
1586 rcStrict = iemMemStackPushCommitSpecial(pIemCpu, uPtrRet.pv, uNewRsp);
1587 if (rcStrict != VINF_SUCCESS)
1588 return rcStrict;
1589
1590 /* commit */
1591 pCtx->rip = uNewRip;
1592 pCtx->cs.Sel = uNewCS & X86_SEL_MASK_OFF_RPL;
1593 pCtx->cs.Sel |= pIemCpu->uCpl;
1594 pCtx->cs.ValidSel = pCtx->cs.Sel;
1595 pCtx->cs.fFlags = CPUMSELREG_FLAGS_VALID;
1596 pCtx->cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCS.Legacy);
1597 pCtx->cs.u32Limit = cbLimit;
1598 pCtx->cs.u64Base = u64Base;
1599 }
1600 }
1601 pCtx->eflags.Bits.u1RF = 0;
1602 return VINF_SUCCESS;
1603#endif
1604}
1605
1606
1607/**
1608 * Implements far jumps and calls thru system selectors.
1609 *
1610 * @param uSel The selector.
1611 * @param enmBranch The kind of branching we're performing.
1612 * @param enmEffOpSize The effective operand size.
1613 * @param pDesc The descriptor corresponding to @a uSel.
1614 */
1615IEM_CIMPL_DEF_4(iemCImpl_BranchSysSel, uint16_t, uSel, IEMBRANCH, enmBranch, IEMMODE, enmEffOpSize, PIEMSELDESC, pDesc)
1616{
1617 Assert(enmBranch == IEMBRANCH_JUMP || enmBranch == IEMBRANCH_CALL);
1618 Assert((uSel & X86_SEL_MASK_OFF_RPL));
1619
1620 if (IEM_IS_LONG_MODE(pIemCpu))
1621 switch (pDesc->Legacy.Gen.u4Type)
1622 {
1623 case AMD64_SEL_TYPE_SYS_CALL_GATE:
1624 return IEM_CIMPL_CALL_4(iemCImpl_BranchCallGate, uSel, enmBranch, enmEffOpSize, pDesc);
1625
1626 default:
1627 case AMD64_SEL_TYPE_SYS_LDT:
1628 case AMD64_SEL_TYPE_SYS_TSS_BUSY:
1629 case AMD64_SEL_TYPE_SYS_TSS_AVAIL:
1630 case AMD64_SEL_TYPE_SYS_TRAP_GATE:
1631 case AMD64_SEL_TYPE_SYS_INT_GATE:
1632 Log(("branch %04x -> wrong sys selector (64-bit): %d\n", uSel, pDesc->Legacy.Gen.u4Type));
1633 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel);
1634 }
1635
1636 switch (pDesc->Legacy.Gen.u4Type)
1637 {
1638 case X86_SEL_TYPE_SYS_286_CALL_GATE:
1639 case X86_SEL_TYPE_SYS_386_CALL_GATE:
1640 return IEM_CIMPL_CALL_4(iemCImpl_BranchCallGate, uSel, enmBranch, enmEffOpSize, pDesc);
1641
1642 case X86_SEL_TYPE_SYS_TASK_GATE:
1643 return IEM_CIMPL_CALL_4(iemCImpl_BranchTaskGate, uSel, enmBranch, enmEffOpSize, pDesc);
1644
1645 case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
1646 case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
1647 return IEM_CIMPL_CALL_4(iemCImpl_BranchTaskSegment, uSel, enmBranch, enmEffOpSize, pDesc);
1648
1649 case X86_SEL_TYPE_SYS_286_TSS_BUSY:
1650 Log(("branch %04x -> busy 286 TSS\n", uSel));
1651 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel);
1652
1653 case X86_SEL_TYPE_SYS_386_TSS_BUSY:
1654 Log(("branch %04x -> busy 386 TSS\n", uSel));
1655 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel);
1656
1657 default:
1658 case X86_SEL_TYPE_SYS_LDT:
1659 case X86_SEL_TYPE_SYS_286_INT_GATE:
1660 case X86_SEL_TYPE_SYS_286_TRAP_GATE:
1661 case X86_SEL_TYPE_SYS_386_INT_GATE:
1662 case X86_SEL_TYPE_SYS_386_TRAP_GATE:
1663 Log(("branch %04x -> wrong sys selector: %d\n", uSel, pDesc->Legacy.Gen.u4Type));
1664 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel);
1665 }
1666}
1667
1668
1669/**
1670 * Implements far jumps.
1671 *
1672 * @param uSel The selector.
1673 * @param offSeg The segment offset.
1674 * @param enmEffOpSize The effective operand size.
1675 */
1676IEM_CIMPL_DEF_3(iemCImpl_FarJmp, uint16_t, uSel, uint64_t, offSeg, IEMMODE, enmEffOpSize)
1677{
1678 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
1679 NOREF(cbInstr);
1680 Assert(offSeg <= UINT32_MAX);
1681
1682 /*
1683 * Real mode and V8086 mode are easy. The only snag seems to be that
1684 * CS.limit doesn't change and the limit check is done against the current
1685 * limit.
1686 */
1687 if ( pIemCpu->enmCpuMode == IEMMODE_16BIT
1688 && IEM_IS_REAL_OR_V86_MODE(pIemCpu))
1689 {
1690 if (offSeg > pCtx->cs.u32Limit)
1691 return iemRaiseGeneralProtectionFault0(pIemCpu);
1692
1693 if (enmEffOpSize == IEMMODE_16BIT) /** @todo WRONG, must pass this. */
1694 pCtx->rip = offSeg;
1695 else
1696 pCtx->rip = offSeg & UINT16_MAX;
1697 pCtx->cs.Sel = uSel;
1698 pCtx->cs.ValidSel = uSel;
1699 pCtx->cs.fFlags = CPUMSELREG_FLAGS_VALID;
1700 pCtx->cs.u64Base = (uint32_t)uSel << 4;
1701 pCtx->eflags.Bits.u1RF = 0;
1702 return VINF_SUCCESS;
1703 }
1704
1705 /*
1706 * Protected mode. Need to parse the specified descriptor...
1707 */
1708 if (!(uSel & X86_SEL_MASK_OFF_RPL))
1709 {
1710 Log(("jmpf %04x:%08RX64 -> invalid selector, #GP(0)\n", uSel, offSeg));
1711 return iemRaiseGeneralProtectionFault0(pIemCpu);
1712 }
1713
1714 /* Fetch the descriptor. */
1715 IEMSELDESC Desc;
1716 VBOXSTRICTRC rcStrict = iemMemFetchSelDesc(pIemCpu, &Desc, uSel, X86_XCPT_GP);
1717 if (rcStrict != VINF_SUCCESS)
1718 return rcStrict;
1719
1720 /* Is it there? */
1721 if (!Desc.Legacy.Gen.u1Present) /** @todo this is probably checked too early. Testcase! */
1722 {
1723 Log(("jmpf %04x:%08RX64 -> segment not present\n", uSel, offSeg));
1724 return iemRaiseSelectorNotPresentBySelector(pIemCpu, uSel);
1725 }
1726
1727 /*
1728 * Deal with it according to its type. We do the standard code selectors
1729 * here and dispatch the system selectors to worker functions.
1730 */
1731 if (!Desc.Legacy.Gen.u1DescType)
1732 return IEM_CIMPL_CALL_4(iemCImpl_BranchSysSel, uSel, IEMBRANCH_JUMP, enmEffOpSize, &Desc);
1733
1734 /* Only code segments. */
1735 if (!(Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE))
1736 {
1737 Log(("jmpf %04x:%08RX64 -> not a code selector (u4Type=%#x).\n", uSel, offSeg, Desc.Legacy.Gen.u4Type));
1738 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel);
1739 }
1740
1741 /* L vs D. */
1742 if ( Desc.Legacy.Gen.u1Long
1743 && Desc.Legacy.Gen.u1DefBig
1744 && IEM_IS_LONG_MODE(pIemCpu))
1745 {
1746 Log(("jmpf %04x:%08RX64 -> both L and D are set.\n", uSel, offSeg));
1747 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel);
1748 }
1749
1750 /* DPL/RPL/CPL check, where conforming segments makes a difference. */
1751 if (Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF)
1752 {
1753 if (pIemCpu->uCpl < Desc.Legacy.Gen.u2Dpl)
1754 {
1755 Log(("jmpf %04x:%08RX64 -> DPL violation (conforming); DPL=%d CPL=%u\n",
1756 uSel, offSeg, Desc.Legacy.Gen.u2Dpl, pIemCpu->uCpl));
1757 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel);
1758 }
1759 }
1760 else
1761 {
1762 if (pIemCpu->uCpl != Desc.Legacy.Gen.u2Dpl)
1763 {
1764 Log(("jmpf %04x:%08RX64 -> CPL != DPL; DPL=%d CPL=%u\n", uSel, offSeg, Desc.Legacy.Gen.u2Dpl, pIemCpu->uCpl));
1765 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel);
1766 }
1767 if ((uSel & X86_SEL_RPL) > pIemCpu->uCpl)
1768 {
1769 Log(("jmpf %04x:%08RX64 -> RPL > DPL; RPL=%d CPL=%u\n", uSel, offSeg, (uSel & X86_SEL_RPL), pIemCpu->uCpl));
1770 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel);
1771 }
1772 }
1773
1774 /* Chop the high bits if 16-bit (Intel says so). */
1775 if (enmEffOpSize == IEMMODE_16BIT)
1776 offSeg &= UINT16_MAX;
1777
1778 /* Limit check. (Should alternatively check for non-canonical addresses
1779 here, but that is ruled out by offSeg being 32-bit, right?) */
1780 uint64_t u64Base;
1781 uint32_t cbLimit = X86DESC_LIMIT_G(&Desc.Legacy);
1782 if (pIemCpu->enmCpuMode == IEMMODE_64BIT)
1783 u64Base = 0;
1784 else
1785 {
1786 if (offSeg > cbLimit)
1787 {
1788 Log(("jmpf %04x:%08RX64 -> out of bounds (%#x)\n", uSel, offSeg, cbLimit));
1789 /** @todo: Intel says this is #GP(0)! */
1790 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel);
1791 }
1792 u64Base = X86DESC_BASE(&Desc.Legacy);
1793 }
1794
1795 /*
1796 * Ok, everything checked out fine. Now set the accessed bit before
1797 * committing the result into CS, CSHID and RIP.
1798 */
1799 if (!(Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
1800 {
1801 rcStrict = iemMemMarkSelDescAccessed(pIemCpu, uSel);
1802 if (rcStrict != VINF_SUCCESS)
1803 return rcStrict;
1804 /** @todo check what VT-x and AMD-V does. */
1805 Desc.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
1806 }
1807
1808 /* commit */
1809 pCtx->rip = offSeg;
1810 pCtx->cs.Sel = uSel & X86_SEL_MASK_OFF_RPL;
1811 pCtx->cs.Sel |= pIemCpu->uCpl; /** @todo is this right for conforming segs? or in general? */
1812 pCtx->cs.ValidSel = pCtx->cs.Sel;
1813 pCtx->cs.fFlags = CPUMSELREG_FLAGS_VALID;
1814 pCtx->cs.Attr.u = X86DESC_GET_HID_ATTR(&Desc.Legacy);
1815 pCtx->cs.u32Limit = cbLimit;
1816 pCtx->cs.u64Base = u64Base;
1817 pCtx->eflags.Bits.u1RF = 0;
1818 /** @todo check if the hidden bits are loaded correctly for 64-bit
1819 * mode. */
1820 return VINF_SUCCESS;
1821}
1822
1823
1824/**
1825 * Implements far calls.
1826 *
1827 * This very similar to iemCImpl_FarJmp.
1828 *
1829 * @param uSel The selector.
1830 * @param offSeg The segment offset.
1831 * @param enmEffOpSize The operand size (in case we need it).
1832 */
1833IEM_CIMPL_DEF_3(iemCImpl_callf, uint16_t, uSel, uint64_t, offSeg, IEMMODE, enmEffOpSize)
1834{
1835 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
1836 VBOXSTRICTRC rcStrict;
1837 uint64_t uNewRsp;
1838 RTPTRUNION uPtrRet;
1839
1840 /*
1841 * Real mode and V8086 mode are easy. The only snag seems to be that
1842 * CS.limit doesn't change and the limit check is done against the current
1843 * limit.
1844 */
1845 if ( pIemCpu->enmCpuMode == IEMMODE_16BIT
1846 && IEM_IS_REAL_OR_V86_MODE(pIemCpu))
1847 {
1848 Assert(enmEffOpSize == IEMMODE_16BIT || enmEffOpSize == IEMMODE_32BIT);
1849
1850 /* Check stack first - may #SS(0). */
1851 rcStrict = iemMemStackPushBeginSpecial(pIemCpu, enmEffOpSize == IEMMODE_32BIT ? 6 : 4,
1852 &uPtrRet.pv, &uNewRsp);
1853 if (rcStrict != VINF_SUCCESS)
1854 return rcStrict;
1855
1856 /* Check the target address range. */
1857 if (offSeg > UINT32_MAX)
1858 return iemRaiseGeneralProtectionFault0(pIemCpu);
1859
1860 /* Everything is fine, push the return address. */
1861 if (enmEffOpSize == IEMMODE_16BIT)
1862 {
1863 uPtrRet.pu16[0] = pCtx->ip + cbInstr;
1864 uPtrRet.pu16[1] = pCtx->cs.Sel;
1865 }
1866 else
1867 {
1868 uPtrRet.pu32[0] = pCtx->eip + cbInstr;
1869 uPtrRet.pu16[3] = pCtx->cs.Sel;
1870 }
1871 rcStrict = iemMemStackPushCommitSpecial(pIemCpu, uPtrRet.pv, uNewRsp);
1872 if (rcStrict != VINF_SUCCESS)
1873 return rcStrict;
1874
1875 /* Branch. */
1876 pCtx->rip = offSeg;
1877 pCtx->cs.Sel = uSel;
1878 pCtx->cs.ValidSel = uSel;
1879 pCtx->cs.fFlags = CPUMSELREG_FLAGS_VALID;
1880 pCtx->cs.u64Base = (uint32_t)uSel << 4;
1881 pCtx->eflags.Bits.u1RF = 0;
1882 return VINF_SUCCESS;
1883 }
1884
1885 /*
1886 * Protected mode. Need to parse the specified descriptor...
1887 */
1888 if (!(uSel & X86_SEL_MASK_OFF_RPL))
1889 {
1890 Log(("callf %04x:%08RX64 -> invalid selector, #GP(0)\n", uSel, offSeg));
1891 return iemRaiseGeneralProtectionFault0(pIemCpu);
1892 }
1893
1894 /* Fetch the descriptor. */
1895 IEMSELDESC Desc;
1896 rcStrict = iemMemFetchSelDesc(pIemCpu, &Desc, uSel, X86_XCPT_GP);
1897 if (rcStrict != VINF_SUCCESS)
1898 return rcStrict;
1899
1900 /*
1901 * Deal with it according to its type. We do the standard code selectors
1902 * here and dispatch the system selectors to worker functions.
1903 */
1904 if (!Desc.Legacy.Gen.u1DescType)
1905 return IEM_CIMPL_CALL_4(iemCImpl_BranchSysSel, uSel, IEMBRANCH_CALL, enmEffOpSize, &Desc);
1906
1907 /* Only code segments. */
1908 if (!(Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE))
1909 {
1910 Log(("callf %04x:%08RX64 -> not a code selector (u4Type=%#x).\n", uSel, offSeg, Desc.Legacy.Gen.u4Type));
1911 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel);
1912 }
1913
1914 /* L vs D. */
1915 if ( Desc.Legacy.Gen.u1Long
1916 && Desc.Legacy.Gen.u1DefBig
1917 && IEM_IS_LONG_MODE(pIemCpu))
1918 {
1919 Log(("callf %04x:%08RX64 -> both L and D are set.\n", uSel, offSeg));
1920 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel);
1921 }
1922
1923 /* DPL/RPL/CPL check, where conforming segments makes a difference. */
1924 if (Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF)
1925 {
1926 if (pIemCpu->uCpl < Desc.Legacy.Gen.u2Dpl)
1927 {
1928 Log(("callf %04x:%08RX64 -> DPL violation (conforming); DPL=%d CPL=%u\n",
1929 uSel, offSeg, Desc.Legacy.Gen.u2Dpl, pIemCpu->uCpl));
1930 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel);
1931 }
1932 }
1933 else
1934 {
1935 if (pIemCpu->uCpl != Desc.Legacy.Gen.u2Dpl)
1936 {
1937 Log(("callf %04x:%08RX64 -> CPL != DPL; DPL=%d CPL=%u\n", uSel, offSeg, Desc.Legacy.Gen.u2Dpl, pIemCpu->uCpl));
1938 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel);
1939 }
1940 if ((uSel & X86_SEL_RPL) > pIemCpu->uCpl)
1941 {
1942 Log(("callf %04x:%08RX64 -> RPL > DPL; RPL=%d CPL=%u\n", uSel, offSeg, (uSel & X86_SEL_RPL), pIemCpu->uCpl));
1943 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel);
1944 }
1945 }
1946
1947 /* Is it there? */
1948 if (!Desc.Legacy.Gen.u1Present)
1949 {
1950 Log(("callf %04x:%08RX64 -> segment not present\n", uSel, offSeg));
1951 return iemRaiseSelectorNotPresentBySelector(pIemCpu, uSel);
1952 }
1953
1954 /* Check stack first - may #SS(0). */
1955 /** @todo check how operand prefix affects pushing of CS! Does callf 16:32 in
1956 * 16-bit code cause a two or four byte CS to be pushed? */
1957 rcStrict = iemMemStackPushBeginSpecial(pIemCpu,
1958 enmEffOpSize == IEMMODE_64BIT ? 8+8
1959 : enmEffOpSize == IEMMODE_32BIT ? 4+4 : 2+2,
1960 &uPtrRet.pv, &uNewRsp);
1961 if (rcStrict != VINF_SUCCESS)
1962 return rcStrict;
1963
1964 /* Chop the high bits if 16-bit (Intel says so). */
1965 if (enmEffOpSize == IEMMODE_16BIT)
1966 offSeg &= UINT16_MAX;
1967
1968 /* Limit / canonical check. */
1969 uint64_t u64Base;
1970 uint32_t cbLimit = X86DESC_LIMIT_G(&Desc.Legacy);
1971 if (pIemCpu->enmCpuMode == IEMMODE_64BIT)
1972 {
1973 if (!IEM_IS_CANONICAL(offSeg))
1974 {
1975 Log(("callf %04x:%016RX64 - not canonical -> #GP\n", uSel, offSeg));
1976 return iemRaiseNotCanonical(pIemCpu);
1977 }
1978 u64Base = 0;
1979 }
1980 else
1981 {
1982 if (offSeg > cbLimit)
1983 {
1984 Log(("callf %04x:%08RX64 -> out of bounds (%#x)\n", uSel, offSeg, cbLimit));
1985 /** @todo: Intel says this is #GP(0)! */
1986 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel);
1987 }
1988 u64Base = X86DESC_BASE(&Desc.Legacy);
1989 }
1990
1991 /*
1992 * Now set the accessed bit before
1993 * writing the return address to the stack and committing the result into
1994 * CS, CSHID and RIP.
1995 */
1996 /** @todo Testcase: Need to check WHEN exactly the accessed bit is set. */
1997 if (!(Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
1998 {
1999 rcStrict = iemMemMarkSelDescAccessed(pIemCpu, uSel);
2000 if (rcStrict != VINF_SUCCESS)
2001 return rcStrict;
2002 /** @todo check what VT-x and AMD-V does. */
2003 Desc.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
2004 }
2005
2006 /* stack */
2007 if (enmEffOpSize == IEMMODE_16BIT)
2008 {
2009 uPtrRet.pu16[0] = pCtx->ip + cbInstr;
2010 uPtrRet.pu16[1] = pCtx->cs.Sel;
2011 }
2012 else if (enmEffOpSize == IEMMODE_32BIT)
2013 {
2014 uPtrRet.pu32[0] = pCtx->eip + cbInstr;
2015 uPtrRet.pu32[1] = pCtx->cs.Sel; /** @todo Testcase: What is written to the high word when callf is pushing CS? */
2016 }
2017 else
2018 {
2019 uPtrRet.pu64[0] = pCtx->rip + cbInstr;
2020 uPtrRet.pu64[1] = pCtx->cs.Sel; /** @todo Testcase: What is written to the high words when callf is pushing CS? */
2021 }
2022 rcStrict = iemMemStackPushCommitSpecial(pIemCpu, uPtrRet.pv, uNewRsp);
2023 if (rcStrict != VINF_SUCCESS)
2024 return rcStrict;
2025
2026 /* commit */
2027 pCtx->rip = offSeg;
2028 pCtx->cs.Sel = uSel & X86_SEL_MASK_OFF_RPL;
2029 pCtx->cs.Sel |= pIemCpu->uCpl;
2030 pCtx->cs.ValidSel = pCtx->cs.Sel;
2031 pCtx->cs.fFlags = CPUMSELREG_FLAGS_VALID;
2032 pCtx->cs.Attr.u = X86DESC_GET_HID_ATTR(&Desc.Legacy);
2033 pCtx->cs.u32Limit = cbLimit;
2034 pCtx->cs.u64Base = u64Base;
2035 pCtx->eflags.Bits.u1RF = 0;
2036 /** @todo check if the hidden bits are loaded correctly for 64-bit
2037 * mode. */
2038 return VINF_SUCCESS;
2039}
2040
2041
2042/**
2043 * Implements retf.
2044 *
2045 * @param enmEffOpSize The effective operand size.
2046 * @param cbPop The amount of arguments to pop from the stack
2047 * (bytes).
2048 */
2049IEM_CIMPL_DEF_2(iemCImpl_retf, IEMMODE, enmEffOpSize, uint16_t, cbPop)
2050{
2051 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
2052 VBOXSTRICTRC rcStrict;
2053 RTCPTRUNION uPtrFrame;
2054 uint64_t uNewRsp;
2055 uint64_t uNewRip;
2056 uint16_t uNewCs;
2057 NOREF(cbInstr);
2058
2059 /*
2060 * Read the stack values first.
2061 */
2062 uint32_t cbRetPtr = enmEffOpSize == IEMMODE_16BIT ? 2+2
2063 : enmEffOpSize == IEMMODE_32BIT ? 4+4 : 8+8;
2064 rcStrict = iemMemStackPopBeginSpecial(pIemCpu, cbRetPtr, &uPtrFrame.pv, &uNewRsp);
2065 if (rcStrict != VINF_SUCCESS)
2066 return rcStrict;
2067 if (enmEffOpSize == IEMMODE_16BIT)
2068 {
2069 uNewRip = uPtrFrame.pu16[0];
2070 uNewCs = uPtrFrame.pu16[1];
2071 }
2072 else if (enmEffOpSize == IEMMODE_32BIT)
2073 {
2074 uNewRip = uPtrFrame.pu32[0];
2075 uNewCs = uPtrFrame.pu16[2];
2076 }
2077 else
2078 {
2079 uNewRip = uPtrFrame.pu64[0];
2080 uNewCs = uPtrFrame.pu16[4];
2081 }
2082
2083 /*
2084 * Real mode and V8086 mode are easy.
2085 */
2086 if ( pIemCpu->enmCpuMode == IEMMODE_16BIT
2087 && IEM_IS_REAL_OR_V86_MODE(pIemCpu))
2088 {
2089 Assert(enmEffOpSize == IEMMODE_32BIT || enmEffOpSize == IEMMODE_16BIT);
2090 /** @todo check how this is supposed to work if sp=0xfffe. */
2091
2092 /* Check the limit of the new EIP. */
2093 /** @todo Intel pseudo code only does the limit check for 16-bit
2094 * operands, AMD does not make any distinction. What is right? */
2095 if (uNewRip > pCtx->cs.u32Limit)
2096 return iemRaiseSelectorBounds(pIemCpu, X86_SREG_CS, IEM_ACCESS_INSTRUCTION);
2097
2098 /* commit the operation. */
2099 rcStrict = iemMemStackPopCommitSpecial(pIemCpu, uPtrFrame.pv, uNewRsp);
2100 if (rcStrict != VINF_SUCCESS)
2101 return rcStrict;
2102 pCtx->rip = uNewRip;
2103 pCtx->cs.Sel = uNewCs;
2104 pCtx->cs.ValidSel = uNewCs;
2105 pCtx->cs.fFlags = CPUMSELREG_FLAGS_VALID;
2106 pCtx->cs.u64Base = (uint32_t)uNewCs << 4;
2107 pCtx->eflags.Bits.u1RF = 0;
2108 /** @todo do we load attribs and limit as well? */
2109 if (cbPop)
2110 iemRegAddToRsp(pIemCpu, pCtx, cbPop);
2111 return VINF_SUCCESS;
2112 }
2113
2114 /*
2115 * Protected mode is complicated, of course.
2116 */
2117 if (!(uNewCs & X86_SEL_MASK_OFF_RPL))
2118 {
2119 Log(("retf %04x:%08RX64 -> invalid selector, #GP(0)\n", uNewCs, uNewRip));
2120 return iemRaiseGeneralProtectionFault0(pIemCpu);
2121 }
2122
2123 /* Fetch the descriptor. */
2124 IEMSELDESC DescCs;
2125 rcStrict = iemMemFetchSelDesc(pIemCpu, &DescCs, uNewCs, X86_XCPT_GP);
2126 if (rcStrict != VINF_SUCCESS)
2127 return rcStrict;
2128
2129 /* Can only return to a code selector. */
2130 if ( !DescCs.Legacy.Gen.u1DescType
2131 || !(DescCs.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE) )
2132 {
2133 Log(("retf %04x:%08RX64 -> not a code selector (u1DescType=%u u4Type=%#x).\n",
2134 uNewCs, uNewRip, DescCs.Legacy.Gen.u1DescType, DescCs.Legacy.Gen.u4Type));
2135 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewCs);
2136 }
2137
2138 /* L vs D. */
2139 if ( DescCs.Legacy.Gen.u1Long /** @todo Testcase: far return to a selector with both L and D set. */
2140 && DescCs.Legacy.Gen.u1DefBig
2141 && IEM_IS_LONG_MODE(pIemCpu))
2142 {
2143 Log(("retf %04x:%08RX64 -> both L & D set.\n", uNewCs, uNewRip));
2144 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewCs);
2145 }
2146
2147 /* DPL/RPL/CPL checks. */
2148 if ((uNewCs & X86_SEL_RPL) < pIemCpu->uCpl)
2149 {
2150 Log(("retf %04x:%08RX64 -> RPL < CPL(%d).\n", uNewCs, uNewRip, pIemCpu->uCpl));
2151 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewCs);
2152 }
2153
2154 if (DescCs.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF)
2155 {
2156 if ((uNewCs & X86_SEL_RPL) < DescCs.Legacy.Gen.u2Dpl)
2157 {
2158 Log(("retf %04x:%08RX64 -> DPL violation (conforming); DPL=%u RPL=%u\n",
2159 uNewCs, uNewRip, DescCs.Legacy.Gen.u2Dpl, (uNewCs & X86_SEL_RPL)));
2160 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewCs);
2161 }
2162 }
2163 else
2164 {
2165 if ((uNewCs & X86_SEL_RPL) != DescCs.Legacy.Gen.u2Dpl)
2166 {
2167 Log(("retf %04x:%08RX64 -> RPL != DPL; DPL=%u RPL=%u\n",
2168 uNewCs, uNewRip, DescCs.Legacy.Gen.u2Dpl, (uNewCs & X86_SEL_RPL)));
2169 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewCs);
2170 }
2171 }
2172
2173 /* Is it there? */
2174 if (!DescCs.Legacy.Gen.u1Present)
2175 {
2176 Log(("retf %04x:%08RX64 -> segment not present\n", uNewCs, uNewRip));
2177 return iemRaiseSelectorNotPresentBySelector(pIemCpu, uNewCs);
2178 }
2179
2180 /*
2181 * Return to outer privilege? (We'll typically have entered via a call gate.)
2182 */
2183 if ((uNewCs & X86_SEL_RPL) != pIemCpu->uCpl)
2184 {
2185 /* Read the outer stack pointer stored *after* the parameters. */
2186 RTCPTRUNION uPtrStack;
2187 rcStrict = iemMemStackPopContinueSpecial(pIemCpu, cbPop + cbRetPtr, &uPtrStack.pv, &uNewRsp);
2188 if (rcStrict != VINF_SUCCESS)
2189 return rcStrict;
2190
2191 uPtrStack.pu8 += cbPop; /* Skip the parameters. */
2192
2193 uint16_t uNewOuterSs;
2194 uint64_t uNewOuterRsp;
2195 if (enmEffOpSize == IEMMODE_16BIT)
2196 {
2197 uNewOuterRsp = uPtrStack.pu16[0];
2198 uNewOuterSs = uPtrStack.pu16[1];
2199 }
2200 else if (enmEffOpSize == IEMMODE_32BIT)
2201 {
2202 uNewOuterRsp = uPtrStack.pu32[0];
2203 uNewOuterSs = uPtrStack.pu16[2];
2204 }
2205 else
2206 {
2207 uNewOuterRsp = uPtrStack.pu64[0];
2208 uNewOuterSs = uPtrStack.pu16[4];
2209 }
2210
2211 /* Check for NULL stack selector (invalid in ring-3 and non-long mode)
2212 and read the selector. */
2213 IEMSELDESC DescSs;
2214 if (!(uNewOuterSs & X86_SEL_MASK_OFF_RPL))
2215 {
2216 if ( !DescCs.Legacy.Gen.u1Long
2217 || (uNewOuterSs & X86_SEL_RPL) == 3)
2218 {
2219 Log(("retf %04x:%08RX64 %04x:%08RX64 -> invalid stack selector, #GP\n",
2220 uNewCs, uNewRip, uNewOuterSs, uNewOuterRsp));
2221 return iemRaiseGeneralProtectionFault0(pIemCpu);
2222 }
2223 /** @todo Testcase: Return far to ring-1 or ring-2 with SS=0. */
2224 iemMemFakeStackSelDesc(&DescSs, (uNewOuterSs & X86_SEL_RPL));
2225 }
2226 else
2227 {
2228 /* Fetch the descriptor for the new stack segment. */
2229 rcStrict = iemMemFetchSelDesc(pIemCpu, &DescSs, uNewOuterSs, X86_XCPT_GP);
2230 if (rcStrict != VINF_SUCCESS)
2231 return rcStrict;
2232 }
2233
2234 /* Check that RPL of stack and code selectors match. */
2235 if ((uNewCs & X86_SEL_RPL) != (uNewOuterSs & X86_SEL_RPL))
2236 {
2237 Log(("retf %04x:%08RX64 %04x:%08RX64 - SS.RPL != CS.RPL -> #GP(SS)\n", uNewCs, uNewRip, uNewOuterSs, uNewOuterRsp));
2238 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewOuterSs);
2239 }
2240
2241 /* Must be a writable data segment. */
2242 if ( !DescSs.Legacy.Gen.u1DescType
2243 || (DescSs.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE)
2244 || !(DescSs.Legacy.Gen.u4Type & X86_SEL_TYPE_WRITE) )
2245 {
2246 Log(("retf %04x:%08RX64 %04x:%08RX64 - SS not a writable data segment (u1DescType=%u u4Type=%#x) -> #GP(SS).\n",
2247 uNewCs, uNewRip, uNewOuterSs, uNewOuterRsp, DescSs.Legacy.Gen.u1DescType, DescSs.Legacy.Gen.u4Type));
2248 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewOuterSs);
2249 }
2250
2251 /* L vs D. (Not mentioned by intel.) */
2252 if ( DescSs.Legacy.Gen.u1Long /** @todo Testcase: far return to a stack selector with both L and D set. */
2253 && DescSs.Legacy.Gen.u1DefBig
2254 && IEM_IS_LONG_MODE(pIemCpu))
2255 {
2256 Log(("retf %04x:%08RX64 %04x:%08RX64 - SS has both L & D set -> #GP(SS).\n",
2257 uNewCs, uNewRip, uNewOuterSs, uNewOuterRsp));
2258 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewOuterSs);
2259 }
2260
2261 /* DPL/RPL/CPL checks. */
2262 if (DescSs.Legacy.Gen.u2Dpl != (uNewCs & X86_SEL_RPL))
2263 {
2264 Log(("retf %04x:%08RX64 %04x:%08RX64 - SS.DPL(%u) != CS.RPL (%u) -> #GP(SS).\n",
2265 uNewCs, uNewRip, uNewOuterSs, uNewOuterRsp, DescSs.Legacy.Gen.u2Dpl, uNewCs & X86_SEL_RPL));
2266 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewOuterSs);
2267 }
2268
2269 /* Is it there? */
2270 if (!DescSs.Legacy.Gen.u1Present)
2271 {
2272 Log(("retf %04x:%08RX64 %04x:%08RX64 - SS not present -> #NP(SS).\n", uNewCs, uNewRip, uNewOuterSs, uNewOuterRsp));
2273 return iemRaiseSelectorNotPresentBySelector(pIemCpu, uNewCs);
2274 }
2275
2276 /* Calc SS limit.*/
2277 uint32_t cbLimitSs = X86DESC_LIMIT_G(&DescSs.Legacy);
2278
2279 /* Is RIP canonical or within CS.limit? */
2280 uint64_t u64Base;
2281 uint32_t cbLimitCs = X86DESC_LIMIT_G(&DescCs.Legacy);
2282
2283 if (pIemCpu->enmCpuMode == IEMMODE_64BIT)
2284 {
2285 if (!IEM_IS_CANONICAL(uNewRip))
2286 {
2287 Log(("retf %04x:%08RX64 %04x:%08RX64 - not canonical -> #GP.\n", uNewCs, uNewRip, uNewOuterSs, uNewOuterRsp));
2288 return iemRaiseNotCanonical(pIemCpu);
2289 }
2290 u64Base = 0;
2291 }
2292 else
2293 {
2294 if (uNewRip > cbLimitCs)
2295 {
2296 Log(("retf %04x:%08RX64 %04x:%08RX64 - out of bounds (%#x)-> #GP(CS).\n",
2297 uNewCs, uNewRip, uNewOuterSs, uNewOuterRsp, cbLimitCs));
2298 /** @todo: Intel says this is #GP(0)! */
2299 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewCs);
2300 }
2301 u64Base = X86DESC_BASE(&DescCs.Legacy);
2302 }
2303
2304 /*
2305 * Now set the accessed bit before
2306 * writing the return address to the stack and committing the result into
2307 * CS, CSHID and RIP.
2308 */
2309 /** @todo Testcase: Need to check WHEN exactly the CS accessed bit is set. */
2310 if (!(DescCs.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
2311 {
2312 rcStrict = iemMemMarkSelDescAccessed(pIemCpu, uNewCs);
2313 if (rcStrict != VINF_SUCCESS)
2314 return rcStrict;
2315 /** @todo check what VT-x and AMD-V does. */
2316 DescCs.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
2317 }
2318 /** @todo Testcase: Need to check WHEN exactly the SS accessed bit is set. */
2319 if (!(DescSs.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
2320 {
2321 rcStrict = iemMemMarkSelDescAccessed(pIemCpu, uNewOuterSs);
2322 if (rcStrict != VINF_SUCCESS)
2323 return rcStrict;
2324 /** @todo check what VT-x and AMD-V does. */
2325 DescSs.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
2326 }
2327
2328 /* commit */
2329 rcStrict = iemMemStackPopCommitSpecial(pIemCpu, uPtrFrame.pv, uNewRsp);
2330 if (rcStrict != VINF_SUCCESS)
2331 return rcStrict;
2332 if (enmEffOpSize == IEMMODE_16BIT)
2333 pCtx->rip = uNewRip & UINT16_MAX; /** @todo Testcase: When exactly does this occur? With call it happens prior to the limit check according to Intel... */
2334 else
2335 pCtx->rip = uNewRip;
2336 pCtx->cs.Sel = uNewCs;
2337 pCtx->cs.ValidSel = uNewCs;
2338 pCtx->cs.fFlags = CPUMSELREG_FLAGS_VALID;
2339 pCtx->cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCs.Legacy);
2340 pCtx->cs.u32Limit = cbLimitCs;
2341 pCtx->cs.u64Base = u64Base;
2342 pCtx->rsp = uNewOuterRsp;
2343 pCtx->ss.Sel = uNewOuterSs;
2344 pCtx->ss.ValidSel = uNewOuterSs;
2345 pCtx->ss.fFlags = CPUMSELREG_FLAGS_VALID;
2346 pCtx->ss.Attr.u = X86DESC_GET_HID_ATTR(&DescSs.Legacy);
2347 pCtx->ss.u32Limit = cbLimitSs;
2348 if (pIemCpu->enmCpuMode == IEMMODE_64BIT)
2349 pCtx->ss.u64Base = 0;
2350 else
2351 pCtx->ss.u64Base = X86DESC_BASE(&DescSs.Legacy);
2352
2353 pIemCpu->uCpl = (uNewCs & X86_SEL_RPL);
2354 iemHlpAdjustSelectorForNewCpl(pIemCpu, uNewCs & X86_SEL_RPL, &pCtx->ds);
2355 iemHlpAdjustSelectorForNewCpl(pIemCpu, uNewCs & X86_SEL_RPL, &pCtx->es);
2356 iemHlpAdjustSelectorForNewCpl(pIemCpu, uNewCs & X86_SEL_RPL, &pCtx->fs);
2357 iemHlpAdjustSelectorForNewCpl(pIemCpu, uNewCs & X86_SEL_RPL, &pCtx->gs);
2358
2359 /** @todo check if the hidden bits are loaded correctly for 64-bit
2360 * mode. */
2361
2362 if (cbPop)
2363 iemRegAddToRsp(pIemCpu, pCtx, cbPop);
2364 pCtx->eflags.Bits.u1RF = 0;
2365
2366 /* Done! */
2367 }
2368 /*
2369 * Return to the same privilege level
2370 */
2371 else
2372 {
2373 /* Limit / canonical check. */
2374 uint64_t u64Base;
2375 uint32_t cbLimitCs = X86DESC_LIMIT_G(&DescCs.Legacy);
2376
2377 if (pIemCpu->enmCpuMode == IEMMODE_64BIT)
2378 {
2379 if (!IEM_IS_CANONICAL(uNewRip))
2380 {
2381 Log(("retf %04x:%08RX64 - not canonical -> #GP\n", uNewCs, uNewRip));
2382 return iemRaiseNotCanonical(pIemCpu);
2383 }
2384 u64Base = 0;
2385 }
2386 else
2387 {
2388 if (uNewRip > cbLimitCs)
2389 {
2390 Log(("retf %04x:%08RX64 -> out of bounds (%#x)\n", uNewCs, uNewRip, cbLimitCs));
2391 /** @todo: Intel says this is #GP(0)! */
2392 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewCs);
2393 }
2394 u64Base = X86DESC_BASE(&DescCs.Legacy);
2395 }
2396
2397 /*
2398 * Now set the accessed bit before
2399 * writing the return address to the stack and committing the result into
2400 * CS, CSHID and RIP.
2401 */
2402 /** @todo Testcase: Need to check WHEN exactly the accessed bit is set. */
2403 if (!(DescCs.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
2404 {
2405 rcStrict = iemMemMarkSelDescAccessed(pIemCpu, uNewCs);
2406 if (rcStrict != VINF_SUCCESS)
2407 return rcStrict;
2408 /** @todo check what VT-x and AMD-V does. */
2409 DescCs.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
2410 }
2411
2412 /* commit */
2413 rcStrict = iemMemStackPopCommitSpecial(pIemCpu, uPtrFrame.pv, uNewRsp);
2414 if (rcStrict != VINF_SUCCESS)
2415 return rcStrict;
2416 if (enmEffOpSize == IEMMODE_16BIT)
2417 pCtx->rip = uNewRip & UINT16_MAX; /** @todo Testcase: When exactly does this occur? With call it happens prior to the limit check according to Intel... */
2418 else
2419 pCtx->rip = uNewRip;
2420 pCtx->cs.Sel = uNewCs;
2421 pCtx->cs.ValidSel = uNewCs;
2422 pCtx->cs.fFlags = CPUMSELREG_FLAGS_VALID;
2423 pCtx->cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCs.Legacy);
2424 pCtx->cs.u32Limit = cbLimitCs;
2425 pCtx->cs.u64Base = u64Base;
2426 /** @todo check if the hidden bits are loaded correctly for 64-bit
2427 * mode. */
2428 if (cbPop)
2429 iemRegAddToRsp(pIemCpu, pCtx, cbPop);
2430 pCtx->eflags.Bits.u1RF = 0;
2431 }
2432 return VINF_SUCCESS;
2433}
2434
2435
2436/**
2437 * Implements retn.
2438 *
2439 * We're doing this in C because of the \#GP that might be raised if the popped
2440 * program counter is out of bounds.
2441 *
2442 * @param enmEffOpSize The effective operand size.
2443 * @param cbPop The amount of arguments to pop from the stack
2444 * (bytes).
2445 */
2446IEM_CIMPL_DEF_2(iemCImpl_retn, IEMMODE, enmEffOpSize, uint16_t, cbPop)
2447{
2448 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
2449 NOREF(cbInstr);
2450
2451 /* Fetch the RSP from the stack. */
2452 VBOXSTRICTRC rcStrict;
2453 RTUINT64U NewRip;
2454 RTUINT64U NewRsp;
2455 NewRsp.u = pCtx->rsp;
2456 switch (enmEffOpSize)
2457 {
2458 case IEMMODE_16BIT:
2459 NewRip.u = 0;
2460 rcStrict = iemMemStackPopU16Ex(pIemCpu, &NewRip.Words.w0, &NewRsp);
2461 break;
2462 case IEMMODE_32BIT:
2463 NewRip.u = 0;
2464 rcStrict = iemMemStackPopU32Ex(pIemCpu, &NewRip.DWords.dw0, &NewRsp);
2465 break;
2466 case IEMMODE_64BIT:
2467 rcStrict = iemMemStackPopU64Ex(pIemCpu, &NewRip.u, &NewRsp);
2468 break;
2469 IEM_NOT_REACHED_DEFAULT_CASE_RET();
2470 }
2471 if (rcStrict != VINF_SUCCESS)
2472 return rcStrict;
2473
2474 /* Check the new RSP before loading it. */
2475 /** @todo Should test this as the intel+amd pseudo code doesn't mention half
2476 * of it. The canonical test is performed here and for call. */
2477 if (enmEffOpSize != IEMMODE_64BIT)
2478 {
2479 if (NewRip.DWords.dw0 > pCtx->cs.u32Limit)
2480 {
2481 Log(("retn newrip=%llx - out of bounds (%x) -> #GP\n", NewRip.u, pCtx->cs.u32Limit));
2482 return iemRaiseSelectorBounds(pIemCpu, X86_SREG_CS, IEM_ACCESS_INSTRUCTION);
2483 }
2484 }
2485 else
2486 {
2487 if (!IEM_IS_CANONICAL(NewRip.u))
2488 {
2489 Log(("retn newrip=%llx - not canonical -> #GP\n", NewRip.u));
2490 return iemRaiseNotCanonical(pIemCpu);
2491 }
2492 }
2493
2494 /* Commit it. */
2495 pCtx->rip = NewRip.u;
2496 pCtx->rsp = NewRsp.u;
2497 if (cbPop)
2498 iemRegAddToRsp(pIemCpu, pCtx, cbPop);
2499 pCtx->eflags.Bits.u1RF = 0;
2500
2501 return VINF_SUCCESS;
2502}
2503
2504
2505/**
2506 * Implements enter.
2507 *
2508 * We're doing this in C because the instruction is insane, even for the
2509 * u8NestingLevel=0 case dealing with the stack is tedious.
2510 *
2511 * @param enmEffOpSize The effective operand size.
2512 */
2513IEM_CIMPL_DEF_3(iemCImpl_enter, IEMMODE, enmEffOpSize, uint16_t, cbFrame, uint8_t, cParameters)
2514{
2515 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
2516
2517 /* Push RBP, saving the old value in TmpRbp. */
2518 RTUINT64U NewRsp; NewRsp.u = pCtx->rsp;
2519 RTUINT64U TmpRbp; TmpRbp.u = pCtx->rbp;
2520 RTUINT64U NewRbp;
2521 VBOXSTRICTRC rcStrict;
2522 if (enmEffOpSize == IEMMODE_64BIT)
2523 {
2524 rcStrict = iemMemStackPushU64Ex(pIemCpu, TmpRbp.u, &NewRsp);
2525 NewRbp = NewRsp;
2526 }
2527 else if (enmEffOpSize == IEMMODE_32BIT)
2528 {
2529 rcStrict = iemMemStackPushU32Ex(pIemCpu, TmpRbp.DWords.dw0, &NewRsp);
2530 NewRbp = NewRsp;
2531 }
2532 else
2533 {
2534 rcStrict = iemMemStackPushU16Ex(pIemCpu, TmpRbp.Words.w0, &NewRsp);
2535 NewRbp = TmpRbp;
2536 NewRbp.Words.w0 = NewRsp.Words.w0;
2537 }
2538 if (rcStrict != VINF_SUCCESS)
2539 return rcStrict;
2540
2541 /* Copy the parameters (aka nesting levels by Intel). */
2542 cParameters &= 0x1f;
2543 if (cParameters > 0)
2544 {
2545 switch (enmEffOpSize)
2546 {
2547 case IEMMODE_16BIT:
2548 if (pCtx->ss.Attr.n.u1DefBig)
2549 TmpRbp.DWords.dw0 -= 2;
2550 else
2551 TmpRbp.Words.w0 -= 2;
2552 do
2553 {
2554 uint16_t u16Tmp;
2555 rcStrict = iemMemStackPopU16Ex(pIemCpu, &u16Tmp, &TmpRbp);
2556 if (rcStrict != VINF_SUCCESS)
2557 break;
2558 rcStrict = iemMemStackPushU16Ex(pIemCpu, u16Tmp, &NewRsp);
2559 } while (--cParameters > 0 && rcStrict == VINF_SUCCESS);
2560 break;
2561
2562 case IEMMODE_32BIT:
2563 if (pCtx->ss.Attr.n.u1DefBig)
2564 TmpRbp.DWords.dw0 -= 4;
2565 else
2566 TmpRbp.Words.w0 -= 4;
2567 do
2568 {
2569 uint32_t u32Tmp;
2570 rcStrict = iemMemStackPopU32Ex(pIemCpu, &u32Tmp, &TmpRbp);
2571 if (rcStrict != VINF_SUCCESS)
2572 break;
2573 rcStrict = iemMemStackPushU32Ex(pIemCpu, u32Tmp, &NewRsp);
2574 } while (--cParameters > 0 && rcStrict == VINF_SUCCESS);
2575 break;
2576
2577 case IEMMODE_64BIT:
2578 TmpRbp.u -= 8;
2579 do
2580 {
2581 uint64_t u64Tmp;
2582 rcStrict = iemMemStackPopU64Ex(pIemCpu, &u64Tmp, &TmpRbp);
2583 if (rcStrict != VINF_SUCCESS)
2584 break;
2585 rcStrict = iemMemStackPushU64Ex(pIemCpu, u64Tmp, &NewRsp);
2586 } while (--cParameters > 0 && rcStrict == VINF_SUCCESS);
2587 break;
2588
2589 IEM_NOT_REACHED_DEFAULT_CASE_RET();
2590 }
2591 if (rcStrict != VINF_SUCCESS)
2592 return VINF_SUCCESS;
2593
2594 /* Push the new RBP */
2595 if (enmEffOpSize == IEMMODE_64BIT)
2596 rcStrict = iemMemStackPushU64Ex(pIemCpu, NewRbp.u, &NewRsp);
2597 else if (enmEffOpSize == IEMMODE_32BIT)
2598 rcStrict = iemMemStackPushU32Ex(pIemCpu, NewRbp.DWords.dw0, &NewRsp);
2599 else
2600 rcStrict = iemMemStackPushU16Ex(pIemCpu, NewRbp.Words.w0, &NewRsp);
2601 if (rcStrict != VINF_SUCCESS)
2602 return rcStrict;
2603
2604 }
2605
2606 /* Recalc RSP. */
2607 iemRegSubFromRspEx(pIemCpu, pCtx, &NewRsp, cbFrame);
2608
2609 /** @todo Should probe write access at the new RSP according to AMD. */
2610
2611 /* Commit it. */
2612 pCtx->rbp = NewRbp.u;
2613 pCtx->rsp = NewRsp.u;
2614 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
2615
2616 return VINF_SUCCESS;
2617}
2618
2619
2620
2621/**
2622 * Implements leave.
2623 *
2624 * We're doing this in C because messing with the stack registers is annoying
2625 * since they depends on SS attributes.
2626 *
2627 * @param enmEffOpSize The effective operand size.
2628 */
2629IEM_CIMPL_DEF_1(iemCImpl_leave, IEMMODE, enmEffOpSize)
2630{
2631 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
2632
2633 /* Calculate the intermediate RSP from RBP and the stack attributes. */
2634 RTUINT64U NewRsp;
2635 if (pIemCpu->enmCpuMode == IEMMODE_64BIT)
2636 NewRsp.u = pCtx->rbp;
2637 else if (pCtx->ss.Attr.n.u1DefBig)
2638 NewRsp.u = pCtx->ebp;
2639 else
2640 {
2641 /** @todo Check that LEAVE actually preserve the high EBP bits. */
2642 NewRsp.u = pCtx->rsp;
2643 NewRsp.Words.w0 = pCtx->bp;
2644 }
2645
2646 /* Pop RBP according to the operand size. */
2647 VBOXSTRICTRC rcStrict;
2648 RTUINT64U NewRbp;
2649 switch (enmEffOpSize)
2650 {
2651 case IEMMODE_16BIT:
2652 NewRbp.u = pCtx->rbp;
2653 rcStrict = iemMemStackPopU16Ex(pIemCpu, &NewRbp.Words.w0, &NewRsp);
2654 break;
2655 case IEMMODE_32BIT:
2656 NewRbp.u = 0;
2657 rcStrict = iemMemStackPopU32Ex(pIemCpu, &NewRbp.DWords.dw0, &NewRsp);
2658 break;
2659 case IEMMODE_64BIT:
2660 rcStrict = iemMemStackPopU64Ex(pIemCpu, &NewRbp.u, &NewRsp);
2661 break;
2662 IEM_NOT_REACHED_DEFAULT_CASE_RET();
2663 }
2664 if (rcStrict != VINF_SUCCESS)
2665 return rcStrict;
2666
2667
2668 /* Commit it. */
2669 pCtx->rbp = NewRbp.u;
2670 pCtx->rsp = NewRsp.u;
2671 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
2672
2673 return VINF_SUCCESS;
2674}
2675
2676
2677/**
2678 * Implements int3 and int XX.
2679 *
2680 * @param u8Int The interrupt vector number.
2681 * @param fIsBpInstr Is it the breakpoint instruction.
2682 */
2683IEM_CIMPL_DEF_2(iemCImpl_int, uint8_t, u8Int, bool, fIsBpInstr)
2684{
2685 Assert(pIemCpu->cXcptRecursions == 0);
2686 return iemRaiseXcptOrInt(pIemCpu,
2687 cbInstr,
2688 u8Int,
2689 (fIsBpInstr ? IEM_XCPT_FLAGS_BP_INSTR : 0) | IEM_XCPT_FLAGS_T_SOFT_INT,
2690 0,
2691 0);
2692}
2693
2694
2695/**
2696 * Implements iret for real mode and V8086 mode.
2697 *
2698 * @param enmEffOpSize The effective operand size.
2699 */
2700IEM_CIMPL_DEF_1(iemCImpl_iret_real_v8086, IEMMODE, enmEffOpSize)
2701{
2702 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
2703 X86EFLAGS Efl;
2704 Efl.u = IEMMISC_GET_EFL(pIemCpu, pCtx);
2705 NOREF(cbInstr);
2706
2707 /*
2708 * iret throws an exception if VME isn't enabled.
2709 */
2710 if ( Efl.Bits.u1VM
2711 && Efl.Bits.u2IOPL != 3
2712 && !(pCtx->cr4 & X86_CR4_VME))
2713 return iemRaiseGeneralProtectionFault0(pIemCpu);
2714
2715 /*
2716 * Do the stack bits, but don't commit RSP before everything checks
2717 * out right.
2718 */
2719 Assert(enmEffOpSize == IEMMODE_32BIT || enmEffOpSize == IEMMODE_16BIT);
2720 VBOXSTRICTRC rcStrict;
2721 RTCPTRUNION uFrame;
2722 uint16_t uNewCs;
2723 uint32_t uNewEip;
2724 uint32_t uNewFlags;
2725 uint64_t uNewRsp;
2726 if (enmEffOpSize == IEMMODE_32BIT)
2727 {
2728 rcStrict = iemMemStackPopBeginSpecial(pIemCpu, 12, &uFrame.pv, &uNewRsp);
2729 if (rcStrict != VINF_SUCCESS)
2730 return rcStrict;
2731 uNewEip = uFrame.pu32[0];
2732 if (uNewEip > UINT16_MAX)
2733 return iemRaiseGeneralProtectionFault0(pIemCpu);
2734
2735 uNewCs = (uint16_t)uFrame.pu32[1];
2736 uNewFlags = uFrame.pu32[2];
2737 uNewFlags &= X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF
2738 | X86_EFL_TF | X86_EFL_IF | X86_EFL_DF | X86_EFL_OF | X86_EFL_IOPL | X86_EFL_NT
2739 | X86_EFL_RF /*| X86_EFL_VM*/ | X86_EFL_AC /*|X86_EFL_VIF*/ /*|X86_EFL_VIP*/
2740 | X86_EFL_ID;
2741 uNewFlags |= Efl.u & (X86_EFL_VM | X86_EFL_VIF | X86_EFL_VIP | X86_EFL_1);
2742 }
2743 else
2744 {
2745 rcStrict = iemMemStackPopBeginSpecial(pIemCpu, 6, &uFrame.pv, &uNewRsp);
2746 if (rcStrict != VINF_SUCCESS)
2747 return rcStrict;
2748 uNewEip = uFrame.pu16[0];
2749 uNewCs = uFrame.pu16[1];
2750 uNewFlags = uFrame.pu16[2];
2751 uNewFlags &= X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF
2752 | X86_EFL_TF | X86_EFL_IF | X86_EFL_DF | X86_EFL_OF | X86_EFL_IOPL | X86_EFL_NT;
2753 uNewFlags |= Efl.u & ((UINT32_C(0xffff0000) | X86_EFL_1) & ~X86_EFL_RF);
2754 /** @todo The intel pseudo code does not indicate what happens to
2755 * reserved flags. We just ignore them. */
2756 }
2757 /** @todo Check how this is supposed to work if sp=0xfffe. */
2758 Log7(("iemCImpl_iret_real_v8086: uNewCs=%#06x uNewRip=%#010x uNewFlags=%#x uNewRsp=%#18llx\n",
2759 uNewCs, uNewEip, uNewFlags, uNewRsp));
2760
2761 /*
2762 * Check the limit of the new EIP.
2763 */
2764 /** @todo Only the AMD pseudo code check the limit here, what's
2765 * right? */
2766 if (uNewEip > pCtx->cs.u32Limit)
2767 return iemRaiseSelectorBounds(pIemCpu, X86_SREG_CS, IEM_ACCESS_INSTRUCTION);
2768
2769 /*
2770 * V8086 checks and flag adjustments
2771 */
2772 if (Efl.Bits.u1VM)
2773 {
2774 if (Efl.Bits.u2IOPL == 3)
2775 {
2776 /* Preserve IOPL and clear RF. */
2777 uNewFlags &= ~(X86_EFL_IOPL | X86_EFL_RF);
2778 uNewFlags |= Efl.u & (X86_EFL_IOPL);
2779 }
2780 else if ( enmEffOpSize == IEMMODE_16BIT
2781 && ( !(uNewFlags & X86_EFL_IF)
2782 || !Efl.Bits.u1VIP )
2783 && !(uNewFlags & X86_EFL_TF) )
2784 {
2785 /* Move IF to VIF, clear RF and preserve IF and IOPL.*/
2786 uNewFlags &= ~X86_EFL_VIF;
2787 uNewFlags |= (uNewFlags & X86_EFL_IF) << (19 - 9);
2788 uNewFlags &= ~(X86_EFL_IF | X86_EFL_IOPL | X86_EFL_RF);
2789 uNewFlags |= Efl.u & (X86_EFL_IF | X86_EFL_IOPL);
2790 }
2791 else
2792 return iemRaiseGeneralProtectionFault0(pIemCpu);
2793 Log7(("iemCImpl_iret_real_v8086: u1VM=1: adjusted uNewFlags=%#x\n", uNewFlags));
2794 }
2795
2796 /*
2797 * Commit the operation.
2798 */
2799 rcStrict = iemMemStackPopCommitSpecial(pIemCpu, uFrame.pv, uNewRsp);
2800 if (rcStrict != VINF_SUCCESS)
2801 return rcStrict;
2802#ifdef DBGFTRACE_ENABLED
2803 RTTraceBufAddMsgF(IEMCPU_TO_VM(pIemCpu)->CTX_SUFF(hTraceBuf), "iret/rm %04x:%04x -> %04x:%04x %x %04llx",
2804 pCtx->cs.Sel, pCtx->eip, uNewCs, uNewEip, uNewFlags, uNewRsp);
2805#endif
2806
2807 pCtx->rip = uNewEip;
2808 pCtx->cs.Sel = uNewCs;
2809 pCtx->cs.ValidSel = uNewCs;
2810 pCtx->cs.fFlags = CPUMSELREG_FLAGS_VALID;
2811 pCtx->cs.u64Base = (uint32_t)uNewCs << 4;
2812 /** @todo do we load attribs and limit as well? */
2813 Assert(uNewFlags & X86_EFL_1);
2814 IEMMISC_SET_EFL(pIemCpu, pCtx, uNewFlags);
2815
2816 return VINF_SUCCESS;
2817}
2818
2819
2820/**
2821 * Loads a segment register when entering V8086 mode.
2822 *
2823 * @param pSReg The segment register.
2824 * @param uSeg The segment to load.
2825 */
2826static void iemCImplCommonV8086LoadSeg(PCPUMSELREG pSReg, uint16_t uSeg)
2827{
2828 pSReg->Sel = uSeg;
2829 pSReg->ValidSel = uSeg;
2830 pSReg->fFlags = CPUMSELREG_FLAGS_VALID;
2831 pSReg->u64Base = (uint32_t)uSeg << 4;
2832 pSReg->u32Limit = 0xffff;
2833 pSReg->Attr.u = X86_SEL_TYPE_RW_ACC | RT_BIT(4) /*!sys*/ | RT_BIT(7) /*P*/ | (3 /*DPL*/ << 5); /* VT-x wants 0xf3 */
2834 /** @todo Testcase: Check if VT-x really needs this and what it does itself when
2835 * IRET'ing to V8086. */
2836}
2837
2838
2839/**
2840 * Implements iret for protected mode returning to V8086 mode.
2841 *
2842 * @param pCtx Pointer to the CPU context.
2843 * @param uNewEip The new EIP.
2844 * @param uNewCs The new CS.
2845 * @param uNewFlags The new EFLAGS.
2846 * @param uNewRsp The RSP after the initial IRET frame.
2847 *
2848 * @note This can only be a 32-bit iret du to the X86_EFL_VM position.
2849 */
2850IEM_CIMPL_DEF_5(iemCImpl_iret_prot_v8086, PCPUMCTX, pCtx, uint32_t, uNewEip, uint16_t, uNewCs,
2851 uint32_t, uNewFlags, uint64_t, uNewRsp)
2852{
2853 /*
2854 * Pop the V8086 specific frame bits off the stack.
2855 */
2856 VBOXSTRICTRC rcStrict;
2857 RTCPTRUNION uFrame;
2858 rcStrict = iemMemStackPopContinueSpecial(pIemCpu, 24, &uFrame.pv, &uNewRsp);
2859 if (rcStrict != VINF_SUCCESS)
2860 return rcStrict;
2861 uint32_t uNewEsp = uFrame.pu32[0];
2862 uint16_t uNewSs = uFrame.pu32[1];
2863 uint16_t uNewEs = uFrame.pu32[2];
2864 uint16_t uNewDs = uFrame.pu32[3];
2865 uint16_t uNewFs = uFrame.pu32[4];
2866 uint16_t uNewGs = uFrame.pu32[5];
2867 rcStrict = iemMemCommitAndUnmap(pIemCpu, (void *)uFrame.pv, IEM_ACCESS_STACK_R); /* don't use iemMemStackPopCommitSpecial here. */
2868 if (rcStrict != VINF_SUCCESS)
2869 return rcStrict;
2870
2871 /*
2872 * Commit the operation.
2873 */
2874 uNewFlags &= X86_EFL_LIVE_MASK;
2875 uNewFlags |= X86_EFL_RA1_MASK;
2876#ifdef DBGFTRACE_ENABLED
2877 RTTraceBufAddMsgF(IEMCPU_TO_VM(pIemCpu)->CTX_SUFF(hTraceBuf), "iret/p/v %04x:%08x -> %04x:%04x %x %04x:%04x",
2878 pCtx->cs.Sel, pCtx->eip, uNewCs, uNewEip, uNewFlags, uNewSs, uNewEsp);
2879#endif
2880
2881 IEMMISC_SET_EFL(pIemCpu, pCtx, uNewFlags);
2882 iemCImplCommonV8086LoadSeg(&pCtx->cs, uNewCs);
2883 iemCImplCommonV8086LoadSeg(&pCtx->ss, uNewSs);
2884 iemCImplCommonV8086LoadSeg(&pCtx->es, uNewEs);
2885 iemCImplCommonV8086LoadSeg(&pCtx->ds, uNewDs);
2886 iemCImplCommonV8086LoadSeg(&pCtx->fs, uNewFs);
2887 iemCImplCommonV8086LoadSeg(&pCtx->gs, uNewGs);
2888 pCtx->rip = uNewEip;
2889 pCtx->rsp = uNewEsp;
2890 pIemCpu->uCpl = 3;
2891
2892 return VINF_SUCCESS;
2893}
2894
2895
2896/**
2897 * Implements iret for protected mode returning via a nested task.
2898 *
2899 * @param enmEffOpSize The effective operand size.
2900 */
2901IEM_CIMPL_DEF_1(iemCImpl_iret_prot_NestedTask, IEMMODE, enmEffOpSize)
2902{
2903 Log7(("iemCImpl_iret_prot_NestedTask:\n"));
2904#ifndef IEM_IMPLEMENTS_TASKSWITCH
2905 IEM_RETURN_ASPECT_NOT_IMPLEMENTED();
2906#else
2907 /*
2908 * Read the segment selector in the link-field of the current TSS.
2909 */
2910 RTSEL uSelRet;
2911 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
2912 VBOXSTRICTRC rcStrict = iemMemFetchSysU16(pIemCpu, &uSelRet, UINT8_MAX, pCtx->tr.u64Base);
2913 if (rcStrict != VINF_SUCCESS)
2914 return rcStrict;
2915
2916 /*
2917 * Fetch the returning task's TSS descriptor from the GDT.
2918 */
2919 if (uSelRet & X86_SEL_LDT)
2920 {
2921 Log(("iret_prot_NestedTask TSS not in LDT. uSelRet=%04x -> #TS\n", uSelRet));
2922 return iemRaiseTaskSwitchFaultBySelector(pIemCpu, uSelRet);
2923 }
2924
2925 IEMSELDESC TssDesc;
2926 rcStrict = iemMemFetchSelDesc(pIemCpu, &TssDesc, uSelRet, X86_XCPT_GP);
2927 if (rcStrict != VINF_SUCCESS)
2928 return rcStrict;
2929
2930 if (TssDesc.Legacy.Gate.u1DescType)
2931 {
2932 Log(("iret_prot_NestedTask Invalid TSS type. uSelRet=%04x -> #TS\n", uSelRet));
2933 return iemRaiseTaskSwitchFaultBySelector(pIemCpu, uSelRet & X86_SEL_MASK_OFF_RPL);
2934 }
2935
2936 if ( TssDesc.Legacy.Gate.u4Type != X86_SEL_TYPE_SYS_286_TSS_BUSY
2937 && TssDesc.Legacy.Gate.u4Type != X86_SEL_TYPE_SYS_386_TSS_BUSY)
2938 {
2939 Log(("iret_prot_NestedTask TSS is not busy. uSelRet=%04x DescType=%#x -> #TS\n", uSelRet, TssDesc.Legacy.Gate.u4Type));
2940 return iemRaiseTaskSwitchFaultBySelector(pIemCpu, uSelRet & X86_SEL_MASK_OFF_RPL);
2941 }
2942
2943 if (!TssDesc.Legacy.Gate.u1Present)
2944 {
2945 Log(("iret_prot_NestedTask TSS is not present. uSelRet=%04x -> #NP\n", uSelRet));
2946 return iemRaiseSelectorNotPresentBySelector(pIemCpu, uSelRet & X86_SEL_MASK_OFF_RPL);
2947 }
2948
2949 uint32_t uNextEip = pCtx->eip + cbInstr;
2950 return iemTaskSwitch(pIemCpu, pIemCpu->CTX_SUFF(pCtx), IEMTASKSWITCH_IRET, uNextEip, 0 /* fFlags */, 0 /* uErr */,
2951 0 /* uCr2 */, uSelRet, &TssDesc);
2952#endif
2953}
2954
2955
2956/**
2957 * Implements iret for protected mode
2958 *
2959 * @param enmEffOpSize The effective operand size.
2960 */
2961IEM_CIMPL_DEF_1(iemCImpl_iret_prot, IEMMODE, enmEffOpSize)
2962{
2963 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
2964 NOREF(cbInstr);
2965
2966 /*
2967 * Nested task return.
2968 */
2969 if (pCtx->eflags.Bits.u1NT)
2970 return IEM_CIMPL_CALL_1(iemCImpl_iret_prot_NestedTask, enmEffOpSize);
2971
2972 /*
2973 * Normal return.
2974 *
2975 * Do the stack bits, but don't commit RSP before everything checks
2976 * out right.
2977 */
2978 Assert(enmEffOpSize == IEMMODE_32BIT || enmEffOpSize == IEMMODE_16BIT);
2979 VBOXSTRICTRC rcStrict;
2980 RTCPTRUNION uFrame;
2981 uint16_t uNewCs;
2982 uint32_t uNewEip;
2983 uint32_t uNewFlags;
2984 uint64_t uNewRsp;
2985 if (enmEffOpSize == IEMMODE_32BIT)
2986 {
2987 rcStrict = iemMemStackPopBeginSpecial(pIemCpu, 12, &uFrame.pv, &uNewRsp);
2988 if (rcStrict != VINF_SUCCESS)
2989 return rcStrict;
2990 uNewEip = uFrame.pu32[0];
2991 uNewCs = (uint16_t)uFrame.pu32[1];
2992 uNewFlags = uFrame.pu32[2];
2993 }
2994 else
2995 {
2996 rcStrict = iemMemStackPopBeginSpecial(pIemCpu, 6, &uFrame.pv, &uNewRsp);
2997 if (rcStrict != VINF_SUCCESS)
2998 return rcStrict;
2999 uNewEip = uFrame.pu16[0];
3000 uNewCs = uFrame.pu16[1];
3001 uNewFlags = uFrame.pu16[2];
3002 }
3003 rcStrict = iemMemCommitAndUnmap(pIemCpu, (void *)uFrame.pv, IEM_ACCESS_STACK_R); /* don't use iemMemStackPopCommitSpecial here. */
3004 if (rcStrict != VINF_SUCCESS)
3005 return rcStrict;
3006 Log7(("iemCImpl_iret_prot: uNewCs=%#06x uNewEip=%#010x uNewFlags=%#x uNewRsp=%#18llx\n", uNewCs, uNewEip, uNewFlags, uNewRsp));
3007
3008 /*
3009 * We're hopefully not returning to V8086 mode...
3010 */
3011 if ( (uNewFlags & X86_EFL_VM)
3012 && pIemCpu->uCpl == 0)
3013 {
3014 Assert(enmEffOpSize == IEMMODE_32BIT);
3015 return IEM_CIMPL_CALL_5(iemCImpl_iret_prot_v8086, pCtx, uNewEip, uNewCs, uNewFlags, uNewRsp);
3016 }
3017
3018 /*
3019 * Protected mode.
3020 */
3021 /* Read the CS descriptor. */
3022 if (!(uNewCs & X86_SEL_MASK_OFF_RPL))
3023 {
3024 Log(("iret %04x:%08x -> invalid CS selector, #GP(0)\n", uNewCs, uNewEip));
3025 return iemRaiseGeneralProtectionFault0(pIemCpu);
3026 }
3027
3028 IEMSELDESC DescCS;
3029 rcStrict = iemMemFetchSelDesc(pIemCpu, &DescCS, uNewCs, X86_XCPT_GP);
3030 if (rcStrict != VINF_SUCCESS)
3031 {
3032 Log(("iret %04x:%08x - rcStrict=%Rrc when fetching CS\n", uNewCs, uNewEip, VBOXSTRICTRC_VAL(rcStrict)));
3033 return rcStrict;
3034 }
3035
3036 /* Must be a code descriptor. */
3037 if (!DescCS.Legacy.Gen.u1DescType)
3038 {
3039 Log(("iret %04x:%08x - CS is system segment (%#x) -> #GP\n", uNewCs, uNewEip, DescCS.Legacy.Gen.u4Type));
3040 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewCs);
3041 }
3042 if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE))
3043 {
3044 Log(("iret %04x:%08x - not code segment (%#x) -> #GP\n", uNewCs, uNewEip, DescCS.Legacy.Gen.u4Type));
3045 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewCs);
3046 }
3047
3048#ifdef VBOX_WITH_RAW_MODE_NOT_R0
3049 /* Raw ring-0 and ring-1 compression adjustments for PATM performance tricks and other CS leaks. */
3050 PVM pVM = IEMCPU_TO_VM(pIemCpu);
3051 if (EMIsRawRing0Enabled(pVM) && !HMIsEnabled(pVM))
3052 {
3053 if ((uNewCs & X86_SEL_RPL) == 1)
3054 {
3055 if ( pIemCpu->uCpl == 0
3056 && ( !EMIsRawRing1Enabled(pVM)
3057 || pCtx->cs.Sel == (uNewCs & X86_SEL_MASK_OFF_RPL)) )
3058 {
3059 Log(("iret: Ring-0 compression fix: uNewCS=%#x -> %#x\n", uNewCs, uNewCs & X86_SEL_MASK_OFF_RPL));
3060 uNewCs &= X86_SEL_MASK_OFF_RPL;
3061 }
3062# ifdef LOG_ENABLED
3063 else if (pIemCpu->uCpl <= 1 && EMIsRawRing1Enabled(pVM))
3064 Log(("iret: uNewCs=%#x genuine return to ring-1.\n", uNewCs));
3065# endif
3066 }
3067 else if ( (uNewCs & X86_SEL_RPL) == 2
3068 && EMIsRawRing1Enabled(pVM)
3069 && pIemCpu->uCpl <= 1)
3070 {
3071 Log(("iret: Ring-1 compression fix: uNewCS=%#x -> %#x\n", uNewCs, (uNewCs & X86_SEL_MASK_OFF_RPL) | 1));
3072 uNewCs = (uNewCs & X86_SEL_MASK_OFF_RPL) | 2;
3073 }
3074 }
3075#endif /* VBOX_WITH_RAW_MODE_NOT_R0 */
3076
3077
3078 /* Privilege checks. */
3079 if ((uNewCs & X86_SEL_RPL) < pIemCpu->uCpl)
3080 {
3081 Log(("iret %04x:%08x - RPL < CPL (%d) -> #GP\n", uNewCs, uNewEip, pIemCpu->uCpl));
3082 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewCs);
3083 }
3084 if ( (DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF)
3085 && (uNewCs & X86_SEL_RPL) < DescCS.Legacy.Gen.u2Dpl)
3086 {
3087 Log(("iret %04x:%08x - RPL < DPL (%d) -> #GP\n", uNewCs, uNewEip, DescCS.Legacy.Gen.u2Dpl));
3088 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewCs);
3089 }
3090
3091 /* Present? */
3092 if (!DescCS.Legacy.Gen.u1Present)
3093 {
3094 Log(("iret %04x:%08x - CS not present -> #NP\n", uNewCs, uNewEip));
3095 return iemRaiseSelectorNotPresentBySelector(pIemCpu, uNewCs);
3096 }
3097
3098 uint32_t cbLimitCS = X86DESC_LIMIT_G(&DescCS.Legacy);
3099
3100 /*
3101 * Return to outer level?
3102 */
3103 if ((uNewCs & X86_SEL_RPL) != pIemCpu->uCpl)
3104 {
3105 uint16_t uNewSS;
3106 uint32_t uNewESP;
3107 if (enmEffOpSize == IEMMODE_32BIT)
3108 {
3109 rcStrict = iemMemStackPopContinueSpecial(pIemCpu, 8, &uFrame.pv, &uNewRsp);
3110 if (rcStrict != VINF_SUCCESS)
3111 return rcStrict;
3112 uNewESP = uFrame.pu32[0];
3113 uNewSS = (uint16_t)uFrame.pu32[1];
3114 }
3115 else
3116 {
3117 rcStrict = iemMemStackPopContinueSpecial(pIemCpu, 4, &uFrame.pv, &uNewRsp);
3118 if (rcStrict != VINF_SUCCESS)
3119 return rcStrict;
3120 uNewESP = uFrame.pu16[0];
3121 uNewSS = uFrame.pu16[1];
3122 }
3123 rcStrict = iemMemCommitAndUnmap(pIemCpu, (void *)uFrame.pv, IEM_ACCESS_STACK_R);
3124 if (rcStrict != VINF_SUCCESS)
3125 return rcStrict;
3126 Log7(("iemCImpl_iret_prot: uNewSS=%#06x uNewESP=%#010x\n", uNewSS, uNewESP));
3127
3128 /* Read the SS descriptor. */
3129 if (!(uNewSS & X86_SEL_MASK_OFF_RPL))
3130 {
3131 Log(("iret %04x:%08x/%04x:%08x -> invalid SS selector, #GP(0)\n", uNewCs, uNewEip, uNewSS, uNewESP));
3132 return iemRaiseGeneralProtectionFault0(pIemCpu);
3133 }
3134
3135 IEMSELDESC DescSS;
3136 rcStrict = iemMemFetchSelDesc(pIemCpu, &DescSS, uNewSS, X86_XCPT_GP); /** @todo Correct exception? */
3137 if (rcStrict != VINF_SUCCESS)
3138 {
3139 Log(("iret %04x:%08x/%04x:%08x - %Rrc when fetching SS\n",
3140 uNewCs, uNewEip, uNewSS, uNewESP, VBOXSTRICTRC_VAL(rcStrict)));
3141 return rcStrict;
3142 }
3143
3144 /* Privilege checks. */
3145 if ((uNewSS & X86_SEL_RPL) != (uNewCs & X86_SEL_RPL))
3146 {
3147 Log(("iret %04x:%08x/%04x:%08x -> SS.RPL != CS.RPL -> #GP\n", uNewCs, uNewEip, uNewSS, uNewESP));
3148 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewSS);
3149 }
3150 if (DescSS.Legacy.Gen.u2Dpl != (uNewCs & X86_SEL_RPL))
3151 {
3152 Log(("iret %04x:%08x/%04x:%08x -> SS.DPL (%d) != CS.RPL -> #GP\n",
3153 uNewCs, uNewEip, uNewSS, uNewESP, DescSS.Legacy.Gen.u2Dpl));
3154 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewSS);
3155 }
3156
3157 /* Must be a writeable data segment descriptor. */
3158 if (!DescSS.Legacy.Gen.u1DescType)
3159 {
3160 Log(("iret %04x:%08x/%04x:%08x -> SS is system segment (%#x) -> #GP\n",
3161 uNewCs, uNewEip, uNewSS, uNewESP, DescSS.Legacy.Gen.u4Type));
3162 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewSS);
3163 }
3164 if ((DescSS.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_WRITE)) != X86_SEL_TYPE_WRITE)
3165 {
3166 Log(("iret %04x:%08x/%04x:%08x - not writable data segment (%#x) -> #GP\n",
3167 uNewCs, uNewEip, uNewSS, uNewESP, DescSS.Legacy.Gen.u4Type));
3168 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewSS);
3169 }
3170
3171 /* Present? */
3172 if (!DescSS.Legacy.Gen.u1Present)
3173 {
3174 Log(("iret %04x:%08x/%04x:%08x -> SS not present -> #SS\n", uNewCs, uNewEip, uNewSS, uNewESP));
3175 return iemRaiseStackSelectorNotPresentBySelector(pIemCpu, uNewSS);
3176 }
3177
3178 uint32_t cbLimitSs = X86DESC_LIMIT_G(&DescSS.Legacy);
3179
3180 /* Check EIP. */
3181 if (uNewEip > cbLimitCS)
3182 {
3183 Log(("iret %04x:%08x/%04x:%08x -> EIP is out of bounds (%#x) -> #GP(0)\n",
3184 uNewCs, uNewEip, uNewSS, uNewESP, cbLimitCS));
3185 /** @todo: Which is it, #GP(0) or #GP(sel)? */
3186 return iemRaiseSelectorBoundsBySelector(pIemCpu, uNewCs);
3187 }
3188
3189 /*
3190 * Commit the changes, marking CS and SS accessed first since
3191 * that may fail.
3192 */
3193 if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
3194 {
3195 rcStrict = iemMemMarkSelDescAccessed(pIemCpu, uNewCs);
3196 if (rcStrict != VINF_SUCCESS)
3197 return rcStrict;
3198 DescCS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
3199 }
3200 if (!(DescSS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
3201 {
3202 rcStrict = iemMemMarkSelDescAccessed(pIemCpu, uNewSS);
3203 if (rcStrict != VINF_SUCCESS)
3204 return rcStrict;
3205 DescSS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
3206 }
3207
3208 uint32_t fEFlagsMask = X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF
3209 | X86_EFL_TF | X86_EFL_DF | X86_EFL_OF | X86_EFL_NT;
3210 if (enmEffOpSize != IEMMODE_16BIT)
3211 fEFlagsMask |= X86_EFL_RF | X86_EFL_AC | X86_EFL_ID;
3212 if (pIemCpu->uCpl == 0)
3213 fEFlagsMask |= X86_EFL_IF | X86_EFL_IOPL | X86_EFL_VIF | X86_EFL_VIP; /* VM is 0 */
3214 else if (pIemCpu->uCpl <= pCtx->eflags.Bits.u2IOPL)
3215 fEFlagsMask |= X86_EFL_IF;
3216 uint32_t fEFlagsNew = IEMMISC_GET_EFL(pIemCpu, pCtx);
3217 fEFlagsNew &= ~fEFlagsMask;
3218 fEFlagsNew |= uNewFlags & fEFlagsMask;
3219#ifdef DBGFTRACE_ENABLED
3220 RTTraceBufAddMsgF(IEMCPU_TO_VM(pIemCpu)->CTX_SUFF(hTraceBuf), "iret/%up%u %04x:%08x -> %04x:%04x %x %04x:%04x",
3221 pIemCpu->uCpl, uNewCs & X86_SEL_RPL, pCtx->cs.Sel, pCtx->eip,
3222 uNewCs, uNewEip, uNewFlags, uNewSS, uNewESP);
3223#endif
3224
3225 IEMMISC_SET_EFL(pIemCpu, pCtx, fEFlagsNew);
3226 pCtx->rip = uNewEip;
3227 pCtx->cs.Sel = uNewCs;
3228 pCtx->cs.ValidSel = uNewCs;
3229 pCtx->cs.fFlags = CPUMSELREG_FLAGS_VALID;
3230 pCtx->cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCS.Legacy);
3231 pCtx->cs.u32Limit = cbLimitCS;
3232 pCtx->cs.u64Base = X86DESC_BASE(&DescCS.Legacy);
3233 if (!pCtx->ss.Attr.n.u1DefBig)
3234 pCtx->sp = (uint16_t)uNewESP;
3235 else
3236 pCtx->rsp = uNewESP;
3237 pCtx->ss.Sel = uNewSS;
3238 pCtx->ss.ValidSel = uNewSS;
3239 pCtx->ss.fFlags = CPUMSELREG_FLAGS_VALID;
3240 pCtx->ss.Attr.u = X86DESC_GET_HID_ATTR(&DescSS.Legacy);
3241 pCtx->ss.u32Limit = cbLimitSs;
3242 pCtx->ss.u64Base = X86DESC_BASE(&DescSS.Legacy);
3243
3244 pIemCpu->uCpl = uNewCs & X86_SEL_RPL;
3245 iemHlpAdjustSelectorForNewCpl(pIemCpu, uNewCs & X86_SEL_RPL, &pCtx->ds);
3246 iemHlpAdjustSelectorForNewCpl(pIemCpu, uNewCs & X86_SEL_RPL, &pCtx->es);
3247 iemHlpAdjustSelectorForNewCpl(pIemCpu, uNewCs & X86_SEL_RPL, &pCtx->fs);
3248 iemHlpAdjustSelectorForNewCpl(pIemCpu, uNewCs & X86_SEL_RPL, &pCtx->gs);
3249
3250 /* Done! */
3251
3252 }
3253 /*
3254 * Return to the same level.
3255 */
3256 else
3257 {
3258 /* Check EIP. */
3259 if (uNewEip > cbLimitCS)
3260 {
3261 Log(("iret %04x:%08x - EIP is out of bounds (%#x) -> #GP(0)\n", uNewCs, uNewEip, cbLimitCS));
3262 /** @todo: Which is it, #GP(0) or #GP(sel)? */
3263 return iemRaiseSelectorBoundsBySelector(pIemCpu, uNewCs);
3264 }
3265
3266 /*
3267 * Commit the changes, marking CS first since it may fail.
3268 */
3269 if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
3270 {
3271 rcStrict = iemMemMarkSelDescAccessed(pIemCpu, uNewCs);
3272 if (rcStrict != VINF_SUCCESS)
3273 return rcStrict;
3274 DescCS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
3275 }
3276
3277 X86EFLAGS NewEfl;
3278 NewEfl.u = IEMMISC_GET_EFL(pIemCpu, pCtx);
3279 uint32_t fEFlagsMask = X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF
3280 | X86_EFL_TF | X86_EFL_DF | X86_EFL_OF | X86_EFL_NT;
3281 if (enmEffOpSize != IEMMODE_16BIT)
3282 fEFlagsMask |= X86_EFL_RF | X86_EFL_AC | X86_EFL_ID;
3283 if (pIemCpu->uCpl == 0)
3284 fEFlagsMask |= X86_EFL_IF | X86_EFL_IOPL | X86_EFL_VIF | X86_EFL_VIP; /* VM is 0 */
3285 else if (pIemCpu->uCpl <= NewEfl.Bits.u2IOPL)
3286 fEFlagsMask |= X86_EFL_IF;
3287 NewEfl.u &= ~fEFlagsMask;
3288 NewEfl.u |= fEFlagsMask & uNewFlags;
3289#ifdef DBGFTRACE_ENABLED
3290 RTTraceBufAddMsgF(IEMCPU_TO_VM(pIemCpu)->CTX_SUFF(hTraceBuf), "iret/%up %04x:%08x -> %04x:%04x %x %04x:%04llx",
3291 pIemCpu->uCpl, pCtx->cs.Sel, pCtx->eip,
3292 uNewCs, uNewEip, uNewFlags, pCtx->ss.Sel, uNewRsp);
3293#endif
3294
3295 IEMMISC_SET_EFL(pIemCpu, pCtx, NewEfl.u);
3296 pCtx->rip = uNewEip;
3297 pCtx->cs.Sel = uNewCs;
3298 pCtx->cs.ValidSel = uNewCs;
3299 pCtx->cs.fFlags = CPUMSELREG_FLAGS_VALID;
3300 pCtx->cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCS.Legacy);
3301 pCtx->cs.u32Limit = cbLimitCS;
3302 pCtx->cs.u64Base = X86DESC_BASE(&DescCS.Legacy);
3303 pCtx->rsp = uNewRsp;
3304 /* Done! */
3305 }
3306 return VINF_SUCCESS;
3307}
3308
3309
3310/**
3311 * Implements iret for long mode
3312 *
3313 * @param enmEffOpSize The effective operand size.
3314 */
3315IEM_CIMPL_DEF_1(iemCImpl_iret_long, IEMMODE, enmEffOpSize)
3316{
3317 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
3318 NOREF(cbInstr);
3319
3320 /*
3321 * Nested task return is not supported in long mode.
3322 */
3323 if (pCtx->eflags.Bits.u1NT)
3324 {
3325 Log(("iretq with NT=1 (eflags=%#x) -> #GP(0)\n", pCtx->eflags.u));
3326 return iemRaiseGeneralProtectionFault0(pIemCpu);
3327 }
3328
3329 /*
3330 * Normal return.
3331 *
3332 * Do the stack bits, but don't commit RSP before everything checks
3333 * out right.
3334 */
3335 VBOXSTRICTRC rcStrict;
3336 RTCPTRUNION uFrame;
3337 uint64_t uNewRip;
3338 uint16_t uNewCs;
3339 uint16_t uNewSs;
3340 uint32_t uNewFlags;
3341 uint64_t uNewRsp;
3342 if (enmEffOpSize == IEMMODE_64BIT)
3343 {
3344 rcStrict = iemMemStackPopBeginSpecial(pIemCpu, 5*8, &uFrame.pv, &uNewRsp);
3345 if (rcStrict != VINF_SUCCESS)
3346 return rcStrict;
3347 uNewRip = uFrame.pu64[0];
3348 uNewCs = (uint16_t)uFrame.pu64[1];
3349 uNewFlags = (uint32_t)uFrame.pu64[2];
3350 uNewRsp = uFrame.pu64[3];
3351 uNewSs = (uint16_t)uFrame.pu64[4];
3352 }
3353 else if (enmEffOpSize == IEMMODE_32BIT)
3354 {
3355 rcStrict = iemMemStackPopBeginSpecial(pIemCpu, 5*4, &uFrame.pv, &uNewRsp);
3356 if (rcStrict != VINF_SUCCESS)
3357 return rcStrict;
3358 uNewRip = uFrame.pu32[0];
3359 uNewCs = (uint16_t)uFrame.pu32[1];
3360 uNewFlags = uFrame.pu32[2];
3361 uNewRsp = uFrame.pu32[3];
3362 uNewSs = (uint16_t)uFrame.pu32[4];
3363 }
3364 else
3365 {
3366 Assert(enmEffOpSize == IEMMODE_16BIT);
3367 rcStrict = iemMemStackPopBeginSpecial(pIemCpu, 5*2, &uFrame.pv, &uNewRsp);
3368 if (rcStrict != VINF_SUCCESS)
3369 return rcStrict;
3370 uNewRip = uFrame.pu16[0];
3371 uNewCs = uFrame.pu16[1];
3372 uNewFlags = uFrame.pu16[2];
3373 uNewRsp = uFrame.pu16[3];
3374 uNewSs = uFrame.pu16[4];
3375 }
3376 rcStrict = iemMemCommitAndUnmap(pIemCpu, (void *)uFrame.pv, IEM_ACCESS_STACK_R); /* don't use iemMemStackPopCommitSpecial here. */
3377 if (rcStrict != VINF_SUCCESS)
3378 return rcStrict;
3379 Log7(("iretq stack: cs:rip=%04x:%016RX64 rflags=%016RX64 ss:rsp=%04x:%016RX64\n", uNewCs, uNewRip, uNewFlags, uNewSs, uNewRsp));
3380
3381 /*
3382 * Check stuff.
3383 */
3384 /* Read the CS descriptor. */
3385 if (!(uNewCs & X86_SEL_MASK_OFF_RPL))
3386 {
3387 Log(("iret %04x:%016RX64/%04x:%016RX64 -> invalid CS selector, #GP(0)\n", uNewCs, uNewRip, uNewSs, uNewRsp));
3388 return iemRaiseGeneralProtectionFault0(pIemCpu);
3389 }
3390
3391 IEMSELDESC DescCS;
3392 rcStrict = iemMemFetchSelDesc(pIemCpu, &DescCS, uNewCs, X86_XCPT_GP);
3393 if (rcStrict != VINF_SUCCESS)
3394 {
3395 Log(("iret %04x:%016RX64/%04x:%016RX64 - rcStrict=%Rrc when fetching CS\n",
3396 uNewCs, uNewRip, uNewSs, uNewRsp, VBOXSTRICTRC_VAL(rcStrict)));
3397 return rcStrict;
3398 }
3399
3400 /* Must be a code descriptor. */
3401 if ( !DescCS.Legacy.Gen.u1DescType
3402 || !(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE))
3403 {
3404 Log(("iret %04x:%016RX64/%04x:%016RX64 - CS is not a code segment T=%u T=%#xu -> #GP\n",
3405 uNewCs, uNewRip, uNewSs, uNewRsp, DescCS.Legacy.Gen.u1DescType, DescCS.Legacy.Gen.u4Type));
3406 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewCs);
3407 }
3408
3409 /* Privilege checks. */
3410 uint8_t const uNewCpl = uNewCs & X86_SEL_RPL;
3411 if ((uNewCs & X86_SEL_RPL) < pIemCpu->uCpl)
3412 {
3413 Log(("iret %04x:%016RX64/%04x:%016RX64 - RPL < CPL (%d) -> #GP\n", uNewCs, uNewRip, uNewSs, uNewRsp, pIemCpu->uCpl));
3414 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewCs);
3415 }
3416 if ( (DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_CONF)
3417 && (uNewCs & X86_SEL_RPL) < DescCS.Legacy.Gen.u2Dpl)
3418 {
3419 Log(("iret %04x:%016RX64/%04x:%016RX64 - RPL < DPL (%d) -> #GP\n",
3420 uNewCs, uNewRip, uNewSs, uNewRsp, DescCS.Legacy.Gen.u2Dpl));
3421 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewCs);
3422 }
3423
3424 /* Present? */
3425 if (!DescCS.Legacy.Gen.u1Present)
3426 {
3427 Log(("iret %04x:%016RX64/%04x:%016RX64 - CS not present -> #NP\n", uNewCs, uNewRip, uNewSs, uNewRsp));
3428 return iemRaiseSelectorNotPresentBySelector(pIemCpu, uNewCs);
3429 }
3430
3431 uint32_t cbLimitCS = X86DESC_LIMIT_G(&DescCS.Legacy);
3432
3433 /* Read the SS descriptor. */
3434 IEMSELDESC DescSS;
3435 if (!(uNewSs & X86_SEL_MASK_OFF_RPL))
3436 {
3437 if ( !DescCS.Legacy.Gen.u1Long
3438 || DescCS.Legacy.Gen.u1DefBig /** @todo exactly how does iret (and others) behave with u1Long=1 and u1DefBig=1? \#GP(sel)? */
3439 || uNewCpl > 2) /** @todo verify SS=0 impossible for ring-3. */
3440 {
3441 Log(("iret %04x:%016RX64/%04x:%016RX64 -> invalid SS selector, #GP(0)\n", uNewCs, uNewRip, uNewSs, uNewRsp));
3442 return iemRaiseGeneralProtectionFault0(pIemCpu);
3443 }
3444 DescSS.Legacy.u = 0;
3445 }
3446 else
3447 {
3448 rcStrict = iemMemFetchSelDesc(pIemCpu, &DescSS, uNewSs, X86_XCPT_GP); /** @todo Correct exception? */
3449 if (rcStrict != VINF_SUCCESS)
3450 {
3451 Log(("iret %04x:%016RX64/%04x:%016RX64 - %Rrc when fetching SS\n",
3452 uNewCs, uNewRip, uNewSs, uNewRsp, VBOXSTRICTRC_VAL(rcStrict)));
3453 return rcStrict;
3454 }
3455 }
3456
3457 /* Privilege checks. */
3458 if ((uNewSs & X86_SEL_RPL) != (uNewCs & X86_SEL_RPL))
3459 {
3460 Log(("iret %04x:%016RX64/%04x:%016RX64 -> SS.RPL != CS.RPL -> #GP\n", uNewCs, uNewRip, uNewSs, uNewRsp));
3461 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewSs);
3462 }
3463
3464 uint32_t cbLimitSs;
3465 if (!(uNewSs & X86_SEL_MASK_OFF_RPL))
3466 cbLimitSs = UINT32_MAX;
3467 else
3468 {
3469 if (DescSS.Legacy.Gen.u2Dpl != (uNewCs & X86_SEL_RPL))
3470 {
3471 Log(("iret %04x:%016RX64/%04x:%016RX64 -> SS.DPL (%d) != CS.RPL -> #GP\n",
3472 uNewCs, uNewRip, uNewSs, uNewRsp, DescSS.Legacy.Gen.u2Dpl));
3473 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewSs);
3474 }
3475
3476 /* Must be a writeable data segment descriptor. */
3477 if (!DescSS.Legacy.Gen.u1DescType)
3478 {
3479 Log(("iret %04x:%016RX64/%04x:%016RX64 -> SS is system segment (%#x) -> #GP\n",
3480 uNewCs, uNewRip, uNewSs, uNewRsp, DescSS.Legacy.Gen.u4Type));
3481 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewSs);
3482 }
3483 if ((DescSS.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_WRITE)) != X86_SEL_TYPE_WRITE)
3484 {
3485 Log(("iret %04x:%016RX64/%04x:%016RX64 - not writable data segment (%#x) -> #GP\n",
3486 uNewCs, uNewRip, uNewSs, uNewRsp, DescSS.Legacy.Gen.u4Type));
3487 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewSs);
3488 }
3489
3490 /* Present? */
3491 if (!DescSS.Legacy.Gen.u1Present)
3492 {
3493 Log(("iret %04x:%016RX64/%04x:%016RX64 -> SS not present -> #SS\n", uNewCs, uNewRip, uNewSs, uNewRsp));
3494 return iemRaiseStackSelectorNotPresentBySelector(pIemCpu, uNewSs);
3495 }
3496 cbLimitSs = X86DESC_LIMIT_G(&DescSS.Legacy);
3497 }
3498
3499 /* Check EIP. */
3500 if (DescCS.Legacy.Gen.u1Long)
3501 {
3502 if (!IEM_IS_CANONICAL(uNewRip))
3503 {
3504 Log(("iret %04x:%016RX64/%04x:%016RX64 -> RIP is not canonical -> #GP(0)\n",
3505 uNewCs, uNewRip, uNewSs, uNewRsp));
3506 return iemRaiseSelectorBoundsBySelector(pIemCpu, uNewCs);
3507 }
3508 }
3509 else
3510 {
3511 if (uNewRip > cbLimitCS)
3512 {
3513 Log(("iret %04x:%016RX64/%04x:%016RX64 -> EIP is out of bounds (%#x) -> #GP(0)\n",
3514 uNewCs, uNewRip, uNewSs, uNewRsp, cbLimitCS));
3515 /** @todo: Which is it, #GP(0) or #GP(sel)? */
3516 return iemRaiseSelectorBoundsBySelector(pIemCpu, uNewCs);
3517 }
3518 }
3519
3520 /*
3521 * Commit the changes, marking CS and SS accessed first since
3522 * that may fail.
3523 */
3524 /** @todo where exactly are these actually marked accessed by a real CPU? */
3525 if (!(DescCS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
3526 {
3527 rcStrict = iemMemMarkSelDescAccessed(pIemCpu, uNewCs);
3528 if (rcStrict != VINF_SUCCESS)
3529 return rcStrict;
3530 DescCS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
3531 }
3532 if (!(DescSS.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
3533 {
3534 rcStrict = iemMemMarkSelDescAccessed(pIemCpu, uNewSs);
3535 if (rcStrict != VINF_SUCCESS)
3536 return rcStrict;
3537 DescSS.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
3538 }
3539
3540 uint32_t fEFlagsMask = X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF
3541 | X86_EFL_TF | X86_EFL_DF | X86_EFL_OF | X86_EFL_NT;
3542 if (enmEffOpSize != IEMMODE_16BIT)
3543 fEFlagsMask |= X86_EFL_RF | X86_EFL_AC | X86_EFL_ID;
3544 if (pIemCpu->uCpl == 0)
3545 fEFlagsMask |= X86_EFL_IF | X86_EFL_IOPL | X86_EFL_VIF | X86_EFL_VIP; /* VM is ignored */
3546 else if (pIemCpu->uCpl <= pCtx->eflags.Bits.u2IOPL)
3547 fEFlagsMask |= X86_EFL_IF;
3548 uint32_t fEFlagsNew = IEMMISC_GET_EFL(pIemCpu, pCtx);
3549 fEFlagsNew &= ~fEFlagsMask;
3550 fEFlagsNew |= uNewFlags & fEFlagsMask;
3551#ifdef DBGFTRACE_ENABLED
3552 RTTraceBufAddMsgF(IEMCPU_TO_VM(pIemCpu)->CTX_SUFF(hTraceBuf), "iret/%ul%u %08llx -> %04x:%04llx %llx %04x:%04llx",
3553 pIemCpu->uCpl, uNewCpl, pCtx->rip, uNewCs, uNewRip, uNewFlags, uNewSs, uNewRsp);
3554#endif
3555
3556 IEMMISC_SET_EFL(pIemCpu, pCtx, fEFlagsNew);
3557 pCtx->rip = uNewRip;
3558 pCtx->cs.Sel = uNewCs;
3559 pCtx->cs.ValidSel = uNewCs;
3560 pCtx->cs.fFlags = CPUMSELREG_FLAGS_VALID;
3561 pCtx->cs.Attr.u = X86DESC_GET_HID_ATTR(&DescCS.Legacy);
3562 pCtx->cs.u32Limit = cbLimitCS;
3563 pCtx->cs.u64Base = X86DESC_BASE(&DescCS.Legacy);
3564 if (pCtx->cs.Attr.n.u1Long || pCtx->cs.Attr.n.u1DefBig)
3565 pCtx->rsp = uNewRsp;
3566 else
3567 pCtx->sp = (uint16_t)uNewRsp;
3568 pCtx->ss.Sel = uNewSs;
3569 pCtx->ss.ValidSel = uNewSs;
3570 if (!(uNewSs & X86_SEL_MASK_OFF_RPL))
3571 {
3572 pCtx->ss.fFlags = CPUMSELREG_FLAGS_VALID;
3573 pCtx->ss.Attr.u = X86DESCATTR_UNUSABLE | (uNewCpl << X86DESCATTR_DPL_SHIFT);
3574 pCtx->ss.u32Limit = UINT32_MAX;
3575 pCtx->ss.u64Base = 0;
3576 Log2(("iretq new SS: NULL\n"));
3577 }
3578 else
3579 {
3580 pCtx->ss.fFlags = CPUMSELREG_FLAGS_VALID;
3581 pCtx->ss.Attr.u = X86DESC_GET_HID_ATTR(&DescSS.Legacy);
3582 pCtx->ss.u32Limit = cbLimitSs;
3583 pCtx->ss.u64Base = X86DESC_BASE(&DescSS.Legacy);
3584 Log2(("iretq new SS: base=%#RX64 lim=%#x attr=%#x\n", pCtx->ss.u64Base, pCtx->ss.u32Limit, pCtx->ss.Attr.u));
3585 }
3586
3587 if (pIemCpu->uCpl != uNewCpl)
3588 {
3589 pIemCpu->uCpl = uNewCpl;
3590 iemHlpAdjustSelectorForNewCpl(pIemCpu, uNewCpl, &pCtx->ds);
3591 iemHlpAdjustSelectorForNewCpl(pIemCpu, uNewCpl, &pCtx->es);
3592 iemHlpAdjustSelectorForNewCpl(pIemCpu, uNewCpl, &pCtx->fs);
3593 iemHlpAdjustSelectorForNewCpl(pIemCpu, uNewCpl, &pCtx->gs);
3594 }
3595
3596 return VINF_SUCCESS;
3597}
3598
3599
3600/**
3601 * Implements iret.
3602 *
3603 * @param enmEffOpSize The effective operand size.
3604 */
3605IEM_CIMPL_DEF_1(iemCImpl_iret, IEMMODE, enmEffOpSize)
3606{
3607 /*
3608 * First, clear NMI blocking, if any, before causing any exceptions.
3609 */
3610 PVMCPU pVCpu = IEMCPU_TO_VMCPU(pIemCpu);
3611 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_BLOCK_NMIS);
3612
3613 /*
3614 * Call a mode specific worker.
3615 */
3616 if (IEM_IS_REAL_OR_V86_MODE(pIemCpu))
3617 return IEM_CIMPL_CALL_1(iemCImpl_iret_real_v8086, enmEffOpSize);
3618 if (IEM_IS_LONG_MODE(pIemCpu))
3619 return IEM_CIMPL_CALL_1(iemCImpl_iret_long, enmEffOpSize);
3620
3621 return IEM_CIMPL_CALL_1(iemCImpl_iret_prot, enmEffOpSize);
3622}
3623
3624
3625/**
3626 * Implements SYSCALL (AMD and Intel64).
3627 *
3628 * @param enmEffOpSize The effective operand size.
3629 */
3630IEM_CIMPL_DEF_0(iemCImpl_syscall)
3631{
3632 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
3633
3634 /*
3635 * Check preconditions.
3636 *
3637 * Note that CPUs described in the documentation may load a few odd values
3638 * into CS and SS than we allow here. This has yet to be checked on real
3639 * hardware.
3640 */
3641 if (!(pCtx->msrEFER & MSR_K6_EFER_SCE))
3642 {
3643 Log(("syscall: Not enabled in EFER -> #UD\n"));
3644 return iemRaiseUndefinedOpcode(pIemCpu);
3645 }
3646 if (!(pCtx->cr0 & X86_CR0_PE))
3647 {
3648 Log(("syscall: Protected mode is required -> #GP(0)\n"));
3649 return iemRaiseGeneralProtectionFault0(pIemCpu);
3650 }
3651 if (IEM_IS_GUEST_CPU_INTEL(pIemCpu) && !CPUMIsGuestInLongModeEx(pCtx))
3652 {
3653 Log(("syscall: Only available in long mode on intel -> #UD\n"));
3654 return iemRaiseUndefinedOpcode(pIemCpu);
3655 }
3656
3657 /** @todo verify RPL ignoring and CS=0xfff8 (i.e. SS == 0). */
3658 /** @todo what about LDT selectors? Shouldn't matter, really. */
3659 uint16_t uNewCs = (pCtx->msrSTAR >> MSR_K6_STAR_SYSCALL_CS_SS_SHIFT) & X86_SEL_MASK_OFF_RPL;
3660 uint16_t uNewSs = uNewCs + 8;
3661 if (uNewCs == 0 || uNewSs == 0)
3662 {
3663 Log(("syscall: msrSTAR.CS = 0 or SS = 0 -> #GP(0)\n"));
3664 return iemRaiseGeneralProtectionFault0(pIemCpu);
3665 }
3666
3667 /* Long mode and legacy mode differs. */
3668 if (CPUMIsGuestInLongModeEx(pCtx))
3669 {
3670 uint64_t uNewRip = pIemCpu->enmCpuMode == IEMMODE_64BIT ? pCtx->msrLSTAR : pCtx-> msrCSTAR;
3671
3672 /* This test isn't in the docs, but I'm not trusting the guys writing
3673 the MSRs to have validated the values as canonical like they should. */
3674 if (!IEM_IS_CANONICAL(uNewRip))
3675 {
3676 Log(("syscall: Only available in long mode on intel -> #UD\n"));
3677 return iemRaiseUndefinedOpcode(pIemCpu);
3678 }
3679
3680 /*
3681 * Commit it.
3682 */
3683 Log(("syscall: %04x:%016RX64 [efl=%#llx] -> %04x:%016RX64\n", pCtx->cs, pCtx->rip, pCtx->rflags.u, uNewCs, uNewRip));
3684 pCtx->rcx = pCtx->rip + cbInstr;
3685 pCtx->rip = uNewRip;
3686
3687 pCtx->rflags.u &= ~X86_EFL_RF;
3688 pCtx->r11 = pCtx->rflags.u;
3689 pCtx->rflags.u &= ~pCtx->msrSFMASK;
3690 pCtx->rflags.u |= X86_EFL_1;
3691
3692 pCtx->cs.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_L | X86DESCATTR_DT | X86_SEL_TYPE_ER_ACC;
3693 pCtx->ss.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_L | X86DESCATTR_DT | X86_SEL_TYPE_RW_ACC;
3694 }
3695 else
3696 {
3697 /*
3698 * Commit it.
3699 */
3700 Log(("syscall: %04x:%08RX32 [efl=%#x] -> %04x:%08RX32\n",
3701 pCtx->cs, pCtx->eip, pCtx->eflags.u, uNewCs, (uint32_t)(pCtx->msrSTAR & MSR_K6_STAR_SYSCALL_EIP_MASK)));
3702 pCtx->rcx = pCtx->eip + cbInstr;
3703 pCtx->rip = pCtx->msrSTAR & MSR_K6_STAR_SYSCALL_EIP_MASK;
3704 pCtx->rflags.u &= ~(X86_EFL_VM | X86_EFL_IF | X86_EFL_RF);
3705
3706 pCtx->cs.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_D | X86DESCATTR_DT | X86_SEL_TYPE_ER_ACC;
3707 pCtx->ss.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_D | X86DESCATTR_DT | X86_SEL_TYPE_RW_ACC;
3708 }
3709 pCtx->cs.Sel = uNewCs;
3710 pCtx->cs.ValidSel = uNewCs;
3711 pCtx->cs.u64Base = 0;
3712 pCtx->cs.u32Limit = UINT32_MAX;
3713 pCtx->cs.fFlags = CPUMSELREG_FLAGS_VALID;
3714
3715 pCtx->ss.Sel = uNewSs;
3716 pCtx->ss.ValidSel = uNewSs;
3717 pCtx->ss.u64Base = 0;
3718 pCtx->ss.u32Limit = UINT32_MAX;
3719 pCtx->ss.fFlags = CPUMSELREG_FLAGS_VALID;
3720
3721 return VINF_SUCCESS;
3722}
3723
3724
3725/**
3726 * Implements SYSRET (AMD and Intel64).
3727 */
3728IEM_CIMPL_DEF_0(iemCImpl_sysret)
3729
3730{
3731 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
3732
3733 /*
3734 * Check preconditions.
3735 *
3736 * Note that CPUs described in the documentation may load a few odd values
3737 * into CS and SS than we allow here. This has yet to be checked on real
3738 * hardware.
3739 */
3740 if (!(pCtx->msrEFER & MSR_K6_EFER_SCE))
3741 {
3742 Log(("sysret: Not enabled in EFER -> #UD\n"));
3743 return iemRaiseUndefinedOpcode(pIemCpu);
3744 }
3745 if (IEM_IS_GUEST_CPU_INTEL(pIemCpu) && !CPUMIsGuestInLongModeEx(pCtx))
3746 {
3747 Log(("sysret: Only available in long mode on intel -> #UD\n"));
3748 return iemRaiseUndefinedOpcode(pIemCpu);
3749 }
3750 if (!(pCtx->cr0 & X86_CR0_PE))
3751 {
3752 Log(("sysret: Protected mode is required -> #GP(0)\n"));
3753 return iemRaiseGeneralProtectionFault0(pIemCpu);
3754 }
3755 if (pIemCpu->uCpl != 0)
3756 {
3757 Log(("sysret: CPL must be 0 not %u -> #GP(0)\n", pIemCpu->uCpl));
3758 return iemRaiseGeneralProtectionFault0(pIemCpu);
3759 }
3760
3761 /** @todo Does SYSRET verify CS != 0 and SS != 0? Neither is valid in ring-3. */
3762 uint16_t uNewCs = (pCtx->msrSTAR >> MSR_K6_STAR_SYSRET_CS_SS_SHIFT) & X86_SEL_MASK_OFF_RPL;
3763 uint16_t uNewSs = uNewCs + 8;
3764 if (pIemCpu->enmEffOpSize == IEMMODE_64BIT)
3765 uNewCs += 16;
3766 if (uNewCs == 0 || uNewSs == 0)
3767 {
3768 Log(("sysret: msrSTAR.CS = 0 or SS = 0 -> #GP(0)\n"));
3769 return iemRaiseGeneralProtectionFault0(pIemCpu);
3770 }
3771
3772 /*
3773 * Commit it.
3774 */
3775 if (CPUMIsGuestInLongModeEx(pCtx))
3776 {
3777 if (pIemCpu->enmEffOpSize == IEMMODE_64BIT)
3778 {
3779 Log(("sysret: %04x:%016RX64 [efl=%#llx] -> %04x:%016RX64 [r11=%#llx]\n",
3780 pCtx->cs, pCtx->rip, pCtx->rflags.u, uNewCs, pCtx->rcx, pCtx->r11));
3781 /* Note! We disregard intel manual regarding the RCX cananonical
3782 check, ask intel+xen why AMD doesn't do it. */
3783 pCtx->rip = pCtx->rcx;
3784 pCtx->cs.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_L | X86DESCATTR_DT | X86_SEL_TYPE_ER_ACC
3785 | (3 << X86DESCATTR_DPL_SHIFT);
3786 }
3787 else
3788 {
3789 Log(("sysret: %04x:%016RX64 [efl=%#llx] -> %04x:%08RX32 [r11=%#llx]\n",
3790 pCtx->cs, pCtx->rip, pCtx->rflags.u, uNewCs, pCtx->ecx, pCtx->r11));
3791 pCtx->rip = pCtx->ecx;
3792 pCtx->cs.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_D | X86DESCATTR_DT | X86_SEL_TYPE_ER_ACC
3793 | (3 << X86DESCATTR_DPL_SHIFT);
3794 }
3795 /** @todo testcase: See what kind of flags we can make SYSRET restore and
3796 * what it really ignores. RF and VM are hinted at being zero, by AMD. */
3797 pCtx->rflags.u = pCtx->r11 & (X86_EFL_POPF_BITS | X86_EFL_VIF | X86_EFL_VIP);
3798 pCtx->rflags.u |= X86_EFL_1;
3799 }
3800 else
3801 {
3802 Log(("sysret: %04x:%08RX32 [efl=%#x] -> %04x:%08RX32\n", pCtx->cs, pCtx->eip, pCtx->eflags.u, uNewCs, pCtx->ecx));
3803 pCtx->rip = pCtx->rcx;
3804 pCtx->rflags.u |= X86_EFL_IF;
3805 pCtx->cs.Attr.u = X86DESCATTR_P | X86DESCATTR_G | X86DESCATTR_D | X86DESCATTR_DT | X86_SEL_TYPE_ER_ACC
3806 | (3 << X86DESCATTR_DPL_SHIFT);
3807 }
3808 pCtx->cs.Sel = uNewCs | 3;
3809 pCtx->cs.ValidSel = uNewCs | 3;
3810 pCtx->cs.u64Base = 0;
3811 pCtx->cs.u32Limit = UINT32_MAX;
3812 pCtx->cs.fFlags = CPUMSELREG_FLAGS_VALID;
3813
3814 pCtx->ss.Sel = uNewSs | 3;
3815 pCtx->ss.ValidSel = uNewSs | 3;
3816 pCtx->ss.fFlags = CPUMSELREG_FLAGS_VALID;
3817 /* The SS hidden bits remains unchanged says AMD. To that I say "Yeah, right!". */
3818 pCtx->ss.Attr.u |= (3 << X86DESCATTR_DPL_SHIFT);
3819 /** @todo Testcase: verify that SS.u1Long and SS.u1DefBig are left unchanged
3820 * on sysret. */
3821
3822 return VINF_SUCCESS;
3823}
3824
3825
3826/**
3827 * Common worker for 'pop SReg', 'mov SReg, GReg' and 'lXs GReg, reg/mem'.
3828 *
3829 * @param iSegReg The segment register number (valid).
3830 * @param uSel The new selector value.
3831 */
3832IEM_CIMPL_DEF_2(iemCImpl_LoadSReg, uint8_t, iSegReg, uint16_t, uSel)
3833{
3834 /*PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);*/
3835 uint16_t *pSel = iemSRegRef(pIemCpu, iSegReg);
3836 PCPUMSELREGHID pHid = iemSRegGetHid(pIemCpu, iSegReg);
3837
3838 Assert(iSegReg <= X86_SREG_GS && iSegReg != X86_SREG_CS);
3839
3840 /*
3841 * Real mode and V8086 mode are easy.
3842 */
3843 if ( pIemCpu->enmCpuMode == IEMMODE_16BIT
3844 && IEM_IS_REAL_OR_V86_MODE(pIemCpu))
3845 {
3846 *pSel = uSel;
3847 pHid->u64Base = (uint32_t)uSel << 4;
3848 pHid->ValidSel = uSel;
3849 pHid->fFlags = CPUMSELREG_FLAGS_VALID;
3850#if 0 /* AMD Volume 2, chapter 4.1 - "real mode segmentation" - states that limit and attributes are untouched. */
3851 /** @todo Does the CPU actually load limits and attributes in the
3852 * real/V8086 mode segment load case? It doesn't for CS in far
3853 * jumps... Affects unreal mode. */
3854 pHid->u32Limit = 0xffff;
3855 pHid->Attr.u = 0;
3856 pHid->Attr.n.u1Present = 1;
3857 pHid->Attr.n.u1DescType = 1;
3858 pHid->Attr.n.u4Type = iSegReg != X86_SREG_CS
3859 ? X86_SEL_TYPE_RW
3860 : X86_SEL_TYPE_READ | X86_SEL_TYPE_CODE;
3861#endif
3862 CPUMSetChangedFlags(IEMCPU_TO_VMCPU(pIemCpu), CPUM_CHANGED_HIDDEN_SEL_REGS);
3863 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
3864 return VINF_SUCCESS;
3865 }
3866
3867 /*
3868 * Protected mode.
3869 *
3870 * Check if it's a null segment selector value first, that's OK for DS, ES,
3871 * FS and GS. If not null, then we have to load and parse the descriptor.
3872 */
3873 if (!(uSel & X86_SEL_MASK_OFF_RPL))
3874 {
3875 Assert(iSegReg != X86_SREG_CS); /** @todo testcase for \#UD on MOV CS, ax! */
3876 if (iSegReg == X86_SREG_SS)
3877 {
3878 /* In 64-bit kernel mode, the stack can be 0 because of the way
3879 interrupts are dispatched. AMD seems to have a slighly more
3880 relaxed relationship to SS.RPL than intel does. */
3881 /** @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! */
3882 if ( pIemCpu->enmCpuMode != IEMMODE_64BIT
3883 || pIemCpu->uCpl > 2
3884 || ( uSel != pIemCpu->uCpl
3885 && !IEM_IS_GUEST_CPU_AMD(pIemCpu)) )
3886 {
3887 Log(("load sreg %#x -> invalid stack selector, #GP(0)\n", uSel));
3888 return iemRaiseGeneralProtectionFault0(pIemCpu);
3889 }
3890 }
3891
3892 *pSel = uSel; /* Not RPL, remember :-) */
3893 iemHlpLoadNullDataSelectorProt(pIemCpu, pHid, uSel);
3894 if (iSegReg == X86_SREG_SS)
3895 pHid->Attr.u |= pIemCpu->uCpl << X86DESCATTR_DPL_SHIFT;
3896
3897 Assert(CPUMSELREG_ARE_HIDDEN_PARTS_VALID(IEMCPU_TO_VMCPU(pIemCpu), pHid));
3898 CPUMSetChangedFlags(IEMCPU_TO_VMCPU(pIemCpu), CPUM_CHANGED_HIDDEN_SEL_REGS);
3899
3900 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
3901 return VINF_SUCCESS;
3902 }
3903
3904 /* Fetch the descriptor. */
3905 IEMSELDESC Desc;
3906 VBOXSTRICTRC rcStrict = iemMemFetchSelDesc(pIemCpu, &Desc, uSel, X86_XCPT_GP); /** @todo Correct exception? */
3907 if (rcStrict != VINF_SUCCESS)
3908 return rcStrict;
3909
3910 /* Check GPs first. */
3911 if (!Desc.Legacy.Gen.u1DescType)
3912 {
3913 Log(("load sreg %d (=%#x) - system selector (%#x) -> #GP\n", iSegReg, uSel, Desc.Legacy.Gen.u4Type));
3914 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel);
3915 }
3916 if (iSegReg == X86_SREG_SS) /* SS gets different treatment */
3917 {
3918 if ( (Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_CODE)
3919 || !(Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_WRITE) )
3920 {
3921 Log(("load sreg SS, %#x - code or read only (%#x) -> #GP\n", uSel, Desc.Legacy.Gen.u4Type));
3922 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel);
3923 }
3924 if ((uSel & X86_SEL_RPL) != pIemCpu->uCpl)
3925 {
3926 Log(("load sreg SS, %#x - RPL and CPL (%d) differs -> #GP\n", uSel, pIemCpu->uCpl));
3927 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel);
3928 }
3929 if (Desc.Legacy.Gen.u2Dpl != pIemCpu->uCpl)
3930 {
3931 Log(("load sreg SS, %#x - DPL (%d) and CPL (%d) differs -> #GP\n", uSel, Desc.Legacy.Gen.u2Dpl, pIemCpu->uCpl));
3932 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel);
3933 }
3934 }
3935 else
3936 {
3937 if ((Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_READ)) == X86_SEL_TYPE_CODE)
3938 {
3939 Log(("load sreg%u, %#x - execute only segment -> #GP\n", iSegReg, uSel));
3940 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel);
3941 }
3942 if ( (Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
3943 != (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
3944 {
3945#if 0 /* this is what intel says. */
3946 if ( (uSel & X86_SEL_RPL) > Desc.Legacy.Gen.u2Dpl
3947 && pIemCpu->uCpl > Desc.Legacy.Gen.u2Dpl)
3948 {
3949 Log(("load sreg%u, %#x - both RPL (%d) and CPL (%d) are greater than DPL (%d) -> #GP\n",
3950 iSegReg, uSel, (uSel & X86_SEL_RPL), pIemCpu->uCpl, Desc.Legacy.Gen.u2Dpl));
3951 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel);
3952 }
3953#else /* this is what makes more sense. */
3954 if ((unsigned)(uSel & X86_SEL_RPL) > Desc.Legacy.Gen.u2Dpl)
3955 {
3956 Log(("load sreg%u, %#x - RPL (%d) is greater than DPL (%d) -> #GP\n",
3957 iSegReg, uSel, (uSel & X86_SEL_RPL), Desc.Legacy.Gen.u2Dpl));
3958 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel);
3959 }
3960 if (pIemCpu->uCpl > Desc.Legacy.Gen.u2Dpl)
3961 {
3962 Log(("load sreg%u, %#x - CPL (%d) is greater than DPL (%d) -> #GP\n",
3963 iSegReg, uSel, pIemCpu->uCpl, Desc.Legacy.Gen.u2Dpl));
3964 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uSel);
3965 }
3966#endif
3967 }
3968 }
3969
3970 /* Is it there? */
3971 if (!Desc.Legacy.Gen.u1Present)
3972 {
3973 Log(("load sreg%d,%#x - segment not present -> #NP\n", iSegReg, uSel));
3974 return iemRaiseSelectorNotPresentBySelector(pIemCpu, uSel);
3975 }
3976
3977 /* The base and limit. */
3978 uint32_t cbLimit = X86DESC_LIMIT_G(&Desc.Legacy);
3979 uint64_t u64Base;
3980 if ( pIemCpu->enmCpuMode == IEMMODE_64BIT
3981 && iSegReg < X86_SREG_FS)
3982 u64Base = 0;
3983 else
3984 u64Base = X86DESC_BASE(&Desc.Legacy);
3985
3986 /*
3987 * Ok, everything checked out fine. Now set the accessed bit before
3988 * committing the result into the registers.
3989 */
3990 if (!(Desc.Legacy.Gen.u4Type & X86_SEL_TYPE_ACCESSED))
3991 {
3992 rcStrict = iemMemMarkSelDescAccessed(pIemCpu, uSel);
3993 if (rcStrict != VINF_SUCCESS)
3994 return rcStrict;
3995 Desc.Legacy.Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
3996 }
3997
3998 /* commit */
3999 *pSel = uSel;
4000 pHid->Attr.u = X86DESC_GET_HID_ATTR(&Desc.Legacy);
4001 pHid->u32Limit = cbLimit;
4002 pHid->u64Base = u64Base;
4003 pHid->ValidSel = uSel;
4004 pHid->fFlags = CPUMSELREG_FLAGS_VALID;
4005
4006 /** @todo check if the hidden bits are loaded correctly for 64-bit
4007 * mode. */
4008 Assert(CPUMSELREG_ARE_HIDDEN_PARTS_VALID(IEMCPU_TO_VMCPU(pIemCpu), pHid));
4009
4010 CPUMSetChangedFlags(IEMCPU_TO_VMCPU(pIemCpu), CPUM_CHANGED_HIDDEN_SEL_REGS);
4011 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
4012 return VINF_SUCCESS;
4013}
4014
4015
4016/**
4017 * Implements 'mov SReg, r/m'.
4018 *
4019 * @param iSegReg The segment register number (valid).
4020 * @param uSel The new selector value.
4021 */
4022IEM_CIMPL_DEF_2(iemCImpl_load_SReg, uint8_t, iSegReg, uint16_t, uSel)
4023{
4024 VBOXSTRICTRC rcStrict = IEM_CIMPL_CALL_2(iemCImpl_LoadSReg, iSegReg, uSel);
4025 if (rcStrict == VINF_SUCCESS)
4026 {
4027 if (iSegReg == X86_SREG_SS)
4028 {
4029 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
4030 EMSetInhibitInterruptsPC(IEMCPU_TO_VMCPU(pIemCpu), pCtx->rip);
4031 }
4032 }
4033 return rcStrict;
4034}
4035
4036
4037/**
4038 * Implements 'pop SReg'.
4039 *
4040 * @param iSegReg The segment register number (valid).
4041 * @param enmEffOpSize The efficient operand size (valid).
4042 */
4043IEM_CIMPL_DEF_2(iemCImpl_pop_Sreg, uint8_t, iSegReg, IEMMODE, enmEffOpSize)
4044{
4045 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
4046 VBOXSTRICTRC rcStrict;
4047
4048 /*
4049 * Read the selector off the stack and join paths with mov ss, reg.
4050 */
4051 RTUINT64U TmpRsp;
4052 TmpRsp.u = pCtx->rsp;
4053 switch (enmEffOpSize)
4054 {
4055 case IEMMODE_16BIT:
4056 {
4057 uint16_t uSel;
4058 rcStrict = iemMemStackPopU16Ex(pIemCpu, &uSel, &TmpRsp);
4059 if (rcStrict == VINF_SUCCESS)
4060 rcStrict = IEM_CIMPL_CALL_2(iemCImpl_LoadSReg, iSegReg, uSel);
4061 break;
4062 }
4063
4064 case IEMMODE_32BIT:
4065 {
4066 uint32_t u32Value;
4067 rcStrict = iemMemStackPopU32Ex(pIemCpu, &u32Value, &TmpRsp);
4068 if (rcStrict == VINF_SUCCESS)
4069 rcStrict = IEM_CIMPL_CALL_2(iemCImpl_LoadSReg, iSegReg, (uint16_t)u32Value);
4070 break;
4071 }
4072
4073 case IEMMODE_64BIT:
4074 {
4075 uint64_t u64Value;
4076 rcStrict = iemMemStackPopU64Ex(pIemCpu, &u64Value, &TmpRsp);
4077 if (rcStrict == VINF_SUCCESS)
4078 rcStrict = IEM_CIMPL_CALL_2(iemCImpl_LoadSReg, iSegReg, (uint16_t)u64Value);
4079 break;
4080 }
4081 IEM_NOT_REACHED_DEFAULT_CASE_RET();
4082 }
4083
4084 /*
4085 * Commit the stack on success.
4086 */
4087 if (rcStrict == VINF_SUCCESS)
4088 {
4089 pCtx->rsp = TmpRsp.u;
4090 if (iSegReg == X86_SREG_SS)
4091 EMSetInhibitInterruptsPC(IEMCPU_TO_VMCPU(pIemCpu), pCtx->rip);
4092 }
4093 return rcStrict;
4094}
4095
4096
4097/**
4098 * Implements lgs, lfs, les, lds & lss.
4099 */
4100IEM_CIMPL_DEF_5(iemCImpl_load_SReg_Greg,
4101 uint16_t, uSel,
4102 uint64_t, offSeg,
4103 uint8_t, iSegReg,
4104 uint8_t, iGReg,
4105 IEMMODE, enmEffOpSize)
4106{
4107 /*PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);*/
4108 VBOXSTRICTRC rcStrict;
4109
4110 /*
4111 * Use iemCImpl_LoadSReg to do the tricky segment register loading.
4112 */
4113 /** @todo verify and test that mov, pop and lXs works the segment
4114 * register loading in the exact same way. */
4115 rcStrict = IEM_CIMPL_CALL_2(iemCImpl_LoadSReg, iSegReg, uSel);
4116 if (rcStrict == VINF_SUCCESS)
4117 {
4118 switch (enmEffOpSize)
4119 {
4120 case IEMMODE_16BIT:
4121 *(uint16_t *)iemGRegRef(pIemCpu, iGReg) = offSeg;
4122 break;
4123 case IEMMODE_32BIT:
4124 *(uint64_t *)iemGRegRef(pIemCpu, iGReg) = offSeg;
4125 break;
4126 case IEMMODE_64BIT:
4127 *(uint64_t *)iemGRegRef(pIemCpu, iGReg) = offSeg;
4128 break;
4129 IEM_NOT_REACHED_DEFAULT_CASE_RET();
4130 }
4131 }
4132
4133 return rcStrict;
4134}
4135
4136
4137/**
4138 * Helper for VERR, VERW, LAR, and LSL and loads the descriptor into memory.
4139 *
4140 * @retval VINF_SUCCESS on success.
4141 * @retval VINF_IEM_SELECTOR_NOT_OK if the selector isn't ok.
4142 * @retval iemMemFetchSysU64 return value.
4143 *
4144 * @param pIemCpu The IEM state of the calling EMT.
4145 * @param uSel The selector value.
4146 * @param fAllowSysDesc Whether system descriptors are OK or not.
4147 * @param pDesc Where to return the descriptor on success.
4148 */
4149static VBOXSTRICTRC iemCImpl_LoadDescHelper(PIEMCPU pIemCpu, uint16_t uSel, bool fAllowSysDesc, PIEMSELDESC pDesc)
4150{
4151 pDesc->Long.au64[0] = 0;
4152 pDesc->Long.au64[1] = 0;
4153
4154 if (!(uSel & X86_SEL_MASK_OFF_RPL)) /** @todo test this on 64-bit. */
4155 return VINF_IEM_SELECTOR_NOT_OK;
4156
4157 /* Within the table limits? */
4158 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
4159 RTGCPTR GCPtrBase;
4160 if (uSel & X86_SEL_LDT)
4161 {
4162 if ( !pCtx->ldtr.Attr.n.u1Present
4163 || (uSel | X86_SEL_RPL_LDT) > pCtx->ldtr.u32Limit )
4164 return VINF_IEM_SELECTOR_NOT_OK;
4165 GCPtrBase = pCtx->ldtr.u64Base;
4166 }
4167 else
4168 {
4169 if ((uSel | X86_SEL_RPL_LDT) > pCtx->gdtr.cbGdt)
4170 return VINF_IEM_SELECTOR_NOT_OK;
4171 GCPtrBase = pCtx->gdtr.pGdt;
4172 }
4173
4174 /* Fetch the descriptor. */
4175 VBOXSTRICTRC rcStrict = iemMemFetchSysU64(pIemCpu, &pDesc->Legacy.u, UINT8_MAX, GCPtrBase + (uSel & X86_SEL_MASK));
4176 if (rcStrict != VINF_SUCCESS)
4177 return rcStrict;
4178 if (!pDesc->Legacy.Gen.u1DescType)
4179 {
4180 if (!fAllowSysDesc)
4181 return VINF_IEM_SELECTOR_NOT_OK;
4182 if (CPUMIsGuestInLongModeEx(pCtx))
4183 {
4184 rcStrict = iemMemFetchSysU64(pIemCpu, &pDesc->Long.au64[1], UINT8_MAX, GCPtrBase + (uSel & X86_SEL_MASK) + 8);
4185 if (rcStrict != VINF_SUCCESS)
4186 return rcStrict;
4187 }
4188
4189 }
4190
4191 return VINF_SUCCESS;
4192}
4193
4194
4195/**
4196 * Implements verr (fWrite = false) and verw (fWrite = true).
4197 */
4198IEM_CIMPL_DEF_2(iemCImpl_VerX, uint16_t, uSel, bool, fWrite)
4199{
4200 Assert(!IEM_IS_REAL_OR_V86_MODE(pIemCpu));
4201
4202 /** @todo figure whether the accessed bit is set or not. */
4203
4204 bool fAccessible = true;
4205 IEMSELDESC Desc;
4206 VBOXSTRICTRC rcStrict = iemCImpl_LoadDescHelper(pIemCpu, uSel, false /*fAllowSysDesc*/, &Desc);
4207 if (rcStrict == VINF_SUCCESS)
4208 {
4209 /* Check the descriptor, order doesn't matter much here. */
4210 if ( !Desc.Legacy.Gen.u1DescType
4211 || !Desc.Legacy.Gen.u1Present)
4212 fAccessible = false;
4213 else
4214 {
4215 if ( fWrite
4216 ? (Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_WRITE)) != X86_SEL_TYPE_WRITE
4217 : (Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_READ)) == X86_SEL_TYPE_CODE)
4218 fAccessible = false;
4219
4220 /** @todo testcase for the conforming behavior. */
4221 if ( (Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
4222 != (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
4223 {
4224 if ((unsigned)(uSel & X86_SEL_RPL) > Desc.Legacy.Gen.u2Dpl)
4225 fAccessible = false;
4226 else if (pIemCpu->uCpl > Desc.Legacy.Gen.u2Dpl)
4227 fAccessible = false;
4228 }
4229 }
4230
4231 }
4232 else if (rcStrict == VINF_IEM_SELECTOR_NOT_OK)
4233 fAccessible = false;
4234 else
4235 return rcStrict;
4236
4237 /* commit */
4238 pIemCpu->CTX_SUFF(pCtx)->eflags.Bits.u1ZF = fAccessible;
4239
4240 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
4241 return VINF_SUCCESS;
4242}
4243
4244
4245/**
4246 * Implements LAR and LSL with 64-bit operand size.
4247 *
4248 * @returns VINF_SUCCESS.
4249 * @param pu16Dst Pointer to the destination register.
4250 * @param uSel The selector to load details for.
4251 * @param pEFlags Pointer to the eflags register.
4252 * @param fIsLar true = LAR, false = LSL.
4253 */
4254IEM_CIMPL_DEF_4(iemCImpl_LarLsl_u64, uint64_t *, pu64Dst, uint16_t, uSel, uint32_t *, pEFlags, bool, fIsLar)
4255{
4256 Assert(!IEM_IS_REAL_OR_V86_MODE(pIemCpu));
4257
4258 /** @todo figure whether the accessed bit is set or not. */
4259
4260 bool fDescOk = true;
4261 IEMSELDESC Desc;
4262 VBOXSTRICTRC rcStrict = iemCImpl_LoadDescHelper(pIemCpu, uSel, false /*fAllowSysDesc*/, &Desc);
4263 if (rcStrict == VINF_SUCCESS)
4264 {
4265 /*
4266 * Check the descriptor type.
4267 */
4268 if (!Desc.Legacy.Gen.u1DescType)
4269 {
4270 if (CPUMIsGuestInLongModeEx(pIemCpu->CTX_SUFF(pCtx)))
4271 {
4272 if (Desc.Long.Gen.u5Zeros)
4273 fDescOk = false;
4274 else
4275 switch (Desc.Long.Gen.u4Type)
4276 {
4277 /** @todo Intel lists 0 as valid for LSL, verify whether that's correct */
4278 case AMD64_SEL_TYPE_SYS_TSS_AVAIL:
4279 case AMD64_SEL_TYPE_SYS_TSS_BUSY:
4280 case AMD64_SEL_TYPE_SYS_LDT: /** @todo Intel lists this as invalid for LAR, AMD and 32-bit does otherwise. */
4281 break;
4282 case AMD64_SEL_TYPE_SYS_CALL_GATE:
4283 fDescOk = fIsLar;
4284 break;
4285 default:
4286 fDescOk = false;
4287 break;
4288 }
4289 }
4290 else
4291 {
4292 switch (Desc.Long.Gen.u4Type)
4293 {
4294 case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
4295 case X86_SEL_TYPE_SYS_286_TSS_BUSY:
4296 case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
4297 case X86_SEL_TYPE_SYS_386_TSS_BUSY:
4298 case X86_SEL_TYPE_SYS_LDT:
4299 break;
4300 case X86_SEL_TYPE_SYS_286_CALL_GATE:
4301 case X86_SEL_TYPE_SYS_TASK_GATE:
4302 case X86_SEL_TYPE_SYS_386_CALL_GATE:
4303 fDescOk = fIsLar;
4304 break;
4305 default:
4306 fDescOk = false;
4307 break;
4308 }
4309 }
4310 }
4311 if (fDescOk)
4312 {
4313 /*
4314 * Check the RPL/DPL/CPL interaction..
4315 */
4316 /** @todo testcase for the conforming behavior. */
4317 if ( (Desc.Legacy.Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF)) != (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF)
4318 || !Desc.Legacy.Gen.u1DescType)
4319 {
4320 if ((unsigned)(uSel & X86_SEL_RPL) > Desc.Legacy.Gen.u2Dpl)
4321 fDescOk = false;
4322 else if (pIemCpu->uCpl > Desc.Legacy.Gen.u2Dpl)
4323 fDescOk = false;
4324 }
4325 }
4326
4327 if (fDescOk)
4328 {
4329 /*
4330 * All fine, start committing the result.
4331 */
4332 if (fIsLar)
4333 *pu64Dst = Desc.Legacy.au32[1] & UINT32_C(0x00ffff00);
4334 else
4335 *pu64Dst = X86DESC_LIMIT_G(&Desc.Legacy);
4336 }
4337
4338 }
4339 else if (rcStrict == VINF_IEM_SELECTOR_NOT_OK)
4340 fDescOk = false;
4341 else
4342 return rcStrict;
4343
4344 /* commit flags value and advance rip. */
4345 pIemCpu->CTX_SUFF(pCtx)->eflags.Bits.u1ZF = fDescOk;
4346 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
4347
4348 return VINF_SUCCESS;
4349}
4350
4351
4352/**
4353 * Implements LAR and LSL with 16-bit operand size.
4354 *
4355 * @returns VINF_SUCCESS.
4356 * @param pu16Dst Pointer to the destination register.
4357 * @param u16Sel The selector to load details for.
4358 * @param pEFlags Pointer to the eflags register.
4359 * @param fIsLar true = LAR, false = LSL.
4360 */
4361IEM_CIMPL_DEF_4(iemCImpl_LarLsl_u16, uint16_t *, pu16Dst, uint16_t, uSel, uint32_t *, pEFlags, bool, fIsLar)
4362{
4363 uint64_t u64TmpDst = *pu16Dst;
4364 IEM_CIMPL_CALL_4(iemCImpl_LarLsl_u64, &u64TmpDst, uSel, pEFlags, fIsLar);
4365 *pu16Dst = (uint16_t)u64TmpDst;
4366 return VINF_SUCCESS;
4367}
4368
4369
4370/**
4371 * Implements lgdt.
4372 *
4373 * @param iEffSeg The segment of the new gdtr contents
4374 * @param GCPtrEffSrc The address of the new gdtr contents.
4375 * @param enmEffOpSize The effective operand size.
4376 */
4377IEM_CIMPL_DEF_3(iemCImpl_lgdt, uint8_t, iEffSeg, RTGCPTR, GCPtrEffSrc, IEMMODE, enmEffOpSize)
4378{
4379 if (pIemCpu->uCpl != 0)
4380 return iemRaiseGeneralProtectionFault0(pIemCpu);
4381 Assert(!pIemCpu->CTX_SUFF(pCtx)->eflags.Bits.u1VM);
4382
4383 /*
4384 * Fetch the limit and base address.
4385 */
4386 uint16_t cbLimit;
4387 RTGCPTR GCPtrBase;
4388 VBOXSTRICTRC rcStrict = iemMemFetchDataXdtr(pIemCpu, &cbLimit, &GCPtrBase, iEffSeg, GCPtrEffSrc, enmEffOpSize);
4389 if (rcStrict == VINF_SUCCESS)
4390 {
4391 if (!IEM_FULL_VERIFICATION_ENABLED(pIemCpu))
4392 rcStrict = CPUMSetGuestGDTR(IEMCPU_TO_VMCPU(pIemCpu), GCPtrBase, cbLimit);
4393 else
4394 {
4395 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
4396 pCtx->gdtr.cbGdt = cbLimit;
4397 pCtx->gdtr.pGdt = GCPtrBase;
4398 }
4399 if (rcStrict == VINF_SUCCESS)
4400 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
4401 }
4402 return rcStrict;
4403}
4404
4405
4406/**
4407 * Implements sgdt.
4408 *
4409 * @param iEffSeg The segment where to store the gdtr content.
4410 * @param GCPtrEffDst The address where to store the gdtr content.
4411 * @param enmEffOpSize The effective operand size.
4412 */
4413IEM_CIMPL_DEF_3(iemCImpl_sgdt, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst, IEMMODE, enmEffOpSize)
4414{
4415 /*
4416 * Join paths with sidt.
4417 * Note! No CPL or V8086 checks here, it's a really sad story, ask Intel if
4418 * you really must know.
4419 */
4420 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
4421 VBOXSTRICTRC rcStrict = iemMemStoreDataXdtr(pIemCpu, pCtx->gdtr.cbGdt, pCtx->gdtr.pGdt, iEffSeg, GCPtrEffDst, enmEffOpSize);
4422 if (rcStrict == VINF_SUCCESS)
4423 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
4424 return rcStrict;
4425}
4426
4427
4428/**
4429 * Implements lidt.
4430 *
4431 * @param iEffSeg The segment of the new idtr contents
4432 * @param GCPtrEffSrc The address of the new idtr contents.
4433 * @param enmEffOpSize The effective operand size.
4434 */
4435IEM_CIMPL_DEF_3(iemCImpl_lidt, uint8_t, iEffSeg, RTGCPTR, GCPtrEffSrc, IEMMODE, enmEffOpSize)
4436{
4437 if (pIemCpu->uCpl != 0)
4438 return iemRaiseGeneralProtectionFault0(pIemCpu);
4439 Assert(!pIemCpu->CTX_SUFF(pCtx)->eflags.Bits.u1VM);
4440
4441 /*
4442 * Fetch the limit and base address.
4443 */
4444 uint16_t cbLimit;
4445 RTGCPTR GCPtrBase;
4446 VBOXSTRICTRC rcStrict = iemMemFetchDataXdtr(pIemCpu, &cbLimit, &GCPtrBase, iEffSeg, GCPtrEffSrc, enmEffOpSize);
4447 if (rcStrict == VINF_SUCCESS)
4448 {
4449 if (!IEM_FULL_VERIFICATION_ENABLED(pIemCpu))
4450 CPUMSetGuestIDTR(IEMCPU_TO_VMCPU(pIemCpu), GCPtrBase, cbLimit);
4451 else
4452 {
4453 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
4454 pCtx->idtr.cbIdt = cbLimit;
4455 pCtx->idtr.pIdt = GCPtrBase;
4456 }
4457 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
4458 }
4459 return rcStrict;
4460}
4461
4462
4463/**
4464 * Implements sidt.
4465 *
4466 * @param iEffSeg The segment where to store the idtr content.
4467 * @param GCPtrEffDst The address where to store the idtr content.
4468 * @param enmEffOpSize The effective operand size.
4469 */
4470IEM_CIMPL_DEF_3(iemCImpl_sidt, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst, IEMMODE, enmEffOpSize)
4471{
4472 /*
4473 * Join paths with sgdt.
4474 * Note! No CPL or V8086 checks here, it's a really sad story, ask Intel if
4475 * you really must know.
4476 */
4477 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
4478 VBOXSTRICTRC rcStrict = iemMemStoreDataXdtr(pIemCpu, pCtx->idtr.cbIdt, pCtx->idtr.pIdt, iEffSeg, GCPtrEffDst, enmEffOpSize);
4479 if (rcStrict == VINF_SUCCESS)
4480 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
4481 return rcStrict;
4482}
4483
4484
4485/**
4486 * Implements lldt.
4487 *
4488 * @param uNewLdt The new LDT selector value.
4489 */
4490IEM_CIMPL_DEF_1(iemCImpl_lldt, uint16_t, uNewLdt)
4491{
4492 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
4493
4494 /*
4495 * Check preconditions.
4496 */
4497 if (IEM_IS_REAL_OR_V86_MODE(pIemCpu))
4498 {
4499 Log(("lldt %04x - real or v8086 mode -> #GP(0)\n", uNewLdt));
4500 return iemRaiseUndefinedOpcode(pIemCpu);
4501 }
4502 if (pIemCpu->uCpl != 0)
4503 {
4504 Log(("lldt %04x - CPL is %d -> #GP(0)\n", uNewLdt, pIemCpu->uCpl));
4505 return iemRaiseGeneralProtectionFault0(pIemCpu);
4506 }
4507 if (uNewLdt & X86_SEL_LDT)
4508 {
4509 Log(("lldt %04x - LDT selector -> #GP\n", uNewLdt));
4510 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewLdt);
4511 }
4512
4513 /*
4514 * Now, loading a NULL selector is easy.
4515 */
4516 if (!(uNewLdt & X86_SEL_MASK_OFF_RPL))
4517 {
4518 Log(("lldt %04x: Loading NULL selector.\n", uNewLdt));
4519 if (!IEM_FULL_VERIFICATION_ENABLED(pIemCpu))
4520 CPUMSetGuestLDTR(IEMCPU_TO_VMCPU(pIemCpu), uNewLdt);
4521 else
4522 pCtx->ldtr.Sel = uNewLdt;
4523 pCtx->ldtr.ValidSel = uNewLdt;
4524 pCtx->ldtr.fFlags = CPUMSELREG_FLAGS_VALID;
4525 if (IEM_FULL_VERIFICATION_REM_ENABLED(pIemCpu))
4526 {
4527 pCtx->ldtr.Attr.u = X86DESCATTR_UNUSABLE;
4528 pCtx->ldtr.u64Base = pCtx->ldtr.u32Limit = 0; /* For verfication against REM. */
4529 }
4530 else if (IEM_IS_GUEST_CPU_AMD(pIemCpu))
4531 {
4532 /* AMD-V seems to leave the base and limit alone. */
4533 pCtx->ldtr.Attr.u = X86DESCATTR_UNUSABLE;
4534 }
4535 else if (!IEM_FULL_VERIFICATION_REM_ENABLED(pIemCpu))
4536 {
4537 /* VT-x (Intel 3960x) seems to be doing the following. */
4538 pCtx->ldtr.Attr.u = X86DESCATTR_UNUSABLE | X86DESCATTR_G | X86DESCATTR_D;
4539 pCtx->ldtr.u64Base = 0;
4540 pCtx->ldtr.u32Limit = UINT32_MAX;
4541 }
4542
4543 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
4544 return VINF_SUCCESS;
4545 }
4546
4547 /*
4548 * Read the descriptor.
4549 */
4550 IEMSELDESC Desc;
4551 VBOXSTRICTRC rcStrict = iemMemFetchSelDesc(pIemCpu, &Desc, uNewLdt, X86_XCPT_GP); /** @todo Correct exception? */
4552 if (rcStrict != VINF_SUCCESS)
4553 return rcStrict;
4554
4555 /* Check GPs first. */
4556 if (Desc.Legacy.Gen.u1DescType)
4557 {
4558 Log(("lldt %#x - not system selector (type %x) -> #GP\n", uNewLdt, Desc.Legacy.Gen.u4Type));
4559 return iemRaiseGeneralProtectionFault(pIemCpu, uNewLdt & X86_SEL_MASK_OFF_RPL);
4560 }
4561 if (Desc.Legacy.Gen.u4Type != X86_SEL_TYPE_SYS_LDT)
4562 {
4563 Log(("lldt %#x - not LDT selector (type %x) -> #GP\n", uNewLdt, Desc.Legacy.Gen.u4Type));
4564 return iemRaiseGeneralProtectionFault(pIemCpu, uNewLdt & X86_SEL_MASK_OFF_RPL);
4565 }
4566 uint64_t u64Base;
4567 if (!IEM_IS_LONG_MODE(pIemCpu))
4568 u64Base = X86DESC_BASE(&Desc.Legacy);
4569 else
4570 {
4571 if (Desc.Long.Gen.u5Zeros)
4572 {
4573 Log(("lldt %#x - u5Zeros=%#x -> #GP\n", uNewLdt, Desc.Long.Gen.u5Zeros));
4574 return iemRaiseGeneralProtectionFault(pIemCpu, uNewLdt & X86_SEL_MASK_OFF_RPL);
4575 }
4576
4577 u64Base = X86DESC64_BASE(&Desc.Long);
4578 if (!IEM_IS_CANONICAL(u64Base))
4579 {
4580 Log(("lldt %#x - non-canonical base address %#llx -> #GP\n", uNewLdt, u64Base));
4581 return iemRaiseGeneralProtectionFault(pIemCpu, uNewLdt & X86_SEL_MASK_OFF_RPL);
4582 }
4583 }
4584
4585 /* NP */
4586 if (!Desc.Legacy.Gen.u1Present)
4587 {
4588 Log(("lldt %#x - segment not present -> #NP\n", uNewLdt));
4589 return iemRaiseSelectorNotPresentBySelector(pIemCpu, uNewLdt);
4590 }
4591
4592 /*
4593 * It checks out alright, update the registers.
4594 */
4595/** @todo check if the actual value is loaded or if the RPL is dropped */
4596 if (!IEM_FULL_VERIFICATION_ENABLED(pIemCpu))
4597 CPUMSetGuestLDTR(IEMCPU_TO_VMCPU(pIemCpu), uNewLdt & X86_SEL_MASK_OFF_RPL);
4598 else
4599 pCtx->ldtr.Sel = uNewLdt & X86_SEL_MASK_OFF_RPL;
4600 pCtx->ldtr.ValidSel = uNewLdt & X86_SEL_MASK_OFF_RPL;
4601 pCtx->ldtr.fFlags = CPUMSELREG_FLAGS_VALID;
4602 pCtx->ldtr.Attr.u = X86DESC_GET_HID_ATTR(&Desc.Legacy);
4603 pCtx->ldtr.u32Limit = X86DESC_LIMIT_G(&Desc.Legacy);
4604 pCtx->ldtr.u64Base = u64Base;
4605
4606 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
4607 return VINF_SUCCESS;
4608}
4609
4610
4611/**
4612 * Implements lldt.
4613 *
4614 * @param uNewLdt The new LDT selector value.
4615 */
4616IEM_CIMPL_DEF_1(iemCImpl_ltr, uint16_t, uNewTr)
4617{
4618 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
4619
4620 /*
4621 * Check preconditions.
4622 */
4623 if (IEM_IS_REAL_OR_V86_MODE(pIemCpu))
4624 {
4625 Log(("ltr %04x - real or v8086 mode -> #GP(0)\n", uNewTr));
4626 return iemRaiseUndefinedOpcode(pIemCpu);
4627 }
4628 if (pIemCpu->uCpl != 0)
4629 {
4630 Log(("ltr %04x - CPL is %d -> #GP(0)\n", uNewTr, pIemCpu->uCpl));
4631 return iemRaiseGeneralProtectionFault0(pIemCpu);
4632 }
4633 if (uNewTr & X86_SEL_LDT)
4634 {
4635 Log(("ltr %04x - LDT selector -> #GP\n", uNewTr));
4636 return iemRaiseGeneralProtectionFaultBySelector(pIemCpu, uNewTr);
4637 }
4638 if (!(uNewTr & X86_SEL_MASK_OFF_RPL))
4639 {
4640 Log(("ltr %04x - NULL selector -> #GP(0)\n", uNewTr));
4641 return iemRaiseGeneralProtectionFault0(pIemCpu);
4642 }
4643
4644 /*
4645 * Read the descriptor.
4646 */
4647 IEMSELDESC Desc;
4648 VBOXSTRICTRC rcStrict = iemMemFetchSelDesc(pIemCpu, &Desc, uNewTr, X86_XCPT_GP); /** @todo Correct exception? */
4649 if (rcStrict != VINF_SUCCESS)
4650 return rcStrict;
4651
4652 /* Check GPs first. */
4653 if (Desc.Legacy.Gen.u1DescType)
4654 {
4655 Log(("ltr %#x - not system selector (type %x) -> #GP\n", uNewTr, Desc.Legacy.Gen.u4Type));
4656 return iemRaiseGeneralProtectionFault(pIemCpu, uNewTr & X86_SEL_MASK_OFF_RPL);
4657 }
4658 if ( Desc.Legacy.Gen.u4Type != X86_SEL_TYPE_SYS_386_TSS_AVAIL /* same as AMD64_SEL_TYPE_SYS_TSS_AVAIL */
4659 && ( Desc.Legacy.Gen.u4Type != X86_SEL_TYPE_SYS_286_TSS_AVAIL
4660 || IEM_IS_LONG_MODE(pIemCpu)) )
4661 {
4662 Log(("ltr %#x - not an available TSS selector (type %x) -> #GP\n", uNewTr, Desc.Legacy.Gen.u4Type));
4663 return iemRaiseGeneralProtectionFault(pIemCpu, uNewTr & X86_SEL_MASK_OFF_RPL);
4664 }
4665 uint64_t u64Base;
4666 if (!IEM_IS_LONG_MODE(pIemCpu))
4667 u64Base = X86DESC_BASE(&Desc.Legacy);
4668 else
4669 {
4670 if (Desc.Long.Gen.u5Zeros)
4671 {
4672 Log(("ltr %#x - u5Zeros=%#x -> #GP\n", uNewTr, Desc.Long.Gen.u5Zeros));
4673 return iemRaiseGeneralProtectionFault(pIemCpu, uNewTr & X86_SEL_MASK_OFF_RPL);
4674 }
4675
4676 u64Base = X86DESC64_BASE(&Desc.Long);
4677 if (!IEM_IS_CANONICAL(u64Base))
4678 {
4679 Log(("ltr %#x - non-canonical base address %#llx -> #GP\n", uNewTr, u64Base));
4680 return iemRaiseGeneralProtectionFault(pIemCpu, uNewTr & X86_SEL_MASK_OFF_RPL);
4681 }
4682 }
4683
4684 /* NP */
4685 if (!Desc.Legacy.Gen.u1Present)
4686 {
4687 Log(("ltr %#x - segment not present -> #NP\n", uNewTr));
4688 return iemRaiseSelectorNotPresentBySelector(pIemCpu, uNewTr);
4689 }
4690
4691 /*
4692 * Set it busy.
4693 * Note! Intel says this should lock down the whole descriptor, but we'll
4694 * restrict our selves to 32-bit for now due to lack of inline
4695 * assembly and such.
4696 */
4697 void *pvDesc;
4698 rcStrict = iemMemMap(pIemCpu, &pvDesc, 8, UINT8_MAX, pCtx->gdtr.pGdt + (uNewTr & X86_SEL_MASK_OFF_RPL), IEM_ACCESS_DATA_RW);
4699 if (rcStrict != VINF_SUCCESS)
4700 return rcStrict;
4701 switch ((uintptr_t)pvDesc & 3)
4702 {
4703 case 0: ASMAtomicBitSet(pvDesc, 40 + 1); break;
4704 case 1: ASMAtomicBitSet((uint8_t *)pvDesc + 3, 40 + 1 - 24); break;
4705 case 2: ASMAtomicBitSet((uint8_t *)pvDesc + 2, 40 + 1 - 16); break;
4706 case 3: ASMAtomicBitSet((uint8_t *)pvDesc + 1, 40 + 1 - 8); break;
4707 }
4708 rcStrict = iemMemCommitAndUnmap(pIemCpu, pvDesc, IEM_ACCESS_DATA_RW);
4709 if (rcStrict != VINF_SUCCESS)
4710 return rcStrict;
4711 Desc.Legacy.Gen.u4Type |= X86_SEL_TYPE_SYS_TSS_BUSY_MASK;
4712
4713 /*
4714 * It checks out alright, update the registers.
4715 */
4716/** @todo check if the actual value is loaded or if the RPL is dropped */
4717 if (!IEM_FULL_VERIFICATION_ENABLED(pIemCpu))
4718 CPUMSetGuestTR(IEMCPU_TO_VMCPU(pIemCpu), uNewTr & X86_SEL_MASK_OFF_RPL);
4719 else
4720 pCtx->tr.Sel = uNewTr & X86_SEL_MASK_OFF_RPL;
4721 pCtx->tr.ValidSel = uNewTr & X86_SEL_MASK_OFF_RPL;
4722 pCtx->tr.fFlags = CPUMSELREG_FLAGS_VALID;
4723 pCtx->tr.Attr.u = X86DESC_GET_HID_ATTR(&Desc.Legacy);
4724 pCtx->tr.u32Limit = X86DESC_LIMIT_G(&Desc.Legacy);
4725 pCtx->tr.u64Base = u64Base;
4726
4727 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
4728 return VINF_SUCCESS;
4729}
4730
4731
4732/**
4733 * Implements mov GReg,CRx.
4734 *
4735 * @param iGReg The general register to store the CRx value in.
4736 * @param iCrReg The CRx register to read (valid).
4737 */
4738IEM_CIMPL_DEF_2(iemCImpl_mov_Rd_Cd, uint8_t, iGReg, uint8_t, iCrReg)
4739{
4740 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
4741 if (pIemCpu->uCpl != 0)
4742 return iemRaiseGeneralProtectionFault0(pIemCpu);
4743 Assert(!pCtx->eflags.Bits.u1VM);
4744
4745 /* read it */
4746 uint64_t crX;
4747 switch (iCrReg)
4748 {
4749 case 0: crX = pCtx->cr0; break;
4750 case 2: crX = pCtx->cr2; break;
4751 case 3: crX = pCtx->cr3; break;
4752 case 4: crX = pCtx->cr4; break;
4753 case 8:
4754 {
4755 uint8_t uTpr;
4756 int rc = PDMApicGetTPR(IEMCPU_TO_VMCPU(pIemCpu), &uTpr, NULL, NULL);
4757 if (RT_SUCCESS(rc))
4758 crX = uTpr >> 4;
4759 else
4760 crX = 0;
4761 break;
4762 }
4763 IEM_NOT_REACHED_DEFAULT_CASE_RET(); /* call checks */
4764 }
4765
4766 /* store it */
4767 if (pIemCpu->enmCpuMode == IEMMODE_64BIT)
4768 *(uint64_t *)iemGRegRef(pIemCpu, iGReg) = crX;
4769 else
4770 *(uint64_t *)iemGRegRef(pIemCpu, iGReg) = (uint32_t)crX;
4771
4772 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
4773 return VINF_SUCCESS;
4774}
4775
4776
4777/**
4778 * Used to implemented 'mov CRx,GReg' and 'lmsw r/m16'.
4779 *
4780 * @param iCrReg The CRx register to write (valid).
4781 * @param uNewCrX The new value.
4782 */
4783IEM_CIMPL_DEF_2(iemCImpl_load_CrX, uint8_t, iCrReg, uint64_t, uNewCrX)
4784{
4785 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
4786 PVMCPU pVCpu = IEMCPU_TO_VMCPU(pIemCpu);
4787 VBOXSTRICTRC rcStrict;
4788 int rc;
4789
4790 /*
4791 * Try store it.
4792 * Unfortunately, CPUM only does a tiny bit of the work.
4793 */
4794 switch (iCrReg)
4795 {
4796 case 0:
4797 {
4798 /*
4799 * Perform checks.
4800 */
4801 uint64_t const uOldCrX = pCtx->cr0;
4802 uNewCrX |= X86_CR0_ET; /* hardcoded */
4803
4804 /* Check for reserved bits. */
4805 uint32_t const fValid = X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS
4806 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM
4807 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG;
4808 if (uNewCrX & ~(uint64_t)fValid)
4809 {
4810 Log(("Trying to set reserved CR0 bits: NewCR0=%#llx InvalidBits=%#llx\n", uNewCrX, uNewCrX & ~(uint64_t)fValid));
4811 return iemRaiseGeneralProtectionFault0(pIemCpu);
4812 }
4813
4814 /* Check for invalid combinations. */
4815 if ( (uNewCrX & X86_CR0_PG)
4816 && !(uNewCrX & X86_CR0_PE) )
4817 {
4818 Log(("Trying to set CR0.PG without CR0.PE\n"));
4819 return iemRaiseGeneralProtectionFault0(pIemCpu);
4820 }
4821
4822 if ( !(uNewCrX & X86_CR0_CD)
4823 && (uNewCrX & X86_CR0_NW) )
4824 {
4825 Log(("Trying to clear CR0.CD while leaving CR0.NW set\n"));
4826 return iemRaiseGeneralProtectionFault0(pIemCpu);
4827 }
4828
4829 /* Long mode consistency checks. */
4830 if ( (uNewCrX & X86_CR0_PG)
4831 && !(uOldCrX & X86_CR0_PG)
4832 && (pCtx->msrEFER & MSR_K6_EFER_LME) )
4833 {
4834 if (!(pCtx->cr4 & X86_CR4_PAE))
4835 {
4836 Log(("Trying to enabled long mode paging without CR4.PAE set\n"));
4837 return iemRaiseGeneralProtectionFault0(pIemCpu);
4838 }
4839 if (pCtx->cs.Attr.n.u1Long)
4840 {
4841 Log(("Trying to enabled long mode paging with a long CS descriptor loaded.\n"));
4842 return iemRaiseGeneralProtectionFault0(pIemCpu);
4843 }
4844 }
4845
4846 /** @todo check reserved PDPTR bits as AMD states. */
4847
4848 /*
4849 * Change CR0.
4850 */
4851 if (!IEM_VERIFICATION_ENABLED(pIemCpu))
4852 CPUMSetGuestCR0(pVCpu, uNewCrX);
4853 else
4854 pCtx->cr0 = uNewCrX;
4855 Assert(pCtx->cr0 == uNewCrX);
4856
4857 /*
4858 * Change EFER.LMA if entering or leaving long mode.
4859 */
4860 if ( (uNewCrX & X86_CR0_PG) != (uOldCrX & X86_CR0_PG)
4861 && (pCtx->msrEFER & MSR_K6_EFER_LME) )
4862 {
4863 uint64_t NewEFER = pCtx->msrEFER;
4864 if (uNewCrX & X86_CR0_PG)
4865 NewEFER |= MSR_K6_EFER_LMA;
4866 else
4867 NewEFER &= ~MSR_K6_EFER_LMA;
4868
4869 if (!IEM_FULL_VERIFICATION_ENABLED(pIemCpu))
4870 CPUMSetGuestEFER(pVCpu, NewEFER);
4871 else
4872 pCtx->msrEFER = NewEFER;
4873 Assert(pCtx->msrEFER == NewEFER);
4874 }
4875
4876 /*
4877 * Inform PGM.
4878 */
4879 if (!IEM_FULL_VERIFICATION_ENABLED(pIemCpu))
4880 {
4881 if ( (uNewCrX & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE))
4882 != (uOldCrX & (X86_CR0_PG | X86_CR0_WP | X86_CR0_PE)) )
4883 {
4884 rc = PGMFlushTLB(pVCpu, pCtx->cr3, true /* global */);
4885 AssertRCReturn(rc, rc);
4886 /* ignore informational status codes */
4887 }
4888 rcStrict = PGMChangeMode(pVCpu, pCtx->cr0, pCtx->cr4, pCtx->msrEFER);
4889 }
4890 else
4891 rcStrict = VINF_SUCCESS;
4892
4893#ifdef IN_RC
4894 /* Return to ring-3 for rescheduling if WP or AM changes. */
4895 if ( rcStrict == VINF_SUCCESS
4896 && ( (uNewCrX & (X86_CR0_WP | X86_CR0_AM))
4897 != (uOldCrX & (X86_CR0_WP | X86_CR0_AM))) )
4898 rcStrict = VINF_EM_RESCHEDULE;
4899#endif
4900 break;
4901 }
4902
4903 /*
4904 * CR2 can be changed without any restrictions.
4905 */
4906 case 2:
4907 pCtx->cr2 = uNewCrX;
4908 rcStrict = VINF_SUCCESS;
4909 break;
4910
4911 /*
4912 * CR3 is relatively simple, although AMD and Intel have different
4913 * accounts of how setting reserved bits are handled. We take intel's
4914 * word for the lower bits and AMD's for the high bits (63:52). The
4915 * lower reserved bits are ignored and left alone; OpenBSD 5.8 relies
4916 * on this.
4917 */
4918 /** @todo Testcase: Setting reserved bits in CR3, especially before
4919 * enabling paging. */
4920 case 3:
4921 {
4922 /* check / mask the value. */
4923 if (uNewCrX & UINT64_C(0xfff0000000000000))
4924 {
4925 Log(("Trying to load CR3 with invalid high bits set: %#llx\n", uNewCrX));
4926 return iemRaiseGeneralProtectionFault0(pIemCpu);
4927 }
4928
4929 uint64_t fValid;
4930 if ( (pCtx->cr4 & X86_CR4_PAE)
4931 && (pCtx->msrEFER & MSR_K6_EFER_LME))
4932 fValid = UINT64_C(0x000fffffffffffff);
4933 else
4934 fValid = UINT64_C(0xffffffff);
4935 if (uNewCrX & ~fValid)
4936 {
4937 Log(("Automatically clearing reserved MBZ bits in CR3 load: NewCR3=%#llx ClearedBits=%#llx\n",
4938 uNewCrX, uNewCrX & ~fValid));
4939 uNewCrX &= fValid;
4940 }
4941
4942 /** @todo If we're in PAE mode we should check the PDPTRs for
4943 * invalid bits. */
4944
4945 /* Make the change. */
4946 if (!IEM_FULL_VERIFICATION_ENABLED(pIemCpu))
4947 {
4948 rc = CPUMSetGuestCR3(pVCpu, uNewCrX);
4949 AssertRCSuccessReturn(rc, rc);
4950 }
4951 else
4952 pCtx->cr3 = uNewCrX;
4953
4954 /* Inform PGM. */
4955 if (!IEM_FULL_VERIFICATION_ENABLED(pIemCpu))
4956 {
4957 if (pCtx->cr0 & X86_CR0_PG)
4958 {
4959 rc = PGMFlushTLB(pVCpu, pCtx->cr3, !(pCtx->cr4 & X86_CR4_PGE));
4960 AssertRCReturn(rc, rc);
4961 /* ignore informational status codes */
4962 }
4963 }
4964 rcStrict = VINF_SUCCESS;
4965 break;
4966 }
4967
4968 /*
4969 * CR4 is a bit more tedious as there are bits which cannot be cleared
4970 * under some circumstances and such.
4971 */
4972 case 4:
4973 {
4974 uint64_t const uOldCrX = pCtx->cr4;
4975
4976 /** @todo Shouldn't this look at the guest CPUID bits to determine
4977 * valid bits? e.g. if guest CPUID doesn't allow X86_CR4_OSXMMEEXCPT, we
4978 * should #GP(0). */
4979 /* reserved bits */
4980 uint32_t fValid = X86_CR4_VME | X86_CR4_PVI
4981 | X86_CR4_TSD | X86_CR4_DE
4982 | X86_CR4_PSE | X86_CR4_PAE
4983 | X86_CR4_MCE | X86_CR4_PGE
4984 | X86_CR4_PCE | X86_CR4_OSFXSR
4985 | X86_CR4_OSXMMEEXCPT;
4986 //if (xxx)
4987 // fValid |= X86_CR4_VMXE;
4988 if (IEM_GET_GUEST_CPU_FEATURES(pIemCpu)->fXSaveRstor)
4989 fValid |= X86_CR4_OSXSAVE;
4990 if (uNewCrX & ~(uint64_t)fValid)
4991 {
4992 Log(("Trying to set reserved CR4 bits: NewCR4=%#llx InvalidBits=%#llx\n", uNewCrX, uNewCrX & ~(uint64_t)fValid));
4993 return iemRaiseGeneralProtectionFault0(pIemCpu);
4994 }
4995
4996 /* long mode checks. */
4997 if ( (uOldCrX & X86_CR4_PAE)
4998 && !(uNewCrX & X86_CR4_PAE)
4999 && CPUMIsGuestInLongModeEx(pCtx) )
5000 {
5001 Log(("Trying to set clear CR4.PAE while long mode is active\n"));
5002 return iemRaiseGeneralProtectionFault0(pIemCpu);
5003 }
5004
5005
5006 /*
5007 * Change it.
5008 */
5009 if (!IEM_FULL_VERIFICATION_ENABLED(pIemCpu))
5010 {
5011 rc = CPUMSetGuestCR4(pVCpu, uNewCrX);
5012 AssertRCSuccessReturn(rc, rc);
5013 }
5014 else
5015 pCtx->cr4 = uNewCrX;
5016 Assert(pCtx->cr4 == uNewCrX);
5017
5018 /*
5019 * Notify SELM and PGM.
5020 */
5021 if (!IEM_FULL_VERIFICATION_ENABLED(pIemCpu))
5022 {
5023 /* SELM - VME may change things wrt to the TSS shadowing. */
5024 if ((uNewCrX ^ uOldCrX) & X86_CR4_VME)
5025 {
5026 Log(("iemCImpl_load_CrX: VME %d -> %d => Setting VMCPU_FF_SELM_SYNC_TSS\n",
5027 RT_BOOL(uOldCrX & X86_CR4_VME), RT_BOOL(uNewCrX & X86_CR4_VME) ));
5028#ifdef VBOX_WITH_RAW_MODE
5029 if (!HMIsEnabled(IEMCPU_TO_VM(pIemCpu)))
5030 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_TSS);
5031#endif
5032 }
5033
5034 /* PGM - flushing and mode. */
5035 if ((uNewCrX ^ uOldCrX) & (X86_CR4_PSE | X86_CR4_PAE | X86_CR4_PGE))
5036 {
5037 rc = PGMFlushTLB(pVCpu, pCtx->cr3, true /* global */);
5038 AssertRCReturn(rc, rc);
5039 /* ignore informational status codes */
5040 }
5041 rcStrict = PGMChangeMode(pVCpu, pCtx->cr0, pCtx->cr4, pCtx->msrEFER);
5042 }
5043 else
5044 rcStrict = VINF_SUCCESS;
5045 break;
5046 }
5047
5048 /*
5049 * CR8 maps to the APIC TPR.
5050 */
5051 case 8:
5052 if (uNewCrX & ~(uint64_t)0xf)
5053 {
5054 Log(("Trying to set reserved CR8 bits (%#RX64)\n", uNewCrX));
5055 return iemRaiseGeneralProtectionFault0(pIemCpu);
5056 }
5057
5058 if (!IEM_FULL_VERIFICATION_ENABLED(pIemCpu))
5059 PDMApicSetTPR(IEMCPU_TO_VMCPU(pIemCpu), (uint8_t)uNewCrX << 4);
5060 rcStrict = VINF_SUCCESS;
5061 break;
5062
5063 IEM_NOT_REACHED_DEFAULT_CASE_RET(); /* call checks */
5064 }
5065
5066 /*
5067 * Advance the RIP on success.
5068 */
5069 if (RT_SUCCESS(rcStrict))
5070 {
5071 if (rcStrict != VINF_SUCCESS)
5072 rcStrict = iemSetPassUpStatus(pIemCpu, rcStrict);
5073 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
5074 }
5075
5076 return rcStrict;
5077}
5078
5079
5080/**
5081 * Implements mov CRx,GReg.
5082 *
5083 * @param iCrReg The CRx register to write (valid).
5084 * @param iGReg The general register to load the DRx value from.
5085 */
5086IEM_CIMPL_DEF_2(iemCImpl_mov_Cd_Rd, uint8_t, iCrReg, uint8_t, iGReg)
5087{
5088 if (pIemCpu->uCpl != 0)
5089 return iemRaiseGeneralProtectionFault0(pIemCpu);
5090 Assert(!pIemCpu->CTX_SUFF(pCtx)->eflags.Bits.u1VM);
5091
5092 /*
5093 * Read the new value from the source register and call common worker.
5094 */
5095 uint64_t uNewCrX;
5096 if (pIemCpu->enmCpuMode == IEMMODE_64BIT)
5097 uNewCrX = iemGRegFetchU64(pIemCpu, iGReg);
5098 else
5099 uNewCrX = iemGRegFetchU32(pIemCpu, iGReg);
5100 return IEM_CIMPL_CALL_2(iemCImpl_load_CrX, iCrReg, uNewCrX);
5101}
5102
5103
5104/**
5105 * Implements 'LMSW r/m16'
5106 *
5107 * @param u16NewMsw The new value.
5108 */
5109IEM_CIMPL_DEF_1(iemCImpl_lmsw, uint16_t, u16NewMsw)
5110{
5111 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
5112
5113 if (pIemCpu->uCpl != 0)
5114 return iemRaiseGeneralProtectionFault0(pIemCpu);
5115 Assert(!pCtx->eflags.Bits.u1VM);
5116
5117 /*
5118 * Compose the new CR0 value and call common worker.
5119 */
5120 uint64_t uNewCr0 = pCtx->cr0 & ~(X86_CR0_MP | X86_CR0_EM | X86_CR0_TS);
5121 uNewCr0 |= u16NewMsw & (X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS);
5122 return IEM_CIMPL_CALL_2(iemCImpl_load_CrX, /*cr*/ 0, uNewCr0);
5123}
5124
5125
5126/**
5127 * Implements 'CLTS'.
5128 */
5129IEM_CIMPL_DEF_0(iemCImpl_clts)
5130{
5131 if (pIemCpu->uCpl != 0)
5132 return iemRaiseGeneralProtectionFault0(pIemCpu);
5133
5134 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
5135 uint64_t uNewCr0 = pCtx->cr0;
5136 uNewCr0 &= ~X86_CR0_TS;
5137 return IEM_CIMPL_CALL_2(iemCImpl_load_CrX, /*cr*/ 0, uNewCr0);
5138}
5139
5140
5141/**
5142 * Implements mov GReg,DRx.
5143 *
5144 * @param iGReg The general register to store the DRx value in.
5145 * @param iDrReg The DRx register to read (0-7).
5146 */
5147IEM_CIMPL_DEF_2(iemCImpl_mov_Rd_Dd, uint8_t, iGReg, uint8_t, iDrReg)
5148{
5149 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
5150
5151 /*
5152 * Check preconditions.
5153 */
5154
5155 /* Raise GPs. */
5156 if (pIemCpu->uCpl != 0)
5157 return iemRaiseGeneralProtectionFault0(pIemCpu);
5158 Assert(!pCtx->eflags.Bits.u1VM);
5159
5160 if ( (iDrReg == 4 || iDrReg == 5)
5161 && (pCtx->cr4 & X86_CR4_DE) )
5162 {
5163 Log(("mov r%u,dr%u: CR4.DE=1 -> #GP(0)\n", iGReg, iDrReg));
5164 return iemRaiseGeneralProtectionFault0(pIemCpu);
5165 }
5166
5167 /* Raise #DB if general access detect is enabled. */
5168 if (pCtx->dr[7] & X86_DR7_GD)
5169 {
5170 Log(("mov r%u,dr%u: DR7.GD=1 -> #DB\n", iGReg, iDrReg));
5171 return iemRaiseDebugException(pIemCpu);
5172 }
5173
5174 /*
5175 * Read the debug register and store it in the specified general register.
5176 */
5177 uint64_t drX;
5178 switch (iDrReg)
5179 {
5180 case 0: drX = pCtx->dr[0]; break;
5181 case 1: drX = pCtx->dr[1]; break;
5182 case 2: drX = pCtx->dr[2]; break;
5183 case 3: drX = pCtx->dr[3]; break;
5184 case 6:
5185 case 4:
5186 drX = pCtx->dr[6];
5187 drX |= X86_DR6_RA1_MASK;
5188 drX &= ~X86_DR6_RAZ_MASK;
5189 break;
5190 case 7:
5191 case 5:
5192 drX = pCtx->dr[7];
5193 drX |=X86_DR7_RA1_MASK;
5194 drX &= ~X86_DR7_RAZ_MASK;
5195 break;
5196 IEM_NOT_REACHED_DEFAULT_CASE_RET(); /* call checks */
5197 }
5198
5199 if (pIemCpu->enmCpuMode == IEMMODE_64BIT)
5200 *(uint64_t *)iemGRegRef(pIemCpu, iGReg) = drX;
5201 else
5202 *(uint64_t *)iemGRegRef(pIemCpu, iGReg) = (uint32_t)drX;
5203
5204 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
5205 return VINF_SUCCESS;
5206}
5207
5208
5209/**
5210 * Implements mov DRx,GReg.
5211 *
5212 * @param iDrReg The DRx register to write (valid).
5213 * @param iGReg The general register to load the DRx value from.
5214 */
5215IEM_CIMPL_DEF_2(iemCImpl_mov_Dd_Rd, uint8_t, iDrReg, uint8_t, iGReg)
5216{
5217 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
5218
5219 /*
5220 * Check preconditions.
5221 */
5222 if (pIemCpu->uCpl != 0)
5223 return iemRaiseGeneralProtectionFault0(pIemCpu);
5224 Assert(!pCtx->eflags.Bits.u1VM);
5225
5226 if (iDrReg == 4 || iDrReg == 5)
5227 {
5228 if (pCtx->cr4 & X86_CR4_DE)
5229 {
5230 Log(("mov dr%u,r%u: CR4.DE=1 -> #GP(0)\n", iDrReg, iGReg));
5231 return iemRaiseGeneralProtectionFault0(pIemCpu);
5232 }
5233 iDrReg += 2;
5234 }
5235
5236 /* Raise #DB if general access detect is enabled. */
5237 /** @todo is \#DB/DR7.GD raised before any reserved high bits in DR7/DR6
5238 * \#GP? */
5239 if (pCtx->dr[7] & X86_DR7_GD)
5240 {
5241 Log(("mov dr%u,r%u: DR7.GD=1 -> #DB\n", iDrReg, iGReg));
5242 return iemRaiseDebugException(pIemCpu);
5243 }
5244
5245 /*
5246 * Read the new value from the source register.
5247 */
5248 uint64_t uNewDrX;
5249 if (pIemCpu->enmCpuMode == IEMMODE_64BIT)
5250 uNewDrX = iemGRegFetchU64(pIemCpu, iGReg);
5251 else
5252 uNewDrX = iemGRegFetchU32(pIemCpu, iGReg);
5253
5254 /*
5255 * Adjust it.
5256 */
5257 switch (iDrReg)
5258 {
5259 case 0:
5260 case 1:
5261 case 2:
5262 case 3:
5263 /* nothing to adjust */
5264 break;
5265
5266 case 6:
5267 if (uNewDrX & X86_DR6_MBZ_MASK)
5268 {
5269 Log(("mov dr%u,%#llx: DR6 high bits are not zero -> #GP(0)\n", iDrReg, uNewDrX));
5270 return iemRaiseGeneralProtectionFault0(pIemCpu);
5271 }
5272 uNewDrX |= X86_DR6_RA1_MASK;
5273 uNewDrX &= ~X86_DR6_RAZ_MASK;
5274 break;
5275
5276 case 7:
5277 if (uNewDrX & X86_DR7_MBZ_MASK)
5278 {
5279 Log(("mov dr%u,%#llx: DR7 high bits are not zero -> #GP(0)\n", iDrReg, uNewDrX));
5280 return iemRaiseGeneralProtectionFault0(pIemCpu);
5281 }
5282 uNewDrX |= X86_DR7_RA1_MASK;
5283 uNewDrX &= ~X86_DR7_RAZ_MASK;
5284 break;
5285
5286 IEM_NOT_REACHED_DEFAULT_CASE_RET();
5287 }
5288
5289 /*
5290 * Do the actual setting.
5291 */
5292 if (!IEM_VERIFICATION_ENABLED(pIemCpu))
5293 {
5294 int rc = CPUMSetGuestDRx(IEMCPU_TO_VMCPU(pIemCpu), iDrReg, uNewDrX);
5295 AssertRCSuccessReturn(rc, RT_SUCCESS_NP(rc) ? VERR_IEM_IPE_1 : rc);
5296 }
5297 else
5298 pCtx->dr[iDrReg] = uNewDrX;
5299
5300 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
5301 return VINF_SUCCESS;
5302}
5303
5304
5305/**
5306 * Implements 'INVLPG m'.
5307 *
5308 * @param GCPtrPage The effective address of the page to invalidate.
5309 * @remarks Updates the RIP.
5310 */
5311IEM_CIMPL_DEF_1(iemCImpl_invlpg, RTGCPTR, GCPtrPage)
5312{
5313 /* ring-0 only. */
5314 if (pIemCpu->uCpl != 0)
5315 return iemRaiseGeneralProtectionFault0(pIemCpu);
5316 Assert(!pIemCpu->CTX_SUFF(pCtx)->eflags.Bits.u1VM);
5317
5318 int rc = PGMInvalidatePage(IEMCPU_TO_VMCPU(pIemCpu), GCPtrPage);
5319 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
5320
5321 if (rc == VINF_SUCCESS)
5322 return VINF_SUCCESS;
5323 if (rc == VINF_PGM_SYNC_CR3)
5324 return iemSetPassUpStatus(pIemCpu, rc);
5325
5326 AssertMsg(rc == VINF_EM_RAW_EMULATE_INSTR || RT_FAILURE_NP(rc), ("%Rrc\n", rc));
5327 Log(("PGMInvalidatePage(%RGv) -> %Rrc\n", GCPtrPage, rc));
5328 return rc;
5329}
5330
5331
5332/**
5333 * Implements RDTSC.
5334 */
5335IEM_CIMPL_DEF_0(iemCImpl_rdtsc)
5336{
5337 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
5338
5339 /*
5340 * Check preconditions.
5341 */
5342 if (!IEM_GET_GUEST_CPU_FEATURES(pIemCpu)->fTsc)
5343 return iemRaiseUndefinedOpcode(pIemCpu);
5344
5345 if ( (pCtx->cr4 & X86_CR4_TSD)
5346 && pIemCpu->uCpl != 0)
5347 {
5348 Log(("rdtsc: CR4.TSD and CPL=%u -> #GP(0)\n", pIemCpu->uCpl));
5349 return iemRaiseGeneralProtectionFault0(pIemCpu);
5350 }
5351
5352 /*
5353 * Do the job.
5354 */
5355 uint64_t uTicks = TMCpuTickGet(IEMCPU_TO_VMCPU(pIemCpu));
5356 pCtx->rax = (uint32_t)uTicks;
5357 pCtx->rdx = uTicks >> 32;
5358#ifdef IEM_VERIFICATION_MODE_FULL
5359 pIemCpu->fIgnoreRaxRdx = true;
5360#endif
5361
5362 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
5363 return VINF_SUCCESS;
5364}
5365
5366
5367/**
5368 * Implements RDMSR.
5369 */
5370IEM_CIMPL_DEF_0(iemCImpl_rdmsr)
5371{
5372 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
5373
5374 /*
5375 * Check preconditions.
5376 */
5377 if (!IEM_GET_GUEST_CPU_FEATURES(pIemCpu)->fMsr)
5378 return iemRaiseUndefinedOpcode(pIemCpu);
5379 if (pIemCpu->uCpl != 0)
5380 return iemRaiseGeneralProtectionFault0(pIemCpu);
5381
5382 /*
5383 * Do the job.
5384 */
5385 RTUINT64U uValue;
5386 VBOXSTRICTRC rcStrict = CPUMQueryGuestMsr(IEMCPU_TO_VMCPU(pIemCpu), pCtx->ecx, &uValue.u);
5387 if (rcStrict == VINF_SUCCESS)
5388 {
5389 pCtx->rax = uValue.s.Lo;
5390 pCtx->rdx = uValue.s.Hi;
5391
5392 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
5393 return VINF_SUCCESS;
5394 }
5395
5396#ifndef IN_RING3
5397 /* Deferred to ring-3. */
5398 if (rcStrict == VINF_CPUM_R3_MSR_READ)
5399 {
5400 Log(("IEM: rdmsr(%#x) -> ring-3\n", pCtx->ecx));
5401 return rcStrict;
5402 }
5403#else /* IN_RING3 */
5404 /* Often a unimplemented MSR or MSR bit, so worth logging. */
5405 static uint32_t s_cTimes = 0;
5406 if (s_cTimes++ < 10)
5407 LogRel(("IEM: rdmsr(%#x) -> #GP(0)\n", pCtx->ecx));
5408 else
5409#endif
5410 Log(("IEM: rdmsr(%#x) -> #GP(0)\n", pCtx->ecx));
5411 AssertMsgReturn(rcStrict == VERR_CPUM_RAISE_GP_0, ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)), VERR_IPE_UNEXPECTED_STATUS);
5412 return iemRaiseGeneralProtectionFault0(pIemCpu);
5413}
5414
5415
5416/**
5417 * Implements WRMSR.
5418 */
5419IEM_CIMPL_DEF_0(iemCImpl_wrmsr)
5420{
5421 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
5422
5423 /*
5424 * Check preconditions.
5425 */
5426 if (!IEM_GET_GUEST_CPU_FEATURES(pIemCpu)->fMsr)
5427 return iemRaiseUndefinedOpcode(pIemCpu);
5428 if (pIemCpu->uCpl != 0)
5429 return iemRaiseGeneralProtectionFault0(pIemCpu);
5430
5431 /*
5432 * Do the job.
5433 */
5434 RTUINT64U uValue;
5435 uValue.s.Lo = pCtx->eax;
5436 uValue.s.Hi = pCtx->edx;
5437
5438 VBOXSTRICTRC rcStrict;
5439 if (!IEM_VERIFICATION_ENABLED(pIemCpu))
5440 rcStrict = CPUMSetGuestMsr(IEMCPU_TO_VMCPU(pIemCpu), pCtx->ecx, uValue.u);
5441 else
5442 {
5443#ifdef IN_RING3
5444 CPUMCTX CtxTmp = *pCtx;
5445 rcStrict = CPUMSetGuestMsr(IEMCPU_TO_VMCPU(pIemCpu), pCtx->ecx, uValue.u);
5446 PCPUMCTX pCtx2 = CPUMQueryGuestCtxPtr(IEMCPU_TO_VMCPU(pIemCpu));
5447 *pCtx = *pCtx2;
5448 *pCtx2 = CtxTmp;
5449#else
5450 AssertReleaseFailedReturn(VERR_IEM_IPE_2);
5451#endif
5452 }
5453 if (rcStrict == VINF_SUCCESS)
5454 {
5455 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
5456 return VINF_SUCCESS;
5457 }
5458
5459#ifndef IN_RING3
5460 /* Deferred to ring-3. */
5461 if (rcStrict == VINF_CPUM_R3_MSR_WRITE)
5462 {
5463 Log(("IEM: rdmsr(%#x) -> ring-3\n", pCtx->ecx));
5464 return rcStrict;
5465 }
5466#else /* IN_RING3 */
5467 /* Often a unimplemented MSR or MSR bit, so worth logging. */
5468 static uint32_t s_cTimes = 0;
5469 if (s_cTimes++ < 10)
5470 LogRel(("IEM: wrmsr(%#x,%#x`%08x) -> #GP(0)\n", pCtx->ecx, uValue.s.Hi, uValue.s.Lo));
5471 else
5472#endif
5473 Log(("IEM: wrmsr(%#x,%#x`%08x) -> #GP(0)\n", pCtx->ecx, uValue.s.Hi, uValue.s.Lo));
5474 AssertMsgReturn(rcStrict == VERR_CPUM_RAISE_GP_0, ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)), VERR_IPE_UNEXPECTED_STATUS);
5475 return iemRaiseGeneralProtectionFault0(pIemCpu);
5476}
5477
5478
5479/**
5480 * Implements 'IN eAX, port'.
5481 *
5482 * @param u16Port The source port.
5483 * @param cbReg The register size.
5484 */
5485IEM_CIMPL_DEF_2(iemCImpl_in, uint16_t, u16Port, uint8_t, cbReg)
5486{
5487 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
5488
5489 /*
5490 * CPL check
5491 */
5492 VBOXSTRICTRC rcStrict = iemHlpCheckPortIOPermission(pIemCpu, pCtx, u16Port, cbReg);
5493 if (rcStrict != VINF_SUCCESS)
5494 return rcStrict;
5495
5496 /*
5497 * Perform the I/O.
5498 */
5499 uint32_t u32Value;
5500 if (!IEM_VERIFICATION_ENABLED(pIemCpu))
5501 rcStrict = IOMIOPortRead(IEMCPU_TO_VM(pIemCpu), IEMCPU_TO_VMCPU(pIemCpu), u16Port, &u32Value, cbReg);
5502 else
5503 rcStrict = iemVerifyFakeIOPortRead(pIemCpu, u16Port, &u32Value, cbReg);
5504 if (IOM_SUCCESS(rcStrict))
5505 {
5506 switch (cbReg)
5507 {
5508 case 1: pCtx->al = (uint8_t)u32Value; break;
5509 case 2: pCtx->ax = (uint16_t)u32Value; break;
5510 case 4: pCtx->rax = u32Value; break;
5511 default: AssertFailedReturn(VERR_IEM_IPE_3);
5512 }
5513 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
5514 pIemCpu->cPotentialExits++;
5515 if (rcStrict != VINF_SUCCESS)
5516 rcStrict = iemSetPassUpStatus(pIemCpu, rcStrict);
5517 Assert(rcStrict == VINF_SUCCESS); /* assumed below */
5518
5519 /*
5520 * Check for I/O breakpoints.
5521 */
5522 uint32_t const uDr7 = pCtx->dr[7];
5523 if (RT_UNLIKELY( ( (uDr7 & X86_DR7_ENABLED_MASK)
5524 && X86_DR7_ANY_RW_IO(uDr7)
5525 && (pCtx->cr4 & X86_CR4_DE))
5526 || DBGFBpIsHwIoArmed(IEMCPU_TO_VM(pIemCpu))))
5527 {
5528 rcStrict = DBGFBpCheckIo(IEMCPU_TO_VM(pIemCpu), IEMCPU_TO_VMCPU(pIemCpu), pCtx, u16Port, cbReg);
5529 if (rcStrict == VINF_EM_RAW_GUEST_TRAP)
5530 rcStrict = iemRaiseDebugException(pIemCpu);
5531 }
5532 }
5533
5534 return rcStrict;
5535}
5536
5537
5538/**
5539 * Implements 'IN eAX, DX'.
5540 *
5541 * @param cbReg The register size.
5542 */
5543IEM_CIMPL_DEF_1(iemCImpl_in_eAX_DX, uint8_t, cbReg)
5544{
5545 return IEM_CIMPL_CALL_2(iemCImpl_in, pIemCpu->CTX_SUFF(pCtx)->dx, cbReg);
5546}
5547
5548
5549/**
5550 * Implements 'OUT port, eAX'.
5551 *
5552 * @param u16Port The destination port.
5553 * @param cbReg The register size.
5554 */
5555IEM_CIMPL_DEF_2(iemCImpl_out, uint16_t, u16Port, uint8_t, cbReg)
5556{
5557 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
5558
5559 /*
5560 * CPL check
5561 */
5562 VBOXSTRICTRC rcStrict = iemHlpCheckPortIOPermission(pIemCpu, pCtx, u16Port, cbReg);
5563 if (rcStrict != VINF_SUCCESS)
5564 return rcStrict;
5565
5566 /*
5567 * Perform the I/O.
5568 */
5569 uint32_t u32Value;
5570 switch (cbReg)
5571 {
5572 case 1: u32Value = pCtx->al; break;
5573 case 2: u32Value = pCtx->ax; break;
5574 case 4: u32Value = pCtx->eax; break;
5575 default: AssertFailedReturn(VERR_IEM_IPE_4);
5576 }
5577 if (!IEM_VERIFICATION_ENABLED(pIemCpu))
5578 rcStrict = IOMIOPortWrite(IEMCPU_TO_VM(pIemCpu), IEMCPU_TO_VMCPU(pIemCpu), u16Port, u32Value, cbReg);
5579 else
5580 rcStrict = iemVerifyFakeIOPortWrite(pIemCpu, u16Port, u32Value, cbReg);
5581 if (IOM_SUCCESS(rcStrict))
5582 {
5583 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
5584 pIemCpu->cPotentialExits++;
5585 if (rcStrict != VINF_SUCCESS)
5586 rcStrict = iemSetPassUpStatus(pIemCpu, rcStrict);
5587 Assert(rcStrict == VINF_SUCCESS); /* assumed below */
5588
5589 /*
5590 * Check for I/O breakpoints.
5591 */
5592 uint32_t const uDr7 = pCtx->dr[7];
5593 if (RT_UNLIKELY( ( (uDr7 & X86_DR7_ENABLED_MASK)
5594 && X86_DR7_ANY_RW_IO(uDr7)
5595 && (pCtx->cr4 & X86_CR4_DE))
5596 || DBGFBpIsHwIoArmed(IEMCPU_TO_VM(pIemCpu))))
5597 {
5598 rcStrict = DBGFBpCheckIo(IEMCPU_TO_VM(pIemCpu), IEMCPU_TO_VMCPU(pIemCpu), pCtx, u16Port, cbReg);
5599 if (rcStrict == VINF_EM_RAW_GUEST_TRAP)
5600 rcStrict = iemRaiseDebugException(pIemCpu);
5601 }
5602 }
5603 return rcStrict;
5604}
5605
5606
5607/**
5608 * Implements 'OUT DX, eAX'.
5609 *
5610 * @param cbReg The register size.
5611 */
5612IEM_CIMPL_DEF_1(iemCImpl_out_DX_eAX, uint8_t, cbReg)
5613{
5614 return IEM_CIMPL_CALL_2(iemCImpl_out, pIemCpu->CTX_SUFF(pCtx)->dx, cbReg);
5615}
5616
5617
5618/**
5619 * Implements 'CLI'.
5620 */
5621IEM_CIMPL_DEF_0(iemCImpl_cli)
5622{
5623 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
5624 uint32_t fEfl = IEMMISC_GET_EFL(pIemCpu, pCtx);
5625 uint32_t const fEflOld = fEfl;
5626 if (pCtx->cr0 & X86_CR0_PE)
5627 {
5628 uint8_t const uIopl = X86_EFL_GET_IOPL(fEfl);
5629 if (!(fEfl & X86_EFL_VM))
5630 {
5631 if (pIemCpu->uCpl <= uIopl)
5632 fEfl &= ~X86_EFL_IF;
5633 else if ( pIemCpu->uCpl == 3
5634 && (pCtx->cr4 & X86_CR4_PVI) )
5635 fEfl &= ~X86_EFL_VIF;
5636 else
5637 return iemRaiseGeneralProtectionFault0(pIemCpu);
5638 }
5639 /* V8086 */
5640 else if (uIopl == 3)
5641 fEfl &= ~X86_EFL_IF;
5642 else if ( uIopl < 3
5643 && (pCtx->cr4 & X86_CR4_VME) )
5644 fEfl &= ~X86_EFL_VIF;
5645 else
5646 return iemRaiseGeneralProtectionFault0(pIemCpu);
5647 }
5648 /* real mode */
5649 else
5650 fEfl &= ~X86_EFL_IF;
5651
5652 /* Commit. */
5653 IEMMISC_SET_EFL(pIemCpu, pCtx, fEfl);
5654 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
5655 Log2(("CLI: %#x -> %#x\n", fEflOld, fEfl)); NOREF(fEflOld);
5656 return VINF_SUCCESS;
5657}
5658
5659
5660/**
5661 * Implements 'STI'.
5662 */
5663IEM_CIMPL_DEF_0(iemCImpl_sti)
5664{
5665 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
5666 uint32_t fEfl = IEMMISC_GET_EFL(pIemCpu, pCtx);
5667 uint32_t const fEflOld = fEfl;
5668
5669 if (pCtx->cr0 & X86_CR0_PE)
5670 {
5671 uint8_t const uIopl = X86_EFL_GET_IOPL(fEfl);
5672 if (!(fEfl & X86_EFL_VM))
5673 {
5674 if (pIemCpu->uCpl <= uIopl)
5675 fEfl |= X86_EFL_IF;
5676 else if ( pIemCpu->uCpl == 3
5677 && (pCtx->cr4 & X86_CR4_PVI)
5678 && !(fEfl & X86_EFL_VIP) )
5679 fEfl |= X86_EFL_VIF;
5680 else
5681 return iemRaiseGeneralProtectionFault0(pIemCpu);
5682 }
5683 /* V8086 */
5684 else if (uIopl == 3)
5685 fEfl |= X86_EFL_IF;
5686 else if ( uIopl < 3
5687 && (pCtx->cr4 & X86_CR4_VME)
5688 && !(fEfl & X86_EFL_VIP) )
5689 fEfl |= X86_EFL_VIF;
5690 else
5691 return iemRaiseGeneralProtectionFault0(pIemCpu);
5692 }
5693 /* real mode */
5694 else
5695 fEfl |= X86_EFL_IF;
5696
5697 /* Commit. */
5698 IEMMISC_SET_EFL(pIemCpu, pCtx, fEfl);
5699 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
5700 if ((!(fEflOld & X86_EFL_IF) && (fEfl & X86_EFL_IF)) || IEM_FULL_VERIFICATION_REM_ENABLED(pIemCpu))
5701 EMSetInhibitInterruptsPC(IEMCPU_TO_VMCPU(pIemCpu), pCtx->rip);
5702 Log2(("STI: %#x -> %#x\n", fEflOld, fEfl));
5703 return VINF_SUCCESS;
5704}
5705
5706
5707/**
5708 * Implements 'HLT'.
5709 */
5710IEM_CIMPL_DEF_0(iemCImpl_hlt)
5711{
5712 if (pIemCpu->uCpl != 0)
5713 return iemRaiseGeneralProtectionFault0(pIemCpu);
5714 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
5715 return VINF_EM_HALT;
5716}
5717
5718
5719/**
5720 * Implements 'MONITOR'.
5721 */
5722IEM_CIMPL_DEF_1(iemCImpl_monitor, uint8_t, iEffSeg)
5723{
5724 /*
5725 * Permission checks.
5726 */
5727 if (pIemCpu->uCpl != 0)
5728 {
5729 Log2(("monitor: CPL != 0\n"));
5730 return iemRaiseUndefinedOpcode(pIemCpu); /** @todo MSR[0xC0010015].MonMwaitUserEn if we care. */
5731 }
5732 if (!IEM_GET_GUEST_CPU_FEATURES(pIemCpu)->fMonitorMWait)
5733 {
5734 Log2(("monitor: Not in CPUID\n"));
5735 return iemRaiseUndefinedOpcode(pIemCpu);
5736 }
5737
5738 /*
5739 * Gather the operands and validate them.
5740 */
5741 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
5742 RTGCPTR GCPtrMem = pIemCpu->enmCpuMode == IEMMODE_64BIT ? pCtx->rax : pCtx->eax;
5743 uint32_t uEcx = pCtx->ecx;
5744 uint32_t uEdx = pCtx->edx;
5745/** @todo Test whether EAX or ECX is processed first, i.e. do we get \#PF or
5746 * \#GP first. */
5747 if (uEcx != 0)
5748 {
5749 Log2(("monitor rax=%RX64, ecx=%RX32, edx=%RX32; ECX != 0 -> #GP(0)\n", GCPtrMem, uEcx, uEdx)); NOREF(uEdx);
5750 return iemRaiseGeneralProtectionFault0(pIemCpu);
5751 }
5752
5753 VBOXSTRICTRC rcStrict = iemMemApplySegment(pIemCpu, IEM_ACCESS_TYPE_READ | IEM_ACCESS_WHAT_DATA, iEffSeg, 1, &GCPtrMem);
5754 if (rcStrict != VINF_SUCCESS)
5755 return rcStrict;
5756
5757 RTGCPHYS GCPhysMem;
5758 rcStrict = iemMemPageTranslateAndCheckAccess(pIemCpu, GCPtrMem, IEM_ACCESS_TYPE_READ | IEM_ACCESS_WHAT_DATA, &GCPhysMem);
5759 if (rcStrict != VINF_SUCCESS)
5760 return rcStrict;
5761
5762 /*
5763 * Call EM to prepare the monitor/wait.
5764 */
5765 rcStrict = EMMonitorWaitPrepare(IEMCPU_TO_VMCPU(pIemCpu), pCtx->rax, pCtx->rcx, pCtx->rdx, GCPhysMem);
5766 Assert(rcStrict == VINF_SUCCESS);
5767
5768 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
5769 return rcStrict;
5770}
5771
5772
5773/**
5774 * Implements 'MWAIT'.
5775 */
5776IEM_CIMPL_DEF_0(iemCImpl_mwait)
5777{
5778 /*
5779 * Permission checks.
5780 */
5781 if (pIemCpu->uCpl != 0)
5782 {
5783 Log2(("mwait: CPL != 0\n"));
5784 /** @todo MSR[0xC0010015].MonMwaitUserEn if we care. (Remember to check
5785 * EFLAGS.VM then.) */
5786 return iemRaiseUndefinedOpcode(pIemCpu);
5787 }
5788 if (!IEM_GET_GUEST_CPU_FEATURES(pIemCpu)->fMonitorMWait)
5789 {
5790 Log2(("mwait: Not in CPUID\n"));
5791 return iemRaiseUndefinedOpcode(pIemCpu);
5792 }
5793
5794 /*
5795 * Gather the operands and validate them.
5796 */
5797 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
5798 uint32_t uEax = pCtx->eax;
5799 uint32_t uEcx = pCtx->ecx;
5800 if (uEcx != 0)
5801 {
5802 /* Only supported extension is break on IRQ when IF=0. */
5803 if (uEcx > 1)
5804 {
5805 Log2(("mwait eax=%RX32, ecx=%RX32; ECX > 1 -> #GP(0)\n", uEax, uEcx));
5806 return iemRaiseGeneralProtectionFault0(pIemCpu);
5807 }
5808 uint32_t fMWaitFeatures = 0;
5809 uint32_t uIgnore = 0;
5810 CPUMGetGuestCpuId(IEMCPU_TO_VMCPU(pIemCpu), 5, 0, &uIgnore, &uIgnore, &fMWaitFeatures, &uIgnore);
5811 if ( (fMWaitFeatures & (X86_CPUID_MWAIT_ECX_EXT | X86_CPUID_MWAIT_ECX_BREAKIRQIF0))
5812 != (X86_CPUID_MWAIT_ECX_EXT | X86_CPUID_MWAIT_ECX_BREAKIRQIF0))
5813 {
5814 Log2(("mwait eax=%RX32, ecx=%RX32; break-on-IRQ-IF=0 extension not enabled -> #GP(0)\n", uEax, uEcx));
5815 return iemRaiseGeneralProtectionFault0(pIemCpu);
5816 }
5817 }
5818
5819 /*
5820 * Call EM to prepare the monitor/wait.
5821 */
5822 VBOXSTRICTRC rcStrict = EMMonitorWaitPerform(IEMCPU_TO_VMCPU(pIemCpu), uEax, uEcx);
5823
5824 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
5825 return rcStrict;
5826}
5827
5828
5829/**
5830 * Implements 'SWAPGS'.
5831 */
5832IEM_CIMPL_DEF_0(iemCImpl_swapgs)
5833{
5834 Assert(pIemCpu->enmCpuMode == IEMMODE_64BIT); /* Caller checks this. */
5835
5836 /*
5837 * Permission checks.
5838 */
5839 if (pIemCpu->uCpl != 0)
5840 {
5841 Log2(("swapgs: CPL != 0\n"));
5842 return iemRaiseUndefinedOpcode(pIemCpu);
5843 }
5844
5845 /*
5846 * Do the job.
5847 */
5848 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
5849 uint64_t uOtherGsBase = pCtx->msrKERNELGSBASE;
5850 pCtx->msrKERNELGSBASE = pCtx->gs.u64Base;
5851 pCtx->gs.u64Base = uOtherGsBase;
5852
5853 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
5854 return VINF_SUCCESS;
5855}
5856
5857
5858/**
5859 * Implements 'CPUID'.
5860 */
5861IEM_CIMPL_DEF_0(iemCImpl_cpuid)
5862{
5863 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
5864
5865 CPUMGetGuestCpuId(IEMCPU_TO_VMCPU(pIemCpu), pCtx->eax, pCtx->ecx, &pCtx->eax, &pCtx->ebx, &pCtx->ecx, &pCtx->edx);
5866 pCtx->rax &= UINT32_C(0xffffffff);
5867 pCtx->rbx &= UINT32_C(0xffffffff);
5868 pCtx->rcx &= UINT32_C(0xffffffff);
5869 pCtx->rdx &= UINT32_C(0xffffffff);
5870
5871 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
5872 return VINF_SUCCESS;
5873}
5874
5875
5876/**
5877 * Implements 'AAD'.
5878 *
5879 * @param bImm The immediate operand.
5880 */
5881IEM_CIMPL_DEF_1(iemCImpl_aad, uint8_t, bImm)
5882{
5883 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
5884
5885 uint16_t const ax = pCtx->ax;
5886 uint8_t const al = (uint8_t)ax + (uint8_t)(ax >> 8) * bImm;
5887 pCtx->ax = al;
5888 iemHlpUpdateArithEFlagsU8(pIemCpu, al,
5889 X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF,
5890 X86_EFL_OF | X86_EFL_AF | X86_EFL_CF);
5891
5892 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
5893 return VINF_SUCCESS;
5894}
5895
5896
5897/**
5898 * Implements 'AAM'.
5899 *
5900 * @param bImm The immediate operand. Cannot be 0.
5901 */
5902IEM_CIMPL_DEF_1(iemCImpl_aam, uint8_t, bImm)
5903{
5904 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
5905 Assert(bImm != 0); /* #DE on 0 is handled in the decoder. */
5906
5907 uint16_t const ax = pCtx->ax;
5908 uint8_t const al = (uint8_t)ax % bImm;
5909 uint8_t const ah = (uint8_t)ax / bImm;
5910 pCtx->ax = (ah << 8) + al;
5911 iemHlpUpdateArithEFlagsU8(pIemCpu, al,
5912 X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF,
5913 X86_EFL_OF | X86_EFL_AF | X86_EFL_CF);
5914
5915 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
5916 return VINF_SUCCESS;
5917}
5918
5919
5920/**
5921 * Implements 'DAA'.
5922 */
5923IEM_CIMPL_DEF_0(iemCImpl_daa)
5924{
5925 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
5926
5927 uint8_t const al = pCtx->al;
5928 bool const fCarry = pCtx->eflags.Bits.u1CF;
5929
5930 if ( pCtx->eflags.Bits.u1AF
5931 || (al & 0xf) >= 10)
5932 {
5933 pCtx->al = al + 6;
5934 pCtx->eflags.Bits.u1AF = 1;
5935 }
5936 else
5937 pCtx->eflags.Bits.u1AF = 0;
5938
5939 if (al >= 0x9a || fCarry)
5940 {
5941 pCtx->al += 0x60;
5942 pCtx->eflags.Bits.u1CF = 1;
5943 }
5944 else
5945 pCtx->eflags.Bits.u1CF = 0;
5946
5947 iemHlpUpdateArithEFlagsU8(pIemCpu, pCtx->al, X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF, X86_EFL_OF);
5948 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
5949 return VINF_SUCCESS;
5950}
5951
5952
5953/**
5954 * Implements 'DAS'.
5955 */
5956IEM_CIMPL_DEF_0(iemCImpl_das)
5957{
5958 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
5959
5960 uint8_t const uInputAL = pCtx->al;
5961 bool const fCarry = pCtx->eflags.Bits.u1CF;
5962
5963 if ( pCtx->eflags.Bits.u1AF
5964 || (uInputAL & 0xf) >= 10)
5965 {
5966 pCtx->eflags.Bits.u1AF = 1;
5967 if (uInputAL < 6)
5968 pCtx->eflags.Bits.u1CF = 1;
5969 pCtx->al = uInputAL - 6;
5970 }
5971 else
5972 {
5973 pCtx->eflags.Bits.u1AF = 0;
5974 pCtx->eflags.Bits.u1CF = 0;
5975 }
5976
5977 if (uInputAL >= 0x9a || fCarry)
5978 {
5979 pCtx->al -= 0x60;
5980 pCtx->eflags.Bits.u1CF = 1;
5981 }
5982
5983 iemHlpUpdateArithEFlagsU8(pIemCpu, pCtx->al, X86_EFL_SF | X86_EFL_ZF | X86_EFL_PF, X86_EFL_OF);
5984 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
5985 return VINF_SUCCESS;
5986}
5987
5988
5989
5990
5991/*
5992 * Instantiate the various string operation combinations.
5993 */
5994#define OP_SIZE 8
5995#define ADDR_SIZE 16
5996#include "IEMAllCImplStrInstr.cpp.h"
5997#define OP_SIZE 8
5998#define ADDR_SIZE 32
5999#include "IEMAllCImplStrInstr.cpp.h"
6000#define OP_SIZE 8
6001#define ADDR_SIZE 64
6002#include "IEMAllCImplStrInstr.cpp.h"
6003
6004#define OP_SIZE 16
6005#define ADDR_SIZE 16
6006#include "IEMAllCImplStrInstr.cpp.h"
6007#define OP_SIZE 16
6008#define ADDR_SIZE 32
6009#include "IEMAllCImplStrInstr.cpp.h"
6010#define OP_SIZE 16
6011#define ADDR_SIZE 64
6012#include "IEMAllCImplStrInstr.cpp.h"
6013
6014#define OP_SIZE 32
6015#define ADDR_SIZE 16
6016#include "IEMAllCImplStrInstr.cpp.h"
6017#define OP_SIZE 32
6018#define ADDR_SIZE 32
6019#include "IEMAllCImplStrInstr.cpp.h"
6020#define OP_SIZE 32
6021#define ADDR_SIZE 64
6022#include "IEMAllCImplStrInstr.cpp.h"
6023
6024#define OP_SIZE 64
6025#define ADDR_SIZE 32
6026#include "IEMAllCImplStrInstr.cpp.h"
6027#define OP_SIZE 64
6028#define ADDR_SIZE 64
6029#include "IEMAllCImplStrInstr.cpp.h"
6030
6031
6032/**
6033 * Implements 'XGETBV'.
6034 */
6035IEM_CIMPL_DEF_0(iemCImpl_xgetbv)
6036{
6037 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
6038 if (pCtx->cr4 & X86_CR4_OSXSAVE)
6039 {
6040 uint32_t uEcx = pCtx->ecx;
6041 switch (uEcx)
6042 {
6043 case 0:
6044 break;
6045
6046 case 1: /** @todo Implement XCR1 support. */
6047 default:
6048 Log(("xgetbv ecx=%RX32 -> #GP(0)\n", uEcx));
6049 return iemRaiseGeneralProtectionFault0(pIemCpu);
6050
6051 }
6052 pCtx->rax = RT_LO_U32(pCtx->aXcr[uEcx]);
6053 pCtx->rdx = RT_HI_U32(pCtx->aXcr[uEcx]);
6054
6055 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
6056 return VINF_SUCCESS;
6057 }
6058 Log(("xgetbv CR4.OSXSAVE=0 -> UD\n"));
6059 return iemRaiseUndefinedOpcode(pIemCpu);
6060}
6061
6062
6063/**
6064 * Implements 'XSETBV'.
6065 */
6066IEM_CIMPL_DEF_0(iemCImpl_xsetbv)
6067{
6068 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
6069 if (pCtx->cr4 & X86_CR4_OSXSAVE)
6070 {
6071 if (pIemCpu->uCpl == 0)
6072 {
6073 uint32_t uEcx = pCtx->ecx;
6074 uint64_t uNewValue = RT_MAKE_U64(pCtx->eax, pCtx->edx);
6075 switch (uEcx)
6076 {
6077 case 0:
6078 {
6079 int rc = CPUMSetGuestXcr0(IEMCPU_TO_VMCPU(pIemCpu), uNewValue);
6080 if (rc == VINF_SUCCESS)
6081 break;
6082 Assert(rc == VERR_CPUM_RAISE_GP_0);
6083 Log(("xsetbv ecx=%RX32 (newvalue=%RX64) -> #GP(0)\n", uEcx, uNewValue));
6084 return iemRaiseGeneralProtectionFault0(pIemCpu);
6085 }
6086
6087 case 1: /** @todo Implement XCR1 support. */
6088 default:
6089 Log(("xsetbv ecx=%RX32 (newvalue=%RX64) -> #GP(0)\n", uEcx, uNewValue));
6090 return iemRaiseGeneralProtectionFault0(pIemCpu);
6091
6092 }
6093
6094 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
6095 return VINF_SUCCESS;
6096 }
6097
6098 Log(("xsetbv cpl=%u -> GP(0)\n", pIemCpu->uCpl));
6099 return iemRaiseGeneralProtectionFault0(pIemCpu);
6100 }
6101 Log(("xsetbv CR4.OSXSAVE=0 -> UD\n"));
6102 return iemRaiseUndefinedOpcode(pIemCpu);
6103}
6104
6105
6106
6107/**
6108 * Implements 'FINIT' and 'FNINIT'.
6109 *
6110 * @param fCheckXcpts Whether to check for umasked pending exceptions or
6111 * not.
6112 */
6113IEM_CIMPL_DEF_1(iemCImpl_finit, bool, fCheckXcpts)
6114{
6115 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
6116
6117 if (pCtx->cr0 & (X86_CR0_EM | X86_CR0_TS))
6118 return iemRaiseDeviceNotAvailable(pIemCpu);
6119
6120 NOREF(fCheckXcpts); /** @todo trigger pending exceptions:
6121 if (fCheckXcpts && TODO )
6122 return iemRaiseMathFault(pIemCpu);
6123 */
6124
6125 PX86XSAVEAREA pXState = pCtx->CTX_SUFF(pXState);
6126 pXState->x87.FCW = 0x37f;
6127 pXState->x87.FSW = 0;
6128 pXState->x87.FTW = 0x00; /* 0 - empty. */
6129 pXState->x87.FPUDP = 0;
6130 pXState->x87.DS = 0; //??
6131 pXState->x87.Rsrvd2= 0;
6132 pXState->x87.FPUIP = 0;
6133 pXState->x87.CS = 0; //??
6134 pXState->x87.Rsrvd1= 0;
6135 pXState->x87.FOP = 0;
6136
6137 iemHlpUsedFpu(pIemCpu);
6138 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
6139 return VINF_SUCCESS;
6140}
6141
6142
6143/**
6144 * Implements 'FXSAVE'.
6145 *
6146 * @param iEffSeg The effective segment.
6147 * @param GCPtrEff The address of the image.
6148 * @param enmEffOpSize The operand size (only REX.W really matters).
6149 */
6150IEM_CIMPL_DEF_3(iemCImpl_fxsave, uint8_t, iEffSeg, RTGCPTR, GCPtrEff, IEMMODE, enmEffOpSize)
6151{
6152 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
6153
6154 /*
6155 * Raise exceptions.
6156 */
6157 if (pCtx->cr0 & X86_CR0_EM)
6158 return iemRaiseUndefinedOpcode(pIemCpu);
6159 if (pCtx->cr0 & (X86_CR0_TS | X86_CR0_EM))
6160 return iemRaiseDeviceNotAvailable(pIemCpu);
6161 if (GCPtrEff & 15)
6162 {
6163 /** @todo CPU/VM detection possible! \#AC might not be signal for
6164 * all/any misalignment sizes, intel says its an implementation detail. */
6165 if ( (pCtx->cr0 & X86_CR0_AM)
6166 && pCtx->eflags.Bits.u1AC
6167 && pIemCpu->uCpl == 3)
6168 return iemRaiseAlignmentCheckException(pIemCpu);
6169 return iemRaiseGeneralProtectionFault0(pIemCpu);
6170 }
6171
6172 /*
6173 * Access the memory.
6174 */
6175 void *pvMem512;
6176 VBOXSTRICTRC rcStrict = iemMemMap(pIemCpu, &pvMem512, 512, iEffSeg, GCPtrEff, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
6177 if (rcStrict != VINF_SUCCESS)
6178 return rcStrict;
6179 PX86FXSTATE pDst = (PX86FXSTATE)pvMem512;
6180 PCX86FXSTATE pSrc = &pCtx->CTX_SUFF(pXState)->x87;
6181
6182 /*
6183 * Store the registers.
6184 */
6185 /** @todo CPU/VM detection possible! If CR4.OSFXSR=0 MXCSR it's
6186 * implementation specific whether MXCSR and XMM0-XMM7 are saved. */
6187
6188 /* common for all formats */
6189 pDst->FCW = pSrc->FCW;
6190 pDst->FSW = pSrc->FSW;
6191 pDst->FTW = pSrc->FTW & UINT16_C(0xff);
6192 pDst->FOP = pSrc->FOP;
6193 pDst->MXCSR = pSrc->MXCSR;
6194 pDst->MXCSR_MASK = pSrc->MXCSR_MASK;
6195 for (uint32_t i = 0; i < RT_ELEMENTS(pDst->aRegs); i++)
6196 {
6197 /** @todo Testcase: What actually happens to the 6 reserved bytes? I'm clearing
6198 * them for now... */
6199 pDst->aRegs[i].au32[0] = pSrc->aRegs[i].au32[0];
6200 pDst->aRegs[i].au32[1] = pSrc->aRegs[i].au32[1];
6201 pDst->aRegs[i].au32[2] = pSrc->aRegs[i].au32[2] & UINT32_C(0xffff);
6202 pDst->aRegs[i].au32[3] = 0;
6203 }
6204
6205 /* FPU IP, CS, DP and DS. */
6206 pDst->FPUIP = pSrc->FPUIP;
6207 pDst->CS = pSrc->CS;
6208 pDst->FPUDP = pSrc->FPUDP;
6209 pDst->DS = pSrc->DS;
6210 if (enmEffOpSize == IEMMODE_64BIT)
6211 {
6212 /* Save upper 16-bits of FPUIP (IP:CS:Rsvd1) and FPUDP (DP:DS:Rsvd2). */
6213 pDst->Rsrvd1 = pSrc->Rsrvd1;
6214 pDst->Rsrvd2 = pSrc->Rsrvd2;
6215 pDst->au32RsrvdForSoftware[0] = 0;
6216 }
6217 else
6218 {
6219 pDst->Rsrvd1 = 0;
6220 pDst->Rsrvd2 = 0;
6221 pDst->au32RsrvdForSoftware[0] = X86_FXSTATE_RSVD_32BIT_MAGIC;
6222 }
6223
6224 /* XMM registers. */
6225 if ( !(pCtx->msrEFER & MSR_K6_EFER_FFXSR)
6226 || pIemCpu->enmCpuMode != IEMMODE_64BIT
6227 || pIemCpu->uCpl != 0)
6228 {
6229 uint32_t cXmmRegs = enmEffOpSize == IEMMODE_64BIT ? 16 : 8;
6230 for (uint32_t i = 0; i < cXmmRegs; i++)
6231 pDst->aXMM[i] = pSrc->aXMM[i];
6232 /** @todo Testcase: What happens to the reserved XMM registers? Untouched,
6233 * right? */
6234 }
6235
6236 /*
6237 * Commit the memory.
6238 */
6239 rcStrict = iemMemCommitAndUnmap(pIemCpu, pvMem512, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
6240 if (rcStrict != VINF_SUCCESS)
6241 return rcStrict;
6242
6243 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
6244 return VINF_SUCCESS;
6245}
6246
6247
6248/**
6249 * Implements 'FXRSTOR'.
6250 *
6251 * @param GCPtrEff The address of the image.
6252 * @param enmEffOpSize The operand size (only REX.W really matters).
6253 */
6254IEM_CIMPL_DEF_3(iemCImpl_fxrstor, uint8_t, iEffSeg, RTGCPTR, GCPtrEff, IEMMODE, enmEffOpSize)
6255{
6256 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
6257
6258 /*
6259 * Raise exceptions.
6260 */
6261 if (pCtx->cr0 & X86_CR0_EM)
6262 return iemRaiseUndefinedOpcode(pIemCpu);
6263 if (pCtx->cr0 & (X86_CR0_TS | X86_CR0_EM))
6264 return iemRaiseDeviceNotAvailable(pIemCpu);
6265 if (GCPtrEff & 15)
6266 {
6267 /** @todo CPU/VM detection possible! \#AC might not be signal for
6268 * all/any misalignment sizes, intel says its an implementation detail. */
6269 if ( (pCtx->cr0 & X86_CR0_AM)
6270 && pCtx->eflags.Bits.u1AC
6271 && pIemCpu->uCpl == 3)
6272 return iemRaiseAlignmentCheckException(pIemCpu);
6273 return iemRaiseGeneralProtectionFault0(pIemCpu);
6274 }
6275
6276 /*
6277 * Access the memory.
6278 */
6279 void *pvMem512;
6280 VBOXSTRICTRC rcStrict = iemMemMap(pIemCpu, &pvMem512, 512, iEffSeg, GCPtrEff, IEM_ACCESS_DATA_R);
6281 if (rcStrict != VINF_SUCCESS)
6282 return rcStrict;
6283 PCX86FXSTATE pSrc = (PCX86FXSTATE)pvMem512;
6284 PX86FXSTATE pDst = &pCtx->CTX_SUFF(pXState)->x87;
6285
6286 /*
6287 * Check the state for stuff which will #GP(0).
6288 */
6289 uint32_t const fMXCSR = pSrc->MXCSR;
6290 uint32_t const fMXCSR_MASK = pDst->MXCSR_MASK ? pDst->MXCSR_MASK : UINT32_C(0xffbf);
6291 if (fMXCSR & ~fMXCSR_MASK)
6292 {
6293 Log(("fxrstor: MXCSR=%#x (MXCSR_MASK=%#x) -> #GP(0)\n", fMXCSR, fMXCSR_MASK));
6294 return iemRaiseGeneralProtectionFault0(pIemCpu);
6295 }
6296
6297 /*
6298 * Load the registers.
6299 */
6300 /** @todo CPU/VM detection possible! If CR4.OSFXSR=0 MXCSR it's
6301 * implementation specific whether MXCSR and XMM0-XMM7 are restored. */
6302
6303 /* common for all formats */
6304 pDst->FCW = pSrc->FCW;
6305 pDst->FSW = pSrc->FSW;
6306 pDst->FTW = pSrc->FTW & UINT16_C(0xff);
6307 pDst->FOP = pSrc->FOP;
6308 pDst->MXCSR = fMXCSR;
6309 /* (MXCSR_MASK is read-only) */
6310 for (uint32_t i = 0; i < RT_ELEMENTS(pSrc->aRegs); i++)
6311 {
6312 pDst->aRegs[i].au32[0] = pSrc->aRegs[i].au32[0];
6313 pDst->aRegs[i].au32[1] = pSrc->aRegs[i].au32[1];
6314 pDst->aRegs[i].au32[2] = pSrc->aRegs[i].au32[2] & UINT32_C(0xffff);
6315 pDst->aRegs[i].au32[3] = 0;
6316 }
6317
6318 /* FPU IP, CS, DP and DS. */
6319 if (pIemCpu->enmCpuMode == IEMMODE_64BIT)
6320 {
6321 pDst->FPUIP = pSrc->FPUIP;
6322 pDst->CS = pSrc->CS;
6323 pDst->Rsrvd1 = pSrc->Rsrvd1;
6324 pDst->FPUDP = pSrc->FPUDP;
6325 pDst->DS = pSrc->DS;
6326 pDst->Rsrvd2 = pSrc->Rsrvd2;
6327 }
6328 else
6329 {
6330 pDst->FPUIP = pSrc->FPUIP;
6331 pDst->CS = pSrc->CS;
6332 pDst->Rsrvd1 = 0;
6333 pDst->FPUDP = pSrc->FPUDP;
6334 pDst->DS = pSrc->DS;
6335 pDst->Rsrvd2 = 0;
6336 }
6337
6338 /* XMM registers. */
6339 if ( !(pCtx->msrEFER & MSR_K6_EFER_FFXSR)
6340 || pIemCpu->enmCpuMode != IEMMODE_64BIT
6341 || pIemCpu->uCpl != 0)
6342 {
6343 uint32_t cXmmRegs = enmEffOpSize == IEMMODE_64BIT ? 16 : 8;
6344 for (uint32_t i = 0; i < cXmmRegs; i++)
6345 pDst->aXMM[i] = pSrc->aXMM[i];
6346 }
6347
6348 /*
6349 * Commit the memory.
6350 */
6351 rcStrict = iemMemCommitAndUnmap(pIemCpu, pvMem512, IEM_ACCESS_DATA_R);
6352 if (rcStrict != VINF_SUCCESS)
6353 return rcStrict;
6354
6355 iemHlpUsedFpu(pIemCpu);
6356 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
6357 return VINF_SUCCESS;
6358}
6359
6360
6361/**
6362 * Commmon routine for fnstenv and fnsave.
6363 *
6364 * @param uPtr Where to store the state.
6365 * @param pCtx The CPU context.
6366 */
6367static void iemCImplCommonFpuStoreEnv(PIEMCPU pIemCpu, IEMMODE enmEffOpSize, RTPTRUNION uPtr, PCCPUMCTX pCtx)
6368{
6369 PCX86FXSTATE pSrcX87 = &pCtx->CTX_SUFF(pXState)->x87;
6370 if (enmEffOpSize == IEMMODE_16BIT)
6371 {
6372 uPtr.pu16[0] = pSrcX87->FCW;
6373 uPtr.pu16[1] = pSrcX87->FSW;
6374 uPtr.pu16[2] = iemFpuCalcFullFtw(pSrcX87);
6375 if (IEM_IS_REAL_OR_V86_MODE(pIemCpu))
6376 {
6377 /** @todo Testcase: How does this work when the FPUIP/CS was saved in
6378 * protected mode or long mode and we save it in real mode? And vice
6379 * versa? And with 32-bit operand size? I think CPU is storing the
6380 * effective address ((CS << 4) + IP) in the offset register and not
6381 * doing any address calculations here. */
6382 uPtr.pu16[3] = (uint16_t)pSrcX87->FPUIP;
6383 uPtr.pu16[4] = ((pSrcX87->FPUIP >> 4) & UINT16_C(0xf000)) | pSrcX87->FOP;
6384 uPtr.pu16[5] = (uint16_t)pSrcX87->FPUDP;
6385 uPtr.pu16[6] = (pSrcX87->FPUDP >> 4) & UINT16_C(0xf000);
6386 }
6387 else
6388 {
6389 uPtr.pu16[3] = pSrcX87->FPUIP;
6390 uPtr.pu16[4] = pSrcX87->CS;
6391 uPtr.pu16[5] = pSrcX87->FPUDP;
6392 uPtr.pu16[6] = pSrcX87->DS;
6393 }
6394 }
6395 else
6396 {
6397 /** @todo Testcase: what is stored in the "gray" areas? (figure 8-9 and 8-10) */
6398 uPtr.pu16[0*2] = pSrcX87->FCW;
6399 uPtr.pu16[1*2] = pSrcX87->FSW;
6400 uPtr.pu16[2*2] = iemFpuCalcFullFtw(pSrcX87);
6401 if (IEM_IS_REAL_OR_V86_MODE(pIemCpu))
6402 {
6403 uPtr.pu16[3*2] = (uint16_t)pSrcX87->FPUIP;
6404 uPtr.pu32[4] = ((pSrcX87->FPUIP & UINT32_C(0xffff0000)) >> 4) | pSrcX87->FOP;
6405 uPtr.pu16[5*2] = (uint16_t)pSrcX87->FPUDP;
6406 uPtr.pu32[6] = (pSrcX87->FPUDP & UINT32_C(0xffff0000)) >> 4;
6407 }
6408 else
6409 {
6410 uPtr.pu32[3] = pSrcX87->FPUIP;
6411 uPtr.pu16[4*2] = pSrcX87->CS;
6412 uPtr.pu16[4*2+1]= pSrcX87->FOP;
6413 uPtr.pu32[5] = pSrcX87->FPUDP;
6414 uPtr.pu16[6*2] = pSrcX87->DS;
6415 }
6416 }
6417}
6418
6419
6420/**
6421 * Commmon routine for fldenv and frstor
6422 *
6423 * @param uPtr Where to store the state.
6424 * @param pCtx The CPU context.
6425 */
6426static void iemCImplCommonFpuRestoreEnv(PIEMCPU pIemCpu, IEMMODE enmEffOpSize, RTCPTRUNION uPtr, PCPUMCTX pCtx)
6427{
6428 PX86FXSTATE pDstX87 = &pCtx->CTX_SUFF(pXState)->x87;
6429 if (enmEffOpSize == IEMMODE_16BIT)
6430 {
6431 pDstX87->FCW = uPtr.pu16[0];
6432 pDstX87->FSW = uPtr.pu16[1];
6433 pDstX87->FTW = uPtr.pu16[2];
6434 if (IEM_IS_REAL_OR_V86_MODE(pIemCpu))
6435 {
6436 pDstX87->FPUIP = uPtr.pu16[3] | ((uint32_t)(uPtr.pu16[4] & UINT16_C(0xf000)) << 4);
6437 pDstX87->FPUDP = uPtr.pu16[5] | ((uint32_t)(uPtr.pu16[6] & UINT16_C(0xf000)) << 4);
6438 pDstX87->FOP = uPtr.pu16[4] & UINT16_C(0x07ff);
6439 pDstX87->CS = 0;
6440 pDstX87->Rsrvd1= 0;
6441 pDstX87->DS = 0;
6442 pDstX87->Rsrvd2= 0;
6443 }
6444 else
6445 {
6446 pDstX87->FPUIP = uPtr.pu16[3];
6447 pDstX87->CS = uPtr.pu16[4];
6448 pDstX87->Rsrvd1= 0;
6449 pDstX87->FPUDP = uPtr.pu16[5];
6450 pDstX87->DS = uPtr.pu16[6];
6451 pDstX87->Rsrvd2= 0;
6452 /** @todo Testcase: Is FOP cleared when doing 16-bit protected mode fldenv? */
6453 }
6454 }
6455 else
6456 {
6457 pDstX87->FCW = uPtr.pu16[0*2];
6458 pDstX87->FSW = uPtr.pu16[1*2];
6459 pDstX87->FTW = uPtr.pu16[2*2];
6460 if (IEM_IS_REAL_OR_V86_MODE(pIemCpu))
6461 {
6462 pDstX87->FPUIP = uPtr.pu16[3*2] | ((uPtr.pu32[4] & UINT32_C(0x0ffff000)) << 4);
6463 pDstX87->FOP = uPtr.pu32[4] & UINT16_C(0x07ff);
6464 pDstX87->FPUDP = uPtr.pu16[5*2] | ((uPtr.pu32[6] & UINT32_C(0x0ffff000)) << 4);
6465 pDstX87->CS = 0;
6466 pDstX87->Rsrvd1= 0;
6467 pDstX87->DS = 0;
6468 pDstX87->Rsrvd2= 0;
6469 }
6470 else
6471 {
6472 pDstX87->FPUIP = uPtr.pu32[3];
6473 pDstX87->CS = uPtr.pu16[4*2];
6474 pDstX87->Rsrvd1= 0;
6475 pDstX87->FOP = uPtr.pu16[4*2+1];
6476 pDstX87->FPUDP = uPtr.pu32[5];
6477 pDstX87->DS = uPtr.pu16[6*2];
6478 pDstX87->Rsrvd2= 0;
6479 }
6480 }
6481
6482 /* Make adjustments. */
6483 pDstX87->FTW = iemFpuCompressFtw(pDstX87->FTW);
6484 pDstX87->FCW &= ~X86_FCW_ZERO_MASK;
6485 iemFpuRecalcExceptionStatus(pDstX87);
6486 /** @todo Testcase: Check if ES and/or B are automatically cleared if no
6487 * exceptions are pending after loading the saved state? */
6488}
6489
6490
6491/**
6492 * Implements 'FNSTENV'.
6493 *
6494 * @param enmEffOpSize The operand size (only REX.W really matters).
6495 * @param iEffSeg The effective segment register for @a GCPtrEff.
6496 * @param GCPtrEffDst The address of the image.
6497 */
6498IEM_CIMPL_DEF_3(iemCImpl_fnstenv, IEMMODE, enmEffOpSize, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
6499{
6500 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
6501 RTPTRUNION uPtr;
6502 VBOXSTRICTRC rcStrict = iemMemMap(pIemCpu, &uPtr.pv, enmEffOpSize == IEMMODE_16BIT ? 14 : 28,
6503 iEffSeg, GCPtrEffDst, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
6504 if (rcStrict != VINF_SUCCESS)
6505 return rcStrict;
6506
6507 iemCImplCommonFpuStoreEnv(pIemCpu, enmEffOpSize, uPtr, pCtx);
6508
6509 rcStrict = iemMemCommitAndUnmap(pIemCpu, uPtr.pv, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
6510 if (rcStrict != VINF_SUCCESS)
6511 return rcStrict;
6512
6513 /* Note: C0, C1, C2 and C3 are documented as undefined, we leave them untouched! */
6514 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
6515 return VINF_SUCCESS;
6516}
6517
6518
6519/**
6520 * Implements 'FNSAVE'.
6521 *
6522 * @param GCPtrEffDst The address of the image.
6523 * @param enmEffOpSize The operand size.
6524 */
6525IEM_CIMPL_DEF_3(iemCImpl_fnsave, IEMMODE, enmEffOpSize, uint8_t, iEffSeg, RTGCPTR, GCPtrEffDst)
6526{
6527 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
6528 RTPTRUNION uPtr;
6529 VBOXSTRICTRC rcStrict = iemMemMap(pIemCpu, &uPtr.pv, enmEffOpSize == IEMMODE_16BIT ? 94 : 108,
6530 iEffSeg, GCPtrEffDst, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
6531 if (rcStrict != VINF_SUCCESS)
6532 return rcStrict;
6533
6534 PX86FXSTATE pFpuCtx = &pCtx->CTX_SUFF(pXState)->x87;
6535 iemCImplCommonFpuStoreEnv(pIemCpu, enmEffOpSize, uPtr, pCtx);
6536 PRTFLOAT80U paRegs = (PRTFLOAT80U)(uPtr.pu8 + (enmEffOpSize == IEMMODE_16BIT ? 14 : 28));
6537 for (uint32_t i = 0; i < RT_ELEMENTS(pFpuCtx->aRegs); i++)
6538 {
6539 paRegs[i].au32[0] = pFpuCtx->aRegs[i].au32[0];
6540 paRegs[i].au32[1] = pFpuCtx->aRegs[i].au32[1];
6541 paRegs[i].au16[4] = pFpuCtx->aRegs[i].au16[4];
6542 }
6543
6544 rcStrict = iemMemCommitAndUnmap(pIemCpu, uPtr.pv, IEM_ACCESS_DATA_W | IEM_ACCESS_PARTIAL_WRITE);
6545 if (rcStrict != VINF_SUCCESS)
6546 return rcStrict;
6547
6548 /*
6549 * Re-initialize the FPU context.
6550 */
6551 pFpuCtx->FCW = 0x37f;
6552 pFpuCtx->FSW = 0;
6553 pFpuCtx->FTW = 0x00; /* 0 - empty */
6554 pFpuCtx->FPUDP = 0;
6555 pFpuCtx->DS = 0;
6556 pFpuCtx->Rsrvd2= 0;
6557 pFpuCtx->FPUIP = 0;
6558 pFpuCtx->CS = 0;
6559 pFpuCtx->Rsrvd1= 0;
6560 pFpuCtx->FOP = 0;
6561
6562 iemHlpUsedFpu(pIemCpu);
6563 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
6564 return VINF_SUCCESS;
6565}
6566
6567
6568
6569/**
6570 * Implements 'FLDENV'.
6571 *
6572 * @param enmEffOpSize The operand size (only REX.W really matters).
6573 * @param iEffSeg The effective segment register for @a GCPtrEff.
6574 * @param GCPtrEffSrc The address of the image.
6575 */
6576IEM_CIMPL_DEF_3(iemCImpl_fldenv, IEMMODE, enmEffOpSize, uint8_t, iEffSeg, RTGCPTR, GCPtrEffSrc)
6577{
6578 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
6579 RTCPTRUNION uPtr;
6580 VBOXSTRICTRC rcStrict = iemMemMap(pIemCpu, (void **)&uPtr.pv, enmEffOpSize == IEMMODE_16BIT ? 14 : 28,
6581 iEffSeg, GCPtrEffSrc, IEM_ACCESS_DATA_R);
6582 if (rcStrict != VINF_SUCCESS)
6583 return rcStrict;
6584
6585 iemCImplCommonFpuRestoreEnv(pIemCpu, enmEffOpSize, uPtr, pCtx);
6586
6587 rcStrict = iemMemCommitAndUnmap(pIemCpu, (void *)uPtr.pv, IEM_ACCESS_DATA_R);
6588 if (rcStrict != VINF_SUCCESS)
6589 return rcStrict;
6590
6591 iemHlpUsedFpu(pIemCpu);
6592 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
6593 return VINF_SUCCESS;
6594}
6595
6596
6597/**
6598 * Implements 'FRSTOR'.
6599 *
6600 * @param GCPtrEffSrc The address of the image.
6601 * @param enmEffOpSize The operand size.
6602 */
6603IEM_CIMPL_DEF_3(iemCImpl_frstor, IEMMODE, enmEffOpSize, uint8_t, iEffSeg, RTGCPTR, GCPtrEffSrc)
6604{
6605 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
6606 RTCPTRUNION uPtr;
6607 VBOXSTRICTRC rcStrict = iemMemMap(pIemCpu, (void **)&uPtr.pv, enmEffOpSize == IEMMODE_16BIT ? 94 : 108,
6608 iEffSeg, GCPtrEffSrc, IEM_ACCESS_DATA_R);
6609 if (rcStrict != VINF_SUCCESS)
6610 return rcStrict;
6611
6612 PX86FXSTATE pFpuCtx = &pCtx->CTX_SUFF(pXState)->x87;
6613 iemCImplCommonFpuRestoreEnv(pIemCpu, enmEffOpSize, uPtr, pCtx);
6614 PCRTFLOAT80U paRegs = (PCRTFLOAT80U)(uPtr.pu8 + (enmEffOpSize == IEMMODE_16BIT ? 14 : 28));
6615 for (uint32_t i = 0; i < RT_ELEMENTS(pFpuCtx->aRegs); i++)
6616 {
6617 pFpuCtx->aRegs[i].au32[0] = paRegs[i].au32[0];
6618 pFpuCtx->aRegs[i].au32[1] = paRegs[i].au32[1];
6619 pFpuCtx->aRegs[i].au32[2] = paRegs[i].au16[4];
6620 pFpuCtx->aRegs[i].au32[3] = 0;
6621 }
6622
6623 rcStrict = iemMemCommitAndUnmap(pIemCpu, (void *)uPtr.pv, IEM_ACCESS_DATA_R);
6624 if (rcStrict != VINF_SUCCESS)
6625 return rcStrict;
6626
6627 iemHlpUsedFpu(pIemCpu);
6628 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
6629 return VINF_SUCCESS;
6630}
6631
6632
6633/**
6634 * Implements 'FLDCW'.
6635 *
6636 * @param u16Fcw The new FCW.
6637 */
6638IEM_CIMPL_DEF_1(iemCImpl_fldcw, uint16_t, u16Fcw)
6639{
6640 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
6641
6642 /** @todo Testcase: Check what happens when trying to load X86_FCW_PC_RSVD. */
6643 /** @todo Testcase: Try see what happens when trying to set undefined bits
6644 * (other than 6 and 7). Currently ignoring them. */
6645 /** @todo Testcase: Test that it raises and loweres the FPU exception bits
6646 * according to FSW. (This is was is currently implemented.) */
6647 PX86FXSTATE pFpuCtx = &pCtx->CTX_SUFF(pXState)->x87;
6648 pFpuCtx->FCW = u16Fcw & ~X86_FCW_ZERO_MASK;
6649 iemFpuRecalcExceptionStatus(pFpuCtx);
6650
6651 /* Note: C0, C1, C2 and C3 are documented as undefined, we leave them untouched! */
6652 iemHlpUsedFpu(pIemCpu);
6653 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
6654 return VINF_SUCCESS;
6655}
6656
6657
6658
6659/**
6660 * Implements the underflow case of fxch.
6661 *
6662 * @param iStReg The other stack register.
6663 */
6664IEM_CIMPL_DEF_1(iemCImpl_fxch_underflow, uint8_t, iStReg)
6665{
6666 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
6667
6668 PX86FXSTATE pFpuCtx = &pCtx->CTX_SUFF(pXState)->x87;
6669 unsigned const iReg1 = X86_FSW_TOP_GET(pFpuCtx->FSW);
6670 unsigned const iReg2 = (iReg1 + iStReg) & X86_FSW_TOP_SMASK;
6671 Assert(!(RT_BIT(iReg1) & pFpuCtx->FTW) || !(RT_BIT(iReg2) & pFpuCtx->FTW));
6672
6673 /** @todo Testcase: fxch underflow. Making assumptions that underflowed
6674 * registers are read as QNaN and then exchanged. This could be
6675 * wrong... */
6676 if (pFpuCtx->FCW & X86_FCW_IM)
6677 {
6678 if (RT_BIT(iReg1) & pFpuCtx->FTW)
6679 {
6680 if (RT_BIT(iReg2) & pFpuCtx->FTW)
6681 iemFpuStoreQNan(&pFpuCtx->aRegs[0].r80);
6682 else
6683 pFpuCtx->aRegs[0].r80 = pFpuCtx->aRegs[iStReg].r80;
6684 iemFpuStoreQNan(&pFpuCtx->aRegs[iStReg].r80);
6685 }
6686 else
6687 {
6688 pFpuCtx->aRegs[iStReg].r80 = pFpuCtx->aRegs[0].r80;
6689 iemFpuStoreQNan(&pFpuCtx->aRegs[0].r80);
6690 }
6691 pFpuCtx->FSW &= ~X86_FSW_C_MASK;
6692 pFpuCtx->FSW |= X86_FSW_C1 | X86_FSW_IE | X86_FSW_SF;
6693 }
6694 else
6695 {
6696 /* raise underflow exception, don't change anything. */
6697 pFpuCtx->FSW &= ~(X86_FSW_TOP_MASK | X86_FSW_XCPT_MASK);
6698 pFpuCtx->FSW |= X86_FSW_C1 | X86_FSW_IE | X86_FSW_SF | X86_FSW_ES | X86_FSW_B;
6699 }
6700
6701 iemFpuUpdateOpcodeAndIpWorker(pIemCpu, pCtx, pFpuCtx);
6702 iemHlpUsedFpu(pIemCpu);
6703 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
6704 return VINF_SUCCESS;
6705}
6706
6707
6708/**
6709 * Implements 'FCOMI', 'FCOMIP', 'FUCOMI', and 'FUCOMIP'.
6710 *
6711 * @param cToAdd 1 or 7.
6712 */
6713IEM_CIMPL_DEF_3(iemCImpl_fcomi_fucomi, uint8_t, iStReg, PFNIEMAIMPLFPUR80EFL, pfnAImpl, bool, fPop)
6714{
6715 PCPUMCTX pCtx = pIemCpu->CTX_SUFF(pCtx);
6716 Assert(iStReg < 8);
6717
6718 /*
6719 * Raise exceptions.
6720 */
6721 if (pCtx->cr0 & (X86_CR0_EM | X86_CR0_TS))
6722 return iemRaiseDeviceNotAvailable(pIemCpu);
6723
6724 PX86FXSTATE pFpuCtx = &pCtx->CTX_SUFF(pXState)->x87;
6725 uint16_t u16Fsw = pFpuCtx->FSW;
6726 if (u16Fsw & X86_FSW_ES)
6727 return iemRaiseMathFault(pIemCpu);
6728
6729 /*
6730 * Check if any of the register accesses causes #SF + #IA.
6731 */
6732 unsigned const iReg1 = X86_FSW_TOP_GET(u16Fsw);
6733 unsigned const iReg2 = (iReg1 + iStReg) & X86_FSW_TOP_SMASK;
6734 if ((pFpuCtx->FTW & (RT_BIT(iReg1) | RT_BIT(iReg2))) == (RT_BIT(iReg1) | RT_BIT(iReg2)))
6735 {
6736 uint32_t u32Eflags = pfnAImpl(pFpuCtx, &u16Fsw, &pFpuCtx->aRegs[0].r80, &pFpuCtx->aRegs[iStReg].r80);
6737 NOREF(u32Eflags);
6738
6739 pFpuCtx->FSW &= ~X86_FSW_C1;
6740 pFpuCtx->FSW |= u16Fsw & ~X86_FSW_TOP_MASK;
6741 if ( !(u16Fsw & X86_FSW_IE)
6742 || (pFpuCtx->FCW & X86_FCW_IM) )
6743 {
6744 pCtx->eflags.u &= ~(X86_EFL_OF | X86_EFL_SF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_PF | X86_EFL_CF);
6745 pCtx->eflags.u |= pCtx->eflags.u & (X86_EFL_ZF | X86_EFL_PF | X86_EFL_CF);
6746 }
6747 }
6748 else if (pFpuCtx->FCW & X86_FCW_IM)
6749 {
6750 /* Masked underflow. */
6751 pFpuCtx->FSW &= ~X86_FSW_C1;
6752 pFpuCtx->FSW |= X86_FSW_IE | X86_FSW_SF;
6753 pCtx->eflags.u &= ~(X86_EFL_OF | X86_EFL_SF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_PF | X86_EFL_CF);
6754 pCtx->eflags.u |= X86_EFL_ZF | X86_EFL_PF | X86_EFL_CF;
6755 }
6756 else
6757 {
6758 /* Raise underflow - don't touch EFLAGS or TOP. */
6759 pFpuCtx->FSW &= ~X86_FSW_C1;
6760 pFpuCtx->FSW |= X86_FSW_IE | X86_FSW_SF | X86_FSW_ES | X86_FSW_B;
6761 fPop = false;
6762 }
6763
6764 /*
6765 * Pop if necessary.
6766 */
6767 if (fPop)
6768 {
6769 pFpuCtx->FTW &= ~RT_BIT(iReg1);
6770 pFpuCtx->FSW &= X86_FSW_TOP_MASK;
6771 pFpuCtx->FSW |= ((iReg1 + 7) & X86_FSW_TOP_SMASK) << X86_FSW_TOP_SHIFT;
6772 }
6773
6774 iemFpuUpdateOpcodeAndIpWorker(pIemCpu, pCtx, pFpuCtx);
6775 iemHlpUsedFpu(pIemCpu);
6776 iemRegAddToRipAndClearRF(pIemCpu, cbInstr);
6777 return VINF_SUCCESS;
6778}
6779
6780/** @} */
6781
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