kprobes-thumb.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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 enum kprobe_insn __kprobes
  34. t32_decode_ldmstm(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  35. {
  36. enum kprobe_insn ret = kprobe_decode_ldmstm(insn, asi);
  37. /* Fixup modified instruction to have halfwords in correct order...*/
  38. insn = asi->insn[0];
  39. ((u16 *)asi->insn)[0] = insn >> 16;
  40. ((u16 *)asi->insn)[1] = insn & 0xffff;
  41. return ret;
  42. }
  43. static const union decode_item t32_table_1110_100x_x0xx[] = {
  44. /* Load/store multiple instructions */
  45. /* Rn is PC 1110 100x x0xx 1111 xxxx xxxx xxxx xxxx */
  46. DECODE_REJECT (0xfe4f0000, 0xe80f0000),
  47. /* SRS 1110 1000 00x0 xxxx xxxx xxxx xxxx xxxx */
  48. /* RFE 1110 1000 00x1 xxxx xxxx xxxx xxxx xxxx */
  49. DECODE_REJECT (0xffc00000, 0xe8000000),
  50. /* SRS 1110 1001 10x0 xxxx xxxx xxxx xxxx xxxx */
  51. /* RFE 1110 1001 10x1 xxxx xxxx xxxx xxxx xxxx */
  52. DECODE_REJECT (0xffc00000, 0xe9800000),
  53. /* STM Rn, {...pc} 1110 100x x0x0 xxxx 1xxx xxxx xxxx xxxx */
  54. DECODE_REJECT (0xfe508000, 0xe8008000),
  55. /* LDM Rn, {...lr,pc} 1110 100x x0x1 xxxx 11xx xxxx xxxx xxxx */
  56. DECODE_REJECT (0xfe50c000, 0xe810c000),
  57. /* LDM/STM Rn, {...sp} 1110 100x x0xx xxxx xx1x xxxx xxxx xxxx */
  58. DECODE_REJECT (0xfe402000, 0xe8002000),
  59. /* STMIA 1110 1000 10x0 xxxx xxxx xxxx xxxx xxxx */
  60. /* LDMIA 1110 1000 10x1 xxxx xxxx xxxx xxxx xxxx */
  61. /* STMDB 1110 1001 00x0 xxxx xxxx xxxx xxxx xxxx */
  62. /* LDMDB 1110 1001 00x1 xxxx xxxx xxxx xxxx xxxx */
  63. DECODE_CUSTOM (0xfe400000, 0xe8000000, t32_decode_ldmstm),
  64. DECODE_END
  65. };
  66. static const union decode_item t32_table_1111_0xxx___1[] = {
  67. /* Branches and miscellaneous control */
  68. /* YIELD 1111 0011 1010 xxxx 10x0 x000 0000 0001 */
  69. DECODE_OR (0xfff0d7ff, 0xf3a08001),
  70. /* SEV 1111 0011 1010 xxxx 10x0 x000 0000 0100 */
  71. DECODE_EMULATE (0xfff0d7ff, 0xf3a08004, kprobe_emulate_none),
  72. /* NOP 1111 0011 1010 xxxx 10x0 x000 0000 0000 */
  73. /* WFE 1111 0011 1010 xxxx 10x0 x000 0000 0010 */
  74. /* WFI 1111 0011 1010 xxxx 10x0 x000 0000 0011 */
  75. DECODE_SIMULATE (0xfff0d7fc, 0xf3a08000, kprobe_simulate_nop),
  76. DECODE_END
  77. };
  78. const union decode_item kprobe_decode_thumb32_table[] = {
  79. /*
  80. * Load/store multiple instructions
  81. * 1110 100x x0xx xxxx xxxx xxxx xxxx xxxx
  82. */
  83. DECODE_TABLE (0xfe400000, 0xe8000000, t32_table_1110_100x_x0xx),
  84. /*
  85. * Branches and miscellaneous control
  86. * 1111 0xxx xxxx xxxx 1xxx xxxx xxxx xxxx
  87. */
  88. DECODE_TABLE (0xf8008000, 0xf0008000, t32_table_1111_0xxx___1),
  89. DECODE_END
  90. };
  91. static void __kprobes
  92. t16_simulate_bxblx(struct kprobe *p, struct pt_regs *regs)
  93. {
  94. kprobe_opcode_t insn = p->opcode;
  95. unsigned long pc = thumb_probe_pc(p);
  96. int rm = (insn >> 3) & 0xf;
  97. unsigned long rmv = (rm == 15) ? pc : regs->uregs[rm];
  98. if (insn & (1 << 7)) /* BLX ? */
  99. regs->ARM_lr = (unsigned long)p->addr + 2;
  100. bx_write_pc(rmv, regs);
  101. }
  102. static void __kprobes
  103. t16_simulate_ldr_literal(struct kprobe *p, struct pt_regs *regs)
  104. {
  105. kprobe_opcode_t insn = p->opcode;
  106. unsigned long* base = (unsigned long *)(thumb_probe_pc(p) & ~3);
  107. long index = insn & 0xff;
  108. int rt = (insn >> 8) & 0x7;
  109. regs->uregs[rt] = base[index];
  110. }
  111. static void __kprobes
  112. t16_simulate_ldrstr_sp_relative(struct kprobe *p, struct pt_regs *regs)
  113. {
  114. kprobe_opcode_t insn = p->opcode;
  115. unsigned long* base = (unsigned long *)regs->ARM_sp;
  116. long index = insn & 0xff;
  117. int rt = (insn >> 8) & 0x7;
  118. if (insn & 0x800) /* LDR */
  119. regs->uregs[rt] = base[index];
  120. else /* STR */
  121. base[index] = regs->uregs[rt];
  122. }
  123. static void __kprobes
  124. t16_simulate_reladr(struct kprobe *p, struct pt_regs *regs)
  125. {
  126. kprobe_opcode_t insn = p->opcode;
  127. unsigned long base = (insn & 0x800) ? regs->ARM_sp
  128. : (thumb_probe_pc(p) & ~3);
  129. long offset = insn & 0xff;
  130. int rt = (insn >> 8) & 0x7;
  131. regs->uregs[rt] = base + offset * 4;
  132. }
  133. static void __kprobes
  134. t16_simulate_add_sp_imm(struct kprobe *p, struct pt_regs *regs)
  135. {
  136. kprobe_opcode_t insn = p->opcode;
  137. long imm = insn & 0x7f;
  138. if (insn & 0x80) /* SUB */
  139. regs->ARM_sp -= imm * 4;
  140. else /* ADD */
  141. regs->ARM_sp += imm * 4;
  142. }
  143. static void __kprobes
  144. t16_simulate_cbz(struct kprobe *p, struct pt_regs *regs)
  145. {
  146. kprobe_opcode_t insn = p->opcode;
  147. int rn = insn & 0x7;
  148. kprobe_opcode_t nonzero = regs->uregs[rn] ? insn : ~insn;
  149. if (nonzero & 0x800) {
  150. long i = insn & 0x200;
  151. long imm5 = insn & 0xf8;
  152. unsigned long pc = thumb_probe_pc(p);
  153. regs->ARM_pc = pc + (i >> 3) + (imm5 >> 2);
  154. }
  155. }
  156. static void __kprobes
  157. t16_simulate_it(struct kprobe *p, struct pt_regs *regs)
  158. {
  159. /*
  160. * The 8 IT state bits are split into two parts in CPSR:
  161. * ITSTATE<1:0> are in CPSR<26:25>
  162. * ITSTATE<7:2> are in CPSR<15:10>
  163. * The new IT state is in the lower byte of insn.
  164. */
  165. kprobe_opcode_t insn = p->opcode;
  166. unsigned long cpsr = regs->ARM_cpsr;
  167. cpsr &= ~PSR_IT_MASK;
  168. cpsr |= (insn & 0xfc) << 8;
  169. cpsr |= (insn & 0x03) << 25;
  170. regs->ARM_cpsr = cpsr;
  171. }
  172. static void __kprobes
  173. t16_singlestep_it(struct kprobe *p, struct pt_regs *regs)
  174. {
  175. regs->ARM_pc += 2;
  176. t16_simulate_it(p, regs);
  177. }
  178. static enum kprobe_insn __kprobes
  179. t16_decode_it(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  180. {
  181. asi->insn_singlestep = t16_singlestep_it;
  182. return INSN_GOOD_NO_SLOT;
  183. }
  184. static void __kprobes
  185. t16_simulate_cond_branch(struct kprobe *p, struct pt_regs *regs)
  186. {
  187. kprobe_opcode_t insn = p->opcode;
  188. unsigned long pc = thumb_probe_pc(p);
  189. long offset = insn & 0x7f;
  190. offset -= insn & 0x80; /* Apply sign bit */
  191. regs->ARM_pc = pc + (offset * 2);
  192. }
  193. static enum kprobe_insn __kprobes
  194. t16_decode_cond_branch(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  195. {
  196. int cc = (insn >> 8) & 0xf;
  197. asi->insn_check_cc = kprobe_condition_checks[cc];
  198. asi->insn_handler = t16_simulate_cond_branch;
  199. return INSN_GOOD_NO_SLOT;
  200. }
  201. static void __kprobes
  202. t16_simulate_branch(struct kprobe *p, struct pt_regs *regs)
  203. {
  204. kprobe_opcode_t insn = p->opcode;
  205. unsigned long pc = thumb_probe_pc(p);
  206. long offset = insn & 0x3ff;
  207. offset -= insn & 0x400; /* Apply sign bit */
  208. regs->ARM_pc = pc + (offset * 2);
  209. }
  210. static unsigned long __kprobes
  211. t16_emulate_loregs(struct kprobe *p, struct pt_regs *regs)
  212. {
  213. unsigned long oldcpsr = regs->ARM_cpsr;
  214. unsigned long newcpsr;
  215. __asm__ __volatile__ (
  216. "msr cpsr_fs, %[oldcpsr] \n\t"
  217. "ldmia %[regs], {r0-r7} \n\t"
  218. "blx %[fn] \n\t"
  219. "stmia %[regs], {r0-r7} \n\t"
  220. "mrs %[newcpsr], cpsr \n\t"
  221. : [newcpsr] "=r" (newcpsr)
  222. : [oldcpsr] "r" (oldcpsr), [regs] "r" (regs),
  223. [fn] "r" (p->ainsn.insn_fn)
  224. : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  225. "lr", "memory", "cc"
  226. );
  227. return (oldcpsr & ~APSR_MASK) | (newcpsr & APSR_MASK);
  228. }
  229. static void __kprobes
  230. t16_emulate_loregs_rwflags(struct kprobe *p, struct pt_regs *regs)
  231. {
  232. regs->ARM_cpsr = t16_emulate_loregs(p, regs);
  233. }
  234. static void __kprobes
  235. t16_emulate_loregs_noitrwflags(struct kprobe *p, struct pt_regs *regs)
  236. {
  237. unsigned long cpsr = t16_emulate_loregs(p, regs);
  238. if (!in_it_block(cpsr))
  239. regs->ARM_cpsr = cpsr;
  240. }
  241. static void __kprobes
  242. t16_emulate_hiregs(struct kprobe *p, struct pt_regs *regs)
  243. {
  244. kprobe_opcode_t insn = p->opcode;
  245. unsigned long pc = thumb_probe_pc(p);
  246. int rdn = (insn & 0x7) | ((insn & 0x80) >> 4);
  247. int rm = (insn >> 3) & 0xf;
  248. register unsigned long rdnv asm("r1");
  249. register unsigned long rmv asm("r0");
  250. unsigned long cpsr = regs->ARM_cpsr;
  251. rdnv = (rdn == 15) ? pc : regs->uregs[rdn];
  252. rmv = (rm == 15) ? pc : regs->uregs[rm];
  253. __asm__ __volatile__ (
  254. "msr cpsr_fs, %[cpsr] \n\t"
  255. "blx %[fn] \n\t"
  256. "mrs %[cpsr], cpsr \n\t"
  257. : "=r" (rdnv), [cpsr] "=r" (cpsr)
  258. : "0" (rdnv), "r" (rmv), "1" (cpsr), [fn] "r" (p->ainsn.insn_fn)
  259. : "lr", "memory", "cc"
  260. );
  261. if (rdn == 15)
  262. rdnv &= ~1;
  263. regs->uregs[rdn] = rdnv;
  264. regs->ARM_cpsr = (regs->ARM_cpsr & ~APSR_MASK) | (cpsr & APSR_MASK);
  265. }
  266. static enum kprobe_insn __kprobes
  267. t16_decode_hiregs(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  268. {
  269. insn &= ~0x00ff;
  270. insn |= 0x001; /* Set Rdn = R1 and Rm = R0 */
  271. ((u16 *)asi->insn)[0] = insn;
  272. asi->insn_handler = t16_emulate_hiregs;
  273. return INSN_GOOD;
  274. }
  275. static void __kprobes
  276. t16_emulate_push(struct kprobe *p, struct pt_regs *regs)
  277. {
  278. __asm__ __volatile__ (
  279. "ldr r9, [%[regs], #13*4] \n\t"
  280. "ldr r8, [%[regs], #14*4] \n\t"
  281. "ldmia %[regs], {r0-r7} \n\t"
  282. "blx %[fn] \n\t"
  283. "str r9, [%[regs], #13*4] \n\t"
  284. :
  285. : [regs] "r" (regs), [fn] "r" (p->ainsn.insn_fn)
  286. : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9",
  287. "lr", "memory", "cc"
  288. );
  289. }
  290. static enum kprobe_insn __kprobes
  291. t16_decode_push(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  292. {
  293. /*
  294. * To simulate a PUSH we use a Thumb-2 "STMDB R9!, {registers}"
  295. * and call it with R9=SP and LR in the register list represented
  296. * by R8.
  297. */
  298. ((u16 *)asi->insn)[0] = 0xe929; /* 1st half STMDB R9!,{} */
  299. ((u16 *)asi->insn)[1] = insn & 0x1ff; /* 2nd half (register list) */
  300. asi->insn_handler = t16_emulate_push;
  301. return INSN_GOOD;
  302. }
  303. static void __kprobes
  304. t16_emulate_pop_nopc(struct kprobe *p, struct pt_regs *regs)
  305. {
  306. __asm__ __volatile__ (
  307. "ldr r9, [%[regs], #13*4] \n\t"
  308. "ldmia %[regs], {r0-r7} \n\t"
  309. "blx %[fn] \n\t"
  310. "stmia %[regs], {r0-r7} \n\t"
  311. "str r9, [%[regs], #13*4] \n\t"
  312. :
  313. : [regs] "r" (regs), [fn] "r" (p->ainsn.insn_fn)
  314. : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r9",
  315. "lr", "memory", "cc"
  316. );
  317. }
  318. static void __kprobes
  319. t16_emulate_pop_pc(struct kprobe *p, struct pt_regs *regs)
  320. {
  321. register unsigned long pc asm("r8");
  322. __asm__ __volatile__ (
  323. "ldr r9, [%[regs], #13*4] \n\t"
  324. "ldmia %[regs], {r0-r7} \n\t"
  325. "blx %[fn] \n\t"
  326. "stmia %[regs], {r0-r7} \n\t"
  327. "str r9, [%[regs], #13*4] \n\t"
  328. : "=r" (pc)
  329. : [regs] "r" (regs), [fn] "r" (p->ainsn.insn_fn)
  330. : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r9",
  331. "lr", "memory", "cc"
  332. );
  333. bx_write_pc(pc, regs);
  334. }
  335. static enum kprobe_insn __kprobes
  336. t16_decode_pop(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  337. {
  338. /*
  339. * To simulate a POP we use a Thumb-2 "LDMDB R9!, {registers}"
  340. * and call it with R9=SP and PC in the register list represented
  341. * by R8.
  342. */
  343. ((u16 *)asi->insn)[0] = 0xe8b9; /* 1st half LDMIA R9!,{} */
  344. ((u16 *)asi->insn)[1] = insn & 0x1ff; /* 2nd half (register list) */
  345. asi->insn_handler = insn & 0x100 ? t16_emulate_pop_pc
  346. : t16_emulate_pop_nopc;
  347. return INSN_GOOD;
  348. }
  349. static const union decode_item t16_table_1011[] = {
  350. /* Miscellaneous 16-bit instructions */
  351. /* ADD (SP plus immediate) 1011 0000 0xxx xxxx */
  352. /* SUB (SP minus immediate) 1011 0000 1xxx xxxx */
  353. DECODE_SIMULATE (0xff00, 0xb000, t16_simulate_add_sp_imm),
  354. /* CBZ 1011 00x1 xxxx xxxx */
  355. /* CBNZ 1011 10x1 xxxx xxxx */
  356. DECODE_SIMULATE (0xf500, 0xb100, t16_simulate_cbz),
  357. /* SXTH 1011 0010 00xx xxxx */
  358. /* SXTB 1011 0010 01xx xxxx */
  359. /* UXTH 1011 0010 10xx xxxx */
  360. /* UXTB 1011 0010 11xx xxxx */
  361. /* REV 1011 1010 00xx xxxx */
  362. /* REV16 1011 1010 01xx xxxx */
  363. /* ??? 1011 1010 10xx xxxx */
  364. /* REVSH 1011 1010 11xx xxxx */
  365. DECODE_REJECT (0xffc0, 0xba80),
  366. DECODE_EMULATE (0xf500, 0xb000, t16_emulate_loregs_rwflags),
  367. /* PUSH 1011 010x xxxx xxxx */
  368. DECODE_CUSTOM (0xfe00, 0xb400, t16_decode_push),
  369. /* POP 1011 110x xxxx xxxx */
  370. DECODE_CUSTOM (0xfe00, 0xbc00, t16_decode_pop),
  371. /*
  372. * If-Then, and hints
  373. * 1011 1111 xxxx xxxx
  374. */
  375. /* YIELD 1011 1111 0001 0000 */
  376. DECODE_OR (0xffff, 0xbf10),
  377. /* SEV 1011 1111 0100 0000 */
  378. DECODE_EMULATE (0xffff, 0xbf40, kprobe_emulate_none),
  379. /* NOP 1011 1111 0000 0000 */
  380. /* WFE 1011 1111 0010 0000 */
  381. /* WFI 1011 1111 0011 0000 */
  382. DECODE_SIMULATE (0xffcf, 0xbf00, kprobe_simulate_nop),
  383. /* Unassigned hints 1011 1111 xxxx 0000 */
  384. DECODE_REJECT (0xff0f, 0xbf00),
  385. /* IT 1011 1111 xxxx xxxx */
  386. DECODE_CUSTOM (0xff00, 0xbf00, t16_decode_it),
  387. /* SETEND 1011 0110 010x xxxx */
  388. /* CPS 1011 0110 011x xxxx */
  389. /* BKPT 1011 1110 xxxx xxxx */
  390. /* And unallocated instructions... */
  391. DECODE_END
  392. };
  393. const union decode_item kprobe_decode_thumb16_table[] = {
  394. /*
  395. * Shift (immediate), add, subtract, move, and compare
  396. * 00xx xxxx xxxx xxxx
  397. */
  398. /* CMP (immediate) 0010 1xxx xxxx xxxx */
  399. DECODE_EMULATE (0xf800, 0x2800, t16_emulate_loregs_rwflags),
  400. /* ADD (register) 0001 100x xxxx xxxx */
  401. /* SUB (register) 0001 101x xxxx xxxx */
  402. /* LSL (immediate) 0000 0xxx xxxx xxxx */
  403. /* LSR (immediate) 0000 1xxx xxxx xxxx */
  404. /* ASR (immediate) 0001 0xxx xxxx xxxx */
  405. /* ADD (immediate, Thumb) 0001 110x xxxx xxxx */
  406. /* SUB (immediate, Thumb) 0001 111x xxxx xxxx */
  407. /* MOV (immediate) 0010 0xxx xxxx xxxx */
  408. /* ADD (immediate, Thumb) 0011 0xxx xxxx xxxx */
  409. /* SUB (immediate, Thumb) 0011 1xxx xxxx xxxx */
  410. DECODE_EMULATE (0xc000, 0x0000, t16_emulate_loregs_noitrwflags),
  411. /*
  412. * 16-bit Thumb data-processing instructions
  413. * 0100 00xx xxxx xxxx
  414. */
  415. /* TST (register) 0100 0010 00xx xxxx */
  416. DECODE_EMULATE (0xffc0, 0x4200, t16_emulate_loregs_rwflags),
  417. /* CMP (register) 0100 0010 10xx xxxx */
  418. /* CMN (register) 0100 0010 11xx xxxx */
  419. DECODE_EMULATE (0xff80, 0x4280, t16_emulate_loregs_rwflags),
  420. /* AND (register) 0100 0000 00xx xxxx */
  421. /* EOR (register) 0100 0000 01xx xxxx */
  422. /* LSL (register) 0100 0000 10xx xxxx */
  423. /* LSR (register) 0100 0000 11xx xxxx */
  424. /* ASR (register) 0100 0001 00xx xxxx */
  425. /* ADC (register) 0100 0001 01xx xxxx */
  426. /* SBC (register) 0100 0001 10xx xxxx */
  427. /* ROR (register) 0100 0001 11xx xxxx */
  428. /* RSB (immediate) 0100 0010 01xx xxxx */
  429. /* ORR (register) 0100 0011 00xx xxxx */
  430. /* MUL 0100 0011 00xx xxxx */
  431. /* BIC (register) 0100 0011 10xx xxxx */
  432. /* MVN (register) 0100 0011 10xx xxxx */
  433. DECODE_EMULATE (0xfc00, 0x4000, t16_emulate_loregs_noitrwflags),
  434. /*
  435. * Special data instructions and branch and exchange
  436. * 0100 01xx xxxx xxxx
  437. */
  438. /* BLX pc 0100 0111 1111 1xxx */
  439. DECODE_REJECT (0xfff8, 0x47f8),
  440. /* BX (register) 0100 0111 0xxx xxxx */
  441. /* BLX (register) 0100 0111 1xxx xxxx */
  442. DECODE_SIMULATE (0xff00, 0x4700, t16_simulate_bxblx),
  443. /* ADD pc, pc 0100 0100 1111 1111 */
  444. DECODE_REJECT (0xffff, 0x44ff),
  445. /* ADD (register) 0100 0100 xxxx xxxx */
  446. /* CMP (register) 0100 0101 xxxx xxxx */
  447. /* MOV (register) 0100 0110 xxxx xxxx */
  448. DECODE_CUSTOM (0xfc00, 0x4400, t16_decode_hiregs),
  449. /*
  450. * Load from Literal Pool
  451. * LDR (literal) 0100 1xxx xxxx xxxx
  452. */
  453. DECODE_SIMULATE (0xf800, 0x4800, t16_simulate_ldr_literal),
  454. /*
  455. * 16-bit Thumb Load/store instructions
  456. * 0101 xxxx xxxx xxxx
  457. * 011x xxxx xxxx xxxx
  458. * 100x xxxx xxxx xxxx
  459. */
  460. /* STR (register) 0101 000x xxxx xxxx */
  461. /* STRH (register) 0101 001x xxxx xxxx */
  462. /* STRB (register) 0101 010x xxxx xxxx */
  463. /* LDRSB (register) 0101 011x xxxx xxxx */
  464. /* LDR (register) 0101 100x xxxx xxxx */
  465. /* LDRH (register) 0101 101x xxxx xxxx */
  466. /* LDRB (register) 0101 110x xxxx xxxx */
  467. /* LDRSH (register) 0101 111x xxxx xxxx */
  468. /* STR (immediate, Thumb) 0110 0xxx xxxx xxxx */
  469. /* LDR (immediate, Thumb) 0110 1xxx xxxx xxxx */
  470. /* STRB (immediate, Thumb) 0111 0xxx xxxx xxxx */
  471. /* LDRB (immediate, Thumb) 0111 1xxx xxxx xxxx */
  472. DECODE_EMULATE (0xc000, 0x4000, t16_emulate_loregs_rwflags),
  473. /* STRH (immediate, Thumb) 1000 0xxx xxxx xxxx */
  474. /* LDRH (immediate, Thumb) 1000 1xxx xxxx xxxx */
  475. DECODE_EMULATE (0xf000, 0x8000, t16_emulate_loregs_rwflags),
  476. /* STR (immediate, Thumb) 1001 0xxx xxxx xxxx */
  477. /* LDR (immediate, Thumb) 1001 1xxx xxxx xxxx */
  478. DECODE_SIMULATE (0xf000, 0x9000, t16_simulate_ldrstr_sp_relative),
  479. /*
  480. * Generate PC-/SP-relative address
  481. * ADR (literal) 1010 0xxx xxxx xxxx
  482. * ADD (SP plus immediate) 1010 1xxx xxxx xxxx
  483. */
  484. DECODE_SIMULATE (0xf000, 0xa000, t16_simulate_reladr),
  485. /*
  486. * Miscellaneous 16-bit instructions
  487. * 1011 xxxx xxxx xxxx
  488. */
  489. DECODE_TABLE (0xf000, 0xb000, t16_table_1011),
  490. /* STM 1100 0xxx xxxx xxxx */
  491. /* LDM 1100 1xxx xxxx xxxx */
  492. DECODE_EMULATE (0xf000, 0xc000, t16_emulate_loregs_rwflags),
  493. /*
  494. * Conditional branch, and Supervisor Call
  495. */
  496. /* Permanently UNDEFINED 1101 1110 xxxx xxxx */
  497. /* SVC 1101 1111 xxxx xxxx */
  498. DECODE_REJECT (0xfe00, 0xde00),
  499. /* Conditional branch 1101 xxxx xxxx xxxx */
  500. DECODE_CUSTOM (0xf000, 0xd000, t16_decode_cond_branch),
  501. /*
  502. * Unconditional branch
  503. * B 1110 0xxx xxxx xxxx
  504. */
  505. DECODE_SIMULATE (0xf800, 0xe000, t16_simulate_branch),
  506. DECODE_END
  507. };
  508. static unsigned long __kprobes thumb_check_cc(unsigned long cpsr)
  509. {
  510. if (unlikely(in_it_block(cpsr)))
  511. return kprobe_condition_checks[current_cond(cpsr)](cpsr);
  512. return true;
  513. }
  514. static void __kprobes thumb16_singlestep(struct kprobe *p, struct pt_regs *regs)
  515. {
  516. regs->ARM_pc += 2;
  517. p->ainsn.insn_handler(p, regs);
  518. regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
  519. }
  520. static void __kprobes thumb32_singlestep(struct kprobe *p, struct pt_regs *regs)
  521. {
  522. regs->ARM_pc += 4;
  523. p->ainsn.insn_handler(p, regs);
  524. regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
  525. }
  526. enum kprobe_insn __kprobes
  527. thumb16_kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  528. {
  529. asi->insn_singlestep = thumb16_singlestep;
  530. asi->insn_check_cc = thumb_check_cc;
  531. return kprobe_decode_insn(insn, asi, kprobe_decode_thumb16_table, true);
  532. }
  533. enum kprobe_insn __kprobes
  534. thumb32_kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  535. {
  536. asi->insn_singlestep = thumb32_singlestep;
  537. asi->insn_check_cc = thumb_check_cc;
  538. return kprobe_decode_insn(insn, asi, kprobe_decode_thumb32_table, true);
  539. }