VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/IOMAllMMIO.cpp@ 10171

Last change on this file since 10171 was 9837, checked in by vboxsync, 16 years ago

build fix

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 53.8 KB
Line 
1/* $Id: IOMAllMMIO.cpp 9837 2008-06-20 09:01:48Z vboxsync $ */
2/** @file
3 * IOM - Input / Output Monitor - Guest Context.
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
38#include <VBox/dis.h>
39#include <VBox/disopcode.h>
40#include <VBox/param.h>
41#include <VBox/err.h>
42#include <iprt/assert.h>
43#include <VBox/log.h>
44#include <iprt/asm.h>
45#include <iprt/string.h>
46
47
48
49/*******************************************************************************
50* Global Variables *
51*******************************************************************************/
52
53/**
54 * Array for fast recode of the operand size (1/2/4/8 bytes) to bit shift value.
55 */
56static const unsigned g_aSize2Shift[] =
57{
58 ~0, /* 0 - invalid */
59 0, /* *1 == 2^0 */
60 1, /* *2 == 2^1 */
61 ~0, /* 3 - invalid */
62 2, /* *4 == 2^2 */
63 ~0, /* 5 - invalid */
64 ~0, /* 6 - invalid */
65 ~0, /* 7 - invalid */
66 3 /* *8 == 2^3 */
67};
68
69/**
70 * Macro for fast recode of the operand size (1/2/4/8 bytes) to bit shift value.
71 */
72#define SIZE_2_SHIFT(cb) (g_aSize2Shift[cb])
73
74
75/**
76 * Wrapper which does the write and updates range statistics when such are enabled.
77 * @warning VBOX_SUCCESS(rc=VINF_IOM_HC_MMIO_WRITE) is TRUE!
78 */
79DECLINLINE(int) iomMMIODoWrite(PVM pVM, PIOMMMIORANGE pRange, RTGCPHYS GCPhysFault, const void *pvData, unsigned cb)
80{
81#ifdef VBOX_WITH_STATISTICS
82 PIOMMMIOSTATS pStats = iomMMIOGetStats(&pVM->iom.s, GCPhysFault, pRange);
83 Assert(pStats);
84#endif
85
86 int rc;
87 if (RT_LIKELY(pRange->CTXALLSUFF(pfnWriteCallback)))
88 rc = pRange->CTXALLSUFF(pfnWriteCallback)(pRange->CTXALLSUFF(pDevIns), pRange->CTXALLSUFF(pvUser), GCPhysFault, (void *)pvData, cb); /* @todo fix const!! */
89 else
90 rc = VINF_SUCCESS;
91 if (rc != VINF_IOM_HC_MMIO_WRITE)
92 STAM_COUNTER_INC(&pStats->CTXALLSUFF(Write));
93 return rc;
94}
95
96/**
97 * Wrapper which does the read and updates range statistics when such are enabled.
98 */
99DECLINLINE(int) iomMMIODoRead(PVM pVM, PIOMMMIORANGE pRange, RTGCPHYS GCPhysFault, void *pvData, unsigned cb)
100{
101#ifdef VBOX_WITH_STATISTICS
102 PIOMMMIOSTATS pStats = iomMMIOGetStats(&pVM->iom.s, GCPhysFault, pRange);
103 Assert(pStats);
104#endif
105
106 int rc;
107 if (RT_LIKELY(pRange->CTXALLSUFF(pfnReadCallback)))
108 rc = pRange->CTXALLSUFF(pfnReadCallback)(pRange->CTXALLSUFF(pDevIns), pRange->CTXALLSUFF(pvUser), GCPhysFault, pvData, cb);
109 else
110 {
111 switch (cb)
112 {
113 case 1: *(uint8_t *)pvData = 0; break;
114 case 2: *(uint16_t *)pvData = 0; break;
115 case 4: *(uint32_t *)pvData = 0; break;
116 case 8: *(uint64_t *)pvData = 0; break;
117 default:
118 memset(pvData, 0, cb);
119 break;
120 }
121 rc = VINF_SUCCESS;
122 }
123 if (rc != VINF_IOM_HC_MMIO_READ)
124 STAM_COUNTER_INC(&pStats->CTXALLSUFF(Read));
125 return rc;
126}
127
128/*
129 * Internal - statistics only.
130 */
131DECLINLINE(void) iomMMIOStatLength(PVM pVM, unsigned cb)
132{
133#ifdef VBOX_WITH_STATISTICS
134 switch (cb)
135 {
136 case 1:
137 STAM_COUNTER_INC(&pVM->iom.s.StatGCMMIO1Byte);
138 break;
139 case 2:
140 STAM_COUNTER_INC(&pVM->iom.s.StatGCMMIO2Bytes);
141 break;
142 case 4:
143 STAM_COUNTER_INC(&pVM->iom.s.StatGCMMIO4Bytes);
144 break;
145 case 8:
146 STAM_COUNTER_INC(&pVM->iom.s.StatGCMMIO8Bytes);
147 break;
148 default:
149 /* No way. */
150 AssertMsgFailed(("Invalid data length %d\n", cb));
151 break;
152 }
153#else
154 NOREF(pVM); NOREF(cb);
155#endif
156}
157
158
159/**
160 * MOV reg, mem (read)
161 * MOVZX reg, mem (read)
162 * MOVSX reg, mem (read)
163 *
164 * @returns VBox status code.
165 *
166 * @param pVM The virtual machine (GC pointer ofcourse).
167 * @param pRegFrame Pointer to CPUMCTXCORE guest registers structure.
168 * @param pCpu Disassembler CPU state.
169 * @param pRange Pointer MMIO range.
170 * @param GCPhysFault The GC physical address corresponding to pvFault.
171 */
172static int iomInterpretMOVxXRead(PVM pVM, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pCpu, PIOMMMIORANGE pRange, RTGCPHYS GCPhysFault)
173{
174 Assert(pRange->CTXALLSUFF(pfnReadCallback) || !pRange->pfnReadCallbackR3);
175
176 /*
177 * Get the data size from parameter 2,
178 * and call the handler function to get the data.
179 */
180 unsigned cb = DISGetParamSize(pCpu, &pCpu->param2);
181 AssertMsg(cb > 0 && cb <= sizeof(uint32_t), ("cb=%d\n", cb));
182
183 uint32_t u32Data = 0;
184 int rc = iomMMIODoRead(pVM, pRange, GCPhysFault, &u32Data, cb);
185 if (rc == VINF_SUCCESS)
186 {
187 /*
188 * Do sign extension for MOVSX.
189 */
190 /** @todo checkup MOVSX implementation! */
191 if (pCpu->pCurInstr->opcode == OP_MOVSX)
192 {
193 if (cb == 1)
194 {
195 /* DWORD <- BYTE */
196 int32_t iData = (int8_t)u32Data;
197 u32Data = (uint32_t)iData;
198 }
199 else
200 {
201 /* DWORD <- WORD */
202 int32_t iData = (int16_t)u32Data;
203 u32Data = (uint32_t)iData;
204 }
205 }
206
207 /*
208 * Store the result to register (parameter 1).
209 */
210 bool fRc = iomSaveDataToReg(pCpu, &pCpu->param1, pRegFrame, u32Data);
211 AssertMsg(fRc, ("Failed to store register value!\n")); NOREF(fRc);
212 }
213
214 if (rc == VINF_SUCCESS)
215 iomMMIOStatLength(pVM, cb);
216 return rc;
217}
218
219
220/**
221 * MOV mem, reg|imm (write)
222 *
223 * @returns VBox status code.
224 *
225 * @param pVM The virtual machine (GC pointer ofcourse).
226 * @param pRegFrame Pointer to CPUMCTXCORE guest registers structure.
227 * @param pCpu Disassembler CPU state.
228 * @param pRange Pointer MMIO range.
229 * @param GCPhysFault The GC physical address corresponding to pvFault.
230 */
231static int iomInterpretMOVxXWrite(PVM pVM, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pCpu, PIOMMMIORANGE pRange, RTGCPHYS GCPhysFault)
232{
233 Assert(pRange->CTXALLSUFF(pfnWriteCallback) || !pRange->pfnWriteCallbackR3);
234
235 /*
236 * Get data to write from second parameter,
237 * and call the callback to write it.
238 */
239 unsigned cb = 0;
240 uint64_t u64Data = 0;
241 bool fRc = iomGetRegImmData(pCpu, &pCpu->param2, pRegFrame, &u64Data, &cb);
242 AssertMsg(fRc, ("Failed to get reg/imm port number!\n")); NOREF(fRc);
243
244 int rc = iomMMIODoWrite(pVM, pRange, GCPhysFault, &u64Data, cb);
245 if (rc == VINF_SUCCESS)
246 iomMMIOStatLength(pVM, cb);
247 return rc;
248}
249
250
251/** Wrapper for reading virtual memory. */
252DECLINLINE(int) iomRamRead(PVM pVM, void *pDest, RTGCPTR GCSrc, uint32_t cb)
253{
254#ifdef IN_GC
255 return MMGCRamReadNoTrapHandler(pDest, (void *)GCSrc, cb);
256#else
257 return PGMPhysReadGCPtrSafe(pVM, pDest, GCSrc, cb);
258#endif
259}
260
261
262/** Wrapper for writing virtual memory. */
263DECLINLINE(int) iomRamWrite(PVM pVM, RTGCPTR GCDest, void *pSrc, uint32_t cb)
264{
265#ifdef IN_GC
266 return MMGCRamWriteNoTrapHandler((void *)GCDest, pSrc, cb);
267#else
268 return PGMPhysWriteGCPtrSafe(pVM, GCDest, pSrc, cb);
269#endif
270}
271
272
273#ifdef iom_MOVS_SUPPORT
274/**
275 * [REP] MOVSB
276 * [REP] MOVSW
277 * [REP] MOVSD
278 *
279 * Restricted implementation.
280 *
281 *
282 * @returns VBox status code.
283 *
284 * @param pVM The virtual machine (GC pointer ofcourse).
285 * @param uErrorCode CPU Error code.
286 * @param pRegFrame Trap register frame.
287 * @param GCPhysFault The GC physical address corresponding to pvFault.
288 * @param pCpu Disassembler CPU state.
289 * @param pRange Pointer MMIO range.
290 */
291static int iomInterpretMOVS(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, PDISCPUSTATE pCpu, PIOMMMIORANGE pRange)
292{
293 /*
294 * We do not support segment prefixes or REPNE.
295 */
296 if (pCpu->prefix & (PREFIX_SEG | PREFIX_REPNE))
297 return VINF_IOM_HC_MMIO_READ_WRITE; /** @todo -> interpret whatever. */
298
299
300 /*
301 * Get bytes/words/dwords count to copy.
302 */
303 uint32_t cTransfers = 1;
304 if (pCpu->prefix & PREFIX_REP)
305 {
306 cTransfers = pRegFrame->ecx;
307 if (SELMGetCpuModeFromSelector(pVM, pRegFrame->eflags, pRegFrame->cs, &pRegFrame->csHid) == CPUMODE_16BIT)
308 cTransfers &= 0xffff;
309
310 if (!cTransfers)
311 return VINF_SUCCESS;
312 }
313
314 /* Get the current privilege level. */
315 uint32_t cpl = CPUMGetGuestCPL(pVM, pRegFrame);
316
317 /*
318 * Get data size.
319 */
320 unsigned cb = DISGetParamSize(pCpu, &pCpu->param1);
321 Assert(cb);
322 int offIncrement = pRegFrame->eflags.Bits.u1DF ? -(signed)cb : (signed)cb;
323
324#ifdef VBOX_WITH_STATISTICS
325 if (pVM->iom.s.cMovsMaxBytes < (cTransfers << SIZE_2_SHIFT(cb)))
326 pVM->iom.s.cMovsMaxBytes = cTransfers << SIZE_2_SHIFT(cb);
327#endif
328
329/** @todo re-evaluate on page boundraries. */
330
331 RTGCPHYS Phys = GCPhysFault;
332 int rc;
333 if (uErrorCode & X86_TRAP_PF_RW)
334 {
335 /*
336 * Write operation: [Mem] -> [MMIO]
337 * ds:esi (Virt Src) -> es:edi (Phys Dst)
338 */
339 STAM_PROFILE_START(&pVM->iom.s.StatGCInstMovsToMMIO, a2);
340
341 /* Check callback. */
342 if (!pRange->CTXALLSUFF(pfnWriteCallback))
343 {
344 STAM_PROFILE_STOP(&pVM->iom.s.StatGCInstMovsToMMIO, a2);
345 return VINF_IOM_HC_MMIO_WRITE;
346 }
347
348 /* Convert source address ds:esi. */
349 RTGCUINTPTR pu8Virt;
350 rc = SELMToFlatEx(pVM, DIS_SELREG_DS, pRegFrame, (RTGCPTR)pRegFrame->esi,
351 SELMTOFLAT_FLAGS_HYPER | SELMTOFLAT_FLAGS_NO_PL,
352 (PRTGCPTR)&pu8Virt);
353 if (VBOX_SUCCESS(rc))
354 {
355
356 /* Access verification first; we currently can't recover properly from traps inside this instruction */
357 rc = PGMVerifyAccess(pVM, pu8Virt, cTransfers * cb, (cpl == 3) ? X86_PTE_US : 0);
358 if (rc != VINF_SUCCESS)
359 {
360 Log(("MOVS will generate a trap -> recompiler, rc=%d\n", rc));
361 STAM_PROFILE_STOP(&pVM->iom.s.StatGCInstMovsToMMIO, a2);
362 return VINF_EM_RAW_EMULATE_INSTR;
363 }
364
365#ifdef IN_GC
366 MMGCRamRegisterTrapHandler(pVM);
367#endif
368
369 /* copy loop. */
370 while (cTransfers)
371 {
372 uint32_t u32Data = 0;
373 rc = iomRamRead(pVM, &u32Data, (RTGCPTR)pu8Virt, cb);
374 if (rc != VINF_SUCCESS)
375 break;
376 rc = iomMMIODoWrite(pVM, pRange, Phys, &u32Data, cb);
377 if (rc != VINF_SUCCESS)
378 break;
379
380 pu8Virt += offIncrement;
381 Phys += offIncrement;
382 pRegFrame->esi += offIncrement;
383 pRegFrame->edi += offIncrement;
384 cTransfers--;
385 }
386#ifdef IN_GC
387 MMGCRamDeregisterTrapHandler(pVM);
388#endif
389 /* Update ecx. */
390 if (pCpu->prefix & PREFIX_REP)
391 pRegFrame->ecx = cTransfers;
392 STAM_PROFILE_STOP(&pVM->iom.s.StatGCInstMovsToMMIO, a2);
393 }
394 else
395 rc = VINF_IOM_HC_MMIO_READ_WRITE;
396 }
397 else
398 {
399 /*
400 * Read operation: [MMIO] -> [mem] or [MMIO] -> [MMIO]
401 * ds:[eSI] (Phys Src) -> es:[eDI] (Virt Dst)
402 */
403 /* Check callback. */
404 if (!pRange->pfnReadCallback)
405 return VINF_IOM_HC_MMIO_READ;
406
407 /* Convert destination address. */
408 RTGCUINTPTR pu8Virt;
409 rc = SELMToFlatEx(pVM, DIS_SELREG_ES, pRegFrame, (RTGCPTR)pRegFrame->edi,
410 SELMTOFLAT_FLAGS_HYPER | SELMTOFLAT_FLAGS_NO_PL,
411 (RTGCPTR *)&pu8Virt);
412 if (VBOX_FAILURE(rc))
413 return VINF_EM_RAW_GUEST_TRAP;
414
415 /* Check if destination address is MMIO. */
416 PIOMMMIORANGE pMMIODst;
417 RTGCPHYS PhysDst;
418 rc = PGMGstGetPage(pVM, (RTGCPTR)pu8Virt, NULL, &PhysDst);
419 PhysDst |= (RTGCUINTPTR)pu8Virt & PAGE_OFFSET_MASK;
420 if ( VBOX_SUCCESS(rc)
421 && (pMMIODst = iomMMIOGetRange(&pVM->iom.s, PhysDst)))
422 {
423 /*
424 * Extra: [MMIO] -> [MMIO]
425 */
426 STAM_PROFILE_START(&pVM->iom.s.StatGCInstMovsMMIO, d);
427 STAM_PROFILE_START(&pVM->iom.s.StatGCInstMovsFromMMIO, c);
428
429 if (!pMMIODst->CTXALLSUFF(pfnWriteCallback) && pMMIODst->pfnWriteCallbackR3)
430 {
431 STAM_PROFILE_STOP(&pVM->iom.s.StatGCInstMovsMMIO, d);
432 STAM_PROFILE_STOP(&pVM->iom.s.StatGCInstMovsFromMMIO, c);
433 return VINF_IOM_HC_MMIO_READ_WRITE;
434 }
435
436 /* copy loop. */
437 while (cTransfers)
438 {
439 uint32_t u32Data;
440 rc = iomMMIODoRead(pVM, pRange, Phys, &u32Data, cb);
441 if (rc != VINF_SUCCESS)
442 break;
443 rc = iomMMIODoWrite(pVM, pMMIODst, PhysDst, &u32Data, cb);
444 if (rc != VINF_SUCCESS)
445 break;
446
447 Phys += offIncrement;
448 PhysDst += offIncrement;
449 pRegFrame->esi += offIncrement;
450 pRegFrame->edi += offIncrement;
451 cTransfers--;
452 }
453 STAM_PROFILE_STOP(&pVM->iom.s.StatGCInstMovsMMIO, d);
454 STAM_PROFILE_STOP(&pVM->iom.s.StatGCInstMovsFromMMIO, c);
455 }
456 else
457 {
458 /*
459 * Normal: [MMIO] -> [Mem]
460 */
461 STAM_PROFILE_START(&pVM->iom.s.StatGCInstMovsFromMMIO, c);
462
463 /* Access verification first; we currently can't recover properly from traps inside this instruction */
464 rc = PGMVerifyAccess(pVM, pu8Virt, cTransfers * cb, X86_PTE_RW | ((cpl == 3) ? X86_PTE_US : 0));
465 if (rc != VINF_SUCCESS)
466 {
467 Log(("MOVS will generate a trap -> recompiler, rc=%d\n", rc));
468 STAM_PROFILE_STOP(&pVM->iom.s.StatGCInstMovsFromMMIO, c);
469 return VINF_EM_RAW_EMULATE_INSTR;
470 }
471
472 /* copy loop. */
473#ifdef IN_GC
474 MMGCRamRegisterTrapHandler(pVM);
475#endif
476 while (cTransfers)
477 {
478 uint32_t u32Data;
479 rc = iomMMIODoRead(pVM, pRange, Phys, &u32Data, cb);
480 if (rc != VINF_SUCCESS)
481 break;
482 rc = iomRamWrite(pVM, (RTGCPTR)pu8Virt, &u32Data, cb);
483 if (rc != VINF_SUCCESS)
484 {
485 Log(("iomRamWrite %08X size=%d failed with %d\n", pu8Virt, cb, rc));
486 break;
487 }
488
489 pu8Virt += offIncrement;
490 Phys += offIncrement;
491 pRegFrame->esi += offIncrement;
492 pRegFrame->edi += offIncrement;
493 cTransfers--;
494 }
495#ifdef IN_GC
496 MMGCRamDeregisterTrapHandler(pVM);
497#endif
498 STAM_PROFILE_STOP(&pVM->iom.s.StatGCInstMovsFromMMIO, c);
499 }
500
501 /* Update ecx on exit. */
502 if (pCpu->prefix & PREFIX_REP)
503 pRegFrame->ecx = cTransfers;
504 }
505
506 /* work statistics. */
507 if (rc == VINF_SUCCESS)
508 {
509 iomMMIOStatLength(pVM, cb);
510 }
511 return rc;
512}
513#endif
514
515
516
517/**
518 * [REP] STOSB
519 * [REP] STOSW
520 * [REP] STOSD
521 *
522 * Restricted implementation.
523 *
524 *
525 * @returns VBox status code.
526 *
527 * @param pVM The virtual machine (GC pointer ofcourse).
528 * @param pRegFrame Trap register frame.
529 * @param GCPhysFault The GC physical address corresponding to pvFault.
530 * @param pCpu Disassembler CPU state.
531 * @param pRange Pointer MMIO range.
532 */
533static int iomInterpretSTOS(PVM pVM, PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, PDISCPUSTATE pCpu, PIOMMMIORANGE pRange)
534{
535 /*
536 * We do not support segment prefixes or REPNE..
537 */
538 if (pCpu->prefix & (PREFIX_SEG | PREFIX_REPNE))
539 return VINF_IOM_HC_MMIO_READ_WRITE; /** @todo -> REM instead of HC */
540
541 /*
542 * Get bytes/words/dwords count to copy.
543 */
544 uint32_t cTransfers = 1;
545 if (pCpu->prefix & PREFIX_REP)
546 {
547 cTransfers = pRegFrame->ecx;
548 if (SELMGetCpuModeFromSelector(pVM, pRegFrame->eflags, pRegFrame->cs, &pRegFrame->csHid) == CPUMODE_16BIT)
549 cTransfers &= 0xffff;
550
551 if (!cTransfers)
552 return VINF_SUCCESS;
553 }
554
555 /*
556 * Get data size.
557 */
558 unsigned cb = DISGetParamSize(pCpu, &pCpu->param1);
559 Assert(cb);
560 int offIncrement = pRegFrame->eflags.Bits.u1DF ? -(signed)cb : (signed)cb;
561
562#ifdef VBOX_WITH_STATISTICS
563 if (pVM->iom.s.cStosMaxBytes < (cTransfers << SIZE_2_SHIFT(cb)))
564 pVM->iom.s.cStosMaxBytes = cTransfers << SIZE_2_SHIFT(cb);
565#endif
566
567
568 RTGCPHYS Phys = GCPhysFault;
569 uint32_t u32Data = pRegFrame->eax;
570 int rc;
571 if (pRange->CTXALLSUFF(pfnFillCallback))
572 {
573 /*
574 * Use the fill callback.
575 */
576 /** @todo pfnFillCallback must return number of bytes successfully written!!! */
577 if (offIncrement > 0)
578 {
579 /* addr++ variant. */
580 rc = pRange->CTXALLSUFF(pfnFillCallback)(pRange->CTXALLSUFF(pDevIns), pRange->CTXALLSUFF(pvUser), Phys, u32Data, cb, cTransfers);
581 if (rc == VINF_SUCCESS)
582 {
583 /* Update registers. */
584 pRegFrame->edi += cTransfers << SIZE_2_SHIFT(cb);
585 if (pCpu->prefix & PREFIX_REP)
586 pRegFrame->ecx = 0;
587 }
588 }
589 else
590 {
591 /* addr-- variant. */
592 rc = pRange->CTXALLSUFF(pfnFillCallback)(pRange->CTXALLSUFF(pDevIns), pRange->CTXALLSUFF(pvUser), (Phys - (cTransfers - 1)) << SIZE_2_SHIFT(cb), u32Data, cb, cTransfers);
593 if (rc == VINF_SUCCESS)
594 {
595 /* Update registers. */
596 pRegFrame->edi -= cTransfers << SIZE_2_SHIFT(cb);
597 if (pCpu->prefix & PREFIX_REP)
598 pRegFrame->ecx = 0;
599 }
600 }
601 }
602 else
603 {
604 /*
605 * Use the write callback.
606 */
607 Assert(pRange->CTXALLSUFF(pfnWriteCallback) || !pRange->pfnWriteCallbackR3);
608
609 /* fill loop. */
610 do
611 {
612 rc = iomMMIODoWrite(pVM, pRange, Phys, &u32Data, cb);
613 if (rc != VINF_SUCCESS)
614 break;
615
616 Phys += offIncrement;
617 pRegFrame->edi += offIncrement;
618 cTransfers--;
619 } while (cTransfers);
620
621 /* Update ecx on exit. */
622 if (pCpu->prefix & PREFIX_REP)
623 pRegFrame->ecx = cTransfers;
624 }
625
626 /*
627 * Work statistics and return.
628 */
629 if (rc == VINF_SUCCESS)
630 iomMMIOStatLength(pVM, cb);
631 return rc;
632}
633
634
635/**
636 * [REP] LODSB
637 * [REP] LODSW
638 * [REP] LODSD
639 *
640 * Restricted implementation.
641 *
642 *
643 * @returns VBox status code.
644 *
645 * @param pVM The virtual machine (GC pointer ofcourse).
646 * @param pRegFrame Trap register frame.
647 * @param GCPhysFault The GC physical address corresponding to pvFault.
648 * @param pCpu Disassembler CPU state.
649 * @param pRange Pointer MMIO range.
650 */
651static int iomInterpretLODS(PVM pVM, PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, PDISCPUSTATE pCpu, PIOMMMIORANGE pRange)
652{
653 Assert(pRange->CTXALLSUFF(pfnReadCallback) || !pRange->pfnReadCallbackR3);
654
655 /*
656 * We do not support segment prefixes or REP*.
657 */
658 if (pCpu->prefix & (PREFIX_SEG | PREFIX_REP | PREFIX_REPNE))
659 return VINF_IOM_HC_MMIO_READ_WRITE; /** @todo -> REM instead of HC */
660
661 /*
662 * Get data size.
663 */
664 unsigned cb = DISGetParamSize(pCpu, &pCpu->param2);
665 Assert(cb);
666 int offIncrement = pRegFrame->eflags.Bits.u1DF ? -(signed)cb : (signed)cb;
667
668 /*
669 * Perform read.
670 */
671 int rc = iomMMIODoRead(pVM, pRange, GCPhysFault, &pRegFrame->eax, cb);
672 if (rc == VINF_SUCCESS)
673 pRegFrame->esi += offIncrement;
674
675 /*
676 * Work statistics and return.
677 */
678 if (rc == VINF_SUCCESS)
679 iomMMIOStatLength(pVM, cb);
680 return rc;
681}
682
683
684/**
685 * CMP [MMIO], reg|imm
686 * CMP reg|imm, [MMIO]
687 *
688 * Restricted implementation.
689 *
690 *
691 * @returns VBox status code.
692 *
693 * @param pVM The virtual machine (GC pointer ofcourse).
694 * @param pRegFrame Trap register frame.
695 * @param GCPhysFault The GC physical address corresponding to pvFault.
696 * @param pCpu Disassembler CPU state.
697 * @param pRange Pointer MMIO range.
698 */
699static int iomInterpretCMP(PVM pVM, PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, PDISCPUSTATE pCpu, PIOMMMIORANGE pRange)
700{
701 Assert(pRange->CTXALLSUFF(pfnReadCallback) || !pRange->pfnReadCallbackR3);
702
703 /*
704 * Get the operands.
705 */
706 unsigned cb = 0;
707 uint64_t uData1;
708 uint64_t uData2;
709 int rc;
710 if (iomGetRegImmData(pCpu, &pCpu->param1, pRegFrame, &uData1, &cb))
711 /* cmp reg, [MMIO]. */
712 rc = iomMMIODoRead(pVM, pRange, GCPhysFault, &uData2, cb);
713 else if (iomGetRegImmData(pCpu, &pCpu->param2, pRegFrame, &uData2, &cb))
714 /* cmp [MMIO], reg|imm. */
715 rc = iomMMIODoRead(pVM, pRange, GCPhysFault, &uData1, cb);
716 else
717 {
718 AssertMsgFailed(("Disassember CMP problem..\n"));
719 rc = VERR_IOM_MMIO_HANDLER_DISASM_ERROR;
720 }
721
722 if (rc == VINF_SUCCESS)
723 {
724 /* Emulate CMP and update guest flags. */
725 uint32_t eflags = EMEmulateCmp(uData1, uData2, cb);
726 pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF))
727 | (eflags & (X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF));
728 iomMMIOStatLength(pVM, cb);
729 }
730
731 return rc;
732}
733
734
735/**
736 * AND [MMIO], reg|imm
737 * AND reg, [MMIO]
738 *
739 * Restricted implementation.
740 *
741 *
742 * @returns VBox status code.
743 *
744 * @param pVM The virtual machine (GC pointer ofcourse).
745 * @param pRegFrame Trap register frame.
746 * @param GCPhysFault The GC physical address corresponding to pvFault.
747 * @param pCpu Disassembler CPU state.
748 * @param pRange Pointer MMIO range.
749 */
750static int iomInterpretAND(PVM pVM, PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, PDISCPUSTATE pCpu, PIOMMMIORANGE pRange)
751{
752 unsigned cb = 0;
753 uint64_t uData1;
754 uint64_t uData2;
755 bool fAndWrite;
756 int rc;
757 if (iomGetRegImmData(pCpu, &pCpu->param1, pRegFrame, &uData1, &cb))
758 {
759 /* and reg, [MMIO]. */
760 Assert(pRange->CTXALLSUFF(pfnReadCallback) || !pRange->pfnReadCallbackR3);
761 fAndWrite = false;
762 rc = iomMMIODoRead(pVM, pRange, GCPhysFault, &uData2, cb);
763 }
764 else if (iomGetRegImmData(pCpu, &pCpu->param2, pRegFrame, &uData2, &cb))
765 {
766 /* and [MMIO], reg|imm. */
767 fAndWrite = true;
768 if ( (pRange->CTXALLSUFF(pfnReadCallback) || !pRange->pfnReadCallbackR3)
769 && (pRange->CTXALLSUFF(pfnWriteCallback) || !pRange->pfnWriteCallbackR3))
770 rc = iomMMIODoRead(pVM, pRange, GCPhysFault, &uData1, cb);
771 else
772 rc = VINF_IOM_HC_MMIO_READ_WRITE;
773 }
774 else
775 {
776 AssertMsgFailed(("Disassember AND problem..\n"));
777 return VERR_IOM_MMIO_HANDLER_DISASM_ERROR;
778 }
779
780 if (rc == VINF_SUCCESS)
781 {
782 /* Emulate AND and update guest flags. */
783 uint32_t eflags = EMEmulateAnd((uint32_t *)&uData1, uData2, cb);
784 if (fAndWrite)
785 /* Store result to MMIO. */
786 rc = iomMMIODoWrite(pVM, pRange, GCPhysFault, &uData1, cb);
787 else
788 {
789 /* Store result to register. */
790 bool fRc = iomSaveDataToReg(pCpu, &pCpu->param1, pRegFrame, uData1);
791 AssertMsg(fRc, ("Failed to store register value!\n")); NOREF(fRc);
792 }
793 if (rc == VINF_SUCCESS)
794 {
795 /* Update guest's eflags and finish. */
796 pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF))
797 | (eflags & (X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF));
798 iomMMIOStatLength(pVM, cb);
799 }
800 }
801
802 return rc;
803}
804
805
806
807/**
808 * TEST [MMIO], reg|imm
809 * TEST reg, [MMIO]
810 *
811 * Restricted implementation.
812 *
813 *
814 * @returns VBox status code.
815 *
816 * @param pVM The virtual machine (GC pointer ofcourse).
817 * @param pRegFrame Trap register frame.
818 * @param GCPhysFault The GC physical address corresponding to pvFault.
819 * @param pCpu Disassembler CPU state.
820 * @param pRange Pointer MMIO range.
821 */
822static int iomInterpretTEST(PVM pVM, PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, PDISCPUSTATE pCpu, PIOMMMIORANGE pRange)
823{
824 Assert(pRange->CTXALLSUFF(pfnReadCallback) || !pRange->pfnReadCallbackR3);
825
826 unsigned cb = 0;
827 uint64_t uData1;
828 uint64_t uData2;
829 int rc;
830
831 if (iomGetRegImmData(pCpu, &pCpu->param1, pRegFrame, &uData1, &cb))
832 {
833 /* and test, [MMIO]. */
834 rc = iomMMIODoRead(pVM, pRange, GCPhysFault, &uData2, cb);
835 }
836 else if (iomGetRegImmData(pCpu, &pCpu->param2, pRegFrame, &uData2, &cb))
837 {
838 /* test [MMIO], reg|imm. */
839 rc = iomMMIODoRead(pVM, pRange, GCPhysFault, &uData1, cb);
840 }
841 else
842 {
843 AssertMsgFailed(("Disassember TEST problem..\n"));
844 return VERR_IOM_MMIO_HANDLER_DISASM_ERROR;
845 }
846
847 if (rc == VINF_SUCCESS)
848 {
849 /* Emulate TEST (=AND without write back) and update guest EFLAGS. */
850 uint32_t eflags = EMEmulateAnd((uint32_t *)&uData1, uData2, cb);
851 pRegFrame->eflags.u32 = (pRegFrame->eflags.u32 & ~(X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF))
852 | (eflags & (X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF));
853 iomMMIOStatLength(pVM, cb);
854 }
855
856 return rc;
857}
858
859/**
860 * XCHG [MMIO], reg
861 * XCHG reg, [MMIO]
862 *
863 * Restricted implementation.
864 *
865 *
866 * @returns VBox status code.
867 *
868 * @param pVM The virtual machine (GC pointer ofcourse).
869 * @param pRegFrame Trap register frame.
870 * @param GCPhysFault The GC physical address corresponding to pvFault.
871 * @param pCpu Disassembler CPU state.
872 * @param pRange Pointer MMIO range.
873 */
874static int iomInterpretXCHG(PVM pVM, PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, PDISCPUSTATE pCpu, PIOMMMIORANGE pRange)
875{
876 /* Check for read & write handlers since IOMMMIOHandler doesn't cover this. */
877 if ( (!pRange->CTXALLSUFF(pfnReadCallback) && pRange->pfnReadCallbackR3)
878 || (!pRange->CTXALLSUFF(pfnWriteCallback) && pRange->pfnWriteCallbackR3))
879 return VINF_IOM_HC_MMIO_READ_WRITE;
880
881 int rc;
882 unsigned cb = 0;
883 uint64_t uData1;
884 uint64_t uData2;
885 if (iomGetRegImmData(pCpu, &pCpu->param1, pRegFrame, &uData1, &cb))
886 {
887 /* xchg reg, [MMIO]. */
888 rc = iomMMIODoRead(pVM, pRange, GCPhysFault, &uData2, cb);
889 if (rc == VINF_SUCCESS)
890 {
891 /* Store result to MMIO. */
892 rc = iomMMIODoWrite(pVM, pRange, GCPhysFault, &uData1, cb);
893
894 if (rc == VINF_SUCCESS)
895 {
896 /* Store result to register. */
897 bool fRc = iomSaveDataToReg(pCpu, &pCpu->param1, pRegFrame, uData2);
898 AssertMsg(fRc, ("Failed to store register value!\n")); NOREF(fRc);
899 }
900 else
901 Assert(rc == VINF_IOM_HC_MMIO_WRITE || rc == VINF_PATM_HC_MMIO_PATCH_WRITE);
902 }
903 else
904 Assert(rc == VINF_IOM_HC_MMIO_READ || rc == VINF_PATM_HC_MMIO_PATCH_READ);
905 }
906 else if (iomGetRegImmData(pCpu, &pCpu->param2, pRegFrame, &uData2, &cb))
907 {
908 /* xchg [MMIO], reg. */
909 rc = iomMMIODoRead(pVM, pRange, GCPhysFault, &uData1, cb);
910 if (rc == VINF_SUCCESS)
911 {
912 /* Store result to MMIO. */
913 rc = iomMMIODoWrite(pVM, pRange, GCPhysFault, &uData2, cb);
914 if (rc == VINF_SUCCESS)
915 {
916 /* Store result to register. */
917 bool fRc = iomSaveDataToReg(pCpu, &pCpu->param2, pRegFrame, uData1);
918 AssertMsg(fRc, ("Failed to store register value!\n")); NOREF(fRc);
919 }
920 else
921 Assert(rc == VINF_IOM_HC_MMIO_WRITE || rc == VINF_PATM_HC_MMIO_PATCH_WRITE);
922 }
923 else
924 Assert(rc == VINF_IOM_HC_MMIO_READ || rc == VINF_PATM_HC_MMIO_PATCH_READ);
925 }
926 else
927 {
928 AssertMsgFailed(("Disassember XCHG problem..\n"));
929 rc = VERR_IOM_MMIO_HANDLER_DISASM_ERROR;
930 }
931 return rc;
932}
933
934
935/**
936 * \#PF Handler callback for MMIO ranges.
937 * Note: we are on ring0 in Hypervisor and interrupts are disabled.
938 *
939 * @returns VBox status code (appropriate for GC return).
940 * @param pVM VM Handle.
941 * @param uErrorCode CPU Error code.
942 * @param pCtxCore Trap register frame.
943 * @param pvFault The fault address (cr2).
944 * @param GCPhysFault The GC physical address corresponding to pvFault.
945 * @param pvUser Pointer to the MMIO ring-3 range entry.
946 */
947IOMDECL(int) IOMMMIOHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pCtxCore, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser)
948{
949 STAM_PROFILE_START(&pVM->iom.s.StatGCMMIOHandler, a);
950 Log3(("IOMMMIOHandler: GCPhys=%RGp uErr=%#x pvFault=%VGv eip=%VGv\n",
951 GCPhysFault, (uint32_t)uErrorCode, pvFault, pCtxCore->rip));
952
953 PIOMMMIORANGE pRange = (PIOMMMIORANGE)pvUser;
954 Assert(pRange);
955 Assert(pRange == iomMMIOGetRange(&pVM->iom.s, GCPhysFault));
956
957#ifdef VBOX_WITH_STATISTICS
958 /*
959 * Locate the statistics, if > PAGE_SIZE we'll use the first byte for everything.
960 */
961 PIOMMMIOSTATS pStats = iomMMIOGetStats(&pVM->iom.s, GCPhysFault, pRange);
962 if (!pStats)
963 {
964# ifdef IN_RING3
965 return VERR_NO_MEMORY;
966# else
967 STAM_PROFILE_STOP(&pVM->iom.s.StatGCMMIOHandler, a);
968 STAM_COUNTER_INC(&pVM->iom.s.StatGCMMIOFailures);
969 return uErrorCode & X86_TRAP_PF_RW ? VINF_IOM_HC_MMIO_WRITE : VINF_IOM_HC_MMIO_READ;
970# endif
971 }
972#endif
973
974#ifndef IN_RING3
975 /*
976 * Should we defer the request right away?
977 */
978 if (uErrorCode & X86_TRAP_PF_RW
979 ? !pRange->CTXALLSUFF(pfnWriteCallback) && pRange->pfnWriteCallbackR3
980 : !pRange->CTXALLSUFF(pfnReadCallback) && pRange->pfnReadCallbackR3)
981 {
982# ifdef VBOX_WITH_STATISTICS
983 if (uErrorCode & X86_TRAP_PF_RW)
984 STAM_COUNTER_INC(&pStats->CTXALLMID(Write,ToR3));
985 else
986 STAM_COUNTER_INC(&pStats->CTXALLMID(Read,ToR3));
987# endif
988
989 STAM_PROFILE_STOP(&pVM->iom.s.StatGCMMIOHandler, a);
990 STAM_COUNTER_INC(&pVM->iom.s.StatGCMMIOFailures);
991 return uErrorCode & X86_TRAP_PF_RW ? VINF_IOM_HC_MMIO_WRITE : VINF_IOM_HC_MMIO_READ;
992 }
993#endif /* !IN_RING3 */
994
995 /*
996 * Disassemble the instruction and interprete it.
997 */
998 DISCPUSTATE Cpu;
999 unsigned cbOp;
1000 int rc = EMInterpretDisasOne(pVM, pCtxCore, &Cpu, &cbOp);
1001 AssertRCReturn(rc, rc);
1002 switch (Cpu.pCurInstr->opcode)
1003 {
1004 case OP_MOV:
1005 case OP_MOVZX:
1006 case OP_MOVSX:
1007 {
1008 STAM_PROFILE_START(&pVM->iom.s.StatGCInstMov, b);
1009 if (uErrorCode & X86_TRAP_PF_RW)
1010 rc = iomInterpretMOVxXWrite(pVM, pCtxCore, &Cpu, pRange, GCPhysFault);
1011 else
1012 rc = iomInterpretMOVxXRead(pVM, pCtxCore, &Cpu, pRange, GCPhysFault);
1013 STAM_PROFILE_STOP(&pVM->iom.s.StatGCInstMov, b);
1014 break;
1015 }
1016
1017
1018#ifdef iom_MOVS_SUPPORT
1019 case OP_MOVSB:
1020 case OP_MOVSWD:
1021 STAM_PROFILE_START(&pVM->iom.s.StatGCInstMovs, c);
1022 rc = iomInterpretMOVS(pVM, uErrorCode, pCtxCore, GCPhysFault, &Cpu, pRange);
1023 STAM_PROFILE_STOP(&pVM->iom.s.StatGCInstMovs, c);
1024 break;
1025#endif
1026
1027 case OP_STOSB:
1028 case OP_STOSWD:
1029 Assert(uErrorCode & X86_TRAP_PF_RW);
1030 STAM_PROFILE_START(&pVM->iom.s.StatGCInstStos, d);
1031 rc = iomInterpretSTOS(pVM, pCtxCore, GCPhysFault, &Cpu, pRange);
1032 STAM_PROFILE_STOP(&pVM->iom.s.StatGCInstStos, d);
1033 break;
1034
1035 case OP_LODSB:
1036 case OP_LODSWD:
1037 Assert(!(uErrorCode & X86_TRAP_PF_RW));
1038 STAM_PROFILE_START(&pVM->iom.s.StatGCInstLods, e);
1039 rc = iomInterpretLODS(pVM, pCtxCore, GCPhysFault, &Cpu, pRange);
1040 STAM_PROFILE_STOP(&pVM->iom.s.StatGCInstLods, e);
1041 break;
1042
1043 case OP_CMP:
1044 Assert(!(uErrorCode & X86_TRAP_PF_RW));
1045 STAM_PROFILE_START(&pVM->iom.s.StatGCInstCmp, f);
1046 rc = iomInterpretCMP(pVM, pCtxCore, GCPhysFault, &Cpu, pRange);
1047 STAM_PROFILE_STOP(&pVM->iom.s.StatGCInstCmp, f);
1048 break;
1049
1050 case OP_AND:
1051 STAM_PROFILE_START(&pVM->iom.s.StatGCInstAnd, g);
1052 rc = iomInterpretAND(pVM, pCtxCore, GCPhysFault, &Cpu, pRange);
1053 STAM_PROFILE_STOP(&pVM->iom.s.StatGCInstAnd, g);
1054 break;
1055
1056 case OP_TEST:
1057 Assert(!(uErrorCode & X86_TRAP_PF_RW));
1058 STAM_PROFILE_START(&pVM->iom.s.StatGCInstTest, h);
1059 rc = iomInterpretTEST(pVM, pCtxCore, GCPhysFault, &Cpu, pRange);
1060 STAM_PROFILE_STOP(&pVM->iom.s.StatGCInstTest, h);
1061 break;
1062
1063 case OP_XCHG:
1064 STAM_PROFILE_START(&pVM->iom.s.StatGCInstXchg, i);
1065 rc = iomInterpretXCHG(pVM, pCtxCore, GCPhysFault, &Cpu, pRange);
1066 STAM_PROFILE_STOP(&pVM->iom.s.StatGCInstXchg, i);
1067 break;
1068
1069
1070 /*
1071 * The instruction isn't supported. Hand it on to ring-3.
1072 */
1073 default:
1074 STAM_COUNTER_INC(&pVM->iom.s.StatGCInstOther);
1075 rc = (uErrorCode & X86_TRAP_PF_RW) ? VINF_IOM_HC_MMIO_WRITE : VINF_IOM_HC_MMIO_READ;
1076 break;
1077 }
1078
1079 /*
1080 * On success advance EIP.
1081 */
1082 if (rc == VINF_SUCCESS)
1083 pCtxCore->rip += cbOp;
1084 else
1085 {
1086 STAM_COUNTER_INC(&pVM->iom.s.StatGCMMIOFailures);
1087#if defined(VBOX_WITH_STATISTICS) && !defined(IN_RING3)
1088 switch (rc)
1089 {
1090 case VINF_IOM_HC_MMIO_READ:
1091 case VINF_IOM_HC_MMIO_READ_WRITE:
1092 STAM_COUNTER_INC(&pStats->CTXALLMID(Read,ToR3));
1093 break;
1094 case VINF_IOM_HC_MMIO_WRITE:
1095 STAM_COUNTER_INC(&pStats->CTXALLMID(Write,ToR3));
1096 break;
1097 }
1098#endif
1099 }
1100
1101 STAM_PROFILE_STOP(&pVM->iom.s.StatGCMMIOHandler, a);
1102 return rc;
1103}
1104
1105
1106/**
1107 * Reads a MMIO register.
1108 *
1109 * @returns VBox status code.
1110 *
1111 * @param pVM VM handle.
1112 * @param GCPhys The physical address to read.
1113 * @param pu32Value Where to store the value read.
1114 * @param cbValue The size of the register to read in bytes. 1, 2 or 4 bytes.
1115 */
1116IOMDECL(int) IOMMMIORead(PVM pVM, RTGCPHYS GCPhys, uint32_t *pu32Value, size_t cbValue)
1117{
1118 /*
1119 * Lookup the current context range node and statistics.
1120 */
1121 PIOMMMIORANGE pRange = iomMMIOGetRange(&pVM->iom.s, GCPhys);
1122 AssertMsgReturn(pRange,
1123 ("Handlers and page tables are out of sync or something! GCPhys=%VGp cbValue=%d\n", GCPhys, cbValue),
1124 VERR_INTERNAL_ERROR);
1125#ifdef VBOX_WITH_STATISTICS
1126 PIOMMMIOSTATS pStats = iomMMIOGetStats(&pVM->iom.s, GCPhys, pRange);
1127 if (!pStats)
1128# ifdef IN_RING3
1129 return VERR_NO_MEMORY;
1130# else
1131 return VINF_IOM_HC_MMIO_READ;
1132# endif
1133#endif /* VBOX_WITH_STATISTICS */
1134 if (pRange->CTXALLSUFF(pfnReadCallback))
1135 {
1136 /*
1137 * Perform the read and deal with the result.
1138 */
1139#ifdef VBOX_WITH_STATISTICS
1140 if (pStats)
1141 STAM_PROFILE_ADV_START(&pStats->CTXALLSUFF(ProfRead), a);
1142#endif
1143 int rc = pRange->CTXALLSUFF(pfnReadCallback)(pRange->CTXALLSUFF(pDevIns), pRange->CTXALLSUFF(pvUser), GCPhys, pu32Value, cbValue);
1144#ifdef VBOX_WITH_STATISTICS
1145 if (pStats)
1146 STAM_PROFILE_ADV_STOP(&pStats->CTXALLSUFF(ProfRead), a);
1147 if (pStats && rc != VINF_IOM_HC_MMIO_READ)
1148 STAM_COUNTER_INC(&pStats->CTXALLSUFF(Read));
1149#endif
1150 switch (rc)
1151 {
1152 case VINF_SUCCESS:
1153 default:
1154 Log4(("IOMMMIORead: GCPhys=%RGp *pu32=%08RX32 cb=%d rc=%Vrc\n", GCPhys, *pu32Value, cbValue, rc));
1155 return rc;
1156
1157 case VINF_IOM_MMIO_UNUSED_00:
1158 switch (cbValue)
1159 {
1160 case 1: *(uint8_t *)pu32Value = UINT8_C(0x00); break;
1161 case 2: *(uint16_t *)pu32Value = UINT16_C(0x0000); break;
1162 case 4: *(uint32_t *)pu32Value = UINT32_C(0x00000000); break;
1163 case 8: *(uint64_t *)pu32Value = UINT64_C(0x0000000000000000); break;
1164 default: AssertReleaseMsgFailed(("cbValue=%d GCPhys=%VGp\n", cbValue, GCPhys)); break;
1165 }
1166 Log4(("IOMMMIORead: GCPhys=%RGp *pu32=%08RX32 cb=%d rc=%Vrc\n", GCPhys, *pu32Value, cbValue, rc));
1167 return VINF_SUCCESS;
1168
1169 case VINF_IOM_MMIO_UNUSED_FF:
1170 switch (cbValue)
1171 {
1172 case 1: *(uint8_t *)pu32Value = UINT8_C(0xff); break;
1173 case 2: *(uint16_t *)pu32Value = UINT16_C(0xffff); break;
1174 case 4: *(uint32_t *)pu32Value = UINT32_C(0xffffffff); break;
1175 case 8: *(uint64_t *)pu32Value = UINT64_C(0xffffffffffffffff); break;
1176 default: AssertReleaseMsgFailed(("cbValue=%d GCPhys=%VGp\n", cbValue, GCPhys)); break;
1177 }
1178 Log4(("IOMMMIORead: GCPhys=%RGp *pu32=%08RX32 cb=%d rc=%Vrc\n", GCPhys, *pu32Value, cbValue, rc));
1179 return VINF_SUCCESS;
1180 }
1181 }
1182#ifndef IN_RING3
1183 if (pRange->pfnReadCallbackR3)
1184 {
1185 STAM_COUNTER_INC(&pStats->CTXALLMID(Read,ToR3));
1186 return VINF_IOM_HC_MMIO_READ;
1187 }
1188#endif
1189
1190 /*
1191 * Lookup the ring-3 range.
1192 */
1193#ifdef VBOX_WITH_STATISTICS
1194 if (pStats)
1195 STAM_COUNTER_INC(&pStats->CTXALLSUFF(Read));
1196#endif
1197 /* Unassigned memory; this is actually not supposed to happen. */
1198 switch (cbValue)
1199 {
1200 case 1: *(uint8_t *)pu32Value = UINT8_C(0xff); break;
1201 case 2: *(uint16_t *)pu32Value = UINT16_C(0xffff); break;
1202 case 4: *(uint32_t *)pu32Value = UINT32_C(0xffffffff); break;
1203 case 8: *(uint64_t *)pu32Value = UINT64_C(0xffffffffffffffff); break;
1204 default: AssertReleaseMsgFailed(("cbValue=%d GCPhys=%VGp\n", cbValue, GCPhys)); break;
1205 }
1206 Log4(("IOMMMIORead: GCPhys=%RGp *pu32=%08RX32 cb=%d rc=VINF_SUCCESS\n", GCPhys, *pu32Value, cbValue));
1207 return VINF_SUCCESS;
1208}
1209
1210
1211/**
1212 * Writes to a MMIO register.
1213 *
1214 * @returns VBox status code.
1215 *
1216 * @param pVM VM handle.
1217 * @param GCPhys The physical address to write to.
1218 * @param u32Value The value to write.
1219 * @param cbValue The size of the register to read in bytes. 1, 2 or 4 bytes.
1220 */
1221IOMDECL(int) IOMMMIOWrite(PVM pVM, RTGCPHYS GCPhys, uint32_t u32Value, size_t cbValue)
1222{
1223 /*
1224 * Lookup the current context range node.
1225 */
1226 PIOMMMIORANGE pRange = iomMMIOGetRange(&pVM->iom.s, GCPhys);
1227 AssertMsgReturn(pRange,
1228 ("Handlers and page tables are out of sync or something! GCPhys=%VGp cbValue=%d\n", GCPhys, cbValue),
1229 VERR_INTERNAL_ERROR);
1230#ifdef VBOX_WITH_STATISTICS
1231 PIOMMMIOSTATS pStats = iomMMIOGetStats(&pVM->iom.s, GCPhys, pRange);
1232 if (!pStats)
1233# ifdef IN_RING3
1234 return VERR_NO_MEMORY;
1235# else
1236 return VINF_IOM_HC_MMIO_WRITE;
1237# endif
1238#endif /* VBOX_WITH_STATISTICS */
1239
1240 /*
1241 * Perform the write if there's a write handler. R0/GC may have
1242 * to defer it to ring-3.
1243 */
1244 if (pRange->CTXALLSUFF(pfnWriteCallback))
1245 {
1246#ifdef VBOX_WITH_STATISTICS
1247 if (pStats)
1248 STAM_PROFILE_ADV_START(&pStats->CTXALLSUFF(ProfWrite), a);
1249#endif
1250 int rc = pRange->CTXALLSUFF(pfnWriteCallback)(pRange->CTXALLSUFF(pDevIns), pRange->CTXALLSUFF(pvUser), GCPhys, &u32Value, cbValue);
1251#ifdef VBOX_WITH_STATISTICS
1252 if (pStats)
1253 STAM_PROFILE_ADV_STOP(&pStats->CTXALLSUFF(ProfWrite), a);
1254 if (pStats && rc != VINF_IOM_HC_MMIO_WRITE)
1255 STAM_COUNTER_INC(&pStats->CTXALLSUFF(Write));
1256#endif
1257 Log4(("IOMMMIOWrite: GCPhys=%RGp u32=%08RX32 cb=%d rc=%Vrc\n", GCPhys, u32Value, cbValue, rc));
1258 return rc;
1259 }
1260#ifndef IN_RING3
1261 if (pRange->pfnWriteCallbackR3)
1262 {
1263 STAM_COUNTER_INC(&pStats->CTXALLMID(Write,ToR3));
1264 return VINF_IOM_HC_MMIO_WRITE;
1265 }
1266#endif
1267
1268 /*
1269 * No write handler, nothing to do.
1270 */
1271#ifdef VBOX_WITH_STATISTICS
1272 if (pStats)
1273 STAM_COUNTER_INC(&pStats->CTXALLSUFF(Write));
1274#endif
1275 Log4(("IOMMMIOWrite: GCPhys=%RGp u32=%08RX32 cb=%d rc=%Vrc\n", GCPhys, u32Value, cbValue, VINF_SUCCESS));
1276 return VINF_SUCCESS;
1277}
1278
1279
1280/**
1281 * [REP*] INSB/INSW/INSD
1282 * ES:EDI,DX[,ECX]
1283 *
1284 * @remark Assumes caller checked the access privileges (IOMInterpretCheckPortIOAccess)
1285 *
1286 * @returns Strict VBox status code. Informational status codes other than the one documented
1287 * here are to be treated as internal failure. Use IOM_SUCCESS() to check for success.
1288 * @retval VINF_SUCCESS Success.
1289 * @retval VINF_EM_FIRST-VINF_EM_LAST Success with some exceptions (see IOM_SUCCESS()), the
1290 * status code must be passed on to EM.
1291 * @retval VINF_IOM_HC_IOPORT_READ Defer the read to ring-3. (R0/GC only)
1292 * @retval VINF_EM_RAW_EMULATE_INSTR Defer the read to the REM.
1293 * @retval VINF_EM_RAW_GUEST_TRAP The exception was left pending. (TRPMRaiseXcptErr)
1294 * @retval VINF_TRPM_XCPT_DISPATCHED The exception was raised and dispatched for raw-mode execution. (TRPMRaiseXcptErr)
1295 * @retval VINF_EM_RESCHEDULE_REM The exception was dispatched and cannot be executed in raw-mode. (TRPMRaiseXcptErr)
1296 *
1297 * @param pVM The virtual machine (GC pointer ofcourse).
1298 * @param pRegFrame Pointer to CPUMCTXCORE guest registers structure.
1299 * @param uPort IO Port
1300 * @param uPrefix IO instruction prefix
1301 * @param cbTransfer Size of transfer unit
1302 */
1303IOMDECL(int) IOMInterpretINSEx(PVM pVM, PCPUMCTXCORE pRegFrame, uint32_t uPort, uint32_t uPrefix, uint32_t cbTransfer)
1304{
1305#ifdef VBOX_WITH_STATISTICS
1306 STAM_COUNTER_INC(&pVM->iom.s.StatGCInstIns);
1307#endif
1308
1309 /*
1310 * We do not support REPNE or decrementing destination
1311 * pointer. Segment prefixes are deliberately ignored, as per the instruction specification.
1312 */
1313 if ( (uPrefix & PREFIX_REPNE)
1314 || pRegFrame->eflags.Bits.u1DF)
1315 return VINF_EM_RAW_EMULATE_INSTR;
1316
1317 /*
1318 * Get bytes/words/dwords count to transfer.
1319 */
1320 RTGCUINTREG cTransfers = 1;
1321 if (uPrefix & PREFIX_REP)
1322 {
1323 cTransfers = pRegFrame->ecx;
1324
1325 if (SELMGetCpuModeFromSelector(pVM, pRegFrame->eflags, pRegFrame->cs, &pRegFrame->csHid) == CPUMODE_16BIT)
1326 cTransfers &= 0xffff;
1327
1328 if (!cTransfers)
1329 return VINF_SUCCESS;
1330 }
1331
1332 /* Convert destination address es:edi. */
1333 RTGCPTR GCPtrDst;
1334 int rc = SELMToFlatEx(pVM, DIS_SELREG_ES, pRegFrame, (RTGCPTR)pRegFrame->edi,
1335 SELMTOFLAT_FLAGS_HYPER | SELMTOFLAT_FLAGS_NO_PL,
1336 &GCPtrDst);
1337 if (VBOX_FAILURE(rc))
1338 {
1339 Log(("INS destination address conversion failed -> fallback, rc=%d\n", rc));
1340 return VINF_EM_RAW_EMULATE_INSTR;
1341 }
1342
1343 /* Access verification first; we can't recover from traps inside this instruction, as the port read cannot be repeated. */
1344 uint32_t cpl = CPUMGetGuestCPL(pVM, pRegFrame);
1345
1346 rc = PGMVerifyAccess(pVM, (RTGCUINTPTR)GCPtrDst, cTransfers * cbTransfer,
1347 X86_PTE_RW | ((cpl == 3) ? X86_PTE_US : 0));
1348 if (rc != VINF_SUCCESS)
1349 {
1350 Log(("INS will generate a trap -> fallback, rc=%d\n", rc));
1351 return VINF_EM_RAW_EMULATE_INSTR;
1352 }
1353
1354 Log(("IOM: rep ins%d port %#x count %d\n", cbTransfer * 8, uPort, cTransfers));
1355 if (cTransfers > 1)
1356 {
1357 /* If the device supports string transfers, ask it to do as
1358 * much as it wants. The rest is done with single-word transfers. */
1359 const RTGCUINTREG cTransfersOrg = cTransfers;
1360 rc = IOMIOPortReadString(pVM, uPort, &GCPtrDst, &cTransfers, cbTransfer);
1361 AssertRC(rc); Assert(cTransfers <= cTransfersOrg);
1362 pRegFrame->edi += (cTransfersOrg - cTransfers) * cbTransfer;
1363 }
1364
1365#ifdef IN_GC
1366 MMGCRamRegisterTrapHandler(pVM);
1367#endif
1368
1369 while (cTransfers && rc == VINF_SUCCESS)
1370 {
1371 uint32_t u32Value;
1372 rc = IOMIOPortRead(pVM, uPort, &u32Value, cbTransfer);
1373 if (!IOM_SUCCESS(rc))
1374 break;
1375 int rc2 = iomRamWrite(pVM, GCPtrDst, &u32Value, cbTransfer);
1376 Assert(rc2 == VINF_SUCCESS); NOREF(rc2);
1377 GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + cbTransfer);
1378 pRegFrame->edi += cbTransfer;
1379 cTransfers--;
1380 }
1381#ifdef IN_GC
1382 MMGCRamDeregisterTrapHandler(pVM);
1383#endif
1384
1385 /* Update ecx on exit. */
1386 if (uPrefix & PREFIX_REP)
1387 pRegFrame->ecx = cTransfers;
1388
1389 AssertMsg(rc == VINF_SUCCESS || rc == VINF_IOM_HC_IOPORT_READ || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST) || VBOX_FAILURE(rc), ("%Vrc\n", rc));
1390 return rc;
1391}
1392
1393
1394/**
1395 * [REP*] INSB/INSW/INSD
1396 * ES:EDI,DX[,ECX]
1397 *
1398 * @returns Strict VBox status code. Informational status codes other than the one documented
1399 * here are to be treated as internal failure. Use IOM_SUCCESS() to check for success.
1400 * @retval VINF_SUCCESS Success.
1401 * @retval VINF_EM_FIRST-VINF_EM_LAST Success with some exceptions (see IOM_SUCCESS()), the
1402 * status code must be passed on to EM.
1403 * @retval VINF_IOM_HC_IOPORT_READ Defer the read to ring-3. (R0/GC only)
1404 * @retval VINF_EM_RAW_EMULATE_INSTR Defer the read to the REM.
1405 * @retval VINF_EM_RAW_GUEST_TRAP The exception was left pending. (TRPMRaiseXcptErr)
1406 * @retval VINF_TRPM_XCPT_DISPATCHED The exception was raised and dispatched for raw-mode execution. (TRPMRaiseXcptErr)
1407 * @retval VINF_EM_RESCHEDULE_REM The exception was dispatched and cannot be executed in raw-mode. (TRPMRaiseXcptErr)
1408 *
1409 * @param pVM The virtual machine (GC pointer ofcourse).
1410 * @param pRegFrame Pointer to CPUMCTXCORE guest registers structure.
1411 * @param pCpu Disassembler CPU state.
1412 */
1413IOMDECL(int) IOMInterpretINS(PVM pVM, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pCpu)
1414{
1415 /*
1416 * Get port number directly from the register (no need to bother the
1417 * disassembler). And get the I/O register size from the opcode / prefix.
1418 */
1419 RTIOPORT Port = pRegFrame->edx & 0xffff;
1420 unsigned cb = 0;
1421 if (pCpu->pCurInstr->opcode == OP_INSB)
1422 cb = 1;
1423 else
1424 cb = (pCpu->opmode == CPUMODE_16BIT) ? 2 : 4; /* dword in both 32 & 64 bits mode */
1425
1426 int rc = IOMInterpretCheckPortIOAccess(pVM, pRegFrame, Port, cb);
1427 if (RT_UNLIKELY(rc != VINF_SUCCESS))
1428 {
1429 AssertMsg(rc == VINF_EM_RAW_GUEST_TRAP || rc == VINF_TRPM_XCPT_DISPATCHED || rc == VINF_TRPM_XCPT_DISPATCHED || VBOX_FAILURE(rc), ("%Vrc\n", rc));
1430 return rc;
1431 }
1432
1433 return IOMInterpretINSEx(pVM, pRegFrame, Port, pCpu->prefix, cb);
1434}
1435
1436
1437/**
1438 * [REP*] OUTSB/OUTSW/OUTSD
1439 * DS:ESI,DX[,ECX]
1440 *
1441 * @remark Assumes caller checked the access privileges (IOMInterpretCheckPortIOAccess)
1442 *
1443 * @returns Strict VBox status code. Informational status codes other than the one documented
1444 * here are to be treated as internal failure. Use IOM_SUCCESS() to check for success.
1445 * @retval VINF_SUCCESS Success.
1446 * @retval VINF_EM_FIRST-VINF_EM_LAST Success with some exceptions (see IOM_SUCCESS()), the
1447 * status code must be passed on to EM.
1448 * @retval VINF_IOM_HC_IOPORT_WRITE Defer the write to ring-3. (R0/GC only)
1449 * @retval VINF_EM_RAW_GUEST_TRAP The exception was left pending. (TRPMRaiseXcptErr)
1450 * @retval VINF_TRPM_XCPT_DISPATCHED The exception was raised and dispatched for raw-mode execution. (TRPMRaiseXcptErr)
1451 * @retval VINF_EM_RESCHEDULE_REM The exception was dispatched and cannot be executed in raw-mode. (TRPMRaiseXcptErr)
1452 *
1453 * @param pVM The virtual machine (GC pointer ofcourse).
1454 * @param pRegFrame Pointer to CPUMCTXCORE guest registers structure.
1455 * @param uPort IO Port
1456 * @param uPrefix IO instruction prefix
1457 * @param cbTransfer Size of transfer unit
1458 */
1459IOMDECL(int) IOMInterpretOUTSEx(PVM pVM, PCPUMCTXCORE pRegFrame, uint32_t uPort, uint32_t uPrefix, uint32_t cbTransfer)
1460{
1461#ifdef VBOX_WITH_STATISTICS
1462 STAM_COUNTER_INC(&pVM->iom.s.StatGCInstOuts);
1463#endif
1464
1465 /*
1466 * We do not support segment prefixes, REPNE or
1467 * decrementing source pointer.
1468 */
1469 if ( (uPrefix & (PREFIX_SEG | PREFIX_REPNE))
1470 || pRegFrame->eflags.Bits.u1DF)
1471 return VINF_EM_RAW_EMULATE_INSTR;
1472
1473 /*
1474 * Get bytes/words/dwords count to transfer.
1475 */
1476 RTGCUINTREG cTransfers = 1;
1477 if (uPrefix & PREFIX_REP)
1478 {
1479 cTransfers = pRegFrame->ecx;
1480 if (SELMGetCpuModeFromSelector(pVM, pRegFrame->eflags, pRegFrame->cs, &pRegFrame->csHid) == CPUMODE_16BIT)
1481 cTransfers &= 0xffff;
1482
1483 if (!cTransfers)
1484 return VINF_SUCCESS;
1485 }
1486
1487 /* Convert source address ds:esi. */
1488 RTGCPTR GCPtrSrc;
1489 int rc = SELMToFlatEx(pVM, DIS_SELREG_DS, pRegFrame, (RTGCPTR)pRegFrame->esi,
1490 SELMTOFLAT_FLAGS_HYPER | SELMTOFLAT_FLAGS_NO_PL,
1491 &GCPtrSrc);
1492 if (VBOX_FAILURE(rc))
1493 {
1494 Log(("OUTS source address conversion failed -> fallback, rc=%Vrc\n", rc));
1495 return VINF_EM_RAW_EMULATE_INSTR;
1496 }
1497
1498 /* Access verification first; we currently can't recover properly from traps inside this instruction */
1499 uint32_t cpl = CPUMGetGuestCPL(pVM, pRegFrame);
1500 rc = PGMVerifyAccess(pVM, (RTGCUINTPTR)GCPtrSrc, cTransfers * cbTransfer,
1501 (cpl == 3) ? X86_PTE_US : 0);
1502 if (rc != VINF_SUCCESS)
1503 {
1504 Log(("OUTS will generate a trap -> fallback, rc=%Vrc\n", rc));
1505 return VINF_EM_RAW_EMULATE_INSTR;
1506 }
1507
1508 Log(("IOM: rep outs%d port %#x count %d\n", cbTransfer * 8, uPort, cTransfers));
1509 if (cTransfers > 1)
1510 {
1511 /*
1512 * If the device supports string transfers, ask it to do as
1513 * much as it wants. The rest is done with single-word transfers.
1514 */
1515 const RTGCUINTREG cTransfersOrg = cTransfers;
1516 rc = IOMIOPortWriteString(pVM, uPort, &GCPtrSrc, &cTransfers, cbTransfer);
1517 AssertRC(rc); Assert(cTransfers <= cTransfersOrg);
1518 pRegFrame->esi += (cTransfersOrg - cTransfers) * cbTransfer;
1519 }
1520
1521#ifdef IN_GC
1522 MMGCRamRegisterTrapHandler(pVM);
1523#endif
1524
1525 while (cTransfers && rc == VINF_SUCCESS)
1526 {
1527 uint32_t u32Value;
1528 rc = iomRamRead(pVM, &u32Value, GCPtrSrc, cbTransfer);
1529 if (rc != VINF_SUCCESS)
1530 break;
1531 rc = IOMIOPortWrite(pVM, uPort, u32Value, cbTransfer);
1532 if (!IOM_SUCCESS(rc))
1533 break;
1534 GCPtrSrc = (RTGCPTR)((RTUINTPTR)GCPtrSrc + cbTransfer);
1535 pRegFrame->esi += cbTransfer;
1536 cTransfers--;
1537 }
1538
1539#ifdef IN_GC
1540 MMGCRamDeregisterTrapHandler(pVM);
1541#endif
1542
1543 /* Update ecx on exit. */
1544 if (uPrefix & PREFIX_REP)
1545 pRegFrame->ecx = cTransfers;
1546
1547 AssertMsg(rc == VINF_SUCCESS || rc == VINF_IOM_HC_IOPORT_WRITE || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST) || VBOX_FAILURE(rc), ("%Vrc\n", rc));
1548 return rc;
1549}
1550
1551
1552/**
1553 * [REP*] OUTSB/OUTSW/OUTSD
1554 * DS:ESI,DX[,ECX]
1555 *
1556 * @returns Strict VBox status code. Informational status codes other than the one documented
1557 * here are to be treated as internal failure. Use IOM_SUCCESS() to check for success.
1558 * @retval VINF_SUCCESS Success.
1559 * @retval VINF_EM_FIRST-VINF_EM_LAST Success with some exceptions (see IOM_SUCCESS()), the
1560 * status code must be passed on to EM.
1561 * @retval VINF_IOM_HC_IOPORT_WRITE Defer the write to ring-3. (R0/GC only)
1562 * @retval VINF_EM_RAW_EMULATE_INSTR Defer the write to the REM.
1563 * @retval VINF_EM_RAW_GUEST_TRAP The exception was left pending. (TRPMRaiseXcptErr)
1564 * @retval VINF_TRPM_XCPT_DISPATCHED The exception was raised and dispatched for raw-mode execution. (TRPMRaiseXcptErr)
1565 * @retval VINF_EM_RESCHEDULE_REM The exception was dispatched and cannot be executed in raw-mode. (TRPMRaiseXcptErr)
1566 *
1567 * @param pVM The virtual machine (GC pointer ofcourse).
1568 * @param pRegFrame Pointer to CPUMCTXCORE guest registers structure.
1569 * @param pCpu Disassembler CPU state.
1570 */
1571IOMDECL(int) IOMInterpretOUTS(PVM pVM, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pCpu)
1572{
1573 /*
1574 * Get port number from the first parameter.
1575 * And get the I/O register size from the opcode / prefix.
1576 */
1577 uint64_t Port = 0;
1578 unsigned cb = 0;
1579 bool fRc = iomGetRegImmData(pCpu, &pCpu->param1, pRegFrame, &Port, &cb);
1580 AssertMsg(fRc, ("Failed to get reg/imm port number!\n")); NOREF(fRc);
1581 if (pCpu->pCurInstr->opcode == OP_OUTSB)
1582 cb = 1;
1583 else
1584 cb = (pCpu->opmode == CPUMODE_16BIT) ? 2 : 4; /* dword in both 32 & 64 bits mode */
1585
1586 int rc = IOMInterpretCheckPortIOAccess(pVM, pRegFrame, Port, cb);
1587 if (RT_UNLIKELY(rc != VINF_SUCCESS))
1588 {
1589 AssertMsg(rc == VINF_EM_RAW_GUEST_TRAP || rc == VINF_TRPM_XCPT_DISPATCHED || rc == VINF_TRPM_XCPT_DISPATCHED || VBOX_FAILURE(rc), ("%Vrc\n", rc));
1590 return rc;
1591 }
1592
1593 return IOMInterpretOUTSEx(pVM, pRegFrame, Port, pCpu->prefix, cb);
1594}
1595
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