uprobes.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  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. #include <asm/mmu_context.h>
  32. /* Post-execution fixups. */
  33. /* Adjust IP back to vicinity of actual insn */
  34. #define UPROBE_FIX_IP 0x01
  35. /* Adjust the return address of a call insn */
  36. #define UPROBE_FIX_CALL 0x02
  37. /* Instruction will modify TF, don't change it */
  38. #define UPROBE_FIX_SETF 0x04
  39. #define UPROBE_FIX_RIP_SI 0x08
  40. #define UPROBE_FIX_RIP_DI 0x10
  41. #define UPROBE_FIX_RIP_BX 0x20
  42. #define UPROBE_FIX_RIP_MASK \
  43. (UPROBE_FIX_RIP_SI | UPROBE_FIX_RIP_DI | UPROBE_FIX_RIP_BX)
  44. #define UPROBE_TRAP_NR UINT_MAX
  45. /* Adaptations for mhiramat x86 decoder v14. */
  46. #define OPCODE1(insn) ((insn)->opcode.bytes[0])
  47. #define OPCODE2(insn) ((insn)->opcode.bytes[1])
  48. #define OPCODE3(insn) ((insn)->opcode.bytes[2])
  49. #define MODRM_REG(insn) X86_MODRM_REG((insn)->modrm.value)
  50. #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
  51. (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
  52. (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
  53. (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
  54. (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
  55. << (row % 32))
  56. /*
  57. * Good-instruction tables for 32-bit apps. This is non-const and volatile
  58. * to keep gcc from statically optimizing it out, as variable_test_bit makes
  59. * some versions of gcc to think only *(unsigned long*) is used.
  60. *
  61. * Opcodes we'll probably never support:
  62. * 6c-6f - ins,outs. SEGVs if used in userspace
  63. * e4-e7 - in,out imm. SEGVs if used in userspace
  64. * ec-ef - in,out acc. SEGVs if used in userspace
  65. * cc - int3. SIGTRAP if used in userspace
  66. * ce - into. Not used in userspace - no kernel support to make it useful. SEGVs
  67. * (why we support bound (62) then? it's similar, and similarly unused...)
  68. * f1 - int1. SIGTRAP if used in userspace
  69. * f4 - hlt. SEGVs if used in userspace
  70. * fa - cli. SEGVs if used in userspace
  71. * fb - sti. SEGVs if used in userspace
  72. *
  73. * Opcodes which need some work to be supported:
  74. * 07,17,1f - pop es/ss/ds
  75. * Normally not used in userspace, but would execute if used.
  76. * Can cause GP or stack exception if tries to load wrong segment descriptor.
  77. * We hesitate to run them under single step since kernel's handling
  78. * of userspace single-stepping (TF flag) is fragile.
  79. * We can easily refuse to support push es/cs/ss/ds (06/0e/16/1e)
  80. * on the same grounds that they are never used.
  81. * cd - int N.
  82. * Used by userspace for "int 80" syscall entry. (Other "int N"
  83. * cause GP -> SEGV since their IDT gates don't allow calls from CPL 3).
  84. * Not supported since kernel's handling of userspace single-stepping
  85. * (TF flag) is fragile.
  86. * cf - iret. Normally not used in userspace. Doesn't SEGV unless arguments are bad
  87. */
  88. #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
  89. static volatile u32 good_insns_32[256 / 32] = {
  90. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  91. /* ---------------------------------------------- */
  92. W(0x00, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* 00 */
  93. W(0x10, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0) , /* 10 */
  94. W(0x20, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 20 */
  95. W(0x30, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 30 */
  96. W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
  97. W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 50 */
  98. W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0) | /* 60 */
  99. W(0x70, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 70 */
  100. W(0x80, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */
  101. W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
  102. W(0xa0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* a0 */
  103. W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* b0 */
  104. W(0xc0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0) | /* c0 */
  105. W(0xd0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */
  106. W(0xe0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* e0 */
  107. W(0xf0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1) /* f0 */
  108. /* ---------------------------------------------- */
  109. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  110. };
  111. #else
  112. #define good_insns_32 NULL
  113. #endif
  114. /* Good-instruction tables for 64-bit apps.
  115. *
  116. * Genuinely invalid opcodes:
  117. * 06,07 - formerly push/pop es
  118. * 0e - formerly push cs
  119. * 16,17 - formerly push/pop ss
  120. * 1e,1f - formerly push/pop ds
  121. * 27,2f,37,3f - formerly daa/das/aaa/aas
  122. * 60,61 - formerly pusha/popa
  123. * 62 - formerly bound. EVEX prefix for AVX512 (not yet supported)
  124. * 82 - formerly redundant encoding of Group1
  125. * 9a - formerly call seg:ofs
  126. * ce - formerly into
  127. * d4,d5 - formerly aam/aad
  128. * d6 - formerly undocumented salc
  129. * ea - formerly jmp seg:ofs
  130. *
  131. * Opcodes we'll probably never support:
  132. * 6c-6f - ins,outs. SEGVs if used in userspace
  133. * e4-e7 - in,out imm. SEGVs if used in userspace
  134. * ec-ef - in,out acc. SEGVs if used in userspace
  135. * cc - int3. SIGTRAP if used in userspace
  136. * f1 - int1. SIGTRAP if used in userspace
  137. * f4 - hlt. SEGVs if used in userspace
  138. * fa - cli. SEGVs if used in userspace
  139. * fb - sti. SEGVs if used in userspace
  140. *
  141. * Opcodes which need some work to be supported:
  142. * cd - int N.
  143. * Used by userspace for "int 80" syscall entry. (Other "int N"
  144. * cause GP -> SEGV since their IDT gates don't allow calls from CPL 3).
  145. * Not supported since kernel's handling of userspace single-stepping
  146. * (TF flag) is fragile.
  147. * cf - iret. Normally not used in userspace. Doesn't SEGV unless arguments are bad
  148. */
  149. #if defined(CONFIG_X86_64)
  150. static volatile u32 good_insns_64[256 / 32] = {
  151. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  152. /* ---------------------------------------------- */
  153. W(0x00, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1) | /* 00 */
  154. W(0x10, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0) , /* 10 */
  155. W(0x20, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0) | /* 20 */
  156. W(0x30, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0) , /* 30 */
  157. W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
  158. W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 50 */
  159. W(0x60, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0) | /* 60 */
  160. W(0x70, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 70 */
  161. W(0x80, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */
  162. W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1) , /* 90 */
  163. W(0xa0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* a0 */
  164. W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* b0 */
  165. W(0xc0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0) | /* c0 */
  166. W(0xd0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */
  167. W(0xe0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0) | /* e0 */
  168. W(0xf0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1) /* f0 */
  169. /* ---------------------------------------------- */
  170. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  171. };
  172. #else
  173. #define good_insns_64 NULL
  174. #endif
  175. /* Using this for both 64-bit and 32-bit apps.
  176. * Opcodes we don't support:
  177. * 0f 00 - SLDT/STR/LLDT/LTR/VERR/VERW/-/- group. System insns
  178. * 0f 01 - SGDT/SIDT/LGDT/LIDT/SMSW/-/LMSW/INVLPG group.
  179. * Also encodes tons of other system insns if mod=11.
  180. * Some are in fact non-system: xend, xtest, rdtscp, maybe more
  181. * 0f 05 - syscall
  182. * 0f 06 - clts (CPL0 insn)
  183. * 0f 07 - sysret
  184. * 0f 08 - invd (CPL0 insn)
  185. * 0f 09 - wbinvd (CPL0 insn)
  186. * 0f 0b - ud2
  187. * 0f 30 - wrmsr (CPL0 insn) (then why rdmsr is allowed, it's also CPL0 insn?)
  188. * 0f 34 - sysenter
  189. * 0f 35 - sysexit
  190. * 0f 37 - getsec
  191. * 0f 78 - vmread (Intel VMX. CPL0 insn)
  192. * 0f 79 - vmwrite (Intel VMX. CPL0 insn)
  193. * Note: with prefixes, these two opcodes are
  194. * extrq/insertq/AVX512 convert vector ops.
  195. * 0f ae - group15: [f]xsave,[f]xrstor,[v]{ld,st}mxcsr,clflush[opt],
  196. * {rd,wr}{fs,gs}base,{s,l,m}fence.
  197. * Why? They are all user-executable.
  198. */
  199. static volatile u32 good_2byte_insns[256 / 32] = {
  200. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  201. /* ---------------------------------------------- */
  202. W(0x00, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1) | /* 00 */
  203. W(0x10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 10 */
  204. W(0x20, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 20 */
  205. W(0x30, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1) , /* 30 */
  206. W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
  207. W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 50 */
  208. W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 60 */
  209. W(0x70, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1) , /* 70 */
  210. W(0x80, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */
  211. W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
  212. W(0xa0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1) | /* a0 */
  213. W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* b0 */
  214. W(0xc0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
  215. W(0xd0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */
  216. W(0xe0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* e0 */
  217. W(0xf0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) /* f0 */
  218. /* ---------------------------------------------- */
  219. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  220. };
  221. #undef W
  222. /*
  223. * opcodes we may need to refine support for:
  224. *
  225. * 0f - 2-byte instructions: For many of these instructions, the validity
  226. * depends on the prefix and/or the reg field. On such instructions, we
  227. * just consider the opcode combination valid if it corresponds to any
  228. * valid instruction.
  229. *
  230. * 8f - Group 1 - only reg = 0 is OK
  231. * c6-c7 - Group 11 - only reg = 0 is OK
  232. * d9-df - fpu insns with some illegal encodings
  233. * f2, f3 - repnz, repz prefixes. These are also the first byte for
  234. * certain floating-point instructions, such as addsd.
  235. *
  236. * fe - Group 4 - only reg = 0 or 1 is OK
  237. * ff - Group 5 - only reg = 0-6 is OK
  238. *
  239. * others -- Do we need to support these?
  240. *
  241. * 0f - (floating-point?) prefetch instructions
  242. * 07, 17, 1f - pop es, pop ss, pop ds
  243. * 26, 2e, 36, 3e - es:, cs:, ss:, ds: segment prefixes --
  244. * but 64 and 65 (fs: and gs:) seem to be used, so we support them
  245. * 67 - addr16 prefix
  246. * ce - into
  247. * f0 - lock prefix
  248. */
  249. /*
  250. * TODO:
  251. * - Where necessary, examine the modrm byte and allow only valid instructions
  252. * in the different Groups and fpu instructions.
  253. */
  254. static bool is_prefix_bad(struct insn *insn)
  255. {
  256. insn_byte_t p;
  257. int i;
  258. for_each_insn_prefix(insn, i, p) {
  259. insn_attr_t attr;
  260. attr = inat_get_opcode_attribute(p);
  261. switch (attr) {
  262. case INAT_MAKE_PREFIX(INAT_PFX_ES):
  263. case INAT_MAKE_PREFIX(INAT_PFX_CS):
  264. case INAT_MAKE_PREFIX(INAT_PFX_DS):
  265. case INAT_MAKE_PREFIX(INAT_PFX_SS):
  266. case INAT_MAKE_PREFIX(INAT_PFX_LOCK):
  267. return true;
  268. }
  269. }
  270. return false;
  271. }
  272. static int uprobe_init_insn(struct arch_uprobe *auprobe, struct insn *insn, bool x86_64)
  273. {
  274. u32 volatile *good_insns;
  275. insn_init(insn, auprobe->insn, sizeof(auprobe->insn), x86_64);
  276. /* has the side-effect of processing the entire instruction */
  277. insn_get_length(insn);
  278. if (!insn_complete(insn))
  279. return -ENOEXEC;
  280. if (is_prefix_bad(insn))
  281. return -ENOTSUPP;
  282. /* We should not singlestep on the exception masking instructions */
  283. if (insn_masking_exception(insn))
  284. return -ENOTSUPP;
  285. if (x86_64)
  286. good_insns = good_insns_64;
  287. else
  288. good_insns = good_insns_32;
  289. if (test_bit(OPCODE1(insn), (unsigned long *)good_insns))
  290. return 0;
  291. if (insn->opcode.nbytes == 2) {
  292. if (test_bit(OPCODE2(insn), (unsigned long *)good_2byte_insns))
  293. return 0;
  294. }
  295. return -ENOTSUPP;
  296. }
  297. #ifdef CONFIG_X86_64
  298. /*
  299. * If arch_uprobe->insn doesn't use rip-relative addressing, return
  300. * immediately. Otherwise, rewrite the instruction so that it accesses
  301. * its memory operand indirectly through a scratch register. Set
  302. * defparam->fixups accordingly. (The contents of the scratch register
  303. * will be saved before we single-step the modified instruction,
  304. * and restored afterward).
  305. *
  306. * We do this because a rip-relative instruction can access only a
  307. * relatively small area (+/- 2 GB from the instruction), and the XOL
  308. * area typically lies beyond that area. At least for instructions
  309. * that store to memory, we can't execute the original instruction
  310. * and "fix things up" later, because the misdirected store could be
  311. * disastrous.
  312. *
  313. * Some useful facts about rip-relative instructions:
  314. *
  315. * - There's always a modrm byte with bit layout "00 reg 101".
  316. * - There's never a SIB byte.
  317. * - The displacement is always 4 bytes.
  318. * - REX.B=1 bit in REX prefix, which normally extends r/m field,
  319. * has no effect on rip-relative mode. It doesn't make modrm byte
  320. * with r/m=101 refer to register 1101 = R13.
  321. */
  322. static void riprel_analyze(struct arch_uprobe *auprobe, struct insn *insn)
  323. {
  324. u8 *cursor;
  325. u8 reg;
  326. u8 reg2;
  327. if (!insn_rip_relative(insn))
  328. return;
  329. /*
  330. * insn_rip_relative() would have decoded rex_prefix, vex_prefix, modrm.
  331. * Clear REX.b bit (extension of MODRM.rm field):
  332. * we want to encode low numbered reg, not r8+.
  333. */
  334. if (insn->rex_prefix.nbytes) {
  335. cursor = auprobe->insn + insn_offset_rex_prefix(insn);
  336. /* REX byte has 0100wrxb layout, clearing REX.b bit */
  337. *cursor &= 0xfe;
  338. }
  339. /*
  340. * Similar treatment for VEX3/EVEX prefix.
  341. * TODO: add XOP treatment when insn decoder supports them
  342. */
  343. if (insn->vex_prefix.nbytes >= 3) {
  344. /*
  345. * vex2: c5 rvvvvLpp (has no b bit)
  346. * vex3/xop: c4/8f rxbmmmmm wvvvvLpp
  347. * evex: 62 rxbR00mm wvvvv1pp zllBVaaa
  348. * Setting VEX3.b (setting because it has inverted meaning).
  349. * Setting EVEX.x since (in non-SIB encoding) EVEX.x
  350. * is the 4th bit of MODRM.rm, and needs the same treatment.
  351. * For VEX3-encoded insns, VEX3.x value has no effect in
  352. * non-SIB encoding, the change is superfluous but harmless.
  353. */
  354. cursor = auprobe->insn + insn_offset_vex_prefix(insn) + 1;
  355. *cursor |= 0x60;
  356. }
  357. /*
  358. * Convert from rip-relative addressing to register-relative addressing
  359. * via a scratch register.
  360. *
  361. * This is tricky since there are insns with modrm byte
  362. * which also use registers not encoded in modrm byte:
  363. * [i]div/[i]mul: implicitly use dx:ax
  364. * shift ops: implicitly use cx
  365. * cmpxchg: implicitly uses ax
  366. * cmpxchg8/16b: implicitly uses dx:ax and bx:cx
  367. * Encoding: 0f c7/1 modrm
  368. * The code below thinks that reg=1 (cx), chooses si as scratch.
  369. * mulx: implicitly uses dx: mulx r/m,r1,r2 does r1:r2 = dx * r/m.
  370. * First appeared in Haswell (BMI2 insn). It is vex-encoded.
  371. * Example where none of bx,cx,dx can be used as scratch reg:
  372. * c4 e2 63 f6 0d disp32 mulx disp32(%rip),%ebx,%ecx
  373. * [v]pcmpistri: implicitly uses cx, xmm0
  374. * [v]pcmpistrm: implicitly uses xmm0
  375. * [v]pcmpestri: implicitly uses ax, dx, cx, xmm0
  376. * [v]pcmpestrm: implicitly uses ax, dx, xmm0
  377. * Evil SSE4.2 string comparison ops from hell.
  378. * maskmovq/[v]maskmovdqu: implicitly uses (ds:rdi) as destination.
  379. * Encoding: 0f f7 modrm, 66 0f f7 modrm, vex-encoded: c5 f9 f7 modrm.
  380. * Store op1, byte-masked by op2 msb's in each byte, to (ds:rdi).
  381. * AMD says it has no 3-operand form (vex.vvvv must be 1111)
  382. * and that it can have only register operands, not mem
  383. * (its modrm byte must have mode=11).
  384. * If these restrictions will ever be lifted,
  385. * we'll need code to prevent selection of di as scratch reg!
  386. *
  387. * Summary: I don't know any insns with modrm byte which
  388. * use SI register implicitly. DI register is used only
  389. * by one insn (maskmovq) and BX register is used
  390. * only by one too (cmpxchg8b).
  391. * BP is stack-segment based (may be a problem?).
  392. * AX, DX, CX are off-limits (many implicit users).
  393. * SP is unusable (it's stack pointer - think about "pop mem";
  394. * also, rsp+disp32 needs sib encoding -> insn length change).
  395. */
  396. reg = MODRM_REG(insn); /* Fetch modrm.reg */
  397. reg2 = 0xff; /* Fetch vex.vvvv */
  398. if (insn->vex_prefix.nbytes)
  399. reg2 = insn->vex_prefix.bytes[2];
  400. /*
  401. * TODO: add XOP vvvv reading.
  402. *
  403. * vex.vvvv field is in bits 6-3, bits are inverted.
  404. * But in 32-bit mode, high-order bit may be ignored.
  405. * Therefore, let's consider only 3 low-order bits.
  406. */
  407. reg2 = ((reg2 >> 3) & 0x7) ^ 0x7;
  408. /*
  409. * Register numbering is ax,cx,dx,bx, sp,bp,si,di, r8..r15.
  410. *
  411. * Choose scratch reg. Order is important: must not select bx
  412. * if we can use si (cmpxchg8b case!)
  413. */
  414. if (reg != 6 && reg2 != 6) {
  415. reg2 = 6;
  416. auprobe->defparam.fixups |= UPROBE_FIX_RIP_SI;
  417. } else if (reg != 7 && reg2 != 7) {
  418. reg2 = 7;
  419. auprobe->defparam.fixups |= UPROBE_FIX_RIP_DI;
  420. /* TODO (paranoia): force maskmovq to not use di */
  421. } else {
  422. reg2 = 3;
  423. auprobe->defparam.fixups |= UPROBE_FIX_RIP_BX;
  424. }
  425. /*
  426. * Point cursor at the modrm byte. The next 4 bytes are the
  427. * displacement. Beyond the displacement, for some instructions,
  428. * is the immediate operand.
  429. */
  430. cursor = auprobe->insn + insn_offset_modrm(insn);
  431. /*
  432. * Change modrm from "00 reg 101" to "10 reg reg2". Example:
  433. * 89 05 disp32 mov %eax,disp32(%rip) becomes
  434. * 89 86 disp32 mov %eax,disp32(%rsi)
  435. */
  436. *cursor = 0x80 | (reg << 3) | reg2;
  437. }
  438. static inline unsigned long *
  439. scratch_reg(struct arch_uprobe *auprobe, struct pt_regs *regs)
  440. {
  441. if (auprobe->defparam.fixups & UPROBE_FIX_RIP_SI)
  442. return &regs->si;
  443. if (auprobe->defparam.fixups & UPROBE_FIX_RIP_DI)
  444. return &regs->di;
  445. return &regs->bx;
  446. }
  447. /*
  448. * If we're emulating a rip-relative instruction, save the contents
  449. * of the scratch register and store the target address in that register.
  450. */
  451. static void riprel_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  452. {
  453. if (auprobe->defparam.fixups & UPROBE_FIX_RIP_MASK) {
  454. struct uprobe_task *utask = current->utask;
  455. unsigned long *sr = scratch_reg(auprobe, regs);
  456. utask->autask.saved_scratch_register = *sr;
  457. *sr = utask->vaddr + auprobe->defparam.ilen;
  458. }
  459. }
  460. static void riprel_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  461. {
  462. if (auprobe->defparam.fixups & UPROBE_FIX_RIP_MASK) {
  463. struct uprobe_task *utask = current->utask;
  464. unsigned long *sr = scratch_reg(auprobe, regs);
  465. *sr = utask->autask.saved_scratch_register;
  466. }
  467. }
  468. #else /* 32-bit: */
  469. /*
  470. * No RIP-relative addressing on 32-bit
  471. */
  472. static void riprel_analyze(struct arch_uprobe *auprobe, struct insn *insn)
  473. {
  474. }
  475. static void riprel_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  476. {
  477. }
  478. static void riprel_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  479. {
  480. }
  481. #endif /* CONFIG_X86_64 */
  482. struct uprobe_xol_ops {
  483. bool (*emulate)(struct arch_uprobe *, struct pt_regs *);
  484. int (*pre_xol)(struct arch_uprobe *, struct pt_regs *);
  485. int (*post_xol)(struct arch_uprobe *, struct pt_regs *);
  486. void (*abort)(struct arch_uprobe *, struct pt_regs *);
  487. };
  488. static inline int sizeof_long(struct pt_regs *regs)
  489. {
  490. /*
  491. * Check registers for mode as in_xxx_syscall() does not apply here.
  492. */
  493. return user_64bit_mode(regs) ? 8 : 4;
  494. }
  495. static int default_pre_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  496. {
  497. riprel_pre_xol(auprobe, regs);
  498. return 0;
  499. }
  500. static int emulate_push_stack(struct pt_regs *regs, unsigned long val)
  501. {
  502. unsigned long new_sp = regs->sp - sizeof_long(regs);
  503. if (copy_to_user((void __user *)new_sp, &val, sizeof_long(regs)))
  504. return -EFAULT;
  505. regs->sp = new_sp;
  506. return 0;
  507. }
  508. /*
  509. * We have to fix things up as follows:
  510. *
  511. * Typically, the new ip is relative to the copied instruction. We need
  512. * to make it relative to the original instruction (FIX_IP). Exceptions
  513. * are return instructions and absolute or indirect jump or call instructions.
  514. *
  515. * If the single-stepped instruction was a call, the return address that
  516. * is atop the stack is the address following the copied instruction. We
  517. * need to make it the address following the original instruction (FIX_CALL).
  518. *
  519. * If the original instruction was a rip-relative instruction such as
  520. * "movl %edx,0xnnnn(%rip)", we have instead executed an equivalent
  521. * instruction using a scratch register -- e.g., "movl %edx,0xnnnn(%rsi)".
  522. * We need to restore the contents of the scratch register
  523. * (FIX_RIP_reg).
  524. */
  525. static int default_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  526. {
  527. struct uprobe_task *utask = current->utask;
  528. riprel_post_xol(auprobe, regs);
  529. if (auprobe->defparam.fixups & UPROBE_FIX_IP) {
  530. long correction = utask->vaddr - utask->xol_vaddr;
  531. regs->ip += correction;
  532. } else if (auprobe->defparam.fixups & UPROBE_FIX_CALL) {
  533. regs->sp += sizeof_long(regs); /* Pop incorrect return address */
  534. if (emulate_push_stack(regs, utask->vaddr + auprobe->defparam.ilen))
  535. return -ERESTART;
  536. }
  537. /* popf; tell the caller to not touch TF */
  538. if (auprobe->defparam.fixups & UPROBE_FIX_SETF)
  539. utask->autask.saved_tf = true;
  540. return 0;
  541. }
  542. static void default_abort_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  543. {
  544. riprel_post_xol(auprobe, regs);
  545. }
  546. static const struct uprobe_xol_ops default_xol_ops = {
  547. .pre_xol = default_pre_xol_op,
  548. .post_xol = default_post_xol_op,
  549. .abort = default_abort_op,
  550. };
  551. static bool branch_is_call(struct arch_uprobe *auprobe)
  552. {
  553. return auprobe->branch.opc1 == 0xe8;
  554. }
  555. #define CASE_COND \
  556. COND(70, 71, XF(OF)) \
  557. COND(72, 73, XF(CF)) \
  558. COND(74, 75, XF(ZF)) \
  559. COND(78, 79, XF(SF)) \
  560. COND(7a, 7b, XF(PF)) \
  561. COND(76, 77, XF(CF) || XF(ZF)) \
  562. COND(7c, 7d, XF(SF) != XF(OF)) \
  563. COND(7e, 7f, XF(ZF) || XF(SF) != XF(OF))
  564. #define COND(op_y, op_n, expr) \
  565. case 0x ## op_y: DO((expr) != 0) \
  566. case 0x ## op_n: DO((expr) == 0)
  567. #define XF(xf) (!!(flags & X86_EFLAGS_ ## xf))
  568. static bool is_cond_jmp_opcode(u8 opcode)
  569. {
  570. switch (opcode) {
  571. #define DO(expr) \
  572. return true;
  573. CASE_COND
  574. #undef DO
  575. default:
  576. return false;
  577. }
  578. }
  579. static bool check_jmp_cond(struct arch_uprobe *auprobe, struct pt_regs *regs)
  580. {
  581. unsigned long flags = regs->flags;
  582. switch (auprobe->branch.opc1) {
  583. #define DO(expr) \
  584. return expr;
  585. CASE_COND
  586. #undef DO
  587. default: /* not a conditional jmp */
  588. return true;
  589. }
  590. }
  591. #undef XF
  592. #undef COND
  593. #undef CASE_COND
  594. static bool branch_emulate_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  595. {
  596. unsigned long new_ip = regs->ip += auprobe->branch.ilen;
  597. unsigned long offs = (long)auprobe->branch.offs;
  598. if (branch_is_call(auprobe)) {
  599. /*
  600. * If it fails we execute this (mangled, see the comment in
  601. * branch_clear_offset) insn out-of-line. In the likely case
  602. * this should trigger the trap, and the probed application
  603. * should die or restart the same insn after it handles the
  604. * signal, arch_uprobe_post_xol() won't be even called.
  605. *
  606. * But there is corner case, see the comment in ->post_xol().
  607. */
  608. if (emulate_push_stack(regs, new_ip))
  609. return false;
  610. } else if (!check_jmp_cond(auprobe, regs)) {
  611. offs = 0;
  612. }
  613. regs->ip = new_ip + offs;
  614. return true;
  615. }
  616. static bool push_emulate_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  617. {
  618. unsigned long *src_ptr = (void *)regs + auprobe->push.reg_offset;
  619. if (emulate_push_stack(regs, *src_ptr))
  620. return false;
  621. regs->ip += auprobe->push.ilen;
  622. return true;
  623. }
  624. static int branch_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  625. {
  626. BUG_ON(!branch_is_call(auprobe));
  627. /*
  628. * We can only get here if branch_emulate_op() failed to push the ret
  629. * address _and_ another thread expanded our stack before the (mangled)
  630. * "call" insn was executed out-of-line. Just restore ->sp and restart.
  631. * We could also restore ->ip and try to call branch_emulate_op() again.
  632. */
  633. regs->sp += sizeof_long(regs);
  634. return -ERESTART;
  635. }
  636. static void branch_clear_offset(struct arch_uprobe *auprobe, struct insn *insn)
  637. {
  638. /*
  639. * Turn this insn into "call 1f; 1:", this is what we will execute
  640. * out-of-line if ->emulate() fails. We only need this to generate
  641. * a trap, so that the probed task receives the correct signal with
  642. * the properly filled siginfo.
  643. *
  644. * But see the comment in ->post_xol(), in the unlikely case it can
  645. * succeed. So we need to ensure that the new ->ip can not fall into
  646. * the non-canonical area and trigger #GP.
  647. *
  648. * We could turn it into (say) "pushf", but then we would need to
  649. * divorce ->insn[] and ->ixol[]. We need to preserve the 1st byte
  650. * of ->insn[] for set_orig_insn().
  651. */
  652. memset(auprobe->insn + insn_offset_immediate(insn),
  653. 0, insn->immediate.nbytes);
  654. }
  655. static const struct uprobe_xol_ops branch_xol_ops = {
  656. .emulate = branch_emulate_op,
  657. .post_xol = branch_post_xol_op,
  658. };
  659. static const struct uprobe_xol_ops push_xol_ops = {
  660. .emulate = push_emulate_op,
  661. };
  662. /* Returns -ENOSYS if branch_xol_ops doesn't handle this insn */
  663. static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
  664. {
  665. u8 opc1 = OPCODE1(insn);
  666. insn_byte_t p;
  667. int i;
  668. switch (opc1) {
  669. case 0xeb: /* jmp 8 */
  670. case 0xe9: /* jmp 32 */
  671. case 0x90: /* prefix* + nop; same as jmp with .offs = 0 */
  672. break;
  673. case 0xe8: /* call relative */
  674. branch_clear_offset(auprobe, insn);
  675. break;
  676. case 0x0f:
  677. if (insn->opcode.nbytes != 2)
  678. return -ENOSYS;
  679. /*
  680. * If it is a "near" conditional jmp, OPCODE2() - 0x10 matches
  681. * OPCODE1() of the "short" jmp which checks the same condition.
  682. */
  683. opc1 = OPCODE2(insn) - 0x10;
  684. default:
  685. if (!is_cond_jmp_opcode(opc1))
  686. return -ENOSYS;
  687. }
  688. /*
  689. * 16-bit overrides such as CALLW (66 e8 nn nn) are not supported.
  690. * Intel and AMD behavior differ in 64-bit mode: Intel ignores 66 prefix.
  691. * No one uses these insns, reject any branch insns with such prefix.
  692. */
  693. for_each_insn_prefix(insn, i, p) {
  694. if (p == 0x66)
  695. return -ENOTSUPP;
  696. }
  697. auprobe->branch.opc1 = opc1;
  698. auprobe->branch.ilen = insn->length;
  699. auprobe->branch.offs = insn->immediate.value;
  700. auprobe->ops = &branch_xol_ops;
  701. return 0;
  702. }
  703. /* Returns -ENOSYS if push_xol_ops doesn't handle this insn */
  704. static int push_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
  705. {
  706. u8 opc1 = OPCODE1(insn), reg_offset = 0;
  707. if (opc1 < 0x50 || opc1 > 0x57)
  708. return -ENOSYS;
  709. if (insn->length > 2)
  710. return -ENOSYS;
  711. if (insn->length == 2) {
  712. /* only support rex_prefix 0x41 (x64 only) */
  713. #ifdef CONFIG_X86_64
  714. if (insn->rex_prefix.nbytes != 1 ||
  715. insn->rex_prefix.bytes[0] != 0x41)
  716. return -ENOSYS;
  717. switch (opc1) {
  718. case 0x50:
  719. reg_offset = offsetof(struct pt_regs, r8);
  720. break;
  721. case 0x51:
  722. reg_offset = offsetof(struct pt_regs, r9);
  723. break;
  724. case 0x52:
  725. reg_offset = offsetof(struct pt_regs, r10);
  726. break;
  727. case 0x53:
  728. reg_offset = offsetof(struct pt_regs, r11);
  729. break;
  730. case 0x54:
  731. reg_offset = offsetof(struct pt_regs, r12);
  732. break;
  733. case 0x55:
  734. reg_offset = offsetof(struct pt_regs, r13);
  735. break;
  736. case 0x56:
  737. reg_offset = offsetof(struct pt_regs, r14);
  738. break;
  739. case 0x57:
  740. reg_offset = offsetof(struct pt_regs, r15);
  741. break;
  742. }
  743. #else
  744. return -ENOSYS;
  745. #endif
  746. } else {
  747. switch (opc1) {
  748. case 0x50:
  749. reg_offset = offsetof(struct pt_regs, ax);
  750. break;
  751. case 0x51:
  752. reg_offset = offsetof(struct pt_regs, cx);
  753. break;
  754. case 0x52:
  755. reg_offset = offsetof(struct pt_regs, dx);
  756. break;
  757. case 0x53:
  758. reg_offset = offsetof(struct pt_regs, bx);
  759. break;
  760. case 0x54:
  761. reg_offset = offsetof(struct pt_regs, sp);
  762. break;
  763. case 0x55:
  764. reg_offset = offsetof(struct pt_regs, bp);
  765. break;
  766. case 0x56:
  767. reg_offset = offsetof(struct pt_regs, si);
  768. break;
  769. case 0x57:
  770. reg_offset = offsetof(struct pt_regs, di);
  771. break;
  772. }
  773. }
  774. auprobe->push.reg_offset = reg_offset;
  775. auprobe->push.ilen = insn->length;
  776. auprobe->ops = &push_xol_ops;
  777. return 0;
  778. }
  779. /**
  780. * arch_uprobe_analyze_insn - instruction analysis including validity and fixups.
  781. * @mm: the probed address space.
  782. * @arch_uprobe: the probepoint information.
  783. * @addr: virtual address at which to install the probepoint
  784. * Return 0 on success or a -ve number on error.
  785. */
  786. int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long addr)
  787. {
  788. struct insn insn;
  789. u8 fix_ip_or_call = UPROBE_FIX_IP;
  790. int ret;
  791. ret = uprobe_init_insn(auprobe, &insn, is_64bit_mm(mm));
  792. if (ret)
  793. return ret;
  794. ret = branch_setup_xol_ops(auprobe, &insn);
  795. if (ret != -ENOSYS)
  796. return ret;
  797. ret = push_setup_xol_ops(auprobe, &insn);
  798. if (ret != -ENOSYS)
  799. return ret;
  800. /*
  801. * Figure out which fixups default_post_xol_op() will need to perform,
  802. * and annotate defparam->fixups accordingly.
  803. */
  804. switch (OPCODE1(&insn)) {
  805. case 0x9d: /* popf */
  806. auprobe->defparam.fixups |= UPROBE_FIX_SETF;
  807. break;
  808. case 0xc3: /* ret or lret -- ip is correct */
  809. case 0xcb:
  810. case 0xc2:
  811. case 0xca:
  812. case 0xea: /* jmp absolute -- ip is correct */
  813. fix_ip_or_call = 0;
  814. break;
  815. case 0x9a: /* call absolute - Fix return addr, not ip */
  816. fix_ip_or_call = UPROBE_FIX_CALL;
  817. break;
  818. case 0xff:
  819. switch (MODRM_REG(&insn)) {
  820. case 2: case 3: /* call or lcall, indirect */
  821. fix_ip_or_call = UPROBE_FIX_CALL;
  822. break;
  823. case 4: case 5: /* jmp or ljmp, indirect */
  824. fix_ip_or_call = 0;
  825. break;
  826. }
  827. /* fall through */
  828. default:
  829. riprel_analyze(auprobe, &insn);
  830. }
  831. auprobe->defparam.ilen = insn.length;
  832. auprobe->defparam.fixups |= fix_ip_or_call;
  833. auprobe->ops = &default_xol_ops;
  834. return 0;
  835. }
  836. /*
  837. * arch_uprobe_pre_xol - prepare to execute out of line.
  838. * @auprobe: the probepoint information.
  839. * @regs: reflects the saved user state of current task.
  840. */
  841. int arch_uprobe_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  842. {
  843. struct uprobe_task *utask = current->utask;
  844. if (auprobe->ops->pre_xol) {
  845. int err = auprobe->ops->pre_xol(auprobe, regs);
  846. if (err)
  847. return err;
  848. }
  849. regs->ip = utask->xol_vaddr;
  850. utask->autask.saved_trap_nr = current->thread.trap_nr;
  851. current->thread.trap_nr = UPROBE_TRAP_NR;
  852. utask->autask.saved_tf = !!(regs->flags & X86_EFLAGS_TF);
  853. regs->flags |= X86_EFLAGS_TF;
  854. if (test_tsk_thread_flag(current, TIF_BLOCKSTEP))
  855. set_task_blockstep(current, false);
  856. return 0;
  857. }
  858. /*
  859. * If xol insn itself traps and generates a signal(Say,
  860. * SIGILL/SIGSEGV/etc), then detect the case where a singlestepped
  861. * instruction jumps back to its own address. It is assumed that anything
  862. * like do_page_fault/do_trap/etc sets thread.trap_nr != -1.
  863. *
  864. * arch_uprobe_pre_xol/arch_uprobe_post_xol save/restore thread.trap_nr,
  865. * arch_uprobe_xol_was_trapped() simply checks that ->trap_nr is not equal to
  866. * UPROBE_TRAP_NR == -1 set by arch_uprobe_pre_xol().
  867. */
  868. bool arch_uprobe_xol_was_trapped(struct task_struct *t)
  869. {
  870. if (t->thread.trap_nr != UPROBE_TRAP_NR)
  871. return true;
  872. return false;
  873. }
  874. /*
  875. * Called after single-stepping. To avoid the SMP problems that can
  876. * occur when we temporarily put back the original opcode to
  877. * single-step, we single-stepped a copy of the instruction.
  878. *
  879. * This function prepares to resume execution after the single-step.
  880. */
  881. int arch_uprobe_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  882. {
  883. struct uprobe_task *utask = current->utask;
  884. bool send_sigtrap = utask->autask.saved_tf;
  885. int err = 0;
  886. WARN_ON_ONCE(current->thread.trap_nr != UPROBE_TRAP_NR);
  887. current->thread.trap_nr = utask->autask.saved_trap_nr;
  888. if (auprobe->ops->post_xol) {
  889. err = auprobe->ops->post_xol(auprobe, regs);
  890. if (err) {
  891. /*
  892. * Restore ->ip for restart or post mortem analysis.
  893. * ->post_xol() must not return -ERESTART unless this
  894. * is really possible.
  895. */
  896. regs->ip = utask->vaddr;
  897. if (err == -ERESTART)
  898. err = 0;
  899. send_sigtrap = false;
  900. }
  901. }
  902. /*
  903. * arch_uprobe_pre_xol() doesn't save the state of TIF_BLOCKSTEP
  904. * so we can get an extra SIGTRAP if we do not clear TF. We need
  905. * to examine the opcode to make it right.
  906. */
  907. if (send_sigtrap)
  908. send_sig(SIGTRAP, current, 0);
  909. if (!utask->autask.saved_tf)
  910. regs->flags &= ~X86_EFLAGS_TF;
  911. return err;
  912. }
  913. /* callback routine for handling exceptions. */
  914. int arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val, void *data)
  915. {
  916. struct die_args *args = data;
  917. struct pt_regs *regs = args->regs;
  918. int ret = NOTIFY_DONE;
  919. /* We are only interested in userspace traps */
  920. if (regs && !user_mode(regs))
  921. return NOTIFY_DONE;
  922. switch (val) {
  923. case DIE_INT3:
  924. if (uprobe_pre_sstep_notifier(regs))
  925. ret = NOTIFY_STOP;
  926. break;
  927. case DIE_DEBUG:
  928. if (uprobe_post_sstep_notifier(regs))
  929. ret = NOTIFY_STOP;
  930. default:
  931. break;
  932. }
  933. return ret;
  934. }
  935. /*
  936. * This function gets called when XOL instruction either gets trapped or
  937. * the thread has a fatal signal. Reset the instruction pointer to its
  938. * probed address for the potential restart or for post mortem analysis.
  939. */
  940. void arch_uprobe_abort_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  941. {
  942. struct uprobe_task *utask = current->utask;
  943. if (auprobe->ops->abort)
  944. auprobe->ops->abort(auprobe, regs);
  945. current->thread.trap_nr = utask->autask.saved_trap_nr;
  946. regs->ip = utask->vaddr;
  947. /* clear TF if it was set by us in arch_uprobe_pre_xol() */
  948. if (!utask->autask.saved_tf)
  949. regs->flags &= ~X86_EFLAGS_TF;
  950. }
  951. static bool __skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
  952. {
  953. if (auprobe->ops->emulate)
  954. return auprobe->ops->emulate(auprobe, regs);
  955. return false;
  956. }
  957. bool arch_uprobe_skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
  958. {
  959. bool ret = __skip_sstep(auprobe, regs);
  960. if (ret && (regs->flags & X86_EFLAGS_TF))
  961. send_sig(SIGTRAP, current, 0);
  962. return ret;
  963. }
  964. unsigned long
  965. arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs)
  966. {
  967. int rasize = sizeof_long(regs), nleft;
  968. unsigned long orig_ret_vaddr = 0; /* clear high bits for 32-bit apps */
  969. if (copy_from_user(&orig_ret_vaddr, (void __user *)regs->sp, rasize))
  970. return -1;
  971. /* check whether address has been already hijacked */
  972. if (orig_ret_vaddr == trampoline_vaddr)
  973. return orig_ret_vaddr;
  974. nleft = copy_to_user((void __user *)regs->sp, &trampoline_vaddr, rasize);
  975. if (likely(!nleft))
  976. return orig_ret_vaddr;
  977. if (nleft != rasize) {
  978. pr_err("return address clobbered: pid=%d, %%sp=%#lx, %%ip=%#lx\n",
  979. current->pid, regs->sp, regs->ip);
  980. force_sig(SIGSEGV, current);
  981. }
  982. return -1;
  983. }
  984. bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx,
  985. struct pt_regs *regs)
  986. {
  987. if (ctx == RP_CHECK_CALL) /* sp was just decremented by "call" insn */
  988. return regs->sp < ret->stack;
  989. else
  990. return regs->sp <= ret->stack;
  991. }