libvex.h 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. /*---------------------------------------------------------------*/
  2. /*--- begin libvex.h ---*/
  3. /*---------------------------------------------------------------*/
  4. /*
  5. This file is part of Valgrind, a dynamic binary instrumentation
  6. framework.
  7. Copyright (C) 2004-2017 OpenWorks LLP
  8. info@open-works.net
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License as
  11. published by the Free Software Foundation; either version 2 of the
  12. License, or (at your option) any later version.
  13. This program is distributed in the hope that it will be useful, but
  14. WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. 02110-1301, USA.
  21. The GNU General Public License is contained in the file COPYING.
  22. Neither the names of the U.S. Department of Energy nor the
  23. University of California nor the names of its contributors may be
  24. used to endorse or promote products derived from this software
  25. without prior written permission.
  26. */
  27. #ifndef __LIBVEX_H
  28. #define __LIBVEX_H
  29. #include "libvex_basictypes.h"
  30. #include "libvex_ir.h"
  31. /*---------------------------------------------------------------*/
  32. /*--- This file defines the top-level interface to LibVEX. ---*/
  33. /*---------------------------------------------------------------*/
  34. /*-------------------------------------------------------*/
  35. /*--- Architectures, variants, and other arch info ---*/
  36. /*-------------------------------------------------------*/
  37. typedef
  38. enum {
  39. VexArch_INVALID=0x400,
  40. VexArchX86,
  41. VexArchAMD64,
  42. VexArchARM,
  43. VexArchARM64,
  44. VexArchPPC32,
  45. VexArchPPC64,
  46. VexArchS390X,
  47. VexArchMIPS32,
  48. VexArchMIPS64
  49. }
  50. VexArch;
  51. /* Information about endianness. */
  52. typedef
  53. enum {
  54. VexEndness_INVALID=0x600, /* unknown endianness */
  55. VexEndnessLE, /* little endian */
  56. VexEndnessBE /* big endian */
  57. }
  58. VexEndness;
  59. /* For a given architecture, these specify extra capabilities beyond
  60. the minimum supported (baseline) capabilities. They may be OR'd
  61. together, although some combinations don't make sense. (eg, SSE2
  62. but not SSE1). LibVEX_Translate will check for nonsensical
  63. combinations. */
  64. /* x86: baseline capability is Pentium-1 (FPU, MMX, but no SSE), with
  65. cmpxchg8b. MMXEXT is a special AMD only subset of SSE1 (Integer SSE). */
  66. #define VEX_HWCAPS_X86_MMXEXT (1<<1) /* A subset of SSE1 on early AMD */
  67. #define VEX_HWCAPS_X86_SSE1 (1<<2) /* SSE1 support (Pentium III) */
  68. #define VEX_HWCAPS_X86_SSE2 (1<<3) /* SSE2 support (Pentium 4) */
  69. #define VEX_HWCAPS_X86_SSE3 (1<<4) /* SSE3 support (>= Prescott) */
  70. #define VEX_HWCAPS_X86_LZCNT (1<<5) /* SSE4a LZCNT insn */
  71. /* amd64: baseline capability is SSE2, with cmpxchg8b but not
  72. cmpxchg16b. */
  73. #define VEX_HWCAPS_AMD64_SSE3 (1<<5) /* SSE3 support */
  74. #define VEX_HWCAPS_AMD64_SSSE3 (1<<12) /* Supplemental SSE3 support */
  75. #define VEX_HWCAPS_AMD64_CX16 (1<<6) /* cmpxchg16b support */
  76. #define VEX_HWCAPS_AMD64_LZCNT (1<<7) /* SSE4a LZCNT insn */
  77. #define VEX_HWCAPS_AMD64_AVX (1<<8) /* AVX instructions */
  78. #define VEX_HWCAPS_AMD64_RDTSCP (1<<9) /* RDTSCP instruction */
  79. #define VEX_HWCAPS_AMD64_BMI (1<<10) /* BMI1 instructions */
  80. #define VEX_HWCAPS_AMD64_AVX2 (1<<11) /* AVX2 instructions */
  81. #define VEX_HWCAPS_AMD64_RDRAND (1<<13) /* RDRAND instructions */
  82. #define VEX_HWCAPS_AMD64_F16C (1<<14) /* F16C instructions */
  83. /* ppc32: baseline capability is integer only */
  84. #define VEX_HWCAPS_PPC32_F (1<<8) /* basic (non-optional) FP */
  85. #define VEX_HWCAPS_PPC32_V (1<<9) /* Altivec (VMX) */
  86. #define VEX_HWCAPS_PPC32_FX (1<<10) /* FP extns (fsqrt, fsqrts) */
  87. #define VEX_HWCAPS_PPC32_GX (1<<11) /* Graphics extns
  88. (fres,frsqrte,fsel,stfiwx) */
  89. #define VEX_HWCAPS_PPC32_VX (1<<12) /* Vector-scalar floating-point (VSX); implies ISA 2.06 or higher */
  90. #define VEX_HWCAPS_PPC32_DFP (1<<17) /* Decimal Floating Point (DFP) -- e.g., dadd */
  91. #define VEX_HWCAPS_PPC32_ISA2_07 (1<<19) /* ISA 2.07 -- e.g., mtvsrd */
  92. #define VEX_HWCAPS_PPC32_ISA3_0 (1<<21) /* ISA 3.0 -- e.g., cnttzw */
  93. /* ppc64: baseline capability is integer and basic FP insns */
  94. #define VEX_HWCAPS_PPC64_V (1<<13) /* Altivec (VMX) */
  95. #define VEX_HWCAPS_PPC64_FX (1<<14) /* FP extns (fsqrt, fsqrts) */
  96. #define VEX_HWCAPS_PPC64_GX (1<<15) /* Graphics extns
  97. (fres,frsqrte,fsel,stfiwx) */
  98. #define VEX_HWCAPS_PPC64_VX (1<<16) /* Vector-scalar floating-point (VSX); implies ISA 2.06 or higher */
  99. #define VEX_HWCAPS_PPC64_DFP (1<<18) /* Decimal Floating Point (DFP) -- e.g., dadd */
  100. #define VEX_HWCAPS_PPC64_ISA2_07 (1<<20) /* ISA 2.07 -- e.g., mtvsrd */
  101. #define VEX_HWCAPS_PPC64_ISA3_0 (1<<22) /* ISA 3.0 -- e.g., cnttzw */
  102. /* s390x: Hardware capability encoding
  103. Bits [26:31] encode the machine model (see VEX_S390X_MODEL... below)
  104. Bits [0:20] encode specific hardware capabilities
  105. (see VEX_HWAPS_S390X_... below)
  106. */
  107. /* Model numbers must be assigned in chronological order.
  108. They are used as array index. */
  109. #define VEX_S390X_MODEL_Z900 0
  110. #define VEX_S390X_MODEL_Z800 1
  111. #define VEX_S390X_MODEL_Z990 2
  112. #define VEX_S390X_MODEL_Z890 3
  113. #define VEX_S390X_MODEL_Z9_EC 4
  114. #define VEX_S390X_MODEL_Z9_BC 5
  115. #define VEX_S390X_MODEL_Z10_EC 6
  116. #define VEX_S390X_MODEL_Z10_BC 7
  117. #define VEX_S390X_MODEL_Z196 8
  118. #define VEX_S390X_MODEL_Z114 9
  119. #define VEX_S390X_MODEL_ZEC12 10
  120. #define VEX_S390X_MODEL_ZBC12 11
  121. #define VEX_S390X_MODEL_Z13 12
  122. #define VEX_S390X_MODEL_Z13S 13
  123. #define VEX_S390X_MODEL_UNKNOWN 14 /* always last in list */
  124. #define VEX_S390X_MODEL_MASK 0x3F
  125. #define VEX_HWCAPS_S390X_LDISP (1<<6) /* Long-displacement facility */
  126. #define VEX_HWCAPS_S390X_EIMM (1<<7) /* Extended-immediate facility */
  127. #define VEX_HWCAPS_S390X_GIE (1<<8) /* General-instruction-extension facility */
  128. #define VEX_HWCAPS_S390X_DFP (1<<9) /* Decimal floating point facility */
  129. #define VEX_HWCAPS_S390X_FGX (1<<10) /* FPR-GR transfer facility */
  130. #define VEX_HWCAPS_S390X_ETF2 (1<<11) /* ETF2-enhancement facility */
  131. #define VEX_HWCAPS_S390X_STFLE (1<<12) /* STFLE facility */
  132. #define VEX_HWCAPS_S390X_ETF3 (1<<13) /* ETF3-enhancement facility */
  133. #define VEX_HWCAPS_S390X_STCKF (1<<14) /* STCKF facility */
  134. #define VEX_HWCAPS_S390X_FPEXT (1<<15) /* Floating point extension facility */
  135. #define VEX_HWCAPS_S390X_LSC (1<<16) /* Conditional load/store facility */
  136. #define VEX_HWCAPS_S390X_PFPO (1<<17) /* Perform floating point ops facility */
  137. #define VEX_HWCAPS_S390X_VX (1<<18) /* Vector facility */
  138. #define VEX_HWCAPS_S390X_MSA5 (1<<19) /* message security assistance facility */
  139. /* Special value representing all available s390x hwcaps */
  140. #define VEX_HWCAPS_S390X_ALL (VEX_HWCAPS_S390X_LDISP | \
  141. VEX_HWCAPS_S390X_EIMM | \
  142. VEX_HWCAPS_S390X_GIE | \
  143. VEX_HWCAPS_S390X_DFP | \
  144. VEX_HWCAPS_S390X_FGX | \
  145. VEX_HWCAPS_S390X_STFLE | \
  146. VEX_HWCAPS_S390X_STCKF | \
  147. VEX_HWCAPS_S390X_FPEXT | \
  148. VEX_HWCAPS_S390X_LSC | \
  149. VEX_HWCAPS_S390X_ETF3 | \
  150. VEX_HWCAPS_S390X_ETF2 | \
  151. VEX_HWCAPS_S390X_PFPO | \
  152. VEX_HWCAPS_S390X_VX | \
  153. VEX_HWCAPS_S390X_MSA5)
  154. #define VEX_HWCAPS_S390X(x) ((x) & ~VEX_S390X_MODEL_MASK)
  155. #define VEX_S390X_MODEL(x) ((x) & VEX_S390X_MODEL_MASK)
  156. /* arm: baseline capability is ARMv4 */
  157. /* Bits 5:0 - architecture level (e.g. 5 for v5, 6 for v6 etc) */
  158. #define VEX_HWCAPS_ARM_VFP (1<<6) /* VFP extension */
  159. #define VEX_HWCAPS_ARM_VFP2 (1<<7) /* VFPv2 */
  160. #define VEX_HWCAPS_ARM_VFP3 (1<<8) /* VFPv3 */
  161. /* Bits 15:10 reserved for (possible) future VFP revisions */
  162. #define VEX_HWCAPS_ARM_NEON (1<<16) /* Advanced SIMD also known as NEON */
  163. /* Get an ARM architecure level from HWCAPS */
  164. #define VEX_ARM_ARCHLEVEL(x) ((x) & 0x3f)
  165. /* ARM64: baseline capability is AArch64 v8. */
  166. /* (no definitions since no variants so far) */
  167. /* MIPS baseline capability */
  168. /* Assigned Company values for bits 23:16 of the PRId Register
  169. (CP0 register 15, select 0). As of the MIPS32 and MIPS64 specs from
  170. MTI, the PRId register is defined in this (backwards compatible)
  171. way:
  172. +----------------+----------------+----------------+----------------+
  173. | Company Options| Company ID | Processor ID | Revision |
  174. +----------------+----------------+----------------+----------------+
  175. 31 24 23 16 15 8 7
  176. */
  177. #define VEX_PRID_COMP_LEGACY 0x00000000
  178. #define VEX_PRID_COMP_MIPS 0x00010000
  179. #define VEX_PRID_COMP_BROADCOM 0x00020000
  180. #define VEX_PRID_COMP_NETLOGIC 0x000C0000
  181. #define VEX_PRID_COMP_CAVIUM 0x000D0000
  182. #define VEX_PRID_COMP_INGENIC_E1 0x00E10000 /* JZ4780 */
  183. /*
  184. * These are valid when 23:16 == PRID_COMP_LEGACY
  185. */
  186. #define VEX_PRID_IMP_LOONGSON_64 0x6300 /* Loongson-2/3 */
  187. /*
  188. * These are the PRID's for when 23:16 == PRID_COMP_MIPS
  189. */
  190. #define VEX_PRID_IMP_34K 0x9500
  191. #define VEX_PRID_IMP_74K 0x9700
  192. #define VEX_PRID_IMP_P5600 0xa800
  193. /*
  194. * Instead of Company Options values, bits 31:24 will be packed with
  195. * additional information, such as isa level and FP mode.
  196. */
  197. #define VEX_MIPS_CPU_ISA_M32R1 0x01000000
  198. #define VEX_MIPS_CPU_ISA_M32R2 0x02000000
  199. #define VEX_MIPS_CPU_ISA_M64R1 0x04000000
  200. #define VEX_MIPS_CPU_ISA_M64R2 0x08000000
  201. #define VEX_MIPS_CPU_ISA_M32R6 0x10000000
  202. #define VEX_MIPS_CPU_ISA_M64R6 0x20000000
  203. /* FP mode is FR = 1 (32 dbl. prec. FP registers) */
  204. #define VEX_MIPS_HOST_FR 0x40000000
  205. /* Get MIPS Extended Information */
  206. #define VEX_MIPS_EX_INFO(x) ((x) & 0xFF000000)
  207. /* Get MIPS Company ID from HWCAPS */
  208. #define VEX_MIPS_COMP_ID(x) ((x) & 0x00FF0000)
  209. /* Get MIPS Processor ID from HWCAPS */
  210. #define VEX_MIPS_PROC_ID(x) ((x) & 0x0000FF00)
  211. /* Get MIPS Revision from HWCAPS */
  212. #define VEX_MIPS_REV(x) ((x) & 0x000000FF)
  213. /* Get host FP mode */
  214. #define VEX_MIPS_HOST_FP_MODE(x) (!!(VEX_MIPS_EX_INFO(x) & VEX_MIPS_HOST_FR))
  215. /* Check if the processor supports MIPS32R2. */
  216. #define VEX_MIPS_CPU_HAS_MIPS32R2(x) (VEX_MIPS_EX_INFO(x) & \
  217. VEX_MIPS_CPU_ISA_M32R2)
  218. /* Check if the processor supports MIPS64R2. */
  219. #define VEX_MIPS_CPU_HAS_MIPS64R2(x) (VEX_MIPS_EX_INFO(x) & \
  220. VEX_MIPS_CPU_ISA_M64R2)
  221. /* Check if the processor supports MIPSR6. */
  222. #define VEX_MIPS_CPU_HAS_MIPSR6(x) (VEX_MIPS_EX_INFO(x) & \
  223. (VEX_MIPS_CPU_ISA_M32R6 | \
  224. VEX_MIPS_CPU_ISA_M64R6))
  225. /* Check if the processor supports DSP ASE Rev 2. */
  226. #define VEX_MIPS_PROC_DSP2(x) ((VEX_MIPS_COMP_ID(x) == VEX_PRID_COMP_MIPS) && \
  227. (VEX_MIPS_PROC_ID(x) == VEX_PRID_IMP_74K))
  228. /* Check if the processor supports DSP ASE Rev 1. */
  229. #define VEX_MIPS_PROC_DSP(x) (VEX_MIPS_PROC_DSP2(x) || \
  230. ((VEX_MIPS_COMP_ID(x) == VEX_PRID_COMP_MIPS) && \
  231. (VEX_MIPS_PROC_ID(x) == VEX_PRID_IMP_34K)))
  232. /* Check if the processor supports MIPS MSA (SIMD)*/
  233. #define VEX_MIPS_PROC_MSA(x) ((VEX_MIPS_COMP_ID(x) == VEX_PRID_COMP_MIPS) && \
  234. (VEX_MIPS_PROC_ID(x) == VEX_PRID_IMP_P5600) && \
  235. (VEX_MIPS_HOST_FP_MODE(x)))
  236. /* These return statically allocated strings. */
  237. extern const HChar* LibVEX_ppVexArch ( VexArch );
  238. extern const HChar* LibVEX_ppVexEndness ( VexEndness endness );
  239. extern const HChar* LibVEX_ppVexHwCaps ( VexArch, UInt );
  240. /* The various kinds of caches */
  241. typedef enum {
  242. DATA_CACHE=0x500,
  243. INSN_CACHE,
  244. UNIFIED_CACHE
  245. } VexCacheKind;
  246. /* Information about a particular cache */
  247. typedef struct {
  248. VexCacheKind kind;
  249. UInt level; /* level this cache is at, e.g. 1 for L1 cache */
  250. UInt sizeB; /* size of this cache in bytes */
  251. UInt line_sizeB; /* cache line size in bytes */
  252. UInt assoc; /* set associativity */
  253. Bool is_trace_cache; /* False, except for certain Pentium 4 models */
  254. } VexCache;
  255. /* Convenience macro to initialise a VexCache */
  256. #define VEX_CACHE_INIT(_kind, _level, _size, _line_size, _assoc) \
  257. ({ (VexCache) { .kind = _kind, .level = _level, .sizeB = _size, \
  258. .line_sizeB = _line_size, .assoc = _assoc, \
  259. .is_trace_cache = False }; })
  260. /* Information about the cache system as a whole */
  261. typedef struct {
  262. UInt num_levels;
  263. UInt num_caches;
  264. /* Unordered array of caches for this host. NULL if there are
  265. no caches. The following can always be assumed:
  266. (1) There is at most one cache of a given kind per cache level.
  267. (2) If there exists a unified cache at a particular level then
  268. no other cache exists at that level.
  269. (3) The existence of a cache at level N > 1 implies the existence of
  270. at least one cache at level N-1. */
  271. VexCache *caches;
  272. Bool icaches_maintain_coherence;
  273. } VexCacheInfo;
  274. /* This struct is a bit of a hack, but is needed to carry misc
  275. important bits of info about an arch. Fields which are meaningless
  276. or ignored for the platform in question should be set to zero.
  277. Nb: if you add fields to the struct make sure to update function
  278. LibVEX_default_VexArchInfo. */
  279. typedef
  280. struct {
  281. /* The following three fields are mandatory. */
  282. UInt hwcaps;
  283. VexEndness endness;
  284. VexCacheInfo hwcache_info;
  285. /* PPC32/PPC64 only: size of instruction cache line */
  286. Int ppc_icache_line_szB;
  287. /* PPC32/PPC64 only: sizes zeroed by the dcbz/dcbzl instructions
  288. (bug#135264) */
  289. UInt ppc_dcbz_szB;
  290. UInt ppc_dcbzl_szB; /* 0 means unsupported (SIGILL) */
  291. /* ARM64: I- and D- minimum line sizes in log2(bytes), as
  292. obtained from ctr_el0.DminLine and .IminLine. For example, a
  293. line size of 64 bytes would be encoded here as 6. */
  294. UInt arm64_dMinLine_lg2_szB;
  295. UInt arm64_iMinLine_lg2_szB;
  296. /* ARM64: does the host require us to use the fallback LLSC
  297. implementation? */
  298. Bool arm64_requires_fallback_LLSC;
  299. }
  300. VexArchInfo;
  301. /* Write default settings info *vai. */
  302. extern
  303. void LibVEX_default_VexArchInfo ( /*OUT*/VexArchInfo* vai );
  304. /* This struct carries guest and host ABI variant information that may
  305. be needed. Fields which are meaningless or ignored for the
  306. platform in question should be set to zero.
  307. Settings which are believed to be correct are:
  308. guest_stack_redzone_size
  309. guest is ppc32-linux ==> 0
  310. guest is ppc64-linux ==> 288
  311. guest is amd64-linux ==> 128
  312. guest is other ==> inapplicable
  313. guest_amd64_assume_fs_is_const
  314. guest is amd64-linux ==> True
  315. guest is amd64-darwin ==> False
  316. guest is amd64-solaris ==> True
  317. guest is other ==> inapplicable
  318. guest_amd64_assume_gs_is_const
  319. guest is amd64-darwin ==> True
  320. guest is amd64-linux ==> True
  321. guest is amd64-solaris ==> False
  322. guest is other ==> inapplicable
  323. guest_ppc_zap_RZ_at_blr
  324. guest is ppc64-linux ==> True
  325. guest is ppc32-linux ==> False
  326. guest is other ==> inapplicable
  327. guest_ppc_zap_RZ_at_bl
  328. guest is ppc64-linux ==> const True
  329. guest is ppc32-linux ==> const False
  330. guest is other ==> inapplicable
  331. guest__use_fallback_LLSC
  332. guest is mips32 ==> applicable, default True
  333. guest is mips64 ==> applicable, default True
  334. guest is arm64 ==> applicable, default False
  335. host_ppc_calls_use_fndescrs:
  336. host is ppc32-linux ==> False
  337. host is ppc64-linux ==> True
  338. host is other ==> inapplicable
  339. */
  340. typedef
  341. struct {
  342. /* PPC and AMD64 GUESTS only: how many bytes below the
  343. stack pointer are validly addressible? */
  344. Int guest_stack_redzone_size;
  345. /* AMD64 GUESTS only: should we translate %fs-prefixed
  346. instructions using the assumption that %fs always contains
  347. the same value? (typically zero on linux and solaris) */
  348. Bool guest_amd64_assume_fs_is_const;
  349. /* AMD64 GUESTS only: should we translate %gs-prefixed
  350. instructions using the assumption that %gs always contains
  351. the same value? (typically 0x60 on darwin)? */
  352. Bool guest_amd64_assume_gs_is_const;
  353. /* PPC GUESTS only: should we zap the stack red zone at a 'blr'
  354. (function return) ? */
  355. Bool guest_ppc_zap_RZ_at_blr;
  356. /* PPC GUESTS only: should we zap the stack red zone at a 'bl'
  357. (function call) ? Is supplied with the guest address of the
  358. target of the call since that may be significant. If NULL,
  359. is assumed equivalent to a fn which always returns False. */
  360. Bool (*guest_ppc_zap_RZ_at_bl)(Addr);
  361. /* Potentially for all guests that use LL/SC: use the fallback
  362. (synthesised) implementation rather than passing LL/SC on to
  363. the host? */
  364. Bool guest__use_fallback_LLSC;
  365. /* PPC32/PPC64 HOSTS only: does '&f' give us a pointer to a
  366. function descriptor on the host, or to the function code
  367. itself? True => descriptor, False => code. */
  368. Bool host_ppc_calls_use_fndescrs;
  369. /* MIPS32/MIPS64 GUESTS only: emulated FPU mode. */
  370. UInt guest_mips_fp_mode;
  371. }
  372. VexAbiInfo;
  373. /* Write default settings info *vbi. */
  374. extern
  375. void LibVEX_default_VexAbiInfo ( /*OUT*/VexAbiInfo* vbi );
  376. /*-------------------------------------------------------*/
  377. /*--- Control of Vex's optimiser (iropt). ---*/
  378. /*-------------------------------------------------------*/
  379. /* VexRegisterUpdates specifies when to ensure that the guest state is
  380. up to date, in order of increasing accuracy but increasing expense.
  381. VexRegUpdSpAtMemAccess: all registers are updated at superblock
  382. exits, and SP is also up to date at memory exception points. The
  383. SP is described by the arch specific functions
  384. guest_<arch>_state_requires_precise_mem_exns.
  385. VexRegUpdUnwindregsAtMemAccess: registers needed to make a stack
  386. trace are up to date at memory exception points. Typically,
  387. these are PC/SP/FP. The minimal registers are described by the
  388. arch specific functions guest_<arch>_state_requires_precise_mem_exns.
  389. This is what Valgrind sets as the default.
  390. VexRegUpdAllregsAtMemAccess: all registers up to date at memory
  391. exception points. This is what normally might be considered as
  392. providing "precise exceptions for memory", but does not
  393. necessarily provide precise register values at any other kind of
  394. exception.
  395. VexRegUpdAllregsAtEachInsn: all registers up to date at each
  396. instruction.
  397. */
  398. typedef
  399. enum {
  400. VexRegUpd_INVALID=0x700,
  401. VexRegUpdSpAtMemAccess,
  402. VexRegUpdUnwindregsAtMemAccess,
  403. VexRegUpdAllregsAtMemAccess,
  404. VexRegUpdAllregsAtEachInsn
  405. }
  406. VexRegisterUpdates;
  407. /* Control of Vex's optimiser. */
  408. typedef
  409. struct {
  410. /* Controls verbosity of iropt. 0 = no output. */
  411. Int iropt_verbosity;
  412. /* Control aggressiveness of iropt. 0 = no opt, 1 = simple
  413. opts, 2 (default) = max optimisation. */
  414. Int iropt_level;
  415. /* Controls when registers are updated in guest state. Note
  416. that this is the default value. The VEX client can override
  417. this on a per-IRSB basis if it wants. bb_to_IR() will query
  418. the client to ask if it wants a different setting for the
  419. block under construction, and that new setting is transported
  420. back to LibVEX_Translate, which feeds it to iropt via the
  421. various do_iropt_BB calls. */
  422. VexRegisterUpdates iropt_register_updates_default;
  423. /* How aggressive should iropt be in unrolling loops? Higher
  424. numbers make it more enthusiastic about loop unrolling.
  425. Default=120. A setting of zero disables unrolling. */
  426. Int iropt_unroll_thresh;
  427. /* What's the maximum basic block length the front end(s) allow?
  428. BBs longer than this are split up. Default=60 (guest
  429. insns). */
  430. Int guest_max_insns;
  431. /* How aggressive should front ends be in following
  432. unconditional branches to known destinations? Default=10,
  433. meaning that if a block contains less than 10 guest insns so
  434. far, the front end(s) will attempt to chase into its
  435. successor. A setting of zero disables chasing. */
  436. Int guest_chase_thresh;
  437. /* EXPERIMENTAL: chase across conditional branches? Not all
  438. front ends honour this. Default: NO. */
  439. Bool guest_chase_cond;
  440. /* Register allocator version. Allowed values are:
  441. - '2': previous, good and slow implementation.
  442. - '3': current, faster implementation; perhaps producing slightly worse
  443. spilling decisions. */
  444. UInt regalloc_version;
  445. }
  446. VexControl;
  447. /* Write the default settings into *vcon. */
  448. extern
  449. void LibVEX_default_VexControl ( /*OUT*/ VexControl* vcon );
  450. /*-------------------------------------------------------*/
  451. /*--- Storage management control ---*/
  452. /*-------------------------------------------------------*/
  453. /* Allocate in Vex's temporary allocation area. Be careful with this.
  454. You can only call it inside an instrumentation or optimisation
  455. callback that you have previously specified in a call to
  456. LibVEX_Translate. The storage allocated will only stay alive until
  457. translation of the current basic block is complete. */
  458. extern void* LibVEX_Alloc ( SizeT nbytes );
  459. /* Show Vex allocation statistics. */
  460. extern void LibVEX_ShowAllocStats ( void );
  461. /*-------------------------------------------------------*/
  462. /*--- Describing guest state layout ---*/
  463. /*-------------------------------------------------------*/
  464. /* Describe the guest state enough that the instrumentation
  465. functions can work. */
  466. /* The max number of guest state chunks which we can describe as
  467. always defined (for the benefit of Memcheck). */
  468. #define VEXGLO_N_ALWAYSDEFD 24
  469. typedef
  470. struct {
  471. /* Total size of the guest state, in bytes. Must be
  472. 16-aligned. */
  473. Int total_sizeB;
  474. /* Whereabouts is the stack pointer? */
  475. Int offset_SP;
  476. Int sizeof_SP; /* 4 or 8 */
  477. /* Whereabouts is the frame pointer? */
  478. Int offset_FP;
  479. Int sizeof_FP; /* 4 or 8 */
  480. /* Whereabouts is the instruction pointer? */
  481. Int offset_IP;
  482. Int sizeof_IP; /* 4 or 8 */
  483. /* Describe parts of the guest state regarded as 'always
  484. defined'. */
  485. Int n_alwaysDefd;
  486. struct {
  487. Int offset;
  488. Int size;
  489. } alwaysDefd[VEXGLO_N_ALWAYSDEFD];
  490. }
  491. VexGuestLayout;
  492. /* A note about guest state layout.
  493. LibVEX defines the layout for the guest state, in the file
  494. pub/libvex_guest_<arch>.h. The struct will have an 16-aligned
  495. size. Each translated bb is assumed to be entered with a specified
  496. register pointing at such a struct. Beyond that is two copies of
  497. the shadow state area with the same size as the struct. Beyond
  498. that is a spill area that LibVEX may spill into. It must have size
  499. LibVEX_N_SPILL_BYTES, and this must be a 16-aligned number.
  500. On entry, the baseblock pointer register must be 16-aligned.
  501. There must be no holes in between the primary guest state, its two
  502. copies, and the spill area. In short, all 4 areas must have a
  503. 16-aligned size and be 16-aligned, and placed back-to-back.
  504. */
  505. #define LibVEX_N_SPILL_BYTES 4096
  506. /* The size of the guest state must be a multiple of this number. */
  507. #define LibVEX_GUEST_STATE_ALIGN 16
  508. /*-------------------------------------------------------*/
  509. /*--- Initialisation of the library ---*/
  510. /*-------------------------------------------------------*/
  511. /* Initialise the library. You must call this first. */
  512. extern void LibVEX_Init (
  513. /* failure exit function */
  514. # if __cplusplus == 1 && __GNUC__ && __GNUC__ <= 3
  515. /* g++ 3.x doesn't understand attributes on function parameters.
  516. See #265762. */
  517. # else
  518. __attribute__ ((noreturn))
  519. # endif
  520. void (*failure_exit) ( void ),
  521. /* logging output function */
  522. void (*log_bytes) ( const HChar*, SizeT nbytes ),
  523. /* debug paranoia level */
  524. Int debuglevel,
  525. /* Control ... */
  526. const VexControl* vcon
  527. );
  528. /*-------------------------------------------------------*/
  529. /*--- Make a translation ---*/
  530. /*-------------------------------------------------------*/
  531. /* Describes the outcome of a translation attempt. */
  532. typedef
  533. struct {
  534. /* overall status */
  535. enum { VexTransOK=0x800,
  536. VexTransAccessFail, VexTransOutputFull } status;
  537. /* The number of extents that have a self-check (0 to 3) */
  538. UInt n_sc_extents;
  539. /* Offset in generated code of the profile inc, or -1 if
  540. none. Needed for later patching. */
  541. Int offs_profInc;
  542. /* Stats only: the number of guest insns included in the
  543. translation. It may be zero (!). */
  544. UInt n_guest_instrs;
  545. }
  546. VexTranslateResult;
  547. /* Describes precisely the pieces of guest code that a translation
  548. covers. Now that Vex can chase across BB boundaries, the old
  549. scheme of describing a chunk of guest code merely by its start
  550. address and length is inadequate.
  551. This struct uses 20 bytes on a 32-bit archtecture and 32 bytes on a
  552. 64-bit architecture. Space is important as clients will have to store
  553. one of these for each translation made.
  554. */
  555. typedef
  556. struct {
  557. Addr base[3];
  558. UShort len[3];
  559. UShort n_used;
  560. }
  561. VexGuestExtents;
  562. /* A structure to carry arguments for LibVEX_Translate. There are so
  563. many of them, it seems better to have a structure. */
  564. typedef
  565. struct {
  566. /* IN: The instruction sets we are translating from and to. And
  567. guest/host misc info. */
  568. VexArch arch_guest;
  569. VexArchInfo archinfo_guest;
  570. VexArch arch_host;
  571. VexArchInfo archinfo_host;
  572. VexAbiInfo abiinfo_both;
  573. /* IN: an opaque value which is passed as the first arg to all
  574. callback functions supplied in this struct. Vex has no idea
  575. what's at the other end of this pointer. */
  576. void* callback_opaque;
  577. /* IN: the block to translate, and its guest address. */
  578. /* where are the actual bytes in the host's address space? */
  579. const UChar* guest_bytes;
  580. /* where do the bytes really come from in the guest's aspace?
  581. This is the post-redirection guest address. Not that Vex
  582. understands anything about redirection; that is all done on
  583. the Valgrind side. */
  584. Addr guest_bytes_addr;
  585. /* Is it OK to chase into this guest address? May not be
  586. NULL. */
  587. Bool (*chase_into_ok) ( /*callback_opaque*/void*, Addr );
  588. /* OUT: which bits of guest code actually got translated */
  589. VexGuestExtents* guest_extents;
  590. /* IN: a place to put the resulting code, and its size */
  591. UChar* host_bytes;
  592. Int host_bytes_size;
  593. /* OUT: how much of the output area is used. */
  594. Int* host_bytes_used;
  595. /* IN: optionally, two instrumentation functions. May be
  596. NULL. */
  597. IRSB* (*instrument1) ( /*callback_opaque*/void*,
  598. IRSB*,
  599. const VexGuestLayout*,
  600. const VexGuestExtents*,
  601. const VexArchInfo*,
  602. IRType gWordTy, IRType hWordTy );
  603. IRSB* (*instrument2) ( /*callback_opaque*/void*,
  604. IRSB*,
  605. const VexGuestLayout*,
  606. const VexGuestExtents*,
  607. const VexArchInfo*,
  608. IRType gWordTy, IRType hWordTy );
  609. IRSB* (*finaltidy) ( IRSB* );
  610. /* IN: a callback used to ask the caller which of the extents,
  611. if any, a self check is required for. Must not be NULL.
  612. The returned value is a bitmask with a 1 in position i indicating
  613. that the i'th extent needs a check. Since there can be at most
  614. 3 extents, the returned values must be between 0 and 7.
  615. This call also gives the VEX client the opportunity to change
  616. the precision of register update preservation as performed by
  617. the IR optimiser. Before the call, VEX will set *pxControl
  618. to hold the default register-update status value as specified
  619. by VexControl::iropt_register_updates_default as passed to
  620. LibVEX_Init at library initialisation time. The client (in
  621. this callback) can if it wants, inspect the value and change
  622. it to something different, and that value will be used for
  623. subsequent IR optimisation of the block. */
  624. UInt (*needs_self_check)( /*callback_opaque*/void*,
  625. /*MAYBE_MOD*/VexRegisterUpdates* pxControl,
  626. const VexGuestExtents* );
  627. /* IN: optionally, a callback which allows the caller to add its
  628. own IR preamble following the self-check and any other
  629. VEX-generated preamble, if any. May be NULL. If non-NULL,
  630. the IRSB under construction is handed to this function, which
  631. presumably adds IR statements to it. The callback may
  632. optionally complete the block and direct bb_to_IR not to
  633. disassemble any instructions into it; this is indicated by
  634. the callback returning True.
  635. */
  636. Bool (*preamble_function)(/*callback_opaque*/void*, IRSB*);
  637. /* IN: debug: trace vex activity at various points */
  638. Int traceflags;
  639. /* IN: debug: print diagnostics when an illegal instr is detected */
  640. Bool sigill_diag;
  641. /* IN: profiling: add a 64 bit profiler counter increment to the
  642. translation? */
  643. Bool addProfInc;
  644. /* IN: address of the dispatcher entry points. Describes the
  645. places where generated code should jump to at the end of each
  646. bb.
  647. At the end of each translation, the next guest address is
  648. placed in the host's standard return register (x86: %eax,
  649. amd64: %rax, ppc32: %r3, ppc64: %r3). Optionally, the guest
  650. state pointer register (on host x86: %ebp; amd64: %rbp;
  651. ppc32/64: r31) may be set to a VEX_TRC_ value to indicate any
  652. special action required before the next block is run.
  653. Control is then passed back to the dispatcher (beyond Vex's
  654. control; caller supplies this) in the following way:
  655. - On host archs which lack a link register (x86, amd64), by a
  656. jump to the host address specified in
  657. 'dispatcher_assisted', if the guest state pointer has been
  658. changed so as to request some action before the next block
  659. is run, or 'dispatcher_unassisted' (the fast path), in
  660. which it is assumed that the guest state pointer is
  661. unchanged and we wish to continue directly with the next
  662. translation. Both of these must be non-NULL.
  663. - On host archs which have a link register (ppc32, ppc64), by
  664. a branch to the link register (which is guaranteed to be
  665. unchanged from whatever it was at entry to the
  666. translation). 'dispatch_assisted' and
  667. 'dispatch_unassisted' must be NULL.
  668. The aim is to get back and forth between translations and the
  669. dispatcher without creating memory traffic to store return
  670. addresses.
  671. FIXME: update this comment
  672. */
  673. const void* disp_cp_chain_me_to_slowEP;
  674. const void* disp_cp_chain_me_to_fastEP;
  675. const void* disp_cp_xindir;
  676. const void* disp_cp_xassisted;
  677. }
  678. VexTranslateArgs;
  679. /* Runs the entire compilation pipeline. */
  680. extern
  681. VexTranslateResult LibVEX_Translate ( /*MOD*/ VexTranslateArgs* );
  682. /* Runs the first half of the compilation pipeline: lifts guest code to IR,
  683. optimises, instruments and optimises it some more. */
  684. extern
  685. IRSB* LibVEX_FrontEnd ( /*MOD*/ VexTranslateArgs*,
  686. /*OUT*/ VexTranslateResult* res,
  687. /*OUT*/ VexRegisterUpdates* pxControl );
  688. /* A subtlety re interaction between self-checking translations and
  689. bb-chasing. The supplied chase_into_ok function should say NO
  690. (False) when presented with any address for which you might want to
  691. make a self-checking translation.
  692. If it doesn't do that, you may end up with Vex chasing from BB #1
  693. to BB #2 (fine); but if you wanted checking for #2 and not #1, that
  694. would not be the result. Therefore chase_into_ok should disallow
  695. following into #2. That will force the caller to eventually
  696. request a new translation starting at #2, at which point Vex will
  697. correctly observe the make-a-self-check flag.
  698. FIXME: is this still up to date? */
  699. /*-------------------------------------------------------*/
  700. /*--- Patch existing translations ---*/
  701. /*-------------------------------------------------------*/
  702. /* A host address range that was modified by the functions below.
  703. Callers must request I-cache syncing after the call as appropriate. */
  704. typedef
  705. struct {
  706. HWord start;
  707. HWord len; /* always > 0 */
  708. }
  709. VexInvalRange;
  710. /* Chain an XDirect jump located at place_to_chain so it jumps to
  711. place_to_jump_to. It is expected (and checked) that this site
  712. currently contains a call to the dispatcher specified by
  713. disp_cp_chain_me_EXPECTED. */
  714. extern
  715. VexInvalRange LibVEX_Chain ( VexArch arch_host,
  716. VexEndness endhess_host,
  717. void* place_to_chain,
  718. const void* disp_cp_chain_me_EXPECTED,
  719. const void* place_to_jump_to );
  720. /* Undo an XDirect jump located at place_to_unchain, so it is
  721. converted back into a call to disp_cp_chain_me. It is expected
  722. (and checked) that this site currently contains a jump directly to
  723. the address specified by place_to_jump_to_EXPECTED. */
  724. extern
  725. VexInvalRange LibVEX_UnChain ( VexArch arch_host,
  726. VexEndness endness_host,
  727. void* place_to_unchain,
  728. const void* place_to_jump_to_EXPECTED,
  729. const void* disp_cp_chain_me );
  730. /* Returns a constant -- the size of the event check that is put at
  731. the start of every translation. This makes it possible to
  732. calculate the fast entry point address if the slow entry point
  733. address is known (the usual case), or vice versa. */
  734. extern
  735. Int LibVEX_evCheckSzB ( VexArch arch_host );
  736. /* Patch the counter location into an existing ProfInc point. The
  737. specified point is checked to make sure it is plausible. */
  738. extern
  739. VexInvalRange LibVEX_PatchProfInc ( VexArch arch_host,
  740. VexEndness endness_host,
  741. void* place_to_patch,
  742. const ULong* location_of_counter );
  743. /*-------------------------------------------------------*/
  744. /*--- Show accumulated statistics ---*/
  745. /*-------------------------------------------------------*/
  746. extern void LibVEX_ShowStats ( void );
  747. /*-------------------------------------------------------*/
  748. /*-- IR injection --*/
  749. /*-------------------------------------------------------*/
  750. /* IR Injection Control Block */
  751. #define NO_ROUNDING_MODE (~0u)
  752. typedef
  753. struct {
  754. IROp op; // the operation to perform
  755. HWord result; // address of the result
  756. HWord opnd1; // address of 1st operand
  757. HWord opnd2; // address of 2nd operand
  758. HWord opnd3; // address of 3rd operand
  759. HWord opnd4; // address of 4th operand
  760. IRType t_result; // type of result
  761. IRType t_opnd1; // type of 1st operand
  762. IRType t_opnd2; // type of 2nd operand
  763. IRType t_opnd3; // type of 3rd operand
  764. IRType t_opnd4; // type of 4th operand
  765. UInt rounding_mode;
  766. UInt num_operands; // excluding rounding mode, if any
  767. /* The following two members describe if this operand has immediate
  768. * operands. There are a few restrictions:
  769. * (1) An operator can have at most one immediate operand.
  770. * (2) If there is an immediate operand, it is the right-most operand
  771. * An immediate_index of 0 means there is no immediate operand.
  772. */
  773. UInt immediate_type; // size of immediate Ity_I8, Ity_16
  774. UInt immediate_index; // operand number: 1, 2
  775. }
  776. IRICB;
  777. extern void LibVEX_InitIRI ( const IRICB * );
  778. /*-------------------------------------------------------*/
  779. /*--- Notes ---*/
  780. /*-------------------------------------------------------*/
  781. /* Code generation conventions that need to be recorded somewhere.
  782. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  783. x86
  784. ~~~
  785. Generated code should be entered using a JMP instruction. On
  786. entry, %ebp should point to the guest state, and %esp should be a
  787. valid stack pointer. The generated code may change %eax, %ebx,
  788. %ecx, %edx, %esi, %edi, all the FP registers and control state, and
  789. all the XMM registers.
  790. On entry, the FPU control word should be set to 0x027F, and the SSE
  791. control word (%mxcsr) should be set to 0x1F80. On exit, they
  792. should still have those values (after masking off the lowest 6 bits
  793. of %mxcsr). If they don't, there is a bug in VEX-generated code.
  794. Generated code returns to the scheduler using a JMP instruction, to
  795. the address specified in the .dispatch field of VexTranslateArgs.
  796. %eax (or %eax:%edx, if simulating a 64-bit target) will contain the
  797. guest address of the next block to execute. %ebp may be changed
  798. to a VEX_TRC_ value, otherwise it should be as it was at entry.
  799. CRITICAL ISSUES in x86 code generation. The only known critical
  800. issue is that the host FPU and SSE state is not properly saved
  801. across calls to helper functions. If any helper references any
  802. such state, it is likely (1) to misbehave itself, since the FP
  803. stack tags will not be as expected, and (2) after returning to
  804. generated code, the generated code is likely to go wrong. This
  805. really should be fixed.
  806. amd64
  807. ~~~~~
  808. Analogous to x86.
  809. ppc32
  810. ~~~~~
  811. On entry, guest state pointer is r31. .dispatch must be NULL.
  812. Control is returned with a branch to the link register. Generated
  813. code will not change lr. At return, r3 holds the next guest addr
  814. (or r3:r4 ?). r31 may be may be changed to a VEX_TRC_ value,
  815. otherwise it should be as it was at entry.
  816. ppc64
  817. ~~~~~
  818. Same as ppc32.
  819. arm32
  820. ~~~~~
  821. r8 is GSP.
  822. arm64
  823. ~~~~~
  824. r21 is GSP.
  825. ALL GUEST ARCHITECTURES
  826. ~~~~~~~~~~~~~~~~~~~~~~~
  827. The guest state must contain two pseudo-registers, guest_CMSTART
  828. and guest_CMLEN. These are used to specify guest address ranges,
  829. either of code to be invalidated, when used in conjunction with
  830. Ijk_InvalICache, or of d-cache ranges to be flushed, when used in
  831. conjunction with Ijk_FlushDCache. In such cases, the two _CM
  832. pseudo-regs should be filled in by the IR, and then an exit with
  833. one of the two abovementioned Ijk_ kinds should happen, so that the
  834. dispatcher can action them. Both pseudo-regs must have size equal
  835. to the guest word size.
  836. The architecture must a third pseudo-register, guest_NRADDR, also
  837. guest-word-sized. This is used to record the unredirected guest
  838. address at the start of a translation whose start has been
  839. redirected. By reading this pseudo-register shortly afterwards,
  840. the translation can find out what the corresponding no-redirection
  841. address was. Note, this is only set for wrap-style redirects, not
  842. for replace-style ones.
  843. */
  844. #endif /* ndef __LIBVEX_H */
  845. /*---------------------------------------------------------------*/
  846. /*--- libvex.h ---*/
  847. /*---------------------------------------------------------------*/