uprobes.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. /*
  2. * User-space Probes (UProbes) for x86
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright (C) IBM Corporation, 2008-2011
  19. * Authors:
  20. * Srikar Dronamraju
  21. * Jim Keniston
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/sched.h>
  25. #include <linux/ptrace.h>
  26. #include <linux/uprobes.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/kdebug.h>
  29. #include <asm/processor.h>
  30. #include <asm/insn.h>
  31. /* Post-execution fixups. */
  32. /* Adjust IP back to vicinity of actual insn */
  33. #define UPROBE_FIX_IP 0x01
  34. /* Adjust the return address of a call insn */
  35. #define UPROBE_FIX_CALL 0x02
  36. /* Instruction will modify TF, don't change it */
  37. #define UPROBE_FIX_SETF 0x04
  38. #define UPROBE_FIX_RIP_SI 0x08
  39. #define UPROBE_FIX_RIP_DI 0x10
  40. #define UPROBE_FIX_RIP_BX 0x20
  41. #define UPROBE_FIX_RIP_MASK \
  42. (UPROBE_FIX_RIP_SI | UPROBE_FIX_RIP_DI | UPROBE_FIX_RIP_BX)
  43. #define UPROBE_TRAP_NR UINT_MAX
  44. /* Adaptations for mhiramat x86 decoder v14. */
  45. #define OPCODE1(insn) ((insn)->opcode.bytes[0])
  46. #define OPCODE2(insn) ((insn)->opcode.bytes[1])
  47. #define OPCODE3(insn) ((insn)->opcode.bytes[2])
  48. #define MODRM_REG(insn) X86_MODRM_REG((insn)->modrm.value)
  49. #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
  50. (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
  51. (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
  52. (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
  53. (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
  54. << (row % 32))
  55. /*
  56. * Good-instruction tables for 32-bit apps. This is non-const and volatile
  57. * to keep gcc from statically optimizing it out, as variable_test_bit makes
  58. * some versions of gcc to think only *(unsigned long*) is used.
  59. */
  60. #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
  61. static volatile u32 good_insns_32[256 / 32] = {
  62. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  63. /* ---------------------------------------------- */
  64. W(0x00, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0) | /* 00 */
  65. W(0x10, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0) , /* 10 */
  66. W(0x20, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1) | /* 20 */
  67. W(0x30, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1) , /* 30 */
  68. W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
  69. W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 50 */
  70. W(0x60, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* 60 */
  71. W(0x70, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 70 */
  72. W(0x80, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */
  73. W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
  74. W(0xa0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* a0 */
  75. W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* b0 */
  76. W(0xc0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0) | /* c0 */
  77. W(0xd0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */
  78. W(0xe0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* e0 */
  79. W(0xf0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1) /* f0 */
  80. /* ---------------------------------------------- */
  81. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  82. };
  83. #else
  84. #define good_insns_32 NULL
  85. #endif
  86. /* Good-instruction tables for 64-bit apps */
  87. #if defined(CONFIG_X86_64)
  88. static volatile u32 good_insns_64[256 / 32] = {
  89. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  90. /* ---------------------------------------------- */
  91. W(0x00, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0) | /* 00 */
  92. W(0x10, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0) , /* 10 */
  93. W(0x20, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0) | /* 20 */
  94. W(0x30, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0) , /* 30 */
  95. W(0x40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 40 */
  96. W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 50 */
  97. W(0x60, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* 60 */
  98. W(0x70, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 70 */
  99. W(0x80, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */
  100. W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
  101. W(0xa0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* a0 */
  102. W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* b0 */
  103. W(0xc0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0) | /* c0 */
  104. W(0xd0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */
  105. W(0xe0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* e0 */
  106. W(0xf0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1) /* f0 */
  107. /* ---------------------------------------------- */
  108. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  109. };
  110. #else
  111. #define good_insns_64 NULL
  112. #endif
  113. /* Using this for both 64-bit and 32-bit apps */
  114. static volatile u32 good_2byte_insns[256 / 32] = {
  115. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  116. /* ---------------------------------------------- */
  117. W(0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1) | /* 00 */
  118. W(0x10, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1) , /* 10 */
  119. W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* 20 */
  120. W(0x30, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
  121. W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
  122. W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 50 */
  123. W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 60 */
  124. W(0x70, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
  125. W(0x80, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */
  126. W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
  127. W(0xa0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1) | /* a0 */
  128. W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1) , /* b0 */
  129. W(0xc0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
  130. W(0xd0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */
  131. W(0xe0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* e0 */
  132. W(0xf0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0) /* f0 */
  133. /* ---------------------------------------------- */
  134. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  135. };
  136. #undef W
  137. /*
  138. * opcodes we'll probably never support:
  139. *
  140. * 6c-6d, e4-e5, ec-ed - in
  141. * 6e-6f, e6-e7, ee-ef - out
  142. * cc, cd - int3, int
  143. * cf - iret
  144. * d6 - illegal instruction
  145. * f1 - int1/icebp
  146. * f4 - hlt
  147. * fa, fb - cli, sti
  148. * 0f - lar, lsl, syscall, clts, sysret, sysenter, sysexit, invd, wbinvd, ud2
  149. *
  150. * invalid opcodes in 64-bit mode:
  151. *
  152. * 06, 0e, 16, 1e, 27, 2f, 37, 3f, 60-62, 82, c4-c5, d4-d5
  153. * 63 - we support this opcode in x86_64 but not in i386.
  154. *
  155. * opcodes we may need to refine support for:
  156. *
  157. * 0f - 2-byte instructions: For many of these instructions, the validity
  158. * depends on the prefix and/or the reg field. On such instructions, we
  159. * just consider the opcode combination valid if it corresponds to any
  160. * valid instruction.
  161. *
  162. * 8f - Group 1 - only reg = 0 is OK
  163. * c6-c7 - Group 11 - only reg = 0 is OK
  164. * d9-df - fpu insns with some illegal encodings
  165. * f2, f3 - repnz, repz prefixes. These are also the first byte for
  166. * certain floating-point instructions, such as addsd.
  167. *
  168. * fe - Group 4 - only reg = 0 or 1 is OK
  169. * ff - Group 5 - only reg = 0-6 is OK
  170. *
  171. * others -- Do we need to support these?
  172. *
  173. * 0f - (floating-point?) prefetch instructions
  174. * 07, 17, 1f - pop es, pop ss, pop ds
  175. * 26, 2e, 36, 3e - es:, cs:, ss:, ds: segment prefixes --
  176. * but 64 and 65 (fs: and gs:) seem to be used, so we support them
  177. * 67 - addr16 prefix
  178. * ce - into
  179. * f0 - lock prefix
  180. */
  181. /*
  182. * TODO:
  183. * - Where necessary, examine the modrm byte and allow only valid instructions
  184. * in the different Groups and fpu instructions.
  185. */
  186. static bool is_prefix_bad(struct insn *insn)
  187. {
  188. int i;
  189. for (i = 0; i < insn->prefixes.nbytes; i++) {
  190. switch (insn->prefixes.bytes[i]) {
  191. case 0x26: /* INAT_PFX_ES */
  192. case 0x2E: /* INAT_PFX_CS */
  193. case 0x36: /* INAT_PFX_DS */
  194. case 0x3E: /* INAT_PFX_SS */
  195. case 0xF0: /* INAT_PFX_LOCK */
  196. return true;
  197. }
  198. }
  199. return false;
  200. }
  201. static int uprobe_init_insn(struct arch_uprobe *auprobe, struct insn *insn, bool x86_64)
  202. {
  203. u32 volatile *good_insns;
  204. insn_init(insn, auprobe->insn, x86_64);
  205. /* has the side-effect of processing the entire instruction */
  206. insn_get_length(insn);
  207. if (WARN_ON_ONCE(!insn_complete(insn)))
  208. return -ENOEXEC;
  209. if (is_prefix_bad(insn))
  210. return -ENOTSUPP;
  211. if (x86_64)
  212. good_insns = good_insns_64;
  213. else
  214. good_insns = good_insns_32;
  215. if (test_bit(OPCODE1(insn), (unsigned long *)good_insns))
  216. return 0;
  217. if (insn->opcode.nbytes == 2) {
  218. if (test_bit(OPCODE2(insn), (unsigned long *)good_2byte_insns))
  219. return 0;
  220. }
  221. return -ENOTSUPP;
  222. }
  223. #ifdef CONFIG_X86_64
  224. static inline bool is_64bit_mm(struct mm_struct *mm)
  225. {
  226. return !config_enabled(CONFIG_IA32_EMULATION) ||
  227. !(mm->context.ia32_compat == TIF_IA32);
  228. }
  229. /*
  230. * If arch_uprobe->insn doesn't use rip-relative addressing, return
  231. * immediately. Otherwise, rewrite the instruction so that it accesses
  232. * its memory operand indirectly through a scratch register. Set
  233. * defparam->fixups accordingly. (The contents of the scratch register
  234. * will be saved before we single-step the modified instruction,
  235. * and restored afterward).
  236. *
  237. * We do this because a rip-relative instruction can access only a
  238. * relatively small area (+/- 2 GB from the instruction), and the XOL
  239. * area typically lies beyond that area. At least for instructions
  240. * that store to memory, we can't execute the original instruction
  241. * and "fix things up" later, because the misdirected store could be
  242. * disastrous.
  243. *
  244. * Some useful facts about rip-relative instructions:
  245. *
  246. * - There's always a modrm byte with bit layout "00 reg 101".
  247. * - There's never a SIB byte.
  248. * - The displacement is always 4 bytes.
  249. * - REX.B=1 bit in REX prefix, which normally extends r/m field,
  250. * has no effect on rip-relative mode. It doesn't make modrm byte
  251. * with r/m=101 refer to register 1101 = R13.
  252. */
  253. static void riprel_analyze(struct arch_uprobe *auprobe, struct insn *insn)
  254. {
  255. u8 *cursor;
  256. u8 reg;
  257. u8 reg2;
  258. if (!insn_rip_relative(insn))
  259. return;
  260. /*
  261. * insn_rip_relative() would have decoded rex_prefix, vex_prefix, modrm.
  262. * Clear REX.b bit (extension of MODRM.rm field):
  263. * we want to encode low numbered reg, not r8+.
  264. */
  265. if (insn->rex_prefix.nbytes) {
  266. cursor = auprobe->insn + insn_offset_rex_prefix(insn);
  267. /* REX byte has 0100wrxb layout, clearing REX.b bit */
  268. *cursor &= 0xfe;
  269. }
  270. /*
  271. * Similar treatment for VEX3 prefix.
  272. * TODO: add XOP/EVEX treatment when insn decoder supports them
  273. */
  274. if (insn->vex_prefix.nbytes == 3) {
  275. /*
  276. * vex2: c5 rvvvvLpp (has no b bit)
  277. * vex3/xop: c4/8f rxbmmmmm wvvvvLpp
  278. * evex: 62 rxbR00mm wvvvv1pp zllBVaaa
  279. * (evex will need setting of both b and x since
  280. * in non-sib encoding evex.x is 4th bit of MODRM.rm)
  281. * Setting VEX3.b (setting because it has inverted meaning):
  282. */
  283. cursor = auprobe->insn + insn_offset_vex_prefix(insn) + 1;
  284. *cursor |= 0x20;
  285. }
  286. /*
  287. * Convert from rip-relative addressing to register-relative addressing
  288. * via a scratch register.
  289. *
  290. * This is tricky since there are insns with modrm byte
  291. * which also use registers not encoded in modrm byte:
  292. * [i]div/[i]mul: implicitly use dx:ax
  293. * shift ops: implicitly use cx
  294. * cmpxchg: implicitly uses ax
  295. * cmpxchg8/16b: implicitly uses dx:ax and bx:cx
  296. * Encoding: 0f c7/1 modrm
  297. * The code below thinks that reg=1 (cx), chooses si as scratch.
  298. * mulx: implicitly uses dx: mulx r/m,r1,r2 does r1:r2 = dx * r/m.
  299. * First appeared in Haswell (BMI2 insn). It is vex-encoded.
  300. * Example where none of bx,cx,dx can be used as scratch reg:
  301. * c4 e2 63 f6 0d disp32 mulx disp32(%rip),%ebx,%ecx
  302. * [v]pcmpistri: implicitly uses cx, xmm0
  303. * [v]pcmpistrm: implicitly uses xmm0
  304. * [v]pcmpestri: implicitly uses ax, dx, cx, xmm0
  305. * [v]pcmpestrm: implicitly uses ax, dx, xmm0
  306. * Evil SSE4.2 string comparison ops from hell.
  307. * maskmovq/[v]maskmovdqu: implicitly uses (ds:rdi) as destination.
  308. * Encoding: 0f f7 modrm, 66 0f f7 modrm, vex-encoded: c5 f9 f7 modrm.
  309. * Store op1, byte-masked by op2 msb's in each byte, to (ds:rdi).
  310. * AMD says it has no 3-operand form (vex.vvvv must be 1111)
  311. * and that it can have only register operands, not mem
  312. * (its modrm byte must have mode=11).
  313. * If these restrictions will ever be lifted,
  314. * we'll need code to prevent selection of di as scratch reg!
  315. *
  316. * Summary: I don't know any insns with modrm byte which
  317. * use SI register implicitly. DI register is used only
  318. * by one insn (maskmovq) and BX register is used
  319. * only by one too (cmpxchg8b).
  320. * BP is stack-segment based (may be a problem?).
  321. * AX, DX, CX are off-limits (many implicit users).
  322. * SP is unusable (it's stack pointer - think about "pop mem";
  323. * also, rsp+disp32 needs sib encoding -> insn length change).
  324. */
  325. reg = MODRM_REG(insn); /* Fetch modrm.reg */
  326. reg2 = 0xff; /* Fetch vex.vvvv */
  327. if (insn->vex_prefix.nbytes == 2)
  328. reg2 = insn->vex_prefix.bytes[1];
  329. else if (insn->vex_prefix.nbytes == 3)
  330. reg2 = insn->vex_prefix.bytes[2];
  331. /*
  332. * TODO: add XOP, EXEV vvvv reading.
  333. *
  334. * vex.vvvv field is in bits 6-3, bits are inverted.
  335. * But in 32-bit mode, high-order bit may be ignored.
  336. * Therefore, let's consider only 3 low-order bits.
  337. */
  338. reg2 = ((reg2 >> 3) & 0x7) ^ 0x7;
  339. /*
  340. * Register numbering is ax,cx,dx,bx, sp,bp,si,di, r8..r15.
  341. *
  342. * Choose scratch reg. Order is important: must not select bx
  343. * if we can use si (cmpxchg8b case!)
  344. */
  345. if (reg != 6 && reg2 != 6) {
  346. reg2 = 6;
  347. auprobe->defparam.fixups |= UPROBE_FIX_RIP_SI;
  348. } else if (reg != 7 && reg2 != 7) {
  349. reg2 = 7;
  350. auprobe->defparam.fixups |= UPROBE_FIX_RIP_DI;
  351. /* TODO (paranoia): force maskmovq to not use di */
  352. } else {
  353. reg2 = 3;
  354. auprobe->defparam.fixups |= UPROBE_FIX_RIP_BX;
  355. }
  356. /*
  357. * Point cursor at the modrm byte. The next 4 bytes are the
  358. * displacement. Beyond the displacement, for some instructions,
  359. * is the immediate operand.
  360. */
  361. cursor = auprobe->insn + insn_offset_modrm(insn);
  362. /*
  363. * Change modrm from "00 reg 101" to "10 reg reg2". Example:
  364. * 89 05 disp32 mov %eax,disp32(%rip) becomes
  365. * 89 86 disp32 mov %eax,disp32(%rsi)
  366. */
  367. *cursor = 0x80 | (reg << 3) | reg2;
  368. }
  369. static inline unsigned long *
  370. scratch_reg(struct arch_uprobe *auprobe, struct pt_regs *regs)
  371. {
  372. if (auprobe->defparam.fixups & UPROBE_FIX_RIP_SI)
  373. return &regs->si;
  374. if (auprobe->defparam.fixups & UPROBE_FIX_RIP_DI)
  375. return &regs->di;
  376. return &regs->bx;
  377. }
  378. /*
  379. * If we're emulating a rip-relative instruction, save the contents
  380. * of the scratch register and store the target address in that register.
  381. */
  382. static void riprel_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  383. {
  384. if (auprobe->defparam.fixups & UPROBE_FIX_RIP_MASK) {
  385. struct uprobe_task *utask = current->utask;
  386. unsigned long *sr = scratch_reg(auprobe, regs);
  387. utask->autask.saved_scratch_register = *sr;
  388. *sr = utask->vaddr + auprobe->defparam.ilen;
  389. }
  390. }
  391. static void riprel_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  392. {
  393. if (auprobe->defparam.fixups & UPROBE_FIX_RIP_MASK) {
  394. struct uprobe_task *utask = current->utask;
  395. unsigned long *sr = scratch_reg(auprobe, regs);
  396. *sr = utask->autask.saved_scratch_register;
  397. }
  398. }
  399. #else /* 32-bit: */
  400. static inline bool is_64bit_mm(struct mm_struct *mm)
  401. {
  402. return false;
  403. }
  404. /*
  405. * No RIP-relative addressing on 32-bit
  406. */
  407. static void riprel_analyze(struct arch_uprobe *auprobe, struct insn *insn)
  408. {
  409. }
  410. static void riprel_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  411. {
  412. }
  413. static void riprel_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  414. {
  415. }
  416. #endif /* CONFIG_X86_64 */
  417. struct uprobe_xol_ops {
  418. bool (*emulate)(struct arch_uprobe *, struct pt_regs *);
  419. int (*pre_xol)(struct arch_uprobe *, struct pt_regs *);
  420. int (*post_xol)(struct arch_uprobe *, struct pt_regs *);
  421. void (*abort)(struct arch_uprobe *, struct pt_regs *);
  422. };
  423. static inline int sizeof_long(void)
  424. {
  425. return is_ia32_task() ? 4 : 8;
  426. }
  427. static int default_pre_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  428. {
  429. riprel_pre_xol(auprobe, regs);
  430. return 0;
  431. }
  432. static int push_ret_address(struct pt_regs *regs, unsigned long ip)
  433. {
  434. unsigned long new_sp = regs->sp - sizeof_long();
  435. if (copy_to_user((void __user *)new_sp, &ip, sizeof_long()))
  436. return -EFAULT;
  437. regs->sp = new_sp;
  438. return 0;
  439. }
  440. /*
  441. * We have to fix things up as follows:
  442. *
  443. * Typically, the new ip is relative to the copied instruction. We need
  444. * to make it relative to the original instruction (FIX_IP). Exceptions
  445. * are return instructions and absolute or indirect jump or call instructions.
  446. *
  447. * If the single-stepped instruction was a call, the return address that
  448. * is atop the stack is the address following the copied instruction. We
  449. * need to make it the address following the original instruction (FIX_CALL).
  450. *
  451. * If the original instruction was a rip-relative instruction such as
  452. * "movl %edx,0xnnnn(%rip)", we have instead executed an equivalent
  453. * instruction using a scratch register -- e.g., "movl %edx,0xnnnn(%rsi)".
  454. * We need to restore the contents of the scratch register
  455. * (FIX_RIP_reg).
  456. */
  457. static int default_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  458. {
  459. struct uprobe_task *utask = current->utask;
  460. riprel_post_xol(auprobe, regs);
  461. if (auprobe->defparam.fixups & UPROBE_FIX_IP) {
  462. long correction = utask->vaddr - utask->xol_vaddr;
  463. regs->ip += correction;
  464. } else if (auprobe->defparam.fixups & UPROBE_FIX_CALL) {
  465. regs->sp += sizeof_long(); /* Pop incorrect return address */
  466. if (push_ret_address(regs, utask->vaddr + auprobe->defparam.ilen))
  467. return -ERESTART;
  468. }
  469. /* popf; tell the caller to not touch TF */
  470. if (auprobe->defparam.fixups & UPROBE_FIX_SETF)
  471. utask->autask.saved_tf = true;
  472. return 0;
  473. }
  474. static void default_abort_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  475. {
  476. riprel_post_xol(auprobe, regs);
  477. }
  478. static struct uprobe_xol_ops default_xol_ops = {
  479. .pre_xol = default_pre_xol_op,
  480. .post_xol = default_post_xol_op,
  481. .abort = default_abort_op,
  482. };
  483. static bool branch_is_call(struct arch_uprobe *auprobe)
  484. {
  485. return auprobe->branch.opc1 == 0xe8;
  486. }
  487. #define CASE_COND \
  488. COND(70, 71, XF(OF)) \
  489. COND(72, 73, XF(CF)) \
  490. COND(74, 75, XF(ZF)) \
  491. COND(78, 79, XF(SF)) \
  492. COND(7a, 7b, XF(PF)) \
  493. COND(76, 77, XF(CF) || XF(ZF)) \
  494. COND(7c, 7d, XF(SF) != XF(OF)) \
  495. COND(7e, 7f, XF(ZF) || XF(SF) != XF(OF))
  496. #define COND(op_y, op_n, expr) \
  497. case 0x ## op_y: DO((expr) != 0) \
  498. case 0x ## op_n: DO((expr) == 0)
  499. #define XF(xf) (!!(flags & X86_EFLAGS_ ## xf))
  500. static bool is_cond_jmp_opcode(u8 opcode)
  501. {
  502. switch (opcode) {
  503. #define DO(expr) \
  504. return true;
  505. CASE_COND
  506. #undef DO
  507. default:
  508. return false;
  509. }
  510. }
  511. static bool check_jmp_cond(struct arch_uprobe *auprobe, struct pt_regs *regs)
  512. {
  513. unsigned long flags = regs->flags;
  514. switch (auprobe->branch.opc1) {
  515. #define DO(expr) \
  516. return expr;
  517. CASE_COND
  518. #undef DO
  519. default: /* not a conditional jmp */
  520. return true;
  521. }
  522. }
  523. #undef XF
  524. #undef COND
  525. #undef CASE_COND
  526. static bool branch_emulate_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  527. {
  528. unsigned long new_ip = regs->ip += auprobe->branch.ilen;
  529. unsigned long offs = (long)auprobe->branch.offs;
  530. if (branch_is_call(auprobe)) {
  531. /*
  532. * If it fails we execute this (mangled, see the comment in
  533. * branch_clear_offset) insn out-of-line. In the likely case
  534. * this should trigger the trap, and the probed application
  535. * should die or restart the same insn after it handles the
  536. * signal, arch_uprobe_post_xol() won't be even called.
  537. *
  538. * But there is corner case, see the comment in ->post_xol().
  539. */
  540. if (push_ret_address(regs, new_ip))
  541. return false;
  542. } else if (!check_jmp_cond(auprobe, regs)) {
  543. offs = 0;
  544. }
  545. regs->ip = new_ip + offs;
  546. return true;
  547. }
  548. static int branch_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  549. {
  550. BUG_ON(!branch_is_call(auprobe));
  551. /*
  552. * We can only get here if branch_emulate_op() failed to push the ret
  553. * address _and_ another thread expanded our stack before the (mangled)
  554. * "call" insn was executed out-of-line. Just restore ->sp and restart.
  555. * We could also restore ->ip and try to call branch_emulate_op() again.
  556. */
  557. regs->sp += sizeof_long();
  558. return -ERESTART;
  559. }
  560. static void branch_clear_offset(struct arch_uprobe *auprobe, struct insn *insn)
  561. {
  562. /*
  563. * Turn this insn into "call 1f; 1:", this is what we will execute
  564. * out-of-line if ->emulate() fails. We only need this to generate
  565. * a trap, so that the probed task receives the correct signal with
  566. * the properly filled siginfo.
  567. *
  568. * But see the comment in ->post_xol(), in the unlikely case it can
  569. * succeed. So we need to ensure that the new ->ip can not fall into
  570. * the non-canonical area and trigger #GP.
  571. *
  572. * We could turn it into (say) "pushf", but then we would need to
  573. * divorce ->insn[] and ->ixol[]. We need to preserve the 1st byte
  574. * of ->insn[] for set_orig_insn().
  575. */
  576. memset(auprobe->insn + insn_offset_immediate(insn),
  577. 0, insn->immediate.nbytes);
  578. }
  579. static struct uprobe_xol_ops branch_xol_ops = {
  580. .emulate = branch_emulate_op,
  581. .post_xol = branch_post_xol_op,
  582. };
  583. /* Returns -ENOSYS if branch_xol_ops doesn't handle this insn */
  584. static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
  585. {
  586. u8 opc1 = OPCODE1(insn);
  587. int i;
  588. switch (opc1) {
  589. case 0xeb: /* jmp 8 */
  590. case 0xe9: /* jmp 32 */
  591. case 0x90: /* prefix* + nop; same as jmp with .offs = 0 */
  592. break;
  593. case 0xe8: /* call relative */
  594. branch_clear_offset(auprobe, insn);
  595. break;
  596. case 0x0f:
  597. if (insn->opcode.nbytes != 2)
  598. return -ENOSYS;
  599. /*
  600. * If it is a "near" conditional jmp, OPCODE2() - 0x10 matches
  601. * OPCODE1() of the "short" jmp which checks the same condition.
  602. */
  603. opc1 = OPCODE2(insn) - 0x10;
  604. default:
  605. if (!is_cond_jmp_opcode(opc1))
  606. return -ENOSYS;
  607. }
  608. /*
  609. * 16-bit overrides such as CALLW (66 e8 nn nn) are not supported.
  610. * Intel and AMD behavior differ in 64-bit mode: Intel ignores 66 prefix.
  611. * No one uses these insns, reject any branch insns with such prefix.
  612. */
  613. for (i = 0; i < insn->prefixes.nbytes; i++) {
  614. if (insn->prefixes.bytes[i] == 0x66)
  615. return -ENOTSUPP;
  616. }
  617. auprobe->branch.opc1 = opc1;
  618. auprobe->branch.ilen = insn->length;
  619. auprobe->branch.offs = insn->immediate.value;
  620. auprobe->ops = &branch_xol_ops;
  621. return 0;
  622. }
  623. /**
  624. * arch_uprobe_analyze_insn - instruction analysis including validity and fixups.
  625. * @mm: the probed address space.
  626. * @arch_uprobe: the probepoint information.
  627. * @addr: virtual address at which to install the probepoint
  628. * Return 0 on success or a -ve number on error.
  629. */
  630. int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long addr)
  631. {
  632. struct insn insn;
  633. u8 fix_ip_or_call = UPROBE_FIX_IP;
  634. int ret;
  635. ret = uprobe_init_insn(auprobe, &insn, is_64bit_mm(mm));
  636. if (ret)
  637. return ret;
  638. ret = branch_setup_xol_ops(auprobe, &insn);
  639. if (ret != -ENOSYS)
  640. return ret;
  641. /*
  642. * Figure out which fixups default_post_xol_op() will need to perform,
  643. * and annotate defparam->fixups accordingly.
  644. */
  645. switch (OPCODE1(&insn)) {
  646. case 0x9d: /* popf */
  647. auprobe->defparam.fixups |= UPROBE_FIX_SETF;
  648. break;
  649. case 0xc3: /* ret or lret -- ip is correct */
  650. case 0xcb:
  651. case 0xc2:
  652. case 0xca:
  653. case 0xea: /* jmp absolute -- ip is correct */
  654. fix_ip_or_call = 0;
  655. break;
  656. case 0x9a: /* call absolute - Fix return addr, not ip */
  657. fix_ip_or_call = UPROBE_FIX_CALL;
  658. break;
  659. case 0xff:
  660. switch (MODRM_REG(&insn)) {
  661. case 2: case 3: /* call or lcall, indirect */
  662. fix_ip_or_call = UPROBE_FIX_CALL;
  663. break;
  664. case 4: case 5: /* jmp or ljmp, indirect */
  665. fix_ip_or_call = 0;
  666. break;
  667. }
  668. /* fall through */
  669. default:
  670. riprel_analyze(auprobe, &insn);
  671. }
  672. auprobe->defparam.ilen = insn.length;
  673. auprobe->defparam.fixups |= fix_ip_or_call;
  674. auprobe->ops = &default_xol_ops;
  675. return 0;
  676. }
  677. /*
  678. * arch_uprobe_pre_xol - prepare to execute out of line.
  679. * @auprobe: the probepoint information.
  680. * @regs: reflects the saved user state of current task.
  681. */
  682. int arch_uprobe_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  683. {
  684. struct uprobe_task *utask = current->utask;
  685. if (auprobe->ops->pre_xol) {
  686. int err = auprobe->ops->pre_xol(auprobe, regs);
  687. if (err)
  688. return err;
  689. }
  690. regs->ip = utask->xol_vaddr;
  691. utask->autask.saved_trap_nr = current->thread.trap_nr;
  692. current->thread.trap_nr = UPROBE_TRAP_NR;
  693. utask->autask.saved_tf = !!(regs->flags & X86_EFLAGS_TF);
  694. regs->flags |= X86_EFLAGS_TF;
  695. if (test_tsk_thread_flag(current, TIF_BLOCKSTEP))
  696. set_task_blockstep(current, false);
  697. return 0;
  698. }
  699. /*
  700. * If xol insn itself traps and generates a signal(Say,
  701. * SIGILL/SIGSEGV/etc), then detect the case where a singlestepped
  702. * instruction jumps back to its own address. It is assumed that anything
  703. * like do_page_fault/do_trap/etc sets thread.trap_nr != -1.
  704. *
  705. * arch_uprobe_pre_xol/arch_uprobe_post_xol save/restore thread.trap_nr,
  706. * arch_uprobe_xol_was_trapped() simply checks that ->trap_nr is not equal to
  707. * UPROBE_TRAP_NR == -1 set by arch_uprobe_pre_xol().
  708. */
  709. bool arch_uprobe_xol_was_trapped(struct task_struct *t)
  710. {
  711. if (t->thread.trap_nr != UPROBE_TRAP_NR)
  712. return true;
  713. return false;
  714. }
  715. /*
  716. * Called after single-stepping. To avoid the SMP problems that can
  717. * occur when we temporarily put back the original opcode to
  718. * single-step, we single-stepped a copy of the instruction.
  719. *
  720. * This function prepares to resume execution after the single-step.
  721. */
  722. int arch_uprobe_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  723. {
  724. struct uprobe_task *utask = current->utask;
  725. bool send_sigtrap = utask->autask.saved_tf;
  726. int err = 0;
  727. WARN_ON_ONCE(current->thread.trap_nr != UPROBE_TRAP_NR);
  728. current->thread.trap_nr = utask->autask.saved_trap_nr;
  729. if (auprobe->ops->post_xol) {
  730. err = auprobe->ops->post_xol(auprobe, regs);
  731. if (err) {
  732. /*
  733. * Restore ->ip for restart or post mortem analysis.
  734. * ->post_xol() must not return -ERESTART unless this
  735. * is really possible.
  736. */
  737. regs->ip = utask->vaddr;
  738. if (err == -ERESTART)
  739. err = 0;
  740. send_sigtrap = false;
  741. }
  742. }
  743. /*
  744. * arch_uprobe_pre_xol() doesn't save the state of TIF_BLOCKSTEP
  745. * so we can get an extra SIGTRAP if we do not clear TF. We need
  746. * to examine the opcode to make it right.
  747. */
  748. if (send_sigtrap)
  749. send_sig(SIGTRAP, current, 0);
  750. if (!utask->autask.saved_tf)
  751. regs->flags &= ~X86_EFLAGS_TF;
  752. return err;
  753. }
  754. /* callback routine for handling exceptions. */
  755. int arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val, void *data)
  756. {
  757. struct die_args *args = data;
  758. struct pt_regs *regs = args->regs;
  759. int ret = NOTIFY_DONE;
  760. /* We are only interested in userspace traps */
  761. if (regs && !user_mode_vm(regs))
  762. return NOTIFY_DONE;
  763. switch (val) {
  764. case DIE_INT3:
  765. if (uprobe_pre_sstep_notifier(regs))
  766. ret = NOTIFY_STOP;
  767. break;
  768. case DIE_DEBUG:
  769. if (uprobe_post_sstep_notifier(regs))
  770. ret = NOTIFY_STOP;
  771. default:
  772. break;
  773. }
  774. return ret;
  775. }
  776. /*
  777. * This function gets called when XOL instruction either gets trapped or
  778. * the thread has a fatal signal. Reset the instruction pointer to its
  779. * probed address for the potential restart or for post mortem analysis.
  780. */
  781. void arch_uprobe_abort_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  782. {
  783. struct uprobe_task *utask = current->utask;
  784. if (auprobe->ops->abort)
  785. auprobe->ops->abort(auprobe, regs);
  786. current->thread.trap_nr = utask->autask.saved_trap_nr;
  787. regs->ip = utask->vaddr;
  788. /* clear TF if it was set by us in arch_uprobe_pre_xol() */
  789. if (!utask->autask.saved_tf)
  790. regs->flags &= ~X86_EFLAGS_TF;
  791. }
  792. static bool __skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
  793. {
  794. if (auprobe->ops->emulate)
  795. return auprobe->ops->emulate(auprobe, regs);
  796. return false;
  797. }
  798. bool arch_uprobe_skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
  799. {
  800. bool ret = __skip_sstep(auprobe, regs);
  801. if (ret && (regs->flags & X86_EFLAGS_TF))
  802. send_sig(SIGTRAP, current, 0);
  803. return ret;
  804. }
  805. unsigned long
  806. arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs)
  807. {
  808. int rasize = sizeof_long(), nleft;
  809. unsigned long orig_ret_vaddr = 0; /* clear high bits for 32-bit apps */
  810. if (copy_from_user(&orig_ret_vaddr, (void __user *)regs->sp, rasize))
  811. return -1;
  812. /* check whether address has been already hijacked */
  813. if (orig_ret_vaddr == trampoline_vaddr)
  814. return orig_ret_vaddr;
  815. nleft = copy_to_user((void __user *)regs->sp, &trampoline_vaddr, rasize);
  816. if (likely(!nleft))
  817. return orig_ret_vaddr;
  818. if (nleft != rasize) {
  819. pr_err("uprobe: return address clobbered: pid=%d, %%sp=%#lx, "
  820. "%%ip=%#lx\n", current->pid, regs->sp, regs->ip);
  821. force_sig_info(SIGSEGV, SEND_SIG_FORCED, current);
  822. }
  823. return -1;
  824. }