kprobes.c 33 KB

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