Commit 5bc46ce2 authored by Guo Ren's avatar Guo Ren

csky: Optimize the trap processing flow

 - Seperate different trap functions
 - Add trap_no()
 - Remove panic code print
 - Redesign die_if_kerenl to die with riscv's
 - Print exact trap info for app segment fault

[   17.389321] gzip[126]: unhandled signal 11 code 0x3 at 0x0007835a in busybox[8000+d4000]
[   17.393882]
[   17.393882] CURRENT PROCESS:
[   17.393882]
[   17.394309] COMM=gzip PID=126
[   17.394513] TEXT=00008000-000db2e4 DATA=000dcf14-000dd1ad BSS=000dd1ad-000ff000
[   17.395499] USER-STACK=7f888e50  KERNEL-STACK=bf130300
[   17.395499]
[   17.396801] PC: 0x0007835a (0x7835a)
[   17.397048] LR: 0x000058b4 (0x58b4)
[   17.397285] SP: 0xbe519f68
[   17.397555] orig_a0: 0x00002852
[   17.397886] PSR: 0x00020341
[   17.398356]  a0: 0x00002852   a1: 0x000f2f5a   a2: 0x0000d7ae   a3: 0x0000005d
[   17.399289]  r4: 0x000de150   r5: 0x00000002   r6: 0x00000102   r7: 0x00007efa
[   17.399800]  r8: 0x7f888bc4   r9: 0x00000001  r10: 0x000002eb  r11: 0x0000aac1
[   17.400166] r12: 0x00002ef2  r13: 0x00000007  r15: 0x000058b4
[   17.400531] r16: 0x0000004c  r17: 0x00000031  r18: 0x000f5816  r19: 0x000e8068
[   17.401006] r20: 0x000f5818  r21: 0x000e8068  r22: 0x000f5918  r23: 0x90000000
[   17.401721] r24: 0x00000031  r25: 0x000000c8  r26: 0x00000000  r27: 0x00000000
[   17.402199] r28: 0x2ac2a000  r29: 0x00000000  r30: 0x00000000  tls: 0x2aadbaa8
[   17.402686]  hi: 0x00120340   lo: 0x7f888bec
/etc/init.ci/ntfs3g_run: line 61:   126 Segmentation fault      gzip -c -9 /mnt/test.bin > /mnt/test_bin.gz
Signed-off-by: default avatarGuo Ren <guoren@linux.alibaba.com>
Cc: Arnd Bergmann <arnd@arndb.de>
parent 71e193d7
...@@ -20,7 +20,8 @@ do { \ ...@@ -20,7 +20,8 @@ do { \
struct pt_regs; struct pt_regs;
void die_if_kernel(char *str, struct pt_regs *regs, int nr); void die(struct pt_regs *regs, const char *str);
void show_regs(struct pt_regs *regs); void show_regs(struct pt_regs *regs);
void show_code(struct pt_regs *regs);
#endif /* __ASM_CSKY_BUG_H */ #endif /* __ASM_CSKY_BUG_H */
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#define user_mode(regs) (!((regs)->sr & PS_S)) #define user_mode(regs) (!((regs)->sr & PS_S))
#define instruction_pointer(regs) ((regs)->pc) #define instruction_pointer(regs) ((regs)->pc)
#define profile_pc(regs) instruction_pointer(regs) #define profile_pc(regs) instruction_pointer(regs)
#define trap_no(regs) ((regs->sr >> 16) & 0xff)
static inline void instruction_pointer_set(struct pt_regs *regs, static inline void instruction_pointer_set(struct pt_regs *regs,
unsigned long val) unsigned long val)
......
...@@ -347,13 +347,8 @@ asmlinkage void syscall_trace_exit(struct pt_regs *regs) ...@@ -347,13 +347,8 @@ asmlinkage void syscall_trace_exit(struct pt_regs *regs)
trace_sys_exit(regs, syscall_get_return_value(current, regs)); trace_sys_exit(regs, syscall_get_return_value(current, regs));
} }
extern void show_stack(struct task_struct *task, unsigned long *stack, const char *loglvl);
void show_regs(struct pt_regs *fp) void show_regs(struct pt_regs *fp)
{ {
unsigned long *sp;
unsigned char *tp;
int i;
pr_info("\nCURRENT PROCESS:\n\n"); pr_info("\nCURRENT PROCESS:\n\n");
pr_info("COMM=%s PID=%d\n", current->comm, current->pid); pr_info("COMM=%s PID=%d\n", current->comm, current->pid);
...@@ -400,29 +395,9 @@ void show_regs(struct pt_regs *fp) ...@@ -400,29 +395,9 @@ void show_regs(struct pt_regs *fp)
fp->regs[0], fp->regs[1], fp->regs[2], fp->regs[3]); fp->regs[0], fp->regs[1], fp->regs[2], fp->regs[3]);
pr_info("r10: 0x%08lx r11: 0x%08lx r12: 0x%08lx r13: 0x%08lx\n", pr_info("r10: 0x%08lx r11: 0x%08lx r12: 0x%08lx r13: 0x%08lx\n",
fp->regs[4], fp->regs[5], fp->regs[6], fp->regs[7]); fp->regs[4], fp->regs[5], fp->regs[6], fp->regs[7]);
pr_info("r14: 0x%08lx r1: 0x%08lx r15: 0x%08lx\n", pr_info("r14: 0x%08lx r1: 0x%08lx\n",
fp->regs[8], fp->regs[9], fp->lr); fp->regs[8], fp->regs[9]);
#endif #endif
pr_info("\nCODE:");
tp = ((unsigned char *) fp->pc) - 0x20;
tp += ((int)tp % 4) ? 2 : 0;
for (sp = (unsigned long *) tp, i = 0; (i < 0x40); i += 4) {
if ((i % 0x10) == 0)
pr_cont("\n%08x: ", (int) (tp + i));
pr_cont("%08x ", (int) *sp++);
}
pr_cont("\n");
pr_info("\nKERNEL STACK:");
tp = ((unsigned char *) fp) - 0x40;
for (sp = (unsigned long *) tp, i = 0; (i < 0xc0); i += 4) {
if ((i % 0x10) == 0)
pr_cont("\n%08x: ", (int) (tp + i));
pr_cont("%08x ", (int) *sp++);
}
pr_cont("\n");
show_stack(NULL, (unsigned long *)fp->regs[4], KERN_INFO);
return; return;
} }
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
#include <linux/rtc.h> #include <linux/rtc.h>
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include <linux/kprobes.h> #include <linux/kprobes.h>
#include <linux/kdebug.h>
#include <linux/sched/debug.h>
#include <asm/setup.h> #include <asm/setup.h>
#include <asm/traps.h> #include <asm/traps.h>
...@@ -27,6 +29,8 @@ ...@@ -27,6 +29,8 @@
#include <abi/fpu.h> #include <abi/fpu.h>
#endif #endif
int show_unhandled_signals = 1;
/* Defined in entry.S */ /* Defined in entry.S */
asmlinkage void csky_trap(void); asmlinkage void csky_trap(void);
...@@ -77,117 +81,184 @@ void __init trap_init(void) ...@@ -77,117 +81,184 @@ void __init trap_init(void)
#endif #endif
} }
void die_if_kernel(char *str, struct pt_regs *regs, int nr) static DEFINE_SPINLOCK(die_lock);
void die(struct pt_regs *regs, const char *str)
{ {
if (user_mode(regs)) static int die_counter;
return; int ret;
oops_enter();
spin_lock_irq(&die_lock);
console_verbose(); console_verbose();
pr_err("%s: %08x\n", str, nr); bust_spinlocks(1);
pr_emerg("%s [#%d]\n", str, ++die_counter);
print_modules();
show_regs(regs); show_regs(regs);
show_stack(current, (unsigned long *)regs->regs[4], KERN_INFO);
ret = notify_die(DIE_OOPS, str, regs, 0, trap_no(regs), SIGSEGV);
bust_spinlocks(0);
add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE); add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
do_exit(SIGSEGV); spin_unlock_irq(&die_lock);
oops_exit();
if (in_interrupt())
panic("Fatal exception in interrupt");
if (panic_on_oops)
panic("Fatal exception");
if (ret != NOTIFY_STOP)
do_exit(SIGSEGV);
} }
void buserr(struct pt_regs *regs) void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr)
{ {
#ifdef CONFIG_CPU_CK810 struct task_struct *tsk = current;
static unsigned long prev_pc;
if ((regs->pc == prev_pc) && prev_pc != 0) { if (show_unhandled_signals && unhandled_signal(tsk, signo)
prev_pc = 0; && printk_ratelimit()) {
} else { pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x%08lx",
prev_pc = regs->pc; tsk->comm, task_pid_nr(tsk), signo, code, addr);
return; print_vma_addr(KERN_CONT " in ", instruction_pointer(regs));
pr_cont("\n");
show_regs(regs);
} }
#endif
die_if_kernel("Kernel mode BUS error", regs, 0); force_sig_fault(signo, code, (void __user *)addr);
}
pr_err("User mode Bus Error\n"); static void do_trap_error(struct pt_regs *regs, int signo, int code,
show_regs(regs); unsigned long addr, const char *str)
{
current->thread.trap_no = trap_no(regs);
force_sig_fault(SIGSEGV, 0, (void __user *)regs->pc); if (user_mode(regs)) {
do_trap(regs, signo, code, addr);
} else {
if (!fixup_exception(regs))
die(regs, str);
}
} }
asmlinkage void trap_c(struct pt_regs *regs) #define DO_ERROR_INFO(name, signo, code, str) \
{ asmlinkage __visible void name(struct pt_regs *regs) \
int sig; { \
unsigned long vector; do_trap_error(regs, signo, code, regs->pc, "Oops - " str); \
siginfo_t info; }
struct task_struct *tsk = current;
vector = (regs->sr >> 16) & 0xff; DO_ERROR_INFO(do_trap_unknown,
SIGILL, ILL_ILLTRP, "unknown exception");
DO_ERROR_INFO(do_trap_zdiv,
SIGFPE, FPE_INTDIV, "error zero div exception");
DO_ERROR_INFO(do_trap_buserr,
SIGSEGV, ILL_ILLADR, "error bus error exception");
switch (vector) { asmlinkage void do_trap_misaligned(struct pt_regs *regs)
case VEC_ZERODIV: {
die_if_kernel("Kernel mode ZERO DIV", regs, vector); #ifdef CONFIG_CPU_NEED_SOFTALIGN
sig = SIGFPE; csky_alignment(regs);
break; #else
/* ptrace */ current->thread.trap_no = trap_no(regs);
case VEC_TRACE: do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->pc,
"Oops - load/store address misaligned");
#endif
}
asmlinkage void do_trap_bkpt(struct pt_regs *regs)
{
#ifdef CONFIG_KPROBES #ifdef CONFIG_KPROBES
if (kprobe_single_step_handler(regs)) if (kprobe_single_step_handler(regs))
return; return;
#endif #endif
#ifdef CONFIG_UPROBES #ifdef CONFIG_UPROBES
if (uprobe_single_step_handler(regs)) if (uprobe_single_step_handler(regs))
return; return;
#endif #endif
info.si_code = TRAP_TRACE; if (user_mode(regs)) {
sig = SIGTRAP; send_sig(SIGTRAP, current, 0);
break; return;
case VEC_ILLEGAL: }
tsk->thread.trap_no = vector;
do_trap_error(regs, SIGILL, ILL_ILLTRP, regs->pc,
"Oops - illegal trap exception");
}
asmlinkage void do_trap_illinsn(struct pt_regs *regs)
{
current->thread.trap_no = trap_no(regs);
#ifdef CONFIG_KPROBES #ifdef CONFIG_KPROBES
if (kprobe_breakpoint_handler(regs)) if (kprobe_breakpoint_handler(regs))
return; return;
#endif #endif
#ifdef CONFIG_UPROBES #ifdef CONFIG_UPROBES
if (uprobe_breakpoint_handler(regs)) if (uprobe_breakpoint_handler(regs))
return; return;
#endif #endif
die_if_kernel("Kernel mode ILLEGAL", regs, vector);
#ifndef CONFIG_CPU_NO_USER_BKPT #ifndef CONFIG_CPU_NO_USER_BKPT
if (*(uint16_t *)instruction_pointer(regs) != USR_BKPT) if (*(uint16_t *)instruction_pointer(regs) != USR_BKPT) {
send_sig(SIGTRAP, current, 0);
return;
}
#endif #endif
{
sig = SIGILL; do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->pc,
break; "Oops - illegal instruction exception");
} }
/* gdbserver breakpoint */
asmlinkage void do_trap_fpe(struct pt_regs *regs)
{
#ifdef CONFIG_CPU_HAS_FP
return fpu_fpe(regs);
#else
do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->pc,
"Oops - fpu instruction exception");
#endif
}
asmlinkage void do_trap_priv(struct pt_regs *regs)
{
#ifdef CONFIG_CPU_HAS_FP
if (user_mode(regs) && fpu_libc_helper(regs))
return;
#endif
do_trap_error(regs, SIGILL, ILL_PRVOPC, regs->pc,
"Oops - illegal privileged exception");
}
asmlinkage void trap_c(struct pt_regs *regs)
{
switch (trap_no(regs)) {
case VEC_ZERODIV:
do_trap_zdiv(regs);
break;
case VEC_TRACE:
do_trap_bkpt(regs);
break;
case VEC_ILLEGAL:
do_trap_illinsn(regs);
break;
case VEC_TRAP1: case VEC_TRAP1:
/* jtagserver breakpoint */
case VEC_BREAKPOINT: case VEC_BREAKPOINT:
die_if_kernel("Kernel mode BKPT", regs, vector); do_trap_bkpt(regs);
info.si_code = TRAP_BRKPT;
sig = SIGTRAP;
break; break;
case VEC_ACCESS: case VEC_ACCESS:
tsk->thread.trap_no = vector; do_trap_buserr(regs);
return buserr(regs); break;
#ifdef CONFIG_CPU_NEED_SOFTALIGN
case VEC_ALIGN: case VEC_ALIGN:
tsk->thread.trap_no = vector; do_trap_misaligned(regs);
return csky_alignment(regs); break;
#endif
#ifdef CONFIG_CPU_HAS_FPU
case VEC_FPE: case VEC_FPE:
tsk->thread.trap_no = vector; do_trap_fpe(regs);
die_if_kernel("Kernel mode FPE", regs, vector); break;
return fpu_fpe(regs);
case VEC_PRIV: case VEC_PRIV:
tsk->thread.trap_no = vector; do_trap_priv(regs);
die_if_kernel("Kernel mode PRIV", regs, vector); break;
if (fpu_libc_helper(regs))
return;
#endif
default: default:
sig = SIGSEGV; do_trap_unknown(regs);
break; break;
} }
tsk->thread.trap_no = vector;
send_sig(sig, current, 0);
} }
...@@ -183,13 +183,13 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write, ...@@ -183,13 +183,13 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write,
bad_area_nosemaphore: bad_area_nosemaphore:
/* User mode accesses just cause a SIGSEGV */ /* User mode accesses just cause a SIGSEGV */
if (user_mode(regs)) { if (user_mode(regs)) {
tsk->thread.trap_no = (regs->sr >> 16) & 0xff; tsk->thread.trap_no = trap_no(regs);
force_sig_fault(SIGSEGV, si_code, (void __user *)address); force_sig_fault(SIGSEGV, si_code, (void __user *)address);
return; return;
} }
no_context: no_context:
tsk->thread.trap_no = (regs->sr >> 16) & 0xff; tsk->thread.trap_no = trap_no(regs);
/* Are we prepared to handle this kernel fault? */ /* Are we prepared to handle this kernel fault? */
if (fixup_exception(regs)) if (fixup_exception(regs))
...@@ -202,10 +202,10 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write, ...@@ -202,10 +202,10 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write,
bust_spinlocks(1); bust_spinlocks(1);
pr_alert("Unable to handle kernel paging request at virtual " pr_alert("Unable to handle kernel paging request at virtual "
"address 0x%08lx, pc: 0x%08lx\n", address, regs->pc); "address 0x%08lx, pc: 0x%08lx\n", address, regs->pc);
die_if_kernel("Oops", regs, write); die(regs, "Oops");
out_of_memory: out_of_memory:
tsk->thread.trap_no = (regs->sr >> 16) & 0xff; tsk->thread.trap_no = trap_no(regs);
/* /*
* We ran out of memory, call the OOM killer, and return the userspace * We ran out of memory, call the OOM killer, and return the userspace
...@@ -215,7 +215,7 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write, ...@@ -215,7 +215,7 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write,
return; return;
do_sigbus: do_sigbus:
tsk->thread.trap_no = (regs->sr >> 16) & 0xff; tsk->thread.trap_no = trap_no(regs);
mmap_read_unlock(mm); mmap_read_unlock(mm);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment