decode.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * arch/arm/probes/decode.h
  3. *
  4. * Copyright (C) 2011 Jon Medhurst <tixy@yxit.co.uk>.
  5. *
  6. * Some contents moved here from arch/arm/include/asm/kprobes.h which is
  7. * Copyright (C) 2006, 2007 Motorola Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but 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. */
  18. #ifndef _ARM_KERNEL_PROBES_H
  19. #define _ARM_KERNEL_PROBES_H
  20. #include <linux/types.h>
  21. #include <linux/stddef.h>
  22. #include <asm/probes.h>
  23. void __init arm_probes_decode_init(void);
  24. extern probes_check_cc * const probes_condition_checks[16];
  25. #if __LINUX_ARM_ARCH__ >= 7
  26. /* str_pc_offset is architecturally defined from ARMv7 onwards */
  27. #define str_pc_offset 8
  28. #define find_str_pc_offset()
  29. #else /* __LINUX_ARM_ARCH__ < 7 */
  30. /* We need a run-time check to determine str_pc_offset */
  31. extern int str_pc_offset;
  32. void __init find_str_pc_offset(void);
  33. #endif
  34. /*
  35. * Update ITSTATE after normal execution of an IT block instruction.
  36. *
  37. * The 8 IT state bits are split into two parts in CPSR:
  38. * ITSTATE<1:0> are in CPSR<26:25>
  39. * ITSTATE<7:2> are in CPSR<15:10>
  40. */
  41. static inline unsigned long it_advance(unsigned long cpsr)
  42. {
  43. if ((cpsr & 0x06000400) == 0) {
  44. /* ITSTATE<2:0> == 0 means end of IT block, so clear IT state */
  45. cpsr &= ~PSR_IT_MASK;
  46. } else {
  47. /* We need to shift left ITSTATE<4:0> */
  48. const unsigned long mask = 0x06001c00; /* Mask ITSTATE<4:0> */
  49. unsigned long it = cpsr & mask;
  50. it <<= 1;
  51. it |= it >> (27 - 10); /* Carry ITSTATE<2> to correct place */
  52. it &= mask;
  53. cpsr &= ~mask;
  54. cpsr |= it;
  55. }
  56. return cpsr;
  57. }
  58. static inline void __kprobes bx_write_pc(long pcv, struct pt_regs *regs)
  59. {
  60. long cpsr = regs->ARM_cpsr;
  61. if (pcv & 0x1) {
  62. cpsr |= PSR_T_BIT;
  63. pcv &= ~0x1;
  64. } else {
  65. cpsr &= ~PSR_T_BIT;
  66. pcv &= ~0x2; /* Avoid UNPREDICTABLE address allignment */
  67. }
  68. regs->ARM_cpsr = cpsr;
  69. regs->ARM_pc = pcv;
  70. }
  71. #if __LINUX_ARM_ARCH__ >= 6
  72. /* Kernels built for >= ARMv6 should never run on <= ARMv5 hardware, so... */
  73. #define load_write_pc_interworks true
  74. #define test_load_write_pc_interworking()
  75. #else /* __LINUX_ARM_ARCH__ < 6 */
  76. /* We need run-time testing to determine if load_write_pc() should interwork. */
  77. extern bool load_write_pc_interworks;
  78. void __init test_load_write_pc_interworking(void);
  79. #endif
  80. static inline void __kprobes load_write_pc(long pcv, struct pt_regs *regs)
  81. {
  82. if (load_write_pc_interworks)
  83. bx_write_pc(pcv, regs);
  84. else
  85. regs->ARM_pc = pcv;
  86. }
  87. #if __LINUX_ARM_ARCH__ >= 7
  88. #define alu_write_pc_interworks true
  89. #define test_alu_write_pc_interworking()
  90. #elif __LINUX_ARM_ARCH__ <= 5
  91. /* Kernels built for <= ARMv5 should never run on >= ARMv6 hardware, so... */
  92. #define alu_write_pc_interworks false
  93. #define test_alu_write_pc_interworking()
  94. #else /* __LINUX_ARM_ARCH__ == 6 */
  95. /* We could be an ARMv6 binary on ARMv7 hardware so we need a run-time check. */
  96. extern bool alu_write_pc_interworks;
  97. void __init test_alu_write_pc_interworking(void);
  98. #endif /* __LINUX_ARM_ARCH__ == 6 */
  99. static inline void __kprobes alu_write_pc(long pcv, struct pt_regs *regs)
  100. {
  101. if (alu_write_pc_interworks)
  102. bx_write_pc(pcv, regs);
  103. else
  104. regs->ARM_pc = pcv;
  105. }
  106. /*
  107. * Test if load/store instructions writeback the address register.
  108. * if P (bit 24) == 0 or W (bit 21) == 1
  109. */
  110. #define is_writeback(insn) ((insn ^ 0x01000000) & 0x01200000)
  111. /*
  112. * The following definitions and macros are used to build instruction
  113. * decoding tables for use by probes_decode_insn.
  114. *
  115. * These tables are a concatenation of entries each of which consist of one of
  116. * the decode_* structs. All of the fields in every type of decode structure
  117. * are of the union type decode_item, therefore the entire decode table can be
  118. * viewed as an array of these and declared like:
  119. *
  120. * static const union decode_item table_name[] = {};
  121. *
  122. * In order to construct each entry in the table, macros are used to
  123. * initialise a number of sequential decode_item values in a layout which
  124. * matches the relevant struct. E.g. DECODE_SIMULATE initialise a struct
  125. * decode_simulate by initialising four decode_item objects like this...
  126. *
  127. * {.bits = _type},
  128. * {.bits = _mask},
  129. * {.bits = _value},
  130. * {.action = _handler},
  131. *
  132. * Initialising a specified member of the union means that the compiler
  133. * will produce a warning if the argument is of an incorrect type.
  134. *
  135. * Below is a list of each of the macros used to initialise entries and a
  136. * description of the action performed when that entry is matched to an
  137. * instruction. A match is found when (instruction & mask) == value.
  138. *
  139. * DECODE_TABLE(mask, value, table)
  140. * Instruction decoding jumps to parsing the new sub-table 'table'.
  141. *
  142. * DECODE_CUSTOM(mask, value, decoder)
  143. * The value of 'decoder' is used as an index into the array of
  144. * action functions, and the retrieved decoder function is invoked
  145. * to complete decoding of the instruction.
  146. *
  147. * DECODE_SIMULATE(mask, value, handler)
  148. * The probes instruction handler is set to the value found by
  149. * indexing into the action array using the value of 'handler'. This
  150. * will be used to simulate the instruction when the probe is hit.
  151. * Decoding returns with INSN_GOOD_NO_SLOT.
  152. *
  153. * DECODE_EMULATE(mask, value, handler)
  154. * The probes instruction handler is set to the value found by
  155. * indexing into the action array using the value of 'handler'. This
  156. * will be used to emulate the instruction when the probe is hit. The
  157. * modified instruction (see below) is placed in the probes instruction
  158. * slot so it may be called by the emulation code. Decoding returns
  159. * with INSN_GOOD.
  160. *
  161. * DECODE_REJECT(mask, value)
  162. * Instruction decoding fails with INSN_REJECTED
  163. *
  164. * DECODE_OR(mask, value)
  165. * This allows the mask/value test of multiple table entries to be
  166. * logically ORed. Once an 'or' entry is matched the decoding action to
  167. * be performed is that of the next entry which isn't an 'or'. E.g.
  168. *
  169. * DECODE_OR (mask1, value1)
  170. * DECODE_OR (mask2, value2)
  171. * DECODE_SIMULATE (mask3, value3, simulation_handler)
  172. *
  173. * This means that if any of the three mask/value pairs match the
  174. * instruction being decoded, then 'simulation_handler' will be used
  175. * for it.
  176. *
  177. * Both the SIMULATE and EMULATE macros have a second form which take an
  178. * additional 'regs' argument.
  179. *
  180. * DECODE_SIMULATEX(mask, value, handler, regs)
  181. * DECODE_EMULATEX (mask, value, handler, regs)
  182. *
  183. * These are used to specify what kind of CPU register is encoded in each of the
  184. * least significant 5 nibbles of the instruction being decoded. The regs value
  185. * is specified using the REGS macro, this takes any of the REG_TYPE_* values
  186. * from enum decode_reg_type as arguments; only the '*' part of the name is
  187. * given. E.g.
  188. *
  189. * REGS(0, ANY, NOPC, 0, ANY)
  190. *
  191. * This indicates an instruction is encoded like:
  192. *
  193. * bits 19..16 ignore
  194. * bits 15..12 any register allowed here
  195. * bits 11.. 8 any register except PC allowed here
  196. * bits 7.. 4 ignore
  197. * bits 3.. 0 any register allowed here
  198. *
  199. * This register specification is checked after a decode table entry is found to
  200. * match an instruction (through the mask/value test). Any invalid register then
  201. * found in the instruction will cause decoding to fail with INSN_REJECTED. In
  202. * the above example this would happen if bits 11..8 of the instruction were
  203. * 1111, indicating R15 or PC.
  204. *
  205. * As well as checking for legal combinations of registers, this data is also
  206. * used to modify the registers encoded in the instructions so that an
  207. * emulation routines can use it. (See decode_regs() and INSN_NEW_BITS.)
  208. *
  209. * Here is a real example which matches ARM instructions of the form
  210. * "AND <Rd>,<Rn>,<Rm>,<shift> <Rs>"
  211. *
  212. * DECODE_EMULATEX (0x0e000090, 0x00000010, PROBES_DATA_PROCESSING_REG,
  213. * REGS(ANY, ANY, NOPC, 0, ANY)),
  214. * ^ ^ ^ ^
  215. * Rn Rd Rs Rm
  216. *
  217. * Decoding the instruction "AND R4, R5, R6, ASL R15" will be rejected because
  218. * Rs == R15
  219. *
  220. * Decoding the instruction "AND R4, R5, R6, ASL R7" will be accepted and the
  221. * instruction will be modified to "AND R0, R2, R3, ASL R1" and then placed into
  222. * the kprobes instruction slot. This can then be called later by the handler
  223. * function emulate_rd12rn16rm0rs8_rwflags (a pointer to which is retrieved from
  224. * the indicated slot in the action array), in order to simulate the instruction.
  225. */
  226. enum decode_type {
  227. DECODE_TYPE_END,
  228. DECODE_TYPE_TABLE,
  229. DECODE_TYPE_CUSTOM,
  230. DECODE_TYPE_SIMULATE,
  231. DECODE_TYPE_EMULATE,
  232. DECODE_TYPE_OR,
  233. DECODE_TYPE_REJECT,
  234. NUM_DECODE_TYPES /* Must be last enum */
  235. };
  236. #define DECODE_TYPE_BITS 4
  237. #define DECODE_TYPE_MASK ((1 << DECODE_TYPE_BITS) - 1)
  238. enum decode_reg_type {
  239. REG_TYPE_NONE = 0, /* Not a register, ignore */
  240. REG_TYPE_ANY, /* Any register allowed */
  241. REG_TYPE_SAMEAS16, /* Register should be same as that at bits 19..16 */
  242. REG_TYPE_SP, /* Register must be SP */
  243. REG_TYPE_PC, /* Register must be PC */
  244. REG_TYPE_NOSP, /* Register must not be SP */
  245. REG_TYPE_NOSPPC, /* Register must not be SP or PC */
  246. REG_TYPE_NOPC, /* Register must not be PC */
  247. REG_TYPE_NOPCWB, /* No PC if load/store write-back flag also set */
  248. /* The following types are used when the encoding for PC indicates
  249. * another instruction form. This distiction only matters for test
  250. * case coverage checks.
  251. */
  252. REG_TYPE_NOPCX, /* Register must not be PC */
  253. REG_TYPE_NOSPPCX, /* Register must not be SP or PC */
  254. /* Alias to allow '0' arg to be used in REGS macro. */
  255. REG_TYPE_0 = REG_TYPE_NONE
  256. };
  257. #define REGS(r16, r12, r8, r4, r0) \
  258. (((REG_TYPE_##r16) << 16) + \
  259. ((REG_TYPE_##r12) << 12) + \
  260. ((REG_TYPE_##r8) << 8) + \
  261. ((REG_TYPE_##r4) << 4) + \
  262. (REG_TYPE_##r0))
  263. union decode_item {
  264. u32 bits;
  265. const union decode_item *table;
  266. int action;
  267. };
  268. struct decode_header;
  269. typedef enum probes_insn (probes_custom_decode_t)(probes_opcode_t,
  270. struct arch_probes_insn *,
  271. const struct decode_header *);
  272. union decode_action {
  273. probes_insn_handler_t *handler;
  274. probes_custom_decode_t *decoder;
  275. };
  276. typedef enum probes_insn (probes_check_t)(probes_opcode_t,
  277. struct arch_probes_insn *,
  278. const struct decode_header *);
  279. struct decode_checker {
  280. probes_check_t *checker;
  281. };
  282. #define DECODE_END \
  283. {.bits = DECODE_TYPE_END}
  284. struct decode_header {
  285. union decode_item type_regs;
  286. union decode_item mask;
  287. union decode_item value;
  288. };
  289. #define DECODE_HEADER(_type, _mask, _value, _regs) \
  290. {.bits = (_type) | ((_regs) << DECODE_TYPE_BITS)}, \
  291. {.bits = (_mask)}, \
  292. {.bits = (_value)}
  293. struct decode_table {
  294. struct decode_header header;
  295. union decode_item table;
  296. };
  297. #define DECODE_TABLE(_mask, _value, _table) \
  298. DECODE_HEADER(DECODE_TYPE_TABLE, _mask, _value, 0), \
  299. {.table = (_table)}
  300. struct decode_custom {
  301. struct decode_header header;
  302. union decode_item decoder;
  303. };
  304. #define DECODE_CUSTOM(_mask, _value, _decoder) \
  305. DECODE_HEADER(DECODE_TYPE_CUSTOM, _mask, _value, 0), \
  306. {.action = (_decoder)}
  307. struct decode_simulate {
  308. struct decode_header header;
  309. union decode_item handler;
  310. };
  311. #define DECODE_SIMULATEX(_mask, _value, _handler, _regs) \
  312. DECODE_HEADER(DECODE_TYPE_SIMULATE, _mask, _value, _regs), \
  313. {.action = (_handler)}
  314. #define DECODE_SIMULATE(_mask, _value, _handler) \
  315. DECODE_SIMULATEX(_mask, _value, _handler, 0)
  316. struct decode_emulate {
  317. struct decode_header header;
  318. union decode_item handler;
  319. };
  320. #define DECODE_EMULATEX(_mask, _value, _handler, _regs) \
  321. DECODE_HEADER(DECODE_TYPE_EMULATE, _mask, _value, _regs), \
  322. {.action = (_handler)}
  323. #define DECODE_EMULATE(_mask, _value, _handler) \
  324. DECODE_EMULATEX(_mask, _value, _handler, 0)
  325. struct decode_or {
  326. struct decode_header header;
  327. };
  328. #define DECODE_OR(_mask, _value) \
  329. DECODE_HEADER(DECODE_TYPE_OR, _mask, _value, 0)
  330. enum probes_insn {
  331. INSN_REJECTED,
  332. INSN_GOOD,
  333. INSN_GOOD_NO_SLOT
  334. };
  335. struct decode_reject {
  336. struct decode_header header;
  337. };
  338. #define DECODE_REJECT(_mask, _value) \
  339. DECODE_HEADER(DECODE_TYPE_REJECT, _mask, _value, 0)
  340. probes_insn_handler_t probes_simulate_nop;
  341. probes_insn_handler_t probes_emulate_none;
  342. int __kprobes
  343. probes_decode_insn(probes_opcode_t insn, struct arch_probes_insn *asi,
  344. const union decode_item *table, bool thumb, bool emulate,
  345. const union decode_action *actions,
  346. const struct decode_checker **checkers);
  347. #endif