summaryrefslogtreecommitdiffstats
path: root/src/VBox/Devices/testcase/tstDeviceR0.cpp
blob: 4912c5759b504f41d7dd9bf67bf05567c9bbdca1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* $Id: tstDeviceR0.cpp $ */
/** @file
 * tstDevice - Test framework for PDM devices/drivers
 */

/*
 * Copyright (C) 2017-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_DEFAULT /** @todo */
#undef IN_RING3
#undef IN_SUP_R3
//#undef CTX_SUFF
//#undef R0PTRTYPE
//#undef R3PTRTYPE
#define IN_RING0
#define IN_SUP_R0
#define LINUX_VERSION_CODE 0
#define KERNEL_VERSION(a,b,c) 1
//#define CTX_SUFF(a_Name) a_Name##R0
//#define R3PTRTYPE(a_R3Type)     RTHCUINTPTR
//#define R0PTRTYPE(a_R0Type)     a_R0Type
#include <VBox/types.h>

#include <VBox/sup.h>
#include <VBox/version.h>
#include <iprt/assert.h>
#include <iprt/getopt.h>
#include <iprt/log.h>
#include <iprt/list.h>
#include <iprt/mem.h>

#include "tstDeviceInternal.h"
#include "tstDeviceCfg.h"
#include "tstDeviceBuiltin.h"



/**
 * Create a new PDM device with default config.
 *
 * @returns VBox status code.
 * @param   pszName                 Name of the device to create.
 * @param   fRCEnabled              Flag whether RC support should be enabled for this device.
 * @param   pDut                    The device under test structure the created PDM device instance is exercised under.
 */
DECLHIDDEN(int) tstDevPdmDevR0R3Create(const char *pszName, bool fRCEnabled, PTSTDEVDUTINT pDut)
{
    int rc = VINF_SUCCESS;
    PCPDMDEVREGR0 pPdmDevR0 = NULL;
    PCTSTDEVPDMDEV pPdmDev = tstDevPdmDeviceFind(pszName, &pPdmDevR0);
    if (RT_LIKELY(pPdmDev))
    {
        uint32_t const cbRing0     = RT_ALIGN_32(RT_UOFFSETOF(PDMDEVINSR0, achInstanceData) + pPdmDevR0->cbInstanceCC,
                                                 HOST_PAGE_SIZE);
        uint32_t const cbRing3     = RT_ALIGN_32(RT_UOFFSETOF(PDMDEVINSR3, achInstanceData) + pPdmDev->pReg->cbInstanceCC,
                                                 fRCEnabled ? HOST_PAGE_SIZE : 64);
        uint32_t const cbRC        = fRCEnabled ? 0
                                   : RT_ALIGN_32(RT_UOFFSETOF(PDMDEVINSRC, achInstanceData) + pPdmDevR0->cbInstanceRC, 64);
        uint32_t const cbShared    = RT_ALIGN_32(pPdmDev->pReg->cbInstanceShared, 64);
        uint32_t const cbCritSect  = RT_ALIGN_32(sizeof(PDMCRITSECT), 64);
        uint32_t const cbMsixState = RT_ALIGN_32(pPdmDev->pReg->cMaxMsixVectors * 16 + (pPdmDev->pReg->cMaxMsixVectors + 7) / 8, _4K);
        uint32_t const cbPciDev    = RT_ALIGN_32(RT_UOFFSETOF_DYN(PDMPCIDEV, abMsixState[cbMsixState]), 64);
        uint32_t const cPciDevs    = RT_MIN(pPdmDev->pReg->cMaxPciDevices, 8);
        uint32_t const cbPciDevs   = cbPciDev * cPciDevs;
        uint32_t const cbTotal     = RT_ALIGN_32(cbRing0 + cbRing3 + cbRC + cbShared + cbCritSect + cbPciDevs, HOST_PAGE_SIZE);
        AssertLogRelMsgReturn(cbTotal <= PDM_MAX_DEVICE_INSTANCE_SIZE,
                              ("Instance of '%s' is too big: cbTotal=%u, max %u\n",
                               pPdmDev->pReg->szName, cbTotal, PDM_MAX_DEVICE_INSTANCE_SIZE),
                              VERR_OUT_OF_RANGE);

        PPDMDEVINSR0 pDevInsR0 = (PPDMDEVINSR0)RTMemAllocZ(cbTotal);
        PDMDEVINSR3 *pDevInsR3   = (PDMDEVINSR3 *)(((uint8_t *)pDevInsR0 + cbRing0));

        pDevInsR0->u32Version             = PDM_DEVINSR0_VERSION;
        pDevInsR0->iInstance              = 0;
        pDevInsR0->pHlpR0                 = &g_tstDevPdmDevHlpR0;
        pDevInsR0->Internal.s.pDut        = pDut;
        pDevInsR0->pvInstanceDataR0       = (uint8_t *)pDevInsR0 + cbRing0 + cbRing3 + cbRC;
        pDevInsR0->pvInstanceDataForR0    = &pDevInsR0->achInstanceData[0];
        pDevInsR0->pCritSectRoR0          = (PPDMCRITSECT)((uint8_t *)pDevInsR0->pvInstanceDataR0 + cbShared);
        pDevInsR0->pReg                   = pPdmDevR0;
        pDevInsR0->pDevInsForR3           = (RTHCUINTPTR)pDevInsR3;
        pDevInsR0->pDevInsForR3R0         = pDevInsR3;
        pDevInsR0->pvInstanceDataForR3R0  = &pDevInsR3->achInstanceData[0];
        pDevInsR0->cbPciDev               = cbPciDev;
        pDevInsR0->cPciDevs               = cPciDevs;
        for (uint32_t iPciDev = 0; iPciDev < cPciDevs; iPciDev++)
        {
            /* Note! PDMDevice.cpp has a copy of this code.  Keep in sync. */
            PPDMPCIDEV pPciDev = (PPDMPCIDEV)((uint8_t *)pDevInsR0->pCritSectRoR0 + cbCritSect + cbPciDev * iPciDev);
            if (iPciDev < RT_ELEMENTS(pDevInsR0->apPciDevs))
                pDevInsR0->apPciDevs[iPciDev] = pPciDev;
            pPciDev->cbConfig           = _4K;
            pPciDev->cbMsixState        = cbMsixState;
            pPciDev->idxSubDev          = (uint16_t)iPciDev;
            //pPciDev->Int.s.idxSubDev    = (uint16_t)iPciDev;
            pPciDev->u32Magic           = PDMPCIDEV_MAGIC;
        }

        pDevInsR3->u32Version               = PDM_DEVINSR3_VERSION;
        pDevInsR3->iInstance                = 0;
        pDevInsR3->cbRing3                  = cbRing3;
        pDevInsR3->fR0Enabled               = true;
        pDevInsR3->fRCEnabled               = fRCEnabled;
        pDevInsR3->pvInstanceDataR3     = pDevInsR0->pDevInsForR3 + cbRing3 + cbRC;
        pDevInsR3->pvInstanceDataForR3  = pDevInsR0->pDevInsForR3 + RT_UOFFSETOF(PDMDEVINSR3, achInstanceData);
        pDevInsR3->pCritSectRoR3        = pDevInsR0->pDevInsForR3 + cbRing3 + cbRC + cbShared;
        pDevInsR3->pDevInsR0RemoveMe    = pDevInsR0;
        pDevInsR3->pvInstanceDataR0     = pDevInsR0->pvInstanceDataR0;
        pDevInsR3->pvInstanceDataRC     = fRCEnabled ? NIL_RTRGPTR : pDevInsR0->pDevInsForRC + RT_UOFFSETOF(PDMDEVINSRC, achInstanceData);
        pDevInsR3->pDevInsForRC         = pDevInsR0->pDevInsForRC;
        pDevInsR3->pDevInsForRCR3       = pDevInsR0->pDevInsForR3 + cbRing3;
        pDevInsR3->pDevInsForRCR3       = pDevInsR3->pDevInsForRCR3 + RT_UOFFSETOF(PDMDEVINSRC, achInstanceData);
        pDevInsR3->cbPciDev             = cbPciDev;
        pDevInsR3->cPciDevs             = cPciDevs;
        for (uint32_t i = 0; i < RT_MIN(cPciDevs, RT_ELEMENTS(pDevInsR3->apPciDevs)); i++)
            pDevInsR3->apPciDevs[i] = pDevInsR3->pCritSectRoR3 + cbCritSect + cbPciDev * i;
        RTCritSectInit(&pDevInsR0->pCritSectRoR0->s.CritSect);
        pDut->pDevIns   = pDevInsR3;
        pDut->pDevInsR0 = pDevInsR0;

        rc = tstDevPdmDeviceR3Construct(pDut);
        if (RT_SUCCESS(rc))
        {
            rc = pPdmDevR0->pfnConstruct(pDevInsR0);
            if (RT_SUCCESS(rc))
                return VINF_SUCCESS;
        }

        if (RT_FAILURE(rc))
        {
            //rc = pPdmDev->pReg->pfnDestruct(pDevIns);
            RTMemFree(pDevInsR0);
        }
    }
    else
        rc = VERR_NOT_FOUND;

    return rc;
}