kprobes-thumb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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 void __kprobes
  66. t16_simulate_reladr(struct kprobe *p, struct pt_regs *regs)
  67. {
  68. kprobe_opcode_t insn = p->opcode;
  69. unsigned long base = (insn & 0x800) ? regs->ARM_sp
  70. : (thumb_probe_pc(p) & ~3);
  71. long offset = insn & 0xff;
  72. int rt = (insn >> 8) & 0x7;
  73. regs->uregs[rt] = base + offset * 4;
  74. }
  75. static void __kprobes
  76. t16_simulate_add_sp_imm(struct kprobe *p, struct pt_regs *regs)
  77. {
  78. kprobe_opcode_t insn = p->opcode;
  79. long imm = insn & 0x7f;
  80. if (insn & 0x80) /* SUB */
  81. regs->ARM_sp -= imm * 4;
  82. else /* ADD */
  83. regs->ARM_sp += imm * 4;
  84. }
  85. static void __kprobes
  86. t16_simulate_cbz(struct kprobe *p, struct pt_regs *regs)
  87. {
  88. kprobe_opcode_t insn = p->opcode;
  89. int rn = insn & 0x7;
  90. kprobe_opcode_t nonzero = regs->uregs[rn] ? insn : ~insn;
  91. if (nonzero & 0x800) {
  92. long i = insn & 0x200;
  93. long imm5 = insn & 0xf8;
  94. unsigned long pc = thumb_probe_pc(p);
  95. regs->ARM_pc = pc + (i >> 3) + (imm5 >> 2);
  96. }
  97. }
  98. static void __kprobes
  99. t16_simulate_it(struct kprobe *p, struct pt_regs *regs)
  100. {
  101. /*
  102. * The 8 IT state bits are split into two parts in CPSR:
  103. * ITSTATE<1:0> are in CPSR<26:25>
  104. * ITSTATE<7:2> are in CPSR<15:10>
  105. * The new IT state is in the lower byte of insn.
  106. */
  107. kprobe_opcode_t insn = p->opcode;
  108. unsigned long cpsr = regs->ARM_cpsr;
  109. cpsr &= ~PSR_IT_MASK;
  110. cpsr |= (insn & 0xfc) << 8;
  111. cpsr |= (insn & 0x03) << 25;
  112. regs->ARM_cpsr = cpsr;
  113. }
  114. static void __kprobes
  115. t16_singlestep_it(struct kprobe *p, struct pt_regs *regs)
  116. {
  117. regs->ARM_pc += 2;
  118. t16_simulate_it(p, regs);
  119. }
  120. static enum kprobe_insn __kprobes
  121. t16_decode_it(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  122. {
  123. asi->insn_singlestep = t16_singlestep_it;
  124. return INSN_GOOD_NO_SLOT;
  125. }
  126. static unsigned long __kprobes
  127. t16_emulate_loregs(struct kprobe *p, struct pt_regs *regs)
  128. {
  129. unsigned long oldcpsr = regs->ARM_cpsr;
  130. unsigned long newcpsr;
  131. __asm__ __volatile__ (
  132. "msr cpsr_fs, %[oldcpsr] \n\t"
  133. "ldmia %[regs], {r0-r7} \n\t"
  134. "blx %[fn] \n\t"
  135. "stmia %[regs], {r0-r7} \n\t"
  136. "mrs %[newcpsr], cpsr \n\t"
  137. : [newcpsr] "=r" (newcpsr)
  138. : [oldcpsr] "r" (oldcpsr), [regs] "r" (regs),
  139. [fn] "r" (p->ainsn.insn_fn)
  140. : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  141. "lr", "memory", "cc"
  142. );
  143. return (oldcpsr & ~APSR_MASK) | (newcpsr & APSR_MASK);
  144. }
  145. static void __kprobes
  146. t16_emulate_loregs_rwflags(struct kprobe *p, struct pt_regs *regs)
  147. {
  148. regs->ARM_cpsr = t16_emulate_loregs(p, regs);
  149. }
  150. static void __kprobes
  151. t16_emulate_loregs_noitrwflags(struct kprobe *p, struct pt_regs *regs)
  152. {
  153. unsigned long cpsr = t16_emulate_loregs(p, regs);
  154. if (!in_it_block(cpsr))
  155. regs->ARM_cpsr = cpsr;
  156. }
  157. static void __kprobes
  158. t16_emulate_hiregs(struct kprobe *p, struct pt_regs *regs)
  159. {
  160. kprobe_opcode_t insn = p->opcode;
  161. unsigned long pc = thumb_probe_pc(p);
  162. int rdn = (insn & 0x7) | ((insn & 0x80) >> 4);
  163. int rm = (insn >> 3) & 0xf;
  164. register unsigned long rdnv asm("r1");
  165. register unsigned long rmv asm("r0");
  166. unsigned long cpsr = regs->ARM_cpsr;
  167. rdnv = (rdn == 15) ? pc : regs->uregs[rdn];
  168. rmv = (rm == 15) ? pc : regs->uregs[rm];
  169. __asm__ __volatile__ (
  170. "msr cpsr_fs, %[cpsr] \n\t"
  171. "blx %[fn] \n\t"
  172. "mrs %[cpsr], cpsr \n\t"
  173. : "=r" (rdnv), [cpsr] "=r" (cpsr)
  174. : "0" (rdnv), "r" (rmv), "1" (cpsr), [fn] "r" (p->ainsn.insn_fn)
  175. : "lr", "memory", "cc"
  176. );
  177. if (rdn == 15)
  178. rdnv &= ~1;
  179. regs->uregs[rdn] = rdnv;
  180. regs->ARM_cpsr = (regs->ARM_cpsr & ~APSR_MASK) | (cpsr & APSR_MASK);
  181. }
  182. static enum kprobe_insn __kprobes
  183. t16_decode_hiregs(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  184. {
  185. insn &= ~0x00ff;
  186. insn |= 0x001; /* Set Rdn = R1 and Rm = R0 */
  187. ((u16 *)asi->insn)[0] = insn;
  188. asi->insn_handler = t16_emulate_hiregs;
  189. return INSN_GOOD;
  190. }
  191. static void __kprobes
  192. t16_emulate_push(struct kprobe *p, struct pt_regs *regs)
  193. {
  194. __asm__ __volatile__ (
  195. "ldr r9, [%[regs], #13*4] \n\t"
  196. "ldr r8, [%[regs], #14*4] \n\t"
  197. "ldmia %[regs], {r0-r7} \n\t"
  198. "blx %[fn] \n\t"
  199. "str r9, [%[regs], #13*4] \n\t"
  200. :
  201. : [regs] "r" (regs), [fn] "r" (p->ainsn.insn_fn)
  202. : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9",
  203. "lr", "memory", "cc"
  204. );
  205. }
  206. static enum kprobe_insn __kprobes
  207. t16_decode_push(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  208. {
  209. /*
  210. * To simulate a PUSH we use a Thumb-2 "STMDB R9!, {registers}"
  211. * and call it with R9=SP and LR in the register list represented
  212. * by R8.
  213. */
  214. ((u16 *)asi->insn)[0] = 0xe929; /* 1st half STMDB R9!,{} */
  215. ((u16 *)asi->insn)[1] = insn & 0x1ff; /* 2nd half (register list) */
  216. asi->insn_handler = t16_emulate_push;
  217. return INSN_GOOD;
  218. }
  219. static void __kprobes
  220. t16_emulate_pop_nopc(struct kprobe *p, struct pt_regs *regs)
  221. {
  222. __asm__ __volatile__ (
  223. "ldr r9, [%[regs], #13*4] \n\t"
  224. "ldmia %[regs], {r0-r7} \n\t"
  225. "blx %[fn] \n\t"
  226. "stmia %[regs], {r0-r7} \n\t"
  227. "str r9, [%[regs], #13*4] \n\t"
  228. :
  229. : [regs] "r" (regs), [fn] "r" (p->ainsn.insn_fn)
  230. : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r9",
  231. "lr", "memory", "cc"
  232. );
  233. }
  234. static void __kprobes
  235. t16_emulate_pop_pc(struct kprobe *p, struct pt_regs *regs)
  236. {
  237. register unsigned long pc asm("r8");
  238. __asm__ __volatile__ (
  239. "ldr r9, [%[regs], #13*4] \n\t"
  240. "ldmia %[regs], {r0-r7} \n\t"
  241. "blx %[fn] \n\t"
  242. "stmia %[regs], {r0-r7} \n\t"
  243. "str r9, [%[regs], #13*4] \n\t"
  244. : "=r" (pc)
  245. : [regs] "r" (regs), [fn] "r" (p->ainsn.insn_fn)
  246. : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r9",
  247. "lr", "memory", "cc"
  248. );
  249. bx_write_pc(pc, regs);
  250. }
  251. static enum kprobe_insn __kprobes
  252. t16_decode_pop(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  253. {
  254. /*
  255. * To simulate a POP we use a Thumb-2 "LDMDB R9!, {registers}"
  256. * and call it with R9=SP and PC in the register list represented
  257. * by R8.
  258. */
  259. ((u16 *)asi->insn)[0] = 0xe8b9; /* 1st half LDMIA R9!,{} */
  260. ((u16 *)asi->insn)[1] = insn & 0x1ff; /* 2nd half (register list) */
  261. asi->insn_handler = insn & 0x100 ? t16_emulate_pop_pc
  262. : t16_emulate_pop_nopc;
  263. return INSN_GOOD;
  264. }
  265. static const union decode_item t16_table_1011[] = {
  266. /* Miscellaneous 16-bit instructions */
  267. /* ADD (SP plus immediate) 1011 0000 0xxx xxxx */
  268. /* SUB (SP minus immediate) 1011 0000 1xxx xxxx */
  269. DECODE_SIMULATE (0xff00, 0xb000, t16_simulate_add_sp_imm),
  270. /* CBZ 1011 00x1 xxxx xxxx */
  271. /* CBNZ 1011 10x1 xxxx xxxx */
  272. DECODE_SIMULATE (0xf500, 0xb100, t16_simulate_cbz),
  273. /* SXTH 1011 0010 00xx xxxx */
  274. /* SXTB 1011 0010 01xx xxxx */
  275. /* UXTH 1011 0010 10xx xxxx */
  276. /* UXTB 1011 0010 11xx xxxx */
  277. /* REV 1011 1010 00xx xxxx */
  278. /* REV16 1011 1010 01xx xxxx */
  279. /* ??? 1011 1010 10xx xxxx */
  280. /* REVSH 1011 1010 11xx xxxx */
  281. DECODE_REJECT (0xffc0, 0xba80),
  282. DECODE_EMULATE (0xf500, 0xb000, t16_emulate_loregs_rwflags),
  283. /* PUSH 1011 010x xxxx xxxx */
  284. DECODE_CUSTOM (0xfe00, 0xb400, t16_decode_push),
  285. /* POP 1011 110x xxxx xxxx */
  286. DECODE_CUSTOM (0xfe00, 0xbc00, t16_decode_pop),
  287. /*
  288. * If-Then, and hints
  289. * 1011 1111 xxxx xxxx
  290. */
  291. /* YIELD 1011 1111 0001 0000 */
  292. DECODE_OR (0xffff, 0xbf10),
  293. /* SEV 1011 1111 0100 0000 */
  294. DECODE_EMULATE (0xffff, 0xbf40, kprobe_emulate_none),
  295. /* NOP 1011 1111 0000 0000 */
  296. /* WFE 1011 1111 0010 0000 */
  297. /* WFI 1011 1111 0011 0000 */
  298. DECODE_SIMULATE (0xffcf, 0xbf00, kprobe_simulate_nop),
  299. /* Unassigned hints 1011 1111 xxxx 0000 */
  300. DECODE_REJECT (0xff0f, 0xbf00),
  301. /* IT 1011 1111 xxxx xxxx */
  302. DECODE_CUSTOM (0xff00, 0xbf00, t16_decode_it),
  303. DECODE_END
  304. };
  305. const union decode_item kprobe_decode_thumb16_table[] = {
  306. /*
  307. * Shift (immediate), add, subtract, move, and compare
  308. * 00xx xxxx xxxx xxxx
  309. */
  310. /* CMP (immediate) 0010 1xxx xxxx xxxx */
  311. DECODE_EMULATE (0xf800, 0x2800, t16_emulate_loregs_rwflags),
  312. /* ADD (register) 0001 100x xxxx xxxx */
  313. /* SUB (register) 0001 101x xxxx xxxx */
  314. /* LSL (immediate) 0000 0xxx xxxx xxxx */
  315. /* LSR (immediate) 0000 1xxx xxxx xxxx */
  316. /* ASR (immediate) 0001 0xxx xxxx xxxx */
  317. /* ADD (immediate, Thumb) 0001 110x xxxx xxxx */
  318. /* SUB (immediate, Thumb) 0001 111x xxxx xxxx */
  319. /* MOV (immediate) 0010 0xxx xxxx xxxx */
  320. /* ADD (immediate, Thumb) 0011 0xxx xxxx xxxx */
  321. /* SUB (immediate, Thumb) 0011 1xxx xxxx xxxx */
  322. DECODE_EMULATE (0xc000, 0x0000, t16_emulate_loregs_noitrwflags),
  323. /*
  324. * 16-bit Thumb data-processing instructions
  325. * 0100 00xx xxxx xxxx
  326. */
  327. /* TST (register) 0100 0010 00xx xxxx */
  328. DECODE_EMULATE (0xffc0, 0x4200, t16_emulate_loregs_rwflags),
  329. /* CMP (register) 0100 0010 10xx xxxx */
  330. /* CMN (register) 0100 0010 11xx xxxx */
  331. DECODE_EMULATE (0xff80, 0x4280, t16_emulate_loregs_rwflags),
  332. /* AND (register) 0100 0000 00xx xxxx */
  333. /* EOR (register) 0100 0000 01xx xxxx */
  334. /* LSL (register) 0100 0000 10xx xxxx */
  335. /* LSR (register) 0100 0000 11xx xxxx */
  336. /* ASR (register) 0100 0001 00xx xxxx */
  337. /* ADC (register) 0100 0001 01xx xxxx */
  338. /* SBC (register) 0100 0001 10xx xxxx */
  339. /* ROR (register) 0100 0001 11xx xxxx */
  340. /* RSB (immediate) 0100 0010 01xx xxxx */
  341. /* ORR (register) 0100 0011 00xx xxxx */
  342. /* MUL 0100 0011 00xx xxxx */
  343. /* BIC (register) 0100 0011 10xx xxxx */
  344. /* MVN (register) 0100 0011 10xx xxxx */
  345. DECODE_EMULATE (0xfc00, 0x4000, t16_emulate_loregs_noitrwflags),
  346. /*
  347. * Special data instructions and branch and exchange
  348. * 0100 01xx xxxx xxxx
  349. */
  350. /* BLX pc 0100 0111 1111 1xxx */
  351. DECODE_REJECT (0xfff8, 0x47f8),
  352. /* BX (register) 0100 0111 0xxx xxxx */
  353. /* BLX (register) 0100 0111 1xxx xxxx */
  354. DECODE_SIMULATE (0xff00, 0x4700, t16_simulate_bxblx),
  355. /* ADD pc, pc 0100 0100 1111 1111 */
  356. DECODE_REJECT (0xffff, 0x44ff),
  357. /* ADD (register) 0100 0100 xxxx xxxx */
  358. /* CMP (register) 0100 0101 xxxx xxxx */
  359. /* MOV (register) 0100 0110 xxxx xxxx */
  360. DECODE_CUSTOM (0xfc00, 0x4400, t16_decode_hiregs),
  361. /*
  362. * Load from Literal Pool
  363. * LDR (literal) 0100 1xxx xxxx xxxx
  364. */
  365. DECODE_SIMULATE (0xf800, 0x4800, t16_simulate_ldr_literal),
  366. /*
  367. * 16-bit Thumb Load/store instructions
  368. * 0101 xxxx xxxx xxxx
  369. * 011x xxxx xxxx xxxx
  370. * 100x xxxx xxxx xxxx
  371. */
  372. /* STR (register) 0101 000x xxxx xxxx */
  373. /* STRH (register) 0101 001x xxxx xxxx */
  374. /* STRB (register) 0101 010x xxxx xxxx */
  375. /* LDRSB (register) 0101 011x xxxx xxxx */
  376. /* LDR (register) 0101 100x xxxx xxxx */
  377. /* LDRH (register) 0101 101x xxxx xxxx */
  378. /* LDRB (register) 0101 110x xxxx xxxx */
  379. /* LDRSH (register) 0101 111x xxxx xxxx */
  380. /* STR (immediate, Thumb) 0110 0xxx xxxx xxxx */
  381. /* LDR (immediate, Thumb) 0110 1xxx xxxx xxxx */
  382. /* STRB (immediate, Thumb) 0111 0xxx xxxx xxxx */
  383. /* LDRB (immediate, Thumb) 0111 1xxx xxxx xxxx */
  384. DECODE_EMULATE (0xc000, 0x4000, t16_emulate_loregs_rwflags),
  385. /* STRH (immediate, Thumb) 1000 0xxx xxxx xxxx */
  386. /* LDRH (immediate, Thumb) 1000 1xxx xxxx xxxx */
  387. DECODE_EMULATE (0xf000, 0x8000, t16_emulate_loregs_rwflags),
  388. /* STR (immediate, Thumb) 1001 0xxx xxxx xxxx */
  389. /* LDR (immediate, Thumb) 1001 1xxx xxxx xxxx */
  390. DECODE_SIMULATE (0xf000, 0x9000, t16_simulate_ldrstr_sp_relative),
  391. /*
  392. * Generate PC-/SP-relative address
  393. * ADR (literal) 1010 0xxx xxxx xxxx
  394. * ADD (SP plus immediate) 1010 1xxx xxxx xxxx
  395. */
  396. DECODE_SIMULATE (0xf000, 0xa000, t16_simulate_reladr),
  397. /*
  398. * Miscellaneous 16-bit instructions
  399. * 1011 xxxx xxxx xxxx
  400. */
  401. DECODE_TABLE (0xf000, 0xb000, t16_table_1011),
  402. /* STM 1100 0xxx xxxx xxxx */
  403. /* LDM 1100 1xxx xxxx xxxx */
  404. DECODE_EMULATE (0xf000, 0xc000, t16_emulate_loregs_rwflags),
  405. DECODE_END
  406. };
  407. static unsigned long __kprobes thumb_check_cc(unsigned long cpsr)
  408. {
  409. if (unlikely(in_it_block(cpsr)))
  410. return kprobe_condition_checks[current_cond(cpsr)](cpsr);
  411. return true;
  412. }
  413. static void __kprobes thumb16_singlestep(struct kprobe *p, struct pt_regs *regs)
  414. {
  415. regs->ARM_pc += 2;
  416. p->ainsn.insn_handler(p, regs);
  417. regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
  418. }
  419. static void __kprobes thumb32_singlestep(struct kprobe *p, struct pt_regs *regs)
  420. {
  421. regs->ARM_pc += 4;
  422. p->ainsn.insn_handler(p, regs);
  423. regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
  424. }
  425. enum kprobe_insn __kprobes
  426. thumb16_kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  427. {
  428. asi->insn_singlestep = thumb16_singlestep;
  429. asi->insn_check_cc = thumb_check_cc;
  430. return kprobe_decode_insn(insn, asi, kprobe_decode_thumb16_table, true);
  431. }
  432. enum kprobe_insn __kprobes
  433. thumb32_kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  434. {
  435. asi->insn_singlestep = thumb32_singlestep;
  436. asi->insn_check_cc = thumb_check_cc;
  437. return INSN_REJECTED;
  438. }