📄 traps.c
字号:
/* * arch/alpha/kernel/traps.c * * (C) Copyright 1994 Linus Torvalds *//* * This file initializes the trap entry points */#include <linux/config.h>#include <linux/mm.h>#include <linux/sched.h>#include <linux/tty.h>#include <linux/delay.h>#include <linux/smp_lock.h>#include <asm/gentrap.h>#include <asm/uaccess.h>#include <asm/unaligned.h>#include <asm/sysinfo.h>#include "proto.h"voiddik_show_regs(struct pt_regs *regs, unsigned long *r9_15){ printk("pc = [<%016lx>] ra = [<%016lx>] ps = %04lx\n", regs->pc, regs->r26, regs->ps); printk("v0 = %016lx t0 = %016lx t1 = %016lx\n", regs->r0, regs->r1, regs->r2); printk("t2 = %016lx t3 = %016lx t4 = %016lx\n", regs->r3, regs->r4, regs->r5); printk("t5 = %016lx t6 = %016lx t7 = %016lx\n", regs->r6, regs->r7, regs->r8); if (r9_15) { printk("s0 = %016lx s1 = %016lx s2 = %016lx\n", r9_15[9], r9_15[10], r9_15[11]); printk("s3 = %016lx s4 = %016lx s5 = %016lx\n", r9_15[12], r9_15[13], r9_15[14]); printk("s6 = %016lx\n", r9_15[15]); } printk("a0 = %016lx a1 = %016lx a2 = %016lx\n", regs->r16, regs->r17, regs->r18); printk("a3 = %016lx a4 = %016lx a5 = %016lx\n", regs->r19, regs->r20, regs->r21); printk("t8 = %016lx t9 = %016lx t10= %016lx\n", regs->r22, regs->r23, regs->r24); printk("t11= %016lx pv = %016lx at = %016lx\n", regs->r25, regs->r27, regs->r28); printk("gp = %016lx sp = %p\n", regs->gp, regs+1);#if 0__halt();#endif}static char * ireg_name[] = {"v0", "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "s0", "s1", "s2", "s3", "s4", "s5", "s6", "a0", "a1", "a2", "a3", "a4", "a5", "t8", "t9", "t10", "t11", "ra", "pv", "at", "gp", "sp", "zero"};static char * inst_name[] = {"call_pal", "", "", "", "", "", "", "", "lda", "ldah", "ldbu", "ldq_u", "ldwu", "stw", "stb", "stq_u", "ALU", "ALU", "ALU", "ALU", "SQRT", "FVAX", "FIEEE", "FLOAT", "MISC", "PAL19", "JMP", "PAL1B", "GRAPH", "PAL1D", "PAL1E", "PAL1F", "ldf", "ldg", "lds", "ldt", "stf", "stg", "sts", "stt", "ldl", "ldq", "ldl_l", "ldq_l", "stl", "stq", "stl_c", "stq_c", "br", "fbeq", "fblt", "fble", "bsr", "fbne", "fbge", "fbgt" "blbc", "beq", "blt", "ble", "blbs", "bne", "bge", "bgt"};static char * jump_name[] = {"jmp", "jsr", "ret", "jsr_coroutine"};typedef struct {int func; char * text;} alist;static alist inta_name[] = {{0, "addl"}, {2, "s4addl"}, {9, "subl"}, {0xb, "s4subl"}, {0xf, "cmpbge"}, {0x12, "s8addl"}, {0x1b, "s8subl"}, {0x1d, "cmpult"}, {0x20, "addq"}, {0x22, "s4addq"}, {0x29, "subq"}, {0x2b, "s4subq"}, {0x2d, "cmpeq"}, {0x32, "s8addq"}, {0x3b, "s8subq"}, {0x3d, "cmpule"}, {0x40, "addl/v"}, {0x49, "subl/v"}, {0x4d, "cmplt"}, {0x60, "addq/v"}, {0x69, "subq/v"}, {0x6d, "cmple"}, {-1, 0}};static alist intl_name[] = {{0, "and"}, {8, "andnot"}, {0x14, "cmovlbs"}, {0x16, "cmovlbc"}, {0x20, "or"}, {0x24, "cmoveq"}, {0x26, "cmovne"}, {0x28, "ornot"}, {0x40, "xor"}, {0x44, "cmovlt"}, {0x46, "cmovge"}, {0x48, "eqv"}, {0x61, "amask"}, {0x64, "cmovle"}, {0x66, "cmovgt"}, {0x6c, "implver"}, {-1, 0}};static alist ints_name[] = {{2, "mskbl"}, {6, "extbl"}, {0xb, "insbl"}, {0x12, "mskwl"}, {0x16, "extwl"}, {0x1b, "inswl"}, {0x22, "mskll"}, {0x26, "extll"}, {0x2b, "insll"}, {0x30, "zap"}, {0x31, "zapnot"}, {0x32, "mskql"}, {0x34, "srl"}, {0x36, "extql"}, {0x39, "sll"}, {0x3b, "insql"}, {0x3c, "sra"}, {0x52, "mskwh"}, {0x57, "inswh"}, {0x5a, "extwh"}, {0x62, "msklh"}, {0x67, "inslh"}, {0x6a, "extlh"}, {0x72, "mskqh"}, {0x77, "insqh"}, {0x7a, "extqh"}, {-1, 0}};static alist intm_name[] = {{0, "mull"}, {0x20, "mulq"}, {0x30, "umulh"}, {0x40, "mull/v"}, {0x60, "mulq/v"}, {-1, 0}};static alist * int_name[] = {inta_name, intl_name, ints_name, intm_name};static char *assoc(int fcode, alist * a){ while ((fcode != a->func) && (a->func != -1)) ++a; return a->text;}static char *iname(unsigned int instr){ int opcode = instr >> 26; char * name = inst_name[opcode]; switch (opcode) { default: break; case 0x10: case 0x11: case 0x12: case 0x13: { char * specific_name = assoc((instr >> 5) & 0x3f, int_name[opcode - 0x10]); if (specific_name) name = specific_name; break; } case 0x1a: name = jump_name[(instr >> 14) & 3]; break; } return name;}static enum {NOT_INST, PAL, BRANCH, MEMORY, JUMP, OPERATE, FOPERATE, MISC}iformat(int opcode){ if (opcode >= 0x30) return BRANCH; if (opcode >= 0x20) return MEMORY; if (opcode == 0) return PAL; if (opcode < 8) return NOT_INST; if (opcode < 0x10) return MEMORY; if (opcode < 0x14) return OPERATE; if (opcode < 0x18) return FOPERATE; switch (opcode) { case 0x18: return MISC; case 0x1A: return JUMP; case 0x1C: return OPERATE; default: return NOT_INST; }}/* * The purpose here is to provide useful clues about a kernel crash, so * less likely instructions, e.g. floating point, aren't fully decoded. */static voiddisassemble(unsigned int instr){ int optype = instr >> 26; char buf[40], *s = buf; s += sprintf(buf, "%08x %s ", instr, iname(instr)); switch (iformat(optype)) { default: case NOT_INST: case MISC: break; case PAL: s += sprintf(s, "%d", instr); break; case BRANCH: { int reg = (instr >> 21) & 0x1f; int offset = instr & 0x1fffff; if (offset >= 0x100000) offset -= 0x200000; if (((optype & 3) == 0) || (optype >= 0x38)) { if ((optype != 0x30) || (reg != 0x1f)) s += sprintf(s, "%s,", ireg_name[reg]); } else s += sprintf(s, "f%d,", reg); s += sprintf(s, ".%+d", (offset + 1) << 2); break; } case MEMORY: { int addr_reg = (instr >> 16) & 0x1f; int value_reg = (instr >> 21) & 0x1f; int offset = instr & 0xffff; if (offset >= 0x8000) offset -= 0x10000; if ((optype >= 0x20) && (optype < 0x28)) s += sprintf(s, "f%d", value_reg); else s += sprintf(s, "%s", ireg_name[value_reg]); s += sprintf(s, ",%d(%s)", offset, ireg_name[addr_reg]); break; } case JUMP: { int target_reg = (instr >> 16) & 0x1f; int return_reg = (instr >> 21) & 0x1f; s += sprintf(s, "%s,", ireg_name[return_reg]); s += sprintf(s, "(%s)", ireg_name[target_reg]); break; } case OPERATE: { int areg = (instr >> 21) & 0x1f; int breg = (instr >> 16) & 0x1f; int creg = instr & 0x1f; int litflag = instr & (1<<12); int lit = (instr >> 13) & 0xff; s += sprintf(s, "%s,", ireg_name[areg]); if (litflag) s += sprintf(s, "%d", lit); else s += sprintf(s, "%s", ireg_name[breg]); s += sprintf(s, ",%s", ireg_name[creg]); break; } case FOPERATE: { int areg = (instr >> 21) & 0x1f; int breg = (instr >> 16) & 0x1f; int creg = instr & 0x1f; s += sprintf(s, "f%d,f%d,f%d", areg, breg, creg); break; } } buf[s-buf] = 0; printk("%s\n", buf);}static voiddik_show_code(unsigned int *pc){ long i; printk("Code:"); for (i = -6; i < 2; i++) { unsigned int insn; if (__get_user(insn, pc+i)) break; printk("%c", i ? ' ' : '*'); disassemble(insn); } printk("\n");}static voiddik_show_trace(unsigned long *sp){ long i = 0; printk("Trace:"); while (0x1ff8 & (unsigned long) sp) { extern unsigned long _stext, _etext; unsigned long tmp = *sp; sp++; if (tmp < (unsigned long) &_stext) continue; if (tmp >= (unsigned long) &_etext) continue; /* * Assume that only the low 24-bits of a kernel text address * is interesting. */ printk("%6x%c", (int)tmp & 0xffffff, (++i % 11) ? ' ' : '\n'); if (i > 40) { printk(" ..."); break; } } printk("\n");}voiddie_if_kernel(char * str, struct pt_regs *regs, long err, unsigned long *r9_15){ if (regs->ps & 8) return;#ifdef CONFIG_SMP printk("CPU %d ", hard_smp_processor_id());#endif printk("%s(%d): %s %ld\n", current->comm, current->pid, str, err); dik_show_regs(regs, r9_15); dik_show_code((unsigned int *)regs->pc); dik_show_trace((unsigned long *)(regs+1)); if (current->thread.flags & (1UL << 63)) { printk("die_if_kernel recursion detected.\n"); sti(); while (1); } current->thread.flags |= (1UL << 63); do_exit(SIGSEGV);}#ifndef CONFIG_MATHEMUstatic long dummy_emul(void) { return 0; }long (*alpha_fp_emul_imprecise)(struct pt_regs *regs, unsigned long writemask) = (void *)dummy_emul;long (*alpha_fp_emul) (unsigned long pc) = (void *)dummy_emul;#elselong alpha_fp_emul_imprecise(struct pt_regs *regs, unsigned long writemask);long alpha_fp_emul (unsigned long pc);#endifasmlinkage voiddo_entArith(unsigned long summary, unsigned long write_mask, unsigned long a2, unsigned long a3, unsigned long a4, unsigned long a5, struct pt_regs regs){ if (summary & 1) { /* Software-completion summary bit is set, so try to emulate the instruction. */ if (!amask(AMASK_PRECISE_TRAP)) { /* 21264 (except pass 1) has precise exceptions. */ if (alpha_fp_emul(regs.pc - 4)) return; } else { if (alpha_fp_emul_imprecise(®s, write_mask)) return; } }#if 0 printk("%s: arithmetic trap at %016lx: %02lx %016lx\n", current->comm, regs.pc, summary, write_mask);#endif die_if_kernel("Arithmetic fault", ®s, 0, 0); send_sig(SIGFPE, current, 1);}asmlinkage voiddo_entIF(unsigned long type, unsigned long a1, unsigned long a2, unsigned long a3, unsigned long a4, unsigned long a5, struct pt_regs regs){ die_if_kernel((type == 1 ? "Kernel Bug" : "Instruction fault"), ®s, type, 0); switch (type) { case 0: /* breakpoint */ if (ptrace_cancel_bpt(current)) { regs.pc -= 4; /* make pc point to former bpt */ } send_sig(SIGTRAP, current, 1); break; case 1: /* bugcheck */ send_sig(SIGTRAP, current, 1); break; case 2: /* gentrap */ /* * The exception code should be passed on to the signal * handler as the second argument. Linux doesn't do that * yet (also notice that Linux *always* behaves like * DEC Unix with SA_SIGINFO off; see DEC Unix man page * for sigaction(2)). */ switch ((long) regs.r16) { case GEN_INTOVF: case GEN_INTDIV: case GEN_FLTOVF: case GEN_FLTDIV: case GEN_FLTUND: case GEN_FLTINV: case GEN_FLTINE: case GEN_ROPRAND: send_sig(SIGFPE, current, 1); break; case GEN_DECOVF: case GEN_DECDIV: case GEN_DECINV: case GEN_ASSERTERR: case GEN_NULPTRERR: case GEN_STKOVF: case GEN_STRLENERR: case GEN_SUBSTRERR: case GEN_RANGERR: case GEN_SUBRNG: case GEN_SUBRNG1: case GEN_SUBRNG2: case GEN_SUBRNG3: case GEN_SUBRNG4: case GEN_SUBRNG5: case GEN_SUBRNG6: case GEN_SUBRNG7: send_sig(SIGTRAP, current, 1); break; } break; case 3: /* FEN fault */ send_sig(SIGILL, current, 1); break; case 4: /* opDEC */ if (implver() == IMPLVER_EV4) { /* EV4 does not implement anything except normal rounding. Everything else will come here as an illegal instruction. Emulate them. */ if (alpha_fp_emul(regs.pc-4)) return; } send_sig(SIGILL, current, 1); break; default: panic("do_entIF: unexpected instruction-fault type"); }}/* There is an ifdef in the PALcode in MILO that enables a "kernel debugging entry point" as an unprivilaged call_pal. We don't want to have anything to do with it, but unfortunately several versions of MILO included in distributions have it enabled, and if we don't put something on the entry point we'll oops. */asmlinkage voiddo_entDbg(unsigned long type, unsigned long a1, unsigned long a2, unsigned long a3, unsigned long a4, unsigned long a5, struct pt_regs regs){ die_if_kernel("Instruction fault", ®s, type, 0); force_sig(SIGILL, current);}/* * entUna has a different register layout to be reasonably simple. It * needs access to all the integer registers (the kernel doesn't use * fp-regs), and it needs to have them in order for simpler access. * * Due to the non-standard register layout (and because we don't want * to handle floating-point regs), user-mode unaligned accesses are * handled separately by do_entUnaUser below. * * Oh, btw, we don't handle the "gp" register correctly, but if we fault * on a gp-register unaligned load/store, something is _very_ wrong * in the kernel anyway.. */struct allregs { unsigned long regs[32]; unsigned long ps, pc, gp, a0, a1, a2;};struct unaligned_stat { unsigned long count, va, pc;} unaligned[2];/* Macro for exception fixup code to access integer registers. */#define una_reg(r) (regs.regs[(r) >= 16 && (r) <= 18 ? (r)+19 : (r)])asmlinkage voiddo_entUna(void * va, unsigned long opcode, unsigned long reg, unsigned long a3, unsigned long a4, unsigned long a5, struct allregs regs){ long error, tmp1, tmp2, tmp3, tmp4; unsigned long pc = regs.pc - 4; unsigned fixup; unaligned[0].count++; unaligned[0].va = (unsigned long) va; unaligned[0].pc = pc; /* We don't want to use the generic get/put unaligned macros as we want to trap exceptions. Only if we actually get an exception will we decide whether we should have caught it. */ switch (opcode) { case 0x0c: /* ldwu */ __asm__ __volatile__( "1: ldq_u %1,0(%3)\n" "2: ldq_u %2,1(%3)\n" " extwl %1,%3,%1\n" " extwh %2,%3,%2\n" "3:\n" ".section __ex_table,\"a\"\n" " .gprel32 1b\n" " lda %1,3b-1b(%0)\n" " .gprel32 2b\n" " lda %2,3b-2b(%0)\n" ".previous" : "=r"(error), "=&r"(tmp1), "=&r"(tmp2) : "r"(va), "0"(0)); if (error) goto got_exception; una_reg(reg) = tmp1|tmp2; return; case 0x28: /* ldl */ __asm__ __volatile__( "1: ldq_u %1,0(%3)\n" "2: ldq_u %2,3(%3)\n" " extll %1,%3,%1\n" " extlh %2,%3,%2\n" "3:\n" ".section __ex_table,\"a\"\n" " .gprel32 1b\n" " lda %1,3b-1b(%0)\n" " .gprel32 2b\n" " lda %2,3b-2b(%0)\n" ".previous" : "=r"(error), "=&r"(tmp1), "=&r"(tmp2) : "r"(va), "0"(0)); if (error) goto got_exception; una_reg(reg) = (int)(tmp1|tmp2); return; case 0x29: /* ldq */ __asm__ __volatile__( "1: ldq_u %1,0(%3)\n" "2: ldq_u %2,7(%3)\n" " extql %1,%3,%1\n" " extqh %2,%3,%2\n" "3:\n" ".section __ex_table,\"a\"\n" " .gprel32 1b\n" " lda %1,3b-1b(%0)\n" " .gprel32 2b\n" " lda %2,3b-2b(%0)\n" ".previous" : "=r"(error), "=&r"(tmp1), "=&r"(tmp2) : "r"(va), "0"(0)); if (error) goto got_exception; una_reg(reg) = tmp1|tmp2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -