kprobes.c 31 KB

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