VirtualBox

source: vbox/trunk/src/VBox/VMM/PDM.cpp@ 4071

最後變更 在這個檔案從4071是 4071,由 vboxsync 提交於 18 年 前

Biggest check-in ever. New source code headers for all (C) innotek files.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 32.4 KB
 
1/* $Id: PDM.cpp 4071 2007-08-07 17:07:59Z vboxsync $ */
2/** @file
3 * PDM - Pluggable Device Manager.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/** @page pg_pdm PDM - The Pluggable Device Manager
20 *
21 * VBox is designed to be very configurable, i.e. the ability to select
22 * virtual devices and configure them uniquely for a VM. For this reason
23 * virtual devices are not statically linked with the VMM but loaded and
24 * linked at runtime thru the Configuration Manager (CFGM). PDM will use
25 * CFGM to enumerate devices which needs loading and instantiation.
26 *
27 *
28 * @section sec_pdm_dev The Pluggable Device
29 *
30 * Devices register themselves when the module containing them is loaded.
31 * PDM will call an entry point 'VBoxDevicesRegister' when loading a device
32 * module. The device module will then use the supplied callback table to
33 * check the VMM version and to register its devices. Each device have an
34 * unique (for the configured VM) name (string). The name is not only used
35 * in PDM but in CFGM - to organize device and device instance settings - and
36 * by anyone who wants to do ioctls to the device.
37 *
38 * When all device modules have been successfully loaded PDM will instantiate
39 * those devices which are configured for the VM. Mark that this might mean
40 * creating several instances of some devices. When instantiating a device
41 * PDM provides device instance memory and a callback table with the VM APIs
42 * which the device instance is trusted with.
43 *
44 * Some devices are trusted devices, most are not. The trusted devices are
45 * an integrated part of the VM and can obtain the VM handle from their
46 * device instance handles, thus enabling them to call any VM api. Untrusted
47 * devices are can only use the callbacks provided during device
48 * instantiation.
49 *
50 * The guest context extention (optional) of a device is initialized as part
51 * of the GC init. A device marks in it's registration structure that it have
52 * a GC part, in which module and which name the entry point have. PDM will
53 * use its loader facilities to load this module into GC and to find the
54 * specified entry point.
55 *
56 * When writing a GC extention the programmer must keep in mind that this
57 * code will be relocated, so that using global/static pointer variables
58 * won't work.
59 *
60 *
61 * @section sec_pdm_drv The Pluggable Drivers
62 *
63 * The VM devices are often accessing host hardware or OS facilities. For
64 * most devices these facilities can be abstracted in one or more levels.
65 * These abstractions are called drivers.
66 *
67 * For instance take a DVD/CD drive. This can be connected to a SCSI
68 * controller, EIDE controller or SATA controller. The basics of the
69 * DVD/CD drive implementation remains the same - eject, insert,
70 * read, seek, and such. (For the scsi case, you might wanna speak SCSI
71 * directly to, but that can of course be fixed.) So, it makes much sense to
72 * have a generic CD/DVD driver which implements this.
73 *
74 * Then the media 'inserted' into the DVD/CD drive can be a ISO image, or
75 * it can be read from a real CD or DVD drive (there are probably other
76 * custom formats someone could desire to read or construct too). So, it
77 * would make sense to have abstracted interfaces for dealing with this
78 * in a generic way so the cdrom unit doesn't have to implement it all.
79 * Thus we have created the CDROM/DVD media driver family.
80 *
81 * So, for this example the IDE controller #1 (i.e. secondary) will have
82 * the DVD/CD Driver attached to it's LUN #0 (master). When a media is mounted
83 * the DVD/CD Driver will have a ISO, NativeCD, NativeDVD or RAW (media) Driver
84 * attached.
85 *
86 * It is possible to configure many levels of drivers inserting filters, loggers,
87 * or whatever you desire into the chain.
88 *
89 *
90 * @subsection sec_pdm_drv_interfaces Interfaces
91 *
92 * The pluggable drivers exposes one standard interface (callback table) which
93 * is used to construct, destruct, attach, detach, and query other interfaces.
94 * A device will query the interfaces required for it's operation during init
95 * and hotplug. PDM will query some interfaces during runtime mounting too.
96 *
97 * ... list interfaces ...
98 *
99 */
100
101
102/*******************************************************************************
103* Header Files *
104*******************************************************************************/
105#define LOG_GROUP LOG_GROUP_PDM
106#include "PDMInternal.h"
107#include <VBox/pdm.h>
108#include <VBox/mm.h>
109#include <VBox/ssm.h>
110#include <VBox/vm.h>
111#include <VBox/vmm.h>
112#include <VBox/param.h>
113#include <VBox/err.h>
114#include <VBox/sup.h>
115
116#include <VBox/log.h>
117#include <iprt/asm.h>
118#include <iprt/assert.h>
119#include <iprt/alloc.h>
120#include <iprt/ldr.h>
121#include <iprt/path.h>
122#include <iprt/string.h>
123
124
125/*******************************************************************************
126* Defined Constants And Macros *
127*******************************************************************************/
128/** The PDM saved state version. */
129#define PDM_SAVED_STATE_VERSION 3
130
131
132/*******************************************************************************
133* Internal Functions *
134*******************************************************************************/
135static DECLCALLBACK(int) pdmR3Save(PVM pVM, PSSMHANDLE pSSM);
136static DECLCALLBACK(int) pdmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version);
137static DECLCALLBACK(int) pdmR3LoadPrep(PVM pVM, PSSMHANDLE pSSM);
138static DECLCALLBACK(void) pdmR3PollerTimer(PVM pVM, PTMTIMER pTimer, void *pvUser);
139
140
141
142/**
143 * Initializes the PDM.
144 *
145 * @returns VBox status code.
146 * @param pVM The VM to operate on.
147 */
148PDMR3DECL(int) PDMR3Init(PVM pVM)
149{
150 LogFlow(("PDMR3Init\n"));
151 /*
152 * Assert alignment and sizes.
153 */
154 AssertRelease(!(RT_OFFSETOF(VM, pdm.s) & 31));
155 AssertRelease(sizeof(pVM->pdm.s) <= sizeof(pVM->pdm.padding));
156
157 /*
158 * Init the structure.
159 */
160 pVM->pdm.s.offVM = RT_OFFSETOF(VM, pdm.s);
161
162 int rc = TMR3TimerCreateInternal(pVM, TMCLOCK_VIRTUAL, pdmR3PollerTimer, NULL, "PDM Poller", &pVM->pdm.s.pTimerPollers);
163 AssertRC(rc);
164
165 /*
166 * Initialize sub compontents.
167 */
168 rc = pdmR3CritSectInit(pVM);
169 if (VBOX_SUCCESS(rc))
170 {
171#ifdef VBOX_WITH_PDM_LOCK
172 rc = PDMR3CritSectInit(pVM, &pVM->pdm.s.CritSect, "PDM");
173 if (VBOX_SUCCESS(rc))
174#endif
175 rc = pdmR3LdrInit(pVM);
176 if (VBOX_SUCCESS(rc))
177 {
178 rc = pdmR3DrvInit(pVM);
179 if (VBOX_SUCCESS(rc))
180 {
181 rc = pdmR3DevInit(pVM);
182 if (VBOX_SUCCESS(rc))
183 {
184 /*
185 * Register the saved state data unit.
186 */
187 rc = SSMR3RegisterInternal(pVM, "pdm", 1, PDM_SAVED_STATE_VERSION, 128,
188 NULL, pdmR3Save, NULL,
189 pdmR3LoadPrep, pdmR3Load, NULL);
190 if (VBOX_SUCCESS(rc))
191 {
192 LogFlow(("PDM: Successfully initialized\n"));
193 return rc;
194 }
195
196 }
197 }
198 }
199 }
200
201 /*
202 * Cleanup and return failure.
203 */
204 PDMR3Term(pVM);
205 LogFlow(("PDMR3Init: returns %Vrc\n", rc));
206 return rc;
207}
208
209
210/**
211 * Applies relocations to data and code managed by this
212 * component. This function will be called at init and
213 * whenever the VMM need to relocate it self inside the GC.
214 *
215 * @param pVM VM handle.
216 * @param offDelta Relocation delta relative to old location.
217 * @remark The loader subcomponent is relocated by PDMR3LdrRelocate() very
218 * early in the relocation phase.
219 */
220PDMR3DECL(void) PDMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
221{
222 LogFlow(("PDMR3Relocate\n"));
223
224 /*
225 * Queues.
226 */
227 pdmR3QueueRelocate(pVM, offDelta);
228 pVM->pdm.s.pDevHlpQueueGC = PDMQueueGCPtr(pVM->pdm.s.pDevHlpQueueHC);
229
230 /*
231 * Critical sections.
232 */
233 pdmR3CritSectRelocate(pVM);
234
235 /*
236 * The registered PIC.
237 */
238 if (pVM->pdm.s.Pic.pDevInsGC)
239 {
240 pVM->pdm.s.Pic.pDevInsGC += offDelta;
241 pVM->pdm.s.Pic.pfnSetIrqGC += offDelta;
242 pVM->pdm.s.Pic.pfnGetInterruptGC += offDelta;
243 }
244
245 /*
246 * The registered APIC.
247 */
248 if (pVM->pdm.s.Apic.pDevInsGC)
249 {
250 pVM->pdm.s.Apic.pDevInsGC += offDelta;
251 pVM->pdm.s.Apic.pfnGetInterruptGC += offDelta;
252 pVM->pdm.s.Apic.pfnSetBaseGC += offDelta;
253 pVM->pdm.s.Apic.pfnGetBaseGC += offDelta;
254 pVM->pdm.s.Apic.pfnSetTPRGC += offDelta;
255 pVM->pdm.s.Apic.pfnGetTPRGC += offDelta;
256 pVM->pdm.s.Apic.pfnBusDeliverGC += offDelta;
257 }
258
259 /*
260 * The registered I/O APIC.
261 */
262 if (pVM->pdm.s.IoApic.pDevInsGC)
263 {
264 pVM->pdm.s.IoApic.pDevInsGC += offDelta;
265 pVM->pdm.s.IoApic.pfnSetIrqGC += offDelta;
266 }
267
268 /*
269 * The register PCI Buses.
270 */
271 for (unsigned i = 0; i < ELEMENTS(pVM->pdm.s.aPciBuses); i++)
272 {
273 if (pVM->pdm.s.aPciBuses[i].pDevInsGC)
274 {
275 pVM->pdm.s.aPciBuses[i].pDevInsGC += offDelta;
276 pVM->pdm.s.aPciBuses[i].pfnSetIrqGC += offDelta;
277 }
278 }
279
280 /*
281 * Devices.
282 */
283 GCPTRTYPE(PCPDMDEVHLPGC) pDevHlpGC;
284 int rc = PDMR3GetSymbolGC(pVM, NULL, "g_pdmGCDevHlp", &pDevHlpGC);
285 AssertReleaseMsgRC(rc, ("rc=%Vrc when resolving g_pdmGCDevHlp\n", rc));
286 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextHC)
287 {
288 if (pDevIns->pDevReg->fFlags & PDM_DEVREG_FLAGS_GC)
289 {
290 pDevIns->pDevHlpGC = pDevHlpGC;
291 pDevIns->pvInstanceDataGC = MMHyperR3ToGC(pVM, pDevIns->pvInstanceDataR3);
292 pDevIns->pvInstanceDataR0 = MMHyperR3ToR0(pVM, pDevIns->pvInstanceDataR3);
293 pDevIns->Internal.s.pVMGC = pVM->pVMGC;
294 if (pDevIns->Internal.s.pPciBusHC)
295 pDevIns->Internal.s.pPciBusGC = MMHyperR3ToGC(pVM, pDevIns->Internal.s.pPciBusHC);
296 if (pDevIns->Internal.s.pPciDeviceHC)
297 pDevIns->Internal.s.pPciDeviceGC = MMHyperR3ToGC(pVM, pDevIns->Internal.s.pPciDeviceHC);
298 if (pDevIns->pDevReg->pfnRelocate)
299 {
300 LogFlow(("PDMR3Relocate: Relocating device '%s'/%d\n",
301 pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
302 pDevIns->pDevReg->pfnRelocate(pDevIns, offDelta);
303 }
304 }
305 }
306}
307
308
309/**
310 * Terminates the PDM.
311 *
312 * Termination means cleaning up and freeing all resources,
313 * the VM it self is at this point powered off or suspended.
314 *
315 * @returns VBox status code.
316 * @param pVM The VM to operate on.
317 */
318PDMR3DECL(int) PDMR3Term(PVM pVM)
319{
320 LogFlow(("PDMR3Term:\n"));
321 AssertMsg(pVM->pdm.s.offVM, ("bad init order!\n"));
322
323 /*
324 * Iterate the device instances.
325 * The attached drivers are processed first.
326 * N.B. There is no need to mess around freeing memory allocated
327 * from any MM heap since MM will do that in its Term function.
328 */
329 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextHC)
330 {
331 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsHC; pLun; pLun = pLun->pNext)
332 {
333 /* Find the bottom driver. */
334 /** @todo Add pBottom to PDMLUN, this might not be the only place we will have to work it from the bottom up. */
335 PPDMDRVINS pDrvIns = pLun->pTop;
336 while (pDrvIns && pDrvIns->Internal.s.pDown)
337 pDrvIns = pDrvIns->Internal.s.pDown;
338
339 /* And destroy them one at a time from the bottom up. */
340 while (pDrvIns)
341 {
342 PPDMDRVINS pDrvNext = pDrvIns->Internal.s.pUp;
343
344 if (pDrvIns->pDrvReg->pfnDestruct)
345 {
346 LogFlow(("pdmR3DevTerm: Destroying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
347 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
348 pDrvIns->pDrvReg->pfnDestruct(pDrvIns);
349 TMR3TimerDestroyDriver(pVM, pDrvIns);
350 }
351
352 pDrvIns = pDrvNext;
353 }
354 }
355
356 if (pDevIns->pDevReg->pfnDestruct)
357 {
358 LogFlow(("pdmR3DevTerm: Destroying - device '%s'/%d\n",
359 pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
360 pDevIns->pDevReg->pfnDestruct(pDevIns);
361 TMR3TimerDestroyDevice(pVM, pDevIns);
362 pdmR3CritSectDeleteDevice(pVM, pDevIns);
363 }
364 }
365
366 /*
367 * Destroy all threads.
368 */
369 pdmR3ThreadDestroyAll(pVM);
370
371 /*
372 * Free modules.
373 */
374 pdmR3LdrTerm(pVM);
375
376#ifdef VBOX_WITH_PDM_LOCK
377 /*
378 * Destroy the PDM lock.
379 */
380 PDMR3CritSectDelete(&pVM->pdm.s.CritSect);
381#endif
382
383 LogFlow(("PDMR3Term: returns %Vrc\n", VINF_SUCCESS));
384 return VINF_SUCCESS;
385}
386
387
388/**
389 * Execute state save operation.
390 *
391 * @returns VBox status code.
392 * @param pVM VM Handle.
393 * @param pSSM SSM operation handle.
394 */
395static DECLCALLBACK(int) pdmR3Save(PVM pVM, PSSMHANDLE pSSM)
396{
397 LogFlow(("pdmR3Save:\n"));
398
399 /*
400 * Save interrupt and DMA states.
401 */
402 SSMR3PutUInt(pSSM, VM_FF_ISSET(pVM, VM_FF_INTERRUPT_APIC));
403 SSMR3PutUInt(pSSM, VM_FF_ISSET(pVM, VM_FF_INTERRUPT_PIC));
404 SSMR3PutUInt(pSSM, VM_FF_ISSET(pVM, VM_FF_PDM_DMA));
405
406 /*
407 * Save the list of device instances so we can check that
408 * they're all still there when we load the state and that
409 * nothing new have been added.
410 */
411 /** @todo We might have to filter out some device classes, like USB attached devices. */
412 uint32_t i = 0;
413 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextHC, i++)
414 {
415 SSMR3PutU32(pSSM, i);
416 SSMR3PutStrZ(pSSM, pDevIns->pDevReg->szDeviceName);
417 SSMR3PutU32(pSSM, pDevIns->iInstance);
418 }
419 return SSMR3PutU32(pSSM, ~0); /* terminator */
420}
421
422
423/**
424 * Prepare state load operation.
425 *
426 * This will dispatch pending operations and clear the FFs governed by PDM and its devices.
427 *
428 * @returns VBox status code.
429 * @param pVM The VM handle.
430 * @param pSSM The SSM handle.
431 */
432static DECLCALLBACK(int) pdmR3LoadPrep(PVM pVM, PSSMHANDLE pSSM)
433{
434 LogFlow(("pdmR3LoadPrep: %s%s%s%s\n",
435 VM_FF_ISSET(pVM, VM_FF_PDM_QUEUES) ? " VM_FF_PDM_QUEUES" : "",
436 VM_FF_ISSET(pVM, VM_FF_PDM_DMA) ? " VM_FF_PDM_DMA" : "",
437 VM_FF_ISSET(pVM, VM_FF_INTERRUPT_APIC) ? " VM_FF_INTERRUPT_APIC" : "",
438 VM_FF_ISSET(pVM, VM_FF_INTERRUPT_PIC) ? " VM_FF_INTERRUPT_PIC" : ""
439 ));
440
441 /*
442 * In case there is work pending that will raise an interrupt,
443 * start a DMA transfer, or release a lock. (unlikely)
444 */
445 if (VM_FF_ISSET(pVM, VM_FF_PDM_QUEUES))
446 PDMR3QueueFlushAll(pVM);
447
448 /* Clear the FFs. */
449 VM_FF_CLEAR(pVM, VM_FF_INTERRUPT_APIC);
450 VM_FF_CLEAR(pVM, VM_FF_INTERRUPT_PIC);
451 VM_FF_CLEAR(pVM, VM_FF_PDM_DMA);
452
453 return VINF_SUCCESS;
454}
455
456
457/**
458 * Execute state load operation.
459 *
460 * @returns VBox status code.
461 * @param pVM VM Handle.
462 * @param pSSM SSM operation handle.
463 * @param u32Version Data layout version.
464 */
465static DECLCALLBACK(int) pdmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version)
466{
467 LogFlow(("pdmR3Load:\n"));
468
469 /*
470 * Validate version.
471 */
472 if (u32Version != PDM_SAVED_STATE_VERSION)
473 {
474 Log(("pdmR3Load: Invalid version u32Version=%d!\n", u32Version));
475 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
476 }
477
478 /*
479 * Load the interrupt and DMA states.
480 */
481 /* APIC interrupt */
482 RTUINT fInterruptPending = 0;
483 int rc = SSMR3GetUInt(pSSM, &fInterruptPending);
484 if (VBOX_FAILURE(rc))
485 return rc;
486 if (fInterruptPending & ~1)
487 {
488 AssertMsgFailed(("fInterruptPending=%#x (APIC)\n", fInterruptPending));
489 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
490 }
491 AssertRelease(!VM_FF_ISSET(pVM, VM_FF_INTERRUPT_APIC));
492 if (fInterruptPending)
493 VM_FF_SET(pVM, VM_FF_INTERRUPT_APIC);
494
495 /* PIC interrupt */
496 fInterruptPending = 0;
497 rc = SSMR3GetUInt(pSSM, &fInterruptPending);
498 if (VBOX_FAILURE(rc))
499 return rc;
500 if (fInterruptPending & ~1)
501 {
502 AssertMsgFailed(("fInterruptPending=%#x (PIC)\n", fInterruptPending));
503 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
504 }
505 AssertRelease(!VM_FF_ISSET(pVM, VM_FF_INTERRUPT_PIC));
506 if (fInterruptPending)
507 VM_FF_SET(pVM, VM_FF_INTERRUPT_PIC);
508
509 /* DMA pending */
510 RTUINT fDMAPending = 0;
511 rc = SSMR3GetUInt(pSSM, &fDMAPending);
512 if (VBOX_FAILURE(rc))
513 return rc;
514 if (fDMAPending & ~1)
515 {
516 AssertMsgFailed(("fDMAPending=%#x\n", fDMAPending));
517 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
518 }
519 AssertRelease(!VM_FF_ISSET(pVM, VM_FF_PDM_DMA));
520 if (fDMAPending)
521 VM_FF_SET(pVM, VM_FF_PDM_DMA);
522
523 /*
524 * Load the list of devices and verify that they are all there.
525 *
526 * We boldly ASSUME that the order is fixed and that it's a good, this
527 * makes it way easier to validate...
528 */
529 uint32_t i = 0;
530 PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances;
531 for (;;pDevIns = pDevIns->Internal.s.pNextHC, i++)
532 {
533 /* Get the separator / terminator. */
534 uint32_t u32Sep;
535 int rc = SSMR3GetU32(pSSM, &u32Sep);
536 if (VBOX_FAILURE(rc))
537 return rc;
538 if (u32Sep == (uint32_t)~0)
539 break;
540 if (u32Sep != i)
541 {
542 AssertMsgFailed(("Out of seqence. u32Sep=%#x i=%#x\n", u32Sep, i));
543 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
544 }
545
546 /* get the name and instance number. */
547 char szDeviceName[sizeof(pDevIns->pDevReg->szDeviceName)];
548 rc = SSMR3GetStrZ(pSSM, szDeviceName, sizeof(szDeviceName));
549 if (VBOX_FAILURE(rc))
550 return rc;
551 RTUINT iInstance;
552 rc = SSMR3GetUInt(pSSM, &iInstance);
553 if (VBOX_FAILURE(rc))
554 return rc;
555
556 /* compare */
557 if (!pDevIns)
558 {
559 LogRel(("Device '%s'/%d not found in current config\n", szDeviceName, iInstance));
560 AssertFailed();
561 return VERR_SSM_LOAD_CONFIG_MISMATCH;
562 }
563 if ( strcmp(szDeviceName, pDevIns->pDevReg->szDeviceName)
564 || pDevIns->iInstance != iInstance
565 )
566 {
567 LogRel(("u32Sep=%d loaded '%s'/%d configured '%s'/%d\n",
568 u32Sep, szDeviceName, iInstance, pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
569 AssertFailed();
570 return VERR_SSM_LOAD_CONFIG_MISMATCH;
571 }
572 }
573
574 /*
575 * Too many devices?
576 */
577 if (pDevIns)
578 {
579 LogRel(("Device '%s'/%d not found in saved state\n", pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
580 AssertFailed();
581 return VERR_SSM_LOAD_CONFIG_MISMATCH;
582 }
583
584 return VINF_SUCCESS;
585}
586
587
588/**
589 * This function will notify all the devices and their
590 * attached drivers about the VM now being powered on.
591 *
592 * @param pVM VM Handle.
593 */
594PDMR3DECL(void) PDMR3PowerOn(PVM pVM)
595{
596 LogFlow(("PDMR3PowerOn:\n"));
597
598 /*
599 * Iterate the device instances.
600 * The attached drivers are processed first.
601 */
602 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextHC)
603 {
604 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsHC; pLun; pLun = pLun->pNext)
605 /** @todo Inverse the order here? */
606 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
607 if (pDrvIns->pDrvReg->pfnPowerOn)
608 {
609 LogFlow(("PDMR3PowerOn: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
610 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
611 pDrvIns->pDrvReg->pfnPowerOn(pDrvIns);
612 }
613
614 if (pDevIns->pDevReg->pfnPowerOn)
615 {
616 LogFlow(("PDMR3PowerOn: Notifying - device '%s'/%d\n",
617 pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
618 pDevIns->pDevReg->pfnPowerOn(pDevIns);
619 }
620 }
621
622 /*
623 * Resume all threads.
624 */
625 pdmR3ThreadResumeAll(pVM);
626
627 LogFlow(("PDMR3PowerOn: returns void\n"));
628}
629
630
631
632
633/**
634 * This function will notify all the devices and their
635 * attached drivers about the VM now being reset.
636 *
637 * @param pVM VM Handle.
638 */
639PDMR3DECL(void) PDMR3Reset(PVM pVM)
640{
641 LogFlow(("PDMR3Reset:\n"));
642
643 /*
644 * Clear all pending interrupts and DMA operations.
645 */
646 VM_FF_CLEAR(pVM, VM_FF_INTERRUPT_APIC);
647 VM_FF_CLEAR(pVM, VM_FF_INTERRUPT_PIC);
648 VM_FF_CLEAR(pVM, VM_FF_PDM_DMA);
649
650 /*
651 * Iterate the device instances.
652 * The attached drivers are processed first.
653 */
654 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextHC)
655 {
656 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsHC; pLun; pLun = pLun->pNext)
657 /** @todo Inverse the order here? */
658 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
659 if (pDrvIns->pDrvReg->pfnReset)
660 {
661 LogFlow(("PDMR3Reset: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
662 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
663 pDrvIns->pDrvReg->pfnReset(pDrvIns);
664 }
665
666 if (pDevIns->pDevReg->pfnReset)
667 {
668 LogFlow(("PDMR3Reset: Notifying - device '%s'/%d\n",
669 pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
670 pDevIns->pDevReg->pfnReset(pDevIns);
671 }
672 }
673
674 LogFlow(("PDMR3Reset: returns void\n"));
675}
676
677
678/**
679 * This function will notify all the devices and their
680 * attached drivers about the VM now being reset.
681 *
682 * @param pVM VM Handle.
683 */
684PDMR3DECL(void) PDMR3Suspend(PVM pVM)
685{
686 LogFlow(("PDMR3Suspend:\n"));
687
688 /*
689 * Iterate the device instances.
690 * The attached drivers are processed first.
691 */
692 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextHC)
693 {
694 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsHC; pLun; pLun = pLun->pNext)
695 /** @todo Inverse the order here? */
696 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
697 if (pDrvIns->pDrvReg->pfnSuspend)
698 {
699 LogFlow(("PDMR3Suspend: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
700 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
701 pDrvIns->pDrvReg->pfnSuspend(pDrvIns);
702 }
703
704 if (pDevIns->pDevReg->pfnSuspend)
705 {
706 LogFlow(("PDMR3Suspend: Notifying - device '%s'/%d\n",
707 pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
708 pDevIns->pDevReg->pfnSuspend(pDevIns);
709 }
710 }
711
712 /*
713 * Suspend all threads.
714 */
715 pdmR3ThreadSuspendAll(pVM);
716
717 LogFlow(("PDMR3Suspend: returns void\n"));
718}
719
720
721/**
722 * This function will notify all the devices and their
723 * attached drivers about the VM now being resumed.
724 *
725 * @param pVM VM Handle.
726 */
727PDMR3DECL(void) PDMR3Resume(PVM pVM)
728{
729 LogFlow(("PDMR3Resume:\n"));
730
731 /*
732 * Iterate the device instances.
733 * The attached drivers are processed first.
734 */
735 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextHC)
736 {
737 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsHC; pLun; pLun = pLun->pNext)
738 /** @todo Inverse the order here? */
739 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
740 if (pDrvIns->pDrvReg->pfnResume)
741 {
742 LogFlow(("PDMR3Resume: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
743 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
744 pDrvIns->pDrvReg->pfnResume(pDrvIns);
745 }
746
747 if (pDevIns->pDevReg->pfnResume)
748 {
749 LogFlow(("PDMR3Resume: Notifying - device '%s'/%d\n",
750 pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
751 pDevIns->pDevReg->pfnResume(pDevIns);
752 }
753 }
754
755 /*
756 * Resume all threads.
757 */
758 pdmR3ThreadResumeAll(pVM);
759
760 LogFlow(("PDMR3Resume: returns void\n"));
761}
762
763
764/**
765 * This function will notify all the devices and their
766 * attached drivers about the VM being powered off.
767 *
768 * @param pVM VM Handle.
769 */
770PDMR3DECL(void) PDMR3PowerOff(PVM pVM)
771{
772 LogFlow(("PDMR3PowerOff:\n"));
773
774 /*
775 * Iterate the device instances.
776 * The attached drivers are processed first.
777 */
778 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextHC)
779 {
780 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsHC; pLun; pLun = pLun->pNext)
781 /** @todo Inverse the order here? */
782 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
783 if (pDrvIns->pDrvReg->pfnPowerOff)
784 {
785 LogFlow(("PDMR3PowerOff: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
786 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
787 pDrvIns->pDrvReg->pfnPowerOff(pDrvIns);
788 }
789
790 if (pDevIns->pDevReg->pfnPowerOff)
791 {
792 LogFlow(("PDMR3PowerOff: Notifying - device '%s'/%d\n",
793 pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
794 pDevIns->pDevReg->pfnPowerOff(pDevIns);
795 }
796 }
797
798 /*
799 * Suspend all threads.
800 */
801 pdmR3ThreadSuspendAll(pVM);
802
803 LogFlow(("PDMR3PowerOff: returns void\n"));
804}
805
806
807/**
808 * Queries the base interace of a device instance.
809 *
810 * The caller can use this to query other interfaces the device implements
811 * and use them to talk to the device.
812 *
813 * @returns VBox status code.
814 * @param pVM VM handle.
815 * @param pszDevice Device name.
816 * @param iInstance Device instance.
817 * @param ppBase Where to store the pointer to the base device interface on success.
818 * @remark We're not doing any locking ATM, so don't try call this at times when the
819 * device chain is known to be updated.
820 */
821PDMR3DECL(int) PDMR3QueryDevice(PVM pVM, const char *pszDevice, unsigned iInstance, PPDMIBASE *ppBase)
822{
823 LogFlow(("PDMR3DeviceQuery: pszDevice=%p:{%s} iInstance=%u ppBase=%p\n", pszDevice, pszDevice, iInstance, ppBase));
824
825 /*
826 * Iterate registered devices looking for the device.
827 */
828 RTUINT cchDevice = strlen(pszDevice);
829 for (PPDMDEV pDev = pVM->pdm.s.pDevs; pDev; pDev = pDev->pNext)
830 {
831 if ( pDev->cchName == cchDevice
832 && !memcmp(pDev->pDevReg->szDeviceName, pszDevice, cchDevice))
833 {
834 /*
835 * Iterate device instances.
836 */
837 for (PPDMDEVINS pDevIns = pDev->pInstances; pDevIns; pDevIns = pDevIns->Internal.s.pPerDeviceNextHC)
838 {
839 if (pDevIns->iInstance == iInstance)
840 {
841 if (pDevIns->IBase.pfnQueryInterface)
842 {
843 *ppBase = &pDevIns->IBase;
844 LogFlow(("PDMR3DeviceQuery: return VINF_SUCCESS and *ppBase=%p\n", *ppBase));
845 return VINF_SUCCESS;
846 }
847
848 LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NO_IBASE\n"));
849 return VERR_PDM_DEVICE_INSTANCE_NO_IBASE;
850 }
851 }
852
853 LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NOT_FOUND\n"));
854 return VERR_PDM_DEVICE_INSTANCE_NOT_FOUND;
855 }
856 }
857
858 LogFlow(("PDMR3QueryDevice: returns VERR_PDM_DEVICE_NOT_FOUND\n"));
859 return VERR_PDM_DEVICE_NOT_FOUND;
860}
861
862
863/**
864 * Queries the base interface of a device LUN.
865 *
866 * This differs from PDMR3QueryLun by that it returns the interface on the
867 * device and not the top level driver.
868 *
869 * @returns VBox status code.
870 * @param pVM VM Handle.
871 * @param pszDevice Device name.
872 * @param iInstance Device instance.
873 * @param iLun The Logical Unit to obtain the interface of.
874 * @param ppBase Where to store the base interface pointer.
875 * @remark We're not doing any locking ATM, so don't try call this at times when the
876 * device chain is known to be updated.
877 */
878PDMR3DECL(int) PDMR3QueryDeviceLun(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
879{
880 LogFlow(("PDMR3QueryLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
881 pszDevice, pszDevice, iInstance, iLun, ppBase));
882
883 /*
884 * Find the LUN.
885 */
886 PPDMLUN pLun;
887 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
888 if (VBOX_SUCCESS(rc))
889 {
890 *ppBase = pLun->pBase;
891 LogFlow(("PDMR3QueryDeviceLun: return VINF_SUCCESS and *ppBase=%p\n", *ppBase));
892 return VINF_SUCCESS;
893 }
894 LogFlow(("PDMR3QueryDeviceLun: returns %Vrc\n", rc));
895 return rc;
896}
897
898
899/**
900 * Query the interface of the top level driver on a LUN.
901 *
902 * @returns VBox status code.
903 * @param pVM VM Handle.
904 * @param pszDevice Device name.
905 * @param iInstance Device instance.
906 * @param iLun The Logical Unit to obtain the interface of.
907 * @param ppBase Where to store the base interface pointer.
908 * @remark We're not doing any locking ATM, so don't try call this at times when the
909 * device chain is known to be updated.
910 */
911PDMR3DECL(int) PDMR3QueryLun(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
912{
913 LogFlow(("PDMR3QueryLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
914 pszDevice, pszDevice, iInstance, iLun, ppBase));
915
916 /*
917 * Find the LUN.
918 */
919 PPDMLUN pLun;
920 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
921 if (VBOX_SUCCESS(rc))
922 {
923 if (pLun->pTop)
924 {
925 *ppBase = &pLun->pTop->IBase;
926 LogFlow(("PDMR3QueryLun: return %Vrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
927 return VINF_SUCCESS;
928 }
929 rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
930 }
931 LogFlow(("PDMR3QueryLun: returns %Vrc\n", rc));
932 return rc;
933}
934
935/**
936 * Executes pending DMA transfers.
937 * Forced Action handler.
938 *
939 * @param pVM VM handle.
940 */
941PDMR3DECL(void) PDMR3DmaRun(PVM pVM)
942{
943 VM_FF_CLEAR(pVM, VM_FF_PDM_DMA);
944 if (pVM->pdm.s.pDmac)
945 {
946 bool fMore = pVM->pdm.s.pDmac->Reg.pfnRun(pVM->pdm.s.pDmac->pDevIns);
947 if (fMore)
948 VM_FF_SET(pVM, VM_FF_PDM_DMA);
949 }
950}
951
952
953/**
954 * Call polling function.
955 *
956 * @param pVM VM handle.
957 */
958PDMR3DECL(void) PDMR3Poll(PVM pVM)
959{
960 /* This is temporary hack and shall be removed ASAP! */
961 unsigned iPoller = pVM->pdm.s.cPollers;
962 if (iPoller)
963 {
964 while (iPoller-- > 0)
965 pVM->pdm.s.apfnPollers[iPoller](pVM->pdm.s.aDrvInsPollers[iPoller]);
966 TMTimerSetMillies(pVM->pdm.s.pTimerPollers, 3);
967 }
968}
969
970
971/**
972 * Internal timer callback function.
973 *
974 * @param pVM The VM.
975 * @param pTimer The timer handle.
976 * @param pvUser User argument specified upon timer creation.
977 */
978static DECLCALLBACK(void) pdmR3PollerTimer(PVM pVM, PTMTIMER pTimer, void *pvUser)
979{
980 PDMR3Poll(pVM);
981}
982
983
984/**
985 * Serivce a VMMCALLHOST_PDM_LOCK call.
986 *
987 * @returns VBox status code.
988 * @param pVM The VM handle.
989 */
990PDMR3DECL(int) PDMR3LockCall(PVM pVM)
991{
992 return pdmLockEx(pVM, VERR_INTERNAL_ERROR);
993}
994
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette