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

📄 ptrace.c

📁 linux-2.6.15.6
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * Kernel support for the ptrace() and syscall tracing interfaces. * * Copyright (C) 1999-2005 Hewlett-Packard Co *	David Mosberger-Tang <davidm@hpl.hp.com> * * Derived from the x86 and Alpha versions. */#include <linux/config.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/slab.h>#include <linux/mm.h>#include <linux/errno.h>#include <linux/ptrace.h>#include <linux/smp_lock.h>#include <linux/user.h>#include <linux/security.h>#include <linux/audit.h>#include <linux/signal.h>#include <asm/pgtable.h>#include <asm/processor.h>#include <asm/ptrace_offsets.h>#include <asm/rse.h>#include <asm/system.h>#include <asm/uaccess.h>#include <asm/unwind.h>#ifdef CONFIG_PERFMON#include <asm/perfmon.h>#endif#include "entry.h"/* * Bits in the PSR that we allow ptrace() to change: *	be, up, ac, mfl, mfh (the user mask; five bits total) *	db (debug breakpoint fault; one bit) *	id (instruction debug fault disable; one bit) *	dd (data debug fault disable; one bit) *	ri (restart instruction; two bits) *	is (instruction set; one bit) */#define IPSR_MASK (IA64_PSR_UM | IA64_PSR_DB | IA64_PSR_IS	\		   | IA64_PSR_ID | IA64_PSR_DD | IA64_PSR_RI)#define MASK(nbits)	((1UL << (nbits)) - 1)	/* mask with NBITS bits set */#define PFM_MASK	MASK(38)#define PTRACE_DEBUG	0#if PTRACE_DEBUG# define dprintk(format...)	printk(format)# define inline#else# define dprintk(format...)#endif/* Return TRUE if PT was created due to kernel-entry via a system-call.  */static inline intin_syscall (struct pt_regs *pt){	return (long) pt->cr_ifs >= 0;}/* * Collect the NaT bits for r1-r31 from scratch_unat and return a NaT * bitset where bit i is set iff the NaT bit of register i is set. */unsigned longia64_get_scratch_nat_bits (struct pt_regs *pt, unsigned long scratch_unat){#	define GET_BITS(first, last, unat)				\	({								\		unsigned long bit = ia64_unat_pos(&pt->r##first);	\		unsigned long nbits = (last - first + 1);		\		unsigned long mask = MASK(nbits) << first;		\		unsigned long dist;					\		if (bit < first)					\			dist = 64 + bit - first;			\		else							\			dist = bit - first;				\		ia64_rotr(unat, dist) & mask;				\	})	unsigned long val;	/*	 * Registers that are stored consecutively in struct pt_regs	 * can be handled in parallel.  If the register order in	 * struct_pt_regs changes, this code MUST be updated.	 */	val  = GET_BITS( 1,  1, scratch_unat);	val |= GET_BITS( 2,  3, scratch_unat);	val |= GET_BITS(12, 13, scratch_unat);	val |= GET_BITS(14, 14, scratch_unat);	val |= GET_BITS(15, 15, scratch_unat);	val |= GET_BITS( 8, 11, scratch_unat);	val |= GET_BITS(16, 31, scratch_unat);	return val;#	undef GET_BITS}/* * Set the NaT bits for the scratch registers according to NAT and * return the resulting unat (assuming the scratch registers are * stored in PT). */unsigned longia64_put_scratch_nat_bits (struct pt_regs *pt, unsigned long nat){#	define PUT_BITS(first, last, nat)				\	({								\		unsigned long bit = ia64_unat_pos(&pt->r##first);	\		unsigned long nbits = (last - first + 1);		\		unsigned long mask = MASK(nbits) << first;		\		long dist;						\		if (bit < first)					\			dist = 64 + bit - first;			\		else							\			dist = bit - first;				\		ia64_rotl(nat & mask, dist);				\	})	unsigned long scratch_unat;	/*	 * Registers that are stored consecutively in struct pt_regs	 * can be handled in parallel.  If the register order in	 * struct_pt_regs changes, this code MUST be updated.	 */	scratch_unat  = PUT_BITS( 1,  1, nat);	scratch_unat |= PUT_BITS( 2,  3, nat);	scratch_unat |= PUT_BITS(12, 13, nat);	scratch_unat |= PUT_BITS(14, 14, nat);	scratch_unat |= PUT_BITS(15, 15, nat);	scratch_unat |= PUT_BITS( 8, 11, nat);	scratch_unat |= PUT_BITS(16, 31, nat);	return scratch_unat;#	undef PUT_BITS}#define IA64_MLX_TEMPLATE	0x2#define IA64_MOVL_OPCODE	6voidia64_increment_ip (struct pt_regs *regs){	unsigned long w0, ri = ia64_psr(regs)->ri + 1;	if (ri > 2) {		ri = 0;		regs->cr_iip += 16;	} else if (ri == 2) {		get_user(w0, (char __user *) regs->cr_iip + 0);		if (((w0 >> 1) & 0xf) == IA64_MLX_TEMPLATE) {			/*			 * rfi'ing to slot 2 of an MLX bundle causes			 * an illegal operation fault.  We don't want			 * that to happen...			 */			ri = 0;			regs->cr_iip += 16;		}	}	ia64_psr(regs)->ri = ri;}voidia64_decrement_ip (struct pt_regs *regs){	unsigned long w0, ri = ia64_psr(regs)->ri - 1;	if (ia64_psr(regs)->ri == 0) {		regs->cr_iip -= 16;		ri = 2;		get_user(w0, (char __user *) regs->cr_iip + 0);		if (((w0 >> 1) & 0xf) == IA64_MLX_TEMPLATE) {			/*			 * rfi'ing to slot 2 of an MLX bundle causes			 * an illegal operation fault.  We don't want			 * that to happen...			 */			ri = 1;		}	}	ia64_psr(regs)->ri = ri;}/* * This routine is used to read an rnat bits that are stored on the * kernel backing store.  Since, in general, the alignment of the user * and kernel are different, this is not completely trivial.  In * essence, we need to construct the user RNAT based on up to two * kernel RNAT values and/or the RNAT value saved in the child's * pt_regs. * * user rbs * * +--------+ <-- lowest address * | slot62 | * +--------+ * |  rnat  | 0x....1f8 * +--------+ * | slot00 | \ * +--------+ | * | slot01 | > child_regs->ar_rnat * +--------+ | * | slot02 | /				kernel rbs * +--------+				+--------+ *	    <- child_regs->ar_bspstore	| slot61 | <-- krbs * +- - - - +				+--------+ *					| slot62 | * +- - - - +				+--------+ *					|  rnat	 | * +- - - - +				+--------+ *   vrnat				| slot00 | * +- - - - +				+--------+ *					=	 = *					+--------+ *					| slot00 | \ *					+--------+ | *					| slot01 | > child_stack->ar_rnat *					+--------+ | *					| slot02 | / *					+--------+ *						  <--- child_stack->ar_bspstore * * The way to think of this code is as follows: bit 0 in the user rnat * corresponds to some bit N (0 <= N <= 62) in one of the kernel rnat * value.  The kernel rnat value holding this bit is stored in * variable rnat0.  rnat1 is loaded with the kernel rnat value that * form the upper bits of the user rnat value. * * Boundary cases: * * o when reading the rnat "below" the first rnat slot on the kernel *   backing store, rnat0/rnat1 are set to 0 and the low order bits are *   merged in from pt->ar_rnat. * * o when reading the rnat "above" the last rnat slot on the kernel *   backing store, rnat0/rnat1 gets its value from sw->ar_rnat. */static unsigned longget_rnat (struct task_struct *task, struct switch_stack *sw,	  unsigned long *krbs, unsigned long *urnat_addr,	  unsigned long *urbs_end){	unsigned long rnat0 = 0, rnat1 = 0, urnat = 0, *slot0_kaddr;	unsigned long umask = 0, mask, m;	unsigned long *kbsp, *ubspstore, *rnat0_kaddr, *rnat1_kaddr, shift;	long num_regs, nbits;	struct pt_regs *pt;	pt = ia64_task_regs(task);	kbsp = (unsigned long *) sw->ar_bspstore;	ubspstore = (unsigned long *) pt->ar_bspstore;	if (urbs_end < urnat_addr)		nbits = ia64_rse_num_regs(urnat_addr - 63, urbs_end);	else		nbits = 63;	mask = MASK(nbits);	/*	 * First, figure out which bit number slot 0 in user-land maps	 * to in the kernel rnat.  Do this by figuring out how many	 * register slots we're beyond the user's backingstore and	 * then computing the equivalent address in kernel space.	 */	num_regs = ia64_rse_num_regs(ubspstore, urnat_addr + 1);	slot0_kaddr = ia64_rse_skip_regs(krbs, num_regs);	shift = ia64_rse_slot_num(slot0_kaddr);	rnat1_kaddr = ia64_rse_rnat_addr(slot0_kaddr);	rnat0_kaddr = rnat1_kaddr - 64;	if (ubspstore + 63 > urnat_addr) {		/* some bits need to be merged in from pt->ar_rnat */		umask = MASK(ia64_rse_slot_num(ubspstore)) & mask;		urnat = (pt->ar_rnat & umask);		mask &= ~umask;		if (!mask)			return urnat;	}	m = mask << shift;	if (rnat0_kaddr >= kbsp)		rnat0 = sw->ar_rnat;	else if (rnat0_kaddr > krbs)		rnat0 = *rnat0_kaddr;	urnat |= (rnat0 & m) >> shift;	m = mask >> (63 - shift);	if (rnat1_kaddr >= kbsp)		rnat1 = sw->ar_rnat;	else if (rnat1_kaddr > krbs)		rnat1 = *rnat1_kaddr;	urnat |= (rnat1 & m) << (63 - shift);	return urnat;}/* * The reverse of get_rnat. */static voidput_rnat (struct task_struct *task, struct switch_stack *sw,	  unsigned long *krbs, unsigned long *urnat_addr, unsigned long urnat,	  unsigned long *urbs_end){	unsigned long rnat0 = 0, rnat1 = 0, *slot0_kaddr, umask = 0, mask, m;	unsigned long *kbsp, *ubspstore, *rnat0_kaddr, *rnat1_kaddr, shift;	long num_regs, nbits;	struct pt_regs *pt;	unsigned long cfm, *urbs_kargs;	pt = ia64_task_regs(task);	kbsp = (unsigned long *) sw->ar_bspstore;	ubspstore = (unsigned long *) pt->ar_bspstore;	urbs_kargs = urbs_end;	if (in_syscall(pt)) {		/*		 * If entered via syscall, don't allow user to set rnat bits		 * for syscall args.		 */		cfm = pt->cr_ifs;		urbs_kargs = ia64_rse_skip_regs(urbs_end, -(cfm & 0x7f));	}	if (urbs_kargs >= urnat_addr)		nbits = 63;	else {		if ((urnat_addr - 63) >= urbs_kargs)			return;		nbits = ia64_rse_num_regs(urnat_addr - 63, urbs_kargs);	}	mask = MASK(nbits);	/*	 * First, figure out which bit number slot 0 in user-land maps	 * to in the kernel rnat.  Do this by figuring out how many	 * register slots we're beyond the user's backingstore and	 * then computing the equivalent address in kernel space.	 */	num_regs = ia64_rse_num_regs(ubspstore, urnat_addr + 1);	slot0_kaddr = ia64_rse_skip_regs(krbs, num_regs);	shift = ia64_rse_slot_num(slot0_kaddr);	rnat1_kaddr = ia64_rse_rnat_addr(slot0_kaddr);	rnat0_kaddr = rnat1_kaddr - 64;	if (ubspstore + 63 > urnat_addr) {		/* some bits need to be place in pt->ar_rnat: */		umask = MASK(ia64_rse_slot_num(ubspstore)) & mask;		pt->ar_rnat = (pt->ar_rnat & ~umask) | (urnat & umask);		mask &= ~umask;		if (!mask)			return;	}	/*	 * Note: Section 11.1 of the EAS guarantees that bit 63 of an	 * rnat slot is ignored. so we don't have to clear it here.	 */	rnat0 = (urnat << shift);	m = mask << shift;	if (rnat0_kaddr >= kbsp)		sw->ar_rnat = (sw->ar_rnat & ~m) | (rnat0 & m);	else if (rnat0_kaddr > krbs)		*rnat0_kaddr = ((*rnat0_kaddr & ~m) | (rnat0 & m));	rnat1 = (urnat >> (63 - shift));	m = mask >> (63 - shift);	if (rnat1_kaddr >= kbsp)		sw->ar_rnat = (sw->ar_rnat & ~m) | (rnat1 & m);	else if (rnat1_kaddr > krbs)		*rnat1_kaddr = ((*rnat1_kaddr & ~m) | (rnat1 & m));}static inline inton_kernel_rbs (unsigned long addr, unsigned long bspstore,	       unsigned long urbs_end){	unsigned long *rnat_addr = ia64_rse_rnat_addr((unsigned long *)						      urbs_end);	return (addr >= bspstore && addr <= (unsigned long) rnat_addr);}/* * Read a word from the user-level backing store of task CHILD.  ADDR * is the user-level address to read the word from, VAL a pointer to * the return value, and USER_BSP gives the end of the user-level * backing store (i.e., it's the address that would be in ar.bsp after * the user executed a "cover" instruction). * * This routine takes care of accessing the kernel register backing * store for those registers that got spilled there.  It also takes * care of calculating the appropriate RNaT collection words. */longia64_peek (struct task_struct *child, struct switch_stack *child_stack,	   unsigned long user_rbs_end, unsigned long addr, long *val){	unsigned long *bspstore, *krbs, regnum, *laddr, *urbs_end, *rnat_addr;	struct pt_regs *child_regs;	size_t copied;	long ret;	urbs_end = (long *) user_rbs_end;	laddr = (unsigned long *) addr;	child_regs = ia64_task_regs(child);	bspstore = (unsigned long *) child_regs->ar_bspstore;	krbs = (unsigned long *) child + IA64_RBS_OFFSET/8;	if (on_kernel_rbs(addr, (unsigned long) bspstore,			  (unsigned long) urbs_end))	{		/*		 * Attempt to read the RBS in an area that's actually		 * on the kernel RBS => read the corresponding bits in		 * the kernel RBS.		 */		rnat_addr = ia64_rse_rnat_addr(laddr);		ret = get_rnat(child, child_stack, krbs, rnat_addr, urbs_end);		if (laddr == rnat_addr) {			/* return NaT collection word itself */			*val = ret;			return 0;		}		if (((1UL << ia64_rse_slot_num(laddr)) & ret) != 0) {			/*			 * It is implementation dependent whether the			 * data portion of a NaT value gets saved on a			 * st8.spill or RSE spill (e.g., see EAS 2.6,			 * 4.4.4.6 Register Spill and Fill).  To get			 * consistent behavior across all possible			 * IA-64 implementations, we return zero in			 * this case.			 */			*val = 0;			return 0;		}		if (laddr < urbs_end) {			/*			 * The desired word is on the kernel RBS and			 * is not a NaT.			 */			regnum = ia64_rse_num_regs(bspstore, laddr);			*val = *ia64_rse_skip_regs(krbs, regnum);			return 0;		}	}	copied = access_process_vm(child, addr, &ret, sizeof(ret), 0);	if (copied != sizeof(ret))		return -EIO;	*val = ret;	return 0;}longia64_poke (struct task_struct *child, struct switch_stack *child_stack,	   unsigned long user_rbs_end, unsigned long addr, long val){	unsigned long *bspstore, *krbs, regnum, *laddr;	unsigned long *urbs_end = (long *) user_rbs_end;	struct pt_regs *child_regs;	laddr = (unsigned long *) addr;	child_regs = ia64_task_regs(child);	bspstore = (unsigned long *) child_regs->ar_bspstore;	krbs = (unsigned long *) child + IA64_RBS_OFFSET/8;	if (on_kernel_rbs(addr, (unsigned long) bspstore,			  (unsigned long) urbs_end))	{		/*		 * Attempt to write the RBS in an area that's actually		 * on the kernel RBS => write the corresponding bits		 * in the kernel RBS.		 */		if (ia64_rse_is_rnat_slot(laddr))			put_rnat(child, child_stack, krbs, laddr, val,				 urbs_end);		else {			if (laddr < urbs_end) {				regnum = ia64_rse_num_regs(bspstore, laddr);				*ia64_rse_skip_regs(krbs, regnum) = val;			}		}	} else if (access_process_vm(child, addr, &val, sizeof(val), 1)		   != sizeof(val))		return -EIO;	return 0;}/* * Calculate the address of the end of the user-level register backing * store.  This is the address that would have been stored in ar.bsp * if the user had executed a "cover" instruction right before * entering the kernel.  If CFMP is not NULL, it is used to return the * "current frame mask" that was active at the time the kernel was * entered. */unsigned longia64_get_user_rbs_end (struct task_struct *child, struct pt_regs *pt,		       unsigned long *cfmp){	unsigned long *krbs, *bspstore, cfm = pt->cr_ifs;	long ndirty;	krbs = (unsigned long *) child + IA64_RBS_OFFSET/8;	bspstore = (unsigned long *) pt->ar_bspstore;	ndirty = ia64_rse_num_regs(krbs, krbs + (pt->loadrs >> 19));	if (in_syscall(pt))		ndirty += (cfm & 0x7f);	else		cfm &= ~(1UL << 63);	/* clear valid bit */	if (cfmp)		*cfmp = cfm;	return (unsigned long) ia64_rse_skip_regs(bspstore, ndirty);}/* * Synchronize (i.e, write) the RSE backing store living in kernel * space to the VM of the CHILD task.  SW and PT are the pointers to * the switch_stack and pt_regs structures, respectively. * USER_RBS_END is the user-level address at which the backing store * ends. */longia64_sync_user_rbs (struct task_struct *child, struct switch_stack *sw,		    unsigned long user_rbs_start, unsigned long user_rbs_end){	unsigned long addr, val;	long ret;	/* now copy word for word from kernel rbs to user rbs: */	for (addr = user_rbs_start; addr < user_rbs_end; addr += 8) {		ret = ia64_peek(child, sw, user_rbs_end, addr, &val);		if (ret < 0)			return ret;		if (access_process_vm(child, addr, &val, sizeof(val), 1)		    != sizeof(val))			return -EIO;	}	return 0;}static inline intthread_matches (struct task_struct *thread, unsigned long addr){	unsigned long thread_rbs_end;	struct pt_regs *thread_regs;	if (ptrace_check_attach(thread, 0) < 0)		/*

⌨️ 快捷键说明

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