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 | |
| 26 | #include <linux/config.h> |
| 27 | #include <linux/kprobes.h> |
| 28 | #include <linux/ptrace.h> |
| 29 | #include <linux/spinlock.h> |
| 30 | #include <linux/string.h> |
| 31 | #include <linux/slab.h> |
| 32 | #include <linux/preempt.h> |
| 33 | #include <linux/moduleloader.h> |
| 34 | |
| 35 | #include <asm/pgtable.h> |
| 36 | #include <asm/kdebug.h> |
Keshavamurthy Anil S | c7b645f | 2005-06-27 15:17:16 -0700 | [diff] [blame] | 37 | #include <asm/sections.h> |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 38 | |
Anil S Keshavamurthy | b2761dc | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 39 | extern void jprobe_inst_return(void); |
| 40 | |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 41 | /* kprobe_status settings */ |
| 42 | #define KPROBE_HIT_ACTIVE 0x00000001 |
| 43 | #define KPROBE_HIT_SS 0x00000002 |
| 44 | |
Anil S Keshavamurthy | 852cacc | 2005-06-23 00:09:40 -0700 | [diff] [blame] | 45 | static struct kprobe *current_kprobe, *kprobe_prev; |
| 46 | static unsigned long kprobe_status, kprobe_status_prev; |
Anil S Keshavamurthy | b2761dc | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 47 | static struct pt_regs jprobe_saved_regs; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 48 | |
| 49 | enum instruction_type {A, I, M, F, B, L, X, u}; |
| 50 | static enum instruction_type bundle_encoding[32][3] = { |
| 51 | { M, I, I }, /* 00 */ |
| 52 | { M, I, I }, /* 01 */ |
| 53 | { M, I, I }, /* 02 */ |
| 54 | { M, I, I }, /* 03 */ |
| 55 | { M, L, X }, /* 04 */ |
| 56 | { M, L, X }, /* 05 */ |
| 57 | { u, u, u }, /* 06 */ |
| 58 | { u, u, u }, /* 07 */ |
| 59 | { M, M, I }, /* 08 */ |
| 60 | { M, M, I }, /* 09 */ |
| 61 | { M, M, I }, /* 0A */ |
| 62 | { M, M, I }, /* 0B */ |
| 63 | { M, F, I }, /* 0C */ |
| 64 | { M, F, I }, /* 0D */ |
| 65 | { M, M, F }, /* 0E */ |
| 66 | { M, M, F }, /* 0F */ |
| 67 | { M, I, B }, /* 10 */ |
| 68 | { M, I, B }, /* 11 */ |
| 69 | { M, B, B }, /* 12 */ |
| 70 | { M, B, B }, /* 13 */ |
| 71 | { u, u, u }, /* 14 */ |
| 72 | { u, u, u }, /* 15 */ |
| 73 | { B, B, B }, /* 16 */ |
| 74 | { B, B, B }, /* 17 */ |
| 75 | { M, M, B }, /* 18 */ |
| 76 | { M, M, B }, /* 19 */ |
| 77 | { u, u, u }, /* 1A */ |
| 78 | { u, u, u }, /* 1B */ |
| 79 | { M, F, B }, /* 1C */ |
| 80 | { M, F, B }, /* 1D */ |
| 81 | { u, u, u }, /* 1E */ |
| 82 | { u, u, u }, /* 1F */ |
| 83 | }; |
| 84 | |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 85 | /* |
| 86 | * In this function we check to see if the instruction |
| 87 | * is IP relative instruction and update the kprobe |
| 88 | * inst flag accordingly |
| 89 | */ |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame^] | 90 | static void __kprobes update_kprobe_inst_flag(uint template, uint slot, |
| 91 | uint major_opcode, |
| 92 | unsigned long kprobe_inst, |
| 93 | struct kprobe *p) |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 94 | { |
Rusty Lynch | 8bc7677 | 2005-06-23 00:09:30 -0700 | [diff] [blame] | 95 | p->ainsn.inst_flag = 0; |
| 96 | p->ainsn.target_br_reg = 0; |
| 97 | |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 98 | if (bundle_encoding[template][slot] == B) { |
| 99 | switch (major_opcode) { |
| 100 | case INDIRECT_CALL_OPCODE: |
| 101 | p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG; |
| 102 | p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7); |
| 103 | break; |
| 104 | case IP_RELATIVE_PREDICT_OPCODE: |
| 105 | case IP_RELATIVE_BRANCH_OPCODE: |
| 106 | p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR; |
| 107 | break; |
| 108 | case IP_RELATIVE_CALL_OPCODE: |
| 109 | p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR; |
| 110 | p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG; |
| 111 | p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7); |
| 112 | break; |
| 113 | } |
| 114 | } else if (bundle_encoding[template][slot] == X) { |
| 115 | switch (major_opcode) { |
| 116 | case LONG_CALL_OPCODE: |
| 117 | p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG; |
| 118 | p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7); |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | return; |
| 123 | } |
Rusty Lynch | 8bc7677 | 2005-06-23 00:09:30 -0700 | [diff] [blame] | 124 | |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 125 | /* |
Anil S Keshavamurthy | 1674eaf | 2005-06-23 00:09:33 -0700 | [diff] [blame] | 126 | * In this function we check to see if the instruction |
Anil S Keshavamurthy | 708de8f | 2005-06-23 00:09:34 -0700 | [diff] [blame] | 127 | * on which we are inserting kprobe is supported. |
| 128 | * Returns 0 if supported |
| 129 | * Returns -EINVAL if unsupported |
| 130 | */ |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame^] | 131 | static int __kprobes unsupported_inst(uint template, uint slot, |
| 132 | uint major_opcode, |
| 133 | unsigned long kprobe_inst, |
| 134 | struct kprobe *p) |
Anil S Keshavamurthy | 708de8f | 2005-06-23 00:09:34 -0700 | [diff] [blame] | 135 | { |
| 136 | unsigned long addr = (unsigned long)p->addr; |
| 137 | |
| 138 | if (bundle_encoding[template][slot] == I) { |
| 139 | switch (major_opcode) { |
| 140 | case 0x0: //I_UNIT_MISC_OPCODE: |
| 141 | /* |
| 142 | * Check for Integer speculation instruction |
| 143 | * - Bit 33-35 to be equal to 0x1 |
| 144 | */ |
| 145 | if (((kprobe_inst >> 33) & 0x7) == 1) { |
| 146 | printk(KERN_WARNING |
| 147 | "Kprobes on speculation inst at <0x%lx> not supported\n", |
| 148 | addr); |
| 149 | return -EINVAL; |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * IP relative mov instruction |
| 154 | * - Bit 27-35 to be equal to 0x30 |
| 155 | */ |
| 156 | if (((kprobe_inst >> 27) & 0x1FF) == 0x30) { |
| 157 | printk(KERN_WARNING |
| 158 | "Kprobes on \"mov r1=ip\" at <0x%lx> not supported\n", |
| 159 | addr); |
| 160 | return -EINVAL; |
| 161 | |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /* |
| 170 | * In this function we check to see if the instruction |
Anil S Keshavamurthy | 1674eaf | 2005-06-23 00:09:33 -0700 | [diff] [blame] | 171 | * (qp) cmpx.crel.ctype p1,p2=r2,r3 |
| 172 | * on which we are inserting kprobe is cmp instruction |
| 173 | * with ctype as unc. |
| 174 | */ |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame^] | 175 | static uint __kprobes is_cmp_ctype_unc_inst(uint template, uint slot, |
| 176 | uint major_opcode, |
| 177 | unsigned long kprobe_inst) |
Anil S Keshavamurthy | 1674eaf | 2005-06-23 00:09:33 -0700 | [diff] [blame] | 178 | { |
| 179 | cmp_inst_t cmp_inst; |
| 180 | uint ctype_unc = 0; |
| 181 | |
| 182 | if (!((bundle_encoding[template][slot] == I) || |
| 183 | (bundle_encoding[template][slot] == M))) |
| 184 | goto out; |
| 185 | |
| 186 | if (!((major_opcode == 0xC) || (major_opcode == 0xD) || |
| 187 | (major_opcode == 0xE))) |
| 188 | goto out; |
| 189 | |
| 190 | cmp_inst.l = kprobe_inst; |
| 191 | if ((cmp_inst.f.x2 == 0) || (cmp_inst.f.x2 == 1)) { |
| 192 | /* Integere compare - Register Register (A6 type)*/ |
| 193 | if ((cmp_inst.f.tb == 0) && (cmp_inst.f.ta == 0) |
| 194 | &&(cmp_inst.f.c == 1)) |
| 195 | ctype_unc = 1; |
| 196 | } else if ((cmp_inst.f.x2 == 2)||(cmp_inst.f.x2 == 3)) { |
| 197 | /* Integere compare - Immediate Register (A8 type)*/ |
| 198 | if ((cmp_inst.f.ta == 0) &&(cmp_inst.f.c == 1)) |
| 199 | ctype_unc = 1; |
| 200 | } |
| 201 | out: |
| 202 | return ctype_unc; |
| 203 | } |
| 204 | |
| 205 | /* |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 206 | * In this function we override the bundle with |
| 207 | * the break instruction at the given slot. |
| 208 | */ |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame^] | 209 | static void __kprobes prepare_break_inst(uint template, uint slot, |
| 210 | uint major_opcode, |
| 211 | unsigned long kprobe_inst, |
| 212 | struct kprobe *p) |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 213 | { |
| 214 | unsigned long break_inst = BREAK_INST; |
| 215 | bundle_t *bundle = &p->ainsn.insn.bundle; |
| 216 | |
| 217 | /* |
| 218 | * Copy the original kprobe_inst qualifying predicate(qp) |
Anil S Keshavamurthy | 1674eaf | 2005-06-23 00:09:33 -0700 | [diff] [blame] | 219 | * to the break instruction iff !is_cmp_ctype_unc_inst |
| 220 | * because for cmp instruction with ctype equal to unc, |
| 221 | * which is a special instruction always needs to be |
| 222 | * executed regradless of qp |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 223 | */ |
Anil S Keshavamurthy | 1674eaf | 2005-06-23 00:09:33 -0700 | [diff] [blame] | 224 | if (!is_cmp_ctype_unc_inst(template, slot, major_opcode, kprobe_inst)) |
| 225 | break_inst |= (0x3f & kprobe_inst); |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 226 | |
| 227 | switch (slot) { |
| 228 | case 0: |
| 229 | bundle->quad0.slot0 = break_inst; |
| 230 | break; |
| 231 | case 1: |
| 232 | bundle->quad0.slot1_p0 = break_inst; |
| 233 | bundle->quad1.slot1_p1 = break_inst >> (64-46); |
| 234 | break; |
| 235 | case 2: |
| 236 | bundle->quad1.slot2 = break_inst; |
| 237 | break; |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * Update the instruction flag, so that we can |
| 242 | * emulate the instruction properly after we |
| 243 | * single step on original instruction |
| 244 | */ |
| 245 | update_kprobe_inst_flag(template, slot, major_opcode, kprobe_inst, p); |
| 246 | } |
| 247 | |
| 248 | static inline void get_kprobe_inst(bundle_t *bundle, uint slot, |
| 249 | unsigned long *kprobe_inst, uint *major_opcode) |
| 250 | { |
| 251 | unsigned long kprobe_inst_p0, kprobe_inst_p1; |
| 252 | unsigned int template; |
| 253 | |
| 254 | template = bundle->quad0.template; |
| 255 | |
| 256 | switch (slot) { |
| 257 | case 0: |
| 258 | *major_opcode = (bundle->quad0.slot0 >> SLOT0_OPCODE_SHIFT); |
| 259 | *kprobe_inst = bundle->quad0.slot0; |
| 260 | break; |
| 261 | case 1: |
| 262 | *major_opcode = (bundle->quad1.slot1_p1 >> SLOT1_p1_OPCODE_SHIFT); |
| 263 | kprobe_inst_p0 = bundle->quad0.slot1_p0; |
| 264 | kprobe_inst_p1 = bundle->quad1.slot1_p1; |
| 265 | *kprobe_inst = kprobe_inst_p0 | (kprobe_inst_p1 << (64-46)); |
| 266 | break; |
| 267 | case 2: |
| 268 | *major_opcode = (bundle->quad1.slot2 >> SLOT2_OPCODE_SHIFT); |
| 269 | *kprobe_inst = bundle->quad1.slot2; |
| 270 | break; |
| 271 | } |
| 272 | } |
| 273 | |
Keshavamurthy Anil S | c7b645f | 2005-06-27 15:17:16 -0700 | [diff] [blame] | 274 | /* Returns non-zero if the addr is in the Interrupt Vector Table */ |
| 275 | static inline int in_ivt_functions(unsigned long addr) |
| 276 | { |
| 277 | return (addr >= (unsigned long)__start_ivt_text |
| 278 | && addr < (unsigned long)__end_ivt_text); |
| 279 | } |
| 280 | |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame^] | 281 | static int __kprobes valid_kprobe_addr(int template, int slot, |
| 282 | unsigned long addr) |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 283 | { |
| 284 | if ((slot > 2) || ((bundle_encoding[template][1] == L) && slot > 1)) { |
Keshavamurthy Anil S | c7b645f | 2005-06-27 15:17:16 -0700 | [diff] [blame] | 285 | printk(KERN_WARNING "Attempting to insert unaligned kprobe " |
| 286 | "at 0x%lx\n", addr); |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 287 | return -EINVAL; |
| 288 | } |
Rusty Lynch | a528e21 | 2005-06-27 15:17:15 -0700 | [diff] [blame] | 289 | |
Keshavamurthy Anil S | c7b645f | 2005-06-27 15:17:16 -0700 | [diff] [blame] | 290 | if (in_ivt_functions(addr)) { |
| 291 | printk(KERN_WARNING "Kprobes can't be inserted inside " |
| 292 | "IVT functions at 0x%lx\n", addr); |
| 293 | return -EINVAL; |
| 294 | } |
| 295 | |
Rusty Lynch | a528e21 | 2005-06-27 15:17:15 -0700 | [diff] [blame] | 296 | if (slot == 1 && bundle_encoding[template][1] != L) { |
| 297 | printk(KERN_WARNING "Inserting kprobes on slot #1 " |
| 298 | "is not supported\n"); |
| 299 | return -EINVAL; |
| 300 | } |
| 301 | |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 302 | return 0; |
| 303 | } |
Rusty Lynch | 8bc7677 | 2005-06-23 00:09:30 -0700 | [diff] [blame] | 304 | |
Anil S Keshavamurthy | 852cacc | 2005-06-23 00:09:40 -0700 | [diff] [blame] | 305 | static inline void save_previous_kprobe(void) |
| 306 | { |
| 307 | kprobe_prev = current_kprobe; |
| 308 | kprobe_status_prev = kprobe_status; |
| 309 | } |
| 310 | |
| 311 | static inline void restore_previous_kprobe(void) |
| 312 | { |
| 313 | current_kprobe = kprobe_prev; |
| 314 | kprobe_status = kprobe_status_prev; |
| 315 | } |
| 316 | |
| 317 | static inline void set_current_kprobe(struct kprobe *p) |
| 318 | { |
| 319 | current_kprobe = p; |
| 320 | } |
| 321 | |
Rusty Lynch | 9508dbf | 2005-06-27 15:17:12 -0700 | [diff] [blame] | 322 | static void kretprobe_trampoline(void) |
| 323 | { |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * At this point the target function has been tricked into |
| 328 | * returning into our trampoline. Lookup the associated instance |
| 329 | * and then: |
| 330 | * - call the handler function |
| 331 | * - cleanup by marking the instance as unused |
| 332 | * - long jump back to the original return address |
| 333 | */ |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame^] | 334 | int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs) |
Rusty Lynch | 9508dbf | 2005-06-27 15:17:12 -0700 | [diff] [blame] | 335 | { |
| 336 | struct kretprobe_instance *ri = NULL; |
| 337 | struct hlist_head *head; |
| 338 | struct hlist_node *node, *tmp; |
| 339 | unsigned long orig_ret_address = 0; |
| 340 | unsigned long trampoline_address = |
| 341 | ((struct fnptr *)kretprobe_trampoline)->ip; |
| 342 | |
| 343 | head = kretprobe_inst_table_head(current); |
| 344 | |
| 345 | /* |
| 346 | * It is possible to have multiple instances associated with a given |
| 347 | * task either because an multiple functions in the call path |
| 348 | * have a return probe installed on them, and/or more then one return |
| 349 | * return probe was registered for a target function. |
| 350 | * |
| 351 | * We can handle this because: |
| 352 | * - instances are always inserted at the head of the list |
| 353 | * - when multiple return probes are registered for the same |
| 354 | * function, the first instance's ret_addr will point to the |
| 355 | * real return address, and all the rest will point to |
| 356 | * kretprobe_trampoline |
| 357 | */ |
| 358 | hlist_for_each_entry_safe(ri, node, tmp, head, hlist) { |
| 359 | if (ri->task != current) |
| 360 | /* another task is sharing our hash bucket */ |
| 361 | continue; |
| 362 | |
| 363 | if (ri->rp && ri->rp->handler) |
| 364 | ri->rp->handler(ri, regs); |
| 365 | |
| 366 | orig_ret_address = (unsigned long)ri->ret_addr; |
| 367 | recycle_rp_inst(ri); |
| 368 | |
| 369 | if (orig_ret_address != trampoline_address) |
| 370 | /* |
| 371 | * This is the real return address. Any other |
| 372 | * instances associated with this task are for |
| 373 | * other calls deeper on the call stack |
| 374 | */ |
| 375 | break; |
| 376 | } |
| 377 | |
| 378 | BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address)); |
| 379 | regs->cr_iip = orig_ret_address; |
| 380 | |
| 381 | unlock_kprobes(); |
| 382 | preempt_enable_no_resched(); |
| 383 | |
| 384 | /* |
| 385 | * By returning a non-zero value, we are telling |
| 386 | * kprobe_handler() that we have handled unlocking |
| 387 | * and re-enabling preemption. |
| 388 | */ |
| 389 | return 1; |
| 390 | } |
| 391 | |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame^] | 392 | void __kprobes arch_prepare_kretprobe(struct kretprobe *rp, |
| 393 | struct pt_regs *regs) |
Rusty Lynch | 9508dbf | 2005-06-27 15:17:12 -0700 | [diff] [blame] | 394 | { |
| 395 | struct kretprobe_instance *ri; |
| 396 | |
| 397 | if ((ri = get_free_rp_inst(rp)) != NULL) { |
| 398 | ri->rp = rp; |
| 399 | ri->task = current; |
| 400 | ri->ret_addr = (kprobe_opcode_t *)regs->b0; |
| 401 | |
| 402 | /* Replace the return addr with trampoline addr */ |
| 403 | regs->b0 = ((struct fnptr *)kretprobe_trampoline)->ip; |
| 404 | |
| 405 | add_rp_inst(ri); |
| 406 | } else { |
| 407 | rp->nmissed++; |
| 408 | } |
| 409 | } |
| 410 | |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame^] | 411 | int __kprobes arch_prepare_kprobe(struct kprobe *p) |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 412 | { |
| 413 | unsigned long addr = (unsigned long) p->addr; |
| 414 | unsigned long *kprobe_addr = (unsigned long *)(addr & ~0xFULL); |
| 415 | unsigned long kprobe_inst=0; |
| 416 | unsigned int slot = addr & 0xf, template, major_opcode = 0; |
| 417 | bundle_t *bundle = &p->ainsn.insn.bundle; |
Rusty Lynch | 8bc7677 | 2005-06-23 00:09:30 -0700 | [diff] [blame] | 418 | |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 419 | memcpy(&p->opcode.bundle, kprobe_addr, sizeof(bundle_t)); |
| 420 | memcpy(&p->ainsn.insn.bundle, kprobe_addr, sizeof(bundle_t)); |
Rusty Lynch | 8bc7677 | 2005-06-23 00:09:30 -0700 | [diff] [blame] | 421 | |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 422 | template = bundle->quad0.template; |
| 423 | |
| 424 | if(valid_kprobe_addr(template, slot, addr)) |
| 425 | return -EINVAL; |
| 426 | |
| 427 | /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */ |
| 428 | if (slot == 1 && bundle_encoding[template][1] == L) |
| 429 | slot++; |
| 430 | |
| 431 | /* Get kprobe_inst and major_opcode from the bundle */ |
| 432 | get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode); |
| 433 | |
Anil S Keshavamurthy | 708de8f | 2005-06-23 00:09:34 -0700 | [diff] [blame] | 434 | if (unsupported_inst(template, slot, major_opcode, kprobe_inst, p)) |
| 435 | return -EINVAL; |
| 436 | |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 437 | prepare_break_inst(template, slot, major_opcode, kprobe_inst, p); |
Rusty Lynch | 8bc7677 | 2005-06-23 00:09:30 -0700 | [diff] [blame] | 438 | |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 439 | return 0; |
| 440 | } |
| 441 | |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame^] | 442 | void __kprobes arch_arm_kprobe(struct kprobe *p) |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 443 | { |
| 444 | unsigned long addr = (unsigned long)p->addr; |
| 445 | unsigned long arm_addr = addr & ~0xFULL; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 446 | |
Rusty Lynch | 8bc7677 | 2005-06-23 00:09:30 -0700 | [diff] [blame] | 447 | memcpy((char *)arm_addr, &p->ainsn.insn.bundle, sizeof(bundle_t)); |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 448 | flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t)); |
| 449 | } |
| 450 | |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame^] | 451 | void __kprobes arch_disarm_kprobe(struct kprobe *p) |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 452 | { |
| 453 | unsigned long addr = (unsigned long)p->addr; |
| 454 | unsigned long arm_addr = addr & ~0xFULL; |
| 455 | |
| 456 | /* p->opcode contains the original unaltered bundle */ |
| 457 | memcpy((char *) arm_addr, (char *) &p->opcode.bundle, sizeof(bundle_t)); |
| 458 | flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t)); |
| 459 | } |
| 460 | |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame^] | 461 | void __kprobes arch_remove_kprobe(struct kprobe *p) |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 462 | { |
| 463 | } |
| 464 | |
| 465 | /* |
| 466 | * We are resuming execution after a single step fault, so the pt_regs |
| 467 | * structure reflects the register state after we executed the instruction |
| 468 | * 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] | 469 | * the ip to point back to the original stack address. To set the IP address |
| 470 | * to original stack address, handle the case where we need to fixup the |
| 471 | * relative IP address and/or fixup branch register. |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 472 | */ |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame^] | 473 | 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] | 474 | { |
Rusty Lynch | 8bc7677 | 2005-06-23 00:09:30 -0700 | [diff] [blame] | 475 | unsigned long bundle_addr = ((unsigned long) (&p->opcode.bundle)) & ~0xFULL; |
Anil S Keshavamurthy | cd2675b | 2005-06-23 00:09:29 -0700 | [diff] [blame] | 476 | unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL; |
| 477 | unsigned long template; |
| 478 | int slot = ((unsigned long)p->addr & 0xf); |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 479 | |
Anil S Keshavamurthy | cd2675b | 2005-06-23 00:09:29 -0700 | [diff] [blame] | 480 | template = p->opcode.bundle.quad0.template; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 481 | |
Anil S Keshavamurthy | cd2675b | 2005-06-23 00:09:29 -0700 | [diff] [blame] | 482 | if (slot == 1 && bundle_encoding[template][1] == L) |
| 483 | slot = 2; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 484 | |
Anil S Keshavamurthy | cd2675b | 2005-06-23 00:09:29 -0700 | [diff] [blame] | 485 | if (p->ainsn.inst_flag) { |
| 486 | |
| 487 | if (p->ainsn.inst_flag & INST_FLAG_FIX_RELATIVE_IP_ADDR) { |
| 488 | /* Fix relative IP address */ |
| 489 | regs->cr_iip = (regs->cr_iip - bundle_addr) + resume_addr; |
| 490 | } |
| 491 | |
| 492 | if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) { |
| 493 | /* |
| 494 | * Fix target branch register, software convention is |
| 495 | * to use either b0 or b6 or b7, so just checking |
| 496 | * only those registers |
| 497 | */ |
| 498 | switch (p->ainsn.target_br_reg) { |
| 499 | case 0: |
| 500 | if ((regs->b0 == bundle_addr) || |
| 501 | (regs->b0 == bundle_addr + 0x10)) { |
| 502 | regs->b0 = (regs->b0 - bundle_addr) + |
| 503 | resume_addr; |
| 504 | } |
| 505 | break; |
| 506 | case 6: |
| 507 | if ((regs->b6 == bundle_addr) || |
| 508 | (regs->b6 == bundle_addr + 0x10)) { |
| 509 | regs->b6 = (regs->b6 - bundle_addr) + |
| 510 | resume_addr; |
| 511 | } |
| 512 | break; |
| 513 | case 7: |
| 514 | if ((regs->b7 == bundle_addr) || |
| 515 | (regs->b7 == bundle_addr + 0x10)) { |
| 516 | regs->b7 = (regs->b7 - bundle_addr) + |
| 517 | resume_addr; |
| 518 | } |
| 519 | break; |
| 520 | } /* end switch */ |
| 521 | } |
| 522 | goto turn_ss_off; |
| 523 | } |
| 524 | |
| 525 | if (slot == 2) { |
| 526 | if (regs->cr_iip == bundle_addr + 0x10) { |
| 527 | regs->cr_iip = resume_addr + 0x10; |
| 528 | } |
| 529 | } else { |
| 530 | if (regs->cr_iip == bundle_addr) { |
| 531 | regs->cr_iip = resume_addr; |
| 532 | } |
Anil S Keshavamurthy | a540318 | 2005-06-23 00:09:32 -0700 | [diff] [blame] | 533 | } |
Anil S Keshavamurthy | cd2675b | 2005-06-23 00:09:29 -0700 | [diff] [blame] | 534 | |
| 535 | turn_ss_off: |
| 536 | /* Turn off Single Step bit */ |
| 537 | ia64_psr(regs)->ss = 0; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame^] | 540 | 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] | 541 | { |
Rusty Lynch | 8bc7677 | 2005-06-23 00:09:30 -0700 | [diff] [blame] | 542 | unsigned long bundle_addr = (unsigned long) &p->opcode.bundle; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 543 | unsigned long slot = (unsigned long)p->addr & 0xf; |
| 544 | |
| 545 | /* Update instruction pointer (IIP) and slot number (IPSR.ri) */ |
| 546 | regs->cr_iip = bundle_addr & ~0xFULL; |
| 547 | |
| 548 | if (slot > 2) |
| 549 | slot = 0; |
| 550 | |
| 551 | ia64_psr(regs)->ri = slot; |
| 552 | |
| 553 | /* turn on single stepping */ |
| 554 | ia64_psr(regs)->ss = 1; |
| 555 | } |
| 556 | |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame^] | 557 | static int __kprobes pre_kprobes_handler(struct die_args *args) |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 558 | { |
| 559 | struct kprobe *p; |
| 560 | int ret = 0; |
Keshavamurthy Anil S | 89cb14c | 2005-06-23 00:09:35 -0700 | [diff] [blame] | 561 | struct pt_regs *regs = args->regs; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 562 | kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs); |
| 563 | |
| 564 | preempt_disable(); |
| 565 | |
| 566 | /* Handle recursion cases */ |
| 567 | if (kprobe_running()) { |
| 568 | p = get_kprobe(addr); |
| 569 | if (p) { |
| 570 | if (kprobe_status == KPROBE_HIT_SS) { |
| 571 | unlock_kprobes(); |
| 572 | goto no_kprobe; |
| 573 | } |
Anil S Keshavamurthy | 852cacc | 2005-06-23 00:09:40 -0700 | [diff] [blame] | 574 | /* We have reentered the pre_kprobe_handler(), since |
| 575 | * another probe was hit while within the handler. |
| 576 | * We here save the original kprobes variables and |
| 577 | * just single step on the instruction of the new probe |
| 578 | * without calling any user handlers. |
| 579 | */ |
| 580 | save_previous_kprobe(); |
| 581 | set_current_kprobe(p); |
| 582 | p->nmissed++; |
| 583 | prepare_ss(p, regs); |
| 584 | kprobe_status = KPROBE_REENTER; |
| 585 | return 1; |
Keshavamurthy Anil S | 89cb14c | 2005-06-23 00:09:35 -0700 | [diff] [blame] | 586 | } else if (args->err == __IA64_BREAK_JPROBE) { |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 587 | /* |
| 588 | * jprobe instrumented function just completed |
| 589 | */ |
| 590 | p = current_kprobe; |
| 591 | if (p->break_handler && p->break_handler(p, regs)) { |
| 592 | goto ss_probe; |
| 593 | } |
Keshavamurthy Anil S | 89cb14c | 2005-06-23 00:09:35 -0700 | [diff] [blame] | 594 | } else { |
| 595 | /* Not our break */ |
| 596 | goto no_kprobe; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | |
| 600 | lock_kprobes(); |
| 601 | p = get_kprobe(addr); |
| 602 | if (!p) { |
| 603 | unlock_kprobes(); |
| 604 | goto no_kprobe; |
| 605 | } |
| 606 | |
| 607 | kprobe_status = KPROBE_HIT_ACTIVE; |
Anil S Keshavamurthy | 852cacc | 2005-06-23 00:09:40 -0700 | [diff] [blame] | 608 | set_current_kprobe(p); |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 609 | |
| 610 | if (p->pre_handler && p->pre_handler(p, regs)) |
| 611 | /* |
| 612 | * Our pre-handler is specifically requesting that we just |
Rusty Lynch | 9508dbf | 2005-06-27 15:17:12 -0700 | [diff] [blame] | 613 | * do a return. This is used for both the jprobe pre-handler |
| 614 | * and the kretprobe trampoline |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 615 | */ |
| 616 | return 1; |
| 617 | |
| 618 | ss_probe: |
| 619 | prepare_ss(p, regs); |
| 620 | kprobe_status = KPROBE_HIT_SS; |
| 621 | return 1; |
| 622 | |
| 623 | no_kprobe: |
| 624 | preempt_enable_no_resched(); |
| 625 | return ret; |
| 626 | } |
| 627 | |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame^] | 628 | static int __kprobes post_kprobes_handler(struct pt_regs *regs) |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 629 | { |
| 630 | if (!kprobe_running()) |
| 631 | return 0; |
| 632 | |
Anil S Keshavamurthy | 852cacc | 2005-06-23 00:09:40 -0700 | [diff] [blame] | 633 | if ((kprobe_status != KPROBE_REENTER) && current_kprobe->post_handler) { |
| 634 | kprobe_status = KPROBE_HIT_SSDONE; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 635 | current_kprobe->post_handler(current_kprobe, regs, 0); |
Anil S Keshavamurthy | 852cacc | 2005-06-23 00:09:40 -0700 | [diff] [blame] | 636 | } |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 637 | |
| 638 | resume_execution(current_kprobe, regs); |
| 639 | |
Anil S Keshavamurthy | 852cacc | 2005-06-23 00:09:40 -0700 | [diff] [blame] | 640 | /*Restore back the original saved kprobes variables and continue. */ |
| 641 | if (kprobe_status == KPROBE_REENTER) { |
| 642 | restore_previous_kprobe(); |
| 643 | goto out; |
| 644 | } |
| 645 | |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 646 | unlock_kprobes(); |
Anil S Keshavamurthy | 852cacc | 2005-06-23 00:09:40 -0700 | [diff] [blame] | 647 | |
| 648 | out: |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 649 | preempt_enable_no_resched(); |
| 650 | return 1; |
| 651 | } |
| 652 | |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame^] | 653 | static int __kprobes kprobes_fault_handler(struct pt_regs *regs, int trapnr) |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 654 | { |
| 655 | if (!kprobe_running()) |
| 656 | return 0; |
| 657 | |
| 658 | if (current_kprobe->fault_handler && |
| 659 | current_kprobe->fault_handler(current_kprobe, regs, trapnr)) |
| 660 | return 1; |
| 661 | |
| 662 | if (kprobe_status & KPROBE_HIT_SS) { |
| 663 | resume_execution(current_kprobe, regs); |
| 664 | unlock_kprobes(); |
| 665 | preempt_enable_no_resched(); |
| 666 | } |
| 667 | |
| 668 | return 0; |
| 669 | } |
| 670 | |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame^] | 671 | int __kprobes kprobe_exceptions_notify(struct notifier_block *self, |
| 672 | unsigned long val, void *data) |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 673 | { |
| 674 | struct die_args *args = (struct die_args *)data; |
| 675 | switch(val) { |
| 676 | case DIE_BREAK: |
Keshavamurthy Anil S | 89cb14c | 2005-06-23 00:09:35 -0700 | [diff] [blame] | 677 | if (pre_kprobes_handler(args)) |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 678 | return NOTIFY_STOP; |
| 679 | break; |
| 680 | case DIE_SS: |
| 681 | if (post_kprobes_handler(args->regs)) |
| 682 | return NOTIFY_STOP; |
| 683 | break; |
| 684 | case DIE_PAGE_FAULT: |
| 685 | if (kprobes_fault_handler(args->regs, args->trapnr)) |
| 686 | return NOTIFY_STOP; |
| 687 | default: |
| 688 | break; |
| 689 | } |
| 690 | return NOTIFY_DONE; |
| 691 | } |
| 692 | |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame^] | 693 | 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] | 694 | { |
Anil S Keshavamurthy | b2761dc | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 695 | struct jprobe *jp = container_of(p, struct jprobe, kp); |
| 696 | unsigned long addr = ((struct fnptr *)(jp->entry))->ip; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 697 | |
Anil S Keshavamurthy | b2761dc | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 698 | /* save architectural state */ |
| 699 | jprobe_saved_regs = *regs; |
| 700 | |
| 701 | /* after rfi, execute the jprobe instrumented function */ |
| 702 | regs->cr_iip = addr & ~0xFULL; |
| 703 | ia64_psr(regs)->ri = addr & 0xf; |
| 704 | regs->r1 = ((struct fnptr *)(jp->entry))->gp; |
| 705 | |
| 706 | /* |
| 707 | * fix the return address to our jprobe_inst_return() function |
| 708 | * in the jprobes.S file |
| 709 | */ |
| 710 | regs->b0 = ((struct fnptr *)(jprobe_inst_return))->ip; |
| 711 | |
| 712 | return 1; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Prasanna S Panchamukhi | 1f7ad57 | 2005-09-06 15:19:30 -0700 | [diff] [blame^] | 715 | 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] | 716 | { |
Anil S Keshavamurthy | b2761dc | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 717 | *regs = jprobe_saved_regs; |
| 718 | return 1; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 719 | } |
Rusty Lynch | 9508dbf | 2005-06-27 15:17:12 -0700 | [diff] [blame] | 720 | |
| 721 | static struct kprobe trampoline_p = { |
| 722 | .pre_handler = trampoline_probe_handler |
| 723 | }; |
| 724 | |
Rusty Lynch | 6772926 | 2005-07-05 18:54:50 -0700 | [diff] [blame] | 725 | int __init arch_init_kprobes(void) |
Rusty Lynch | 9508dbf | 2005-06-27 15:17:12 -0700 | [diff] [blame] | 726 | { |
| 727 | trampoline_p.addr = |
| 728 | (kprobe_opcode_t *)((struct fnptr *)kretprobe_trampoline)->ip; |
| 729 | return register_kprobe(&trampoline_p); |
| 730 | } |