VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/PDMDevMiscHlp.cpp@ 87777

Last change on this file since 87777 was 87691, checked in by vboxsync, 4 years ago

AMD IOMMU: bugref:9654 Replace IOMMU device lock with the PDM lock and use a separate lock for the IOTLB/DTE cache.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 13.9 KB
Line 
1/* $Id: PDMDevMiscHlp.cpp 87691 2021-02-10 16:20:11Z vboxsync $ */
2/** @file
3 * PDM - Pluggable Device and Driver Manager, Misc. Device Helpers.
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_PDM_DEVICE
23#include "PDMInternal.h"
24#include <VBox/vmm/pdm.h>
25#include <VBox/vmm/pgm.h>
26#include <VBox/vmm/hm.h>
27#include <VBox/vmm/apic.h>
28#include <VBox/vmm/vm.h>
29#include <VBox/vmm/vmm.h>
30
31#include <VBox/log.h>
32#include <VBox/err.h>
33#include <VBox/msi.h>
34#include <iprt/asm.h>
35#include <iprt/assert.h>
36#include <iprt/thread.h>
37
38
39#include "PDMInline.h"
40#include "dtrace/VBoxVMM.h"
41
42
43
44/** @name Ring-3 PIC Helpers
45 * @{
46 */
47
48/** @interface_method_impl{PDMPICHLP,pfnSetInterruptFF} */
49static DECLCALLBACK(void) pdmR3PicHlp_SetInterruptFF(PPDMDEVINS pDevIns)
50{
51 PDMDEV_ASSERT_DEVINS(pDevIns);
52 PVM pVM = pDevIns->Internal.s.pVMR3;
53 PVMCPU pVCpu = pVM->apCpusR3[0]; /* for PIC we always deliver to CPU 0, SMP uses APIC */
54
55 /* IRQ state should be loaded as-is by "LoadExec". Changes can be made from LoadDone. */
56 Assert(pVM->enmVMState != VMSTATE_LOADING || pVM->pdm.s.fStateLoaded);
57
58 APICLocalInterrupt(pVCpu, 0 /* u8Pin */, 1 /* u8Level */, VINF_SUCCESS /* rcRZ */);
59}
60
61
62/** @interface_method_impl{PDMPICHLP,pfnClearInterruptFF} */
63static DECLCALLBACK(void) pdmR3PicHlp_ClearInterruptFF(PPDMDEVINS pDevIns)
64{
65 PDMDEV_ASSERT_DEVINS(pDevIns);
66 PVM pVM = pDevIns->Internal.s.pVMR3;
67 PVMCPU pVCpu = pVM->apCpusR3[0]; /* for PIC we always deliver to CPU 0, SMP uses APIC */
68
69 /* IRQ state should be loaded as-is by "LoadExec". Changes can be made from LoadDone. */
70 Assert(pVM->enmVMState != VMSTATE_LOADING || pVM->pdm.s.fStateLoaded);
71
72 APICLocalInterrupt(pVCpu, 0 /* u8Pin */, 0 /* u8Level */, VINF_SUCCESS /* rcRZ */);
73}
74
75
76/** @interface_method_impl{PDMPICHLP,pfnLock} */
77static DECLCALLBACK(int) pdmR3PicHlp_Lock(PPDMDEVINS pDevIns, int rc)
78{
79 PDMDEV_ASSERT_DEVINS(pDevIns);
80 return pdmLockEx(pDevIns->Internal.s.pVMR3, rc);
81}
82
83
84/** @interface_method_impl{PDMPICHLP,pfnUnlock} */
85static DECLCALLBACK(void) pdmR3PicHlp_Unlock(PPDMDEVINS pDevIns)
86{
87 PDMDEV_ASSERT_DEVINS(pDevIns);
88 pdmUnlock(pDevIns->Internal.s.pVMR3);
89}
90
91
92/**
93 * PIC Device Helpers.
94 */
95const PDMPICHLP g_pdmR3DevPicHlp =
96{
97 PDM_PICHLP_VERSION,
98 pdmR3PicHlp_SetInterruptFF,
99 pdmR3PicHlp_ClearInterruptFF,
100 pdmR3PicHlp_Lock,
101 pdmR3PicHlp_Unlock,
102 PDM_PICHLP_VERSION /* the end */
103};
104
105/** @} */
106
107
108/** @name Ring-3 I/O APIC Helpers
109 * @{
110 */
111
112/** @interface_method_impl{PDMIOAPICHLP,pfnApicBusDeliver} */
113static DECLCALLBACK(int) pdmR3IoApicHlp_ApicBusDeliver(PPDMDEVINS pDevIns, uint8_t u8Dest, uint8_t u8DestMode,
114 uint8_t u8DeliveryMode, uint8_t uVector, uint8_t u8Polarity,
115 uint8_t u8TriggerMode, uint32_t uTagSrc)
116{
117 PDMDEV_ASSERT_DEVINS(pDevIns);
118 PVM pVM = pDevIns->Internal.s.pVMR3;
119 LogFlow(("pdmR3IoApicHlp_ApicBusDeliver: caller='%s'/%d: u8Dest=%RX8 u8DestMode=%RX8 u8DeliveryMode=%RX8 uVector=%RX8 u8Polarity=%RX8 u8TriggerMode=%RX8 uTagSrc=%#x\n",
120 pDevIns->pReg->szName, pDevIns->iInstance, u8Dest, u8DestMode, u8DeliveryMode, uVector, u8Polarity, u8TriggerMode, uTagSrc));
121 return APICBusDeliver(pVM, u8Dest, u8DestMode, u8DeliveryMode, uVector, u8Polarity, u8TriggerMode, uTagSrc);
122}
123
124
125/** @interface_method_impl{PDMIOAPICHLP,pfnLock} */
126static DECLCALLBACK(int) pdmR3IoApicHlp_Lock(PPDMDEVINS pDevIns, int rc)
127{
128 PDMDEV_ASSERT_DEVINS(pDevIns);
129 LogFlow(("pdmR3IoApicHlp_Lock: caller='%s'/%d: rc=%Rrc\n", pDevIns->pReg->szName, pDevIns->iInstance, rc));
130 return pdmLockEx(pDevIns->Internal.s.pVMR3, rc);
131}
132
133
134/** @interface_method_impl{PDMIOAPICHLP,pfnUnlock} */
135static DECLCALLBACK(void) pdmR3IoApicHlp_Unlock(PPDMDEVINS pDevIns)
136{
137 PDMDEV_ASSERT_DEVINS(pDevIns);
138 LogFlow(("pdmR3IoApicHlp_Unlock: caller='%s'/%d:\n", pDevIns->pReg->szName, pDevIns->iInstance));
139 pdmUnlock(pDevIns->Internal.s.pVMR3);
140}
141
142
143/** @interface_method_impl{PDMIOAPICHLP,pfnIommuMsiRemap} */
144static DECLCALLBACK(int) pdmR3IoApicHlp_IommuMsiRemap(PPDMDEVINS pDevIns, uint16_t uDeviceId, PCMSIMSG pMsiIn, PMSIMSG pMsiOut)
145{
146 PDMDEV_ASSERT_DEVINS(pDevIns);
147 LogFlow(("pdmR3IoApicHlp_IommuRemapMsi: caller='%s'/%d: pMsiIn=(%#RX64, %#RU32)\n", pDevIns->pReg->szName,
148 pDevIns->iInstance, pMsiIn->Addr.u64, pMsiIn->Data.u32));
149
150#ifdef VBOX_WITH_IOMMU_AMD
151 int rc = pdmIommuMsiRemap(pDevIns, uDeviceId, pMsiIn, pMsiOut);
152 if (RT_SUCCESS(rc) || rc != VERR_IOMMU_NOT_PRESENT)
153 return rc;
154#else
155 RT_NOREF(pDevIns, uDeviceId);
156#endif
157
158 *pMsiOut = *pMsiIn;
159 return VINF_SUCCESS;
160}
161
162
163/**
164 * I/O APIC Device Helpers.
165 */
166const PDMIOAPICHLP g_pdmR3DevIoApicHlp =
167{
168 PDM_IOAPICHLP_VERSION,
169 pdmR3IoApicHlp_ApicBusDeliver,
170 pdmR3IoApicHlp_Lock,
171 pdmR3IoApicHlp_Unlock,
172 pdmR3IoApicHlp_IommuMsiRemap,
173 PDM_IOAPICHLP_VERSION /* the end */
174};
175
176/** @} */
177
178
179
180
181/** @name Ring-3 PCI Bus Helpers
182 * @{
183 */
184
185/** @interface_method_impl{PDMPCIHLPR3,pfnIsaSetIrq} */
186static DECLCALLBACK(void) pdmR3PciHlp_IsaSetIrq(PPDMDEVINS pDevIns, int iIrq, int iLevel, uint32_t uTagSrc)
187{
188 PDMDEV_ASSERT_DEVINS(pDevIns);
189 Log4(("pdmR3PciHlp_IsaSetIrq: iIrq=%d iLevel=%d uTagSrc=%#x\n", iIrq, iLevel, uTagSrc));
190 PDMIsaSetIrq(pDevIns->Internal.s.pVMR3, iIrq, iLevel, uTagSrc);
191}
192
193
194/** @interface_method_impl{PDMPCIHLPR3,pfnIoApicSetIrq} */
195static DECLCALLBACK(void) pdmR3PciHlp_IoApicSetIrq(PPDMDEVINS pDevIns, PCIBDF uBusDevFn, int iIrq, int iLevel, uint32_t uTagSrc)
196{
197 PDMDEV_ASSERT_DEVINS(pDevIns);
198 Log4(("pdmR3PciHlp_IoApicSetIrq: uBusDevFn=%#x iIrq=%d iLevel=%d uTagSrc=%#x\n", uBusDevFn, iIrq, iLevel, uTagSrc));
199 PDMIoApicSetIrq(pDevIns->Internal.s.pVMR3, uBusDevFn, iIrq, iLevel, uTagSrc);
200}
201
202
203/** @interface_method_impl{PDMPCIHLPR3,pfnIoApicSendMsi} */
204static DECLCALLBACK(void) pdmR3PciHlp_IoApicSendMsi(PPDMDEVINS pDevIns, PCIBDF uBusDevFn, PCMSIMSG pMsi, uint32_t uTagSrc)
205{
206 PDMDEV_ASSERT_DEVINS(pDevIns);
207 Assert(PCIBDF_IS_VALID(uBusDevFn));
208 Log4(("pdmR3PciHlp_IoApicSendMsi: uBusDevFn=%#x Msi (Addr=%#RX64 Data=%#x) uTagSrc=%#x\n", uBusDevFn,
209 pMsi->Addr.u64, pMsi->Data.u32, uTagSrc));
210 PDMIoApicSendMsi(pDevIns->Internal.s.pVMR3, uBusDevFn, pMsi, uTagSrc);
211}
212
213
214/** @interface_method_impl{PDMPCIHLPR3,pfnLock} */
215static DECLCALLBACK(int) pdmR3PciHlp_Lock(PPDMDEVINS pDevIns, int rc)
216{
217 PDMDEV_ASSERT_DEVINS(pDevIns);
218 LogFlow(("pdmR3PciHlp_Lock: caller='%s'/%d: rc=%Rrc\n", pDevIns->pReg->szName, pDevIns->iInstance, rc));
219 return pdmLockEx(pDevIns->Internal.s.pVMR3, rc);
220}
221
222
223/** @interface_method_impl{PDMPCIHLPR3,pfnUnlock} */
224static DECLCALLBACK(void) pdmR3PciHlp_Unlock(PPDMDEVINS pDevIns)
225{
226 PDMDEV_ASSERT_DEVINS(pDevIns);
227 LogFlow(("pdmR3PciHlp_Unlock: caller='%s'/%d:\n", pDevIns->pReg->szName, pDevIns->iInstance));
228 pdmUnlock(pDevIns->Internal.s.pVMR3);
229}
230
231
232/** @interface_method_impl{PDMPCIHLPR3,pfnGetBusByNo} */
233static DECLCALLBACK(PPDMDEVINS) pdmR3PciHlp_GetBusByNo(PPDMDEVINS pDevIns, uint32_t idxPdmBus)
234{
235 PDMDEV_ASSERT_DEVINS(pDevIns);
236 PVM pVM = pDevIns->Internal.s.pVMR3;
237 AssertReturn(idxPdmBus < RT_ELEMENTS(pVM->pdm.s.aPciBuses), NULL);
238 PPDMDEVINS pRetDevIns = pVM->pdm.s.aPciBuses[idxPdmBus].pDevInsR3;
239 LogFlow(("pdmR3PciHlp_GetBusByNo: caller='%s'/%d: returns %p\n", pDevIns->pReg->szName, pDevIns->iInstance, pRetDevIns));
240 return pRetDevIns;
241}
242
243
244/**
245 * PCI Bus Device Helpers.
246 */
247const PDMPCIHLPR3 g_pdmR3DevPciHlp =
248{
249 PDM_PCIHLPR3_VERSION,
250 pdmR3PciHlp_IsaSetIrq,
251 pdmR3PciHlp_IoApicSetIrq,
252 pdmR3PciHlp_IoApicSendMsi,
253 pdmR3PciHlp_Lock,
254 pdmR3PciHlp_Unlock,
255 pdmR3PciHlp_GetBusByNo,
256 PDM_PCIHLPR3_VERSION, /* the end */
257};
258
259/** @} */
260
261
262/** @name Ring-3 IOMMU Helpers
263 * @{
264 */
265
266/** @interface_method_impl{PDMIOMMUHLPR3,pfnLock} */
267static DECLCALLBACK(int) pdmR3IommuHlp_Lock(PPDMDEVINS pDevIns, int rc)
268{
269 PDMDEV_ASSERT_DEVINS(pDevIns);
270 LogFlowFunc(("caller='%s'/%d: rc=%Rrc\n", pDevIns->pReg->szName, pDevIns->iInstance, rc));
271 return pdmLockEx(pDevIns->Internal.s.pVMR3, rc);
272}
273
274
275/** @interface_method_impl{PDMIOMMUHLPR3,pfnUnlock} */
276static DECLCALLBACK(void) pdmR3IommuHlp_Unlock(PPDMDEVINS pDevIns)
277{
278 PDMDEV_ASSERT_DEVINS(pDevIns);
279 LogFlowFunc(("caller='%s'/%d:\n", pDevIns->pReg->szName, pDevIns->iInstance));
280 pdmUnlock(pDevIns->Internal.s.pVMR3);
281}
282
283
284/**
285 * IOMMU Device Helpers.
286 */
287const PDMIOMMUHLPR3 g_pdmR3DevIommuHlp =
288{
289 PDM_IOMMUHLPR3_VERSION,
290 pdmR3IommuHlp_Lock,
291 pdmR3IommuHlp_Unlock,
292 PDM_IOMMUHLPR3_VERSION /* the end */
293};
294
295/** @} */
296
297
298/** @name Ring-3 HPET Helpers
299 * @{
300 */
301
302/** @interface_method_impl{PDMHPETHLPR3,pfnSetLegacyMode} */
303static DECLCALLBACK(int) pdmR3HpetHlp_SetLegacyMode(PPDMDEVINS pDevIns, bool fActivated)
304{
305 PDMDEV_ASSERT_DEVINS(pDevIns);
306 LogFlow(("pdmR3HpetHlp_SetLegacyMode: caller='%s'/%d: fActivated=%RTbool\n", pDevIns->pReg->szName, pDevIns->iInstance, fActivated));
307
308 size_t i;
309 int rc = VINF_SUCCESS;
310 static const char * const s_apszDevsToNotify[] =
311 {
312 "i8254",
313 "mc146818"
314 };
315 for (i = 0; i < RT_ELEMENTS(s_apszDevsToNotify); i++)
316 {
317 PPDMIBASE pBase;
318 rc = PDMR3QueryDevice(pDevIns->Internal.s.pVMR3->pUVM, "i8254", 0, &pBase);
319 if (RT_SUCCESS(rc))
320 {
321 PPDMIHPETLEGACYNOTIFY pPort = PDMIBASE_QUERY_INTERFACE(pBase, PDMIHPETLEGACYNOTIFY);
322 AssertLogRelMsgBreakStmt(pPort, ("%s\n", s_apszDevsToNotify[i]), rc = VERR_PDM_HPET_LEGACY_NOTIFY_MISSING);
323 pPort->pfnModeChanged(pPort, fActivated);
324 }
325 else if ( rc == VERR_PDM_DEVICE_NOT_FOUND
326 || rc == VERR_PDM_DEVICE_INSTANCE_NOT_FOUND)
327 rc = VINF_SUCCESS; /* the device isn't configured, ignore. */
328 else
329 AssertLogRelMsgFailedBreak(("%s -> %Rrc\n", s_apszDevsToNotify[i], rc));
330 }
331
332 /* Don't bother cleaning up, any failure here will cause a guru meditation. */
333
334 LogFlow(("pdmR3HpetHlp_SetLegacyMode: caller='%s'/%d: returns %Rrc\n", pDevIns->pReg->szName, pDevIns->iInstance, rc));
335 return rc;
336}
337
338
339/** @interface_method_impl{PDMHPETHLPR3,pfnSetIrq} */
340static DECLCALLBACK(int) pdmR3HpetHlp_SetIrq(PPDMDEVINS pDevIns, int iIrq, int iLevel)
341{
342 PDMDEV_ASSERT_DEVINS(pDevIns);
343 LogFlow(("pdmR3HpetHlp_SetIrq: caller='%s'/%d: iIrq=%d iLevel=%d\n", pDevIns->pReg->szName, pDevIns->iInstance, iIrq, iLevel));
344 PVM pVM = pDevIns->Internal.s.pVMR3;
345
346 pdmLock(pVM);
347 uint32_t uTagSrc;
348 if (iLevel & PDM_IRQ_LEVEL_HIGH)
349 {
350 pDevIns->Internal.s.uLastIrqTag = uTagSrc = pdmCalcIrqTag(pVM, pDevIns->idTracing);
351 if (iLevel == PDM_IRQ_LEVEL_HIGH)
352 VBOXVMM_PDM_IRQ_HIGH(VMMGetCpu(pVM), RT_LOWORD(uTagSrc), RT_HIWORD(uTagSrc));
353 else
354 VBOXVMM_PDM_IRQ_HILO(VMMGetCpu(pVM), RT_LOWORD(uTagSrc), RT_HIWORD(uTagSrc));
355 }
356 else
357 uTagSrc = pDevIns->Internal.s.uLastIrqTag;
358
359 PDMIsaSetIrq(pVM, iIrq, iLevel, uTagSrc); /* (The API takes the lock recursively.) */
360
361 if (iLevel == PDM_IRQ_LEVEL_LOW)
362 VBOXVMM_PDM_IRQ_LOW(VMMGetCpu(pVM), RT_LOWORD(uTagSrc), RT_HIWORD(uTagSrc));
363 pdmUnlock(pVM);
364 return 0;
365}
366
367
368/**
369 * HPET Device Helpers.
370 */
371const PDMHPETHLPR3 g_pdmR3DevHpetHlp =
372{
373 PDM_HPETHLPR3_VERSION,
374 pdmR3HpetHlp_SetLegacyMode,
375 pdmR3HpetHlp_SetIrq,
376 PDM_HPETHLPR3_VERSION, /* the end */
377};
378
379/** @} */
380
381
382/** @name Ring-3 Raw PCI Device Helpers
383 * @{
384 */
385
386/** @interface_method_impl{PDMPCIRAWHLPR3,pfnGetRCHelpers} */
387static DECLCALLBACK(PCPDMPCIRAWHLPRC) pdmR3PciRawHlp_GetRCHelpers(PPDMDEVINS pDevIns)
388{
389 PDMDEV_ASSERT_DEVINS(pDevIns);
390 PVM pVM = pDevIns->Internal.s.pVMR3;
391 VM_ASSERT_EMT(pVM);
392
393 RTRCPTR pRCHelpers = NIL_RTRCPTR;
394 if (VM_IS_RAW_MODE_ENABLED(pVM))
395 {
396 int rc = PDMR3LdrGetSymbolRC(pVM, NULL, "g_pdmRCPciRawHlp", &pRCHelpers);
397 AssertReleaseRC(rc);
398 AssertRelease(pRCHelpers);
399 }
400
401 LogFlow(("pdmR3PciRawHlp_GetGCHelpers: caller='%s'/%d: returns %RRv\n",
402 pDevIns->pReg->szName, pDevIns->iInstance, pRCHelpers));
403 return pRCHelpers;
404}
405
406
407/** @interface_method_impl{PDMPCIRAWHLPR3,pfnGetR0Helpers} */
408static DECLCALLBACK(PCPDMPCIRAWHLPR0) pdmR3PciRawHlp_GetR0Helpers(PPDMDEVINS pDevIns)
409{
410 PDMDEV_ASSERT_DEVINS(pDevIns);
411 PVM pVM = pDevIns->Internal.s.pVMR3;
412 VM_ASSERT_EMT(pVM);
413 PCPDMHPETHLPR0 pR0Helpers = NIL_RTR0PTR;
414 int rc = PDMR3LdrGetSymbolR0(pVM, NULL, "g_pdmR0PciRawHlp", &pR0Helpers);
415 AssertReleaseRC(rc);
416 AssertRelease(pR0Helpers);
417 LogFlow(("pdmR3PciRawHlp_GetR0Helpers: caller='%s'/%d: returns %RHv\n",
418 pDevIns->pReg->szName, pDevIns->iInstance, pR0Helpers));
419 return pR0Helpers;
420}
421
422
423/**
424 * Raw PCI Device Helpers.
425 */
426const PDMPCIRAWHLPR3 g_pdmR3DevPciRawHlp =
427{
428 PDM_PCIRAWHLPR3_VERSION,
429 pdmR3PciRawHlp_GetRCHelpers,
430 pdmR3PciRawHlp_GetR0Helpers,
431 PDM_PCIRAWHLPR3_VERSION, /* the end */
432};
433
434/** @} */
435
436
437/* none yet */
438
439/**
440 * Firmware Device Helpers.
441 */
442const PDMFWHLPR3 g_pdmR3DevFirmwareHlp =
443{
444 PDM_FWHLPR3_VERSION,
445 PDM_FWHLPR3_VERSION
446};
447
448/**
449 * DMAC Device Helpers.
450 */
451const PDMDMACHLP g_pdmR3DevDmacHlp =
452{
453 PDM_DMACHLP_VERSION
454};
455
456
457
458
459/* none yet */
460
461/**
462 * RTC Device Helpers.
463 */
464const PDMRTCHLP g_pdmR3DevRtcHlp =
465{
466 PDM_RTCHLP_VERSION
467};
468
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