VirtualBox

source: vbox/trunk/include/VBox/trpm.h@ 659

Last change on this file since 659 was 416, checked in by vboxsync, 18 years ago

Fixing interrupt dispatching on amd64.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.0 KB
Line 
1/** @file
2 * TRPM - The Trap Monitor.
3 */
4
5/*
6 * Copyright (C) 2006 InnoTek Systemberatung GmbH
7 *
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.
15 *
16 * If you received this file as part of a commercial VirtualBox
17 * distribution, then only the terms of your commercial VirtualBox
18 * license agreement apply instead of the previous paragraph.
19 */
20
21#ifndef __VBox_im_h__
22#define __VBox_im_h__
23
24#include <VBox/cdefs.h>
25#include <VBox/types.h>
26#include <VBox/cpum.h>
27
28
29__BEGIN_DECLS
30/** @defgroup grp_trpm The Trap Monitor API
31 * @{
32 */
33
34/**
35 * Trap: error code present or not
36 */
37typedef enum
38{
39 TRPM_TRAP_HAS_ERRORCODE = 0,
40 TRPM_TRAP_NO_ERRORCODE
41} TRPMERRORCODE;
42
43/**
44 * TRPM event type
45 */
46typedef enum
47{
48 TRPM_TRAP = 0,
49 TRPM_HARDWARE_INT,
50 TRPM_SOFTWARE_INT
51} TRPMEVENT;
52
53/**
54 * Invalid trap handler for trampoline calls
55 */
56#define TRPM_INVALID_HANDLER 0
57
58/**
59 * Query info about the current active trap/interrupt.
60 * If no trap is active active an error code is returned.
61 *
62 * @returns VBox status code.
63 * @param pVM The virtual machine.
64 * @param pu8TrapNo Where to store the trap number.
65 * @param pfSoftwareInterrupt Where to store the software interrupt indicator.
66 */
67TRPMDECL(int) TRPMQueryTrap(PVM pVM, uint8_t *pu8TrapNo, bool *pfSoftwareInterrupt);
68
69/**
70 * Gets the trap number for the current trap.
71 *
72 * The caller is responsible for making sure there is an active trap which
73 * takes an error code when making this request.
74 *
75 * @returns The current trap number.
76 * @param pVM VM handle.
77 */
78TRPMDECL(uint8_t) TRPMGetTrapNo(PVM pVM);
79
80/**
81 * Gets the error code for the current trap.
82 *
83 * The caller is responsible for making sure there is an active trap which
84 * takes an error code when making this request.
85 *
86 * @returns Error code.
87 * @param pVM VM handle.
88 */
89TRPMDECL(RTGCUINT) TRPMGetErrorCode(PVM pVM);
90
91/**
92 * Gets the fault address for the current trap.
93 *
94 * The caller is responsible for making sure there is an active trap 0x0e when
95 * making this request.
96 *
97 * @returns Fault address associated with the trap.
98 * @param pVM VM handle.
99 */
100TRPMDECL(RTGCUINTPTR) TRPMGetFaultAddress(PVM pVM);
101
102/**
103 * Clears the current active trap/exception/interrupt.
104 *
105 * The caller is responsible for making sure there is an active trap
106 * when making this request.
107 *
108 * @returns VBox status code.
109 * @param pVM The virtual machine handle.
110 */
111TRPMDECL(int) TRPMResetTrap(PVM pVM);
112
113/**
114 * Assert trap/exception/interrupt.
115 *
116 * The caller is responsible for making sure there is no active trap
117 * when making this request.
118 *
119 * @returns VBox status code.
120 * @param pVM The virtual machine.
121 * @param u8TrapNo The trap vector to assert.
122 * @param fSoftwareInterrupt Indicate if it's a software interrupt or not.
123 */
124TRPMDECL(int) TRPMAssertTrap(PVM pVM, uint8_t u8TrapNo, bool fSoftwareInterrupt);
125
126/**
127 * Sets the error code of the current trap.
128 * (This function is for use in trap handlers and such.)
129 *
130 * The caller is responsible for making sure there is an active trap
131 * which takes an errorcode when making this request.
132 *
133 * @param pVM The virtual machine.
134 * @param uErrorCode The new error code.
135 */
136TRPMDECL(void) TRPMSetErrorCode(PVM pVM, RTGCUINT uErrorCode);
137
138/**
139 * Sets the error code of the current trap.
140 * (This function is for use in trap handlers and such.)
141 *
142 * The caller is responsible for making sure there is an active trap 0e
143 * when making this request.
144 *
145 * @param pVM The virtual machine.
146 * @param uCR2 The new fault address (cr2 register).
147 */
148TRPMDECL(void) TRPMSetFaultAddress(PVM pVM, RTGCUINTPTR uCR2);
149
150/**
151 * Checks if the current active trap/interrupt/exception/fault/whatever is a software
152 * interrupt or not.
153 *
154 * The caller is responsible for making sure there is an active trap
155 * when making this request.
156 *
157 * @returns true if software interrupt, false if not.
158 *
159 * @param pVM VM handle.
160 */
161TRPMDECL(bool) TRPMIsSoftwareInterrupt(PVM pVM);
162
163/**
164 * Check if there is an active trap.
165 *
166 * @returns true if trap active, false if not.
167 * @param pVM The virtual machine.
168 */
169TRPMDECL(bool) TRPMHasTrap(PVM pVM);
170
171/**
172 * Query all info about the current active trap/interrupt.
173 * If no trap is active active an error code is returned.
174 *
175 * @returns VBox status code.
176 * @param pVM The virtual machine.
177 * @param pu8TrapNo Where to store the trap number.
178 * @param pfSoftwareInterrupt Where to store the software interrupt indicator.
179 * @param puErrorCode Where to store the error code associated with some traps.
180 * ~0U is stored if the trap have no error code.
181 * @param puCR2 Where to store the CR2 associated with a trap 0E.
182 */
183TRPMDECL(int) TRPMQueryTrapAll(PVM pVM, uint8_t *pu8TrapNo, bool *pfSoftwareInterrupt, PRTGCUINT puErrorCode, PRTGCUINTPTR puCR2);
184
185
186/**
187 * Save the active trap.
188 *
189 * This routine useful when doing try/catch in the hypervisor.
190 * Any function which uses temporary trap handlers should
191 * probably also use this facility to save the original trap.
192 *
193 * @param pVM VM handle.
194 */
195TRPMDECL(void) TRPMSaveTrap(PVM pVM);
196
197/**
198 * Restore a saved trap.
199 *
200 * Multiple restores of a saved trap is possible.
201 *
202 * @param pVM VM handle.
203 */
204TRPMDECL(void) TRPMRestoreTrap(PVM pVM);
205
206/**
207 * Forward trap or interrupt to the guest's handler
208 *
209 *
210 * @returns VBox status code.
211 * or does not return at all (when the trap is actually forwarded)
212 *
213 * @param pVM The VM to operate on.
214 * @param pRegFrame Pointer to the register frame for the trap.
215 * @param iGate Trap or interrupt gate number
216 * @param opsize Instruction size (only relevant for software interrupts)
217 * @param enmError TRPM_TRAP_HAS_ERRORCODE or TRPM_TRAP_NO_ERRORCODE.
218 * @param enmType TRPM event type
219 * @internal
220 */
221TRPMDECL(int) TRPMForwardTrap(PVM pVM, PCPUMCTXCORE pRegFrame, uint32_t iGate, uint32_t opsize, TRPMERRORCODE enmError, TRPMEVENT enmType);
222
223/**
224 * Raises a cpu exception which doesn't take an error code.
225 *
226 * This function may or may not dispatch the exception before returning.
227 *
228 * @returns VBox status code fit for scheduling.
229 * @retval VINF_EM_RAW_GUEST_TRAP if the exception was left pending.
230 * @retval VINF_TRPM_XCPT_DISPATCHED if the exception was raised and dispatched for raw-mode execution.
231 * @retval VINF_EM_RESCHEDULE_REM if the exception was dispatched and cannot be executed in raw-mode.
232 *
233 * @param pVM The VM handle.
234 * @param pCtxCore The CPU context core.
235 * @param enmXcpt The exception.
236 */
237TRPMDECL(int) TRPMRaiseXcpt(PVM pVM, PCPUMCTXCORE pCtxCore, X86XCPT enmXcpt);
238
239/**
240 * Raises a cpu exception with an errorcode.
241 *
242 * This function may or may not dispatch the exception before returning.
243 *
244 * @returns VBox status code fit for scheduling.
245 * @retval VINF_EM_RAW_GUEST_TRAP if the exception was left pending.
246 * @retval VINF_TRPM_XCPT_DISPATCHED if the exception was raised and dispatched for raw-mode execution.
247 * @retval VINF_EM_RESCHEDULE_REM if the exception was dispatched and cannot be executed in raw-mode.
248 *
249 * @param pVM The VM handle.
250 * @param pCtxCore The CPU context core.
251 * @param enmXcpt The exception.
252 * @param uErr The error code.
253 */
254TRPMDECL(int) TRPMRaiseXcptErr(PVM pVM, PCPUMCTXCORE pCtxCore, X86XCPT enmXcpt, uint32_t uErr);
255
256/**
257 * Raises a cpu exception with an errorcode and CR2.
258 *
259 * This function may or may not dispatch the exception before returning.
260 *
261 * @returns VBox status code fit for scheduling.
262 * @retval VINF_EM_RAW_GUEST_TRAP if the exception was left pending.
263 * @retval VINF_TRPM_XCPT_DISPATCHED if the exception was raised and dispatched for raw-mode execution.
264 * @retval VINF_EM_RESCHEDULE_REM if the exception was dispatched and cannot be executed in raw-mode.
265 *
266 * @param pVM The VM handle.
267 * @param pCtxCore The CPU context core.
268 * @param enmXcpt The exception.
269 * @param uErr The error code.
270 * @param uCR2 The CR2 value.
271 */
272TRPMDECL(int) TRPMRaiseXcptErrCR2(PVM pVM, PCPUMCTXCORE pCtxCore, X86XCPT enmXcpt, uint32_t uErr, RTGCUINTPTR uCR2);
273
274
275#ifdef IN_RING3
276/** @defgroup grp_trpm_r3 TRPM Host Context Ring 3 API
277 * @ingroup grp_trpm
278 * @{
279 */
280
281/**
282 * Initializes the SELM.
283 *
284 * @returns VBox status code.
285 * @param pVM The VM to operate on.
286 */
287TRPMR3DECL(int) TRPMR3Init(PVM pVM);
288
289/**
290 * Applies relocations to data and code managed by this component.
291 *
292 * This function will be called at init and whenever the VMM need
293 * to relocate itself inside the GC.
294 *
295 * @param pVM The VM handle.
296 * @param offDelta Relocation delta relative to old location.
297 */
298TRPMR3DECL(void) TRPMR3Relocate(PVM pVM, RTGCINTPTR offDelta);
299
300/**
301 * The VM is being reset.
302 *
303 * For the TRPM component this means that any IDT write monitors
304 * needs to be removed, any pending trap cleared, and the IDT reset.
305 *
306 * @param pVM VM handle.
307 */
308TRPMR3DECL(void) TRPMR3Reset(PVM pVM);
309
310/**
311 * Set interrupt gate handler
312 * Used for setting up interrupt gates used for kernel calls.
313 *
314 * @returns VBox status code.
315 * @param pVM The VM to operate on.
316 * @param iTrap Interrupt number.
317 */
318TRPMR3DECL(int) TRPMR3EnableGuestTrapHandler(PVM pVM, unsigned iTrap);
319
320/**
321 * Set guest trap/interrupt gate handler
322 * Used for setting up trap gates used for kernel calls.
323 *
324 * @returns VBox status code.
325 * @param pVM The VM to operate on.
326 * @param iTrap Interrupt/trap number.
327 * @parapm pHandler GC handler pointer
328 */
329TRPMR3DECL(int) TRPMR3SetGuestTrapHandler(PVM pVM, unsigned iTrap, RTGCPTR pHandler);
330
331/**
332 * Get guest trap/interrupt gate handler
333 *
334 * @returns Guest trap handler address or TRPM_INVALID_HANDLER if none installed
335 * @param pVM The VM to operate on.
336 * @param iTrap Interrupt/trap number.
337 */
338TRPMR3DECL(RTGCPTR) TRPMR3GetGuestTrapHandler(PVM pVM, unsigned iTrap);
339
340/**
341 * Disable IDT monitoring and syncing
342 *
343 * @param pVM The VM to operate on.
344 */
345TRPMR3DECL(void) TRPMR3DisableMonitoring(PVM pVM);
346
347/**
348 * Check if gate handlers were updated
349 *
350 * @returns VBox status code.
351 * @param pVM The VM to operate on.
352 */
353TRPMR3DECL(int) TRPMR3SyncIDT(PVM pVM);
354
355/**
356 * Check if address is a gate handler (interrupt/trap/task/anything).
357 *
358 * @returns True is gate handler, false if not.
359 *
360 * @param pVM VM handle.
361 * @param GCPtr GC address to check.
362 */
363TRPMR3DECL(bool) TRPMR3IsGateHandler(PVM pVM, RTGCPTR GCPtr);
364
365/**
366 * Check if address is a gate handler (interrupt or trap).
367 *
368 * @returns gate nr or ~0 is not found
369 *
370 * @param pVM VM handle.
371 * @param GCPtr GC address to check.
372 */
373TRPMR3DECL(uint32_t) TRPMR3QueryGateByHandler(PVM pVM, RTGCPTR GCPtr);
374
375/**
376 * Initializes the SELM.
377 *
378 * @returns VBox status code.
379 * @param pVM The VM to operate on.
380 */
381TRPMR3DECL(int) TRPMR3Term(PVM pVM);
382
383
384/**
385 * Inject event (such as external irq or trap)
386 *
387 * @returns VBox status code.
388 * @param pVM The VM to operate on.
389 * @param enmEvent Trpm event type
390 */
391TRPMR3DECL(int) TRPMR3InjectEvent(PVM pVM, TRPMEVENT enmEvent);
392
393/** @} */
394#endif
395
396
397#ifdef IN_GC
398/** @defgroup grp_trpm_gc The TRPM Guest Context API
399 * @ingroup grp_trpm
400 * @{
401 */
402
403/**
404 * Guest Context temporary trap handler
405 *
406 * @returns VBox status code (appropriate for GC return).
407 * In this context VBOX_SUCCESS means to restart the instruction.
408 * @param pVM VM handle.
409 * @param pRegFrame Trap register frame.
410 */
411typedef DECLCALLBACK(int) FNTRPMGCTRAPHANDLER(PVM pVM, PCPUMCTXCORE pRegFrame);
412/** Pointer to a TRPMGCTRAPHANDLER() function. */
413typedef FNTRPMGCTRAPHANDLER *PFNTRPMGCTRAPHANDLER;
414
415/**
416 * Arms a temporary trap handler for traps in Hypervisor code.
417 *
418 * The operation is similar to a System V signal handler. I.e. when the handler
419 * is called it is first set to default action. So, if you need to handler more
420 * than one trap, you must reinstall the handler.
421 *
422 * To uninstall the temporary handler, call this function with pfnHandler set to NULL.
423 *
424 * @returns VBox status.
425 * @param pVM VM handle.
426 * @param iTrap Trap number to install handler [0..255].
427 * @param pfnHandler Pointer to the handler. Use NULL for uninstalling the handler.
428 */
429TRPMGCDECL(int) TRPMGCSetTempHandler(PVM pVM, unsigned iTrap, PFNTRPMGCTRAPHANDLER pfnHandler);
430
431/**
432 * Return to host context from a hypervisor trap handler.
433 * It will also reset any traps that are pending.
434 *
435 * This function will *never* return.
436 *
437 * @param pVM The VM handle.
438 * @param rc The return code for host context.
439 */
440TRPMGCDECL(void) TRPMGCHyperReturnToHost(PVM pVM, int rc);
441
442/** @} */
443#endif
444
445
446#ifdef IN_RING0
447/** @defgroup grp_trpm_r0 TRPM Host Context Ring 0 API
448 * @ingroup grp_trpm
449 * @{
450 */
451
452/**
453 * Dispatches an interrupt that arrived while we were in the guest context.
454 *
455 * @param pVM The VM handle.
456 * @remark Must be called with interrupts disabled.
457 */
458TRPMR0DECL(void) TRPMR0DispatchHostInterrupt(PVM pVM);
459
460# ifndef VBOX_WITHOUT_IDT_PATCHING
461
462/**
463 * Changes the VMMR0Entry() call frame and stack used by the IDT patch code
464 * so that we'll dispatch an interrupt rather than returning directly to Ring-3
465 * when VMMR0Entry() returns.
466 *
467 * @param pVM Pointer to the VM.
468 * @param pvRet Pointer to the return address of VMMR0Entry() on the stack.
469 */
470TRPMR0DECL(void) TRPMR0SetupInterruptDispatcherFrame(PVM pVM, void *pvRet);
471
472# endif /* !VBOX_WITHOUT_IDT_PATCHING */
473
474/** @} */
475#endif
476
477/** @} */
478__END_DECLS
479
480#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette