1 | /* $Id: tstDeviceR0.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * tstDevice - Test framework for PDM devices/drivers
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2017-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #define LOG_GROUP LOG_GROUP_DEFAULT /** @todo */
|
---|
33 | #undef IN_RING3
|
---|
34 | #undef IN_SUP_R3
|
---|
35 | //#undef CTX_SUFF
|
---|
36 | //#undef R0PTRTYPE
|
---|
37 | //#undef R3PTRTYPE
|
---|
38 | #define IN_RING0
|
---|
39 | #define IN_SUP_R0
|
---|
40 | #define LINUX_VERSION_CODE 0
|
---|
41 | #define KERNEL_VERSION(a,b,c) 1
|
---|
42 | //#define CTX_SUFF(a_Name) a_Name##R0
|
---|
43 | //#define R3PTRTYPE(a_R3Type) RTHCUINTPTR
|
---|
44 | //#define R0PTRTYPE(a_R0Type) a_R0Type
|
---|
45 | #include <VBox/types.h>
|
---|
46 |
|
---|
47 | #include <VBox/sup.h>
|
---|
48 | #include <VBox/version.h>
|
---|
49 | #include <iprt/assert.h>
|
---|
50 | #include <iprt/getopt.h>
|
---|
51 | #include <iprt/log.h>
|
---|
52 | #include <iprt/list.h>
|
---|
53 | #include <iprt/mem.h>
|
---|
54 |
|
---|
55 | #include "tstDeviceInternal.h"
|
---|
56 | #include "tstDeviceCfg.h"
|
---|
57 | #include "tstDeviceBuiltin.h"
|
---|
58 |
|
---|
59 |
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Create a new PDM device with default config.
|
---|
63 | *
|
---|
64 | * @returns VBox status code.
|
---|
65 | * @param pszName Name of the device to create.
|
---|
66 | * @param fRCEnabled Flag whether RC support should be enabled for this device.
|
---|
67 | * @param pDut The device under test structure the created PDM device instance is exercised under.
|
---|
68 | */
|
---|
69 | DECLHIDDEN(int) tstDevPdmDevR0R3Create(const char *pszName, bool fRCEnabled, PTSTDEVDUTINT pDut)
|
---|
70 | {
|
---|
71 | int rc = VINF_SUCCESS;
|
---|
72 | PCPDMDEVREGR0 pPdmDevR0 = NULL;
|
---|
73 | PCTSTDEVPDMDEV pPdmDev = tstDevPdmDeviceFind(pszName, &pPdmDevR0);
|
---|
74 | if (RT_LIKELY(pPdmDev))
|
---|
75 | {
|
---|
76 | uint32_t const cbRing0 = RT_ALIGN_32(RT_UOFFSETOF(PDMDEVINSR0, achInstanceData) + pPdmDevR0->cbInstanceCC,
|
---|
77 | HOST_PAGE_SIZE);
|
---|
78 | uint32_t const cbRing3 = RT_ALIGN_32(RT_UOFFSETOF(PDMDEVINSR3, achInstanceData) + pPdmDev->pReg->cbInstanceCC,
|
---|
79 | fRCEnabled ? HOST_PAGE_SIZE : 64);
|
---|
80 | uint32_t const cbRC = fRCEnabled ? 0
|
---|
81 | : RT_ALIGN_32(RT_UOFFSETOF(PDMDEVINSRC, achInstanceData) + pPdmDevR0->cbInstanceRC, 64);
|
---|
82 | uint32_t const cbShared = RT_ALIGN_32(pPdmDev->pReg->cbInstanceShared, 64);
|
---|
83 | uint32_t const cbCritSect = RT_ALIGN_32(sizeof(PDMCRITSECT), 64);
|
---|
84 | uint32_t const cbMsixState = RT_ALIGN_32(pPdmDev->pReg->cMaxMsixVectors * 16 + (pPdmDev->pReg->cMaxMsixVectors + 7) / 8, _4K);
|
---|
85 | uint32_t const cbPciDev = RT_ALIGN_32(RT_UOFFSETOF_DYN(PDMPCIDEV, abMsixState[cbMsixState]), 64);
|
---|
86 | uint32_t const cPciDevs = RT_MIN(pPdmDev->pReg->cMaxPciDevices, 8);
|
---|
87 | uint32_t const cbPciDevs = cbPciDev * cPciDevs;
|
---|
88 | uint32_t const cbTotal = RT_ALIGN_32(cbRing0 + cbRing3 + cbRC + cbShared + cbCritSect + cbPciDevs, HOST_PAGE_SIZE);
|
---|
89 | AssertLogRelMsgReturn(cbTotal <= PDM_MAX_DEVICE_INSTANCE_SIZE,
|
---|
90 | ("Instance of '%s' is too big: cbTotal=%u, max %u\n",
|
---|
91 | pPdmDev->pReg->szName, cbTotal, PDM_MAX_DEVICE_INSTANCE_SIZE),
|
---|
92 | VERR_OUT_OF_RANGE);
|
---|
93 |
|
---|
94 | PPDMDEVINSR0 pDevInsR0 = (PPDMDEVINSR0)RTMemAllocZ(cbTotal);
|
---|
95 | PDMDEVINSR3 *pDevInsR3 = (PDMDEVINSR3 *)(((uint8_t *)pDevInsR0 + cbRing0));
|
---|
96 |
|
---|
97 | pDevInsR0->u32Version = PDM_DEVINSR0_VERSION;
|
---|
98 | pDevInsR0->iInstance = 0;
|
---|
99 | pDevInsR0->pHlpR0 = &g_tstDevPdmDevHlpR0;
|
---|
100 | pDevInsR0->Internal.s.pDut = pDut;
|
---|
101 | pDevInsR0->pvInstanceDataR0 = (uint8_t *)pDevInsR0 + cbRing0 + cbRing3 + cbRC;
|
---|
102 | pDevInsR0->pvInstanceDataForR0 = &pDevInsR0->achInstanceData[0];
|
---|
103 | pDevInsR0->pCritSectRoR0 = (PPDMCRITSECT)((uint8_t *)pDevInsR0->pvInstanceDataR0 + cbShared);
|
---|
104 | pDevInsR0->pReg = pPdmDevR0;
|
---|
105 | pDevInsR0->pDevInsForR3 = (RTHCUINTPTR)pDevInsR3;
|
---|
106 | pDevInsR0->pDevInsForR3R0 = pDevInsR3;
|
---|
107 | pDevInsR0->pvInstanceDataForR3R0 = &pDevInsR3->achInstanceData[0];
|
---|
108 | pDevInsR0->cbPciDev = cbPciDev;
|
---|
109 | pDevInsR0->cPciDevs = cPciDevs;
|
---|
110 | for (uint32_t iPciDev = 0; iPciDev < cPciDevs; iPciDev++)
|
---|
111 | {
|
---|
112 | /* Note! PDMDevice.cpp has a copy of this code. Keep in sync. */
|
---|
113 | PPDMPCIDEV pPciDev = (PPDMPCIDEV)((uint8_t *)pDevInsR0->pCritSectRoR0 + cbCritSect + cbPciDev * iPciDev);
|
---|
114 | if (iPciDev < RT_ELEMENTS(pDevInsR0->apPciDevs))
|
---|
115 | pDevInsR0->apPciDevs[iPciDev] = pPciDev;
|
---|
116 | pPciDev->cbConfig = _4K;
|
---|
117 | pPciDev->cbMsixState = cbMsixState;
|
---|
118 | pPciDev->idxSubDev = (uint16_t)iPciDev;
|
---|
119 | //pPciDev->Int.s.idxSubDev = (uint16_t)iPciDev;
|
---|
120 | pPciDev->u32Magic = PDMPCIDEV_MAGIC;
|
---|
121 | }
|
---|
122 |
|
---|
123 | pDevInsR3->u32Version = PDM_DEVINSR3_VERSION;
|
---|
124 | pDevInsR3->iInstance = 0;
|
---|
125 | pDevInsR3->cbRing3 = cbRing3;
|
---|
126 | pDevInsR3->fR0Enabled = true;
|
---|
127 | pDevInsR3->fRCEnabled = fRCEnabled;
|
---|
128 | pDevInsR3->pvInstanceDataR3 = pDevInsR0->pDevInsForR3 + cbRing3 + cbRC;
|
---|
129 | pDevInsR3->pvInstanceDataForR3 = pDevInsR0->pDevInsForR3 + RT_UOFFSETOF(PDMDEVINSR3, achInstanceData);
|
---|
130 | pDevInsR3->pCritSectRoR3 = pDevInsR0->pDevInsForR3 + cbRing3 + cbRC + cbShared;
|
---|
131 | pDevInsR3->pDevInsR0RemoveMe = pDevInsR0;
|
---|
132 | pDevInsR3->pvInstanceDataR0 = pDevInsR0->pvInstanceDataR0;
|
---|
133 | pDevInsR3->pvInstanceDataRC = fRCEnabled ? NIL_RTRGPTR : pDevInsR0->pDevInsForRC + RT_UOFFSETOF(PDMDEVINSRC, achInstanceData);
|
---|
134 | pDevInsR3->pDevInsForRC = pDevInsR0->pDevInsForRC;
|
---|
135 | pDevInsR3->pDevInsForRCR3 = pDevInsR0->pDevInsForR3 + cbRing3;
|
---|
136 | pDevInsR3->pDevInsForRCR3 = pDevInsR3->pDevInsForRCR3 + RT_UOFFSETOF(PDMDEVINSRC, achInstanceData);
|
---|
137 | pDevInsR3->cbPciDev = cbPciDev;
|
---|
138 | pDevInsR3->cPciDevs = cPciDevs;
|
---|
139 | for (uint32_t i = 0; i < RT_MIN(cPciDevs, RT_ELEMENTS(pDevInsR3->apPciDevs)); i++)
|
---|
140 | pDevInsR3->apPciDevs[i] = pDevInsR3->pCritSectRoR3 + cbCritSect + cbPciDev * i;
|
---|
141 | RTCritSectInit(&pDevInsR0->pCritSectRoR0->s.CritSect);
|
---|
142 | pDut->pDevIns = pDevInsR3;
|
---|
143 | pDut->pDevInsR0 = pDevInsR0;
|
---|
144 |
|
---|
145 | rc = tstDevPdmDeviceR3Construct(pDut);
|
---|
146 | if (RT_SUCCESS(rc))
|
---|
147 | {
|
---|
148 | rc = pPdmDevR0->pfnConstruct(pDevInsR0);
|
---|
149 | if (RT_SUCCESS(rc))
|
---|
150 | return VINF_SUCCESS;
|
---|
151 | }
|
---|
152 |
|
---|
153 | if (RT_FAILURE(rc))
|
---|
154 | {
|
---|
155 | //rc = pPdmDev->pReg->pfnDestruct(pDevIns);
|
---|
156 | RTMemFree(pDevInsR0);
|
---|
157 | }
|
---|
158 | }
|
---|
159 | else
|
---|
160 | rc = VERR_NOT_FOUND;
|
---|
161 |
|
---|
162 | return rc;
|
---|
163 | }
|
---|