uprobes.c 23 KB

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