kprobes.c 31 KB

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