uprobes.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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. int i;
  257. for (i = 0; i < insn->prefixes.nbytes; i++) {
  258. insn_attr_t attr;
  259. attr = inat_get_opcode_attribute(insn->prefixes.bytes[i]);
  260. switch (attr) {
  261. case INAT_MAKE_PREFIX(INAT_PFX_ES):
  262. case INAT_MAKE_PREFIX(INAT_PFX_CS):
  263. case INAT_MAKE_PREFIX(INAT_PFX_DS):
  264. case INAT_MAKE_PREFIX(INAT_PFX_SS):
  265. case INAT_MAKE_PREFIX(INAT_PFX_LOCK):
  266. return true;
  267. }
  268. }
  269. return false;
  270. }
  271. static int uprobe_init_insn(struct arch_uprobe *auprobe, struct insn *insn, bool x86_64)
  272. {
  273. u32 volatile *good_insns;
  274. insn_init(insn, auprobe->insn, sizeof(auprobe->insn), x86_64);
  275. /* has the side-effect of processing the entire instruction */
  276. insn_get_length(insn);
  277. if (!insn_complete(insn))
  278. return -ENOEXEC;
  279. if (is_prefix_bad(insn))
  280. return -ENOTSUPP;
  281. /* We should not singlestep on the exception masking instructions */
  282. if (insn_masking_exception(insn))
  283. return -ENOTSUPP;
  284. if (x86_64)
  285. good_insns = good_insns_64;
  286. else
  287. good_insns = good_insns_32;
  288. if (test_bit(OPCODE1(insn), (unsigned long *)good_insns))
  289. return 0;
  290. if (insn->opcode.nbytes == 2) {
  291. if (test_bit(OPCODE2(insn), (unsigned long *)good_2byte_insns))
  292. return 0;
  293. }
  294. return -ENOTSUPP;
  295. }
  296. #ifdef CONFIG_X86_64
  297. /*
  298. * If arch_uprobe->insn doesn't use rip-relative addressing, return
  299. * immediately. Otherwise, rewrite the instruction so that it accesses
  300. * its memory operand indirectly through a scratch register. Set
  301. * defparam->fixups accordingly. (The contents of the scratch register
  302. * will be saved before we single-step the modified instruction,
  303. * and restored afterward).
  304. *
  305. * We do this because a rip-relative instruction can access only a
  306. * relatively small area (+/- 2 GB from the instruction), and the XOL
  307. * area typically lies beyond that area. At least for instructions
  308. * that store to memory, we can't execute the original instruction
  309. * and "fix things up" later, because the misdirected store could be
  310. * disastrous.
  311. *
  312. * Some useful facts about rip-relative instructions:
  313. *
  314. * - There's always a modrm byte with bit layout "00 reg 101".
  315. * - There's never a SIB byte.
  316. * - The displacement is always 4 bytes.
  317. * - REX.B=1 bit in REX prefix, which normally extends r/m field,
  318. * has no effect on rip-relative mode. It doesn't make modrm byte
  319. * with r/m=101 refer to register 1101 = R13.
  320. */
  321. static void riprel_analyze(struct arch_uprobe *auprobe, struct insn *insn)
  322. {
  323. u8 *cursor;
  324. u8 reg;
  325. u8 reg2;
  326. if (!insn_rip_relative(insn))
  327. return;
  328. /*
  329. * insn_rip_relative() would have decoded rex_prefix, vex_prefix, modrm.
  330. * Clear REX.b bit (extension of MODRM.rm field):
  331. * we want to encode low numbered reg, not r8+.
  332. */
  333. if (insn->rex_prefix.nbytes) {
  334. cursor = auprobe->insn + insn_offset_rex_prefix(insn);
  335. /* REX byte has 0100wrxb layout, clearing REX.b bit */
  336. *cursor &= 0xfe;
  337. }
  338. /*
  339. * Similar treatment for VEX3/EVEX prefix.
  340. * TODO: add XOP treatment when insn decoder supports them
  341. */
  342. if (insn->vex_prefix.nbytes >= 3) {
  343. /*
  344. * vex2: c5 rvvvvLpp (has no b bit)
  345. * vex3/xop: c4/8f rxbmmmmm wvvvvLpp
  346. * evex: 62 rxbR00mm wvvvv1pp zllBVaaa
  347. * Setting VEX3.b (setting because it has inverted meaning).
  348. * Setting EVEX.x since (in non-SIB encoding) EVEX.x
  349. * is the 4th bit of MODRM.rm, and needs the same treatment.
  350. * For VEX3-encoded insns, VEX3.x value has no effect in
  351. * non-SIB encoding, the change is superfluous but harmless.
  352. */
  353. cursor = auprobe->insn + insn_offset_vex_prefix(insn) + 1;
  354. *cursor |= 0x60;
  355. }
  356. /*
  357. * Convert from rip-relative addressing to register-relative addressing
  358. * via a scratch register.
  359. *
  360. * This is tricky since there are insns with modrm byte
  361. * which also use registers not encoded in modrm byte:
  362. * [i]div/[i]mul: implicitly use dx:ax
  363. * shift ops: implicitly use cx
  364. * cmpxchg: implicitly uses ax
  365. * cmpxchg8/16b: implicitly uses dx:ax and bx:cx
  366. * Encoding: 0f c7/1 modrm
  367. * The code below thinks that reg=1 (cx), chooses si as scratch.
  368. * mulx: implicitly uses dx: mulx r/m,r1,r2 does r1:r2 = dx * r/m.
  369. * First appeared in Haswell (BMI2 insn). It is vex-encoded.
  370. * Example where none of bx,cx,dx can be used as scratch reg:
  371. * c4 e2 63 f6 0d disp32 mulx disp32(%rip),%ebx,%ecx
  372. * [v]pcmpistri: implicitly uses cx, xmm0
  373. * [v]pcmpistrm: implicitly uses xmm0
  374. * [v]pcmpestri: implicitly uses ax, dx, cx, xmm0
  375. * [v]pcmpestrm: implicitly uses ax, dx, xmm0
  376. * Evil SSE4.2 string comparison ops from hell.
  377. * maskmovq/[v]maskmovdqu: implicitly uses (ds:rdi) as destination.
  378. * Encoding: 0f f7 modrm, 66 0f f7 modrm, vex-encoded: c5 f9 f7 modrm.
  379. * Store op1, byte-masked by op2 msb's in each byte, to (ds:rdi).
  380. * AMD says it has no 3-operand form (vex.vvvv must be 1111)
  381. * and that it can have only register operands, not mem
  382. * (its modrm byte must have mode=11).
  383. * If these restrictions will ever be lifted,
  384. * we'll need code to prevent selection of di as scratch reg!
  385. *
  386. * Summary: I don't know any insns with modrm byte which
  387. * use SI register implicitly. DI register is used only
  388. * by one insn (maskmovq) and BX register is used
  389. * only by one too (cmpxchg8b).
  390. * BP is stack-segment based (may be a problem?).
  391. * AX, DX, CX are off-limits (many implicit users).
  392. * SP is unusable (it's stack pointer - think about "pop mem";
  393. * also, rsp+disp32 needs sib encoding -> insn length change).
  394. */
  395. reg = MODRM_REG(insn); /* Fetch modrm.reg */
  396. reg2 = 0xff; /* Fetch vex.vvvv */
  397. if (insn->vex_prefix.nbytes)
  398. reg2 = insn->vex_prefix.bytes[2];
  399. /*
  400. * TODO: add XOP vvvv reading.
  401. *
  402. * vex.vvvv field is in bits 6-3, bits are inverted.
  403. * But in 32-bit mode, high-order bit may be ignored.
  404. * Therefore, let's consider only 3 low-order bits.
  405. */
  406. reg2 = ((reg2 >> 3) & 0x7) ^ 0x7;
  407. /*
  408. * Register numbering is ax,cx,dx,bx, sp,bp,si,di, r8..r15.
  409. *
  410. * Choose scratch reg. Order is important: must not select bx
  411. * if we can use si (cmpxchg8b case!)
  412. */
  413. if (reg != 6 && reg2 != 6) {
  414. reg2 = 6;
  415. auprobe->defparam.fixups |= UPROBE_FIX_RIP_SI;
  416. } else if (reg != 7 && reg2 != 7) {
  417. reg2 = 7;
  418. auprobe->defparam.fixups |= UPROBE_FIX_RIP_DI;
  419. /* TODO (paranoia): force maskmovq to not use di */
  420. } else {
  421. reg2 = 3;
  422. auprobe->defparam.fixups |= UPROBE_FIX_RIP_BX;
  423. }
  424. /*
  425. * Point cursor at the modrm byte. The next 4 bytes are the
  426. * displacement. Beyond the displacement, for some instructions,
  427. * is the immediate operand.
  428. */
  429. cursor = auprobe->insn + insn_offset_modrm(insn);
  430. /*
  431. * Change modrm from "00 reg 101" to "10 reg reg2". Example:
  432. * 89 05 disp32 mov %eax,disp32(%rip) becomes
  433. * 89 86 disp32 mov %eax,disp32(%rsi)
  434. */
  435. *cursor = 0x80 | (reg << 3) | reg2;
  436. }
  437. static inline unsigned long *
  438. scratch_reg(struct arch_uprobe *auprobe, struct pt_regs *regs)
  439. {
  440. if (auprobe->defparam.fixups & UPROBE_FIX_RIP_SI)
  441. return &regs->si;
  442. if (auprobe->defparam.fixups & UPROBE_FIX_RIP_DI)
  443. return &regs->di;
  444. return &regs->bx;
  445. }
  446. /*
  447. * If we're emulating a rip-relative instruction, save the contents
  448. * of the scratch register and store the target address in that register.
  449. */
  450. static void riprel_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  451. {
  452. if (auprobe->defparam.fixups & UPROBE_FIX_RIP_MASK) {
  453. struct uprobe_task *utask = current->utask;
  454. unsigned long *sr = scratch_reg(auprobe, regs);
  455. utask->autask.saved_scratch_register = *sr;
  456. *sr = utask->vaddr + auprobe->defparam.ilen;
  457. }
  458. }
  459. static void riprel_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  460. {
  461. if (auprobe->defparam.fixups & UPROBE_FIX_RIP_MASK) {
  462. struct uprobe_task *utask = current->utask;
  463. unsigned long *sr = scratch_reg(auprobe, regs);
  464. *sr = utask->autask.saved_scratch_register;
  465. }
  466. }
  467. #else /* 32-bit: */
  468. /*
  469. * No RIP-relative addressing on 32-bit
  470. */
  471. static void riprel_analyze(struct arch_uprobe *auprobe, struct insn *insn)
  472. {
  473. }
  474. static void riprel_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  475. {
  476. }
  477. static void riprel_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  478. {
  479. }
  480. #endif /* CONFIG_X86_64 */
  481. struct uprobe_xol_ops {
  482. bool (*emulate)(struct arch_uprobe *, struct pt_regs *);
  483. int (*pre_xol)(struct arch_uprobe *, struct pt_regs *);
  484. int (*post_xol)(struct arch_uprobe *, struct pt_regs *);
  485. void (*abort)(struct arch_uprobe *, struct pt_regs *);
  486. };
  487. static inline int sizeof_long(void)
  488. {
  489. return in_ia32_syscall() ? 4 : 8;
  490. }
  491. static int default_pre_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  492. {
  493. riprel_pre_xol(auprobe, regs);
  494. return 0;
  495. }
  496. static int emulate_push_stack(struct pt_regs *regs, unsigned long val)
  497. {
  498. unsigned long new_sp = regs->sp - sizeof_long();
  499. if (copy_to_user((void __user *)new_sp, &val, sizeof_long()))
  500. return -EFAULT;
  501. regs->sp = new_sp;
  502. return 0;
  503. }
  504. /*
  505. * We have to fix things up as follows:
  506. *
  507. * Typically, the new ip is relative to the copied instruction. We need
  508. * to make it relative to the original instruction (FIX_IP). Exceptions
  509. * are return instructions and absolute or indirect jump or call instructions.
  510. *
  511. * If the single-stepped instruction was a call, the return address that
  512. * is atop the stack is the address following the copied instruction. We
  513. * need to make it the address following the original instruction (FIX_CALL).
  514. *
  515. * If the original instruction was a rip-relative instruction such as
  516. * "movl %edx,0xnnnn(%rip)", we have instead executed an equivalent
  517. * instruction using a scratch register -- e.g., "movl %edx,0xnnnn(%rsi)".
  518. * We need to restore the contents of the scratch register
  519. * (FIX_RIP_reg).
  520. */
  521. static int default_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  522. {
  523. struct uprobe_task *utask = current->utask;
  524. riprel_post_xol(auprobe, regs);
  525. if (auprobe->defparam.fixups & UPROBE_FIX_IP) {
  526. long correction = utask->vaddr - utask->xol_vaddr;
  527. regs->ip += correction;
  528. } else if (auprobe->defparam.fixups & UPROBE_FIX_CALL) {
  529. regs->sp += sizeof_long(); /* Pop incorrect return address */
  530. if (emulate_push_stack(regs, utask->vaddr + auprobe->defparam.ilen))
  531. return -ERESTART;
  532. }
  533. /* popf; tell the caller to not touch TF */
  534. if (auprobe->defparam.fixups & UPROBE_FIX_SETF)
  535. utask->autask.saved_tf = true;
  536. return 0;
  537. }
  538. static void default_abort_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  539. {
  540. riprel_post_xol(auprobe, regs);
  541. }
  542. static const struct uprobe_xol_ops default_xol_ops = {
  543. .pre_xol = default_pre_xol_op,
  544. .post_xol = default_post_xol_op,
  545. .abort = default_abort_op,
  546. };
  547. static bool branch_is_call(struct arch_uprobe *auprobe)
  548. {
  549. return auprobe->branch.opc1 == 0xe8;
  550. }
  551. #define CASE_COND \
  552. COND(70, 71, XF(OF)) \
  553. COND(72, 73, XF(CF)) \
  554. COND(74, 75, XF(ZF)) \
  555. COND(78, 79, XF(SF)) \
  556. COND(7a, 7b, XF(PF)) \
  557. COND(76, 77, XF(CF) || XF(ZF)) \
  558. COND(7c, 7d, XF(SF) != XF(OF)) \
  559. COND(7e, 7f, XF(ZF) || XF(SF) != XF(OF))
  560. #define COND(op_y, op_n, expr) \
  561. case 0x ## op_y: DO((expr) != 0) \
  562. case 0x ## op_n: DO((expr) == 0)
  563. #define XF(xf) (!!(flags & X86_EFLAGS_ ## xf))
  564. static bool is_cond_jmp_opcode(u8 opcode)
  565. {
  566. switch (opcode) {
  567. #define DO(expr) \
  568. return true;
  569. CASE_COND
  570. #undef DO
  571. default:
  572. return false;
  573. }
  574. }
  575. static bool check_jmp_cond(struct arch_uprobe *auprobe, struct pt_regs *regs)
  576. {
  577. unsigned long flags = regs->flags;
  578. switch (auprobe->branch.opc1) {
  579. #define DO(expr) \
  580. return expr;
  581. CASE_COND
  582. #undef DO
  583. default: /* not a conditional jmp */
  584. return true;
  585. }
  586. }
  587. #undef XF
  588. #undef COND
  589. #undef CASE_COND
  590. static bool branch_emulate_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  591. {
  592. unsigned long new_ip = regs->ip += auprobe->branch.ilen;
  593. unsigned long offs = (long)auprobe->branch.offs;
  594. if (branch_is_call(auprobe)) {
  595. /*
  596. * If it fails we execute this (mangled, see the comment in
  597. * branch_clear_offset) insn out-of-line. In the likely case
  598. * this should trigger the trap, and the probed application
  599. * should die or restart the same insn after it handles the
  600. * signal, arch_uprobe_post_xol() won't be even called.
  601. *
  602. * But there is corner case, see the comment in ->post_xol().
  603. */
  604. if (emulate_push_stack(regs, new_ip))
  605. return false;
  606. } else if (!check_jmp_cond(auprobe, regs)) {
  607. offs = 0;
  608. }
  609. regs->ip = new_ip + offs;
  610. return true;
  611. }
  612. static bool push_emulate_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  613. {
  614. unsigned long *src_ptr = (void *)regs + auprobe->push.reg_offset;
  615. if (emulate_push_stack(regs, *src_ptr))
  616. return false;
  617. regs->ip += auprobe->push.ilen;
  618. return true;
  619. }
  620. static int branch_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  621. {
  622. BUG_ON(!branch_is_call(auprobe));
  623. /*
  624. * We can only get here if branch_emulate_op() failed to push the ret
  625. * address _and_ another thread expanded our stack before the (mangled)
  626. * "call" insn was executed out-of-line. Just restore ->sp and restart.
  627. * We could also restore ->ip and try to call branch_emulate_op() again.
  628. */
  629. regs->sp += sizeof_long();
  630. return -ERESTART;
  631. }
  632. static void branch_clear_offset(struct arch_uprobe *auprobe, struct insn *insn)
  633. {
  634. /*
  635. * Turn this insn into "call 1f; 1:", this is what we will execute
  636. * out-of-line if ->emulate() fails. We only need this to generate
  637. * a trap, so that the probed task receives the correct signal with
  638. * the properly filled siginfo.
  639. *
  640. * But see the comment in ->post_xol(), in the unlikely case it can
  641. * succeed. So we need to ensure that the new ->ip can not fall into
  642. * the non-canonical area and trigger #GP.
  643. *
  644. * We could turn it into (say) "pushf", but then we would need to
  645. * divorce ->insn[] and ->ixol[]. We need to preserve the 1st byte
  646. * of ->insn[] for set_orig_insn().
  647. */
  648. memset(auprobe->insn + insn_offset_immediate(insn),
  649. 0, insn->immediate.nbytes);
  650. }
  651. static const struct uprobe_xol_ops branch_xol_ops = {
  652. .emulate = branch_emulate_op,
  653. .post_xol = branch_post_xol_op,
  654. };
  655. static const struct uprobe_xol_ops push_xol_ops = {
  656. .emulate = push_emulate_op,
  657. };
  658. /* Returns -ENOSYS if branch_xol_ops doesn't handle this insn */
  659. static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
  660. {
  661. u8 opc1 = OPCODE1(insn);
  662. int i;
  663. switch (opc1) {
  664. case 0xeb: /* jmp 8 */
  665. case 0xe9: /* jmp 32 */
  666. case 0x90: /* prefix* + nop; same as jmp with .offs = 0 */
  667. break;
  668. case 0xe8: /* call relative */
  669. branch_clear_offset(auprobe, insn);
  670. break;
  671. case 0x0f:
  672. if (insn->opcode.nbytes != 2)
  673. return -ENOSYS;
  674. /*
  675. * If it is a "near" conditional jmp, OPCODE2() - 0x10 matches
  676. * OPCODE1() of the "short" jmp which checks the same condition.
  677. */
  678. opc1 = OPCODE2(insn) - 0x10;
  679. default:
  680. if (!is_cond_jmp_opcode(opc1))
  681. return -ENOSYS;
  682. }
  683. /*
  684. * 16-bit overrides such as CALLW (66 e8 nn nn) are not supported.
  685. * Intel and AMD behavior differ in 64-bit mode: Intel ignores 66 prefix.
  686. * No one uses these insns, reject any branch insns with such prefix.
  687. */
  688. for (i = 0; i < insn->prefixes.nbytes; i++) {
  689. if (insn->prefixes.bytes[i] == 0x66)
  690. return -ENOTSUPP;
  691. }
  692. auprobe->branch.opc1 = opc1;
  693. auprobe->branch.ilen = insn->length;
  694. auprobe->branch.offs = insn->immediate.value;
  695. auprobe->ops = &branch_xol_ops;
  696. return 0;
  697. }
  698. /* Returns -ENOSYS if push_xol_ops doesn't handle this insn */
  699. static int push_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
  700. {
  701. u8 opc1 = OPCODE1(insn), reg_offset = 0;
  702. if (opc1 < 0x50 || opc1 > 0x57)
  703. return -ENOSYS;
  704. if (insn->length > 2)
  705. return -ENOSYS;
  706. if (insn->length == 2) {
  707. /* only support rex_prefix 0x41 (x64 only) */
  708. #ifdef CONFIG_X86_64
  709. if (insn->rex_prefix.nbytes != 1 ||
  710. insn->rex_prefix.bytes[0] != 0x41)
  711. return -ENOSYS;
  712. switch (opc1) {
  713. case 0x50:
  714. reg_offset = offsetof(struct pt_regs, r8);
  715. break;
  716. case 0x51:
  717. reg_offset = offsetof(struct pt_regs, r9);
  718. break;
  719. case 0x52:
  720. reg_offset = offsetof(struct pt_regs, r10);
  721. break;
  722. case 0x53:
  723. reg_offset = offsetof(struct pt_regs, r11);
  724. break;
  725. case 0x54:
  726. reg_offset = offsetof(struct pt_regs, r12);
  727. break;
  728. case 0x55:
  729. reg_offset = offsetof(struct pt_regs, r13);
  730. break;
  731. case 0x56:
  732. reg_offset = offsetof(struct pt_regs, r14);
  733. break;
  734. case 0x57:
  735. reg_offset = offsetof(struct pt_regs, r15);
  736. break;
  737. }
  738. #else
  739. return -ENOSYS;
  740. #endif
  741. } else {
  742. switch (opc1) {
  743. case 0x50:
  744. reg_offset = offsetof(struct pt_regs, ax);
  745. break;
  746. case 0x51:
  747. reg_offset = offsetof(struct pt_regs, cx);
  748. break;
  749. case 0x52:
  750. reg_offset = offsetof(struct pt_regs, dx);
  751. break;
  752. case 0x53:
  753. reg_offset = offsetof(struct pt_regs, bx);
  754. break;
  755. case 0x54:
  756. reg_offset = offsetof(struct pt_regs, sp);
  757. break;
  758. case 0x55:
  759. reg_offset = offsetof(struct pt_regs, bp);
  760. break;
  761. case 0x56:
  762. reg_offset = offsetof(struct pt_regs, si);
  763. break;
  764. case 0x57:
  765. reg_offset = offsetof(struct pt_regs, di);
  766. break;
  767. }
  768. }
  769. auprobe->push.reg_offset = reg_offset;
  770. auprobe->push.ilen = insn->length;
  771. auprobe->ops = &push_xol_ops;
  772. return 0;
  773. }
  774. /**
  775. * arch_uprobe_analyze_insn - instruction analysis including validity and fixups.
  776. * @mm: the probed address space.
  777. * @arch_uprobe: the probepoint information.
  778. * @addr: virtual address at which to install the probepoint
  779. * Return 0 on success or a -ve number on error.
  780. */
  781. int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long addr)
  782. {
  783. struct insn insn;
  784. u8 fix_ip_or_call = UPROBE_FIX_IP;
  785. int ret;
  786. ret = uprobe_init_insn(auprobe, &insn, is_64bit_mm(mm));
  787. if (ret)
  788. return ret;
  789. ret = branch_setup_xol_ops(auprobe, &insn);
  790. if (ret != -ENOSYS)
  791. return ret;
  792. ret = push_setup_xol_ops(auprobe, &insn);
  793. if (ret != -ENOSYS)
  794. return ret;
  795. /*
  796. * Figure out which fixups default_post_xol_op() will need to perform,
  797. * and annotate defparam->fixups accordingly.
  798. */
  799. switch (OPCODE1(&insn)) {
  800. case 0x9d: /* popf */
  801. auprobe->defparam.fixups |= UPROBE_FIX_SETF;
  802. break;
  803. case 0xc3: /* ret or lret -- ip is correct */
  804. case 0xcb:
  805. case 0xc2:
  806. case 0xca:
  807. case 0xea: /* jmp absolute -- ip is correct */
  808. fix_ip_or_call = 0;
  809. break;
  810. case 0x9a: /* call absolute - Fix return addr, not ip */
  811. fix_ip_or_call = UPROBE_FIX_CALL;
  812. break;
  813. case 0xff:
  814. switch (MODRM_REG(&insn)) {
  815. case 2: case 3: /* call or lcall, indirect */
  816. fix_ip_or_call = UPROBE_FIX_CALL;
  817. break;
  818. case 4: case 5: /* jmp or ljmp, indirect */
  819. fix_ip_or_call = 0;
  820. break;
  821. }
  822. /* fall through */
  823. default:
  824. riprel_analyze(auprobe, &insn);
  825. }
  826. auprobe->defparam.ilen = insn.length;
  827. auprobe->defparam.fixups |= fix_ip_or_call;
  828. auprobe->ops = &default_xol_ops;
  829. return 0;
  830. }
  831. /*
  832. * arch_uprobe_pre_xol - prepare to execute out of line.
  833. * @auprobe: the probepoint information.
  834. * @regs: reflects the saved user state of current task.
  835. */
  836. int arch_uprobe_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  837. {
  838. struct uprobe_task *utask = current->utask;
  839. if (auprobe->ops->pre_xol) {
  840. int err = auprobe->ops->pre_xol(auprobe, regs);
  841. if (err)
  842. return err;
  843. }
  844. regs->ip = utask->xol_vaddr;
  845. utask->autask.saved_trap_nr = current->thread.trap_nr;
  846. current->thread.trap_nr = UPROBE_TRAP_NR;
  847. utask->autask.saved_tf = !!(regs->flags & X86_EFLAGS_TF);
  848. regs->flags |= X86_EFLAGS_TF;
  849. if (test_tsk_thread_flag(current, TIF_BLOCKSTEP))
  850. set_task_blockstep(current, false);
  851. return 0;
  852. }
  853. /*
  854. * If xol insn itself traps and generates a signal(Say,
  855. * SIGILL/SIGSEGV/etc), then detect the case where a singlestepped
  856. * instruction jumps back to its own address. It is assumed that anything
  857. * like do_page_fault/do_trap/etc sets thread.trap_nr != -1.
  858. *
  859. * arch_uprobe_pre_xol/arch_uprobe_post_xol save/restore thread.trap_nr,
  860. * arch_uprobe_xol_was_trapped() simply checks that ->trap_nr is not equal to
  861. * UPROBE_TRAP_NR == -1 set by arch_uprobe_pre_xol().
  862. */
  863. bool arch_uprobe_xol_was_trapped(struct task_struct *t)
  864. {
  865. if (t->thread.trap_nr != UPROBE_TRAP_NR)
  866. return true;
  867. return false;
  868. }
  869. /*
  870. * Called after single-stepping. To avoid the SMP problems that can
  871. * occur when we temporarily put back the original opcode to
  872. * single-step, we single-stepped a copy of the instruction.
  873. *
  874. * This function prepares to resume execution after the single-step.
  875. */
  876. int arch_uprobe_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  877. {
  878. struct uprobe_task *utask = current->utask;
  879. bool send_sigtrap = utask->autask.saved_tf;
  880. int err = 0;
  881. WARN_ON_ONCE(current->thread.trap_nr != UPROBE_TRAP_NR);
  882. current->thread.trap_nr = utask->autask.saved_trap_nr;
  883. if (auprobe->ops->post_xol) {
  884. err = auprobe->ops->post_xol(auprobe, regs);
  885. if (err) {
  886. /*
  887. * Restore ->ip for restart or post mortem analysis.
  888. * ->post_xol() must not return -ERESTART unless this
  889. * is really possible.
  890. */
  891. regs->ip = utask->vaddr;
  892. if (err == -ERESTART)
  893. err = 0;
  894. send_sigtrap = false;
  895. }
  896. }
  897. /*
  898. * arch_uprobe_pre_xol() doesn't save the state of TIF_BLOCKSTEP
  899. * so we can get an extra SIGTRAP if we do not clear TF. We need
  900. * to examine the opcode to make it right.
  901. */
  902. if (send_sigtrap)
  903. send_sig(SIGTRAP, current, 0);
  904. if (!utask->autask.saved_tf)
  905. regs->flags &= ~X86_EFLAGS_TF;
  906. return err;
  907. }
  908. /* callback routine for handling exceptions. */
  909. int arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val, void *data)
  910. {
  911. struct die_args *args = data;
  912. struct pt_regs *regs = args->regs;
  913. int ret = NOTIFY_DONE;
  914. /* We are only interested in userspace traps */
  915. if (regs && !user_mode(regs))
  916. return NOTIFY_DONE;
  917. switch (val) {
  918. case DIE_INT3:
  919. if (uprobe_pre_sstep_notifier(regs))
  920. ret = NOTIFY_STOP;
  921. break;
  922. case DIE_DEBUG:
  923. if (uprobe_post_sstep_notifier(regs))
  924. ret = NOTIFY_STOP;
  925. default:
  926. break;
  927. }
  928. return ret;
  929. }
  930. /*
  931. * This function gets called when XOL instruction either gets trapped or
  932. * the thread has a fatal signal. Reset the instruction pointer to its
  933. * probed address for the potential restart or for post mortem analysis.
  934. */
  935. void arch_uprobe_abort_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  936. {
  937. struct uprobe_task *utask = current->utask;
  938. if (auprobe->ops->abort)
  939. auprobe->ops->abort(auprobe, regs);
  940. current->thread.trap_nr = utask->autask.saved_trap_nr;
  941. regs->ip = utask->vaddr;
  942. /* clear TF if it was set by us in arch_uprobe_pre_xol() */
  943. if (!utask->autask.saved_tf)
  944. regs->flags &= ~X86_EFLAGS_TF;
  945. }
  946. static bool __skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
  947. {
  948. if (auprobe->ops->emulate)
  949. return auprobe->ops->emulate(auprobe, regs);
  950. return false;
  951. }
  952. bool arch_uprobe_skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
  953. {
  954. bool ret = __skip_sstep(auprobe, regs);
  955. if (ret && (regs->flags & X86_EFLAGS_TF))
  956. send_sig(SIGTRAP, current, 0);
  957. return ret;
  958. }
  959. unsigned long
  960. arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs)
  961. {
  962. int rasize = sizeof_long(), nleft;
  963. unsigned long orig_ret_vaddr = 0; /* clear high bits for 32-bit apps */
  964. if (copy_from_user(&orig_ret_vaddr, (void __user *)regs->sp, rasize))
  965. return -1;
  966. /* check whether address has been already hijacked */
  967. if (orig_ret_vaddr == trampoline_vaddr)
  968. return orig_ret_vaddr;
  969. nleft = copy_to_user((void __user *)regs->sp, &trampoline_vaddr, rasize);
  970. if (likely(!nleft))
  971. return orig_ret_vaddr;
  972. if (nleft != rasize) {
  973. pr_err("return address clobbered: pid=%d, %%sp=%#lx, %%ip=%#lx\n",
  974. current->pid, regs->sp, regs->ip);
  975. force_sig(SIGSEGV, current);
  976. }
  977. return -1;
  978. }
  979. bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx,
  980. struct pt_regs *regs)
  981. {
  982. if (ctx == RP_CHECK_CALL) /* sp was just decremented by "call" insn */
  983. return regs->sp < ret->stack;
  984. else
  985. return regs->sp <= ret->stack;
  986. }