VirtualBox

source: vbox/trunk/src/VBox/VMM/include/IOMInternal.h@ 46175

Last change on this file since 46175 was 45312, checked in by vboxsync, 12 years ago

IOM: Start using the read/write crit sect.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 17.4 KB
Line 
1/* $Id: IOMInternal.h 45312 2013-04-03 15:05:29Z vboxsync $ */
2/** @file
3 * IOM - Internal header file.
4 */
5
6/*
7 * Copyright (C) 2006-2013 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#ifndef ___IOMInternal_h
19#define ___IOMInternal_h
20
21#define IOM_WITH_CRIT_SECT_RW
22
23#include <VBox/cdefs.h>
24#include <VBox/types.h>
25#include <VBox/vmm/iom.h>
26#include <VBox/vmm/stam.h>
27#include <VBox/vmm/pgm.h>
28#include <VBox/vmm/pdmcritsect.h>
29#ifdef IOM_WITH_CRIT_SECT_RW
30# include <VBox/vmm/pdmcritsectrw.h>
31#endif
32#include <VBox/param.h>
33#include <iprt/assert.h>
34#include <iprt/avl.h>
35
36
37
38/** @defgroup grp_iom_int Internals
39 * @ingroup grp_iom
40 * @internal
41 * @{
42 */
43
44/**
45 * MMIO range descriptor.
46 */
47typedef struct IOMMMIORANGE
48{
49 /** Avl node core with GCPhys as Key and GCPhys + cbSize - 1 as KeyLast. */
50 AVLROGCPHYSNODECORE Core;
51 /** Start physical address. */
52 RTGCPHYS GCPhys;
53 /** Size of the range. */
54 uint32_t cb;
55 /** The reference counter. */
56 uint32_t volatile cRefs;
57
58 /** Pointer to user argument - R0. */
59 RTR0PTR pvUserR0;
60 /** Pointer to device instance - R0. */
61 PPDMDEVINSR0 pDevInsR0;
62 /** Pointer to write callback function - R0. */
63 R0PTRTYPE(PFNIOMMMIOWRITE) pfnWriteCallbackR0;
64 /** Pointer to read callback function - R0. */
65 R0PTRTYPE(PFNIOMMMIOREAD) pfnReadCallbackR0;
66 /** Pointer to fill (memset) callback function - R0. */
67 R0PTRTYPE(PFNIOMMMIOFILL) pfnFillCallbackR0;
68
69 /** Flags, see IOMMMIO_FLAGS_XXX. */ /* (Placed here for alignment reasons.) */
70 uint32_t fFlags;
71
72 /** Pointer to user argument - RC. */
73 RTRCPTR pvUserRC;
74 /** Pointer to device instance - RC. */
75 PPDMDEVINSRC pDevInsRC;
76 /** Pointer to write callback function - RC. */
77 RCPTRTYPE(PFNIOMMMIOWRITE) pfnWriteCallbackRC;
78 /** Pointer to read callback function - RC. */
79 RCPTRTYPE(PFNIOMMMIOREAD) pfnReadCallbackRC;
80 /** Pointer to fill (memset) callback function - RC. */
81 RCPTRTYPE(PFNIOMMMIOFILL) pfnFillCallbackRC;
82
83 /** Pointer to user argument - R3. */
84 RTR3PTR pvUserR3;
85 /** Pointer to device instance - R3. */
86 PPDMDEVINSR3 pDevInsR3;
87 /** Pointer to write callback function - R3. */
88 R3PTRTYPE(PFNIOMMMIOWRITE) pfnWriteCallbackR3;
89 /** Pointer to read callback function - R3. */
90 R3PTRTYPE(PFNIOMMMIOREAD) pfnReadCallbackR3;
91 /** Pointer to fill (memset) callback function - R3. */
92 R3PTRTYPE(PFNIOMMMIOFILL) pfnFillCallbackR3;
93
94 /** Description / Name. For easing debugging. */
95 R3PTRTYPE(const char *) pszDesc;
96} IOMMMIORANGE;
97/** Pointer to a MMIO range descriptor, R3 version. */
98typedef struct IOMMMIORANGE *PIOMMMIORANGE;
99
100
101/**
102 * MMIO address statistics. (one address)
103 *
104 * This is a simple way of making on demand statistics, however it's a
105 * bit free with the hypervisor heap memory.
106 */
107typedef struct IOMMMIOSTATS
108{
109 /** Avl node core with the address as Key. */
110 AVLOGCPHYSNODECORE Core;
111
112 /** Number of accesses (subtract ReadRZToR3 and WriteRZToR3 to get the right
113 * number). */
114 STAMCOUNTER Accesses;
115
116 /** Profiling read handler overhead in R3. */
117 STAMPROFILE ProfReadR3;
118 /** Profiling write handler overhead in R3. */
119 STAMPROFILE ProfWriteR3;
120 /** Counting and profiling reads in R0/RC. */
121 STAMPROFILE ProfReadRZ;
122 /** Counting and profiling writes in R0/RC. */
123 STAMPROFILE ProfWriteRZ;
124
125 /** Number of reads to this address from R0/RC which was serviced in R3. */
126 STAMCOUNTER ReadRZToR3;
127 /** Number of writes to this address from R0/RC which was serviced in R3. */
128 STAMCOUNTER WriteRZToR3;
129} IOMMMIOSTATS;
130AssertCompileMemberAlignment(IOMMMIOSTATS, Accesses, 8);
131/** Pointer to I/O port statistics. */
132typedef IOMMMIOSTATS *PIOMMMIOSTATS;
133
134
135/**
136 * I/O port range descriptor, R3 version.
137 */
138typedef struct IOMIOPORTRANGER3
139{
140 /** Avl node core with Port as Key and Port + cPorts - 1 as KeyLast. */
141 AVLROIOPORTNODECORE Core;
142#if HC_ARCH_BITS == 64 && !defined(RT_OS_WINDOWS)
143 uint32_t u32Alignment; /**< The sizeof(Core) differs. */
144#endif
145 /** Start I/O port address. */
146 RTIOPORT Port;
147 /** Size of the range. */
148 uint16_t cPorts;
149 /** Pointer to user argument. */
150 RTR3PTR pvUser;
151 /** Pointer to the associated device instance. */
152 R3PTRTYPE(PPDMDEVINS) pDevIns;
153 /** Pointer to OUT callback function. */
154 R3PTRTYPE(PFNIOMIOPORTOUT) pfnOutCallback;
155 /** Pointer to IN callback function. */
156 R3PTRTYPE(PFNIOMIOPORTIN) pfnInCallback;
157 /** Pointer to string OUT callback function. */
158 R3PTRTYPE(PFNIOMIOPORTOUTSTRING) pfnOutStrCallback;
159 /** Pointer to string IN callback function. */
160 R3PTRTYPE(PFNIOMIOPORTINSTRING) pfnInStrCallback;
161 /** Description / Name. For easing debugging. */
162 R3PTRTYPE(const char *) pszDesc;
163} IOMIOPORTRANGER3;
164/** Pointer to I/O port range descriptor, R3 version. */
165typedef IOMIOPORTRANGER3 *PIOMIOPORTRANGER3;
166
167/**
168 * I/O port range descriptor, R0 version.
169 */
170typedef struct IOMIOPORTRANGER0
171{
172 /** Avl node core with Port as Key and Port + cPorts - 1 as KeyLast. */
173 AVLROIOPORTNODECORE Core;
174#if HC_ARCH_BITS == 64 && !defined(RT_OS_WINDOWS)
175 uint32_t u32Alignment; /**< The sizeof(Core) differs. */
176#endif
177 /** Start I/O port address. */
178 RTIOPORT Port;
179 /** Size of the range. */
180 uint16_t cPorts;
181 /** Pointer to user argument. */
182 RTR0PTR pvUser;
183 /** Pointer to the associated device instance. */
184 R0PTRTYPE(PPDMDEVINS) pDevIns;
185 /** Pointer to OUT callback function. */
186 R0PTRTYPE(PFNIOMIOPORTOUT) pfnOutCallback;
187 /** Pointer to IN callback function. */
188 R0PTRTYPE(PFNIOMIOPORTIN) pfnInCallback;
189 /** Pointer to string OUT callback function. */
190 R0PTRTYPE(PFNIOMIOPORTOUTSTRING) pfnOutStrCallback;
191 /** Pointer to string IN callback function. */
192 R0PTRTYPE(PFNIOMIOPORTINSTRING) pfnInStrCallback;
193 /** Description / Name. For easing debugging. */
194 R3PTRTYPE(const char *) pszDesc;
195} IOMIOPORTRANGER0;
196/** Pointer to I/O port range descriptor, R0 version. */
197typedef IOMIOPORTRANGER0 *PIOMIOPORTRANGER0;
198
199/**
200 * I/O port range descriptor, RC version.
201 */
202typedef struct IOMIOPORTRANGERC
203{
204 /** Avl node core with Port as Key and Port + cPorts - 1 as KeyLast. */
205 AVLROIOPORTNODECORE Core;
206 /** Start I/O port address. */
207 RTIOPORT Port;
208 /** Size of the range. */
209 uint16_t cPorts;
210 /** Pointer to user argument. */
211 RTRCPTR pvUser;
212 /** Pointer to the associated device instance. */
213 RCPTRTYPE(PPDMDEVINS) pDevIns;
214 /** Pointer to OUT callback function. */
215 RCPTRTYPE(PFNIOMIOPORTOUT) pfnOutCallback;
216 /** Pointer to IN callback function. */
217 RCPTRTYPE(PFNIOMIOPORTIN) pfnInCallback;
218 /** Pointer to string OUT callback function. */
219 RCPTRTYPE(PFNIOMIOPORTOUTSTRING) pfnOutStrCallback;
220 /** Pointer to string IN callback function. */
221 RCPTRTYPE(PFNIOMIOPORTINSTRING) pfnInStrCallback;
222#if HC_ARCH_BITS == 64
223 RTRCPTR RCPtrAlignment; /**< pszDesc is 8 byte aligned. */
224#endif
225 /** Description / Name. For easing debugging. */
226 R3PTRTYPE(const char *) pszDesc;
227} IOMIOPORTRANGERC;
228/** Pointer to I/O port range descriptor, RC version. */
229typedef IOMIOPORTRANGERC *PIOMIOPORTRANGERC;
230
231
232/**
233 * I/O port statistics. (one I/O port)
234 *
235 * This is a simple way of making on demand statistics, however it's a
236 * bit free with the hypervisor heap memory.
237 */
238typedef struct IOMIOPORTSTATS
239{
240 /** Avl node core with the port as Key. */
241 AVLOIOPORTNODECORE Core;
242#if HC_ARCH_BITS != 64 || !defined(RT_OS_WINDOWS)
243 uint32_t u32Alignment; /**< The sizeof(Core) differs. */
244#endif
245 /** Number of INs to this port from R3. */
246 STAMCOUNTER InR3;
247 /** Profiling IN handler overhead in R3. */
248 STAMPROFILE ProfInR3;
249 /** Number of OUTs to this port from R3. */
250 STAMCOUNTER OutR3;
251 /** Profiling OUT handler overhead in R3. */
252 STAMPROFILE ProfOutR3;
253
254 /** Number of INs to this port from R0/RC. */
255 STAMCOUNTER InRZ;
256 /** Profiling IN handler overhead in R0/RC. */
257 STAMPROFILE ProfInRZ;
258 /** Number of INs to this port from R0/RC which was serviced in R3. */
259 STAMCOUNTER InRZToR3;
260
261 /** Number of OUTs to this port from R0/RC. */
262 STAMCOUNTER OutRZ;
263 /** Profiling OUT handler overhead in R0/RC. */
264 STAMPROFILE ProfOutRZ;
265 /** Number of OUTs to this port from R0/RC which was serviced in R3. */
266 STAMCOUNTER OutRZToR3;
267} IOMIOPORTSTATS;
268AssertCompileMemberAlignment(IOMIOPORTSTATS, InR3, 8);
269/** Pointer to I/O port statistics. */
270typedef IOMIOPORTSTATS *PIOMIOPORTSTATS;
271
272
273/**
274 * The IOM trees.
275 * These are offset based the nodes and root must be in the same
276 * memory block in HC. The locations of IOM structure and the hypervisor heap
277 * are quite different in R3, R0 and RC.
278 */
279typedef struct IOMTREES
280{
281 /** Tree containing I/O port range descriptors registered for HC (IOMIOPORTRANGEHC). */
282 AVLROIOPORTTREE IOPortTreeR3;
283 /** Tree containing I/O port range descriptors registered for R0 (IOMIOPORTRANGER0). */
284 AVLROIOPORTTREE IOPortTreeR0;
285 /** Tree containing I/O port range descriptors registered for RC (IOMIOPORTRANGERC). */
286 AVLROIOPORTTREE IOPortTreeRC;
287
288 /** Tree containing the MMIO range descriptors (IOMMMIORANGE). */
289 AVLROGCPHYSTREE MMIOTree;
290
291 /** Tree containing I/O port statistics (IOMIOPORTSTATS). */
292 AVLOIOPORTTREE IOPortStatTree;
293 /** Tree containing MMIO statistics (IOMMMIOSTATS). */
294 AVLOGCPHYSTREE MmioStatTree;
295} IOMTREES;
296/** Pointer to the IOM trees. */
297typedef IOMTREES *PIOMTREES;
298
299
300/**
301 * Converts an IOM pointer into a VM pointer.
302 * @returns Pointer to the VM structure the PGM is part of.
303 * @param pIOM Pointer to IOM instance data.
304 */
305#define IOM2VM(pIOM) ( (PVM)((char*)pIOM - pIOM->offVM) )
306
307/**
308 * IOM Data (part of VM)
309 */
310typedef struct IOM
311{
312 /** Offset to the VM structure. */
313 RTINT offVM;
314
315 /** Pointer to the trees - RC ptr. */
316 RCPTRTYPE(PIOMTREES) pTreesRC;
317 /** Pointer to the trees - R3 ptr. */
318 R3PTRTYPE(PIOMTREES) pTreesR3;
319 /** Pointer to the trees - R0 ptr. */
320 R0PTRTYPE(PIOMTREES) pTreesR0;
321
322 /** The ring-0 address of IOMMMIOHandler. */
323 R0PTRTYPE(PFNPGMR0PHYSHANDLER) pfnMMIOHandlerR0;
324 /** The RC address of IOMMMIOHandler. */
325 RCPTRTYPE(PFNPGMRCPHYSHANDLER) pfnMMIOHandlerRC;
326#if HC_ARCH_BITS == 64
327 RTRCPTR padding;
328#endif
329
330 /** Lock serializing EMT access to IOM. */
331#ifdef IOM_WITH_CRIT_SECT_RW
332 PDMCRITSECTRW CritSect;
333#else
334 PDMCRITSECT CritSect;
335#endif
336
337 /** @name I/O Port statistics.
338 * @{ */
339 STAMCOUNTER StatInstIn;
340 STAMCOUNTER StatInstOut;
341 STAMCOUNTER StatInstIns;
342 STAMCOUNTER StatInstOuts;
343 /** @} */
344
345 /** @name MMIO statistics.
346 * @{ */
347 STAMPROFILE StatRZMMIOHandler;
348 STAMCOUNTER StatRZMMIOFailures;
349
350 STAMPROFILE StatRZInstMov;
351 STAMPROFILE StatRZInstCmp;
352 STAMPROFILE StatRZInstAnd;
353 STAMPROFILE StatRZInstOr;
354 STAMPROFILE StatRZInstXor;
355 STAMPROFILE StatRZInstBt;
356 STAMPROFILE StatRZInstTest;
357 STAMPROFILE StatRZInstXchg;
358 STAMPROFILE StatRZInstStos;
359 STAMPROFILE StatRZInstLods;
360#ifdef IOM_WITH_MOVS_SUPPORT
361 STAMPROFILEADV StatRZInstMovs;
362 STAMPROFILE StatRZInstMovsToMMIO;
363 STAMPROFILE StatRZInstMovsFromMMIO;
364 STAMPROFILE StatRZInstMovsMMIO;
365#endif
366 STAMCOUNTER StatRZInstOther;
367
368 STAMCOUNTER StatRZMMIO1Byte;
369 STAMCOUNTER StatRZMMIO2Bytes;
370 STAMCOUNTER StatRZMMIO4Bytes;
371 STAMCOUNTER StatRZMMIO8Bytes;
372
373 STAMCOUNTER StatR3MMIOHandler;
374
375 RTUINT cMovsMaxBytes;
376 RTUINT cStosMaxBytes;
377 /** @} */
378} IOM;
379/** Pointer to IOM instance data. */
380typedef IOM *PIOM;
381
382
383/**
384 * IOM per virtual CPU instance data.
385 */
386typedef struct IOMCPU
387{
388 /** For saving stack space, the disassembler state is allocated here instead of
389 * on the stack. */
390 DISCPUSTATE DisState;
391
392 /** @name Caching of I/O Port and MMIO ranges and statistics.
393 * (Saves quite some time in rep outs/ins instruction emulation.)
394 * @{ */
395 R3PTRTYPE(PIOMIOPORTRANGER3) pRangeLastReadR3;
396 R3PTRTYPE(PIOMIOPORTRANGER3) pRangeLastWriteR3;
397 R3PTRTYPE(PIOMIOPORTSTATS) pStatsLastReadR3;
398 R3PTRTYPE(PIOMIOPORTSTATS) pStatsLastWriteR3;
399 R3PTRTYPE(PIOMMMIORANGE) pMMIORangeLastR3;
400 R3PTRTYPE(PIOMMMIOSTATS) pMMIOStatsLastR3;
401
402 R0PTRTYPE(PIOMIOPORTRANGER0) pRangeLastReadR0;
403 R0PTRTYPE(PIOMIOPORTRANGER0) pRangeLastWriteR0;
404 R0PTRTYPE(PIOMIOPORTSTATS) pStatsLastReadR0;
405 R0PTRTYPE(PIOMIOPORTSTATS) pStatsLastWriteR0;
406 R0PTRTYPE(PIOMMMIORANGE) pMMIORangeLastR0;
407 R0PTRTYPE(PIOMMMIOSTATS) pMMIOStatsLastR0;
408
409 RCPTRTYPE(PIOMIOPORTRANGERC) pRangeLastReadRC;
410 RCPTRTYPE(PIOMIOPORTRANGERC) pRangeLastWriteRC;
411 RCPTRTYPE(PIOMIOPORTSTATS) pStatsLastReadRC;
412 RCPTRTYPE(PIOMIOPORTSTATS) pStatsLastWriteRC;
413 RCPTRTYPE(PIOMMMIORANGE) pMMIORangeLastRC;
414 RCPTRTYPE(PIOMMMIOSTATS) pMMIOStatsLastRC;
415 /** @} */
416} IOMCPU;
417/** Pointer to IOM per virtual CPU instance data. */
418typedef IOMCPU *PIOMCPU;
419
420
421RT_C_DECLS_BEGIN
422
423void iomMmioFreeRange(PVM pVM, PIOMMMIORANGE pRange);
424#ifdef IN_RING3
425PIOMMMIOSTATS iomR3MMIOStatsCreate(PVM pVM, RTGCPHYS GCPhys, const char *pszDesc);
426#endif /* IN_RING3 */
427
428VMMDECL(int) IOMMMIOHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault,
429 RTGCPHYS GCPhysFault, void *pvUser);
430#ifdef IN_RING3
431DECLCALLBACK(int) IOMR3MMIOHandler(PVM pVM, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf,
432 PGMACCESSTYPE enmAccessType, void *pvUser);
433#endif
434
435/* IOM locking helpers. */
436#ifdef IOM_WITH_CRIT_SECT_RW
437# define IOM_LOCK_EXCL(a_pVM) PDMCritSectRwEnterExcl(&(a_pVM)->iom.s.CritSect, VERR_SEM_BUSY)
438# define IOM_UNLOCK_EXCL(a_pVM) do { PDMCritSectRwLeaveExcl(&(a_pVM)->iom.s.CritSect); } while (0)
439# if 0 /* (in case needed for debugging) */
440# define IOM_LOCK_SHARED_EX(a_pVM, a_rcBusy) PDMCritSectRwEnterExcl(&(a_pVM)->iom.s.CritSect, (a_rcBusy))
441# define IOM_UNLOCK_SHARED(a_pVM) do { PDMCritSectRwLeaveExcl(&(a_pVM)->iom.s.CritSect); } while (0)
442# define IOM_IS_SHARED_LOCK_OWNER(a_pVM) PDMCritSectRwIsWriteOwner(&(a_pVM)->iom.s.CritSect)
443# else
444# define IOM_LOCK_SHARED_EX(a_pVM, a_rcBusy) PDMCritSectRwEnterShared(&(a_pVM)->iom.s.CritSect, (a_rcBusy))
445# define IOM_UNLOCK_SHARED(a_pVM) do { PDMCritSectRwLeaveShared(&(a_pVM)->iom.s.CritSect); } while (0)
446# define IOM_IS_SHARED_LOCK_OWNER(a_pVM) PDMCritSectRwIsReadOwner(&(a_pVM)->iom.s.CritSect, true)
447# endif
448# define IOM_IS_EXCL_LOCK_OWNER(a_pVM) PDMCritSectRwIsWriteOwner(&(a_pVM)->iom.s.CritSect)
449#else
450# define IOM_LOCK_EXCL(a_pVM) PDMCritSectEnter(&(a_pVM)->iom.s.CritSect, VERR_SEM_BUSY)
451# define IOM_UNLOCK_EXCL(a_pVM) do { PDMCritSectLeave(&(a_pVM)->iom.s.CritSect); } while (0)
452# define IOM_LOCK_SHARED_EX(a_pVM, a_rcBusy) PDMCritSectEnter(&(a_pVM)->iom.s.CritSect, (a_rcBusy))
453# define IOM_UNLOCK_SHARED(a_pVM) do { PDMCritSectLeave(&(a_pVM)->iom.s.CritSect); } while (0)
454# define IOM_IS_SHARED_LOCK_OWNER(a_pVM) PDMCritSectIsOwner(&(a_pVM)->iom.s.CritSect)
455# define IOM_IS_EXCL_LOCK_OWNER(a_pVM) PDMCritSectIsOwner(&(a_pVM)->iom.s.CritSect)
456#endif
457#define IOM_LOCK_SHARED(a_pVM) IOM_LOCK_SHARED_EX(a_pVM, VERR_SEM_BUSY)
458
459
460/* Disassembly helpers used in IOMAll.cpp & IOMAllMMIO.cpp */
461bool iomGetRegImmData(PDISCPUSTATE pCpu, PCDISOPPARAM pParam, PCPUMCTXCORE pRegFrame, uint64_t *pu64Data, unsigned *pcbSize);
462bool iomSaveDataToReg(PDISCPUSTATE pCpu, PCDISOPPARAM pParam, PCPUMCTXCORE pRegFrame, uint64_t u32Data);
463
464RT_C_DECLS_END
465
466
467#ifdef IN_RING3
468
469#endif
470
471/** @} */
472
473#endif /* !___IOMInternal_h */
474
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