kprobes.c 31 KB

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