kprobes.c 31 KB

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