Redesign KProbe module (separating core and arch parts).
[kernel/swap-modules.git] / kprobe / arch / asm-x86 / dbi_kprobes.h
1 #ifndef _SRC_ASM_X86_KPROBES_H
2 #define _SRC_ASM_X86_KPROBES_H
3
4 /*
5  *  Kernel Probes (KProbes)
6  *  include/linux/kprobes.h
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  *
22  * Copyright (C) IBM Corporation, 2002, 2004
23  */
24
25 /*
26  *  Dynamic Binary Instrumentation Module based on KProbes
27  *  modules/kprobe/arch/asm-x86/dbi_kprobes.c
28  *
29  * This program is free software; you can redistribute it and/or modify
30  * it under the terms of the GNU General Public License as published by
31  * the Free Software Foundation; either version 2 of the License, or
32  * (at your option) any later version.
33  *
34  * This program is distributed in the hope that it will be useful,
35  * but WITHOUT ANY WARRANTY; without even the implied warranty of
36  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37  * GNU General Public License for more details.
38  *
39  * You should have received a copy of the GNU General Public License
40  * along with this program; if not, write to the Free Software
41  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
42  *
43  * Copyright (C) Samsung Electronics, 2006-2010
44  *
45  * 2008-2009    Alexey Gerenkov <a.gerenkov@samsung.com> User-Space
46  *              Probes initial implementation; Support x86/ARM/MIPS for both user and kernel spaces.
47  * 2010         Ekaterina Gorelkina <e.gorelkina@samsung.com>: redesign module for separating core and arch parts 
48  *
49
50  */
51
52 #include "dbi_kprobe_deps.h"
53 #include "arch/dbi_kprobes.h"
54
55 typedef u8 kprobe_opcode_t;
56
57 #define BREAKPOINT_INSTRUCTION          0xcc
58 #define RELATIVEJUMP_INSTRUCTION        0xe9
59
60 #define MAX_INSN_SIZE                   16
61 #define MAX_STACK_SIZE                  64
62
63 #define MIN_STACK_SIZE(ADDR)   (((MAX_STACK_SIZE) <                       \
64                         (((unsigned long)current_thread_info())  \
65                          + THREAD_SIZE - (ADDR)))                 \
66                 ? (MAX_STACK_SIZE)                        \
67                 : (((unsigned long)current_thread_info()) \
68                         + THREAD_SIZE - (ADDR)))
69
70 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)
71
72 #define EREG(rg)                e##rg
73 #define XREG(rg)                x##rg
74 #define ORIG_EAX_REG            orig_eax
75
76 #else 
77
78 #define EREG(rg)                rg
79 #define XREG(rg)                rg
80 #define ORIG_EAX_REG            orig_ax
81
82 #endif /*  LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25) */
83
84 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)
85 #define TF_MASK                         X86_EFLAGS_TF
86 #define IF_MASK                         X86_EFLAGS_IF
87 #endif
88 #define UPROBES_TRAMP_LEN               (MAX_INSN_SIZE+sizeof(kprobe_opcode_t))
89 #define UPROBES_TRAMP_INSN_IDX          0
90 #define UPROBES_TRAMP_RET_BREAK_IDX     MAX_INSN_SIZE
91 #define KPROBES_TRAMP_LEN               MAX_INSN_SIZE
92 #define KPROBES_TRAMP_INSN_IDX          0
93
94 static struct notifier_block kprobe_exceptions_nb = {
95         .notifier_call = kprobe_exceptions_notify,
96         .priority = INT_MAX
97 };
98
99 struct prev_kprobe {
100         struct kprobe *kp;
101         unsigned long status;
102         unsigned long old_eflags;
103         unsigned long saved_eflags;
104 };
105
106 /* per-cpu kprobe control block */
107 struct kprobe_ctlblk {
108         unsigned long kprobe_status;
109         struct prev_kprobe prev_kprobe;
110         struct pt_regs jprobe_saved_regs;
111         unsigned long kprobe_old_eflags;
112         unsigned long kprobe_saved_eflags;
113         unsigned long *jprobe_saved_esp;
114         kprobe_opcode_t jprobes_stack[MAX_STACK_SIZE];
115 };
116
117 extern int kprobe_exceptions_notify (struct notifier_block *self, unsigned long val, void *data);
118
119 void __kprobes resume_execution 
120 (struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb);
121
122 int __kprobes post_kprobe_handler (struct pt_regs *regs);
123
124 int __kprobes
125 kprobe_fault_handler (struct pt_regs *regs, int trapnr);
126
127 void *__kprobes trampoline_probe_handler_x86 (struct pt_regs *regs);
128
129 DECLARE_MOD_FUNC_DEP(module_alloc, void *, unsigned long size);
130 DECLARE_MOD_FUNC_DEP(module_free, void, struct module *mod, void *module_region);
131 DECLARE_MOD_FUNC_DEP(fixup_exception, int, struct pt_regs * regs);
132
133 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
134 DECLARE_MOD_FUNC_DEP(text_poke, void, void *addr, unsigned char *opcode, int len);
135 #else
136 DECLARE_MOD_FUNC_DEP(text_poke, void *, void *addr, const void *opcode, size_t len);
137 #endif
138 DECLARE_MOD_FUNC_DEP(show_registers, void, struct pt_regs * regs);
139
140 /* Architecture specific copy of original instruction */
141 struct arch_specific_insn {
142         /* copy of the original instruction */
143         kprobe_opcode_t *insn;
144         /*
145          * If this flag is not 0, this kprobe can be boost when its
146          * post_handler and break_handler is not set.
147          */
148         int boostable;
149 };
150
151 typedef kprobe_opcode_t (*entry_point_t) (unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long);
152
153
154
155 #endif /* _SRC_ASM_X86_KPROBES_H */