decode.h 12 KB

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