kprobes-common.c 10 KB

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