kprobes.h 13 KB

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