VirtualBox

source: vbox/trunk/src/VBox/VMM/PATM/PATMInternal.h@ 17610

Last change on this file since 17610 was 13832, checked in by vboxsync, 16 years ago

IN_GC -> IN_RC.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 28.7 KB
Line 
1/* $Id: PATMInternal.h 13832 2008-11-05 02:01:12Z vboxsync $ */
2/** @file
3 * PATM - Internal header file.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22#ifndef ___PATMInternal_h
23#define ___PATMInternal_h
24
25#include <VBox/cdefs.h>
26#include <VBox/types.h>
27#include <VBox/patm.h>
28#include <VBox/stam.h>
29#include <VBox/dis.h>
30#include <VBox/pgm.h>
31#include <iprt/avl.h>
32#include <iprt/param.h>
33#include <VBox/log.h>
34
35
36
37#ifdef PATM_WITH_NEW_SSM
38# define PATM_SSM_VERSION 54
39/* Last version to use SSMR3Put/GetMem */
40# define PATM_SSM_VERSION_GETPUTMEM 53
41#else
42# define PATM_SSM_VERSION 54
43# define PATM_SSM_VERSION_VER16 53
44#endif
45
46/* Enable for call patching. */
47#define PATM_ENABLE_CALL
48#define PATCH_MEMORY_SIZE (2*1024*1024)
49#define MAX_PATCH_SIZE (1024*4)
50
51/*
52 * Internal patch type flags (starts at RT_BIT(11))
53 */
54
55#define PATMFL_CHECK_SIZE RT_BIT_64(11)
56#define PATMFL_FOUND_PATCHEND RT_BIT_64(12)
57#define PATMFL_SINGLE_INSTRUCTION RT_BIT_64(13)
58#define PATMFL_SYSENTER_XP RT_BIT_64(14)
59#define PATMFL_JUMP_CONFLICT RT_BIT_64(15)
60#define PATMFL_READ_ORIGINAL_BYTES RT_BIT_64(16) /** opcode might have already been patched */
61#define PATMFL_INT3_REPLACEMENT RT_BIT_64(17)
62#define PATMFL_SUPPORT_CALLS RT_BIT_64(18)
63#define PATMFL_SUPPORT_INDIRECT_CALLS RT_BIT_64(19)
64#define PATMFL_IDTHANDLER_WITHOUT_ENTRYPOINT RT_BIT_64(20) /** internal flag to avoid duplicate entrypoints */
65#define PATMFL_INHIBIT_IRQS RT_BIT_64(21) /** temporary internal flag */
66#define PATMFL_GENERATE_JUMPTOGUEST RT_BIT_64(22) /** temporary internal flag */
67#define PATMFL_RECOMPILE_NEXT RT_BIT_64(23) /** for recompilation of the next instruction */
68#define PATMFL_CODE_MONITORED RT_BIT_64(24) /** code pages of guest monitored for self-modifying code. */
69#define PATMFL_CALLABLE_AS_FUNCTION RT_BIT_64(25) /** cli and pushf blocks can be used as callable functions. */
70#define PATMFL_GLOBAL_FUNCTIONS RT_BIT_64(26) /** fake patch for global patm functions. */
71#define PATMFL_TRAMPOLINE RT_BIT_64(27) /** trampoline patch that clears PATM_INTERRUPTFLAG and jumps to patch destination */
72#define PATMFL_GENERATE_SETPIF RT_BIT_64(28) /** generate set PIF for the next instruction */
73#define PATMFL_INSTR_HINT RT_BIT_64(29) /** Generate patch, but don't activate it. */
74#define PATMFL_PATCHED_GUEST_CODE RT_BIT_64(30) /** Patched guest code. */
75#define PATMFL_MUST_INSTALL_PATCHJMP RT_BIT_64(31) /** Need to patch guest code in order to activate patch. */
76#define PATMFL_INT3_REPLACEMENT_BLOCK RT_BIT_64(32) /** int 3 replacement block */
77#define PATMFL_EXTERNAL_JUMP_INSIDE RT_BIT_64(33) /** A trampoline patch was created that jumps to an instruction in the patch block */
78#define PATMFL_CODE_REFERENCED RT_BIT_64(34) /** patch block referenced (called, jumped to) by another patch. */
79
80#define SIZEOF_NEARJUMP8 2 //opcode byte + 1 byte relative offset
81#define SIZEOF_NEARJUMP16 3 //opcode byte + 2 byte relative offset
82#define SIZEOF_NEARJUMP32 5 //opcode byte + 4 byte relative offset
83#define SIZEOF_NEAR_COND_JUMP32 6 //0xF + opcode byte + 4 byte relative offset
84
85#define MAX_INSTR_SIZE 16
86
87//Patch states
88#define PATCH_REFUSED 1
89#define PATCH_DISABLED 2
90#define PATCH_ENABLED 4
91#define PATCH_UNUSABLE 8
92#define PATCH_DIRTY 16
93#define PATCH_DISABLE_PENDING 32
94
95
96#define MAX_PATCH_TRAPS 4
97#define PATM_MAX_CALL_DEPTH 32
98/* Maximum nr of writes before a patch is marked dirty. (disabled) */
99#define PATM_MAX_CODE_WRITES 32
100/* Maximum nr of invalid writes before a patch is disabled. */
101#define PATM_MAX_INVALID_WRITES 16384
102
103#define FIXUP_ABSOLUTE 0
104#define FIXUP_REL_JMPTOPATCH 1
105#define FIXUP_REL_JMPTOGUEST 2
106
107#define PATM_ILLEGAL_DESTINATION 0xDEADBEEF
108
109/** Size of the instruction that's used for requests from patch code (currently only call) */
110#define PATM_ILLEGAL_INSTR_SIZE 2
111
112
113/** No statistics counter index allocated just yet */
114#define PATM_STAT_INDEX_NONE (uint32_t)-1
115/** Dummy counter to handle overflows */
116#define PATM_STAT_INDEX_DUMMY 0
117#define PATM_STAT_INDEX_IS_VALID(a) (a != PATM_STAT_INDEX_DUMMY && a != PATM_STAT_INDEX_NONE)
118
119#ifdef VBOX_WITH_STATISTICS
120#define PATM_STAT_RUN_INC(pPatch) \
121 if (PATM_STAT_INDEX_IS_VALID((pPatch)->uPatchIdx)) \
122 CTXSUFF(pVM->patm.s.pStats)[(pPatch)->uPatchIdx].u32A++;
123#define PATM_STAT_FAULT_INC(pPatch) \
124 if (PATM_STAT_INDEX_IS_VALID((pPatch)->uPatchIdx)) \
125 CTXSUFF(pVM->patm.s.pStats)[(pPatch)->uPatchIdx].u32B++;
126#else
127#define PATM_STAT_RUN_INC(pPatch) do { } while (0)
128#define PATM_STAT_FAULT_INC(pPatch) do { } while (0)
129#endif
130
131/** Maximum number of stat counters. */
132#define PATM_STAT_MAX_COUNTERS 1024
133/** Size of memory allocated for patch statistics. */
134#define PATM_STAT_MEMSIZE (PATM_STAT_MAX_COUNTERS*sizeof(STAMRATIOU32))
135
136
137typedef struct
138{
139 /** The key is a HC virtual address. */
140 AVLPVNODECORE Core;
141
142 uint32_t uType;
143 R3PTRTYPE(uint8_t *) pRelocPos;
144 RTRCPTR pSource;
145 RTRCPTR pDest;
146} RELOCREC, *PRELOCREC;
147
148typedef struct
149{
150 R3PTRTYPE(uint8_t *) pPatchLocStartHC;
151 R3PTRTYPE(uint8_t *) pPatchLocEndHC;
152 RCPTRTYPE(uint8_t *) pGuestLoc;
153 uint32_t opsize;
154} P2GLOOKUPREC, *PP2GLOOKUPREC;
155
156typedef struct
157{
158 /** The key is a pointer to a JUMPREC structure. */
159 AVLPVNODECORE Core;
160
161 R3PTRTYPE(uint8_t *) pJumpHC;
162 RCPTRTYPE(uint8_t *) pTargetGC;
163 uint32_t offDispl;
164 uint32_t opcode;
165} JUMPREC, *PJUMPREC;
166
167/**
168 * Patch to guest lookup type (single or both direction)
169 */
170typedef enum
171{
172 PATM_LOOKUP_PATCH2GUEST, /* patch to guest */
173 PATM_LOOKUP_BOTHDIR /* guest to patch + patch to guest */
174} PATM_LOOKUP_TYPE;
175
176/**
177 * Patch to guest address lookup record
178 */
179typedef struct RECPATCHTOGUEST
180{
181 /** The key is an offset inside the patch memory block. */
182 AVLU32NODECORE Core;
183
184 RTRCPTR pOrgInstrGC;
185 PATM_LOOKUP_TYPE enmType;
186 bool fDirty;
187 bool fJumpTarget;
188 uint8_t u8DirtyOpcode; /* original opcode before writing 0xCC there to mark it dirty */
189} RECPATCHTOGUEST, *PRECPATCHTOGUEST;
190
191/**
192 * Guest to patch address lookup record
193 */
194typedef struct RECGUESTTOPATCH
195{
196 /** The key is a GC virtual address. */
197 AVLU32NODECORE Core;
198
199 /** Patch offset (relative to PATM::pPatchMemGC / PATM::pPatchMemHC). */
200 uint32_t PatchOffset;
201} RECGUESTTOPATCH, *PRECGUESTTOPATCH;
202
203/**
204 * Temporary information used in ring 3 only; no need to waste memory in the patch record itself.
205 */
206typedef struct
207{
208 /* Temporary tree for storing the addresses of illegal instructions. */
209 R3PTRTYPE(PAVLPVNODECORE) IllegalInstrTree;
210 uint32_t nrIllegalInstr;
211
212 int32_t nrJumps;
213 uint32_t nrRetInstr;
214
215 /* Temporary tree of encountered jumps. (debug only) */
216 R3PTRTYPE(PAVLPVNODECORE) DisasmJumpTree;
217
218 int32_t nrCalls;
219
220 /** Last original guest instruction pointer; used for disassmebly log. */
221 RTRCPTR pLastDisasmInstrGC;
222
223 /** Keeping track of multiple ret instructions. */
224 RTRCPTR pPatchRetInstrGC;
225 uint32_t uPatchRetParam1;
226} PATCHINFOTEMP, *PPATCHINFOTEMP;
227
228typedef struct _PATCHINFO
229{
230 uint32_t uState;
231 uint32_t uOldState;
232 DISCPUMODE uOpMode;
233
234 RCPTRTYPE(uint8_t *) pPrivInstrGC; //GC pointer of privileged instruction
235 R3PTRTYPE(uint8_t *) pPrivInstrHC; //HC pointer of privileged instruction
236 uint8_t aPrivInstr[MAX_INSTR_SIZE];
237 uint32_t cbPrivInstr;
238 uint32_t opcode; //opcode for priv instr (OP_*)
239 uint32_t cbPatchJump; //patch jump size
240
241 /* Only valid for PATMFL_JUMP_CONFLICT patches */
242 RTRCPTR pPatchJumpDestGC;
243
244 RTGCUINTPTR32 pPatchBlockOffset;
245 uint32_t cbPatchBlockSize;
246 uint32_t uCurPatchOffset;
247#if HC_ARCH_BITS == 64
248 uint32_t Alignment0; /**< Align flags correctly. */
249#endif
250
251 uint64_t flags;
252
253 /**
254 * Lowest and highest patched GC instruction address. To optimize searches.
255 */
256 RTRCPTR pInstrGCLowest;
257 RTRCPTR pInstrGCHighest;
258
259 /* Tree of fixup records for the patch. */
260 R3PTRTYPE(PAVLPVNODECORE) FixupTree;
261 int32_t nrFixups;
262
263 /* Tree of jumps inside the generated patch code. */
264 int32_t nrJumpRecs;
265 R3PTRTYPE(PAVLPVNODECORE) JumpTree;
266
267 /**
268 * Lookup trees for determining the corresponding guest address of an
269 * instruction in the patch block.
270 */
271 R3PTRTYPE(PAVLU32NODECORE) Patch2GuestAddrTree;
272 R3PTRTYPE(PAVLU32NODECORE) Guest2PatchAddrTree;
273 uint32_t nrPatch2GuestRecs;
274#if HC_ARCH_BITS == 64
275 uint32_t Alignment1;
276#endif
277
278 // Cache record for PATMGCVirtToHCVirt
279 P2GLOOKUPREC cacheRec;
280
281 /* Temporary information during patch creation. Don't waste hypervisor memory for this. */
282 R3PTRTYPE(PPATCHINFOTEMP) pTempInfo;
283
284 /* Count the number of writes to the corresponding guest code. */
285 uint32_t cCodeWrites;
286
287 /* Count the number of invalid writes to pages monitored for the patch. */
288 //some statistics to determine if we should keep this patch activated
289 uint32_t cTraps;
290
291 uint32_t cInvalidWrites;
292
293 // Index into the uPatchRun and uPatchTrap arrays (0..MAX_PATCHES-1)
294 uint32_t uPatchIdx;
295
296 /* First opcode byte, that's overwritten when a patch is marked dirty. */
297 uint8_t bDirtyOpcode;
298 uint8_t Alignment2[7]; /**< Align the structure size on a 8-byte boundrary. */
299} PATCHINFO, *PPATCHINFO;
300
301#define PATCHCODE_PTR_GC(pPatch) (RTRCPTR) (pVM->patm.s.pPatchMemGC + (pPatch)->pPatchBlockOffset)
302#define PATCHCODE_PTR_HC(pPatch) (uint8_t *)(pVM->patm.s.pPatchMemHC + (pPatch)->pPatchBlockOffset)
303
304/**
305 * Lookup record for patches
306 */
307typedef struct PATMPATCHREC
308{
309 /** The key is a GC virtual address. */
310 AVLOU32NODECORE Core;
311 /** The key is a patch offset. */
312 AVLOU32NODECORE CoreOffset;
313
314 PATCHINFO patch;
315} PATMPATCHREC, *PPATMPATCHREC;
316
317/** Increment for allocating room for pointer array */
318#define PATMPATCHPAGE_PREALLOC_INCREMENT 16
319
320/**
321 * Lookup record for patch pages
322 */
323typedef struct PATMPATCHPAGE
324{
325 /** The key is a GC virtual address. */
326 AVLOU32NODECORE Core;
327 /** Region to monitor. */
328 RTRCPTR pLowestAddrGC;
329 RTRCPTR pHighestAddrGC;
330 /** Number of patches for this page. */
331 uint32_t cCount;
332 /** Maximum nr of pointers in the array. */
333 uint32_t cMaxPatches;
334 /** Array of patch pointers for this page. */
335 R3PTRTYPE(PPATCHINFO *) aPatch;
336} PATMPATCHPAGE, *PPATMPATCHPAGE;
337
338#define PATM_PATCHREC_FROM_COREOFFSET(a) (PPATMPATCHREC)((uintptr_t)a - RT_OFFSETOF(PATMPATCHREC, CoreOffset))
339#define PATM_PATCHREC_FROM_PATCHINFO(a) (PPATMPATCHREC)((uintptr_t)a - RT_OFFSETOF(PATMPATCHREC, patch))
340
341typedef struct PATMTREES
342{
343 /**
344 * AVL tree with all patches (active or disabled) sorted by guest instruction address
345 */
346 AVLOU32TREE PatchTree;
347
348 /**
349 * AVL tree with all patches sorted by patch address (offset actually)
350 */
351 AVLOU32TREE PatchTreeByPatchAddr;
352
353 /**
354 * AVL tree with all pages which were (partly) patched
355 */
356 AVLOU32TREE PatchTreeByPage;
357
358 uint32_t align[1];
359} PATMTREES, *PPATMTREES;
360
361/**
362 * PATM VM Instance data.
363 * Changes to this must checked against the padding of the patm union in VM!
364 */
365typedef struct PATM
366{
367 /** Offset to the VM structure.
368 * See PATM2VM(). */
369 RTINT offVM;
370
371 RCPTRTYPE(uint8_t *) pPatchMemGC;
372 R3PTRTYPE(uint8_t *) pPatchMemHC;
373 uint32_t cbPatchMem;
374 uint32_t offPatchMem;
375 bool fOutOfMemory;
376
377 int32_t deltaReloc;
378
379 /* GC PATM state pointers */
380 R3PTRTYPE(PPATMGCSTATE) pGCStateHC;
381 RCPTRTYPE(PPATMGCSTATE) pGCStateGC;
382
383 /** PATM stack page for call instruction execution. (2 parts: one for our private stack and one to store the original return address */
384 RCPTRTYPE(RTRCPTR *) pGCStackGC;
385 R3PTRTYPE(RTRCPTR *) pGCStackHC;
386
387 /** GC pointer to CPUMCTX structure. */
388 RCPTRTYPE(PCPUMCTX) pCPUMCtxGC;
389
390 /* GC statistics pointers */
391 RCPTRTYPE(PSTAMRATIOU32) pStatsGC;
392 R3PTRTYPE(PSTAMRATIOU32) pStatsHC;
393
394 /* Current free index value (uPatchRun/uPatchTrap arrays). */
395 uint32_t uCurrentPatchIdx;
396
397 /* Temporary counter for patch installation call depth. (in order not to go on forever) */
398 uint32_t ulCallDepth;
399
400 /** Number of page lookup records. */
401 uint32_t cPageRecords;
402
403 /**
404 * Lowest and highest patched GC instruction addresses. To optimize searches.
405 */
406 RTRCPTR pPatchedInstrGCLowest;
407 RTRCPTR pPatchedInstrGCHighest;
408
409 /** Pointer to the patch tree for instructions replaced by 'int 3'. */
410 RCPTRTYPE(PPATMTREES) PatchLookupTreeGC;
411 R3PTRTYPE(PPATMTREES) PatchLookupTreeHC;
412
413 /** Global PATM lookup and call function (used by call patches). */
414 RTRCPTR pfnHelperCallGC;
415 /** Global PATM return function (used by ret patches). */
416 RTRCPTR pfnHelperRetGC;
417 /** Global PATM jump function (used by indirect jmp patches). */
418 RTRCPTR pfnHelperJumpGC;
419 /** Global PATM return function (used by iret patches). */
420 RTRCPTR pfnHelperIretGC;
421
422 /** Fake patch record for global functions. */
423 R3PTRTYPE(PPATMPATCHREC) pGlobalPatchRec;
424
425 /** Pointer to original sysenter handler */
426 RTRCPTR pfnSysEnterGC;
427 /** Pointer to sysenter handler trampoline */
428 RTRCPTR pfnSysEnterPatchGC;
429 /** Sysenter patch index (for stats only) */
430 uint32_t uSysEnterPatchIdx;
431
432 // GC address of fault in monitored page (set by PATMGCMonitorPage, used by PATMR3HandleMonitoredPage)
433 RTRCPTR pvFaultMonitor;
434
435 /* Temporary information for pending MMIO patch. Set in GC or R0 context. */
436 struct
437 {
438 RTGCPHYS GCPhys;
439 RTRCPTR pCachedData;
440 RTRCPTR Alignment0; /**< Align the structure size on a 8-byte boundrary. */
441 } mmio;
442
443 /* Temporary storage during load/save state */
444 struct
445 {
446 R3PTRTYPE(PSSMHANDLE) pSSM;
447 uint32_t cPatches;
448#if HC_ARCH_BITS == 64
449 uint32_t Alignment0; /**< Align the structure size on a 8-byte boundrary. */
450#endif
451 } savedstate;
452
453 STAMCOUNTER StatNrOpcodeRead;
454 STAMCOUNTER StatDisabled;
455 STAMCOUNTER StatUnusable;
456 STAMCOUNTER StatEnabled;
457 STAMCOUNTER StatInstalled;
458 STAMCOUNTER StatInstalledFunctionPatches;
459 STAMCOUNTER StatInstalledTrampoline;
460 STAMCOUNTER StatInstalledJump;
461 STAMCOUNTER StatInt3Callable;
462 STAMCOUNTER StatInt3BlockRun;
463 STAMCOUNTER StatOverwritten;
464 STAMCOUNTER StatFixedConflicts;
465 STAMCOUNTER StatFlushed;
466 STAMCOUNTER StatPageBoundaryCrossed;
467 STAMCOUNTER StatMonitored;
468 STAMPROFILEADV StatHandleTrap;
469 STAMCOUNTER StatSwitchBack;
470 STAMCOUNTER StatSwitchBackFail;
471 STAMCOUNTER StatPATMMemoryUsed;
472 STAMCOUNTER StatDuplicateREQSuccess;
473 STAMCOUNTER StatDuplicateREQFailed;
474 STAMCOUNTER StatDuplicateUseExisting;
475 STAMCOUNTER StatFunctionFound;
476 STAMCOUNTER StatFunctionNotFound;
477 STAMPROFILEADV StatPatchWrite;
478 STAMPROFILEADV StatPatchWriteDetect;
479 STAMCOUNTER StatDirty;
480 STAMCOUNTER StatPushTrap;
481 STAMCOUNTER StatPatchWriteInterpreted;
482 STAMCOUNTER StatPatchWriteInterpretedFailed;
483
484 STAMCOUNTER StatSysEnter;
485 STAMCOUNTER StatSysExit;
486 STAMCOUNTER StatEmulIret;
487 STAMCOUNTER StatEmulIretFailed;
488
489 STAMCOUNTER StatInstrDirty;
490 STAMCOUNTER StatInstrDirtyGood;
491 STAMCOUNTER StatInstrDirtyBad;
492
493 STAMCOUNTER StatPatchPageInserted;
494 STAMCOUNTER StatPatchPageRemoved;
495
496 STAMCOUNTER StatPatchRefreshSuccess;
497 STAMCOUNTER StatPatchRefreshFailed;
498
499 STAMCOUNTER StatGenRet;
500 STAMCOUNTER StatGenRetReused;
501 STAMCOUNTER StatGenJump;
502 STAMCOUNTER StatGenCall;
503 STAMCOUNTER StatGenPopf;
504
505 STAMCOUNTER StatCheckPendingIRQ;
506
507 STAMCOUNTER StatFunctionLookupReplace;
508 STAMCOUNTER StatFunctionLookupInsert;
509 uint32_t StatU32FunctionMaxSlotsUsed;
510 uint32_t Alignment0; /**< Align the structure size on a 8-byte boundrary. */
511} PATM, *PPATM;
512
513
514/**
515 * Execute state save operation.
516 *
517 * @returns VBox status code.
518 * @param pVM VM Handle.
519 * @param pSSM SSM operation handle.
520 */
521DECLCALLBACK(int) patmr3Save(PVM pVM, PSSMHANDLE pSSM);
522
523DECLCALLBACK(int) patmVirtPageHandler(PVM pVM, RTGCPTR GCPtr, void *pvPtr, void *pvBuf, size_t cbBuf, PGMACCESSTYPE enmAccessType, void *pvUser);
524
525/**
526 * Execute state load operation.
527 *
528 * @returns VBox status code.
529 * @param pVM VM Handle.
530 * @param pSSM SSM operation handle.
531 * @param u32Version Data layout version.
532 */
533DECLCALLBACK(int) patmr3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version);
534
535#ifdef IN_RING3
536RTRCPTR patmPatchGCPtr2GuestGCPtr(PVM pVM, PPATCHINFO pPatch, RCPTRTYPE(uint8_t *) pPatchGC);
537RTRCPTR patmGuestGCPtrToPatchGCPtr(PVM pVM, PPATCHINFO pPatch, RCPTRTYPE(uint8_t*) pInstrGC);
538RTRCPTR patmGuestGCPtrToClosestPatchGCPtr(PVM pVM, PPATCHINFO pPatch, RCPTRTYPE(uint8_t*) pInstrGC);
539#endif
540
541/* Add a patch to guest lookup record
542 *
543 * @param pVM The VM to operate on.
544 * @param pPatch Patch structure ptr
545 * @param pPatchInstrHC Guest context pointer to patch block
546 * @param pInstrGC Guest context pointer to privileged instruction
547 * @param enmType Lookup type
548 * @param fDirty Dirty flag
549 *
550 */
551void patmr3AddP2GLookupRecord(PVM pVM, PPATCHINFO pPatch, uint8_t *pPatchInstrHC, RTRCPTR pInstrGC, PATM_LOOKUP_TYPE enmType, bool fDirty=false);
552
553/**
554 * Insert page records for all guest pages that contain instructions that were recompiled for this patch
555 *
556 * @returns VBox status code.
557 * @param pVM The VM to operate on.
558 * @param pPatch Patch record
559 */
560int patmInsertPatchPages(PVM pVM, PPATCHINFO pPatch);
561
562/**
563 * Remove page records for all guest pages that contain instructions that were recompiled for this patch
564 *
565 * @returns VBox status code.
566 * @param pVM The VM to operate on.
567 * @param pPatch Patch record
568 */
569int patmRemovePatchPages(PVM pVM, PPATCHINFO pPatch);
570
571/**
572 * Returns the GC address of the corresponding patch statistics counter
573 *
574 * @returns Stat address
575 * @param pVM The VM to operate on.
576 * @param pPatch Patch structure
577 */
578RTRCPTR patmPatchQueryStatAddress(PVM pVM, PPATCHINFO pPatch);
579
580/**
581 * Remove patch for privileged instruction at specified location
582 *
583 * @returns VBox status code.
584 * @param pVM The VM to operate on.
585 * @param pPatchRec Patch record
586 * @param fForceRemove Remove *all* patches
587 */
588int PATMRemovePatch(PVM pVM, PPATMPATCHREC pPatchRec, bool fForceRemove);
589
590/**
591 * Call for analysing the instructions following the privileged instr. for compliance with our heuristics
592 *
593 * @returns VBox status code.
594 * @param pVM The VM to operate on.
595 * @param pCpu CPU disassembly state
596 * @param pInstrHC Guest context pointer to privileged instruction
597 * @param pCurInstrHC Guest context pointer to current instruction
598 * @param pUserData User pointer
599 *
600 */
601typedef int (VBOXCALL *PFN_PATMR3ANALYSE)(PVM pVM, DISCPUSTATE *pCpu, RCPTRTYPE(uint8_t *) pInstrGC, RCPTRTYPE(uint8_t *) pCurInstrGC, void *pUserData);
602
603/**
604 * Install guest OS specific patch
605 *
606 * @returns VBox status code.
607 * @param pVM The VM to operate on
608 * @param pCpu Disassembly state of instruction.
609 * @param pInstrGC GC Instruction pointer for instruction
610 * @param pInstrHC GC Instruction pointer for instruction
611 * @param pPatchRec Patch structure
612 *
613 */
614int PATMInstallGuestSpecificPatch(PVM pVM, PDISCPUSTATE pCpu, RTRCPTR pInstrGC, uint8_t *pInstrHC, PPATMPATCHREC pPatchRec);
615
616/**
617 * Convert guest context address to host context pointer
618 *
619 * @returns VBox status code.
620 * @param pVM The VM to operate on.
621 * @param pPatch Patch block structure pointer
622 * @param pGCPtr Guest context pointer
623 *
624 * @returns Host context pointer or NULL in case of an error
625 *
626 */
627R3PTRTYPE(uint8_t *) PATMGCVirtToHCVirt(PVM pVM, PPATCHINFO pPatch, RCPTRTYPE(uint8_t *) pGCPtr);
628
629
630/**
631 * Check if the instruction is patched as a duplicated function
632 *
633 * @returns patch record
634 * @param pVM The VM to operate on.
635 * @param pInstrGC Guest context point to the instruction
636 *
637 */
638VMMDECL(PPATMPATCHREC) PATMQueryFunctionPatch(PVM pVM, RTRCPTR pInstrGC);
639
640
641/**
642 * Empty the specified tree (PV tree, MMR3 heap)
643 *
644 * @param pVM The VM to operate on.
645 * @param ppTree Tree to empty
646 */
647void patmEmptyTree(PVM pVM, PPAVLPVNODECORE ppTree);
648
649
650/**
651 * Empty the specified tree (U32 tree, MMR3 heap)
652 *
653 * @param pVM The VM to operate on.
654 * @param ppTree Tree to empty
655 */
656void patmEmptyTreeU32(PVM pVM, PPAVLU32NODECORE ppTree);
657
658
659/**
660 * Return the name of the patched instruction
661 *
662 * @returns instruction name
663 *
664 * @param opcode DIS instruction opcode
665 * @param fPatchFlags Patch flags
666 */
667VMMDECL(const char *) patmGetInstructionString(uint32_t opcode, uint32_t fPatchFlags);
668
669
670/**
671 * Read callback for disassembly function; supports reading bytes that cross a page boundary
672 *
673 * @returns VBox status code.
674 * @param pSrc GC source pointer
675 * @param pDest HC destination pointer
676 * @param size Number of bytes to read
677 * @param pvUserdata Callback specific user data (pCpu)
678 *
679 */
680int patmReadBytes(RTUINTPTR pSrc, uint8_t *pDest, unsigned size, void *pvUserdata);
681
682
683#ifndef IN_RC
684
685#define PATMREAD_RAWCODE 1 /* read code as-is */
686#define PATMREAD_ORGCODE 2 /* read original guest opcode bytes; not the patched bytes */
687#define PATMREAD_NOCHECK 4 /* don't check for patch conflicts */
688
689/*
690 * Private structure used during disassembly
691 */
692typedef struct
693{
694 PVM pVM;
695 PPATCHINFO pPatchInfo;
696 R3PTRTYPE(uint8_t *) pInstrHC;
697 RTRCPTR pInstrGC;
698 uint32_t fReadFlags;
699} PATMDISASM, *PPATMDISASM;
700
701inline bool PATMR3DISInstr(PVM pVM, PPATCHINFO pPatch, DISCPUSTATE *pCpu, RTRCPTR InstrGC,
702 uint8_t *InstrHC, uint32_t *pOpsize, char *pszOutput,
703 uint32_t fReadFlags = PATMREAD_ORGCODE)
704{
705 PATMDISASM disinfo;
706 disinfo.pVM = pVM;
707 disinfo.pPatchInfo = pPatch;
708 disinfo.pInstrHC = InstrHC;
709 disinfo.pInstrGC = InstrGC;
710 disinfo.fReadFlags = fReadFlags;
711 (pCpu)->pfnReadBytes = patmReadBytes;
712 (pCpu)->apvUserData[0] = &disinfo;
713 return RT_SUCCESS(DISInstr(pCpu, InstrGC, 0, pOpsize, pszOutput));
714}
715#endif /* !IN_RC */
716
717__BEGIN_DECLS
718/**
719 * #PF Virtual Handler callback for Guest access a page monitored by PATM
720 *
721 * @returns VBox status code (appropritate for trap handling and GC return).
722 * @param pVM VM Handle.
723 * @param uErrorCode CPU Error code.
724 * @param pRegFrame Trap register frame.
725 * @param pvFault The fault address (cr2).
726 * @param pvRange The base address of the handled virtual range.
727 * @param offRange The offset of the access into this range.
728 * (If it's a EIP range this's the EIP, if not it's pvFault.)
729 */
730VMMRCDECL(int) PATMGCMonitorPage(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPTR pvRange, uintptr_t offRange);
731
732/**
733 * Find patch for privileged instruction at specified location
734 *
735 * @returns Patch structure pointer if found; else NULL
736 * @param pVM The VM to operate on.
737 * @param pInstr Guest context point to instruction that might lie within 5 bytes of an existing patch jump
738 * @param fIncludeHints Include hinted patches or not
739 *
740 */
741PPATCHINFO PATMFindActivePatchByEntrypoint(PVM pVM, RTRCPTR pInstrGC, bool fIncludeHints=false);
742
743/**
744 * Patch cli/sti pushf/popf instruction block at specified location
745 *
746 * @returns VBox status code.
747 * @param pVM The VM to operate on.
748 * @param pInstrGC Guest context point to privileged instruction
749 * @param pInstrHC Host context point to privileged instruction
750 * @param uOpcode Instruction opcodee
751 * @param uOpSize Size of starting instruction
752 * @param pPatchRec Patch record
753 *
754 * @note returns failure if patching is not allowed or possible
755 *
756 */
757VMMR3DECL(int) PATMR3PatchBlock(PVM pVM, RTRCPTR pInstrGC, R3PTRTYPE(uint8_t *) pInstrHC,
758 uint32_t uOpcode, uint32_t uOpSize, PPATMPATCHREC pPatchRec);
759
760
761/**
762 * Replace an instruction with a breakpoint (0xCC), that is handled dynamically in the guest context.
763 *
764 * @returns VBox status code.
765 * @param pVM The VM to operate on.
766 * @param pInstrGC Guest context point to privileged instruction
767 * @param pInstrHC Host context point to privileged instruction
768 * @param pCpu Disassembly CPU structure ptr
769 * @param pPatch Patch record
770 *
771 * @note returns failure if patching is not allowed or possible
772 *
773 */
774VMMR3DECL(int) PATMR3PatchInstrInt3(PVM pVM, RTRCPTR pInstrGC, R3PTRTYPE(uint8_t *) pInstrHC, DISCPUSTATE *pCpu, PPATCHINFO pPatch);
775
776/**
777 * Mark patch as dirty
778 *
779 * @returns VBox status code.
780 * @param pVM The VM to operate on.
781 * @param pPatch Patch record
782 *
783 * @note returns failure if patching is not allowed or possible
784 *
785 */
786VMMR3DECL(int) PATMR3MarkDirtyPatch(PVM pVM, PPATCHINFO pPatch);
787
788/**
789 * Calculate the branch destination
790 *
791 * @returns branch destination or 0 if failed
792 * @param pCpu Disassembly state of instruction.
793 * @param pBranchInstrGC GC pointer of branch instruction
794 */
795inline RTRCPTR PATMResolveBranch(PDISCPUSTATE pCpu, RTRCPTR pBranchInstrGC)
796{
797 uint32_t disp;
798 if (pCpu->param1.flags & USE_IMMEDIATE8_REL)
799 {
800 disp = (int32_t)(char)pCpu->param1.parval;
801 }
802 else
803 if (pCpu->param1.flags & USE_IMMEDIATE16_REL)
804 {
805 disp = (int32_t)(uint16_t)pCpu->param1.parval;
806 }
807 else
808 if (pCpu->param1.flags & USE_IMMEDIATE32_REL)
809 {
810 disp = (int32_t)pCpu->param1.parval;
811 }
812 else
813 {
814 Log(("We don't support far jumps here!! (%08X)\n", pCpu->param1.flags));
815 return 0;
816 }
817#ifdef IN_RC
818 return (RTRCPTR)((uint8_t *)pBranchInstrGC + pCpu->opsize + disp);
819#else
820 return pBranchInstrGC + pCpu->opsize + disp;
821#endif
822}
823
824__END_DECLS
825
826#ifdef DEBUG
827int patmr3DisasmCallback(PVM pVM, DISCPUSTATE *pCpu, RCPTRTYPE(uint8_t *) pInstrGC, RCPTRTYPE(uint8_t *) pCurInstrGC, void *pUserData);
828int patmr3DisasmCodeStream(PVM pVM, RCPTRTYPE(uint8_t *) pInstrGC, RCPTRTYPE(uint8_t *) pCurInstrGC, PFN_PATMR3ANALYSE pfnPATMR3Analyse, void *pUserData);
829#endif
830
831#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