Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright (C) 2012 Regents of the University of California
  4 */
  5
  6#include <linux/cpu.h>
  7#include <linux/kernel.h>
  8#include <linux/init.h>
  9#include <linux/sched.h>
 10#include <linux/sched/debug.h>
 11#include <linux/sched/signal.h>
 12#include <linux/signal.h>
 13#include <linux/kdebug.h>
 14#include <linux/uaccess.h>
 15#include <linux/kprobes.h>
 16#include <linux/mm.h>
 17#include <linux/module.h>
 18#include <linux/irq.h>
 19
 20#include <asm/asm-prototypes.h>
 21#include <asm/bug.h>
 22#include <asm/processor.h>
 23#include <asm/ptrace.h>
 24#include <asm/csr.h>
 25
 26int show_unhandled_signals = 1;
 27
 28static DEFINE_SPINLOCK(die_lock);
 29
 30void die(struct pt_regs *regs, const char *str)
 31{
 32	static int die_counter;
 33	int ret;
 34
 35	oops_enter();
 36
 37	spin_lock_irq(&die_lock);
 38	console_verbose();
 39	bust_spinlocks(1);
 40
 41	pr_emerg("%s [#%d]\n", str, ++die_counter);
 42	print_modules();
 43	show_regs(regs);
 44
 45	ret = notify_die(DIE_OOPS, str, regs, 0, regs->cause, SIGSEGV);
 46
 47	bust_spinlocks(0);
 48	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
 49	spin_unlock_irq(&die_lock);
 50	oops_exit();
 51
 52	if (in_interrupt())
 53		panic("Fatal exception in interrupt");
 54	if (panic_on_oops)
 55		panic("Fatal exception");
 56	if (ret != NOTIFY_STOP)
 57		do_exit(SIGSEGV);
 58}
 59
 60void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr)
 61{
 62	struct task_struct *tsk = current;
 63
 64	if (show_unhandled_signals && unhandled_signal(tsk, signo)
 65	    && printk_ratelimit()) {
 66		pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x" REG_FMT,
 67			tsk->comm, task_pid_nr(tsk), signo, code, addr);
 68		print_vma_addr(KERN_CONT " in ", instruction_pointer(regs));
 69		pr_cont("\n");
 70		__show_regs(regs);
 71	}
 72
 73	force_sig_fault(signo, code, (void __user *)addr);
 74}
 75
 76static void do_trap_error(struct pt_regs *regs, int signo, int code,
 77	unsigned long addr, const char *str)
 78{
 79	current->thread.bad_cause = regs->cause;
 80
 81	if (user_mode(regs)) {
 82		do_trap(regs, signo, code, addr);
 83	} else {
 84		if (!fixup_exception(regs))
 85			die(regs, str);
 86	}
 87}
 88
 89#if defined (CONFIG_XIP_KERNEL) && defined (CONFIG_RISCV_ERRATA_ALTERNATIVE)
 90#define __trap_section		__section(".xip.traps")
 91#else
 92#define __trap_section
 93#endif
 94#define DO_ERROR_INFO(name, signo, code, str)				\
 95asmlinkage __visible __trap_section void name(struct pt_regs *regs)	\
 96{									\
 97	do_trap_error(regs, signo, code, regs->epc, "Oops - " str);	\
 98}
 99
100DO_ERROR_INFO(do_trap_unknown,
101	SIGILL, ILL_ILLTRP, "unknown exception");
102DO_ERROR_INFO(do_trap_insn_misaligned,
103	SIGBUS, BUS_ADRALN, "instruction address misaligned");
104DO_ERROR_INFO(do_trap_insn_fault,
105	SIGSEGV, SEGV_ACCERR, "instruction access fault");
106DO_ERROR_INFO(do_trap_insn_illegal,
107	SIGILL, ILL_ILLOPC, "illegal instruction");
108DO_ERROR_INFO(do_trap_load_fault,
109	SIGSEGV, SEGV_ACCERR, "load access fault");
110#ifndef CONFIG_RISCV_M_MODE
111DO_ERROR_INFO(do_trap_load_misaligned,
112	SIGBUS, BUS_ADRALN, "Oops - load address misaligned");
113DO_ERROR_INFO(do_trap_store_misaligned,
114	SIGBUS, BUS_ADRALN, "Oops - store (or AMO) address misaligned");
115#else
116int handle_misaligned_load(struct pt_regs *regs);
117int handle_misaligned_store(struct pt_regs *regs);
118
119asmlinkage void __trap_section do_trap_load_misaligned(struct pt_regs *regs)
120{
121	if (!handle_misaligned_load(regs))
122		return;
123	do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
124		      "Oops - load address misaligned");
125}
126
127asmlinkage void __trap_section do_trap_store_misaligned(struct pt_regs *regs)
128{
129	if (!handle_misaligned_store(regs))
130		return;
131	do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
132		      "Oops - store (or AMO) address misaligned");
133}
134#endif
135DO_ERROR_INFO(do_trap_store_fault,
136	SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault");
137DO_ERROR_INFO(do_trap_ecall_u,
138	SIGILL, ILL_ILLTRP, "environment call from U-mode");
139DO_ERROR_INFO(do_trap_ecall_s,
140	SIGILL, ILL_ILLTRP, "environment call from S-mode");
141DO_ERROR_INFO(do_trap_ecall_m,
142	SIGILL, ILL_ILLTRP, "environment call from M-mode");
143
144static inline unsigned long get_break_insn_length(unsigned long pc)
145{
146	bug_insn_t insn;
147
148	if (get_kernel_nofault(insn, (bug_insn_t *)pc))
149		return 0;
150
151	return GET_INSN_LENGTH(insn);
152}
153
154asmlinkage __visible __trap_section void do_trap_break(struct pt_regs *regs)
155{
156#ifdef CONFIG_KPROBES
157	if (kprobe_single_step_handler(regs))
158		return;
159
160	if (kprobe_breakpoint_handler(regs))
161		return;
162#endif
163#ifdef CONFIG_UPROBES
164	if (uprobe_single_step_handler(regs))
165		return;
166
167	if (uprobe_breakpoint_handler(regs))
168		return;
169#endif
170	current->thread.bad_cause = regs->cause;
171
172	if (user_mode(regs))
173		force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->epc);
174#ifdef CONFIG_KGDB
175	else if (notify_die(DIE_TRAP, "EBREAK", regs, 0, regs->cause, SIGTRAP)
176								== NOTIFY_STOP)
177		return;
178#endif
179	else if (report_bug(regs->epc, regs) == BUG_TRAP_TYPE_WARN)
180		regs->epc += get_break_insn_length(regs->epc);
181	else
182		die(regs, "Kernel BUG");
183}
184NOKPROBE_SYMBOL(do_trap_break);
185
186#ifdef CONFIG_GENERIC_BUG
187int is_valid_bugaddr(unsigned long pc)
188{
189	bug_insn_t insn;
190
191	if (pc < VMALLOC_START)
192		return 0;
193	if (get_kernel_nofault(insn, (bug_insn_t *)pc))
194		return 0;
195	if ((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32)
196		return (insn == __BUG_INSN_32);
197	else
198		return ((insn & __COMPRESSED_INSN_MASK) == __BUG_INSN_16);
199}
200#endif /* CONFIG_GENERIC_BUG */
201
202/* stvec & scratch is already set from head.S */
203void __init trap_init(void)
204{
205}
206
207#ifdef CONFIG_VMAP_STACK
208static DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)],
209		overflow_stack)__aligned(16);
210/*
211 * shadow stack, handled_ kernel_ stack_ overflow(in kernel/entry.S) is used
212 * to get per-cpu overflow stack(get_overflow_stack).
213 */
214long shadow_stack[SHADOW_OVERFLOW_STACK_SIZE/sizeof(long)];
215asmlinkage unsigned long get_overflow_stack(void)
216{
217	return (unsigned long)this_cpu_ptr(overflow_stack) +
218		OVERFLOW_STACK_SIZE;
219}
220
221asmlinkage void handle_bad_stack(struct pt_regs *regs)
222{
223	unsigned long tsk_stk = (unsigned long)current->stack;
224	unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
225
226	console_verbose();
227
228	pr_emerg("Insufficient stack space to handle exception!\n");
229	pr_emerg("Task stack:     [0x%016lx..0x%016lx]\n",
230			tsk_stk, tsk_stk + THREAD_SIZE);
231	pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
232			ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE);
233
234	__show_regs(regs);
235	panic("Kernel stack overflow");
236
237	for (;;)
238		wait_for_interrupt();
239}
240#endif