Skip to content

Commit 5bc46ce

Browse files
committed
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: Guo Ren <[email protected]> Cc: Arnd Bergmann <[email protected]>
1 parent 71e193d commit 5bc46ce

File tree

5 files changed

+157
-109
lines changed

5 files changed

+157
-109
lines changed

arch/csky/include/asm/bug.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ do { \
2020

2121
struct pt_regs;
2222

23-
void die_if_kernel(char *str, struct pt_regs *regs, int nr);
23+
void die(struct pt_regs *regs, const char *str);
2424
void show_regs(struct pt_regs *regs);
25+
void show_code(struct pt_regs *regs);
2526

2627
#endif /* __ASM_CSKY_BUG_H */

arch/csky/include/asm/ptrace.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#define user_mode(regs) (!((regs)->sr & PS_S))
2525
#define instruction_pointer(regs) ((regs)->pc)
2626
#define profile_pc(regs) instruction_pointer(regs)
27+
#define trap_no(regs) ((regs->sr >> 16) & 0xff)
2728

2829
static inline void instruction_pointer_set(struct pt_regs *regs,
2930
unsigned long val)

arch/csky/kernel/ptrace.c

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -347,13 +347,8 @@ asmlinkage void syscall_trace_exit(struct pt_regs *regs)
347347
trace_sys_exit(regs, syscall_get_return_value(current, regs));
348348
}
349349

350-
extern void show_stack(struct task_struct *task, unsigned long *stack, const char *loglvl);
351350
void show_regs(struct pt_regs *fp)
352351
{
353-
unsigned long *sp;
354-
unsigned char *tp;
355-
int i;
356-
357352
pr_info("\nCURRENT PROCESS:\n\n");
358353
pr_info("COMM=%s PID=%d\n", current->comm, current->pid);
359354

@@ -400,29 +395,9 @@ void show_regs(struct pt_regs *fp)
400395
fp->regs[0], fp->regs[1], fp->regs[2], fp->regs[3]);
401396
pr_info("r10: 0x%08lx r11: 0x%08lx r12: 0x%08lx r13: 0x%08lx\n",
402397
fp->regs[4], fp->regs[5], fp->regs[6], fp->regs[7]);
403-
pr_info("r14: 0x%08lx r1: 0x%08lx r15: 0x%08lx\n",
404-
fp->regs[8], fp->regs[9], fp->lr);
398+
pr_info("r14: 0x%08lx r1: 0x%08lx\n",
399+
fp->regs[8], fp->regs[9]);
405400
#endif
406401

407-
pr_info("\nCODE:");
408-
tp = ((unsigned char *) fp->pc) - 0x20;
409-
tp += ((int)tp % 4) ? 2 : 0;
410-
for (sp = (unsigned long *) tp, i = 0; (i < 0x40); i += 4) {
411-
if ((i % 0x10) == 0)
412-
pr_cont("\n%08x: ", (int) (tp + i));
413-
pr_cont("%08x ", (int) *sp++);
414-
}
415-
pr_cont("\n");
416-
417-
pr_info("\nKERNEL STACK:");
418-
tp = ((unsigned char *) fp) - 0x40;
419-
for (sp = (unsigned long *) tp, i = 0; (i < 0xc0); i += 4) {
420-
if ((i % 0x10) == 0)
421-
pr_cont("\n%08x: ", (int) (tp + i));
422-
pr_cont("%08x ", (int) *sp++);
423-
}
424-
pr_cont("\n");
425-
426-
show_stack(NULL, (unsigned long *)fp->regs[4], KERN_INFO);
427402
return;
428403
}

arch/csky/kernel/traps.c

Lines changed: 147 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <linux/rtc.h>
1616
#include <linux/uaccess.h>
1717
#include <linux/kprobes.h>
18+
#include <linux/kdebug.h>
19+
#include <linux/sched/debug.h>
1820

1921
#include <asm/setup.h>
2022
#include <asm/traps.h>
@@ -27,6 +29,8 @@
2729
#include <abi/fpu.h>
2830
#endif
2931

32+
int show_unhandled_signals = 1;
33+
3034
/* Defined in entry.S */
3135
asmlinkage void csky_trap(void);
3236

@@ -77,117 +81,184 @@ void __init trap_init(void)
7781
#endif
7882
}
7983

80-
void die_if_kernel(char *str, struct pt_regs *regs, int nr)
84+
static DEFINE_SPINLOCK(die_lock);
85+
86+
void die(struct pt_regs *regs, const char *str)
8187
{
82-
if (user_mode(regs))
83-
return;
88+
static int die_counter;
89+
int ret;
8490

91+
oops_enter();
92+
93+
spin_lock_irq(&die_lock);
8594
console_verbose();
86-
pr_err("%s: %08x\n", str, nr);
95+
bust_spinlocks(1);
96+
97+
pr_emerg("%s [#%d]\n", str, ++die_counter);
98+
print_modules();
8799
show_regs(regs);
100+
show_stack(current, (unsigned long *)regs->regs[4], KERN_INFO);
101+
102+
ret = notify_die(DIE_OOPS, str, regs, 0, trap_no(regs), SIGSEGV);
103+
104+
bust_spinlocks(0);
88105
add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
89-
do_exit(SIGSEGV);
106+
spin_unlock_irq(&die_lock);
107+
oops_exit();
108+
109+
if (in_interrupt())
110+
panic("Fatal exception in interrupt");
111+
if (panic_on_oops)
112+
panic("Fatal exception");
113+
if (ret != NOTIFY_STOP)
114+
do_exit(SIGSEGV);
90115
}
91116

92-
void buserr(struct pt_regs *regs)
117+
void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr)
93118
{
94-
#ifdef CONFIG_CPU_CK810
95-
static unsigned long prev_pc;
119+
struct task_struct *tsk = current;
96120

97-
if ((regs->pc == prev_pc) && prev_pc != 0) {
98-
prev_pc = 0;
99-
} else {
100-
prev_pc = regs->pc;
101-
return;
121+
if (show_unhandled_signals && unhandled_signal(tsk, signo)
122+
&& printk_ratelimit()) {
123+
pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x%08lx",
124+
tsk->comm, task_pid_nr(tsk), signo, code, addr);
125+
print_vma_addr(KERN_CONT " in ", instruction_pointer(regs));
126+
pr_cont("\n");
127+
show_regs(regs);
102128
}
103-
#endif
104129

105-
die_if_kernel("Kernel mode BUS error", regs, 0);
130+
force_sig_fault(signo, code, (void __user *)addr);
131+
}
106132

107-
pr_err("User mode Bus Error\n");
108-
show_regs(regs);
133+
static void do_trap_error(struct pt_regs *regs, int signo, int code,
134+
unsigned long addr, const char *str)
135+
{
136+
current->thread.trap_no = trap_no(regs);
109137

110-
force_sig_fault(SIGSEGV, 0, (void __user *)regs->pc);
138+
if (user_mode(regs)) {
139+
do_trap(regs, signo, code, addr);
140+
} else {
141+
if (!fixup_exception(regs))
142+
die(regs, str);
143+
}
111144
}
112145

113-
asmlinkage void trap_c(struct pt_regs *regs)
114-
{
115-
int sig;
116-
unsigned long vector;
117-
siginfo_t info;
118-
struct task_struct *tsk = current;
146+
#define DO_ERROR_INFO(name, signo, code, str) \
147+
asmlinkage __visible void name(struct pt_regs *regs) \
148+
{ \
149+
do_trap_error(regs, signo, code, regs->pc, "Oops - " str); \
150+
}
119151

120-
vector = (regs->sr >> 16) & 0xff;
152+
DO_ERROR_INFO(do_trap_unknown,
153+
SIGILL, ILL_ILLTRP, "unknown exception");
154+
DO_ERROR_INFO(do_trap_zdiv,
155+
SIGFPE, FPE_INTDIV, "error zero div exception");
156+
DO_ERROR_INFO(do_trap_buserr,
157+
SIGSEGV, ILL_ILLADR, "error bus error exception");
121158

122-
switch (vector) {
123-
case VEC_ZERODIV:
124-
die_if_kernel("Kernel mode ZERO DIV", regs, vector);
125-
sig = SIGFPE;
126-
break;
127-
/* ptrace */
128-
case VEC_TRACE:
159+
asmlinkage void do_trap_misaligned(struct pt_regs *regs)
160+
{
161+
#ifdef CONFIG_CPU_NEED_SOFTALIGN
162+
csky_alignment(regs);
163+
#else
164+
current->thread.trap_no = trap_no(regs);
165+
do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->pc,
166+
"Oops - load/store address misaligned");
167+
#endif
168+
}
169+
170+
asmlinkage void do_trap_bkpt(struct pt_regs *regs)
171+
{
129172
#ifdef CONFIG_KPROBES
130-
if (kprobe_single_step_handler(regs))
131-
return;
173+
if (kprobe_single_step_handler(regs))
174+
return;
132175
#endif
133176
#ifdef CONFIG_UPROBES
134-
if (uprobe_single_step_handler(regs))
135-
return;
177+
if (uprobe_single_step_handler(regs))
178+
return;
136179
#endif
137-
info.si_code = TRAP_TRACE;
138-
sig = SIGTRAP;
139-
break;
140-
case VEC_ILLEGAL:
141-
tsk->thread.trap_no = vector;
180+
if (user_mode(regs)) {
181+
send_sig(SIGTRAP, current, 0);
182+
return;
183+
}
184+
185+
do_trap_error(regs, SIGILL, ILL_ILLTRP, regs->pc,
186+
"Oops - illegal trap exception");
187+
}
188+
189+
asmlinkage void do_trap_illinsn(struct pt_regs *regs)
190+
{
191+
current->thread.trap_no = trap_no(regs);
192+
142193
#ifdef CONFIG_KPROBES
143-
if (kprobe_breakpoint_handler(regs))
144-
return;
194+
if (kprobe_breakpoint_handler(regs))
195+
return;
145196
#endif
146197
#ifdef CONFIG_UPROBES
147-
if (uprobe_breakpoint_handler(regs))
148-
return;
198+
if (uprobe_breakpoint_handler(regs))
199+
return;
149200
#endif
150-
die_if_kernel("Kernel mode ILLEGAL", regs, vector);
151201
#ifndef CONFIG_CPU_NO_USER_BKPT
152-
if (*(uint16_t *)instruction_pointer(regs) != USR_BKPT)
202+
if (*(uint16_t *)instruction_pointer(regs) != USR_BKPT) {
203+
send_sig(SIGTRAP, current, 0);
204+
return;
205+
}
153206
#endif
154-
{
155-
sig = SIGILL;
156-
break;
157-
}
158-
/* gdbserver breakpoint */
207+
208+
do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->pc,
209+
"Oops - illegal instruction exception");
210+
}
211+
212+
asmlinkage void do_trap_fpe(struct pt_regs *regs)
213+
{
214+
#ifdef CONFIG_CPU_HAS_FP
215+
return fpu_fpe(regs);
216+
#else
217+
do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->pc,
218+
"Oops - fpu instruction exception");
219+
#endif
220+
}
221+
222+
asmlinkage void do_trap_priv(struct pt_regs *regs)
223+
{
224+
#ifdef CONFIG_CPU_HAS_FP
225+
if (user_mode(regs) && fpu_libc_helper(regs))
226+
return;
227+
#endif
228+
do_trap_error(regs, SIGILL, ILL_PRVOPC, regs->pc,
229+
"Oops - illegal privileged exception");
230+
}
231+
232+
asmlinkage void trap_c(struct pt_regs *regs)
233+
{
234+
switch (trap_no(regs)) {
235+
case VEC_ZERODIV:
236+
do_trap_zdiv(regs);
237+
break;
238+
case VEC_TRACE:
239+
do_trap_bkpt(regs);
240+
break;
241+
case VEC_ILLEGAL:
242+
do_trap_illinsn(regs);
243+
break;
159244
case VEC_TRAP1:
160-
/* jtagserver breakpoint */
161245
case VEC_BREAKPOINT:
162-
die_if_kernel("Kernel mode BKPT", regs, vector);
163-
info.si_code = TRAP_BRKPT;
164-
sig = SIGTRAP;
246+
do_trap_bkpt(regs);
165247
break;
166248
case VEC_ACCESS:
167-
tsk->thread.trap_no = vector;
168-
return buserr(regs);
169-
#ifdef CONFIG_CPU_NEED_SOFTALIGN
249+
do_trap_buserr(regs);
250+
break;
170251
case VEC_ALIGN:
171-
tsk->thread.trap_no = vector;
172-
return csky_alignment(regs);
173-
#endif
174-
#ifdef CONFIG_CPU_HAS_FPU
252+
do_trap_misaligned(regs);
253+
break;
175254
case VEC_FPE:
176-
tsk->thread.trap_no = vector;
177-
die_if_kernel("Kernel mode FPE", regs, vector);
178-
return fpu_fpe(regs);
255+
do_trap_fpe(regs);
256+
break;
179257
case VEC_PRIV:
180-
tsk->thread.trap_no = vector;
181-
die_if_kernel("Kernel mode PRIV", regs, vector);
182-
if (fpu_libc_helper(regs))
183-
return;
184-
#endif
258+
do_trap_priv(regs);
259+
break;
185260
default:
186-
sig = SIGSEGV;
261+
do_trap_unknown(regs);
187262
break;
188263
}
189-
190-
tsk->thread.trap_no = vector;
191-
192-
send_sig(sig, current, 0);
193264
}

arch/csky/mm/fault.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,13 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write,
183183
bad_area_nosemaphore:
184184
/* User mode accesses just cause a SIGSEGV */
185185
if (user_mode(regs)) {
186-
tsk->thread.trap_no = (regs->sr >> 16) & 0xff;
186+
tsk->thread.trap_no = trap_no(regs);
187187
force_sig_fault(SIGSEGV, si_code, (void __user *)address);
188188
return;
189189
}
190190

191191
no_context:
192-
tsk->thread.trap_no = (regs->sr >> 16) & 0xff;
192+
tsk->thread.trap_no = trap_no(regs);
193193

194194
/* Are we prepared to handle this kernel fault? */
195195
if (fixup_exception(regs))
@@ -202,10 +202,10 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write,
202202
bust_spinlocks(1);
203203
pr_alert("Unable to handle kernel paging request at virtual "
204204
"address 0x%08lx, pc: 0x%08lx\n", address, regs->pc);
205-
die_if_kernel("Oops", regs, write);
205+
die(regs, "Oops");
206206

207207
out_of_memory:
208-
tsk->thread.trap_no = (regs->sr >> 16) & 0xff;
208+
tsk->thread.trap_no = trap_no(regs);
209209

210210
/*
211211
* 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,
215215
return;
216216

217217
do_sigbus:
218-
tsk->thread.trap_no = (regs->sr >> 16) & 0xff;
218+
tsk->thread.trap_no = trap_no(regs);
219219

220220
mmap_read_unlock(mm);
221221

0 commit comments

Comments
 (0)