/arch/i386/kernel/kprobes.c

https://bitbucket.org/evzijst/gittest · C · 385 lines · 250 code · 36 blank · 99 comment · 44 complexity · 5d4ede5411518fed95daf136aedb8394 MD5 · raw file

  1. /*
  2. * Kernel Probes (KProbes)
  3. * arch/i386/kernel/kprobes.c
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. *
  19. * Copyright (C) IBM Corporation, 2002, 2004
  20. *
  21. * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
  22. * Probes initial implementation ( includes contributions from
  23. * Rusty Russell).
  24. * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
  25. * interface to access function arguments.
  26. */
  27. #include <linux/config.h>
  28. #include <linux/kprobes.h>
  29. #include <linux/ptrace.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/preempt.h>
  32. #include <asm/kdebug.h>
  33. #include <asm/desc.h>
  34. /* kprobe_status settings */
  35. #define KPROBE_HIT_ACTIVE 0x00000001
  36. #define KPROBE_HIT_SS 0x00000002
  37. static struct kprobe *current_kprobe;
  38. static unsigned long kprobe_status, kprobe_old_eflags, kprobe_saved_eflags;
  39. static struct pt_regs jprobe_saved_regs;
  40. static long *jprobe_saved_esp;
  41. /* copy of the kernel stack at the probe fire time */
  42. static kprobe_opcode_t jprobes_stack[MAX_STACK_SIZE];
  43. void jprobe_return_end(void);
  44. /*
  45. * returns non-zero if opcode modifies the interrupt flag.
  46. */
  47. static inline int is_IF_modifier(kprobe_opcode_t opcode)
  48. {
  49. switch (opcode) {
  50. case 0xfa: /* cli */
  51. case 0xfb: /* sti */
  52. case 0xcf: /* iret/iretd */
  53. case 0x9d: /* popf/popfd */
  54. return 1;
  55. }
  56. return 0;
  57. }
  58. int arch_prepare_kprobe(struct kprobe *p)
  59. {
  60. return 0;
  61. }
  62. void arch_copy_kprobe(struct kprobe *p)
  63. {
  64. memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
  65. }
  66. void arch_remove_kprobe(struct kprobe *p)
  67. {
  68. }
  69. static inline void disarm_kprobe(struct kprobe *p, struct pt_regs *regs)
  70. {
  71. *p->addr = p->opcode;
  72. regs->eip = (unsigned long)p->addr;
  73. }
  74. static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  75. {
  76. regs->eflags |= TF_MASK;
  77. regs->eflags &= ~IF_MASK;
  78. /*single step inline if the instruction is an int3*/
  79. if (p->opcode == BREAKPOINT_INSTRUCTION)
  80. regs->eip = (unsigned long)p->addr;
  81. else
  82. regs->eip = (unsigned long)&p->ainsn.insn;
  83. }
  84. /*
  85. * Interrupts are disabled on entry as trap3 is an interrupt gate and they
  86. * remain disabled thorough out this function.
  87. */
  88. static int kprobe_handler(struct pt_regs *regs)
  89. {
  90. struct kprobe *p;
  91. int ret = 0;
  92. kprobe_opcode_t *addr = NULL;
  93. unsigned long *lp;
  94. /* We're in an interrupt, but this is clear and BUG()-safe. */
  95. preempt_disable();
  96. /* Check if the application is using LDT entry for its code segment and
  97. * calculate the address by reading the base address from the LDT entry.
  98. */
  99. if ((regs->xcs & 4) && (current->mm)) {
  100. lp = (unsigned long *) ((unsigned long)((regs->xcs >> 3) * 8)
  101. + (char *) current->mm->context.ldt);
  102. addr = (kprobe_opcode_t *) (get_desc_base(lp) + regs->eip -
  103. sizeof(kprobe_opcode_t));
  104. } else {
  105. addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t));
  106. }
  107. /* Check we're not actually recursing */
  108. if (kprobe_running()) {
  109. /* We *are* holding lock here, so this is safe.
  110. Disarm the probe we just hit, and ignore it. */
  111. p = get_kprobe(addr);
  112. if (p) {
  113. if (kprobe_status == KPROBE_HIT_SS) {
  114. regs->eflags &= ~TF_MASK;
  115. regs->eflags |= kprobe_saved_eflags;
  116. unlock_kprobes();
  117. goto no_kprobe;
  118. }
  119. disarm_kprobe(p, regs);
  120. ret = 1;
  121. } else {
  122. p = current_kprobe;
  123. if (p->break_handler && p->break_handler(p, regs)) {
  124. goto ss_probe;
  125. }
  126. }
  127. /* If it's not ours, can't be delete race, (we hold lock). */
  128. goto no_kprobe;
  129. }
  130. lock_kprobes();
  131. p = get_kprobe(addr);
  132. if (!p) {
  133. unlock_kprobes();
  134. if (regs->eflags & VM_MASK) {
  135. /* We are in virtual-8086 mode. Return 0 */
  136. goto no_kprobe;
  137. }
  138. if (*addr != BREAKPOINT_INSTRUCTION) {
  139. /*
  140. * The breakpoint instruction was removed right
  141. * after we hit it. Another cpu has removed
  142. * either a probepoint or a debugger breakpoint
  143. * at this address. In either case, no further
  144. * handling of this interrupt is appropriate.
  145. */
  146. ret = 1;
  147. }
  148. /* Not one of ours: let kernel handle it */
  149. goto no_kprobe;
  150. }
  151. kprobe_status = KPROBE_HIT_ACTIVE;
  152. current_kprobe = p;
  153. kprobe_saved_eflags = kprobe_old_eflags
  154. = (regs->eflags & (TF_MASK | IF_MASK));
  155. if (is_IF_modifier(p->opcode))
  156. kprobe_saved_eflags &= ~IF_MASK;
  157. if (p->pre_handler && p->pre_handler(p, regs))
  158. /* handler has already set things up, so skip ss setup */
  159. return 1;
  160. ss_probe:
  161. prepare_singlestep(p, regs);
  162. kprobe_status = KPROBE_HIT_SS;
  163. return 1;
  164. no_kprobe:
  165. preempt_enable_no_resched();
  166. return ret;
  167. }
  168. /*
  169. * Called after single-stepping. p->addr is the address of the
  170. * instruction whose first byte has been replaced by the "int 3"
  171. * instruction. To avoid the SMP problems that can occur when we
  172. * temporarily put back the original opcode to single-step, we
  173. * single-stepped a copy of the instruction. The address of this
  174. * copy is p->ainsn.insn.
  175. *
  176. * This function prepares to return from the post-single-step
  177. * interrupt. We have to fix up the stack as follows:
  178. *
  179. * 0) Except in the case of absolute or indirect jump or call instructions,
  180. * the new eip is relative to the copied instruction. We need to make
  181. * it relative to the original instruction.
  182. *
  183. * 1) If the single-stepped instruction was pushfl, then the TF and IF
  184. * flags are set in the just-pushed eflags, and may need to be cleared.
  185. *
  186. * 2) If the single-stepped instruction was a call, the return address
  187. * that is atop the stack is the address following the copied instruction.
  188. * We need to make it the address following the original instruction.
  189. */
  190. static void resume_execution(struct kprobe *p, struct pt_regs *regs)
  191. {
  192. unsigned long *tos = (unsigned long *)&regs->esp;
  193. unsigned long next_eip = 0;
  194. unsigned long copy_eip = (unsigned long)&p->ainsn.insn;
  195. unsigned long orig_eip = (unsigned long)p->addr;
  196. switch (p->ainsn.insn[0]) {
  197. case 0x9c: /* pushfl */
  198. *tos &= ~(TF_MASK | IF_MASK);
  199. *tos |= kprobe_old_eflags;
  200. break;
  201. case 0xe8: /* call relative - Fix return addr */
  202. *tos = orig_eip + (*tos - copy_eip);
  203. break;
  204. case 0xff:
  205. if ((p->ainsn.insn[1] & 0x30) == 0x10) {
  206. /* call absolute, indirect */
  207. /* Fix return addr; eip is correct. */
  208. next_eip = regs->eip;
  209. *tos = orig_eip + (*tos - copy_eip);
  210. } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
  211. ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
  212. /* eip is correct. */
  213. next_eip = regs->eip;
  214. }
  215. break;
  216. case 0xea: /* jmp absolute -- eip is correct */
  217. next_eip = regs->eip;
  218. break;
  219. default:
  220. break;
  221. }
  222. regs->eflags &= ~TF_MASK;
  223. if (next_eip) {
  224. regs->eip = next_eip;
  225. } else {
  226. regs->eip = orig_eip + (regs->eip - copy_eip);
  227. }
  228. }
  229. /*
  230. * Interrupts are disabled on entry as trap1 is an interrupt gate and they
  231. * remain disabled thoroughout this function. And we hold kprobe lock.
  232. */
  233. static inline int post_kprobe_handler(struct pt_regs *regs)
  234. {
  235. if (!kprobe_running())
  236. return 0;
  237. if (current_kprobe->post_handler)
  238. current_kprobe->post_handler(current_kprobe, regs, 0);
  239. resume_execution(current_kprobe, regs);
  240. regs->eflags |= kprobe_saved_eflags;
  241. unlock_kprobes();
  242. preempt_enable_no_resched();
  243. /*
  244. * if somebody else is singlestepping across a probe point, eflags
  245. * will have TF set, in which case, continue the remaining processing
  246. * of do_debug, as if this is not a probe hit.
  247. */
  248. if (regs->eflags & TF_MASK)
  249. return 0;
  250. return 1;
  251. }
  252. /* Interrupts disabled, kprobe_lock held. */
  253. static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  254. {
  255. if (current_kprobe->fault_handler
  256. && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
  257. return 1;
  258. if (kprobe_status & KPROBE_HIT_SS) {
  259. resume_execution(current_kprobe, regs);
  260. regs->eflags |= kprobe_old_eflags;
  261. unlock_kprobes();
  262. preempt_enable_no_resched();
  263. }
  264. return 0;
  265. }
  266. /*
  267. * Wrapper routine to for handling exceptions.
  268. */
  269. int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
  270. void *data)
  271. {
  272. struct die_args *args = (struct die_args *)data;
  273. switch (val) {
  274. case DIE_INT3:
  275. if (kprobe_handler(args->regs))
  276. return NOTIFY_STOP;
  277. break;
  278. case DIE_DEBUG:
  279. if (post_kprobe_handler(args->regs))
  280. return NOTIFY_STOP;
  281. break;
  282. case DIE_GPF:
  283. if (kprobe_running() &&
  284. kprobe_fault_handler(args->regs, args->trapnr))
  285. return NOTIFY_STOP;
  286. break;
  287. case DIE_PAGE_FAULT:
  288. if (kprobe_running() &&
  289. kprobe_fault_handler(args->regs, args->trapnr))
  290. return NOTIFY_STOP;
  291. break;
  292. default:
  293. break;
  294. }
  295. return NOTIFY_DONE;
  296. }
  297. int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  298. {
  299. struct jprobe *jp = container_of(p, struct jprobe, kp);
  300. unsigned long addr;
  301. jprobe_saved_regs = *regs;
  302. jprobe_saved_esp = &regs->esp;
  303. addr = (unsigned long)jprobe_saved_esp;
  304. /*
  305. * TBD: As Linus pointed out, gcc assumes that the callee
  306. * owns the argument space and could overwrite it, e.g.
  307. * tailcall optimization. So, to be absolutely safe
  308. * we also save and restore enough stack bytes to cover
  309. * the argument area.
  310. */
  311. memcpy(jprobes_stack, (kprobe_opcode_t *) addr, MIN_STACK_SIZE(addr));
  312. regs->eflags &= ~IF_MASK;
  313. regs->eip = (unsigned long)(jp->entry);
  314. return 1;
  315. }
  316. void jprobe_return(void)
  317. {
  318. preempt_enable_no_resched();
  319. asm volatile (" xchgl %%ebx,%%esp \n"
  320. " int3 \n"
  321. " .globl jprobe_return_end \n"
  322. " jprobe_return_end: \n"
  323. " nop \n"::"b"
  324. (jprobe_saved_esp):"memory");
  325. }
  326. int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  327. {
  328. u8 *addr = (u8 *) (regs->eip - 1);
  329. unsigned long stack_addr = (unsigned long)jprobe_saved_esp;
  330. struct jprobe *jp = container_of(p, struct jprobe, kp);
  331. if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
  332. if (&regs->esp != jprobe_saved_esp) {
  333. struct pt_regs *saved_regs =
  334. container_of(jprobe_saved_esp, struct pt_regs, esp);
  335. printk("current esp %p does not match saved esp %p\n",
  336. &regs->esp, jprobe_saved_esp);
  337. printk("Saved registers for jprobe %p\n", jp);
  338. show_registers(saved_regs);
  339. printk("Current registers\n");
  340. show_registers(regs);
  341. BUG();
  342. }
  343. *regs = jprobe_saved_regs;
  344. memcpy((kprobe_opcode_t *) stack_addr, jprobes_stack,
  345. MIN_STACK_SIZE(stack_addr));
  346. return 1;
  347. }
  348. return 0;
  349. }