1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
4 #include <linux/sched.h>
5 #include <linux/signal.h>
6 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/user.h>
10 #include <linux/string.h>
11 #include <linux/linkage.h>
12 #include <linux/init.h>
13 #include <linux/ptrace.h>
14 #include <linux/kallsyms.h>
15 #include <linux/rtc.h>
16 #include <linux/uaccess.h>
17 #include <linux/kprobes.h>
18 #include <linux/kdebug.h>
19 #include <linux/sched/debug.h>
21 #include <asm/setup.h>
22 #include <asm/traps.h>
23 #include <asm/pgalloc.h>
24 #include <asm/siginfo.h>
26 #include <asm/mmu_context.h>
28 #ifdef CONFIG_CPU_HAS_FPU
32 int show_unhandled_signals
= 1;
34 /* Defined in entry.S */
35 asmlinkage
void csky_trap(void);
37 asmlinkage
void csky_systemcall(void);
38 asmlinkage
void csky_cmpxchg(void);
39 asmlinkage
void csky_get_tls(void);
40 asmlinkage
void csky_irq(void);
42 asmlinkage
void csky_tlbinvalidl(void);
43 asmlinkage
void csky_tlbinvalids(void);
44 asmlinkage
void csky_tlbmodified(void);
46 /* Defined in head.S */
47 asmlinkage
void _start_smp_secondary(void);
49 void __init
pre_trap_init(void)
53 mtcr("vbr", vec_base
);
55 for (i
= 1; i
< 128; i
++)
56 VEC_INIT(i
, csky_trap
);
59 void __init
trap_init(void)
61 VEC_INIT(VEC_AUTOVEC
, csky_irq
);
63 /* setup trap0 trap2 trap3 */
64 VEC_INIT(VEC_TRAP0
, csky_systemcall
);
65 VEC_INIT(VEC_TRAP2
, csky_cmpxchg
);
66 VEC_INIT(VEC_TRAP3
, csky_get_tls
);
68 /* setup MMU TLB exception */
69 VEC_INIT(VEC_TLBINVALIDL
, csky_tlbinvalidl
);
70 VEC_INIT(VEC_TLBINVALIDS
, csky_tlbinvalids
);
71 VEC_INIT(VEC_TLBMODIFIED
, csky_tlbmodified
);
73 #ifdef CONFIG_CPU_HAS_FPU
78 mtcr("cr<28, 0>", virt_to_phys(vec_base
));
80 VEC_INIT(VEC_RESET
, (void *)virt_to_phys(_start_smp_secondary
));
84 static DEFINE_SPINLOCK(die_lock
);
86 void die(struct pt_regs
*regs
, const char *str
)
88 static int die_counter
;
93 spin_lock_irq(&die_lock
);
97 pr_emerg("%s [#%d]\n", str
, ++die_counter
);
100 show_stack(current
, (unsigned long *)regs
->regs
[4], KERN_INFO
);
102 ret
= notify_die(DIE_OOPS
, str
, regs
, 0, trap_no(regs
), SIGSEGV
);
105 add_taint(TAINT_DIE
, LOCKDEP_NOW_UNRELIABLE
);
106 spin_unlock_irq(&die_lock
);
110 panic("Fatal exception in interrupt");
112 panic("Fatal exception");
113 if (ret
!= NOTIFY_STOP
)
117 void do_trap(struct pt_regs
*regs
, int signo
, int code
, unsigned long addr
)
119 struct task_struct
*tsk
= current
;
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
));
130 force_sig_fault(signo
, code
, (void __user
*)addr
);
133 static void do_trap_error(struct pt_regs
*regs
, int signo
, int code
,
134 unsigned long addr
, const char *str
)
136 current
->thread
.trap_no
= trap_no(regs
);
138 if (user_mode(regs
)) {
139 do_trap(regs
, signo
, code
, addr
);
141 if (!fixup_exception(regs
))
146 #define DO_ERROR_INFO(name, signo, code, str) \
147 asmlinkage __visible void name(struct pt_regs *regs) \
149 do_trap_error(regs, signo, code, regs->pc, "Oops - " str); \
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");
159 asmlinkage
void do_trap_misaligned(struct pt_regs
*regs
)
161 #ifdef CONFIG_CPU_NEED_SOFTALIGN
162 csky_alignment(regs
);
164 current
->thread
.trap_no
= trap_no(regs
);
165 do_trap_error(regs
, SIGBUS
, BUS_ADRALN
, regs
->pc
,
166 "Oops - load/store address misaligned");
170 asmlinkage
void do_trap_bkpt(struct pt_regs
*regs
)
172 #ifdef CONFIG_KPROBES
173 if (kprobe_single_step_handler(regs
))
176 #ifdef CONFIG_UPROBES
177 if (uprobe_single_step_handler(regs
))
180 if (user_mode(regs
)) {
181 send_sig(SIGTRAP
, current
, 0);
185 do_trap_error(regs
, SIGILL
, ILL_ILLTRP
, regs
->pc
,
186 "Oops - illegal trap exception");
189 asmlinkage
void do_trap_illinsn(struct pt_regs
*regs
)
191 current
->thread
.trap_no
= trap_no(regs
);
193 #ifdef CONFIG_KPROBES
194 if (kprobe_breakpoint_handler(regs
))
197 #ifdef CONFIG_UPROBES
198 if (uprobe_breakpoint_handler(regs
))
201 #ifndef CONFIG_CPU_NO_USER_BKPT
202 if (*(uint16_t *)instruction_pointer(regs
) != USR_BKPT
) {
203 send_sig(SIGTRAP
, current
, 0);
208 do_trap_error(regs
, SIGILL
, ILL_ILLOPC
, regs
->pc
,
209 "Oops - illegal instruction exception");
212 asmlinkage
void do_trap_fpe(struct pt_regs
*regs
)
214 #ifdef CONFIG_CPU_HAS_FP
215 return fpu_fpe(regs
);
217 do_trap_error(regs
, SIGILL
, ILL_ILLOPC
, regs
->pc
,
218 "Oops - fpu instruction exception");
222 asmlinkage
void do_trap_priv(struct pt_regs
*regs
)
224 #ifdef CONFIG_CPU_HAS_FP
225 if (user_mode(regs
) && fpu_libc_helper(regs
))
228 do_trap_error(regs
, SIGILL
, ILL_PRVOPC
, regs
->pc
,
229 "Oops - illegal privileged exception");
232 asmlinkage
void trap_c(struct pt_regs
*regs
)
234 switch (trap_no(regs
)) {
242 do_trap_illinsn(regs
);
249 do_trap_buserr(regs
);
252 do_trap_misaligned(regs
);
261 do_trap_unknown(regs
);