VirtualBox

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

Last change on this file since 12923 was 12807, checked in by vboxsync, 16 years ago

PDM - VMMDevHeap: PDM_DEVHLP_VERSION minor increment, GCPhysVMMDevHeap init, spaces and doxygen.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 42.2 KB
Line 
1/* $Id: PDM.cpp 12807 2008-09-29 15:03:55Z vboxsync $ */
2/** @file
3 * PDM - Pluggable Device Manager.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/** @page pg_pdm PDM - The Pluggable Device Manager
24 *
25 * VBox is designed to be very configurable, i.e. the ability to select
26 * virtual devices and configure them uniquely for a VM. For this reason
27 * virtual devices are not statically linked with the VMM but loaded and
28 * linked at runtime thru the Configuration Manager (CFGM). PDM will use
29 * CFGM to enumerate devices which needs loading and instantiation.
30 *
31 *
32 * @section sec_pdm_dev The Pluggable Device
33 *
34 * Devices register themselves when the module containing them is loaded.
35 * PDM will call an entry point 'VBoxDevicesRegister' when loading a device
36 * module. The device module will then use the supplied callback table to
37 * check the VMM version and to register its devices. Each device have an
38 * unique (for the configured VM) name (string). The name is not only used
39 * in PDM but in CFGM - to organize device and device instance settings - and
40 * by anyone who wants to do ioctls to the device.
41 *
42 * When all device modules have been successfully loaded PDM will instantiate
43 * those devices which are configured for the VM. Mark that this might mean
44 * creating several instances of some devices. When instantiating a device
45 * PDM provides device instance memory and a callback table with the VM APIs
46 * which the device instance is trusted with.
47 *
48 * Some devices are trusted devices, most are not. The trusted devices are
49 * an integrated part of the VM and can obtain the VM handle from their
50 * device instance handles, thus enabling them to call any VM api. Untrusted
51 * devices are can only use the callbacks provided during device
52 * instantiation.
53 *
54 * The guest context extention (optional) of a device is initialized as part
55 * of the GC init. A device marks in it's registration structure that it have
56 * a GC part, in which module and which name the entry point have. PDM will
57 * use its loader facilities to load this module into GC and to find the
58 * specified entry point.
59 *
60 * When writing a GC extention the programmer must keep in mind that this
61 * code will be relocated, so that using global/static pointer variables
62 * won't work.
63 *
64 *
65 * @section sec_pdm_drv The Pluggable Drivers
66 *
67 * The VM devices are often accessing host hardware or OS facilities. For
68 * most devices these facilities can be abstracted in one or more levels.
69 * These abstractions are called drivers.
70 *
71 * For instance take a DVD/CD drive. This can be connected to a SCSI
72 * controller, EIDE controller or SATA controller. The basics of the
73 * DVD/CD drive implementation remains the same - eject, insert,
74 * read, seek, and such. (For the scsi case, you might wanna speak SCSI
75 * directly to, but that can of course be fixed.) So, it makes much sense to
76 * have a generic CD/DVD driver which implements this.
77 *
78 * Then the media 'inserted' into the DVD/CD drive can be a ISO image, or
79 * it can be read from a real CD or DVD drive (there are probably other
80 * custom formats someone could desire to read or construct too). So, it
81 * would make sense to have abstracted interfaces for dealing with this
82 * in a generic way so the cdrom unit doesn't have to implement it all.
83 * Thus we have created the CDROM/DVD media driver family.
84 *
85 * So, for this example the IDE controller #1 (i.e. secondary) will have
86 * the DVD/CD Driver attached to it's LUN #0 (master). When a media is mounted
87 * the DVD/CD Driver will have a ISO, NativeCD, NativeDVD or RAW (media) Driver
88 * attached.
89 *
90 * It is possible to configure many levels of drivers inserting filters, loggers,
91 * or whatever you desire into the chain.
92 *
93 *
94 * @subsection sec_pdm_drv_interfaces Interfaces
95 *
96 * The pluggable drivers exposes one standard interface (callback table) which
97 * is used to construct, destruct, attach, detach, and query other interfaces.
98 * A device will query the interfaces required for it's operation during init
99 * and hotplug. PDM will query some interfaces during runtime mounting too.
100 *
101 * ... list interfaces ...
102 *
103 */
104
105
106/*******************************************************************************
107* Header Files *
108*******************************************************************************/
109#define LOG_GROUP LOG_GROUP_PDM
110#include "PDMInternal.h"
111#include <VBox/pdm.h>
112#include <VBox/mm.h>
113#include <VBox/pgm.h>
114#include <VBox/ssm.h>
115#include <VBox/vm.h>
116#include <VBox/uvm.h>
117#include <VBox/vmm.h>
118#include <VBox/param.h>
119#include <VBox/err.h>
120#include <VBox/sup.h>
121
122#include <VBox/log.h>
123#include <iprt/asm.h>
124#include <iprt/assert.h>
125#include <iprt/alloc.h>
126#include <iprt/ldr.h>
127#include <iprt/path.h>
128#include <iprt/string.h>
129
130
131/*******************************************************************************
132* Defined Constants And Macros *
133*******************************************************************************/
134/** The PDM saved state version. */
135#define PDM_SAVED_STATE_VERSION 3
136
137
138/*******************************************************************************
139* Internal Functions *
140*******************************************************************************/
141static DECLCALLBACK(int) pdmR3Save(PVM pVM, PSSMHANDLE pSSM);
142static DECLCALLBACK(int) pdmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version);
143static DECLCALLBACK(int) pdmR3LoadPrep(PVM pVM, PSSMHANDLE pSSM);
144static DECLCALLBACK(void) pdmR3PollerTimer(PVM pVM, PTMTIMER pTimer, void *pvUser);
145
146
147
148/**
149 * Initializes the PDM part of the UVM.
150 *
151 * This doesn't really do much right now but has to be here for the sake
152 * of completeness.
153 *
154 * @returns VBox status code.
155 * @param pUVM Pointer to the user mode VM structure.
156 */
157PDMR3DECL(int) PDMR3InitUVM(PUVM pUVM)
158{
159 AssertCompile(sizeof(pUVM->pdm.s) <= sizeof(pUVM->pdm.padding));
160 AssertRelease(sizeof(pUVM->pdm.s) <= sizeof(pUVM->pdm.padding));
161 pUVM->pdm.s.pModules = NULL;
162 return VINF_SUCCESS;
163}
164
165
166/**
167 * Initializes the PDM.
168 *
169 * @returns VBox status code.
170 * @param pVM The VM to operate on.
171 */
172PDMR3DECL(int) PDMR3Init(PVM pVM)
173{
174 LogFlow(("PDMR3Init\n"));
175
176 /*
177 * Assert alignment and sizes.
178 */
179 AssertRelease(!(RT_OFFSETOF(VM, pdm.s) & 31));
180 AssertRelease(sizeof(pVM->pdm.s) <= sizeof(pVM->pdm.padding));
181
182 /*
183 * Init the structure.
184 */
185 pVM->pdm.s.offVM = RT_OFFSETOF(VM, pdm.s);
186 pVM->pdm.s.GCPhysVMMDevHeap = NIL_RTGCPHYS;
187
188 int rc = TMR3TimerCreateInternal(pVM, TMCLOCK_VIRTUAL, pdmR3PollerTimer, NULL, "PDM Poller", &pVM->pdm.s.pTimerPollers);
189 AssertRC(rc);
190
191 /*
192 * Initialize sub compontents.
193 */
194 rc = pdmR3CritSectInit(pVM);
195 if (VBOX_SUCCESS(rc))
196 {
197 rc = PDMR3CritSectInit(pVM, &pVM->pdm.s.CritSect, "PDM");
198 if (VBOX_SUCCESS(rc))
199 rc = pdmR3LdrInitU(pVM->pUVM);
200 if (VBOX_SUCCESS(rc))
201 {
202 rc = pdmR3DrvInit(pVM);
203 if (VBOX_SUCCESS(rc))
204 {
205 rc = pdmR3DevInit(pVM);
206 if (VBOX_SUCCESS(rc))
207 {
208#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
209 rc = pdmR3AsyncCompletionInit(pVM);
210 if (VBOX_SUCCESS(rc))
211#endif
212 {
213 /*
214 * Register the saved state data unit.
215 */
216 rc = SSMR3RegisterInternal(pVM, "pdm", 1, PDM_SAVED_STATE_VERSION, 128,
217 NULL, pdmR3Save, NULL,
218 pdmR3LoadPrep, pdmR3Load, NULL);
219 if (VBOX_SUCCESS(rc))
220 {
221 LogFlow(("PDM: Successfully initialized\n"));
222 return rc;
223 }
224
225 }
226 }
227 }
228 }
229 }
230
231 /*
232 * Cleanup and return failure.
233 */
234 PDMR3Term(pVM);
235 LogFlow(("PDMR3Init: returns %Vrc\n", rc));
236 return rc;
237}
238
239
240/**
241 * Applies relocations to data and code managed by this
242 * component. This function will be called at init and
243 * whenever the VMM need to relocate it self inside the GC.
244 *
245 * @param pVM VM handle.
246 * @param offDelta Relocation delta relative to old location.
247 * @remark The loader subcomponent is relocated by PDMR3LdrRelocate() very
248 * early in the relocation phase.
249 */
250PDMR3DECL(void) PDMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
251{
252 LogFlow(("PDMR3Relocate\n"));
253
254 /*
255 * Queues.
256 */
257 pdmR3QueueRelocate(pVM, offDelta);
258 pVM->pdm.s.pDevHlpQueueGC = PDMQueueGCPtr(pVM->pdm.s.pDevHlpQueueHC);
259
260 /*
261 * Critical sections.
262 */
263 pdmR3CritSectRelocate(pVM);
264
265 /*
266 * The registered PIC.
267 */
268 if (pVM->pdm.s.Pic.pDevInsRC)
269 {
270 pVM->pdm.s.Pic.pDevInsRC += offDelta;
271 pVM->pdm.s.Pic.pfnSetIrqRC += offDelta;
272 pVM->pdm.s.Pic.pfnGetInterruptRC += offDelta;
273 }
274
275 /*
276 * The registered APIC.
277 */
278 if (pVM->pdm.s.Apic.pDevInsRC)
279 {
280 pVM->pdm.s.Apic.pDevInsRC += offDelta;
281 pVM->pdm.s.Apic.pfnGetInterruptRC += offDelta;
282 pVM->pdm.s.Apic.pfnSetBaseRC += offDelta;
283 pVM->pdm.s.Apic.pfnGetBaseRC += offDelta;
284 pVM->pdm.s.Apic.pfnSetTPRRC += offDelta;
285 pVM->pdm.s.Apic.pfnGetTPRRC += offDelta;
286 pVM->pdm.s.Apic.pfnBusDeliverRC += offDelta;
287 }
288
289 /*
290 * The registered I/O APIC.
291 */
292 if (pVM->pdm.s.IoApic.pDevInsRC)
293 {
294 pVM->pdm.s.IoApic.pDevInsRC += offDelta;
295 pVM->pdm.s.IoApic.pfnSetIrqRC += offDelta;
296 }
297
298 /*
299 * The register PCI Buses.
300 */
301 for (unsigned i = 0; i < RT_ELEMENTS(pVM->pdm.s.aPciBuses); i++)
302 {
303 if (pVM->pdm.s.aPciBuses[i].pDevInsRC)
304 {
305 pVM->pdm.s.aPciBuses[i].pDevInsRC += offDelta;
306 pVM->pdm.s.aPciBuses[i].pfnSetIrqRC += offDelta;
307 }
308 }
309
310 /*
311 * Devices.
312 */
313 RCPTRTYPE(PCPDMDEVHLPGC) pDevHlpGC;
314 int rc = PDMR3GetSymbolGC(pVM, NULL, "g_pdmGCDevHlp", &pDevHlpGC);
315 AssertReleaseMsgRC(rc, ("rc=%Vrc when resolving g_pdmGCDevHlp\n", rc));
316 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextHC)
317 {
318 if (pDevIns->pDevReg->fFlags & PDM_DEVREG_FLAGS_GC)
319 {
320 pDevIns->pDevHlpGC = pDevHlpGC;
321 pDevIns->pvInstanceDataGC = MMHyperR3ToRC(pVM, pDevIns->pvInstanceDataR3);
322 pDevIns->pvInstanceDataR0 = MMHyperR3ToR0(pVM, pDevIns->pvInstanceDataR3);
323 pDevIns->Internal.s.pVMGC = pVM->pVMGC;
324 if (pDevIns->Internal.s.pPciBusHC)
325 pDevIns->Internal.s.pPciBusGC = MMHyperR3ToRC(pVM, pDevIns->Internal.s.pPciBusHC);
326 if (pDevIns->Internal.s.pPciDeviceHC)
327 pDevIns->Internal.s.pPciDeviceGC = MMHyperR3ToRC(pVM, pDevIns->Internal.s.pPciDeviceHC);
328 if (pDevIns->pDevReg->pfnRelocate)
329 {
330 LogFlow(("PDMR3Relocate: Relocating device '%s'/%d\n",
331 pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
332 pDevIns->pDevReg->pfnRelocate(pDevIns, offDelta);
333 }
334 }
335 }
336}
337
338
339/**
340 * Worker for pdmR3Term that terminates a LUN chain.
341 *
342 * @param pVM Pointer to the shared VM structure.
343 * @param pLun The head of the chain.
344 * @param pszDevice The name of the device (for logging).
345 * @param iInstance The device instance number (for logging).
346 */
347static void pdmR3TermLuns(PVM pVM, PPDMLUN pLun, const char *pszDevice, unsigned iInstance)
348{
349 for (; pLun; pLun = pLun->pNext)
350 {
351 /*
352 * Destroy them one at a time from the bottom up.
353 * (The serial device/drivers depends on this - bad.)
354 */
355 PPDMDRVINS pDrvIns = pLun->pBottom;
356 pLun->pBottom = pLun->pTop = NULL;
357 while (pDrvIns)
358 {
359 PPDMDRVINS pDrvNext = pDrvIns->Internal.s.pUp;
360
361 if (pDrvIns->pDrvReg->pfnDestruct)
362 {
363 LogFlow(("pdmR3DevTerm: Destroying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
364 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pszDevice, iInstance));
365 pDrvIns->pDrvReg->pfnDestruct(pDrvIns);
366 }
367
368 TMR3TimerDestroyDriver(pVM, pDrvIns);
369 //PDMR3QueueDestroyDriver(pVM, pDrvIns);
370 //pdmR3ThreadDestroyDriver(pVM, pDrvIns);
371 SSMR3DeregisterDriver(pVM, pDrvIns, NULL, 0);
372
373 pDrvIns = pDrvNext;
374 }
375 }
376}
377
378
379/**
380 * Terminates the PDM.
381 *
382 * Termination means cleaning up and freeing all resources,
383 * the VM it self is at this point powered off or suspended.
384 *
385 * @returns VBox status code.
386 * @param pVM The VM to operate on.
387 */
388PDMR3DECL(int) PDMR3Term(PVM pVM)
389{
390 LogFlow(("PDMR3Term:\n"));
391 AssertMsg(pVM->pdm.s.offVM, ("bad init order!\n"));
392
393 /*
394 * Iterate the device instances and attach drivers, doing
395 * relevant destruction processing.
396 *
397 * N.B. There is no need to mess around freeing memory allocated
398 * from any MM heap since MM will do that in its Term function.
399 */
400 /* usb ones first. */
401 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
402 {
403 pdmR3TermLuns(pVM, pUsbIns->Internal.s.pLuns, pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance);
404
405 if (pUsbIns->pUsbReg->pfnDestruct)
406 {
407 LogFlow(("pdmR3DevTerm: Destroying - device '%s'/%d\n",
408 pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
409 pUsbIns->pUsbReg->pfnDestruct(pUsbIns);
410 }
411
412 //TMR3TimerDestroyUsb(pVM, pUsbIns);
413 //SSMR3DeregisterUsb(pVM, pUsbIns, NULL, 0);
414 pdmR3ThreadDestroyUsb(pVM, pUsbIns);
415 }
416
417 /* then the 'normal' ones. */
418 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextHC)
419 {
420 pdmR3TermLuns(pVM, pDevIns->Internal.s.pLunsHC, pDevIns->pDevReg->szDeviceName, pDevIns->iInstance);
421
422 if (pDevIns->pDevReg->pfnDestruct)
423 {
424 LogFlow(("pdmR3DevTerm: Destroying - device '%s'/%d\n",
425 pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
426 pDevIns->pDevReg->pfnDestruct(pDevIns);
427 }
428
429 TMR3TimerDestroyDevice(pVM, pDevIns);
430 //SSMR3DeregisterDriver(pVM, pDevIns, NULL, 0);
431 pdmR3CritSectDeleteDevice(pVM, pDevIns);
432 //pdmR3ThreadDestroyDevice(pVM, pDevIns);
433 //PDMR3QueueDestroyDevice(pVM, pDevIns);
434 PGMR3PhysMMIO2Deregister(pVM, pDevIns, UINT32_MAX);
435 }
436
437 /*
438 * Destroy all threads.
439 */
440 pdmR3ThreadDestroyAll(pVM);
441
442#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
443 /*
444 * Free async completion managers.
445 */
446 pdmR3AsyncCompletionTerm(pVM);
447#endif
448
449 /*
450 * Free modules.
451 */
452 pdmR3LdrTermU(pVM->pUVM);
453
454 /*
455 * Destroy the PDM lock.
456 */
457 PDMR3CritSectDelete(&pVM->pdm.s.CritSect);
458
459 LogFlow(("PDMR3Term: returns %Vrc\n", VINF_SUCCESS));
460 return VINF_SUCCESS;
461}
462
463
464/**
465 * Terminates the PDM part of the UVM.
466 *
467 * This will unload any modules left behind.
468 *
469 * @param pUVM Pointer to the user mode VM structure.
470 */
471PDMR3DECL(void) PDMR3TermUVM(PUVM pUVM)
472{
473 /*
474 * In the normal cause of events we will now call pdmR3LdrTermU for
475 * the second time. In the case of init failure however, this might
476 * the first time, which is why we do it.
477 */
478 pdmR3LdrTermU(pUVM);
479}
480
481
482
483
484
485/**
486 * Execute state save operation.
487 *
488 * @returns VBox status code.
489 * @param pVM VM Handle.
490 * @param pSSM SSM operation handle.
491 */
492static DECLCALLBACK(int) pdmR3Save(PVM pVM, PSSMHANDLE pSSM)
493{
494 LogFlow(("pdmR3Save:\n"));
495
496 /*
497 * Save interrupt and DMA states.
498 */
499 SSMR3PutUInt(pSSM, VM_FF_ISSET(pVM, VM_FF_INTERRUPT_APIC));
500 SSMR3PutUInt(pSSM, VM_FF_ISSET(pVM, VM_FF_INTERRUPT_PIC));
501 SSMR3PutUInt(pSSM, VM_FF_ISSET(pVM, VM_FF_PDM_DMA));
502
503 /*
504 * Save the list of device instances so we can check that
505 * they're all still there when we load the state and that
506 * nothing new have been added.
507 */
508 /** @todo We might have to filter out some device classes, like USB attached devices. */
509 uint32_t i = 0;
510 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextHC, i++)
511 {
512 SSMR3PutU32(pSSM, i);
513 SSMR3PutStrZ(pSSM, pDevIns->pDevReg->szDeviceName);
514 SSMR3PutU32(pSSM, pDevIns->iInstance);
515 }
516 return SSMR3PutU32(pSSM, ~0); /* terminator */
517}
518
519
520/**
521 * Prepare state load operation.
522 *
523 * This will dispatch pending operations and clear the FFs governed by PDM and its devices.
524 *
525 * @returns VBox status code.
526 * @param pVM The VM handle.
527 * @param pSSM The SSM handle.
528 */
529static DECLCALLBACK(int) pdmR3LoadPrep(PVM pVM, PSSMHANDLE pSSM)
530{
531 LogFlow(("pdmR3LoadPrep: %s%s%s%s\n",
532 VM_FF_ISSET(pVM, VM_FF_PDM_QUEUES) ? " VM_FF_PDM_QUEUES" : "",
533 VM_FF_ISSET(pVM, VM_FF_PDM_DMA) ? " VM_FF_PDM_DMA" : "",
534 VM_FF_ISSET(pVM, VM_FF_INTERRUPT_APIC) ? " VM_FF_INTERRUPT_APIC" : "",
535 VM_FF_ISSET(pVM, VM_FF_INTERRUPT_PIC) ? " VM_FF_INTERRUPT_PIC" : ""
536 ));
537
538 /*
539 * In case there is work pending that will raise an interrupt,
540 * start a DMA transfer, or release a lock. (unlikely)
541 */
542 if (VM_FF_ISSET(pVM, VM_FF_PDM_QUEUES))
543 PDMR3QueueFlushAll(pVM);
544
545 /* Clear the FFs. */
546 VM_FF_CLEAR(pVM, VM_FF_INTERRUPT_APIC);
547 VM_FF_CLEAR(pVM, VM_FF_INTERRUPT_PIC);
548 VM_FF_CLEAR(pVM, VM_FF_PDM_DMA);
549
550 return VINF_SUCCESS;
551}
552
553
554/**
555 * Execute state load operation.
556 *
557 * @returns VBox status code.
558 * @param pVM VM Handle.
559 * @param pSSM SSM operation handle.
560 * @param u32Version Data layout version.
561 */
562static DECLCALLBACK(int) pdmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version)
563{
564 LogFlow(("pdmR3Load:\n"));
565
566 /*
567 * Validate version.
568 */
569 if (u32Version != PDM_SAVED_STATE_VERSION)
570 {
571 AssertMsgFailed(("pdmR3Load: Invalid version u32Version=%d!\n", u32Version));
572 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
573 }
574
575 /*
576 * Load the interrupt and DMA states.
577 */
578 /* APIC interrupt */
579 RTUINT fInterruptPending = 0;
580 int rc = SSMR3GetUInt(pSSM, &fInterruptPending);
581 if (VBOX_FAILURE(rc))
582 return rc;
583 if (fInterruptPending & ~1)
584 {
585 AssertMsgFailed(("fInterruptPending=%#x (APIC)\n", fInterruptPending));
586 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
587 }
588 AssertRelease(!VM_FF_ISSET(pVM, VM_FF_INTERRUPT_APIC));
589 if (fInterruptPending)
590 VM_FF_SET(pVM, VM_FF_INTERRUPT_APIC);
591
592 /* PIC interrupt */
593 fInterruptPending = 0;
594 rc = SSMR3GetUInt(pSSM, &fInterruptPending);
595 if (VBOX_FAILURE(rc))
596 return rc;
597 if (fInterruptPending & ~1)
598 {
599 AssertMsgFailed(("fInterruptPending=%#x (PIC)\n", fInterruptPending));
600 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
601 }
602 AssertRelease(!VM_FF_ISSET(pVM, VM_FF_INTERRUPT_PIC));
603 if (fInterruptPending)
604 VM_FF_SET(pVM, VM_FF_INTERRUPT_PIC);
605
606 /* DMA pending */
607 RTUINT fDMAPending = 0;
608 rc = SSMR3GetUInt(pSSM, &fDMAPending);
609 if (VBOX_FAILURE(rc))
610 return rc;
611 if (fDMAPending & ~1)
612 {
613 AssertMsgFailed(("fDMAPending=%#x\n", fDMAPending));
614 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
615 }
616 AssertRelease(!VM_FF_ISSET(pVM, VM_FF_PDM_DMA));
617 if (fDMAPending)
618 VM_FF_SET(pVM, VM_FF_PDM_DMA);
619
620 /*
621 * Load the list of devices and verify that they are all there.
622 *
623 * We boldly ASSUME that the order is fixed and that it's a good, this
624 * makes it way easier to validate...
625 */
626 uint32_t i = 0;
627 PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances;
628 for (;;pDevIns = pDevIns->Internal.s.pNextHC, i++)
629 {
630 /* Get the separator / terminator. */
631 uint32_t u32Sep;
632 int rc = SSMR3GetU32(pSSM, &u32Sep);
633 if (VBOX_FAILURE(rc))
634 return rc;
635 if (u32Sep == (uint32_t)~0)
636 break;
637 if (u32Sep != i)
638 AssertMsgFailedReturn(("Out of seqence. u32Sep=%#x i=%#x\n", u32Sep, i), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
639
640 /* get the name and instance number. */
641 char szDeviceName[sizeof(pDevIns->pDevReg->szDeviceName)];
642 rc = SSMR3GetStrZ(pSSM, szDeviceName, sizeof(szDeviceName));
643 if (VBOX_FAILURE(rc))
644 return rc;
645 RTUINT iInstance;
646 rc = SSMR3GetUInt(pSSM, &iInstance);
647 if (VBOX_FAILURE(rc))
648 return rc;
649
650 /* compare */
651 if (!pDevIns)
652 {
653 LogRel(("Device '%s'/%d not found in current config\n", szDeviceName, iInstance));
654 if (SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT)
655 AssertFailedReturn(VERR_SSM_LOAD_CONFIG_MISMATCH);
656 break;
657 }
658 if ( strcmp(szDeviceName, pDevIns->pDevReg->szDeviceName)
659 || pDevIns->iInstance != iInstance)
660 {
661 LogRel(("u32Sep=%d loaded '%s'/%d configured '%s'/%d\n",
662 u32Sep, szDeviceName, iInstance, pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
663 if (SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT)
664 AssertFailedReturn(VERR_SSM_LOAD_CONFIG_MISMATCH);
665 }
666 }
667
668 /*
669 * Too many devices?
670 */
671 if (pDevIns)
672 {
673 LogRel(("Device '%s'/%d not found in saved state\n", pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
674 if (SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT)
675 AssertFailedReturn(VERR_SSM_LOAD_CONFIG_MISMATCH);
676 }
677
678 return VINF_SUCCESS;
679}
680
681
682/**
683 * This function will notify all the devices and their
684 * attached drivers about the VM now being powered on.
685 *
686 * @param pVM VM Handle.
687 */
688PDMR3DECL(void) PDMR3PowerOn(PVM pVM)
689{
690 LogFlow(("PDMR3PowerOn:\n"));
691
692 /*
693 * Iterate the device instances.
694 * The attached drivers are processed first.
695 */
696 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextHC)
697 {
698 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsHC; pLun; pLun = pLun->pNext)
699 /** @todo Inverse the order here? */
700 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
701 if (pDrvIns->pDrvReg->pfnPowerOn)
702 {
703 LogFlow(("PDMR3PowerOn: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
704 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
705 pDrvIns->pDrvReg->pfnPowerOn(pDrvIns);
706 }
707
708 if (pDevIns->pDevReg->pfnPowerOn)
709 {
710 LogFlow(("PDMR3PowerOn: Notifying - device '%s'/%d\n",
711 pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
712 pDevIns->pDevReg->pfnPowerOn(pDevIns);
713 }
714 }
715
716#ifdef VBOX_WITH_USB
717 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
718 {
719 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
720 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
721 if (pDrvIns->pDrvReg->pfnPowerOn)
722 {
723 LogFlow(("PDMR3PowerOn: Notifying - driver '%s'/%d on LUN#%d of usb device '%s'/%d\n",
724 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
725 pDrvIns->pDrvReg->pfnPowerOn(pDrvIns);
726 }
727
728 if (pUsbIns->pUsbReg->pfnVMPowerOn)
729 {
730 LogFlow(("PDMR3PowerOn: Notifying - device '%s'/%d\n",
731 pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
732 pUsbIns->pUsbReg->pfnVMPowerOn(pUsbIns);
733 }
734 }
735#endif
736
737 /*
738 * Resume all threads.
739 */
740 pdmR3ThreadResumeAll(pVM);
741
742 LogFlow(("PDMR3PowerOn: returns void\n"));
743}
744
745
746
747
748/**
749 * This function will notify all the devices and their
750 * attached drivers about the VM now being reset.
751 *
752 * @param pVM VM Handle.
753 */
754PDMR3DECL(void) PDMR3Reset(PVM pVM)
755{
756 LogFlow(("PDMR3Reset:\n"));
757
758 /*
759 * Clear all pending interrupts and DMA operations.
760 */
761 VM_FF_CLEAR(pVM, VM_FF_INTERRUPT_APIC);
762 VM_FF_CLEAR(pVM, VM_FF_INTERRUPT_PIC);
763 VM_FF_CLEAR(pVM, VM_FF_PDM_DMA);
764
765 /*
766 * Iterate the device instances.
767 * The attached drivers are processed first.
768 */
769 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextHC)
770 {
771 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsHC; pLun; pLun = pLun->pNext)
772 /** @todo Inverse the order here? */
773 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
774 if (pDrvIns->pDrvReg->pfnReset)
775 {
776 LogFlow(("PDMR3Reset: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
777 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
778 pDrvIns->pDrvReg->pfnReset(pDrvIns);
779 }
780
781 if (pDevIns->pDevReg->pfnReset)
782 {
783 LogFlow(("PDMR3Reset: Notifying - device '%s'/%d\n",
784 pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
785 pDevIns->pDevReg->pfnReset(pDevIns);
786 }
787 }
788
789#ifdef VBOX_WITH_USB
790 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
791 {
792 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
793 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
794 if (pDrvIns->pDrvReg->pfnReset)
795 {
796 LogFlow(("PDMR3Reset: Notifying - driver '%s'/%d on LUN#%d of usb device '%s'/%d\n",
797 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
798 pDrvIns->pDrvReg->pfnReset(pDrvIns);
799 }
800
801 if (pUsbIns->pUsbReg->pfnVMReset)
802 {
803 LogFlow(("PDMR3Reset: Notifying - device '%s'/%d\n",
804 pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
805 pUsbIns->pUsbReg->pfnVMReset(pUsbIns);
806 }
807 }
808#endif
809
810 LogFlow(("PDMR3Reset: returns void\n"));
811}
812
813
814/**
815 * This function will notify all the devices and their
816 * attached drivers about the VM now being reset.
817 *
818 * @param pVM VM Handle.
819 */
820PDMR3DECL(void) PDMR3Suspend(PVM pVM)
821{
822 LogFlow(("PDMR3Suspend:\n"));
823
824 /*
825 * Iterate the device instances.
826 * The attached drivers are processed first.
827 */
828 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextHC)
829 {
830 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsHC; pLun; pLun = pLun->pNext)
831 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
832 if (pDrvIns->pDrvReg->pfnSuspend)
833 {
834 LogFlow(("PDMR3Suspend: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
835 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
836 pDrvIns->pDrvReg->pfnSuspend(pDrvIns);
837 }
838
839 if (pDevIns->pDevReg->pfnSuspend)
840 {
841 LogFlow(("PDMR3Suspend: Notifying - device '%s'/%d\n",
842 pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
843 pDevIns->pDevReg->pfnSuspend(pDevIns);
844 }
845 }
846
847#ifdef VBOX_WITH_USB
848 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
849 {
850 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
851 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
852 if (pDrvIns->pDrvReg->pfnSuspend)
853 {
854 LogFlow(("PDMR3Suspend: Notifying - driver '%s'/%d on LUN#%d of usb device '%s'/%d\n",
855 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
856 pDrvIns->pDrvReg->pfnSuspend(pDrvIns);
857 }
858
859 if (pUsbIns->pUsbReg->pfnVMSuspend)
860 {
861 LogFlow(("PDMR3Suspend: Notifying - device '%s'/%d\n",
862 pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
863 pUsbIns->pUsbReg->pfnVMSuspend(pUsbIns);
864 }
865 }
866#endif
867
868 /*
869 * Suspend all threads.
870 */
871 pdmR3ThreadSuspendAll(pVM);
872
873 LogFlow(("PDMR3Suspend: returns void\n"));
874}
875
876
877/**
878 * This function will notify all the devices and their
879 * attached drivers about the VM now being resumed.
880 *
881 * @param pVM VM Handle.
882 */
883PDMR3DECL(void) PDMR3Resume(PVM pVM)
884{
885 LogFlow(("PDMR3Resume:\n"));
886
887 /*
888 * Iterate the device instances.
889 * The attached drivers are processed first.
890 */
891 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextHC)
892 {
893 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsHC; pLun; pLun = pLun->pNext)
894 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
895 if (pDrvIns->pDrvReg->pfnResume)
896 {
897 LogFlow(("PDMR3Resume: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
898 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
899 pDrvIns->pDrvReg->pfnResume(pDrvIns);
900 }
901
902 if (pDevIns->pDevReg->pfnResume)
903 {
904 LogFlow(("PDMR3Resume: Notifying - device '%s'/%d\n",
905 pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
906 pDevIns->pDevReg->pfnResume(pDevIns);
907 }
908 }
909
910#ifdef VBOX_WITH_USB
911 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
912 {
913 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
914 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
915 if (pDrvIns->pDrvReg->pfnResume)
916 {
917 LogFlow(("PDMR3Resume: Notifying - driver '%s'/%d on LUN#%d of usb device '%s'/%d\n",
918 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
919 pDrvIns->pDrvReg->pfnResume(pDrvIns);
920 }
921
922 if (pUsbIns->pUsbReg->pfnVMResume)
923 {
924 LogFlow(("PDMR3Resume: Notifying - device '%s'/%d\n",
925 pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
926 pUsbIns->pUsbReg->pfnVMResume(pUsbIns);
927 }
928 }
929#endif
930
931 /*
932 * Resume all threads.
933 */
934 pdmR3ThreadResumeAll(pVM);
935
936 LogFlow(("PDMR3Resume: returns void\n"));
937}
938
939
940/**
941 * This function will notify all the devices and their
942 * attached drivers about the VM being powered off.
943 *
944 * @param pVM VM Handle.
945 */
946PDMR3DECL(void) PDMR3PowerOff(PVM pVM)
947{
948 LogFlow(("PDMR3PowerOff:\n"));
949
950 /*
951 * Iterate the device instances.
952 * The attached drivers are processed first.
953 */
954 for (PPDMDEVINS pDevIns = pVM->pdm.s.pDevInstances; pDevIns; pDevIns = pDevIns->Internal.s.pNextHC)
955 {
956 for (PPDMLUN pLun = pDevIns->Internal.s.pLunsHC; pLun; pLun = pLun->pNext)
957 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
958 if (pDrvIns->pDrvReg->pfnPowerOff)
959 {
960 LogFlow(("PDMR3PowerOff: Notifying - driver '%s'/%d on LUN#%d of device '%s'/%d\n",
961 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
962 pDrvIns->pDrvReg->pfnPowerOff(pDrvIns);
963 }
964
965 if (pDevIns->pDevReg->pfnPowerOff)
966 {
967 LogFlow(("PDMR3PowerOff: Notifying - device '%s'/%d\n",
968 pDevIns->pDevReg->szDeviceName, pDevIns->iInstance));
969 pDevIns->pDevReg->pfnPowerOff(pDevIns);
970 }
971 }
972
973#ifdef VBOX_WITH_USB
974 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
975 {
976 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
977 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
978 if (pDrvIns->pDrvReg->pfnPowerOff)
979 {
980 LogFlow(("PDMR3PowerOff: Notifying - driver '%s'/%d on LUN#%d of usb device '%s'/%d\n",
981 pDrvIns->pDrvReg->szDriverName, pDrvIns->iInstance, pLun->iLun, pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
982 pDrvIns->pDrvReg->pfnPowerOff(pDrvIns);
983 }
984
985 if (pUsbIns->pUsbReg->pfnVMPowerOff)
986 {
987 LogFlow(("PDMR3PowerOff: Notifying - device '%s'/%d\n",
988 pUsbIns->pUsbReg->szDeviceName, pUsbIns->iInstance));
989 pUsbIns->pUsbReg->pfnVMPowerOff(pUsbIns);
990 }
991 }
992#endif
993
994 /*
995 * Suspend all threads.
996 */
997 pdmR3ThreadSuspendAll(pVM);
998
999 LogFlow(("PDMR3PowerOff: returns void\n"));
1000}
1001
1002
1003/**
1004 * Queries the base interace of a device instance.
1005 *
1006 * The caller can use this to query other interfaces the device implements
1007 * and use them to talk to the device.
1008 *
1009 * @returns VBox status code.
1010 * @param pVM VM handle.
1011 * @param pszDevice Device name.
1012 * @param iInstance Device instance.
1013 * @param ppBase Where to store the pointer to the base device interface on success.
1014 * @remark We're not doing any locking ATM, so don't try call this at times when the
1015 * device chain is known to be updated.
1016 */
1017PDMR3DECL(int) PDMR3QueryDevice(PVM pVM, const char *pszDevice, unsigned iInstance, PPDMIBASE *ppBase)
1018{
1019 LogFlow(("PDMR3DeviceQuery: pszDevice=%p:{%s} iInstance=%u ppBase=%p\n", pszDevice, pszDevice, iInstance, ppBase));
1020
1021 /*
1022 * Iterate registered devices looking for the device.
1023 */
1024 RTUINT cchDevice = strlen(pszDevice);
1025 for (PPDMDEV pDev = pVM->pdm.s.pDevs; pDev; pDev = pDev->pNext)
1026 {
1027 if ( pDev->cchName == cchDevice
1028 && !memcmp(pDev->pDevReg->szDeviceName, pszDevice, cchDevice))
1029 {
1030 /*
1031 * Iterate device instances.
1032 */
1033 for (PPDMDEVINS pDevIns = pDev->pInstances; pDevIns; pDevIns = pDevIns->Internal.s.pPerDeviceNextHC)
1034 {
1035 if (pDevIns->iInstance == iInstance)
1036 {
1037 if (pDevIns->IBase.pfnQueryInterface)
1038 {
1039 *ppBase = &pDevIns->IBase;
1040 LogFlow(("PDMR3DeviceQuery: return VINF_SUCCESS and *ppBase=%p\n", *ppBase));
1041 return VINF_SUCCESS;
1042 }
1043
1044 LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NO_IBASE\n"));
1045 return VERR_PDM_DEVICE_INSTANCE_NO_IBASE;
1046 }
1047 }
1048
1049 LogFlow(("PDMR3DeviceQuery: returns VERR_PDM_DEVICE_INSTANCE_NOT_FOUND\n"));
1050 return VERR_PDM_DEVICE_INSTANCE_NOT_FOUND;
1051 }
1052 }
1053
1054 LogFlow(("PDMR3QueryDevice: returns VERR_PDM_DEVICE_NOT_FOUND\n"));
1055 return VERR_PDM_DEVICE_NOT_FOUND;
1056}
1057
1058
1059/**
1060 * Queries the base interface of a device LUN.
1061 *
1062 * This differs from PDMR3QueryLun by that it returns the interface on the
1063 * device and not the top level driver.
1064 *
1065 * @returns VBox status code.
1066 * @param pVM VM Handle.
1067 * @param pszDevice Device name.
1068 * @param iInstance Device instance.
1069 * @param iLun The Logical Unit to obtain the interface of.
1070 * @param ppBase Where to store the base interface pointer.
1071 * @remark We're not doing any locking ATM, so don't try call this at times when the
1072 * device chain is known to be updated.
1073 */
1074PDMR3DECL(int) PDMR3QueryDeviceLun(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
1075{
1076 LogFlow(("PDMR3QueryLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
1077 pszDevice, pszDevice, iInstance, iLun, ppBase));
1078
1079 /*
1080 * Find the LUN.
1081 */
1082 PPDMLUN pLun;
1083 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
1084 if (VBOX_SUCCESS(rc))
1085 {
1086 *ppBase = pLun->pBase;
1087 LogFlow(("PDMR3QueryDeviceLun: return VINF_SUCCESS and *ppBase=%p\n", *ppBase));
1088 return VINF_SUCCESS;
1089 }
1090 LogFlow(("PDMR3QueryDeviceLun: returns %Vrc\n", rc));
1091 return rc;
1092}
1093
1094
1095/**
1096 * Query the interface of the top level driver on a LUN.
1097 *
1098 * @returns VBox status code.
1099 * @param pVM VM Handle.
1100 * @param pszDevice Device name.
1101 * @param iInstance Device instance.
1102 * @param iLun The Logical Unit to obtain the interface of.
1103 * @param ppBase Where to store the base interface pointer.
1104 * @remark We're not doing any locking ATM, so don't try call this at times when the
1105 * device chain is known to be updated.
1106 */
1107PDMR3DECL(int) PDMR3QueryLun(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
1108{
1109 LogFlow(("PDMR3QueryLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
1110 pszDevice, pszDevice, iInstance, iLun, ppBase));
1111
1112 /*
1113 * Find the LUN.
1114 */
1115 PPDMLUN pLun;
1116 int rc = pdmR3DevFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
1117 if (VBOX_SUCCESS(rc))
1118 {
1119 if (pLun->pTop)
1120 {
1121 *ppBase = &pLun->pTop->IBase;
1122 LogFlow(("PDMR3QueryLun: return %Vrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
1123 return VINF_SUCCESS;
1124 }
1125 rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
1126 }
1127 LogFlow(("PDMR3QueryLun: returns %Vrc\n", rc));
1128 return rc;
1129}
1130
1131/**
1132 * Executes pending DMA transfers.
1133 * Forced Action handler.
1134 *
1135 * @param pVM VM handle.
1136 */
1137PDMR3DECL(void) PDMR3DmaRun(PVM pVM)
1138{
1139 VM_FF_CLEAR(pVM, VM_FF_PDM_DMA);
1140 if (pVM->pdm.s.pDmac)
1141 {
1142 bool fMore = pVM->pdm.s.pDmac->Reg.pfnRun(pVM->pdm.s.pDmac->pDevIns);
1143 if (fMore)
1144 VM_FF_SET(pVM, VM_FF_PDM_DMA);
1145 }
1146}
1147
1148
1149/**
1150 * Call polling function.
1151 *
1152 * @param pVM VM handle.
1153 */
1154PDMR3DECL(void) PDMR3Poll(PVM pVM)
1155{
1156 /* This is temporary hack and shall be removed ASAP! */
1157 unsigned iPoller = pVM->pdm.s.cPollers;
1158 if (iPoller)
1159 {
1160 while (iPoller-- > 0)
1161 pVM->pdm.s.apfnPollers[iPoller](pVM->pdm.s.aDrvInsPollers[iPoller]);
1162 TMTimerSetMillies(pVM->pdm.s.pTimerPollers, 3);
1163 }
1164}
1165
1166
1167/**
1168 * Internal timer callback function.
1169 *
1170 * @param pVM The VM.
1171 * @param pTimer The timer handle.
1172 * @param pvUser User argument specified upon timer creation.
1173 */
1174static DECLCALLBACK(void) pdmR3PollerTimer(PVM pVM, PTMTIMER pTimer, void *pvUser)
1175{
1176 PDMR3Poll(pVM);
1177}
1178
1179
1180/**
1181 * Service a VMMCALLHOST_PDM_LOCK call.
1182 *
1183 * @returns VBox status code.
1184 * @param pVM The VM handle.
1185 */
1186PDMR3DECL(int) PDMR3LockCall(PVM pVM)
1187{
1188 return PDMR3CritSectEnterEx(&pVM->pdm.s.CritSect, true /* fHostCall */);
1189}
1190
1191
1192/**
1193 * Registers the VMM device heap
1194 *
1195 * @returns VBox status code.
1196 * @param pVM VM handle.
1197 * @param GCPhys The physical address.
1198 * @param pvHeap Ring-3 pointer.
1199 * @param cbSize Size of the heap.
1200 */
1201PDMR3DECL(int) PDMR3RegisterVMMDevHeap(PVM pVM, RTGCPHYS GCPhys, RTR3PTR pvHeap, unsigned cbSize)
1202{
1203 Assert(pVM->pdm.s.pvVMMDevHeap == NULL);
1204
1205 Log(("PDMR3RegisterVMMDevHeap %VGp %VHv %x\n", GCPhys, pvHeap, cbSize));
1206 pVM->pdm.s.pvVMMDevHeap = pvHeap;
1207 pVM->pdm.s.GCPhysVMMDevHeap = GCPhys;
1208 pVM->pdm.s.cbVMMDevHeap = cbSize;
1209 pVM->pdm.s.cbVMMDevHeapLeft = cbSize;
1210 return VINF_SUCCESS;
1211}
1212
1213
1214/**
1215 * Unregisters the VMM device heap
1216 *
1217 * @returns VBox status code.
1218 * @param pVM VM handle.
1219 * @param GCPhys The physical address.
1220 */
1221PDMR3DECL(int) PDMR3UnregisterVMMDevHeap(PVM pVM, RTGCPHYS GCPhys)
1222{
1223 Assert(pVM->pdm.s.GCPhysVMMDevHeap == GCPhys);
1224
1225 Log(("PDMR3UnregisterVMMDevHeap %VGp\n", GCPhys));
1226 pVM->pdm.s.pvVMMDevHeap = NULL;
1227 pVM->pdm.s.GCPhysVMMDevHeap = NIL_RTGCPHYS;
1228 pVM->pdm.s.cbVMMDevHeap = 0;
1229 pVM->pdm.s.cbVMMDevHeapLeft = 0;
1230 return VINF_SUCCESS;
1231}
1232
1233
1234/**
1235 * Allocates memory from the VMM device heap
1236 *
1237 * @returns VBox status code.
1238 * @param pVM VM handle.
1239 * @param cbSize Allocation size.
1240 * @param pv Ring-3 pointer. (out)
1241 */
1242PDMR3DECL(int) PDMR3VMMDevHeapAlloc(PVM pVM, unsigned cbSize, RTR3PTR *ppv)
1243{
1244 AssertReturn(cbSize && cbSize <= pVM->pdm.s.cbVMMDevHeapLeft, VERR_NO_MEMORY);
1245
1246 Log(("PDMR3VMMDevHeapAlloc %x\n", cbSize));
1247
1248 /** @todo not a real heap as there's currently only one user. */
1249 *ppv = pVM->pdm.s.pvVMMDevHeap;
1250 pVM->pdm.s.cbVMMDevHeapLeft = 0;
1251 return VINF_SUCCESS;
1252}
1253
1254
1255/**
1256 * Frees memory from the VMM device heap
1257 *
1258 * @returns VBox status code.
1259 * @param pVM VM handle.
1260 * @param pv Ring-3 pointer.
1261 */
1262PDMR3DECL(int) PDMR3VMMDevHeapFree(PVM pVM, RTR3PTR pv)
1263{
1264 Log(("PDMR3VMMDevHeapFree %VHv\n", pv));
1265
1266 /** @todo not a real heap as there's currently only one user. */
1267 pVM->pdm.s.cbVMMDevHeapLeft = pVM->pdm.s.cbVMMDevHeap;
1268 return VINF_SUCCESS;
1269}
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