1 | /* $Id: IOMAllMMIO.cpp 19474 2009-05-07 10:08:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IOM - Input / Output Monitor - Any Context, MMIO & String I/O.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #define LOG_GROUP LOG_GROUP_IOM
|
---|
27 | #include <VBox/iom.h>
|
---|
28 | #include <VBox/cpum.h>
|
---|
29 | #include <VBox/pgm.h>
|
---|
30 | #include <VBox/selm.h>
|
---|
31 | #include <VBox/mm.h>
|
---|
32 | #include <VBox/em.h>
|
---|
33 | #include <VBox/pgm.h>
|
---|
34 | #include <VBox/trpm.h>
|
---|
35 | #include "IOMInternal.h"
|
---|
36 | #include <VBox/vm.h>
|
---|
37 | #include <VBox/vmm.h>
|
---|
38 | #include <VBox/hwaccm.h>
|
---|
39 |
|
---|
40 | #include <VBox/dis.h>
|
---|
41 | #include <VBox/disopcode.h>
|
---|
42 | #include <VBox/param.h>
|
---|
43 | #include <VBox/err.h>
|
---|
44 | #include <iprt/assert.h>
|
---|
45 | #include <VBox/log.h>
|
---|
46 | #include <iprt/asm.h>
|
---|
47 | #include <iprt/string.h>
|
---|
48 |
|
---|
49 |
|
---|
50 | /*******************************************************************************
|
---|
51 | * Global Variables *
|
---|
52 | *******************************************************************************/
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Array for fast recode of the operand size (1/2/4/8 bytes) to bit shift value.
|
---|
56 | */
|
---|
57 | static const unsigned g_aSize2Shift[] =
|
---|
58 | {
|
---|
59 | ~0, /* 0 - invalid */
|
---|
60 | 0, /* *1 == 2^0 */
|
---|
61 | 1, /* *2 == 2^1 */
|
---|
62 | ~0, /* 3 - invalid */
|
---|
63 | 2, /* *4 == 2^2 */
|
---|
64 | ~0, /* 5 - invalid */
|
---|
65 | ~0, /* 6 - invalid */
|
---|
66 | ~0, /* 7 - invalid */
|
---|
67 | 3 /* *8 == 2^3 */
|
---|
68 | };
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Macro for fast recode of the operand size (1/2/4/8 bytes) to bit shift value.
|
---|
72 | */
|
---|
73 | #define SIZE_2_SHIFT(cb) (g_aSize2Shift[cb])
|
---|
74 |
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Wrapper which does the write and updates range statistics when such are enabled.
|
---|
78 | * @warning RT_SUCCESS(rc=VINF_IOM_HC_MMIO_WRITE) is TRUE!
|
---|
79 | */
|
---|
80 | DECLINLINE(int) iomMMIODoWrite(PVM pVM, PIOMMMIORANGE pRange, RTGCPHYS GCPhysFault, const void *pvData, unsigned cb)
|
---|
81 | {
|
---|
82 | #ifdef VBOX_WITH_STATISTICS
|
---|
83 | PIOMMMIOSTATS pStats = iomMMIOGetStats(&pVM->iom.s, GCPhysFault, pRange);
|
---|
84 | Assert(pStats);
|
---|
85 | #endif
|
---|
86 |
|
---|
87 | int rc;
|
---|
88 | if (RT_LIKELY(pRange->CTX_SUFF(pfnWriteCallback)))
|
---|
89 | rc = pRange->CTX_SUFF(pfnWriteCallback)(pRange->CTX_SUFF(pDevIns), pRange->CTX_SUFF(pvUser), GCPhysFault, (void *)pvData, cb); /* @todo fix const!! */
|
---|
90 | else
|
---|
91 | rc = VINF_SUCCESS;
|
---|
92 | if (rc != VINF_IOM_HC_MMIO_WRITE)
|
---|
93 | STAM_COUNTER_INC(&pStats->CTX_SUFF_Z(Write));
|
---|
94 | return rc;
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Wrapper which does the read and updates range statistics when such are enabled.
|
---|
100 | */
|
---|
101 | DECLINLINE(int) iomMMIODoRead(PVM pVM, PIOMMMIORANGE pRange, RTGCPHYS GCPhys, void *pvValue, unsigned cbValue)
|
---|
102 | {
|
---|
103 | #ifdef VBOX_WITH_STATISTICS
|
---|
104 | PIOMMMIOSTATS pStats = iomMMIOGetStats(&pVM->iom.s, GCPhys, pRange);
|
---|
105 | Assert(pStats);
|
---|
106 | #endif
|
---|
107 |
|
---|
108 | int rc;
|
---|
109 | if (RT_LIKELY(pRange->CTX_SUFF(pfnReadCallback)))
|
---|
110 | rc = pRange->CTX_SUFF(pfnReadCallback)(pRange->CTX_SUFF(pDevIns), pRange->CTX_SUFF(pvUser), GCPhys, pvValue, cbValue);
|
---|
111 | else
|
---|
112 | rc = VINF_IOM_MMIO_UNUSED_FF;
|
---|
113 | if (rc != VINF_SUCCESS)
|
---|
114 | {
|
---|
115 | switch (rc)
|
---|
116 | {
|
---|
117 | case VINF_IOM_MMIO_UNUSED_FF:
|
---|
118 | switch (cbValue)
|
---|
119 | {
|
---|
120 | case 1: *(uint8_t *)pvValue = UINT8_C(0xff); break;
|
---|
121 | case 2: *(uint16_t *)pvValue = UINT16_C(0xffff); break;
|
---|
122 | case 4: *(uint32_t *)pvValue = UINT32_C(0xffffffff); break;
|
---|
123 | case 8: *(uint64_t *)pvValue = UINT64_C(0xffffffffffffffff); break;
|
---|
124 | default: AssertReleaseMsgFailed(("cbValue=%d GCPhys=%RGp\n", cbValue, GCPhys)); break;
|
---|
125 | }
|
---|
126 | rc = VINF_SUCCESS;
|
---|
127 | break;
|
---|
128 |
|
---|
129 | case VINF_IOM_MMIO_UNUSED_00:
|
---|
130 | switch (cbValue)
|
---|
131 | {
|
---|
132 | case 1: *(uint8_t *)pvValue = UINT8_C(0x00); break;
|
---|
133 | case 2: *(uint16_t *)pvValue = UINT16_C(0x0000); break;
|
---|
134 | case 4: *(uint32_t *)pvValue = UINT32_C(0x00000000); break;
|
---|
135 | case 8: *(uint64_t *)pvValue = UINT64_C(0x0000000000000000); break;
|
---|
136 | default: AssertReleaseMsgFailed(("cbValue=%d GCPhys=%RGp\n", cbValue, GCPhys)); break;
|
---|
137 | }
|
---|
138 | rc = VINF_SUCCESS;
|
---|
139 | break;
|
---|
140 | }
|
---|
141 | if (rc != VINF_IOM_HC_MMIO_READ)
|
---|
142 | STAM_COUNTER_INC(&pStats->CTX_SUFF_Z(Read));
|
---|
143 | }
|
---|
144 | return rc;
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Internal - statistics only.
|
---|
150 | */
|
---|
151 | DECLINLINE(void) iomMMIOStatLength(PVM pVM, unsigned cb)
|
---|
152 | {
|
---|
153 | #ifdef VBOX_WITH_STATISTICS
|
---|
154 | switch (cb)
|
---|
155 | {
|
---|
156 | case 1:
|
---|
157 | STAM_COUNTER_INC(&pVM->iom.s.StatRZMMIO1Byte);
|
---|
158 | break;
|
---|
159 | case 2:
|
---|
160 | STAM_COUNTER_INC(&pVM->iom.s.StatRZMMIO2Bytes);
|
---|
161 | break;
|
---|
162 | case 4:
|
---|
163 | STAM_COUNTER_INC(&pVM->iom.s.StatRZMMIO4Bytes);
|
---|
164 | break;
|
---|
165 | case 8:
|
---|
166 | STAM_COUNTER_INC(&pVM->iom.s.StatRZMMIO8Bytes);
|
---|
167 | break;
|
---|
168 | default:
|
---|
169 | /* No way. */
|
---|
170 | AssertMsgFailed(("Invalid data length %d\n", cb));
|
---|
171 | break;
|
---|
172 | }
|
---|
173 | #else
|
---|
174 | NOREF(pVM); NOREF(cb);
|
---|
175 | #endif
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * MOV reg, mem (read)
|
---|
181 | * MOVZX reg, mem (read)
|
---|
182 | * MOVSX reg, mem (read)
|
---|
183 | *
|
---|
184 | * @returns VBox status code.
|
---|
185 | *
|
---|
186 | * @param pVM The virtual machine.
|
---|
187 | * @param pRegFrame Pointer to CPUMCTXCORE guest registers structure.
|
---|
188 | * @param pCpu Disassembler CPU state.
|
---|
189 | * @param pRange Pointer MMIO range.
|
---|
190 | * @param GCPhysFault The GC physical address corresponding to pvFault.
|
---|
191 | */
|
---|
192 | static int iomInterpretMOVxXRead(PVM pVM, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pCpu, PIOMMMIORANGE pRange, RTGCPHYS GCPhysFault)
|
---|
193 | {
|
---|
194 | Assert(pRange->CTX_SUFF(pfnReadCallback) || !pRange->pfnReadCallbackR3);
|
---|
195 |
|
---|
196 | /*
|
---|
197 | * Get the data size from parameter 2,
|
---|
198 | * and call the handler function to get the data.
|
---|
199 | */
|
---|
200 | unsigned cb = DISGetParamSize(pCpu, &pCpu->param2);
|
---|
201 | AssertMsg(cb > 0 && cb <= sizeof(uint64_t), ("cb=%d\n", cb));
|
---|
202 |
|
---|
203 | uint64_t u64Data = 0;
|
---|
204 | int rc = iomMMIODoRead(pVM, pRange, GCPhysFault, &u64Data, cb);
|
---|
205 | if (rc == VINF_SUCCESS)
|
---|
206 | {
|
---|
207 | /*
|
---|
208 | * Do sign extension for MOVSX.
|
---|
209 | */
|
---|
210 | /** @todo checkup MOVSX implementation! */
|
---|
211 | if (pCpu->pCurInstr->opcode == OP_MOVSX)
|
---|
212 | {
|
---|
213 | if (cb == 1)
|
---|
214 | {
|
---|
215 | /* DWORD <- BYTE */
|
---|
216 | int64_t iData = (int8_t)u64Data;
|
---|
217 | u64Data = (uint64_t)iData;
|
---|
218 | }
|
---|
219 | else
|
---|
220 | {
|
---|
221 | /* DWORD <- WORD */
|
---|
222 | int64_t iData = (int16_t)u64Data;
|
---|
223 | u64Data = (uint64_t)iData;
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | /*
|
---|
228 | * Store the result to register (parameter 1).
|
---|
229 | */
|
---|
230 | bool fRc = iomSaveDataToReg(pCpu, &pCpu->param1, pRegFrame, u64Data);
|
---|
231 | AssertMsg(fRc, ("Failed to store register value!\n")); NOREF(fRc);
|
---|
232 | }
|
---|
233 |
|
---|
234 | if (rc == VINF_SUCCESS)
|
---|
235 | iomMMIOStatLength(pVM, cb);
|
---|
236 | return rc;
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * MOV mem, reg|imm (write)
|
---|
242 | *
|
---|
243 | * @returns VBox status code.
|
---|
244 | *
|
---|
245 | * @param pVM The virtual machine.
|
---|
246 | * @param pRegFrame Pointer to CPUMCTXCORE guest registers structure.
|
---|
247 | * @param pCpu Disassembler CPU state.
|
---|
248 | * @param pRange Pointer MMIO range.
|
---|
249 | * @param GCPhysFault The GC physical address corresponding to pvFault.
|
---|
250 | */
|
---|
251 | static int iomInterpretMOVxXWrite(PVM pVM, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pCpu, PIOMMMIORANGE pRange, RTGCPHYS GCPhysFault)
|
---|
252 | {
|
---|
253 | Assert(pRange->CTX_SUFF(pfnWriteCallback) || !pRange->pfnWriteCallbackR3);
|
---|
254 |
|
---|
255 | /*
|
---|
256 | * Get data to write from second parameter,
|
---|
257 | * and call the callback to write it.
|
---|
258 | */
|
---|
259 | unsigned cb = 0;
|
---|
260 | uint64_t u64Data = 0;
|
---|
261 | bool fRc = iomGetRegImmData(pCpu, &pCpu->param2, pRegFrame, &u64Data, &cb);
|
---|
262 | AssertMsg(fRc, ("Failed to get reg/imm port number!\n")); NOREF(fRc);
|
---|
263 |
|
---|
264 | int rc = iomMMIODoWrite(pVM, pRange, GCPhysFault, &u64Data, cb);
|
---|
265 | if (rc == VINF_SUCCESS)
|
---|
266 | iomMMIOStatLength(pVM, cb);
|
---|
267 | return rc;
|
---|
268 | }
|
---|
269 |
|
---|
270 |
|
---|
271 | /** Wrapper for reading virtual memory. */
|
---|
272 | DECLINLINE(int) iomRamRead(PVMCPU pVCpu, void *pDest, RTGCPTR GCSrc, uint32_t cb)
|
---|
273 | {
|
---|
274 | /* Note: This will fail in R0 or RC if it hits an access handler. That
|
---|
275 | isn't a problem though since the operation can be restarted in REM. */
|
---|
276 | #ifdef IN_RC
|
---|
277 | return MMGCRamReadNoTrapHandler(pDest, (void *)GCSrc, cb);
|
---|
278 | #else
|
---|
279 | return PGMPhysReadGCPtr(pVCpu, pDest, GCSrc, cb);
|
---|
280 | #endif
|
---|
281 | }
|
---|
282 |
|
---|
283 |
|
---|
284 | /** Wrapper for writing virtual memory. */
|
---|
285 | DECLINLINE(int) iomRamWrite(PVMCPU pVCpu, PCPUMCTXCORE pCtxCore, RTGCPTR GCPtrDst, void *pvSrc, uint32_t cb)
|
---|
286 | {
|
---|
287 | /** @todo Need to update PGMVerifyAccess to take access handlers into account for Ring-0 and
|
---|
288 | * raw mode code. Some thought needs to be spent on theoretical concurrency issues as
|
---|
289 | * as well since we're not behind the pgm lock and handler may change between calls.
|
---|
290 | * MMGCRamWriteNoTrapHandler may also trap if the page isn't shadowed, or was kicked
|
---|
291 | * out from both the shadow pt (SMP or our changes) and TLB.
|
---|
292 | *
|
---|
293 | * Currently MMGCRamWriteNoTrapHandler may also fail when it hits a write access handler.
|
---|
294 | * PGMPhysInterpretedWriteNoHandlers/PGMPhysWriteGCPtr OTOH may mess up the state
|
---|
295 | * of some shadowed structure in R0. */
|
---|
296 | #ifdef IN_RC
|
---|
297 | NOREF(pCtxCore);
|
---|
298 | return MMGCRamWriteNoTrapHandler((void *)GCPtrDst, pvSrc, cb);
|
---|
299 | #elif IN_RING0
|
---|
300 | return PGMPhysInterpretedWriteNoHandlers(pVCpu, pCtxCore, GCPtrDst, pvSrc, cb, false /*fRaiseTrap*/);
|
---|
301 | #else
|
---|
302 | NOREF(pCtxCore);
|
---|
303 | return PGMPhysWriteGCPtr(pVCpu, GCPtrDst, pvSrc, cb);
|
---|
304 | #endif
|
---|
305 | }
|
---|
306 |
|
---|
307 |
|
---|
308 | #ifdef IOM_WITH_MOVS_SUPPORT
|
---|
309 | /**
|
---|
310 | * [REP] MOVSB
|
---|
311 | * [REP] MOVSW
|
---|
312 | * [REP] MOVSD
|
---|
313 | *
|
---|
314 | * Restricted implementation.
|
---|
315 | *
|
---|
316 | *
|
---|
317 | * @returns VBox status code.
|
---|
318 | *
|
---|
319 | * @param pVM The virtual machine.
|
---|
320 | * @param uErrorCode CPU Error code.
|
---|
321 | * @param pRegFrame Trap register frame.
|
---|
322 | * @param GCPhysFault The GC physical address corresponding to pvFault.
|
---|
323 | * @param pCpu Disassembler CPU state.
|
---|
324 | * @param pRange Pointer MMIO range.
|
---|
325 | * @param ppStat Which sub-sample to attribute this call to.
|
---|
326 | */
|
---|
327 | static int iomInterpretMOVS(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, PDISCPUSTATE pCpu, PIOMMMIORANGE pRange, PSTAMPROFILE *ppStat)
|
---|
328 | {
|
---|
329 | /*
|
---|
330 | * We do not support segment prefixes or REPNE.
|
---|
331 | */
|
---|
332 | if (pCpu->prefix & (PREFIX_SEG | PREFIX_REPNE))
|
---|
333 | return VINF_IOM_HC_MMIO_READ_WRITE; /** @todo -> interpret whatever. */
|
---|
334 |
|
---|
335 | PVMCPU pVCpu = VMMGetCpu(pVM)
|
---|
336 |
|
---|
337 | /*
|
---|
338 | * Get bytes/words/dwords/qword count to copy.
|
---|
339 | */
|
---|
340 | uint32_t cTransfers = 1;
|
---|
341 | if (pCpu->prefix & PREFIX_REP)
|
---|
342 | {
|
---|
343 | #ifndef IN_RC
|
---|
344 | if ( CPUMIsGuestIn64BitCode(pVCpu, pRegFrame)
|
---|
345 | && pRegFrame->rcx >= _4G)
|
---|
346 | return VINF_EM_RAW_EMULATE_INSTR;
|
---|
347 | #endif
|
---|
348 |
|
---|
349 | cTransfers = pRegFrame->ecx;
|
---|
350 | if (SELMGetCpuModeFromSelector(pVM, pRegFrame->eflags, pRegFrame->cs, &pRegFrame->csHid) == CPUMODE_16BIT)
|
---|
351 | cTransfers &= 0xffff;
|
---|
352 |
|
---|
353 | if (!cTransfers)
|
---|
354 | return VINF_SUCCESS;
|
---|
355 | }
|
---|
356 |
|
---|
357 | /* Get the current privilege level. */
|
---|
358 | uint32_t cpl = CPUMGetGuestCPL(pVCpu, pRegFrame);
|
---|
359 |
|
---|
360 | /*
|
---|
361 | * Get data size.
|
---|
362 | */
|
---|
363 | unsigned cb = DISGetParamSize(pCpu, &pCpu->param1);
|
---|
364 | AssertMsg(cb > 0 && cb <= sizeof(uint64_t), ("cb=%d\n", cb));
|
---|
365 | int offIncrement = pRegFrame->eflags.Bits.u1DF ? -(signed)cb : (signed)cb;
|
---|
366 |
|
---|
367 | #ifdef VBOX_WITH_STATISTICS
|
---|
368 | if (pVM->iom.s.cMovsMaxBytes < (cTransfers << SIZE_2_SHIFT(cb)))
|
---|
369 | pVM->iom.s.cMovsMaxBytes = cTransfers << SIZE_2_SHIFT(cb);
|
---|
370 | #endif
|
---|
371 |
|
---|
372 | /** @todo re-evaluate on page boundraries. */
|
---|
373 |
|
---|
374 | RTGCPHYS Phys = GCPhysFault;
|
---|
375 | int rc;
|
---|
376 | if (uErrorCode & X86_TRAP_PF_RW)
|
---|
377 | {
|
---|
378 | /*
|
---|
379 | * Write operation: [Mem] -> [MMIO]
|
---|
380 | * ds:esi (Virt Src) -> es:edi (Phys Dst)
|
---|
381 | */
|
---|
382 | STAM_STATS({ *ppStat = &pVM->iom.s.StatRZInstMovsToMMIO; });
|
---|
383 |
|
---|
384 | /* Check callback. */
|
---|
385 | if (!pRange->CTX_SUFF(pfnWriteCallback))
|
---|
386 | return VINF_IOM_HC_MMIO_WRITE;
|
---|
387 |
|
---|
388 | /* Convert source address ds:esi. */
|
---|
389 | RTGCUINTPTR pu8Virt;
|
---|
390 | rc = SELMToFlatEx(pVM, DIS_SELREG_DS, pRegFrame, (RTGCPTR)pRegFrame->rsi,
|
---|
391 | SELMTOFLAT_FLAGS_HYPER | SELMTOFLAT_FLAGS_NO_PL,
|
---|
392 | (PRTGCPTR)&pu8Virt);
|
---|
393 | if (RT_SUCCESS(rc))
|
---|
394 | {
|
---|
395 |
|
---|
396 | /* Access verification first; we currently can't recover properly from traps inside this instruction */
|
---|
397 | rc = PGMVerifyAccess(pVCpu, pu8Virt, cTransfers * cb, (cpl == 3) ? X86_PTE_US : 0);
|
---|
398 | if (rc != VINF_SUCCESS)
|
---|
399 | {
|
---|
400 | Log(("MOVS will generate a trap -> recompiler, rc=%d\n", rc));
|
---|
401 | return VINF_EM_RAW_EMULATE_INSTR;
|
---|
402 | }
|
---|
403 |
|
---|
404 | #ifdef IN_RC
|
---|
405 | MMGCRamRegisterTrapHandler(pVM);
|
---|
406 | #endif
|
---|
407 |
|
---|
408 | /* copy loop. */
|
---|
409 | while (cTransfers)
|
---|
410 | {
|
---|
411 | uint32_t u32Data = 0;
|
---|
412 | rc = iomRamRead(pVCpu, &u32Data, (RTGCPTR)pu8Virt, cb);
|
---|
413 | if (rc != VINF_SUCCESS)
|
---|
414 | break;
|
---|
415 | rc = iomMMIODoWrite(pVM, pRange, Phys, &u32Data, cb);
|
---|
416 | if (rc != VINF_SUCCESS)
|
---|
417 | break;
|
---|
418 |
|
---|
419 | pu8Virt += offIncrement;
|
---|
420 | Phys += offIncrement;
|
---|
421 | pRegFrame->rsi += offIncrement;
|
---|
422 | pRegFrame->rdi += offIncrement;
|
---|
423 | cTransfers--;
|
---|
424 | }
|
---|
425 | #ifdef IN_RC
|
---|
426 | MMGCRamDeregisterTrapHandler(pVM);
|
---|
427 | #endif
|
---|
428 | /* Update ecx. */
|
---|
429 | if (pCpu->prefix & PREFIX_REP)
|
---|
430 | pRegFrame->ecx = cTransfers;
|
---|
431 | }
|
---|
432 | else
|
---|
433 | rc = VINF_IOM_HC_MMIO_READ_WRITE;
|
---|
434 | }
|
---|
435 | else
|
---|
436 | {
|
---|
437 | /*
|
---|
438 | * Read operation: [MMIO] -> [mem] or [MMIO] -> [MMIO]
|
---|
439 | * ds:[eSI] (Phys Src) -> es:[eDI] (Virt Dst)
|
---|
440 | */
|
---|
441 | STAM_STATS({ *ppStat = &pVM->iom.s.StatRZInstMovsFromMMIO; });
|
---|
442 |
|
---|
443 | /* Check callback. */
|
---|
444 | if (!pRange->CTX_SUFF(pfnReadCallback))
|
---|
445 | return VINF_IOM_HC_MMIO_READ;
|
---|
446 |
|
---|
447 | /* Convert destination address. */
|
---|
448 | RTGCUINTPTR pu8Virt;
|
---|
449 | rc = SELMToFlatEx(pVM, DIS_SELREG_ES, pRegFrame, (RTGCPTR)pRegFrame->rdi,
|
---|
450 | SELMTOFLAT_FLAGS_HYPER | SELMTOFLAT_FLAGS_NO_PL,
|
---|
451 | (RTGCPTR *)&pu8Virt);
|
---|
452 | if (RT_FAILURE(rc))
|
---|
453 | return VINF_IOM_HC_MMIO_READ;
|
---|
454 |
|
---|
455 | /* Check if destination address is MMIO. */
|
---|
456 | PIOMMMIORANGE pMMIODst;
|
---|
457 | RTGCPHYS PhysDst;
|
---|
458 | rc = PGMGstGetPage((RTGCPTR)pu8Virt, NULL, &PhysDst);
|
---|
459 | PhysDst |= (RTGCUINTPTR)pu8Virt & PAGE_OFFSET_MASK;
|
---|
460 | if ( RT_SUCCESS(rc)
|
---|
461 | && (pMMIODst = iomMMIOGetRange(&pVM->iom.s, PhysDst)))
|
---|
462 | {
|
---|
463 | /*
|
---|
464 | * Extra: [MMIO] -> [MMIO]
|
---|
465 | */
|
---|
466 | STAM_STATS({ *ppStat = &pVM->iom.s.StatRZInstMovsMMIO; });
|
---|
467 | if (!pMMIODst->CTX_SUFF(pfnWriteCallback) && pMMIODst->pfnWriteCallbackR3)
|
---|
468 | return VINF_IOM_HC_MMIO_READ_WRITE;
|
---|
469 |
|
---|
470 | /* copy loop. */
|
---|
471 | while (cTransfers)
|
---|
472 | {
|
---|
473 | uint32_t u32Data;
|
---|
474 | rc = iomMMIODoRead(pVM, pRange, Phys, &u32Data, cb);
|
---|
475 | if (rc != VINF_SUCCESS)
|
---|
476 | break;
|
---|
477 | rc = iomMMIODoWrite(pVM, pMMIODst, PhysDst, &u32Data, cb);
|
---|
478 | if (rc != VINF_SUCCESS)
|
---|
479 | break;
|
---|
480 |
|
---|
481 | Phys += offIncrement;
|
---|
482 | PhysDst += offIncrement;
|
---|
483 | pRegFrame->rsi += offIncrement;
|
---|
484 | pRegFrame->rdi += offIncrement;
|
---|
485 | cTransfers--;
|
---|
486 | }
|
---|
487 | }
|
---|
488 | else
|
---|
489 | {
|
---|
490 | /*
|
---|
491 | * Normal: [MMIO] -> [Mem]
|
---|
492 | */
|
---|
493 | /* Access verification first; we currently can't recover properly from traps inside this instruction */
|
---|
494 | rc = PGMVerifyAccess(pVCpu, pu8Virt, cTransfers * cb, X86_PTE_RW | ((cpl == 3) ? X86_PTE_US : 0));
|
---|
495 | if (rc != VINF_SUCCESS)
|
---|
496 | {
|
---|
497 | Log(("MOVS will generate a trap -> recompiler, rc=%d\n", rc));
|
---|
498 | return VINF_EM_RAW_EMULATE_INSTR;
|
---|
499 | }
|
---|
500 |
|
---|
501 | /* copy loop. */
|
---|
502 | #ifdef IN_RC
|
---|
503 | MMGCRamRegisterTrapHandler(pVM);
|
---|
504 | #endif
|
---|
505 | while (cTransfers)
|
---|
506 | {
|
---|
507 | uint32_t u32Data;
|
---|
508 | rc = iomMMIODoRead(pVM, pRange, Phys, &u32Data, cb);
|
---|
509 | if (rc != VINF_SUCCESS)
|
---|
510 | break;
|
---|
511 | rc = iomRamWrite(pVCpu, pRegFrame, (RTGCPTR)pu8Virt, &u32Data, cb);
|
---|
512 | if (rc != VINF_SUCCESS)
|
---|
513 | {
|
---|
514 | Log(("iomRamWrite %08X size=%d failed with %d\n", pu8Virt, cb, rc));
|
---|
515 | break;
|
---|
516 | }
|
---|
517 |
|
---|
518 | pu8Virt += offIncrement;
|
---|
519 | Phys += offIncrement;
|
---|
520 | pRegFrame->rsi += offIncrement;
|
---|
521 | pRegFrame->rdi += offIncrement;
|
---|
522 | cTransfers--;
|
---|
523 | }
|
---|
524 | #ifdef IN_RC
|
---|
525 | MMGCRamDeregisterTrapHandler(pVM);
|
---|
526 | #endif
|
---|
527 | }
|
---|
528 |
|
---|
529 | /* Update ecx on exit. */
|
---|
530 | if (pCpu->prefix & PREFIX_REP)
|
---|
531 | pRegFrame->ecx = cTransfers;
|
---|
532 | }
|
---|
533 |
|
---|
534 | /* work statistics. */
|
---|
535 | if (rc == VINF_SUCCESS)
|
---|
536 | iomMMIOStatLength(pVM, cb);
|
---|
537 | NOREF(ppStat);
|
---|
538 | return rc;
|
---|
539 | }
|
---|
540 | #endif /* IOM_WITH_MOVS_SUPPORT */
|
---|
541 |
|
---|
542 |
|
---|
543 | /**
|
---|
544 | * [REP] STOSB
|
---|
545 | * [REP] STOSW
|
---|
546 | * [REP] STOSD
|
---|
547 | *
|
---|
548 | * Restricted implementation.
|
---|
549 | *
|
---|
550 | *
|
---|
551 | * @returns VBox status code.
|
---|
552 | *
|
---|
553 | * @param pVM The virtual machine.
|
---|
554 | * @param pRegFrame Trap register frame.
|
---|
555 | * @param GCPhysFault The GC physical address corresponding to pvFault.
|
---|
556 | * @param pCpu Disassembler CPU state.
|
---|
557 | * @param pRange Pointer MMIO range.
|
---|
558 | */
|
---|
559 | static int iomInterpretSTOS(PVM pVM, PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, PDISCPUSTATE pCpu, PIOMMMIORANGE pRange)
|
---|
560 | {
|
---|
561 | /*
|
---|
562 | * We do not support segment prefixes or REPNE..
|
---|
563 | */
|
---|
564 | if (pCpu->prefix & (PREFIX_SEG | PREFIX_REPNE))
|
---|
565 | return VINF_IOM_HC_MMIO_READ_WRITE; /** @todo -> REM instead of HC */
|
---|
566 |
|
---|
567 | /*
|
---|
568 | * Get bytes/words/dwords count to copy.
|
---|
569 | */
|
---|
570 | uint32_t cTransfers = 1;
|
---|
571 | if (pCpu->prefix & PREFIX_REP)
|
---|
572 | {
|
---|
573 | #ifndef IN_RC
|
---|
574 | if ( CPUMIsGuestIn64BitCode(VMMGetCpu(pVM), pRegFrame)
|
---|
575 | && pRegFrame->rcx >= _4G)
|
---|
576 | return VINF_EM_RAW_EMULATE_INSTR;
|
---|
577 | #endif
|
---|
578 |
|
---|
579 | cTransfers = pRegFrame->ecx;
|
---|
580 | if (SELMGetCpuModeFromSelector(pVM, pRegFrame->eflags, pRegFrame->cs, &pRegFrame->csHid) == CPUMODE_16BIT)
|
---|
581 | cTransfers &= 0xffff;
|
---|
582 |
|
---|
583 | if (!cTransfers)
|
---|
584 | return VINF_SUCCESS;
|
---|
585 | }
|
---|
586 |
|
---|
587 | /** @todo r=bird: bounds checks! */
|
---|
588 |
|
---|
589 | /*
|
---|
590 | * Get data size.
|
---|
591 | */
|
---|
592 | unsigned cb = DISGetParamSize(pCpu, &pCpu->param1);
|
---|
593 | AssertMsg(cb > 0 && cb <= sizeof(uint64_t), ("cb=%d\n", cb));
|
---|
594 | int offIncrement = pRegFrame->eflags.Bits.u1DF ? -(signed)cb : (signed)cb;
|
---|
595 |
|
---|
596 | #ifdef VBOX_WITH_STATISTICS
|
---|
597 | if (pVM->iom.s.cStosMaxBytes < (cTransfers << SIZE_2_SHIFT(cb)))
|
---|
598 | pVM->iom.s.cStosMaxBytes = cTransfers << SIZE_2_SHIFT(cb);
|
---|
599 | #endif
|
---|
600 |
|
---|
601 |
|
---|
602 | RTGCPHYS Phys = GCPhysFault;
|
---|
603 | uint32_t u32Data = pRegFrame->eax;
|
---|
604 | int rc;
|
---|
605 | if (pRange->CTX_SUFF(pfnFillCallback))
|
---|
606 | {
|
---|
607 | /*
|
---|
608 | * Use the fill callback.
|
---|
609 | */
|
---|
610 | /** @todo pfnFillCallback must return number of bytes successfully written!!! */
|
---|
611 | if (offIncrement > 0)
|
---|
612 | {
|
---|
613 | /* addr++ variant. */
|
---|
614 | rc = pRange->CTX_SUFF(pfnFillCallback)(pRange->CTX_SUFF(pDevIns), pRange->CTX_SUFF(pvUser), Phys, u32Data, cb, cTransfers);
|
---|
615 | if (rc == VINF_SUCCESS)
|
---|
616 | {
|
---|
617 | /* Update registers. */
|
---|
618 | pRegFrame->rdi += cTransfers << SIZE_2_SHIFT(cb);
|
---|
619 | if (pCpu->prefix & PREFIX_REP)
|
---|
620 | pRegFrame->ecx = 0;
|
---|
621 | }
|
---|
622 | }
|
---|
623 | else
|
---|
624 | {
|
---|
625 | /* addr-- variant. */
|
---|
626 | rc = pRange->CTX_SUFF(pfnFillCallback)(pRange->CTX_SUFF(pDevIns), pRange->CTX_SUFF(pvUser), (Phys - (cTransfers - 1)) << SIZE_2_SHIFT(cb), u32Data, cb, cTransfers);
|
---|
627 | if (rc == VINF_SUCCESS)
|
---|
628 | {
|
---|
629 | /* Update registers. */
|
---|
630 | pRegFrame->rdi -= cTransfers << SIZE_2_SHIFT(cb);
|
---|
631 | if (pCpu->prefix & PREFIX_REP)
|
---|
632 | pRegFrame->ecx = 0;
|
---|
633 | }
|
---|
634 | }
|
---|
635 | }
|
---|
636 | else
|
---|
637 | {
|
---|
638 | /*
|
---|
639 | * Use the write callback.
|
---|
640 | */
|
---|
641 | Assert(pRange->CTX_SUFF(pfnWriteCallback) || !pRange->pfnWriteCallbackR3);
|
---|
642 |
|
---|
643 | /* fill loop. */
|
---|
644 | do
|
---|
645 | {
|
---|
646 | rc = iomMMIODoWrite(pVM, pRange, Phys, &u32Data, cb);
|
---|
647 | if (rc != VINF_SUCCESS)
|
---|
648 | break;
|
---|
649 |
|
---|
650 | Phys += offIncrement;
|
---|
651 | pRegFrame->rdi += offIncrement;
|
---|
652 | cTransfers--;
|
---|
653 | } while (cTransfers);
|
---|
654 |
|
---|
655 | /* Update ecx on exit. */
|
---|
656 | if (pCpu->prefix & PREFIX_REP)
|
---|
657 | pRegFrame->ecx = cTransfers;
|
---|
658 | }
|
---|
659 |
|
---|
660 | /*
|
---|
661 | * Work statistics and return.
|
---|
662 | */
|
---|
663 | if (rc == VINF_SUCCESS)
|
---|
664 | iomMMIOStatLength(pVM, cb);
|
---|
665 | return rc;
|
---|
666 | }
|
---|
667 |
|
---|
668 |
|
---|
669 | /**
|
---|
670 | * [REP] LODSB
|
---|
671 | * [REP] LODSW
|
---|
672 | * [REP] LODSD
|
---|
673 | *
|
---|
674 | * Restricted implementation.
|
---|
675 | *
|
---|
676 | *
|
---|
677 | * @returns VBox status code.
|
---|
678 | *
|
---|
679 | * @param pVM The virtual machine.
|
---|
680 | * @param pRegFrame Trap register frame.
|
---|
681 | * @param GCPhysFault The GC physical address corresponding to pvFault.
|
---|
682 | * @param pCpu Disassembler CPU state.
|
---|
683 | * @param pRange Pointer MMIO range.
|
---|
684 | */
|
---|
685 | static int iomInterpretLODS(PVM pVM, PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, PDISCPUSTATE pCpu, PIOMMMIORANGE pRange)
|
---|
686 | {
|
---|
687 | Assert(pRange->CTX_SUFF(pfnReadCallback) || !pRange->pfnReadCallbackR3);
|
---|
688 |
|
---|
689 | /*
|
---|
690 | * We do not support segment prefixes or REP*.
|
---|
691 | */
|
---|
692 | if (pCpu->prefix & (PREFIX_SEG | PREFIX_REP | PREFIX_REPNE))
|
---|
693 | return VINF_IOM_HC_MMIO_READ_WRITE; /** @todo -> REM instead of HC */
|
---|
694 |
|
---|
695 | /*
|
---|
696 | * Get data size.
|
---|
697 | */
|
---|
698 | unsigned cb = DISGetParamSize(pCpu, &pCpu->param2);
|
---|
699 | AssertMsg(cb > 0 && cb <= sizeof(uint64_t), ("cb=%d\n", cb));
|
---|
700 | int offIncrement = pRegFrame->eflags.Bits.u1DF ? -(signed)cb : (signed)cb;
|
---|
701 |
|
---|
702 | /*
|
---|
703 | * Perform read.
|
---|
704 | */
|
---|
705 | int rc = iomMMIODoRead(pVM, pRange, GCPhysFault, &pRegFrame->rax, cb);
|
---|
706 | if (rc == VINF_SUCCESS)
|
---|
707 | pRegFrame->rsi += offIncrement;
|
---|
708 |
|
---|
709 | /*
|
---|
710 | * Work statistics and return.
|
---|
711 | */
|
---|
712 | if (rc == VINF_SUCCESS)
|
---|
713 | iomMMIOStatLength(pVM, cb);
|
---|
714 | return rc;
|
---|
715 | }
|
---|
716 |
|
---|
717 |
|
---|
718 | /**
|
---|
719 | * CMP [MMIO], reg|imm
|
---|
720 | * CMP reg|imm, [MMIO]
|
---|
721 | *
|
---|
722 | * Restricted implementation.
|
---|
723 | *
|
---|
724 | *
|
---|
725 | * @returns VBox status code.
|
---|
726 | *
|
---|
727 | * @param pVM The virtual machine.
|
---|
728 | * @param pRegFrame Trap register frame.
|
---|
729 | * @param GCPhysFault The GC physical address corresponding to pvFault.
|
---|
730 | * @param pCpu Disassembler CPU state.
|
---|
731 | * @param pRange Pointer MMIO range.
|
---|
732 | */
|
---|
733 | static int iomInterpretCMP(PVM pVM, PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, PDISCPUSTATE pCpu, PIOMMMIORANGE pRange)
|
---|
734 | {
|
---|
735 | Assert(pRange->CTX_SUFF(pfnReadCallback) || !pRange->pfnReadCallbackR3);
|
---|
736 |
|
---|
737 | /*
|
---|
738 | * Get the operands.
|
---|
739 | */
|
---|
740 | unsigned cb = 0;
|
---|
741 | uint64_t uData1 = 0;
|
---|
742 | uint64_t uData2 = 0;
|
---|
743 | int rc;
|
---|
744 | if (iomGetRegImmData(pCpu, &pCpu->param1, pRegFrame, &uData1, &cb))
|
---|
745 | /* cmp reg, [MMIO]. */
|
---|
746 | rc = iomMMIODoRead(pVM, pRange, GCPhysFault, &uData2, cb);
|
---|
747 | else if (iomGetRegImmData(pCpu, &pCpu->param2, pRegFrame, &uData2, &cb))
|
---|
748 | /* cmp [MMIO], reg|imm. */
|
---|
749 | rc = iomMMIODoRead(pVM, pRange, GCPhysFault, &uData1, cb);
|
---|
750 | else
|
---|
751 | {
|
---|
752 | AssertMsgFailed(("Disassember CMP problem..\n"));
|
---|
753 | rc = VERR_IOM_MMIO_HANDLER_DISASM_ERROR;
|
---|
754 | }
|
---|
755 |
|
---|
756 | if (rc == VINF_SUCCESS)
|
---|
757 | {
|
---|
758 | /* Emulate CMP and update guest flags. */
|
---|
759 | uint32_t eflags = EMEmulateCmp(uData1, uData2, cb);
|
---|
760 | pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF))
|
---|
761 | | (eflags & (X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF));
|
---|
762 | iomMMIOStatLength(pVM, cb);
|
---|
763 | }
|
---|
764 |
|
---|
765 | return rc;
|
---|
766 | }
|
---|
767 |
|
---|
768 |
|
---|
769 | /**
|
---|
770 | * AND [MMIO], reg|imm
|
---|
771 | * AND reg, [MMIO]
|
---|
772 | * OR [MMIO], reg|imm
|
---|
773 | * OR reg, [MMIO]
|
---|
774 | *
|
---|
775 | * Restricted implementation.
|
---|
776 | *
|
---|
777 | *
|
---|
778 | * @returns VBox status code.
|
---|
779 | *
|
---|
780 | * @param pVM The virtual machine.
|
---|
781 | * @param pRegFrame Trap register frame.
|
---|
782 | * @param GCPhysFault The GC physical address corresponding to pvFault.
|
---|
783 | * @param pCpu Disassembler CPU state.
|
---|
784 | * @param pRange Pointer MMIO range.
|
---|
785 | * @param pfnEmulate Instruction emulation function.
|
---|
786 | */
|
---|
787 | static int iomInterpretOrXorAnd(PVM pVM, PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, PDISCPUSTATE pCpu, PIOMMMIORANGE pRange, PFNEMULATEPARAM3 pfnEmulate)
|
---|
788 | {
|
---|
789 | unsigned cb = 0;
|
---|
790 | uint64_t uData1 = 0;
|
---|
791 | uint64_t uData2 = 0;
|
---|
792 | bool fAndWrite;
|
---|
793 | int rc;
|
---|
794 |
|
---|
795 | #ifdef LOG_ENABLED
|
---|
796 | const char *pszInstr;
|
---|
797 |
|
---|
798 | if (pCpu->pCurInstr->opcode == OP_XOR)
|
---|
799 | pszInstr = "Xor";
|
---|
800 | else if (pCpu->pCurInstr->opcode == OP_OR)
|
---|
801 | pszInstr = "Or";
|
---|
802 | else if (pCpu->pCurInstr->opcode == OP_AND)
|
---|
803 | pszInstr = "And";
|
---|
804 | else
|
---|
805 | pszInstr = "OrXorAnd??";
|
---|
806 | #endif
|
---|
807 |
|
---|
808 | if (iomGetRegImmData(pCpu, &pCpu->param1, pRegFrame, &uData1, &cb))
|
---|
809 | {
|
---|
810 | /* and reg, [MMIO]. */
|
---|
811 | Assert(pRange->CTX_SUFF(pfnReadCallback) || !pRange->pfnReadCallbackR3);
|
---|
812 | fAndWrite = false;
|
---|
813 | rc = iomMMIODoRead(pVM, pRange, GCPhysFault, &uData2, cb);
|
---|
814 | }
|
---|
815 | else if (iomGetRegImmData(pCpu, &pCpu->param2, pRegFrame, &uData2, &cb))
|
---|
816 | {
|
---|
817 | /* and [MMIO], reg|imm. */
|
---|
818 | fAndWrite = true;
|
---|
819 | if ( (pRange->CTX_SUFF(pfnReadCallback) || !pRange->pfnReadCallbackR3)
|
---|
820 | && (pRange->CTX_SUFF(pfnWriteCallback) || !pRange->pfnWriteCallbackR3))
|
---|
821 | rc = iomMMIODoRead(pVM, pRange, GCPhysFault, &uData1, cb);
|
---|
822 | else
|
---|
823 | rc = VINF_IOM_HC_MMIO_READ_WRITE;
|
---|
824 | }
|
---|
825 | else
|
---|
826 | {
|
---|
827 | AssertMsgFailed(("Disassember AND problem..\n"));
|
---|
828 | return VERR_IOM_MMIO_HANDLER_DISASM_ERROR;
|
---|
829 | }
|
---|
830 |
|
---|
831 | if (rc == VINF_SUCCESS)
|
---|
832 | {
|
---|
833 | /* Emulate AND and update guest flags. */
|
---|
834 | uint32_t eflags = pfnEmulate((uint32_t *)&uData1, uData2, cb);
|
---|
835 |
|
---|
836 | LogFlow(("iomInterpretOrXorAnd %s result %RX64\n", pszInstr, uData1));
|
---|
837 |
|
---|
838 | if (fAndWrite)
|
---|
839 | /* Store result to MMIO. */
|
---|
840 | rc = iomMMIODoWrite(pVM, pRange, GCPhysFault, &uData1, cb);
|
---|
841 | else
|
---|
842 | {
|
---|
843 | /* Store result to register. */
|
---|
844 | bool fRc = iomSaveDataToReg(pCpu, &pCpu->param1, pRegFrame, uData1);
|
---|
845 | AssertMsg(fRc, ("Failed to store register value!\n")); NOREF(fRc);
|
---|
846 | }
|
---|
847 | if (rc == VINF_SUCCESS)
|
---|
848 | {
|
---|
849 | /* Update guest's eflags and finish. */
|
---|
850 | pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF))
|
---|
851 | | (eflags & (X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF));
|
---|
852 | iomMMIOStatLength(pVM, cb);
|
---|
853 | }
|
---|
854 | }
|
---|
855 |
|
---|
856 | return rc;
|
---|
857 | }
|
---|
858 |
|
---|
859 |
|
---|
860 | /**
|
---|
861 | * TEST [MMIO], reg|imm
|
---|
862 | * TEST reg, [MMIO]
|
---|
863 | *
|
---|
864 | * Restricted implementation.
|
---|
865 | *
|
---|
866 | *
|
---|
867 | * @returns VBox status code.
|
---|
868 | *
|
---|
869 | * @param pVM The virtual machine.
|
---|
870 | * @param pRegFrame Trap register frame.
|
---|
871 | * @param GCPhysFault The GC physical address corresponding to pvFault.
|
---|
872 | * @param pCpu Disassembler CPU state.
|
---|
873 | * @param pRange Pointer MMIO range.
|
---|
874 | */
|
---|
875 | static int iomInterpretTEST(PVM pVM, PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, PDISCPUSTATE pCpu, PIOMMMIORANGE pRange)
|
---|
876 | {
|
---|
877 | Assert(pRange->CTX_SUFF(pfnReadCallback) || !pRange->pfnReadCallbackR3);
|
---|
878 |
|
---|
879 | unsigned cb = 0;
|
---|
880 | uint64_t uData1 = 0;
|
---|
881 | uint64_t uData2 = 0;
|
---|
882 | int rc;
|
---|
883 |
|
---|
884 | if (iomGetRegImmData(pCpu, &pCpu->param1, pRegFrame, &uData1, &cb))
|
---|
885 | {
|
---|
886 | /* and test, [MMIO]. */
|
---|
887 | rc = iomMMIODoRead(pVM, pRange, GCPhysFault, &uData2, cb);
|
---|
888 | }
|
---|
889 | else if (iomGetRegImmData(pCpu, &pCpu->param2, pRegFrame, &uData2, &cb))
|
---|
890 | {
|
---|
891 | /* test [MMIO], reg|imm. */
|
---|
892 | rc = iomMMIODoRead(pVM, pRange, GCPhysFault, &uData1, cb);
|
---|
893 | }
|
---|
894 | else
|
---|
895 | {
|
---|
896 | AssertMsgFailed(("Disassember TEST problem..\n"));
|
---|
897 | return VERR_IOM_MMIO_HANDLER_DISASM_ERROR;
|
---|
898 | }
|
---|
899 |
|
---|
900 | if (rc == VINF_SUCCESS)
|
---|
901 | {
|
---|
902 | /* Emulate TEST (=AND without write back) and update guest EFLAGS. */
|
---|
903 | uint32_t eflags = EMEmulateAnd((uint32_t *)&uData1, uData2, cb);
|
---|
904 | pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF))
|
---|
905 | | (eflags & (X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF));
|
---|
906 | iomMMIOStatLength(pVM, cb);
|
---|
907 | }
|
---|
908 |
|
---|
909 | return rc;
|
---|
910 | }
|
---|
911 |
|
---|
912 |
|
---|
913 | /**
|
---|
914 | * BT [MMIO], reg|imm
|
---|
915 | *
|
---|
916 | * Restricted implementation.
|
---|
917 | *
|
---|
918 | *
|
---|
919 | * @returns VBox status code.
|
---|
920 | *
|
---|
921 | * @param pVM The virtual machine.
|
---|
922 | * @param pRegFrame Trap register frame.
|
---|
923 | * @param GCPhysFault The GC physical address corresponding to pvFault.
|
---|
924 | * @param pCpu Disassembler CPU state.
|
---|
925 | * @param pRange Pointer MMIO range.
|
---|
926 | */
|
---|
927 | static int iomInterpretBT(PVM pVM, PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, PDISCPUSTATE pCpu, PIOMMMIORANGE pRange)
|
---|
928 | {
|
---|
929 | Assert(pRange->CTX_SUFF(pfnReadCallback) || !pRange->pfnReadCallbackR3);
|
---|
930 |
|
---|
931 | uint64_t uBit = 0;
|
---|
932 | uint64_t uData1 = 0;
|
---|
933 | unsigned cb = 0;
|
---|
934 | int rc;
|
---|
935 |
|
---|
936 | if (iomGetRegImmData(pCpu, &pCpu->param2, pRegFrame, &uBit, &cb))
|
---|
937 | {
|
---|
938 | /* bt [MMIO], reg|imm. */
|
---|
939 | rc = iomMMIODoRead(pVM, pRange, GCPhysFault, &uData1, cb);
|
---|
940 | }
|
---|
941 | else
|
---|
942 | {
|
---|
943 | AssertMsgFailed(("Disassember BT problem..\n"));
|
---|
944 | return VERR_IOM_MMIO_HANDLER_DISASM_ERROR;
|
---|
945 | }
|
---|
946 |
|
---|
947 | if (rc == VINF_SUCCESS)
|
---|
948 | {
|
---|
949 | /* The size of the memory operand only matters here. */
|
---|
950 | cb = DISGetParamSize(pCpu, &pCpu->param1);
|
---|
951 |
|
---|
952 | /* Find the bit inside the faulting address */
|
---|
953 | uBit &= (cb*8 - 1);
|
---|
954 |
|
---|
955 | pRegFrame->eflags.Bits.u1CF = (uData1 >> uBit);
|
---|
956 | iomMMIOStatLength(pVM, cb);
|
---|
957 | }
|
---|
958 |
|
---|
959 | return rc;
|
---|
960 | }
|
---|
961 |
|
---|
962 | /**
|
---|
963 | * XCHG [MMIO], reg
|
---|
964 | * XCHG reg, [MMIO]
|
---|
965 | *
|
---|
966 | * Restricted implementation.
|
---|
967 | *
|
---|
968 | *
|
---|
969 | * @returns VBox status code.
|
---|
970 | *
|
---|
971 | * @param pVM The virtual machine.
|
---|
972 | * @param pRegFrame Trap register frame.
|
---|
973 | * @param GCPhysFault The GC physical address corresponding to pvFault.
|
---|
974 | * @param pCpu Disassembler CPU state.
|
---|
975 | * @param pRange Pointer MMIO range.
|
---|
976 | */
|
---|
977 | static int iomInterpretXCHG(PVM pVM, PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, PDISCPUSTATE pCpu, PIOMMMIORANGE pRange)
|
---|
978 | {
|
---|
979 | /* Check for read & write handlers since IOMMMIOHandler doesn't cover this. */
|
---|
980 | if ( (!pRange->CTX_SUFF(pfnReadCallback) && pRange->pfnReadCallbackR3)
|
---|
981 | || (!pRange->CTX_SUFF(pfnWriteCallback) && pRange->pfnWriteCallbackR3))
|
---|
982 | return VINF_IOM_HC_MMIO_READ_WRITE;
|
---|
983 |
|
---|
984 | int rc;
|
---|
985 | unsigned cb = 0;
|
---|
986 | uint64_t uData1 = 0;
|
---|
987 | uint64_t uData2 = 0;
|
---|
988 | if (iomGetRegImmData(pCpu, &pCpu->param1, pRegFrame, &uData1, &cb))
|
---|
989 | {
|
---|
990 | /* xchg reg, [MMIO]. */
|
---|
991 | rc = iomMMIODoRead(pVM, pRange, GCPhysFault, &uData2, cb);
|
---|
992 | if (rc == VINF_SUCCESS)
|
---|
993 | {
|
---|
994 | /* Store result to MMIO. */
|
---|
995 | rc = iomMMIODoWrite(pVM, pRange, GCPhysFault, &uData1, cb);
|
---|
996 |
|
---|
997 | if (rc == VINF_SUCCESS)
|
---|
998 | {
|
---|
999 | /* Store result to register. */
|
---|
1000 | bool fRc = iomSaveDataToReg(pCpu, &pCpu->param1, pRegFrame, uData2);
|
---|
1001 | AssertMsg(fRc, ("Failed to store register value!\n")); NOREF(fRc);
|
---|
1002 | }
|
---|
1003 | else
|
---|
1004 | Assert(rc == VINF_IOM_HC_MMIO_WRITE || rc == VINF_PATM_HC_MMIO_PATCH_WRITE);
|
---|
1005 | }
|
---|
1006 | else
|
---|
1007 | Assert(rc == VINF_IOM_HC_MMIO_READ || rc == VINF_PATM_HC_MMIO_PATCH_READ);
|
---|
1008 | }
|
---|
1009 | else if (iomGetRegImmData(pCpu, &pCpu->param2, pRegFrame, &uData2, &cb))
|
---|
1010 | {
|
---|
1011 | /* xchg [MMIO], reg. */
|
---|
1012 | rc = iomMMIODoRead(pVM, pRange, GCPhysFault, &uData1, cb);
|
---|
1013 | if (rc == VINF_SUCCESS)
|
---|
1014 | {
|
---|
1015 | /* Store result to MMIO. */
|
---|
1016 | rc = iomMMIODoWrite(pVM, pRange, GCPhysFault, &uData2, cb);
|
---|
1017 | if (rc == VINF_SUCCESS)
|
---|
1018 | {
|
---|
1019 | /* Store result to register. */
|
---|
1020 | bool fRc = iomSaveDataToReg(pCpu, &pCpu->param2, pRegFrame, uData1);
|
---|
1021 | AssertMsg(fRc, ("Failed to store register value!\n")); NOREF(fRc);
|
---|
1022 | }
|
---|
1023 | else
|
---|
1024 | Assert(rc == VINF_IOM_HC_MMIO_WRITE || rc == VINF_PATM_HC_MMIO_PATCH_WRITE);
|
---|
1025 | }
|
---|
1026 | else
|
---|
1027 | Assert(rc == VINF_IOM_HC_MMIO_READ || rc == VINF_PATM_HC_MMIO_PATCH_READ);
|
---|
1028 | }
|
---|
1029 | else
|
---|
1030 | {
|
---|
1031 | AssertMsgFailed(("Disassember XCHG problem..\n"));
|
---|
1032 | rc = VERR_IOM_MMIO_HANDLER_DISASM_ERROR;
|
---|
1033 | }
|
---|
1034 | return rc;
|
---|
1035 | }
|
---|
1036 |
|
---|
1037 |
|
---|
1038 | /**
|
---|
1039 | * \#PF Handler callback for MMIO ranges.
|
---|
1040 | *
|
---|
1041 | * @returns VBox status code (appropriate for GC return).
|
---|
1042 | * @param pVM VM Handle.
|
---|
1043 | * @param uErrorCode CPU Error code.
|
---|
1044 | * @param pCtxCore Trap register frame.
|
---|
1045 | * @param pvFault The fault address (cr2).
|
---|
1046 | * @param GCPhysFault The GC physical address corresponding to pvFault.
|
---|
1047 | * @param pvUser Pointer to the MMIO ring-3 range entry.
|
---|
1048 | */
|
---|
1049 | VMMDECL(int) IOMMMIOHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pCtxCore, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser)
|
---|
1050 | {
|
---|
1051 | /* Take the IOM lock before performing any MMIO. */
|
---|
1052 | int rc = iomLock(pVM);
|
---|
1053 | #ifndef IN_RING3
|
---|
1054 | if (rc == VERR_SEM_BUSY)
|
---|
1055 | return (uErrorCode & X86_TRAP_PF_RW) ? VINF_IOM_HC_MMIO_WRITE : VINF_IOM_HC_MMIO_READ;
|
---|
1056 | #endif
|
---|
1057 | AssertRC(rc);
|
---|
1058 |
|
---|
1059 | STAM_PROFILE_START(&pVM->iom.s.StatRZMMIOHandler, a);
|
---|
1060 | Log(("IOMMMIOHandler: GCPhys=%RGp uErr=%#x pvFault=%RGv rip=%RGv\n",
|
---|
1061 | GCPhysFault, (uint32_t)uErrorCode, pvFault, (RTGCPTR)pCtxCore->rip));
|
---|
1062 |
|
---|
1063 | PIOMMMIORANGE pRange = (PIOMMMIORANGE)pvUser;
|
---|
1064 | Assert(pRange);
|
---|
1065 | Assert(pRange == iomMMIOGetRange(&pVM->iom.s, GCPhysFault));
|
---|
1066 |
|
---|
1067 | #ifdef VBOX_WITH_STATISTICS
|
---|
1068 | /*
|
---|
1069 | * Locate the statistics, if > PAGE_SIZE we'll use the first byte for everything.
|
---|
1070 | */
|
---|
1071 | PIOMMMIOSTATS pStats = iomMMIOGetStats(&pVM->iom.s, GCPhysFault, pRange);
|
---|
1072 | if (!pStats)
|
---|
1073 | {
|
---|
1074 | # ifdef IN_RING3
|
---|
1075 | iomUnlock(pVM);
|
---|
1076 | return VERR_NO_MEMORY;
|
---|
1077 | # else
|
---|
1078 | STAM_PROFILE_STOP(&pVM->iom.s.StatRZMMIOHandler, a);
|
---|
1079 | STAM_COUNTER_INC(&pVM->iom.s.StatRZMMIOFailures);
|
---|
1080 | iomUnlock(pVM);
|
---|
1081 | return (uErrorCode & X86_TRAP_PF_RW) ? VINF_IOM_HC_MMIO_WRITE : VINF_IOM_HC_MMIO_READ;
|
---|
1082 | # endif
|
---|
1083 | }
|
---|
1084 | #endif
|
---|
1085 |
|
---|
1086 | #ifndef IN_RING3
|
---|
1087 | /*
|
---|
1088 | * Should we defer the request right away?
|
---|
1089 | */
|
---|
1090 | if (uErrorCode & X86_TRAP_PF_RW
|
---|
1091 | ? !pRange->CTX_SUFF(pfnWriteCallback) && pRange->pfnWriteCallbackR3
|
---|
1092 | : !pRange->CTX_SUFF(pfnReadCallback) && pRange->pfnReadCallbackR3)
|
---|
1093 | {
|
---|
1094 | # ifdef VBOX_WITH_STATISTICS
|
---|
1095 | if (uErrorCode & X86_TRAP_PF_RW)
|
---|
1096 | STAM_COUNTER_INC(&pStats->CTX_MID_Z(Write,ToR3));
|
---|
1097 | else
|
---|
1098 | STAM_COUNTER_INC(&pStats->CTX_MID_Z(Read,ToR3));
|
---|
1099 | # endif
|
---|
1100 |
|
---|
1101 | STAM_PROFILE_STOP(&pVM->iom.s.StatRZMMIOHandler, a);
|
---|
1102 | STAM_COUNTER_INC(&pVM->iom.s.StatRZMMIOFailures);
|
---|
1103 | iomUnlock(pVM);
|
---|
1104 | return (uErrorCode & X86_TRAP_PF_RW ? VINF_IOM_HC_MMIO_WRITE : VINF_IOM_HC_MMIO_READ);
|
---|
1105 | }
|
---|
1106 | #endif /* !IN_RING3 */
|
---|
1107 |
|
---|
1108 | /*
|
---|
1109 | * Disassemble the instruction and interpret it.
|
---|
1110 | */
|
---|
1111 | DISCPUSTATE Cpu;
|
---|
1112 | unsigned cbOp;
|
---|
1113 | rc = EMInterpretDisasOne(pVM, VMMGetCpu(pVM), pCtxCore, &Cpu, &cbOp);
|
---|
1114 | AssertRC(rc);
|
---|
1115 | if (RT_FAILURE(rc))
|
---|
1116 | {
|
---|
1117 | iomUnlock(pVM);
|
---|
1118 | return rc;
|
---|
1119 | }
|
---|
1120 | switch (Cpu.pCurInstr->opcode)
|
---|
1121 | {
|
---|
1122 | case OP_MOV:
|
---|
1123 | case OP_MOVZX:
|
---|
1124 | case OP_MOVSX:
|
---|
1125 | {
|
---|
1126 | STAM_PROFILE_START(&pVM->iom.s.StatRZInstMov, b);
|
---|
1127 | if (uErrorCode & X86_TRAP_PF_RW)
|
---|
1128 | rc = iomInterpretMOVxXWrite(pVM, pCtxCore, &Cpu, pRange, GCPhysFault);
|
---|
1129 | else
|
---|
1130 | rc = iomInterpretMOVxXRead(pVM, pCtxCore, &Cpu, pRange, GCPhysFault);
|
---|
1131 | STAM_PROFILE_STOP(&pVM->iom.s.StatRZInstMov, b);
|
---|
1132 | break;
|
---|
1133 | }
|
---|
1134 |
|
---|
1135 |
|
---|
1136 | #ifdef IOM_WITH_MOVS_SUPPORT
|
---|
1137 | case OP_MOVSB:
|
---|
1138 | case OP_MOVSWD:
|
---|
1139 | {
|
---|
1140 | STAM_PROFILE_ADV_START(&pVM->iom.s.StatRZInstMovs, c);
|
---|
1141 | PSTAMPROFILE pStat = NULL;
|
---|
1142 | rc = iomInterpretMOVS(pVM, uErrorCode, pCtxCore, GCPhysFault, &Cpu, pRange, &pStat);
|
---|
1143 | STAM_PROFILE_ADV_STOP_EX(&pVM->iom.s.StatRZInstMovs, pStat, c);
|
---|
1144 | break;
|
---|
1145 | }
|
---|
1146 | #endif
|
---|
1147 |
|
---|
1148 | case OP_STOSB:
|
---|
1149 | case OP_STOSWD:
|
---|
1150 | Assert(uErrorCode & X86_TRAP_PF_RW);
|
---|
1151 | STAM_PROFILE_START(&pVM->iom.s.StatRZInstStos, d);
|
---|
1152 | rc = iomInterpretSTOS(pVM, pCtxCore, GCPhysFault, &Cpu, pRange);
|
---|
1153 | STAM_PROFILE_STOP(&pVM->iom.s.StatRZInstStos, d);
|
---|
1154 | break;
|
---|
1155 |
|
---|
1156 | case OP_LODSB:
|
---|
1157 | case OP_LODSWD:
|
---|
1158 | Assert(!(uErrorCode & X86_TRAP_PF_RW));
|
---|
1159 | STAM_PROFILE_START(&pVM->iom.s.StatRZInstLods, e);
|
---|
1160 | rc = iomInterpretLODS(pVM, pCtxCore, GCPhysFault, &Cpu, pRange);
|
---|
1161 | STAM_PROFILE_STOP(&pVM->iom.s.StatRZInstLods, e);
|
---|
1162 | break;
|
---|
1163 |
|
---|
1164 | case OP_CMP:
|
---|
1165 | Assert(!(uErrorCode & X86_TRAP_PF_RW));
|
---|
1166 | STAM_PROFILE_START(&pVM->iom.s.StatRZInstCmp, f);
|
---|
1167 | rc = iomInterpretCMP(pVM, pCtxCore, GCPhysFault, &Cpu, pRange);
|
---|
1168 | STAM_PROFILE_STOP(&pVM->iom.s.StatRZInstCmp, f);
|
---|
1169 | break;
|
---|
1170 |
|
---|
1171 | case OP_AND:
|
---|
1172 | STAM_PROFILE_START(&pVM->iom.s.StatRZInstAnd, g);
|
---|
1173 | rc = iomInterpretOrXorAnd(pVM, pCtxCore, GCPhysFault, &Cpu, pRange, EMEmulateAnd);
|
---|
1174 | STAM_PROFILE_STOP(&pVM->iom.s.StatRZInstAnd, g);
|
---|
1175 | break;
|
---|
1176 |
|
---|
1177 | case OP_OR:
|
---|
1178 | STAM_PROFILE_START(&pVM->iom.s.StatRZInstOr, k);
|
---|
1179 | rc = iomInterpretOrXorAnd(pVM, pCtxCore, GCPhysFault, &Cpu, pRange, EMEmulateOr);
|
---|
1180 | STAM_PROFILE_STOP(&pVM->iom.s.StatRZInstOr, k);
|
---|
1181 | break;
|
---|
1182 |
|
---|
1183 | case OP_XOR:
|
---|
1184 | STAM_PROFILE_START(&pVM->iom.s.StatRZInstXor, m);
|
---|
1185 | rc = iomInterpretOrXorAnd(pVM, pCtxCore, GCPhysFault, &Cpu, pRange, EMEmulateXor);
|
---|
1186 | STAM_PROFILE_STOP(&pVM->iom.s.StatRZInstXor, m);
|
---|
1187 | break;
|
---|
1188 |
|
---|
1189 | case OP_TEST:
|
---|
1190 | Assert(!(uErrorCode & X86_TRAP_PF_RW));
|
---|
1191 | STAM_PROFILE_START(&pVM->iom.s.StatRZInstTest, h);
|
---|
1192 | rc = iomInterpretTEST(pVM, pCtxCore, GCPhysFault, &Cpu, pRange);
|
---|
1193 | STAM_PROFILE_STOP(&pVM->iom.s.StatRZInstTest, h);
|
---|
1194 | break;
|
---|
1195 |
|
---|
1196 | case OP_BT:
|
---|
1197 | Assert(!(uErrorCode & X86_TRAP_PF_RW));
|
---|
1198 | STAM_PROFILE_START(&pVM->iom.s.StatRZInstBt, l);
|
---|
1199 | rc = iomInterpretBT(pVM, pCtxCore, GCPhysFault, &Cpu, pRange);
|
---|
1200 | STAM_PROFILE_STOP(&pVM->iom.s.StatRZInstBt, l);
|
---|
1201 | break;
|
---|
1202 |
|
---|
1203 | case OP_XCHG:
|
---|
1204 | STAM_PROFILE_START(&pVM->iom.s.StatRZInstXchg, i);
|
---|
1205 | rc = iomInterpretXCHG(pVM, pCtxCore, GCPhysFault, &Cpu, pRange);
|
---|
1206 | STAM_PROFILE_STOP(&pVM->iom.s.StatRZInstXchg, i);
|
---|
1207 | break;
|
---|
1208 |
|
---|
1209 |
|
---|
1210 | /*
|
---|
1211 | * The instruction isn't supported. Hand it on to ring-3.
|
---|
1212 | */
|
---|
1213 | default:
|
---|
1214 | STAM_COUNTER_INC(&pVM->iom.s.StatRZInstOther);
|
---|
1215 | rc = (uErrorCode & X86_TRAP_PF_RW) ? VINF_IOM_HC_MMIO_WRITE : VINF_IOM_HC_MMIO_READ;
|
---|
1216 | break;
|
---|
1217 | }
|
---|
1218 |
|
---|
1219 | /*
|
---|
1220 | * On success advance EIP.
|
---|
1221 | */
|
---|
1222 | if (rc == VINF_SUCCESS)
|
---|
1223 | pCtxCore->rip += cbOp;
|
---|
1224 | else
|
---|
1225 | {
|
---|
1226 | STAM_COUNTER_INC(&pVM->iom.s.StatRZMMIOFailures);
|
---|
1227 | #if defined(VBOX_WITH_STATISTICS) && !defined(IN_RING3)
|
---|
1228 | switch (rc)
|
---|
1229 | {
|
---|
1230 | case VINF_IOM_HC_MMIO_READ:
|
---|
1231 | case VINF_IOM_HC_MMIO_READ_WRITE:
|
---|
1232 | STAM_COUNTER_INC(&pStats->CTX_MID_Z(Read,ToR3));
|
---|
1233 | break;
|
---|
1234 | case VINF_IOM_HC_MMIO_WRITE:
|
---|
1235 | STAM_COUNTER_INC(&pStats->CTX_MID_Z(Write,ToR3));
|
---|
1236 | break;
|
---|
1237 | }
|
---|
1238 | #endif
|
---|
1239 | }
|
---|
1240 |
|
---|
1241 | STAM_PROFILE_STOP(&pVM->iom.s.StatRZMMIOHandler, a);
|
---|
1242 | iomUnlock(pVM);
|
---|
1243 | return rc;
|
---|
1244 | }
|
---|
1245 |
|
---|
1246 |
|
---|
1247 | #ifdef IN_RING3
|
---|
1248 | /**
|
---|
1249 | * \#PF Handler callback for MMIO ranges.
|
---|
1250 | *
|
---|
1251 | * @returns VINF_SUCCESS if the handler have carried out the operation.
|
---|
1252 | * @returns VINF_PGM_HANDLER_DO_DEFAULT if the caller should carry out the access operation.
|
---|
1253 | * @param pVM VM Handle.
|
---|
1254 | * @param GCPhys The physical address the guest is writing to.
|
---|
1255 | * @param pvPhys The HC mapping of that address.
|
---|
1256 | * @param pvBuf What the guest is reading/writing.
|
---|
1257 | * @param cbBuf How much it's reading/writing.
|
---|
1258 | * @param enmAccessType The access type.
|
---|
1259 | * @param pvUser Pointer to the MMIO range entry.
|
---|
1260 | */
|
---|
1261 | DECLCALLBACK(int) IOMR3MMIOHandler(PVM pVM, RTGCPHYS GCPhysFault, void *pvPhys, void *pvBuf, size_t cbBuf, PGMACCESSTYPE enmAccessType, void *pvUser)
|
---|
1262 | {
|
---|
1263 | PIOMMMIORANGE pRange = (PIOMMMIORANGE)pvUser;
|
---|
1264 | STAM_COUNTER_INC(&pVM->iom.s.StatR3MMIOHandler);
|
---|
1265 |
|
---|
1266 | /* Take the IOM lock before performing any MMIO. */
|
---|
1267 | int rc = iomLock(pVM);
|
---|
1268 | AssertRC(rc);
|
---|
1269 |
|
---|
1270 | AssertMsg(cbBuf == 1 || cbBuf == 2 || cbBuf == 4 || cbBuf == 8, ("%zu\n", cbBuf));
|
---|
1271 |
|
---|
1272 | Assert(pRange);
|
---|
1273 | Assert(pRange == iomMMIOGetRange(&pVM->iom.s, GCPhysFault));
|
---|
1274 |
|
---|
1275 | if (enmAccessType == PGMACCESSTYPE_READ)
|
---|
1276 | rc = iomMMIODoRead(pVM, pRange, GCPhysFault, pvBuf, (unsigned)cbBuf);
|
---|
1277 | else
|
---|
1278 | rc = iomMMIODoWrite(pVM, pRange, GCPhysFault, pvBuf, (unsigned)cbBuf);
|
---|
1279 |
|
---|
1280 | AssertRC(rc);
|
---|
1281 | iomUnlock(pVM);
|
---|
1282 | return rc;
|
---|
1283 | }
|
---|
1284 | #endif /* IN_RING3 */
|
---|
1285 |
|
---|
1286 | /**
|
---|
1287 | * Reads a MMIO register.
|
---|
1288 | *
|
---|
1289 | * @returns VBox status code.
|
---|
1290 | *
|
---|
1291 | * @param pVM VM handle.
|
---|
1292 | * @param GCPhys The physical address to read.
|
---|
1293 | * @param pu32Value Where to store the value read.
|
---|
1294 | * @param cbValue The size of the register to read in bytes. 1, 2 or 4 bytes.
|
---|
1295 | */
|
---|
1296 | VMMDECL(int) IOMMMIORead(PVM pVM, RTGCPHYS GCPhys, uint32_t *pu32Value, size_t cbValue)
|
---|
1297 | {
|
---|
1298 | /* Take the IOM lock before performing any MMIO. */
|
---|
1299 | int rc = iomLock(pVM);
|
---|
1300 | #ifndef IN_RING3
|
---|
1301 | if (rc == VERR_SEM_BUSY)
|
---|
1302 | return VINF_IOM_HC_MMIO_WRITE;
|
---|
1303 | #endif
|
---|
1304 | AssertRC(rc);
|
---|
1305 |
|
---|
1306 | /*
|
---|
1307 | * Lookup the current context range node and statistics.
|
---|
1308 | */
|
---|
1309 | PIOMMMIORANGE pRange = iomMMIOGetRange(&pVM->iom.s, GCPhys);
|
---|
1310 | AssertMsg(pRange, ("Handlers and page tables are out of sync or something! GCPhys=%RGp cbValue=%d\n", GCPhys, cbValue));
|
---|
1311 | if (!pRange)
|
---|
1312 | {
|
---|
1313 | iomUnlock(pVM);
|
---|
1314 | return VERR_INTERNAL_ERROR;
|
---|
1315 | }
|
---|
1316 | #ifdef VBOX_WITH_STATISTICS
|
---|
1317 | PIOMMMIOSTATS pStats = iomMMIOGetStats(&pVM->iom.s, GCPhys, pRange);
|
---|
1318 | if (!pStats)
|
---|
1319 | {
|
---|
1320 | iomUnlock(pVM);
|
---|
1321 | # ifdef IN_RING3
|
---|
1322 | return VERR_NO_MEMORY;
|
---|
1323 | # else
|
---|
1324 | return VINF_IOM_HC_MMIO_READ;
|
---|
1325 | # endif
|
---|
1326 | }
|
---|
1327 | #endif /* VBOX_WITH_STATISTICS */
|
---|
1328 | if (pRange->CTX_SUFF(pfnReadCallback))
|
---|
1329 | {
|
---|
1330 | /*
|
---|
1331 | * Perform the read and deal with the result.
|
---|
1332 | */
|
---|
1333 | #ifdef VBOX_WITH_STATISTICS
|
---|
1334 | STAM_PROFILE_ADV_START(&pStats->CTX_SUFF_Z(ProfRead), a);
|
---|
1335 | #endif
|
---|
1336 | rc = pRange->CTX_SUFF(pfnReadCallback)(pRange->CTX_SUFF(pDevIns), pRange->CTX_SUFF(pvUser), GCPhys, pu32Value, (unsigned)cbValue);
|
---|
1337 | #ifdef VBOX_WITH_STATISTICS
|
---|
1338 | STAM_PROFILE_ADV_STOP(&pStats->CTX_SUFF_Z(ProfRead), a);
|
---|
1339 | if (rc != VINF_IOM_HC_MMIO_READ)
|
---|
1340 | STAM_COUNTER_INC(&pStats->CTX_SUFF_Z(Read));
|
---|
1341 | #endif
|
---|
1342 | switch (rc)
|
---|
1343 | {
|
---|
1344 | case VINF_SUCCESS:
|
---|
1345 | default:
|
---|
1346 | Log4(("IOMMMIORead: GCPhys=%RGp *pu32=%08RX32 cb=%d rc=%Rrc\n", GCPhys, *pu32Value, cbValue, rc));
|
---|
1347 | iomUnlock(pVM);
|
---|
1348 | return rc;
|
---|
1349 |
|
---|
1350 | case VINF_IOM_MMIO_UNUSED_00:
|
---|
1351 | switch (cbValue)
|
---|
1352 | {
|
---|
1353 | case 1: *(uint8_t *)pu32Value = UINT8_C(0x00); break;
|
---|
1354 | case 2: *(uint16_t *)pu32Value = UINT16_C(0x0000); break;
|
---|
1355 | case 4: *(uint32_t *)pu32Value = UINT32_C(0x00000000); break;
|
---|
1356 | case 8: *(uint64_t *)pu32Value = UINT64_C(0x0000000000000000); break;
|
---|
1357 | default: AssertReleaseMsgFailed(("cbValue=%d GCPhys=%RGp\n", cbValue, GCPhys)); break;
|
---|
1358 | }
|
---|
1359 | Log4(("IOMMMIORead: GCPhys=%RGp *pu32=%08RX32 cb=%d rc=%Rrc\n", GCPhys, *pu32Value, cbValue, rc));
|
---|
1360 | iomUnlock(pVM);
|
---|
1361 | return VINF_SUCCESS;
|
---|
1362 |
|
---|
1363 | case VINF_IOM_MMIO_UNUSED_FF:
|
---|
1364 | switch (cbValue)
|
---|
1365 | {
|
---|
1366 | case 1: *(uint8_t *)pu32Value = UINT8_C(0xff); break;
|
---|
1367 | case 2: *(uint16_t *)pu32Value = UINT16_C(0xffff); break;
|
---|
1368 | case 4: *(uint32_t *)pu32Value = UINT32_C(0xffffffff); break;
|
---|
1369 | case 8: *(uint64_t *)pu32Value = UINT64_C(0xffffffffffffffff); break;
|
---|
1370 | default: AssertReleaseMsgFailed(("cbValue=%d GCPhys=%RGp\n", cbValue, GCPhys)); break;
|
---|
1371 | }
|
---|
1372 | Log4(("IOMMMIORead: GCPhys=%RGp *pu32=%08RX32 cb=%d rc=%Rrc\n", GCPhys, *pu32Value, cbValue, rc));
|
---|
1373 | iomUnlock(pVM);
|
---|
1374 | return VINF_SUCCESS;
|
---|
1375 | }
|
---|
1376 | }
|
---|
1377 | #ifndef IN_RING3
|
---|
1378 | if (pRange->pfnReadCallbackR3)
|
---|
1379 | {
|
---|
1380 | STAM_COUNTER_INC(&pStats->CTX_MID_Z(Read,ToR3));
|
---|
1381 | iomUnlock(pVM);
|
---|
1382 | return VINF_IOM_HC_MMIO_READ;
|
---|
1383 | }
|
---|
1384 | #endif
|
---|
1385 |
|
---|
1386 | /*
|
---|
1387 | * Lookup the ring-3 range.
|
---|
1388 | */
|
---|
1389 | #ifdef VBOX_WITH_STATISTICS
|
---|
1390 | STAM_COUNTER_INC(&pStats->CTX_SUFF_Z(Read));
|
---|
1391 | #endif
|
---|
1392 | /* Unassigned memory; this is actually not supposed to happen. */
|
---|
1393 | switch (cbValue)
|
---|
1394 | {
|
---|
1395 | case 1: *(uint8_t *)pu32Value = UINT8_C(0xff); break;
|
---|
1396 | case 2: *(uint16_t *)pu32Value = UINT16_C(0xffff); break;
|
---|
1397 | case 4: *(uint32_t *)pu32Value = UINT32_C(0xffffffff); break;
|
---|
1398 | case 8: *(uint64_t *)pu32Value = UINT64_C(0xffffffffffffffff); break;
|
---|
1399 | default: AssertReleaseMsgFailed(("cbValue=%d GCPhys=%RGp\n", cbValue, GCPhys)); break;
|
---|
1400 | }
|
---|
1401 | Log4(("IOMMMIORead: GCPhys=%RGp *pu32=%08RX32 cb=%d rc=VINF_SUCCESS\n", GCPhys, *pu32Value, cbValue));
|
---|
1402 | iomUnlock(pVM);
|
---|
1403 | return VINF_SUCCESS;
|
---|
1404 | }
|
---|
1405 |
|
---|
1406 |
|
---|
1407 | /**
|
---|
1408 | * Writes to a MMIO register.
|
---|
1409 | *
|
---|
1410 | * @returns VBox status code.
|
---|
1411 | *
|
---|
1412 | * @param pVM VM handle.
|
---|
1413 | * @param GCPhys The physical address to write to.
|
---|
1414 | * @param u32Value The value to write.
|
---|
1415 | * @param cbValue The size of the register to read in bytes. 1, 2 or 4 bytes.
|
---|
1416 | */
|
---|
1417 | VMMDECL(int) IOMMMIOWrite(PVM pVM, RTGCPHYS GCPhys, uint32_t u32Value, size_t cbValue)
|
---|
1418 | {
|
---|
1419 | /* Take the IOM lock before performing any MMIO. */
|
---|
1420 | int rc = iomLock(pVM);
|
---|
1421 | #ifndef IN_RING3
|
---|
1422 | if (rc == VERR_SEM_BUSY)
|
---|
1423 | return VINF_IOM_HC_MMIO_WRITE;
|
---|
1424 | #endif
|
---|
1425 | AssertRC(rc);
|
---|
1426 |
|
---|
1427 | /*
|
---|
1428 | * Lookup the current context range node.
|
---|
1429 | */
|
---|
1430 | PIOMMMIORANGE pRange = iomMMIOGetRange(&pVM->iom.s, GCPhys);
|
---|
1431 | AssertMsg(pRange, ("Handlers and page tables are out of sync or something! GCPhys=%RGp cbValue=%d\n", GCPhys, cbValue));
|
---|
1432 | if (!pRange)
|
---|
1433 | {
|
---|
1434 | iomUnlock(pVM);
|
---|
1435 | return VERR_INTERNAL_ERROR;
|
---|
1436 | }
|
---|
1437 | #ifdef VBOX_WITH_STATISTICS
|
---|
1438 | PIOMMMIOSTATS pStats = iomMMIOGetStats(&pVM->iom.s, GCPhys, pRange);
|
---|
1439 | if (!pStats)
|
---|
1440 | {
|
---|
1441 | iomUnlock(pVM);
|
---|
1442 | # ifdef IN_RING3
|
---|
1443 | return VERR_NO_MEMORY;
|
---|
1444 | # else
|
---|
1445 | return VINF_IOM_HC_MMIO_WRITE;
|
---|
1446 | # endif
|
---|
1447 | }
|
---|
1448 | #endif /* VBOX_WITH_STATISTICS */
|
---|
1449 |
|
---|
1450 | /*
|
---|
1451 | * Perform the write if there's a write handler. R0/GC may have
|
---|
1452 | * to defer it to ring-3.
|
---|
1453 | */
|
---|
1454 | if (pRange->CTX_SUFF(pfnWriteCallback))
|
---|
1455 | {
|
---|
1456 | #ifdef VBOX_WITH_STATISTICS
|
---|
1457 | STAM_PROFILE_ADV_START(&pStats->CTX_SUFF_Z(ProfWrite), a);
|
---|
1458 | #endif
|
---|
1459 | rc = pRange->CTX_SUFF(pfnWriteCallback)(pRange->CTX_SUFF(pDevIns), pRange->CTX_SUFF(pvUser), GCPhys, &u32Value, (unsigned)cbValue);
|
---|
1460 | #ifdef VBOX_WITH_STATISTICS
|
---|
1461 | STAM_PROFILE_ADV_STOP(&pStats->CTX_SUFF_Z(ProfWrite), a);
|
---|
1462 | if (rc != VINF_IOM_HC_MMIO_WRITE)
|
---|
1463 | STAM_COUNTER_INC(&pStats->CTX_SUFF_Z(Write));
|
---|
1464 | #endif
|
---|
1465 | Log4(("IOMMMIOWrite: GCPhys=%RGp u32=%08RX32 cb=%d rc=%Rrc\n", GCPhys, u32Value, cbValue, rc));
|
---|
1466 | iomUnlock(pVM);
|
---|
1467 | return rc;
|
---|
1468 | }
|
---|
1469 | #ifndef IN_RING3
|
---|
1470 | if (pRange->pfnWriteCallbackR3)
|
---|
1471 | {
|
---|
1472 | STAM_COUNTER_INC(&pStats->CTX_MID_Z(Write,ToR3));
|
---|
1473 | iomUnlock(pVM);
|
---|
1474 | return VINF_IOM_HC_MMIO_WRITE;
|
---|
1475 | }
|
---|
1476 | #endif
|
---|
1477 |
|
---|
1478 | /*
|
---|
1479 | * No write handler, nothing to do.
|
---|
1480 | */
|
---|
1481 | #ifdef VBOX_WITH_STATISTICS
|
---|
1482 | STAM_COUNTER_INC(&pStats->CTX_SUFF_Z(Write));
|
---|
1483 | #endif
|
---|
1484 | Log4(("IOMMMIOWrite: GCPhys=%RGp u32=%08RX32 cb=%d rc=%Rrc\n", GCPhys, u32Value, cbValue, VINF_SUCCESS));
|
---|
1485 | iomUnlock(pVM);
|
---|
1486 | return VINF_SUCCESS;
|
---|
1487 | }
|
---|
1488 |
|
---|
1489 | /**
|
---|
1490 | * [REP*] INSB/INSW/INSD
|
---|
1491 | * ES:EDI,DX[,ECX]
|
---|
1492 | *
|
---|
1493 | * @remark Assumes caller checked the access privileges (IOMInterpretCheckPortIOAccess)
|
---|
1494 | *
|
---|
1495 | * @returns Strict VBox status code. Informational status codes other than the one documented
|
---|
1496 | * here are to be treated as internal failure. Use IOM_SUCCESS() to check for success.
|
---|
1497 | * @retval VINF_SUCCESS Success.
|
---|
1498 | * @retval VINF_EM_FIRST-VINF_EM_LAST Success with some exceptions (see IOM_SUCCESS()), the
|
---|
1499 | * status code must be passed on to EM.
|
---|
1500 | * @retval VINF_IOM_HC_IOPORT_READ Defer the read to ring-3. (R0/GC only)
|
---|
1501 | * @retval VINF_EM_RAW_EMULATE_INSTR Defer the read to the REM.
|
---|
1502 | * @retval VINF_EM_RAW_GUEST_TRAP The exception was left pending. (TRPMRaiseXcptErr)
|
---|
1503 | * @retval VINF_TRPM_XCPT_DISPATCHED The exception was raised and dispatched for raw-mode execution. (TRPMRaiseXcptErr)
|
---|
1504 | * @retval VINF_EM_RESCHEDULE_REM The exception was dispatched and cannot be executed in raw-mode. (TRPMRaiseXcptErr)
|
---|
1505 | *
|
---|
1506 | * @param pVM The virtual machine.
|
---|
1507 | * @param pRegFrame Pointer to CPUMCTXCORE guest registers structure.
|
---|
1508 | * @param uPort IO Port
|
---|
1509 | * @param uPrefix IO instruction prefix
|
---|
1510 | * @param cbTransfer Size of transfer unit
|
---|
1511 | */
|
---|
1512 | VMMDECL(int) IOMInterpretINSEx(PVM pVM, PCPUMCTXCORE pRegFrame, uint32_t uPort, uint32_t uPrefix, uint32_t cbTransfer)
|
---|
1513 | {
|
---|
1514 | #ifdef VBOX_WITH_STATISTICS
|
---|
1515 | STAM_COUNTER_INC(&pVM->iom.s.StatInstIns);
|
---|
1516 | #endif
|
---|
1517 |
|
---|
1518 | /*
|
---|
1519 | * We do not support REPNE or decrementing destination
|
---|
1520 | * pointer. Segment prefixes are deliberately ignored, as per the instruction specification.
|
---|
1521 | */
|
---|
1522 | if ( (uPrefix & PREFIX_REPNE)
|
---|
1523 | || pRegFrame->eflags.Bits.u1DF)
|
---|
1524 | return VINF_EM_RAW_EMULATE_INSTR;
|
---|
1525 |
|
---|
1526 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
1527 |
|
---|
1528 | /*
|
---|
1529 | * Get bytes/words/dwords count to transfer.
|
---|
1530 | */
|
---|
1531 | RTGCUINTREG cTransfers = 1;
|
---|
1532 | if (uPrefix & PREFIX_REP)
|
---|
1533 | {
|
---|
1534 | #ifndef IN_RC
|
---|
1535 | if ( CPUMIsGuestIn64BitCode(pVCpu, pRegFrame)
|
---|
1536 | && pRegFrame->rcx >= _4G)
|
---|
1537 | return VINF_EM_RAW_EMULATE_INSTR;
|
---|
1538 | #endif
|
---|
1539 | cTransfers = pRegFrame->ecx;
|
---|
1540 |
|
---|
1541 | if (SELMGetCpuModeFromSelector(pVM, pRegFrame->eflags, pRegFrame->cs, &pRegFrame->csHid) == CPUMODE_16BIT)
|
---|
1542 | cTransfers &= 0xffff;
|
---|
1543 |
|
---|
1544 | if (!cTransfers)
|
---|
1545 | return VINF_SUCCESS;
|
---|
1546 | }
|
---|
1547 |
|
---|
1548 | /* Convert destination address es:edi. */
|
---|
1549 | RTGCPTR GCPtrDst;
|
---|
1550 | int rc = SELMToFlatEx(pVM, DIS_SELREG_ES, pRegFrame, (RTGCPTR)pRegFrame->rdi,
|
---|
1551 | SELMTOFLAT_FLAGS_HYPER | SELMTOFLAT_FLAGS_NO_PL,
|
---|
1552 | &GCPtrDst);
|
---|
1553 | if (RT_FAILURE(rc))
|
---|
1554 | {
|
---|
1555 | Log(("INS destination address conversion failed -> fallback, rc=%d\n", rc));
|
---|
1556 | return VINF_EM_RAW_EMULATE_INSTR;
|
---|
1557 | }
|
---|
1558 |
|
---|
1559 | /* Access verification first; we can't recover from traps inside this instruction, as the port read cannot be repeated. */
|
---|
1560 | uint32_t cpl = CPUMGetGuestCPL(pVCpu, pRegFrame);
|
---|
1561 |
|
---|
1562 | rc = PGMVerifyAccess(pVCpu, (RTGCUINTPTR)GCPtrDst, cTransfers * cbTransfer,
|
---|
1563 | X86_PTE_RW | ((cpl == 3) ? X86_PTE_US : 0));
|
---|
1564 | if (rc != VINF_SUCCESS)
|
---|
1565 | {
|
---|
1566 | Log(("INS will generate a trap -> fallback, rc=%d\n", rc));
|
---|
1567 | return VINF_EM_RAW_EMULATE_INSTR;
|
---|
1568 | }
|
---|
1569 |
|
---|
1570 | Log(("IOM: rep ins%d port %#x count %d\n", cbTransfer * 8, uPort, cTransfers));
|
---|
1571 | if (cTransfers > 1)
|
---|
1572 | {
|
---|
1573 | /* If the device supports string transfers, ask it to do as
|
---|
1574 | * much as it wants. The rest is done with single-word transfers. */
|
---|
1575 | const RTGCUINTREG cTransfersOrg = cTransfers;
|
---|
1576 | rc = IOMIOPortReadString(pVM, uPort, &GCPtrDst, &cTransfers, cbTransfer);
|
---|
1577 | AssertRC(rc); Assert(cTransfers <= cTransfersOrg);
|
---|
1578 | pRegFrame->rdi += (cTransfersOrg - cTransfers) * cbTransfer;
|
---|
1579 | }
|
---|
1580 |
|
---|
1581 | #ifdef IN_RC
|
---|
1582 | MMGCRamRegisterTrapHandler(pVM);
|
---|
1583 | #endif
|
---|
1584 |
|
---|
1585 | while (cTransfers && rc == VINF_SUCCESS)
|
---|
1586 | {
|
---|
1587 | uint32_t u32Value;
|
---|
1588 | rc = IOMIOPortRead(pVM, uPort, &u32Value, cbTransfer);
|
---|
1589 | if (!IOM_SUCCESS(rc))
|
---|
1590 | break;
|
---|
1591 | int rc2 = iomRamWrite(pVCpu, pRegFrame, GCPtrDst, &u32Value, cbTransfer);
|
---|
1592 | Assert(rc2 == VINF_SUCCESS); NOREF(rc2);
|
---|
1593 | GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + cbTransfer);
|
---|
1594 | pRegFrame->rdi += cbTransfer;
|
---|
1595 | cTransfers--;
|
---|
1596 | }
|
---|
1597 | #ifdef IN_RC
|
---|
1598 | MMGCRamDeregisterTrapHandler(pVM);
|
---|
1599 | #endif
|
---|
1600 |
|
---|
1601 | /* Update ecx on exit. */
|
---|
1602 | if (uPrefix & PREFIX_REP)
|
---|
1603 | pRegFrame->ecx = cTransfers;
|
---|
1604 |
|
---|
1605 | AssertMsg(rc == VINF_SUCCESS || rc == VINF_IOM_HC_IOPORT_READ || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST) || RT_FAILURE(rc), ("%Rrc\n", rc));
|
---|
1606 | return rc;
|
---|
1607 | }
|
---|
1608 |
|
---|
1609 |
|
---|
1610 | /**
|
---|
1611 | * [REP*] INSB/INSW/INSD
|
---|
1612 | * ES:EDI,DX[,ECX]
|
---|
1613 | *
|
---|
1614 | * @returns Strict VBox status code. Informational status codes other than the one documented
|
---|
1615 | * here are to be treated as internal failure. Use IOM_SUCCESS() to check for success.
|
---|
1616 | * @retval VINF_SUCCESS Success.
|
---|
1617 | * @retval VINF_EM_FIRST-VINF_EM_LAST Success with some exceptions (see IOM_SUCCESS()), the
|
---|
1618 | * status code must be passed on to EM.
|
---|
1619 | * @retval VINF_IOM_HC_IOPORT_READ Defer the read to ring-3. (R0/GC only)
|
---|
1620 | * @retval VINF_EM_RAW_EMULATE_INSTR Defer the read to the REM.
|
---|
1621 | * @retval VINF_EM_RAW_GUEST_TRAP The exception was left pending. (TRPMRaiseXcptErr)
|
---|
1622 | * @retval VINF_TRPM_XCPT_DISPATCHED The exception was raised and dispatched for raw-mode execution. (TRPMRaiseXcptErr)
|
---|
1623 | * @retval VINF_EM_RESCHEDULE_REM The exception was dispatched and cannot be executed in raw-mode. (TRPMRaiseXcptErr)
|
---|
1624 | *
|
---|
1625 | * @param pVM The virtual machine.
|
---|
1626 | * @param pRegFrame Pointer to CPUMCTXCORE guest registers structure.
|
---|
1627 | * @param pCpu Disassembler CPU state.
|
---|
1628 | */
|
---|
1629 | VMMDECL(int) IOMInterpretINS(PVM pVM, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pCpu)
|
---|
1630 | {
|
---|
1631 | /*
|
---|
1632 | * Get port number directly from the register (no need to bother the
|
---|
1633 | * disassembler). And get the I/O register size from the opcode / prefix.
|
---|
1634 | */
|
---|
1635 | RTIOPORT Port = pRegFrame->edx & 0xffff;
|
---|
1636 | unsigned cb = 0;
|
---|
1637 | if (pCpu->pCurInstr->opcode == OP_INSB)
|
---|
1638 | cb = 1;
|
---|
1639 | else
|
---|
1640 | cb = (pCpu->opmode == CPUMODE_16BIT) ? 2 : 4; /* dword in both 32 & 64 bits mode */
|
---|
1641 |
|
---|
1642 | int rc = IOMInterpretCheckPortIOAccess(pVM, pRegFrame, Port, cb);
|
---|
1643 | if (RT_UNLIKELY(rc != VINF_SUCCESS))
|
---|
1644 | {
|
---|
1645 | AssertMsg(rc == VINF_EM_RAW_GUEST_TRAP || rc == VINF_TRPM_XCPT_DISPATCHED || rc == VINF_TRPM_XCPT_DISPATCHED || RT_FAILURE(rc), ("%Rrc\n", rc));
|
---|
1646 | return rc;
|
---|
1647 | }
|
---|
1648 |
|
---|
1649 | return IOMInterpretINSEx(pVM, pRegFrame, Port, pCpu->prefix, cb);
|
---|
1650 | }
|
---|
1651 |
|
---|
1652 |
|
---|
1653 | /**
|
---|
1654 | * [REP*] OUTSB/OUTSW/OUTSD
|
---|
1655 | * DS:ESI,DX[,ECX]
|
---|
1656 | *
|
---|
1657 | * @remark Assumes caller checked the access privileges (IOMInterpretCheckPortIOAccess)
|
---|
1658 | *
|
---|
1659 | * @returns Strict VBox status code. Informational status codes other than the one documented
|
---|
1660 | * here are to be treated as internal failure. Use IOM_SUCCESS() to check for success.
|
---|
1661 | * @retval VINF_SUCCESS Success.
|
---|
1662 | * @retval VINF_EM_FIRST-VINF_EM_LAST Success with some exceptions (see IOM_SUCCESS()), the
|
---|
1663 | * status code must be passed on to EM.
|
---|
1664 | * @retval VINF_IOM_HC_IOPORT_WRITE Defer the write to ring-3. (R0/GC only)
|
---|
1665 | * @retval VINF_EM_RAW_GUEST_TRAP The exception was left pending. (TRPMRaiseXcptErr)
|
---|
1666 | * @retval VINF_TRPM_XCPT_DISPATCHED The exception was raised and dispatched for raw-mode execution. (TRPMRaiseXcptErr)
|
---|
1667 | * @retval VINF_EM_RESCHEDULE_REM The exception was dispatched and cannot be executed in raw-mode. (TRPMRaiseXcptErr)
|
---|
1668 | *
|
---|
1669 | * @param pVM The virtual machine.
|
---|
1670 | * @param pRegFrame Pointer to CPUMCTXCORE guest registers structure.
|
---|
1671 | * @param uPort IO Port
|
---|
1672 | * @param uPrefix IO instruction prefix
|
---|
1673 | * @param cbTransfer Size of transfer unit
|
---|
1674 | */
|
---|
1675 | VMMDECL(int) IOMInterpretOUTSEx(PVM pVM, PCPUMCTXCORE pRegFrame, uint32_t uPort, uint32_t uPrefix, uint32_t cbTransfer)
|
---|
1676 | {
|
---|
1677 | #ifdef VBOX_WITH_STATISTICS
|
---|
1678 | STAM_COUNTER_INC(&pVM->iom.s.StatInstOuts);
|
---|
1679 | #endif
|
---|
1680 |
|
---|
1681 | /*
|
---|
1682 | * We do not support segment prefixes, REPNE or
|
---|
1683 | * decrementing source pointer.
|
---|
1684 | */
|
---|
1685 | if ( (uPrefix & (PREFIX_SEG | PREFIX_REPNE))
|
---|
1686 | || pRegFrame->eflags.Bits.u1DF)
|
---|
1687 | return VINF_EM_RAW_EMULATE_INSTR;
|
---|
1688 |
|
---|
1689 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
1690 |
|
---|
1691 | /*
|
---|
1692 | * Get bytes/words/dwords count to transfer.
|
---|
1693 | */
|
---|
1694 | RTGCUINTREG cTransfers = 1;
|
---|
1695 | if (uPrefix & PREFIX_REP)
|
---|
1696 | {
|
---|
1697 | #ifndef IN_RC
|
---|
1698 | if ( CPUMIsGuestIn64BitCode(pVCpu, pRegFrame)
|
---|
1699 | && pRegFrame->rcx >= _4G)
|
---|
1700 | return VINF_EM_RAW_EMULATE_INSTR;
|
---|
1701 | #endif
|
---|
1702 | cTransfers = pRegFrame->ecx;
|
---|
1703 | if (SELMGetCpuModeFromSelector(pVM, pRegFrame->eflags, pRegFrame->cs, &pRegFrame->csHid) == CPUMODE_16BIT)
|
---|
1704 | cTransfers &= 0xffff;
|
---|
1705 |
|
---|
1706 | if (!cTransfers)
|
---|
1707 | return VINF_SUCCESS;
|
---|
1708 | }
|
---|
1709 |
|
---|
1710 | /* Convert source address ds:esi. */
|
---|
1711 | RTGCPTR GCPtrSrc;
|
---|
1712 | int rc = SELMToFlatEx(pVM, DIS_SELREG_DS, pRegFrame, (RTGCPTR)pRegFrame->rsi,
|
---|
1713 | SELMTOFLAT_FLAGS_HYPER | SELMTOFLAT_FLAGS_NO_PL,
|
---|
1714 | &GCPtrSrc);
|
---|
1715 | if (RT_FAILURE(rc))
|
---|
1716 | {
|
---|
1717 | Log(("OUTS source address conversion failed -> fallback, rc=%Rrc\n", rc));
|
---|
1718 | return VINF_EM_RAW_EMULATE_INSTR;
|
---|
1719 | }
|
---|
1720 |
|
---|
1721 | /* Access verification first; we currently can't recover properly from traps inside this instruction */
|
---|
1722 | uint32_t cpl = CPUMGetGuestCPL(pVCpu, pRegFrame);
|
---|
1723 | rc = PGMVerifyAccess(pVCpu, (RTGCUINTPTR)GCPtrSrc, cTransfers * cbTransfer,
|
---|
1724 | (cpl == 3) ? X86_PTE_US : 0);
|
---|
1725 | if (rc != VINF_SUCCESS)
|
---|
1726 | {
|
---|
1727 | Log(("OUTS will generate a trap -> fallback, rc=%Rrc\n", rc));
|
---|
1728 | return VINF_EM_RAW_EMULATE_INSTR;
|
---|
1729 | }
|
---|
1730 |
|
---|
1731 | Log(("IOM: rep outs%d port %#x count %d\n", cbTransfer * 8, uPort, cTransfers));
|
---|
1732 | if (cTransfers > 1)
|
---|
1733 | {
|
---|
1734 | /*
|
---|
1735 | * If the device supports string transfers, ask it to do as
|
---|
1736 | * much as it wants. The rest is done with single-word transfers.
|
---|
1737 | */
|
---|
1738 | const RTGCUINTREG cTransfersOrg = cTransfers;
|
---|
1739 | rc = IOMIOPortWriteString(pVM, uPort, &GCPtrSrc, &cTransfers, cbTransfer);
|
---|
1740 | AssertRC(rc); Assert(cTransfers <= cTransfersOrg);
|
---|
1741 | pRegFrame->rsi += (cTransfersOrg - cTransfers) * cbTransfer;
|
---|
1742 | }
|
---|
1743 |
|
---|
1744 | #ifdef IN_RC
|
---|
1745 | MMGCRamRegisterTrapHandler(pVM);
|
---|
1746 | #endif
|
---|
1747 |
|
---|
1748 | while (cTransfers && rc == VINF_SUCCESS)
|
---|
1749 | {
|
---|
1750 | uint32_t u32Value;
|
---|
1751 | rc = iomRamRead(pVCpu, &u32Value, GCPtrSrc, cbTransfer);
|
---|
1752 | if (rc != VINF_SUCCESS)
|
---|
1753 | break;
|
---|
1754 | rc = IOMIOPortWrite(pVM, uPort, u32Value, cbTransfer);
|
---|
1755 | if (!IOM_SUCCESS(rc))
|
---|
1756 | break;
|
---|
1757 | GCPtrSrc = (RTGCPTR)((RTUINTPTR)GCPtrSrc + cbTransfer);
|
---|
1758 | pRegFrame->rsi += cbTransfer;
|
---|
1759 | cTransfers--;
|
---|
1760 | }
|
---|
1761 |
|
---|
1762 | #ifdef IN_RC
|
---|
1763 | MMGCRamDeregisterTrapHandler(pVM);
|
---|
1764 | #endif
|
---|
1765 |
|
---|
1766 | /* Update ecx on exit. */
|
---|
1767 | if (uPrefix & PREFIX_REP)
|
---|
1768 | pRegFrame->ecx = cTransfers;
|
---|
1769 |
|
---|
1770 | AssertMsg(rc == VINF_SUCCESS || rc == VINF_IOM_HC_IOPORT_WRITE || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST) || RT_FAILURE(rc), ("%Rrc\n", rc));
|
---|
1771 | return rc;
|
---|
1772 | }
|
---|
1773 |
|
---|
1774 |
|
---|
1775 | /**
|
---|
1776 | * [REP*] OUTSB/OUTSW/OUTSD
|
---|
1777 | * DS:ESI,DX[,ECX]
|
---|
1778 | *
|
---|
1779 | * @returns Strict VBox status code. Informational status codes other than the one documented
|
---|
1780 | * here are to be treated as internal failure. Use IOM_SUCCESS() to check for success.
|
---|
1781 | * @retval VINF_SUCCESS Success.
|
---|
1782 | * @retval VINF_EM_FIRST-VINF_EM_LAST Success with some exceptions (see IOM_SUCCESS()), the
|
---|
1783 | * status code must be passed on to EM.
|
---|
1784 | * @retval VINF_IOM_HC_IOPORT_WRITE Defer the write to ring-3. (R0/GC only)
|
---|
1785 | * @retval VINF_EM_RAW_EMULATE_INSTR Defer the write to the REM.
|
---|
1786 | * @retval VINF_EM_RAW_GUEST_TRAP The exception was left pending. (TRPMRaiseXcptErr)
|
---|
1787 | * @retval VINF_TRPM_XCPT_DISPATCHED The exception was raised and dispatched for raw-mode execution. (TRPMRaiseXcptErr)
|
---|
1788 | * @retval VINF_EM_RESCHEDULE_REM The exception was dispatched and cannot be executed in raw-mode. (TRPMRaiseXcptErr)
|
---|
1789 | *
|
---|
1790 | * @param pVM The virtual machine.
|
---|
1791 | * @param pRegFrame Pointer to CPUMCTXCORE guest registers structure.
|
---|
1792 | * @param pCpu Disassembler CPU state.
|
---|
1793 | */
|
---|
1794 | VMMDECL(int) IOMInterpretOUTS(PVM pVM, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pCpu)
|
---|
1795 | {
|
---|
1796 | /*
|
---|
1797 | * Get port number from the first parameter.
|
---|
1798 | * And get the I/O register size from the opcode / prefix.
|
---|
1799 | */
|
---|
1800 | uint64_t Port = 0;
|
---|
1801 | unsigned cb = 0;
|
---|
1802 | bool fRc = iomGetRegImmData(pCpu, &pCpu->param1, pRegFrame, &Port, &cb);
|
---|
1803 | AssertMsg(fRc, ("Failed to get reg/imm port number!\n")); NOREF(fRc);
|
---|
1804 | if (pCpu->pCurInstr->opcode == OP_OUTSB)
|
---|
1805 | cb = 1;
|
---|
1806 | else
|
---|
1807 | cb = (pCpu->opmode == CPUMODE_16BIT) ? 2 : 4; /* dword in both 32 & 64 bits mode */
|
---|
1808 |
|
---|
1809 | int rc = IOMInterpretCheckPortIOAccess(pVM, pRegFrame, Port, cb);
|
---|
1810 | if (RT_UNLIKELY(rc != VINF_SUCCESS))
|
---|
1811 | {
|
---|
1812 | AssertMsg(rc == VINF_EM_RAW_GUEST_TRAP || rc == VINF_TRPM_XCPT_DISPATCHED || rc == VINF_TRPM_XCPT_DISPATCHED || RT_FAILURE(rc), ("%Rrc\n", rc));
|
---|
1813 | return rc;
|
---|
1814 | }
|
---|
1815 |
|
---|
1816 | return IOMInterpretOUTSEx(pVM, pRegFrame, Port, pCpu->prefix, cb);
|
---|
1817 | }
|
---|
1818 |
|
---|
1819 |
|
---|
1820 | #ifndef IN_RC
|
---|
1821 | /**
|
---|
1822 | * Mapping an MMIO2 page in place of an MMIO page for direct access.
|
---|
1823 | *
|
---|
1824 | * (This is a special optimization used by the VGA device.)
|
---|
1825 | *
|
---|
1826 | * @returns VBox status code.
|
---|
1827 | *
|
---|
1828 | * @param pVM The virtual machine.
|
---|
1829 | * @param GCPhys The address of the MMIO page to be changed.
|
---|
1830 | * @param GCPhysRemapped The address of the MMIO2 page.
|
---|
1831 | * @param fPageFlags Page flags to set. Must be (X86_PTE_RW | X86_PTE_P)
|
---|
1832 | * for the time being.
|
---|
1833 | */
|
---|
1834 | VMMDECL(int) IOMMMIOMapMMIO2Page(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS GCPhysRemapped, uint64_t fPageFlags)
|
---|
1835 | {
|
---|
1836 | Log(("IOMMMIOMapMMIO2Page %RGp -> %RGp flags=%RX64\n", GCPhys, GCPhysRemapped, fPageFlags));
|
---|
1837 |
|
---|
1838 | AssertReturn(fPageFlags == (X86_PTE_RW | X86_PTE_P), VERR_INVALID_PARAMETER);
|
---|
1839 |
|
---|
1840 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
1841 |
|
---|
1842 | /* This currently only works in real mode, protected mode without paging or with nested paging. */
|
---|
1843 | if ( !HWACCMIsEnabled(pVM) /* useless without VT-x/AMD-V */
|
---|
1844 | || ( CPUMIsGuestInPagedProtectedMode(pVCpu)
|
---|
1845 | && !HWACCMIsNestedPagingActive(pVM)))
|
---|
1846 | return VINF_SUCCESS; /* ignore */
|
---|
1847 |
|
---|
1848 | /*
|
---|
1849 | * Lookup the context range node the page belongs to.
|
---|
1850 | */
|
---|
1851 | PIOMMMIORANGE pRange = iomMMIOGetRange(&pVM->iom.s, GCPhys);
|
---|
1852 | AssertMsgReturn(pRange,
|
---|
1853 | ("Handlers and page tables are out of sync or something! GCPhys=%RGp\n", GCPhys),
|
---|
1854 | VERR_IOM_MMIO_RANGE_NOT_FOUND);
|
---|
1855 | Assert((pRange->GCPhys & PAGE_OFFSET_MASK) == 0);
|
---|
1856 | Assert((pRange->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK);
|
---|
1857 |
|
---|
1858 | /*
|
---|
1859 | * Do the aliasing; page align the addresses since PGM is picky.
|
---|
1860 | */
|
---|
1861 | GCPhys &= ~(RTGCPHYS)PAGE_OFFSET_MASK;
|
---|
1862 | GCPhysRemapped &= ~(RTGCPHYS)PAGE_OFFSET_MASK;
|
---|
1863 |
|
---|
1864 | int rc = PGMHandlerPhysicalPageAlias(pVM, pRange->GCPhys, GCPhys, GCPhysRemapped);
|
---|
1865 | AssertRCReturn(rc, rc);
|
---|
1866 |
|
---|
1867 | /*
|
---|
1868 | * Modify the shadow page table. Since it's an MMIO page it won't be present and we
|
---|
1869 | * can simply prefetch it.
|
---|
1870 | *
|
---|
1871 | * Note: This is a NOP in the EPT case; we'll just let it fault again to resync the page.
|
---|
1872 | */
|
---|
1873 | #if 0 /* The assertion is wrong for the PGM_SYNC_CLEAR_PGM_POOL and VINF_PGM_HANDLER_ALREADY_ALIASED cases. */
|
---|
1874 | # ifdef VBOX_STRICT
|
---|
1875 | uint64_t fFlags;
|
---|
1876 | RTHCPHYS HCPhys;
|
---|
1877 | rc = PGMShwGetPage(pVCpu, (RTGCPTR)GCPhys, &fFlags, &HCPhys);
|
---|
1878 | Assert(rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT);
|
---|
1879 | # endif
|
---|
1880 | #endif
|
---|
1881 | rc = PGMPrefetchPage(pVCpu, (RTGCPTR)GCPhys);
|
---|
1882 | Assert(rc == VINF_SUCCESS || rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT);
|
---|
1883 | return VINF_SUCCESS;
|
---|
1884 | }
|
---|
1885 |
|
---|
1886 |
|
---|
1887 | /**
|
---|
1888 | * Reset a previously modified MMIO region; restore the access flags.
|
---|
1889 | *
|
---|
1890 | * @returns VBox status code.
|
---|
1891 | *
|
---|
1892 | * @param pVM The virtual machine.
|
---|
1893 | * @param GCPhys Physical address that's part of the MMIO region to be reset.
|
---|
1894 | */
|
---|
1895 | VMMDECL(int) IOMMMIOResetRegion(PVM pVM, RTGCPHYS GCPhys)
|
---|
1896 | {
|
---|
1897 | Log(("IOMMMIOResetRegion %RGp\n", GCPhys));
|
---|
1898 |
|
---|
1899 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
1900 |
|
---|
1901 | /* This currently only works in real mode, protected mode without paging or with nested paging. */
|
---|
1902 | if ( !HWACCMIsEnabled(pVM) /* useless without VT-x/AMD-V */
|
---|
1903 | || ( CPUMIsGuestInPagedProtectedMode(pVCpu)
|
---|
1904 | && !HWACCMIsNestedPagingActive(pVM)))
|
---|
1905 | return VINF_SUCCESS; /* ignore */
|
---|
1906 |
|
---|
1907 | /*
|
---|
1908 | * Lookup the context range node the page belongs to.
|
---|
1909 | */
|
---|
1910 | PIOMMMIORANGE pRange = iomMMIOGetRange(&pVM->iom.s, GCPhys);
|
---|
1911 | AssertMsgReturn(pRange,
|
---|
1912 | ("Handlers and page tables are out of sync or something! GCPhys=%RGp\n", GCPhys),
|
---|
1913 | VERR_IOM_MMIO_RANGE_NOT_FOUND);
|
---|
1914 |
|
---|
1915 | /*
|
---|
1916 | * Call PGM to do the job work.
|
---|
1917 | *
|
---|
1918 | * After the call, all the pages should be non-present... unless there is
|
---|
1919 | * a page pool flush pending (unlikely).
|
---|
1920 | */
|
---|
1921 | int rc = PGMHandlerPhysicalReset(pVM, pRange->GCPhys);
|
---|
1922 | AssertRC(rc);
|
---|
1923 |
|
---|
1924 | #ifdef VBOX_STRICT
|
---|
1925 | if (!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_PGM_SYNC_CR3))
|
---|
1926 | {
|
---|
1927 | uint32_t cb = pRange->cb;
|
---|
1928 | GCPhys = pRange->GCPhys;
|
---|
1929 | while (cb)
|
---|
1930 | {
|
---|
1931 | uint64_t fFlags;
|
---|
1932 | RTHCPHYS HCPhys;
|
---|
1933 | rc = PGMShwGetPage(pVCpu, (RTGCPTR)GCPhys, &fFlags, &HCPhys);
|
---|
1934 | Assert(rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT);
|
---|
1935 | cb -= PAGE_SIZE;
|
---|
1936 | GCPhys += PAGE_SIZE;
|
---|
1937 | }
|
---|
1938 | }
|
---|
1939 | #endif
|
---|
1940 | return rc;
|
---|
1941 | }
|
---|
1942 | #endif /* !IN_RC */
|
---|
1943 |
|
---|