VirtualBox

source: vbox/trunk/src/VBox/Devices/testcase/tstDeviceR0.cpp@ 92404

Last change on this file since 92404 was 92124, checked in by vboxsync, 3 years ago

Devices/testcase/tstDevice: Fuzz ring-0 device handlers as well, bugref:9006

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.8 KB
Line 
1/* $Id: tstDeviceR0.cpp 92124 2021-10-28 07:32:42Z vboxsync $ */
2/** @file
3 * tstDevice - Test framework for PDM devices/drivers
4 */
5
6/*
7 * Copyright (C) 2017-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_DEFAULT /** @todo */
23#undef IN_RING3
24#undef IN_SUP_R3
25//#undef CTX_SUFF
26//#undef R0PTRTYPE
27//#undef R3PTRTYPE
28#define IN_RING0
29#define IN_SUP_R0
30#define LINUX_VERSION_CODE 0
31#define KERNEL_VERSION(a,b,c) 1
32//#define CTX_SUFF(a_Name) a_Name##R0
33//#define R3PTRTYPE(a_R3Type) RTHCUINTPTR
34//#define R0PTRTYPE(a_R0Type) a_R0Type
35#include <VBox/types.h>
36
37#include <VBox/sup.h>
38#include <VBox/version.h>
39#include <iprt/assert.h>
40#include <iprt/getopt.h>
41#include <iprt/log.h>
42#include <iprt/list.h>
43#include <iprt/mem.h>
44
45#include "tstDeviceInternal.h"
46#include "tstDeviceCfg.h"
47#include "tstDeviceBuiltin.h"
48
49
50/*********************************************************************************************************************************
51* Defined Constants And Macros *
52*********************************************************************************************************************************/
53
54
55/*********************************************************************************************************************************
56* Structures and Typedefs *
57*********************************************************************************************************************************/
58
59
60/*********************************************************************************************************************************
61* Global Variables *
62*********************************************************************************************************************************/
63
64
65/*********************************************************************************************************************************
66* Internal Functions *
67*********************************************************************************************************************************/
68
69/**
70 * Create a new PDM device with default config.
71 *
72 * @returns VBox status code.
73 * @param pszName Name of the device to create.
74 * @param fRCEnabled Flag whether RC support should be enabled for this device.
75 * @param pDut The device under test structure the created PDM device instance is exercised under.
76 */
77DECLHIDDEN(int) tstDevPdmDevR0R3Create(const char *pszName, bool fRCEnabled, PTSTDEVDUTINT pDut)
78{
79 int rc = VINF_SUCCESS;
80 PCPDMDEVREGR0 pPdmDevR0 = NULL;
81 PCTSTDEVPDMDEV pPdmDev = tstDevPdmDeviceFind(pszName, &pPdmDevR0);
82 if (RT_LIKELY(pPdmDev))
83 {
84 uint32_t const cbRing0 = RT_ALIGN_32(RT_UOFFSETOF(PDMDEVINSR0, achInstanceData) + pPdmDevR0->cbInstanceCC, PAGE_SIZE);
85 uint32_t const cbRing3 = RT_ALIGN_32(RT_UOFFSETOF(PDMDEVINSR3, achInstanceData) + pPdmDev->pReg->cbInstanceCC,
86 fRCEnabled ? PAGE_SIZE : 64);
87 uint32_t const cbRC = fRCEnabled ? 0
88 : RT_ALIGN_32(RT_UOFFSETOF(PDMDEVINSRC, achInstanceData) + pPdmDevR0->cbInstanceRC, 64);
89 uint32_t const cbShared = RT_ALIGN_32(pPdmDev->pReg->cbInstanceShared, 64);
90 uint32_t const cbCritSect = RT_ALIGN_32(sizeof(PDMCRITSECT), 64);
91 uint32_t const cbMsixState = RT_ALIGN_32(pPdmDev->pReg->cMaxMsixVectors * 16 + (pPdmDev->pReg->cMaxMsixVectors + 7) / 8, _4K);
92 uint32_t const cbPciDev = RT_ALIGN_32(RT_UOFFSETOF_DYN(PDMPCIDEV, abMsixState[cbMsixState]), 64);
93 uint32_t const cPciDevs = RT_MIN(pPdmDev->pReg->cMaxPciDevices, 8);
94 uint32_t const cbPciDevs = cbPciDev * cPciDevs;
95 uint32_t const cbTotal = RT_ALIGN_32(cbRing0 + cbRing3 + cbRC + cbShared + cbCritSect + cbPciDevs, PAGE_SIZE);
96 AssertLogRelMsgReturn(cbTotal <= PDM_MAX_DEVICE_INSTANCE_SIZE,
97 ("Instance of '%s' is too big: cbTotal=%u, max %u\n",
98 pPdmDev->pReg->szName, cbTotal, PDM_MAX_DEVICE_INSTANCE_SIZE),
99 VERR_OUT_OF_RANGE);
100
101 PPDMDEVINSR0 pDevInsR0 = (PPDMDEVINSR0)RTMemAllocZ(cbTotal);
102 PDMDEVINSR3 *pDevInsR3 = (PDMDEVINSR3 *)(((uint8_t *)pDevInsR0 + cbRing0));
103
104 pDevInsR0->u32Version = PDM_DEVINSR0_VERSION;
105 pDevInsR0->iInstance = 0;
106 pDevInsR0->pHlpR0 = &g_tstDevPdmDevHlpR0;
107 pDevInsR0->Internal.s.pDut = pDut;
108 pDevInsR0->pvInstanceDataR0 = (uint8_t *)pDevInsR0 + cbRing0 + cbRing3 + cbRC;
109 pDevInsR0->pvInstanceDataForR0 = &pDevInsR0->achInstanceData[0];
110 pDevInsR0->pCritSectRoR0 = (PPDMCRITSECT)((uint8_t *)pDevInsR0->pvInstanceDataR0 + cbShared);
111 pDevInsR0->pReg = pPdmDevR0;
112 pDevInsR0->pDevInsForR3 = (RTHCUINTPTR)pDevInsR3;
113 pDevInsR0->pDevInsForR3R0 = pDevInsR3;
114 pDevInsR0->pvInstanceDataForR3R0 = &pDevInsR3->achInstanceData[0];
115 pDevInsR0->cbPciDev = cbPciDev;
116 pDevInsR0->cPciDevs = cPciDevs;
117 for (uint32_t iPciDev = 0; iPciDev < cPciDevs; iPciDev++)
118 {
119 /* Note! PDMDevice.cpp has a copy of this code. Keep in sync. */
120 PPDMPCIDEV pPciDev = (PPDMPCIDEV)((uint8_t *)pDevInsR0->pCritSectRoR0 + cbCritSect + cbPciDev * iPciDev);
121 if (iPciDev < RT_ELEMENTS(pDevInsR0->apPciDevs))
122 pDevInsR0->apPciDevs[iPciDev] = pPciDev;
123 pPciDev->cbConfig = _4K;
124 pPciDev->cbMsixState = cbMsixState;
125 pPciDev->idxSubDev = (uint16_t)iPciDev;
126 //pPciDev->Int.s.idxSubDev = (uint16_t)iPciDev;
127 pPciDev->u32Magic = PDMPCIDEV_MAGIC;
128 }
129
130 pDevInsR3->u32Version = PDM_DEVINSR3_VERSION;
131 pDevInsR3->iInstance = 0;
132 pDevInsR3->cbRing3 = cbRing3;
133 pDevInsR3->fR0Enabled = true;
134 pDevInsR3->fRCEnabled = fRCEnabled;
135 pDevInsR3->pvInstanceDataR3 = pDevInsR0->pDevInsForR3 + cbRing3 + cbRC;
136 pDevInsR3->pvInstanceDataForR3 = pDevInsR0->pDevInsForR3 + RT_UOFFSETOF(PDMDEVINSR3, achInstanceData);
137 pDevInsR3->pCritSectRoR3 = pDevInsR0->pDevInsForR3 + cbRing3 + cbRC + cbShared;
138 pDevInsR3->pDevInsR0RemoveMe = pDevInsR0;
139 pDevInsR3->pvInstanceDataR0 = pDevInsR0->pvInstanceDataR0;
140 pDevInsR3->pvInstanceDataRC = fRCEnabled ? NIL_RTRGPTR : pDevInsR0->pDevInsForRC + RT_UOFFSETOF(PDMDEVINSRC, achInstanceData);
141 pDevInsR3->pDevInsForRC = pDevInsR0->pDevInsForRC;
142 pDevInsR3->pDevInsForRCR3 = pDevInsR0->pDevInsForR3 + cbRing3;
143 pDevInsR3->pDevInsForRCR3 = pDevInsR3->pDevInsForRCR3 + RT_UOFFSETOF(PDMDEVINSRC, achInstanceData);
144 pDevInsR3->cbPciDev = cbPciDev;
145 pDevInsR3->cPciDevs = cPciDevs;
146 for (uint32_t i = 0; i < RT_MIN(cPciDevs, RT_ELEMENTS(pDevInsR3->apPciDevs)); i++)
147 pDevInsR3->apPciDevs[i] = pDevInsR3->pCritSectRoR3 + cbCritSect + cbPciDev * i;
148 RTCritSectInit(&pDevInsR0->pCritSectRoR0->s.CritSect);
149 pDut->pDevIns = pDevInsR3;
150 pDut->pDevInsR0 = pDevInsR0;
151
152 rc = tstDevPdmDeviceR3Construct(pDut);
153 if (RT_SUCCESS(rc))
154 {
155 rc = pPdmDevR0->pfnConstruct(pDevInsR0);
156 if (RT_SUCCESS(rc))
157 return VINF_SUCCESS;
158 }
159
160 if (RT_FAILURE(rc))
161 {
162 //rc = pPdmDev->pReg->pfnDestruct(pDevIns);
163 RTMemFree(pDevInsR0);
164 }
165 }
166 else
167 rc = VERR_NOT_FOUND;
168
169 return rc;
170}
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