VirtualBox

source: vbox/trunk/src/VBox/Devices/Samples/DevPlayground.cpp@ 76553

Last change on this file since 76553 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.1 KB
Line 
1/* $Id: DevPlayground.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * DevPlayground - Device for making PDM/PCI/... experiments.
4 *
5 * This device uses big PCI BAR64 resources, which needs the ICH9 chipset.
6 * The device works without any PCI config (because the default setup with the
7 * ICH9 chipset doesn't have anything at bus=0, device=0, function=0.
8 *
9 * To enable this device for a particular VM:
10 * VBoxManage setextradata vmname VBoxInternal/PDM/Devices/playground/Path .../obj/VBoxPlaygroundDevice/VBoxPlaygroundDevice
11 * VBoxManage setextradata vmname VBoxInternal/Devices/playground/0/Config/Whatever1 0
12 */
13
14/*
15 * Copyright (C) 2009-2019 Oracle Corporation
16 *
17 * This file is part of VirtualBox Open Source Edition (OSE), as
18 * available from http://www.virtualbox.org. This file is free software;
19 * you can redistribute it and/or modify it under the terms of the GNU
20 * General Public License (GPL) as published by the Free Software
21 * Foundation, in version 2 as it comes in the "COPYING" file of the
22 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
23 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
24 */
25
26
27/*********************************************************************************************************************************
28* Header Files *
29*********************************************************************************************************************************/
30#define LOG_GROUP LOG_GROUP_MISC
31#include <VBox/vmm/pdmdev.h>
32#include <VBox/version.h>
33#include <VBox/err.h>
34#include <VBox/log.h>
35
36#include <VBox/com/assert.h>
37#include <VBox/com/defs.h>
38#include <VBox/com/string.h>
39#include <VBox/com/Guid.h>
40#include <VBox/com/VirtualBox.h>
41
42#include <iprt/assert.h>
43
44
45/*********************************************************************************************************************************
46* Structures and Typedefs *
47*********************************************************************************************************************************/
48/**
49 * Playground device per function (sub-device) data.
50 */
51typedef struct VBOXPLAYGROUNDDEVICEFUNCTION
52{
53 /** The PCI devices. */
54 PDMPCIDEV PciDev;
55 /** The function number. */
56 uint8_t iFun;
57 /** Device function name. */
58 char szName[31];
59} VBOXPLAYGROUNDDEVICEFUNCTION;
60/** Pointer to a PCI function of the playground device. */
61typedef VBOXPLAYGROUNDDEVICEFUNCTION *PVBOXPLAYGROUNDDEVICEFUNCTION;
62
63/**
64 * Playground device instance data.
65 */
66typedef struct VBOXPLAYGROUNDDEVICE
67{
68 /** PCI device functions. */
69 VBOXPLAYGROUNDDEVICEFUNCTION aPciFuns[8];
70} VBOXPLAYGROUNDDEVICE;
71/** Pointer to the instance data of a playground device instance. */
72typedef VBOXPLAYGROUNDDEVICE *PVBOXPLAYGROUNDDEVICE;
73
74
75#define PLAYGROUND_SSM_VERSION 3
76
77
78/*********************************************************************************************************************************
79* Device Functions *
80*********************************************************************************************************************************/
81
82PDMBOTHCBDECL(int) devPlaygroundMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
83{
84 NOREF(pDevIns);
85 NOREF(pvUser);
86 NOREF(GCPhysAddr);
87 NOREF(pv);
88 NOREF(cb);
89 return VINF_SUCCESS;
90}
91
92
93PDMBOTHCBDECL(int) devPlaygroundMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void const *pv, unsigned cb)
94{
95 NOREF(pDevIns);
96 NOREF(pvUser);
97 NOREF(GCPhysAddr);
98 NOREF(pv);
99 NOREF(cb);
100 return VINF_SUCCESS;
101}
102
103
104/**
105 * @callback_method_impl{FNPCIIOREGIONMAP}
106 */
107static DECLCALLBACK(int) devPlaygroundMap(PPDMDEVINS pDevIns, PPDMPCIDEV pPciDev, uint32_t iRegion,
108 RTGCPHYS GCPhysAddress, RTGCPHYS cb, PCIADDRESSSPACE enmType)
109{
110 RT_NOREF(pPciDev, enmType, cb);
111
112 switch (iRegion)
113 {
114 case 0:
115 case 2:
116 Assert( enmType == (PCIADDRESSSPACE)(PCI_ADDRESS_SPACE_MEM | PCI_ADDRESS_SPACE_BAR64)
117 || enmType == (PCIADDRESSSPACE)(PCI_ADDRESS_SPACE_MEM_PREFETCH | PCI_ADDRESS_SPACE_BAR64));
118 if (GCPhysAddress == NIL_RTGCPHYS)
119 return VINF_SUCCESS; /* We ignore the unmap notification. */
120 return PDMDevHlpMMIOExMap(pDevIns, pPciDev, iRegion, GCPhysAddress);
121
122 default:
123 /* We should never get here */
124 AssertMsgFailedReturn(("Invalid PCI region param in map callback"), VERR_INTERNAL_ERROR);
125 }
126}
127
128
129/**
130 * @callback_method_impl{FNSSMDEVSAVEEXEC}
131 */
132static DECLCALLBACK(int) devPlaygroundSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
133{
134 PVBOXPLAYGROUNDDEVICE pThis = PDMINS_2_DATA(pDevIns, PVBOXPLAYGROUNDDEVICE);
135
136 /* dummy (real devices would need to save their state here) */
137 RT_NOREF(pThis);
138
139 /* Demo of some API stuff - very unusual, think twice if there's no better
140 * solution which doesn't need API interaction. */
141 HRESULT hrc = S_OK;
142 com::Bstr bstrSnapName;
143 com::Guid uuid(COM_IIDOF(ISnapshot));
144 ISnapshot *pSnap = (ISnapshot *)PDMDevHlpQueryGenericUserObject(pDevIns, uuid.raw());
145 if (pSnap)
146 {
147 hrc = pSnap->COMGETTER(Name)(bstrSnapName.asOutParam());
148 AssertComRCReturn(hrc, VERR_INVALID_STATE);
149 }
150 com::Utf8Str strSnapName(bstrSnapName);
151 SSMR3PutStrZ(pSSM, strSnapName.c_str());
152 LogRel(("Playground: saving state of snapshot '%s', hrc=%Rhrc\n", strSnapName.c_str(), hrc));
153
154 return VINF_SUCCESS;
155}
156
157/**
158 * @callback_method_impl{FNSSMDEVLOADEXEC}
159 */
160static DECLCALLBACK(int) devPlaygroundLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
161{
162 PVBOXPLAYGROUNDDEVICE pThis = PDMINS_2_DATA(pDevIns, PVBOXPLAYGROUNDDEVICE);
163
164 if (uVersion > PLAYGROUND_SSM_VERSION)
165 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
166 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
167
168 /* dummy (real devices would need to load their state here) */
169 RT_NOREF(pThis);
170
171 /* Reading the stuff written to saved state, just a demo. */
172 char szSnapName[256];
173 int rc = SSMR3GetStrZ(pSSM, szSnapName, sizeof(szSnapName));
174 AssertRCReturn(rc, rc);
175 LogRel(("Playground: loading state of snapshot '%s'\n", szSnapName));
176
177 return VINF_SUCCESS;
178}
179
180
181/**
182 * @interface_method_impl{PDMDEVREG,pfnConstruct}
183 */
184static DECLCALLBACK(int) devPlaygroundConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
185{
186 RT_NOREF(iInstance, pCfg);
187 int rc = VINF_SUCCESS;
188
189 /*
190 * Check that the device instance and device helper structures are compatible.
191 */
192 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
193
194 /*
195 * Initialize the instance data so that the destructor won't mess up.
196 */
197 PVBOXPLAYGROUNDDEVICE pThis = PDMINS_2_DATA(pDevIns, PVBOXPLAYGROUNDDEVICE);
198
199 /*
200 * Validate and read the configuration.
201 */
202 PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns, "Whatever1|Whatever2", "");
203
204 /*
205 * PCI device setup.
206 */
207 uint32_t iPciDevNo = PDMPCIDEVREG_DEV_NO_FIRST_UNUSED;
208 for (uint32_t iPciFun = 0; iPciFun < RT_ELEMENTS(pThis->aPciFuns); iPciFun++)
209 {
210 PVBOXPLAYGROUNDDEVICEFUNCTION pFun = &pThis->aPciFuns[iPciFun];
211 RTStrPrintf(pFun->szName, sizeof(pThis->aPciFuns[iPciFun].PciDev), "playground%u", iPciFun);
212 pFun->iFun = iPciFun;
213
214 PCIDevSetVendorId( &pFun->PciDev, 0x80ee);
215 PCIDevSetDeviceId( &pFun->PciDev, 0xde4e);
216 PCIDevSetClassBase(&pFun->PciDev, 0x07); /* communications device */
217 PCIDevSetClassSub( &pFun->PciDev, 0x80); /* other communications device */
218 if (iPciFun == 0) /* only for the primary function */
219 PCIDevSetHeaderType(&pFun->PciDev, 0x80); /* normal, multifunction device */
220
221 rc = PDMDevHlpPCIRegisterEx(pDevIns, &pFun->PciDev, iPciFun, 0 /*fFlags*/, iPciDevNo, iPciFun,
222 pThis->aPciFuns[iPciFun].szName);
223 AssertLogRelRCReturn(rc, rc);
224
225 /* First region. */
226 RTGCPHYS const cbFirst = iPciFun == 0 ? 8*_1M : iPciFun * _4K;
227 rc = PDMDevHlpPCIIORegionRegisterEx(pDevIns, &pFun->PciDev, 0, cbFirst,
228 (PCIADDRESSSPACE)( PCI_ADDRESS_SPACE_MEM | PCI_ADDRESS_SPACE_BAR64
229 | (iPciFun == 0 ? PCI_ADDRESS_SPACE_MEM_PREFETCH : 0)),
230 devPlaygroundMap);
231 AssertLogRelRCReturn(rc, rc);
232 char *pszRegionName = NULL;
233 RTStrAPrintf(&pszRegionName, "PG-F%d-BAR0", iPciFun);
234 Assert(pszRegionName);
235 rc = PDMDevHlpMMIOExPreRegister(pDevIns, &pFun->PciDev, 0, cbFirst,
236 IOMMMIO_FLAGS_READ_PASSTHRU | IOMMMIO_FLAGS_WRITE_PASSTHRU, pszRegionName,
237 NULL /*pvUser*/, devPlaygroundMMIOWrite, devPlaygroundMMIORead, NULL /*pfnFill*/,
238 NIL_RTR0PTR /*pvUserR0*/, NULL /*pszWriteR0*/, NULL /*pszReadR0*/, NULL /*pszFillR0*/,
239 NIL_RTRCPTR /*pvUserRC*/, NULL /*pszWriteRC*/, NULL /*pszReadRC*/, NULL /*pszFillRC*/);
240 AssertLogRelRCReturn(rc, rc);
241
242 /* Second region. */
243 RTGCPHYS const cbSecond = iPciFun == 0 ? 32*_1M : iPciFun * _32K;
244 rc = PDMDevHlpPCIIORegionRegisterEx(pDevIns, &pFun->PciDev, 2, cbSecond,
245 (PCIADDRESSSPACE)( PCI_ADDRESS_SPACE_MEM | PCI_ADDRESS_SPACE_BAR64
246 | (iPciFun == 0 ? PCI_ADDRESS_SPACE_MEM_PREFETCH : 0)),
247 devPlaygroundMap);
248 AssertLogRelRCReturn(rc, rc);
249 pszRegionName = NULL;
250 RTStrAPrintf(&pszRegionName, "PG-F%d-BAR2", iPciFun);
251 Assert(pszRegionName);
252 rc = PDMDevHlpMMIOExPreRegister(pDevIns, &pFun->PciDev, 2, cbSecond,
253 IOMMMIO_FLAGS_READ_PASSTHRU | IOMMMIO_FLAGS_WRITE_PASSTHRU, pszRegionName,
254 NULL /*pvUser*/, devPlaygroundMMIOWrite, devPlaygroundMMIORead, NULL /*pfnFill*/,
255 NIL_RTR0PTR /*pvUserR0*/, NULL /*pszWriteR0*/, NULL /*pszReadR0*/, NULL /*pszFillR0*/,
256 NIL_RTRCPTR /*pvUserRC*/, NULL /*pszWriteRC*/, NULL /*pszReadRC*/, NULL /*pszFillRC*/);
257 AssertLogRelRCReturn(rc, rc);
258
259 /* Subsequent function should use the same major as the previous one. */
260 iPciDevNo = PDMPCIDEVREG_DEV_NO_SAME_AS_PREV;
261 }
262
263 /*
264 * Save state handling.
265 */
266 rc = PDMDevHlpSSMRegister(pDevIns, PLAYGROUND_SSM_VERSION, sizeof(*pThis), devPlaygroundSaveExec, devPlaygroundLoadExec);
267 if (RT_FAILURE(rc))
268 return rc;
269
270 return VINF_SUCCESS;
271}
272
273
274/**
275 * @interface_method_impl{PDMDEVREG,pfnDestruct}
276 */
277static DECLCALLBACK(int) devPlaygroundDestruct(PPDMDEVINS pDevIns)
278{
279 /*
280 * Check the versions here as well since the destructor is *always* called.
281 */
282 PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);
283
284 return VINF_SUCCESS;
285}
286
287
288/**
289 * The device registration structure.
290 */
291static const PDMDEVREG g_DevicePlayground =
292{
293 /* u32Version */
294 PDM_DEVREG_VERSION,
295 /* szName */
296 "playground",
297 /* szRCMod */
298 "",
299 /* szR0Mod */
300 "",
301 /* pszDescription */
302 "VBox Playground Device.",
303 /* fFlags */
304 PDM_DEVREG_FLAGS_DEFAULT_BITS,
305 /* fClass */
306 PDM_DEVREG_CLASS_MISC,
307 /* cMaxInstances */
308 1,
309 /* cbInstance */
310 sizeof(VBOXPLAYGROUNDDEVICE),
311 /* pfnConstruct */
312 devPlaygroundConstruct,
313 /* pfnDestruct */
314 devPlaygroundDestruct,
315 /* pfnRelocate */
316 NULL,
317 /* pfnMemSetup */
318 NULL,
319 /* pfnPowerOn */
320 NULL,
321 /* pfnReset */
322 NULL,
323 /* pfnSuspend */
324 NULL,
325 /* pfnResume */
326 NULL,
327 /* pfnAttach */
328 NULL,
329 /* pfnDetach */
330 NULL,
331 /* pfnQueryInterface */
332 NULL,
333 /* pfnInitComplete */
334 NULL,
335 /* pfnPowerOff */
336 NULL,
337 /* pfnSoftReset */
338 NULL,
339 /* u32VersionEnd */
340 PDM_DEVREG_VERSION
341};
342
343
344/**
345 * Register devices provided by the plugin.
346 *
347 * @returns VBox status code.
348 * @param pCallbacks Pointer to the callback table.
349 * @param u32Version VBox version number.
350 */
351extern "C" DECLEXPORT(int) VBoxDevicesRegister(PPDMDEVREGCB pCallbacks, uint32_t u32Version)
352{
353 LogFlow(("VBoxPlaygroundDevice::VBoxDevicesRegister: u32Version=%#x pCallbacks->u32Version=%#x\n", u32Version, pCallbacks->u32Version));
354
355 AssertLogRelMsgReturn(u32Version >= VBOX_VERSION,
356 ("VirtualBox version %#x, expected %#x or higher\n", u32Version, VBOX_VERSION),
357 VERR_VERSION_MISMATCH);
358 AssertLogRelMsgReturn(pCallbacks->u32Version == PDM_DEVREG_CB_VERSION,
359 ("callback version %#x, expected %#x\n", pCallbacks->u32Version, PDM_DEVREG_CB_VERSION),
360 VERR_VERSION_MISMATCH);
361
362 return pCallbacks->pfnRegister(pCallbacks, &g_DevicePlayground);
363}
364
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