summaryrefslogtreecommitdiff
path: root/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-TrapDefaultHandler.c
blob: bab26f97c6dd77f047143c785d4d7f638b7ab95e (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/* $Id$ */
/** @file
 * BS3Kit - Bs3TrapDefaultHandler
 */

/*
 * Copyright (C) 2007-2022 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>.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
 * in the VirtualBox distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 *
 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#include "bs3kit-template-header.h"
#if TMPL_BITS != 64
# include <VBox/VMMDevTesting.h>
# include <iprt/asm-amd64-x86.h>
#endif


/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/
#define g_pBs3TrapSetJmpFrame   BS3_DATA_NM(g_pBs3TrapSetJmpFrame)
extern uint32_t                 g_pBs3TrapSetJmpFrame;

#define g_Bs3TrapSetJmpCtx      BS3_DATA_NM(g_Bs3TrapSetJmpCtx)
extern BS3REGCTX                g_Bs3TrapSetJmpCtx;


#if TMPL_BITS != 64
/**
 * V86 syscall handler.
 */
static void bs3TrapDefaultHandlerV8086Syscall(PBS3TRAPFRAME pTrapFrame)
{
    /* Minimal syscall. */
    uint16_t uSyscallNo = pTrapFrame->Ctx.rax.u16;
    if (uSyscallNo == BS3_SYSCALL_PRINT_CHR)
        Bs3PrintChr(pTrapFrame->Ctx.rcx.u8);
    else if (uSyscallNo == BS3_SYSCALL_PRINT_STR)
        Bs3PrintStrN(Bs3XptrFlatToCurrent(((uint32_t)pTrapFrame->Ctx.rcx.u16 << 4) + pTrapFrame->Ctx.rsi.u16),
                     pTrapFrame->Ctx.rdx.u16);
    else if (uSyscallNo == BS3_SYSCALL_RESTORE_CTX)
        Bs3RegCtxRestore(Bs3XptrFlatToCurrent(((uint32_t)pTrapFrame->Ctx.rcx.u16 << 4) + pTrapFrame->Ctx.rsi.u16),
                         pTrapFrame->Ctx.rdx.u16);
    else if (   uSyscallNo == BS3_SYSCALL_TO_RING0
             || uSyscallNo == BS3_SYSCALL_TO_RING1
             || uSyscallNo == BS3_SYSCALL_TO_RING2
             || uSyscallNo == BS3_SYSCALL_TO_RING3)
    {
        Bs3RegCtxConvertToRingX(&pTrapFrame->Ctx, pTrapFrame->Ctx.rax.u16 - BS3_SYSCALL_TO_RING0);
    }
    else if (uSyscallNo == BS3_SYSCALL_SET_DRX)
    {
        uint32_t uValue = pTrapFrame->Ctx.rsi.u32;
        switch (pTrapFrame->Ctx.rdx.u8)
        {
            case 0: ASMSetDR0(uValue); break;
            case 1: ASMSetDR1(uValue); break;
            case 2: ASMSetDR2(uValue); break;
            case 3: ASMSetDR3(uValue); break;
            case 6: ASMSetDR6(uValue); break;
            case 7: ASMSetDR7(uValue); break;
            default: Bs3Panic();
        }
    }
    else if (uSyscallNo == BS3_SYSCALL_GET_DRX)
    {
        uint32_t uValue;
        switch (pTrapFrame->Ctx.rdx.u8)
        {
            case 0: uValue = ASMGetDR0(); break;
            case 1: uValue = ASMGetDR1(); break;
            case 2: uValue = ASMGetDR2(); break;
            case 3: uValue = ASMGetDR3(); break;
            case 6: uValue = ASMGetDR6(); break;
            case 7: uValue = ASMGetDR7(); break;
            default: uValue = 0; Bs3Panic();
        }
        pTrapFrame->Ctx.rax.u32 = uValue;
        pTrapFrame->Ctx.rdx.u32 = uValue >> 16;
    }
    else if (uSyscallNo == BS3_SYSCALL_SET_CRX)
    {
        uint32_t uValue = pTrapFrame->Ctx.rsi.u32;
        switch (pTrapFrame->Ctx.rdx.u8)
        {
            case 0: ASMSetCR0(uValue); pTrapFrame->Ctx.cr0.u32 = uValue; break;
            case 2: ASMSetCR2(uValue); pTrapFrame->Ctx.cr2.u32 = uValue; break;
            case 3: ASMSetCR3(uValue); pTrapFrame->Ctx.cr3.u32 = uValue; break;
            case 4: ASMSetCR4(uValue); pTrapFrame->Ctx.cr4.u32 = uValue; break;
            default: Bs3Panic();
        }
    }
    else if (uSyscallNo == BS3_SYSCALL_GET_CRX)
    {
        uint32_t uValue;
        switch (pTrapFrame->Ctx.rdx.u8)
        {
            case 0: uValue = ASMGetCR0(); break;
            case 2: uValue = ASMGetCR2(); break;
            case 3: uValue = ASMGetCR3(); break;
            case 4: uValue = ASMGetCR4(); break;
            default: uValue = 0; Bs3Panic();
        }
        pTrapFrame->Ctx.rax.u32 = uValue;
        pTrapFrame->Ctx.rdx.u32 = uValue >> 16;
    }
    else if (uSyscallNo == BS3_SYSCALL_SET_TR)
    {
        Bs3RegSetTr(pTrapFrame->Ctx.rdx.u16);
        pTrapFrame->Ctx.tr = pTrapFrame->Ctx.rdx.u16;
    }
    else if (uSyscallNo == BS3_SYSCALL_GET_TR)
        pTrapFrame->Ctx.rax.u16 = ASMGetTR();
    else if (uSyscallNo == BS3_SYSCALL_SET_LDTR)
    {
        Bs3RegSetLdtr(pTrapFrame->Ctx.rdx.u16);
        pTrapFrame->Ctx.ldtr = pTrapFrame->Ctx.rdx.u16;
    }
    else if (uSyscallNo == BS3_SYSCALL_GET_LDTR)
        pTrapFrame->Ctx.rax.u16 = ASMGetLDTR();
    else if (uSyscallNo == BS3_SYSCALL_SET_XCR0)
        ASMSetXcr0(RT_MAKE_U64(pTrapFrame->Ctx.rsi.u32, pTrapFrame->Ctx.rdx.u32));
    else if (uSyscallNo == BS3_SYSCALL_GET_XCR0)
    {
        uint64_t const uValue = ASMGetXcr0();
        pTrapFrame->Ctx.rax.u32 = (uint32_t)uValue;
        pTrapFrame->Ctx.rdx.u32 = (uint32_t)(uValue >> 32);
    }
    else
        Bs3Panic();
}
#endif

#undef Bs3TrapDefaultHandler
BS3_CMN_DEF(void, Bs3TrapDefaultHandler,(PBS3TRAPFRAME pTrapFrame))
{
#if TMPL_BITS != 64
    /*
     * v8086 VMM tasks.
     */
    //Bs3TestHostPrintf("Bs3____DefaultHandler: %02xh %04RX16:%08RX32 %08RX32 %04RX16:%08RX32 %d %d\n", pTrapFrame->bXcpt,
    //                  pTrapFrame->Ctx.cs, pTrapFrame->Ctx.rip.u32, pTrapFrame->Ctx.rflags.u32, pTrapFrame->Ctx.ss,
    //                  pTrapFrame->Ctx.rsp.u32, g_fBs3TrapNoV86Assist, 42);
    if ((pTrapFrame->Ctx.rflags.u32 & X86_EFL_VM))
    {
        bool                    fHandled    = true;
        uint8_t                 cBitsOpcode = 16;
        uint8_t                 bOpCode;
        uint8_t const BS3_FAR  *pbCodeStart;
        uint8_t const BS3_FAR  *pbCode;
        uint16_t BS3_FAR       *pusStack;

        pusStack    = (uint16_t      BS3_FAR *)BS3_MAKE_PROT_R0PTR_FROM_REAL(pTrapFrame->Ctx.ss, pTrapFrame->Ctx.rsp.u16);
        pbCode      = (uint8_t const BS3_FAR *)BS3_MAKE_PROT_R0PTR_FROM_REAL(pTrapFrame->Ctx.cs, pTrapFrame->Ctx.rip.u16);
        pbCodeStart = pbCode;

        /*
         * Deal with GPs in V8086 mode.
         */
        if (   pTrapFrame->bXcpt == X86_XCPT_GP
            && !g_fBs3TrapNoV86Assist)
        {
            bOpCode = *pbCode++;
            if (bOpCode == 0x66)
            {
                cBitsOpcode = 32;
                bOpCode = *pbCode++;
            }

            /* INT xx: Real mode behaviour, but intercepting and implementing most of our syscall interface. */
            if (bOpCode == 0xcd)
            {
                uint8_t bVector = *pbCode++;
                if (bVector == BS3_TRAP_SYSCALL)
                    bs3TrapDefaultHandlerV8086Syscall(pTrapFrame);
                else
                {
                    /* Real mode behaviour. */
                    uint16_t BS3_FAR *pusIvte = (uint16_t BS3_FAR *)BS3_MAKE_PROT_R0PTR_FROM_REAL(0, 0);
                    pusIvte += (uint16_t)bVector *2;

                    pusStack[0] = pTrapFrame->Ctx.rflags.u16;
                    pusStack[1] = pTrapFrame->Ctx.cs;
                    pusStack[2] = pTrapFrame->Ctx.rip.u16 + (uint16_t)(pbCode - pbCodeStart);

                    pTrapFrame->Ctx.rip.u16 = pusIvte[0];
                    pTrapFrame->Ctx.cs      = pusIvte[1];
                    pTrapFrame->Ctx.rflags.u16 &= ~X86_EFL_IF; /** @todo this isn't all, but it'll do for now, I hope. */
                    Bs3RegCtxRestore(&pTrapFrame->Ctx, 0/*fFlags*/); /* does not return. */
                }
            }
            /* PUSHF: Real mode behaviour. */
            else if (bOpCode == 0x9c)
            {
                if (cBitsOpcode == 32)
                    *pusStack++ = pTrapFrame->Ctx.rflags.au16[1] & ~(X86_EFL_VM | X86_EFL_RF);
                *pusStack++ = pTrapFrame->Ctx.rflags.u16;
                pTrapFrame->Ctx.rsp.u16 += cBitsOpcode / 8;
            }
            /* POPF:  Real mode behaviour. */
            else if (bOpCode == 0x9d)
            {
                if (cBitsOpcode == 32)
                {
                    pTrapFrame->Ctx.rflags.u32 &= ~X86_EFL_POPF_BITS;
                    pTrapFrame->Ctx.rflags.u32 |= X86_EFL_POPF_BITS & *(uint32_t const *)pusStack;
                }
                else
                {
                    pTrapFrame->Ctx.rflags.u32 &= ~(X86_EFL_POPF_BITS | UINT32_C(0xffff0000)) & ~X86_EFL_RF;
                    pTrapFrame->Ctx.rflags.u16 |= (uint16_t)X86_EFL_POPF_BITS & *pusStack;
                }
                pTrapFrame->Ctx.rsp.u16 -= cBitsOpcode / 8;
            }
            /* CLI: Real mode behaviour. */
            else if (bOpCode == 0xfa)
                pTrapFrame->Ctx.rflags.u16 &= ~X86_EFL_IF;
            /* STI: Real mode behaviour. */
            else if (bOpCode == 0xfb)
                pTrapFrame->Ctx.rflags.u16 |= X86_EFL_IF;
            /* OUT: byte I/O to VMMDev. */
            else if (   bOpCode == 0xee
                     && ((unsigned)(pTrapFrame->Ctx.rdx.u16 - VMMDEV_TESTING_IOPORT_BASE) < (unsigned)VMMDEV_TESTING_IOPORT_COUNT))
                ASMOutU8(pTrapFrame->Ctx.rdx.u16, pTrapFrame->Ctx.rax.u8);
            /* OUT: [d]word I/O to VMMDev. */
            else if (   bOpCode == 0xef
                     && ((unsigned)(pTrapFrame->Ctx.rdx.u16 - VMMDEV_TESTING_IOPORT_BASE) < (unsigned)VMMDEV_TESTING_IOPORT_COUNT))
            {
                if (cBitsOpcode != 32)
                    ASMOutU16(pTrapFrame->Ctx.rdx.u16, pTrapFrame->Ctx.rax.u16);
                else
                    ASMOutU32(pTrapFrame->Ctx.rdx.u16, pTrapFrame->Ctx.rax.u32);
            }
            /* IN: byte I/O to VMMDev. */
            else if (   bOpCode == 0xec
                     && ((unsigned)(pTrapFrame->Ctx.rdx.u16 - VMMDEV_TESTING_IOPORT_BASE) < (unsigned)VMMDEV_TESTING_IOPORT_COUNT))
                pTrapFrame->Ctx.rax.u8 = ASMInU8(pTrapFrame->Ctx.rdx.u16);
            /* IN: [d]word I/O to VMMDev. */
            else if (   bOpCode == 0xed
                     && ((unsigned)(pTrapFrame->Ctx.rdx.u16 - VMMDEV_TESTING_IOPORT_BASE) < (unsigned)VMMDEV_TESTING_IOPORT_COUNT))
            {
                if (cBitsOpcode != 32)
                    pTrapFrame->Ctx.rax.u16 = ASMInU16(pTrapFrame->Ctx.rdx.u16);
                else
                    pTrapFrame->Ctx.rax.u32 = ASMInU32(pTrapFrame->Ctx.rdx.u32);
            }
            /* Unexpected. */
            else
                fHandled = false;
        }
        /*
         * Deal with lock prefixed int xxh syscall in v8086 mode.
         */
        else if (   pTrapFrame->bXcpt == X86_XCPT_UD
                 && pTrapFrame->Ctx.cs == BS3_SEL_TEXT16
                 && pTrapFrame->Ctx.rax.u16 <= BS3_SYSCALL_LAST
                 && pbCode[0] == 0xf0
                 && pbCode[1] == 0xcd
                 && pbCode[2] == BS3_TRAP_SYSCALL)
        {
            pbCode += 3;
            bs3TrapDefaultHandlerV8086Syscall(pTrapFrame);
        }
        else
        {
            fHandled = false;
        }
        if (fHandled)
        {
            pTrapFrame->Ctx.rip.u16 += (uint16_t)(pbCode - pbCodeStart);
# if 0
            Bs3Printf("Calling Bs3RegCtxRestore\n");
            Bs3RegCtxPrint(&pTrapFrame->Ctx);
# endif
            Bs3RegCtxRestore(&pTrapFrame->Ctx, 0 /*fFlags*/); /* does not return. */
            return;
        }
    }
#endif

    /*
     * Any pending setjmp?
     */
    if (g_pBs3TrapSetJmpFrame != 0)
    {
        PBS3TRAPFRAME pSetJmpFrame = (PBS3TRAPFRAME)Bs3XptrFlatToCurrent(g_pBs3TrapSetJmpFrame);
        //Bs3Printf("Calling longjmp: pSetJmpFrame=%p (%#lx)\n", pSetJmpFrame, g_pBs3TrapSetJmpFrame);
        g_pBs3TrapSetJmpFrame = 0;
        Bs3MemCpy(pSetJmpFrame, pTrapFrame, sizeof(*pSetJmpFrame));
        //Bs3RegCtxPrint(&g_Bs3TrapSetJmpCtx);
        Bs3RegCtxRestore(&g_Bs3TrapSetJmpCtx, 0 /*fFlags*/);
    }

    /*
     * Fatal.
     */
    Bs3TestPrintf("*** GURU ***\n");
    Bs3TrapPrintFrame(pTrapFrame);
    Bs3Panic();
}