VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/IOMR3Mmio.cpp@ 92473

Last change on this file since 92473 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: 23.3 KB
Line 
1/* $Id: IOMR3Mmio.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * IOM - Input / Output Monitor, MMIO related APIs.
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/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_IOM_MMIO
23#include <VBox/vmm/iom.h>
24#include <VBox/sup.h>
25#include <VBox/vmm/mm.h>
26#include <VBox/vmm/stam.h>
27#include <VBox/vmm/dbgf.h>
28#include <VBox/vmm/pdmapi.h>
29#include <VBox/vmm/pdmdev.h>
30#include "IOMInternal.h"
31#include <VBox/vmm/vm.h>
32
33#include <VBox/param.h>
34#include <iprt/assert.h>
35#include <iprt/string.h>
36#include <VBox/log.h>
37#include <VBox/err.h>
38
39#include "IOMInline.h"
40
41
42#ifdef VBOX_WITH_STATISTICS
43
44/**
45 * Register statistics for a MMIO entry.
46 */
47void iomR3MmioRegStats(PVM pVM, PIOMMMIOENTRYR3 pRegEntry)
48{
49 bool const fDoRZ = pRegEntry->fRing0 || pRegEntry->fRawMode;
50 PIOMMMIOSTATSENTRY pStats = &pVM->iom.s.paMmioStats[pRegEntry->idxStats];
51
52 /* Format the prefix: */
53 char szName[80];
54 size_t cchPrefix = RTStrPrintf(szName, sizeof(szName), "/IOM/MmioRegions/%RGp-%RGp",
55 pRegEntry->GCPhysMapping, pRegEntry->GCPhysMapping + pRegEntry->cbRegion - 1);
56
57 /* Mangle the description if this isn't the first device instance: */
58 const char *pszDesc = pRegEntry->pszDesc;
59 char *pszFreeDesc = NULL;
60 if (pRegEntry->pDevIns && pRegEntry->pDevIns->iInstance > 0 && pszDesc)
61 pszDesc = pszFreeDesc = RTStrAPrintf2("%u / %s", pRegEntry->pDevIns->iInstance, pszDesc);
62
63 /* Register statistics: */
64 int rc = STAMR3Register(pVM, &pRegEntry->idxSelf, STAMTYPE_U16, STAMVISIBILITY_ALWAYS, szName, STAMUNIT_NONE, pszDesc); AssertRC(rc);
65 RTStrFree(pszFreeDesc);
66
67# define SET_NM_SUFFIX(a_sz) memcpy(&szName[cchPrefix], a_sz, sizeof(a_sz))
68 SET_NM_SUFFIX("/Read-Complicated");
69 rc = STAMR3Register(pVM, &pStats->ComplicatedReads, STAMTYPE_COUNTER, STAMVISIBILITY_USED, szName, STAMUNIT_OCCURENCES, NULL); AssertRC(rc);
70 SET_NM_SUFFIX("/Read-FFor00");
71 rc = STAMR3Register(pVM, &pStats->FFor00Reads, STAMTYPE_COUNTER, STAMVISIBILITY_USED, szName, STAMUNIT_OCCURENCES, NULL); AssertRC(rc);
72 SET_NM_SUFFIX("/Read-R3");
73 rc = STAMR3Register(pVM, &pStats->ProfReadR3, STAMTYPE_PROFILE, STAMVISIBILITY_USED, szName, STAMUNIT_TICKS_PER_CALL, NULL); AssertRC(rc);
74 if (fDoRZ)
75 {
76 SET_NM_SUFFIX("/Read-RZ");
77 rc = STAMR3Register(pVM, &pStats->ProfReadRZ, STAMTYPE_PROFILE, STAMVISIBILITY_USED, szName, STAMUNIT_TICKS_PER_CALL, NULL); AssertRC(rc);
78 SET_NM_SUFFIX("/Read-RZtoR3");
79 rc = STAMR3Register(pVM, &pStats->ReadRZToR3, STAMTYPE_COUNTER, STAMVISIBILITY_USED, szName, STAMUNIT_OCCURENCES, NULL); AssertRC(rc);
80 }
81 SET_NM_SUFFIX("/Read-Total");
82 rc = STAMR3Register(pVM, &pStats->Reads, STAMTYPE_COUNTER, STAMVISIBILITY_USED, szName, STAMUNIT_OCCURENCES, NULL); AssertRC(rc);
83
84 SET_NM_SUFFIX("/Write-Complicated");
85 rc = STAMR3Register(pVM, &pStats->ComplicatedWrites, STAMTYPE_COUNTER, STAMVISIBILITY_USED, szName, STAMUNIT_OCCURENCES, NULL); AssertRC(rc);
86 SET_NM_SUFFIX("/Write-R3");
87 rc = STAMR3Register(pVM, &pStats->ProfWriteR3, STAMTYPE_PROFILE, STAMVISIBILITY_USED, szName, STAMUNIT_TICKS_PER_CALL, NULL); AssertRC(rc);
88 if (fDoRZ)
89 {
90 SET_NM_SUFFIX("/Write-RZ");
91 rc = STAMR3Register(pVM, &pStats->ProfWriteRZ, STAMTYPE_PROFILE, STAMVISIBILITY_USED, szName, STAMUNIT_TICKS_PER_CALL, NULL); AssertRC(rc);
92 SET_NM_SUFFIX("/Write-RZtoR3");
93 rc = STAMR3Register(pVM, &pStats->WriteRZToR3, STAMTYPE_COUNTER, STAMVISIBILITY_USED, szName, STAMUNIT_OCCURENCES, NULL); AssertRC(rc);
94 SET_NM_SUFFIX("/Write-RZtoR3-Commit");
95 rc = STAMR3Register(pVM, &pStats->CommitRZToR3, STAMTYPE_COUNTER, STAMVISIBILITY_USED, szName, STAMUNIT_OCCURENCES, NULL); AssertRC(rc);
96 }
97 SET_NM_SUFFIX("/Write-Total");
98 rc = STAMR3Register(pVM, &pStats->Writes, STAMTYPE_COUNTER, STAMVISIBILITY_USED, szName, STAMUNIT_OCCURENCES, NULL); AssertRC(rc);
99}
100
101
102/**
103 * Deregister statistics for a MMIO entry.
104 */
105static void iomR3MmioDeregStats(PVM pVM, PIOMMMIOENTRYR3 pRegEntry, RTGCPHYS GCPhys)
106{
107 char szPrefix[80];
108 RTStrPrintf(szPrefix, sizeof(szPrefix), "/IOM/MmioRegions/%RGp-%RGp", GCPhys, GCPhys + pRegEntry->cbRegion - 1);
109 STAMR3DeregisterByPrefix(pVM->pUVM, szPrefix);
110}
111
112#endif /* VBOX_WITH_STATISTICS */
113
114
115/**
116 * Worker for PDMDEVHLPR3::pfnMmioCreateEx.
117 */
118VMMR3_INT_DECL(int) IOMR3MmioCreate(PVM pVM, PPDMDEVINS pDevIns, RTGCPHYS cbRegion, uint32_t fFlags, PPDMPCIDEV pPciDev,
119 uint32_t iPciRegion, PFNIOMMMIONEWWRITE pfnWrite, PFNIOMMMIONEWREAD pfnRead,
120 PFNIOMMMIONEWFILL pfnFill, void *pvUser, const char *pszDesc, PIOMMMIOHANDLE phRegion)
121{
122 /*
123 * Validate input.
124 */
125 AssertPtrReturn(phRegion, VERR_INVALID_POINTER);
126 *phRegion = UINT32_MAX;
127 VM_ASSERT_EMT0_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
128 VM_ASSERT_STATE_RETURN(pVM, VMSTATE_CREATING, VERR_VM_INVALID_VM_STATE);
129
130 AssertPtrReturn(pDevIns, VERR_INVALID_POINTER);
131
132 AssertMsgReturn(cbRegion > 0 && cbRegion <= MM_MMIO_64_MAX, ("cbRegion=%#RGp (max %#RGp)\n", cbRegion, MM_MMIO_64_MAX),
133 VERR_OUT_OF_RANGE);
134 AssertMsgReturn(!(cbRegion & PAGE_OFFSET_MASK), ("cbRegion=%#RGp\n", cbRegion), VERR_UNSUPPORTED_ALIGNMENT);
135
136 AssertMsgReturn( !(fFlags & ~IOMMMIO_FLAGS_VALID_MASK)
137 && (fFlags & IOMMMIO_FLAGS_READ_MODE) <= IOMMMIO_FLAGS_READ_DWORD_QWORD
138 && (fFlags & IOMMMIO_FLAGS_WRITE_MODE) <= IOMMMIO_FLAGS_WRITE_ONLY_DWORD_QWORD,
139 ("%#x\n", fFlags),
140 VERR_INVALID_FLAGS);
141
142 AssertReturn(pfnWrite || pfnRead, VERR_INVALID_PARAMETER);
143 AssertPtrNullReturn(pfnWrite, VERR_INVALID_POINTER);
144 AssertPtrNullReturn(pfnRead, VERR_INVALID_POINTER);
145 AssertPtrNullReturn(pfnFill, VERR_INVALID_POINTER);
146
147 AssertPtrReturn(pszDesc, VERR_INVALID_POINTER);
148 AssertReturn(*pszDesc != '\0', VERR_INVALID_POINTER);
149 AssertReturn(strlen(pszDesc) < 128, VERR_INVALID_POINTER);
150
151 /*
152 * Ensure that we've got table space for it.
153 */
154#ifndef VBOX_WITH_STATISTICS
155 uint16_t const idxStats = UINT16_MAX;
156#else
157 uint32_t const idxStats = pVM->iom.s.cMmioStats;
158 uint32_t const cNewMmioStats = idxStats + 1;
159 AssertReturn(cNewMmioStats <= _64K, VERR_IOM_TOO_MANY_MMIO_REGISTRATIONS);
160 if (cNewMmioStats > pVM->iom.s.cMmioStatsAllocation)
161 {
162 int rc = VMMR3CallR0Emt(pVM, pVM->apCpusR3[0], VMMR0_DO_IOM_GROW_MMIO_STATS, cNewMmioStats, NULL);
163 AssertLogRelRCReturn(rc, rc);
164 AssertReturn(idxStats == pVM->iom.s.cMmioStats, VERR_IOM_MMIO_IPE_1);
165 AssertReturn(cNewMmioStats <= pVM->iom.s.cMmioStatsAllocation, VERR_IOM_MMIO_IPE_2);
166 }
167#endif
168
169 uint32_t idx = pVM->iom.s.cMmioRegs;
170 if (idx >= pVM->iom.s.cMmioAlloc)
171 {
172 int rc = VMMR3CallR0Emt(pVM, pVM->apCpusR3[0], VMMR0_DO_IOM_GROW_MMIO_REGS, pVM->iom.s.cMmioAlloc + 1, NULL);
173 AssertLogRelRCReturn(rc, rc);
174 AssertReturn(idx == pVM->iom.s.cMmioRegs, VERR_IOM_MMIO_IPE_1);
175 AssertReturn(idx < pVM->iom.s.cMmioAlloc, VERR_IOM_MMIO_IPE_2);
176 }
177
178 /*
179 * Enter it.
180 */
181 pVM->iom.s.paMmioRegs[idx].cbRegion = cbRegion;
182 pVM->iom.s.paMmioRegs[idx].GCPhysMapping = NIL_RTGCPHYS;
183 pVM->iom.s.paMmioRegs[idx].pvUser = pvUser;
184 pVM->iom.s.paMmioRegs[idx].pDevIns = pDevIns;
185 pVM->iom.s.paMmioRegs[idx].pfnWriteCallback = pfnWrite;
186 pVM->iom.s.paMmioRegs[idx].pfnReadCallback = pfnRead;
187 pVM->iom.s.paMmioRegs[idx].pfnFillCallback = pfnFill;
188 pVM->iom.s.paMmioRegs[idx].pszDesc = pszDesc;
189 pVM->iom.s.paMmioRegs[idx].pPciDev = pPciDev;
190 pVM->iom.s.paMmioRegs[idx].iPciRegion = iPciRegion;
191 pVM->iom.s.paMmioRegs[idx].idxStats = (uint16_t)idxStats;
192 pVM->iom.s.paMmioRegs[idx].fMapped = false;
193 pVM->iom.s.paMmioRegs[idx].fFlags = fFlags;
194 pVM->iom.s.paMmioRegs[idx].idxSelf = idx;
195
196 pVM->iom.s.cMmioRegs = idx + 1;
197#ifdef VBOX_WITH_STATISTICS
198 pVM->iom.s.cMmioStats = cNewMmioStats;
199#endif
200 *phRegion = idx;
201 return VINF_SUCCESS;
202}
203
204
205/**
206 * Worker for PDMDEVHLPR3::pfnMmioMap.
207 */
208VMMR3_INT_DECL(int) IOMR3MmioMap(PVM pVM, PPDMDEVINS pDevIns, IOMMMIOHANDLE hRegion, RTGCPHYS GCPhys)
209{
210 /*
211 * Validate input and state.
212 */
213 AssertPtrReturn(pDevIns, VERR_INVALID_HANDLE);
214 AssertReturn(hRegion < pVM->iom.s.cMmioRegs, VERR_IOM_INVALID_MMIO_HANDLE);
215 PIOMMMIOENTRYR3 const pRegEntry = &pVM->iom.s.paMmioRegs[hRegion];
216 AssertReturn(pRegEntry->pDevIns == pDevIns, VERR_IOM_INVALID_MMIO_HANDLE);
217
218 RTGCPHYS const cbRegion = pRegEntry->cbRegion;
219 AssertMsgReturn(cbRegion > 0 && cbRegion <= MM_MMIO_64_MAX, ("cbRegion=%RGp\n", cbRegion), VERR_IOM_MMIO_IPE_1);
220 RTGCPHYS const GCPhysLast = GCPhys + cbRegion - 1;
221
222 AssertLogRelMsgReturn(!(GCPhys & PAGE_OFFSET_MASK),
223 ("Misaligned! GCPhys=%RGp LB %RGp %s (%s[#%u])\n",
224 GCPhys, cbRegion, pRegEntry->pszDesc, pDevIns->pReg->szName, pDevIns->iInstance),
225 VERR_IOM_INVALID_MMIO_RANGE);
226 AssertLogRelMsgReturn(GCPhysLast > GCPhys,
227 ("Wrapped! GCPhys=%RGp LB %RGp %s (%s[#%u])\n",
228 GCPhys, cbRegion, pRegEntry->pszDesc, pDevIns->pReg->szName, pDevIns->iInstance),
229 VERR_IOM_INVALID_MMIO_RANGE);
230
231 /*
232 * Do the mapping.
233 */
234 int rc = VINF_SUCCESS;
235 IOM_LOCK_EXCL(pVM);
236
237 if (!pRegEntry->fMapped)
238 {
239 uint32_t const cEntries = RT_MIN(pVM->iom.s.cMmioLookupEntries, pVM->iom.s.cMmioRegs);
240 Assert(pVM->iom.s.cMmioLookupEntries == cEntries);
241
242 PIOMMMIOLOOKUPENTRY paEntries = pVM->iom.s.paMmioLookup;
243 PIOMMMIOLOOKUPENTRY pEntry;
244 if (cEntries > 0)
245 {
246 uint32_t iFirst = 0;
247 uint32_t iEnd = cEntries;
248 uint32_t i = cEntries / 2;
249 for (;;)
250 {
251 pEntry = &paEntries[i];
252 if (pEntry->GCPhysLast < GCPhys)
253 {
254 i += 1;
255 if (i < iEnd)
256 iFirst = i;
257 else
258 {
259 /* Register with PGM before we shuffle the array: */
260 ASMAtomicWriteU64(&pRegEntry->GCPhysMapping, GCPhys);
261 rc = PGMR3PhysMMIORegister(pVM, GCPhys, cbRegion, pVM->iom.s.hNewMmioHandlerType,
262 (void *)(uintptr_t)hRegion, hRegion, hRegion, pRegEntry->pszDesc);
263 AssertRCReturnStmt(rc, ASMAtomicWriteU64(&pRegEntry->GCPhysMapping, NIL_RTGCPHYS); IOM_UNLOCK_EXCL(pVM), rc);
264
265 /* Insert after the entry we just considered: */
266 pEntry += 1;
267 if (i < cEntries)
268 memmove(pEntry + 1, pEntry, sizeof(*pEntry) * (cEntries - i));
269 break;
270 }
271 }
272 else if (pEntry->GCPhysFirst > GCPhysLast)
273 {
274 if (i > iFirst)
275 iEnd = i;
276 else
277 {
278 /* Register with PGM before we shuffle the array: */
279 ASMAtomicWriteU64(&pRegEntry->GCPhysMapping, GCPhys);
280 rc = PGMR3PhysMMIORegister(pVM, GCPhys, cbRegion, pVM->iom.s.hNewMmioHandlerType,
281 (void *)(uintptr_t)hRegion, hRegion, hRegion, pRegEntry->pszDesc);
282 AssertRCReturnStmt(rc, ASMAtomicWriteU64(&pRegEntry->GCPhysMapping, NIL_RTGCPHYS); IOM_UNLOCK_EXCL(pVM), rc);
283
284 /* Insert at the entry we just considered: */
285 if (i < cEntries)
286 memmove(pEntry + 1, pEntry, sizeof(*pEntry) * (cEntries - i));
287 break;
288 }
289 }
290 else
291 {
292 /* Oops! We've got a conflict. */
293 AssertLogRelMsgFailed(("%RGp..%RGp (%s) conflicts with existing mapping %RGp..%RGp (%s)\n",
294 GCPhys, GCPhysLast, pRegEntry->pszDesc,
295 pEntry->GCPhysFirst, pEntry->GCPhysLast, pVM->iom.s.paMmioRegs[pEntry->idx].pszDesc));
296 IOM_UNLOCK_EXCL(pVM);
297 return VERR_IOM_MMIO_RANGE_CONFLICT;
298 }
299
300 i = iFirst + (iEnd - iFirst) / 2;
301 }
302 }
303 else
304 {
305 /* First entry in the lookup table: */
306 ASMAtomicWriteU64(&pRegEntry->GCPhysMapping, GCPhys);
307 rc = PGMR3PhysMMIORegister(pVM, GCPhys, cbRegion, pVM->iom.s.hNewMmioHandlerType,
308 (void *)(uintptr_t)hRegion, hRegion, hRegion, pRegEntry->pszDesc);
309 AssertRCReturnStmt(rc, ASMAtomicWriteU64(&pRegEntry->GCPhysMapping, NIL_RTGCPHYS); IOM_UNLOCK_EXCL(pVM), rc);
310
311 pEntry = paEntries;
312 }
313
314 /*
315 * Fill in the entry and bump the table size.
316 */
317 pRegEntry->fMapped = true;
318 pEntry->idx = hRegion;
319 pEntry->GCPhysFirst = GCPhys;
320 pEntry->GCPhysLast = GCPhysLast;
321 pVM->iom.s.cMmioLookupEntries = cEntries + 1;
322
323#ifdef VBOX_WITH_STATISTICS
324 /* Don't register stats here when we're creating the VM as the
325 statistics table may still be reallocated. */
326 if (pVM->enmVMState >= VMSTATE_CREATED)
327 iomR3MmioRegStats(pVM, pRegEntry);
328#endif
329
330#ifdef VBOX_STRICT
331 /*
332 * Assert table sanity.
333 */
334 AssertMsg(paEntries[0].GCPhysLast >= paEntries[0].GCPhysFirst, ("%RGp %RGp\n", paEntries[0].GCPhysLast, paEntries[0].GCPhysFirst));
335 AssertMsg(paEntries[0].idx < pVM->iom.s.cMmioRegs, ("%#x %#x\n", paEntries[0].idx, pVM->iom.s.cMmioRegs));
336
337 RTGCPHYS GCPhysPrev = paEntries[0].GCPhysLast;
338 for (size_t i = 1; i <= cEntries; i++)
339 {
340 AssertMsg(paEntries[i].GCPhysLast >= paEntries[i].GCPhysFirst, ("%u: %RGp %RGp\n", i, paEntries[i].GCPhysLast, paEntries[i].GCPhysFirst));
341 AssertMsg(paEntries[i].idx < pVM->iom.s.cMmioRegs, ("%u: %#x %#x\n", i, paEntries[i].idx, pVM->iom.s.cMmioRegs));
342 AssertMsg(GCPhysPrev < paEntries[i].GCPhysFirst, ("%u: %RGp %RGp\n", i, GCPhysPrev, paEntries[i].GCPhysFirst));
343 GCPhysPrev = paEntries[i].GCPhysLast;
344 }
345#endif
346 }
347 else
348 {
349 AssertFailed();
350 rc = VERR_IOM_MMIO_REGION_ALREADY_MAPPED;
351 }
352
353 IOM_UNLOCK_EXCL(pVM);
354 return rc;
355}
356
357
358/**
359 * Worker for PDMDEVHLPR3::pfnMmioUnmap.
360 */
361VMMR3_INT_DECL(int) IOMR3MmioUnmap(PVM pVM, PPDMDEVINS pDevIns, IOMMMIOHANDLE hRegion)
362{
363 /*
364 * Validate input and state.
365 */
366 AssertPtrReturn(pDevIns, VERR_INVALID_HANDLE);
367 AssertReturn(hRegion < pVM->iom.s.cMmioRegs, VERR_IOM_INVALID_MMIO_HANDLE);
368 PIOMMMIOENTRYR3 const pRegEntry = &pVM->iom.s.paMmioRegs[hRegion];
369 AssertReturn(pRegEntry->pDevIns == pDevIns, VERR_IOM_INVALID_MMIO_HANDLE);
370
371 /*
372 * Do the mapping.
373 */
374 int rc;
375 IOM_LOCK_EXCL(pVM);
376
377 if (pRegEntry->fMapped)
378 {
379 RTGCPHYS const GCPhys = pRegEntry->GCPhysMapping;
380 RTGCPHYS const GCPhysLast = GCPhys + pRegEntry->cbRegion - 1;
381 uint32_t const cEntries = RT_MIN(pVM->iom.s.cMmioLookupEntries, pVM->iom.s.cMmioRegs);
382 Assert(pVM->iom.s.cMmioLookupEntries == cEntries);
383 Assert(cEntries > 0);
384
385 PIOMMMIOLOOKUPENTRY paEntries = pVM->iom.s.paMmioLookup;
386 uint32_t iFirst = 0;
387 uint32_t iEnd = cEntries;
388 uint32_t i = cEntries / 2;
389 for (;;)
390 {
391 PIOMMMIOLOOKUPENTRY pEntry = &paEntries[i];
392 if (pEntry->GCPhysLast < GCPhys)
393 {
394 i += 1;
395 if (i < iEnd)
396 iFirst = i;
397 else
398 {
399 rc = VERR_IOM_MMIO_IPE_1;
400 AssertLogRelMsgFailedBreak(("%RGp..%RGp (%s) not found!\n", GCPhys, GCPhysLast, pRegEntry->pszDesc));
401 }
402 }
403 else if (pEntry->GCPhysFirst > GCPhysLast)
404 {
405 if (i > iFirst)
406 iEnd = i;
407 else
408 {
409 rc = VERR_IOM_MMIO_IPE_1;
410 AssertLogRelMsgFailedBreak(("%RGp..%RGp (%s) not found!\n", GCPhys, GCPhysLast, pRegEntry->pszDesc));
411 }
412 }
413 else if (pEntry->idx == hRegion)
414 {
415 Assert(pEntry->GCPhysFirst == GCPhys);
416 Assert(pEntry->GCPhysLast == GCPhysLast);
417#ifdef VBOX_WITH_STATISTICS
418 iomR3MmioDeregStats(pVM, pRegEntry, GCPhys);
419#endif
420 if (i + 1 < cEntries)
421 memmove(pEntry, pEntry + 1, sizeof(*pEntry) * (cEntries - i - 1));
422 pVM->iom.s.cMmioLookupEntries = cEntries - 1;
423
424 rc = PGMR3PhysMMIODeregister(pVM, GCPhys, pRegEntry->cbRegion);
425 AssertRC(rc);
426
427 pRegEntry->fMapped = false;
428 ASMAtomicWriteU64(&pRegEntry->GCPhysMapping, NIL_RTGCPHYS);
429 break;
430 }
431 else
432 {
433 AssertLogRelMsgFailed(("Lookig for %RGp..%RGp (%s), found %RGp..%RGp (%s) instead!\n",
434 GCPhys, GCPhysLast, pRegEntry->pszDesc,
435 pEntry->GCPhysFirst, pEntry->GCPhysLast, pVM->iom.s.paMmioRegs[pEntry->idx].pszDesc));
436 rc = VERR_IOM_MMIO_IPE_1;
437 break;
438 }
439
440 i = iFirst + (iEnd - iFirst) / 2;
441 }
442
443#ifdef VBOX_STRICT
444 /*
445 * Assert table sanity.
446 */
447 AssertMsg(paEntries[0].GCPhysLast >= paEntries[0].GCPhysFirst, ("%RGp %RGp\n", paEntries[0].GCPhysLast, paEntries[0].GCPhysFirst));
448 AssertMsg(paEntries[0].idx < pVM->iom.s.cMmioRegs, ("%#x %#x\n", paEntries[0].idx, pVM->iom.s.cMmioRegs));
449
450 RTGCPHYS GCPhysPrev = paEntries[0].GCPhysLast;
451 for (i = 1; i < cEntries - 1; i++)
452 {
453 AssertMsg(paEntries[i].GCPhysLast >= paEntries[i].GCPhysFirst, ("%u: %RGp %RGp\n", i, paEntries[i].GCPhysLast, paEntries[i].GCPhysFirst));
454 AssertMsg(paEntries[i].idx < pVM->iom.s.cMmioRegs, ("%u: %#x %#x\n", i, paEntries[i].idx, pVM->iom.s.cMmioRegs));
455 AssertMsg(GCPhysPrev < paEntries[i].GCPhysFirst, ("%u: %RGp %RGp\n", i, GCPhysPrev, paEntries[i].GCPhysFirst));
456 GCPhysPrev = paEntries[i].GCPhysLast;
457 }
458#endif
459 }
460 else
461 {
462 AssertFailed();
463 rc = VERR_IOM_MMIO_REGION_NOT_MAPPED;
464 }
465
466 IOM_UNLOCK_EXCL(pVM);
467 return rc;
468}
469
470
471VMMR3_INT_DECL(int) IOMR3MmioReduce(PVM pVM, PPDMDEVINS pDevIns, IOMMMIOHANDLE hRegion, RTGCPHYS cbRegion)
472{
473 RT_NOREF(pVM, pDevIns, hRegion, cbRegion);
474 return VERR_NOT_IMPLEMENTED;
475}
476
477
478/**
479 * Validates @a hRegion, making sure it belongs to @a pDevIns.
480 *
481 * @returns VBox status code.
482 * @param pVM The cross context VM structure.
483 * @param pDevIns The device which allegedly owns @a hRegion.
484 * @param hRegion The handle to validate.
485 */
486VMMR3_INT_DECL(int) IOMR3MmioValidateHandle(PVM pVM, PPDMDEVINS pDevIns, IOMMMIOHANDLE hRegion)
487{
488 AssertPtrReturn(pDevIns, VERR_INVALID_HANDLE);
489 AssertReturn(hRegion < RT_MIN(pVM->iom.s.cMmioRegs, pVM->iom.s.cMmioAlloc), VERR_IOM_INVALID_MMIO_HANDLE);
490 PIOMMMIOENTRYR3 const pRegEntry = &pVM->iom.s.paMmioRegs[hRegion];
491 AssertReturn(pRegEntry->pDevIns == pDevIns, VERR_IOM_INVALID_MMIO_HANDLE);
492 return VINF_SUCCESS;
493}
494
495
496/**
497 * Gets the mapping address of MMIO region @a hRegion.
498 *
499 * @returns Mapping address if mapped, NIL_RTGCPHYS if not mapped or invalid
500 * input.
501 * @param pVM The cross context VM structure.
502 * @param pDevIns The device which allegedly owns @a hRegion.
503 * @param hRegion The handle to validate.
504 */
505VMMR3_INT_DECL(RTGCPHYS) IOMR3MmioGetMappingAddress(PVM pVM, PPDMDEVINS pDevIns, IOMMMIOHANDLE hRegion)
506{
507 AssertPtrReturn(pDevIns, NIL_RTGCPHYS);
508 AssertReturn(hRegion < RT_MIN(pVM->iom.s.cMmioRegs, pVM->iom.s.cMmioAlloc), NIL_RTGCPHYS);
509 PIOMMMIOENTRYR3 const pRegEntry = &pVM->iom.s.paMmioRegs[hRegion];
510 AssertReturn(pRegEntry->pDevIns == pDevIns, NIL_RTGCPHYS);
511 return pRegEntry->GCPhysMapping;
512}
513
514
515/**
516 * Display all registered MMIO ranges.
517 *
518 * @param pVM The cross context VM structure.
519 * @param pHlp The info helpers.
520 * @param pszArgs Arguments, ignored.
521 */
522DECLCALLBACK(void) iomR3MmioInfo(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
523{
524 RT_NOREF(pszArgs);
525
526 /* No locking needed here as registerations are only happening during VMSTATE_CREATING. */
527 pHlp->pfnPrintf(pHlp,
528 "MMIO registrations: %u (%u allocated)\n"
529 " ## Ctx %.*s %.*s PCI Description\n",
530 pVM->iom.s.cMmioRegs, pVM->iom.s.cMmioAlloc,
531 sizeof(RTGCPHYS) * 2, "Size",
532 sizeof(RTGCPHYS) * 2 * 2 + 1, "Mapping");
533 PIOMMMIOENTRYR3 paRegs = pVM->iom.s.paMmioRegs;
534 for (uint32_t i = 0; i < pVM->iom.s.cMmioRegs; i++)
535 {
536 const char * const pszRing = paRegs[i].fRing0 ? paRegs[i].fRawMode ? "+0+C" : "+0 "
537 : paRegs[i].fRawMode ? "+C " : " ";
538 if (paRegs[i].fMapped && paRegs[i].pPciDev)
539 pHlp->pfnPrintf(pHlp, "%3u R3%s %RGp %RGp-%RGp pci%u/%u %s\n", paRegs[i].idxSelf, pszRing, paRegs[i].cbRegion,
540 paRegs[i].GCPhysMapping, paRegs[i].GCPhysMapping + paRegs[i].cbRegion - 1,
541 paRegs[i].pPciDev->idxSubDev, paRegs[i].iPciRegion, paRegs[i].pszDesc);
542 else if (paRegs[i].fMapped && !paRegs[i].pPciDev)
543 pHlp->pfnPrintf(pHlp, "%3u R3%s %RGp %RGp-%RGp %s\n", paRegs[i].idxSelf, pszRing, paRegs[i].cbRegion,
544 paRegs[i].GCPhysMapping, paRegs[i].GCPhysMapping + paRegs[i].cbRegion - 1, paRegs[i].pszDesc);
545 else if (paRegs[i].pPciDev)
546 pHlp->pfnPrintf(pHlp, "%3u R3%s %RGp %.*s pci%u/%u %s\n", paRegs[i].idxSelf, pszRing, paRegs[i].cbRegion,
547 sizeof(RTGCPHYS) * 2, "unmapped", paRegs[i].pPciDev->idxSubDev, paRegs[i].iPciRegion, paRegs[i].pszDesc);
548 else
549 pHlp->pfnPrintf(pHlp, "%3u R3%s %RGp %.*s %s\n", paRegs[i].idxSelf, pszRing, paRegs[i].cbRegion,
550 sizeof(RTGCPHYS) * 2, "unmapped", paRegs[i].pszDesc);
551 }
552}
553
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