kprobes-common.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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 <asm/system_info.h>
  16. #include "kprobes.h"
  17. #ifndef find_str_pc_offset
  18. /*
  19. * For STR and STM instructions, an ARM core may choose to use either
  20. * a +8 or a +12 displacement from the current instruction's address.
  21. * Whichever value is chosen for a given core, it must be the same for
  22. * both instructions and may not change. This function measures it.
  23. */
  24. int str_pc_offset;
  25. void __init find_str_pc_offset(void)
  26. {
  27. int addr, scratch, ret;
  28. __asm__ (
  29. "sub %[ret], pc, #4 \n\t"
  30. "str pc, %[addr] \n\t"
  31. "ldr %[scr], %[addr] \n\t"
  32. "sub %[ret], %[scr], %[ret] \n\t"
  33. : [ret] "=r" (ret), [scr] "=r" (scratch), [addr] "+m" (addr));
  34. str_pc_offset = ret;
  35. }
  36. #endif /* !find_str_pc_offset */
  37. #ifndef test_load_write_pc_interworking
  38. bool load_write_pc_interworks;
  39. void __init test_load_write_pc_interworking(void)
  40. {
  41. int arch = cpu_architecture();
  42. BUG_ON(arch == CPU_ARCH_UNKNOWN);
  43. load_write_pc_interworks = arch >= CPU_ARCH_ARMv5T;
  44. }
  45. #endif /* !test_load_write_pc_interworking */
  46. #ifndef test_alu_write_pc_interworking
  47. bool alu_write_pc_interworks;
  48. void __init test_alu_write_pc_interworking(void)
  49. {
  50. int arch = cpu_architecture();
  51. BUG_ON(arch == CPU_ARCH_UNKNOWN);
  52. alu_write_pc_interworks = arch >= CPU_ARCH_ARMv7;
  53. }
  54. #endif /* !test_alu_write_pc_interworking */
  55. void __init arm_kprobe_decode_init(void)
  56. {
  57. find_str_pc_offset();
  58. test_load_write_pc_interworking();
  59. test_alu_write_pc_interworking();
  60. }
  61. static unsigned long __kprobes __check_eq(unsigned long cpsr)
  62. {
  63. return cpsr & PSR_Z_BIT;
  64. }
  65. static unsigned long __kprobes __check_ne(unsigned long cpsr)
  66. {
  67. return (~cpsr) & PSR_Z_BIT;
  68. }
  69. static unsigned long __kprobes __check_cs(unsigned long cpsr)
  70. {
  71. return cpsr & PSR_C_BIT;
  72. }
  73. static unsigned long __kprobes __check_cc(unsigned long cpsr)
  74. {
  75. return (~cpsr) & PSR_C_BIT;
  76. }
  77. static unsigned long __kprobes __check_mi(unsigned long cpsr)
  78. {
  79. return cpsr & PSR_N_BIT;
  80. }
  81. static unsigned long __kprobes __check_pl(unsigned long cpsr)
  82. {
  83. return (~cpsr) & PSR_N_BIT;
  84. }
  85. static unsigned long __kprobes __check_vs(unsigned long cpsr)
  86. {
  87. return cpsr & PSR_V_BIT;
  88. }
  89. static unsigned long __kprobes __check_vc(unsigned long cpsr)
  90. {
  91. return (~cpsr) & PSR_V_BIT;
  92. }
  93. static unsigned long __kprobes __check_hi(unsigned long cpsr)
  94. {
  95. cpsr &= ~(cpsr >> 1); /* PSR_C_BIT &= ~PSR_Z_BIT */
  96. return cpsr & PSR_C_BIT;
  97. }
  98. static unsigned long __kprobes __check_ls(unsigned long cpsr)
  99. {
  100. cpsr &= ~(cpsr >> 1); /* PSR_C_BIT &= ~PSR_Z_BIT */
  101. return (~cpsr) & PSR_C_BIT;
  102. }
  103. static unsigned long __kprobes __check_ge(unsigned long cpsr)
  104. {
  105. cpsr ^= (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */
  106. return (~cpsr) & PSR_N_BIT;
  107. }
  108. static unsigned long __kprobes __check_lt(unsigned long cpsr)
  109. {
  110. cpsr ^= (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */
  111. return cpsr & PSR_N_BIT;
  112. }
  113. static unsigned long __kprobes __check_gt(unsigned long cpsr)
  114. {
  115. unsigned long temp = cpsr ^ (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */
  116. temp |= (cpsr << 1); /* PSR_N_BIT |= PSR_Z_BIT */
  117. return (~temp) & PSR_N_BIT;
  118. }
  119. static unsigned long __kprobes __check_le(unsigned long cpsr)
  120. {
  121. unsigned long temp = cpsr ^ (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */
  122. temp |= (cpsr << 1); /* PSR_N_BIT |= PSR_Z_BIT */
  123. return temp & PSR_N_BIT;
  124. }
  125. static unsigned long __kprobes __check_al(unsigned long cpsr)
  126. {
  127. return true;
  128. }
  129. kprobe_check_cc * const kprobe_condition_checks[16] = {
  130. &__check_eq, &__check_ne, &__check_cs, &__check_cc,
  131. &__check_mi, &__check_pl, &__check_vs, &__check_vc,
  132. &__check_hi, &__check_ls, &__check_ge, &__check_lt,
  133. &__check_gt, &__check_le, &__check_al, &__check_al
  134. };
  135. void __kprobes kprobe_simulate_nop(struct kprobe *p, struct pt_regs *regs)
  136. {
  137. }
  138. void __kprobes kprobe_emulate_none(struct kprobe *p, struct pt_regs *regs)
  139. {
  140. p->ainsn.insn_fn();
  141. }
  142. static void __kprobes simulate_ldm1stm1(struct kprobe *p, struct pt_regs *regs)
  143. {
  144. kprobe_opcode_t insn = p->opcode;
  145. int rn = (insn >> 16) & 0xf;
  146. int lbit = insn & (1 << 20);
  147. int wbit = insn & (1 << 21);
  148. int ubit = insn & (1 << 23);
  149. int pbit = insn & (1 << 24);
  150. long *addr = (long *)regs->uregs[rn];
  151. int reg_bit_vector;
  152. int reg_count;
  153. reg_count = 0;
  154. reg_bit_vector = insn & 0xffff;
  155. while (reg_bit_vector) {
  156. reg_bit_vector &= (reg_bit_vector - 1);
  157. ++reg_count;
  158. }
  159. if (!ubit)
  160. addr -= reg_count;
  161. addr += (!pbit == !ubit);
  162. reg_bit_vector = insn & 0xffff;
  163. while (reg_bit_vector) {
  164. int reg = __ffs(reg_bit_vector);
  165. reg_bit_vector &= (reg_bit_vector - 1);
  166. if (lbit)
  167. regs->uregs[reg] = *addr++;
  168. else
  169. *addr++ = regs->uregs[reg];
  170. }
  171. if (wbit) {
  172. if (!ubit)
  173. addr -= reg_count;
  174. addr -= (!pbit == !ubit);
  175. regs->uregs[rn] = (long)addr;
  176. }
  177. }
  178. static void __kprobes simulate_stm1_pc(struct kprobe *p, struct pt_regs *regs)
  179. {
  180. regs->ARM_pc = (long)p->addr + str_pc_offset;
  181. simulate_ldm1stm1(p, regs);
  182. regs->ARM_pc = (long)p->addr + 4;
  183. }
  184. static void __kprobes simulate_ldm1_pc(struct kprobe *p, struct pt_regs *regs)
  185. {
  186. simulate_ldm1stm1(p, regs);
  187. load_write_pc(regs->ARM_pc, regs);
  188. }
  189. static void __kprobes
  190. emulate_generic_r0_12_noflags(struct kprobe *p, struct pt_regs *regs)
  191. {
  192. register void *rregs asm("r1") = regs;
  193. register void *rfn asm("lr") = p->ainsn.insn_fn;
  194. __asm__ __volatile__ (
  195. "stmdb sp!, {%[regs], r11} \n\t"
  196. "ldmia %[regs], {r0-r12} \n\t"
  197. #if __LINUX_ARM_ARCH__ >= 6
  198. "blx %[fn] \n\t"
  199. #else
  200. "str %[fn], [sp, #-4]! \n\t"
  201. "adr lr, 1f \n\t"
  202. "ldr pc, [sp], #4 \n\t"
  203. "1: \n\t"
  204. #endif
  205. "ldr lr, [sp], #4 \n\t" /* lr = regs */
  206. "stmia lr, {r0-r12} \n\t"
  207. "ldr r11, [sp], #4 \n\t"
  208. : [regs] "=r" (rregs), [fn] "=r" (rfn)
  209. : "0" (rregs), "1" (rfn)
  210. : "r0", "r2", "r3", "r4", "r5", "r6", "r7",
  211. "r8", "r9", "r10", "r12", "memory", "cc"
  212. );
  213. }
  214. static void __kprobes
  215. emulate_generic_r2_14_noflags(struct kprobe *p, struct pt_regs *regs)
  216. {
  217. emulate_generic_r0_12_noflags(p, (struct pt_regs *)(regs->uregs+2));
  218. }
  219. static void __kprobes
  220. emulate_ldm_r3_15(struct kprobe *p, struct pt_regs *regs)
  221. {
  222. emulate_generic_r0_12_noflags(p, (struct pt_regs *)(regs->uregs+3));
  223. load_write_pc(regs->ARM_pc, regs);
  224. }
  225. enum kprobe_insn __kprobes
  226. kprobe_decode_ldmstm(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  227. {
  228. kprobe_insn_handler_t *handler = 0;
  229. unsigned reglist = insn & 0xffff;
  230. int is_ldm = insn & 0x100000;
  231. int rn = (insn >> 16) & 0xf;
  232. if (rn <= 12 && (reglist & 0xe000) == 0) {
  233. /* Instruction only uses registers in the range R0..R12 */
  234. handler = emulate_generic_r0_12_noflags;
  235. } else if (rn >= 2 && (reglist & 0x8003) == 0) {
  236. /* Instruction only uses registers in the range R2..R14 */
  237. rn -= 2;
  238. reglist >>= 2;
  239. handler = emulate_generic_r2_14_noflags;
  240. } else if (rn >= 3 && (reglist & 0x0007) == 0) {
  241. /* Instruction only uses registers in the range R3..R15 */
  242. if (is_ldm && (reglist & 0x8000)) {
  243. rn -= 3;
  244. reglist >>= 3;
  245. handler = emulate_ldm_r3_15;
  246. }
  247. }
  248. if (handler) {
  249. /* We can emulate the instruction in (possibly) modified form */
  250. asi->insn[0] = (insn & 0xfff00000) | (rn << 16) | reglist;
  251. asi->insn_handler = handler;
  252. return INSN_GOOD;
  253. }
  254. /* Fallback to slower simulation... */
  255. if (reglist & 0x8000)
  256. handler = is_ldm ? simulate_ldm1_pc : simulate_stm1_pc;
  257. else
  258. handler = simulate_ldm1stm1;
  259. asi->insn_handler = handler;
  260. return INSN_GOOD_NO_SLOT;
  261. }
  262. /*
  263. * Prepare an instruction slot to receive an instruction for emulating.
  264. * This is done by placing a subroutine return after the location where the
  265. * instruction will be placed. We also modify ARM instructions to be
  266. * unconditional as the condition code will already be checked before any
  267. * emulation handler is called.
  268. */
  269. static kprobe_opcode_t __kprobes
  270. prepare_emulated_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi,
  271. bool thumb)
  272. {
  273. #ifdef CONFIG_THUMB2_KERNEL
  274. if (thumb) {
  275. u16 *thumb_insn = (u16 *)asi->insn;
  276. thumb_insn[1] = 0x4770; /* Thumb bx lr */
  277. thumb_insn[2] = 0x4770; /* Thumb bx lr */
  278. return insn;
  279. }
  280. asi->insn[1] = 0xe12fff1e; /* ARM bx lr */
  281. #else
  282. asi->insn[1] = 0xe1a0f00e; /* mov pc, lr */
  283. #endif
  284. /* Make an ARM instruction unconditional */
  285. if (insn < 0xe0000000)
  286. insn = (insn | 0xe0000000) & ~0x10000000;
  287. return insn;
  288. }
  289. /*
  290. * Write a (probably modified) instruction into the slot previously prepared by
  291. * prepare_emulated_insn
  292. */
  293. static void __kprobes
  294. set_emulated_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi,
  295. bool thumb)
  296. {
  297. #ifdef CONFIG_THUMB2_KERNEL
  298. if (thumb) {
  299. u16 *ip = (u16 *)asi->insn;
  300. if (is_wide_instruction(insn))
  301. *ip++ = insn >> 16;
  302. *ip++ = insn;
  303. return;
  304. }
  305. #endif
  306. asi->insn[0] = insn;
  307. }
  308. /*
  309. * When we modify the register numbers encoded in an instruction to be emulated,
  310. * the new values come from this define. For ARM and 32-bit Thumb instructions
  311. * this gives...
  312. *
  313. * bit position 16 12 8 4 0
  314. * ---------------+---+---+---+---+---+
  315. * register r2 r0 r1 -- r3
  316. */
  317. #define INSN_NEW_BITS 0x00020103
  318. /* Each nibble has same value as that at INSN_NEW_BITS bit 16 */
  319. #define INSN_SAMEAS16_BITS 0x22222222
  320. /*
  321. * Validate and modify each of the registers encoded in an instruction.
  322. *
  323. * Each nibble in regs contains a value from enum decode_reg_type. For each
  324. * non-zero value, the corresponding nibble in pinsn is validated and modified
  325. * according to the type.
  326. */
  327. static bool __kprobes decode_regs(kprobe_opcode_t* pinsn, u32 regs)
  328. {
  329. kprobe_opcode_t insn = *pinsn;
  330. kprobe_opcode_t mask = 0xf; /* Start at least significant nibble */
  331. for (; regs != 0; regs >>= 4, mask <<= 4) {
  332. kprobe_opcode_t new_bits = INSN_NEW_BITS;
  333. switch (regs & 0xf) {
  334. case REG_TYPE_NONE:
  335. /* Nibble not a register, skip to next */
  336. continue;
  337. case REG_TYPE_ANY:
  338. /* Any register is allowed */
  339. break;
  340. case REG_TYPE_SAMEAS16:
  341. /* Replace register with same as at bit position 16 */
  342. new_bits = INSN_SAMEAS16_BITS;
  343. break;
  344. case REG_TYPE_SP:
  345. /* Only allow SP (R13) */
  346. if ((insn ^ 0xdddddddd) & mask)
  347. goto reject;
  348. break;
  349. case REG_TYPE_PC:
  350. /* Only allow PC (R15) */
  351. if ((insn ^ 0xffffffff) & mask)
  352. goto reject;
  353. break;
  354. case REG_TYPE_NOSP:
  355. /* Reject SP (R13) */
  356. if (((insn ^ 0xdddddddd) & mask) == 0)
  357. goto reject;
  358. break;
  359. case REG_TYPE_NOSPPC:
  360. case REG_TYPE_NOSPPCX:
  361. /* Reject SP and PC (R13 and R15) */
  362. if (((insn ^ 0xdddddddd) & 0xdddddddd & mask) == 0)
  363. goto reject;
  364. break;
  365. case REG_TYPE_NOPCWB:
  366. if (!is_writeback(insn))
  367. break; /* No writeback, so any register is OK */
  368. /* fall through... */
  369. case REG_TYPE_NOPC:
  370. case REG_TYPE_NOPCX:
  371. /* Reject PC (R15) */
  372. if (((insn ^ 0xffffffff) & mask) == 0)
  373. goto reject;
  374. break;
  375. }
  376. /* Replace value of nibble with new register number... */
  377. insn &= ~mask;
  378. insn |= new_bits & mask;
  379. }
  380. *pinsn = insn;
  381. return true;
  382. reject:
  383. return false;
  384. }
  385. static const int decode_struct_sizes[NUM_DECODE_TYPES] = {
  386. [DECODE_TYPE_TABLE] = sizeof(struct decode_table),
  387. [DECODE_TYPE_CUSTOM] = sizeof(struct decode_custom),
  388. [DECODE_TYPE_SIMULATE] = sizeof(struct decode_simulate),
  389. [DECODE_TYPE_EMULATE] = sizeof(struct decode_emulate),
  390. [DECODE_TYPE_OR] = sizeof(struct decode_or),
  391. [DECODE_TYPE_REJECT] = sizeof(struct decode_reject)
  392. };
  393. /*
  394. * kprobe_decode_insn operates on data tables in order to decode an ARM
  395. * architecture instruction onto which a kprobe has been placed.
  396. *
  397. * These instruction decoding tables are a concatenation of entries each
  398. * of which consist of one of the following structs:
  399. *
  400. * decode_table
  401. * decode_custom
  402. * decode_simulate
  403. * decode_emulate
  404. * decode_or
  405. * decode_reject
  406. *
  407. * Each of these starts with a struct decode_header which has the following
  408. * fields:
  409. *
  410. * type_regs
  411. * mask
  412. * value
  413. *
  414. * The least significant DECODE_TYPE_BITS of type_regs contains a value
  415. * from enum decode_type, this indicates which of the decode_* structs
  416. * the entry contains. The value DECODE_TYPE_END indicates the end of the
  417. * table.
  418. *
  419. * When the table is parsed, each entry is checked in turn to see if it
  420. * matches the instruction to be decoded using the test:
  421. *
  422. * (insn & mask) == value
  423. *
  424. * If no match is found before the end of the table is reached then decoding
  425. * fails with INSN_REJECTED.
  426. *
  427. * When a match is found, decode_regs() is called to validate and modify each
  428. * of the registers encoded in the instruction; the data it uses to do this
  429. * is (type_regs >> DECODE_TYPE_BITS). A validation failure will cause decoding
  430. * to fail with INSN_REJECTED.
  431. *
  432. * Once the instruction has passed the above tests, further processing
  433. * depends on the type of the table entry's decode struct.
  434. *
  435. */
  436. int __kprobes
  437. kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi,
  438. const union decode_item *table, bool thumb)
  439. {
  440. const struct decode_header *h = (struct decode_header *)table;
  441. const struct decode_header *next;
  442. bool matched = false;
  443. insn = prepare_emulated_insn(insn, asi, thumb);
  444. for (;; h = next) {
  445. enum decode_type type = h->type_regs.bits & DECODE_TYPE_MASK;
  446. u32 regs = h->type_regs.bits >> DECODE_TYPE_BITS;
  447. if (type == DECODE_TYPE_END)
  448. return INSN_REJECTED;
  449. next = (struct decode_header *)
  450. ((uintptr_t)h + decode_struct_sizes[type]);
  451. if (!matched && (insn & h->mask.bits) != h->value.bits)
  452. continue;
  453. if (!decode_regs(&insn, regs))
  454. return INSN_REJECTED;
  455. switch (type) {
  456. case DECODE_TYPE_TABLE: {
  457. struct decode_table *d = (struct decode_table *)h;
  458. next = (struct decode_header *)d->table.table;
  459. break;
  460. }
  461. case DECODE_TYPE_CUSTOM: {
  462. struct decode_custom *d = (struct decode_custom *)h;
  463. return (*d->decoder.decoder)(insn, asi);
  464. }
  465. case DECODE_TYPE_SIMULATE: {
  466. struct decode_simulate *d = (struct decode_simulate *)h;
  467. asi->insn_handler = d->handler.handler;
  468. return INSN_GOOD_NO_SLOT;
  469. }
  470. case DECODE_TYPE_EMULATE: {
  471. struct decode_emulate *d = (struct decode_emulate *)h;
  472. asi->insn_handler = d->handler.handler;
  473. set_emulated_insn(insn, asi, thumb);
  474. return INSN_GOOD;
  475. }
  476. case DECODE_TYPE_OR:
  477. matched = true;
  478. break;
  479. case DECODE_TYPE_REJECT:
  480. default:
  481. return INSN_REJECTED;
  482. }
  483. }
  484. }