kprobes-thumb.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * arch/arm/kernel/kprobes-thumb.c
  3. *
  4. * Copyright (C) 2011 Jon Medhurst <tixy@yxit.co.uk>.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/kprobes.h>
  12. #include "kprobes.h"
  13. /*
  14. * True if current instruction is in an IT block.
  15. */
  16. #define in_it_block(cpsr) ((cpsr & 0x06000c00) != 0x00000000)
  17. /*
  18. * Return the condition code to check for the currently executing instruction.
  19. * This is in ITSTATE<7:4> which is in CPSR<15:12> but is only valid if
  20. * in_it_block returns true.
  21. */
  22. #define current_cond(cpsr) ((cpsr >> 12) & 0xf)
  23. /*
  24. * Return the PC value for a probe in thumb code.
  25. * This is the address of the probed instruction plus 4.
  26. * We subtract one because the address will have bit zero set to indicate
  27. * a pointer to thumb code.
  28. */
  29. static inline unsigned long __kprobes thumb_probe_pc(struct kprobe *p)
  30. {
  31. return (unsigned long)p->addr - 1 + 4;
  32. }
  33. static void __kprobes
  34. t16_simulate_bxblx(struct kprobe *p, struct pt_regs *regs)
  35. {
  36. kprobe_opcode_t insn = p->opcode;
  37. unsigned long pc = thumb_probe_pc(p);
  38. int rm = (insn >> 3) & 0xf;
  39. unsigned long rmv = (rm == 15) ? pc : regs->uregs[rm];
  40. if (insn & (1 << 7)) /* BLX ? */
  41. regs->ARM_lr = (unsigned long)p->addr + 2;
  42. bx_write_pc(rmv, regs);
  43. }
  44. static void __kprobes
  45. t16_simulate_ldr_literal(struct kprobe *p, struct pt_regs *regs)
  46. {
  47. kprobe_opcode_t insn = p->opcode;
  48. unsigned long* base = (unsigned long *)(thumb_probe_pc(p) & ~3);
  49. long index = insn & 0xff;
  50. int rt = (insn >> 8) & 0x7;
  51. regs->uregs[rt] = base[index];
  52. }
  53. static void __kprobes
  54. t16_simulate_ldrstr_sp_relative(struct kprobe *p, struct pt_regs *regs)
  55. {
  56. kprobe_opcode_t insn = p->opcode;
  57. unsigned long* base = (unsigned long *)regs->ARM_sp;
  58. long index = insn & 0xff;
  59. int rt = (insn >> 8) & 0x7;
  60. if (insn & 0x800) /* LDR */
  61. regs->uregs[rt] = base[index];
  62. else /* STR */
  63. base[index] = regs->uregs[rt];
  64. }
  65. static unsigned long __kprobes
  66. t16_emulate_loregs(struct kprobe *p, struct pt_regs *regs)
  67. {
  68. unsigned long oldcpsr = regs->ARM_cpsr;
  69. unsigned long newcpsr;
  70. __asm__ __volatile__ (
  71. "msr cpsr_fs, %[oldcpsr] \n\t"
  72. "ldmia %[regs], {r0-r7} \n\t"
  73. "blx %[fn] \n\t"
  74. "stmia %[regs], {r0-r7} \n\t"
  75. "mrs %[newcpsr], cpsr \n\t"
  76. : [newcpsr] "=r" (newcpsr)
  77. : [oldcpsr] "r" (oldcpsr), [regs] "r" (regs),
  78. [fn] "r" (p->ainsn.insn_fn)
  79. : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  80. "lr", "memory", "cc"
  81. );
  82. return (oldcpsr & ~APSR_MASK) | (newcpsr & APSR_MASK);
  83. }
  84. static void __kprobes
  85. t16_emulate_loregs_rwflags(struct kprobe *p, struct pt_regs *regs)
  86. {
  87. regs->ARM_cpsr = t16_emulate_loregs(p, regs);
  88. }
  89. static void __kprobes
  90. t16_emulate_loregs_noitrwflags(struct kprobe *p, struct pt_regs *regs)
  91. {
  92. unsigned long cpsr = t16_emulate_loregs(p, regs);
  93. if (!in_it_block(cpsr))
  94. regs->ARM_cpsr = cpsr;
  95. }
  96. static void __kprobes
  97. t16_emulate_hiregs(struct kprobe *p, struct pt_regs *regs)
  98. {
  99. kprobe_opcode_t insn = p->opcode;
  100. unsigned long pc = thumb_probe_pc(p);
  101. int rdn = (insn & 0x7) | ((insn & 0x80) >> 4);
  102. int rm = (insn >> 3) & 0xf;
  103. register unsigned long rdnv asm("r1");
  104. register unsigned long rmv asm("r0");
  105. unsigned long cpsr = regs->ARM_cpsr;
  106. rdnv = (rdn == 15) ? pc : regs->uregs[rdn];
  107. rmv = (rm == 15) ? pc : regs->uregs[rm];
  108. __asm__ __volatile__ (
  109. "msr cpsr_fs, %[cpsr] \n\t"
  110. "blx %[fn] \n\t"
  111. "mrs %[cpsr], cpsr \n\t"
  112. : "=r" (rdnv), [cpsr] "=r" (cpsr)
  113. : "0" (rdnv), "r" (rmv), "1" (cpsr), [fn] "r" (p->ainsn.insn_fn)
  114. : "lr", "memory", "cc"
  115. );
  116. if (rdn == 15)
  117. rdnv &= ~1;
  118. regs->uregs[rdn] = rdnv;
  119. regs->ARM_cpsr = (regs->ARM_cpsr & ~APSR_MASK) | (cpsr & APSR_MASK);
  120. }
  121. static enum kprobe_insn __kprobes
  122. t16_decode_hiregs(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  123. {
  124. insn &= ~0x00ff;
  125. insn |= 0x001; /* Set Rdn = R1 and Rm = R0 */
  126. ((u16 *)asi->insn)[0] = insn;
  127. asi->insn_handler = t16_emulate_hiregs;
  128. return INSN_GOOD;
  129. }
  130. static const union decode_item t16_table_1011[] = {
  131. /* Miscellaneous 16-bit instructions */
  132. /*
  133. * If-Then, and hints
  134. * 1011 1111 xxxx xxxx
  135. */
  136. /* YIELD 1011 1111 0001 0000 */
  137. DECODE_OR (0xffff, 0xbf10),
  138. /* SEV 1011 1111 0100 0000 */
  139. DECODE_EMULATE (0xffff, 0xbf40, kprobe_emulate_none),
  140. /* NOP 1011 1111 0000 0000 */
  141. /* WFE 1011 1111 0010 0000 */
  142. /* WFI 1011 1111 0011 0000 */
  143. DECODE_SIMULATE (0xffcf, 0xbf00, kprobe_simulate_nop),
  144. /* Unassigned hints 1011 1111 xxxx 0000 */
  145. DECODE_REJECT (0xff0f, 0xbf00),
  146. DECODE_END
  147. };
  148. const union decode_item kprobe_decode_thumb16_table[] = {
  149. /*
  150. * Shift (immediate), add, subtract, move, and compare
  151. * 00xx xxxx xxxx xxxx
  152. */
  153. /* CMP (immediate) 0010 1xxx xxxx xxxx */
  154. DECODE_EMULATE (0xf800, 0x2800, t16_emulate_loregs_rwflags),
  155. /* ADD (register) 0001 100x xxxx xxxx */
  156. /* SUB (register) 0001 101x xxxx xxxx */
  157. /* LSL (immediate) 0000 0xxx xxxx xxxx */
  158. /* LSR (immediate) 0000 1xxx xxxx xxxx */
  159. /* ASR (immediate) 0001 0xxx xxxx xxxx */
  160. /* ADD (immediate, Thumb) 0001 110x xxxx xxxx */
  161. /* SUB (immediate, Thumb) 0001 111x xxxx xxxx */
  162. /* MOV (immediate) 0010 0xxx xxxx xxxx */
  163. /* ADD (immediate, Thumb) 0011 0xxx xxxx xxxx */
  164. /* SUB (immediate, Thumb) 0011 1xxx xxxx xxxx */
  165. DECODE_EMULATE (0xc000, 0x0000, t16_emulate_loregs_noitrwflags),
  166. /*
  167. * 16-bit Thumb data-processing instructions
  168. * 0100 00xx xxxx xxxx
  169. */
  170. /* TST (register) 0100 0010 00xx xxxx */
  171. DECODE_EMULATE (0xffc0, 0x4200, t16_emulate_loregs_rwflags),
  172. /* CMP (register) 0100 0010 10xx xxxx */
  173. /* CMN (register) 0100 0010 11xx xxxx */
  174. DECODE_EMULATE (0xff80, 0x4280, t16_emulate_loregs_rwflags),
  175. /* AND (register) 0100 0000 00xx xxxx */
  176. /* EOR (register) 0100 0000 01xx xxxx */
  177. /* LSL (register) 0100 0000 10xx xxxx */
  178. /* LSR (register) 0100 0000 11xx xxxx */
  179. /* ASR (register) 0100 0001 00xx xxxx */
  180. /* ADC (register) 0100 0001 01xx xxxx */
  181. /* SBC (register) 0100 0001 10xx xxxx */
  182. /* ROR (register) 0100 0001 11xx xxxx */
  183. /* RSB (immediate) 0100 0010 01xx xxxx */
  184. /* ORR (register) 0100 0011 00xx xxxx */
  185. /* MUL 0100 0011 00xx xxxx */
  186. /* BIC (register) 0100 0011 10xx xxxx */
  187. /* MVN (register) 0100 0011 10xx xxxx */
  188. DECODE_EMULATE (0xfc00, 0x4000, t16_emulate_loregs_noitrwflags),
  189. /*
  190. * Special data instructions and branch and exchange
  191. * 0100 01xx xxxx xxxx
  192. */
  193. /* BLX pc 0100 0111 1111 1xxx */
  194. DECODE_REJECT (0xfff8, 0x47f8),
  195. /* BX (register) 0100 0111 0xxx xxxx */
  196. /* BLX (register) 0100 0111 1xxx xxxx */
  197. DECODE_SIMULATE (0xff00, 0x4700, t16_simulate_bxblx),
  198. /* ADD pc, pc 0100 0100 1111 1111 */
  199. DECODE_REJECT (0xffff, 0x44ff),
  200. /* ADD (register) 0100 0100 xxxx xxxx */
  201. /* CMP (register) 0100 0101 xxxx xxxx */
  202. /* MOV (register) 0100 0110 xxxx xxxx */
  203. DECODE_CUSTOM (0xfc00, 0x4400, t16_decode_hiregs),
  204. /*
  205. * Load from Literal Pool
  206. * LDR (literal) 0100 1xxx xxxx xxxx
  207. */
  208. DECODE_SIMULATE (0xf800, 0x4800, t16_simulate_ldr_literal),
  209. /*
  210. * 16-bit Thumb Load/store instructions
  211. * 0101 xxxx xxxx xxxx
  212. * 011x xxxx xxxx xxxx
  213. * 100x xxxx xxxx xxxx
  214. */
  215. /* STR (register) 0101 000x xxxx xxxx */
  216. /* STRH (register) 0101 001x xxxx xxxx */
  217. /* STRB (register) 0101 010x xxxx xxxx */
  218. /* LDRSB (register) 0101 011x xxxx xxxx */
  219. /* LDR (register) 0101 100x xxxx xxxx */
  220. /* LDRH (register) 0101 101x xxxx xxxx */
  221. /* LDRB (register) 0101 110x xxxx xxxx */
  222. /* LDRSH (register) 0101 111x xxxx xxxx */
  223. /* STR (immediate, Thumb) 0110 0xxx xxxx xxxx */
  224. /* LDR (immediate, Thumb) 0110 1xxx xxxx xxxx */
  225. /* STRB (immediate, Thumb) 0111 0xxx xxxx xxxx */
  226. /* LDRB (immediate, Thumb) 0111 1xxx xxxx xxxx */
  227. DECODE_EMULATE (0xc000, 0x4000, t16_emulate_loregs_rwflags),
  228. /* STRH (immediate, Thumb) 1000 0xxx xxxx xxxx */
  229. /* LDRH (immediate, Thumb) 1000 1xxx xxxx xxxx */
  230. DECODE_EMULATE (0xf000, 0x8000, t16_emulate_loregs_rwflags),
  231. /* STR (immediate, Thumb) 1001 0xxx xxxx xxxx */
  232. /* LDR (immediate, Thumb) 1001 1xxx xxxx xxxx */
  233. DECODE_SIMULATE (0xf000, 0x9000, t16_simulate_ldrstr_sp_relative),
  234. /*
  235. * Miscellaneous 16-bit instructions
  236. * 1011 xxxx xxxx xxxx
  237. */
  238. DECODE_TABLE (0xf000, 0xb000, t16_table_1011),
  239. /* STM 1100 0xxx xxxx xxxx */
  240. /* LDM 1100 1xxx xxxx xxxx */
  241. DECODE_EMULATE (0xf000, 0xc000, t16_emulate_loregs_rwflags),
  242. DECODE_END
  243. };
  244. static unsigned long __kprobes thumb_check_cc(unsigned long cpsr)
  245. {
  246. if (unlikely(in_it_block(cpsr)))
  247. return kprobe_condition_checks[current_cond(cpsr)](cpsr);
  248. return true;
  249. }
  250. static void __kprobes thumb16_singlestep(struct kprobe *p, struct pt_regs *regs)
  251. {
  252. regs->ARM_pc += 2;
  253. p->ainsn.insn_handler(p, regs);
  254. regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
  255. }
  256. static void __kprobes thumb32_singlestep(struct kprobe *p, struct pt_regs *regs)
  257. {
  258. regs->ARM_pc += 4;
  259. p->ainsn.insn_handler(p, regs);
  260. regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
  261. }
  262. enum kprobe_insn __kprobes
  263. thumb16_kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  264. {
  265. asi->insn_singlestep = thumb16_singlestep;
  266. asi->insn_check_cc = thumb_check_cc;
  267. return kprobe_decode_insn(insn, asi, kprobe_decode_thumb16_table, true);
  268. }
  269. enum kprobe_insn __kprobes
  270. thumb32_kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  271. {
  272. asi->insn_singlestep = thumb32_singlestep;
  273. asi->insn_check_cc = thumb_check_cc;
  274. return INSN_REJECTED;
  275. }