traps.c

来自「优龙2410linux2.6.8内核源代码」· C语言 代码 · 共 950 行 · 第 1/2 页

C
950
字号
/* *  linux/arch/i386/traps.c * *  Copyright (C) 1991, 1992  Linus Torvalds * *  Pentium III FXSR, SSE support *	Gareth Hughes <gareth@valinux.com>, May 2000 *//* * 'Traps.c' handles hardware traps and faults after we have saved some * state in 'asm.s'. */#include <linux/config.h>#include <linux/sched.h>#include <linux/kernel.h>#include <linux/string.h>#include <linux/errno.h>#include <linux/timer.h>#include <linux/mm.h>#include <linux/init.h>#include <linux/delay.h>#include <linux/spinlock.h>#include <linux/interrupt.h>#include <linux/highmem.h>#include <linux/kallsyms.h>#include <linux/ptrace.h>#include <linux/version.h>#ifdef CONFIG_EISA#include <linux/ioport.h>#include <linux/eisa.h>#endif#ifdef CONFIG_MCA#include <linux/mca.h>#endif#include <asm/processor.h>#include <asm/system.h>#include <asm/uaccess.h>#include <asm/io.h>#include <asm/atomic.h>#include <asm/debugreg.h>#include <asm/desc.h>#include <asm/i387.h>#include <asm/nmi.h>#include <asm/smp.h>#include <asm/arch_hooks.h>#include <linux/irq.h>#include <linux/module.h>#include "mach_traps.h"asmlinkage int system_call(void);asmlinkage void lcall7(void);asmlinkage void lcall27(void);struct desc_struct default_ldt[] = { { 0, 0 }, { 0, 0 }, { 0, 0 },		{ 0, 0 }, { 0, 0 } };/* Do we ignore FPU interrupts ? */char ignore_fpu_irq = 0;/* * The IDT has to be page-aligned to simplify the Pentium * F0 0F bug workaround.. We have a special link segment * for this. */struct desc_struct idt_table[256] __attribute__((__section__(".data.idt"))) = { {0, 0}, };asmlinkage void divide_error(void);asmlinkage void debug(void);asmlinkage void nmi(void);asmlinkage void int3(void);asmlinkage void overflow(void);asmlinkage void bounds(void);asmlinkage void invalid_op(void);asmlinkage void device_not_available(void);asmlinkage void coprocessor_segment_overrun(void);asmlinkage void invalid_TSS(void);asmlinkage void segment_not_present(void);asmlinkage void stack_segment(void);asmlinkage void general_protection(void);asmlinkage void page_fault(void);asmlinkage void coprocessor_error(void);asmlinkage void simd_coprocessor_error(void);asmlinkage void alignment_check(void);asmlinkage void spurious_interrupt_bug(void);asmlinkage void machine_check(void);static int kstack_depth_to_print = 24;static int valid_stack_ptr(struct task_struct *task, void *p){	if (p <= (void *)task->thread_info)		return 0;	if (kstack_end(p))		return 0;	return 1;}#ifdef CONFIG_FRAME_POINTERstatic void print_context_stack(struct task_struct *task, unsigned long *stack,			 unsigned long ebp){	unsigned long addr;	while (valid_stack_ptr(task, (void *)ebp)) {		addr = *(unsigned long *)(ebp + 4);		printk(" [<%08lx>] ", addr);		print_symbol("%s", addr);		printk("\n");		ebp = *(unsigned long *)ebp;	}}#elsestatic void print_context_stack(struct task_struct *task, unsigned long *stack,			 unsigned long ebp){	unsigned long addr;	while (!kstack_end(stack)) {		addr = *stack++;		if (__kernel_text_address(addr)) {			printk(" [<%08lx>]", addr);			print_symbol(" %s", addr);			printk("\n");		}	}}#endifvoid show_trace(struct task_struct *task, unsigned long * stack){	unsigned long ebp;	if (!task)		task = current;	if (!valid_stack_ptr(task, stack)) {		printk("Stack pointer is garbage, not printing trace\n");		return;	}	if (task == current) {		/* Grab ebp right from our regs */		asm ("movl %%ebp, %0" : "=r" (ebp) : );	} else {		/* ebp is the last reg pushed by switch_to */		ebp = *(unsigned long *) task->thread.esp;	}	while (1) {		struct thread_info *context;		context = (struct thread_info *)			((unsigned long)stack & (~(THREAD_SIZE - 1)));		print_context_stack(task, stack, ebp);		stack = (unsigned long*)context->previous_esp;		if (!stack)			break;		printk(" =======================\n");	}}void show_stack(struct task_struct *task, unsigned long *esp){	unsigned long *stack;	int i;	if (esp == NULL) {		if (task)			esp = (unsigned long*)task->thread.esp;		else			esp = (unsigned long *)&esp;	}	stack = esp;	for(i = 0; i < kstack_depth_to_print; i++) {		if (kstack_end(stack))			break;		if (i && ((i % 8) == 0))			printk("\n       ");		printk("%08lx ", *stack++);	}	printk("\nCall Trace:\n");	show_trace(task, esp);}/* * The architecture-independent dump_stack generator */void dump_stack(void){	unsigned long stack;	show_trace(current, &stack);}EXPORT_SYMBOL(dump_stack);void show_registers(struct pt_regs *regs){	int i;	int in_kernel = 1;	unsigned long esp;	unsigned short ss;	esp = (unsigned long) (&regs->esp);	ss = __KERNEL_DS;	if (regs->xcs & 3) {		in_kernel = 0;		esp = regs->esp;		ss = regs->xss & 0xffff;	}	print_modules();	printk("CPU:    %d\nEIP:    %04x:[<%08lx>]    %s\nEFLAGS: %08lx"			"   (%s) \n",		smp_processor_id(), 0xffff & regs->xcs, regs->eip,		print_tainted(), regs->eflags, UTS_RELEASE);	print_symbol("EIP is at %s\n", regs->eip);	printk("eax: %08lx   ebx: %08lx   ecx: %08lx   edx: %08lx\n",		regs->eax, regs->ebx, regs->ecx, regs->edx);	printk("esi: %08lx   edi: %08lx   ebp: %08lx   esp: %08lx\n",		regs->esi, regs->edi, regs->ebp, esp);	printk("ds: %04x   es: %04x   ss: %04x\n",		regs->xds & 0xffff, regs->xes & 0xffff, ss);	printk("Process %s (pid: %d, threadinfo=%p task=%p)",		current->comm, current->pid, current_thread_info(), current);	/*	 * When in-kernel, we also print out the stack and code at the	 * time of the fault..	 */	if (in_kernel) {		printk("\nStack: ");		show_stack(NULL, (unsigned long*)esp);		printk("Code: ");		if(regs->eip < PAGE_OFFSET)			goto bad;		for(i=0;i<20;i++)		{			unsigned char c;			if(__get_user(c, &((unsigned char*)regs->eip)[i])) {bad:				printk(" Bad EIP value.");				break;			}			printk("%02x ", c);		}	}	printk("\n");}	static void handle_BUG(struct pt_regs *regs){	unsigned short ud2;	unsigned short line;	char *file;	char c;	unsigned long eip;	if (regs->xcs & 3)		goto no_bug;		/* Not in kernel */	eip = regs->eip;	if (eip < PAGE_OFFSET)		goto no_bug;	if (__get_user(ud2, (unsigned short *)eip))		goto no_bug;	if (ud2 != 0x0b0f)		goto no_bug;	if (__get_user(line, (unsigned short *)(eip + 2)))		goto bug;	if (__get_user(file, (char **)(eip + 4)) ||		(unsigned long)file < PAGE_OFFSET || __get_user(c, file))		file = "<bad filename>";	printk("------------[ cut here ]------------\n");	printk(KERN_ALERT "kernel BUG at %s:%d!\n", file, line);no_bug:	return;	/* Here we know it was a BUG but file-n-line is unavailable */bug:	printk("Kernel BUG\n");}spinlock_t die_lock = SPIN_LOCK_UNLOCKED;void die(const char * str, struct pt_regs * regs, long err){	static int die_counter;	int nl = 0;	console_verbose();	spin_lock_irq(&die_lock);	bust_spinlocks(1);	handle_BUG(regs);	printk(KERN_ALERT "%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);#ifdef CONFIG_PREEMPT	printk("PREEMPT ");	nl = 1;#endif#ifdef CONFIG_SMP	printk("SMP ");	nl = 1;#endif#ifdef CONFIG_DEBUG_PAGEALLOC	printk("DEBUG_PAGEALLOC");	nl = 1;#endif	if (nl)		printk("\n");	show_registers(regs);	bust_spinlocks(0);	spin_unlock_irq(&die_lock);	if (in_interrupt())		panic("Fatal exception in interrupt");	if (panic_on_oops) {		printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");		set_current_state(TASK_UNINTERRUPTIBLE);		schedule_timeout(5 * HZ);		panic("Fatal exception");	}	do_exit(SIGSEGV);}static inline void die_if_kernel(const char * str, struct pt_regs * regs, long err){	if (!(regs->eflags & VM_MASK) && !(3 & regs->xcs))		die(str, regs, err);}static inline unsigned long get_cr2(void){	unsigned long address;	/* get the address */	__asm__("movl %%cr2,%0":"=r" (address));	return address;}static inline void do_trap(int trapnr, int signr, char *str, int vm86,			   struct pt_regs * regs, long error_code, siginfo_t *info){	if (regs->eflags & VM_MASK) {		if (vm86)			goto vm86_trap;		goto trap_signal;	}	if (!(regs->xcs & 3))		goto kernel_trap;	trap_signal: {		struct task_struct *tsk = current;		tsk->thread.error_code = error_code;		tsk->thread.trap_no = trapnr;		if (info)			force_sig_info(signr, info, tsk);		else			force_sig(signr, tsk);		return;	}	kernel_trap: {		if (!fixup_exception(regs))			die(str, regs, error_code);		return;	}	vm86_trap: {		int ret = handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, trapnr);		if (ret) goto trap_signal;		return;	}}#define DO_ERROR(trapnr, signr, str, name) \asmlinkage void do_##name(struct pt_regs * regs, long error_code) \{ \	do_trap(trapnr, signr, str, 0, regs, error_code, NULL); \}#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \asmlinkage void do_##name(struct pt_regs * regs, long error_code) \{ \	siginfo_t info; \	info.si_signo = signr; \	info.si_errno = 0; \	info.si_code = sicode; \	info.si_addr = (void __user *)siaddr; \	do_trap(trapnr, signr, str, 0, regs, error_code, &info); \}#define DO_VM86_ERROR(trapnr, signr, str, name) \asmlinkage void do_##name(struct pt_regs * regs, long error_code) \{ \	do_trap(trapnr, signr, str, 1, regs, error_code, NULL); \}#define DO_VM86_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \asmlinkage void do_##name(struct pt_regs * regs, long error_code) \{ \	siginfo_t info; \	info.si_signo = signr; \	info.si_errno = 0; \	info.si_code = sicode; \	info.si_addr = (void __user *)siaddr; \	do_trap(trapnr, signr, str, 1, regs, error_code, &info); \}DO_VM86_ERROR_INFO( 0, SIGFPE,  "divide error", divide_error, FPE_INTDIV, regs->eip)DO_VM86_ERROR( 3, SIGTRAP, "int3", int3)DO_VM86_ERROR( 4, SIGSEGV, "overflow", overflow)DO_VM86_ERROR( 5, SIGSEGV, "bounds", bounds)DO_ERROR_INFO( 6, SIGILL,  "invalid operand", invalid_op, ILL_ILLOPN, regs->eip)DO_ERROR( 9, SIGFPE,  "coprocessor segment overrun", coprocessor_segment_overrun)DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)DO_ERROR(11, SIGBUS,  "segment not present", segment_not_present)DO_ERROR(12, SIGBUS,  "stack segment", stack_segment)DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, get_cr2())asmlinkage void do_general_protection(struct pt_regs * regs, long error_code){	if (regs->eflags & X86_EFLAGS_IF)		local_irq_enable(); 	if (regs->eflags & VM_MASK)		goto gp_in_vm86;	if (!(regs->xcs & 3))		goto gp_in_kernel;	current->thread.error_code = error_code;	current->thread.trap_no = 13;	force_sig(SIGSEGV, current);	return;gp_in_vm86:	local_irq_enable();	handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);	return;gp_in_kernel:	if (!fixup_exception(regs))		die("general protection fault", regs, error_code);}static void mem_parity_error(unsigned char reason, struct pt_regs * regs){	printk("Uhhuh. NMI received. Dazed and confused, but trying to continue\n");	printk("You probably have a hardware problem with your RAM chips\n");	/* Clear and disable the memory parity error line. */	clear_mem_error(reason);}static void io_check_error(unsigned char reason, struct pt_regs * regs){	unsigned long i;	printk("NMI: IOCK error (debug interrupt?)\n");	show_registers(regs);	/* Re-enable the IOCK line, wait for a few seconds */	reason = (reason & 0xf) | 8;

⌨️ 快捷键说明

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