kprobes.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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 void __kprobes 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 int __kprobes 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 & (X86_EFLAGS_TF | X86_EFLAGS_IF));
  364. if (is_IF_modifier(p->ainsn.insn))
  365. kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
  366. }
  367. static void __kprobes clear_btf(void)
  368. {
  369. if (test_thread_flag(TIF_DEBUGCTLMSR))
  370. wrmsr(MSR_IA32_DEBUGCTLMSR, 0, 0);
  371. }
  372. static void __kprobes 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 |= X86_EFLAGS_TF;
  381. regs->flags &= ~X86_EFLAGS_IF;
  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. * We have reentered the kprobe_handler(), since another probe was hit while
  399. * within the handler. We save the original kprobes variables and just single
  400. * step on the instruction of the new probe without calling any user handlers.
  401. */
  402. static void __kprobes reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
  403. struct kprobe_ctlblk *kcb)
  404. {
  405. save_previous_kprobe(kcb);
  406. set_current_kprobe(p, regs, kcb);
  407. kprobes_inc_nmissed_count(p);
  408. prepare_singlestep(p, regs);
  409. kcb->kprobe_status = KPROBE_REENTER;
  410. }
  411. /*
  412. * Interrupts are disabled on entry as trap3 is an interrupt gate and they
  413. * remain disabled thorough out this function.
  414. */
  415. static int __kprobes kprobe_handler(struct pt_regs *regs)
  416. {
  417. struct kprobe *p;
  418. int ret = 0;
  419. kprobe_opcode_t *addr;
  420. struct kprobe_ctlblk *kcb;
  421. addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
  422. /*
  423. * We don't want to be preempted for the entire
  424. * duration of kprobe processing
  425. */
  426. preempt_disable();
  427. kcb = get_kprobe_ctlblk();
  428. /* Check we're not actually recursing */
  429. if (kprobe_running()) {
  430. p = get_kprobe(addr);
  431. if (p) {
  432. if (kcb->kprobe_status == KPROBE_HIT_SS &&
  433. *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
  434. regs->flags &= ~X86_EFLAGS_TF;
  435. regs->flags |= kcb->kprobe_saved_flags;
  436. goto no_kprobe;
  437. #ifdef CONFIG_X86_64
  438. } else if (kcb->kprobe_status == KPROBE_HIT_SSDONE) {
  439. /* TODO: Provide re-entrancy from
  440. * post_kprobes_handler() and avoid exception
  441. * stack corruption while single-stepping on
  442. * the instruction of the new probe.
  443. */
  444. arch_disarm_kprobe(p);
  445. regs->ip = (unsigned long)p->addr;
  446. reset_current_kprobe();
  447. ret = 1;
  448. goto no_kprobe;
  449. #endif
  450. }
  451. reenter_kprobe(p, regs, kcb);
  452. return 1;
  453. } else {
  454. if (*addr != BREAKPOINT_INSTRUCTION) {
  455. /* The breakpoint instruction was removed by
  456. * another cpu right after we hit, no further
  457. * handling of this interrupt is appropriate
  458. */
  459. regs->ip = (unsigned long)addr;
  460. ret = 1;
  461. goto no_kprobe;
  462. }
  463. p = __get_cpu_var(current_kprobe);
  464. if (p->break_handler && p->break_handler(p, regs))
  465. goto ss_probe;
  466. }
  467. goto no_kprobe;
  468. }
  469. p = get_kprobe(addr);
  470. if (!p) {
  471. if (*addr != BREAKPOINT_INSTRUCTION) {
  472. /*
  473. * The breakpoint instruction was removed right
  474. * after we hit it. Another cpu has removed
  475. * either a probepoint or a debugger breakpoint
  476. * at this address. In either case, no further
  477. * handling of this interrupt is appropriate.
  478. * Back up over the (now missing) int3 and run
  479. * the original instruction.
  480. */
  481. regs->ip = (unsigned long)addr;
  482. ret = 1;
  483. }
  484. /* Not one of ours: let kernel handle it */
  485. goto no_kprobe;
  486. }
  487. set_current_kprobe(p, regs, kcb);
  488. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  489. if (p->pre_handler && p->pre_handler(p, regs))
  490. /* handler has already set things up, so skip ss setup */
  491. return 1;
  492. ss_probe:
  493. #if !defined(CONFIG_PREEMPT) || defined(CONFIG_PM)
  494. if (p->ainsn.boostable == 1 && !p->post_handler) {
  495. /* Boost up -- we can execute copied instructions directly */
  496. reset_current_kprobe();
  497. regs->ip = (unsigned long)p->ainsn.insn;
  498. preempt_enable_no_resched();
  499. return 1;
  500. }
  501. #endif
  502. prepare_singlestep(p, regs);
  503. kcb->kprobe_status = KPROBE_HIT_SS;
  504. return 1;
  505. no_kprobe:
  506. preempt_enable_no_resched();
  507. return ret;
  508. }
  509. /*
  510. * When a retprobed function returns, this code saves registers and
  511. * calls trampoline_handler() runs, which calls the kretprobe's handler.
  512. */
  513. void __kprobes kretprobe_trampoline_holder(void)
  514. {
  515. asm volatile (
  516. ".global kretprobe_trampoline\n"
  517. "kretprobe_trampoline: \n"
  518. #ifdef CONFIG_X86_64
  519. /* We don't bother saving the ss register */
  520. " pushq %rsp\n"
  521. " pushfq\n"
  522. /*
  523. * Skip cs, ip, orig_ax.
  524. * trampoline_handler() will plug in these values
  525. */
  526. " subq $24, %rsp\n"
  527. " pushq %rdi\n"
  528. " pushq %rsi\n"
  529. " pushq %rdx\n"
  530. " pushq %rcx\n"
  531. " pushq %rax\n"
  532. " pushq %r8\n"
  533. " pushq %r9\n"
  534. " pushq %r10\n"
  535. " pushq %r11\n"
  536. " pushq %rbx\n"
  537. " pushq %rbp\n"
  538. " pushq %r12\n"
  539. " pushq %r13\n"
  540. " pushq %r14\n"
  541. " pushq %r15\n"
  542. " movq %rsp, %rdi\n"
  543. " call trampoline_handler\n"
  544. /* Replace saved sp with true return address. */
  545. " movq %rax, 152(%rsp)\n"
  546. " popq %r15\n"
  547. " popq %r14\n"
  548. " popq %r13\n"
  549. " popq %r12\n"
  550. " popq %rbp\n"
  551. " popq %rbx\n"
  552. " popq %r11\n"
  553. " popq %r10\n"
  554. " popq %r9\n"
  555. " popq %r8\n"
  556. " popq %rax\n"
  557. " popq %rcx\n"
  558. " popq %rdx\n"
  559. " popq %rsi\n"
  560. " popq %rdi\n"
  561. /* Skip orig_ax, ip, cs */
  562. " addq $24, %rsp\n"
  563. " popfq\n"
  564. #else
  565. " pushf\n"
  566. /*
  567. * Skip cs, ip, orig_ax.
  568. * trampoline_handler() will plug in these values
  569. */
  570. " subl $12, %esp\n"
  571. " pushl %fs\n"
  572. " pushl %ds\n"
  573. " pushl %es\n"
  574. " pushl %eax\n"
  575. " pushl %ebp\n"
  576. " pushl %edi\n"
  577. " pushl %esi\n"
  578. " pushl %edx\n"
  579. " pushl %ecx\n"
  580. " pushl %ebx\n"
  581. " movl %esp, %eax\n"
  582. " call trampoline_handler\n"
  583. /* Move flags to cs */
  584. " movl 52(%esp), %edx\n"
  585. " movl %edx, 48(%esp)\n"
  586. /* Replace saved flags with true return address. */
  587. " movl %eax, 52(%esp)\n"
  588. " popl %ebx\n"
  589. " popl %ecx\n"
  590. " popl %edx\n"
  591. " popl %esi\n"
  592. " popl %edi\n"
  593. " popl %ebp\n"
  594. " popl %eax\n"
  595. /* Skip ip, orig_ax, es, ds, fs */
  596. " addl $20, %esp\n"
  597. " popf\n"
  598. #endif
  599. " ret\n");
  600. }
  601. /*
  602. * Called from kretprobe_trampoline
  603. */
  604. void * __kprobes trampoline_handler(struct pt_regs *regs)
  605. {
  606. struct kretprobe_instance *ri = NULL;
  607. struct hlist_head *head, empty_rp;
  608. struct hlist_node *node, *tmp;
  609. unsigned long flags, orig_ret_address = 0;
  610. unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
  611. INIT_HLIST_HEAD(&empty_rp);
  612. spin_lock_irqsave(&kretprobe_lock, flags);
  613. head = kretprobe_inst_table_head(current);
  614. /* fixup registers */
  615. #ifdef CONFIG_X86_64
  616. regs->cs = __KERNEL_CS;
  617. #else
  618. regs->cs = __KERNEL_CS | get_kernel_rpl();
  619. #endif
  620. regs->ip = trampoline_address;
  621. regs->orig_ax = ~0UL;
  622. /*
  623. * It is possible to have multiple instances associated with a given
  624. * task either because multiple functions in the call path have
  625. * return probes installed on them, and/or more then one
  626. * return probe was registered for a target function.
  627. *
  628. * We can handle this because:
  629. * - instances are always pushed into the head of the list
  630. * - when multiple return probes are registered for the same
  631. * function, the (chronologically) first instance's ret_addr
  632. * will be the real return address, and all the rest will
  633. * point to kretprobe_trampoline.
  634. */
  635. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  636. if (ri->task != current)
  637. /* another task is sharing our hash bucket */
  638. continue;
  639. if (ri->rp && ri->rp->handler) {
  640. __get_cpu_var(current_kprobe) = &ri->rp->kp;
  641. get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
  642. ri->rp->handler(ri, regs);
  643. __get_cpu_var(current_kprobe) = NULL;
  644. }
  645. orig_ret_address = (unsigned long)ri->ret_addr;
  646. recycle_rp_inst(ri, &empty_rp);
  647. if (orig_ret_address != trampoline_address)
  648. /*
  649. * This is the real return address. Any other
  650. * instances associated with this task are for
  651. * other calls deeper on the call stack
  652. */
  653. break;
  654. }
  655. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  656. spin_unlock_irqrestore(&kretprobe_lock, flags);
  657. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  658. hlist_del(&ri->hlist);
  659. kfree(ri);
  660. }
  661. return (void *)orig_ret_address;
  662. }
  663. /*
  664. * Called after single-stepping. p->addr is the address of the
  665. * instruction whose first byte has been replaced by the "int 3"
  666. * instruction. To avoid the SMP problems that can occur when we
  667. * temporarily put back the original opcode to single-step, we
  668. * single-stepped a copy of the instruction. The address of this
  669. * copy is p->ainsn.insn.
  670. *
  671. * This function prepares to return from the post-single-step
  672. * interrupt. We have to fix up the stack as follows:
  673. *
  674. * 0) Except in the case of absolute or indirect jump or call instructions,
  675. * the new ip is relative to the copied instruction. We need to make
  676. * it relative to the original instruction.
  677. *
  678. * 1) If the single-stepped instruction was pushfl, then the TF and IF
  679. * flags are set in the just-pushed flags, and may need to be cleared.
  680. *
  681. * 2) If the single-stepped instruction was a call, the return address
  682. * that is atop the stack is the address following the copied instruction.
  683. * We need to make it the address following the original instruction.
  684. *
  685. * If this is the first time we've single-stepped the instruction at
  686. * this probepoint, and the instruction is boostable, boost it: add a
  687. * jump instruction after the copied instruction, that jumps to the next
  688. * instruction after the probepoint.
  689. */
  690. static void __kprobes resume_execution(struct kprobe *p,
  691. struct pt_regs *regs, struct kprobe_ctlblk *kcb)
  692. {
  693. unsigned long *tos = stack_addr(regs);
  694. unsigned long copy_ip = (unsigned long)p->ainsn.insn;
  695. unsigned long orig_ip = (unsigned long)p->addr;
  696. kprobe_opcode_t *insn = p->ainsn.insn;
  697. #ifdef CONFIG_X86_64
  698. /*skip the REX prefix*/
  699. if (*insn >= 0x40 && *insn <= 0x4f)
  700. insn++;
  701. #endif
  702. regs->flags &= ~X86_EFLAGS_TF;
  703. switch (*insn) {
  704. case 0x9c: /* pushfl */
  705. *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
  706. *tos |= kcb->kprobe_old_flags;
  707. break;
  708. case 0xc2: /* iret/ret/lret */
  709. case 0xc3:
  710. case 0xca:
  711. case 0xcb:
  712. case 0xcf:
  713. case 0xea: /* jmp absolute -- ip is correct */
  714. /* ip is already adjusted, no more changes required */
  715. p->ainsn.boostable = 1;
  716. goto no_change;
  717. case 0xe8: /* call relative - Fix return addr */
  718. *tos = orig_ip + (*tos - copy_ip);
  719. break;
  720. #ifdef CONFIG_X86_32
  721. case 0x9a: /* call absolute -- same as call absolute, indirect */
  722. *tos = orig_ip + (*tos - copy_ip);
  723. goto no_change;
  724. #endif
  725. case 0xff:
  726. if ((insn[1] & 0x30) == 0x10) {
  727. /*
  728. * call absolute, indirect
  729. * Fix return addr; ip is correct.
  730. * But this is not boostable
  731. */
  732. *tos = orig_ip + (*tos - copy_ip);
  733. goto no_change;
  734. } else if (((insn[1] & 0x31) == 0x20) ||
  735. ((insn[1] & 0x31) == 0x21)) {
  736. /*
  737. * jmp near and far, absolute indirect
  738. * ip is correct. And this is boostable
  739. */
  740. p->ainsn.boostable = 1;
  741. goto no_change;
  742. }
  743. default:
  744. break;
  745. }
  746. if (p->ainsn.boostable == 0) {
  747. if ((regs->ip > copy_ip) &&
  748. (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) {
  749. /*
  750. * These instructions can be executed directly if it
  751. * jumps back to correct address.
  752. */
  753. set_jmp_op((void *)regs->ip,
  754. (void *)orig_ip + (regs->ip - copy_ip));
  755. p->ainsn.boostable = 1;
  756. } else {
  757. p->ainsn.boostable = -1;
  758. }
  759. }
  760. regs->ip += orig_ip - copy_ip;
  761. no_change:
  762. restore_btf();
  763. }
  764. /*
  765. * Interrupts are disabled on entry as trap1 is an interrupt gate and they
  766. * remain disabled thoroughout this function.
  767. */
  768. static int __kprobes post_kprobe_handler(struct pt_regs *regs)
  769. {
  770. struct kprobe *cur = kprobe_running();
  771. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  772. if (!cur)
  773. return 0;
  774. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  775. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  776. cur->post_handler(cur, regs, 0);
  777. }
  778. resume_execution(cur, regs, kcb);
  779. regs->flags |= kcb->kprobe_saved_flags;
  780. trace_hardirqs_fixup_flags(regs->flags);
  781. /* Restore back the original saved kprobes variables and continue. */
  782. if (kcb->kprobe_status == KPROBE_REENTER) {
  783. restore_previous_kprobe(kcb);
  784. goto out;
  785. }
  786. reset_current_kprobe();
  787. out:
  788. preempt_enable_no_resched();
  789. /*
  790. * if somebody else is singlestepping across a probe point, flags
  791. * will have TF set, in which case, continue the remaining processing
  792. * of do_debug, as if this is not a probe hit.
  793. */
  794. if (regs->flags & X86_EFLAGS_TF)
  795. return 0;
  796. return 1;
  797. }
  798. int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  799. {
  800. struct kprobe *cur = kprobe_running();
  801. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  802. switch (kcb->kprobe_status) {
  803. case KPROBE_HIT_SS:
  804. case KPROBE_REENTER:
  805. /*
  806. * We are here because the instruction being single
  807. * stepped caused a page fault. We reset the current
  808. * kprobe and the ip points back to the probe address
  809. * and allow the page fault handler to continue as a
  810. * normal page fault.
  811. */
  812. regs->ip = (unsigned long)cur->addr;
  813. regs->flags |= kcb->kprobe_old_flags;
  814. if (kcb->kprobe_status == KPROBE_REENTER)
  815. restore_previous_kprobe(kcb);
  816. else
  817. reset_current_kprobe();
  818. preempt_enable_no_resched();
  819. break;
  820. case KPROBE_HIT_ACTIVE:
  821. case KPROBE_HIT_SSDONE:
  822. /*
  823. * We increment the nmissed count for accounting,
  824. * we can also use npre/npostfault count for accounting
  825. * these specific fault cases.
  826. */
  827. kprobes_inc_nmissed_count(cur);
  828. /*
  829. * We come here because instructions in the pre/post
  830. * handler caused the page_fault, this could happen
  831. * if handler tries to access user space by
  832. * copy_from_user(), get_user() etc. Let the
  833. * user-specified handler try to fix it first.
  834. */
  835. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  836. return 1;
  837. /*
  838. * In case the user-specified fault handler returned
  839. * zero, try to fix up.
  840. */
  841. if (fixup_exception(regs))
  842. return 1;
  843. /*
  844. * fixup routine could not handle it,
  845. * Let do_page_fault() fix it.
  846. */
  847. break;
  848. default:
  849. break;
  850. }
  851. return 0;
  852. }
  853. /*
  854. * Wrapper routine for handling exceptions.
  855. */
  856. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  857. unsigned long val, void *data)
  858. {
  859. struct die_args *args = (struct die_args *)data;
  860. int ret = NOTIFY_DONE;
  861. if (args->regs && user_mode_vm(args->regs))
  862. return ret;
  863. switch (val) {
  864. case DIE_INT3:
  865. if (kprobe_handler(args->regs))
  866. ret = NOTIFY_STOP;
  867. break;
  868. case DIE_DEBUG:
  869. if (post_kprobe_handler(args->regs))
  870. ret = NOTIFY_STOP;
  871. break;
  872. case DIE_GPF:
  873. /* kprobe_running() needs smp_processor_id() */
  874. preempt_disable();
  875. if (kprobe_running() &&
  876. kprobe_fault_handler(args->regs, args->trapnr))
  877. ret = NOTIFY_STOP;
  878. preempt_enable();
  879. break;
  880. default:
  881. break;
  882. }
  883. return ret;
  884. }
  885. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  886. {
  887. struct jprobe *jp = container_of(p, struct jprobe, kp);
  888. unsigned long addr;
  889. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  890. kcb->jprobe_saved_regs = *regs;
  891. kcb->jprobe_saved_sp = stack_addr(regs);
  892. addr = (unsigned long)(kcb->jprobe_saved_sp);
  893. /*
  894. * As Linus pointed out, gcc assumes that the callee
  895. * owns the argument space and could overwrite it, e.g.
  896. * tailcall optimization. So, to be absolutely safe
  897. * we also save and restore enough stack bytes to cover
  898. * the argument area.
  899. */
  900. memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
  901. MIN_STACK_SIZE(addr));
  902. regs->flags &= ~X86_EFLAGS_IF;
  903. trace_hardirqs_off();
  904. regs->ip = (unsigned long)(jp->entry);
  905. return 1;
  906. }
  907. void __kprobes jprobe_return(void)
  908. {
  909. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  910. asm volatile (
  911. #ifdef CONFIG_X86_64
  912. " xchg %%rbx,%%rsp \n"
  913. #else
  914. " xchgl %%ebx,%%esp \n"
  915. #endif
  916. " int3 \n"
  917. " .globl jprobe_return_end\n"
  918. " jprobe_return_end: \n"
  919. " nop \n"::"b"
  920. (kcb->jprobe_saved_sp):"memory");
  921. }
  922. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  923. {
  924. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  925. u8 *addr = (u8 *) (regs->ip - 1);
  926. struct jprobe *jp = container_of(p, struct jprobe, kp);
  927. if ((addr > (u8 *) jprobe_return) &&
  928. (addr < (u8 *) jprobe_return_end)) {
  929. if (stack_addr(regs) != kcb->jprobe_saved_sp) {
  930. struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
  931. printk(KERN_ERR
  932. "current sp %p does not match saved sp %p\n",
  933. stack_addr(regs), kcb->jprobe_saved_sp);
  934. printk(KERN_ERR "Saved registers for jprobe %p\n", jp);
  935. show_registers(saved_regs);
  936. printk(KERN_ERR "Current registers\n");
  937. show_registers(regs);
  938. BUG();
  939. }
  940. *regs = kcb->jprobe_saved_regs;
  941. memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp),
  942. kcb->jprobes_stack,
  943. MIN_STACK_SIZE(kcb->jprobe_saved_sp));
  944. preempt_enable_no_resched();
  945. return 1;
  946. }
  947. return 0;
  948. }
  949. int __init arch_init_kprobes(void)
  950. {
  951. return 0;
  952. }
  953. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  954. {
  955. return 0;
  956. }