summaryrefslogtreecommitdiffstats
path: root/src/VBox/VMM/testcase/tstMicroRC.cpp
blob: c5d85ff536df6cec6bd7a223f6096ff95dc887e4 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* $Id: tstMicroRC.cpp $ */
/** @file
 * Micro Testcase, profiling special CPU operations - GC Code (hacks).
 */

/*
 * Copyright (C) 2006-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                                                                                                                 *
*********************************************************************************************************************************/
#include <VBox/vmm/vm.h>
#include <VBox/vmm/vmm.h>
#include <VBox/vmm/selm.h>
#include "tstMicro.h"

#include <iprt/errcore.h>
#include <iprt/asm-amd64-x86.h>
#include <VBox/log.h>
#include <iprt/assert.h>
#include <iprt/string.h>


/*********************************************************************************************************************************
*   Internal Functions                                                                                                           *
*********************************************************************************************************************************/
RT_C_DECLS_BEGIN
DECLEXPORT(int) tstMicroRC(PTSTMICRO pTst, unsigned uTestcase);
RT_C_DECLS_END


/**
 * Save and load our IDT.
 *
 * @param   pTst    Pointer to the instance data.
 * @param   iIDT    The index of the IDT entry which should be hooked.
 */
void idtInstall(PTSTMICRO pTst, int iIDT)
{
    RTIDTR Idtr;
    ASMGetIDTR(&Idtr);
    if (Idtr.pIdt == (uintptr_t)&pTst->aIDT[0])
        return;
    pTst->OriginalIDTR.cbIdt = Idtr.cbIdt;
    pTst->OriginalIDTR.pIdt  = Idtr.pIdt;

    /*
     * Copy the IDT.
     */
    if (Idtr.cbIdt >= sizeof(pTst->aIDT))
        Idtr.cbIdt = sizeof(pTst->aIDT) - 1;
    memcpy(&pTst->aIDT[0], (void *)Idtr.pIdt, Idtr.cbIdt + 1);


    /* Hook up IDT entry. */
    if (iIDT >= 0)
    {
        uintptr_t uHandler = (uintptr_t)tstTrapHandlerNoErr;
        if (    iIDT == 8
            ||  iIDT == 0xa
            ||  iIDT == 0xb
            ||  iIDT == 0xc
            ||  iIDT == 0xd
            ||  iIDT == 0xe
            ||  iIDT == 0x11)
            uHandler = (uintptr_t)tstTrapHandler;
        pTst->aIDT[iIDT].Int.u16OffsetHigh  = uHandler >> 16;
        pTst->aIDT[iIDT].Int.u16OffsetLow   = uHandler & 0xffff;
        pTst->aIDT[iIDT].Int.u16SegSel      = SELMGetHyperCS(&g_VM);
        pTst->aIDT[iIDT].Int.u2DPL          = 3;
        pTst->aIDT[iIDT].Int.u1Present      = 1;
        pTst->aIDT[iIDT].Int.u1Fixed0       = 0;
        pTst->aIDT[iIDT].Int.u1Fixed1       = 0;
        pTst->aIDT[iIDT].Int.u1Fixed2       = 0;
        pTst->aIDT[iIDT].Int.u1Fixed3       = 0;
        pTst->aIDT[iIDT].Int.u1Fixed4       = 1;
        pTst->aIDT[iIDT].Int.u1Fixed5       = 1;
        pTst->aIDT[iIDT].Int.u132BitGate    = 1;
        pTst->aIDT[iIDT].Int.u1Fixed6       = 0;
        pTst->aIDT[iIDT].Int.u5Reserved2    = 0;
    }

    /* Install int 42h, R3 gate */
    pTst->aIDT[0x42].Int.u16OffsetHigh  = (uintptr_t)tstInterrupt42 >> 16;
    pTst->aIDT[0x42].Int.u16OffsetLow   = (uintptr_t)tstInterrupt42 & 0xffff;
    pTst->aIDT[0x42].Int.u16SegSel      = SELMGetHyperCS(&g_VM);
    pTst->aIDT[0x42].Int.u2DPL          = 3;
    pTst->aIDT[0x42].Int.u1Present      = 1;
    pTst->aIDT[0x42].Int.u1Fixed0       = 0;
    pTst->aIDT[0x42].Int.u1Fixed1       = 0;
    pTst->aIDT[0x42].Int.u1Fixed2       = 0;
    pTst->aIDT[0x42].Int.u1Fixed3       = 0;
    pTst->aIDT[0x42].Int.u1Fixed4       = 1;
    pTst->aIDT[0x42].Int.u1Fixed5       = 1;
    pTst->aIDT[0x42].Int.u132BitGate    = 1;
    pTst->aIDT[0x42].Int.u1Fixed6       = 0;
    pTst->aIDT[0x42].Int.u5Reserved2    = 0;

    /*
     * Load our IDT.
     */
    Idtr.pIdt = (uintptr_t)&pTst->aIDT[0];
    ASMSetIDTR(&Idtr);

    RTIDTR Idtr2;
    ASMGetIDTR(&Idtr2);
    Assert(Idtr2.pIdt == (uintptr_t)&pTst->aIDT[0]);
}


/**
 * Removes all trap overrides except for gate 42.
 */
DECLASM(void) idtOnly42(PTSTMICRO pTst)
{
    if (pTst->OriginalIDTR.pIdt)
        memcpy(&pTst->aIDT[0], (void *)(uintptr_t)pTst->OriginalIDTR.pIdt, sizeof(VBOXIDTE) * 32);
}



DECLEXPORT(int) tstMicroRC(PTSTMICRO pTst, unsigned uTestcase)
{
    RTLogPrintf("pTst=%p uTestcase=%d\n", pTst, uTestcase);

    /*
     * Validate input.
     */
    if (uTestcase >= TSTMICROTEST_MAX)
        return VERR_INVALID_PARAMETER;

    /*
     * Clear the results.
     */
    pTst->u64TSCR0Start = 0;
    pTst->u64TSCRxStart = 0;
    pTst->u64TSCR0Enter = 0;
    pTst->u64TSCR0Exit = 0;
    pTst->u64TSCRxEnd = 0;
    pTst->u64TSCR0End = 0;
    pTst->cHits = 0;
    pTst->offEIPAdd = 0;
    pTst->u32CR2 = 0;
    pTst->u32EIP = 0;
    pTst->u32ErrCd = 0;
    PTSTMICRORESULT pRes = &pTst->aResults[uTestcase];
    memset(&pTst->aResults[uTestcase], 0, sizeof(pTst->aResults[uTestcase]));


    /*
     * Do the testcase.
     */
    int rc = VINF_SUCCESS;
    switch (uTestcase)
    {
        case TSTMICROTEST_OVERHEAD:
        {
            tstOverhead(pTst);
            break;
        }

        case TSTMICROTEST_INVLPG_0:
        {
            tstInvlpg0(pTst);
            break;
        }

        case TSTMICROTEST_INVLPG_EIP:
        {
            tstInvlpgEIP(pTst);
            break;
        }

        case TSTMICROTEST_INVLPG_ESP:
        {
            tstInvlpgESP(pTst);
            break;
        }

        case TSTMICROTEST_CR3_RELOAD:
        {
            tstCR3Reload(pTst);
            break;
        }

        case TSTMICROTEST_WP_DISABLE:
        {
            tstWPDisable(pTst);
            break;
        }

        case TSTMICROTEST_WP_ENABLE:
        {
            tstWPEnable(pTst);
            break;
        }

        case TSTMICROTEST_PF_R0:
        {
            idtInstall(pTst, 0xe);
            pTst->offEIPAdd = 2;
            rc = tstPFR0(pTst);
            break;
        }

        case TSTMICROTEST_PF_R1:
        {
            idtInstall(pTst, 0xe);
            pTst->offEIPAdd = 2;
            rc = tstPFR1(pTst);
            break;
        }

        case TSTMICROTEST_PF_R2:
        {
            idtInstall(pTst, 0xe);
            pTst->offEIPAdd = 2;
            rc = tstPFR2(pTst);
            break;
        }

        case TSTMICROTEST_PF_R3:
        {
            idtInstall(pTst, 0xe);
            pTst->offEIPAdd = 2;
            rc = tstPFR3(pTst);
            break;
        }

    }

    /*
     * Compute the results.
     */
    if (pTst->u64TSCR0End   && pTst->u64TSCR0Start)
        pRes->cTotalTicks       = pTst->u64TSCR0End     - pTst->u64TSCR0Start   - pTst->u64Overhead;
    if (pTst->u64TSCRxStart && pTst->u64TSCR0Start)
        pRes->cToRxFirstTicks   = pTst->u64TSCRxStart   - pTst->u64TSCR0Start   - pTst->u64Overhead;
    if (pTst->u64TSCR0Enter && pTst->u64TSCRxStart)
        pRes->cTrapTicks        = pTst->u64TSCR0Enter   - pTst->u64TSCRxStart   - pTst->u64Overhead;
    if (pTst->u64TSCRxEnd   && pTst->u64TSCR0Exit)
        pRes->cToRxTrapTicks    = pTst->u64TSCRxEnd     - pTst->u64TSCR0Exit    - pTst->u64Overhead;
    if (pTst->u64TSCR0End   && pTst->u64TSCRxEnd)
        pRes->cToR0Ticks        = pTst->u64TSCR0End     - pTst->u64TSCRxEnd     - pTst->u64Overhead;

    return rc;
}