uprobes.c 21 KB

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