Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

272 lines
9.0 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Function entry cache.
  4. //
  5. // Copyright (C) Microsoft Corporation, 2000-2001.
  6. //
  7. //----------------------------------------------------------------------------
  8. #ifndef __FECACHE_HPP__
  9. #define __FECACHE_HPP__
  10. typedef IMAGE_ALPHA_RUNTIME_FUNCTION_ENTRY IMAGE_ARM_RUNTIME_FUNCTION_ENTRY;
  11. typedef IMAGE_ARM_RUNTIME_FUNCTION_ENTRY *PIMAGE_ARM_RUNTIME_FUNCTION_ENTRY;
  12. union FeCacheData
  13. {
  14. // 32-bit Alpha entry is only temporary storage;
  15. // cache entries are always converted to 64-bit
  16. // to allow common code.
  17. IMAGE_IA64_RUNTIME_FUNCTION_ENTRY Ia64;
  18. _IMAGE_RUNTIME_FUNCTION_ENTRY Amd64;
  19. IMAGE_ARM_RUNTIME_FUNCTION_ENTRY Arm;
  20. UCHAR Data[1];
  21. };
  22. struct FeCacheEntry
  23. {
  24. FeCacheData Data;
  25. // Generic values translated from the raw data.
  26. ULONG RelBegin;
  27. ULONG RelEnd;
  28. ULONG64 Address;
  29. HANDLE Process;
  30. ULONG64 ModuleBase;
  31. #if DBG
  32. PSTR Description;
  33. #endif
  34. };
  35. #if DBG
  36. #define FE_DEBUG(x) if (tlsvar(DebugFunctionEntries)) dbPrint##x
  37. #define FE_ShowRuntimeFunctionIa64(x) ShowRuntimeFunctionIa64##x
  38. #define FE_SET_DESC(Ent, Desc) ((Ent)->Description = (Desc))
  39. void
  40. ShowRuntimeFunctionEntryIa64(
  41. FeCacheEntry* FunctionEntry,
  42. PSTR Label
  43. );
  44. #else
  45. #define FE_DEBUG(x)
  46. #define FE_ShowRuntimeFunctionIa64(x)
  47. #define FE_SET_DESC(Ent, Desc)
  48. #endif // #if DBG
  49. //----------------------------------------------------------------------------
  50. //
  51. // FunctionEntryCache.
  52. //
  53. //----------------------------------------------------------------------------
  54. class FunctionEntryCache
  55. {
  56. public:
  57. FunctionEntryCache(ULONG ImageDataSize, ULONG CacheDataSize,
  58. ULONG Machine);
  59. ~FunctionEntryCache(void);
  60. BOOL Initialize(ULONG MaxEntries, ULONG ReplaceAt);
  61. FeCacheEntry* Find(
  62. HANDLE Process,
  63. ULONG64 CodeOffset,
  64. PREAD_PROCESS_MEMORY_ROUTINE64 ReadMemory,
  65. PGET_MODULE_BASE_ROUTINE64 GetModuleBase,
  66. PFUNCTION_TABLE_ACCESS_ROUTINE64 GetFunctionEntry
  67. );
  68. FeCacheEntry* FindDirect(
  69. HANDLE Process,
  70. ULONG64 CodeOffset,
  71. PREAD_PROCESS_MEMORY_ROUTINE64 ReadMemory,
  72. PGET_MODULE_BASE_ROUTINE64 GetModuleBase,
  73. PFUNCTION_TABLE_ACCESS_ROUTINE64 GetFunctionEntry
  74. );
  75. FeCacheEntry* ReadImage(
  76. HANDLE Process,
  77. ULONG64 Address,
  78. PREAD_PROCESS_MEMORY_ROUTINE64 ReadMemory,
  79. PGET_MODULE_BASE_ROUTINE64 GetModuleBase
  80. );
  81. void InvalidateProcessOrModule(HANDLE Process, ULONG64 Base);
  82. protected:
  83. // Total number of entries to store.
  84. ULONG m_MaxEntries;
  85. // When the cache is full entries are overwritten starting
  86. // at the replacement point. Cache hits are moved up
  87. // prior to the replacement point so frequently used
  88. // entries stay unmodified in the front of the cache
  89. // and other entries are stored temporarily at the end.
  90. ULONG m_ReplaceAt;
  91. // Size of raw function entry data in the image.
  92. ULONG m_ImageDataSize;
  93. // Size of raw function entry data in the cache.
  94. // This may be different from the image data size if
  95. // the image data is translated into a different form,
  96. // such as Axp32 keeping cache entries in Axp64 form.
  97. ULONG m_CacheDataSize;
  98. // Machine type.
  99. ULONG m_Machine;
  100. FeCacheEntry* m_Entries;
  101. // Number of entries currently used.
  102. ULONG m_Used;
  103. // Index of next slot to fill.
  104. ULONG m_Next;
  105. // Temporary data area for callback-filled data.
  106. FeCacheEntry m_Temporary;
  107. FeCacheEntry* FillTemporary(HANDLE Process, PVOID RawEntry)
  108. {
  109. // No need to translate as this entry is not part of
  110. // the cache that will be searched.
  111. ZeroMemory(&m_Temporary, sizeof(m_Temporary));
  112. memcpy(&m_Temporary.Data, RawEntry, m_CacheDataSize);
  113. m_Temporary.Process = Process;
  114. return &m_Temporary;
  115. }
  116. virtual void TranslateRawData(FeCacheEntry* Entry) = 0;
  117. virtual void TranslateRvaDataToRawData(PIMGHLP_RVA_FUNCTION_DATA RvaData,
  118. ULONG64 ModuleBase,
  119. FeCacheData* Data) = 0;
  120. // Base implementation just returns the given entry.
  121. virtual FeCacheEntry* SearchForPrimaryEntry(
  122. FeCacheEntry* CacheEntry,
  123. HANDLE Process,
  124. PREAD_PROCESS_MEMORY_ROUTINE64 ReadMemory,
  125. PGET_MODULE_BASE_ROUTINE64 GetModuleBase,
  126. PFUNCTION_TABLE_ACCESS_ROUTINE64 GetFunctionEntry
  127. );
  128. FeCacheEntry* FindStatic(
  129. HANDLE Process,
  130. ULONG64 CodeOffset,
  131. PREAD_PROCESS_MEMORY_ROUTINE64 ReadMemory,
  132. PGET_MODULE_BASE_ROUTINE64 GetModuleBase,
  133. PFUNCTION_TABLE_ACCESS_ROUTINE64 GetFunctionEntry,
  134. PULONG64 ModuleBase
  135. );
  136. FeCacheEntry* Promote(FeCacheEntry* Entry);
  137. ULONG64 FunctionTableBase(
  138. HANDLE Process,
  139. PREAD_PROCESS_MEMORY_ROUTINE64 ReadMemory,
  140. ULONG64 Base,
  141. PULONG Size
  142. );
  143. };
  144. //----------------------------------------------------------------------------
  145. //
  146. // Ia64FunctionEntryCache.
  147. //
  148. //----------------------------------------------------------------------------
  149. class Ia64FunctionEntryCache : public FunctionEntryCache
  150. {
  151. public:
  152. Ia64FunctionEntryCache(void) :
  153. FunctionEntryCache(sizeof(IMAGE_IA64_RUNTIME_FUNCTION_ENTRY),
  154. sizeof(IMAGE_IA64_RUNTIME_FUNCTION_ENTRY),
  155. IMAGE_FILE_MACHINE_IA64)
  156. {
  157. }
  158. protected:
  159. virtual void TranslateRawData(FeCacheEntry* Entry);
  160. virtual void TranslateRvaDataToRawData(PIMGHLP_RVA_FUNCTION_DATA RvaData,
  161. ULONG64 ModuleBase,
  162. FeCacheData* Data);
  163. };
  164. //----------------------------------------------------------------------------
  165. //
  166. // Amd64FunctionEntryCache.
  167. //
  168. //----------------------------------------------------------------------------
  169. class Amd64FunctionEntryCache : public FunctionEntryCache
  170. {
  171. public:
  172. Amd64FunctionEntryCache(void) :
  173. FunctionEntryCache(sizeof(_IMAGE_RUNTIME_FUNCTION_ENTRY),
  174. sizeof(_IMAGE_RUNTIME_FUNCTION_ENTRY),
  175. IMAGE_FILE_MACHINE_AMD64)
  176. {
  177. }
  178. protected:
  179. virtual void TranslateRawData(FeCacheEntry* Entry);
  180. virtual void TranslateRvaDataToRawData(PIMGHLP_RVA_FUNCTION_DATA RvaData,
  181. ULONG64 ModuleBase,
  182. FeCacheData* Data);
  183. };
  184. //----------------------------------------------------------------------------
  185. //
  186. // AlphaFunctionEntryCache.
  187. //
  188. //----------------------------------------------------------------------------
  189. class AlphaFunctionEntryCache : public FunctionEntryCache
  190. {
  191. public:
  192. AlphaFunctionEntryCache(ULONG ImageDataSize, ULONG CacheDataSize,
  193. ULONG Machine) :
  194. FunctionEntryCache(ImageDataSize, CacheDataSize, Machine)
  195. {
  196. }
  197. protected:
  198. virtual void TranslateRvaDataToRawData(PIMGHLP_RVA_FUNCTION_DATA RvaData,
  199. ULONG64 ModuleBase,
  200. FeCacheData* Data);
  201. virtual FeCacheEntry* SearchForPrimaryEntry(
  202. FeCacheEntry* CacheEntry,
  203. HANDLE Process,
  204. PREAD_PROCESS_MEMORY_ROUTINE64 ReadMemory,
  205. PGET_MODULE_BASE_ROUTINE64 GetModuleBase,
  206. PFUNCTION_TABLE_ACCESS_ROUTINE64 GetFunctionEntry
  207. );
  208. };
  209. //----------------------------------------------------------------------------
  210. //
  211. // ArmFunctionEntryCache.
  212. //
  213. //----------------------------------------------------------------------------
  214. class ArmFunctionEntryCache : public FunctionEntryCache
  215. {
  216. public:
  217. ArmFunctionEntryCache(void) :
  218. FunctionEntryCache(sizeof(IMAGE_ARM_RUNTIME_FUNCTION_ENTRY),
  219. sizeof(IMAGE_ARM_RUNTIME_FUNCTION_ENTRY),
  220. IMAGE_FILE_MACHINE_ARM)
  221. {
  222. }
  223. protected:
  224. virtual void TranslateRawData(FeCacheEntry* Entry);
  225. virtual void TranslateRvaDataToRawData(PIMGHLP_RVA_FUNCTION_DATA RvaData,
  226. ULONG64 ModuleBase,
  227. FeCacheData* Data);
  228. };
  229. //----------------------------------------------------------------------------
  230. //
  231. // Functions.
  232. //
  233. //----------------------------------------------------------------------------
  234. FunctionEntryCache* GetFeCache(ULONG Machine, BOOL Create);
  235. void ClearFeCaches(void);
  236. #endif // #ifndef __FECACHE_HPP__