📄 traps.c
字号:
/* * This file is subject to the terms and conditions of the GNU General Public * License. See the file "COPYING" in the main directory of this archive * for more details. * * Copyright (C) 1994 - 1999, 2000, 01 Ralf Baechle * Copyright (C) 1995, 1996 Paul M. Antoine * Copyright (C) 1998 Ulf Carlsson * Copyright (C) 1999 Silicon Graphics, Inc. * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com * Copyright (C) 2000, 01 MIPS Technologies, Inc. * Copyright (C) 2002, 2003, 2004 Maciej W. Rozycki */#include <linux/config.h>#include <linux/init.h>#include <linux/mm.h>#include <linux/module.h>#include <linux/sched.h>#include <linux/smp.h>#include <linux/smp_lock.h>#include <linux/spinlock.h>#include <asm/bootinfo.h>#include <asm/branch.h>#include <asm/break.h>#include <asm/cpu.h>#include <asm/fpu.h>#include <asm/cachectl.h>#include <asm/inst.h>#include <asm/jazz.h>#include <asm/module.h>#include <asm/pgtable.h>#include <asm/io.h>#include <asm/siginfo.h>#include <asm/watch.h>#include <asm/tlbdebug.h>#include <asm/types.h>#include <asm/system.h>#include <asm/traps.h>#include <asm/uaccess.h>#include <asm/mmu_context.h>extern asmlinkage void handle_mod(void);extern asmlinkage void handle_tlbl(void);extern asmlinkage void handle_tlbs(void);extern asmlinkage void handle_adel(void);extern asmlinkage void handle_ades(void);extern asmlinkage void handle_ibe(void);extern asmlinkage void handle_dbe(void);extern asmlinkage void handle_sys(void);extern asmlinkage void handle_bp(void);extern asmlinkage void handle_ri(void);extern asmlinkage void handle_cpu(void);extern asmlinkage void handle_ov(void);extern asmlinkage void handle_tr(void);extern asmlinkage void handle_fpe(void);extern asmlinkage void handle_mdmx(void);extern asmlinkage void handle_watch(void);extern asmlinkage void handle_mcheck(void);extern asmlinkage void handle_reserved(void);extern int fpu_emulator_cop1Handler(int xcptno, struct pt_regs *xcp, struct mips_fpu_soft_struct *ctx);void (*board_be_init)(void);int (*board_be_handler)(struct pt_regs *regs, int is_fixup);void (*board_nmi_handler_setup)(void);int kstack_depth_to_print = 24;/* * These constant is for searching for possible module text segments. * MODULE_RANGE is a guess of how much space is likely to be vmalloced. */#define MODULE_RANGE (8*1024*1024)/* * If the address is either in the .text section of the * kernel, or in the vmalloc'ed module regions, it *may* * be the address of a calling routine */#ifdef CONFIG_MODULESextern struct module *module_list;extern struct module kernel_module;static inline int kernel_text_address(long addr){ extern char _stext, _etext; int retval = 0; struct module *mod; if (addr >= (long) &_stext && addr <= (long) &_etext) return 1; for (mod = module_list; mod != &kernel_module; mod = mod->next) { /* mod_bound tests for addr being inside the vmalloc'ed * module area. Of course it'd be better to test only * for the .text subset... */ if (mod_bound(addr, 0, mod)) { retval = 1; break; } } return retval;}#elsestatic inline int kernel_text_address(long addr){ extern char _stext, _etext; return (addr >= (long) &_stext && addr <= (long) &_etext);}#endif/* * This routine abuses get_user()/put_user() to reference pointers * with at least a bit of error checking ... */void show_stack(long *sp){ int i; long stackdata; sp = sp ? sp : (long *)&sp; printk("Stack: "); i = 1; while ((long) sp & (PAGE_SIZE - 1)) { if (i && ((i % 8) == 0)) printk("\n"); if (i > 40) { printk(" ..."); break; } if (__get_user(stackdata, sp++)) { printk(" (Bad stack address)"); break; } printk(" %08lx", stackdata); i++; } printk("\n");}void show_trace(long *sp){ int i; long addr; sp = sp ? sp : (long *) &sp; printk("Call Trace: "); i = 1; while ((long) sp & (PAGE_SIZE - 1)) { if (__get_user(addr, sp++)) { if (i && ((i % 6) == 0)) printk("\n"); printk(" (Bad stack address)\n"); break; } /* * If the address is either in the text segment of the * kernel, or in the region which contains vmalloc'ed * memory, it *may* be the address of a calling * routine; if so, print it so that someone tracing * down the cause of the crash will be able to figure * out the call path that was taken. */ if (kernel_text_address(addr)) { if (i && ((i % 6) == 0)) printk("\n"); if (i > 40) { printk(" ..."); break; } printk(" [<%08lx>]", addr); i++; } } printk("\n");}void show_trace_task(struct task_struct *tsk){ show_trace((long *)tsk->thread.reg29);}void show_code(unsigned int *pc){ long i; printk("\nCode:"); for(i = -3 ; i < 6 ; i++) { unsigned long insn; if (__get_user(insn, pc + i)) { printk(" (Bad address in epc)\n"); break; } printk("%c%08lx%c",(i?' ':'<'),insn,(i?' ':'>')); }}void show_regs(struct pt_regs *regs){ /* * Saved main processor registers */ printk("$0 : %08x %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n", 0, regs->regs[1], regs->regs[2], regs->regs[3], regs->regs[4], regs->regs[5], regs->regs[6], regs->regs[7]); printk("$8 : %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n", regs->regs[8], regs->regs[9], regs->regs[10], regs->regs[11], regs->regs[12], regs->regs[13], regs->regs[14], regs->regs[15]); printk("$16: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n", regs->regs[16], regs->regs[17], regs->regs[18], regs->regs[19], regs->regs[20], regs->regs[21], regs->regs[22], regs->regs[23]); printk("$24: %08lx %08lx %08lx %08lx %08lx %08lx\n", regs->regs[24], regs->regs[25], regs->regs[28], regs->regs[29], regs->regs[30], regs->regs[31]); printk("Hi : %08lx\n", regs->hi); printk("Lo : %08lx\n", regs->lo); /* * Saved cp0 registers */ printk("epc : %08lx %s\n", regs->cp0_epc, print_tainted()); printk("Status: %08lx\n", regs->cp0_status); printk("Cause : %08lx\n", regs->cp0_cause); printk("PrId : %08x\n", read_c0_prid());}void show_registers(struct pt_regs *regs){ show_regs(regs); printk("Process %s (pid: %d, stackpage=%08lx)\n", current->comm, current->pid, (unsigned long) current); show_stack((long *) regs->regs[29]); show_trace((long *) regs->regs[29]); show_code((unsigned int *) regs->cp0_epc); printk("\n");}static spinlock_t die_lock = SPIN_LOCK_UNLOCKED;void __die(const char * str, struct pt_regs * regs, const char * file, const char * func, unsigned long line){ console_verbose(); spin_lock_irq(&die_lock); printk("%s", str); if (file && func) printk(" in %s:%s, line %ld", file, func, line); printk(":\n"); show_registers(regs); spin_unlock_irq(&die_lock); do_exit(SIGSEGV);}void __die_if_kernel(const char * str, struct pt_regs * regs, const char * file, const char * func, unsigned long line){ if (!user_mode(regs)) __die(str, regs, file, func, line);}extern const struct exception_table_entry __start___dbe_table[];extern const struct exception_table_entry __stop___dbe_table[];void __declare_dbe_table(void){ __asm__ __volatile__( ".section\t__dbe_table,\"a\"\n\t" ".previous" );}static inline unsigned longsearch_one_table(const struct exception_table_entry *first, const struct exception_table_entry *last, unsigned long value){ const struct exception_table_entry *mid; long diff; while (first < last) { mid = (last - first) / 2 + first; diff = mid->insn - value; if (diff < 0) first = mid + 1; else last = mid; } return (first == last && first->insn == value) ? first->nextinsn : 0;}extern spinlock_t modlist_lock;static inline unsigned longsearch_dbe_table(unsigned long addr){ unsigned long ret = 0;#ifndef CONFIG_MODULES /* There is only the kernel to search. */ ret = search_one_table(__start___dbe_table, __stop___dbe_table-1, addr); return ret;#else unsigned long flags; /* The kernel is the last "module" -- no need to treat it special. */ struct module *mp; struct archdata *ap; spin_lock_irqsave(&modlist_lock, flags); for (mp = module_list; mp != NULL; mp = mp->next) { if (!mod_member_present(mp, archdata_end) || !mod_archdata_member_present(mp, struct archdata, dbe_table_end)) continue; ap = (struct archdata *)(mp->archdata_start); if (ap->dbe_table_start == NULL || !(mp->flags & (MOD_RUNNING | MOD_INITIALIZING))) continue; ret = search_one_table(ap->dbe_table_start, ap->dbe_table_end - 1, addr); if (ret) break; } spin_unlock_irqrestore(&modlist_lock, flags); return ret;#endif}asmlinkage void do_be(struct pt_regs *regs){ unsigned long new_epc; unsigned long fixup = 0; int data = regs->cp0_cause & 4; int action = MIPS_BE_FATAL; if (data && !user_mode(regs)) fixup = search_dbe_table(exception_epc(regs)); if (fixup) action = MIPS_BE_FIXUP; if (board_be_handler) action = board_be_handler(regs, fixup != 0); switch (action) { case MIPS_BE_DISCARD: return; case MIPS_BE_FIXUP: if (fixup) { new_epc = fixup_exception(dpf_reg, fixup, regs->cp0_epc); regs->cp0_epc = new_epc; return; } break; default: break; } /* * Assume it would be too dangerous to continue ... */ printk(KERN_ALERT "%s bus error, epc == %08lx, ra == %08lx\n", data ? "Data" : "Instruction", regs->cp0_epc, regs->regs[31]); die_if_kernel("Oops", regs); force_sig(SIGBUS, current);}static inline int get_insn_opcode(struct pt_regs *regs, unsigned int *opcode){ unsigned int *epc; epc = (unsigned int *) regs->cp0_epc + ((regs->cp0_cause & CAUSEF_BD) != 0); if (!get_user(*opcode, epc)) return 0; force_sig(SIGSEGV, current); return 1;}/* * ll/sc emulation */#define OPCODE 0xfc000000#define BASE 0x03e00000#define RT 0x001f0000#define OFFSET 0x0000ffff#define LL 0xc0000000#define SC 0xe0000000/* * The ll_bit is cleared by r*_switch.S */unsigned long ll_bit;static struct task_struct *ll_task = NULL;static inline void simulate_ll(struct pt_regs *regs, unsigned int opcode){ unsigned long value, *vaddr; long offset; int signal = 0; /* * analyse the ll instruction that just caused a ri exception * and put the referenced address to addr. */ /* sign extend offset */ offset = opcode & OFFSET; offset <<= 16; offset >>= 16; vaddr = (unsigned long *)((long)(regs->regs[(opcode & BASE) >> 21]) + offset); if ((unsigned long)vaddr & 3) { signal = SIGBUS; goto sig; } if (get_user(value, vaddr)) { signal = SIGSEGV; goto sig; } if (ll_task == NULL || ll_task == current) { ll_bit = 1; } else { ll_bit = 0; } ll_task = current; regs->regs[(opcode & RT) >> 16] = value; compute_return_epc(regs); return;sig: force_sig(signal, current);}static inline void simulate_sc(struct pt_regs *regs, unsigned int opcode){ unsigned long *vaddr, reg; long offset; int signal = 0; /* * analyse the sc instruction that just caused a ri exception * and put the referenced address to addr. */ /* sign extend offset */ offset = opcode & OFFSET; offset <<= 16; offset >>= 16; vaddr = (unsigned long *)((long)(regs->regs[(opcode & BASE) >> 21]) + offset); reg = (opcode & RT) >> 16; if ((unsigned long)vaddr & 3) { signal = SIGBUS; goto sig; } if (ll_bit == 0 || ll_task != current) { regs->regs[reg] = 0; compute_return_epc(regs); return; } if (put_user(regs->regs[reg], vaddr)) { signal = SIGSEGV; goto sig; } regs->regs[reg] = 1; compute_return_epc(regs); return;sig: force_sig(signal, current);}/* * ll uses the opcode of lwc0 and sc uses the opcode of swc0. That is both * opcodes are supposed to result in coprocessor unusable exceptions if * executed on ll/sc-less processors. That's the theory. In practice a * few processors such as NEC's VR4100 throw reserved instruction exceptions * instead, so we're doing the emulation thing in both exception handlers. */static inline int simulate_llsc(struct pt_regs *regs){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -