VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR0/PDMR0Device.cpp@ 84007

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

VMMR0: Move the PDM R0 device helper into its own file like it is done in R3, move the PDM R0 driver helpers to PDMR0Driver as it belongs there really

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 33.5 KB
Line 
1/* $Id: PDMR0Device.cpp 84007 2020-04-27 13:23:11Z vboxsync $ */
2/** @file
3 * PDM - Pluggable Device and Driver Manager, R0 Device parts.
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#define PDMPCIDEV_INCLUDE_PRIVATE /* Hack to get pdmpcidevint.h included at the right point. */
24#include "PDMInternal.h"
25#include <VBox/vmm/pdm.h>
26#include <VBox/vmm/apic.h>
27#include <VBox/vmm/mm.h>
28#include <VBox/vmm/pgm.h>
29#include <VBox/vmm/gvm.h>
30#include <VBox/vmm/vmm.h>
31#include <VBox/vmm/hm.h>
32#include <VBox/vmm/vmcc.h>
33#include <VBox/vmm/gvmm.h>
34
35#include <VBox/log.h>
36#include <VBox/err.h>
37#include <VBox/msi.h>
38#include <VBox/sup.h>
39#include <iprt/asm.h>
40#include <iprt/assert.h>
41#include <iprt/ctype.h>
42#include <iprt/mem.h>
43#include <iprt/memobj.h>
44#include <iprt/process.h>
45#include <iprt/string.h>
46
47#include "dtrace/VBoxVMM.h"
48#include "PDMInline.h"
49
50
51/*********************************************************************************************************************************
52* Global Variables *
53*********************************************************************************************************************************/
54RT_C_DECLS_BEGIN
55extern DECLEXPORT(const PDMDEVHLPR0) g_pdmR0DevHlp;
56extern DECLEXPORT(const PDMPICHLP) g_pdmR0PicHlp;
57extern DECLEXPORT(const PDMIOAPICHLP) g_pdmR0IoApicHlp;
58extern DECLEXPORT(const PDMPCIHLPR0) g_pdmR0PciHlp;
59extern DECLEXPORT(const PDMIOMMUHLPR0) g_pdmR0IommuHlp;
60extern DECLEXPORT(const PDMHPETHLPR0) g_pdmR0HpetHlp;
61extern DECLEXPORT(const PDMPCIRAWHLPR0) g_pdmR0PciRawHlp;
62RT_C_DECLS_END
63
64/** List of PDMDEVMODREGR0 structures protected by the loader lock. */
65static RTLISTANCHOR g_PDMDevModList;
66
67
68/**
69 * Pointer to the ring-0 device registrations for VMMR0.
70 */
71static const PDMDEVREGR0 *g_apVMM0DevRegs[] =
72{
73 &g_DeviceAPIC,
74};
75
76/**
77 * Module device registration record for VMMR0.
78 */
79static PDMDEVMODREGR0 g_VBoxDDR0ModDevReg =
80{
81 /* .u32Version = */ PDM_DEVMODREGR0_VERSION,
82 /* .cDevRegs = */ RT_ELEMENTS(g_apVMM0DevRegs),
83 /* .papDevRegs = */ &g_apVMM0DevRegs[0],
84 /* .hMod = */ NULL,
85 /* .ListEntry = */ { NULL, NULL },
86};
87
88
89/*********************************************************************************************************************************
90* Internal Functions *
91*********************************************************************************************************************************/
92
93
94/**
95 * Initializes the global ring-0 PDM data.
96 */
97VMMR0_INT_DECL(void) PDMR0Init(void *hMod)
98{
99 RTListInit(&g_PDMDevModList);
100 g_VBoxDDR0ModDevReg.hMod = hMod;
101 RTListAppend(&g_PDMDevModList, &g_VBoxDDR0ModDevReg.ListEntry);
102}
103
104
105/**
106 * Used by PDMR0CleanupVM to destroy a device instance.
107 *
108 * This is done during VM cleanup so that we're sure there are no active threads
109 * inside the device code.
110 *
111 * @param pGVM The global (ring-0) VM structure.
112 * @param pDevIns The device instance.
113 * @param idxR0Device The device instance handle.
114 */
115static int pdmR0DeviceDestroy(PGVM pGVM, PPDMDEVINSR0 pDevIns, uint32_t idxR0Device)
116{
117 /*
118 * Assert sanity.
119 */
120 Assert(idxR0Device < pGVM->pdmr0.s.cDevInstances);
121 AssertPtrReturn(pDevIns, VERR_INVALID_HANDLE);
122 Assert(pDevIns->u32Version == PDM_DEVINSR0_VERSION);
123 Assert(pDevIns->Internal.s.idxR0Device == idxR0Device);
124
125 /*
126 * Call the final destructor if there is one.
127 */
128 if (pDevIns->pReg->pfnFinalDestruct)
129 pDevIns->pReg->pfnFinalDestruct(pDevIns);
130 pDevIns->u32Version = ~PDM_DEVINSR0_VERSION;
131
132 /*
133 * Remove the device from the instance table.
134 */
135 Assert(pGVM->pdmr0.s.apDevInstances[idxR0Device] == pDevIns);
136 pGVM->pdmr0.s.apDevInstances[idxR0Device] = NULL;
137 if (idxR0Device + 1 == pGVM->pdmr0.s.cDevInstances)
138 pGVM->pdmr0.s.cDevInstances = idxR0Device;
139
140 /*
141 * Free the ring-3 mapping and instance memory.
142 */
143 RTR0MEMOBJ hMemObj = pDevIns->Internal.s.hMapObj;
144 pDevIns->Internal.s.hMapObj = NIL_RTR0MEMOBJ;
145 RTR0MemObjFree(hMemObj, true);
146
147 hMemObj = pDevIns->Internal.s.hMemObj;
148 pDevIns->Internal.s.hMemObj = NIL_RTR0MEMOBJ;
149 RTR0MemObjFree(hMemObj, true);
150
151 return VINF_SUCCESS;
152}
153
154
155/**
156 * Initializes the per-VM data for the PDM.
157 *
158 * This is called from under the GVMM lock, so it only need to initialize the
159 * data so PDMR0CleanupVM and others will work smoothly.
160 *
161 * @param pGVM Pointer to the global VM structure.
162 */
163VMMR0_INT_DECL(void) PDMR0InitPerVMData(PGVM pGVM)
164{
165 AssertCompile(sizeof(pGVM->pdm.s) <= sizeof(pGVM->pdm.padding));
166 AssertCompile(sizeof(pGVM->pdmr0.s) <= sizeof(pGVM->pdmr0.padding));
167
168 pGVM->pdmr0.s.cDevInstances = 0;
169}
170
171
172/**
173 * Cleans up any loose ends before the GVM structure is destroyed.
174 */
175VMMR0_INT_DECL(void) PDMR0CleanupVM(PGVM pGVM)
176{
177 uint32_t i = pGVM->pdmr0.s.cDevInstances;
178 while (i-- > 0)
179 {
180 PPDMDEVINSR0 pDevIns = pGVM->pdmr0.s.apDevInstances[i];
181 if (pDevIns)
182 pdmR0DeviceDestroy(pGVM, pDevIns, i);
183 }
184}
185
186
187/**
188 * Worker for PDMR0DeviceCreate that does the actual instantiation.
189 *
190 * Allocates a memory object and divides it up as follows:
191 * @verbatim
192 --------------------------------------
193 ring-0 devins
194 --------------------------------------
195 ring-0 instance data
196 --------------------------------------
197 ring-0 PCI device data (optional) ??
198 --------------------------------------
199 page alignment padding
200 --------------------------------------
201 ring-3 devins
202 --------------------------------------
203 ring-3 instance data
204 --------------------------------------
205 ring-3 PCI device data (optional) ??
206 --------------------------------------
207 [page alignment padding ] -
208 [--------------------------------------] \
209 [raw-mode devins ] \
210 [--------------------------------------] - Optional, only when raw-mode is enabled.
211 [raw-mode instance data ] /
212 [--------------------------------------] /
213 [raw-mode PCI device data (optional)?? ] -
214 --------------------------------------
215 shared instance data
216 --------------------------------------
217 default crit section
218 --------------------------------------
219 shared PCI device data (optional)
220 --------------------------------------
221 @endverbatim
222 *
223 * @returns VBox status code.
224 * @param pGVM The global (ring-0) VM structure.
225 * @param pDevReg The device registration structure.
226 * @param iInstance The device instance number.
227 * @param cbInstanceR3 The size of the ring-3 instance data.
228 * @param cbInstanceRC The size of the raw-mode instance data.
229 * @param hMod The module implementing the device. On success, the
230 * @param RCPtrMapping The raw-mode context mapping address, NIL_RTGCPTR if
231 * not to include raw-mode.
232 * @param ppDevInsR3 Where to return the ring-3 device instance address.
233 * @thread EMT(0)
234 */
235static int pdmR0DeviceCreateWorker(PGVM pGVM, PCPDMDEVREGR0 pDevReg, uint32_t iInstance, uint32_t cbInstanceR3,
236 uint32_t cbInstanceRC, RTRGPTR RCPtrMapping, void *hMod, PPDMDEVINSR3 *ppDevInsR3)
237{
238 /*
239 * Check that the instance number isn't a duplicate.
240 */
241 for (size_t i = 0; i < pGVM->pdmr0.s.cDevInstances; i++)
242 {
243 PPDMDEVINS pCur = pGVM->pdmr0.s.apDevInstances[i];
244 AssertLogRelReturn(!pCur || pCur->pReg != pDevReg || pCur->iInstance != iInstance, VERR_DUPLICATE);
245 }
246
247 /*
248 * Figure out how much memory we need and allocate it.
249 */
250 uint32_t const cbRing0 = RT_ALIGN_32(RT_UOFFSETOF(PDMDEVINSR0, achInstanceData) + pDevReg->cbInstanceCC, PAGE_SIZE);
251 uint32_t const cbRing3 = RT_ALIGN_32(RT_UOFFSETOF(PDMDEVINSR3, achInstanceData) + cbInstanceR3,
252 RCPtrMapping != NIL_RTRGPTR ? PAGE_SIZE : 64);
253 uint32_t const cbRC = RCPtrMapping != NIL_RTRGPTR ? 0
254 : RT_ALIGN_32(RT_UOFFSETOF(PDMDEVINSRC, achInstanceData) + cbInstanceRC, 64);
255 uint32_t const cbShared = RT_ALIGN_32(pDevReg->cbInstanceShared, 64);
256 uint32_t const cbCritSect = RT_ALIGN_32(sizeof(PDMCRITSECT), 64);
257 uint32_t const cbMsixState = RT_ALIGN_32(pDevReg->cMaxMsixVectors * 16 + (pDevReg->cMaxMsixVectors + 7) / 8, _4K);
258 uint32_t const cbPciDev = RT_ALIGN_32(RT_UOFFSETOF_DYN(PDMPCIDEV, abMsixState[cbMsixState]), 64);
259 uint32_t const cPciDevs = RT_MIN(pDevReg->cMaxPciDevices, 8);
260 uint32_t const cbPciDevs = cbPciDev * cPciDevs;
261 uint32_t const cbTotal = RT_ALIGN_32(cbRing0 + cbRing3 + cbRC + cbShared + cbCritSect + cbPciDevs, PAGE_SIZE);
262 AssertLogRelMsgReturn(cbTotal <= PDM_MAX_DEVICE_INSTANCE_SIZE,
263 ("Instance of '%s' is too big: cbTotal=%u, max %u\n",
264 pDevReg->szName, cbTotal, PDM_MAX_DEVICE_INSTANCE_SIZE),
265 VERR_OUT_OF_RANGE);
266
267 RTR0MEMOBJ hMemObj;
268 int rc = RTR0MemObjAllocPage(&hMemObj, cbTotal, false /*fExecutable*/);
269 if (RT_FAILURE(rc))
270 return rc;
271 RT_BZERO(RTR0MemObjAddress(hMemObj), cbTotal);
272
273 /* Map it. */
274 RTR0MEMOBJ hMapObj;
275 rc = RTR0MemObjMapUserEx(&hMapObj, hMemObj, (RTR3PTR)-1, 0, RTMEM_PROT_READ | RTMEM_PROT_WRITE, RTR0ProcHandleSelf(),
276 cbRing0, cbTotal - cbRing0);
277 if (RT_SUCCESS(rc))
278 {
279 PPDMDEVINSR0 pDevIns = (PPDMDEVINSR0)RTR0MemObjAddress(hMemObj);
280 struct PDMDEVINSR3 *pDevInsR3 = (struct PDMDEVINSR3 *)((uint8_t *)pDevIns + cbRing0);
281
282 /*
283 * Initialize the ring-0 instance.
284 */
285 pDevIns->u32Version = PDM_DEVINSR0_VERSION;
286 pDevIns->iInstance = iInstance;
287 pDevIns->pHlpR0 = &g_pdmR0DevHlp;
288 pDevIns->pvInstanceDataR0 = (uint8_t *)pDevIns + cbRing0 + cbRing3 + cbRC;
289 pDevIns->pvInstanceDataForR0 = &pDevIns->achInstanceData[0];
290 pDevIns->pCritSectRoR0 = (PPDMCRITSECT)((uint8_t *)pDevIns->pvInstanceDataR0 + cbShared);
291 pDevIns->pReg = pDevReg;
292 pDevIns->pDevInsForR3 = RTR0MemObjAddressR3(hMapObj);
293 pDevIns->pDevInsForR3R0 = pDevInsR3;
294 pDevIns->pvInstanceDataForR3R0 = &pDevInsR3->achInstanceData[0];
295 pDevIns->cbPciDev = cbPciDev;
296 pDevIns->cPciDevs = cPciDevs;
297 for (uint32_t iPciDev = 0; iPciDev < cPciDevs; iPciDev++)
298 {
299 /* Note! PDMDevice.cpp has a copy of this code. Keep in sync. */
300 PPDMPCIDEV pPciDev = (PPDMPCIDEV)((uint8_t *)pDevIns->pCritSectRoR0 + cbCritSect + cbPciDev * iPciDev);
301 if (iPciDev < RT_ELEMENTS(pDevIns->apPciDevs))
302 pDevIns->apPciDevs[iPciDev] = pPciDev;
303 pPciDev->cbConfig = _4K;
304 pPciDev->cbMsixState = cbMsixState;
305 pPciDev->idxSubDev = (uint16_t)iPciDev;
306 pPciDev->Int.s.idxSubDev = (uint16_t)iPciDev;
307 pPciDev->u32Magic = PDMPCIDEV_MAGIC;
308 }
309 pDevIns->Internal.s.pGVM = pGVM;
310 pDevIns->Internal.s.pRegR0 = pDevReg;
311 pDevIns->Internal.s.hMod = hMod;
312 pDevIns->Internal.s.hMemObj = hMemObj;
313 pDevIns->Internal.s.hMapObj = hMapObj;
314 pDevIns->Internal.s.pInsR3R0 = pDevInsR3;
315 pDevIns->Internal.s.pIntR3R0 = &pDevInsR3->Internal.s;
316
317 /*
318 * Initialize the ring-3 instance data as much as we can.
319 * Note! PDMDevice.cpp does this job for ring-3 only devices. Keep in sync.
320 */
321 pDevInsR3->u32Version = PDM_DEVINSR3_VERSION;
322 pDevInsR3->iInstance = iInstance;
323 pDevInsR3->cbRing3 = cbTotal - cbRing0;
324 pDevInsR3->fR0Enabled = true;
325 pDevInsR3->fRCEnabled = RCPtrMapping != NIL_RTRGPTR;
326 pDevInsR3->pvInstanceDataR3 = pDevIns->pDevInsForR3 + cbRing3 + cbRC;
327 pDevInsR3->pvInstanceDataForR3 = pDevIns->pDevInsForR3 + RT_UOFFSETOF(PDMDEVINSR3, achInstanceData);
328 pDevInsR3->pCritSectRoR3 = pDevIns->pDevInsForR3 + cbRing3 + cbRC + cbShared;
329 pDevInsR3->pDevInsR0RemoveMe = pDevIns;
330 pDevInsR3->pvInstanceDataR0 = pDevIns->pvInstanceDataR0;
331 pDevInsR3->pvInstanceDataRC = RCPtrMapping == NIL_RTRGPTR
332 ? NIL_RTRGPTR : pDevIns->pDevInsForRC + RT_UOFFSETOF(PDMDEVINSRC, achInstanceData);
333 pDevInsR3->pDevInsForRC = pDevIns->pDevInsForRC;
334 pDevInsR3->pDevInsForRCR3 = pDevIns->pDevInsForR3 + cbRing3;
335 pDevInsR3->pDevInsForRCR3 = pDevInsR3->pDevInsForRCR3 + RT_UOFFSETOF(PDMDEVINSRC, achInstanceData);
336 pDevInsR3->cbPciDev = cbPciDev;
337 pDevInsR3->cPciDevs = cPciDevs;
338 for (uint32_t i = 0; i < RT_MIN(cPciDevs, RT_ELEMENTS(pDevIns->apPciDevs)); i++)
339 pDevInsR3->apPciDevs[i] = pDevInsR3->pCritSectRoR3 + cbCritSect + cbPciDev * i;
340
341 pDevInsR3->Internal.s.pVMR3 = pGVM->pVMR3;
342 pDevInsR3->Internal.s.fIntFlags = RCPtrMapping == NIL_RTRGPTR ? PDMDEVINSINT_FLAGS_R0_ENABLED
343 : PDMDEVINSINT_FLAGS_R0_ENABLED | PDMDEVINSINT_FLAGS_RC_ENABLED;
344
345 /*
346 * Initialize the raw-mode instance data as much as possible.
347 */
348 if (RCPtrMapping != NIL_RTRGPTR)
349 {
350 struct PDMDEVINSRC *pDevInsRC = RCPtrMapping == NIL_RTRGPTR ? NULL
351 : (struct PDMDEVINSRC *)((uint8_t *)pDevIns + cbRing0 + cbRing3);
352
353 pDevIns->pDevInsForRC = RCPtrMapping;
354 pDevIns->pDevInsForRCR0 = pDevInsRC;
355 pDevIns->pvInstanceDataForRCR0 = &pDevInsRC->achInstanceData[0];
356
357 pDevInsRC->u32Version = PDM_DEVINSRC_VERSION;
358 pDevInsRC->iInstance = iInstance;
359 pDevInsRC->pvInstanceDataRC = pDevIns->pDevInsForRC + cbRC;
360 pDevInsRC->pvInstanceDataForRC = pDevIns->pDevInsForRC + RT_UOFFSETOF(PDMDEVINSRC, achInstanceData);
361 pDevInsRC->pCritSectRoRC = pDevIns->pDevInsForRC + cbRC + cbShared;
362 pDevInsRC->cbPciDev = cbPciDev;
363 pDevInsRC->cPciDevs = cPciDevs;
364 for (uint32_t i = 0; i < RT_MIN(cPciDevs, RT_ELEMENTS(pDevIns->apPciDevs)); i++)
365 pDevInsRC->apPciDevs[i] = pDevInsRC->pCritSectRoRC + cbCritSect + cbPciDev * i;
366
367 pDevInsRC->Internal.s.pVMRC = pGVM->pVMRC;
368 }
369
370 /*
371 * Add to the device instance array and set its handle value.
372 */
373 AssertCompile(sizeof(pGVM->pdmr0.padding) == sizeof(pGVM->pdmr0));
374 uint32_t idxR0Device = pGVM->pdmr0.s.cDevInstances;
375 if (idxR0Device < RT_ELEMENTS(pGVM->pdmr0.s.apDevInstances))
376 {
377 pGVM->pdmr0.s.apDevInstances[idxR0Device] = pDevIns;
378 pGVM->pdmr0.s.cDevInstances = idxR0Device + 1;
379 pDevIns->Internal.s.idxR0Device = idxR0Device;
380 pDevInsR3->Internal.s.idxR0Device = idxR0Device;
381
382 /*
383 * Call the early constructor if present.
384 */
385 if (pDevReg->pfnEarlyConstruct)
386 rc = pDevReg->pfnEarlyConstruct(pDevIns);
387 if (RT_SUCCESS(rc))
388 {
389 /*
390 * We're done.
391 */
392 *ppDevInsR3 = RTR0MemObjAddressR3(hMapObj);
393 return rc;
394 }
395
396 /*
397 * Bail out.
398 */
399 if (pDevIns->pReg->pfnFinalDestruct)
400 pDevIns->pReg->pfnFinalDestruct(pDevIns);
401
402 pGVM->pdmr0.s.apDevInstances[idxR0Device] = NULL;
403 Assert(pGVM->pdmr0.s.cDevInstances == idxR0Device + 1);
404 pGVM->pdmr0.s.cDevInstances = idxR0Device;
405 }
406
407 RTR0MemObjFree(hMapObj, true);
408 }
409 RTR0MemObjFree(hMemObj, true);
410 return rc;
411}
412
413
414/**
415 * Used by ring-3 PDM to create a device instance that operates both in ring-3
416 * and ring-0.
417 *
418 * Creates an instance of a device (for both ring-3 and ring-0, and optionally
419 * raw-mode context).
420 *
421 * @returns VBox status code.
422 * @param pGVM The global (ring-0) VM structure.
423 * @param pReq Pointer to the request buffer.
424 * @thread EMT(0)
425 */
426VMMR0_INT_DECL(int) PDMR0DeviceCreateReqHandler(PGVM pGVM, PPDMDEVICECREATEREQ pReq)
427{
428 LogFlow(("PDMR0DeviceCreateReqHandler: %s in %s\n", pReq->szDevName, pReq->szModName));
429
430 /*
431 * Validate the request.
432 */
433 AssertReturn(pReq->Hdr.cbReq == sizeof(*pReq), VERR_INVALID_PARAMETER);
434 pReq->pDevInsR3 = NIL_RTR3PTR;
435
436 int rc = GVMMR0ValidateGVMandEMT(pGVM, 0);
437 AssertRCReturn(rc, rc);
438
439 AssertReturn(pReq->fFlags != 0, VERR_INVALID_FLAGS);
440 AssertReturn(pReq->fClass != 0, VERR_WRONG_TYPE);
441 AssertReturn(pReq->uSharedVersion != 0, VERR_INVALID_PARAMETER);
442 AssertReturn(pReq->cbInstanceShared != 0, VERR_INVALID_PARAMETER);
443 size_t const cchDevName = RTStrNLen(pReq->szDevName, sizeof(pReq->szDevName));
444 AssertReturn(cchDevName < sizeof(pReq->szDevName), VERR_NO_STRING_TERMINATOR);
445 AssertReturn(cchDevName > 0, VERR_EMPTY_STRING);
446 AssertReturn(cchDevName < RT_SIZEOFMEMB(PDMDEVREG, szName), VERR_NOT_FOUND);
447
448 size_t const cchModName = RTStrNLen(pReq->szModName, sizeof(pReq->szModName));
449 AssertReturn(cchModName < sizeof(pReq->szModName), VERR_NO_STRING_TERMINATOR);
450 AssertReturn(cchModName > 0, VERR_EMPTY_STRING);
451 AssertReturn(pReq->cbInstanceShared <= PDM_MAX_DEVICE_INSTANCE_SIZE, VERR_OUT_OF_RANGE);
452 AssertReturn(pReq->cbInstanceR3 <= PDM_MAX_DEVICE_INSTANCE_SIZE, VERR_OUT_OF_RANGE);
453 AssertReturn(pReq->cbInstanceRC <= PDM_MAX_DEVICE_INSTANCE_SIZE, VERR_OUT_OF_RANGE);
454 AssertReturn(pReq->iInstance < 1024, VERR_OUT_OF_RANGE);
455 AssertReturn(pReq->iInstance < pReq->cMaxInstances, VERR_OUT_OF_RANGE);
456 AssertReturn(pReq->cMaxPciDevices <= 8, VERR_OUT_OF_RANGE);
457 AssertReturn(pReq->cMaxMsixVectors <= VBOX_MSIX_MAX_ENTRIES, VERR_OUT_OF_RANGE);
458
459 /*
460 * Reference the module.
461 */
462 void *hMod = NULL;
463 rc = SUPR0LdrModByName(pGVM->pSession, pReq->szModName, &hMod);
464 if (RT_FAILURE(rc))
465 {
466 LogRel(("PDMR0DeviceCreateReqHandler: SUPR0LdrModByName(,%s,) failed: %Rrc\n", pReq->szModName, rc));
467 return rc;
468 }
469
470 /*
471 * Look for the the module and the device registration structure.
472 */
473 int rcLock = SUPR0LdrLock(pGVM->pSession);
474 AssertRC(rc);
475
476 rc = VERR_NOT_FOUND;
477 PPDMDEVMODREGR0 pMod;
478 RTListForEach(&g_PDMDevModList, pMod, PDMDEVMODREGR0, ListEntry)
479 {
480 if (pMod->hMod == hMod)
481 {
482 /*
483 * Found the module. We can drop the loader lock now before we
484 * search the devices it registers.
485 */
486 if (RT_SUCCESS(rcLock))
487 {
488 rcLock = SUPR0LdrUnlock(pGVM->pSession);
489 AssertRC(rcLock);
490 }
491 rcLock = VERR_ALREADY_RESET;
492
493 PCPDMDEVREGR0 *papDevRegs = pMod->papDevRegs;
494 size_t i = pMod->cDevRegs;
495 while (i-- > 0)
496 {
497 PCPDMDEVREGR0 pDevReg = papDevRegs[i];
498 LogFlow(("PDMR0DeviceCreateReqHandler: candidate #%u: %s %#x\n", i, pReq->szDevName, pDevReg->u32Version));
499 if ( PDM_VERSION_ARE_COMPATIBLE(pDevReg->u32Version, PDM_DEVREGR0_VERSION)
500 && pDevReg->szName[cchDevName] == '\0'
501 && memcmp(pDevReg->szName, pReq->szDevName, cchDevName) == 0)
502 {
503
504 /*
505 * Found the device, now check whether it matches the ring-3 registration.
506 */
507 if ( pReq->uSharedVersion == pDevReg->uSharedVersion
508 && pReq->cbInstanceShared == pDevReg->cbInstanceShared
509 && pReq->cbInstanceRC == pDevReg->cbInstanceRC
510 && pReq->fFlags == pDevReg->fFlags
511 && pReq->fClass == pDevReg->fClass
512 && pReq->cMaxInstances == pDevReg->cMaxInstances
513 && pReq->cMaxPciDevices == pDevReg->cMaxPciDevices
514 && pReq->cMaxMsixVectors == pDevReg->cMaxMsixVectors)
515 {
516 rc = pdmR0DeviceCreateWorker(pGVM, pDevReg, pReq->iInstance, pReq->cbInstanceR3, pReq->cbInstanceRC,
517 NIL_RTRCPTR /** @todo new raw-mode */, hMod, &pReq->pDevInsR3);
518 if (RT_SUCCESS(rc))
519 hMod = NULL; /* keep the module reference */
520 }
521 else
522 {
523 LogRel(("PDMR0DeviceCreate: Ring-3 does not match ring-0 device registration (%s):\n"
524 " uSharedVersion: %#x vs %#x\n"
525 " cbInstanceShared: %#x vs %#x\n"
526 " cbInstanceRC: %#x vs %#x\n"
527 " fFlags: %#x vs %#x\n"
528 " fClass: %#x vs %#x\n"
529 " cMaxInstances: %#x vs %#x\n"
530 " cMaxPciDevices: %#x vs %#x\n"
531 " cMaxMsixVectors: %#x vs %#x\n"
532 ,
533 pReq->szDevName,
534 pReq->uSharedVersion, pDevReg->uSharedVersion,
535 pReq->cbInstanceShared, pDevReg->cbInstanceShared,
536 pReq->cbInstanceRC, pDevReg->cbInstanceRC,
537 pReq->fFlags, pDevReg->fFlags,
538 pReq->fClass, pDevReg->fClass,
539 pReq->cMaxInstances, pDevReg->cMaxInstances,
540 pReq->cMaxPciDevices, pDevReg->cMaxPciDevices,
541 pReq->cMaxMsixVectors, pDevReg->cMaxMsixVectors));
542 rc = VERR_INCOMPATIBLE_CONFIG;
543 }
544 }
545 }
546 break;
547 }
548 }
549
550 if (RT_SUCCESS_NP(rcLock))
551 {
552 rcLock = SUPR0LdrUnlock(pGVM->pSession);
553 AssertRC(rcLock);
554 }
555 SUPR0LdrModRelease(pGVM->pSession, hMod);
556 return rc;
557}
558
559
560/**
561 * Used by ring-3 PDM to call standard ring-0 device methods.
562 *
563 * @returns VBox status code.
564 * @param pGVM The global (ring-0) VM structure.
565 * @param pReq Pointer to the request buffer.
566 * @param idCpu The ID of the calling EMT.
567 * @thread EMT(0), except for PDMDEVICEGENCALL_REQUEST which can be any EMT.
568 */
569VMMR0_INT_DECL(int) PDMR0DeviceGenCallReqHandler(PGVM pGVM, PPDMDEVICEGENCALLREQ pReq, VMCPUID idCpu)
570{
571 /*
572 * Validate the request.
573 */
574 AssertReturn(pReq->Hdr.cbReq == sizeof(*pReq), VERR_INVALID_PARAMETER);
575
576 int rc = GVMMR0ValidateGVMandEMT(pGVM, idCpu);
577 AssertRCReturn(rc, rc);
578
579 AssertReturn(pReq->idxR0Device < pGVM->pdmr0.s.cDevInstances, VERR_INVALID_HANDLE);
580 PPDMDEVINSR0 pDevIns = pGVM->pdmr0.s.apDevInstances[pReq->idxR0Device];
581 AssertPtrReturn(pDevIns, VERR_INVALID_HANDLE);
582 AssertReturn(pDevIns->pDevInsForR3 == pReq->pDevInsR3, VERR_INVALID_HANDLE);
583
584 /*
585 * Make the call.
586 */
587 rc = VINF_SUCCESS /*VINF_NOT_IMPLEMENTED*/;
588 switch (pReq->enmCall)
589 {
590 case PDMDEVICEGENCALL_CONSTRUCT:
591 AssertMsgBreakStmt(pGVM->enmVMState < VMSTATE_CREATED, ("enmVMState=%d\n", pGVM->enmVMState), rc = VERR_INVALID_STATE);
592 AssertReturn(idCpu == 0, VERR_VM_THREAD_NOT_EMT);
593 if (pDevIns->pReg->pfnConstruct)
594 rc = pDevIns->pReg->pfnConstruct(pDevIns);
595 break;
596
597 case PDMDEVICEGENCALL_DESTRUCT:
598 AssertMsgBreakStmt(pGVM->enmVMState < VMSTATE_CREATED || pGVM->enmVMState >= VMSTATE_DESTROYING,
599 ("enmVMState=%d\n", pGVM->enmVMState), rc = VERR_INVALID_STATE);
600 AssertReturn(idCpu == 0, VERR_VM_THREAD_NOT_EMT);
601 if (pDevIns->pReg->pfnDestruct)
602 {
603 pDevIns->pReg->pfnDestruct(pDevIns);
604 rc = VINF_SUCCESS;
605 }
606 break;
607
608 case PDMDEVICEGENCALL_REQUEST:
609 if (pDevIns->pReg->pfnRequest)
610 rc = pDevIns->pReg->pfnRequest(pDevIns, pReq->Params.Req.uReq, pReq->Params.Req.uArg);
611 else
612 rc = VERR_INVALID_FUNCTION;
613 break;
614
615 default:
616 AssertMsgFailed(("enmCall=%d\n", pReq->enmCall));
617 rc = VERR_INVALID_FUNCTION;
618 break;
619 }
620
621 return rc;
622}
623
624
625/**
626 * Legacy device mode compatiblity.
627 *
628 * @returns VBox status code.
629 * @param pGVM The global (ring-0) VM structure.
630 * @param pReq Pointer to the request buffer.
631 * @thread EMT(0)
632 */
633VMMR0_INT_DECL(int) PDMR0DeviceCompatSetCritSectReqHandler(PGVM pGVM, PPDMDEVICECOMPATSETCRITSECTREQ pReq)
634{
635 /*
636 * Validate the request.
637 */
638 AssertReturn(pReq->Hdr.cbReq == sizeof(*pReq), VERR_INVALID_PARAMETER);
639
640 int rc = GVMMR0ValidateGVMandEMT(pGVM, 0);
641 AssertRCReturn(rc, rc);
642
643 AssertReturn(pReq->idxR0Device < pGVM->pdmr0.s.cDevInstances, VERR_INVALID_HANDLE);
644 PPDMDEVINSR0 pDevIns = pGVM->pdmr0.s.apDevInstances[pReq->idxR0Device];
645 AssertPtrReturn(pDevIns, VERR_INVALID_HANDLE);
646 AssertReturn(pDevIns->pDevInsForR3 == pReq->pDevInsR3, VERR_INVALID_HANDLE);
647
648 AssertReturn(pGVM->enmVMState == VMSTATE_CREATING, VERR_INVALID_STATE);
649
650 /*
651 * The critical section address can be in a few different places:
652 * 1. shared data.
653 * 2. nop section.
654 * 3. pdm critsect.
655 */
656 PPDMCRITSECT pCritSect;
657 if (pReq->pCritSectR3 == pGVM->pVMR3 + RT_UOFFSETOF(VM, pdm.s.NopCritSect))
658 {
659 pCritSect = &pGVM->pdm.s.NopCritSect;
660 Log(("PDMR0DeviceCompatSetCritSectReqHandler: Nop - %p %#x\n", pCritSect, pCritSect->s.Core.u32Magic));
661 }
662 else if (pReq->pCritSectR3 == pGVM->pVMR3 + RT_UOFFSETOF(VM, pdm.s.CritSect))
663 {
664 pCritSect = &pGVM->pdm.s.CritSect;
665 Log(("PDMR0DeviceCompatSetCritSectReqHandler: PDM - %p %#x\n", pCritSect, pCritSect->s.Core.u32Magic));
666 }
667 else
668 {
669 size_t offCritSect = pReq->pCritSectR3 - pDevIns->pDevInsForR3R0->pvInstanceDataR3;
670 AssertLogRelMsgReturn( offCritSect < pDevIns->pReg->cbInstanceShared
671 && offCritSect + sizeof(PDMCRITSECT) <= pDevIns->pReg->cbInstanceShared,
672 ("offCritSect=%p pCritSectR3=%p cbInstanceShared=%#x (%s)\n",
673 offCritSect, pReq->pCritSectR3, pDevIns->pReg->cbInstanceShared, pDevIns->pReg->szName),
674 VERR_INVALID_POINTER);
675 pCritSect = (PPDMCRITSECT)((uint8_t *)pDevIns->pvInstanceDataR0 + offCritSect);
676 Log(("PDMR0DeviceCompatSetCritSectReqHandler: custom - %#x/%p %#x\n", offCritSect, pCritSect, pCritSect->s.Core.u32Magic));
677 }
678 AssertLogRelMsgReturn(pCritSect->s.Core.u32Magic == RTCRITSECT_MAGIC,
679 ("cs=%p magic=%#x dev=%s\n", pCritSect, pCritSect->s.Core.u32Magic, pDevIns->pReg->szName),
680 VERR_INVALID_MAGIC);
681
682 /*
683 * Make the update.
684 */
685 pDevIns->pCritSectRoR0 = pCritSect;
686
687 return VINF_SUCCESS;
688}
689
690
691/**
692 * Registers the device implementations living in a module.
693 *
694 * This should normally only be called during ModuleInit(). The should be a
695 * call to PDMR0DeviceDeregisterModule from the ModuleTerm() function to undo
696 * the effects of this call.
697 *
698 * @returns VBox status code.
699 * @param hMod The module handle of the module being registered.
700 * @param pModReg The module registration structure. This will be
701 * used directly so it must live as long as the module
702 * and be writable.
703 *
704 * @note Caller must own the loader lock!
705 */
706VMMR0DECL(int) PDMR0DeviceRegisterModule(void *hMod, PPDMDEVMODREGR0 pModReg)
707{
708 /*
709 * Validate the input.
710 */
711 AssertPtrReturn(hMod, VERR_INVALID_HANDLE);
712 Assert(SUPR0LdrIsLockOwnerByMod(hMod, true));
713
714 AssertPtrReturn(pModReg, VERR_INVALID_POINTER);
715 AssertLogRelMsgReturn(PDM_VERSION_ARE_COMPATIBLE(pModReg->u32Version, PDM_DEVMODREGR0_VERSION),
716 ("pModReg->u32Version=%#x vs %#x\n", pModReg->u32Version, PDM_DEVMODREGR0_VERSION),
717 VERR_VERSION_MISMATCH);
718 AssertLogRelMsgReturn(pModReg->cDevRegs <= 256 && pModReg->cDevRegs > 0, ("cDevRegs=%u\n", pModReg->cDevRegs),
719 VERR_OUT_OF_RANGE);
720 AssertLogRelMsgReturn(pModReg->hMod == NULL, ("hMod=%p\n", pModReg->hMod), VERR_INVALID_PARAMETER);
721 AssertLogRelMsgReturn(pModReg->ListEntry.pNext == NULL, ("pNext=%p\n", pModReg->ListEntry.pNext), VERR_INVALID_PARAMETER);
722 AssertLogRelMsgReturn(pModReg->ListEntry.pPrev == NULL, ("pPrev=%p\n", pModReg->ListEntry.pPrev), VERR_INVALID_PARAMETER);
723
724 for (size_t i = 0; i < pModReg->cDevRegs; i++)
725 {
726 PCPDMDEVREGR0 pDevReg = pModReg->papDevRegs[i];
727 AssertLogRelMsgReturn(RT_VALID_PTR(pDevReg), ("[%u]: %p\n", i, pDevReg), VERR_INVALID_POINTER);
728 AssertLogRelMsgReturn(PDM_VERSION_ARE_COMPATIBLE(pDevReg->u32Version, PDM_DEVREGR0_VERSION),
729 ("pDevReg->u32Version=%#x vs %#x\n", pModReg->u32Version, PDM_DEVREGR0_VERSION), VERR_VERSION_MISMATCH);
730 AssertLogRelMsgReturn(RT_VALID_PTR(pDevReg->pszDescription), ("[%u]: %p\n", i, pDevReg->pszDescription), VERR_INVALID_POINTER);
731 AssertLogRelMsgReturn(pDevReg->uReserved0 == 0, ("[%u]: %#x\n", i, pDevReg->uReserved0), VERR_INVALID_PARAMETER);
732 AssertLogRelMsgReturn(pDevReg->fClass != 0, ("[%u]: %#x\n", i, pDevReg->fClass), VERR_INVALID_PARAMETER);
733 AssertLogRelMsgReturn(pDevReg->fFlags != 0, ("[%u]: %#x\n", i, pDevReg->fFlags), VERR_INVALID_PARAMETER);
734 AssertLogRelMsgReturn(pDevReg->cMaxInstances > 0, ("[%u]: %#x\n", i, pDevReg->cMaxInstances), VERR_INVALID_PARAMETER);
735 AssertLogRelMsgReturn(pDevReg->cMaxPciDevices <= 8, ("[%u]: %#x\n", i, pDevReg->cMaxPciDevices), VERR_INVALID_PARAMETER);
736 AssertLogRelMsgReturn(pDevReg->cMaxMsixVectors <= VBOX_MSIX_MAX_ENTRIES,
737 ("[%u]: %#x\n", i, pDevReg->cMaxMsixVectors), VERR_INVALID_PARAMETER);
738
739 /* The name must be printable ascii and correctly terminated. */
740 for (size_t off = 0; off < RT_ELEMENTS(pDevReg->szName); off++)
741 {
742 char ch = pDevReg->szName[off];
743 AssertLogRelMsgReturn(RT_C_IS_PRINT(ch) || (ch == '\0' && off > 0),
744 ("[%u]: off=%u szName: %.*Rhxs\n", i, off, sizeof(pDevReg->szName), &pDevReg->szName[0]),
745 VERR_INVALID_NAME);
746 if (ch == '\0')
747 break;
748 }
749 }
750
751 /*
752 * Add it, assuming we're being called at ModuleInit/ModuleTerm time only, or
753 * that the caller has already taken the loader lock.
754 */
755 pModReg->hMod = hMod;
756 RTListAppend(&g_PDMDevModList, &pModReg->ListEntry);
757
758 return VINF_SUCCESS;
759}
760
761
762/**
763 * Deregisters the device implementations living in a module.
764 *
765 * This should normally only be called during ModuleTerm().
766 *
767 * @returns VBox status code.
768 * @param hMod The module handle of the module being registered.
769 * @param pModReg The module registration structure. This will be
770 * used directly so it must live as long as the module
771 * and be writable.
772 *
773 * @note Caller must own the loader lock!
774 */
775VMMR0DECL(int) PDMR0DeviceDeregisterModule(void *hMod, PPDMDEVMODREGR0 pModReg)
776{
777 /*
778 * Validate the input.
779 */
780 AssertPtrReturn(hMod, VERR_INVALID_HANDLE);
781 Assert(SUPR0LdrIsLockOwnerByMod(hMod, true));
782
783 AssertPtrReturn(pModReg, VERR_INVALID_POINTER);
784 AssertLogRelMsgReturn(PDM_VERSION_ARE_COMPATIBLE(pModReg->u32Version, PDM_DEVMODREGR0_VERSION),
785 ("pModReg->u32Version=%#x vs %#x\n", pModReg->u32Version, PDM_DEVMODREGR0_VERSION),
786 VERR_VERSION_MISMATCH);
787 AssertLogRelMsgReturn(pModReg->hMod == hMod || pModReg->hMod == NULL, ("pModReg->hMod=%p vs %p\n", pModReg->hMod, hMod),
788 VERR_INVALID_PARAMETER);
789
790 /*
791 * Unlink the registration record and return it to virgin conditions. Ignore
792 * the call if not registered.
793 */
794 if (pModReg->hMod)
795 {
796 pModReg->hMod = NULL;
797 RTListNodeRemove(&pModReg->ListEntry);
798 pModReg->ListEntry.pNext = NULL;
799 pModReg->ListEntry.pPrev = NULL;
800 return VINF_SUCCESS;
801 }
802 return VWRN_NOT_FOUND;
803}
804
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