VirtualBox

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

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

Increased SSM version

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