VirtualBox

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

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

Lowered maximum number of invalid patch writes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 27.2 KB
Line 
1/* $Id: PATMInternal.h 369 2007-01-26 17:38:59Z 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 50
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 16384
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 HCPTRTYPE(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 /** Global PATM return function (used by iret patches). */
405 RTGCPTR pfnHelperIretGC;
406
407 /** Fake patch record for global functions. */
408 PPATMPATCHREC pGlobalPatchRec;
409
410 /** Pointer to original sysenter handler */
411 RTGCPTR pfnSysEnterGC;
412 /** Pointer to sysenter handler trampoline */
413 RTGCPTR pfnSysEnterPatchGC;
414 /** Sysenter patch index (for stats only) */
415 uint32_t uSysEnterPatchIdx;
416
417 // GC address of fault in monitored page (set by PATMGCMonitorPage, used by PATMR3HandleMonitoredPage)
418 RTGCPTR pvFaultMonitor;
419
420 /* Temporary information for pending MMIO patch. Set in GC or R0 context. */
421 struct
422 {
423 RTGCPHYS GCPhys;
424 RTGCPTR pCachedData;
425 } mmio;
426
427 /* Temporary storage during load/save state */
428 struct
429 {
430 PSSMHANDLE pSSM;
431 uint32_t cPatches;
432 } savedstate;
433
434 STAMCOUNTER StatNrOpcodeRead;
435 STAMCOUNTER StatDisabled;
436 STAMCOUNTER StatUnusable;
437 STAMCOUNTER StatEnabled;
438 STAMCOUNTER StatInstalled;
439 STAMCOUNTER StatInstalledFunctionPatches;
440 STAMCOUNTER StatInstalledTrampoline;
441 STAMCOUNTER StatInstalledJump;
442 STAMCOUNTER StatInt3Callable;
443 STAMCOUNTER StatInt3BlockRun;
444 STAMCOUNTER StatOverwritten;
445 STAMCOUNTER StatFixedConflicts;
446 STAMCOUNTER StatFlushed;
447 STAMCOUNTER StatPageBoundaryCrossed;
448 STAMCOUNTER StatMonitored;
449 STAMPROFILEADV StatHandleTrap;
450 STAMCOUNTER StatSwitchBack;
451 STAMCOUNTER StatSwitchBackFail;
452 STAMCOUNTER StatPATMMemoryUsed;
453 STAMCOUNTER StatDuplicateREQSuccess;
454 STAMCOUNTER StatDuplicateREQFailed;
455 STAMCOUNTER StatDuplicateUseExisting;
456 STAMCOUNTER StatFunctionFound;
457 STAMCOUNTER StatFunctionNotFound;
458 STAMPROFILEADV StatPatchWrite;
459 STAMPROFILEADV StatPatchWriteDetect;
460 STAMCOUNTER StatDirty;
461 STAMCOUNTER StatPushTrap;
462 STAMCOUNTER StatPatchWriteInterpreted;
463 STAMCOUNTER StatPatchWriteInterpretedFailed;
464
465 STAMCOUNTER StatSysEnter;
466 STAMCOUNTER StatSysExit;
467
468 STAMCOUNTER StatInstrDirty;
469 STAMCOUNTER StatInstrDirtyGood;
470 STAMCOUNTER StatInstrDirtyBad;
471
472 STAMCOUNTER StatPatchPageInserted;
473 STAMCOUNTER StatPatchPageRemoved;
474
475 STAMCOUNTER StatGenRet;
476 STAMCOUNTER StatGenRetReused;
477 STAMCOUNTER StatGenJump;
478 STAMCOUNTER StatGenCall;
479 STAMCOUNTER StatGenPopf;
480
481 STAMCOUNTER StatCheckPendingIRQ;
482
483 STAMCOUNTER StatFunctionLookupReplace;
484 STAMCOUNTER StatFunctionLookupInsert;
485 uint32_t StatU32FunctionMaxSlotsUsed;
486} PATM, *PPATM;
487
488
489/**
490 * Execute state save operation.
491 *
492 * @returns VBox status code.
493 * @param pVM VM Handle.
494 * @param pSSM SSM operation handle.
495 */
496DECLCALLBACK(int) patmr3Save(PVM pVM, PSSMHANDLE pSSM);
497
498
499/**
500 * Execute state load operation.
501 *
502 * @returns VBox status code.
503 * @param pVM VM Handle.
504 * @param pSSM SSM operation handle.
505 * @param u32Version Data layout version.
506 */
507DECLCALLBACK(int) patmr3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version);
508
509#ifdef IN_RING3
510RTGCPTR patmPatchGCPtr2GuestGCPtr(PVM pVM, PPATCHINFO pPatch, GCPTRTYPE(uint8_t *) pPatchGC);
511RTGCPTR patmGuestGCPtrToPatchGCPtr(PVM pVM, PPATCHINFO pPatch, GCPTRTYPE(uint8_t*)pInstrGC);
512#endif
513
514/* Add a patch to guest lookup record
515 *
516 * @param pVM The VM to operate on.
517 * @param pPatch Patch structure ptr
518 * @param pPatchInstrHC Guest context pointer to patch block
519 * @param pInstrGC Guest context pointer to privileged instruction
520 * @param enmType Lookup type
521 * @param fDirty Dirty flag
522 *
523 */
524void patmr3AddP2GLookupRecord(PVM pVM, PPATCHINFO pPatch, uint8_t *pPatchInstrHC, RTGCPTR pInstrGC, PATM_LOOKUP_TYPE enmType, bool fDirty=false);
525
526/**
527 * Insert page records for all guest pages that contain instructions that were recompiled for this patch
528 *
529 * @returns VBox status code.
530 * @param pVM The VM to operate on.
531 * @param pPatch Patch record
532 */
533int patmInsertPatchPages(PVM pVM, PPATCHINFO pPatch);
534
535/**
536 * Remove page records for all guest pages that contain instructions that were recompiled for this patch
537 *
538 * @returns VBox status code.
539 * @param pVM The VM to operate on.
540 * @param pPatch Patch record
541 */
542int patmRemovePatchPages(PVM pVM, PPATCHINFO pPatch);
543
544/**
545 * Returns the GC address of the corresponding patch statistics counter
546 *
547 * @returns Stat address
548 * @param pVM The VM to operate on.
549 * @param pPatch Patch structure
550 */
551RTGCPTR patmPatchQueryStatAddress(PVM pVM, PPATCHINFO pPatch);
552
553/**
554 * Remove patch for privileged instruction at specified location
555 *
556 * @returns VBox status code.
557 * @param pVM The VM to operate on.
558 * @param pPatchRec Patch record
559 * @param fForceRemove Remove *all* patches
560 */
561int PATMRemovePatch(PVM pVM, PPATMPATCHREC pPatchRec, bool fForceRemove);
562
563/**
564 * Call for analysing the instructions following the privileged instr. for compliance with our heuristics
565 *
566 * @returns VBox status code.
567 * @param pVM The VM to operate on.
568 * @param pCpu CPU disassembly state
569 * @param pInstrHC Guest context pointer to privileged instruction
570 * @param pCurInstrHC Guest context pointer to current instruction
571 * @param pUserData User pointer
572 *
573 */
574typedef int (VBOXCALL *PFN_PATMR3ANALYSE)(PVM pVM, DISCPUSTATE *pCpu, GCPTRTYPE(uint8_t *) pInstrGC, GCPTRTYPE(uint8_t *) pCurInstrGC, void *pUserData);
575
576/**
577 * Install guest OS specific patch
578 *
579 * @returns VBox status code.
580 * @param pVM The VM to operate on
581 * @param pCpu Disassembly state of instruction.
582 * @param pInstrGC GC Instruction pointer for instruction
583 * @param pInstrHC GC Instruction pointer for instruction
584 * @param pPatchRec Patch structure
585 *
586 */
587int PATMInstallGuestSpecificPatch(PVM pVM, PDISCPUSTATE pCpu, RTGCPTR pInstrGC, uint8_t *pInstrHC, PPATMPATCHREC pPatchRec);
588
589/**
590 * Convert guest context address to host context pointer
591 *
592 * @returns VBox status code.
593 * @param pVM The VM to operate on.
594 * @param pPatch Patch block structure pointer
595 * @param pGCPtr Guest context pointer
596 *
597 * @returns Host context pointer or NULL in case of an error
598 *
599 */
600HCPTRTYPE(uint8_t *) PATMGCVirtToHCVirt(PVM pVM, PPATCHINFO pPatch, GCPTRTYPE(uint8_t *)pGCPtr);
601
602
603/**
604 * Check if the instruction is patched as a duplicated function
605 *
606 * @returns patch record
607 * @param pVM The VM to operate on.
608 * @param pInstrGC Guest context point to the instruction
609 *
610 */
611PATMDECL(PPATMPATCHREC) PATMQueryFunctionPatch(PVM pVM, RTGCPTR pInstrGC);
612
613
614/**
615 * Empty the specified tree (PV tree, MMR3 heap)
616 *
617 * @param pVM The VM to operate on.
618 * @param ppTree Tree to empty
619 */
620void patmEmptyTree(PVM pVM, PPAVLPVNODECORE ppTree);
621
622
623/**
624 * Empty the specified tree (U32 tree, MMR3 heap)
625 *
626 * @param pVM The VM to operate on.
627 * @param ppTree Tree to empty
628 */
629void patmEmptyTreeU32(PVM pVM, PPAVLU32NODECORE ppTree);
630
631
632/**
633 * Return the name of the patched instruction
634 *
635 * @returns instruction name
636 *
637 * @param opcode DIS instruction opcode
638 * @param fPatchFlags Patch flags
639 */
640PATMDECL(char *)patmGetInstructionString(uint32_t opcode, uint32_t fPatchFlags);
641
642
643/**
644 * Read callback for disassembly function; supports reading bytes that cross a page boundary
645 *
646 * @returns VBox status code.
647 * @param pSrc GC source pointer
648 * @param pDest HC destination pointer
649 * @param size Number of bytes to read
650 * @param dwUserdata Callback specific user data (pCpu)
651 *
652 */
653int32_t patmReadBytes(RTHCUINTPTR pSrc, uint8_t *pDest, uint32_t size, RTHCUINTPTR dwUserdata);
654
655
656#ifndef IN_GC
657
658#define PATMREAD_RAWCODE 1 /* read code as-is */
659#define PATMREAD_ORGCODE 2 /* read original guest opcode bytes; not the patched bytes */
660
661/*
662 * Private structure used during disassembly
663 */
664typedef struct
665{
666 PVM pVM;
667 PPATCHINFO pPatchInfo;
668 HCPTRTYPE(uint8_t *) pInstrHC;
669 RTGCPTR pInstrGC;
670 uint32_t fReadFlags;
671} PATMDISASM, *PPATMDISASM;
672
673inline bool PATMR3DISInstr(PVM pVM, PPATCHINFO pPatch, DISCPUSTATE *pCpu, RTGCPTR InstrGC,
674 uint8_t *InstrHC, uint32_t *pOpsize, char *pszOutput,
675 uint32_t fReadFlags = PATMREAD_ORGCODE)
676{
677 PATMDISASM disinfo;
678 disinfo.pVM = pVM;
679 disinfo.pPatchInfo = pPatch;
680 disinfo.pInstrHC = InstrHC;
681 disinfo.pInstrGC = InstrGC;
682 disinfo.fReadFlags = fReadFlags;
683 (pCpu)->pfnReadBytes = patmReadBytes;
684 (pCpu)->dwUserData[0] = (RTHCUINTPTR)&disinfo;
685 return DISInstr(pCpu, InstrGC, 0, pOpsize, pszOutput);
686}
687#endif /* !IN_GC */
688
689__BEGIN_DECLS
690/**
691 * #PF Virtual Handler callback for Guest access a page monitored by PATM
692 *
693 * @returns VBox status code (appropritate for trap handling and GC return).
694 * @param pVM VM Handle.
695 * @param uErrorCode CPU Error code.
696 * @param pRegFrame Trap register frame.
697 * @param pvFault The fault address (cr2).
698 * @param pvRange The base address of the handled virtual range.
699 * @param offRange The offset of the access into this range.
700 * (If it's a EIP range this's the EIP, if not it's pvFault.)
701 */
702PATMGCDECL(int) PATMGCMonitorPage(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, void *pvFault, void *pvRange, uintptr_t offRange);
703
704/**
705 * Find patch for privileged instruction at specified location
706 *
707 * @returns Patch structure pointer if found; else NULL
708 * @param pVM The VM to operate on.
709 * @param pInstr Guest context point to instruction that might lie within 5 bytes of an existing patch jump
710 * @param fIncludeHints Include hinted patches or not
711 *
712 */
713PPATCHINFO PATMFindActivePatchByEntrypoint(PVM pVM, RTGCPTR pInstrGC, bool fIncludeHints=false);
714
715/**
716 * Patch cli/sti pushf/popf instruction block at specified location
717 *
718 * @returns VBox status code.
719 * @param pVM The VM to operate on.
720 * @param pInstrGC Guest context point to privileged instruction
721 * @param pInstrHC Host context point to privileged instruction
722 * @param uOpcode Instruction opcodee
723 * @param uOpSize Size of starting instruction
724 * @param pPatchRec Patch record
725 *
726 * @note returns failure if patching is not allowed or possible
727 *
728 */
729PATMR3DECL(int) PATMR3PatchBlock(PVM pVM, RTGCPTR pInstrGC, HCPTRTYPE(uint8_t *) pInstrHC,
730 uint32_t uOpcode, uint32_t uOpSize, PPATMPATCHREC pPatchRec);
731
732
733/**
734 * Replace an instruction with a breakpoint (0xCC), that is handled dynamically in the guest context.
735 *
736 * @returns VBox status code.
737 * @param pVM The VM to operate on.
738 * @param pInstrGC Guest context point to privileged instruction
739 * @param pInstrHC Host context point to privileged instruction
740 * @param pCpu Disassembly CPU structure ptr
741 * @param pPatch Patch record
742 *
743 * @note returns failure if patching is not allowed or possible
744 *
745 */
746PATMR3DECL(int) PATMR3PatchInstrInt3(PVM pVM, RTGCPTR pInstrGC, HCPTRTYPE(uint8_t *) pInstrHC, DISCPUSTATE *pCpu, PPATCHINFO pPatch);
747
748/**
749 * Mark patch as dirty
750 *
751 * @returns VBox status code.
752 * @param pVM The VM to operate on.
753 * @param pPatch Patch record
754 *
755 * @note returns failure if patching is not allowed or possible
756 *
757 */
758PATMR3DECL(int) PATMR3MarkDirtyPatch(PVM pVM, PPATCHINFO pPatch);
759
760/**
761 * Calculate the branch destination
762 *
763 * @returns branch destination or 0 if failed
764 * @param pCpu Disassembly state of instruction.
765 * @param pBranchInstrGC GC pointer of branch instruction
766 */
767inline RTGCPTR PATMResolveBranch(PDISCPUSTATE pCpu, RTGCPTR pBranchInstrGC)
768{
769 uint32_t disp;
770 if (pCpu->param1.flags & USE_IMMEDIATE8_REL)
771 {
772 disp = (int32_t)(char)pCpu->param1.parval;
773 }
774 else
775 if (pCpu->param1.flags & USE_IMMEDIATE16_REL)
776 {
777 disp = (int32_t)(uint16_t)pCpu->param1.parval;
778 }
779 else
780 if (pCpu->param1.flags & USE_IMMEDIATE32_REL)
781 {
782 disp = (int32_t)pCpu->param1.parval;
783 }
784 else
785 {
786 Log(("We don't support far jumps here!! (%08X)\n", pCpu->param1.flags));
787 return 0;
788 }
789#ifdef IN_GC
790 return (RTGCPTR)((uint8_t *)pBranchInstrGC + pCpu->opsize + disp);
791#else
792 return pBranchInstrGC + pCpu->opsize + disp;
793#endif
794}
795
796__END_DECLS
797
798#ifdef DEBUG
799int patmr3DisasmCallback(PVM pVM, DISCPUSTATE *pCpu, GCPTRTYPE(uint8_t *) pInstrGC, GCPTRTYPE(uint8_t *) pCurInstrGC, void *pUserData);
800int patmr3DisasmCodeStream(PVM pVM, GCPTRTYPE(uint8_t *) pInstrGC, GCPTRTYPE(uint8_t *) pCurInstrGC, PFN_PATMR3ANALYSE pfnPATMR3Analyse, void *pUserData);
801#endif
802
803#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