probes-arm.c 28 KB

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