uprobes.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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/kdebug.h>
  28. #include <asm/insn.h>
  29. /* Post-execution fixups. */
  30. /* No fixup needed */
  31. #define UPROBES_FIX_NONE 0x0
  32. /* Adjust IP back to vicinity of actual insn */
  33. #define UPROBES_FIX_IP 0x1
  34. /* Adjust the return address of a call insn */
  35. #define UPROBES_FIX_CALL 0x2
  36. #define UPROBES_FIX_RIP_AX 0x8000
  37. #define UPROBES_FIX_RIP_CX 0x4000
  38. /* Adaptations for mhiramat x86 decoder v14. */
  39. #define OPCODE1(insn) ((insn)->opcode.bytes[0])
  40. #define OPCODE2(insn) ((insn)->opcode.bytes[1])
  41. #define OPCODE3(insn) ((insn)->opcode.bytes[2])
  42. #define MODRM_REG(insn) X86_MODRM_REG(insn->modrm.value)
  43. #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
  44. (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
  45. (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
  46. (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
  47. (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
  48. << (row % 32))
  49. /*
  50. * Good-instruction tables for 32-bit apps. This is non-const and volatile
  51. * to keep gcc from statically optimizing it out, as variable_test_bit makes
  52. * some versions of gcc to think only *(unsigned long*) is used.
  53. */
  54. static volatile u32 good_insns_32[256 / 32] = {
  55. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  56. /* ---------------------------------------------- */
  57. W(0x00, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0) | /* 00 */
  58. W(0x10, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0) , /* 10 */
  59. W(0x20, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1) | /* 20 */
  60. W(0x30, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1) , /* 30 */
  61. W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
  62. W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 50 */
  63. W(0x60, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* 60 */
  64. W(0x70, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 70 */
  65. W(0x80, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */
  66. W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
  67. W(0xa0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* a0 */
  68. W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* b0 */
  69. W(0xc0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0) | /* c0 */
  70. W(0xd0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */
  71. W(0xe0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* e0 */
  72. W(0xf0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1) /* f0 */
  73. /* ---------------------------------------------- */
  74. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  75. };
  76. /* Using this for both 64-bit and 32-bit apps */
  77. static volatile u32 good_2byte_insns[256 / 32] = {
  78. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  79. /* ---------------------------------------------- */
  80. W(0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1) | /* 00 */
  81. W(0x10, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1) , /* 10 */
  82. W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* 20 */
  83. W(0x30, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
  84. W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
  85. W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 50 */
  86. W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 60 */
  87. W(0x70, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
  88. W(0x80, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */
  89. W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
  90. W(0xa0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1) | /* a0 */
  91. W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1) , /* b0 */
  92. W(0xc0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
  93. W(0xd0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */
  94. W(0xe0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* e0 */
  95. W(0xf0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0) /* f0 */
  96. /* ---------------------------------------------- */
  97. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  98. };
  99. #ifdef CONFIG_X86_64
  100. /* Good-instruction tables for 64-bit apps */
  101. static volatile u32 good_insns_64[256 / 32] = {
  102. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  103. /* ---------------------------------------------- */
  104. W(0x00, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0) | /* 00 */
  105. W(0x10, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0) , /* 10 */
  106. W(0x20, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0) | /* 20 */
  107. W(0x30, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0) , /* 30 */
  108. W(0x40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 40 */
  109. W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 50 */
  110. W(0x60, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* 60 */
  111. W(0x70, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 70 */
  112. W(0x80, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */
  113. W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
  114. W(0xa0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* a0 */
  115. W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* b0 */
  116. W(0xc0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0) | /* c0 */
  117. W(0xd0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */
  118. W(0xe0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* e0 */
  119. W(0xf0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1) /* f0 */
  120. /* ---------------------------------------------- */
  121. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  122. };
  123. #endif
  124. #undef W
  125. /*
  126. * opcodes we'll probably never support:
  127. *
  128. * 6c-6d, e4-e5, ec-ed - in
  129. * 6e-6f, e6-e7, ee-ef - out
  130. * cc, cd - int3, int
  131. * cf - iret
  132. * d6 - illegal instruction
  133. * f1 - int1/icebp
  134. * f4 - hlt
  135. * fa, fb - cli, sti
  136. * 0f - lar, lsl, syscall, clts, sysret, sysenter, sysexit, invd, wbinvd, ud2
  137. *
  138. * invalid opcodes in 64-bit mode:
  139. *
  140. * 06, 0e, 16, 1e, 27, 2f, 37, 3f, 60-62, 82, c4-c5, d4-d5
  141. * 63 - we support this opcode in x86_64 but not in i386.
  142. *
  143. * opcodes we may need to refine support for:
  144. *
  145. * 0f - 2-byte instructions: For many of these instructions, the validity
  146. * depends on the prefix and/or the reg field. On such instructions, we
  147. * just consider the opcode combination valid if it corresponds to any
  148. * valid instruction.
  149. *
  150. * 8f - Group 1 - only reg = 0 is OK
  151. * c6-c7 - Group 11 - only reg = 0 is OK
  152. * d9-df - fpu insns with some illegal encodings
  153. * f2, f3 - repnz, repz prefixes. These are also the first byte for
  154. * certain floating-point instructions, such as addsd.
  155. *
  156. * fe - Group 4 - only reg = 0 or 1 is OK
  157. * ff - Group 5 - only reg = 0-6 is OK
  158. *
  159. * others -- Do we need to support these?
  160. *
  161. * 0f - (floating-point?) prefetch instructions
  162. * 07, 17, 1f - pop es, pop ss, pop ds
  163. * 26, 2e, 36, 3e - es:, cs:, ss:, ds: segment prefixes --
  164. * but 64 and 65 (fs: and gs:) seem to be used, so we support them
  165. * 67 - addr16 prefix
  166. * ce - into
  167. * f0 - lock prefix
  168. */
  169. /*
  170. * TODO:
  171. * - Where necessary, examine the modrm byte and allow only valid instructions
  172. * in the different Groups and fpu instructions.
  173. */
  174. static bool is_prefix_bad(struct insn *insn)
  175. {
  176. int i;
  177. for (i = 0; i < insn->prefixes.nbytes; i++) {
  178. switch (insn->prefixes.bytes[i]) {
  179. case 0x26: /* INAT_PFX_ES */
  180. case 0x2E: /* INAT_PFX_CS */
  181. case 0x36: /* INAT_PFX_DS */
  182. case 0x3E: /* INAT_PFX_SS */
  183. case 0xF0: /* INAT_PFX_LOCK */
  184. return true;
  185. }
  186. }
  187. return false;
  188. }
  189. static int validate_insn_32bits(struct arch_uprobe *auprobe, struct insn *insn)
  190. {
  191. insn_init(insn, auprobe->insn, false);
  192. /* Skip good instruction prefixes; reject "bad" ones. */
  193. insn_get_opcode(insn);
  194. if (is_prefix_bad(insn))
  195. return -ENOTSUPP;
  196. if (test_bit(OPCODE1(insn), (unsigned long *)good_insns_32))
  197. return 0;
  198. if (insn->opcode.nbytes == 2) {
  199. if (test_bit(OPCODE2(insn), (unsigned long *)good_2byte_insns))
  200. return 0;
  201. }
  202. return -ENOTSUPP;
  203. }
  204. /*
  205. * Figure out which fixups post_xol() will need to perform, and annotate
  206. * arch_uprobe->fixups accordingly. To start with,
  207. * arch_uprobe->fixups is either zero or it reflects rip-related
  208. * fixups.
  209. */
  210. static void prepare_fixups(struct arch_uprobe *auprobe, struct insn *insn)
  211. {
  212. bool fix_ip = true, fix_call = false; /* defaults */
  213. int reg;
  214. insn_get_opcode(insn); /* should be a nop */
  215. switch (OPCODE1(insn)) {
  216. case 0xc3: /* ret/lret */
  217. case 0xcb:
  218. case 0xc2:
  219. case 0xca:
  220. /* ip is correct */
  221. fix_ip = false;
  222. break;
  223. case 0xe8: /* call relative - Fix return addr */
  224. fix_call = true;
  225. break;
  226. case 0x9a: /* call absolute - Fix return addr, not ip */
  227. fix_call = true;
  228. fix_ip = false;
  229. break;
  230. case 0xff:
  231. insn_get_modrm(insn);
  232. reg = MODRM_REG(insn);
  233. if (reg == 2 || reg == 3) {
  234. /* call or lcall, indirect */
  235. /* Fix return addr; ip is correct. */
  236. fix_call = true;
  237. fix_ip = false;
  238. } else if (reg == 4 || reg == 5) {
  239. /* jmp or ljmp, indirect */
  240. /* ip is correct. */
  241. fix_ip = false;
  242. }
  243. break;
  244. case 0xea: /* jmp absolute -- ip is correct */
  245. fix_ip = false;
  246. break;
  247. default:
  248. break;
  249. }
  250. if (fix_ip)
  251. auprobe->fixups |= UPROBES_FIX_IP;
  252. if (fix_call)
  253. auprobe->fixups |= UPROBES_FIX_CALL;
  254. }
  255. #ifdef CONFIG_X86_64
  256. /*
  257. * If arch_uprobe->insn doesn't use rip-relative addressing, return
  258. * immediately. Otherwise, rewrite the instruction so that it accesses
  259. * its memory operand indirectly through a scratch register. Set
  260. * arch_uprobe->fixups and arch_uprobe->rip_rela_target_address
  261. * accordingly. (The contents of the scratch register will be saved
  262. * before we single-step the modified instruction, and restored
  263. * afterward.)
  264. *
  265. * We do this because a rip-relative instruction can access only a
  266. * relatively small area (+/- 2 GB from the instruction), and the XOL
  267. * area typically lies beyond that area. At least for instructions
  268. * that store to memory, we can't execute the original instruction
  269. * and "fix things up" later, because the misdirected store could be
  270. * disastrous.
  271. *
  272. * Some useful facts about rip-relative instructions:
  273. *
  274. * - There's always a modrm byte.
  275. * - There's never a SIB byte.
  276. * - The displacement is always 4 bytes.
  277. */
  278. static void handle_riprel_insn(struct mm_struct *mm, struct arch_uprobe *auprobe, struct insn *insn)
  279. {
  280. u8 *cursor;
  281. u8 reg;
  282. if (mm->context.ia32_compat)
  283. return;
  284. auprobe->rip_rela_target_address = 0x0;
  285. if (!insn_rip_relative(insn))
  286. return;
  287. /*
  288. * insn_rip_relative() would have decoded rex_prefix, modrm.
  289. * Clear REX.b bit (extension of MODRM.rm field):
  290. * we want to encode rax/rcx, not r8/r9.
  291. */
  292. if (insn->rex_prefix.nbytes) {
  293. cursor = auprobe->insn + insn_offset_rex_prefix(insn);
  294. *cursor &= 0xfe; /* Clearing REX.B bit */
  295. }
  296. /*
  297. * Point cursor at the modrm byte. The next 4 bytes are the
  298. * displacement. Beyond the displacement, for some instructions,
  299. * is the immediate operand.
  300. */
  301. cursor = auprobe->insn + insn_offset_modrm(insn);
  302. insn_get_length(insn);
  303. /*
  304. * Convert from rip-relative addressing to indirect addressing
  305. * via a scratch register. Change the r/m field from 0x5 (%rip)
  306. * to 0x0 (%rax) or 0x1 (%rcx), and squeeze out the offset field.
  307. */
  308. reg = MODRM_REG(insn);
  309. if (reg == 0) {
  310. /*
  311. * The register operand (if any) is either the A register
  312. * (%rax, %eax, etc.) or (if the 0x4 bit is set in the
  313. * REX prefix) %r8. In any case, we know the C register
  314. * is NOT the register operand, so we use %rcx (register
  315. * #1) for the scratch register.
  316. */
  317. auprobe->fixups = UPROBES_FIX_RIP_CX;
  318. /* Change modrm from 00 000 101 to 00 000 001. */
  319. *cursor = 0x1;
  320. } else {
  321. /* Use %rax (register #0) for the scratch register. */
  322. auprobe->fixups = UPROBES_FIX_RIP_AX;
  323. /* Change modrm from 00 xxx 101 to 00 xxx 000 */
  324. *cursor = (reg << 3);
  325. }
  326. /* Target address = address of next instruction + (signed) offset */
  327. auprobe->rip_rela_target_address = (long)insn->length + insn->displacement.value;
  328. /* Displacement field is gone; slide immediate field (if any) over. */
  329. if (insn->immediate.nbytes) {
  330. cursor++;
  331. memmove(cursor, cursor + insn->displacement.nbytes, insn->immediate.nbytes);
  332. }
  333. return;
  334. }
  335. static int validate_insn_64bits(struct arch_uprobe *auprobe, struct insn *insn)
  336. {
  337. insn_init(insn, auprobe->insn, true);
  338. /* Skip good instruction prefixes; reject "bad" ones. */
  339. insn_get_opcode(insn);
  340. if (is_prefix_bad(insn))
  341. return -ENOTSUPP;
  342. if (test_bit(OPCODE1(insn), (unsigned long *)good_insns_64))
  343. return 0;
  344. if (insn->opcode.nbytes == 2) {
  345. if (test_bit(OPCODE2(insn), (unsigned long *)good_2byte_insns))
  346. return 0;
  347. }
  348. return -ENOTSUPP;
  349. }
  350. static int validate_insn_bits(struct mm_struct *mm, struct arch_uprobe *auprobe, struct insn *insn)
  351. {
  352. if (mm->context.ia32_compat)
  353. return validate_insn_32bits(auprobe, insn);
  354. return validate_insn_64bits(auprobe, insn);
  355. }
  356. #else /* 32-bit: */
  357. static void handle_riprel_insn(struct mm_struct *mm, struct arch_uprobe *auprobe, struct insn *insn)
  358. {
  359. /* No RIP-relative addressing on 32-bit */
  360. }
  361. static int validate_insn_bits(struct mm_struct *mm, struct arch_uprobe *auprobe, struct insn *insn)
  362. {
  363. return validate_insn_32bits(auprobe, insn);
  364. }
  365. #endif /* CONFIG_X86_64 */
  366. /**
  367. * arch_uprobes_analyze_insn - instruction analysis including validity and fixups.
  368. * @mm: the probed address space.
  369. * @arch_uprobe: the probepoint information.
  370. * Return 0 on success or a -ve number on error.
  371. */
  372. int arch_uprobes_analyze_insn(struct mm_struct *mm, struct arch_uprobe *auprobe)
  373. {
  374. int ret;
  375. struct insn insn;
  376. auprobe->fixups = 0;
  377. ret = validate_insn_bits(mm, auprobe, &insn);
  378. if (ret != 0)
  379. return ret;
  380. handle_riprel_insn(mm, auprobe, &insn);
  381. prepare_fixups(auprobe, &insn);
  382. return 0;
  383. }