decode-arm.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * arch/arm/probes/decode-arm.c
  5. *
  6. * Some code moved here from arch/arm/kernel/kprobes-arm.c
  7. *
  8. * Copyright (C) 2006, 2007 Motorola Inc.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/stddef.h>
  13. #include <linux/ptrace.h>
  14. #include "decode.h"
  15. #include "decode-arm.h"
  16. #define sign_extend(x, signbit) ((x) | (0 - ((x) & (1 << (signbit)))))
  17. #define branch_displacement(insn) sign_extend(((insn) & 0xffffff) << 2, 25)
  18. /*
  19. * To avoid the complications of mimicing single-stepping on a
  20. * processor without a Next-PC or a single-step mode, and to
  21. * avoid having to deal with the side-effects of boosting, we
  22. * simulate or emulate (almost) all ARM instructions.
  23. *
  24. * "Simulation" is where the instruction's behavior is duplicated in
  25. * C code. "Emulation" is where the original instruction is rewritten
  26. * and executed, often by altering its registers.
  27. *
  28. * By having all behavior of the kprobe'd instruction completed before
  29. * returning from the kprobe_handler(), all locks (scheduler and
  30. * interrupt) can safely be released. There is no need for secondary
  31. * breakpoints, no race with MP or preemptable kernels, nor having to
  32. * clean up resources counts at a later time impacting overall system
  33. * performance. By rewriting the instruction, only the minimum registers
  34. * need to be loaded and saved back optimizing performance.
  35. *
  36. * Calling the insnslot_*_rwflags version of a function doesn't hurt
  37. * anything even when the CPSR flags aren't updated by the
  38. * instruction. It's just a little slower in return for saving
  39. * a little space by not having a duplicate function that doesn't
  40. * update the flags. (The same optimization can be said for
  41. * instructions that do or don't perform register writeback)
  42. * Also, instructions can either read the flags, only write the
  43. * flags, or read and write the flags. To save combinations
  44. * rather than for sheer performance, flag functions just assume
  45. * read and write of flags.
  46. */
  47. void __kprobes simulate_bbl(probes_opcode_t insn,
  48. struct arch_probes_insn *asi, struct pt_regs *regs)
  49. {
  50. long iaddr = (long) regs->ARM_pc - 4;
  51. int disp = branch_displacement(insn);
  52. if (insn & (1 << 24))
  53. regs->ARM_lr = iaddr + 4;
  54. regs->ARM_pc = iaddr + 8 + disp;
  55. }
  56. void __kprobes simulate_blx1(probes_opcode_t insn,
  57. struct arch_probes_insn *asi, struct pt_regs *regs)
  58. {
  59. long iaddr = (long) regs->ARM_pc - 4;
  60. int disp = branch_displacement(insn);
  61. regs->ARM_lr = iaddr + 4;
  62. regs->ARM_pc = iaddr + 8 + disp + ((insn >> 23) & 0x2);
  63. regs->ARM_cpsr |= PSR_T_BIT;
  64. }
  65. void __kprobes simulate_blx2bx(probes_opcode_t insn,
  66. struct arch_probes_insn *asi, struct pt_regs *regs)
  67. {
  68. int rm = insn & 0xf;
  69. long rmv = regs->uregs[rm];
  70. if (insn & (1 << 5))
  71. regs->ARM_lr = (long) regs->ARM_pc;
  72. regs->ARM_pc = rmv & ~0x1;
  73. regs->ARM_cpsr &= ~PSR_T_BIT;
  74. if (rmv & 0x1)
  75. regs->ARM_cpsr |= PSR_T_BIT;
  76. }
  77. void __kprobes simulate_mrs(probes_opcode_t insn,
  78. struct arch_probes_insn *asi, struct pt_regs *regs)
  79. {
  80. int rd = (insn >> 12) & 0xf;
  81. unsigned long mask = 0xf8ff03df; /* Mask out execution state */
  82. regs->uregs[rd] = regs->ARM_cpsr & mask;
  83. }
  84. void __kprobes simulate_mov_ipsp(probes_opcode_t insn,
  85. struct arch_probes_insn *asi, struct pt_regs *regs)
  86. {
  87. regs->uregs[12] = regs->uregs[13];
  88. }
  89. /*
  90. * For the instruction masking and comparisons in all the "space_*"
  91. * functions below, Do _not_ rearrange the order of tests unless
  92. * you're very, very sure of what you are doing. For the sake of
  93. * efficiency, the masks for some tests sometimes assume other test
  94. * have been done prior to them so the number of patterns to test
  95. * for an instruction set can be as broad as possible to reduce the
  96. * number of tests needed.
  97. */
  98. static const union decode_item arm_1111_table[] = {
  99. /* Unconditional instructions */
  100. /* memory hint 1111 0100 x001 xxxx xxxx xxxx xxxx xxxx */
  101. /* PLDI (immediate) 1111 0100 x101 xxxx xxxx xxxx xxxx xxxx */
  102. /* PLDW (immediate) 1111 0101 x001 xxxx xxxx xxxx xxxx xxxx */
  103. /* PLD (immediate) 1111 0101 x101 xxxx xxxx xxxx xxxx xxxx */
  104. DECODE_SIMULATE (0xfe300000, 0xf4100000, PROBES_PRELOAD_IMM),
  105. /* memory hint 1111 0110 x001 xxxx xxxx xxxx xxx0 xxxx */
  106. /* PLDI (register) 1111 0110 x101 xxxx xxxx xxxx xxx0 xxxx */
  107. /* PLDW (register) 1111 0111 x001 xxxx xxxx xxxx xxx0 xxxx */
  108. /* PLD (register) 1111 0111 x101 xxxx xxxx xxxx xxx0 xxxx */
  109. DECODE_SIMULATE (0xfe300010, 0xf6100000, PROBES_PRELOAD_REG),
  110. /* BLX (immediate) 1111 101x xxxx xxxx xxxx xxxx xxxx xxxx */
  111. DECODE_SIMULATE (0xfe000000, 0xfa000000, PROBES_BRANCH_IMM),
  112. /* CPS 1111 0001 0000 xxx0 xxxx xxxx xx0x xxxx */
  113. /* SETEND 1111 0001 0000 0001 xxxx xxxx 0000 xxxx */
  114. /* SRS 1111 100x x1x0 xxxx xxxx xxxx xxxx xxxx */
  115. /* RFE 1111 100x x0x1 xxxx xxxx xxxx xxxx xxxx */
  116. /* Coprocessor instructions... */
  117. /* MCRR2 1111 1100 0100 xxxx xxxx xxxx xxxx xxxx */
  118. /* MRRC2 1111 1100 0101 xxxx xxxx xxxx xxxx xxxx */
  119. /* LDC2 1111 110x xxx1 xxxx xxxx xxxx xxxx xxxx */
  120. /* STC2 1111 110x xxx0 xxxx xxxx xxxx xxxx xxxx */
  121. /* CDP2 1111 1110 xxxx xxxx xxxx xxxx xxx0 xxxx */
  122. /* MCR2 1111 1110 xxx0 xxxx xxxx xxxx xxx1 xxxx */
  123. /* MRC2 1111 1110 xxx1 xxxx xxxx xxxx xxx1 xxxx */
  124. /* Other unallocated instructions... */
  125. DECODE_END
  126. };
  127. static const union decode_item arm_cccc_0001_0xx0____0xxx_table[] = {
  128. /* Miscellaneous instructions */
  129. /* MRS cpsr cccc 0001 0000 xxxx xxxx xxxx 0000 xxxx */
  130. DECODE_SIMULATEX(0x0ff000f0, 0x01000000, PROBES_MRS,
  131. REGS(0, NOPC, 0, 0, 0)),
  132. /* BX cccc 0001 0010 xxxx xxxx xxxx 0001 xxxx */
  133. DECODE_SIMULATE (0x0ff000f0, 0x01200010, PROBES_BRANCH_REG),
  134. /* BLX (register) cccc 0001 0010 xxxx xxxx xxxx 0011 xxxx */
  135. DECODE_SIMULATEX(0x0ff000f0, 0x01200030, PROBES_BRANCH_REG,
  136. REGS(0, 0, 0, 0, NOPC)),
  137. /* CLZ cccc 0001 0110 xxxx xxxx xxxx 0001 xxxx */
  138. DECODE_EMULATEX (0x0ff000f0, 0x01600010, PROBES_CLZ,
  139. REGS(0, NOPC, 0, 0, NOPC)),
  140. /* QADD cccc 0001 0000 xxxx xxxx xxxx 0101 xxxx */
  141. /* QSUB cccc 0001 0010 xxxx xxxx xxxx 0101 xxxx */
  142. /* QDADD cccc 0001 0100 xxxx xxxx xxxx 0101 xxxx */
  143. /* QDSUB cccc 0001 0110 xxxx xxxx xxxx 0101 xxxx */
  144. DECODE_EMULATEX (0x0f9000f0, 0x01000050, PROBES_SATURATING_ARITHMETIC,
  145. REGS(NOPC, NOPC, 0, 0, NOPC)),
  146. /* BXJ cccc 0001 0010 xxxx xxxx xxxx 0010 xxxx */
  147. /* MSR cccc 0001 0x10 xxxx xxxx xxxx 0000 xxxx */
  148. /* MRS spsr cccc 0001 0100 xxxx xxxx xxxx 0000 xxxx */
  149. /* BKPT 1110 0001 0010 xxxx xxxx xxxx 0111 xxxx */
  150. /* SMC cccc 0001 0110 xxxx xxxx xxxx 0111 xxxx */
  151. /* And unallocated instructions... */
  152. DECODE_END
  153. };
  154. static const union decode_item arm_cccc_0001_0xx0____1xx0_table[] = {
  155. /* Halfword multiply and multiply-accumulate */
  156. /* SMLALxy cccc 0001 0100 xxxx xxxx xxxx 1xx0 xxxx */
  157. DECODE_EMULATEX (0x0ff00090, 0x01400080, PROBES_MUL1,
  158. REGS(NOPC, NOPC, NOPC, 0, NOPC)),
  159. /* SMULWy cccc 0001 0010 xxxx xxxx xxxx 1x10 xxxx */
  160. DECODE_OR (0x0ff000b0, 0x012000a0),
  161. /* SMULxy cccc 0001 0110 xxxx xxxx xxxx 1xx0 xxxx */
  162. DECODE_EMULATEX (0x0ff00090, 0x01600080, PROBES_MUL2,
  163. REGS(NOPC, 0, NOPC, 0, NOPC)),
  164. /* SMLAxy cccc 0001 0000 xxxx xxxx xxxx 1xx0 xxxx */
  165. DECODE_OR (0x0ff00090, 0x01000080),
  166. /* SMLAWy cccc 0001 0010 xxxx xxxx xxxx 1x00 xxxx */
  167. DECODE_EMULATEX (0x0ff000b0, 0x01200080, PROBES_MUL2,
  168. REGS(NOPC, NOPC, NOPC, 0, NOPC)),
  169. DECODE_END
  170. };
  171. static const union decode_item arm_cccc_0000_____1001_table[] = {
  172. /* Multiply and multiply-accumulate */
  173. /* MUL cccc 0000 0000 xxxx xxxx xxxx 1001 xxxx */
  174. /* MULS cccc 0000 0001 xxxx xxxx xxxx 1001 xxxx */
  175. DECODE_EMULATEX (0x0fe000f0, 0x00000090, PROBES_MUL2,
  176. REGS(NOPC, 0, NOPC, 0, NOPC)),
  177. /* MLA cccc 0000 0010 xxxx xxxx xxxx 1001 xxxx */
  178. /* MLAS cccc 0000 0011 xxxx xxxx xxxx 1001 xxxx */
  179. DECODE_OR (0x0fe000f0, 0x00200090),
  180. /* MLS cccc 0000 0110 xxxx xxxx xxxx 1001 xxxx */
  181. DECODE_EMULATEX (0x0ff000f0, 0x00600090, PROBES_MUL2,
  182. REGS(NOPC, NOPC, NOPC, 0, NOPC)),
  183. /* UMAAL cccc 0000 0100 xxxx xxxx xxxx 1001 xxxx */
  184. DECODE_OR (0x0ff000f0, 0x00400090),
  185. /* UMULL cccc 0000 1000 xxxx xxxx xxxx 1001 xxxx */
  186. /* UMULLS cccc 0000 1001 xxxx xxxx xxxx 1001 xxxx */
  187. /* UMLAL cccc 0000 1010 xxxx xxxx xxxx 1001 xxxx */
  188. /* UMLALS cccc 0000 1011 xxxx xxxx xxxx 1001 xxxx */
  189. /* SMULL cccc 0000 1100 xxxx xxxx xxxx 1001 xxxx */
  190. /* SMULLS cccc 0000 1101 xxxx xxxx xxxx 1001 xxxx */
  191. /* SMLAL cccc 0000 1110 xxxx xxxx xxxx 1001 xxxx */
  192. /* SMLALS cccc 0000 1111 xxxx xxxx xxxx 1001 xxxx */
  193. DECODE_EMULATEX (0x0f8000f0, 0x00800090, PROBES_MUL1,
  194. REGS(NOPC, NOPC, NOPC, 0, NOPC)),
  195. DECODE_END
  196. };
  197. static const union decode_item arm_cccc_0001_____1001_table[] = {
  198. /* Synchronization primitives */
  199. #if __LINUX_ARM_ARCH__ < 6
  200. /* Deprecated on ARMv6 and may be UNDEFINED on v7 */
  201. /* SMP/SWPB cccc 0001 0x00 xxxx xxxx xxxx 1001 xxxx */
  202. DECODE_EMULATEX (0x0fb000f0, 0x01000090, PROBES_SWP,
  203. REGS(NOPC, NOPC, 0, 0, NOPC)),
  204. #endif
  205. /* LDREX/STREX{,D,B,H} cccc 0001 1xxx xxxx xxxx xxxx 1001 xxxx */
  206. /* And unallocated instructions... */
  207. DECODE_END
  208. };
  209. static const union decode_item arm_cccc_000x_____1xx1_table[] = {
  210. /* Extra load/store instructions */
  211. /* STRHT cccc 0000 xx10 xxxx xxxx xxxx 1011 xxxx */
  212. /* ??? cccc 0000 xx10 xxxx xxxx xxxx 11x1 xxxx */
  213. /* LDRHT cccc 0000 xx11 xxxx xxxx xxxx 1011 xxxx */
  214. /* LDRSBT cccc 0000 xx11 xxxx xxxx xxxx 1101 xxxx */
  215. /* LDRSHT cccc 0000 xx11 xxxx xxxx xxxx 1111 xxxx */
  216. DECODE_REJECT (0x0f200090, 0x00200090),
  217. /* LDRD/STRD lr,pc,{... cccc 000x x0x0 xxxx 111x xxxx 1101 xxxx */
  218. DECODE_REJECT (0x0e10e0d0, 0x0000e0d0),
  219. /* LDRD (register) cccc 000x x0x0 xxxx xxxx xxxx 1101 xxxx */
  220. /* STRD (register) cccc 000x x0x0 xxxx xxxx xxxx 1111 xxxx */
  221. DECODE_EMULATEX (0x0e5000d0, 0x000000d0, PROBES_LDRSTRD,
  222. REGS(NOPCWB, NOPCX, 0, 0, NOPC)),
  223. /* LDRD (immediate) cccc 000x x1x0 xxxx xxxx xxxx 1101 xxxx */
  224. /* STRD (immediate) cccc 000x x1x0 xxxx xxxx xxxx 1111 xxxx */
  225. DECODE_EMULATEX (0x0e5000d0, 0x004000d0, PROBES_LDRSTRD,
  226. REGS(NOPCWB, NOPCX, 0, 0, 0)),
  227. /* STRH (register) cccc 000x x0x0 xxxx xxxx xxxx 1011 xxxx */
  228. DECODE_EMULATEX (0x0e5000f0, 0x000000b0, PROBES_STORE_EXTRA,
  229. REGS(NOPCWB, NOPC, 0, 0, NOPC)),
  230. /* LDRH (register) cccc 000x x0x1 xxxx xxxx xxxx 1011 xxxx */
  231. /* LDRSB (register) cccc 000x x0x1 xxxx xxxx xxxx 1101 xxxx */
  232. /* LDRSH (register) cccc 000x x0x1 xxxx xxxx xxxx 1111 xxxx */
  233. DECODE_EMULATEX (0x0e500090, 0x00100090, PROBES_LOAD_EXTRA,
  234. REGS(NOPCWB, NOPC, 0, 0, NOPC)),
  235. /* STRH (immediate) cccc 000x x1x0 xxxx xxxx xxxx 1011 xxxx */
  236. DECODE_EMULATEX (0x0e5000f0, 0x004000b0, PROBES_STORE_EXTRA,
  237. REGS(NOPCWB, NOPC, 0, 0, 0)),
  238. /* LDRH (immediate) cccc 000x x1x1 xxxx xxxx xxxx 1011 xxxx */
  239. /* LDRSB (immediate) cccc 000x x1x1 xxxx xxxx xxxx 1101 xxxx */
  240. /* LDRSH (immediate) cccc 000x x1x1 xxxx xxxx xxxx 1111 xxxx */
  241. DECODE_EMULATEX (0x0e500090, 0x00500090, PROBES_LOAD_EXTRA,
  242. REGS(NOPCWB, NOPC, 0, 0, 0)),
  243. DECODE_END
  244. };
  245. static const union decode_item arm_cccc_000x_table[] = {
  246. /* Data-processing (register) */
  247. /* <op>S PC, ... cccc 000x xxx1 xxxx 1111 xxxx xxxx xxxx */
  248. DECODE_REJECT (0x0e10f000, 0x0010f000),
  249. /* MOV IP, SP 1110 0001 1010 0000 1100 0000 0000 1101 */
  250. DECODE_SIMULATE (0xffffffff, 0xe1a0c00d, PROBES_MOV_IP_SP),
  251. /* TST (register) cccc 0001 0001 xxxx xxxx xxxx xxx0 xxxx */
  252. /* TEQ (register) cccc 0001 0011 xxxx xxxx xxxx xxx0 xxxx */
  253. /* CMP (register) cccc 0001 0101 xxxx xxxx xxxx xxx0 xxxx */
  254. /* CMN (register) cccc 0001 0111 xxxx xxxx xxxx xxx0 xxxx */
  255. DECODE_EMULATEX (0x0f900010, 0x01100000, PROBES_DATA_PROCESSING_REG,
  256. REGS(ANY, 0, 0, 0, ANY)),
  257. /* MOV (register) cccc 0001 101x xxxx xxxx xxxx xxx0 xxxx */
  258. /* MVN (register) cccc 0001 111x xxxx xxxx xxxx xxx0 xxxx */
  259. DECODE_EMULATEX (0x0fa00010, 0x01a00000, PROBES_DATA_PROCESSING_REG,
  260. REGS(0, ANY, 0, 0, ANY)),
  261. /* AND (register) cccc 0000 000x xxxx xxxx xxxx xxx0 xxxx */
  262. /* EOR (register) cccc 0000 001x xxxx xxxx xxxx xxx0 xxxx */
  263. /* SUB (register) cccc 0000 010x xxxx xxxx xxxx xxx0 xxxx */
  264. /* RSB (register) cccc 0000 011x xxxx xxxx xxxx xxx0 xxxx */
  265. /* ADD (register) cccc 0000 100x xxxx xxxx xxxx xxx0 xxxx */
  266. /* ADC (register) cccc 0000 101x xxxx xxxx xxxx xxx0 xxxx */
  267. /* SBC (register) cccc 0000 110x xxxx xxxx xxxx xxx0 xxxx */
  268. /* RSC (register) cccc 0000 111x xxxx xxxx xxxx xxx0 xxxx */
  269. /* ORR (register) cccc 0001 100x xxxx xxxx xxxx xxx0 xxxx */
  270. /* BIC (register) cccc 0001 110x xxxx xxxx xxxx xxx0 xxxx */
  271. DECODE_EMULATEX (0x0e000010, 0x00000000, PROBES_DATA_PROCESSING_REG,
  272. REGS(ANY, ANY, 0, 0, ANY)),
  273. /* TST (reg-shift reg) cccc 0001 0001 xxxx xxxx xxxx 0xx1 xxxx */
  274. /* TEQ (reg-shift reg) cccc 0001 0011 xxxx xxxx xxxx 0xx1 xxxx */
  275. /* CMP (reg-shift reg) cccc 0001 0101 xxxx xxxx xxxx 0xx1 xxxx */
  276. /* CMN (reg-shift reg) cccc 0001 0111 xxxx xxxx xxxx 0xx1 xxxx */
  277. DECODE_EMULATEX (0x0f900090, 0x01100010, PROBES_DATA_PROCESSING_REG,
  278. REGS(NOPC, 0, NOPC, 0, NOPC)),
  279. /* MOV (reg-shift reg) cccc 0001 101x xxxx xxxx xxxx 0xx1 xxxx */
  280. /* MVN (reg-shift reg) cccc 0001 111x xxxx xxxx xxxx 0xx1 xxxx */
  281. DECODE_EMULATEX (0x0fa00090, 0x01a00010, PROBES_DATA_PROCESSING_REG,
  282. REGS(0, NOPC, NOPC, 0, NOPC)),
  283. /* AND (reg-shift reg) cccc 0000 000x xxxx xxxx xxxx 0xx1 xxxx */
  284. /* EOR (reg-shift reg) cccc 0000 001x xxxx xxxx xxxx 0xx1 xxxx */
  285. /* SUB (reg-shift reg) cccc 0000 010x xxxx xxxx xxxx 0xx1 xxxx */
  286. /* RSB (reg-shift reg) cccc 0000 011x xxxx xxxx xxxx 0xx1 xxxx */
  287. /* ADD (reg-shift reg) cccc 0000 100x xxxx xxxx xxxx 0xx1 xxxx */
  288. /* ADC (reg-shift reg) cccc 0000 101x xxxx xxxx xxxx 0xx1 xxxx */
  289. /* SBC (reg-shift reg) cccc 0000 110x xxxx xxxx xxxx 0xx1 xxxx */
  290. /* RSC (reg-shift reg) cccc 0000 111x xxxx xxxx xxxx 0xx1 xxxx */
  291. /* ORR (reg-shift reg) cccc 0001 100x xxxx xxxx xxxx 0xx1 xxxx */
  292. /* BIC (reg-shift reg) cccc 0001 110x xxxx xxxx xxxx 0xx1 xxxx */
  293. DECODE_EMULATEX (0x0e000090, 0x00000010, PROBES_DATA_PROCESSING_REG,
  294. REGS(NOPC, NOPC, NOPC, 0, NOPC)),
  295. DECODE_END
  296. };
  297. static const union decode_item arm_cccc_001x_table[] = {
  298. /* Data-processing (immediate) */
  299. /* MOVW cccc 0011 0000 xxxx xxxx xxxx xxxx xxxx */
  300. /* MOVT cccc 0011 0100 xxxx xxxx xxxx xxxx xxxx */
  301. DECODE_EMULATEX (0x0fb00000, 0x03000000, PROBES_MOV_HALFWORD,
  302. REGS(0, NOPC, 0, 0, 0)),
  303. /* YIELD cccc 0011 0010 0000 xxxx xxxx 0000 0001 */
  304. DECODE_OR (0x0fff00ff, 0x03200001),
  305. /* SEV cccc 0011 0010 0000 xxxx xxxx 0000 0100 */
  306. DECODE_EMULATE (0x0fff00ff, 0x03200004, PROBES_SEV),
  307. /* NOP cccc 0011 0010 0000 xxxx xxxx 0000 0000 */
  308. /* WFE cccc 0011 0010 0000 xxxx xxxx 0000 0010 */
  309. /* WFI cccc 0011 0010 0000 xxxx xxxx 0000 0011 */
  310. DECODE_SIMULATE (0x0fff00fc, 0x03200000, PROBES_WFE),
  311. /* DBG cccc 0011 0010 0000 xxxx xxxx ffff xxxx */
  312. /* unallocated hints cccc 0011 0010 0000 xxxx xxxx xxxx xxxx */
  313. /* MSR (immediate) cccc 0011 0x10 xxxx xxxx xxxx xxxx xxxx */
  314. DECODE_REJECT (0x0fb00000, 0x03200000),
  315. /* <op>S PC, ... cccc 001x xxx1 xxxx 1111 xxxx xxxx xxxx */
  316. DECODE_REJECT (0x0e10f000, 0x0210f000),
  317. /* TST (immediate) cccc 0011 0001 xxxx xxxx xxxx xxxx xxxx */
  318. /* TEQ (immediate) cccc 0011 0011 xxxx xxxx xxxx xxxx xxxx */
  319. /* CMP (immediate) cccc 0011 0101 xxxx xxxx xxxx xxxx xxxx */
  320. /* CMN (immediate) cccc 0011 0111 xxxx xxxx xxxx xxxx xxxx */
  321. DECODE_EMULATEX (0x0f900000, 0x03100000, PROBES_DATA_PROCESSING_IMM,
  322. REGS(ANY, 0, 0, 0, 0)),
  323. /* MOV (immediate) cccc 0011 101x xxxx xxxx xxxx xxxx xxxx */
  324. /* MVN (immediate) cccc 0011 111x xxxx xxxx xxxx xxxx xxxx */
  325. DECODE_EMULATEX (0x0fa00000, 0x03a00000, PROBES_DATA_PROCESSING_IMM,
  326. REGS(0, ANY, 0, 0, 0)),
  327. /* AND (immediate) cccc 0010 000x xxxx xxxx xxxx xxxx xxxx */
  328. /* EOR (immediate) cccc 0010 001x xxxx xxxx xxxx xxxx xxxx */
  329. /* SUB (immediate) cccc 0010 010x xxxx xxxx xxxx xxxx xxxx */
  330. /* RSB (immediate) cccc 0010 011x xxxx xxxx xxxx xxxx xxxx */
  331. /* ADD (immediate) cccc 0010 100x xxxx xxxx xxxx xxxx xxxx */
  332. /* ADC (immediate) cccc 0010 101x xxxx xxxx xxxx xxxx xxxx */
  333. /* SBC (immediate) cccc 0010 110x xxxx xxxx xxxx xxxx xxxx */
  334. /* RSC (immediate) cccc 0010 111x xxxx xxxx xxxx xxxx xxxx */
  335. /* ORR (immediate) cccc 0011 100x xxxx xxxx xxxx xxxx xxxx */
  336. /* BIC (immediate) cccc 0011 110x xxxx xxxx xxxx xxxx xxxx */
  337. DECODE_EMULATEX (0x0e000000, 0x02000000, PROBES_DATA_PROCESSING_IMM,
  338. REGS(ANY, ANY, 0, 0, 0)),
  339. DECODE_END
  340. };
  341. static const union decode_item arm_cccc_0110_____xxx1_table[] = {
  342. /* Media instructions */
  343. /* SEL cccc 0110 1000 xxxx xxxx xxxx 1011 xxxx */
  344. DECODE_EMULATEX (0x0ff000f0, 0x068000b0, PROBES_SATURATE,
  345. REGS(NOPC, NOPC, 0, 0, NOPC)),
  346. /* SSAT cccc 0110 101x xxxx xxxx xxxx xx01 xxxx */
  347. /* USAT cccc 0110 111x xxxx xxxx xxxx xx01 xxxx */
  348. DECODE_OR(0x0fa00030, 0x06a00010),
  349. /* SSAT16 cccc 0110 1010 xxxx xxxx xxxx 0011 xxxx */
  350. /* USAT16 cccc 0110 1110 xxxx xxxx xxxx 0011 xxxx */
  351. DECODE_EMULATEX (0x0fb000f0, 0x06a00030, PROBES_SATURATE,
  352. REGS(0, NOPC, 0, 0, NOPC)),
  353. /* REV cccc 0110 1011 xxxx xxxx xxxx 0011 xxxx */
  354. /* REV16 cccc 0110 1011 xxxx xxxx xxxx 1011 xxxx */
  355. /* RBIT cccc 0110 1111 xxxx xxxx xxxx 0011 xxxx */
  356. /* REVSH cccc 0110 1111 xxxx xxxx xxxx 1011 xxxx */
  357. DECODE_EMULATEX (0x0fb00070, 0x06b00030, PROBES_REV,
  358. REGS(0, NOPC, 0, 0, NOPC)),
  359. /* ??? cccc 0110 0x00 xxxx xxxx xxxx xxx1 xxxx */
  360. DECODE_REJECT (0x0fb00010, 0x06000010),
  361. /* ??? cccc 0110 0xxx xxxx xxxx xxxx 1011 xxxx */
  362. DECODE_REJECT (0x0f8000f0, 0x060000b0),
  363. /* ??? cccc 0110 0xxx xxxx xxxx xxxx 1101 xxxx */
  364. DECODE_REJECT (0x0f8000f0, 0x060000d0),
  365. /* SADD16 cccc 0110 0001 xxxx xxxx xxxx 0001 xxxx */
  366. /* SADDSUBX cccc 0110 0001 xxxx xxxx xxxx 0011 xxxx */
  367. /* SSUBADDX cccc 0110 0001 xxxx xxxx xxxx 0101 xxxx */
  368. /* SSUB16 cccc 0110 0001 xxxx xxxx xxxx 0111 xxxx */
  369. /* SADD8 cccc 0110 0001 xxxx xxxx xxxx 1001 xxxx */
  370. /* SSUB8 cccc 0110 0001 xxxx xxxx xxxx 1111 xxxx */
  371. /* QADD16 cccc 0110 0010 xxxx xxxx xxxx 0001 xxxx */
  372. /* QADDSUBX cccc 0110 0010 xxxx xxxx xxxx 0011 xxxx */
  373. /* QSUBADDX cccc 0110 0010 xxxx xxxx xxxx 0101 xxxx */
  374. /* QSUB16 cccc 0110 0010 xxxx xxxx xxxx 0111 xxxx */
  375. /* QADD8 cccc 0110 0010 xxxx xxxx xxxx 1001 xxxx */
  376. /* QSUB8 cccc 0110 0010 xxxx xxxx xxxx 1111 xxxx */
  377. /* SHADD16 cccc 0110 0011 xxxx xxxx xxxx 0001 xxxx */
  378. /* SHADDSUBX cccc 0110 0011 xxxx xxxx xxxx 0011 xxxx */
  379. /* SHSUBADDX cccc 0110 0011 xxxx xxxx xxxx 0101 xxxx */
  380. /* SHSUB16 cccc 0110 0011 xxxx xxxx xxxx 0111 xxxx */
  381. /* SHADD8 cccc 0110 0011 xxxx xxxx xxxx 1001 xxxx */
  382. /* SHSUB8 cccc 0110 0011 xxxx xxxx xxxx 1111 xxxx */
  383. /* UADD16 cccc 0110 0101 xxxx xxxx xxxx 0001 xxxx */
  384. /* UADDSUBX cccc 0110 0101 xxxx xxxx xxxx 0011 xxxx */
  385. /* USUBADDX cccc 0110 0101 xxxx xxxx xxxx 0101 xxxx */
  386. /* USUB16 cccc 0110 0101 xxxx xxxx xxxx 0111 xxxx */
  387. /* UADD8 cccc 0110 0101 xxxx xxxx xxxx 1001 xxxx */
  388. /* USUB8 cccc 0110 0101 xxxx xxxx xxxx 1111 xxxx */
  389. /* UQADD16 cccc 0110 0110 xxxx xxxx xxxx 0001 xxxx */
  390. /* UQADDSUBX cccc 0110 0110 xxxx xxxx xxxx 0011 xxxx */
  391. /* UQSUBADDX cccc 0110 0110 xxxx xxxx xxxx 0101 xxxx */
  392. /* UQSUB16 cccc 0110 0110 xxxx xxxx xxxx 0111 xxxx */
  393. /* UQADD8 cccc 0110 0110 xxxx xxxx xxxx 1001 xxxx */
  394. /* UQSUB8 cccc 0110 0110 xxxx xxxx xxxx 1111 xxxx */
  395. /* UHADD16 cccc 0110 0111 xxxx xxxx xxxx 0001 xxxx */
  396. /* UHADDSUBX cccc 0110 0111 xxxx xxxx xxxx 0011 xxxx */
  397. /* UHSUBADDX cccc 0110 0111 xxxx xxxx xxxx 0101 xxxx */
  398. /* UHSUB16 cccc 0110 0111 xxxx xxxx xxxx 0111 xxxx */
  399. /* UHADD8 cccc 0110 0111 xxxx xxxx xxxx 1001 xxxx */
  400. /* UHSUB8 cccc 0110 0111 xxxx xxxx xxxx 1111 xxxx */
  401. DECODE_EMULATEX (0x0f800010, 0x06000010, PROBES_MMI,
  402. REGS(NOPC, NOPC, 0, 0, NOPC)),
  403. /* PKHBT cccc 0110 1000 xxxx xxxx xxxx x001 xxxx */
  404. /* PKHTB cccc 0110 1000 xxxx xxxx xxxx x101 xxxx */
  405. DECODE_EMULATEX (0x0ff00030, 0x06800010, PROBES_PACK,
  406. REGS(NOPC, NOPC, 0, 0, NOPC)),
  407. /* ??? cccc 0110 1001 xxxx xxxx xxxx 0111 xxxx */
  408. /* ??? cccc 0110 1101 xxxx xxxx xxxx 0111 xxxx */
  409. DECODE_REJECT (0x0fb000f0, 0x06900070),
  410. /* SXTB16 cccc 0110 1000 1111 xxxx xxxx 0111 xxxx */
  411. /* SXTB cccc 0110 1010 1111 xxxx xxxx 0111 xxxx */
  412. /* SXTH cccc 0110 1011 1111 xxxx xxxx 0111 xxxx */
  413. /* UXTB16 cccc 0110 1100 1111 xxxx xxxx 0111 xxxx */
  414. /* UXTB cccc 0110 1110 1111 xxxx xxxx 0111 xxxx */
  415. /* UXTH cccc 0110 1111 1111 xxxx xxxx 0111 xxxx */
  416. DECODE_EMULATEX (0x0f8f00f0, 0x068f0070, PROBES_EXTEND,
  417. REGS(0, NOPC, 0, 0, NOPC)),
  418. /* SXTAB16 cccc 0110 1000 xxxx xxxx xxxx 0111 xxxx */
  419. /* SXTAB cccc 0110 1010 xxxx xxxx xxxx 0111 xxxx */
  420. /* SXTAH cccc 0110 1011 xxxx xxxx xxxx 0111 xxxx */
  421. /* UXTAB16 cccc 0110 1100 xxxx xxxx xxxx 0111 xxxx */
  422. /* UXTAB cccc 0110 1110 xxxx xxxx xxxx 0111 xxxx */
  423. /* UXTAH cccc 0110 1111 xxxx xxxx xxxx 0111 xxxx */
  424. DECODE_EMULATEX (0x0f8000f0, 0x06800070, PROBES_EXTEND_ADD,
  425. REGS(NOPCX, NOPC, 0, 0, NOPC)),
  426. DECODE_END
  427. };
  428. static const union decode_item arm_cccc_0111_____xxx1_table[] = {
  429. /* Media instructions */
  430. /* UNDEFINED cccc 0111 1111 xxxx xxxx xxxx 1111 xxxx */
  431. DECODE_REJECT (0x0ff000f0, 0x07f000f0),
  432. /* SMLALD cccc 0111 0100 xxxx xxxx xxxx 00x1 xxxx */
  433. /* SMLSLD cccc 0111 0100 xxxx xxxx xxxx 01x1 xxxx */
  434. DECODE_EMULATEX (0x0ff00090, 0x07400010, PROBES_MUL_ADD_LONG,
  435. REGS(NOPC, NOPC, NOPC, 0, NOPC)),
  436. /* SMUAD cccc 0111 0000 xxxx 1111 xxxx 00x1 xxxx */
  437. /* SMUSD cccc 0111 0000 xxxx 1111 xxxx 01x1 xxxx */
  438. DECODE_OR (0x0ff0f090, 0x0700f010),
  439. /* SMMUL cccc 0111 0101 xxxx 1111 xxxx 00x1 xxxx */
  440. DECODE_OR (0x0ff0f0d0, 0x0750f010),
  441. /* USAD8 cccc 0111 1000 xxxx 1111 xxxx 0001 xxxx */
  442. DECODE_EMULATEX (0x0ff0f0f0, 0x0780f010, PROBES_MUL_ADD,
  443. REGS(NOPC, 0, NOPC, 0, NOPC)),
  444. /* SMLAD cccc 0111 0000 xxxx xxxx xxxx 00x1 xxxx */
  445. /* SMLSD cccc 0111 0000 xxxx xxxx xxxx 01x1 xxxx */
  446. DECODE_OR (0x0ff00090, 0x07000010),
  447. /* SMMLA cccc 0111 0101 xxxx xxxx xxxx 00x1 xxxx */
  448. DECODE_OR (0x0ff000d0, 0x07500010),
  449. /* USADA8 cccc 0111 1000 xxxx xxxx xxxx 0001 xxxx */
  450. DECODE_EMULATEX (0x0ff000f0, 0x07800010, PROBES_MUL_ADD,
  451. REGS(NOPC, NOPCX, NOPC, 0, NOPC)),
  452. /* SMMLS cccc 0111 0101 xxxx xxxx xxxx 11x1 xxxx */
  453. DECODE_EMULATEX (0x0ff000d0, 0x075000d0, PROBES_MUL_ADD,
  454. REGS(NOPC, NOPC, NOPC, 0, NOPC)),
  455. /* SBFX cccc 0111 101x xxxx xxxx xxxx x101 xxxx */
  456. /* UBFX cccc 0111 111x xxxx xxxx xxxx x101 xxxx */
  457. DECODE_EMULATEX (0x0fa00070, 0x07a00050, PROBES_BITFIELD,
  458. REGS(0, NOPC, 0, 0, NOPC)),
  459. /* BFC cccc 0111 110x xxxx xxxx xxxx x001 1111 */
  460. DECODE_EMULATEX (0x0fe0007f, 0x07c0001f, PROBES_BITFIELD,
  461. REGS(0, NOPC, 0, 0, 0)),
  462. /* BFI cccc 0111 110x xxxx xxxx xxxx x001 xxxx */
  463. DECODE_EMULATEX (0x0fe00070, 0x07c00010, PROBES_BITFIELD,
  464. REGS(0, NOPC, 0, 0, NOPCX)),
  465. DECODE_END
  466. };
  467. static const union decode_item arm_cccc_01xx_table[] = {
  468. /* Load/store word and unsigned byte */
  469. /* LDRB/STRB pc,[...] cccc 01xx x0xx xxxx xxxx xxxx xxxx xxxx */
  470. DECODE_REJECT (0x0c40f000, 0x0440f000),
  471. /* STRT cccc 01x0 x010 xxxx xxxx xxxx xxxx xxxx */
  472. /* LDRT cccc 01x0 x011 xxxx xxxx xxxx xxxx xxxx */
  473. /* STRBT cccc 01x0 x110 xxxx xxxx xxxx xxxx xxxx */
  474. /* LDRBT cccc 01x0 x111 xxxx xxxx xxxx xxxx xxxx */
  475. DECODE_REJECT (0x0d200000, 0x04200000),
  476. /* STR (immediate) cccc 010x x0x0 xxxx xxxx xxxx xxxx xxxx */
  477. /* STRB (immediate) cccc 010x x1x0 xxxx xxxx xxxx xxxx xxxx */
  478. DECODE_EMULATEX (0x0e100000, 0x04000000, PROBES_STORE,
  479. REGS(NOPCWB, ANY, 0, 0, 0)),
  480. /* LDR (immediate) cccc 010x x0x1 xxxx xxxx xxxx xxxx xxxx */
  481. /* LDRB (immediate) cccc 010x x1x1 xxxx xxxx xxxx xxxx xxxx */
  482. DECODE_EMULATEX (0x0e100000, 0x04100000, PROBES_LOAD,
  483. REGS(NOPCWB, ANY, 0, 0, 0)),
  484. /* STR (register) cccc 011x x0x0 xxxx xxxx xxxx xxxx xxxx */
  485. /* STRB (register) cccc 011x x1x0 xxxx xxxx xxxx xxxx xxxx */
  486. DECODE_EMULATEX (0x0e100000, 0x06000000, PROBES_STORE,
  487. REGS(NOPCWB, ANY, 0, 0, NOPC)),
  488. /* LDR (register) cccc 011x x0x1 xxxx xxxx xxxx xxxx xxxx */
  489. /* LDRB (register) cccc 011x x1x1 xxxx xxxx xxxx xxxx xxxx */
  490. DECODE_EMULATEX (0x0e100000, 0x06100000, PROBES_LOAD,
  491. REGS(NOPCWB, ANY, 0, 0, NOPC)),
  492. DECODE_END
  493. };
  494. static const union decode_item arm_cccc_100x_table[] = {
  495. /* Block data transfer instructions */
  496. /* LDM cccc 100x x0x1 xxxx xxxx xxxx xxxx xxxx */
  497. /* STM cccc 100x x0x0 xxxx xxxx xxxx xxxx xxxx */
  498. DECODE_CUSTOM (0x0e400000, 0x08000000, PROBES_LDMSTM),
  499. /* STM (user registers) cccc 100x x1x0 xxxx xxxx xxxx xxxx xxxx */
  500. /* LDM (user registers) cccc 100x x1x1 xxxx 0xxx xxxx xxxx xxxx */
  501. /* LDM (exception ret) cccc 100x x1x1 xxxx 1xxx xxxx xxxx xxxx */
  502. DECODE_END
  503. };
  504. const union decode_item probes_decode_arm_table[] = {
  505. /*
  506. * Unconditional instructions
  507. * 1111 xxxx xxxx xxxx xxxx xxxx xxxx xxxx
  508. */
  509. DECODE_TABLE (0xf0000000, 0xf0000000, arm_1111_table),
  510. /*
  511. * Miscellaneous instructions
  512. * cccc 0001 0xx0 xxxx xxxx xxxx 0xxx xxxx
  513. */
  514. DECODE_TABLE (0x0f900080, 0x01000000, arm_cccc_0001_0xx0____0xxx_table),
  515. /*
  516. * Halfword multiply and multiply-accumulate
  517. * cccc 0001 0xx0 xxxx xxxx xxxx 1xx0 xxxx
  518. */
  519. DECODE_TABLE (0x0f900090, 0x01000080, arm_cccc_0001_0xx0____1xx0_table),
  520. /*
  521. * Multiply and multiply-accumulate
  522. * cccc 0000 xxxx xxxx xxxx xxxx 1001 xxxx
  523. */
  524. DECODE_TABLE (0x0f0000f0, 0x00000090, arm_cccc_0000_____1001_table),
  525. /*
  526. * Synchronization primitives
  527. * cccc 0001 xxxx xxxx xxxx xxxx 1001 xxxx
  528. */
  529. DECODE_TABLE (0x0f0000f0, 0x01000090, arm_cccc_0001_____1001_table),
  530. /*
  531. * Extra load/store instructions
  532. * cccc 000x xxxx xxxx xxxx xxxx 1xx1 xxxx
  533. */
  534. DECODE_TABLE (0x0e000090, 0x00000090, arm_cccc_000x_____1xx1_table),
  535. /*
  536. * Data-processing (register)
  537. * cccc 000x xxxx xxxx xxxx xxxx xxx0 xxxx
  538. * Data-processing (register-shifted register)
  539. * cccc 000x xxxx xxxx xxxx xxxx 0xx1 xxxx
  540. */
  541. DECODE_TABLE (0x0e000000, 0x00000000, arm_cccc_000x_table),
  542. /*
  543. * Data-processing (immediate)
  544. * cccc 001x xxxx xxxx xxxx xxxx xxxx xxxx
  545. */
  546. DECODE_TABLE (0x0e000000, 0x02000000, arm_cccc_001x_table),
  547. /*
  548. * Media instructions
  549. * cccc 011x xxxx xxxx xxxx xxxx xxx1 xxxx
  550. */
  551. DECODE_TABLE (0x0f000010, 0x06000010, arm_cccc_0110_____xxx1_table),
  552. DECODE_TABLE (0x0f000010, 0x07000010, arm_cccc_0111_____xxx1_table),
  553. /*
  554. * Load/store word and unsigned byte
  555. * cccc 01xx xxxx xxxx xxxx xxxx xxxx xxxx
  556. */
  557. DECODE_TABLE (0x0c000000, 0x04000000, arm_cccc_01xx_table),
  558. /*
  559. * Block data transfer instructions
  560. * cccc 100x xxxx xxxx xxxx xxxx xxxx xxxx
  561. */
  562. DECODE_TABLE (0x0e000000, 0x08000000, arm_cccc_100x_table),
  563. /* B cccc 1010 xxxx xxxx xxxx xxxx xxxx xxxx */
  564. /* BL cccc 1011 xxxx xxxx xxxx xxxx xxxx xxxx */
  565. DECODE_SIMULATE (0x0e000000, 0x0a000000, PROBES_BRANCH),
  566. /*
  567. * Supervisor Call, and coprocessor instructions
  568. */
  569. /* MCRR cccc 1100 0100 xxxx xxxx xxxx xxxx xxxx */
  570. /* MRRC cccc 1100 0101 xxxx xxxx xxxx xxxx xxxx */
  571. /* LDC cccc 110x xxx1 xxxx xxxx xxxx xxxx xxxx */
  572. /* STC cccc 110x xxx0 xxxx xxxx xxxx xxxx xxxx */
  573. /* CDP cccc 1110 xxxx xxxx xxxx xxxx xxx0 xxxx */
  574. /* MCR cccc 1110 xxx0 xxxx xxxx xxxx xxx1 xxxx */
  575. /* MRC cccc 1110 xxx1 xxxx xxxx xxxx xxx1 xxxx */
  576. /* SVC cccc 1111 xxxx xxxx xxxx xxxx xxxx xxxx */
  577. DECODE_REJECT (0x0c000000, 0x0c000000),
  578. DECODE_END
  579. };
  580. #ifdef CONFIG_ARM_KPROBES_TEST_MODULE
  581. EXPORT_SYMBOL_GPL(probes_decode_arm_table);
  582. #endif
  583. static void __kprobes arm_singlestep(probes_opcode_t insn,
  584. struct arch_probes_insn *asi, struct pt_regs *regs)
  585. {
  586. regs->ARM_pc += 4;
  587. asi->insn_handler(insn, asi, regs);
  588. }
  589. /* Return:
  590. * INSN_REJECTED If instruction is one not allowed to kprobe,
  591. * INSN_GOOD If instruction is supported and uses instruction slot,
  592. * INSN_GOOD_NO_SLOT If instruction is supported but doesn't use its slot.
  593. *
  594. * For instructions we don't want to kprobe (INSN_REJECTED return result):
  595. * These are generally ones that modify the processor state making
  596. * them "hard" to simulate such as switches processor modes or
  597. * make accesses in alternate modes. Any of these could be simulated
  598. * if the work was put into it, but low return considering they
  599. * should also be very rare.
  600. */
  601. enum probes_insn __kprobes
  602. arm_probes_decode_insn(probes_opcode_t insn, struct arch_probes_insn *asi,
  603. bool emulate, const union decode_action *actions,
  604. const struct decode_checker *checkers[])
  605. {
  606. asi->insn_singlestep = arm_singlestep;
  607. asi->insn_check_cc = probes_condition_checks[insn>>28];
  608. return probes_decode_insn(insn, asi, probes_decode_arm_table, false,
  609. emulate, actions, checkers);
  610. }