kprobes-thumb.c 20 KB

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