uprobes.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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 0x1
  34. /* Adjust the return address of a call insn */
  35. #define UPROBE_FIX_CALL 0x2
  36. /* Instruction will modify TF, don't change it */
  37. #define UPROBE_FIX_SETF 0x4
  38. #define UPROBE_FIX_RIP_AX 0x8000
  39. #define UPROBE_FIX_RIP_CX 0x4000
  40. #define UPROBE_TRAP_NR UINT_MAX
  41. /* Adaptations for mhiramat x86 decoder v14. */
  42. #define OPCODE1(insn) ((insn)->opcode.bytes[0])
  43. #define OPCODE2(insn) ((insn)->opcode.bytes[1])
  44. #define OPCODE3(insn) ((insn)->opcode.bytes[2])
  45. #define MODRM_REG(insn) X86_MODRM_REG((insn)->modrm.value)
  46. #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
  47. (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
  48. (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
  49. (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
  50. (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
  51. << (row % 32))
  52. /*
  53. * Good-instruction tables for 32-bit apps. This is non-const and volatile
  54. * to keep gcc from statically optimizing it out, as variable_test_bit makes
  55. * some versions of gcc to think only *(unsigned long*) is used.
  56. */
  57. #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
  58. static volatile u32 good_insns_32[256 / 32] = {
  59. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  60. /* ---------------------------------------------- */
  61. W(0x00, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0) | /* 00 */
  62. W(0x10, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0) , /* 10 */
  63. W(0x20, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1) | /* 20 */
  64. W(0x30, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1) , /* 30 */
  65. W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
  66. W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 50 */
  67. W(0x60, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* 60 */
  68. W(0x70, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 70 */
  69. W(0x80, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */
  70. W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
  71. W(0xa0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* a0 */
  72. W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* b0 */
  73. W(0xc0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0) | /* c0 */
  74. W(0xd0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */
  75. W(0xe0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* e0 */
  76. W(0xf0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1) /* f0 */
  77. /* ---------------------------------------------- */
  78. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  79. };
  80. #else
  81. #define good_insns_32 NULL
  82. #endif
  83. /* Good-instruction tables for 64-bit apps */
  84. #if defined(CONFIG_X86_64)
  85. static volatile u32 good_insns_64[256 / 32] = {
  86. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  87. /* ---------------------------------------------- */
  88. W(0x00, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0) | /* 00 */
  89. W(0x10, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0) , /* 10 */
  90. W(0x20, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0) | /* 20 */
  91. W(0x30, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0) , /* 30 */
  92. W(0x40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 40 */
  93. W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 50 */
  94. W(0x60, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* 60 */
  95. W(0x70, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 70 */
  96. W(0x80, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */
  97. W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
  98. W(0xa0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* a0 */
  99. W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* b0 */
  100. W(0xc0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0) | /* c0 */
  101. W(0xd0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */
  102. W(0xe0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* e0 */
  103. W(0xf0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1) /* f0 */
  104. /* ---------------------------------------------- */
  105. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  106. };
  107. #else
  108. #define good_insns_64 NULL
  109. #endif
  110. /* Using this for both 64-bit and 32-bit apps */
  111. static volatile u32 good_2byte_insns[256 / 32] = {
  112. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  113. /* ---------------------------------------------- */
  114. W(0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1) | /* 00 */
  115. W(0x10, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1) , /* 10 */
  116. W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* 20 */
  117. W(0x30, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
  118. W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
  119. W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 50 */
  120. W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 60 */
  121. W(0x70, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
  122. W(0x80, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */
  123. W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
  124. W(0xa0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1) | /* a0 */
  125. W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1) , /* b0 */
  126. W(0xc0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
  127. W(0xd0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */
  128. W(0xe0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* e0 */
  129. W(0xf0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0) /* f0 */
  130. /* ---------------------------------------------- */
  131. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  132. };
  133. #undef W
  134. /*
  135. * opcodes we'll probably never support:
  136. *
  137. * 6c-6d, e4-e5, ec-ed - in
  138. * 6e-6f, e6-e7, ee-ef - out
  139. * cc, cd - int3, int
  140. * cf - iret
  141. * d6 - illegal instruction
  142. * f1 - int1/icebp
  143. * f4 - hlt
  144. * fa, fb - cli, sti
  145. * 0f - lar, lsl, syscall, clts, sysret, sysenter, sysexit, invd, wbinvd, ud2
  146. *
  147. * invalid opcodes in 64-bit mode:
  148. *
  149. * 06, 0e, 16, 1e, 27, 2f, 37, 3f, 60-62, 82, c4-c5, d4-d5
  150. * 63 - we support this opcode in x86_64 but not in i386.
  151. *
  152. * opcodes we may need to refine support for:
  153. *
  154. * 0f - 2-byte instructions: For many of these instructions, the validity
  155. * depends on the prefix and/or the reg field. On such instructions, we
  156. * just consider the opcode combination valid if it corresponds to any
  157. * valid instruction.
  158. *
  159. * 8f - Group 1 - only reg = 0 is OK
  160. * c6-c7 - Group 11 - only reg = 0 is OK
  161. * d9-df - fpu insns with some illegal encodings
  162. * f2, f3 - repnz, repz prefixes. These are also the first byte for
  163. * certain floating-point instructions, such as addsd.
  164. *
  165. * fe - Group 4 - only reg = 0 or 1 is OK
  166. * ff - Group 5 - only reg = 0-6 is OK
  167. *
  168. * others -- Do we need to support these?
  169. *
  170. * 0f - (floating-point?) prefetch instructions
  171. * 07, 17, 1f - pop es, pop ss, pop ds
  172. * 26, 2e, 36, 3e - es:, cs:, ss:, ds: segment prefixes --
  173. * but 64 and 65 (fs: and gs:) seem to be used, so we support them
  174. * 67 - addr16 prefix
  175. * ce - into
  176. * f0 - lock prefix
  177. */
  178. /*
  179. * TODO:
  180. * - Where necessary, examine the modrm byte and allow only valid instructions
  181. * in the different Groups and fpu instructions.
  182. */
  183. static bool is_prefix_bad(struct insn *insn)
  184. {
  185. int i;
  186. for (i = 0; i < insn->prefixes.nbytes; i++) {
  187. switch (insn->prefixes.bytes[i]) {
  188. case 0x26: /* INAT_PFX_ES */
  189. case 0x2E: /* INAT_PFX_CS */
  190. case 0x36: /* INAT_PFX_DS */
  191. case 0x3E: /* INAT_PFX_SS */
  192. case 0xF0: /* INAT_PFX_LOCK */
  193. return true;
  194. }
  195. }
  196. return false;
  197. }
  198. static int uprobe_init_insn(struct arch_uprobe *auprobe, struct insn *insn, bool x86_64)
  199. {
  200. u32 volatile *good_insns;
  201. insn_init(insn, auprobe->insn, x86_64);
  202. /* has the side-effect of processing the entire instruction */
  203. insn_get_length(insn);
  204. if (WARN_ON_ONCE(!insn_complete(insn)))
  205. return -ENOEXEC;
  206. if (is_prefix_bad(insn))
  207. return -ENOTSUPP;
  208. if (x86_64)
  209. good_insns = good_insns_64;
  210. else
  211. good_insns = good_insns_32;
  212. if (test_bit(OPCODE1(insn), (unsigned long *)good_insns))
  213. return 0;
  214. if (insn->opcode.nbytes == 2) {
  215. if (test_bit(OPCODE2(insn), (unsigned long *)good_2byte_insns))
  216. return 0;
  217. }
  218. return -ENOTSUPP;
  219. }
  220. #ifdef CONFIG_X86_64
  221. static inline bool is_64bit_mm(struct mm_struct *mm)
  222. {
  223. return !config_enabled(CONFIG_IA32_EMULATION) ||
  224. !mm->context.ia32_compat;
  225. }
  226. /*
  227. * If arch_uprobe->insn doesn't use rip-relative addressing, return
  228. * immediately. Otherwise, rewrite the instruction so that it accesses
  229. * its memory operand indirectly through a scratch register. Set
  230. * arch_uprobe->fixups and arch_uprobe->rip_rela_target_address
  231. * accordingly. (The contents of the scratch register will be saved
  232. * before we single-step the modified instruction, and restored
  233. * afterward.)
  234. *
  235. * We do this because a rip-relative instruction can access only a
  236. * relatively small area (+/- 2 GB from the instruction), and the XOL
  237. * area typically lies beyond that area. At least for instructions
  238. * that store to memory, we can't execute the original instruction
  239. * and "fix things up" later, because the misdirected store could be
  240. * disastrous.
  241. *
  242. * Some useful facts about rip-relative instructions:
  243. *
  244. * - There's always a modrm byte.
  245. * - There's never a SIB byte.
  246. * - The displacement is always 4 bytes.
  247. */
  248. static void
  249. handle_riprel_insn(struct arch_uprobe *auprobe, struct insn *insn)
  250. {
  251. u8 *cursor;
  252. u8 reg;
  253. if (!insn_rip_relative(insn))
  254. return;
  255. /*
  256. * insn_rip_relative() would have decoded rex_prefix, modrm.
  257. * Clear REX.b bit (extension of MODRM.rm field):
  258. * we want to encode rax/rcx, not r8/r9.
  259. */
  260. if (insn->rex_prefix.nbytes) {
  261. cursor = auprobe->insn + insn_offset_rex_prefix(insn);
  262. *cursor &= 0xfe; /* Clearing REX.B bit */
  263. }
  264. /*
  265. * Point cursor at the modrm byte. The next 4 bytes are the
  266. * displacement. Beyond the displacement, for some instructions,
  267. * is the immediate operand.
  268. */
  269. cursor = auprobe->insn + insn_offset_modrm(insn);
  270. /*
  271. * Convert from rip-relative addressing to indirect addressing
  272. * via a scratch register. Change the r/m field from 0x5 (%rip)
  273. * to 0x0 (%rax) or 0x1 (%rcx), and squeeze out the offset field.
  274. */
  275. reg = MODRM_REG(insn);
  276. if (reg == 0) {
  277. /*
  278. * The register operand (if any) is either the A register
  279. * (%rax, %eax, etc.) or (if the 0x4 bit is set in the
  280. * REX prefix) %r8. In any case, we know the C register
  281. * is NOT the register operand, so we use %rcx (register
  282. * #1) for the scratch register.
  283. */
  284. auprobe->fixups = UPROBE_FIX_RIP_CX;
  285. /* Change modrm from 00 000 101 to 00 000 001. */
  286. *cursor = 0x1;
  287. } else {
  288. /* Use %rax (register #0) for the scratch register. */
  289. auprobe->fixups = UPROBE_FIX_RIP_AX;
  290. /* Change modrm from 00 xxx 101 to 00 xxx 000 */
  291. *cursor = (reg << 3);
  292. }
  293. /* Target address = address of next instruction + (signed) offset */
  294. auprobe->rip_rela_target_address = (long)insn->length + insn->displacement.value;
  295. /* Displacement field is gone; slide immediate field (if any) over. */
  296. if (insn->immediate.nbytes) {
  297. cursor++;
  298. memmove(cursor, cursor + insn->displacement.nbytes, insn->immediate.nbytes);
  299. }
  300. }
  301. /*
  302. * If we're emulating a rip-relative instruction, save the contents
  303. * of the scratch register and store the target address in that register.
  304. */
  305. static void
  306. pre_xol_rip_insn(struct arch_uprobe *auprobe, struct pt_regs *regs,
  307. struct arch_uprobe_task *autask)
  308. {
  309. if (auprobe->fixups & UPROBE_FIX_RIP_AX) {
  310. autask->saved_scratch_register = regs->ax;
  311. regs->ax = current->utask->vaddr;
  312. regs->ax += auprobe->rip_rela_target_address;
  313. } else if (auprobe->fixups & UPROBE_FIX_RIP_CX) {
  314. autask->saved_scratch_register = regs->cx;
  315. regs->cx = current->utask->vaddr;
  316. regs->cx += auprobe->rip_rela_target_address;
  317. }
  318. }
  319. static void
  320. handle_riprel_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs, long *correction)
  321. {
  322. if (auprobe->fixups & (UPROBE_FIX_RIP_AX | UPROBE_FIX_RIP_CX)) {
  323. struct arch_uprobe_task *autask;
  324. autask = &current->utask->autask;
  325. if (auprobe->fixups & UPROBE_FIX_RIP_AX)
  326. regs->ax = autask->saved_scratch_register;
  327. else
  328. regs->cx = autask->saved_scratch_register;
  329. /*
  330. * The original instruction includes a displacement, and so
  331. * is 4 bytes longer than what we've just single-stepped.
  332. * Caller may need to apply other fixups to handle stuff
  333. * like "jmpq *...(%rip)" and "callq *...(%rip)".
  334. */
  335. if (correction)
  336. *correction += 4;
  337. }
  338. }
  339. #else /* 32-bit: */
  340. static inline bool is_64bit_mm(struct mm_struct *mm)
  341. {
  342. return false;
  343. }
  344. /*
  345. * No RIP-relative addressing on 32-bit
  346. */
  347. static void handle_riprel_insn(struct arch_uprobe *auprobe, struct insn *insn)
  348. {
  349. }
  350. static void pre_xol_rip_insn(struct arch_uprobe *auprobe, struct pt_regs *regs,
  351. struct arch_uprobe_task *autask)
  352. {
  353. }
  354. static void handle_riprel_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs,
  355. long *correction)
  356. {
  357. }
  358. #endif /* CONFIG_X86_64 */
  359. struct uprobe_xol_ops {
  360. bool (*emulate)(struct arch_uprobe *, struct pt_regs *);
  361. int (*pre_xol)(struct arch_uprobe *, struct pt_regs *);
  362. int (*post_xol)(struct arch_uprobe *, struct pt_regs *);
  363. };
  364. static inline int sizeof_long(void)
  365. {
  366. return is_ia32_task() ? 4 : 8;
  367. }
  368. static int default_pre_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  369. {
  370. pre_xol_rip_insn(auprobe, regs, &current->utask->autask);
  371. return 0;
  372. }
  373. /*
  374. * Adjust the return address pushed by a call insn executed out of line.
  375. */
  376. static int adjust_ret_addr(unsigned long sp, long correction)
  377. {
  378. int rasize = sizeof_long();
  379. long ra;
  380. if (copy_from_user(&ra, (void __user *)sp, rasize))
  381. return -EFAULT;
  382. ra += correction;
  383. if (copy_to_user((void __user *)sp, &ra, rasize))
  384. return -EFAULT;
  385. return 0;
  386. }
  387. static int default_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  388. {
  389. struct uprobe_task *utask = current->utask;
  390. long correction = (long)(utask->vaddr - utask->xol_vaddr);
  391. handle_riprel_post_xol(auprobe, regs, &correction);
  392. if (auprobe->fixups & UPROBE_FIX_IP)
  393. regs->ip += correction;
  394. if (auprobe->fixups & UPROBE_FIX_CALL) {
  395. if (adjust_ret_addr(regs->sp, correction)) {
  396. regs->sp += sizeof_long();
  397. return -ERESTART;
  398. }
  399. }
  400. return 0;
  401. }
  402. static struct uprobe_xol_ops default_xol_ops = {
  403. .pre_xol = default_pre_xol_op,
  404. .post_xol = default_post_xol_op,
  405. };
  406. static bool branch_is_call(struct arch_uprobe *auprobe)
  407. {
  408. return auprobe->branch.opc1 == 0xe8;
  409. }
  410. #define CASE_COND \
  411. COND(70, 71, XF(OF)) \
  412. COND(72, 73, XF(CF)) \
  413. COND(74, 75, XF(ZF)) \
  414. COND(78, 79, XF(SF)) \
  415. COND(7a, 7b, XF(PF)) \
  416. COND(76, 77, XF(CF) || XF(ZF)) \
  417. COND(7c, 7d, XF(SF) != XF(OF)) \
  418. COND(7e, 7f, XF(ZF) || XF(SF) != XF(OF))
  419. #define COND(op_y, op_n, expr) \
  420. case 0x ## op_y: DO((expr) != 0) \
  421. case 0x ## op_n: DO((expr) == 0)
  422. #define XF(xf) (!!(flags & X86_EFLAGS_ ## xf))
  423. static bool is_cond_jmp_opcode(u8 opcode)
  424. {
  425. switch (opcode) {
  426. #define DO(expr) \
  427. return true;
  428. CASE_COND
  429. #undef DO
  430. default:
  431. return false;
  432. }
  433. }
  434. static bool check_jmp_cond(struct arch_uprobe *auprobe, struct pt_regs *regs)
  435. {
  436. unsigned long flags = regs->flags;
  437. switch (auprobe->branch.opc1) {
  438. #define DO(expr) \
  439. return expr;
  440. CASE_COND
  441. #undef DO
  442. default: /* not a conditional jmp */
  443. return true;
  444. }
  445. }
  446. #undef XF
  447. #undef COND
  448. #undef CASE_COND
  449. static bool branch_emulate_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  450. {
  451. unsigned long new_ip = regs->ip += auprobe->branch.ilen;
  452. unsigned long offs = (long)auprobe->branch.offs;
  453. if (branch_is_call(auprobe)) {
  454. unsigned long new_sp = regs->sp - sizeof_long();
  455. /*
  456. * If it fails we execute this (mangled, see the comment in
  457. * branch_clear_offset) insn out-of-line. In the likely case
  458. * this should trigger the trap, and the probed application
  459. * should die or restart the same insn after it handles the
  460. * signal, arch_uprobe_post_xol() won't be even called.
  461. *
  462. * But there is corner case, see the comment in ->post_xol().
  463. */
  464. if (copy_to_user((void __user *)new_sp, &new_ip, sizeof_long()))
  465. return false;
  466. regs->sp = new_sp;
  467. } else if (!check_jmp_cond(auprobe, regs)) {
  468. offs = 0;
  469. }
  470. regs->ip = new_ip + offs;
  471. return true;
  472. }
  473. static int branch_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  474. {
  475. BUG_ON(!branch_is_call(auprobe));
  476. /*
  477. * We can only get here if branch_emulate_op() failed to push the ret
  478. * address _and_ another thread expanded our stack before the (mangled)
  479. * "call" insn was executed out-of-line. Just restore ->sp and restart.
  480. * We could also restore ->ip and try to call branch_emulate_op() again.
  481. */
  482. regs->sp += sizeof_long();
  483. return -ERESTART;
  484. }
  485. static void branch_clear_offset(struct arch_uprobe *auprobe, struct insn *insn)
  486. {
  487. /*
  488. * Turn this insn into "call 1f; 1:", this is what we will execute
  489. * out-of-line if ->emulate() fails. We only need this to generate
  490. * a trap, so that the probed task receives the correct signal with
  491. * the properly filled siginfo.
  492. *
  493. * But see the comment in ->post_xol(), in the unlikely case it can
  494. * succeed. So we need to ensure that the new ->ip can not fall into
  495. * the non-canonical area and trigger #GP.
  496. *
  497. * We could turn it into (say) "pushf", but then we would need to
  498. * divorce ->insn[] and ->ixol[]. We need to preserve the 1st byte
  499. * of ->insn[] for set_orig_insn().
  500. */
  501. memset(auprobe->insn + insn_offset_immediate(insn),
  502. 0, insn->immediate.nbytes);
  503. }
  504. static struct uprobe_xol_ops branch_xol_ops = {
  505. .emulate = branch_emulate_op,
  506. .post_xol = branch_post_xol_op,
  507. };
  508. /* Returns -ENOSYS if branch_xol_ops doesn't handle this insn */
  509. static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
  510. {
  511. u8 opc1 = OPCODE1(insn);
  512. int i;
  513. switch (opc1) {
  514. case 0xeb: /* jmp 8 */
  515. case 0xe9: /* jmp 32 */
  516. case 0x90: /* prefix* + nop; same as jmp with .offs = 0 */
  517. break;
  518. case 0xe8: /* call relative */
  519. branch_clear_offset(auprobe, insn);
  520. break;
  521. case 0x0f:
  522. if (insn->opcode.nbytes != 2)
  523. return -ENOSYS;
  524. /*
  525. * If it is a "near" conditional jmp, OPCODE2() - 0x10 matches
  526. * OPCODE1() of the "short" jmp which checks the same condition.
  527. */
  528. opc1 = OPCODE2(insn) - 0x10;
  529. default:
  530. if (!is_cond_jmp_opcode(opc1))
  531. return -ENOSYS;
  532. }
  533. /*
  534. * 16-bit overrides such as CALLW (66 e8 nn nn) are not supported.
  535. * Intel and AMD behavior differ in 64-bit mode: Intel ignores 66 prefix.
  536. * No one uses these insns, reject any branch insns with such prefix.
  537. */
  538. for (i = 0; i < insn->prefixes.nbytes; i++) {
  539. if (insn->prefixes.bytes[i] == 0x66)
  540. return -ENOTSUPP;
  541. }
  542. auprobe->branch.opc1 = opc1;
  543. auprobe->branch.ilen = insn->length;
  544. auprobe->branch.offs = insn->immediate.value;
  545. auprobe->ops = &branch_xol_ops;
  546. return 0;
  547. }
  548. /**
  549. * arch_uprobe_analyze_insn - instruction analysis including validity and fixups.
  550. * @mm: the probed address space.
  551. * @arch_uprobe: the probepoint information.
  552. * @addr: virtual address at which to install the probepoint
  553. * Return 0 on success or a -ve number on error.
  554. */
  555. int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long addr)
  556. {
  557. struct insn insn;
  558. bool fix_ip = true, fix_call = false;
  559. int ret;
  560. ret = uprobe_init_insn(auprobe, &insn, is_64bit_mm(mm));
  561. if (ret)
  562. return ret;
  563. ret = branch_setup_xol_ops(auprobe, &insn);
  564. if (ret != -ENOSYS)
  565. return ret;
  566. /*
  567. * Figure out which fixups arch_uprobe_post_xol() will need to perform,
  568. * and annotate arch_uprobe->fixups accordingly. To start with, ->fixups
  569. * is either zero or it reflects rip-related fixups.
  570. */
  571. switch (OPCODE1(&insn)) {
  572. case 0x9d: /* popf */
  573. auprobe->fixups |= UPROBE_FIX_SETF;
  574. break;
  575. case 0xc3: /* ret or lret -- ip is correct */
  576. case 0xcb:
  577. case 0xc2:
  578. case 0xca:
  579. fix_ip = false;
  580. break;
  581. case 0x9a: /* call absolute - Fix return addr, not ip */
  582. fix_call = true;
  583. fix_ip = false;
  584. break;
  585. case 0xea: /* jmp absolute -- ip is correct */
  586. fix_ip = false;
  587. break;
  588. case 0xff:
  589. switch (MODRM_REG(&insn)) {
  590. case 2: case 3: /* call or lcall, indirect */
  591. fix_call = true;
  592. case 4: case 5: /* jmp or ljmp, indirect */
  593. fix_ip = false;
  594. }
  595. /* fall through */
  596. default:
  597. handle_riprel_insn(auprobe, &insn);
  598. }
  599. if (fix_ip)
  600. auprobe->fixups |= UPROBE_FIX_IP;
  601. if (fix_call)
  602. auprobe->fixups |= UPROBE_FIX_CALL;
  603. auprobe->ops = &default_xol_ops;
  604. return 0;
  605. }
  606. /*
  607. * arch_uprobe_pre_xol - prepare to execute out of line.
  608. * @auprobe: the probepoint information.
  609. * @regs: reflects the saved user state of current task.
  610. */
  611. int arch_uprobe_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  612. {
  613. struct uprobe_task *utask = current->utask;
  614. regs->ip = utask->xol_vaddr;
  615. utask->autask.saved_trap_nr = current->thread.trap_nr;
  616. current->thread.trap_nr = UPROBE_TRAP_NR;
  617. utask->autask.saved_tf = !!(regs->flags & X86_EFLAGS_TF);
  618. regs->flags |= X86_EFLAGS_TF;
  619. if (test_tsk_thread_flag(current, TIF_BLOCKSTEP))
  620. set_task_blockstep(current, false);
  621. if (auprobe->ops->pre_xol)
  622. return auprobe->ops->pre_xol(auprobe, regs);
  623. return 0;
  624. }
  625. /*
  626. * If xol insn itself traps and generates a signal(Say,
  627. * SIGILL/SIGSEGV/etc), then detect the case where a singlestepped
  628. * instruction jumps back to its own address. It is assumed that anything
  629. * like do_page_fault/do_trap/etc sets thread.trap_nr != -1.
  630. *
  631. * arch_uprobe_pre_xol/arch_uprobe_post_xol save/restore thread.trap_nr,
  632. * arch_uprobe_xol_was_trapped() simply checks that ->trap_nr is not equal to
  633. * UPROBE_TRAP_NR == -1 set by arch_uprobe_pre_xol().
  634. */
  635. bool arch_uprobe_xol_was_trapped(struct task_struct *t)
  636. {
  637. if (t->thread.trap_nr != UPROBE_TRAP_NR)
  638. return true;
  639. return false;
  640. }
  641. /*
  642. * Called after single-stepping. To avoid the SMP problems that can
  643. * occur when we temporarily put back the original opcode to
  644. * single-step, we single-stepped a copy of the instruction.
  645. *
  646. * This function prepares to resume execution after the single-step.
  647. * We have to fix things up as follows:
  648. *
  649. * Typically, the new ip is relative to the copied instruction. We need
  650. * to make it relative to the original instruction (FIX_IP). Exceptions
  651. * are return instructions and absolute or indirect jump or call instructions.
  652. *
  653. * If the single-stepped instruction was a call, the return address that
  654. * is atop the stack is the address following the copied instruction. We
  655. * need to make it the address following the original instruction (FIX_CALL).
  656. *
  657. * If the original instruction was a rip-relative instruction such as
  658. * "movl %edx,0xnnnn(%rip)", we have instead executed an equivalent
  659. * instruction using a scratch register -- e.g., "movl %edx,(%rax)".
  660. * We need to restore the contents of the scratch register and adjust
  661. * the ip, keeping in mind that the instruction we executed is 4 bytes
  662. * shorter than the original instruction (since we squeezed out the offset
  663. * field). (FIX_RIP_AX or FIX_RIP_CX)
  664. */
  665. int arch_uprobe_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  666. {
  667. struct uprobe_task *utask = current->utask;
  668. WARN_ON_ONCE(current->thread.trap_nr != UPROBE_TRAP_NR);
  669. if (auprobe->ops->post_xol) {
  670. int err = auprobe->ops->post_xol(auprobe, regs);
  671. if (err) {
  672. arch_uprobe_abort_xol(auprobe, regs);
  673. /*
  674. * Restart the probed insn. ->post_xol() must ensure
  675. * this is really possible if it returns -ERESTART.
  676. */
  677. if (err == -ERESTART)
  678. return 0;
  679. return err;
  680. }
  681. }
  682. current->thread.trap_nr = utask->autask.saved_trap_nr;
  683. /*
  684. * arch_uprobe_pre_xol() doesn't save the state of TIF_BLOCKSTEP
  685. * so we can get an extra SIGTRAP if we do not clear TF. We need
  686. * to examine the opcode to make it right.
  687. */
  688. if (utask->autask.saved_tf)
  689. send_sig(SIGTRAP, current, 0);
  690. else if (!(auprobe->fixups & UPROBE_FIX_SETF))
  691. regs->flags &= ~X86_EFLAGS_TF;
  692. return 0;
  693. }
  694. /* callback routine for handling exceptions. */
  695. int arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val, void *data)
  696. {
  697. struct die_args *args = data;
  698. struct pt_regs *regs = args->regs;
  699. int ret = NOTIFY_DONE;
  700. /* We are only interested in userspace traps */
  701. if (regs && !user_mode_vm(regs))
  702. return NOTIFY_DONE;
  703. switch (val) {
  704. case DIE_INT3:
  705. if (uprobe_pre_sstep_notifier(regs))
  706. ret = NOTIFY_STOP;
  707. break;
  708. case DIE_DEBUG:
  709. if (uprobe_post_sstep_notifier(regs))
  710. ret = NOTIFY_STOP;
  711. default:
  712. break;
  713. }
  714. return ret;
  715. }
  716. /*
  717. * This function gets called when XOL instruction either gets trapped or
  718. * the thread has a fatal signal, or if arch_uprobe_post_xol() failed.
  719. * Reset the instruction pointer to its probed address for the potential
  720. * restart or for post mortem analysis.
  721. */
  722. void arch_uprobe_abort_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  723. {
  724. struct uprobe_task *utask = current->utask;
  725. current->thread.trap_nr = utask->autask.saved_trap_nr;
  726. handle_riprel_post_xol(auprobe, regs, NULL);
  727. instruction_pointer_set(regs, utask->vaddr);
  728. /* clear TF if it was set by us in arch_uprobe_pre_xol() */
  729. if (!utask->autask.saved_tf)
  730. regs->flags &= ~X86_EFLAGS_TF;
  731. }
  732. static bool __skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
  733. {
  734. if (auprobe->ops->emulate)
  735. return auprobe->ops->emulate(auprobe, regs);
  736. return false;
  737. }
  738. bool arch_uprobe_skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
  739. {
  740. bool ret = __skip_sstep(auprobe, regs);
  741. if (ret && (regs->flags & X86_EFLAGS_TF))
  742. send_sig(SIGTRAP, current, 0);
  743. return ret;
  744. }
  745. unsigned long
  746. arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs)
  747. {
  748. int rasize = sizeof_long(), nleft;
  749. unsigned long orig_ret_vaddr = 0; /* clear high bits for 32-bit apps */
  750. if (copy_from_user(&orig_ret_vaddr, (void __user *)regs->sp, rasize))
  751. return -1;
  752. /* check whether address has been already hijacked */
  753. if (orig_ret_vaddr == trampoline_vaddr)
  754. return orig_ret_vaddr;
  755. nleft = copy_to_user((void __user *)regs->sp, &trampoline_vaddr, rasize);
  756. if (likely(!nleft))
  757. return orig_ret_vaddr;
  758. if (nleft != rasize) {
  759. pr_err("uprobe: return address clobbered: pid=%d, %%sp=%#lx, "
  760. "%%ip=%#lx\n", current->pid, regs->sp, regs->ip);
  761. force_sig_info(SIGSEGV, SEND_SIG_FORCED, current);
  762. }
  763. return -1;
  764. }