VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/PDMAll.cpp@ 96860

Last change on this file since 96860 was 96407, checked in by vboxsync, 21 months ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 13.4 KB
Line 
1/* $Id: PDMAll.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * PDM Critical Sections
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_PDM
33#include "PDMInternal.h"
34#include <VBox/vmm/pdm.h>
35#include <VBox/vmm/mm.h>
36#include <VBox/vmm/vmcc.h>
37#include <VBox/err.h>
38#include <VBox/vmm/apic.h>
39
40#include <VBox/log.h>
41#include <iprt/asm.h>
42#include <iprt/assert.h>
43
44#include "PDMInline.h"
45#include "dtrace/VBoxVMM.h"
46
47
48
49/**
50 * Gets the pending interrupt.
51 *
52 * @returns VBox status code.
53 * @retval VINF_SUCCESS on success.
54 * @retval VERR_APIC_INTR_MASKED_BY_TPR when an APIC interrupt is pending but
55 * can't be delivered due to TPR priority.
56 * @retval VERR_NO_DATA if there is no interrupt to be delivered (either APIC
57 * has been software-disabled since it flagged something was pending,
58 * or other reasons).
59 *
60 * @param pVCpu The cross context virtual CPU structure.
61 * @param pu8Interrupt Where to store the interrupt.
62 */
63VMMDECL(int) PDMGetInterrupt(PVMCPUCC pVCpu, uint8_t *pu8Interrupt)
64{
65 /*
66 * The local APIC has a higher priority than the PIC.
67 */
68 int rc = VERR_NO_DATA;
69 if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_APIC))
70 {
71 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_APIC);
72 uint32_t uTagSrc;
73 rc = APICGetInterrupt(pVCpu, pu8Interrupt, &uTagSrc);
74 if (RT_SUCCESS(rc))
75 {
76 if (rc == VINF_SUCCESS)
77 VBOXVMM_PDM_IRQ_GET(pVCpu, RT_LOWORD(uTagSrc), RT_HIWORD(uTagSrc), *pu8Interrupt);
78 return rc;
79 }
80 /* else if it's masked by TPR/PPR/whatever, go ahead checking the PIC. Such masked
81 interrupts shouldn't prevent ExtINT from being delivered. */
82 }
83
84 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
85 pdmLock(pVM);
86
87 /*
88 * Check the PIC.
89 */
90 if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_PIC))
91 {
92 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_PIC);
93 Assert(pVM->pdm.s.Pic.CTX_SUFF(pDevIns));
94 Assert(pVM->pdm.s.Pic.CTX_SUFF(pfnGetInterrupt));
95 uint32_t uTagSrc;
96 int i = pVM->pdm.s.Pic.CTX_SUFF(pfnGetInterrupt)(pVM->pdm.s.Pic.CTX_SUFF(pDevIns), &uTagSrc);
97 AssertMsg(i <= 255 && i >= 0, ("i=%d\n", i));
98 if (i >= 0)
99 {
100 pdmUnlock(pVM);
101 *pu8Interrupt = (uint8_t)i;
102 VBOXVMM_PDM_IRQ_GET(pVCpu, RT_LOWORD(uTagSrc), RT_HIWORD(uTagSrc), i);
103 return VINF_SUCCESS;
104 }
105 }
106
107 /*
108 * One scenario where we may possibly get here is if the APIC signaled a pending interrupt,
109 * got an APIC MMIO/MSR VM-exit which disabled the APIC. We could, in theory, clear the APIC
110 * force-flag from all the places which disables the APIC but letting PDMGetInterrupt() fail
111 * without returning a valid interrupt still needs to be handled for the TPR masked case,
112 * so we shall just handle it here regardless if we choose to update the APIC code in the future.
113 */
114
115 pdmUnlock(pVM);
116 return rc;
117}
118
119
120/**
121 * Sets the pending interrupt coming from ISA source or HPET.
122 *
123 * @returns VBox status code.
124 * @param pVM The cross context VM structure.
125 * @param u8Irq The IRQ line.
126 * @param u8Level The new level.
127 * @param uTagSrc The IRQ tag and source tracer ID.
128 */
129VMMDECL(int) PDMIsaSetIrq(PVMCC pVM, uint8_t u8Irq, uint8_t u8Level, uint32_t uTagSrc)
130{
131 pdmLock(pVM);
132
133 /** @todo put the IRQ13 code elsewhere to avoid this unnecessary bloat. */
134 if (!uTagSrc && (u8Level & PDM_IRQ_LEVEL_HIGH)) /* FPU IRQ */
135 {
136 if (u8Level == PDM_IRQ_LEVEL_HIGH)
137 VBOXVMM_PDM_IRQ_HIGH(VMMGetCpu(pVM), 0, 0);
138 else
139 VBOXVMM_PDM_IRQ_HILO(VMMGetCpu(pVM), 0, 0);
140 }
141
142 int rc = VERR_PDM_NO_PIC_INSTANCE;
143/** @todo r=bird: This code is incorrect, as it ASSUMES the PIC and I/O APIC
144 * are always ring-0 enabled! */
145 if (pVM->pdm.s.Pic.CTX_SUFF(pDevIns))
146 {
147 Assert(pVM->pdm.s.Pic.CTX_SUFF(pfnSetIrq));
148 pVM->pdm.s.Pic.CTX_SUFF(pfnSetIrq)(pVM->pdm.s.Pic.CTX_SUFF(pDevIns), u8Irq, u8Level, uTagSrc);
149 rc = VINF_SUCCESS;
150 }
151
152 if (pVM->pdm.s.IoApic.CTX_SUFF(pDevIns))
153 {
154 Assert(pVM->pdm.s.IoApic.CTX_SUFF(pfnSetIrq));
155
156 /*
157 * Apply Interrupt Source Override rules.
158 * See ACPI 4.0 specification 5.2.12.4 and 5.2.12.5 for details on
159 * interrupt source override.
160 * Shortly, ISA IRQ0 is electically connected to pin 2 on IO-APIC, and some OSes,
161 * notably recent OS X rely upon this configuration.
162 * If changing, also update override rules in MADT and MPS.
163 */
164 /* ISA IRQ0 routed to pin 2, all others ISA sources are identity mapped */
165 if (u8Irq == 0)
166 u8Irq = 2;
167
168 pVM->pdm.s.IoApic.CTX_SUFF(pfnSetIrq)(pVM->pdm.s.IoApic.CTX_SUFF(pDevIns), NIL_PCIBDF, u8Irq, u8Level, uTagSrc);
169 rc = VINF_SUCCESS;
170 }
171
172 if (!uTagSrc && u8Level == PDM_IRQ_LEVEL_LOW)
173 VBOXVMM_PDM_IRQ_LOW(VMMGetCpu(pVM), 0, 0);
174 pdmUnlock(pVM);
175 return rc;
176}
177
178
179/**
180 * Sets the pending I/O APIC interrupt.
181 *
182 * @returns VBox status code.
183 * @param pVM The cross context VM structure.
184 * @param u8Irq The IRQ line.
185 * @param uBusDevFn The bus:device:function of the device initiating the IRQ.
186 * Pass NIL_PCIBDF when it's not a PCI device or interrupt.
187 * @param u8Level The new level.
188 * @param uTagSrc The IRQ tag and source tracer ID.
189 */
190VMM_INT_DECL(int) PDMIoApicSetIrq(PVM pVM, PCIBDF uBusDevFn, uint8_t u8Irq, uint8_t u8Level, uint32_t uTagSrc)
191{
192 if (pVM->pdm.s.IoApic.CTX_SUFF(pDevIns))
193 {
194 Assert(pVM->pdm.s.IoApic.CTX_SUFF(pfnSetIrq));
195 pVM->pdm.s.IoApic.CTX_SUFF(pfnSetIrq)(pVM->pdm.s.IoApic.CTX_SUFF(pDevIns), uBusDevFn, u8Irq, u8Level, uTagSrc);
196 return VINF_SUCCESS;
197 }
198 return VERR_PDM_NO_PIC_INSTANCE;
199}
200
201
202/**
203 * Broadcasts an EOI to the I/O APIC(s).
204 *
205 * @param pVM The cross context VM structure.
206 * @param uVector The interrupt vector corresponding to the EOI.
207 */
208VMM_INT_DECL(void) PDMIoApicBroadcastEoi(PVMCC pVM, uint8_t uVector)
209{
210 /*
211 * At present, we support only a maximum of one I/O APIC per-VM. If we ever implement having
212 * multiple I/O APICs per-VM, we'll have to broadcast this EOI to all of the I/O APICs.
213 */
214 PCPDMIOAPIC pIoApic = &pVM->pdm.s.IoApic;
215#ifdef IN_RING0
216 if (pIoApic->pDevInsR0)
217 {
218 Assert(pIoApic->pfnSetEoiR0);
219 pIoApic->pfnSetEoiR0(pIoApic->pDevInsR0, uVector);
220 }
221 else if (pIoApic->pDevInsR3)
222 {
223 /* Queue for ring-3 execution. */
224 PPDMDEVHLPTASK pTask = (PPDMDEVHLPTASK)PDMQueueAlloc(pVM, pVM->pdm.s.hDevHlpQueue, pVM);
225 if (pTask)
226 {
227 pTask->enmOp = PDMDEVHLPTASKOP_IOAPIC_SET_EOI;
228 pTask->pDevInsR3 = NIL_RTR3PTR; /* not required */
229 pTask->u.IoApicSetEoi.uVector = uVector;
230 PDMQueueInsert(pVM, pVM->pdm.s.hDevHlpQueue, pVM, &pTask->Core);
231 }
232 else
233 AssertMsgFailed(("We're out of devhlp queue items!!!\n"));
234 }
235#else
236 if (pIoApic->pDevInsR3)
237 {
238 Assert(pIoApic->pfnSetEoiR3);
239 pIoApic->pfnSetEoiR3(pIoApic->pDevInsR3, uVector);
240 }
241#endif
242}
243
244
245/**
246 * Send a MSI to an I/O APIC.
247 *
248 * @param pVM The cross context VM structure.
249 * @param uBusDevFn The bus:device:function of the device initiating the MSI.
250 * @param pMsi The MSI to send.
251 * @param uTagSrc The IRQ tag and source tracer ID.
252 */
253VMM_INT_DECL(void) PDMIoApicSendMsi(PVMCC pVM, PCIBDF uBusDevFn, PCMSIMSG pMsi, uint32_t uTagSrc)
254{
255 PCPDMIOAPIC pIoApic = &pVM->pdm.s.IoApic;
256#ifdef IN_RING0
257 if (pIoApic->pDevInsR0)
258 pIoApic->pfnSendMsiR0(pIoApic->pDevInsR0, uBusDevFn, pMsi, uTagSrc);
259 else if (pIoApic->pDevInsR3)
260 {
261 /* Queue for ring-3 execution. */
262 PPDMDEVHLPTASK pTask = (PPDMDEVHLPTASK)PDMQueueAlloc(pVM, pVM->pdm.s.hDevHlpQueue, pVM);
263 if (pTask)
264 {
265 pTask->enmOp = PDMDEVHLPTASKOP_IOAPIC_SEND_MSI;
266 pTask->pDevInsR3 = NIL_RTR3PTR; /* not required */
267 pTask->u.IoApicSendMsi.uBusDevFn = uBusDevFn;
268 pTask->u.IoApicSendMsi.Msi = *pMsi;
269 pTask->u.IoApicSendMsi.uTagSrc = uTagSrc;
270 PDMQueueInsert(pVM, pVM->pdm.s.hDevHlpQueue, pVM, &pTask->Core);
271 }
272 else
273 AssertMsgFailed(("We're out of devhlp queue items!!!\n"));
274 }
275#else
276 if (pIoApic->pDevInsR3)
277 {
278 Assert(pIoApic->pfnSendMsiR3);
279 pIoApic->pfnSendMsiR3(pIoApic->pDevInsR3, uBusDevFn, pMsi, uTagSrc);
280 }
281#endif
282}
283
284
285
286/**
287 * Returns the presence of an IO-APIC.
288 *
289 * @returns true if an IO-APIC is present.
290 * @param pVM The cross context VM structure.
291 */
292VMM_INT_DECL(bool) PDMHasIoApic(PVM pVM)
293{
294 return pVM->pdm.s.IoApic.pDevInsR3 != NULL;
295}
296
297
298/**
299 * Returns the presence of an APIC.
300 *
301 * @returns true if an APIC is present.
302 * @param pVM The cross context VM structure.
303 */
304VMM_INT_DECL(bool) PDMHasApic(PVM pVM)
305{
306 return pVM->pdm.s.Apic.pDevInsR3 != NIL_RTR3PTR;
307}
308
309
310/**
311 * Translates a ring-0 device instance index to a pointer.
312 *
313 * This is used by PGM for device access handlers.
314 *
315 * @returns Device instance pointer if valid index, otherwise NULL (asserted).
316 * @param pVM The cross context VM structure.
317 * @param idxR0Device The ring-0 device instance index.
318 */
319VMM_INT_DECL(PPDMDEVINS) PDMDeviceRing0IdxToInstance(PVMCC pVM, uint64_t idxR0Device)
320{
321#ifdef IN_RING0
322 AssertMsgReturn(idxR0Device < RT_ELEMENTS(pVM->pdmr0.s.apDevInstances), ("%#RX64\n", idxR0Device), NULL);
323 PPDMDEVINS pDevIns = pVM->pdmr0.s.apDevInstances[idxR0Device];
324#elif defined(IN_RING3)
325 AssertMsgReturn(idxR0Device < RT_ELEMENTS(pVM->pdm.s.apDevRing0Instances), ("%#RX64\n", idxR0Device), NULL);
326 PPDMDEVINS pDevIns = pVM->pdm.s.apDevRing0Instances[idxR0Device];
327#else
328# error "Unsupported context"
329#endif
330 AssertMsg(pDevIns, ("%#RX64\n", idxR0Device));
331 return pDevIns;
332}
333
334
335/**
336 * Locks PDM.
337 *
338 * This might block.
339 *
340 * @param pVM The cross context VM structure.
341 */
342void pdmLock(PVMCC pVM)
343{
344 int rc = PDMCritSectEnter(pVM, &pVM->pdm.s.CritSect, VINF_SUCCESS);
345 PDM_CRITSECT_RELEASE_ASSERT_RC(pVM, &pVM->pdm.s.CritSect, rc);
346}
347
348
349/**
350 * Locks PDM but don't go to ring-3 if it's owned by someone.
351 *
352 * @returns VINF_SUCCESS on success.
353 * @returns rc if we're in GC or R0 and can't get the lock.
354 * @param pVM The cross context VM structure.
355 * @param rcBusy The RC to return in GC or R0 when we can't get the lock.
356 */
357int pdmLockEx(PVMCC pVM, int rcBusy)
358{
359 return PDMCritSectEnter(pVM, &pVM->pdm.s.CritSect, rcBusy);
360}
361
362
363/**
364 * Unlocks PDM.
365 *
366 * @param pVM The cross context VM structure.
367 */
368void pdmUnlock(PVMCC pVM)
369{
370 PDMCritSectLeave(pVM, &pVM->pdm.s.CritSect);
371}
372
373
374/**
375 * Checks if this thread is owning the PDM lock.
376 *
377 * @returns @c true if the lock is taken, @c false otherwise.
378 * @param pVM The cross context VM structure.
379 */
380bool pdmLockIsOwner(PVMCC pVM)
381{
382 return PDMCritSectIsOwner(pVM, &pVM->pdm.s.CritSect);
383}
384
385
386/**
387 * Converts ring 3 VMM heap pointer to a guest physical address
388 *
389 * @returns VBox status code.
390 * @param pVM The cross context VM structure.
391 * @param pv Ring-3 pointer.
392 * @param pGCPhys GC phys address (out).
393 */
394VMM_INT_DECL(int) PDMVmmDevHeapR3ToGCPhys(PVM pVM, RTR3PTR pv, RTGCPHYS *pGCPhys)
395{
396 if (RT_LIKELY(pVM->pdm.s.GCPhysVMMDevHeap != NIL_RTGCPHYS))
397 {
398 RTR3UINTPTR const offHeap = (RTR3UINTPTR)pv - (RTR3UINTPTR)pVM->pdm.s.pvVMMDevHeap;
399 if (RT_LIKELY(offHeap < pVM->pdm.s.cbVMMDevHeap))
400 {
401 *pGCPhys = pVM->pdm.s.GCPhysVMMDevHeap + offHeap;
402 return VINF_SUCCESS;
403 }
404
405 /* Don't assert here as this is called before we can catch ring-0 assertions. */
406 Log(("PDMVmmDevHeapR3ToGCPhys: pv=%p pvVMMDevHeap=%p cbVMMDevHeap=%#x\n",
407 pv, pVM->pdm.s.pvVMMDevHeap, pVM->pdm.s.cbVMMDevHeap));
408 }
409 else
410 Log(("PDMVmmDevHeapR3ToGCPhys: GCPhysVMMDevHeap=%RGp (pv=%p)\n", pVM->pdm.s.GCPhysVMMDevHeap, pv));
411 return VERR_PDM_DEV_HEAP_R3_TO_GCPHYS;
412}
413
414
415/**
416 * Checks if the vmm device heap is enabled (== vmm device's pci region mapped)
417 *
418 * @returns dev heap enabled status (true/false)
419 * @param pVM The cross context VM structure.
420 */
421VMM_INT_DECL(bool) PDMVmmDevHeapIsEnabled(PVM pVM)
422{
423 return pVM->pdm.s.GCPhysVMMDevHeap != NIL_RTGCPHYS;
424}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use