1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Regents of the University of California
7 #include <linux/kernel.h>
8 #include <linux/init.h>
9 #include <linux/randomize_kstack.h>
10 #include <linux/sched.h>
11 #include <linux/sched/debug.h>
12 #include <linux/sched/signal.h>
13 #include <linux/signal.h>
14 #include <linux/kdebug.h>
15 #include <linux/uaccess.h>
16 #include <linux/kprobes.h>
17 #include <linux/uprobes.h>
18 #include <asm/uprobes.h>
20 #include <linux/module.h>
21 #include <linux/irq.h>
22 #include <linux/kexec.h>
23 #include <linux/entry-common.h>
25 #include <asm/asm-prototypes.h>
29 #include <asm/processor.h>
30 #include <asm/ptrace.h>
31 #include <asm/syscall.h>
32 #include <asm/thread_info.h>
33 #include <asm/vector.h>
34 #include <asm/irq_stack.h>
36 int show_unhandled_signals
= 1;
38 static DEFINE_SPINLOCK(die_lock
);
40 static int copy_code(struct pt_regs
*regs
, u16
*val
, const u16
*insns
)
42 const void __user
*uaddr
= (__force
const void __user
*)insns
;
45 return get_kernel_nofault(*val
, insns
);
47 /* The user space code from other tasks cannot be accessed. */
48 if (regs
!= task_pt_regs(current
))
51 return copy_from_user_nofault(val
, uaddr
, sizeof(*val
));
54 static void dump_instr(const char *loglvl
, struct pt_regs
*regs
)
56 char str
[sizeof("0000 ") * 12 + 2 + 1], *p
= str
;
57 const u16
*insns
= (u16
*)instruction_pointer(regs
);
62 for (i
= -10; i
< 2; i
++) {
63 bad
= copy_code(regs
, &val
, &insns
[i
]);
65 p
+= sprintf(p
, i
== 0 ? "(%04hx) " : "%04hx ", val
);
67 printk("%sCode: Unable to access instruction at 0x%px.\n",
72 printk("%sCode: %s\n", loglvl
, str
);
75 void die(struct pt_regs
*regs
, const char *str
)
77 static int die_counter
;
84 spin_lock_irqsave(&die_lock
, flags
);
88 pr_emerg("%s [#%d]\n", str
, ++die_counter
);
92 dump_instr(KERN_EMERG
, regs
);
95 cause
= regs
? regs
->cause
: -1;
96 ret
= notify_die(DIE_OOPS
, str
, regs
, 0, cause
, SIGSEGV
);
98 if (kexec_should_crash(current
))
102 add_taint(TAINT_DIE
, LOCKDEP_NOW_UNRELIABLE
);
103 spin_unlock_irqrestore(&die_lock
, flags
);
107 panic("Fatal exception in interrupt");
109 panic("Fatal exception");
110 if (ret
!= NOTIFY_STOP
)
111 make_task_dead(SIGSEGV
);
114 void do_trap(struct pt_regs
*regs
, int signo
, int code
, unsigned long addr
)
116 struct task_struct
*tsk
= current
;
118 if (show_unhandled_signals
&& unhandled_signal(tsk
, signo
)
119 && printk_ratelimit()) {
120 pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x" REG_FMT
,
121 tsk
->comm
, task_pid_nr(tsk
), signo
, code
, addr
);
122 print_vma_addr(KERN_CONT
" in ", instruction_pointer(regs
));
125 dump_instr(KERN_INFO
, regs
);
128 force_sig_fault(signo
, code
, (void __user
*)addr
);
131 static void do_trap_error(struct pt_regs
*regs
, int signo
, int code
,
132 unsigned long addr
, const char *str
)
134 current
->thread
.bad_cause
= regs
->cause
;
136 if (user_mode(regs
)) {
137 do_trap(regs
, signo
, code
, addr
);
139 if (!fixup_exception(regs
))
144 #if defined(CONFIG_XIP_KERNEL) && defined(CONFIG_RISCV_ALTERNATIVE)
145 #define __trap_section __noinstr_section(".xip.traps")
147 #define __trap_section noinstr
149 #define DO_ERROR_INFO(name, signo, code, str) \
150 asmlinkage __visible __trap_section void name(struct pt_regs *regs) \
152 if (user_mode(regs)) { \
153 irqentry_enter_from_user_mode(regs); \
154 do_trap_error(regs, signo, code, regs->epc, "Oops - " str); \
155 irqentry_exit_to_user_mode(regs); \
157 irqentry_state_t state = irqentry_nmi_enter(regs); \
158 do_trap_error(regs, signo, code, regs->epc, "Oops - " str); \
159 irqentry_nmi_exit(regs, state); \
163 DO_ERROR_INFO(do_trap_unknown
,
164 SIGILL
, ILL_ILLTRP
, "unknown exception");
165 DO_ERROR_INFO(do_trap_insn_misaligned
,
166 SIGBUS
, BUS_ADRALN
, "instruction address misaligned");
167 DO_ERROR_INFO(do_trap_insn_fault
,
168 SIGSEGV
, SEGV_ACCERR
, "instruction access fault");
170 asmlinkage __visible __trap_section
void do_trap_insn_illegal(struct pt_regs
*regs
)
174 if (user_mode(regs
)) {
175 irqentry_enter_from_user_mode(regs
);
179 handled
= riscv_v_first_use_handler(regs
);
184 do_trap_error(regs
, SIGILL
, ILL_ILLOPC
, regs
->epc
,
185 "Oops - illegal instruction");
187 irqentry_exit_to_user_mode(regs
);
189 irqentry_state_t state
= irqentry_nmi_enter(regs
);
191 do_trap_error(regs
, SIGILL
, ILL_ILLOPC
, regs
->epc
,
192 "Oops - illegal instruction");
194 irqentry_nmi_exit(regs
, state
);
198 DO_ERROR_INFO(do_trap_load_fault
,
199 SIGSEGV
, SEGV_ACCERR
, "load access fault");
201 asmlinkage __visible __trap_section
void do_trap_load_misaligned(struct pt_regs
*regs
)
203 if (user_mode(regs
)) {
204 irqentry_enter_from_user_mode(regs
);
206 if (handle_misaligned_load(regs
))
207 do_trap_error(regs
, SIGBUS
, BUS_ADRALN
, regs
->epc
,
208 "Oops - load address misaligned");
210 irqentry_exit_to_user_mode(regs
);
212 irqentry_state_t state
= irqentry_nmi_enter(regs
);
214 if (handle_misaligned_load(regs
))
215 do_trap_error(regs
, SIGBUS
, BUS_ADRALN
, regs
->epc
,
216 "Oops - load address misaligned");
218 irqentry_nmi_exit(regs
, state
);
222 asmlinkage __visible __trap_section
void do_trap_store_misaligned(struct pt_regs
*regs
)
224 if (user_mode(regs
)) {
225 irqentry_enter_from_user_mode(regs
);
227 if (handle_misaligned_store(regs
))
228 do_trap_error(regs
, SIGBUS
, BUS_ADRALN
, regs
->epc
,
229 "Oops - store (or AMO) address misaligned");
231 irqentry_exit_to_user_mode(regs
);
233 irqentry_state_t state
= irqentry_nmi_enter(regs
);
235 if (handle_misaligned_store(regs
))
236 do_trap_error(regs
, SIGBUS
, BUS_ADRALN
, regs
->epc
,
237 "Oops - store (or AMO) address misaligned");
239 irqentry_nmi_exit(regs
, state
);
242 DO_ERROR_INFO(do_trap_store_fault
,
243 SIGSEGV
, SEGV_ACCERR
, "store (or AMO) access fault");
244 DO_ERROR_INFO(do_trap_ecall_s
,
245 SIGILL
, ILL_ILLTRP
, "environment call from S-mode");
246 DO_ERROR_INFO(do_trap_ecall_m
,
247 SIGILL
, ILL_ILLTRP
, "environment call from M-mode");
249 static inline unsigned long get_break_insn_length(unsigned long pc
)
253 if (get_kernel_nofault(insn
, (bug_insn_t
*)pc
))
256 return GET_INSN_LENGTH(insn
);
259 static bool probe_single_step_handler(struct pt_regs
*regs
)
261 bool user
= user_mode(regs
);
263 return user
? uprobe_single_step_handler(regs
) : kprobe_single_step_handler(regs
);
266 static bool probe_breakpoint_handler(struct pt_regs
*regs
)
268 bool user
= user_mode(regs
);
270 return user
? uprobe_breakpoint_handler(regs
) : kprobe_breakpoint_handler(regs
);
273 void handle_break(struct pt_regs
*regs
)
275 if (probe_single_step_handler(regs
))
278 if (probe_breakpoint_handler(regs
))
281 current
->thread
.bad_cause
= regs
->cause
;
284 force_sig_fault(SIGTRAP
, TRAP_BRKPT
, (void __user
*)regs
->epc
);
286 else if (notify_die(DIE_TRAP
, "EBREAK", regs
, 0, regs
->cause
, SIGTRAP
)
290 else if (report_bug(regs
->epc
, regs
) == BUG_TRAP_TYPE_WARN
||
291 handle_cfi_failure(regs
) == BUG_TRAP_TYPE_WARN
)
292 regs
->epc
+= get_break_insn_length(regs
->epc
);
294 die(regs
, "Kernel BUG");
297 asmlinkage __visible __trap_section
void do_trap_break(struct pt_regs
*regs
)
299 if (user_mode(regs
)) {
300 irqentry_enter_from_user_mode(regs
);
304 irqentry_exit_to_user_mode(regs
);
306 irqentry_state_t state
= irqentry_nmi_enter(regs
);
310 irqentry_nmi_exit(regs
, state
);
314 asmlinkage __visible __trap_section __no_stack_protector
315 void do_trap_ecall_u(struct pt_regs
*regs
)
317 if (user_mode(regs
)) {
318 long syscall
= regs
->a7
;
321 regs
->orig_a0
= regs
->a0
;
324 riscv_v_vstate_discard(regs
);
326 syscall
= syscall_enter_from_user_mode(regs
, syscall
);
328 add_random_kstack_offset();
330 if (syscall
>= 0 && syscall
< NR_syscalls
)
331 syscall_handler(regs
, syscall
);
334 * Ultimately, this value will get limited by KSTACK_OFFSET_MAX(),
335 * so the maximum stack offset is 1k bytes (10 bits).
337 * The actual entropy will be further reduced by the compiler when
338 * applying stack alignment constraints: 16-byte (i.e. 4-bit) aligned
339 * for RV32I or RV64I.
341 * The resulting 6 bits of entropy is seen in SP[9:4].
343 choose_random_kstack_offset(get_random_u16());
345 syscall_exit_to_user_mode(regs
);
347 irqentry_state_t state
= irqentry_nmi_enter(regs
);
349 do_trap_error(regs
, SIGILL
, ILL_ILLTRP
, regs
->epc
,
350 "Oops - environment call from U-mode");
352 irqentry_nmi_exit(regs
, state
);
358 asmlinkage __visible noinstr
void do_page_fault(struct pt_regs
*regs
)
360 irqentry_state_t state
= irqentry_enter(regs
);
362 handle_page_fault(regs
);
366 irqentry_exit(regs
, state
);
370 static void noinstr
handle_riscv_irq(struct pt_regs
*regs
)
372 struct pt_regs
*old_regs
;
375 old_regs
= set_irq_regs(regs
);
376 handle_arch_irq(regs
);
377 set_irq_regs(old_regs
);
381 asmlinkage
void noinstr
do_irq(struct pt_regs
*regs
)
383 irqentry_state_t state
= irqentry_enter(regs
);
385 if (IS_ENABLED(CONFIG_IRQ_STACKS
) && on_thread_stack())
386 call_on_irq_stack(regs
, handle_riscv_irq
);
388 handle_riscv_irq(regs
);
390 irqentry_exit(regs
, state
);
393 #ifdef CONFIG_GENERIC_BUG
394 int is_valid_bugaddr(unsigned long pc
)
398 if (pc
< VMALLOC_START
)
400 if (get_kernel_nofault(insn
, (bug_insn_t
*)pc
))
402 if ((insn
& __INSN_LENGTH_MASK
) == __INSN_LENGTH_32
)
403 return (insn
== __BUG_INSN_32
);
405 return ((insn
& __COMPRESSED_INSN_MASK
) == __BUG_INSN_16
);
407 #endif /* CONFIG_GENERIC_BUG */
409 #ifdef CONFIG_VMAP_STACK
410 DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE
/sizeof(long)],
411 overflow_stack
)__aligned(16);
413 asmlinkage
void handle_bad_stack(struct pt_regs
*regs
)
415 unsigned long tsk_stk
= (unsigned long)current
->stack
;
416 unsigned long ovf_stk
= (unsigned long)this_cpu_ptr(overflow_stack
);
420 pr_emerg("Insufficient stack space to handle exception!\n");
421 pr_emerg("Task stack: [0x%016lx..0x%016lx]\n",
422 tsk_stk
, tsk_stk
+ THREAD_SIZE
);
423 pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
424 ovf_stk
, ovf_stk
+ OVERFLOW_STACK_SIZE
);
427 panic("Kernel stack overflow");
430 wait_for_interrupt();