decode.c 13 KB

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