kprobes-thumb.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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. t32_simulate_table_branch(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 rn = (insn >> 16) & 0xf;
  39. int rm = insn & 0xf;
  40. unsigned long rnv = (rn == 15) ? pc : regs->uregs[rn];
  41. unsigned long rmv = regs->uregs[rm];
  42. unsigned int halfwords;
  43. if (insn & 0x10) /* TBH */
  44. halfwords = ((u16 *)rnv)[rmv];
  45. else /* TBB */
  46. halfwords = ((u8 *)rnv)[rmv];
  47. regs->ARM_pc = pc + 2 * halfwords;
  48. }
  49. static void __kprobes
  50. t32_simulate_mrs(struct kprobe *p, struct pt_regs *regs)
  51. {
  52. kprobe_opcode_t insn = p->opcode;
  53. int rd = (insn >> 8) & 0xf;
  54. unsigned long mask = 0xf8ff03df; /* Mask out execution state */
  55. regs->uregs[rd] = regs->ARM_cpsr & mask;
  56. }
  57. static void __kprobes
  58. t32_simulate_cond_branch(struct kprobe *p, struct pt_regs *regs)
  59. {
  60. kprobe_opcode_t insn = p->opcode;
  61. unsigned long pc = thumb_probe_pc(p);
  62. long offset = insn & 0x7ff; /* imm11 */
  63. offset += (insn & 0x003f0000) >> 5; /* imm6 */
  64. offset += (insn & 0x00002000) << 4; /* J1 */
  65. offset += (insn & 0x00000800) << 7; /* J2 */
  66. offset -= (insn & 0x04000000) >> 7; /* Apply sign bit */
  67. regs->ARM_pc = pc + (offset * 2);
  68. }
  69. static enum kprobe_insn __kprobes
  70. t32_decode_cond_branch(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  71. {
  72. int cc = (insn >> 22) & 0xf;
  73. asi->insn_check_cc = kprobe_condition_checks[cc];
  74. asi->insn_handler = t32_simulate_cond_branch;
  75. return INSN_GOOD_NO_SLOT;
  76. }
  77. static void __kprobes
  78. t32_simulate_branch(struct kprobe *p, struct pt_regs *regs)
  79. {
  80. kprobe_opcode_t insn = p->opcode;
  81. unsigned long pc = thumb_probe_pc(p);
  82. long offset = insn & 0x7ff; /* imm11 */
  83. offset += (insn & 0x03ff0000) >> 5; /* imm10 */
  84. offset += (insn & 0x00002000) << 9; /* J1 */
  85. offset += (insn & 0x00000800) << 10; /* J2 */
  86. if (insn & 0x04000000)
  87. offset -= 0x00800000; /* Apply sign bit */
  88. else
  89. offset ^= 0x00600000; /* Invert J1 and J2 */
  90. if (insn & (1 << 14)) {
  91. /* BL or BLX */
  92. regs->ARM_lr = (unsigned long)p->addr + 4;
  93. if (!(insn & (1 << 12))) {
  94. /* BLX so switch to ARM mode */
  95. regs->ARM_cpsr &= ~PSR_T_BIT;
  96. pc &= ~3;
  97. }
  98. }
  99. regs->ARM_pc = pc + (offset * 2);
  100. }
  101. static enum kprobe_insn __kprobes
  102. t32_decode_ldmstm(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  103. {
  104. enum kprobe_insn ret = kprobe_decode_ldmstm(insn, asi);
  105. /* Fixup modified instruction to have halfwords in correct order...*/
  106. insn = asi->insn[0];
  107. ((u16 *)asi->insn)[0] = insn >> 16;
  108. ((u16 *)asi->insn)[1] = insn & 0xffff;
  109. return ret;
  110. }
  111. static void __kprobes
  112. t32_emulate_ldrdstrd(struct kprobe *p, struct pt_regs *regs)
  113. {
  114. kprobe_opcode_t insn = p->opcode;
  115. unsigned long pc = thumb_probe_pc(p) & ~3;
  116. int rt1 = (insn >> 12) & 0xf;
  117. int rt2 = (insn >> 8) & 0xf;
  118. int rn = (insn >> 16) & 0xf;
  119. register unsigned long rt1v asm("r0") = regs->uregs[rt1];
  120. register unsigned long rt2v asm("r1") = regs->uregs[rt2];
  121. register unsigned long rnv asm("r2") = (rn == 15) ? pc
  122. : regs->uregs[rn];
  123. __asm__ __volatile__ (
  124. "blx %[fn]"
  125. : "=r" (rt1v), "=r" (rt2v), "=r" (rnv)
  126. : "0" (rt1v), "1" (rt2v), "2" (rnv), [fn] "r" (p->ainsn.insn_fn)
  127. : "lr", "memory", "cc"
  128. );
  129. if (rn != 15)
  130. regs->uregs[rn] = rnv; /* Writeback base register */
  131. regs->uregs[rt1] = rt1v;
  132. regs->uregs[rt2] = rt2v;
  133. }
  134. static void __kprobes
  135. t32_emulate_rd8rn16rm0_rwflags(struct kprobe *p, struct pt_regs *regs)
  136. {
  137. kprobe_opcode_t insn = p->opcode;
  138. int rd = (insn >> 8) & 0xf;
  139. int rn = (insn >> 16) & 0xf;
  140. int rm = insn & 0xf;
  141. register unsigned long rdv asm("r1") = regs->uregs[rd];
  142. register unsigned long rnv asm("r2") = regs->uregs[rn];
  143. register unsigned long rmv asm("r3") = regs->uregs[rm];
  144. unsigned long cpsr = regs->ARM_cpsr;
  145. __asm__ __volatile__ (
  146. "msr cpsr_fs, %[cpsr] \n\t"
  147. "blx %[fn] \n\t"
  148. "mrs %[cpsr], cpsr \n\t"
  149. : "=r" (rdv), [cpsr] "=r" (cpsr)
  150. : "0" (rdv), "r" (rnv), "r" (rmv),
  151. "1" (cpsr), [fn] "r" (p->ainsn.insn_fn)
  152. : "lr", "memory", "cc"
  153. );
  154. regs->uregs[rd] = rdv;
  155. regs->ARM_cpsr = (regs->ARM_cpsr & ~APSR_MASK) | (cpsr & APSR_MASK);
  156. }
  157. static void __kprobes
  158. t32_emulate_rd8pc16_noflags(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 rd = (insn >> 8) & 0xf;
  163. register unsigned long rdv asm("r1") = regs->uregs[rd];
  164. register unsigned long rnv asm("r2") = pc & ~3;
  165. __asm__ __volatile__ (
  166. "blx %[fn]"
  167. : "=r" (rdv)
  168. : "0" (rdv), "r" (rnv), [fn] "r" (p->ainsn.insn_fn)
  169. : "lr", "memory", "cc"
  170. );
  171. regs->uregs[rd] = rdv;
  172. }
  173. static void __kprobes
  174. t32_emulate_rd8rn16_noflags(struct kprobe *p, struct pt_regs *regs)
  175. {
  176. kprobe_opcode_t insn = p->opcode;
  177. int rd = (insn >> 8) & 0xf;
  178. int rn = (insn >> 16) & 0xf;
  179. register unsigned long rdv asm("r1") = regs->uregs[rd];
  180. register unsigned long rnv asm("r2") = regs->uregs[rn];
  181. __asm__ __volatile__ (
  182. "blx %[fn]"
  183. : "=r" (rdv)
  184. : "0" (rdv), "r" (rnv), [fn] "r" (p->ainsn.insn_fn)
  185. : "lr", "memory", "cc"
  186. );
  187. regs->uregs[rd] = rdv;
  188. }
  189. static const union decode_item t32_table_1110_100x_x0xx[] = {
  190. /* Load/store multiple instructions */
  191. /* Rn is PC 1110 100x x0xx 1111 xxxx xxxx xxxx xxxx */
  192. DECODE_REJECT (0xfe4f0000, 0xe80f0000),
  193. /* SRS 1110 1000 00x0 xxxx xxxx xxxx xxxx xxxx */
  194. /* RFE 1110 1000 00x1 xxxx xxxx xxxx xxxx xxxx */
  195. DECODE_REJECT (0xffc00000, 0xe8000000),
  196. /* SRS 1110 1001 10x0 xxxx xxxx xxxx xxxx xxxx */
  197. /* RFE 1110 1001 10x1 xxxx xxxx xxxx xxxx xxxx */
  198. DECODE_REJECT (0xffc00000, 0xe9800000),
  199. /* STM Rn, {...pc} 1110 100x x0x0 xxxx 1xxx xxxx xxxx xxxx */
  200. DECODE_REJECT (0xfe508000, 0xe8008000),
  201. /* LDM Rn, {...lr,pc} 1110 100x x0x1 xxxx 11xx xxxx xxxx xxxx */
  202. DECODE_REJECT (0xfe50c000, 0xe810c000),
  203. /* LDM/STM Rn, {...sp} 1110 100x x0xx xxxx xx1x xxxx xxxx xxxx */
  204. DECODE_REJECT (0xfe402000, 0xe8002000),
  205. /* STMIA 1110 1000 10x0 xxxx xxxx xxxx xxxx xxxx */
  206. /* LDMIA 1110 1000 10x1 xxxx xxxx xxxx xxxx xxxx */
  207. /* STMDB 1110 1001 00x0 xxxx xxxx xxxx xxxx xxxx */
  208. /* LDMDB 1110 1001 00x1 xxxx xxxx xxxx xxxx xxxx */
  209. DECODE_CUSTOM (0xfe400000, 0xe8000000, t32_decode_ldmstm),
  210. DECODE_END
  211. };
  212. static const union decode_item t32_table_1110_100x_x1xx[] = {
  213. /* Load/store dual, load/store exclusive, table branch */
  214. /* STRD (immediate) 1110 1000 x110 xxxx xxxx xxxx xxxx xxxx */
  215. /* LDRD (immediate) 1110 1000 x111 xxxx xxxx xxxx xxxx xxxx */
  216. DECODE_OR (0xff600000, 0xe8600000),
  217. /* STRD (immediate) 1110 1001 x1x0 xxxx xxxx xxxx xxxx xxxx */
  218. /* LDRD (immediate) 1110 1001 x1x1 xxxx xxxx xxxx xxxx xxxx */
  219. DECODE_EMULATEX (0xff400000, 0xe9400000, t32_emulate_ldrdstrd,
  220. REGS(NOPCWB, NOSPPC, NOSPPC, 0, 0)),
  221. /* TBB 1110 1000 1101 xxxx xxxx xxxx 0000 xxxx */
  222. /* TBH 1110 1000 1101 xxxx xxxx xxxx 0001 xxxx */
  223. DECODE_SIMULATEX(0xfff000e0, 0xe8d00000, t32_simulate_table_branch,
  224. REGS(NOSP, 0, 0, 0, NOSPPC)),
  225. /* STREX 1110 1000 0100 xxxx xxxx xxxx xxxx xxxx */
  226. /* LDREX 1110 1000 0101 xxxx xxxx xxxx xxxx xxxx */
  227. /* STREXB 1110 1000 1100 xxxx xxxx xxxx 0100 xxxx */
  228. /* STREXH 1110 1000 1100 xxxx xxxx xxxx 0101 xxxx */
  229. /* STREXD 1110 1000 1100 xxxx xxxx xxxx 0111 xxxx */
  230. /* LDREXB 1110 1000 1101 xxxx xxxx xxxx 0100 xxxx */
  231. /* LDREXH 1110 1000 1101 xxxx xxxx xxxx 0101 xxxx */
  232. /* LDREXD 1110 1000 1101 xxxx xxxx xxxx 0111 xxxx */
  233. /* And unallocated instructions... */
  234. DECODE_END
  235. };
  236. static const union decode_item t32_table_1110_101x[] = {
  237. /* Data-processing (shifted register) */
  238. /* TST 1110 1010 0001 xxxx xxxx 1111 xxxx xxxx */
  239. /* TEQ 1110 1010 1001 xxxx xxxx 1111 xxxx xxxx */
  240. DECODE_EMULATEX (0xff700f00, 0xea100f00, t32_emulate_rd8rn16rm0_rwflags,
  241. REGS(NOSPPC, 0, 0, 0, NOSPPC)),
  242. /* CMN 1110 1011 0001 xxxx xxxx 1111 xxxx xxxx */
  243. DECODE_OR (0xfff00f00, 0xeb100f00),
  244. /* CMP 1110 1011 1011 xxxx xxxx 1111 xxxx xxxx */
  245. DECODE_EMULATEX (0xfff00f00, 0xebb00f00, t32_emulate_rd8rn16rm0_rwflags,
  246. REGS(NOPC, 0, 0, 0, NOSPPC)),
  247. /* MOV 1110 1010 010x 1111 xxxx xxxx xxxx xxxx */
  248. /* MVN 1110 1010 011x 1111 xxxx xxxx xxxx xxxx */
  249. DECODE_EMULATEX (0xffcf0000, 0xea4f0000, t32_emulate_rd8rn16rm0_rwflags,
  250. REGS(0, 0, NOSPPC, 0, NOSPPC)),
  251. /* ??? 1110 1010 101x xxxx xxxx xxxx xxxx xxxx */
  252. /* ??? 1110 1010 111x xxxx xxxx xxxx xxxx xxxx */
  253. DECODE_REJECT (0xffa00000, 0xeaa00000),
  254. /* ??? 1110 1011 001x xxxx xxxx xxxx xxxx xxxx */
  255. DECODE_REJECT (0xffe00000, 0xeb200000),
  256. /* ??? 1110 1011 100x xxxx xxxx xxxx xxxx xxxx */
  257. DECODE_REJECT (0xffe00000, 0xeb800000),
  258. /* ??? 1110 1011 111x xxxx xxxx xxxx xxxx xxxx */
  259. DECODE_REJECT (0xffe00000, 0xebe00000),
  260. /* ADD/SUB SP, SP, Rm, LSL #0..3 */
  261. /* 1110 1011 x0xx 1101 x000 1101 xx00 xxxx */
  262. DECODE_EMULATEX (0xff4f7f30, 0xeb0d0d00, t32_emulate_rd8rn16rm0_rwflags,
  263. REGS(SP, 0, SP, 0, NOSPPC)),
  264. /* ADD/SUB SP, SP, Rm, shift */
  265. /* 1110 1011 x0xx 1101 xxxx 1101 xxxx xxxx */
  266. DECODE_REJECT (0xff4f0f00, 0xeb0d0d00),
  267. /* ADD/SUB Rd, SP, Rm, shift */
  268. /* 1110 1011 x0xx 1101 xxxx xxxx xxxx xxxx */
  269. DECODE_EMULATEX (0xff4f0000, 0xeb0d0000, t32_emulate_rd8rn16rm0_rwflags,
  270. REGS(SP, 0, NOPC, 0, NOSPPC)),
  271. /* AND 1110 1010 000x xxxx xxxx xxxx xxxx xxxx */
  272. /* BIC 1110 1010 001x xxxx xxxx xxxx xxxx xxxx */
  273. /* ORR 1110 1010 010x xxxx xxxx xxxx xxxx xxxx */
  274. /* ORN 1110 1010 011x xxxx xxxx xxxx xxxx xxxx */
  275. /* EOR 1110 1010 100x xxxx xxxx xxxx xxxx xxxx */
  276. /* PKH 1110 1010 110x xxxx xxxx xxxx xxxx xxxx */
  277. /* ADD 1110 1011 000x xxxx xxxx xxxx xxxx xxxx */
  278. /* ADC 1110 1011 010x xxxx xxxx xxxx xxxx xxxx */
  279. /* SBC 1110 1011 011x xxxx xxxx xxxx xxxx xxxx */
  280. /* SUB 1110 1011 101x xxxx xxxx xxxx xxxx xxxx */
  281. /* RSB 1110 1011 110x xxxx xxxx xxxx xxxx xxxx */
  282. DECODE_EMULATEX (0xfe000000, 0xea000000, t32_emulate_rd8rn16rm0_rwflags,
  283. REGS(NOSPPC, 0, NOSPPC, 0, NOSPPC)),
  284. DECODE_END
  285. };
  286. static const union decode_item t32_table_1111_0x0x___0[] = {
  287. /* Data-processing (modified immediate) */
  288. /* TST 1111 0x00 0001 xxxx 0xxx 1111 xxxx xxxx */
  289. /* TEQ 1111 0x00 1001 xxxx 0xxx 1111 xxxx xxxx */
  290. DECODE_EMULATEX (0xfb708f00, 0xf0100f00, t32_emulate_rd8rn16rm0_rwflags,
  291. REGS(NOSPPC, 0, 0, 0, 0)),
  292. /* CMN 1111 0x01 0001 xxxx 0xxx 1111 xxxx xxxx */
  293. DECODE_OR (0xfbf08f00, 0xf1100f00),
  294. /* CMP 1111 0x01 1011 xxxx 0xxx 1111 xxxx xxxx */
  295. DECODE_EMULATEX (0xfbf08f00, 0xf1b00f00, t32_emulate_rd8rn16rm0_rwflags,
  296. REGS(NOPC, 0, 0, 0, 0)),
  297. /* MOV 1111 0x00 010x 1111 0xxx xxxx xxxx xxxx */
  298. /* MVN 1111 0x00 011x 1111 0xxx xxxx xxxx xxxx */
  299. DECODE_EMULATEX (0xfbcf8000, 0xf04f0000, t32_emulate_rd8rn16rm0_rwflags,
  300. REGS(0, 0, NOSPPC, 0, 0)),
  301. /* ??? 1111 0x00 101x xxxx 0xxx xxxx xxxx xxxx */
  302. DECODE_REJECT (0xfbe08000, 0xf0a00000),
  303. /* ??? 1111 0x00 110x xxxx 0xxx xxxx xxxx xxxx */
  304. /* ??? 1111 0x00 111x xxxx 0xxx xxxx xxxx xxxx */
  305. DECODE_REJECT (0xfbc08000, 0xf0c00000),
  306. /* ??? 1111 0x01 001x xxxx 0xxx xxxx xxxx xxxx */
  307. DECODE_REJECT (0xfbe08000, 0xf1200000),
  308. /* ??? 1111 0x01 100x xxxx 0xxx xxxx xxxx xxxx */
  309. DECODE_REJECT (0xfbe08000, 0xf1800000),
  310. /* ??? 1111 0x01 111x xxxx 0xxx xxxx xxxx xxxx */
  311. DECODE_REJECT (0xfbe08000, 0xf1e00000),
  312. /* ADD Rd, SP, #imm 1111 0x01 000x 1101 0xxx xxxx xxxx xxxx */
  313. /* SUB Rd, SP, #imm 1111 0x01 101x 1101 0xxx xxxx xxxx xxxx */
  314. DECODE_EMULATEX (0xfb4f8000, 0xf10d0000, t32_emulate_rd8rn16rm0_rwflags,
  315. REGS(SP, 0, NOPC, 0, 0)),
  316. /* AND 1111 0x00 000x xxxx 0xxx xxxx xxxx xxxx */
  317. /* BIC 1111 0x00 001x xxxx 0xxx xxxx xxxx xxxx */
  318. /* ORR 1111 0x00 010x xxxx 0xxx xxxx xxxx xxxx */
  319. /* ORN 1111 0x00 011x xxxx 0xxx xxxx xxxx xxxx */
  320. /* EOR 1111 0x00 100x xxxx 0xxx xxxx xxxx xxxx */
  321. /* ADD 1111 0x01 000x xxxx 0xxx xxxx xxxx xxxx */
  322. /* ADC 1111 0x01 010x xxxx 0xxx xxxx xxxx xxxx */
  323. /* SBC 1111 0x01 011x xxxx 0xxx xxxx xxxx xxxx */
  324. /* SUB 1111 0x01 101x xxxx 0xxx xxxx xxxx xxxx */
  325. /* RSB 1111 0x01 110x xxxx 0xxx xxxx xxxx xxxx */
  326. DECODE_EMULATEX (0xfa008000, 0xf0000000, t32_emulate_rd8rn16rm0_rwflags,
  327. REGS(NOSPPC, 0, NOSPPC, 0, 0)),
  328. DECODE_END
  329. };
  330. static const union decode_item t32_table_1111_0x1x___0[] = {
  331. /* Data-processing (plain binary immediate) */
  332. /* ADDW Rd, PC, #imm 1111 0x10 0000 1111 0xxx xxxx xxxx xxxx */
  333. DECODE_OR (0xfbff8000, 0xf20f0000),
  334. /* SUBW Rd, PC, #imm 1111 0x10 1010 1111 0xxx xxxx xxxx xxxx */
  335. DECODE_EMULATEX (0xfbff8000, 0xf2af0000, t32_emulate_rd8pc16_noflags,
  336. REGS(PC, 0, NOSPPC, 0, 0)),
  337. /* ADDW SP, SP, #imm 1111 0x10 0000 1101 0xxx 1101 xxxx xxxx */
  338. DECODE_OR (0xfbff8f00, 0xf20d0d00),
  339. /* SUBW SP, SP, #imm 1111 0x10 1010 1101 0xxx 1101 xxxx xxxx */
  340. DECODE_EMULATEX (0xfbff8f00, 0xf2ad0d00, t32_emulate_rd8rn16_noflags,
  341. REGS(SP, 0, SP, 0, 0)),
  342. /* ADDW 1111 0x10 0000 xxxx 0xxx xxxx xxxx xxxx */
  343. DECODE_OR (0xfbf08000, 0xf2000000),
  344. /* SUBW 1111 0x10 1010 xxxx 0xxx xxxx xxxx xxxx */
  345. DECODE_EMULATEX (0xfbf08000, 0xf2a00000, t32_emulate_rd8rn16_noflags,
  346. REGS(NOPCX, 0, NOSPPC, 0, 0)),
  347. /* MOVW 1111 0x10 0100 xxxx 0xxx xxxx xxxx xxxx */
  348. /* MOVT 1111 0x10 1100 xxxx 0xxx xxxx xxxx xxxx */
  349. DECODE_EMULATEX (0xfb708000, 0xf2400000, t32_emulate_rd8rn16_noflags,
  350. REGS(0, 0, NOSPPC, 0, 0)),
  351. /* SSAT16 1111 0x11 0010 xxxx 0000 xxxx 00xx xxxx */
  352. /* SSAT 1111 0x11 00x0 xxxx 0xxx xxxx xxxx xxxx */
  353. /* USAT16 1111 0x11 1010 xxxx 0000 xxxx 00xx xxxx */
  354. /* USAT 1111 0x11 10x0 xxxx 0xxx xxxx xxxx xxxx */
  355. DECODE_EMULATEX (0xfb508000, 0xf3000000, t32_emulate_rd8rn16rm0_rwflags,
  356. REGS(NOSPPC, 0, NOSPPC, 0, 0)),
  357. /* SFBX 1111 0x11 0100 xxxx 0xxx xxxx xxxx xxxx */
  358. /* UFBX 1111 0x11 1100 xxxx 0xxx xxxx xxxx xxxx */
  359. DECODE_EMULATEX (0xfb708000, 0xf3400000, t32_emulate_rd8rn16_noflags,
  360. REGS(NOSPPC, 0, NOSPPC, 0, 0)),
  361. /* BFC 1111 0x11 0110 1111 0xxx xxxx xxxx xxxx */
  362. DECODE_EMULATEX (0xfbff8000, 0xf36f0000, t32_emulate_rd8rn16_noflags,
  363. REGS(0, 0, NOSPPC, 0, 0)),
  364. /* BFI 1111 0x11 0110 xxxx 0xxx xxxx xxxx xxxx */
  365. DECODE_EMULATEX (0xfbf08000, 0xf3600000, t32_emulate_rd8rn16_noflags,
  366. REGS(NOSPPCX, 0, NOSPPC, 0, 0)),
  367. DECODE_END
  368. };
  369. static const union decode_item t32_table_1111_0xxx___1[] = {
  370. /* Branches and miscellaneous control */
  371. /* YIELD 1111 0011 1010 xxxx 10x0 x000 0000 0001 */
  372. DECODE_OR (0xfff0d7ff, 0xf3a08001),
  373. /* SEV 1111 0011 1010 xxxx 10x0 x000 0000 0100 */
  374. DECODE_EMULATE (0xfff0d7ff, 0xf3a08004, kprobe_emulate_none),
  375. /* NOP 1111 0011 1010 xxxx 10x0 x000 0000 0000 */
  376. /* WFE 1111 0011 1010 xxxx 10x0 x000 0000 0010 */
  377. /* WFI 1111 0011 1010 xxxx 10x0 x000 0000 0011 */
  378. DECODE_SIMULATE (0xfff0d7fc, 0xf3a08000, kprobe_simulate_nop),
  379. /* MRS Rd, CPSR 1111 0011 1110 xxxx 10x0 xxxx xxxx xxxx */
  380. DECODE_SIMULATEX(0xfff0d000, 0xf3e08000, t32_simulate_mrs,
  381. REGS(0, 0, NOSPPC, 0, 0)),
  382. /*
  383. * Unsupported instructions
  384. * 1111 0x11 1xxx xxxx 10x0 xxxx xxxx xxxx
  385. *
  386. * MSR 1111 0011 100x xxxx 10x0 xxxx xxxx xxxx
  387. * DBG hint 1111 0011 1010 xxxx 10x0 x000 1111 xxxx
  388. * Unallocated hints 1111 0011 1010 xxxx 10x0 x000 xxxx xxxx
  389. * CPS 1111 0011 1010 xxxx 10x0 xxxx xxxx xxxx
  390. * CLREX/DSB/DMB/ISB 1111 0011 1011 xxxx 10x0 xxxx xxxx xxxx
  391. * BXJ 1111 0011 1100 xxxx 10x0 xxxx xxxx xxxx
  392. * SUBS PC,LR,#<imm8> 1111 0011 1101 xxxx 10x0 xxxx xxxx xxxx
  393. * MRS Rd, SPSR 1111 0011 1111 xxxx 10x0 xxxx xxxx xxxx
  394. * SMC 1111 0111 1111 xxxx 1000 xxxx xxxx xxxx
  395. * UNDEFINED 1111 0111 1111 xxxx 1010 xxxx xxxx xxxx
  396. * ??? 1111 0111 1xxx xxxx 1010 xxxx xxxx xxxx
  397. */
  398. DECODE_REJECT (0xfb80d000, 0xf3808000),
  399. /* Bcc 1111 0xxx xxxx xxxx 10x0 xxxx xxxx xxxx */
  400. DECODE_CUSTOM (0xf800d000, 0xf0008000, t32_decode_cond_branch),
  401. /* BLX 1111 0xxx xxxx xxxx 11x0 xxxx xxxx xxx0 */
  402. DECODE_OR (0xf800d001, 0xf000c000),
  403. /* B 1111 0xxx xxxx xxxx 10x1 xxxx xxxx xxxx */
  404. /* BL 1111 0xxx xxxx xxxx 11x1 xxxx xxxx xxxx */
  405. DECODE_SIMULATE (0xf8009000, 0xf0009000, t32_simulate_branch),
  406. DECODE_END
  407. };
  408. const union decode_item kprobe_decode_thumb32_table[] = {
  409. /*
  410. * Load/store multiple instructions
  411. * 1110 100x x0xx xxxx xxxx xxxx xxxx xxxx
  412. */
  413. DECODE_TABLE (0xfe400000, 0xe8000000, t32_table_1110_100x_x0xx),
  414. /*
  415. * Load/store dual, load/store exclusive, table branch
  416. * 1110 100x x1xx xxxx xxxx xxxx xxxx xxxx
  417. */
  418. DECODE_TABLE (0xfe400000, 0xe8400000, t32_table_1110_100x_x1xx),
  419. /*
  420. * Data-processing (shifted register)
  421. * 1110 101x xxxx xxxx xxxx xxxx xxxx xxxx
  422. */
  423. DECODE_TABLE (0xfe000000, 0xea000000, t32_table_1110_101x),
  424. /*
  425. * Data-processing (modified immediate)
  426. * 1111 0x0x xxxx xxxx 0xxx xxxx xxxx xxxx
  427. */
  428. DECODE_TABLE (0xfa008000, 0xf0000000, t32_table_1111_0x0x___0),
  429. /*
  430. * Data-processing (plain binary immediate)
  431. * 1111 0x1x xxxx xxxx 0xxx xxxx xxxx xxxx
  432. */
  433. DECODE_TABLE (0xfa008000, 0xf2000000, t32_table_1111_0x1x___0),
  434. /*
  435. * Branches and miscellaneous control
  436. * 1111 0xxx xxxx xxxx 1xxx xxxx xxxx xxxx
  437. */
  438. DECODE_TABLE (0xf8008000, 0xf0008000, t32_table_1111_0xxx___1),
  439. DECODE_END
  440. };
  441. static void __kprobes
  442. t16_simulate_bxblx(struct kprobe *p, struct pt_regs *regs)
  443. {
  444. kprobe_opcode_t insn = p->opcode;
  445. unsigned long pc = thumb_probe_pc(p);
  446. int rm = (insn >> 3) & 0xf;
  447. unsigned long rmv = (rm == 15) ? pc : regs->uregs[rm];
  448. if (insn & (1 << 7)) /* BLX ? */
  449. regs->ARM_lr = (unsigned long)p->addr + 2;
  450. bx_write_pc(rmv, regs);
  451. }
  452. static void __kprobes
  453. t16_simulate_ldr_literal(struct kprobe *p, struct pt_regs *regs)
  454. {
  455. kprobe_opcode_t insn = p->opcode;
  456. unsigned long* base = (unsigned long *)(thumb_probe_pc(p) & ~3);
  457. long index = insn & 0xff;
  458. int rt = (insn >> 8) & 0x7;
  459. regs->uregs[rt] = base[index];
  460. }
  461. static void __kprobes
  462. t16_simulate_ldrstr_sp_relative(struct kprobe *p, struct pt_regs *regs)
  463. {
  464. kprobe_opcode_t insn = p->opcode;
  465. unsigned long* base = (unsigned long *)regs->ARM_sp;
  466. long index = insn & 0xff;
  467. int rt = (insn >> 8) & 0x7;
  468. if (insn & 0x800) /* LDR */
  469. regs->uregs[rt] = base[index];
  470. else /* STR */
  471. base[index] = regs->uregs[rt];
  472. }
  473. static void __kprobes
  474. t16_simulate_reladr(struct kprobe *p, struct pt_regs *regs)
  475. {
  476. kprobe_opcode_t insn = p->opcode;
  477. unsigned long base = (insn & 0x800) ? regs->ARM_sp
  478. : (thumb_probe_pc(p) & ~3);
  479. long offset = insn & 0xff;
  480. int rt = (insn >> 8) & 0x7;
  481. regs->uregs[rt] = base + offset * 4;
  482. }
  483. static void __kprobes
  484. t16_simulate_add_sp_imm(struct kprobe *p, struct pt_regs *regs)
  485. {
  486. kprobe_opcode_t insn = p->opcode;
  487. long imm = insn & 0x7f;
  488. if (insn & 0x80) /* SUB */
  489. regs->ARM_sp -= imm * 4;
  490. else /* ADD */
  491. regs->ARM_sp += imm * 4;
  492. }
  493. static void __kprobes
  494. t16_simulate_cbz(struct kprobe *p, struct pt_regs *regs)
  495. {
  496. kprobe_opcode_t insn = p->opcode;
  497. int rn = insn & 0x7;
  498. kprobe_opcode_t nonzero = regs->uregs[rn] ? insn : ~insn;
  499. if (nonzero & 0x800) {
  500. long i = insn & 0x200;
  501. long imm5 = insn & 0xf8;
  502. unsigned long pc = thumb_probe_pc(p);
  503. regs->ARM_pc = pc + (i >> 3) + (imm5 >> 2);
  504. }
  505. }
  506. static void __kprobes
  507. t16_simulate_it(struct kprobe *p, struct pt_regs *regs)
  508. {
  509. /*
  510. * The 8 IT state bits are split into two parts in CPSR:
  511. * ITSTATE<1:0> are in CPSR<26:25>
  512. * ITSTATE<7:2> are in CPSR<15:10>
  513. * The new IT state is in the lower byte of insn.
  514. */
  515. kprobe_opcode_t insn = p->opcode;
  516. unsigned long cpsr = regs->ARM_cpsr;
  517. cpsr &= ~PSR_IT_MASK;
  518. cpsr |= (insn & 0xfc) << 8;
  519. cpsr |= (insn & 0x03) << 25;
  520. regs->ARM_cpsr = cpsr;
  521. }
  522. static void __kprobes
  523. t16_singlestep_it(struct kprobe *p, struct pt_regs *regs)
  524. {
  525. regs->ARM_pc += 2;
  526. t16_simulate_it(p, regs);
  527. }
  528. static enum kprobe_insn __kprobes
  529. t16_decode_it(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  530. {
  531. asi->insn_singlestep = t16_singlestep_it;
  532. return INSN_GOOD_NO_SLOT;
  533. }
  534. static void __kprobes
  535. t16_simulate_cond_branch(struct kprobe *p, struct pt_regs *regs)
  536. {
  537. kprobe_opcode_t insn = p->opcode;
  538. unsigned long pc = thumb_probe_pc(p);
  539. long offset = insn & 0x7f;
  540. offset -= insn & 0x80; /* Apply sign bit */
  541. regs->ARM_pc = pc + (offset * 2);
  542. }
  543. static enum kprobe_insn __kprobes
  544. t16_decode_cond_branch(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  545. {
  546. int cc = (insn >> 8) & 0xf;
  547. asi->insn_check_cc = kprobe_condition_checks[cc];
  548. asi->insn_handler = t16_simulate_cond_branch;
  549. return INSN_GOOD_NO_SLOT;
  550. }
  551. static void __kprobes
  552. t16_simulate_branch(struct kprobe *p, struct pt_regs *regs)
  553. {
  554. kprobe_opcode_t insn = p->opcode;
  555. unsigned long pc = thumb_probe_pc(p);
  556. long offset = insn & 0x3ff;
  557. offset -= insn & 0x400; /* Apply sign bit */
  558. regs->ARM_pc = pc + (offset * 2);
  559. }
  560. static unsigned long __kprobes
  561. t16_emulate_loregs(struct kprobe *p, struct pt_regs *regs)
  562. {
  563. unsigned long oldcpsr = regs->ARM_cpsr;
  564. unsigned long newcpsr;
  565. __asm__ __volatile__ (
  566. "msr cpsr_fs, %[oldcpsr] \n\t"
  567. "ldmia %[regs], {r0-r7} \n\t"
  568. "blx %[fn] \n\t"
  569. "stmia %[regs], {r0-r7} \n\t"
  570. "mrs %[newcpsr], cpsr \n\t"
  571. : [newcpsr] "=r" (newcpsr)
  572. : [oldcpsr] "r" (oldcpsr), [regs] "r" (regs),
  573. [fn] "r" (p->ainsn.insn_fn)
  574. : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  575. "lr", "memory", "cc"
  576. );
  577. return (oldcpsr & ~APSR_MASK) | (newcpsr & APSR_MASK);
  578. }
  579. static void __kprobes
  580. t16_emulate_loregs_rwflags(struct kprobe *p, struct pt_regs *regs)
  581. {
  582. regs->ARM_cpsr = t16_emulate_loregs(p, regs);
  583. }
  584. static void __kprobes
  585. t16_emulate_loregs_noitrwflags(struct kprobe *p, struct pt_regs *regs)
  586. {
  587. unsigned long cpsr = t16_emulate_loregs(p, regs);
  588. if (!in_it_block(cpsr))
  589. regs->ARM_cpsr = cpsr;
  590. }
  591. static void __kprobes
  592. t16_emulate_hiregs(struct kprobe *p, struct pt_regs *regs)
  593. {
  594. kprobe_opcode_t insn = p->opcode;
  595. unsigned long pc = thumb_probe_pc(p);
  596. int rdn = (insn & 0x7) | ((insn & 0x80) >> 4);
  597. int rm = (insn >> 3) & 0xf;
  598. register unsigned long rdnv asm("r1");
  599. register unsigned long rmv asm("r0");
  600. unsigned long cpsr = regs->ARM_cpsr;
  601. rdnv = (rdn == 15) ? pc : regs->uregs[rdn];
  602. rmv = (rm == 15) ? pc : regs->uregs[rm];
  603. __asm__ __volatile__ (
  604. "msr cpsr_fs, %[cpsr] \n\t"
  605. "blx %[fn] \n\t"
  606. "mrs %[cpsr], cpsr \n\t"
  607. : "=r" (rdnv), [cpsr] "=r" (cpsr)
  608. : "0" (rdnv), "r" (rmv), "1" (cpsr), [fn] "r" (p->ainsn.insn_fn)
  609. : "lr", "memory", "cc"
  610. );
  611. if (rdn == 15)
  612. rdnv &= ~1;
  613. regs->uregs[rdn] = rdnv;
  614. regs->ARM_cpsr = (regs->ARM_cpsr & ~APSR_MASK) | (cpsr & APSR_MASK);
  615. }
  616. static enum kprobe_insn __kprobes
  617. t16_decode_hiregs(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  618. {
  619. insn &= ~0x00ff;
  620. insn |= 0x001; /* Set Rdn = R1 and Rm = R0 */
  621. ((u16 *)asi->insn)[0] = insn;
  622. asi->insn_handler = t16_emulate_hiregs;
  623. return INSN_GOOD;
  624. }
  625. static void __kprobes
  626. t16_emulate_push(struct kprobe *p, struct pt_regs *regs)
  627. {
  628. __asm__ __volatile__ (
  629. "ldr r9, [%[regs], #13*4] \n\t"
  630. "ldr r8, [%[regs], #14*4] \n\t"
  631. "ldmia %[regs], {r0-r7} \n\t"
  632. "blx %[fn] \n\t"
  633. "str r9, [%[regs], #13*4] \n\t"
  634. :
  635. : [regs] "r" (regs), [fn] "r" (p->ainsn.insn_fn)
  636. : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9",
  637. "lr", "memory", "cc"
  638. );
  639. }
  640. static enum kprobe_insn __kprobes
  641. t16_decode_push(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  642. {
  643. /*
  644. * To simulate a PUSH we use a Thumb-2 "STMDB R9!, {registers}"
  645. * and call it with R9=SP and LR in the register list represented
  646. * by R8.
  647. */
  648. ((u16 *)asi->insn)[0] = 0xe929; /* 1st half STMDB R9!,{} */
  649. ((u16 *)asi->insn)[1] = insn & 0x1ff; /* 2nd half (register list) */
  650. asi->insn_handler = t16_emulate_push;
  651. return INSN_GOOD;
  652. }
  653. static void __kprobes
  654. t16_emulate_pop_nopc(struct kprobe *p, struct pt_regs *regs)
  655. {
  656. __asm__ __volatile__ (
  657. "ldr r9, [%[regs], #13*4] \n\t"
  658. "ldmia %[regs], {r0-r7} \n\t"
  659. "blx %[fn] \n\t"
  660. "stmia %[regs], {r0-r7} \n\t"
  661. "str r9, [%[regs], #13*4] \n\t"
  662. :
  663. : [regs] "r" (regs), [fn] "r" (p->ainsn.insn_fn)
  664. : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r9",
  665. "lr", "memory", "cc"
  666. );
  667. }
  668. static void __kprobes
  669. t16_emulate_pop_pc(struct kprobe *p, struct pt_regs *regs)
  670. {
  671. register unsigned long pc asm("r8");
  672. __asm__ __volatile__ (
  673. "ldr r9, [%[regs], #13*4] \n\t"
  674. "ldmia %[regs], {r0-r7} \n\t"
  675. "blx %[fn] \n\t"
  676. "stmia %[regs], {r0-r7} \n\t"
  677. "str r9, [%[regs], #13*4] \n\t"
  678. : "=r" (pc)
  679. : [regs] "r" (regs), [fn] "r" (p->ainsn.insn_fn)
  680. : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r9",
  681. "lr", "memory", "cc"
  682. );
  683. bx_write_pc(pc, regs);
  684. }
  685. static enum kprobe_insn __kprobes
  686. t16_decode_pop(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  687. {
  688. /*
  689. * To simulate a POP we use a Thumb-2 "LDMDB R9!, {registers}"
  690. * and call it with R9=SP and PC in the register list represented
  691. * by R8.
  692. */
  693. ((u16 *)asi->insn)[0] = 0xe8b9; /* 1st half LDMIA R9!,{} */
  694. ((u16 *)asi->insn)[1] = insn & 0x1ff; /* 2nd half (register list) */
  695. asi->insn_handler = insn & 0x100 ? t16_emulate_pop_pc
  696. : t16_emulate_pop_nopc;
  697. return INSN_GOOD;
  698. }
  699. static const union decode_item t16_table_1011[] = {
  700. /* Miscellaneous 16-bit instructions */
  701. /* ADD (SP plus immediate) 1011 0000 0xxx xxxx */
  702. /* SUB (SP minus immediate) 1011 0000 1xxx xxxx */
  703. DECODE_SIMULATE (0xff00, 0xb000, t16_simulate_add_sp_imm),
  704. /* CBZ 1011 00x1 xxxx xxxx */
  705. /* CBNZ 1011 10x1 xxxx xxxx */
  706. DECODE_SIMULATE (0xf500, 0xb100, t16_simulate_cbz),
  707. /* SXTH 1011 0010 00xx xxxx */
  708. /* SXTB 1011 0010 01xx xxxx */
  709. /* UXTH 1011 0010 10xx xxxx */
  710. /* UXTB 1011 0010 11xx xxxx */
  711. /* REV 1011 1010 00xx xxxx */
  712. /* REV16 1011 1010 01xx xxxx */
  713. /* ??? 1011 1010 10xx xxxx */
  714. /* REVSH 1011 1010 11xx xxxx */
  715. DECODE_REJECT (0xffc0, 0xba80),
  716. DECODE_EMULATE (0xf500, 0xb000, t16_emulate_loregs_rwflags),
  717. /* PUSH 1011 010x xxxx xxxx */
  718. DECODE_CUSTOM (0xfe00, 0xb400, t16_decode_push),
  719. /* POP 1011 110x xxxx xxxx */
  720. DECODE_CUSTOM (0xfe00, 0xbc00, t16_decode_pop),
  721. /*
  722. * If-Then, and hints
  723. * 1011 1111 xxxx xxxx
  724. */
  725. /* YIELD 1011 1111 0001 0000 */
  726. DECODE_OR (0xffff, 0xbf10),
  727. /* SEV 1011 1111 0100 0000 */
  728. DECODE_EMULATE (0xffff, 0xbf40, kprobe_emulate_none),
  729. /* NOP 1011 1111 0000 0000 */
  730. /* WFE 1011 1111 0010 0000 */
  731. /* WFI 1011 1111 0011 0000 */
  732. DECODE_SIMULATE (0xffcf, 0xbf00, kprobe_simulate_nop),
  733. /* Unassigned hints 1011 1111 xxxx 0000 */
  734. DECODE_REJECT (0xff0f, 0xbf00),
  735. /* IT 1011 1111 xxxx xxxx */
  736. DECODE_CUSTOM (0xff00, 0xbf00, t16_decode_it),
  737. /* SETEND 1011 0110 010x xxxx */
  738. /* CPS 1011 0110 011x xxxx */
  739. /* BKPT 1011 1110 xxxx xxxx */
  740. /* And unallocated instructions... */
  741. DECODE_END
  742. };
  743. const union decode_item kprobe_decode_thumb16_table[] = {
  744. /*
  745. * Shift (immediate), add, subtract, move, and compare
  746. * 00xx xxxx xxxx xxxx
  747. */
  748. /* CMP (immediate) 0010 1xxx xxxx xxxx */
  749. DECODE_EMULATE (0xf800, 0x2800, t16_emulate_loregs_rwflags),
  750. /* ADD (register) 0001 100x xxxx xxxx */
  751. /* SUB (register) 0001 101x xxxx xxxx */
  752. /* LSL (immediate) 0000 0xxx xxxx xxxx */
  753. /* LSR (immediate) 0000 1xxx xxxx xxxx */
  754. /* ASR (immediate) 0001 0xxx xxxx xxxx */
  755. /* ADD (immediate, Thumb) 0001 110x xxxx xxxx */
  756. /* SUB (immediate, Thumb) 0001 111x xxxx xxxx */
  757. /* MOV (immediate) 0010 0xxx xxxx xxxx */
  758. /* ADD (immediate, Thumb) 0011 0xxx xxxx xxxx */
  759. /* SUB (immediate, Thumb) 0011 1xxx xxxx xxxx */
  760. DECODE_EMULATE (0xc000, 0x0000, t16_emulate_loregs_noitrwflags),
  761. /*
  762. * 16-bit Thumb data-processing instructions
  763. * 0100 00xx xxxx xxxx
  764. */
  765. /* TST (register) 0100 0010 00xx xxxx */
  766. DECODE_EMULATE (0xffc0, 0x4200, t16_emulate_loregs_rwflags),
  767. /* CMP (register) 0100 0010 10xx xxxx */
  768. /* CMN (register) 0100 0010 11xx xxxx */
  769. DECODE_EMULATE (0xff80, 0x4280, t16_emulate_loregs_rwflags),
  770. /* AND (register) 0100 0000 00xx xxxx */
  771. /* EOR (register) 0100 0000 01xx xxxx */
  772. /* LSL (register) 0100 0000 10xx xxxx */
  773. /* LSR (register) 0100 0000 11xx xxxx */
  774. /* ASR (register) 0100 0001 00xx xxxx */
  775. /* ADC (register) 0100 0001 01xx xxxx */
  776. /* SBC (register) 0100 0001 10xx xxxx */
  777. /* ROR (register) 0100 0001 11xx xxxx */
  778. /* RSB (immediate) 0100 0010 01xx xxxx */
  779. /* ORR (register) 0100 0011 00xx xxxx */
  780. /* MUL 0100 0011 00xx xxxx */
  781. /* BIC (register) 0100 0011 10xx xxxx */
  782. /* MVN (register) 0100 0011 10xx xxxx */
  783. DECODE_EMULATE (0xfc00, 0x4000, t16_emulate_loregs_noitrwflags),
  784. /*
  785. * Special data instructions and branch and exchange
  786. * 0100 01xx xxxx xxxx
  787. */
  788. /* BLX pc 0100 0111 1111 1xxx */
  789. DECODE_REJECT (0xfff8, 0x47f8),
  790. /* BX (register) 0100 0111 0xxx xxxx */
  791. /* BLX (register) 0100 0111 1xxx xxxx */
  792. DECODE_SIMULATE (0xff00, 0x4700, t16_simulate_bxblx),
  793. /* ADD pc, pc 0100 0100 1111 1111 */
  794. DECODE_REJECT (0xffff, 0x44ff),
  795. /* ADD (register) 0100 0100 xxxx xxxx */
  796. /* CMP (register) 0100 0101 xxxx xxxx */
  797. /* MOV (register) 0100 0110 xxxx xxxx */
  798. DECODE_CUSTOM (0xfc00, 0x4400, t16_decode_hiregs),
  799. /*
  800. * Load from Literal Pool
  801. * LDR (literal) 0100 1xxx xxxx xxxx
  802. */
  803. DECODE_SIMULATE (0xf800, 0x4800, t16_simulate_ldr_literal),
  804. /*
  805. * 16-bit Thumb Load/store instructions
  806. * 0101 xxxx xxxx xxxx
  807. * 011x xxxx xxxx xxxx
  808. * 100x xxxx xxxx xxxx
  809. */
  810. /* STR (register) 0101 000x xxxx xxxx */
  811. /* STRH (register) 0101 001x xxxx xxxx */
  812. /* STRB (register) 0101 010x xxxx xxxx */
  813. /* LDRSB (register) 0101 011x xxxx xxxx */
  814. /* LDR (register) 0101 100x xxxx xxxx */
  815. /* LDRH (register) 0101 101x xxxx xxxx */
  816. /* LDRB (register) 0101 110x xxxx xxxx */
  817. /* LDRSH (register) 0101 111x xxxx xxxx */
  818. /* STR (immediate, Thumb) 0110 0xxx xxxx xxxx */
  819. /* LDR (immediate, Thumb) 0110 1xxx xxxx xxxx */
  820. /* STRB (immediate, Thumb) 0111 0xxx xxxx xxxx */
  821. /* LDRB (immediate, Thumb) 0111 1xxx xxxx xxxx */
  822. DECODE_EMULATE (0xc000, 0x4000, t16_emulate_loregs_rwflags),
  823. /* STRH (immediate, Thumb) 1000 0xxx xxxx xxxx */
  824. /* LDRH (immediate, Thumb) 1000 1xxx xxxx xxxx */
  825. DECODE_EMULATE (0xf000, 0x8000, t16_emulate_loregs_rwflags),
  826. /* STR (immediate, Thumb) 1001 0xxx xxxx xxxx */
  827. /* LDR (immediate, Thumb) 1001 1xxx xxxx xxxx */
  828. DECODE_SIMULATE (0xf000, 0x9000, t16_simulate_ldrstr_sp_relative),
  829. /*
  830. * Generate PC-/SP-relative address
  831. * ADR (literal) 1010 0xxx xxxx xxxx
  832. * ADD (SP plus immediate) 1010 1xxx xxxx xxxx
  833. */
  834. DECODE_SIMULATE (0xf000, 0xa000, t16_simulate_reladr),
  835. /*
  836. * Miscellaneous 16-bit instructions
  837. * 1011 xxxx xxxx xxxx
  838. */
  839. DECODE_TABLE (0xf000, 0xb000, t16_table_1011),
  840. /* STM 1100 0xxx xxxx xxxx */
  841. /* LDM 1100 1xxx xxxx xxxx */
  842. DECODE_EMULATE (0xf000, 0xc000, t16_emulate_loregs_rwflags),
  843. /*
  844. * Conditional branch, and Supervisor Call
  845. */
  846. /* Permanently UNDEFINED 1101 1110 xxxx xxxx */
  847. /* SVC 1101 1111 xxxx xxxx */
  848. DECODE_REJECT (0xfe00, 0xde00),
  849. /* Conditional branch 1101 xxxx xxxx xxxx */
  850. DECODE_CUSTOM (0xf000, 0xd000, t16_decode_cond_branch),
  851. /*
  852. * Unconditional branch
  853. * B 1110 0xxx xxxx xxxx
  854. */
  855. DECODE_SIMULATE (0xf800, 0xe000, t16_simulate_branch),
  856. DECODE_END
  857. };
  858. static unsigned long __kprobes thumb_check_cc(unsigned long cpsr)
  859. {
  860. if (unlikely(in_it_block(cpsr)))
  861. return kprobe_condition_checks[current_cond(cpsr)](cpsr);
  862. return true;
  863. }
  864. static void __kprobes thumb16_singlestep(struct kprobe *p, struct pt_regs *regs)
  865. {
  866. regs->ARM_pc += 2;
  867. p->ainsn.insn_handler(p, regs);
  868. regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
  869. }
  870. static void __kprobes thumb32_singlestep(struct kprobe *p, struct pt_regs *regs)
  871. {
  872. regs->ARM_pc += 4;
  873. p->ainsn.insn_handler(p, regs);
  874. regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
  875. }
  876. enum kprobe_insn __kprobes
  877. thumb16_kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  878. {
  879. asi->insn_singlestep = thumb16_singlestep;
  880. asi->insn_check_cc = thumb_check_cc;
  881. return kprobe_decode_insn(insn, asi, kprobe_decode_thumb16_table, true);
  882. }
  883. enum kprobe_insn __kprobes
  884. thumb32_kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  885. {
  886. asi->insn_singlestep = thumb32_singlestep;
  887. asi->insn_check_cc = thumb_check_cc;
  888. return kprobe_decode_insn(insn, asi, kprobe_decode_thumb32_table, true);
  889. }