1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
5 #include <linux/sched.h>
6 #include <linux/signal.h>
7 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/user.h>
11 #include <linux/string.h>
12 #include <linux/linkage.h>
13 #include <linux/init.h>
14 #include <linux/ptrace.h>
15 #include <linux/kallsyms.h>
16 #include <linux/rtc.h>
17 #include <linux/uaccess.h>
18 #include <linux/kprobes.h>
19 #include <linux/kdebug.h>
20 #include <linux/sched/debug.h>
22 #include <asm/setup.h>
23 #include <asm/traps.h>
24 #include <asm/pgalloc.h>
25 #include <asm/siginfo.h>
27 #include <asm/mmu_context.h>
29 #ifdef CONFIG_CPU_HAS_FPU
33 int show_unhandled_signals = 1;
35 /* Defined in entry.S */
36 asmlinkage void csky_trap(void);
38 asmlinkage void csky_systemcall(void);
39 asmlinkage void csky_cmpxchg(void);
40 asmlinkage void csky_get_tls(void);
41 asmlinkage void csky_irq(void);
43 asmlinkage void csky_pagefault(void);
45 /* Defined in head.S */
46 asmlinkage void _start_smp_secondary(void);
48 void __init pre_trap_init(void)
52 mtcr("vbr", vec_base);
54 for (i = 1; i < 128; i++)
55 VEC_INIT(i, csky_trap);
58 void __init trap_init(void)
60 VEC_INIT(VEC_AUTOVEC, csky_irq);
62 /* setup trap0 trap2 trap3 */
63 VEC_INIT(VEC_TRAP0, csky_systemcall);
64 VEC_INIT(VEC_TRAP2, csky_cmpxchg);
65 VEC_INIT(VEC_TRAP3, csky_get_tls);
67 /* setup MMU TLB exception */
68 VEC_INIT(VEC_TLBINVALIDL, csky_pagefault);
69 VEC_INIT(VEC_TLBINVALIDS, csky_pagefault);
70 VEC_INIT(VEC_TLBMODIFIED, csky_pagefault);
72 #ifdef CONFIG_CPU_HAS_FPU
77 mtcr("cr<28, 0>", virt_to_phys(vec_base));
79 VEC_INIT(VEC_RESET, (void *)virt_to_phys(_start_smp_secondary));
83 static DEFINE_SPINLOCK(die_lock);
85 void die(struct pt_regs *regs, const char *str)
87 static int die_counter;
92 spin_lock_irq(&die_lock);
96 pr_emerg("%s [#%d]\n", str, ++die_counter);
99 show_stack(current, (unsigned long *)regs->regs[4], KERN_INFO);
101 ret = notify_die(DIE_OOPS, str, regs, 0, trap_no(regs), SIGSEGV);
104 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
105 spin_unlock_irq(&die_lock);
109 panic("Fatal exception in interrupt");
111 panic("Fatal exception");
112 if (ret != NOTIFY_STOP)
113 make_task_dead(SIGSEGV);
116 void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr)
118 struct task_struct *tsk = current;
120 if (show_unhandled_signals && unhandled_signal(tsk, signo)
121 && printk_ratelimit()) {
122 pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x%08lx",
123 tsk->comm, task_pid_nr(tsk), signo, code, addr);
124 print_vma_addr(KERN_CONT " in ", instruction_pointer(regs));
129 force_sig_fault(signo, code, (void __user *)addr);
132 static void do_trap_error(struct pt_regs *regs, int signo, int code,
133 unsigned long addr, const char *str)
135 current->thread.trap_no = trap_no(regs);
137 if (user_mode(regs)) {
138 do_trap(regs, signo, code, addr);
140 if (!fixup_exception(regs))
145 #define DO_ERROR_INFO(name, signo, code, str) \
146 asmlinkage __visible void name(struct pt_regs *regs) \
148 do_trap_error(regs, signo, code, regs->pc, "Oops - " str); \
151 DO_ERROR_INFO(do_trap_unknown,
152 SIGILL, ILL_ILLTRP, "unknown exception");
153 DO_ERROR_INFO(do_trap_zdiv,
154 SIGFPE, FPE_INTDIV, "error zero div exception");
155 DO_ERROR_INFO(do_trap_buserr,
156 SIGSEGV, ILL_ILLADR, "error bus error exception");
158 asmlinkage void do_trap_misaligned(struct pt_regs *regs)
160 #ifdef CONFIG_CPU_NEED_SOFTALIGN
161 csky_alignment(regs);
163 current->thread.trap_no = trap_no(regs);
164 do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->pc,
165 "Oops - load/store address misaligned");
169 asmlinkage void do_trap_bkpt(struct pt_regs *regs)
171 #ifdef CONFIG_KPROBES
172 if (kprobe_single_step_handler(regs))
175 #ifdef CONFIG_UPROBES
176 if (uprobe_single_step_handler(regs))
179 if (user_mode(regs)) {
180 send_sig(SIGTRAP, current, 0);
184 do_trap_error(regs, SIGILL, ILL_ILLTRP, regs->pc,
185 "Oops - illegal trap exception");
188 asmlinkage void do_trap_illinsn(struct pt_regs *regs)
190 current->thread.trap_no = trap_no(regs);
192 #ifdef CONFIG_KPROBES
193 if (kprobe_breakpoint_handler(regs))
196 #ifdef CONFIG_UPROBES
197 if (uprobe_breakpoint_handler(regs))
200 #ifndef CONFIG_CPU_NO_USER_BKPT
201 if (*(uint16_t *)instruction_pointer(regs) != USR_BKPT) {
202 send_sig(SIGTRAP, current, 0);
207 do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->pc,
208 "Oops - illegal instruction exception");
211 asmlinkage void do_trap_fpe(struct pt_regs *regs)
213 #ifdef CONFIG_CPU_HAS_FPU
214 return fpu_fpe(regs);
216 do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->pc,
217 "Oops - fpu instruction exception");
221 asmlinkage void do_trap_priv(struct pt_regs *regs)
223 #ifdef CONFIG_CPU_HAS_FPU
224 if (user_mode(regs) && fpu_libc_helper(regs))
227 do_trap_error(regs, SIGILL, ILL_PRVOPC, regs->pc,
228 "Oops - illegal privileged exception");
231 asmlinkage void trap_c(struct pt_regs *regs)
233 switch (trap_no(regs)) {
241 do_trap_illinsn(regs);
248 do_trap_buserr(regs);
251 do_trap_misaligned(regs);
260 do_trap_unknown(regs);