VirtualBox

source: vbox/trunk/include/VBox/patm.h@ 6000

Last change on this file since 6000 was 5999, checked in by vboxsync, 17 years ago

The Giant CDDL Dual-License Header Change.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.9 KB
Line 
1/** @file
2 * PATM - Dynamic Guest OS Patching Manager
3 */
4
5/*
6 * Copyright (C) 2006-2007 innotek 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 (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___VBox_patm_h
27#define ___VBox_patm_h
28
29#include <VBox/cdefs.h>
30#include <VBox/types.h>
31#include <VBox/cpum.h>
32#include <VBox/dis.h>
33
34
35__BEGIN_DECLS
36
37/** @defgroup grp_patm The Patch Manager API
38 * @{
39 */
40#define MAX_PATCHES 512
41
42/**
43 * Flags for specifying the type of patch to install with PATMR3InstallPatch
44 * @{
45 */
46#define PATMFL_CODE32 RT_BIT_64(0)
47#define PATMFL_INTHANDLER RT_BIT_64(1)
48#define PATMFL_SYSENTER RT_BIT_64(2)
49#define PATMFL_GUEST_SPECIFIC RT_BIT_64(3)
50#define PATMFL_USER_MODE RT_BIT_64(4)
51#define PATMFL_IDTHANDLER RT_BIT_64(5)
52#define PATMFL_TRAPHANDLER RT_BIT_64(6)
53#define PATMFL_DUPLICATE_FUNCTION RT_BIT_64(7)
54#define PATMFL_REPLACE_FUNCTION_CALL RT_BIT_64(8)
55#define PATMFL_TRAPHANDLER_WITH_ERRORCODE RT_BIT_64(9)
56#define PATMFL_INTHANDLER_WITH_ERRORCODE (PATMFL_TRAPHANDLER_WITH_ERRORCODE)
57#define PATMFL_MMIO_ACCESS RT_BIT_64(10)
58/* no more room -> change PATMInternal.h if more is needed!! */
59
60/*
61 * Flags above 1024 are reserved for internal use!
62 */
63/** @} */
64
65/** Enable to activate sysenter emulation in GC. */
66/* #define PATM_EMULATE_SYSENTER */
67
68/**
69 * Maximum number of cached VGA writes
70 */
71#define MAX_VGA_WRITE_CACHE 64
72
73typedef struct PATMGCSTATE
74{
75 // Virtual Flags register (IF + more later on)
76 uint32_t uVMFlags;
77
78 /* Pending PATM actions (internal use only) */
79 uint32_t uPendingAction;
80
81 // Records the number of times all patches are called (indicating how many exceptions we managed to avoid)
82 uint32_t uPatchCalls;
83 // Scratchpad dword
84 uint32_t uScratch;
85 // Debugging info
86 uint32_t uIretEFlags, uIretCS, uIretEIP;
87
88 /* PATM stack pointer */
89 uint32_t Psp;
90
91 /* PATM interrupt flag */
92 uint32_t fPIF;
93 /* PATM inhibit irq address (used by sti) */
94 RTGCPTR GCPtrInhibitInterrupts;
95
96 /* Scratch room for call patch */
97 RTGCPTR GCCallPatchTargetAddr;
98 RTGCPTR GCCallReturnAddr;
99
100 /* Temporary storage for guest registers. */
101 struct
102 {
103 uint32_t uEAX;
104 uint32_t uECX;
105 uint32_t uEDI;
106 uint32_t eFlags;
107 uint32_t uFlags;
108 } Restore;
109
110} PATMGCSTATE, *PPATMGCSTATE;
111
112typedef struct PATMTRAPREC
113{
114 // pointer to original guest code instruction (for emulation)
115 RTGCPTR pNewEIP;
116 // pointer to the next guest code instruction
117 RTGCPTR pNextInstr;
118 //pointer to the corresponding next instruction in the patch block
119 RTGCPTR pNextPatchInstr;
120} PATMTRAPREC, *PPATMTRAPREC;
121
122
123/**
124 * Translation state (currently patch to GC ptr)
125 */
126typedef enum
127{
128 PATMTRANS_FAILED,
129 PATMTRANS_SAFE, /* Safe translation */
130 PATMTRANS_PATCHSTART, /* Instruction starts a patch block */
131 PATMTRANS_OVERWRITTEN, /* Instruction overwritten by patchjump */
132 PATMTRANS_INHIBITIRQ /* Instruction must be executed due to instruction fusing */
133} PATMTRANSSTATE;
134
135/**
136 * Load virtualized flags.
137 *
138 * This function is called from CPUMRawEnter(). It doesn't have to update the
139 * IF and IOPL eflags bits, the caller will enforce those to set and 0 repectively.
140 *
141 * @param pVM VM handle.
142 * @param pCtxCore The cpu context core.
143 * @see pg_raw
144 */
145PATMDECL(void) PATMRawEnter(PVM pVM, PCPUMCTXCORE pCtxCore);
146
147/**
148 * Restores virtualized flags.
149 *
150 * This function is called from CPUMRawLeave(). It will update the eflags register.
151 *
152 * @param pVM VM handle.
153 * @param pCtxCore The cpu context core.
154 * @param rawRC Raw mode return code
155 * @see @ref pg_raw
156 */
157PATMDECL(void) PATMRawLeave(PVM pVM, PCPUMCTXCORE pCtxCore, int rawRC);
158
159/**
160 * Get the EFLAGS.
161 * This is a worker for CPUMRawGetEFlags().
162 *
163 * @returns The eflags.
164 * @param pVM The VM handle.
165 * @param pCtxCore The context core.
166 */
167PATMDECL(uint32_t) PATMRawGetEFlags(PVM pVM, PCCPUMCTXCORE pCtxCore);
168
169/**
170 * Updates the EFLAGS.
171 * This is a worker for CPUMRawSetEFlags().
172 *
173 * @param pVM The VM handle.
174 * @param pCtxCore The context core.
175 * @param efl The new EFLAGS value.
176 */
177PATMDECL(void) PATMRawSetEFlags(PVM pVM, PCPUMCTXCORE pCtxCore, uint32_t efl);
178
179/**
180 * Returns the guest context pointer of the GC context structure
181 *
182 * @returns VBox status code.
183 * @param pVM The VM to operate on.
184 */
185PATMDECL(GCPTRTYPE(PPATMGCSTATE)) PATMQueryGCState(PVM pVM);
186
187/**
188 * Checks whether the GC address is part of our patch region
189 *
190 * @returns true -> yes, false -> no
191 * @param pVM The VM to operate on.
192 * @param pAddr Guest context address
193 */
194PATMDECL(bool) PATMIsPatchGCAddr(PVM pVM, RTGCPTR pAddr);
195
196/**
197 * Check if we must use raw mode (patch code being executed or marked safe for IF=0)
198 *
199 * @param pVM VM handle.
200 * @param pAddrGC Guest context address
201 */
202PATMDECL(bool) PATMShouldUseRawMode(PVM pVM, RTGCPTR pAddrGC);
203
204/**
205 * Query PATM state (enabled/disabled)
206 *
207 * @returns 0 - disabled, 1 - enabled
208 * @param pVM The VM to operate on.
209 */
210#define PATMIsEnabled(pVM) (pVM->fPATMEnabled)
211
212/**
213 * Set parameters for pending MMIO patch operation
214 *
215 * @returns VBox status code.
216 * @param pDevIns Device instance.
217 * @param GCPhys MMIO physical address
218 * @param pCachedData GC pointer to cached data
219 */
220PATMDECL(int) PATMSetMMIOPatchInfo(PVM pVM, RTGCPHYS GCPhys, RTGCPTR pCachedData);
221
222
223/**
224 * Adds branch pair to the lookup cache of the particular branch instruction
225 *
226 * @returns VBox status
227 * @param pVM The VM to operate on.
228 * @param pJumpTableGC Pointer to branch instruction lookup cache
229 * @param pBranchTarget Original branch target
230 * @param pRelBranchPatch Relative duplicated function address
231 */
232PATMDECL(int) PATMAddBranchToLookupCache(PVM pVM, RTGCPTR pJumpTableGC, RTGCPTR pBranchTarget, RTGCUINTPTR pRelBranchPatch);
233
234
235/**
236 * Checks if the int 3 was caused by a patched instruction
237 *
238 * @returns VBox status
239 *
240 * @param pVM The VM handle.
241 * @param pCtxCore The relevant core context.
242 */
243PATMDECL(int) PATMHandleInt3PatchTrap(PVM pVM, PCPUMCTXCORE pRegFrame);
244
245/**
246 * Checks if the int 3 was caused by a patched instruction
247 *
248 * @returns VBox status
249 *
250 * @param pVM The VM handle.
251 * @param pInstrGC Instruction pointer
252 * @param pOpcode Original instruction opcode (out, optional)
253 * @param pSize Original instruction size (out, optional)
254 */
255PATMDECL(bool) PATMIsInt3Patch(PVM pVM, RTGCPTR pInstrGC, uint32_t *pOpcode, uint32_t *pSize);
256
257
258/**
259 * Checks if the interrupt flag is enabled or not.
260 *
261 * @returns true if it's enabled.
262 * @returns false if it's diabled.
263 *
264 * @param pVM The VM handle.
265 */
266PATMDECL(bool) PATMAreInterruptsEnabled(PVM pVM);
267
268/**
269 * Checks if the interrupt flag is enabled or not.
270 *
271 * @returns true if it's enabled.
272 * @returns false if it's diabled.
273 *
274 * @param pVM The VM handle.
275 * @param pCtxCore CPU context
276 */
277PATMDECL(bool) PATMAreInterruptsEnabledByCtxCore(PVM pVM, PCPUMCTXCORE pCtxCore);
278
279#ifdef PATM_EMULATE_SYSENTER
280/**
281 * Emulate sysenter, sysexit and syscall instructions
282 *
283 * @returns VBox status
284 *
285 * @param pVM The VM handle.
286 * @param pCtxCore The relevant core context.
287 * @param pCpu Disassembly context
288 */
289PATMDECL(int) PATMSysCall(PVM pVM, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pCpu);
290#endif
291
292#ifdef IN_GC
293/** @defgroup grp_patm_gc The Patch Manager API
294 * @ingroup grp_patm
295 * @{
296 */
297
298/**
299 * Checks if the write is located on a page with was patched before.
300 * (if so, then we are not allowed to turn on r/w)
301 *
302 * @returns VBox status
303 * @param pVM The VM to operate on.
304 * @param pRegFrame CPU context
305 * @param GCPtr GC pointer to write address
306 * @param cbWrite Nr of bytes to write
307 *
308 */
309PATMGCDECL(int) PATMGCHandleWriteToPatchPage(PVM pVM, PCPUMCTXCORE pRegFrame, RTGCPTR GCPtr, uint32_t cbWrite);
310
311/**
312 * Checks if the illegal instruction was caused by a patched instruction
313 *
314 * @returns VBox status
315 *
316 * @param pVM The VM handle.
317 * @param pCtxCore The relevant core context.
318 */
319PATMDECL(int) PATMGCHandleIllegalInstrTrap(PVM pVM, PCPUMCTXCORE pRegFrame);
320
321/** @} */
322
323#endif
324
325#ifdef IN_RING3
326/** @defgroup grp_patm_r3 The Patch Manager API
327 * @ingroup grp_patm
328 * @{
329 */
330
331/**
332 * Query PATM state (enabled/disabled)
333 *
334 * @returns 0 - disabled, 1 - enabled
335 * @param pVM The VM to operate on.
336 */
337PATMR3DECL(int) PATMR3IsEnabled(PVM pVM);
338
339/**
340 * Initializes the PATM.
341 *
342 * @returns VBox status code.
343 * @param pVM The VM to operate on.
344 */
345PATMR3DECL(int) PATMR3Init(PVM pVM);
346
347/**
348 * Finalizes HMA page attributes.
349 *
350 * @returns VBox status code.
351 * @param pVM The VM handle.
352 */
353PATMR3DECL(int) PATMR3InitFinalize(PVM pVM);
354
355/**
356 * Applies relocations to data and code managed by this
357 * component. This function will be called at init and
358 * whenever the VMM need to relocate it self inside the GC.
359 *
360 * The PATM will update the addresses used by the switcher.
361 *
362 * @param pVM The VM.
363 */
364PATMR3DECL(void) PATMR3Relocate(PVM pVM);
365
366/**
367 * Terminates the PATM.
368 *
369 * Termination means cleaning up and freeing all resources,
370 * the VM it self is at this point powered off or suspended.
371 *
372 * @returns VBox status code.
373 * @param pVM The VM to operate on.
374 */
375PATMR3DECL(int) PATMR3Term(PVM pVM);
376
377/**
378 * PATM reset callback.
379 *
380 * @returns VBox status code.
381 * @param pVM The VM which is reset.
382 */
383PATMR3DECL(int) PATMR3Reset(PVM pVM);
384
385/**
386 * Returns the host context pointer and size of the patch memory block
387 *
388 * @returns VBox status code.
389 * @param pVM The VM to operate on.
390 * @param pcb Size of the patch memory block
391 */
392PATMR3DECL(void *) PATMR3QueryPatchMemHC(PVM pVM, uint32_t *pcb);
393
394/**
395 * Returns the guest context pointer and size of the patch memory block
396 *
397 * @returns VBox status code.
398 * @param pVM The VM to operate on.
399 * @param pcb Size of the patch memory block
400 */
401PATMR3DECL(RTGCPTR) PATMR3QueryPatchMemGC(PVM pVM, uint32_t *pcb);
402
403/**
404 * Checks whether the GC address is inside a generated patch jump
405 *
406 * @returns true -> yes, false -> no
407 * @param pVM The VM to operate on.
408 * @param pAddr Guest context address
409 * @param pPatchAddr Guest context patch address (if true)
410 */
411PATMR3DECL(bool) PATMR3IsInsidePatchJump(PVM pVM, RTGCPTR pAddr, PRTGCPTR pPatchAddr);
412
413
414/**
415 * Returns the GC pointer of the patch for the specified GC address
416 *
417 * @returns VBox status code.
418 * @param pVM The VM to operate on.
419 * @param pAddrGC Guest context address
420 */
421PATMR3DECL(RTGCPTR) PATMR3QueryPatchGCPtr(PVM pVM, RTGCPTR pAddrGC);
422
423/**
424 * Checks whether the HC address is part of our patch region
425 *
426 * @returns VBox status code.
427 * @param pVM The VM to operate on.
428 * @param pAddrGC Guest context address
429 */
430PATMR3DECL(bool) PATMR3IsPatchHCAddr(PVM pVM, R3PTRTYPE(uint8_t *) pAddrHC);
431
432/**
433 * Convert a GC patch block pointer to a HC patch pointer
434 *
435 * @returns HC pointer or NULL if it's not a GC patch pointer
436 * @param pVM The VM to operate on.
437 * @param pAddrGC GC pointer
438 */
439PATMR3DECL(R3PTRTYPE(void *)) PATMR3GCPtrToHCPtr(PVM pVM, RTGCPTR pAddrGC);
440
441
442/**
443 * Returns the host context pointer and size of the GC context structure
444 *
445 * @returns VBox status code.
446 * @param pVM The VM to operate on.
447 */
448PATMR3DECL(PPATMGCSTATE) PATMR3QueryGCStateHC(PVM pVM);
449
450/**
451 * Handle trap inside patch code
452 *
453 * @returns VBox status code.
454 * @param pVM The VM to operate on.
455 * @param pCtx CPU context
456 * @param pEip GC pointer of trapping instruction
457 * @param pNewEip GC pointer to new instruction
458 */
459PATMR3DECL(int) PATMR3HandleTrap(PVM pVM, PCPUMCTX pCtx, RTGCPTR pEip, RTGCPTR *ppNewEip);
460
461/**
462 * Handle page-fault in monitored page
463 *
464 * @returns VBox status code.
465 * @param pVM The VM to operate on.
466 */
467PATMR3DECL(int) PATMR3HandleMonitoredPage(PVM pVM);
468
469/**
470 * Notifies PATM about a (potential) write to code that has been patched.
471 *
472 * @returns VBox status code.
473 * @param pVM The VM to operate on.
474 * @param GCPtr GC pointer to write address
475 * @param cbWrite Nr of bytes to write
476 *
477 */
478PATMR3DECL(int) PATMR3PatchWrite(PVM pVM, RTGCPTR GCPtr, uint32_t cbWrite);
479
480/**
481 * Notify PATM of a page flush
482 *
483 * @returns VBox status code
484 * @param pVM The VM to operate on.
485 * @param addr GC address of the page to flush
486 */
487PATMR3DECL(int) PATMR3FlushPage(PVM pVM, RTGCPTR addr);
488
489/**
490 * Allows or disallow patching of privileged instructions executed by the guest OS
491 *
492 * @returns VBox status code.
493 * @param pVM The VM to operate on.
494 * @param fAllowPatching Allow/disallow patching
495 */
496PATMR3DECL(int) PATMR3AllowPatching(PVM pVM, uint32_t fAllowPatching);
497
498/**
499 * Patch privileged instruction at specified location
500 *
501 * @returns VBox status code.
502 * @param pVM The VM to operate on.
503 * @param pInstr Guest context point to privileged instruction (0:32 flat address)
504 * @param flags Patch flags
505 *
506 * @note returns failure if patching is not allowed or possible
507 */
508PATMR3DECL(int) PATMR3InstallPatch(PVM pVM, RTGCPTR pInstrGC, uint64_t flags);
509
510/**
511 * Gives hint to PATM about supervisor guest instructions
512 *
513 * @returns VBox status code.
514 * @param pVM The VM to operate on.
515 * @param pInstr Guest context point to privileged instruction
516 * @param flags Patch flags
517 */
518PATMR3DECL(int) PATMR3AddHint(PVM pVM, RTGCPTR pInstrGC, uint32_t flags);
519
520/**
521 * Patch branch target function for call/jump at specified location.
522 * (in responds to a VINF_PATM_DUPLICATE_FUNCTION GC exit reason)
523 *
524 * @returns VBox status code.
525 * @param pVM The VM to operate on.
526 * @param pCtx Guest context
527 *
528 */
529PATMR3DECL(int) PATMR3DuplicateFunctionRequest(PVM pVM, PCPUMCTX pCtx);
530
531/**
532 * Query the corresponding GC instruction pointer from a pointer inside the patch block itself
533 *
534 * @returns original GC instruction pointer or 0 if not found
535 * @param pVM The VM to operate on.
536 * @param pPatchGC GC address in patch block
537 * @param pEnmState State of the translated address (out)
538 *
539 */
540PATMR3DECL(RTGCPTR) PATMR3PatchToGCPtr(PVM pVM, RTGCPTR pPatchGC, PATMTRANSSTATE *pEnmState);
541
542/**
543 * Converts Guest code GC ptr to Patch code GC ptr (if found)
544 *
545 * @returns corresponding GC pointer in patch block
546 * @param pVM The VM to operate on.
547 * @param pInstrGC Guest context pointer to privileged instruction
548 *
549 */
550PATMR3DECL(RTGCPTR) PATMR3GuestGCPtrToPatchGCPtr(PVM pVM, GCPTRTYPE(uint8_t*) pInstrGC);
551
552/**
553 * Query the opcode of the original code that was overwritten by the 5 bytes patch jump
554 *
555 * @returns VBox status code.
556 * @param pVM The VM to operate on.
557 * @param pInstrGC GC address of instr
558 * @param pByte opcode byte pointer (OUT)
559 * @returns VBOX error code
560 *
561 */
562PATMR3DECL(int) PATMR3QueryOpcode(PVM pVM, RTGCPTR pInstrGC, uint8_t *pByte);
563
564/**
565 * Disable patch for privileged instruction at specified location
566 *
567 * @returns VBox status code.
568 * @param pVM The VM to operate on.
569 * @param pInstr Guest context point to privileged instruction
570 *
571 * @note returns failure if patching is not allowed or possible
572 *
573 */
574PATMR3DECL(int) PATMR3DisablePatch(PVM pVM, RTGCPTR pInstrGC);
575
576
577/**
578 * Enable patch for privileged instruction at specified location
579 *
580 * @returns VBox status code.
581 * @param pVM The VM to operate on.
582 * @param pInstr Guest context point to privileged instruction
583 *
584 * @note returns failure if patching is not allowed or possible
585 *
586 */
587PATMR3DECL(int) PATMR3EnablePatch(PVM pVM, RTGCPTR pInstrGC);
588
589
590/**
591 * Remove patch for privileged instruction at specified location
592 *
593 * @returns VBox status code.
594 * @param pVM The VM to operate on.
595 * @param pInstr Guest context point to privileged instruction
596 *
597 * @note returns failure if patching is not allowed or possible
598 *
599 */
600PATMR3DECL(int) PATMR3RemovePatch(PVM pVM, RTGCPTR pInstrGC);
601
602
603/**
604 * Detects it the specified address falls within a 5 byte jump generated for an active patch.
605 * If so, this patch is permanently disabled.
606 *
607 * @param pVM The VM to operate on.
608 * @param pInstrGC Guest context pointer to instruction
609 * @param pConflictGC Guest context pointer to check
610 */
611PATMR3DECL(int) PATMR3DetectConflict(PVM pVM, RTGCPTR pInstrGC, RTGCPTR pConflictGC);
612
613
614/**
615 * Checks if the instructions at the specified address has been patched already.
616 *
617 * @returns boolean, patched or not
618 * @param pVM The VM to operate on.
619 * @param pInstrGC Guest context pointer to instruction
620 */
621PATMR3DECL(bool) PATMR3HasBeenPatched(PVM pVM, RTGCPTR pInstrGC);
622
623
624/**
625 * Install Linux 2.6 spinlock patch
626 *
627 * @returns VBox status code.
628 * @param pVM The VM to operate on
629 * @param pCallAcquireSpinlockGC GC pointer of call instruction
630 * @param cbAcquireSpinlockCall Instruction size
631 *
632 */
633PATMR3DECL(int) PATMInstallSpinlockPatch(PVM pVM, RTGCPTR pCallAcquireSpinlockGC, uint32_t cbAcquireSpinlockCall);
634
635
636/**
637 * Check if supplied call target is the Linux 2.6 spinlock acquire function
638 *
639 * @returns boolean
640 * @param pVM The VM to operate on
641 * @param pCallAcquireSpinlockGC Call target GC address
642 *
643 */
644PATMR3DECL(bool) PATMIsSpinlockAcquire(PVM pVM, RTGCPTR pCallTargetGC);
645
646/**
647 * Check if supplied call target is the Linux 2.6 spinlock release function
648 *
649 * @returns boolean
650 * @param pVM The VM to operate on
651 * @param pCallTargetGC Call target GC address
652 *
653 */
654PATMR3DECL(bool) PATMIsSpinlockRelease(PVM pVM, RTGCPTR pCallTargetGC);
655
656/**
657 * Check if supplied call target is the Linux 2.6 spinlock release function (patched equivalent)
658 *
659 * @returns boolean
660 * @param pVM The VM to operate on
661 * @param pCallTargetGC Call target GC address
662 *
663 */
664PATMR3DECL(bool) PATMIsSpinlockReleasePatch(PVM pVM, RTGCPTR pCallTargetGC);
665
666/** @} */
667#endif
668
669
670/** @} */
671__END_DECLS
672
673
674#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