2
* TRPM - The Trap Monitor.
6
* Copyright (C) 2006-2007 innotek GmbH
8
* This file is part of VirtualBox Open Source Edition (OSE), as
9
* available from http://www.virtualbox.org. This file is free software;
10
* you can redistribute it and/or modify it under the terms of the GNU
11
* General Public License as published by the Free Software Foundation,
12
* in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
13
* distribution. VirtualBox OSE is distributed in the hope that it will
14
* be useful, but WITHOUT ANY WARRANTY of any kind.
17
#ifndef ___VBox_trpm_h
18
#define ___VBox_trpm_h
20
#include <VBox/cdefs.h>
21
#include <VBox/types.h>
22
#include <VBox/cpum.h>
26
/** @defgroup grp_trpm The Trap Monitor API
31
* Trap: error code present or not
35
TRPM_TRAP_HAS_ERRORCODE = 0,
36
TRPM_TRAP_NO_ERRORCODE,
37
/** The usual 32-bit paranoia. */
38
TRPM_TRAP_32BIT_HACK = 0x7fffffff
44
/** Note: must match trpm.mac! */
48
TRPM_HARDWARE_INT = 1,
49
TRPM_SOFTWARE_INT = 2,
50
/** The usual 32-bit paranoia. */
51
TRPM_32BIT_HACK = 0x7fffffff
53
/** Pointer to a TRPM event type. */
54
typedef TRPMEVENT *PTRPMEVENT;
55
/** Pointer to a const TRPM event type. */
56
typedef TRPMEVENT const *PCTRPMEVENT;
59
* Invalid trap handler for trampoline calls
61
#define TRPM_INVALID_HANDLER 0
64
* Query info about the current active trap/interrupt.
65
* If no trap is active active an error code is returned.
67
* @returns VBox status code.
68
* @param pVM The virtual machine.
69
* @param pu8TrapNo Where to store the trap number.
70
* @param pEnmType Where to store the trap type.
72
TRPMDECL(int) TRPMQueryTrap(PVM pVM, uint8_t *pu8TrapNo, PTRPMEVENT pEnmType);
75
* Gets the trap number for the current trap.
77
* The caller is responsible for making sure there is an active trap which
78
* takes an error code when making this request.
80
* @returns The current trap number.
81
* @param pVM VM handle.
83
TRPMDECL(uint8_t) TRPMGetTrapNo(PVM pVM);
86
* Gets the error code for the current trap.
88
* The caller is responsible for making sure there is an active trap which
89
* takes an error code when making this request.
91
* @returns Error code.
92
* @param pVM VM handle.
94
TRPMDECL(RTGCUINT) TRPMGetErrorCode(PVM pVM);
97
* Gets the fault address for the current trap.
99
* The caller is responsible for making sure there is an active trap 0x0e when
100
* making this request.
102
* @returns Fault address associated with the trap.
103
* @param pVM VM handle.
105
TRPMDECL(RTGCUINTPTR) TRPMGetFaultAddress(PVM pVM);
108
* Clears the current active trap/exception/interrupt.
110
* The caller is responsible for making sure there is an active trap
111
* when making this request.
113
* @returns VBox status code.
114
* @param pVM The virtual machine handle.
116
TRPMDECL(int) TRPMResetTrap(PVM pVM);
119
* Assert trap/exception/interrupt.
121
* The caller is responsible for making sure there is no active trap
122
* when making this request.
124
* @returns VBox status code.
125
* @param pVM The virtual machine.
126
* @param u8TrapNo The trap vector to assert.
127
* @param enmType Trap type.
129
TRPMDECL(int) TRPMAssertTrap(PVM pVM, uint8_t u8TrapNo, TRPMEVENT enmType);
132
* Sets the error code of the current trap.
133
* (This function is for use in trap handlers and such.)
135
* The caller is responsible for making sure there is an active trap
136
* which takes an errorcode when making this request.
138
* @param pVM The virtual machine.
139
* @param uErrorCode The new error code.
141
TRPMDECL(void) TRPMSetErrorCode(PVM pVM, RTGCUINT uErrorCode);
144
* Sets the error code of the current trap.
145
* (This function is for use in trap handlers and such.)
147
* The caller is responsible for making sure there is an active trap 0e
148
* when making this request.
150
* @param pVM The virtual machine.
151
* @param uCR2 The new fault address (cr2 register).
153
TRPMDECL(void) TRPMSetFaultAddress(PVM pVM, RTGCUINTPTR uCR2);
156
* Checks if the current active trap/interrupt/exception/fault/whatever is a software
159
* The caller is responsible for making sure there is an active trap
160
* when making this request.
162
* @returns true if software interrupt, false if not.
164
* @param pVM VM handle.
166
TRPMDECL(bool) TRPMIsSoftwareInterrupt(PVM pVM);
169
* Check if there is an active trap.
171
* @returns true if trap active, false if not.
172
* @param pVM The virtual machine.
174
TRPMDECL(bool) TRPMHasTrap(PVM pVM);
177
* Query all info about the current active trap/interrupt.
178
* If no trap is active active an error code is returned.
180
* @returns VBox status code.
181
* @param pVM The virtual machine.
182
* @param pu8TrapNo Where to store the trap number.
183
* @param pEnmType Where to store the trap type.
184
* @param puErrorCode Where to store the error code associated with some traps.
185
* ~0U is stored if the trap have no error code.
186
* @param puCR2 Where to store the CR2 associated with a trap 0E.
188
TRPMDECL(int) TRPMQueryTrapAll(PVM pVM, uint8_t *pu8TrapNo, PTRPMEVENT pEnmType, PRTGCUINT puErrorCode, PRTGCUINTPTR puCR2);
192
* Save the active trap.
194
* This routine useful when doing try/catch in the hypervisor.
195
* Any function which uses temporary trap handlers should
196
* probably also use this facility to save the original trap.
198
* @param pVM VM handle.
200
TRPMDECL(void) TRPMSaveTrap(PVM pVM);
203
* Restore a saved trap.
205
* Multiple restores of a saved trap is possible.
207
* @param pVM VM handle.
209
TRPMDECL(void) TRPMRestoreTrap(PVM pVM);
212
* Forward trap or interrupt to the guest's handler
215
* @returns VBox status code.
216
* or does not return at all (when the trap is actually forwarded)
218
* @param pVM The VM to operate on.
219
* @param pRegFrame Pointer to the register frame for the trap.
220
* @param iGate Trap or interrupt gate number
221
* @param opsize Instruction size (only relevant for software interrupts)
222
* @param enmError TRPM_TRAP_HAS_ERRORCODE or TRPM_TRAP_NO_ERRORCODE.
223
* @param enmType TRPM event type
226
TRPMDECL(int) TRPMForwardTrap(PVM pVM, PCPUMCTXCORE pRegFrame, uint32_t iGate, uint32_t opsize, TRPMERRORCODE enmError, TRPMEVENT enmType);
229
* Raises a cpu exception which doesn't take an error code.
231
* This function may or may not dispatch the exception before returning.
233
* @returns VBox status code fit for scheduling.
234
* @retval VINF_EM_RAW_GUEST_TRAP if the exception was left pending.
235
* @retval VINF_TRPM_XCPT_DISPATCHED if the exception was raised and dispatched for raw-mode execution.
236
* @retval VINF_EM_RESCHEDULE_REM if the exception was dispatched and cannot be executed in raw-mode.
238
* @param pVM The VM handle.
239
* @param pCtxCore The CPU context core.
240
* @param enmXcpt The exception.
242
TRPMDECL(int) TRPMRaiseXcpt(PVM pVM, PCPUMCTXCORE pCtxCore, X86XCPT enmXcpt);
245
* Raises a cpu exception with an errorcode.
247
* This function may or may not dispatch the exception before returning.
249
* @returns VBox status code fit for scheduling.
250
* @retval VINF_EM_RAW_GUEST_TRAP if the exception was left pending.
251
* @retval VINF_TRPM_XCPT_DISPATCHED if the exception was raised and dispatched for raw-mode execution.
252
* @retval VINF_EM_RESCHEDULE_REM if the exception was dispatched and cannot be executed in raw-mode.
254
* @param pVM The VM handle.
255
* @param pCtxCore The CPU context core.
256
* @param enmXcpt The exception.
257
* @param uErr The error code.
259
TRPMDECL(int) TRPMRaiseXcptErr(PVM pVM, PCPUMCTXCORE pCtxCore, X86XCPT enmXcpt, uint32_t uErr);
262
* Raises a cpu exception with an errorcode and CR2.
264
* This function may or may not dispatch the exception before returning.
266
* @returns VBox status code fit for scheduling.
267
* @retval VINF_EM_RAW_GUEST_TRAP if the exception was left pending.
268
* @retval VINF_TRPM_XCPT_DISPATCHED if the exception was raised and dispatched for raw-mode execution.
269
* @retval VINF_EM_RESCHEDULE_REM if the exception was dispatched and cannot be executed in raw-mode.
271
* @param pVM The VM handle.
272
* @param pCtxCore The CPU context core.
273
* @param enmXcpt The exception.
274
* @param uErr The error code.
275
* @param uCR2 The CR2 value.
277
TRPMDECL(int) TRPMRaiseXcptErrCR2(PVM pVM, PCPUMCTXCORE pCtxCore, X86XCPT enmXcpt, uint32_t uErr, RTGCUINTPTR uCR2);
281
/** @defgroup grp_trpm_r3 TRPM Host Context Ring 3 API
287
* Initializes the SELM.
289
* @returns VBox status code.
290
* @param pVM The VM to operate on.
292
TRPMR3DECL(int) TRPMR3Init(PVM pVM);
295
* Applies relocations to data and code managed by this component.
297
* This function will be called at init and whenever the VMM need
298
* to relocate itself inside the GC.
300
* @param pVM The VM handle.
301
* @param offDelta Relocation delta relative to old location.
303
TRPMR3DECL(void) TRPMR3Relocate(PVM pVM, RTGCINTPTR offDelta);
306
* The VM is being reset.
308
* For the TRPM component this means that any IDT write monitors
309
* needs to be removed, any pending trap cleared, and the IDT reset.
311
* @param pVM VM handle.
313
TRPMR3DECL(void) TRPMR3Reset(PVM pVM);
316
* Set interrupt gate handler
317
* Used for setting up interrupt gates used for kernel calls.
319
* @returns VBox status code.
320
* @param pVM The VM to operate on.
321
* @param iTrap Interrupt number.
323
TRPMR3DECL(int) TRPMR3EnableGuestTrapHandler(PVM pVM, unsigned iTrap);
326
* Set guest trap/interrupt gate handler
327
* Used for setting up trap gates used for kernel calls.
329
* @returns VBox status code.
330
* @param pVM The VM to operate on.
331
* @param iTrap Interrupt/trap number.
332
* @parapm pHandler GC handler pointer
334
TRPMR3DECL(int) TRPMR3SetGuestTrapHandler(PVM pVM, unsigned iTrap, RTGCPTR pHandler);
337
* Get guest trap/interrupt gate handler
339
* @returns Guest trap handler address or TRPM_INVALID_HANDLER if none installed
340
* @param pVM The VM to operate on.
341
* @param iTrap Interrupt/trap number.
343
TRPMR3DECL(RTGCPTR) TRPMR3GetGuestTrapHandler(PVM pVM, unsigned iTrap);
346
* Disable IDT monitoring and syncing
348
* @param pVM The VM to operate on.
350
TRPMR3DECL(void) TRPMR3DisableMonitoring(PVM pVM);
353
* Check if gate handlers were updated
355
* @returns VBox status code.
356
* @param pVM The VM to operate on.
358
TRPMR3DECL(int) TRPMR3SyncIDT(PVM pVM);
361
* Check if address is a gate handler (interrupt/trap/task/anything).
363
* @returns True is gate handler, false if not.
365
* @param pVM VM handle.
366
* @param GCPtr GC address to check.
368
TRPMR3DECL(bool) TRPMR3IsGateHandler(PVM pVM, RTGCPTR GCPtr);
371
* Check if address is a gate handler (interrupt or trap).
373
* @returns gate nr or ~0 is not found
375
* @param pVM VM handle.
376
* @param GCPtr GC address to check.
378
TRPMR3DECL(uint32_t) TRPMR3QueryGateByHandler(PVM pVM, RTGCPTR GCPtr);
381
* Initializes the SELM.
383
* @returns VBox status code.
384
* @param pVM The VM to operate on.
386
TRPMR3DECL(int) TRPMR3Term(PVM pVM);
390
* Inject event (such as external irq or trap)
392
* @returns VBox status code.
393
* @param pVM The VM to operate on.
394
* @param enmEvent Trpm event type
396
TRPMR3DECL(int) TRPMR3InjectEvent(PVM pVM, TRPMEVENT enmEvent);
403
/** @defgroup grp_trpm_gc The TRPM Guest Context API
409
* Guest Context temporary trap handler
411
* @returns VBox status code (appropriate for GC return).
412
* In this context VBOX_SUCCESS means to restart the instruction.
413
* @param pVM VM handle.
414
* @param pRegFrame Trap register frame.
416
typedef DECLCALLBACK(int) FNTRPMGCTRAPHANDLER(PVM pVM, PCPUMCTXCORE pRegFrame);
417
/** Pointer to a TRPMGCTRAPHANDLER() function. */
418
typedef FNTRPMGCTRAPHANDLER *PFNTRPMGCTRAPHANDLER;
421
* Arms a temporary trap handler for traps in Hypervisor code.
423
* The operation is similar to a System V signal handler. I.e. when the handler
424
* is called it is first set to default action. So, if you need to handler more
425
* than one trap, you must reinstall the handler.
427
* To uninstall the temporary handler, call this function with pfnHandler set to NULL.
429
* @returns VBox status.
430
* @param pVM VM handle.
431
* @param iTrap Trap number to install handler [0..255].
432
* @param pfnHandler Pointer to the handler. Use NULL for uninstalling the handler.
434
TRPMGCDECL(int) TRPMGCSetTempHandler(PVM pVM, unsigned iTrap, PFNTRPMGCTRAPHANDLER pfnHandler);
437
* Return to host context from a hypervisor trap handler.
438
* It will also reset any traps that are pending.
440
* This function will *never* return.
442
* @param pVM The VM handle.
443
* @param rc The return code for host context.
445
TRPMGCDECL(void) TRPMGCHyperReturnToHost(PVM pVM, int rc);
452
/** @defgroup grp_trpm_r0 TRPM Host Context Ring 0 API
458
* Dispatches an interrupt that arrived while we were in the guest context.
460
* @param pVM The VM handle.
461
* @remark Must be called with interrupts disabled.
463
TRPMR0DECL(void) TRPMR0DispatchHostInterrupt(PVM pVM);
465
# ifndef VBOX_WITHOUT_IDT_PATCHING
468
* Changes the VMMR0Entry() call frame and stack used by the IDT patch code
469
* so that we'll dispatch an interrupt rather than returning directly to Ring-3
470
* when VMMR0Entry() returns.
472
* @param pVM Pointer to the VM.
473
* @param pvRet Pointer to the return address of VMMR0Entry() on the stack.
475
TRPMR0DECL(void) TRPMR0SetupInterruptDispatcherFrame(PVM pVM, void *pvRet);
477
# endif /* !VBOX_WITHOUT_IDT_PATCHING */