summaryrefslogtreecommitdiffstats
path: root/src/VBox/VMM/include/TRPMInternal.h
blob: 857f5dc04e97e157facb0e89ad2697c830199c10 (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
/* $Id: TRPMInternal.h $ */
/** @file
 * TRPM - Internal header file.
 */

/*
 * Copyright (C) 2006-2019 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 */

#ifndef VMM_INCLUDED_SRC_include_TRPMInternal_h
#define VMM_INCLUDED_SRC_include_TRPMInternal_h
#ifndef RT_WITHOUT_PRAGMA_ONCE
# pragma once
#endif

#include <VBox/cdefs.h>
#include <VBox/types.h>
#include <VBox/vmm/stam.h>
#include <VBox/vmm/cpum.h>
#include <VBox/vmm/pgm.h>

RT_C_DECLS_BEGIN


/** @defgroup grp_trpm_int   Internals
 * @ingroup grp_trpm
 * @internal
 * @{
 */


#ifdef VBOX_WITH_RAW_MODE
/** Enable or disable tracking of Guest's IDT. */
# define TRPM_TRACK_GUEST_IDT_CHANGES
/** Enable or disable tracking of Shadow IDT. */
# define TRPM_TRACK_SHADOW_IDT_CHANGES
#endif


/** Enable to allow trap forwarding in GC. */
#ifdef VBOX_WITH_RAW_MODE
# define TRPM_FORWARD_TRAPS_IN_GC
#endif

/** First interrupt handler. Used for validating input. */
#define TRPM_HANDLER_INT_BASE  0x20


/** @name   TRPMGCTrapIn* flags.
 * The lower bits are offsets into the CPUMCTXCORE structure.
 * @{ */
/** The mask for the operation. */
#define TRPM_TRAP_IN_OP_MASK    0xffff
/** Traps on MOV GS, eax. */
#define TRPM_TRAP_IN_MOV_GS     1
/** Traps on MOV FS, eax. */
#define TRPM_TRAP_IN_MOV_FS     2
/** Traps on MOV ES, eax. */
#define TRPM_TRAP_IN_MOV_ES     3
/** Traps on MOV DS, eax. */
#define TRPM_TRAP_IN_MOV_DS     4
/** Traps on IRET. */
#define TRPM_TRAP_IN_IRET       5
/** Set if this is a V86 resume. */
#define TRPM_TRAP_IN_V86        RT_BIT(30)
/** @} */


#if 0 /* not used */
/**
 * Converts a TRPM pointer into a VM pointer.
 * @returns Pointer to the VM structure the TRPM is part of.
 * @param   pTRPM       Pointer to TRPM instance data.
 */
#define TRPM_2_VM(pTRPM)            ( (PVM)((uint8_t *)(pTRPM) - (pTRPM)->offVM) )
#endif

/**
 * Converts a TRPM pointer into a TRPMCPU pointer.
 * @returns Pointer to the VM structure the TRPMCPU is part of.
 * @param   pTrpmCpu    Pointer to TRPMCPU instance data.
 * @remarks Raw-mode only, not SMP safe.
 */
#define TRPM_2_TRPMCPU(pTrpmCpu)     ( (PTRPMCPU)((uint8_t *)(pTrpmCpu) + (pTrpmCpu)->offTRPMCPU) )


/**
 * TRPM Data (part of VM)
 *
 * IMPORTANT! Keep the nasm version of this struct up-to-date.
 */
typedef struct TRPM
{
    /** Offset to the VM structure.
     * See TRPM_2_VM(). */
    RTINT                   offVM;
    /** Offset to the TRPMCPU structure.
     * See TRPM2TRPMCPU(). */
    RTINT                   offTRPMCPU;

    /** Whether monitoring of the guest IDT is enabled or not.
     *
     * This configuration option is provided for speeding up guest like Solaris
     * that put the IDT on the same page as a whole lot of other data that is
     * frequently updated. The updates will cause \#PFs and have to be interpreted
     * by PGMInterpretInstruction which is slow compared to raw execution.
     *
     * If the guest is well behaved and doesn't change the IDT after loading it,
     * there is no problem with dropping the IDT monitoring.
     *
     * @cfgm{/TRPM/SafeToDropGuestIDTMonitoring, boolean, defaults to false.}
     */
    bool                    fSafeToDropGuestIDTMonitoring;

    /** Padding to get the IDTs at a 16 byte alignment. */
    uint8_t                 abPadding1[7];
    /** IDTs. Aligned at 16 byte offset for speed. */
    VBOXIDTE                aIdt[256];

    /** Bitmap for IDTEs that contain PATM handlers. (needed for relocation) */
    uint32_t                au32IdtPatched[8];

    /** Temporary Hypervisor trap handlers.
     * NULL means default action. */
    RCPTRTYPE(void *)       aTmpTrapHandlers[256];

    /** RC Pointer to the IDT shadow area (aIdt) in HMA. */
    RCPTRTYPE(void *)       pvMonShwIdtRC;
    /** padding. */
    uint8_t                 au8Padding[2];
    /** Current (last) Guest's IDTR. */
    VBOXIDTR                GuestIdtr;
    /** Shadow IDT virtual write access handler type. */
    PGMVIRTHANDLERTYPE      hShadowIdtWriteHandlerType;
    /** Guest IDT virtual write access handler type. */
    PGMVIRTHANDLERTYPE      hGuestIdtWriteHandlerType;

    /** Checked trap & interrupt handler array */
    RCPTRTYPE(void *)       aGuestTrapHandler[256];

    /** RC: The number of times writes to the Guest IDT were detected. */
    STAMCOUNTER             StatRCWriteGuestIDTFault;
    STAMCOUNTER             StatRCWriteGuestIDTHandled;

    /** HC: Profiling of the TRPMR3SyncIDT() method. */
    STAMPROFILE             StatSyncIDT;
    /** GC: Statistics for the trap handlers. */
    STAMPROFILEADV          aStatGCTraps[0x14];

    STAMPROFILEADV          StatForwardProfR3;
    STAMPROFILEADV          StatForwardProfRZ;
    STAMCOUNTER             StatForwardFailNoHandler;
    STAMCOUNTER             StatForwardFailPatchAddr;
    STAMCOUNTER             StatForwardFailR3;
    STAMCOUNTER             StatForwardFailRZ;

    STAMPROFILE             StatTrap0dDisasm;
    STAMCOUNTER             StatTrap0dRdTsc;    /**< Number of RDTSC \#GPs. */

#ifdef VBOX_WITH_STATISTICS
    /** Statistics for interrupt handlers (allocated on the hypervisor heap) - R3
     * pointer. */
    R3PTRTYPE(PSTAMCOUNTER) paStatForwardedIRQR3;
    /** Statistics for interrupt handlers - RC pointer. */
    RCPTRTYPE(PSTAMCOUNTER) paStatForwardedIRQRC;

    /** Host interrupt statistics (allocated on the hypervisor heap) - RC ptr. */
    RCPTRTYPE(PSTAMCOUNTER) paStatHostIrqRC;
    /** Host interrupt statistics (allocated on the hypervisor heap) - R3 ptr. */
    R3PTRTYPE(PSTAMCOUNTER) paStatHostIrqR3;
#endif
} TRPM;
AssertCompileMemberAlignment(TRPM, GuestIdtr.pIdt, 8);

/** Pointer to TRPM Data. */
typedef TRPM *PTRPM;


/**
 * Converts a TRPMCPU pointer into a VM pointer.
 * @returns Pointer to the VM structure the TRPMCPU is part of.
 * @param   pTrpmCpu    Pointer to TRPMCPU instance data.
 */
#define TRPMCPU_2_VM(pTrpmCpu)      ( (PVM)((uint8_t *)(pTrpmCpu) - (pTrpmCpu)->offVM) )

/**
 * Converts a TRPMCPU pointer into a VMCPU pointer.
 * @returns Pointer to the VMCPU structure the TRPMCPU is part of.
 * @param   pTrpmCpu    Pointer to TRPMCPU instance data.
 */
#define TRPMCPU_2_VMCPU(pTrpmCpu)   ( (PVMCPU)((uint8_t *)(pTrpmCpu) - (pTrpmCpu)->offVMCpu) )


/**
 * Per CPU data for TRPM.
 */
typedef struct TRPMCPU
{
    /** Offset into the VM structure.
     * See TRPMCPU_2_VM(). */
    uint32_t                offVM;
    /** Offset into the VMCPU structure.
     * See TRPMCPU_2_VMCPU().  */
    uint32_t                offVMCpu;

    /** Active Interrupt or trap vector number.
     * If not UINT32_MAX this indicates that we're currently processing a
     * interrupt, trap, fault, abort, whatever which have arrived at that
     * vector number.
     */
    uint32_t                uActiveVector;

    /** Active trap type. */
    TRPMEVENT               enmActiveType;

    /** Errorcode for the active interrupt/trap. */
    RTGCUINT                uActiveErrorCode; /**< @todo don't use RTGCUINT */

    /** CR2 at the time of the active exception. */
    RTGCUINTPTR             uActiveCR2;

    /** Saved trap vector number. */
    RTGCUINT                uSavedVector; /**< @todo don't use RTGCUINT */

    /** Saved errorcode. */
    RTGCUINT                uSavedErrorCode;

    /** Saved cr2. */
    RTGCUINTPTR             uSavedCR2;

    /** Saved trap type. */
    TRPMEVENT               enmSavedType;

    /** Instruction length for software interrupts and software exceptions
     * (\#BP, \#OF) */
    uint8_t                 cbInstr;

    /** Saved instruction length. */
    uint8_t                 cbSavedInstr;

    /** Padding. */
    uint8_t                 au8Padding[2];

    /** Previous trap vector # - for debugging. */
    RTGCUINT                uPrevVector;
} TRPMCPU;

/** Pointer to TRPMCPU Data. */
typedef TRPMCPU *PTRPMCPU;


PGM_ALL_CB2_PROTO(FNPGMVIRTHANDLER) trpmGuestIDTWriteHandler;
DECLEXPORT(FNPGMRCVIRTPFHANDLER)    trpmRCGuestIDTWritePfHandler;
DECLEXPORT(FNPGMRCVIRTPFHANDLER)    trpmRCShadowIDTWritePfHandler;

/**
 * Clear guest trap/interrupt gate handler
 *
 * @returns VBox status code.
 * @param   pVM         The cross context VM structure.
 * @param   iTrap       Interrupt/trap number.
 */
VMMDECL(int) trpmClearGuestTrapHandler(PVM pVM, unsigned iTrap);


#ifdef IN_RING3
int trpmR3ClearPassThroughHandler(PVM pVM, unsigned iTrap);
#endif


#ifdef IN_RING0

/**
 * Calls the interrupt gate as if we received an interrupt while in Ring-0.
 *
 * @param   uIP     The interrupt gate IP.
 * @param   SelCS   The interrupt gate CS.
 * @param   RSP     The interrupt gate RSP. ~0 if no stack switch should take place. (only AMD64)
 */
DECLASM(void) trpmR0DispatchHostInterrupt(RTR0UINTPTR uIP, RTSEL SelCS, RTR0UINTPTR RSP);

/**
 * Issues a software interrupt to the specified interrupt vector.
 *
 * @param   uActiveVector   The vector number.
 */
DECLASM(void) trpmR0DispatchHostInterruptSimple(RTUINT uActiveVector);

#endif /* IN_RING0 */

/** @} */

RT_C_DECLS_END

#endif /* !VMM_INCLUDED_SRC_include_TRPMInternal_h */