⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kprobes.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *  Kernel Probes (KProbes) *  arch/ia64/kernel/kprobes.c * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * Copyright (C) IBM Corporation, 2002, 2004 * Copyright (C) Intel Corporation, 2005 * * 2005-Apr     Rusty Lynch <rusty.lynch@intel.com> and Anil S Keshavamurthy *              <anil.s.keshavamurthy@intel.com> adapted from i386 */#include <linux/kprobes.h>#include <linux/ptrace.h>#include <linux/string.h>#include <linux/slab.h>#include <linux/preempt.h>#include <linux/moduleloader.h>#include <linux/kdebug.h>#include <asm/pgtable.h>#include <asm/sections.h>#include <asm/uaccess.h>extern void jprobe_inst_return(void);DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}};enum instruction_type {A, I, M, F, B, L, X, u};static enum instruction_type bundle_encoding[32][3] = {  { M, I, I },				/* 00 */  { M, I, I },				/* 01 */  { M, I, I },				/* 02 */  { M, I, I },				/* 03 */  { M, L, X },				/* 04 */  { M, L, X },				/* 05 */  { u, u, u },  			/* 06 */  { u, u, u },  			/* 07 */  { M, M, I },				/* 08 */  { M, M, I },				/* 09 */  { M, M, I },				/* 0A */  { M, M, I },				/* 0B */  { M, F, I },				/* 0C */  { M, F, I },				/* 0D */  { M, M, F },				/* 0E */  { M, M, F },				/* 0F */  { M, I, B },				/* 10 */  { M, I, B },				/* 11 */  { M, B, B },				/* 12 */  { M, B, B },				/* 13 */  { u, u, u },  			/* 14 */  { u, u, u },  			/* 15 */  { B, B, B },				/* 16 */  { B, B, B },				/* 17 */  { M, M, B },				/* 18 */  { M, M, B },				/* 19 */  { u, u, u },  			/* 1A */  { u, u, u },  			/* 1B */  { M, F, B },				/* 1C */  { M, F, B },				/* 1D */  { u, u, u },  			/* 1E */  { u, u, u },  			/* 1F */};/* * In this function we check to see if the instruction * is IP relative instruction and update the kprobe * inst flag accordingly */static void __kprobes update_kprobe_inst_flag(uint template, uint  slot,					      uint major_opcode,					      unsigned long kprobe_inst,					      struct kprobe *p){	p->ainsn.inst_flag = 0;	p->ainsn.target_br_reg = 0;	p->ainsn.slot = slot;	/* Check 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)) ) {		/* is a break instruction */	 	p->ainsn.inst_flag |= INST_FLAG_BREAK_INST;		return;	}	if (bundle_encoding[template][slot] == B) {		switch (major_opcode) {		  case INDIRECT_CALL_OPCODE:	 		p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;			p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);			break;		  case IP_RELATIVE_PREDICT_OPCODE:		  case IP_RELATIVE_BRANCH_OPCODE:			p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;			break;		  case IP_RELATIVE_CALL_OPCODE:			p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;			p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;			p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);			break;		}	} else if (bundle_encoding[template][slot] == X) {		switch (major_opcode) {		  case LONG_CALL_OPCODE:			p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;			p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);		  break;		}	}	return;}/* * In this function we check to see if the instruction * (qp) cmpx.crel.ctype p1,p2=r2,r3 * on which we are inserting kprobe is cmp instruction * with ctype as unc. */static uint __kprobes is_cmp_ctype_unc_inst(uint template, uint slot,					    uint major_opcode,					    unsigned long kprobe_inst){	cmp_inst_t cmp_inst;	uint ctype_unc = 0;	if (!((bundle_encoding[template][slot] == I) ||		(bundle_encoding[template][slot] == M)))		goto out;	if (!((major_opcode == 0xC) || (major_opcode == 0xD) ||		(major_opcode == 0xE)))		goto out;	cmp_inst.l = kprobe_inst;	if ((cmp_inst.f.x2 == 0) || (cmp_inst.f.x2 == 1)) {		/* Integer compare - Register Register (A6 type)*/		if ((cmp_inst.f.tb == 0) && (cmp_inst.f.ta == 0)				&&(cmp_inst.f.c == 1))			ctype_unc = 1;	} else if ((cmp_inst.f.x2 == 2)||(cmp_inst.f.x2 == 3)) {		/* Integer compare - Immediate Register (A8 type)*/		if ((cmp_inst.f.ta == 0) &&(cmp_inst.f.c == 1))			ctype_unc = 1;	}out:	return ctype_unc;}/* * In this function we check to see if the instruction * on which we are inserting kprobe is supported. * Returns qp value if supported * Returns -EINVAL if unsupported */static int __kprobes unsupported_inst(uint template, uint  slot,				      uint major_opcode,				      unsigned long kprobe_inst,				      unsigned long addr){	int qp;	qp = kprobe_inst & 0x3f;	if (is_cmp_ctype_unc_inst(template, slot, major_opcode, kprobe_inst)) {		if (slot == 1 && qp)  {			printk(KERN_WARNING "Kprobes on cmp unc "					"instruction on slot 1 at <0x%lx> "					"is not supported\n", addr);			return -EINVAL;		}		qp = 0;	}	else if (bundle_encoding[template][slot] == I) {		if (major_opcode == 0) {			/*			 * Check for Integer speculation instruction			 * - Bit 33-35 to be equal to 0x1			 */			if (((kprobe_inst >> 33) & 0x7) == 1) {				printk(KERN_WARNING					"Kprobes on speculation inst at <0x%lx> not supported\n",						addr);				return -EINVAL;			}			/*			 * IP relative mov instruction			 *  - Bit 27-35 to be equal to 0x30			 */			if (((kprobe_inst >> 27) & 0x1FF) == 0x30) {				printk(KERN_WARNING					"Kprobes on \"mov r1=ip\" at <0x%lx> not supported\n",						addr);				return -EINVAL;			}		}		else if ((major_opcode == 5) &&	!(kprobe_inst & (0xFUl << 33)) &&				(kprobe_inst & (0x1UL << 12))) {			/* test bit instructions, tbit,tnat,tf			 * bit 33-36 to be equal to 0			 * bit 12 to be equal to 1			 */			if (slot == 1 && qp) {				printk(KERN_WARNING "Kprobes on test bit "						"instruction on slot at <0x%lx> "						"is not supported\n", addr);				return -EINVAL;			}			qp = 0;		}	}	else if (bundle_encoding[template][slot] == B) {		if (major_opcode == 7) {			/* IP-Relative Predict major code is 7 */			printk(KERN_WARNING "Kprobes on IP-Relative"					"Predict is not supported\n");			return -EINVAL;		}		else if (major_opcode == 2) {			/* Indirect Predict, major code is 2			 * bit 27-32 to be equal to 10 or 11			 */			int x6=(kprobe_inst >> 27) & 0x3F;			if ((x6 == 0x10) || (x6 == 0x11)) {				printk(KERN_WARNING "Kprobes on "					"Indirect Predict is not supported\n");				return -EINVAL;			}		}	}	/* kernel does not use float instruction, here for safety kprobe	 * will judge whether it is fcmp/flass/float approximation instruction	 */	else if (unlikely(bundle_encoding[template][slot] == F)) {		if ((major_opcode == 4 || major_opcode == 5) &&				(kprobe_inst  & (0x1 << 12))) {			/* fcmp/fclass unc instruction */			if (slot == 1 && qp) {				printk(KERN_WARNING "Kprobes on fcmp/fclass "					"instruction on slot at <0x%lx> "					"is not supported\n", addr);				return -EINVAL;			}			qp = 0;		}		if ((major_opcode == 0 || major_opcode == 1) &&			(kprobe_inst & (0x1UL << 33))) {			/* float Approximation instruction */			if (slot == 1 && qp) {				printk(KERN_WARNING "Kprobes on float Approx "					"instr at <0x%lx> is not supported\n",						addr);				return -EINVAL;			}			qp = 0;		}	}	return qp;}/* * In this function we override the bundle with * the break instruction at the given slot. */static void __kprobes prepare_break_inst(uint template, uint  slot,					 uint major_opcode,					 unsigned long kprobe_inst,					 struct kprobe *p,					 int qp){	unsigned long break_inst = BREAK_INST;	bundle_t *bundle = &p->opcode.bundle;	/*	 * Copy the original kprobe_inst qualifying predicate(qp)	 * to the break instruction	 */	break_inst |= qp;	switch (slot) {	  case 0:		bundle->quad0.slot0 = break_inst;		break;	  case 1:		bundle->quad0.slot1_p0 = break_inst;		bundle->quad1.slot1_p1 = break_inst >> (64-46);		break;	  case 2:		bundle->quad1.slot2 = break_inst;		break;	}	/*	 * Update the instruction flag, so that we can	 * emulate the instruction properly after we	 * single step on original instruction	 */	update_kprobe_inst_flag(template, slot, major_opcode, kprobe_inst, p);}static void __kprobes get_kprobe_inst(bundle_t *bundle, uint slot,	       	unsigned long *kprobe_inst, uint *major_opcode){	unsigned long kprobe_inst_p0, kprobe_inst_p1;	unsigned int template;	template = bundle->quad0.template;	switch (slot) {	  case 0:		*major_opcode = (bundle->quad0.slot0 >> SLOT0_OPCODE_SHIFT);		*kprobe_inst = bundle->quad0.slot0;		  break;	  case 1:		*major_opcode = (bundle->quad1.slot1_p1 >> SLOT1_p1_OPCODE_SHIFT);		kprobe_inst_p0 = bundle->quad0.slot1_p0;		kprobe_inst_p1 = bundle->quad1.slot1_p1;		*kprobe_inst = kprobe_inst_p0 | (kprobe_inst_p1 << (64-46));		break;	  case 2:		*major_opcode = (bundle->quad1.slot2 >> SLOT2_OPCODE_SHIFT);		*kprobe_inst = bundle->quad1.slot2;		break;	}}/* Returns non-zero if the addr is in the Interrupt Vector Table */static int __kprobes in_ivt_functions(unsigned long addr){	return (addr >= (unsigned long)__start_ivt_text		&& addr < (unsigned long)__end_ivt_text);}static int __kprobes valid_kprobe_addr(int template, int slot,				       unsigned long addr){	if ((slot > 2) || ((bundle_encoding[template][1] == L) && slot > 1)) {		printk(KERN_WARNING "Attempting to insert unaligned kprobe "				"at 0x%lx\n", addr);		return -EINVAL;	}	if (in_ivt_functions(addr)) {		printk(KERN_WARNING "Kprobes can't be inserted inside "				"IVT functions at 0x%lx\n", addr);		return -EINVAL;	}	return 0;}static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb){	unsigned int i;	i = atomic_add_return(1, &kcb->prev_kprobe_index);	kcb->prev_kprobe[i-1].kp = kprobe_running();	kcb->prev_kprobe[i-1].status = kcb->kprobe_status;}static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb){	unsigned int i;	i = atomic_sub_return(1, &kcb->prev_kprobe_index);	__get_cpu_var(current_kprobe) = kcb->prev_kprobe[i].kp;	kcb->kprobe_status = kcb->prev_kprobe[i].status;}static void __kprobes set_current_kprobe(struct kprobe *p,			struct kprobe_ctlblk *kcb){	__get_cpu_var(current_kprobe) = p;}static void kretprobe_trampoline(void){}/* * At this point the target function has been tricked into * returning into our trampoline.  Lookup the associated instance * and then: *    - call the handler function *    - cleanup by marking the instance as unused *    - long jump back to the original return address */int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs){	struct kretprobe_instance *ri = NULL;	struct hlist_head *head, empty_rp;	struct hlist_node *node, *tmp;	unsigned long flags, orig_ret_address = 0;	unsigned long trampoline_address =		((struct fnptr *)kretprobe_trampoline)->ip;	INIT_HLIST_HEAD(&empty_rp);	spin_lock_irqsave(&kretprobe_lock, flags);	head = kretprobe_inst_table_head(current);	/*	 * It is possible to have multiple instances associated with a given	 * task either because an multiple functions in the call path	 * have a return probe installed on them, and/or more then one return	 * return probe was registered for a target function.	 *	 * We can handle this because:	 *     - instances are always inserted at the head of the list	 *     - when multiple return probes are registered for the same	 *       function, the first instance's ret_addr will point to the	 *       real return address, and all the rest will point to	 *       kretprobe_trampoline	 */	hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {		if (ri->task != current)			/* another task is sharing our hash bucket */			continue;		orig_ret_address = (unsigned long)ri->ret_addr;		if (orig_ret_address != trampoline_address)			/*			 * This is the real return address. Any other			 * instances associated with this task are for			 * other calls deeper on the call stack			 */			break;	}	regs->cr_iip = orig_ret_address;	hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {		if (ri->task != current)			/* another task is sharing our hash bucket */			continue;		if (ri->rp && ri->rp->handler)			ri->rp->handler(ri, regs);		orig_ret_address = (unsigned long)ri->ret_addr;		recycle_rp_inst(ri, &empty_rp);		if (orig_ret_address != trampoline_address)			/*			 * This is the real return address. Any other			 * instances associated with this task are for			 * other calls deeper on the call stack			 */			break;	}	kretprobe_assert(ri, orig_ret_address, trampoline_address);	reset_current_kprobe();	spin_unlock_irqrestore(&kretprobe_lock, flags);	preempt_enable_no_resched();	hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {		hlist_del(&ri->hlist);		kfree(ri);	}	/*	 * By returning a non-zero value, we are telling	 * kprobe_handler() that we don't want the post_handler	 * to run (and have re-enabled preemption)	 */	return 1;}/* Called with kretprobe_lock held */void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,				      struct pt_regs *regs){	ri->ret_addr = (kprobe_opcode_t *)regs->b0;	/* Replace the return addr with trampoline addr */	regs->b0 = ((struct fnptr *)kretprobe_trampoline)->ip;}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;	int qp;	bundle = &((kprobe_opcode_t *)kprobe_addr)->bundle;	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);	qp = unsupported_inst(template, slot, major_opcode, kprobe_inst, addr);	if (qp < 0)		return -EINVAL;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -