kprobes-common.c 14 KB

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