Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Kernel Probes (KProbes) |
| 3 | * arch/ia64/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 | * Copyright (C) Intel Corporation, 2005 |
| 21 | * |
| 22 | * 2005-Apr Rusty Lynch <rusty.lynch@intel.com> and Anil S Keshavamurthy |
| 23 | * <anil.s.keshavamurthy@intel.com> adapted from i386 |
| 24 | */ |
| 25 | |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 26 | #include <linux/kprobes.h> |
| 27 | #include <linux/ptrace.h> |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 28 | #include <linux/string.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/preempt.h> |
| 31 | #include <linux/moduleloader.h> |
Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 32 | #include <linux/kdebug.h> |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 33 | |
| 34 | #include <asm/pgtable.h> |
Keshavamurthy Anil S | c7b645f | 2005-06-27 15:17:16 -0700 | [diff] [blame] | 35 | #include <asm/sections.h> |
Prasanna S Panchamukhi | c04c1c8 | 2006-03-26 01:38:25 -0800 | [diff] [blame] | 36 | #include <asm/uaccess.h> |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 37 | |
Anil S Keshavamurthy | b2761dc | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 38 | extern void jprobe_inst_return(void); |
| 39 | |
Ananth N Mavinakayanahalli | 8a5c4dc | 2005-11-07 01:00:09 -0800 | [diff] [blame] | 40 | DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL; |
| 41 | DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 42 | |
Masami Hiramatsu | f438d91 | 2007-10-16 01:27:49 -0700 | [diff] [blame] | 43 | struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}}; |
| 44 | |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 45 | enum instruction_type {A, I, M, F, B, L, X, u}; |
| 46 | static enum instruction_type bundle_encoding[32][3] = { |
| 47 | { M, I, I }, /* 00 */ |
| 48 | { M, I, I }, /* 01 */ |
| 49 | { M, I, I }, /* 02 */ |
| 50 | { M, I, I }, /* 03 */ |
| 51 | { M, L, X }, /* 04 */ |
| 52 | { M, L, X }, /* 05 */ |
| 53 | { u, u, u }, /* 06 */ |
| 54 | { u, u, u }, /* 07 */ |
| 55 | { M, M, I }, /* 08 */ |
| 56 | { M, M, I }, /* 09 */ |
| 57 | { M, M, I }, /* 0A */ |
| 58 | { M, M, I }, /* 0B */ |
| 59 | { M, F, I }, /* 0C */ |
| 60 | { M, F, I }, /* 0D */ |
| 61 | { M, M, F }, /* 0E */ |
| 62 | { M, M, F }, /* 0F */ |
| 63 | { M, I, B }, /* 10 */ |
| 64 | { M, I, B }, /* 11 */ |
| 65 | { M, B, B }, /* 12 */ |
| 66 | { M, B, B }, /* 13 */ |
| 67 | { u, u, u }, /* 14 */ |
| 68 | { u, u, u }, /* 15 */ |
| 69 | { B, B, B }, /* 16 */ |
| 70 | { B, B, B }, /* 17 */ |
| 71 | { M, M, B }, /* 18 */ |
| 72 | { M, M, B }, /* 19 */ |
| 73 | { u, u, u }, /* 1A */ |
| 74 | { u, u, u }, /* 1B */ |
| 75 | { M, F, B }, /* 1C */ |
| 76 | { M, F, B }, /* 1D */ |
| 77 | { u, u, u }, /* 1E */ |
| 78 | { u, u, u }, /* 1F */ |
| 79 | }; |
| 80 | |
Masami Hiramatsu | 34e1ceb | 2008-03-28 14:27:02 -0700 | [diff] [blame] | 81 | /* Insert a long branch code */ |
| 82 | static void __kprobes set_brl_inst(void *from, void *to) |
| 83 | { |
| 84 | s64 rel = ((s64) to - (s64) from) >> 4; |
| 85 | bundle_t *brl; |
| 86 | brl = (bundle_t *) ((u64) from & ~0xf); |
| 87 | brl->quad0.template = 0x05; /* [MLX](stop) */ |
| 88 | brl->quad0.slot0 = NOP_M_INST; /* nop.m 0x0 */ |
| 89 | brl->quad0.slot1_p0 = ((rel >> 20) & 0x7fffffffff) << 2; |
| 90 | brl->quad1.slot1_p1 = (((rel >> 20) & 0x7fffffffff) << 2) >> (64 - 46); |
| 91 | /* brl.cond.sptk.many.clr rel<<4 (qp=0) */ |
| 92 | brl->quad1.slot2 = BRL_INST(rel >> 59, rel & 0xfffff); |
| 93 | } |
| 94 | |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 95 | /* |
| 96 | * In this function we check to see if the instruction |
| 97 | * is IP relative instruction and update the kprobe |
| 98 | * inst flag accordingly |
| 99 | */ |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 100 | static void __kprobes update_kprobe_inst_flag(uint template, uint slot, |
| 101 | uint major_opcode, |
| 102 | unsigned long kprobe_inst, |
| 103 | struct kprobe *p) |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 104 | { |
Rusty Lynch | 8bc7677 | 2005-06-23 00:09:30 -0700 | [diff] [blame] | 105 | p->ainsn.inst_flag = 0; |
| 106 | p->ainsn.target_br_reg = 0; |
Tony Luck | 08ed38b | 2006-11-14 09:33:38 -0800 | [diff] [blame] | 107 | p->ainsn.slot = slot; |
Rusty Lynch | 8bc7677 | 2005-06-23 00:09:30 -0700 | [diff] [blame] | 108 | |
Keshavamurthy Anil S | deac66a | 2005-09-06 15:19:35 -0700 | [diff] [blame] | 109 | /* Check for Break instruction |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 110 | * Bits 37:40 Major opcode to be zero |
Keshavamurthy Anil S | deac66a | 2005-09-06 15:19:35 -0700 | [diff] [blame] | 111 | * Bits 27:32 X6 to be zero |
| 112 | * Bits 32:35 X3 to be zero |
| 113 | */ |
| 114 | if ((!major_opcode) && (!((kprobe_inst >> 27) & 0x1FF)) ) { |
| 115 | /* is a break instruction */ |
| 116 | p->ainsn.inst_flag |= INST_FLAG_BREAK_INST; |
| 117 | return; |
| 118 | } |
| 119 | |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 120 | if (bundle_encoding[template][slot] == B) { |
| 121 | switch (major_opcode) { |
| 122 | case INDIRECT_CALL_OPCODE: |
| 123 | p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG; |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 124 | p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7); |
| 125 | break; |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 126 | case IP_RELATIVE_PREDICT_OPCODE: |
| 127 | case IP_RELATIVE_BRANCH_OPCODE: |
| 128 | p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR; |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 129 | break; |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 130 | case IP_RELATIVE_CALL_OPCODE: |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 131 | p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR; |
| 132 | p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG; |
| 133 | p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7); |
| 134 | break; |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 135 | } |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 136 | } else if (bundle_encoding[template][slot] == X) { |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 137 | switch (major_opcode) { |
| 138 | case LONG_CALL_OPCODE: |
| 139 | p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG; |
| 140 | p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7); |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | return; |
| 145 | } |
Rusty Lynch | 8bc7677 | 2005-06-23 00:09:30 -0700 | [diff] [blame] | 146 | |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 147 | /* |
Anil S Keshavamurthy | 1674eaf | 2005-06-23 00:09:33 -0700 | [diff] [blame] | 148 | * In this function we check to see if the instruction |
| 149 | * (qp) cmpx.crel.ctype p1,p2=r2,r3 |
| 150 | * on which we are inserting kprobe is cmp instruction |
| 151 | * with ctype as unc. |
| 152 | */ |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 153 | static uint __kprobes is_cmp_ctype_unc_inst(uint template, uint slot, |
| 154 | uint major_opcode, |
| 155 | unsigned long kprobe_inst) |
Anil S Keshavamurthy | 1674eaf | 2005-06-23 00:09:33 -0700 | [diff] [blame] | 156 | { |
| 157 | cmp_inst_t cmp_inst; |
| 158 | uint ctype_unc = 0; |
| 159 | |
| 160 | if (!((bundle_encoding[template][slot] == I) || |
| 161 | (bundle_encoding[template][slot] == M))) |
| 162 | goto out; |
| 163 | |
| 164 | if (!((major_opcode == 0xC) || (major_opcode == 0xD) || |
| 165 | (major_opcode == 0xE))) |
| 166 | goto out; |
| 167 | |
| 168 | cmp_inst.l = kprobe_inst; |
| 169 | if ((cmp_inst.f.x2 == 0) || (cmp_inst.f.x2 == 1)) { |
Simon Arlott | 72fdbdc | 2007-05-11 14:55:43 -0700 | [diff] [blame] | 170 | /* Integer compare - Register Register (A6 type)*/ |
Anil S Keshavamurthy | 1674eaf | 2005-06-23 00:09:33 -0700 | [diff] [blame] | 171 | if ((cmp_inst.f.tb == 0) && (cmp_inst.f.ta == 0) |
| 172 | &&(cmp_inst.f.c == 1)) |
| 173 | ctype_unc = 1; |
| 174 | } else if ((cmp_inst.f.x2 == 2)||(cmp_inst.f.x2 == 3)) { |
Simon Arlott | 72fdbdc | 2007-05-11 14:55:43 -0700 | [diff] [blame] | 175 | /* Integer compare - Immediate Register (A8 type)*/ |
Anil S Keshavamurthy | 1674eaf | 2005-06-23 00:09:33 -0700 | [diff] [blame] | 176 | if ((cmp_inst.f.ta == 0) &&(cmp_inst.f.c == 1)) |
| 177 | ctype_unc = 1; |
| 178 | } |
| 179 | out: |
| 180 | return ctype_unc; |
| 181 | } |
| 182 | |
| 183 | /* |
bibo,mao | df3e0d1 | 2006-12-12 12:04:42 -0800 | [diff] [blame] | 184 | * In this function we check to see if the instruction |
| 185 | * on which we are inserting kprobe is supported. |
| 186 | * Returns qp value if supported |
| 187 | * Returns -EINVAL if unsupported |
| 188 | */ |
| 189 | static int __kprobes unsupported_inst(uint template, uint slot, |
| 190 | uint major_opcode, |
| 191 | unsigned long kprobe_inst, |
| 192 | unsigned long addr) |
| 193 | { |
| 194 | int qp; |
| 195 | |
| 196 | qp = kprobe_inst & 0x3f; |
| 197 | if (is_cmp_ctype_unc_inst(template, slot, major_opcode, kprobe_inst)) { |
| 198 | if (slot == 1 && qp) { |
Joe Perches | c2eeb32 | 2007-11-19 17:47:53 -0800 | [diff] [blame] | 199 | printk(KERN_WARNING "Kprobes on cmp unc " |
| 200 | "instruction on slot 1 at <0x%lx> " |
bibo,mao | df3e0d1 | 2006-12-12 12:04:42 -0800 | [diff] [blame] | 201 | "is not supported\n", addr); |
| 202 | return -EINVAL; |
| 203 | |
| 204 | } |
| 205 | qp = 0; |
| 206 | } |
| 207 | else if (bundle_encoding[template][slot] == I) { |
| 208 | if (major_opcode == 0) { |
| 209 | /* |
| 210 | * Check for Integer speculation instruction |
| 211 | * - Bit 33-35 to be equal to 0x1 |
| 212 | */ |
| 213 | if (((kprobe_inst >> 33) & 0x7) == 1) { |
| 214 | printk(KERN_WARNING |
| 215 | "Kprobes on speculation inst at <0x%lx> not supported\n", |
| 216 | addr); |
| 217 | return -EINVAL; |
| 218 | } |
| 219 | /* |
| 220 | * IP relative mov instruction |
| 221 | * - Bit 27-35 to be equal to 0x30 |
| 222 | */ |
| 223 | if (((kprobe_inst >> 27) & 0x1FF) == 0x30) { |
| 224 | printk(KERN_WARNING |
| 225 | "Kprobes on \"mov r1=ip\" at <0x%lx> not supported\n", |
| 226 | addr); |
| 227 | return -EINVAL; |
| 228 | |
| 229 | } |
| 230 | } |
| 231 | else if ((major_opcode == 5) && !(kprobe_inst & (0xFUl << 33)) && |
| 232 | (kprobe_inst & (0x1UL << 12))) { |
| 233 | /* test bit instructions, tbit,tnat,tf |
| 234 | * bit 33-36 to be equal to 0 |
| 235 | * bit 12 to be equal to 1 |
| 236 | */ |
| 237 | if (slot == 1 && qp) { |
Joe Perches | c2eeb32 | 2007-11-19 17:47:53 -0800 | [diff] [blame] | 238 | printk(KERN_WARNING "Kprobes on test bit " |
| 239 | "instruction on slot at <0x%lx> " |
bibo,mao | df3e0d1 | 2006-12-12 12:04:42 -0800 | [diff] [blame] | 240 | "is not supported\n", addr); |
| 241 | return -EINVAL; |
| 242 | } |
| 243 | qp = 0; |
| 244 | } |
| 245 | } |
| 246 | else if (bundle_encoding[template][slot] == B) { |
| 247 | if (major_opcode == 7) { |
| 248 | /* IP-Relative Predict major code is 7 */ |
| 249 | printk(KERN_WARNING "Kprobes on IP-Relative" |
| 250 | "Predict is not supported\n"); |
| 251 | return -EINVAL; |
| 252 | } |
| 253 | else if (major_opcode == 2) { |
| 254 | /* Indirect Predict, major code is 2 |
| 255 | * bit 27-32 to be equal to 10 or 11 |
| 256 | */ |
| 257 | int x6=(kprobe_inst >> 27) & 0x3F; |
| 258 | if ((x6 == 0x10) || (x6 == 0x11)) { |
Joe Perches | c2eeb32 | 2007-11-19 17:47:53 -0800 | [diff] [blame] | 259 | printk(KERN_WARNING "Kprobes on " |
bibo,mao | df3e0d1 | 2006-12-12 12:04:42 -0800 | [diff] [blame] | 260 | "Indirect Predict is not supported\n"); |
| 261 | return -EINVAL; |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | /* kernel does not use float instruction, here for safety kprobe |
| 266 | * will judge whether it is fcmp/flass/float approximation instruction |
| 267 | */ |
| 268 | else if (unlikely(bundle_encoding[template][slot] == F)) { |
| 269 | if ((major_opcode == 4 || major_opcode == 5) && |
| 270 | (kprobe_inst & (0x1 << 12))) { |
| 271 | /* fcmp/fclass unc instruction */ |
| 272 | if (slot == 1 && qp) { |
| 273 | printk(KERN_WARNING "Kprobes on fcmp/fclass " |
| 274 | "instruction on slot at <0x%lx> " |
| 275 | "is not supported\n", addr); |
| 276 | return -EINVAL; |
| 277 | |
| 278 | } |
| 279 | qp = 0; |
| 280 | } |
| 281 | if ((major_opcode == 0 || major_opcode == 1) && |
| 282 | (kprobe_inst & (0x1UL << 33))) { |
| 283 | /* float Approximation instruction */ |
| 284 | if (slot == 1 && qp) { |
| 285 | printk(KERN_WARNING "Kprobes on float Approx " |
| 286 | "instr at <0x%lx> is not supported\n", |
| 287 | addr); |
| 288 | return -EINVAL; |
| 289 | } |
| 290 | qp = 0; |
| 291 | } |
| 292 | } |
| 293 | return qp; |
| 294 | } |
| 295 | |
| 296 | /* |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 297 | * In this function we override the bundle with |
| 298 | * the break instruction at the given slot. |
| 299 | */ |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 300 | static void __kprobes prepare_break_inst(uint template, uint slot, |
| 301 | uint major_opcode, |
| 302 | unsigned long kprobe_inst, |
bibo,mao | df3e0d1 | 2006-12-12 12:04:42 -0800 | [diff] [blame] | 303 | struct kprobe *p, |
| 304 | int qp) |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 305 | { |
| 306 | unsigned long break_inst = BREAK_INST; |
bibo mao | 214ddde | 2006-09-26 11:20:37 -0700 | [diff] [blame] | 307 | bundle_t *bundle = &p->opcode.bundle; |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 308 | |
| 309 | /* |
| 310 | * Copy the original kprobe_inst qualifying predicate(qp) |
bibo,mao | df3e0d1 | 2006-12-12 12:04:42 -0800 | [diff] [blame] | 311 | * to the break instruction |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 312 | */ |
bibo,mao | df3e0d1 | 2006-12-12 12:04:42 -0800 | [diff] [blame] | 313 | break_inst |= qp; |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 314 | |
| 315 | switch (slot) { |
| 316 | case 0: |
| 317 | bundle->quad0.slot0 = break_inst; |
| 318 | break; |
| 319 | case 1: |
| 320 | bundle->quad0.slot1_p0 = break_inst; |
| 321 | bundle->quad1.slot1_p1 = break_inst >> (64-46); |
| 322 | break; |
| 323 | case 2: |
| 324 | bundle->quad1.slot2 = break_inst; |
| 325 | break; |
| 326 | } |
| 327 | |
| 328 | /* |
| 329 | * Update the instruction flag, so that we can |
| 330 | * emulate the instruction properly after we |
| 331 | * single step on original instruction |
| 332 | */ |
| 333 | update_kprobe_inst_flag(template, slot, major_opcode, kprobe_inst, p); |
| 334 | } |
| 335 | |
Prasanna S Panchamukhi | 3ca269d | 2006-04-18 22:22:02 -0700 | [diff] [blame] | 336 | static void __kprobes get_kprobe_inst(bundle_t *bundle, uint slot, |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 337 | unsigned long *kprobe_inst, uint *major_opcode) |
| 338 | { |
| 339 | unsigned long kprobe_inst_p0, kprobe_inst_p1; |
| 340 | unsigned int template; |
| 341 | |
| 342 | template = bundle->quad0.template; |
| 343 | |
| 344 | switch (slot) { |
| 345 | case 0: |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 346 | *major_opcode = (bundle->quad0.slot0 >> SLOT0_OPCODE_SHIFT); |
| 347 | *kprobe_inst = bundle->quad0.slot0; |
| 348 | break; |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 349 | case 1: |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 350 | *major_opcode = (bundle->quad1.slot1_p1 >> SLOT1_p1_OPCODE_SHIFT); |
| 351 | kprobe_inst_p0 = bundle->quad0.slot1_p0; |
| 352 | kprobe_inst_p1 = bundle->quad1.slot1_p1; |
| 353 | *kprobe_inst = kprobe_inst_p0 | (kprobe_inst_p1 << (64-46)); |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 354 | break; |
| 355 | case 2: |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 356 | *major_opcode = (bundle->quad1.slot2 >> SLOT2_OPCODE_SHIFT); |
| 357 | *kprobe_inst = bundle->quad1.slot2; |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 358 | break; |
| 359 | } |
| 360 | } |
| 361 | |
Keshavamurthy Anil S | c7b645f | 2005-06-27 15:17:16 -0700 | [diff] [blame] | 362 | /* Returns non-zero if the addr is in the Interrupt Vector Table */ |
Prasanna S Panchamukhi | 3ca269d | 2006-04-18 22:22:02 -0700 | [diff] [blame] | 363 | static int __kprobes in_ivt_functions(unsigned long addr) |
Keshavamurthy Anil S | c7b645f | 2005-06-27 15:17:16 -0700 | [diff] [blame] | 364 | { |
| 365 | return (addr >= (unsigned long)__start_ivt_text |
| 366 | && addr < (unsigned long)__end_ivt_text); |
| 367 | } |
| 368 | |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 369 | static int __kprobes valid_kprobe_addr(int template, int slot, |
| 370 | unsigned long addr) |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 371 | { |
| 372 | if ((slot > 2) || ((bundle_encoding[template][1] == L) && slot > 1)) { |
Keshavamurthy Anil S | c7b645f | 2005-06-27 15:17:16 -0700 | [diff] [blame] | 373 | printk(KERN_WARNING "Attempting to insert unaligned kprobe " |
| 374 | "at 0x%lx\n", addr); |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 375 | return -EINVAL; |
| 376 | } |
Rusty Lynch | a528e21 | 2005-06-27 15:17:15 -0700 | [diff] [blame] | 377 | |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 378 | if (in_ivt_functions(addr)) { |
| 379 | printk(KERN_WARNING "Kprobes can't be inserted inside " |
Keshavamurthy Anil S | c7b645f | 2005-06-27 15:17:16 -0700 | [diff] [blame] | 380 | "IVT functions at 0x%lx\n", addr); |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 381 | return -EINVAL; |
| 382 | } |
Keshavamurthy Anil S | c7b645f | 2005-06-27 15:17:16 -0700 | [diff] [blame] | 383 | |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 384 | return 0; |
| 385 | } |
Rusty Lynch | 8bc7677 | 2005-06-23 00:09:30 -0700 | [diff] [blame] | 386 | |
Prasanna S Panchamukhi | 3ca269d | 2006-04-18 22:22:02 -0700 | [diff] [blame] | 387 | static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb) |
Anil S Keshavamurthy | 852cacc | 2005-06-23 00:09:40 -0700 | [diff] [blame] | 388 | { |
Anil S Keshavamurthy | cdc7dbd | 2007-05-11 09:38:40 -0700 | [diff] [blame] | 389 | unsigned int i; |
| 390 | i = atomic_add_return(1, &kcb->prev_kprobe_index); |
| 391 | kcb->prev_kprobe[i-1].kp = kprobe_running(); |
| 392 | kcb->prev_kprobe[i-1].status = kcb->kprobe_status; |
Anil S Keshavamurthy | 852cacc | 2005-06-23 00:09:40 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Prasanna S Panchamukhi | 3ca269d | 2006-04-18 22:22:02 -0700 | [diff] [blame] | 395 | static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb) |
Anil S Keshavamurthy | 852cacc | 2005-06-23 00:09:40 -0700 | [diff] [blame] | 396 | { |
Anil S Keshavamurthy | cdc7dbd | 2007-05-11 09:38:40 -0700 | [diff] [blame] | 397 | unsigned int i; |
Masami Hiramatsu | 97075c4 | 2008-01-18 11:20:46 -0500 | [diff] [blame] | 398 | i = atomic_read(&kcb->prev_kprobe_index); |
| 399 | __get_cpu_var(current_kprobe) = kcb->prev_kprobe[i-1].kp; |
| 400 | kcb->kprobe_status = kcb->prev_kprobe[i-1].status; |
| 401 | atomic_sub(1, &kcb->prev_kprobe_index); |
Anil S Keshavamurthy | 852cacc | 2005-06-23 00:09:40 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Prasanna S Panchamukhi | 3ca269d | 2006-04-18 22:22:02 -0700 | [diff] [blame] | 404 | static void __kprobes set_current_kprobe(struct kprobe *p, |
Ananth N Mavinakayanahalli | 8a5c4dc | 2005-11-07 01:00:09 -0800 | [diff] [blame] | 405 | struct kprobe_ctlblk *kcb) |
Anil S Keshavamurthy | 852cacc | 2005-06-23 00:09:40 -0700 | [diff] [blame] | 406 | { |
Ananth N Mavinakayanahalli | 8a5c4dc | 2005-11-07 01:00:09 -0800 | [diff] [blame] | 407 | __get_cpu_var(current_kprobe) = p; |
Anil S Keshavamurthy | 852cacc | 2005-06-23 00:09:40 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Rusty Lynch | 9508dbf | 2005-06-27 15:17:12 -0700 | [diff] [blame] | 410 | static void kretprobe_trampoline(void) |
| 411 | { |
| 412 | } |
| 413 | |
| 414 | /* |
| 415 | * At this point the target function has been tricked into |
| 416 | * returning into our trampoline. Lookup the associated instance |
| 417 | * and then: |
| 418 | * - call the handler function |
| 419 | * - cleanup by marking the instance as unused |
| 420 | * - long jump back to the original return address |
| 421 | */ |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 422 | int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs) |
Rusty Lynch | 9508dbf | 2005-06-27 15:17:12 -0700 | [diff] [blame] | 423 | { |
| 424 | struct kretprobe_instance *ri = NULL; |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 425 | struct hlist_head *head, empty_rp; |
Rusty Lynch | 9508dbf | 2005-06-27 15:17:12 -0700 | [diff] [blame] | 426 | struct hlist_node *node, *tmp; |
Ananth N Mavinakayanahalli | 991a51d | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 427 | unsigned long flags, orig_ret_address = 0; |
Rusty Lynch | 9508dbf | 2005-06-27 15:17:12 -0700 | [diff] [blame] | 428 | unsigned long trampoline_address = |
| 429 | ((struct fnptr *)kretprobe_trampoline)->ip; |
| 430 | |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 431 | INIT_HLIST_HEAD(&empty_rp); |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 432 | kretprobe_hash_lock(current, &head, &flags); |
Rusty Lynch | 9508dbf | 2005-06-27 15:17:12 -0700 | [diff] [blame] | 433 | |
| 434 | /* |
| 435 | * It is possible to have multiple instances associated with a given |
| 436 | * task either because an multiple functions in the call path |
| 437 | * have a return probe installed on them, and/or more then one return |
| 438 | * return probe was registered for a target function. |
| 439 | * |
| 440 | * We can handle this because: |
| 441 | * - instances are always inserted at the head of the list |
| 442 | * - when multiple return probes are registered for the same |
| 443 | * function, the first instance's ret_addr will point to the |
| 444 | * real return address, and all the rest will point to |
| 445 | * kretprobe_trampoline |
| 446 | */ |
| 447 | hlist_for_each_entry_safe(ri, node, tmp, head, hlist) { |
Keith Owens | 9138d58 | 2005-11-07 11:27:13 -0800 | [diff] [blame] | 448 | if (ri->task != current) |
Rusty Lynch | 9508dbf | 2005-06-27 15:17:12 -0700 | [diff] [blame] | 449 | /* another task is sharing our hash bucket */ |
Keith Owens | 9138d58 | 2005-11-07 11:27:13 -0800 | [diff] [blame] | 450 | continue; |
Rusty Lynch | 9508dbf | 2005-06-27 15:17:12 -0700 | [diff] [blame] | 451 | |
Shaohua Li | 3661999 | 2007-11-13 14:55:20 +0800 | [diff] [blame] | 452 | orig_ret_address = (unsigned long)ri->ret_addr; |
| 453 | if (orig_ret_address != trampoline_address) |
| 454 | /* |
| 455 | * This is the real return address. Any other |
| 456 | * instances associated with this task are for |
| 457 | * other calls deeper on the call stack |
| 458 | */ |
| 459 | break; |
| 460 | } |
| 461 | |
| 462 | regs->cr_iip = orig_ret_address; |
| 463 | |
| 464 | hlist_for_each_entry_safe(ri, node, tmp, head, hlist) { |
| 465 | if (ri->task != current) |
| 466 | /* another task is sharing our hash bucket */ |
| 467 | continue; |
| 468 | |
Rusty Lynch | 9508dbf | 2005-06-27 15:17:12 -0700 | [diff] [blame] | 469 | if (ri->rp && ri->rp->handler) |
| 470 | ri->rp->handler(ri, regs); |
| 471 | |
| 472 | orig_ret_address = (unsigned long)ri->ret_addr; |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 473 | recycle_rp_inst(ri, &empty_rp); |
Rusty Lynch | 9508dbf | 2005-06-27 15:17:12 -0700 | [diff] [blame] | 474 | |
| 475 | if (orig_ret_address != trampoline_address) |
| 476 | /* |
| 477 | * This is the real return address. Any other |
| 478 | * instances associated with this task are for |
| 479 | * other calls deeper on the call stack |
| 480 | */ |
| 481 | break; |
| 482 | } |
| 483 | |
Ananth N Mavinakayanahalli | 0f95b7f | 2007-05-08 00:28:27 -0700 | [diff] [blame] | 484 | kretprobe_assert(ri, orig_ret_address, trampoline_address); |
| 485 | |
Ananth N Mavinakayanahalli | 8a5c4dc | 2005-11-07 01:00:09 -0800 | [diff] [blame] | 486 | reset_current_kprobe(); |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 487 | kretprobe_hash_unlock(current, &flags); |
Rusty Lynch | 9508dbf | 2005-06-27 15:17:12 -0700 | [diff] [blame] | 488 | preempt_enable_no_resched(); |
| 489 | |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 490 | hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) { |
| 491 | hlist_del(&ri->hlist); |
| 492 | kfree(ri); |
| 493 | } |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 494 | /* |
| 495 | * By returning a non-zero value, we are telling |
| 496 | * kprobe_handler() that we don't want the post_handler |
| 497 | * to run (and have re-enabled preemption) |
| 498 | */ |
Keith Owens | 9138d58 | 2005-11-07 11:27:13 -0800 | [diff] [blame] | 499 | return 1; |
Rusty Lynch | 9508dbf | 2005-06-27 15:17:12 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 502 | void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri, |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 503 | struct pt_regs *regs) |
Rusty Lynch | 9508dbf | 2005-06-27 15:17:12 -0700 | [diff] [blame] | 504 | { |
Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 505 | ri->ret_addr = (kprobe_opcode_t *)regs->b0; |
Rusty Lynch | 9508dbf | 2005-06-27 15:17:12 -0700 | [diff] [blame] | 506 | |
Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 507 | /* Replace the return addr with trampoline addr */ |
| 508 | regs->b0 = ((struct fnptr *)kretprobe_trampoline)->ip; |
Rusty Lynch | 9508dbf | 2005-06-27 15:17:12 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Masami Hiramatsu | 34e1ceb | 2008-03-28 14:27:02 -0700 | [diff] [blame] | 511 | /* Check the instruction in the slot is break */ |
| 512 | static int __kprobes __is_ia64_break_inst(bundle_t *bundle, uint slot) |
| 513 | { |
| 514 | unsigned int major_opcode; |
| 515 | unsigned int template = bundle->quad0.template; |
| 516 | unsigned long kprobe_inst; |
| 517 | |
| 518 | /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */ |
| 519 | if (slot == 1 && bundle_encoding[template][1] == L) |
| 520 | slot++; |
| 521 | |
| 522 | /* Get Kprobe probe instruction at given slot*/ |
| 523 | get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode); |
| 524 | |
| 525 | /* For break instruction, |
| 526 | * Bits 37:40 Major opcode to be zero |
| 527 | * Bits 27:32 X6 to be zero |
| 528 | * Bits 32:35 X3 to be zero |
| 529 | */ |
| 530 | if (major_opcode || ((kprobe_inst >> 27) & 0x1FF)) { |
| 531 | /* Not a break instruction */ |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | /* Is a break instruction */ |
| 536 | return 1; |
| 537 | } |
| 538 | |
| 539 | /* |
| 540 | * In this function, we check whether the target bundle modifies IP or |
| 541 | * it triggers an exception. If so, it cannot be boostable. |
| 542 | */ |
| 543 | static int __kprobes can_boost(bundle_t *bundle, uint slot, |
| 544 | unsigned long bundle_addr) |
| 545 | { |
| 546 | unsigned int template = bundle->quad0.template; |
| 547 | |
| 548 | do { |
| 549 | if (search_exception_tables(bundle_addr + slot) || |
| 550 | __is_ia64_break_inst(bundle, slot)) |
| 551 | return 0; /* exception may occur in this bundle*/ |
| 552 | } while ((++slot) < 3); |
| 553 | template &= 0x1e; |
| 554 | if (template >= 0x10 /* including B unit */ || |
| 555 | template == 0x04 /* including X unit */ || |
| 556 | template == 0x06) /* undefined */ |
| 557 | return 0; |
| 558 | |
| 559 | return 1; |
| 560 | } |
| 561 | |
| 562 | /* Prepare long jump bundle and disables other boosters if need */ |
| 563 | static void __kprobes prepare_booster(struct kprobe *p) |
| 564 | { |
| 565 | unsigned long addr = (unsigned long)p->addr & ~0xFULL; |
| 566 | unsigned int slot = (unsigned long)p->addr & 0xf; |
| 567 | struct kprobe *other_kp; |
| 568 | |
| 569 | if (can_boost(&p->ainsn.insn[0].bundle, slot, addr)) { |
| 570 | set_brl_inst(&p->ainsn.insn[1].bundle, (bundle_t *)addr + 1); |
| 571 | p->ainsn.inst_flag |= INST_FLAG_BOOSTABLE; |
| 572 | } |
| 573 | |
| 574 | /* disables boosters in previous slots */ |
| 575 | for (; addr < (unsigned long)p->addr; addr++) { |
| 576 | other_kp = get_kprobe((void *)addr); |
| 577 | if (other_kp) |
| 578 | other_kp->ainsn.inst_flag &= ~INST_FLAG_BOOSTABLE; |
| 579 | } |
| 580 | } |
| 581 | |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 582 | int __kprobes arch_prepare_kprobe(struct kprobe *p) |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 583 | { |
| 584 | unsigned long addr = (unsigned long) p->addr; |
| 585 | unsigned long *kprobe_addr = (unsigned long *)(addr & ~0xFULL); |
| 586 | unsigned long kprobe_inst=0; |
| 587 | unsigned int slot = addr & 0xf, template, major_opcode = 0; |
bibo mao | 214ddde | 2006-09-26 11:20:37 -0700 | [diff] [blame] | 588 | bundle_t *bundle; |
bibo,mao | df3e0d1 | 2006-12-12 12:04:42 -0800 | [diff] [blame] | 589 | int qp; |
Rusty Lynch | 8bc7677 | 2005-06-23 00:09:30 -0700 | [diff] [blame] | 590 | |
bibo mao | 214ddde | 2006-09-26 11:20:37 -0700 | [diff] [blame] | 591 | bundle = &((kprobe_opcode_t *)kprobe_addr)->bundle; |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 592 | template = bundle->quad0.template; |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 593 | |
| 594 | if(valid_kprobe_addr(template, slot, addr)) |
| 595 | return -EINVAL; |
| 596 | |
| 597 | /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */ |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 598 | if (slot == 1 && bundle_encoding[template][1] == L) |
| 599 | slot++; |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 600 | |
| 601 | /* Get kprobe_inst and major_opcode from the bundle */ |
| 602 | get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode); |
| 603 | |
bibo,mao | df3e0d1 | 2006-12-12 12:04:42 -0800 | [diff] [blame] | 604 | qp = unsupported_inst(template, slot, major_opcode, kprobe_inst, addr); |
| 605 | if (qp < 0) |
| 606 | return -EINVAL; |
bibo mao | 214ddde | 2006-09-26 11:20:37 -0700 | [diff] [blame] | 607 | |
| 608 | p->ainsn.insn = get_insn_slot(); |
| 609 | if (!p->ainsn.insn) |
| 610 | return -ENOMEM; |
| 611 | memcpy(&p->opcode, kprobe_addr, sizeof(kprobe_opcode_t)); |
| 612 | memcpy(p->ainsn.insn, kprobe_addr, sizeof(kprobe_opcode_t)); |
| 613 | |
bibo,mao | df3e0d1 | 2006-12-12 12:04:42 -0800 | [diff] [blame] | 614 | prepare_break_inst(template, slot, major_opcode, kprobe_inst, p, qp); |
Rusty Lynch | 8bc7677 | 2005-06-23 00:09:30 -0700 | [diff] [blame] | 615 | |
Masami Hiramatsu | 34e1ceb | 2008-03-28 14:27:02 -0700 | [diff] [blame] | 616 | prepare_booster(p); |
| 617 | |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 618 | return 0; |
| 619 | } |
| 620 | |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 621 | void __kprobes arch_arm_kprobe(struct kprobe *p) |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 622 | { |
Tony Luck | 08ed38b | 2006-11-14 09:33:38 -0800 | [diff] [blame] | 623 | unsigned long arm_addr; |
| 624 | bundle_t *src, *dest; |
| 625 | |
| 626 | arm_addr = ((unsigned long)p->addr) & ~0xFUL; |
| 627 | dest = &((kprobe_opcode_t *)arm_addr)->bundle; |
| 628 | src = &p->opcode.bundle; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 629 | |
bibo mao | 214ddde | 2006-09-26 11:20:37 -0700 | [diff] [blame] | 630 | flush_icache_range((unsigned long)p->ainsn.insn, |
Masami Hiramatsu | 34e1ceb | 2008-03-28 14:27:02 -0700 | [diff] [blame] | 631 | (unsigned long)p->ainsn.insn + |
| 632 | sizeof(kprobe_opcode_t) * MAX_INSN_SIZE); |
| 633 | |
Tony Luck | 08ed38b | 2006-11-14 09:33:38 -0800 | [diff] [blame] | 634 | switch (p->ainsn.slot) { |
| 635 | case 0: |
| 636 | dest->quad0.slot0 = src->quad0.slot0; |
| 637 | break; |
| 638 | case 1: |
| 639 | dest->quad1.slot1_p1 = src->quad1.slot1_p1; |
| 640 | break; |
| 641 | case 2: |
| 642 | dest->quad1.slot2 = src->quad1.slot2; |
| 643 | break; |
| 644 | } |
bibo mao | 214ddde | 2006-09-26 11:20:37 -0700 | [diff] [blame] | 645 | flush_icache_range(arm_addr, arm_addr + sizeof(kprobe_opcode_t)); |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 648 | void __kprobes arch_disarm_kprobe(struct kprobe *p) |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 649 | { |
Tony Luck | 08ed38b | 2006-11-14 09:33:38 -0800 | [diff] [blame] | 650 | unsigned long arm_addr; |
| 651 | bundle_t *src, *dest; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 652 | |
Tony Luck | 08ed38b | 2006-11-14 09:33:38 -0800 | [diff] [blame] | 653 | arm_addr = ((unsigned long)p->addr) & ~0xFUL; |
| 654 | dest = &((kprobe_opcode_t *)arm_addr)->bundle; |
bibo mao | 214ddde | 2006-09-26 11:20:37 -0700 | [diff] [blame] | 655 | /* p->ainsn.insn contains the original unaltered kprobe_opcode_t */ |
Tony Luck | 08ed38b | 2006-11-14 09:33:38 -0800 | [diff] [blame] | 656 | src = &p->ainsn.insn->bundle; |
| 657 | switch (p->ainsn.slot) { |
| 658 | case 0: |
| 659 | dest->quad0.slot0 = src->quad0.slot0; |
| 660 | break; |
| 661 | case 1: |
| 662 | dest->quad1.slot1_p1 = src->quad1.slot1_p1; |
| 663 | break; |
| 664 | case 2: |
| 665 | dest->quad1.slot2 = src->quad1.slot2; |
| 666 | break; |
| 667 | } |
bibo mao | 214ddde | 2006-09-26 11:20:37 -0700 | [diff] [blame] | 668 | flush_icache_range(arm_addr, arm_addr + sizeof(kprobe_opcode_t)); |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 669 | } |
| 670 | |
bibo mao | 214ddde | 2006-09-26 11:20:37 -0700 | [diff] [blame] | 671 | void __kprobes arch_remove_kprobe(struct kprobe *p) |
| 672 | { |
| 673 | mutex_lock(&kprobe_mutex); |
Masami Hiramatsu | 34e1ceb | 2008-03-28 14:27:02 -0700 | [diff] [blame] | 674 | free_insn_slot(p->ainsn.insn, p->ainsn.inst_flag & INST_FLAG_BOOSTABLE); |
bibo mao | 214ddde | 2006-09-26 11:20:37 -0700 | [diff] [blame] | 675 | mutex_unlock(&kprobe_mutex); |
| 676 | } |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 677 | /* |
| 678 | * We are resuming execution after a single step fault, so the pt_regs |
| 679 | * structure reflects the register state after we executed the instruction |
Masami Hiramatsu | 34e1ceb | 2008-03-28 14:27:02 -0700 | [diff] [blame] | 680 | * located in the kprobe (p->ainsn.insn->bundle). We still need to adjust |
Anil S Keshavamurthy | cd2675b | 2005-06-23 00:09:29 -0700 | [diff] [blame] | 681 | * the ip to point back to the original stack address. To set the IP address |
| 682 | * to original stack address, handle the case where we need to fixup the |
| 683 | * relative IP address and/or fixup branch register. |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 684 | */ |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 685 | static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs) |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 686 | { |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 687 | unsigned long bundle_addr = (unsigned long) (&p->ainsn.insn->bundle); |
| 688 | unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL; |
| 689 | unsigned long template; |
| 690 | int slot = ((unsigned long)p->addr & 0xf); |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 691 | |
bibo mao | 214ddde | 2006-09-26 11:20:37 -0700 | [diff] [blame] | 692 | template = p->ainsn.insn->bundle.quad0.template; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 693 | |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 694 | if (slot == 1 && bundle_encoding[template][1] == L) |
| 695 | slot = 2; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 696 | |
Masami Hiramatsu | 34e1ceb | 2008-03-28 14:27:02 -0700 | [diff] [blame] | 697 | if (p->ainsn.inst_flag & ~INST_FLAG_BOOSTABLE) { |
Anil S Keshavamurthy | cd2675b | 2005-06-23 00:09:29 -0700 | [diff] [blame] | 698 | |
| 699 | if (p->ainsn.inst_flag & INST_FLAG_FIX_RELATIVE_IP_ADDR) { |
| 700 | /* Fix relative IP address */ |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 701 | regs->cr_iip = (regs->cr_iip - bundle_addr) + |
| 702 | resume_addr; |
Anil S Keshavamurthy | cd2675b | 2005-06-23 00:09:29 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) { |
| 706 | /* |
| 707 | * Fix target branch register, software convention is |
| 708 | * to use either b0 or b6 or b7, so just checking |
| 709 | * only those registers |
| 710 | */ |
| 711 | switch (p->ainsn.target_br_reg) { |
| 712 | case 0: |
| 713 | if ((regs->b0 == bundle_addr) || |
| 714 | (regs->b0 == bundle_addr + 0x10)) { |
| 715 | regs->b0 = (regs->b0 - bundle_addr) + |
| 716 | resume_addr; |
| 717 | } |
| 718 | break; |
| 719 | case 6: |
| 720 | if ((regs->b6 == bundle_addr) || |
| 721 | (regs->b6 == bundle_addr + 0x10)) { |
| 722 | regs->b6 = (regs->b6 - bundle_addr) + |
| 723 | resume_addr; |
| 724 | } |
| 725 | break; |
| 726 | case 7: |
| 727 | if ((regs->b7 == bundle_addr) || |
| 728 | (regs->b7 == bundle_addr + 0x10)) { |
| 729 | regs->b7 = (regs->b7 - bundle_addr) + |
| 730 | resume_addr; |
| 731 | } |
| 732 | break; |
| 733 | } /* end switch */ |
| 734 | } |
| 735 | goto turn_ss_off; |
| 736 | } |
| 737 | |
| 738 | if (slot == 2) { |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 739 | if (regs->cr_iip == bundle_addr + 0x10) { |
| 740 | regs->cr_iip = resume_addr + 0x10; |
| 741 | } |
| 742 | } else { |
| 743 | if (regs->cr_iip == bundle_addr) { |
| 744 | regs->cr_iip = resume_addr; |
| 745 | } |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 746 | } |
Anil S Keshavamurthy | cd2675b | 2005-06-23 00:09:29 -0700 | [diff] [blame] | 747 | |
| 748 | turn_ss_off: |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 749 | /* Turn off Single Step bit */ |
| 750 | ia64_psr(regs)->ss = 0; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 751 | } |
| 752 | |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 753 | static void __kprobes prepare_ss(struct kprobe *p, struct pt_regs *regs) |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 754 | { |
bibo mao | 214ddde | 2006-09-26 11:20:37 -0700 | [diff] [blame] | 755 | unsigned long bundle_addr = (unsigned long) &p->ainsn.insn->bundle; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 756 | unsigned long slot = (unsigned long)p->addr & 0xf; |
| 757 | |
Keshavamurthy Anil S | deac66a | 2005-09-06 15:19:35 -0700 | [diff] [blame] | 758 | /* single step inline if break instruction */ |
| 759 | if (p->ainsn.inst_flag == INST_FLAG_BREAK_INST) |
| 760 | regs->cr_iip = (unsigned long)p->addr & ~0xFULL; |
| 761 | else |
| 762 | regs->cr_iip = bundle_addr & ~0xFULL; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 763 | |
| 764 | if (slot > 2) |
| 765 | slot = 0; |
| 766 | |
| 767 | ia64_psr(regs)->ri = slot; |
| 768 | |
| 769 | /* turn on single stepping */ |
| 770 | ia64_psr(regs)->ss = 1; |
| 771 | } |
| 772 | |
Keshavamurthy Anil S | 661e5a3 | 2005-09-06 15:19:32 -0700 | [diff] [blame] | 773 | static int __kprobes is_ia64_break_inst(struct pt_regs *regs) |
| 774 | { |
| 775 | unsigned int slot = ia64_psr(regs)->ri; |
Keshavamurthy Anil S | 661e5a3 | 2005-09-06 15:19:32 -0700 | [diff] [blame] | 776 | unsigned long *kprobe_addr = (unsigned long *)regs->cr_iip; |
| 777 | bundle_t bundle; |
| 778 | |
| 779 | memcpy(&bundle, kprobe_addr, sizeof(bundle_t)); |
Keshavamurthy Anil S | 661e5a3 | 2005-09-06 15:19:32 -0700 | [diff] [blame] | 780 | |
Masami Hiramatsu | 34e1ceb | 2008-03-28 14:27:02 -0700 | [diff] [blame] | 781 | return __is_ia64_break_inst(&bundle, slot); |
Keshavamurthy Anil S | 661e5a3 | 2005-09-06 15:19:32 -0700 | [diff] [blame] | 782 | } |
| 783 | |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 784 | static int __kprobes pre_kprobes_handler(struct die_args *args) |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 785 | { |
| 786 | struct kprobe *p; |
| 787 | int ret = 0; |
Keshavamurthy Anil S | 89cb14c | 2005-06-23 00:09:35 -0700 | [diff] [blame] | 788 | struct pt_regs *regs = args->regs; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 789 | kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs); |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 790 | struct kprobe_ctlblk *kcb; |
| 791 | |
| 792 | /* |
| 793 | * We don't want to be preempted for the entire |
| 794 | * duration of kprobe processing |
| 795 | */ |
| 796 | preempt_disable(); |
| 797 | kcb = get_kprobe_ctlblk(); |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 798 | |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 799 | /* Handle recursion cases */ |
| 800 | if (kprobe_running()) { |
| 801 | p = get_kprobe(addr); |
| 802 | if (p) { |
Ananth N Mavinakayanahalli | 8a5c4dc | 2005-11-07 01:00:09 -0800 | [diff] [blame] | 803 | if ((kcb->kprobe_status == KPROBE_HIT_SS) && |
Keshavamurthy Anil S | deac66a | 2005-09-06 15:19:35 -0700 | [diff] [blame] | 804 | (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)) { |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 805 | ia64_psr(regs)->ss = 0; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 806 | goto no_kprobe; |
| 807 | } |
Anil S Keshavamurthy | 852cacc | 2005-06-23 00:09:40 -0700 | [diff] [blame] | 808 | /* We have reentered the pre_kprobe_handler(), since |
| 809 | * another probe was hit while within the handler. |
| 810 | * We here save the original kprobes variables and |
| 811 | * just single step on the instruction of the new probe |
| 812 | * without calling any user handlers. |
| 813 | */ |
Ananth N Mavinakayanahalli | 8a5c4dc | 2005-11-07 01:00:09 -0800 | [diff] [blame] | 814 | save_previous_kprobe(kcb); |
| 815 | set_current_kprobe(p, kcb); |
Keshavamurthy Anil S | bf8d5c5 | 2005-12-12 00:37:34 -0800 | [diff] [blame] | 816 | kprobes_inc_nmissed_count(p); |
Anil S Keshavamurthy | 852cacc | 2005-06-23 00:09:40 -0700 | [diff] [blame] | 817 | prepare_ss(p, regs); |
Ananth N Mavinakayanahalli | 8a5c4dc | 2005-11-07 01:00:09 -0800 | [diff] [blame] | 818 | kcb->kprobe_status = KPROBE_REENTER; |
Anil S Keshavamurthy | 852cacc | 2005-06-23 00:09:40 -0700 | [diff] [blame] | 819 | return 1; |
Keshavamurthy Anil S | 89cb14c | 2005-06-23 00:09:35 -0700 | [diff] [blame] | 820 | } else if (args->err == __IA64_BREAK_JPROBE) { |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 821 | /* |
| 822 | * jprobe instrumented function just completed |
| 823 | */ |
Ananth N Mavinakayanahalli | 8a5c4dc | 2005-11-07 01:00:09 -0800 | [diff] [blame] | 824 | p = __get_cpu_var(current_kprobe); |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 825 | if (p->break_handler && p->break_handler(p, regs)) { |
| 826 | goto ss_probe; |
| 827 | } |
Keshavamurthy Anil S | eb3a729 | 2006-01-11 12:17:42 -0800 | [diff] [blame] | 828 | } else if (!is_ia64_break_inst(regs)) { |
| 829 | /* The breakpoint instruction was removed by |
| 830 | * another cpu right after we hit, no further |
| 831 | * handling of this interrupt is appropriate |
| 832 | */ |
| 833 | ret = 1; |
| 834 | goto no_kprobe; |
Keshavamurthy Anil S | 89cb14c | 2005-06-23 00:09:35 -0700 | [diff] [blame] | 835 | } else { |
| 836 | /* Not our break */ |
| 837 | goto no_kprobe; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 841 | p = get_kprobe(addr); |
| 842 | if (!p) { |
Keshavamurthy Anil S | 661e5a3 | 2005-09-06 15:19:32 -0700 | [diff] [blame] | 843 | if (!is_ia64_break_inst(regs)) { |
| 844 | /* |
| 845 | * The breakpoint instruction was removed right |
| 846 | * after we hit it. Another cpu has removed |
| 847 | * either a probepoint or a debugger breakpoint |
| 848 | * at this address. In either case, no further |
| 849 | * handling of this interrupt is appropriate. |
| 850 | */ |
| 851 | ret = 1; |
| 852 | |
| 853 | } |
| 854 | |
| 855 | /* Not one of our break, let kernel handle it */ |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 856 | goto no_kprobe; |
| 857 | } |
| 858 | |
Ananth N Mavinakayanahalli | 8a5c4dc | 2005-11-07 01:00:09 -0800 | [diff] [blame] | 859 | set_current_kprobe(p, kcb); |
| 860 | kcb->kprobe_status = KPROBE_HIT_ACTIVE; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 861 | |
| 862 | if (p->pre_handler && p->pre_handler(p, regs)) |
| 863 | /* |
| 864 | * Our pre-handler is specifically requesting that we just |
Rusty Lynch | 9508dbf | 2005-06-27 15:17:12 -0700 | [diff] [blame] | 865 | * do a return. This is used for both the jprobe pre-handler |
| 866 | * and the kretprobe trampoline |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 867 | */ |
| 868 | return 1; |
| 869 | |
| 870 | ss_probe: |
Masami Hiramatsu | 34e1ceb | 2008-03-28 14:27:02 -0700 | [diff] [blame] | 871 | #if !defined(CONFIG_PREEMPT) || defined(CONFIG_PM) |
| 872 | if (p->ainsn.inst_flag == INST_FLAG_BOOSTABLE && !p->post_handler) { |
| 873 | /* Boost up -- we can execute copied instructions directly */ |
| 874 | ia64_psr(regs)->ri = p->ainsn.slot; |
| 875 | regs->cr_iip = (unsigned long)&p->ainsn.insn->bundle & ~0xFULL; |
| 876 | /* turn single stepping off */ |
| 877 | ia64_psr(regs)->ss = 0; |
| 878 | |
| 879 | reset_current_kprobe(); |
| 880 | preempt_enable_no_resched(); |
| 881 | return 1; |
| 882 | } |
| 883 | #endif |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 884 | prepare_ss(p, regs); |
Ananth N Mavinakayanahalli | 8a5c4dc | 2005-11-07 01:00:09 -0800 | [diff] [blame] | 885 | kcb->kprobe_status = KPROBE_HIT_SS; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 886 | return 1; |
| 887 | |
| 888 | no_kprobe: |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 889 | preempt_enable_no_resched(); |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 890 | return ret; |
| 891 | } |
| 892 | |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 893 | static int __kprobes post_kprobes_handler(struct pt_regs *regs) |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 894 | { |
Ananth N Mavinakayanahalli | 8a5c4dc | 2005-11-07 01:00:09 -0800 | [diff] [blame] | 895 | struct kprobe *cur = kprobe_running(); |
| 896 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
| 897 | |
| 898 | if (!cur) |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 899 | return 0; |
| 900 | |
Ananth N Mavinakayanahalli | 8a5c4dc | 2005-11-07 01:00:09 -0800 | [diff] [blame] | 901 | if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) { |
| 902 | kcb->kprobe_status = KPROBE_HIT_SSDONE; |
| 903 | cur->post_handler(cur, regs, 0); |
Anil S Keshavamurthy | 852cacc | 2005-06-23 00:09:40 -0700 | [diff] [blame] | 904 | } |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 905 | |
Ananth N Mavinakayanahalli | 8a5c4dc | 2005-11-07 01:00:09 -0800 | [diff] [blame] | 906 | resume_execution(cur, regs); |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 907 | |
Anil S Keshavamurthy | 852cacc | 2005-06-23 00:09:40 -0700 | [diff] [blame] | 908 | /*Restore back the original saved kprobes variables and continue. */ |
Ananth N Mavinakayanahalli | 8a5c4dc | 2005-11-07 01:00:09 -0800 | [diff] [blame] | 909 | if (kcb->kprobe_status == KPROBE_REENTER) { |
| 910 | restore_previous_kprobe(kcb); |
Anil S Keshavamurthy | 852cacc | 2005-06-23 00:09:40 -0700 | [diff] [blame] | 911 | goto out; |
| 912 | } |
Ananth N Mavinakayanahalli | 8a5c4dc | 2005-11-07 01:00:09 -0800 | [diff] [blame] | 913 | reset_current_kprobe(); |
Anil S Keshavamurthy | 852cacc | 2005-06-23 00:09:40 -0700 | [diff] [blame] | 914 | |
| 915 | out: |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 916 | preempt_enable_no_resched(); |
| 917 | return 1; |
| 918 | } |
| 919 | |
Harvey Harrison | 45e18c2 | 2008-03-06 09:49:01 -0800 | [diff] [blame] | 920 | int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr) |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 921 | { |
Ananth N Mavinakayanahalli | 8a5c4dc | 2005-11-07 01:00:09 -0800 | [diff] [blame] | 922 | struct kprobe *cur = kprobe_running(); |
| 923 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
| 924 | |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 925 | |
Prasanna S Panchamukhi | c04c1c8 | 2006-03-26 01:38:25 -0800 | [diff] [blame] | 926 | switch(kcb->kprobe_status) { |
| 927 | case KPROBE_HIT_SS: |
| 928 | case KPROBE_REENTER: |
| 929 | /* |
| 930 | * We are here because the instruction being single |
| 931 | * stepped caused a page fault. We reset the current |
| 932 | * kprobe and the instruction pointer points back to |
| 933 | * the probe address and allow the page fault handler |
| 934 | * to continue as a normal page fault. |
| 935 | */ |
| 936 | regs->cr_iip = ((unsigned long)cur->addr) & ~0xFULL; |
| 937 | ia64_psr(regs)->ri = ((unsigned long)cur->addr) & 0xf; |
| 938 | if (kcb->kprobe_status == KPROBE_REENTER) |
| 939 | restore_previous_kprobe(kcb); |
| 940 | else |
| 941 | reset_current_kprobe(); |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 942 | preempt_enable_no_resched(); |
Prasanna S Panchamukhi | c04c1c8 | 2006-03-26 01:38:25 -0800 | [diff] [blame] | 943 | break; |
| 944 | case KPROBE_HIT_ACTIVE: |
| 945 | case KPROBE_HIT_SSDONE: |
| 946 | /* |
| 947 | * We increment the nmissed count for accounting, |
| 948 | * we can also use npre/npostfault count for accouting |
| 949 | * these specific fault cases. |
| 950 | */ |
| 951 | kprobes_inc_nmissed_count(cur); |
| 952 | |
| 953 | /* |
| 954 | * We come here because instructions in the pre/post |
| 955 | * handler caused the page_fault, this could happen |
| 956 | * if handler tries to access user space by |
| 957 | * copy_from_user(), get_user() etc. Let the |
| 958 | * user-specified handler try to fix it first. |
| 959 | */ |
| 960 | if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr)) |
| 961 | return 1; |
Keshavamurthy Anil S | fd32cb3 | 2006-09-25 16:32:20 -0700 | [diff] [blame] | 962 | /* |
| 963 | * In case the user-specified fault handler returned |
| 964 | * zero, try to fix up. |
| 965 | */ |
| 966 | if (ia64_done_with_exception(regs)) |
| 967 | return 1; |
Prasanna S Panchamukhi | c04c1c8 | 2006-03-26 01:38:25 -0800 | [diff] [blame] | 968 | |
| 969 | /* |
| 970 | * Let ia64_do_page_fault() fix it. |
| 971 | */ |
| 972 | break; |
| 973 | default: |
| 974 | break; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | return 0; |
| 978 | } |
| 979 | |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 980 | int __kprobes kprobe_exceptions_notify(struct notifier_block *self, |
| 981 | unsigned long val, void *data) |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 982 | { |
| 983 | struct die_args *args = (struct die_args *)data; |
Ananth N Mavinakayanahalli | 66ff2d0 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 984 | int ret = NOTIFY_DONE; |
| 985 | |
bibo,mao | 2326c77 | 2006-03-26 01:38:21 -0800 | [diff] [blame] | 986 | if (args->regs && user_mode(args->regs)) |
| 987 | return ret; |
| 988 | |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 989 | switch(val) { |
| 990 | case DIE_BREAK: |
Keith Owens | 9138d58 | 2005-11-07 11:27:13 -0800 | [diff] [blame] | 991 | /* err is break number from ia64_bad_break() */ |
Tony Luck | 08ed38b | 2006-11-14 09:33:38 -0800 | [diff] [blame] | 992 | if ((args->err >> 12) == (__IA64_BREAK_KPROBE >> 12) |
| 993 | || args->err == __IA64_BREAK_JPROBE |
| 994 | || args->err == 0) |
Keith Owens | 9138d58 | 2005-11-07 11:27:13 -0800 | [diff] [blame] | 995 | if (pre_kprobes_handler(args)) |
| 996 | ret = NOTIFY_STOP; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 997 | break; |
Keith Owens | 9138d58 | 2005-11-07 11:27:13 -0800 | [diff] [blame] | 998 | case DIE_FAULT: |
| 999 | /* err is vector number from ia64_fault() */ |
| 1000 | if (args->err == 36) |
| 1001 | if (post_kprobes_handler(args->regs)) |
| 1002 | ret = NOTIFY_STOP; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 1003 | break; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 1004 | default: |
| 1005 | break; |
| 1006 | } |
Ananth N Mavinakayanahalli | 66ff2d0 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 1007 | return ret; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
Zhang Yanmin | d3ef1f5 | 2006-01-13 14:45:21 -0800 | [diff] [blame] | 1010 | struct param_bsp_cfm { |
| 1011 | unsigned long ip; |
| 1012 | unsigned long *bsp; |
| 1013 | unsigned long cfm; |
| 1014 | }; |
| 1015 | |
| 1016 | static void ia64_get_bsp_cfm(struct unw_frame_info *info, void *arg) |
| 1017 | { |
| 1018 | unsigned long ip; |
| 1019 | struct param_bsp_cfm *lp = arg; |
| 1020 | |
| 1021 | do { |
| 1022 | unw_get_ip(info, &ip); |
| 1023 | if (ip == 0) |
| 1024 | break; |
| 1025 | if (ip == lp->ip) { |
| 1026 | unw_get_bsp(info, (unsigned long*)&lp->bsp); |
| 1027 | unw_get_cfm(info, (unsigned long*)&lp->cfm); |
| 1028 | return; |
| 1029 | } |
| 1030 | } while (unw_unwind(info) >= 0); |
Matthew Wilcox | d61b49c | 2006-10-26 12:22:32 -0600 | [diff] [blame] | 1031 | lp->bsp = NULL; |
Zhang Yanmin | d3ef1f5 | 2006-01-13 14:45:21 -0800 | [diff] [blame] | 1032 | lp->cfm = 0; |
| 1033 | return; |
| 1034 | } |
| 1035 | |
Michael Ellerman | 3d7e338 | 2007-07-19 01:48:11 -0700 | [diff] [blame] | 1036 | unsigned long arch_deref_entry_point(void *entry) |
| 1037 | { |
| 1038 | return ((struct fnptr *)entry)->ip; |
| 1039 | } |
| 1040 | |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 1041 | int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs) |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 1042 | { |
Anil S Keshavamurthy | b2761dc | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 1043 | struct jprobe *jp = container_of(p, struct jprobe, kp); |
Michael Ellerman | 3d7e338 | 2007-07-19 01:48:11 -0700 | [diff] [blame] | 1044 | unsigned long addr = arch_deref_entry_point(jp->entry); |
Ananth N Mavinakayanahalli | 8a5c4dc | 2005-11-07 01:00:09 -0800 | [diff] [blame] | 1045 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
Zhang Yanmin | d3ef1f5 | 2006-01-13 14:45:21 -0800 | [diff] [blame] | 1046 | struct param_bsp_cfm pa; |
| 1047 | int bytes; |
| 1048 | |
| 1049 | /* |
| 1050 | * Callee owns the argument space and could overwrite it, eg |
| 1051 | * tail call optimization. So to be absolutely safe |
Simon Arlott | 72fdbdc | 2007-05-11 14:55:43 -0700 | [diff] [blame] | 1052 | * we save the argument space before transferring the control |
Zhang Yanmin | d3ef1f5 | 2006-01-13 14:45:21 -0800 | [diff] [blame] | 1053 | * to instrumented jprobe function which runs in |
| 1054 | * the process context |
| 1055 | */ |
| 1056 | pa.ip = regs->cr_iip; |
| 1057 | unw_init_running(ia64_get_bsp_cfm, &pa); |
| 1058 | bytes = (char *)ia64_rse_skip_regs(pa.bsp, pa.cfm & 0x3f) |
| 1059 | - (char *)pa.bsp; |
| 1060 | memcpy( kcb->jprobes_saved_stacked_regs, |
| 1061 | pa.bsp, |
| 1062 | bytes ); |
| 1063 | kcb->bsp = pa.bsp; |
| 1064 | kcb->cfm = pa.cfm; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 1065 | |
Anil S Keshavamurthy | b2761dc | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 1066 | /* save architectural state */ |
Ananth N Mavinakayanahalli | 8a5c4dc | 2005-11-07 01:00:09 -0800 | [diff] [blame] | 1067 | kcb->jprobe_saved_regs = *regs; |
Anil S Keshavamurthy | b2761dc | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 1068 | |
| 1069 | /* after rfi, execute the jprobe instrumented function */ |
| 1070 | regs->cr_iip = addr & ~0xFULL; |
| 1071 | ia64_psr(regs)->ri = addr & 0xf; |
| 1072 | regs->r1 = ((struct fnptr *)(jp->entry))->gp; |
| 1073 | |
| 1074 | /* |
| 1075 | * fix the return address to our jprobe_inst_return() function |
| 1076 | * in the jprobes.S file |
| 1077 | */ |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 1078 | regs->b0 = ((struct fnptr *)(jprobe_inst_return))->ip; |
Anil S Keshavamurthy | b2761dc | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 1079 | |
| 1080 | return 1; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
Adrian Bunk | 9dad6f5 | 2008-03-03 20:07:22 +0200 | [diff] [blame] | 1083 | /* ia64 does not need this */ |
| 1084 | void __kprobes jprobe_return(void) |
| 1085 | { |
| 1086 | } |
| 1087 | |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 1088 | int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs) |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 1089 | { |
Ananth N Mavinakayanahalli | 8a5c4dc | 2005-11-07 01:00:09 -0800 | [diff] [blame] | 1090 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
Zhang Yanmin | d3ef1f5 | 2006-01-13 14:45:21 -0800 | [diff] [blame] | 1091 | int bytes; |
Ananth N Mavinakayanahalli | 8a5c4dc | 2005-11-07 01:00:09 -0800 | [diff] [blame] | 1092 | |
Zhang Yanmin | d3ef1f5 | 2006-01-13 14:45:21 -0800 | [diff] [blame] | 1093 | /* restoring architectural state */ |
Ananth N Mavinakayanahalli | 8a5c4dc | 2005-11-07 01:00:09 -0800 | [diff] [blame] | 1094 | *regs = kcb->jprobe_saved_regs; |
Zhang Yanmin | d3ef1f5 | 2006-01-13 14:45:21 -0800 | [diff] [blame] | 1095 | |
| 1096 | /* restoring the original argument space */ |
| 1097 | flush_register_stack(); |
| 1098 | bytes = (char *)ia64_rse_skip_regs(kcb->bsp, kcb->cfm & 0x3f) |
| 1099 | - (char *)kcb->bsp; |
| 1100 | memcpy( kcb->bsp, |
| 1101 | kcb->jprobes_saved_stacked_regs, |
| 1102 | bytes ); |
| 1103 | invalidate_stacked_regs(); |
| 1104 | |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 1105 | preempt_enable_no_resched(); |
Anil S Keshavamurthy | b2761dc | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 1106 | return 1; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 1107 | } |
Rusty Lynch | 9508dbf | 2005-06-27 15:17:12 -0700 | [diff] [blame] | 1108 | |
| 1109 | static struct kprobe trampoline_p = { |
| 1110 | .pre_handler = trampoline_probe_handler |
| 1111 | }; |
| 1112 | |
Rusty Lynch | 6772926 | 2005-07-05 18:54:50 -0700 | [diff] [blame] | 1113 | int __init arch_init_kprobes(void) |
Rusty Lynch | 9508dbf | 2005-06-27 15:17:12 -0700 | [diff] [blame] | 1114 | { |
| 1115 | trampoline_p.addr = |
| 1116 | (kprobe_opcode_t *)((struct fnptr *)kretprobe_trampoline)->ip; |
| 1117 | return register_kprobe(&trampoline_p); |
| 1118 | } |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 1119 | |
| 1120 | int __kprobes arch_trampoline_kprobe(struct kprobe *p) |
| 1121 | { |
| 1122 | if (p->addr == |
| 1123 | (kprobe_opcode_t *)((struct fnptr *)kretprobe_trampoline)->ip) |
| 1124 | return 1; |
| 1125 | |
| 1126 | return 0; |
| 1127 | } |