kprobes.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. void __kprobes kprobe_simulate_nop(struct kprobe *p, struct pt_regs *regs);
  108. void __kprobes kprobe_emulate_none(struct kprobe *p, struct pt_regs *regs);
  109. /*
  110. * Test if load/store instructions writeback the address register.
  111. * if P (bit 24) == 0 or W (bit 21) == 1
  112. */
  113. #define is_writeback(insn) ((insn ^ 0x01000000) & 0x01200000)
  114. /*
  115. * The following definitions and macros are used to build instruction
  116. * decoding tables for use by kprobe_decode_insn.
  117. *
  118. * These tables are a concatenation of entries each of which consist of one of
  119. * the decode_* structs. All of the fields in every type of decode structure
  120. * are of the union type decode_item, therefore the entire decode table can be
  121. * viewed as an array of these and declared like:
  122. *
  123. * static const union decode_item table_name[] = {};
  124. *
  125. * In order to construct each entry in the table, macros are used to
  126. * initialise a number of sequential decode_item values in a layout which
  127. * matches the relevant struct. E.g. DECODE_SIMULATE initialise a struct
  128. * decode_simulate by initialising four decode_item objects like this...
  129. *
  130. * {.bits = _type},
  131. * {.bits = _mask},
  132. * {.bits = _value},
  133. * {.handler = _handler},
  134. *
  135. * Initialising a specified member of the union means that the compiler
  136. * will produce a warning if the argument is of an incorrect type.
  137. *
  138. * Below is a list of each of the macros used to initialise entries and a
  139. * description of the action performed when that entry is matched to an
  140. * instruction. A match is found when (instruction & mask) == value.
  141. *
  142. * DECODE_TABLE(mask, value, table)
  143. * Instruction decoding jumps to parsing the new sub-table 'table'.
  144. *
  145. * DECODE_CUSTOM(mask, value, decoder)
  146. * The custom function 'decoder' is called to the complete decoding
  147. * of an instruction.
  148. *
  149. * DECODE_SIMULATE(mask, value, handler)
  150. * Set the probes instruction handler to 'handler', this will be used
  151. * to simulate the instruction when the probe is hit. Decoding returns
  152. * with INSN_GOOD_NO_SLOT.
  153. *
  154. * DECODE_EMULATE(mask, value, handler)
  155. * Set the probes instruction handler to 'handler', this will be used
  156. * to emulate the instruction when the probe is hit. The modified
  157. * instruction (see below) is placed in the probes instruction slot so it
  158. * may be called by the emulation code. Decoding returns with INSN_GOOD.
  159. *
  160. * DECODE_REJECT(mask, value)
  161. * Instruction decoding fails with INSN_REJECTED
  162. *
  163. * DECODE_OR(mask, value)
  164. * This allows the mask/value test of multiple table entries to be
  165. * logically ORed. Once an 'or' entry is matched the decoding action to
  166. * be performed is that of the next entry which isn't an 'or'. E.g.
  167. *
  168. * DECODE_OR (mask1, value1)
  169. * DECODE_OR (mask2, value2)
  170. * DECODE_SIMULATE (mask3, value3, simulation_handler)
  171. *
  172. * This means that if any of the three mask/value pairs match the
  173. * instruction being decoded, then 'simulation_handler' will be used
  174. * for it.
  175. *
  176. * Both the SIMULATE and EMULATE macros have a second form which take an
  177. * additional 'regs' argument.
  178. *
  179. * DECODE_SIMULATEX(mask, value, handler, regs)
  180. * DECODE_EMULATEX (mask, value, handler, regs)
  181. *
  182. * These are used to specify what kind of CPU register is encoded in each of the
  183. * least significant 5 nibbles of the instruction being decoded. The regs value
  184. * is specified using the REGS macro, this takes any of the REG_TYPE_* values
  185. * from enum decode_reg_type as arguments; only the '*' part of the name is
  186. * given. E.g.
  187. *
  188. * REGS(0, ANY, NOPC, 0, ANY)
  189. *
  190. * This indicates an instruction is encoded like:
  191. *
  192. * bits 19..16 ignore
  193. * bits 15..12 any register allowed here
  194. * bits 11.. 8 any register except PC allowed here
  195. * bits 7.. 4 ignore
  196. * bits 3.. 0 any register allowed here
  197. *
  198. * This register specification is checked after a decode table entry is found to
  199. * match an instruction (through the mask/value test). Any invalid register then
  200. * found in the instruction will cause decoding to fail with INSN_REJECTED. In
  201. * the above example this would happen if bits 11..8 of the instruction were
  202. * 1111, indicating R15 or PC.
  203. *
  204. * As well as checking for legal combinations of registers, this data is also
  205. * used to modify the registers encoded in the instructions so that an
  206. * emulation routines can use it. (See decode_regs() and INSN_NEW_BITS.)
  207. *
  208. * Here is a real example which matches ARM instructions of the form
  209. * "AND <Rd>,<Rn>,<Rm>,<shift> <Rs>"
  210. *
  211. * DECODE_EMULATEX (0x0e000090, 0x00000010, emulate_rd12rn16rm0rs8_rwflags,
  212. * REGS(ANY, ANY, NOPC, 0, ANY)),
  213. * ^ ^ ^ ^
  214. * Rn Rd Rs Rm
  215. *
  216. * Decoding the instruction "AND R4, R5, R6, ASL R15" will be rejected because
  217. * Rs == R15
  218. *
  219. * Decoding the instruction "AND R4, R5, R6, ASL R7" will be accepted and the
  220. * instruction will be modified to "AND R0, R2, R3, ASL R1" and then placed into
  221. * the kprobes instruction slot. This can then be called later by the handler
  222. * function emulate_rd12rn16rm0rs8_rwflags in order to simulate the instruction.
  223. */
  224. enum decode_type {
  225. DECODE_TYPE_END,
  226. DECODE_TYPE_TABLE,
  227. DECODE_TYPE_CUSTOM,
  228. DECODE_TYPE_SIMULATE,
  229. DECODE_TYPE_EMULATE,
  230. DECODE_TYPE_OR,
  231. DECODE_TYPE_REJECT,
  232. NUM_DECODE_TYPES /* Must be last enum */
  233. };
  234. #define DECODE_TYPE_BITS 4
  235. #define DECODE_TYPE_MASK ((1 << DECODE_TYPE_BITS) - 1)
  236. enum decode_reg_type {
  237. REG_TYPE_NONE = 0, /* Not a register, ignore */
  238. REG_TYPE_ANY, /* Any register allowed */
  239. REG_TYPE_SAMEAS16, /* Register should be same as that at bits 19..16 */
  240. REG_TYPE_SP, /* Register must be SP */
  241. REG_TYPE_PC, /* Register must be PC */
  242. REG_TYPE_NOSP, /* Register must not be SP */
  243. REG_TYPE_NOSPPC, /* Register must not be SP or PC */
  244. REG_TYPE_NOPC, /* Register must not be PC */
  245. REG_TYPE_NOPCWB, /* No PC if load/store write-back flag also set */
  246. /* The following types are used when the encoding for PC indicates
  247. * another instruction form. This distiction only matters for test
  248. * case coverage checks.
  249. */
  250. REG_TYPE_NOPCX, /* Register must not be PC */
  251. REG_TYPE_NOSPPCX, /* Register must not be SP or PC */
  252. /* Alias to allow '0' arg to be used in REGS macro. */
  253. REG_TYPE_0 = REG_TYPE_NONE
  254. };
  255. #define REGS(r16, r12, r8, r4, r0) \
  256. ((REG_TYPE_##r16) << 16) + \
  257. ((REG_TYPE_##r12) << 12) + \
  258. ((REG_TYPE_##r8) << 8) + \
  259. ((REG_TYPE_##r4) << 4) + \
  260. (REG_TYPE_##r0)
  261. union decode_item {
  262. u32 bits;
  263. const union decode_item *table;
  264. kprobe_insn_handler_t *handler;
  265. kprobe_decode_insn_t *decoder;
  266. };
  267. #define DECODE_END \
  268. {.bits = DECODE_TYPE_END}
  269. struct decode_header {
  270. union decode_item type_regs;
  271. union decode_item mask;
  272. union decode_item value;
  273. };
  274. #define DECODE_HEADER(_type, _mask, _value, _regs) \
  275. {.bits = (_type) | ((_regs) << DECODE_TYPE_BITS)}, \
  276. {.bits = (_mask)}, \
  277. {.bits = (_value)}
  278. struct decode_table {
  279. struct decode_header header;
  280. union decode_item table;
  281. };
  282. #define DECODE_TABLE(_mask, _value, _table) \
  283. DECODE_HEADER(DECODE_TYPE_TABLE, _mask, _value, 0), \
  284. {.table = (_table)}
  285. struct decode_custom {
  286. struct decode_header header;
  287. union decode_item decoder;
  288. };
  289. #define DECODE_CUSTOM(_mask, _value, _decoder) \
  290. DECODE_HEADER(DECODE_TYPE_CUSTOM, _mask, _value, 0), \
  291. {.decoder = (_decoder)}
  292. struct decode_simulate {
  293. struct decode_header header;
  294. union decode_item handler;
  295. };
  296. #define DECODE_SIMULATEX(_mask, _value, _handler, _regs) \
  297. DECODE_HEADER(DECODE_TYPE_SIMULATE, _mask, _value, _regs), \
  298. {.handler = (_handler)}
  299. #define DECODE_SIMULATE(_mask, _value, _handler) \
  300. DECODE_SIMULATEX(_mask, _value, _handler, 0)
  301. struct decode_emulate {
  302. struct decode_header header;
  303. union decode_item handler;
  304. };
  305. #define DECODE_EMULATEX(_mask, _value, _handler, _regs) \
  306. DECODE_HEADER(DECODE_TYPE_EMULATE, _mask, _value, _regs), \
  307. {.handler = (_handler)}
  308. #define DECODE_EMULATE(_mask, _value, _handler) \
  309. DECODE_EMULATEX(_mask, _value, _handler, 0)
  310. struct decode_or {
  311. struct decode_header header;
  312. };
  313. #define DECODE_OR(_mask, _value) \
  314. DECODE_HEADER(DECODE_TYPE_OR, _mask, _value, 0)
  315. struct decode_reject {
  316. struct decode_header header;
  317. };
  318. #define DECODE_REJECT(_mask, _value) \
  319. DECODE_HEADER(DECODE_TYPE_REJECT, _mask, _value, 0)
  320. int kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi,
  321. const union decode_item *table, bool thumb16);
  322. #endif /* _ARM_KERNEL_KPROBES_H */