kprobes-common.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * arch/arm/kernel/kprobes-common.c
  3. *
  4. * Copyright (C) 2011 Jon Medhurst <tixy@yxit.co.uk>.
  5. *
  6. * Some contents moved here from arch/arm/include/asm/kprobes-arm.c 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. #include <linux/kernel.h>
  14. #include <linux/kprobes.h>
  15. #include "kprobes.h"
  16. #ifndef find_str_pc_offset
  17. /*
  18. * For STR and STM instructions, an ARM core may choose to use either
  19. * a +8 or a +12 displacement from the current instruction's address.
  20. * Whichever value is chosen for a given core, it must be the same for
  21. * both instructions and may not change. This function measures it.
  22. */
  23. int str_pc_offset;
  24. void __init find_str_pc_offset(void)
  25. {
  26. int addr, scratch, ret;
  27. __asm__ (
  28. "sub %[ret], pc, #4 \n\t"
  29. "str pc, %[addr] \n\t"
  30. "ldr %[scr], %[addr] \n\t"
  31. "sub %[ret], %[scr], %[ret] \n\t"
  32. : [ret] "=r" (ret), [scr] "=r" (scratch), [addr] "+m" (addr));
  33. str_pc_offset = ret;
  34. }
  35. #endif /* !find_str_pc_offset */
  36. void __init arm_kprobe_decode_init(void)
  37. {
  38. find_str_pc_offset();
  39. }
  40. static unsigned long __kprobes __check_eq(unsigned long cpsr)
  41. {
  42. return cpsr & PSR_Z_BIT;
  43. }
  44. static unsigned long __kprobes __check_ne(unsigned long cpsr)
  45. {
  46. return (~cpsr) & PSR_Z_BIT;
  47. }
  48. static unsigned long __kprobes __check_cs(unsigned long cpsr)
  49. {
  50. return cpsr & PSR_C_BIT;
  51. }
  52. static unsigned long __kprobes __check_cc(unsigned long cpsr)
  53. {
  54. return (~cpsr) & PSR_C_BIT;
  55. }
  56. static unsigned long __kprobes __check_mi(unsigned long cpsr)
  57. {
  58. return cpsr & PSR_N_BIT;
  59. }
  60. static unsigned long __kprobes __check_pl(unsigned long cpsr)
  61. {
  62. return (~cpsr) & PSR_N_BIT;
  63. }
  64. static unsigned long __kprobes __check_vs(unsigned long cpsr)
  65. {
  66. return cpsr & PSR_V_BIT;
  67. }
  68. static unsigned long __kprobes __check_vc(unsigned long cpsr)
  69. {
  70. return (~cpsr) & PSR_V_BIT;
  71. }
  72. static unsigned long __kprobes __check_hi(unsigned long cpsr)
  73. {
  74. cpsr &= ~(cpsr >> 1); /* PSR_C_BIT &= ~PSR_Z_BIT */
  75. return cpsr & PSR_C_BIT;
  76. }
  77. static unsigned long __kprobes __check_ls(unsigned long cpsr)
  78. {
  79. cpsr &= ~(cpsr >> 1); /* PSR_C_BIT &= ~PSR_Z_BIT */
  80. return (~cpsr) & PSR_C_BIT;
  81. }
  82. static unsigned long __kprobes __check_ge(unsigned long cpsr)
  83. {
  84. cpsr ^= (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */
  85. return (~cpsr) & PSR_N_BIT;
  86. }
  87. static unsigned long __kprobes __check_lt(unsigned long cpsr)
  88. {
  89. cpsr ^= (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */
  90. return cpsr & PSR_N_BIT;
  91. }
  92. static unsigned long __kprobes __check_gt(unsigned long cpsr)
  93. {
  94. unsigned long temp = cpsr ^ (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */
  95. temp |= (cpsr << 1); /* PSR_N_BIT |= PSR_Z_BIT */
  96. return (~temp) & PSR_N_BIT;
  97. }
  98. static unsigned long __kprobes __check_le(unsigned long cpsr)
  99. {
  100. unsigned long temp = cpsr ^ (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */
  101. temp |= (cpsr << 1); /* PSR_N_BIT |= PSR_Z_BIT */
  102. return temp & PSR_N_BIT;
  103. }
  104. static unsigned long __kprobes __check_al(unsigned long cpsr)
  105. {
  106. return true;
  107. }
  108. kprobe_check_cc * const kprobe_condition_checks[16] = {
  109. &__check_eq, &__check_ne, &__check_cs, &__check_cc,
  110. &__check_mi, &__check_pl, &__check_vs, &__check_vc,
  111. &__check_hi, &__check_ls, &__check_ge, &__check_lt,
  112. &__check_gt, &__check_le, &__check_al, &__check_al
  113. };
  114. void __kprobes kprobe_simulate_nop(struct kprobe *p, struct pt_regs *regs)
  115. {
  116. }
  117. void __kprobes kprobe_emulate_none(struct kprobe *p, struct pt_regs *regs)
  118. {
  119. p->ainsn.insn_fn();
  120. }
  121. /*
  122. * Prepare an instruction slot to receive an instruction for emulating.
  123. * This is done by placing a subroutine return after the location where the
  124. * instruction will be placed. We also modify ARM instructions to be
  125. * unconditional as the condition code will already be checked before any
  126. * emulation handler is called.
  127. */
  128. static kprobe_opcode_t __kprobes
  129. prepare_emulated_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi,
  130. bool thumb)
  131. {
  132. #ifdef CONFIG_THUMB2_KERNEL
  133. if (thumb) {
  134. u16 *thumb_insn = (u16 *)asi->insn;
  135. thumb_insn[1] = 0x4770; /* Thumb bx lr */
  136. thumb_insn[2] = 0x4770; /* Thumb bx lr */
  137. return insn;
  138. }
  139. asi->insn[1] = 0xe12fff1e; /* ARM bx lr */
  140. #else
  141. asi->insn[1] = 0xe1a0f00e; /* mov pc, lr */
  142. #endif
  143. /* Make an ARM instruction unconditional */
  144. if (insn < 0xe0000000)
  145. insn = (insn | 0xe0000000) & ~0x10000000;
  146. return insn;
  147. }
  148. /*
  149. * Write a (probably modified) instruction into the slot previously prepared by
  150. * prepare_emulated_insn
  151. */
  152. static void __kprobes
  153. set_emulated_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi,
  154. bool thumb)
  155. {
  156. #ifdef CONFIG_THUMB2_KERNEL
  157. if (thumb) {
  158. u16 *ip = (u16 *)asi->insn;
  159. if (is_wide_instruction(insn))
  160. *ip++ = insn >> 16;
  161. *ip++ = insn;
  162. return;
  163. }
  164. #endif
  165. asi->insn[0] = insn;
  166. }
  167. /*
  168. * When we modify the register numbers encoded in an instruction to be emulated,
  169. * the new values come from this define. For ARM and 32-bit Thumb instructions
  170. * this gives...
  171. *
  172. * bit position 16 12 8 4 0
  173. * ---------------+---+---+---+---+---+
  174. * register r2 r0 r1 -- r3
  175. */
  176. #define INSN_NEW_BITS 0x00020103
  177. /* Each nibble has same value as that at INSN_NEW_BITS bit 16 */
  178. #define INSN_SAMEAS16_BITS 0x22222222
  179. /*
  180. * Validate and modify each of the registers encoded in an instruction.
  181. *
  182. * Each nibble in regs contains a value from enum decode_reg_type. For each
  183. * non-zero value, the corresponding nibble in pinsn is validated and modified
  184. * according to the type.
  185. */
  186. static bool __kprobes decode_regs(kprobe_opcode_t* pinsn, u32 regs)
  187. {
  188. kprobe_opcode_t insn = *pinsn;
  189. kprobe_opcode_t mask = 0xf; /* Start at least significant nibble */
  190. for (; regs != 0; regs >>= 4, mask <<= 4) {
  191. kprobe_opcode_t new_bits = INSN_NEW_BITS;
  192. switch (regs & 0xf) {
  193. case REG_TYPE_NONE:
  194. /* Nibble not a register, skip to next */
  195. continue;
  196. case REG_TYPE_ANY:
  197. /* Any register is allowed */
  198. break;
  199. case REG_TYPE_SAMEAS16:
  200. /* Replace register with same as at bit position 16 */
  201. new_bits = INSN_SAMEAS16_BITS;
  202. break;
  203. case REG_TYPE_SP:
  204. /* Only allow SP (R13) */
  205. if ((insn ^ 0xdddddddd) & mask)
  206. goto reject;
  207. break;
  208. case REG_TYPE_PC:
  209. /* Only allow PC (R15) */
  210. if ((insn ^ 0xffffffff) & mask)
  211. goto reject;
  212. break;
  213. case REG_TYPE_NOSP:
  214. /* Reject SP (R13) */
  215. if (((insn ^ 0xdddddddd) & mask) == 0)
  216. goto reject;
  217. break;
  218. case REG_TYPE_NOSPPC:
  219. case REG_TYPE_NOSPPCX:
  220. /* Reject SP and PC (R13 and R15) */
  221. if (((insn ^ 0xdddddddd) & 0xdddddddd & mask) == 0)
  222. goto reject;
  223. break;
  224. case REG_TYPE_NOPCWB:
  225. if (!is_writeback(insn))
  226. break; /* No writeback, so any register is OK */
  227. /* fall through... */
  228. case REG_TYPE_NOPC:
  229. case REG_TYPE_NOPCX:
  230. /* Reject PC (R15) */
  231. if (((insn ^ 0xffffffff) & mask) == 0)
  232. goto reject;
  233. break;
  234. }
  235. /* Replace value of nibble with new register number... */
  236. insn &= ~mask;
  237. insn |= new_bits & mask;
  238. }
  239. *pinsn = insn;
  240. return true;
  241. reject:
  242. return false;
  243. }
  244. static const int decode_struct_sizes[NUM_DECODE_TYPES] = {
  245. [DECODE_TYPE_TABLE] = sizeof(struct decode_table),
  246. [DECODE_TYPE_CUSTOM] = sizeof(struct decode_custom),
  247. [DECODE_TYPE_SIMULATE] = sizeof(struct decode_simulate),
  248. [DECODE_TYPE_EMULATE] = sizeof(struct decode_emulate),
  249. [DECODE_TYPE_OR] = sizeof(struct decode_or),
  250. [DECODE_TYPE_REJECT] = sizeof(struct decode_reject)
  251. };
  252. /*
  253. * kprobe_decode_insn operates on data tables in order to decode an ARM
  254. * architecture instruction onto which a kprobe has been placed.
  255. *
  256. * These instruction decoding tables are a concatenation of entries each
  257. * of which consist of one of the following structs:
  258. *
  259. * decode_table
  260. * decode_custom
  261. * decode_simulate
  262. * decode_emulate
  263. * decode_or
  264. * decode_reject
  265. *
  266. * Each of these starts with a struct decode_header which has the following
  267. * fields:
  268. *
  269. * type_regs
  270. * mask
  271. * value
  272. *
  273. * The least significant DECODE_TYPE_BITS of type_regs contains a value
  274. * from enum decode_type, this indicates which of the decode_* structs
  275. * the entry contains. The value DECODE_TYPE_END indicates the end of the
  276. * table.
  277. *
  278. * When the table is parsed, each entry is checked in turn to see if it
  279. * matches the instruction to be decoded using the test:
  280. *
  281. * (insn & mask) == value
  282. *
  283. * If no match is found before the end of the table is reached then decoding
  284. * fails with INSN_REJECTED.
  285. *
  286. * When a match is found, decode_regs() is called to validate and modify each
  287. * of the registers encoded in the instruction; the data it uses to do this
  288. * is (type_regs >> DECODE_TYPE_BITS). A validation failure will cause decoding
  289. * to fail with INSN_REJECTED.
  290. *
  291. * Once the instruction has passed the above tests, further processing
  292. * depends on the type of the table entry's decode struct.
  293. *
  294. */
  295. int __kprobes
  296. kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi,
  297. const union decode_item *table, bool thumb)
  298. {
  299. const struct decode_header *h = (struct decode_header *)table;
  300. const struct decode_header *next;
  301. bool matched = false;
  302. insn = prepare_emulated_insn(insn, asi, thumb);
  303. for (;; h = next) {
  304. enum decode_type type = h->type_regs.bits & DECODE_TYPE_MASK;
  305. u32 regs = h->type_regs.bits >> DECODE_TYPE_BITS;
  306. if (type == DECODE_TYPE_END)
  307. return INSN_REJECTED;
  308. next = (struct decode_header *)
  309. ((uintptr_t)h + decode_struct_sizes[type]);
  310. if (!matched && (insn & h->mask.bits) != h->value.bits)
  311. continue;
  312. if (!decode_regs(&insn, regs))
  313. return INSN_REJECTED;
  314. switch (type) {
  315. case DECODE_TYPE_TABLE: {
  316. struct decode_table *d = (struct decode_table *)h;
  317. next = (struct decode_header *)d->table.table;
  318. break;
  319. }
  320. case DECODE_TYPE_CUSTOM: {
  321. struct decode_custom *d = (struct decode_custom *)h;
  322. return (*d->decoder.decoder)(insn, asi);
  323. }
  324. case DECODE_TYPE_SIMULATE: {
  325. struct decode_simulate *d = (struct decode_simulate *)h;
  326. asi->insn_handler = d->handler.handler;
  327. return INSN_GOOD_NO_SLOT;
  328. }
  329. case DECODE_TYPE_EMULATE: {
  330. struct decode_emulate *d = (struct decode_emulate *)h;
  331. asi->insn_handler = d->handler.handler;
  332. set_emulated_insn(insn, asi, thumb);
  333. return INSN_GOOD;
  334. }
  335. case DECODE_TYPE_OR:
  336. matched = true;
  337. break;
  338. case DECODE_TYPE_REJECT:
  339. default:
  340. return INSN_REJECTED;
  341. }
  342. }
  343. }