mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2025-01-18 03:44:27 +08:00
872d8327ce
This patch adds stack overflow detection to arm64, usable when vmap'd stacks are in use. Overflow is detected in a small preamble executed for each exception entry, which checks whether there is enough space on the current stack for the general purpose registers to be saved. If there is not enough space, the overflow handler is invoked on a per-cpu overflow stack. This approach preserves the original exception information in ESR_EL1 (and where appropriate, FAR_EL1). Task and IRQ stacks are aligned to double their size, enabling overflow to be detected with a single bit test. For example, a 16K stack is aligned to 32K, ensuring that bit 14 of the SP must be zero. On an overflow (or underflow), this bit is flipped. Thus, overflow (of less than the size of the stack) can be detected by testing whether this bit is set. The overflow check is performed before any attempt is made to access the stack, avoiding recursive faults (and the loss of exception information these would entail). As logical operations cannot be performed on the SP directly, the SP is temporarily swapped with a general purpose register using arithmetic operations to enable the test to be performed. This gives us a useful error message on stack overflow, as can be trigger with the LKDTM overflow test: [ 305.388749] lkdtm: Performing direct entry OVERFLOW [ 305.395444] Insufficient stack space to handle exception! [ 305.395482] ESR: 0x96000047 -- DABT (current EL) [ 305.399890] FAR: 0xffff00000a5e7f30 [ 305.401315] Task stack: [0xffff00000a5e8000..0xffff00000a5ec000] [ 305.403815] IRQ stack: [0xffff000008000000..0xffff000008004000] [ 305.407035] Overflow stack: [0xffff80003efce4e0..0xffff80003efcf4e0] [ 305.409622] CPU: 0 PID: 1219 Comm: sh Not tainted 4.13.0-rc3-00021-g9636aea #5 [ 305.412785] Hardware name: linux,dummy-virt (DT) [ 305.415756] task: ffff80003d051c00 task.stack: ffff00000a5e8000 [ 305.419221] PC is at recursive_loop+0x10/0x48 [ 305.421637] LR is at recursive_loop+0x38/0x48 [ 305.423768] pc : [<ffff00000859f330>] lr : [<ffff00000859f358>] pstate: 40000145 [ 305.428020] sp : ffff00000a5e7f50 [ 305.430469] x29: ffff00000a5e8350 x28: ffff80003d051c00 [ 305.433191] x27: ffff000008981000 x26: ffff000008f80400 [ 305.439012] x25: ffff00000a5ebeb8 x24: ffff00000a5ebeb8 [ 305.440369] x23: ffff000008f80138 x22: 0000000000000009 [ 305.442241] x21: ffff80003ce65000 x20: ffff000008f80188 [ 305.444552] x19: 0000000000000013 x18: 0000000000000006 [ 305.446032] x17: 0000ffffa2601280 x16: ffff0000081fe0b8 [ 305.448252] x15: ffff000008ff546d x14: 000000000047a4c8 [ 305.450246] x13: ffff000008ff7872 x12: 0000000005f5e0ff [ 305.452953] x11: ffff000008ed2548 x10: 000000000005ee8d [ 305.454824] x9 : ffff000008545380 x8 : ffff00000a5e8770 [ 305.457105] x7 : 1313131313131313 x6 : 00000000000000e1 [ 305.459285] x5 : 0000000000000000 x4 : 0000000000000000 [ 305.461781] x3 : 0000000000000000 x2 : 0000000000000400 [ 305.465119] x1 : 0000000000000013 x0 : 0000000000000012 [ 305.467724] Kernel panic - not syncing: kernel stack overflow [ 305.470561] CPU: 0 PID: 1219 Comm: sh Not tainted 4.13.0-rc3-00021-g9636aea #5 [ 305.473325] Hardware name: linux,dummy-virt (DT) [ 305.475070] Call trace: [ 305.476116] [<ffff000008088ad8>] dump_backtrace+0x0/0x378 [ 305.478991] [<ffff000008088e64>] show_stack+0x14/0x20 [ 305.481237] [<ffff00000895a178>] dump_stack+0x98/0xb8 [ 305.483294] [<ffff0000080c3288>] panic+0x118/0x280 [ 305.485673] [<ffff0000080c2e9c>] nmi_panic+0x6c/0x70 [ 305.486216] [<ffff000008089710>] handle_bad_stack+0x118/0x128 [ 305.486612] Exception stack(0xffff80003efcf3a0 to 0xffff80003efcf4e0) [ 305.487334] f3a0: 0000000000000012 0000000000000013 0000000000000400 0000000000000000 [ 305.488025] f3c0: 0000000000000000 0000000000000000 00000000000000e1 1313131313131313 [ 305.488908] f3e0: ffff00000a5e8770 ffff000008545380 000000000005ee8d ffff000008ed2548 [ 305.489403] f400: 0000000005f5e0ff ffff000008ff7872 000000000047a4c8 ffff000008ff546d [ 305.489759] f420: ffff0000081fe0b8 0000ffffa2601280 0000000000000006 0000000000000013 [ 305.490256] f440: ffff000008f80188 ffff80003ce65000 0000000000000009 ffff000008f80138 [ 305.490683] f460: ffff00000a5ebeb8 ffff00000a5ebeb8 ffff000008f80400 ffff000008981000 [ 305.491051] f480: ffff80003d051c00 ffff00000a5e8350 ffff00000859f358 ffff00000a5e7f50 [ 305.491444] f4a0: ffff00000859f330 0000000040000145 0000000000000000 0000000000000000 [ 305.492008] f4c0: 0001000000000000 0000000000000000 ffff00000a5e8350 ffff00000859f330 [ 305.493063] [<ffff00000808205c>] __bad_stack+0x88/0x8c [ 305.493396] [<ffff00000859f330>] recursive_loop+0x10/0x48 [ 305.493731] [<ffff00000859f358>] recursive_loop+0x38/0x48 [ 305.494088] [<ffff00000859f358>] recursive_loop+0x38/0x48 [ 305.494425] [<ffff00000859f358>] recursive_loop+0x38/0x48 [ 305.494649] [<ffff00000859f358>] recursive_loop+0x38/0x48 [ 305.494898] [<ffff00000859f358>] recursive_loop+0x38/0x48 [ 305.495205] [<ffff00000859f358>] recursive_loop+0x38/0x48 [ 305.495453] [<ffff00000859f358>] recursive_loop+0x38/0x48 [ 305.495708] [<ffff00000859f358>] recursive_loop+0x38/0x48 [ 305.496000] [<ffff00000859f358>] recursive_loop+0x38/0x48 [ 305.496302] [<ffff00000859f358>] recursive_loop+0x38/0x48 [ 305.496644] [<ffff00000859f358>] recursive_loop+0x38/0x48 [ 305.496894] [<ffff00000859f358>] recursive_loop+0x38/0x48 [ 305.497138] [<ffff00000859f358>] recursive_loop+0x38/0x48 [ 305.497325] [<ffff00000859f3dc>] lkdtm_OVERFLOW+0x14/0x20 [ 305.497506] [<ffff00000859f314>] lkdtm_do_action+0x1c/0x28 [ 305.497786] [<ffff00000859f178>] direct_entry+0xe0/0x170 [ 305.498095] [<ffff000008345568>] full_proxy_write+0x60/0xa8 [ 305.498387] [<ffff0000081fb7f4>] __vfs_write+0x1c/0x128 [ 305.498679] [<ffff0000081fcc68>] vfs_write+0xa0/0x1b0 [ 305.498926] [<ffff0000081fe0fc>] SyS_write+0x44/0xa0 [ 305.499182] Exception stack(0xffff00000a5ebec0 to 0xffff00000a5ec000) [ 305.499429] bec0: 0000000000000001 000000001c4cf5e0 0000000000000009 000000001c4cf5e0 [ 305.499674] bee0: 574f4c465245564f 0000000000000000 0000000000000000 8000000080808080 [ 305.499904] bf00: 0000000000000040 0000000000000038 fefefeff1b4bc2ff 7f7f7f7f7f7fff7f [ 305.500189] bf20: 0101010101010101 0000000000000000 000000000047a4c8 0000000000000038 [ 305.500712] bf40: 0000000000000000 0000ffffa2601280 0000ffffc63f6068 00000000004b5000 [ 305.501241] bf60: 0000000000000001 000000001c4cf5e0 0000000000000009 000000001c4cf5e0 [ 305.501791] bf80: 0000000000000020 0000000000000000 00000000004b5000 000000001c4cc458 [ 305.502314] bfa0: 0000000000000000 0000ffffc63f7950 000000000040a3c4 0000ffffc63f70e0 [ 305.502762] bfc0: 0000ffffa2601268 0000000080000000 0000000000000001 0000000000000040 [ 305.503207] bfe0: 0000000000000000 0000000000000000 0000000000000000 0000000000000000 [ 305.503680] [<ffff000008082fb0>] el0_svc_naked+0x24/0x28 [ 305.504720] Kernel Offset: disabled [ 305.505189] CPU features: 0x002082 [ 305.505473] Memory Limit: none [ 305.506181] ---[ end Kernel panic - not syncing: kernel stack overflow This patch was co-authored by Ard Biesheuvel and Mark Rutland. Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Signed-off-by: Mark Rutland <mark.rutland@arm.com> Reviewed-by: Will Deacon <will.deacon@arm.com> Tested-by: Laura Abbott <labbott@redhat.com> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: James Morse <james.morse@arm.com>
786 lines
19 KiB
C
786 lines
19 KiB
C
/*
|
|
* Based on arch/arm/kernel/traps.c
|
|
*
|
|
* Copyright (C) 1995-2009 Russell King
|
|
* Copyright (C) 2012 ARM Ltd.
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
* published by the Free Software Foundation.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <linux/bug.h>
|
|
#include <linux/signal.h>
|
|
#include <linux/personality.h>
|
|
#include <linux/kallsyms.h>
|
|
#include <linux/spinlock.h>
|
|
#include <linux/uaccess.h>
|
|
#include <linux/hardirq.h>
|
|
#include <linux/kdebug.h>
|
|
#include <linux/module.h>
|
|
#include <linux/kexec.h>
|
|
#include <linux/delay.h>
|
|
#include <linux/init.h>
|
|
#include <linux/sched/signal.h>
|
|
#include <linux/sched/debug.h>
|
|
#include <linux/sched/task_stack.h>
|
|
#include <linux/sizes.h>
|
|
#include <linux/syscalls.h>
|
|
#include <linux/mm_types.h>
|
|
|
|
#include <asm/atomic.h>
|
|
#include <asm/bug.h>
|
|
#include <asm/debug-monitors.h>
|
|
#include <asm/esr.h>
|
|
#include <asm/insn.h>
|
|
#include <asm/traps.h>
|
|
#include <asm/smp.h>
|
|
#include <asm/stack_pointer.h>
|
|
#include <asm/stacktrace.h>
|
|
#include <asm/exception.h>
|
|
#include <asm/system_misc.h>
|
|
#include <asm/sysreg.h>
|
|
|
|
static const char *handler[]= {
|
|
"Synchronous Abort",
|
|
"IRQ",
|
|
"FIQ",
|
|
"Error"
|
|
};
|
|
|
|
int show_unhandled_signals = 1;
|
|
|
|
/*
|
|
* Dump out the contents of some kernel memory nicely...
|
|
*/
|
|
static void dump_mem(const char *lvl, const char *str, unsigned long bottom,
|
|
unsigned long top)
|
|
{
|
|
unsigned long first;
|
|
mm_segment_t fs;
|
|
int i;
|
|
|
|
/*
|
|
* We need to switch to kernel mode so that we can use __get_user
|
|
* to safely read from kernel space.
|
|
*/
|
|
fs = get_fs();
|
|
set_fs(KERNEL_DS);
|
|
|
|
printk("%s%s(0x%016lx to 0x%016lx)\n", lvl, str, bottom, top);
|
|
|
|
for (first = bottom & ~31; first < top; first += 32) {
|
|
unsigned long p;
|
|
char str[sizeof(" 12345678") * 8 + 1];
|
|
|
|
memset(str, ' ', sizeof(str));
|
|
str[sizeof(str) - 1] = '\0';
|
|
|
|
for (p = first, i = 0; i < (32 / 8)
|
|
&& p < top; i++, p += 8) {
|
|
if (p >= bottom && p < top) {
|
|
unsigned long val;
|
|
|
|
if (__get_user(val, (unsigned long *)p) == 0)
|
|
sprintf(str + i * 17, " %016lx", val);
|
|
else
|
|
sprintf(str + i * 17, " ????????????????");
|
|
}
|
|
}
|
|
printk("%s%04lx:%s\n", lvl, first & 0xffff, str);
|
|
}
|
|
|
|
set_fs(fs);
|
|
}
|
|
|
|
static void dump_backtrace_entry(unsigned long where)
|
|
{
|
|
/*
|
|
* Note that 'where' can have a physical address, but it's not handled.
|
|
*/
|
|
print_ip_sym(where);
|
|
}
|
|
|
|
static void __dump_instr(const char *lvl, struct pt_regs *regs)
|
|
{
|
|
unsigned long addr = instruction_pointer(regs);
|
|
char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
|
|
int i;
|
|
|
|
for (i = -4; i < 1; i++) {
|
|
unsigned int val, bad;
|
|
|
|
bad = __get_user(val, &((u32 *)addr)[i]);
|
|
|
|
if (!bad)
|
|
p += sprintf(p, i == 0 ? "(%08x) " : "%08x ", val);
|
|
else {
|
|
p += sprintf(p, "bad PC value");
|
|
break;
|
|
}
|
|
}
|
|
printk("%sCode: %s\n", lvl, str);
|
|
}
|
|
|
|
static void dump_instr(const char *lvl, struct pt_regs *regs)
|
|
{
|
|
if (!user_mode(regs)) {
|
|
mm_segment_t fs = get_fs();
|
|
set_fs(KERNEL_DS);
|
|
__dump_instr(lvl, regs);
|
|
set_fs(fs);
|
|
} else {
|
|
__dump_instr(lvl, regs);
|
|
}
|
|
}
|
|
|
|
void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
|
|
{
|
|
struct stackframe frame;
|
|
int skip;
|
|
|
|
pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
|
|
|
|
if (!tsk)
|
|
tsk = current;
|
|
|
|
if (!try_get_task_stack(tsk))
|
|
return;
|
|
|
|
if (tsk == current) {
|
|
frame.fp = (unsigned long)__builtin_frame_address(0);
|
|
frame.pc = (unsigned long)dump_backtrace;
|
|
} else {
|
|
/*
|
|
* task blocked in __switch_to
|
|
*/
|
|
frame.fp = thread_saved_fp(tsk);
|
|
frame.pc = thread_saved_pc(tsk);
|
|
}
|
|
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
|
|
frame.graph = tsk->curr_ret_stack;
|
|
#endif
|
|
|
|
skip = !!regs;
|
|
printk("Call trace:\n");
|
|
while (1) {
|
|
unsigned long stack;
|
|
int ret;
|
|
|
|
/* skip until specified stack frame */
|
|
if (!skip) {
|
|
dump_backtrace_entry(frame.pc);
|
|
} else if (frame.fp == regs->regs[29]) {
|
|
skip = 0;
|
|
/*
|
|
* Mostly, this is the case where this function is
|
|
* called in panic/abort. As exception handler's
|
|
* stack frame does not contain the corresponding pc
|
|
* at which an exception has taken place, use regs->pc
|
|
* instead.
|
|
*/
|
|
dump_backtrace_entry(regs->pc);
|
|
}
|
|
ret = unwind_frame(tsk, &frame);
|
|
if (ret < 0)
|
|
break;
|
|
if (in_entry_text(frame.pc)) {
|
|
stack = frame.fp - offsetof(struct pt_regs, stackframe);
|
|
|
|
if (on_accessible_stack(tsk, stack))
|
|
dump_mem("", "Exception stack", stack,
|
|
stack + sizeof(struct pt_regs));
|
|
}
|
|
}
|
|
|
|
put_task_stack(tsk);
|
|
}
|
|
|
|
void show_stack(struct task_struct *tsk, unsigned long *sp)
|
|
{
|
|
dump_backtrace(NULL, tsk);
|
|
barrier();
|
|
}
|
|
|
|
#ifdef CONFIG_PREEMPT
|
|
#define S_PREEMPT " PREEMPT"
|
|
#else
|
|
#define S_PREEMPT ""
|
|
#endif
|
|
#define S_SMP " SMP"
|
|
|
|
static int __die(const char *str, int err, struct pt_regs *regs)
|
|
{
|
|
struct task_struct *tsk = current;
|
|
static int die_counter;
|
|
int ret;
|
|
|
|
pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP "\n",
|
|
str, err, ++die_counter);
|
|
|
|
/* trap and error numbers are mostly meaningless on ARM */
|
|
ret = notify_die(DIE_OOPS, str, regs, err, 0, SIGSEGV);
|
|
if (ret == NOTIFY_STOP)
|
|
return ret;
|
|
|
|
print_modules();
|
|
__show_regs(regs);
|
|
pr_emerg("Process %.*s (pid: %d, stack limit = 0x%p)\n",
|
|
TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk),
|
|
end_of_stack(tsk));
|
|
|
|
if (!user_mode(regs)) {
|
|
dump_backtrace(regs, tsk);
|
|
dump_instr(KERN_EMERG, regs);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
static DEFINE_RAW_SPINLOCK(die_lock);
|
|
|
|
/*
|
|
* This function is protected against re-entrancy.
|
|
*/
|
|
void die(const char *str, struct pt_regs *regs, int err)
|
|
{
|
|
int ret;
|
|
unsigned long flags;
|
|
|
|
raw_spin_lock_irqsave(&die_lock, flags);
|
|
|
|
oops_enter();
|
|
|
|
console_verbose();
|
|
bust_spinlocks(1);
|
|
ret = __die(str, err, regs);
|
|
|
|
if (regs && kexec_should_crash(current))
|
|
crash_kexec(regs);
|
|
|
|
bust_spinlocks(0);
|
|
add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
|
|
oops_exit();
|
|
|
|
if (in_interrupt())
|
|
panic("Fatal exception in interrupt");
|
|
if (panic_on_oops)
|
|
panic("Fatal exception");
|
|
|
|
raw_spin_unlock_irqrestore(&die_lock, flags);
|
|
|
|
if (ret != NOTIFY_STOP)
|
|
do_exit(SIGSEGV);
|
|
}
|
|
|
|
void arm64_notify_die(const char *str, struct pt_regs *regs,
|
|
struct siginfo *info, int err)
|
|
{
|
|
if (user_mode(regs)) {
|
|
current->thread.fault_address = 0;
|
|
current->thread.fault_code = err;
|
|
force_sig_info(info->si_signo, info, current);
|
|
} else {
|
|
die(str, regs, err);
|
|
}
|
|
}
|
|
|
|
static LIST_HEAD(undef_hook);
|
|
static DEFINE_RAW_SPINLOCK(undef_lock);
|
|
|
|
void register_undef_hook(struct undef_hook *hook)
|
|
{
|
|
unsigned long flags;
|
|
|
|
raw_spin_lock_irqsave(&undef_lock, flags);
|
|
list_add(&hook->node, &undef_hook);
|
|
raw_spin_unlock_irqrestore(&undef_lock, flags);
|
|
}
|
|
|
|
void unregister_undef_hook(struct undef_hook *hook)
|
|
{
|
|
unsigned long flags;
|
|
|
|
raw_spin_lock_irqsave(&undef_lock, flags);
|
|
list_del(&hook->node);
|
|
raw_spin_unlock_irqrestore(&undef_lock, flags);
|
|
}
|
|
|
|
static int call_undef_hook(struct pt_regs *regs)
|
|
{
|
|
struct undef_hook *hook;
|
|
unsigned long flags;
|
|
u32 instr;
|
|
int (*fn)(struct pt_regs *regs, u32 instr) = NULL;
|
|
void __user *pc = (void __user *)instruction_pointer(regs);
|
|
|
|
if (!user_mode(regs))
|
|
return 1;
|
|
|
|
if (compat_thumb_mode(regs)) {
|
|
/* 16-bit Thumb instruction */
|
|
__le16 instr_le;
|
|
if (get_user(instr_le, (__le16 __user *)pc))
|
|
goto exit;
|
|
instr = le16_to_cpu(instr_le);
|
|
if (aarch32_insn_is_wide(instr)) {
|
|
u32 instr2;
|
|
|
|
if (get_user(instr_le, (__le16 __user *)(pc + 2)))
|
|
goto exit;
|
|
instr2 = le16_to_cpu(instr_le);
|
|
instr = (instr << 16) | instr2;
|
|
}
|
|
} else {
|
|
/* 32-bit ARM instruction */
|
|
__le32 instr_le;
|
|
if (get_user(instr_le, (__le32 __user *)pc))
|
|
goto exit;
|
|
instr = le32_to_cpu(instr_le);
|
|
}
|
|
|
|
raw_spin_lock_irqsave(&undef_lock, flags);
|
|
list_for_each_entry(hook, &undef_hook, node)
|
|
if ((instr & hook->instr_mask) == hook->instr_val &&
|
|
(regs->pstate & hook->pstate_mask) == hook->pstate_val)
|
|
fn = hook->fn;
|
|
|
|
raw_spin_unlock_irqrestore(&undef_lock, flags);
|
|
exit:
|
|
return fn ? fn(regs, instr) : 1;
|
|
}
|
|
|
|
static void force_signal_inject(int signal, int code, struct pt_regs *regs,
|
|
unsigned long address)
|
|
{
|
|
siginfo_t info;
|
|
void __user *pc = (void __user *)instruction_pointer(regs);
|
|
const char *desc;
|
|
|
|
switch (signal) {
|
|
case SIGILL:
|
|
desc = "undefined instruction";
|
|
break;
|
|
case SIGSEGV:
|
|
desc = "illegal memory access";
|
|
break;
|
|
default:
|
|
desc = "bad mode";
|
|
break;
|
|
}
|
|
|
|
if (unhandled_signal(current, signal) &&
|
|
show_unhandled_signals_ratelimited()) {
|
|
pr_info("%s[%d]: %s: pc=%p\n",
|
|
current->comm, task_pid_nr(current), desc, pc);
|
|
dump_instr(KERN_INFO, regs);
|
|
}
|
|
|
|
info.si_signo = signal;
|
|
info.si_errno = 0;
|
|
info.si_code = code;
|
|
info.si_addr = pc;
|
|
|
|
arm64_notify_die(desc, regs, &info, 0);
|
|
}
|
|
|
|
/*
|
|
* Set up process info to signal segmentation fault - called on access error.
|
|
*/
|
|
void arm64_notify_segfault(struct pt_regs *regs, unsigned long addr)
|
|
{
|
|
int code;
|
|
|
|
down_read(¤t->mm->mmap_sem);
|
|
if (find_vma(current->mm, addr) == NULL)
|
|
code = SEGV_MAPERR;
|
|
else
|
|
code = SEGV_ACCERR;
|
|
up_read(¤t->mm->mmap_sem);
|
|
|
|
force_signal_inject(SIGSEGV, code, regs, addr);
|
|
}
|
|
|
|
asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
|
|
{
|
|
/* check for AArch32 breakpoint instructions */
|
|
if (!aarch32_break_handler(regs))
|
|
return;
|
|
|
|
if (call_undef_hook(regs) == 0)
|
|
return;
|
|
|
|
force_signal_inject(SIGILL, ILL_ILLOPC, regs, 0);
|
|
}
|
|
|
|
int cpu_enable_cache_maint_trap(void *__unused)
|
|
{
|
|
config_sctlr_el1(SCTLR_EL1_UCI, 0);
|
|
return 0;
|
|
}
|
|
|
|
#define __user_cache_maint(insn, address, res) \
|
|
if (address >= user_addr_max()) { \
|
|
res = -EFAULT; \
|
|
} else { \
|
|
uaccess_ttbr0_enable(); \
|
|
asm volatile ( \
|
|
"1: " insn ", %1\n" \
|
|
" mov %w0, #0\n" \
|
|
"2:\n" \
|
|
" .pushsection .fixup,\"ax\"\n" \
|
|
" .align 2\n" \
|
|
"3: mov %w0, %w2\n" \
|
|
" b 2b\n" \
|
|
" .popsection\n" \
|
|
_ASM_EXTABLE(1b, 3b) \
|
|
: "=r" (res) \
|
|
: "r" (address), "i" (-EFAULT)); \
|
|
uaccess_ttbr0_disable(); \
|
|
}
|
|
|
|
static void user_cache_maint_handler(unsigned int esr, struct pt_regs *regs)
|
|
{
|
|
unsigned long address;
|
|
int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
|
|
int crm = (esr & ESR_ELx_SYS64_ISS_CRM_MASK) >> ESR_ELx_SYS64_ISS_CRM_SHIFT;
|
|
int ret = 0;
|
|
|
|
address = untagged_addr(pt_regs_read_reg(regs, rt));
|
|
|
|
switch (crm) {
|
|
case ESR_ELx_SYS64_ISS_CRM_DC_CVAU: /* DC CVAU, gets promoted */
|
|
__user_cache_maint("dc civac", address, ret);
|
|
break;
|
|
case ESR_ELx_SYS64_ISS_CRM_DC_CVAC: /* DC CVAC, gets promoted */
|
|
__user_cache_maint("dc civac", address, ret);
|
|
break;
|
|
case ESR_ELx_SYS64_ISS_CRM_DC_CIVAC: /* DC CIVAC */
|
|
__user_cache_maint("dc civac", address, ret);
|
|
break;
|
|
case ESR_ELx_SYS64_ISS_CRM_IC_IVAU: /* IC IVAU */
|
|
__user_cache_maint("ic ivau", address, ret);
|
|
break;
|
|
default:
|
|
force_signal_inject(SIGILL, ILL_ILLOPC, regs, 0);
|
|
return;
|
|
}
|
|
|
|
if (ret)
|
|
arm64_notify_segfault(regs, address);
|
|
else
|
|
regs->pc += 4;
|
|
}
|
|
|
|
static void ctr_read_handler(unsigned int esr, struct pt_regs *regs)
|
|
{
|
|
int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
|
|
unsigned long val = arm64_ftr_reg_user_value(&arm64_ftr_reg_ctrel0);
|
|
|
|
pt_regs_write_reg(regs, rt, val);
|
|
|
|
regs->pc += 4;
|
|
}
|
|
|
|
static void cntvct_read_handler(unsigned int esr, struct pt_regs *regs)
|
|
{
|
|
int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
|
|
|
|
pt_regs_write_reg(regs, rt, arch_counter_get_cntvct());
|
|
regs->pc += 4;
|
|
}
|
|
|
|
static void cntfrq_read_handler(unsigned int esr, struct pt_regs *regs)
|
|
{
|
|
int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
|
|
|
|
pt_regs_write_reg(regs, rt, read_sysreg(cntfrq_el0));
|
|
regs->pc += 4;
|
|
}
|
|
|
|
struct sys64_hook {
|
|
unsigned int esr_mask;
|
|
unsigned int esr_val;
|
|
void (*handler)(unsigned int esr, struct pt_regs *regs);
|
|
};
|
|
|
|
static struct sys64_hook sys64_hooks[] = {
|
|
{
|
|
.esr_mask = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_MASK,
|
|
.esr_val = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_VAL,
|
|
.handler = user_cache_maint_handler,
|
|
},
|
|
{
|
|
/* Trap read access to CTR_EL0 */
|
|
.esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
|
|
.esr_val = ESR_ELx_SYS64_ISS_SYS_CTR_READ,
|
|
.handler = ctr_read_handler,
|
|
},
|
|
{
|
|
/* Trap read access to CNTVCT_EL0 */
|
|
.esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
|
|
.esr_val = ESR_ELx_SYS64_ISS_SYS_CNTVCT,
|
|
.handler = cntvct_read_handler,
|
|
},
|
|
{
|
|
/* Trap read access to CNTFRQ_EL0 */
|
|
.esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
|
|
.esr_val = ESR_ELx_SYS64_ISS_SYS_CNTFRQ,
|
|
.handler = cntfrq_read_handler,
|
|
},
|
|
{},
|
|
};
|
|
|
|
asmlinkage void __exception do_sysinstr(unsigned int esr, struct pt_regs *regs)
|
|
{
|
|
struct sys64_hook *hook;
|
|
|
|
for (hook = sys64_hooks; hook->handler; hook++)
|
|
if ((hook->esr_mask & esr) == hook->esr_val) {
|
|
hook->handler(esr, regs);
|
|
return;
|
|
}
|
|
|
|
/*
|
|
* New SYS instructions may previously have been undefined at EL0. Fall
|
|
* back to our usual undefined instruction handler so that we handle
|
|
* these consistently.
|
|
*/
|
|
do_undefinstr(regs);
|
|
}
|
|
|
|
long compat_arm_syscall(struct pt_regs *regs);
|
|
|
|
asmlinkage long do_ni_syscall(struct pt_regs *regs)
|
|
{
|
|
#ifdef CONFIG_COMPAT
|
|
long ret;
|
|
if (is_compat_task()) {
|
|
ret = compat_arm_syscall(regs);
|
|
if (ret != -ENOSYS)
|
|
return ret;
|
|
}
|
|
#endif
|
|
|
|
if (show_unhandled_signals_ratelimited()) {
|
|
pr_info("%s[%d]: syscall %d\n", current->comm,
|
|
task_pid_nr(current), (int)regs->syscallno);
|
|
dump_instr("", regs);
|
|
if (user_mode(regs))
|
|
__show_regs(regs);
|
|
}
|
|
|
|
return sys_ni_syscall();
|
|
}
|
|
|
|
static const char *esr_class_str[] = {
|
|
[0 ... ESR_ELx_EC_MAX] = "UNRECOGNIZED EC",
|
|
[ESR_ELx_EC_UNKNOWN] = "Unknown/Uncategorized",
|
|
[ESR_ELx_EC_WFx] = "WFI/WFE",
|
|
[ESR_ELx_EC_CP15_32] = "CP15 MCR/MRC",
|
|
[ESR_ELx_EC_CP15_64] = "CP15 MCRR/MRRC",
|
|
[ESR_ELx_EC_CP14_MR] = "CP14 MCR/MRC",
|
|
[ESR_ELx_EC_CP14_LS] = "CP14 LDC/STC",
|
|
[ESR_ELx_EC_FP_ASIMD] = "ASIMD",
|
|
[ESR_ELx_EC_CP10_ID] = "CP10 MRC/VMRS",
|
|
[ESR_ELx_EC_CP14_64] = "CP14 MCRR/MRRC",
|
|
[ESR_ELx_EC_ILL] = "PSTATE.IL",
|
|
[ESR_ELx_EC_SVC32] = "SVC (AArch32)",
|
|
[ESR_ELx_EC_HVC32] = "HVC (AArch32)",
|
|
[ESR_ELx_EC_SMC32] = "SMC (AArch32)",
|
|
[ESR_ELx_EC_SVC64] = "SVC (AArch64)",
|
|
[ESR_ELx_EC_HVC64] = "HVC (AArch64)",
|
|
[ESR_ELx_EC_SMC64] = "SMC (AArch64)",
|
|
[ESR_ELx_EC_SYS64] = "MSR/MRS (AArch64)",
|
|
[ESR_ELx_EC_IMP_DEF] = "EL3 IMP DEF",
|
|
[ESR_ELx_EC_IABT_LOW] = "IABT (lower EL)",
|
|
[ESR_ELx_EC_IABT_CUR] = "IABT (current EL)",
|
|
[ESR_ELx_EC_PC_ALIGN] = "PC Alignment",
|
|
[ESR_ELx_EC_DABT_LOW] = "DABT (lower EL)",
|
|
[ESR_ELx_EC_DABT_CUR] = "DABT (current EL)",
|
|
[ESR_ELx_EC_SP_ALIGN] = "SP Alignment",
|
|
[ESR_ELx_EC_FP_EXC32] = "FP (AArch32)",
|
|
[ESR_ELx_EC_FP_EXC64] = "FP (AArch64)",
|
|
[ESR_ELx_EC_SERROR] = "SError",
|
|
[ESR_ELx_EC_BREAKPT_LOW] = "Breakpoint (lower EL)",
|
|
[ESR_ELx_EC_BREAKPT_CUR] = "Breakpoint (current EL)",
|
|
[ESR_ELx_EC_SOFTSTP_LOW] = "Software Step (lower EL)",
|
|
[ESR_ELx_EC_SOFTSTP_CUR] = "Software Step (current EL)",
|
|
[ESR_ELx_EC_WATCHPT_LOW] = "Watchpoint (lower EL)",
|
|
[ESR_ELx_EC_WATCHPT_CUR] = "Watchpoint (current EL)",
|
|
[ESR_ELx_EC_BKPT32] = "BKPT (AArch32)",
|
|
[ESR_ELx_EC_VECTOR32] = "Vector catch (AArch32)",
|
|
[ESR_ELx_EC_BRK64] = "BRK (AArch64)",
|
|
};
|
|
|
|
const char *esr_get_class_string(u32 esr)
|
|
{
|
|
return esr_class_str[ESR_ELx_EC(esr)];
|
|
}
|
|
|
|
/*
|
|
* bad_mode handles the impossible case in the exception vector. This is always
|
|
* fatal.
|
|
*/
|
|
asmlinkage void bad_mode(struct pt_regs *regs, int reason, unsigned int esr)
|
|
{
|
|
console_verbose();
|
|
|
|
pr_crit("Bad mode in %s handler detected on CPU%d, code 0x%08x -- %s\n",
|
|
handler[reason], smp_processor_id(), esr,
|
|
esr_get_class_string(esr));
|
|
|
|
die("Oops - bad mode", regs, 0);
|
|
local_irq_disable();
|
|
panic("bad mode");
|
|
}
|
|
|
|
/*
|
|
* bad_el0_sync handles unexpected, but potentially recoverable synchronous
|
|
* exceptions taken from EL0. Unlike bad_mode, this returns.
|
|
*/
|
|
asmlinkage void bad_el0_sync(struct pt_regs *regs, int reason, unsigned int esr)
|
|
{
|
|
siginfo_t info;
|
|
void __user *pc = (void __user *)instruction_pointer(regs);
|
|
console_verbose();
|
|
|
|
pr_crit("Bad EL0 synchronous exception detected on CPU%d, code 0x%08x -- %s\n",
|
|
smp_processor_id(), esr, esr_get_class_string(esr));
|
|
__show_regs(regs);
|
|
|
|
info.si_signo = SIGILL;
|
|
info.si_errno = 0;
|
|
info.si_code = ILL_ILLOPC;
|
|
info.si_addr = pc;
|
|
|
|
current->thread.fault_address = 0;
|
|
current->thread.fault_code = 0;
|
|
|
|
force_sig_info(info.si_signo, &info, current);
|
|
}
|
|
|
|
#ifdef CONFIG_VMAP_STACK
|
|
|
|
DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack)
|
|
__aligned(16);
|
|
|
|
asmlinkage void handle_bad_stack(struct pt_regs *regs)
|
|
{
|
|
unsigned long tsk_stk = (unsigned long)current->stack;
|
|
unsigned long irq_stk = (unsigned long)this_cpu_read(irq_stack_ptr);
|
|
unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
|
|
unsigned int esr = read_sysreg(esr_el1);
|
|
unsigned long far = read_sysreg(far_el1);
|
|
|
|
console_verbose();
|
|
pr_emerg("Insufficient stack space to handle exception!");
|
|
|
|
pr_emerg("ESR: 0x%08x -- %s\n", esr, esr_get_class_string(esr));
|
|
pr_emerg("FAR: 0x%016lx\n", far);
|
|
|
|
pr_emerg("Task stack: [0x%016lx..0x%016lx]\n",
|
|
tsk_stk, tsk_stk + THREAD_SIZE);
|
|
pr_emerg("IRQ stack: [0x%016lx..0x%016lx]\n",
|
|
irq_stk, irq_stk + THREAD_SIZE);
|
|
pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
|
|
ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE);
|
|
|
|
__show_regs(regs);
|
|
|
|
/*
|
|
* We use nmi_panic to limit the potential for recusive overflows, and
|
|
* to get a better stack trace.
|
|
*/
|
|
nmi_panic(NULL, "kernel stack overflow");
|
|
cpu_park_loop();
|
|
}
|
|
#endif
|
|
|
|
void __pte_error(const char *file, int line, unsigned long val)
|
|
{
|
|
pr_err("%s:%d: bad pte %016lx.\n", file, line, val);
|
|
}
|
|
|
|
void __pmd_error(const char *file, int line, unsigned long val)
|
|
{
|
|
pr_err("%s:%d: bad pmd %016lx.\n", file, line, val);
|
|
}
|
|
|
|
void __pud_error(const char *file, int line, unsigned long val)
|
|
{
|
|
pr_err("%s:%d: bad pud %016lx.\n", file, line, val);
|
|
}
|
|
|
|
void __pgd_error(const char *file, int line, unsigned long val)
|
|
{
|
|
pr_err("%s:%d: bad pgd %016lx.\n", file, line, val);
|
|
}
|
|
|
|
/* GENERIC_BUG traps */
|
|
|
|
int is_valid_bugaddr(unsigned long addr)
|
|
{
|
|
/*
|
|
* bug_handler() only called for BRK #BUG_BRK_IMM.
|
|
* So the answer is trivial -- any spurious instances with no
|
|
* bug table entry will be rejected by report_bug() and passed
|
|
* back to the debug-monitors code and handled as a fatal
|
|
* unexpected debug exception.
|
|
*/
|
|
return 1;
|
|
}
|
|
|
|
static int bug_handler(struct pt_regs *regs, unsigned int esr)
|
|
{
|
|
if (user_mode(regs))
|
|
return DBG_HOOK_ERROR;
|
|
|
|
switch (report_bug(regs->pc, regs)) {
|
|
case BUG_TRAP_TYPE_BUG:
|
|
die("Oops - BUG", regs, 0);
|
|
break;
|
|
|
|
case BUG_TRAP_TYPE_WARN:
|
|
break;
|
|
|
|
default:
|
|
/* unknown/unrecognised bug trap type */
|
|
return DBG_HOOK_ERROR;
|
|
}
|
|
|
|
/* If thread survives, skip over the BUG instruction and continue: */
|
|
regs->pc += AARCH64_INSN_SIZE; /* skip BRK and resume */
|
|
return DBG_HOOK_HANDLED;
|
|
}
|
|
|
|
static struct break_hook bug_break_hook = {
|
|
.esr_val = 0xf2000000 | BUG_BRK_IMM,
|
|
.esr_mask = 0xffffffff,
|
|
.fn = bug_handler,
|
|
};
|
|
|
|
/*
|
|
* Initial handler for AArch64 BRK exceptions
|
|
* This handler only used until debug_traps_init().
|
|
*/
|
|
int __init early_brk64(unsigned long addr, unsigned int esr,
|
|
struct pt_regs *regs)
|
|
{
|
|
return bug_handler(regs, esr) != DBG_HOOK_HANDLED;
|
|
}
|
|
|
|
/* This registration must happen early, before debug_traps_init(). */
|
|
void __init trap_init(void)
|
|
{
|
|
register_break_hook(&bug_break_hook);
|
|
}
|