VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/IOM.cpp@ 86523

Last change on this file since 86523 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 19.2 KB
Line 
1/* $Id: IOM.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * IOM - Input / Output Monitor.
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/** @page pg_iom IOM - The Input / Output Monitor
20 *
21 * The input/output monitor will handle I/O exceptions routing them to the
22 * appropriate device. It implements an API to register and deregister virtual
23 * I/0 port handlers and memory mapped I/O handlers. A handler is PDM devices
24 * and a set of callback functions.
25 *
26 * @see grp_iom
27 *
28 *
29 * @section sec_iom_rawmode Raw-Mode
30 *
31 * In raw-mode I/O port access is trapped (\#GP(0)) by ensuring that the actual
32 * IOPL is 0 regardless of what the guest IOPL is. The \#GP handler use the
33 * disassembler (DIS) to figure which instruction caused it (there are a number
34 * of instructions in addition to the I/O ones) and if it's an I/O port access
35 * it will hand it to IOMRCIOPortHandler (via EMInterpretPortIO).
36 * IOMRCIOPortHandler will lookup the port in the AVL tree of registered
37 * handlers. If found, the handler will be called otherwise default action is
38 * taken. (Default action is to write into the void and read all set bits.)
39 *
40 * Memory Mapped I/O (MMIO) is implemented as a slightly special case of PGM
41 * access handlers. An MMIO range is registered with IOM which then registers it
42 * with the PGM access handler sub-system. The access handler catches all
43 * access and will be called in the context of a \#PF handler. In RC and R0 this
44 * handler is iomMmioPfHandler while in ring-3 it's iomR3MmioHandler (although
45 * in ring-3 there can be alternative ways). iomMmioPfHandler will attempt to
46 * emulate the instruction that is doing the access and pass the corresponding
47 * reads / writes to the device.
48 *
49 * Emulating I/O port access is less complex and should be slightly faster than
50 * emulating MMIO, so in most cases we should encourage the OS to use port I/O.
51 * Devices which are frequently accessed should register GC handlers to speed up
52 * execution.
53 *
54 *
55 * @section sec_iom_hm Hardware Assisted Virtualization Mode
56 *
57 * When running in hardware assisted virtualization mode we'll be doing much the
58 * same things as in raw-mode. The main difference is that we're running in the
59 * host ring-0 context and that we don't get faults (\#GP(0) and \#PG) but
60 * exits.
61 *
62 *
63 * @section sec_iom_rem Recompiled Execution Mode
64 *
65 * When running in the recompiler things are different. I/O port access is
66 * handled by calling IOMIOPortRead and IOMIOPortWrite directly. While MMIO can
67 * be handled in one of two ways. The normal way is that we have a registered a
68 * special RAM range with the recompiler and in the three callbacks (for byte,
69 * word and dword access) we call IOMMMIORead and IOMMMIOWrite directly. The
70 * alternative ways that the physical memory access which goes via PGM will take
71 * care of it by calling iomR3MmioHandler via the PGM access handler machinery
72 * - this shouldn't happen but it is an alternative...
73 *
74 *
75 * @section sec_iom_other Other Accesses
76 *
77 * I/O ports aren't really exposed in any other way, unless you count the
78 * instruction interpreter in EM, but that's just what we're doing in the
79 * raw-mode \#GP(0) case really. Now, it's possible to call IOMIOPortRead and
80 * IOMIOPortWrite directly to talk to a device, but this is really bad behavior
81 * and should only be done as temporary hacks (the PC BIOS device used to setup
82 * the CMOS this way back in the dark ages).
83 *
84 * MMIO has similar direct routes as the I/O ports and these shouldn't be used
85 * for the same reasons and with the same restrictions. OTOH since MMIO is
86 * mapped into the physical memory address space, it can be accessed in a number
87 * of ways thru PGM.
88 *
89 *
90 * @section sec_iom_logging Logging Levels
91 *
92 * Following assignments:
93 * - Level 5 is used for defering I/O port and MMIO writes to ring-3.
94 *
95 */
96
97/** @todo MMIO - simplifying the device end.
98 * - Add a return status for doing DBGFSTOP on access where there are no known
99 * registers.
100 * -
101 *
102 * */
103
104
105/*********************************************************************************************************************************
106* Header Files *
107*********************************************************************************************************************************/
108#define LOG_GROUP LOG_GROUP_IOM
109#include <VBox/vmm/iom.h>
110#include <VBox/vmm/cpum.h>
111#include <VBox/vmm/pgm.h>
112#include <VBox/sup.h>
113#include <VBox/vmm/hm.h>
114#include <VBox/vmm/mm.h>
115#include <VBox/vmm/stam.h>
116#include <VBox/vmm/dbgf.h>
117#include <VBox/vmm/pdmapi.h>
118#include <VBox/vmm/pdmdev.h>
119#include "IOMInternal.h"
120#include <VBox/vmm/vm.h>
121
122#include <VBox/param.h>
123#include <iprt/assert.h>
124#include <iprt/alloc.h>
125#include <iprt/string.h>
126#include <VBox/log.h>
127#include <VBox/err.h>
128
129
130
131/**
132 * Initializes the IOM.
133 *
134 * @returns VBox status code.
135 * @param pVM The cross context VM structure.
136 */
137VMMR3_INT_DECL(int) IOMR3Init(PVM pVM)
138{
139 LogFlow(("IOMR3Init:\n"));
140
141 /*
142 * Assert alignment and sizes.
143 */
144 AssertCompileMemberAlignment(VM, iom.s, 32);
145 AssertCompile(sizeof(pVM->iom.s) <= sizeof(pVM->iom.padding));
146 AssertCompileMemberAlignment(IOM, CritSect, sizeof(uintptr_t));
147
148 /*
149 * Initialize the REM critical section.
150 */
151#ifdef IOM_WITH_CRIT_SECT_RW
152 int rc = PDMR3CritSectRwInit(pVM, &pVM->iom.s.CritSect, RT_SRC_POS, "IOM Lock");
153#else
154 int rc = PDMR3CritSectInit(pVM, &pVM->iom.s.CritSect, RT_SRC_POS, "IOM Lock");
155#endif
156 AssertRCReturn(rc, rc);
157
158 /*
159 * Register the MMIO access handler type.
160 */
161 rc = PGMR3HandlerPhysicalTypeRegister(pVM, PGMPHYSHANDLERKIND_MMIO,
162 iomMmioHandlerNew,
163 NULL, "iomMmioHandlerNew", "iomMmioPfHandlerNew",
164 NULL, "iomMmioHandlerNew", "iomMmioPfHandlerNew",
165 "MMIO New", &pVM->iom.s.hNewMmioHandlerType);
166 AssertRCReturn(rc, rc);
167
168 /*
169 * Info.
170 */
171 DBGFR3InfoRegisterInternal(pVM, "ioport", "Dumps all IOPort ranges. No arguments.", &iomR3IoPortInfo);
172 DBGFR3InfoRegisterInternal(pVM, "mmio", "Dumps all MMIO ranges. No arguments.", &iomR3MmioInfo);
173
174 /*
175 * Statistics (names are somewhat contorted to make the registration
176 * sub-trees appear at the end of each group).
177 */
178 STAM_REG(pVM, &pVM->iom.s.StatIoPortCommits, STAMTYPE_COUNTER, "/IOM/IoPortCommits", STAMUNIT_OCCURENCES, "Number of ring-3 I/O port commits.");
179 STAM_REG(pVM, &pVM->iom.s.StatIoPortIn, STAMTYPE_PROFILE, "/IOM/IoPortIN", STAMUNIT_OCCURENCES, "Number of IN instructions (attempts)");
180 STAM_REG(pVM, &pVM->iom.s.StatIoPortInS, STAMTYPE_PROFILE, "/IOM/IoPortINS", STAMUNIT_OCCURENCES, "Number of INS instructions (attempts)");
181 STAM_REG(pVM, &pVM->iom.s.StatIoPortOutS, STAMTYPE_PROFILE, "/IOM/IoPortOUT", STAMUNIT_OCCURENCES, "Number of OUT instructions (attempts)");
182 STAM_REG(pVM, &pVM->iom.s.StatIoPortOutS, STAMTYPE_PROFILE, "/IOM/IoPortOUTS", STAMUNIT_OCCURENCES, "Number of OUTS instructions (attempts)");
183
184 STAM_REG(pVM, &pVM->iom.s.StatMmioHandlerR3, STAMTYPE_COUNTER, "/IOM/MmioHandlerR3", STAMUNIT_OCCURENCES, "Number of calls to iomMmioHandlerNew from ring-3.");
185 STAM_REG(pVM, &pVM->iom.s.StatMmioHandlerR0, STAMTYPE_COUNTER, "/IOM/MmioHandlerR0", STAMUNIT_OCCURENCES, "Number of calls to iomMmioHandlerNew from ring-0.");
186 STAM_REG(pVM, &pVM->iom.s.StatMmioReadsR0ToR3, STAMTYPE_COUNTER, "/IOM/MmioR0ToR3Reads", STAMUNIT_OCCURENCES, "Number of reads deferred to ring-3.");
187 STAM_REG(pVM, &pVM->iom.s.StatMmioWritesR0ToR3, STAMTYPE_COUNTER, "/IOM/MmioR0ToR3Writes", STAMUNIT_OCCURENCES, "Number of writes deferred to ring-3.");
188 STAM_REG(pVM, &pVM->iom.s.StatMmioCommitsR0ToR3,STAMTYPE_COUNTER, "/IOM/MmioR0ToR3Commits", STAMUNIT_OCCURENCES, "Number of commits deferred to ring-3.");
189 STAM_REG(pVM, &pVM->iom.s.StatMmioPfHandler, STAMTYPE_PROFILE, "/IOM/MmioPfHandler", STAMUNIT_OCCURENCES, "Number of calls to iomMmioPfHandlerNew.");
190 STAM_REG(pVM, &pVM->iom.s.StatMmioPhysHandler, STAMTYPE_PROFILE, "/IOM/MmioPhysHandler", STAMUNIT_OCCURENCES, "Number of calls to IOMR0MmioPhysHandler.");
191 STAM_REG(pVM, &pVM->iom.s.StatMmioCommitsDirect,STAMTYPE_COUNTER, "/IOM/MmioCommitsDirect", STAMUNIT_OCCURENCES, "Number of ring-3 MMIO commits direct to handler via handle hint.");
192 STAM_REG(pVM, &pVM->iom.s.StatMmioCommitsPgm, STAMTYPE_COUNTER, "/IOM/MmioCommitsPgm", STAMUNIT_OCCURENCES, "Number of ring-3 MMIO commits via PGM.");
193 STAM_REL_REG(pVM, &pVM->iom.s.StatMmioStaleMappings, STAMTYPE_PROFILE, "/IOM/MmioMappingsStale", STAMUNIT_TICKS_PER_CALL, "Number of times iomMmioHandlerNew got a call for a remapped range at the old mapping.");
194 STAM_REG(pVM, &pVM->iom.s.StatMmioDevLockContentionR0, STAMTYPE_COUNTER, "/IOM/MmioDevLockContentionR0", STAMUNIT_OCCURENCES, "Number of device lock contention force return to ring-3.");
195
196 LogFlow(("IOMR3Init: returns VINF_SUCCESS\n"));
197 return VINF_SUCCESS;
198}
199
200
201/**
202 * Called when a VM initialization stage is completed.
203 *
204 * @returns VBox status code.
205 * @param pVM The cross context VM structure.
206 * @param enmWhat The initialization state that was completed.
207 */
208VMMR3_INT_DECL(int) IOMR3InitCompleted(PVM pVM, VMINITCOMPLETED enmWhat)
209{
210#ifdef VBOX_WITH_STATISTICS
211 if (enmWhat == VMINITCOMPLETED_RING0)
212 {
213 /*
214 * Synchronize the ring-3 I/O port and MMIO statistics indices into the
215 * ring-0 tables to simplify ring-0 code. This also make sure that any
216 * later calls to grow the statistics tables will fail.
217 */
218 int rc = VMMR3CallR0Emt(pVM, pVM->apCpusR3[0], VMMR0_DO_IOM_SYNC_STATS_INDICES, 0, NULL);
219 AssertLogRelRCReturn(rc, rc);
220
221 /*
222 * Register I/O port and MMIO stats now that we're done registering MMIO
223 * regions and won't grow the table again.
224 */
225 for (uint32_t i = 0; i < pVM->iom.s.cIoPortRegs; i++)
226 {
227 PIOMIOPORTENTRYR3 pRegEntry = &pVM->iom.s.paIoPortRegs[i];
228 if ( pRegEntry->fMapped
229 && pRegEntry->idxStats != UINT16_MAX)
230 iomR3IoPortRegStats(pVM, pRegEntry);
231 }
232
233 for (uint32_t i = 0; i < pVM->iom.s.cMmioRegs; i++)
234 {
235 PIOMMMIOENTRYR3 pRegEntry = &pVM->iom.s.paMmioRegs[i];
236 if ( pRegEntry->fMapped
237 && pRegEntry->idxStats != UINT16_MAX)
238 iomR3MmioRegStats(pVM, pRegEntry);
239 }
240 }
241#else
242 RT_NOREF(pVM, enmWhat);
243#endif
244 return VINF_SUCCESS;
245}
246
247
248/**
249 * The VM is being reset.
250 *
251 * @param pVM The cross context VM structure.
252 */
253VMMR3_INT_DECL(void) IOMR3Reset(PVM pVM)
254{
255 RT_NOREF(pVM);
256}
257
258
259/**
260 * Applies relocations to data and code managed by this
261 * component. This function will be called at init and
262 * whenever the VMM need to relocate it self inside the GC.
263 *
264 * The IOM will update the addresses used by the switcher.
265 *
266 * @param pVM The cross context VM structure.
267 * @param offDelta Relocation delta relative to old location.
268 */
269VMMR3_INT_DECL(void) IOMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
270{
271 RT_NOREF(pVM, offDelta);
272}
273
274/**
275 * Terminates the IOM.
276 *
277 * Termination means cleaning up and freeing all resources,
278 * the VM it self is at this point powered off or suspended.
279 *
280 * @returns VBox status code.
281 * @param pVM The cross context VM structure.
282 */
283VMMR3_INT_DECL(int) IOMR3Term(PVM pVM)
284{
285 /*
286 * IOM is not owning anything but automatically freed resources,
287 * so there's nothing to do here.
288 */
289 NOREF(pVM);
290 return VINF_SUCCESS;
291}
292
293
294/**
295 * Handles the unlikely and probably fatal merge cases.
296 *
297 * @returns Merged status code.
298 * @param rcStrict Current EM status code.
299 * @param rcStrictCommit The IOM I/O or MMIO write commit status to merge
300 * with @a rcStrict.
301 * @param rcIom For logging purposes only.
302 * @param pVCpu The cross context virtual CPU structure of the
303 * calling EMT. For logging purposes.
304 */
305DECL_NO_INLINE(static, VBOXSTRICTRC) iomR3MergeStatusSlow(VBOXSTRICTRC rcStrict, VBOXSTRICTRC rcStrictCommit,
306 int rcIom, PVMCPU pVCpu)
307{
308 if (RT_FAILURE_NP(rcStrict))
309 return rcStrict;
310
311 if (RT_FAILURE_NP(rcStrictCommit))
312 return rcStrictCommit;
313
314 if (rcStrict == rcStrictCommit)
315 return rcStrictCommit;
316
317 AssertLogRelMsgFailed(("rcStrictCommit=%Rrc rcStrict=%Rrc IOPort={%#06x<-%#xx/%u} MMIO={%RGp<-%.*Rhxs} (rcIom=%Rrc)\n",
318 VBOXSTRICTRC_VAL(rcStrictCommit), VBOXSTRICTRC_VAL(rcStrict),
319 pVCpu->iom.s.PendingIOPortWrite.IOPort,
320 pVCpu->iom.s.PendingIOPortWrite.u32Value, pVCpu->iom.s.PendingIOPortWrite.cbValue,
321 pVCpu->iom.s.PendingMmioWrite.GCPhys,
322 pVCpu->iom.s.PendingMmioWrite.cbValue, &pVCpu->iom.s.PendingMmioWrite.abValue[0], rcIom));
323 return VERR_IOM_FF_STATUS_IPE;
324}
325
326
327/**
328 * Helper for IOMR3ProcessForceFlag.
329 *
330 * @returns Merged status code.
331 * @param rcStrict Current EM status code.
332 * @param rcStrictCommit The IOM I/O or MMIO write commit status to merge
333 * with @a rcStrict.
334 * @param rcIom Either VINF_IOM_R3_IOPORT_COMMIT_WRITE or
335 * VINF_IOM_R3_MMIO_COMMIT_WRITE.
336 * @param pVCpu The cross context virtual CPU structure of the
337 * calling EMT.
338 */
339DECLINLINE(VBOXSTRICTRC) iomR3MergeStatus(VBOXSTRICTRC rcStrict, VBOXSTRICTRC rcStrictCommit, int rcIom, PVMCPU pVCpu)
340{
341 /* Simple. */
342 if (RT_LIKELY(rcStrict == rcIom || rcStrict == VINF_EM_RAW_TO_R3 || rcStrict == VINF_SUCCESS))
343 return rcStrictCommit;
344
345 if (RT_LIKELY(rcStrictCommit == VINF_SUCCESS))
346 return rcStrict;
347
348 /* EM scheduling status codes. */
349 if (RT_LIKELY( rcStrict >= VINF_EM_FIRST
350 && rcStrict <= VINF_EM_LAST))
351 {
352 if (RT_LIKELY( rcStrictCommit >= VINF_EM_FIRST
353 && rcStrictCommit <= VINF_EM_LAST))
354 return rcStrict < rcStrictCommit ? rcStrict : rcStrictCommit;
355 }
356
357 /* Unlikely */
358 return iomR3MergeStatusSlow(rcStrict, rcStrictCommit, rcIom, pVCpu);
359}
360
361
362/**
363 * Called by force-flag handling code when VMCPU_FF_IOM is set.
364 *
365 * @returns Merge between @a rcStrict and what the commit operation returned.
366 * @param pVM The cross context VM structure.
367 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
368 * @param rcStrict The status code returned by ring-0 or raw-mode.
369 * @thread EMT(pVCpu)
370 *
371 * @remarks The VMCPU_FF_IOM flag is handled before the status codes by EM, so
372 * we're very likely to see @a rcStrict set to
373 * VINF_IOM_R3_IOPORT_COMMIT_WRITE and VINF_IOM_R3_MMIO_COMMIT_WRITE
374 * here.
375 */
376VMMR3_INT_DECL(VBOXSTRICTRC) IOMR3ProcessForceFlag(PVM pVM, PVMCPU pVCpu, VBOXSTRICTRC rcStrict)
377{
378 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_IOM);
379 Assert(pVCpu->iom.s.PendingIOPortWrite.cbValue || pVCpu->iom.s.PendingMmioWrite.cbValue);
380
381 if (pVCpu->iom.s.PendingIOPortWrite.cbValue)
382 {
383 Log5(("IOM: Dispatching pending I/O port write: %#x LB %u -> %RTiop\n", pVCpu->iom.s.PendingIOPortWrite.u32Value,
384 pVCpu->iom.s.PendingIOPortWrite.cbValue, pVCpu->iom.s.PendingIOPortWrite.IOPort));
385 STAM_COUNTER_INC(&pVM->iom.s.StatIoPortCommits);
386 VBOXSTRICTRC rcStrictCommit = IOMIOPortWrite(pVM, pVCpu, pVCpu->iom.s.PendingIOPortWrite.IOPort,
387 pVCpu->iom.s.PendingIOPortWrite.u32Value,
388 pVCpu->iom.s.PendingIOPortWrite.cbValue);
389 pVCpu->iom.s.PendingIOPortWrite.cbValue = 0;
390 rcStrict = iomR3MergeStatus(rcStrict, rcStrictCommit, VINF_IOM_R3_IOPORT_COMMIT_WRITE, pVCpu);
391 }
392
393
394 if (pVCpu->iom.s.PendingMmioWrite.cbValue)
395 {
396 Log5(("IOM: Dispatching pending MMIO write: %RGp LB %#x\n",
397 pVCpu->iom.s.PendingMmioWrite.GCPhys, pVCpu->iom.s.PendingMmioWrite.cbValue));
398
399 /* Use new MMIO handle hint and bypass PGM if it still looks right. */
400 size_t idxMmioRegionHint = pVCpu->iom.s.PendingMmioWrite.idxMmioRegionHint;
401 if (idxMmioRegionHint < pVM->iom.s.cMmioRegs)
402 {
403 PIOMMMIOENTRYR3 pRegEntry = &pVM->iom.s.paMmioRegs[idxMmioRegionHint];
404 RTGCPHYS const GCPhysMapping = pRegEntry->GCPhysMapping;
405 RTGCPHYS const offRegion = pVCpu->iom.s.PendingMmioWrite.GCPhys - GCPhysMapping;
406 if (offRegion < pRegEntry->cbRegion && GCPhysMapping != NIL_RTGCPHYS)
407 {
408 STAM_COUNTER_INC(&pVM->iom.s.StatMmioCommitsDirect);
409 VBOXSTRICTRC rcStrictCommit = iomR3MmioCommitWorker(pVM, pVCpu, pRegEntry, offRegion);
410 pVCpu->iom.s.PendingMmioWrite.cbValue = 0;
411 return iomR3MergeStatus(rcStrict, rcStrictCommit, VINF_IOM_R3_MMIO_COMMIT_WRITE, pVCpu);
412 }
413 }
414
415 /* Fall back on PGM. */
416 STAM_COUNTER_INC(&pVM->iom.s.StatMmioCommitsPgm);
417 VBOXSTRICTRC rcStrictCommit = PGMPhysWrite(pVM, pVCpu->iom.s.PendingMmioWrite.GCPhys,
418 pVCpu->iom.s.PendingMmioWrite.abValue, pVCpu->iom.s.PendingMmioWrite.cbValue,
419 PGMACCESSORIGIN_IOM);
420 pVCpu->iom.s.PendingMmioWrite.cbValue = 0;
421 rcStrict = iomR3MergeStatus(rcStrict, rcStrictCommit, VINF_IOM_R3_MMIO_COMMIT_WRITE, pVCpu);
422 }
423
424 return rcStrict;
425}
426
427
428/**
429 * Notification from DBGF that the number of active I/O port or MMIO
430 * breakpoints has change.
431 *
432 * For performance reasons, IOM will only call DBGF before doing I/O and MMIO
433 * accesses where there are armed breakpoints.
434 *
435 * @param pVM The cross context VM structure.
436 * @param fPortIo True if there are armed I/O port breakpoints.
437 * @param fMmio True if there are armed MMIO breakpoints.
438 */
439VMMR3_INT_DECL(void) IOMR3NotifyBreakpointCountChange(PVM pVM, bool fPortIo, bool fMmio)
440{
441 /** @todo I/O breakpoints. */
442 RT_NOREF3(pVM, fPortIo, fMmio);
443}
444
445
446/**
447 * Notification from DBGF that an event has been enabled or disabled.
448 *
449 * For performance reasons, IOM may cache the state of events it implements.
450 *
451 * @param pVM The cross context VM structure.
452 * @param enmEvent The event.
453 * @param fEnabled The new state.
454 */
455VMMR3_INT_DECL(void) IOMR3NotifyDebugEventChange(PVM pVM, DBGFEVENT enmEvent, bool fEnabled)
456{
457 /** @todo IOM debug events. */
458 RT_NOREF3(pVM, enmEvent, fEnabled);
459}
460
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