kprobes.c
来自「底层驱动开发」· C语言 代码 · 共 792 行 · 第 1/2 页
C
792 行
* kprobe_handler() that we have handled unlocking * and re-enabling preemption. */ return 1;}void __kprobes arch_prepare_kretprobe(struct kretprobe *rp, struct pt_regs *regs){ struct kretprobe_instance *ri; if ((ri = get_free_rp_inst(rp)) != NULL) { ri->rp = rp; ri->task = current; ri->ret_addr = (kprobe_opcode_t *)regs->b0; /* Replace the return addr with trampoline addr */ regs->b0 = ((struct fnptr *)kretprobe_trampoline)->ip; add_rp_inst(ri); } else { rp->nmissed++; }}int __kprobes arch_prepare_kprobe(struct kprobe *p){ unsigned long addr = (unsigned long) p->addr; unsigned long *kprobe_addr = (unsigned long *)(addr & ~0xFULL); unsigned long kprobe_inst=0; unsigned int slot = addr & 0xf, template, major_opcode = 0; bundle_t *bundle = &p->ainsn.insn.bundle; memcpy(&p->opcode.bundle, kprobe_addr, sizeof(bundle_t)); memcpy(&p->ainsn.insn.bundle, kprobe_addr, sizeof(bundle_t)); template = bundle->quad0.template; if(valid_kprobe_addr(template, slot, addr)) return -EINVAL; /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */ if (slot == 1 && bundle_encoding[template][1] == L) slot++; /* Get kprobe_inst and major_opcode from the bundle */ get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode); if (unsupported_inst(template, slot, major_opcode, kprobe_inst, p)) return -EINVAL; prepare_break_inst(template, slot, major_opcode, kprobe_inst, p); return 0;}void __kprobes arch_arm_kprobe(struct kprobe *p){ unsigned long addr = (unsigned long)p->addr; unsigned long arm_addr = addr & ~0xFULL; memcpy((char *)arm_addr, &p->ainsn.insn.bundle, sizeof(bundle_t)); flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));}void __kprobes arch_disarm_kprobe(struct kprobe *p){ unsigned long addr = (unsigned long)p->addr; unsigned long arm_addr = addr & ~0xFULL; /* p->opcode contains the original unaltered bundle */ memcpy((char *) arm_addr, (char *) &p->opcode.bundle, sizeof(bundle_t)); flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));}void __kprobes arch_remove_kprobe(struct kprobe *p){}/* * We are resuming execution after a single step fault, so the pt_regs * structure reflects the register state after we executed the instruction * located in the kprobe (p->ainsn.insn.bundle). We still need to adjust * the ip to point back to the original stack address. To set the IP address * to original stack address, handle the case where we need to fixup the * relative IP address and/or fixup branch register. */static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs){ unsigned long bundle_addr = ((unsigned long) (&p->opcode.bundle)) & ~0xFULL; unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL; unsigned long template; int slot = ((unsigned long)p->addr & 0xf); template = p->opcode.bundle.quad0.template; if (slot == 1 && bundle_encoding[template][1] == L) slot = 2; if (p->ainsn.inst_flag) { if (p->ainsn.inst_flag & INST_FLAG_FIX_RELATIVE_IP_ADDR) { /* Fix relative IP address */ regs->cr_iip = (regs->cr_iip - bundle_addr) + resume_addr; } if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) { /* * Fix target branch register, software convention is * to use either b0 or b6 or b7, so just checking * only those registers */ switch (p->ainsn.target_br_reg) { case 0: if ((regs->b0 == bundle_addr) || (regs->b0 == bundle_addr + 0x10)) { regs->b0 = (regs->b0 - bundle_addr) + resume_addr; } break; case 6: if ((regs->b6 == bundle_addr) || (regs->b6 == bundle_addr + 0x10)) { regs->b6 = (regs->b6 - bundle_addr) + resume_addr; } break; case 7: if ((regs->b7 == bundle_addr) || (regs->b7 == bundle_addr + 0x10)) { regs->b7 = (regs->b7 - bundle_addr) + resume_addr; } break; } /* end switch */ } goto turn_ss_off; } if (slot == 2) { if (regs->cr_iip == bundle_addr + 0x10) { regs->cr_iip = resume_addr + 0x10; } } else { if (regs->cr_iip == bundle_addr) { regs->cr_iip = resume_addr; } }turn_ss_off: /* Turn off Single Step bit */ ia64_psr(regs)->ss = 0;}static void __kprobes prepare_ss(struct kprobe *p, struct pt_regs *regs){ unsigned long bundle_addr = (unsigned long) &p->opcode.bundle; unsigned long slot = (unsigned long)p->addr & 0xf; /* single step inline if break instruction */ if (p->ainsn.inst_flag == INST_FLAG_BREAK_INST) regs->cr_iip = (unsigned long)p->addr & ~0xFULL; else regs->cr_iip = bundle_addr & ~0xFULL; if (slot > 2) slot = 0; ia64_psr(regs)->ri = slot; /* turn on single stepping */ ia64_psr(regs)->ss = 1;}static int __kprobes is_ia64_break_inst(struct pt_regs *regs){ unsigned int slot = ia64_psr(regs)->ri; unsigned int template, major_opcode; unsigned long kprobe_inst; unsigned long *kprobe_addr = (unsigned long *)regs->cr_iip; bundle_t bundle; memcpy(&bundle, kprobe_addr, sizeof(bundle_t)); template = bundle.quad0.template; /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */ if (slot == 1 && bundle_encoding[template][1] == L) slot++; /* Get Kprobe probe instruction at given slot*/ get_kprobe_inst(&bundle, slot, &kprobe_inst, &major_opcode); /* For break instruction, * Bits 37:40 Major opcode to be zero * Bits 27:32 X6 to be zero * Bits 32:35 X3 to be zero */ if (major_opcode || ((kprobe_inst >> 27) & 0x1FF) ) { /* Not a break instruction */ return 0; } /* Is a break instruction */ return 1;}static int __kprobes pre_kprobes_handler(struct die_args *args){ struct kprobe *p; int ret = 0; struct pt_regs *regs = args->regs; kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs); preempt_disable(); /* Handle recursion cases */ if (kprobe_running()) { p = get_kprobe(addr); if (p) { if ( (kprobe_status == KPROBE_HIT_SS) && (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)) { ia64_psr(regs)->ss = 0; unlock_kprobes(); goto no_kprobe; } /* We have reentered the pre_kprobe_handler(), since * another probe was hit while within the handler. * We here save the original kprobes variables and * just single step on the instruction of the new probe * without calling any user handlers. */ save_previous_kprobe(); set_current_kprobe(p); p->nmissed++; prepare_ss(p, regs); kprobe_status = KPROBE_REENTER; return 1; } else if (args->err == __IA64_BREAK_JPROBE) { /* * jprobe instrumented function just completed */ p = current_kprobe; if (p->break_handler && p->break_handler(p, regs)) { goto ss_probe; } } else { /* Not our break */ goto no_kprobe; } } lock_kprobes(); p = get_kprobe(addr); if (!p) { unlock_kprobes(); if (!is_ia64_break_inst(regs)) { /* * The breakpoint instruction was removed right * after we hit it. Another cpu has removed * either a probepoint or a debugger breakpoint * at this address. In either case, no further * handling of this interrupt is appropriate. */ ret = 1; } /* Not one of our break, let kernel handle it */ goto no_kprobe; } kprobe_status = KPROBE_HIT_ACTIVE; set_current_kprobe(p); if (p->pre_handler && p->pre_handler(p, regs)) /* * Our pre-handler is specifically requesting that we just * do a return. This is used for both the jprobe pre-handler * and the kretprobe trampoline */ return 1;ss_probe: prepare_ss(p, regs); kprobe_status = KPROBE_HIT_SS; return 1;no_kprobe: preempt_enable_no_resched(); return ret;}static int __kprobes post_kprobes_handler(struct pt_regs *regs){ if (!kprobe_running()) return 0; if ((kprobe_status != KPROBE_REENTER) && current_kprobe->post_handler) { kprobe_status = KPROBE_HIT_SSDONE; current_kprobe->post_handler(current_kprobe, regs, 0); } resume_execution(current_kprobe, regs); /*Restore back the original saved kprobes variables and continue. */ if (kprobe_status == KPROBE_REENTER) { restore_previous_kprobe(); goto out; } unlock_kprobes();out: preempt_enable_no_resched(); return 1;}static int __kprobes kprobes_fault_handler(struct pt_regs *regs, int trapnr){ if (!kprobe_running()) return 0; if (current_kprobe->fault_handler && current_kprobe->fault_handler(current_kprobe, regs, trapnr)) return 1; if (kprobe_status & KPROBE_HIT_SS) { resume_execution(current_kprobe, regs); unlock_kprobes(); preempt_enable_no_resched(); } return 0;}int __kprobes kprobe_exceptions_notify(struct notifier_block *self, unsigned long val, void *data){ struct die_args *args = (struct die_args *)data; switch(val) { case DIE_BREAK: if (pre_kprobes_handler(args)) return NOTIFY_STOP; break; case DIE_SS: if (post_kprobes_handler(args->regs)) return NOTIFY_STOP; break; case DIE_PAGE_FAULT: if (kprobes_fault_handler(args->regs, args->trapnr)) return NOTIFY_STOP; default: break; } return NOTIFY_DONE;}int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs){ struct jprobe *jp = container_of(p, struct jprobe, kp); unsigned long addr = ((struct fnptr *)(jp->entry))->ip; /* save architectural state */ jprobe_saved_regs = *regs; /* after rfi, execute the jprobe instrumented function */ regs->cr_iip = addr & ~0xFULL; ia64_psr(regs)->ri = addr & 0xf; regs->r1 = ((struct fnptr *)(jp->entry))->gp; /* * fix the return address to our jprobe_inst_return() function * in the jprobes.S file */ regs->b0 = ((struct fnptr *)(jprobe_inst_return))->ip; return 1;}int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs){ *regs = jprobe_saved_regs; return 1;}static struct kprobe trampoline_p = { .pre_handler = trampoline_probe_handler};int __init arch_init_kprobes(void){ trampoline_p.addr = (kprobe_opcode_t *)((struct fnptr *)kretprobe_trampoline)->ip; return register_kprobe(&trampoline_p);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?