VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMRC/IOMRC.cpp@ 78382

Last change on this file since 78382 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 11.6 KB
Line 
1/* $Id: IOMRC.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * IOM - Input / Output Monitor - Raw-Mode Context.
4 */
5
6/*
7 * Copyright (C) 2006-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_IOM
23#include <VBox/vmm/iom.h>
24#include <VBox/vmm/cpum.h>
25#include <VBox/vmm/pgm.h>
26#include <VBox/vmm/selm.h>
27#include <VBox/vmm/mm.h>
28#include <VBox/vmm/em.h>
29#include <VBox/vmm/iem.h>
30#include <VBox/vmm/pgm.h>
31#include <VBox/vmm/trpm.h>
32#include "IOMInternal.h"
33#include <VBox/vmm/vm.h>
34
35#include <VBox/dis.h>
36#include <VBox/disopcode.h>
37#include <VBox/param.h>
38#include <VBox/err.h>
39#include <iprt/assert.h>
40#include <VBox/log.h>
41#include <iprt/asm.h>
42#include <iprt/string.h>
43
44
45/**
46 * Converts disassembler mode to IEM mode.
47 * @return IEM CPU mode.
48 * @param enmDisMode Disassembler CPU mode.
49 */
50DECLINLINE(IEMMODE) iomDisModeToIemMode(DISCPUMODE enmDisMode)
51{
52 switch (enmDisMode)
53 {
54 case DISCPUMODE_16BIT: return IEMMODE_16BIT;
55 case DISCPUMODE_32BIT: return IEMMODE_32BIT;
56 case DISCPUMODE_64BIT: return IEMMODE_64BIT;
57 default:
58 AssertFailed();
59 return IEMMODE_32BIT;
60 }
61}
62
63
64/**
65 * IN <AL|AX|EAX>, <DX|imm16>
66 *
67 * @returns Strict VBox status code. Informational status codes other than the one documented
68 * here are to be treated as internal failure. Use IOM_SUCCESS() to check for success.
69 * @retval VINF_SUCCESS Success.
70 * @retval VINF_EM_FIRST-VINF_EM_LAST Success with some exceptions (see IOM_SUCCESS()), the
71 * status code must be passed on to EM.
72 * @retval VINF_IOM_R3_IOPORT_READ Defer the read to ring-3. (R0/GC only)
73 * @retval VINF_EM_RAW_GUEST_TRAP The exception was left pending. (TRPMRaiseXcptErr)
74 * @retval VINF_TRPM_XCPT_DISPATCHED The exception was raised and dispatched for raw-mode execution. (TRPMRaiseXcptErr)
75 * @retval VINF_EM_RESCHEDULE_REM The exception was dispatched and cannot be executed in raw-mode. (TRPMRaiseXcptErr)
76 *
77 * @param pVM The cross context VM structure.
78 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
79 * @param pRegFrame Pointer to CPUMCTXCORE guest registers structure.
80 * @param pCpu Disassembler CPU state.
81 */
82static VBOXSTRICTRC iomRCInterpretIN(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pCpu)
83{
84 STAM_COUNTER_INC(&pVM->iom.s.StatInstIn); RT_NOREF_PV(pVM);
85 Assert(pCpu->Param2.fUse & (DISUSE_IMMEDIATE8 | DISUSE_REG_GEN16));
86 bool const fUseReg = RT_BOOL(pCpu->Param2.fUse & DISUSE_REG_GEN16);
87 uint16_t const u16Port = fUseReg ? pRegFrame->dx : (uint16_t)pCpu->Param2.uValue;
88
89 Assert(pCpu->Param1.fUse & (DISUSE_REG_GEN32 | DISUSE_REG_GEN16 | DISUSE_REG_GEN8));
90 uint8_t cbValue = pCpu->Param1.fUse & DISUSE_REG_GEN32 ? 4 : pCpu->Param1.fUse & DISUSE_REG_GEN16 ? 2 : 1;
91
92 return IEMExecDecodedIn(pVCpu, pCpu->cbInstr, u16Port, !fUseReg, cbValue);
93}
94
95
96/**
97 * OUT <DX|imm16>, <AL|AX|EAX>
98 *
99 * @returns Strict VBox status code. Informational status codes other than the one documented
100 * here are to be treated as internal failure. Use IOM_SUCCESS() to check for success.
101 * @retval VINF_SUCCESS Success.
102 * @retval VINF_EM_FIRST-VINF_EM_LAST Success with some exceptions (see IOM_SUCCESS()), the
103 * status code must be passed on to EM.
104 * @retval VINF_IOM_R3_IOPORT_WRITE Defer the write to ring-3. (R0/GC only)
105 * @retval VINF_IOM_R3_IOPORT_COMMIT_WRITE Defer the write to ring-3. (R0/GC only)
106 * @retval VINF_EM_RAW_GUEST_TRAP The exception was left pending. (TRPMRaiseXcptErr)
107 * @retval VINF_TRPM_XCPT_DISPATCHED The exception was raised and dispatched for raw-mode execution. (TRPMRaiseXcptErr)
108 * @retval VINF_EM_RESCHEDULE_REM The exception was dispatched and cannot be executed in raw-mode. (TRPMRaiseXcptErr)
109 *
110 * @param pVM The cross context VM structure.
111 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
112 * @param pRegFrame Pointer to CPUMCTXCORE guest registers structure.
113 * @param pCpu Disassembler CPU state.
114 */
115static VBOXSTRICTRC iomRCInterpretOUT(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pCpu)
116{
117 STAM_COUNTER_INC(&pVM->iom.s.StatInstOut); RT_NOREF_PV(pVM);
118 Assert(pCpu->Param1.fUse & (DISUSE_IMMEDIATE8 | DISUSE_REG_GEN16));
119 bool const fUseReg = RT_BOOL(pCpu->Param1.fUse & DISUSE_REG_GEN16);
120 uint16_t const u16Port = fUseReg ? pRegFrame->dx : (uint16_t)pCpu->Param1.uValue;
121
122 Assert(pCpu->Param2.fUse & (DISUSE_REG_GEN32 | DISUSE_REG_GEN16 | DISUSE_REG_GEN8));
123 uint8_t const cbValue = pCpu->Param2.fUse & DISUSE_REG_GEN32 ? 4 : pCpu->Param2.fUse & DISUSE_REG_GEN16 ? 2 : 1;
124
125 return IEMExecDecodedOut(pVCpu, pCpu->cbInstr, u16Port, !fUseReg, cbValue);
126}
127
128
129/**
130 * [REP*] INSB/INSW/INSD
131 * ES:EDI,DX[,ECX]
132 *
133 * @returns Strict VBox status code. Informational status codes other than the one documented
134 * here are to be treated as internal failure. Use IOM_SUCCESS() to check for success.
135 * @retval VINF_SUCCESS Success.
136 * @retval VINF_EM_FIRST-VINF_EM_LAST Success with some exceptions (see IOM_SUCCESS()), the
137 * status code must be passed on to EM.
138 * @retval VINF_IOM_R3_IOPORT_READ Defer the read to ring-3. (R0/GC only)
139 * @retval VINF_EM_RAW_EMULATE_INSTR Defer the read to the REM.
140 * @retval VINF_EM_RAW_GUEST_TRAP The exception was left pending. (TRPMRaiseXcptErr)
141 * @retval VINF_TRPM_XCPT_DISPATCHED The exception was raised and dispatched for raw-mode execution. (TRPMRaiseXcptErr)
142 * @retval VINF_EM_RESCHEDULE_REM The exception was dispatched and cannot be executed in raw-mode. (TRPMRaiseXcptErr)
143 *
144 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
145 * @param pCpu Disassembler CPU state.
146 */
147static VBOXSTRICTRC iomRCInterpretINS(PVMCPU pVCpu, PDISCPUSTATE pCpu)
148{
149 uint8_t cbValue = pCpu->pCurInstr->uOpcode == OP_INSB ? 1
150 : pCpu->uOpMode == DISCPUMODE_16BIT ? 2 : 4; /* dword in both 32 & 64 bits mode */
151 return IEMExecStringIoRead(pVCpu,
152 cbValue,
153 iomDisModeToIemMode((DISCPUMODE)pCpu->uCpuMode),
154 RT_BOOL(pCpu->fPrefix & (DISPREFIX_REPNE | DISPREFIX_REP)),
155 pCpu->cbInstr,
156 false /*fIoChecked*/);
157}
158
159
160/**
161 * [REP*] OUTSB/OUTSW/OUTSD
162 * DS:ESI,DX[,ECX]
163 *
164 * @returns Strict VBox status code. Informational status codes other than the one documented
165 * here are to be treated as internal failure. Use IOM_SUCCESS() to check for success.
166 * @retval VINF_SUCCESS Success.
167 * @retval VINF_EM_FIRST-VINF_EM_LAST Success with some exceptions (see IOM_SUCCESS()), the
168 * status code must be passed on to EM.
169 * @retval VINF_IOM_R3_IOPORT_WRITE Defer the write to ring-3. (R0/GC only)
170 * @retval VINF_IOM_R3_IOPORT_COMMIT_WRITE Defer the write to ring-3. (R0/GC only)
171 * @retval VINF_EM_RAW_EMULATE_INSTR Defer the write to the REM.
172 * @retval VINF_EM_RAW_GUEST_TRAP The exception was left pending. (TRPMRaiseXcptErr)
173 * @retval VINF_TRPM_XCPT_DISPATCHED The exception was raised and dispatched for raw-mode execution. (TRPMRaiseXcptErr)
174 * @retval VINF_EM_RESCHEDULE_REM The exception was dispatched and cannot be executed in raw-mode. (TRPMRaiseXcptErr)
175 *
176 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
177 * @param pCpu Disassembler CPU state.
178 */
179static VBOXSTRICTRC iomRCInterpretOUTS(PVMCPU pVCpu, PDISCPUSTATE pCpu)
180{
181 uint8_t cbValue = pCpu->pCurInstr->uOpcode == OP_OUTSB ? 1
182 : pCpu->uOpMode == DISCPUMODE_16BIT ? 2 : 4; /* dword in both 32 & 64 bits mode */
183 return IEMExecStringIoWrite(pVCpu,
184 cbValue,
185 iomDisModeToIemMode((DISCPUMODE)pCpu->uCpuMode),
186 RT_BOOL(pCpu->fPrefix & (DISPREFIX_REPNE | DISPREFIX_REP)),
187 pCpu->cbInstr,
188 pCpu->fPrefix & DISPREFIX_SEG ? pCpu->idxSegPrefix : X86_SREG_DS,
189 false /*fIoChecked*/);
190}
191
192
193
194/**
195 * Attempts to service an IN/OUT instruction.
196 *
197 * The \#GP trap handler in RC will call this function if the opcode causing
198 * the trap is a in or out type instruction. (Call it indirectly via EM that
199 * is.)
200 *
201 * @returns Strict VBox status code. Informational status codes other than the one documented
202 * here are to be treated as internal failure. Use IOM_SUCCESS() to check for success.
203 * @retval VINF_SUCCESS Success.
204 * @retval VINF_EM_FIRST-VINF_EM_LAST Success with some exceptions (see IOM_SUCCESS()), the
205 * status code must be passed on to EM.
206 * @retval VINF_EM_RESCHEDULE_REM The exception was dispatched and cannot be executed in raw-mode. (TRPMRaiseXcptErr)
207 * @retval VINF_EM_RAW_EMULATE_INSTR Defer the read to the REM.
208 * @retval VINF_IOM_R3_IOPORT_READ Defer the read to ring-3.
209 * @retval VINF_EM_RAW_GUEST_TRAP The exception was left pending. (TRPMRaiseXcptErr)
210 * @retval VINF_TRPM_XCPT_DISPATCHED The exception was raised and dispatched for raw-mode execution. (TRPMRaiseXcptErr)
211 *
212 * @param pVM The cross context VM structure.
213 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
214 * @param pRegFrame Pointer to CPUMCTXCORE guest registers structure.
215 * @param pCpu Disassembler CPU state.
216 */
217VMMRCDECL(VBOXSTRICTRC) IOMRCIOPortHandler(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pCpu)
218{
219 switch (pCpu->pCurInstr->uOpcode)
220 {
221 case OP_IN:
222 EMHistoryUpdateFlagsAndType(pVCpu, EMEXIT_MAKE_FT(EMEXIT_F_KIND_EM, EMEXITTYPE_IO_PORT_READ));
223 return iomRCInterpretIN(pVM, pVCpu, pRegFrame, pCpu);
224
225 case OP_OUT:
226 EMHistoryUpdateFlagsAndType(pVCpu, EMEXIT_MAKE_FT(EMEXIT_F_KIND_EM, EMEXITTYPE_IO_PORT_WRITE));
227 return iomRCInterpretOUT(pVM, pVCpu, pRegFrame, pCpu);
228
229 case OP_INSB:
230 case OP_INSWD:
231 EMHistoryUpdateFlagsAndType(pVCpu, EMEXIT_MAKE_FT(EMEXIT_F_KIND_EM, EMEXITTYPE_IO_PORT_STR_READ));
232 return iomRCInterpretINS(pVCpu, pCpu);
233
234 case OP_OUTSB:
235 case OP_OUTSWD:
236 EMHistoryUpdateFlagsAndType(pVCpu, EMEXIT_MAKE_FT(EMEXIT_F_KIND_EM, EMEXITTYPE_IO_PORT_STR_WRITE));
237 return iomRCInterpretOUTS(pVCpu, pCpu);
238
239 /*
240 * The opcode wasn't know to us, freak out.
241 */
242 default:
243 AssertMsgFailed(("Unknown I/O port access opcode %d.\n", pCpu->pCurInstr->uOpcode));
244 return VERR_IOM_IOPORT_UNKNOWN_OPCODE;
245 }
246}
247
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