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

📄 traps.c

📁 linux-2.6.15.6
💻 C
📖 第 1 页 / 共 2 页
字号:
{	int code = 0;	unsigned long fpscr;	flush_fp_to_thread(current);	fpscr = current->thread.fpscr.val;	/* Invalid operation */	if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))		code = FPE_FLTINV;	/* Overflow */	else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))		code = FPE_FLTOVF;	/* Underflow */	else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))		code = FPE_FLTUND;	/* Divide by zero */	else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))		code = FPE_FLTDIV;	/* Inexact result */	else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))		code = FPE_FLTRES;	_exception(SIGFPE, regs, code, regs->nip);}/* * Illegal instruction emulation support.  Originally written to * provide the PVR to user applications using the mfspr rd, PVR. * Return non-zero if we can't emulate, or -EFAULT if the associated * memory access caused an access fault.  Return zero on success. * * There are a couple of ways to do this, either "decode" the instruction * or directly match lots of bits.  In this case, matching lots of * bits is faster and easier. * */#define INST_MFSPR_PVR		0x7c1f42a6#define INST_MFSPR_PVR_MASK	0xfc1fffff#define INST_DCBA		0x7c0005ec#define INST_DCBA_MASK		0x7c0007fe#define INST_MCRXR		0x7c000400#define INST_MCRXR_MASK		0x7c0007fe#define INST_STRING		0x7c00042a#define INST_STRING_MASK	0x7c0007fe#define INST_STRING_GEN_MASK	0x7c00067e#define INST_LSWI		0x7c0004aa#define INST_LSWX		0x7c00042a#define INST_STSWI		0x7c0005aa#define INST_STSWX		0x7c00052astatic int emulate_string_inst(struct pt_regs *regs, u32 instword){	u8 rT = (instword >> 21) & 0x1f;	u8 rA = (instword >> 16) & 0x1f;	u8 NB_RB = (instword >> 11) & 0x1f;	u32 num_bytes;	unsigned long EA;	int pos = 0;	/* Early out if we are an invalid form of lswx */	if ((instword & INST_STRING_MASK) == INST_LSWX)		if ((rT == rA) || (rT == NB_RB))			return -EINVAL;	EA = (rA == 0) ? 0 : regs->gpr[rA];	switch (instword & INST_STRING_MASK) {		case INST_LSWX:		case INST_STSWX:			EA += NB_RB;			num_bytes = regs->xer & 0x7f;			break;		case INST_LSWI:		case INST_STSWI:			num_bytes = (NB_RB == 0) ? 32 : NB_RB;			break;		default:			return -EINVAL;	}	while (num_bytes != 0)	{		u8 val;		u32 shift = 8 * (3 - (pos & 0x3));		switch ((instword & INST_STRING_MASK)) {			case INST_LSWX:			case INST_LSWI:				if (get_user(val, (u8 __user *)EA))					return -EFAULT;				/* first time updating this reg,				 * zero it out */				if (pos == 0)					regs->gpr[rT] = 0;				regs->gpr[rT] |= val << shift;				break;			case INST_STSWI:			case INST_STSWX:				val = regs->gpr[rT] >> shift;				if (put_user(val, (u8 __user *)EA))					return -EFAULT;				break;		}		/* move EA to next address */		EA += 1;		num_bytes--;		/* manage our position within the register */		if (++pos == 4) {			pos = 0;			if (++rT == 32)				rT = 0;		}	}	return 0;}static int emulate_instruction(struct pt_regs *regs){	u32 instword;	u32 rd;	if (!user_mode(regs))		return -EINVAL;	CHECK_FULL_REGS(regs);	if (get_user(instword, (u32 __user *)(regs->nip)))		return -EFAULT;	/* Emulate the mfspr rD, PVR. */	if ((instword & INST_MFSPR_PVR_MASK) == INST_MFSPR_PVR) {		rd = (instword >> 21) & 0x1f;		regs->gpr[rd] = mfspr(SPRN_PVR);		return 0;	}	/* Emulating the dcba insn is just a no-op.  */	if ((instword & INST_DCBA_MASK) == INST_DCBA)		return 0;	/* Emulate the mcrxr insn.  */	if ((instword & INST_MCRXR_MASK) == INST_MCRXR) {		int shift = (instword >> 21) & 0x1c;		unsigned long msk = 0xf0000000UL >> shift;		regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);		regs->xer &= ~0xf0000000UL;		return 0;	}	/* Emulate load/store string insn. */	if ((instword & INST_STRING_GEN_MASK) == INST_STRING)		return emulate_string_inst(regs, instword);	return -EINVAL;}/* * Look through the list of trap instructions that are used for BUG(), * BUG_ON() and WARN_ON() and see if we hit one.  At this point we know * that the exception was caused by a trap instruction of some kind. * Returns 1 if we should continue (i.e. it was a WARN_ON) or 0 * otherwise. */extern struct bug_entry __start___bug_table[], __stop___bug_table[];#ifndef CONFIG_MODULES#define module_find_bug(x)	NULL#endifstruct bug_entry *find_bug(unsigned long bugaddr){	struct bug_entry *bug;	for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)		if (bugaddr == bug->bug_addr)			return bug;	return module_find_bug(bugaddr);}static int check_bug_trap(struct pt_regs *regs){	struct bug_entry *bug;	unsigned long addr;	if (regs->msr & MSR_PR)		return 0;	/* not in kernel */	addr = regs->nip;	/* address of trap instruction */	if (addr < PAGE_OFFSET)		return 0;	bug = find_bug(regs->nip);	if (bug == NULL)		return 0;	if (bug->line & BUG_WARNING_TRAP) {		/* this is a WARN_ON rather than BUG/BUG_ON */		printk(KERN_ERR "Badness in %s at %s:%ld\n",		       bug->function, bug->file,		       bug->line & ~BUG_WARNING_TRAP);		dump_stack();		return 1;	}	printk(KERN_CRIT "kernel BUG in %s at %s:%ld!\n",	       bug->function, bug->file, bug->line);	return 0;}void __kprobes program_check_exception(struct pt_regs *regs){	unsigned int reason = get_reason(regs);	extern int do_mathemu(struct pt_regs *regs);#ifdef CONFIG_MATH_EMULATION	/* (reason & REASON_ILLEGAL) would be the obvious thing here,	 * but there seems to be a hardware bug on the 405GP (RevD)	 * that means ESR is sometimes set incorrectly - either to	 * ESR_DST (!?) or 0.  In the process of chasing this with the	 * hardware people - not sure if it can happen on any illegal	 * instruction or only on FP instructions, whether there is a	 * pattern to occurences etc. -dgibson 31/Mar/2003 */	if (!(reason & REASON_TRAP) && do_mathemu(regs) == 0) {		emulate_single_step(regs);		return;	}#endif /* CONFIG_MATH_EMULATION */	if (reason & REASON_FP) {		/* IEEE FP exception */		parse_fpe(regs);		return;	}	if (reason & REASON_TRAP) {		/* trap exception */		if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)				== NOTIFY_STOP)			return;		if (debugger_bpt(regs))			return;		if (check_bug_trap(regs)) {			regs->nip += 4;			return;		}		_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);		return;	}	/* Try to emulate it if we should. */	if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {		switch (emulate_instruction(regs)) {		case 0:			regs->nip += 4;			emulate_single_step(regs);			return;		case -EFAULT:			_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);			return;		}	}	if (reason & REASON_PRIVILEGED)		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);	else		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);}void alignment_exception(struct pt_regs *regs){	int fixed;	fixed = fix_alignment(regs);	if (fixed == 1) {		regs->nip += 4;	/* skip over emulated instruction */		emulate_single_step(regs);		return;	}	/* Operand address was bad */	if (fixed == -EFAULT) {		if (user_mode(regs))			_exception(SIGSEGV, regs, SEGV_ACCERR, regs->dar);		else			/* Search exception table */			bad_page_fault(regs, regs->dar, SIGSEGV);		return;	}	_exception(SIGBUS, regs, BUS_ADRALN, regs->dar);}void StackOverflow(struct pt_regs *regs){	printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",	       current, regs->gpr[1]);	debugger(regs);	show_regs(regs);	panic("kernel stack overflow");}void nonrecoverable_exception(struct pt_regs *regs){	printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",	       regs->nip, regs->msr);	debugger(regs);	die("nonrecoverable exception", regs, SIGKILL);}void trace_syscall(struct pt_regs *regs){	printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld    %s\n",	       current, current->pid, regs->nip, regs->link, regs->gpr[0],	       regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());}void kernel_fp_unavailable_exception(struct pt_regs *regs){	printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "			  "%lx at %lx\n", regs->trap, regs->nip);	die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);}void altivec_unavailable_exception(struct pt_regs *regs){#if !defined(CONFIG_ALTIVEC)	if (user_mode(regs)) {		/* A user program has executed an altivec instruction,		   but this kernel doesn't support altivec. */		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);		return;	}#endif	printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "			"%lx at %lx\n", regs->trap, regs->nip);	die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);}#if defined(CONFIG_PPC64) || defined(CONFIG_E500)void performance_monitor_exception(struct pt_regs *regs){	perf_irq(regs);}#endif#ifdef CONFIG_8xxvoid SoftwareEmulation(struct pt_regs *regs){	extern int do_mathemu(struct pt_regs *);	extern int Soft_emulate_8xx(struct pt_regs *);	int errcode;	CHECK_FULL_REGS(regs);	if (!user_mode(regs)) {		debugger(regs);		die("Kernel Mode Software FPU Emulation", regs, SIGFPE);	}#ifdef CONFIG_MATH_EMULATION	errcode = do_mathemu(regs);#else	errcode = Soft_emulate_8xx(regs);#endif	if (errcode) {		if (errcode > 0)			_exception(SIGFPE, regs, 0, 0);		else if (errcode == -EFAULT)			_exception(SIGSEGV, regs, 0, 0);		else			_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);	} else		emulate_single_step(regs);}#endif /* CONFIG_8xx */#if defined(CONFIG_40x) || defined(CONFIG_BOOKE)void DebugException(struct pt_regs *regs, unsigned long debug_status){	if (debug_status & DBSR_IC) {	/* instruction completion */		regs->msr &= ~MSR_DE;		if (user_mode(regs)) {			current->thread.dbcr0 &= ~DBCR0_IC;		} else {			/* Disable instruction completion */			mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);			/* Clear the instruction completion event */			mtspr(SPRN_DBSR, DBSR_IC);			if (debugger_sstep(regs))				return;		}		_exception(SIGTRAP, regs, TRAP_TRACE, 0);	}}#endif /* CONFIG_4xx || CONFIG_BOOKE */#if !defined(CONFIG_TAU_INT)void TAUException(struct pt_regs *regs){	printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",	       regs->nip, regs->msr, regs->trap, print_tainted());}#endif /* CONFIG_INT_TAU */#ifdef CONFIG_ALTIVECvoid altivec_assist_exception(struct pt_regs *regs){	int err;	if (!user_mode(regs)) {		printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"		       " at %lx\n", regs->nip);		die("Kernel VMX/Altivec assist exception", regs, SIGILL);	}	flush_altivec_to_thread(current);	err = emulate_altivec(regs);	if (err == 0) {		regs->nip += 4;		/* skip emulated instruction */		emulate_single_step(regs);		return;	}	if (err == -EFAULT) {		/* got an error reading the instruction */		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);	} else {		/* didn't recognize the instruction */		/* XXX quick hack for now: set the non-Java bit in the VSCR */		if (printk_ratelimit())			printk(KERN_ERR "Unrecognized altivec instruction "			       "in %s at %lx\n", current->comm, regs->nip);		current->thread.vscr.u[3] |= 0x10000;	}}#endif /* CONFIG_ALTIVEC */#ifdef CONFIG_FSL_BOOKEvoid CacheLockingException(struct pt_regs *regs, unsigned long address,			   unsigned long error_code){	/* We treat cache locking instructions from the user	 * as priv ops, in the future we could try to do	 * something smarter	 */	if (error_code & (ESR_DLK|ESR_ILK))		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);	return;}#endif /* CONFIG_FSL_BOOKE */#ifdef CONFIG_SPEvoid SPEFloatingPointException(struct pt_regs *regs){	unsigned long spefscr;	int fpexc_mode;	int code = 0;	spefscr = current->thread.spefscr;	fpexc_mode = current->thread.fpexc_mode;	/* Hardware does not neccessarily set sticky	 * underflow/overflow/invalid flags */	if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {		code = FPE_FLTOVF;		spefscr |= SPEFSCR_FOVFS;	}	else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {		code = FPE_FLTUND;		spefscr |= SPEFSCR_FUNFS;	}	else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))		code = FPE_FLTDIV;	else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {		code = FPE_FLTINV;		spefscr |= SPEFSCR_FINVS;	}	else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))		code = FPE_FLTRES;	current->thread.spefscr = spefscr;	_exception(SIGFPE, regs, code, regs->nip);	return;}#endif/* * We enter here if we get an unrecoverable exception, that is, one * that happened at a point where the RI (recoverable interrupt) bit * in the MSR is 0.  This indicates that SRR0/1 are live, and that * we therefore lost state by taking this exception. */void unrecoverable_exception(struct pt_regs *regs){	printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",	       regs->trap, regs->nip);	die("Unrecoverable exception", regs, SIGABRT);}#ifdef CONFIG_BOOKE_WDT/* * Default handler for a Watchdog exception, * spins until a reboot occurs */void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs){	/* Generic WatchdogHandler, implement your own */	mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));	return;}void WatchdogException(struct pt_regs *regs){	printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");	WatchdogHandler(regs);}#endif/* * We enter here if we discover during exception entry that we are * running in supervisor mode with a userspace value in the stack pointer. */void kernel_bad_stack(struct pt_regs *regs){	printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",	       regs->gpr[1], regs->nip);	die("Bad kernel stack pointer", regs, SIGABRT);}void __init trap_init(void){}

⌨️ 快捷键说明

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