kprobes.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. /*
  2. * Kernel Probes (KProbes)
  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, 2002, 2004
  19. *
  20. * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
  21. * Probes initial implementation ( includes contributions from
  22. * Rusty Russell).
  23. * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
  24. * interface to access function arguments.
  25. * 2004-Oct Jim Keniston <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
  26. * <prasanna@in.ibm.com> adapted for x86_64 from i386.
  27. * 2005-Mar Roland McGrath <roland@redhat.com>
  28. * Fixed to handle %rip-relative addressing mode correctly.
  29. * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
  30. * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
  31. * <prasanna@in.ibm.com> added function-return probes.
  32. * 2005-May Rusty Lynch <rusty.lynch@intel.com>
  33. * Added function return probes functionality
  34. * 2006-Feb Masami Hiramatsu <hiramatu@sdl.hitachi.co.jp> added
  35. * kprobe-booster and kretprobe-booster for i386.
  36. * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com> added kprobe-booster
  37. * and kretprobe-booster for x86-64
  38. * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com>, Arjan van de Ven
  39. * <arjan@infradead.org> and Jim Keniston <jkenisto@us.ibm.com>
  40. * unified x86 kprobes code.
  41. */
  42. #include <linux/kprobes.h>
  43. #include <linux/ptrace.h>
  44. #include <linux/string.h>
  45. #include <linux/slab.h>
  46. #include <linux/preempt.h>
  47. #include <linux/module.h>
  48. #include <linux/kdebug.h>
  49. #include <asm/cacheflush.h>
  50. #include <asm/desc.h>
  51. #include <asm/pgtable.h>
  52. #include <asm/uaccess.h>
  53. #include <asm/alternative.h>
  54. void jprobe_return_end(void);
  55. DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  56. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  57. #ifdef CONFIG_X86_64
  58. #define stack_addr(regs) ((unsigned long *)regs->sp)
  59. #else
  60. /*
  61. * "&regs->sp" looks wrong, but it's correct for x86_32. x86_32 CPUs
  62. * don't save the ss and esp registers if the CPU is already in kernel
  63. * mode when it traps. So for kprobes, regs->sp and regs->ss are not
  64. * the [nonexistent] saved stack pointer and ss register, but rather
  65. * the top 8 bytes of the pre-int3 stack. So &regs->sp happens to
  66. * point to the top of the pre-int3 stack.
  67. */
  68. #define stack_addr(regs) ((unsigned long *)&regs->sp)
  69. #endif
  70. #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
  71. (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
  72. (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
  73. (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
  74. (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
  75. << (row % 32))
  76. /*
  77. * Undefined/reserved opcodes, conditional jump, Opcode Extension
  78. * Groups, and some special opcodes can not boost.
  79. */
  80. static const u32 twobyte_is_boostable[256 / 32] = {
  81. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  82. /* ---------------------------------------------- */
  83. W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */
  84. W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 10 */
  85. W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */
  86. W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
  87. W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
  88. W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
  89. W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */
  90. W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
  91. W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */
  92. W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
  93. W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */
  94. W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */
  95. W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
  96. W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */
  97. W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */
  98. W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */
  99. /* ----------------------------------------------- */
  100. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  101. };
  102. static const u32 onebyte_has_modrm[256 / 32] = {
  103. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  104. /* ----------------------------------------------- */
  105. W(0x00, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* 00 */
  106. W(0x10, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) , /* 10 */
  107. W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* 20 */
  108. W(0x30, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) , /* 30 */
  109. W(0x40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 40 */
  110. W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
  111. W(0x60, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0) | /* 60 */
  112. W(0x70, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 70 */
  113. W(0x80, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */
  114. W(0x90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 90 */
  115. W(0xa0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* a0 */
  116. W(0xb0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* b0 */
  117. W(0xc0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0) | /* c0 */
  118. W(0xd0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */
  119. W(0xe0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* e0 */
  120. W(0xf0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) /* f0 */
  121. /* ----------------------------------------------- */
  122. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  123. };
  124. static const u32 twobyte_has_modrm[256 / 32] = {
  125. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  126. /* ----------------------------------------------- */
  127. W(0x00, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1) | /* 0f */
  128. W(0x10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0) , /* 1f */
  129. W(0x20, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* 2f */
  130. W(0x30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 3f */
  131. W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 4f */
  132. W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 5f */
  133. W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 6f */
  134. W(0x70, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1) , /* 7f */
  135. W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 8f */
  136. W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 9f */
  137. W(0xa0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) | /* af */
  138. W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1) , /* bf */
  139. W(0xc0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0) | /* cf */
  140. W(0xd0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* df */
  141. W(0xe0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* ef */
  142. W(0xf0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0) /* ff */
  143. /* ----------------------------------------------- */
  144. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  145. };
  146. #undef W
  147. struct kretprobe_blackpoint kretprobe_blacklist[] = {
  148. {"__switch_to", }, /* This function switches only current task, but
  149. doesn't switch kernel stack.*/
  150. {NULL, NULL} /* Terminator */
  151. };
  152. const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
  153. /* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
  154. static __always_inline void set_jmp_op(void *from, void *to)
  155. {
  156. struct __arch_jmp_op {
  157. char op;
  158. s32 raddr;
  159. } __attribute__((packed)) * jop;
  160. jop = (struct __arch_jmp_op *)from;
  161. jop->raddr = (s32)((long)(to) - ((long)(from) + 5));
  162. jop->op = RELATIVEJUMP_INSTRUCTION;
  163. }
  164. /*
  165. * Returns non-zero if opcode is boostable.
  166. * RIP relative instructions are adjusted at copying time in 64 bits mode
  167. */
  168. static __always_inline int can_boost(kprobe_opcode_t *opcodes)
  169. {
  170. kprobe_opcode_t opcode;
  171. kprobe_opcode_t *orig_opcodes = opcodes;
  172. retry:
  173. if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
  174. return 0;
  175. opcode = *(opcodes++);
  176. /* 2nd-byte opcode */
  177. if (opcode == 0x0f) {
  178. if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
  179. return 0;
  180. return test_bit(*opcodes,
  181. (unsigned long *)twobyte_is_boostable);
  182. }
  183. switch (opcode & 0xf0) {
  184. #ifdef CONFIG_X86_64
  185. case 0x40:
  186. goto retry; /* REX prefix is boostable */
  187. #endif
  188. case 0x60:
  189. if (0x63 < opcode && opcode < 0x67)
  190. goto retry; /* prefixes */
  191. /* can't boost Address-size override and bound */
  192. return (opcode != 0x62 && opcode != 0x67);
  193. case 0x70:
  194. return 0; /* can't boost conditional jump */
  195. case 0xc0:
  196. /* can't boost software-interruptions */
  197. return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
  198. case 0xd0:
  199. /* can boost AA* and XLAT */
  200. return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
  201. case 0xe0:
  202. /* can boost in/out and absolute jmps */
  203. return ((opcode & 0x04) || opcode == 0xea);
  204. case 0xf0:
  205. if ((opcode & 0x0c) == 0 && opcode != 0xf1)
  206. goto retry; /* lock/rep(ne) prefix */
  207. /* clear and set flags are boostable */
  208. return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
  209. default:
  210. /* segment override prefixes are boostable */
  211. if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
  212. goto retry; /* prefixes */
  213. /* CS override prefix and call are not boostable */
  214. return (opcode != 0x2e && opcode != 0x9a);
  215. }
  216. }
  217. /*
  218. * Returns non-zero if opcode modifies the interrupt flag.
  219. */
  220. static int __kprobes is_IF_modifier(kprobe_opcode_t *insn)
  221. {
  222. switch (*insn) {
  223. case 0xfa: /* cli */
  224. case 0xfb: /* sti */
  225. case 0xcf: /* iret/iretd */
  226. case 0x9d: /* popf/popfd */
  227. return 1;
  228. }
  229. #ifdef CONFIG_X86_64
  230. /*
  231. * on 64 bit x86, 0x40-0x4f are prefixes so we need to look
  232. * at the next byte instead.. but of course not recurse infinitely
  233. */
  234. if (*insn >= 0x40 && *insn <= 0x4f)
  235. return is_IF_modifier(++insn);
  236. #endif
  237. return 0;
  238. }
  239. #ifdef CONFIG_X86_64
  240. /*
  241. * Adjust the displacement if the instruction uses the %rip-relative
  242. * addressing mode.
  243. * If it does, Return the address of the 32-bit displacement word.
  244. * If not, return null.
  245. */
  246. static void __kprobes fix_riprel(struct kprobe *p)
  247. {
  248. u8 *insn = p->ainsn.insn;
  249. s64 disp;
  250. int need_modrm;
  251. /* Skip legacy instruction prefixes. */
  252. while (1) {
  253. switch (*insn) {
  254. case 0x66:
  255. case 0x67:
  256. case 0x2e:
  257. case 0x3e:
  258. case 0x26:
  259. case 0x64:
  260. case 0x65:
  261. case 0x36:
  262. case 0xf0:
  263. case 0xf3:
  264. case 0xf2:
  265. ++insn;
  266. continue;
  267. }
  268. break;
  269. }
  270. /* Skip REX instruction prefix. */
  271. if ((*insn & 0xf0) == 0x40)
  272. ++insn;
  273. if (*insn == 0x0f) {
  274. /* Two-byte opcode. */
  275. ++insn;
  276. need_modrm = test_bit(*insn,
  277. (unsigned long *)twobyte_has_modrm);
  278. } else
  279. /* One-byte opcode. */
  280. need_modrm = test_bit(*insn,
  281. (unsigned long *)onebyte_has_modrm);
  282. if (need_modrm) {
  283. u8 modrm = *++insn;
  284. if ((modrm & 0xc7) == 0x05) {
  285. /* %rip+disp32 addressing mode */
  286. /* Displacement follows ModRM byte. */
  287. ++insn;
  288. /*
  289. * The copied instruction uses the %rip-relative
  290. * addressing mode. Adjust the displacement for the
  291. * difference between the original location of this
  292. * instruction and the location of the copy that will
  293. * actually be run. The tricky bit here is making sure
  294. * that the sign extension happens correctly in this
  295. * calculation, since we need a signed 32-bit result to
  296. * be sign-extended to 64 bits when it's added to the
  297. * %rip value and yield the same 64-bit result that the
  298. * sign-extension of the original signed 32-bit
  299. * displacement would have given.
  300. */
  301. disp = (u8 *) p->addr + *((s32 *) insn) -
  302. (u8 *) p->ainsn.insn;
  303. BUG_ON((s64) (s32) disp != disp); /* Sanity check. */
  304. *(s32 *)insn = (s32) disp;
  305. }
  306. }
  307. }
  308. #endif
  309. static void __kprobes arch_copy_kprobe(struct kprobe *p)
  310. {
  311. memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
  312. #ifdef CONFIG_X86_64
  313. fix_riprel(p);
  314. #endif
  315. if (can_boost(p->addr))
  316. p->ainsn.boostable = 0;
  317. else
  318. p->ainsn.boostable = -1;
  319. p->opcode = *p->addr;
  320. }
  321. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  322. {
  323. /* insn: must be on special executable page on x86. */
  324. p->ainsn.insn = get_insn_slot();
  325. if (!p->ainsn.insn)
  326. return -ENOMEM;
  327. arch_copy_kprobe(p);
  328. return 0;
  329. }
  330. void __kprobes arch_arm_kprobe(struct kprobe *p)
  331. {
  332. text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
  333. }
  334. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  335. {
  336. text_poke(p->addr, &p->opcode, 1);
  337. }
  338. void __kprobes arch_remove_kprobe(struct kprobe *p)
  339. {
  340. mutex_lock(&kprobe_mutex);
  341. free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
  342. mutex_unlock(&kprobe_mutex);
  343. }
  344. static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
  345. {
  346. kcb->prev_kprobe.kp = kprobe_running();
  347. kcb->prev_kprobe.status = kcb->kprobe_status;
  348. kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
  349. kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
  350. }
  351. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  352. {
  353. __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
  354. kcb->kprobe_status = kcb->prev_kprobe.status;
  355. kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
  356. kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
  357. }
  358. static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  359. struct kprobe_ctlblk *kcb)
  360. {
  361. __get_cpu_var(current_kprobe) = p;
  362. kcb->kprobe_saved_flags = kcb->kprobe_old_flags
  363. = (regs->flags & (TF_MASK | IF_MASK));
  364. if (is_IF_modifier(p->ainsn.insn))
  365. kcb->kprobe_saved_flags &= ~IF_MASK;
  366. }
  367. static __always_inline void clear_btf(void)
  368. {
  369. if (test_thread_flag(TIF_DEBUGCTLMSR))
  370. wrmsr(MSR_IA32_DEBUGCTLMSR, 0, 0);
  371. }
  372. static __always_inline void restore_btf(void)
  373. {
  374. if (test_thread_flag(TIF_DEBUGCTLMSR))
  375. wrmsr(MSR_IA32_DEBUGCTLMSR, current->thread.debugctlmsr, 0);
  376. }
  377. static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  378. {
  379. clear_btf();
  380. regs->flags |= TF_MASK;
  381. regs->flags &= ~IF_MASK;
  382. /*single step inline if the instruction is an int3*/
  383. if (p->opcode == BREAKPOINT_INSTRUCTION)
  384. regs->ip = (unsigned long)p->addr;
  385. else
  386. regs->ip = (unsigned long)p->ainsn.insn;
  387. }
  388. /* Called with kretprobe_lock held */
  389. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  390. struct pt_regs *regs)
  391. {
  392. unsigned long *sara = stack_addr(regs);
  393. ri->ret_addr = (kprobe_opcode_t *) *sara;
  394. /* Replace the return addr with trampoline addr */
  395. *sara = (unsigned long) &kretprobe_trampoline;
  396. }
  397. /*
  398. * Interrupts are disabled on entry as trap3 is an interrupt gate and they
  399. * remain disabled thorough out this function.
  400. */
  401. static int __kprobes kprobe_handler(struct pt_regs *regs)
  402. {
  403. struct kprobe *p;
  404. int ret = 0;
  405. kprobe_opcode_t *addr;
  406. struct kprobe_ctlblk *kcb;
  407. addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
  408. /*
  409. * We don't want to be preempted for the entire
  410. * duration of kprobe processing
  411. */
  412. preempt_disable();
  413. kcb = get_kprobe_ctlblk();
  414. /* Check we're not actually recursing */
  415. if (kprobe_running()) {
  416. p = get_kprobe(addr);
  417. if (p) {
  418. if (kcb->kprobe_status == KPROBE_HIT_SS &&
  419. *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
  420. regs->flags &= ~TF_MASK;
  421. regs->flags |= kcb->kprobe_saved_flags;
  422. goto no_kprobe;
  423. #ifdef CONFIG_X86_64
  424. } else if (kcb->kprobe_status == KPROBE_HIT_SSDONE) {
  425. /* TODO: Provide re-entrancy from
  426. * post_kprobes_handler() and avoid exception
  427. * stack corruption while single-stepping on
  428. * the instruction of the new probe.
  429. */
  430. arch_disarm_kprobe(p);
  431. regs->ip = (unsigned long)p->addr;
  432. reset_current_kprobe();
  433. return 1;
  434. #endif
  435. }
  436. /* We have reentered the kprobe_handler(), since
  437. * another probe was hit while within the handler.
  438. * We here save the original kprobes variables and
  439. * just single step on the instruction of the new probe
  440. * without calling any user handlers.
  441. */
  442. save_previous_kprobe(kcb);
  443. set_current_kprobe(p, regs, kcb);
  444. kprobes_inc_nmissed_count(p);
  445. prepare_singlestep(p, regs);
  446. kcb->kprobe_status = KPROBE_REENTER;
  447. return 1;
  448. } else {
  449. if (*addr != BREAKPOINT_INSTRUCTION) {
  450. /* The breakpoint instruction was removed by
  451. * another cpu right after we hit, no further
  452. * handling of this interrupt is appropriate
  453. */
  454. regs->ip = (unsigned long)addr;
  455. ret = 1;
  456. goto no_kprobe;
  457. }
  458. p = __get_cpu_var(current_kprobe);
  459. if (p->break_handler && p->break_handler(p, regs))
  460. goto ss_probe;
  461. }
  462. goto no_kprobe;
  463. }
  464. p = get_kprobe(addr);
  465. if (!p) {
  466. if (*addr != BREAKPOINT_INSTRUCTION) {
  467. /*
  468. * The breakpoint instruction was removed right
  469. * after we hit it. Another cpu has removed
  470. * either a probepoint or a debugger breakpoint
  471. * at this address. In either case, no further
  472. * handling of this interrupt is appropriate.
  473. * Back up over the (now missing) int3 and run
  474. * the original instruction.
  475. */
  476. regs->ip = (unsigned long)addr;
  477. ret = 1;
  478. }
  479. /* Not one of ours: let kernel handle it */
  480. goto no_kprobe;
  481. }
  482. set_current_kprobe(p, regs, kcb);
  483. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  484. if (p->pre_handler && p->pre_handler(p, regs))
  485. /* handler has already set things up, so skip ss setup */
  486. return 1;
  487. ss_probe:
  488. #if !defined(CONFIG_PREEMPT) || defined(CONFIG_PM)
  489. if (p->ainsn.boostable == 1 && !p->post_handler) {
  490. /* Boost up -- we can execute copied instructions directly */
  491. reset_current_kprobe();
  492. regs->ip = (unsigned long)p->ainsn.insn;
  493. preempt_enable_no_resched();
  494. return 1;
  495. }
  496. #endif
  497. prepare_singlestep(p, regs);
  498. kcb->kprobe_status = KPROBE_HIT_SS;
  499. return 1;
  500. no_kprobe:
  501. preempt_enable_no_resched();
  502. return ret;
  503. }
  504. /*
  505. * When a retprobed function returns, this code saves registers and
  506. * calls trampoline_handler() runs, which calls the kretprobe's handler.
  507. */
  508. void __kprobes kretprobe_trampoline_holder(void)
  509. {
  510. asm volatile (
  511. ".global kretprobe_trampoline\n"
  512. "kretprobe_trampoline: \n"
  513. #ifdef CONFIG_X86_64
  514. /* We don't bother saving the ss register */
  515. " pushq %rsp\n"
  516. " pushfq\n"
  517. /*
  518. * Skip cs, ip, orig_ax.
  519. * trampoline_handler() will plug in these values
  520. */
  521. " subq $24, %rsp\n"
  522. " pushq %rdi\n"
  523. " pushq %rsi\n"
  524. " pushq %rdx\n"
  525. " pushq %rcx\n"
  526. " pushq %rax\n"
  527. " pushq %r8\n"
  528. " pushq %r9\n"
  529. " pushq %r10\n"
  530. " pushq %r11\n"
  531. " pushq %rbx\n"
  532. " pushq %rbp\n"
  533. " pushq %r12\n"
  534. " pushq %r13\n"
  535. " pushq %r14\n"
  536. " pushq %r15\n"
  537. " movq %rsp, %rdi\n"
  538. " call trampoline_handler\n"
  539. /* Replace saved sp with true return address. */
  540. " movq %rax, 152(%rsp)\n"
  541. " popq %r15\n"
  542. " popq %r14\n"
  543. " popq %r13\n"
  544. " popq %r12\n"
  545. " popq %rbp\n"
  546. " popq %rbx\n"
  547. " popq %r11\n"
  548. " popq %r10\n"
  549. " popq %r9\n"
  550. " popq %r8\n"
  551. " popq %rax\n"
  552. " popq %rcx\n"
  553. " popq %rdx\n"
  554. " popq %rsi\n"
  555. " popq %rdi\n"
  556. /* Skip orig_ax, ip, cs */
  557. " addq $24, %rsp\n"
  558. " popfq\n"
  559. #else
  560. " pushf\n"
  561. /*
  562. * Skip cs, ip, orig_ax.
  563. * trampoline_handler() will plug in these values
  564. */
  565. " subl $12, %esp\n"
  566. " pushl %fs\n"
  567. " pushl %ds\n"
  568. " pushl %es\n"
  569. " pushl %eax\n"
  570. " pushl %ebp\n"
  571. " pushl %edi\n"
  572. " pushl %esi\n"
  573. " pushl %edx\n"
  574. " pushl %ecx\n"
  575. " pushl %ebx\n"
  576. " movl %esp, %eax\n"
  577. " call trampoline_handler\n"
  578. /* Move flags to cs */
  579. " movl 52(%esp), %edx\n"
  580. " movl %edx, 48(%esp)\n"
  581. /* Replace saved flags with true return address. */
  582. " movl %eax, 52(%esp)\n"
  583. " popl %ebx\n"
  584. " popl %ecx\n"
  585. " popl %edx\n"
  586. " popl %esi\n"
  587. " popl %edi\n"
  588. " popl %ebp\n"
  589. " popl %eax\n"
  590. /* Skip ip, orig_ax, es, ds, fs */
  591. " addl $20, %esp\n"
  592. " popf\n"
  593. #endif
  594. " ret\n");
  595. }
  596. /*
  597. * Called from kretprobe_trampoline
  598. */
  599. void * __kprobes trampoline_handler(struct pt_regs *regs)
  600. {
  601. struct kretprobe_instance *ri = NULL;
  602. struct hlist_head *head, empty_rp;
  603. struct hlist_node *node, *tmp;
  604. unsigned long flags, orig_ret_address = 0;
  605. unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
  606. INIT_HLIST_HEAD(&empty_rp);
  607. spin_lock_irqsave(&kretprobe_lock, flags);
  608. head = kretprobe_inst_table_head(current);
  609. /* fixup registers */
  610. #ifdef CONFIG_X86_64
  611. regs->cs = __KERNEL_CS;
  612. #else
  613. regs->cs = __KERNEL_CS | get_kernel_rpl();
  614. #endif
  615. regs->ip = trampoline_address;
  616. regs->orig_ax = ~0UL;
  617. /*
  618. * It is possible to have multiple instances associated with a given
  619. * task either because multiple functions in the call path have
  620. * return probes installed on them, and/or more then one
  621. * return probe was registered for a target function.
  622. *
  623. * We can handle this because:
  624. * - instances are always pushed into the head of the list
  625. * - when multiple return probes are registered for the same
  626. * function, the (chronologically) first instance's ret_addr
  627. * will be the real return address, and all the rest will
  628. * point to kretprobe_trampoline.
  629. */
  630. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  631. if (ri->task != current)
  632. /* another task is sharing our hash bucket */
  633. continue;
  634. if (ri->rp && ri->rp->handler) {
  635. __get_cpu_var(current_kprobe) = &ri->rp->kp;
  636. get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
  637. ri->rp->handler(ri, regs);
  638. __get_cpu_var(current_kprobe) = NULL;
  639. }
  640. orig_ret_address = (unsigned long)ri->ret_addr;
  641. recycle_rp_inst(ri, &empty_rp);
  642. if (orig_ret_address != trampoline_address)
  643. /*
  644. * This is the real return address. Any other
  645. * instances associated with this task are for
  646. * other calls deeper on the call stack
  647. */
  648. break;
  649. }
  650. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  651. spin_unlock_irqrestore(&kretprobe_lock, flags);
  652. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  653. hlist_del(&ri->hlist);
  654. kfree(ri);
  655. }
  656. return (void *)orig_ret_address;
  657. }
  658. /*
  659. * Called after single-stepping. p->addr is the address of the
  660. * instruction whose first byte has been replaced by the "int 3"
  661. * instruction. To avoid the SMP problems that can occur when we
  662. * temporarily put back the original opcode to single-step, we
  663. * single-stepped a copy of the instruction. The address of this
  664. * copy is p->ainsn.insn.
  665. *
  666. * This function prepares to return from the post-single-step
  667. * interrupt. We have to fix up the stack as follows:
  668. *
  669. * 0) Except in the case of absolute or indirect jump or call instructions,
  670. * the new ip is relative to the copied instruction. We need to make
  671. * it relative to the original instruction.
  672. *
  673. * 1) If the single-stepped instruction was pushfl, then the TF and IF
  674. * flags are set in the just-pushed flags, and may need to be cleared.
  675. *
  676. * 2) If the single-stepped instruction was a call, the return address
  677. * that is atop the stack is the address following the copied instruction.
  678. * We need to make it the address following the original instruction.
  679. *
  680. * If this is the first time we've single-stepped the instruction at
  681. * this probepoint, and the instruction is boostable, boost it: add a
  682. * jump instruction after the copied instruction, that jumps to the next
  683. * instruction after the probepoint.
  684. */
  685. static void __kprobes resume_execution(struct kprobe *p,
  686. struct pt_regs *regs, struct kprobe_ctlblk *kcb)
  687. {
  688. unsigned long *tos = stack_addr(regs);
  689. unsigned long copy_ip = (unsigned long)p->ainsn.insn;
  690. unsigned long orig_ip = (unsigned long)p->addr;
  691. kprobe_opcode_t *insn = p->ainsn.insn;
  692. #ifdef CONFIG_X86_64
  693. /*skip the REX prefix*/
  694. if (*insn >= 0x40 && *insn <= 0x4f)
  695. insn++;
  696. #endif
  697. regs->flags &= ~TF_MASK;
  698. switch (*insn) {
  699. case 0x9c: /* pushfl */
  700. *tos &= ~(TF_MASK | IF_MASK);
  701. *tos |= kcb->kprobe_old_flags;
  702. break;
  703. case 0xc2: /* iret/ret/lret */
  704. case 0xc3:
  705. case 0xca:
  706. case 0xcb:
  707. case 0xcf:
  708. case 0xea: /* jmp absolute -- ip is correct */
  709. /* ip is already adjusted, no more changes required */
  710. p->ainsn.boostable = 1;
  711. goto no_change;
  712. case 0xe8: /* call relative - Fix return addr */
  713. *tos = orig_ip + (*tos - copy_ip);
  714. break;
  715. #ifndef CONFIG_X86_64
  716. case 0x9a: /* call absolute -- same as call absolute, indirect */
  717. *tos = orig_ip + (*tos - copy_ip);
  718. goto no_change;
  719. #endif
  720. case 0xff:
  721. if ((insn[1] & 0x30) == 0x10) {
  722. /*
  723. * call absolute, indirect
  724. * Fix return addr; ip is correct.
  725. * But this is not boostable
  726. */
  727. *tos = orig_ip + (*tos - copy_ip);
  728. goto no_change;
  729. } else if (((insn[1] & 0x31) == 0x20) ||
  730. ((insn[1] & 0x31) == 0x21)) {
  731. /*
  732. * jmp near and far, absolute indirect
  733. * ip is correct. And this is boostable
  734. */
  735. p->ainsn.boostable = 1;
  736. goto no_change;
  737. }
  738. default:
  739. break;
  740. }
  741. if (p->ainsn.boostable == 0) {
  742. if ((regs->ip > copy_ip) &&
  743. (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) {
  744. /*
  745. * These instructions can be executed directly if it
  746. * jumps back to correct address.
  747. */
  748. set_jmp_op((void *)regs->ip,
  749. (void *)orig_ip + (regs->ip - copy_ip));
  750. p->ainsn.boostable = 1;
  751. } else {
  752. p->ainsn.boostable = -1;
  753. }
  754. }
  755. regs->ip += orig_ip - copy_ip;
  756. no_change:
  757. restore_btf();
  758. return;
  759. }
  760. /*
  761. * Interrupts are disabled on entry as trap1 is an interrupt gate and they
  762. * remain disabled thoroughout this function.
  763. */
  764. static int __kprobes post_kprobe_handler(struct pt_regs *regs)
  765. {
  766. struct kprobe *cur = kprobe_running();
  767. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  768. if (!cur)
  769. return 0;
  770. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  771. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  772. cur->post_handler(cur, regs, 0);
  773. }
  774. resume_execution(cur, regs, kcb);
  775. regs->flags |= kcb->kprobe_saved_flags;
  776. trace_hardirqs_fixup_flags(regs->flags);
  777. /* Restore back the original saved kprobes variables and continue. */
  778. if (kcb->kprobe_status == KPROBE_REENTER) {
  779. restore_previous_kprobe(kcb);
  780. goto out;
  781. }
  782. reset_current_kprobe();
  783. out:
  784. preempt_enable_no_resched();
  785. /*
  786. * if somebody else is singlestepping across a probe point, flags
  787. * will have TF set, in which case, continue the remaining processing
  788. * of do_debug, as if this is not a probe hit.
  789. */
  790. if (regs->flags & TF_MASK)
  791. return 0;
  792. return 1;
  793. }
  794. int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  795. {
  796. struct kprobe *cur = kprobe_running();
  797. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  798. switch (kcb->kprobe_status) {
  799. case KPROBE_HIT_SS:
  800. case KPROBE_REENTER:
  801. /*
  802. * We are here because the instruction being single
  803. * stepped caused a page fault. We reset the current
  804. * kprobe and the ip points back to the probe address
  805. * and allow the page fault handler to continue as a
  806. * normal page fault.
  807. */
  808. regs->ip = (unsigned long)cur->addr;
  809. regs->flags |= kcb->kprobe_old_flags;
  810. if (kcb->kprobe_status == KPROBE_REENTER)
  811. restore_previous_kprobe(kcb);
  812. else
  813. reset_current_kprobe();
  814. preempt_enable_no_resched();
  815. break;
  816. case KPROBE_HIT_ACTIVE:
  817. case KPROBE_HIT_SSDONE:
  818. /*
  819. * We increment the nmissed count for accounting,
  820. * we can also use npre/npostfault count for accounting
  821. * these specific fault cases.
  822. */
  823. kprobes_inc_nmissed_count(cur);
  824. /*
  825. * We come here because instructions in the pre/post
  826. * handler caused the page_fault, this could happen
  827. * if handler tries to access user space by
  828. * copy_from_user(), get_user() etc. Let the
  829. * user-specified handler try to fix it first.
  830. */
  831. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  832. return 1;
  833. /*
  834. * In case the user-specified fault handler returned
  835. * zero, try to fix up.
  836. */
  837. #ifdef CONFIG_X86_64
  838. {
  839. const struct exception_table_entry *fixup;
  840. fixup = search_exception_tables(regs->ip);
  841. if (fixup) {
  842. regs->ip = fixup->fixup;
  843. return 1;
  844. }
  845. }
  846. #else
  847. if (fixup_exception(regs))
  848. return 1;
  849. #endif
  850. /*
  851. * fixup routine could not handle it,
  852. * Let do_page_fault() fix it.
  853. */
  854. break;
  855. default:
  856. break;
  857. }
  858. return 0;
  859. }
  860. /*
  861. * Wrapper routine for handling exceptions.
  862. */
  863. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  864. unsigned long val, void *data)
  865. {
  866. struct die_args *args = (struct die_args *)data;
  867. int ret = NOTIFY_DONE;
  868. if (args->regs && user_mode_vm(args->regs))
  869. return ret;
  870. switch (val) {
  871. case DIE_INT3:
  872. if (kprobe_handler(args->regs))
  873. ret = NOTIFY_STOP;
  874. break;
  875. case DIE_DEBUG:
  876. if (post_kprobe_handler(args->regs))
  877. ret = NOTIFY_STOP;
  878. break;
  879. case DIE_GPF:
  880. /* kprobe_running() needs smp_processor_id() */
  881. preempt_disable();
  882. if (kprobe_running() &&
  883. kprobe_fault_handler(args->regs, args->trapnr))
  884. ret = NOTIFY_STOP;
  885. preempt_enable();
  886. break;
  887. default:
  888. break;
  889. }
  890. return ret;
  891. }
  892. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  893. {
  894. struct jprobe *jp = container_of(p, struct jprobe, kp);
  895. unsigned long addr;
  896. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  897. kcb->jprobe_saved_regs = *regs;
  898. kcb->jprobe_saved_sp = stack_addr(regs);
  899. addr = (unsigned long)(kcb->jprobe_saved_sp);
  900. /*
  901. * As Linus pointed out, gcc assumes that the callee
  902. * owns the argument space and could overwrite it, e.g.
  903. * tailcall optimization. So, to be absolutely safe
  904. * we also save and restore enough stack bytes to cover
  905. * the argument area.
  906. */
  907. memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
  908. MIN_STACK_SIZE(addr));
  909. regs->flags &= ~IF_MASK;
  910. trace_hardirqs_off();
  911. regs->ip = (unsigned long)(jp->entry);
  912. return 1;
  913. }
  914. void __kprobes jprobe_return(void)
  915. {
  916. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  917. asm volatile (
  918. #ifdef CONFIG_X86_64
  919. " xchg %%rbx,%%rsp \n"
  920. #else
  921. " xchgl %%ebx,%%esp \n"
  922. #endif
  923. " int3 \n"
  924. " .globl jprobe_return_end\n"
  925. " jprobe_return_end: \n"
  926. " nop \n"::"b"
  927. (kcb->jprobe_saved_sp):"memory");
  928. }
  929. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  930. {
  931. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  932. u8 *addr = (u8 *) (regs->ip - 1);
  933. struct jprobe *jp = container_of(p, struct jprobe, kp);
  934. if ((addr > (u8 *) jprobe_return) &&
  935. (addr < (u8 *) jprobe_return_end)) {
  936. if (stack_addr(regs) != kcb->jprobe_saved_sp) {
  937. struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
  938. printk(KERN_ERR
  939. "current sp %p does not match saved sp %p\n",
  940. stack_addr(regs), kcb->jprobe_saved_sp);
  941. printk(KERN_ERR "Saved registers for jprobe %p\n", jp);
  942. show_registers(saved_regs);
  943. printk(KERN_ERR "Current registers\n");
  944. show_registers(regs);
  945. BUG();
  946. }
  947. *regs = kcb->jprobe_saved_regs;
  948. memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp),
  949. kcb->jprobes_stack,
  950. MIN_STACK_SIZE(kcb->jprobe_saved_sp));
  951. preempt_enable_no_resched();
  952. return 1;
  953. }
  954. return 0;
  955. }
  956. int __init arch_init_kprobes(void)
  957. {
  958. return 0;
  959. }
  960. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  961. {
  962. return 0;
  963. }