]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - arch/riscv/kernel/traps.c
Merge branch 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-jammy-kernel.git] / arch / riscv / kernel / traps.c
CommitLineData
50acfb2b 1// SPDX-License-Identifier: GPL-2.0-only
76d2a049
PD
2/*
3 * Copyright (C) 2012 Regents of the University of California
76d2a049
PD
4 */
5
6#include <linux/kernel.h>
7#include <linux/init.h>
8#include <linux/sched.h>
9#include <linux/sched/debug.h>
10#include <linux/sched/signal.h>
11#include <linux/signal.h>
12#include <linux/kdebug.h>
13#include <linux/uaccess.h>
14#include <linux/mm.h>
15#include <linux/module.h>
16#include <linux/irq.h>
17
18#include <asm/processor.h>
19#include <asm/ptrace.h>
20#include <asm/csr.h>
21
22int show_unhandled_signals = 1;
23
24extern asmlinkage void handle_exception(void);
25
26static DEFINE_SPINLOCK(die_lock);
27
28void die(struct pt_regs *regs, const char *str)
29{
30 static int die_counter;
31 int ret;
32
33 oops_enter();
34
35 spin_lock_irq(&die_lock);
36 console_verbose();
37 bust_spinlocks(1);
38
39 pr_emerg("%s [#%d]\n", str, ++die_counter);
40 print_modules();
41 show_regs(regs);
42
43 ret = notify_die(DIE_OOPS, str, regs, 0, regs->scause, SIGSEGV);
44
45 bust_spinlocks(0);
46 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
47 spin_unlock_irq(&die_lock);
48 oops_exit();
49
50 if (in_interrupt())
51 panic("Fatal exception in interrupt");
52 if (panic_on_oops)
53 panic("Fatal exception");
54 if (ret != NOTIFY_STOP)
55 do_exit(SIGSEGV);
56}
57
76d2a049
PD
58void do_trap(struct pt_regs *regs, int signo, int code,
59 unsigned long addr, struct task_struct *tsk)
60{
61 if (show_unhandled_signals && unhandled_signal(tsk, signo)
62 && printk_ratelimit()) {
63 pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x" REG_FMT,
64 tsk->comm, task_pid_nr(tsk), signo, code, addr);
6ab77af4 65 print_vma_addr(KERN_CONT " in ", instruction_pointer(regs));
76d2a049
PD
66 pr_cont("\n");
67 show_regs(regs);
68 }
69
7ff3a762 70 force_sig_fault(signo, code, (void __user *)addr, tsk);
76d2a049
PD
71}
72
73static void do_trap_error(struct pt_regs *regs, int signo, int code,
74 unsigned long addr, const char *str)
75{
76 if (user_mode(regs)) {
77 do_trap(regs, signo, code, addr, current);
78 } else {
79 if (!fixup_exception(regs))
80 die(regs, str);
81 }
82}
83
84#define DO_ERROR_INFO(name, signo, code, str) \
85asmlinkage void name(struct pt_regs *regs) \
86{ \
87 do_trap_error(regs, signo, code, regs->sepc, "Oops - " str); \
88}
89
90DO_ERROR_INFO(do_trap_unknown,
91 SIGILL, ILL_ILLTRP, "unknown exception");
92DO_ERROR_INFO(do_trap_insn_misaligned,
93 SIGBUS, BUS_ADRALN, "instruction address misaligned");
94DO_ERROR_INFO(do_trap_insn_fault,
95 SIGSEGV, SEGV_ACCERR, "instruction access fault");
96DO_ERROR_INFO(do_trap_insn_illegal,
97 SIGILL, ILL_ILLOPC, "illegal instruction");
98DO_ERROR_INFO(do_trap_load_misaligned,
99 SIGBUS, BUS_ADRALN, "load address misaligned");
100DO_ERROR_INFO(do_trap_load_fault,
101 SIGSEGV, SEGV_ACCERR, "load access fault");
102DO_ERROR_INFO(do_trap_store_misaligned,
103 SIGBUS, BUS_ADRALN, "store (or AMO) address misaligned");
104DO_ERROR_INFO(do_trap_store_fault,
105 SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault");
106DO_ERROR_INFO(do_trap_ecall_u,
107 SIGILL, ILL_ILLTRP, "environment call from U-mode");
108DO_ERROR_INFO(do_trap_ecall_s,
109 SIGILL, ILL_ILLTRP, "environment call from S-mode");
110DO_ERROR_INFO(do_trap_ecall_m,
111 SIGILL, ILL_ILLTRP, "environment call from M-mode");
112
ee72e0e7
VC
113#ifdef CONFIG_GENERIC_BUG
114static inline unsigned long get_break_insn_length(unsigned long pc)
115{
116 bug_insn_t insn;
117
118 if (probe_kernel_address((bug_insn_t *)pc, insn))
119 return 0;
120 return (((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32) ? 4UL : 2UL);
121}
122#endif /* CONFIG_GENERIC_BUG */
123
76d2a049
PD
124asmlinkage void do_trap_break(struct pt_regs *regs)
125{
126#ifdef CONFIG_GENERIC_BUG
127 if (!user_mode(regs)) {
128 enum bug_trap_type type;
129
130 type = report_bug(regs->sepc, regs);
131 switch (type) {
132 case BUG_TRAP_TYPE_NONE:
133 break;
134 case BUG_TRAP_TYPE_WARN:
ee72e0e7
VC
135 regs->sepc += get_break_insn_length(regs->sepc);
136 break;
76d2a049
PD
137 case BUG_TRAP_TYPE_BUG:
138 die(regs, "Kernel BUG");
139 }
140 }
141#endif /* CONFIG_GENERIC_BUG */
142
7ff3a762 143 force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)(regs->sepc), current);
76d2a049
PD
144}
145
146#ifdef CONFIG_GENERIC_BUG
147int is_valid_bugaddr(unsigned long pc)
148{
149 bug_insn_t insn;
150
9a6e7af0 151 if (pc < VMALLOC_START)
76d2a049 152 return 0;
9bf97390 153 if (probe_kernel_address((bug_insn_t *)pc, insn))
76d2a049 154 return 0;
ee72e0e7
VC
155 if ((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32)
156 return (insn == __BUG_INSN_32);
157 else
158 return ((insn & __COMPRESSED_INSN_MASK) == __BUG_INSN_16);
76d2a049
PD
159}
160#endif /* CONFIG_GENERIC_BUG */
161
162void __init trap_init(void)
163{
164 /*
165 * Set sup0 scratch register to 0, indicating to exception vector
166 * that we are presently executing in the kernel
167 */
a3182c91 168 csr_write(CSR_SSCRATCH, 0);
76d2a049 169 /* Set the exception vector address */
a3182c91 170 csr_write(CSR_STVEC, &handle_exception);
76d2a049 171 /* Enable all interrupts */
a3182c91 172 csr_write(CSR_SIE, -1);
76d2a049 173}