Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (C) 1991, 1992 Linus Torvalds |
Alexander van Heukelum | a8c1be9 | 2008-07-02 01:29:44 +0200 | [diff] [blame] | 3 | * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * Pentium III FXSR, SSE support |
| 6 | * Gareth Hughes <gareth@valinux.com>, May 2000 |
| 7 | */ |
| 8 | |
| 9 | /* |
Alexander van Heukelum | c1d518c | 2008-10-03 23:17:11 +0200 | [diff] [blame] | 10 | * Handle hardware traps and faults. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | */ |
Joe Perches | c767a54 | 2012-05-21 19:50:07 -0700 | [diff] [blame] | 12 | |
| 13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 14 | |
Frederic Weisbecker | 56dd947 | 2013-02-24 00:23:25 +0100 | [diff] [blame] | 15 | #include <linux/context_tracking.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/kallsyms.h> |
Ingo Molnar | b596440 | 2008-02-26 11:15:50 +0100 | [diff] [blame] | 18 | #include <linux/spinlock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/kprobes.h> |
Andrew Morton | 1e2af92 | 2006-09-27 01:51:15 -0700 | [diff] [blame] | 20 | #include <linux/uaccess.h> |
Ingo Molnar | b596440 | 2008-02-26 11:15:50 +0100 | [diff] [blame] | 21 | #include <linux/kdebug.h> |
Jason Wessel | f503b5a | 2010-05-20 21:04:25 -0500 | [diff] [blame] | 22 | #include <linux/kgdb.h> |
Ingo Molnar | b596440 | 2008-02-26 11:15:50 +0100 | [diff] [blame] | 23 | #include <linux/kernel.h> |
Paul Gortmaker | 186f436 | 2016-07-13 20:18:56 -0400 | [diff] [blame] | 24 | #include <linux/export.h> |
Ingo Molnar | b596440 | 2008-02-26 11:15:50 +0100 | [diff] [blame] | 25 | #include <linux/ptrace.h> |
Oleg Nesterov | b02ef20 | 2014-05-12 18:24:45 +0200 | [diff] [blame] | 26 | #include <linux/uprobes.h> |
Ingo Molnar | b596440 | 2008-02-26 11:15:50 +0100 | [diff] [blame] | 27 | #include <linux/string.h> |
Ingo Molnar | b596440 | 2008-02-26 11:15:50 +0100 | [diff] [blame] | 28 | #include <linux/delay.h> |
| 29 | #include <linux/errno.h> |
| 30 | #include <linux/kexec.h> |
| 31 | #include <linux/sched.h> |
Ingo Molnar | 68db0cf | 2017-02-08 18:51:37 +0100 | [diff] [blame] | 32 | #include <linux/sched/task_stack.h> |
Ingo Molnar | b596440 | 2008-02-26 11:15:50 +0100 | [diff] [blame] | 33 | #include <linux/timer.h> |
| 34 | #include <linux/init.h> |
Jeremy Fitzhardinge | 91768d6 | 2006-12-08 02:36:21 -0800 | [diff] [blame] | 35 | #include <linux/bug.h> |
Ingo Molnar | b596440 | 2008-02-26 11:15:50 +0100 | [diff] [blame] | 36 | #include <linux/nmi.h> |
| 37 | #include <linux/mm.h> |
Alexander van Heukelum | c1d518c | 2008-10-03 23:17:11 +0200 | [diff] [blame] | 38 | #include <linux/smp.h> |
| 39 | #include <linux/io.h> |
Peter Zijlstra | 0d00449 | 2020-02-19 09:46:43 +0100 | [diff] [blame] | 40 | #include <linux/hardirq.h> |
| 41 | #include <linux/atomic.h> |
| 42 | |
Ingo Molnar | b596440 | 2008-02-26 11:15:50 +0100 | [diff] [blame] | 43 | #include <asm/stacktrace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <asm/processor.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <asm/debugreg.h> |
Andy Lutomirski | 35de5b0 | 2016-04-26 12:23:24 -0700 | [diff] [blame] | 46 | #include <asm/text-patching.h> |
Steven Rostedt | 08d636b | 2011-08-16 09:57:10 -0400 | [diff] [blame] | 47 | #include <asm/ftrace.h> |
Alexander van Heukelum | c1d518c | 2008-10-03 23:17:11 +0200 | [diff] [blame] | 48 | #include <asm/traps.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <asm/desc.h> |
Ingo Molnar | 78f7f1e | 2015-04-24 02:54:44 +0200 | [diff] [blame] | 50 | #include <asm/fpu/internal.h> |
Peter Zijlstra (Intel) | 6650cdd | 2020-01-26 12:05:35 -0800 | [diff] [blame] | 51 | #include <asm/cpu.h> |
Thomas Gleixner | ed1bbc4 | 2017-12-20 18:28:54 +0100 | [diff] [blame] | 52 | #include <asm/cpu_entry_area.h> |
Hidetoshi Seto | 9e55e44 | 2009-06-15 17:22:15 +0900 | [diff] [blame] | 53 | #include <asm/mce.h> |
Kees Cook | 4eefbe7 | 2013-04-10 12:24:22 -0700 | [diff] [blame] | 54 | #include <asm/fixmap.h> |
Ingo Molnar | 1164dd0 | 2009-01-28 19:34:09 +0100 | [diff] [blame] | 55 | #include <asm/mach_traps.h> |
Jiri Kosina | 17f4157 | 2013-07-23 10:09:28 +0200 | [diff] [blame] | 56 | #include <asm/alternative.h> |
Dave Hansen | a84eeaa | 2015-06-07 11:37:01 -0700 | [diff] [blame] | 57 | #include <asm/fpu/xstate.h> |
Brian Gerst | ba3e127 | 2015-07-29 01:41:21 -0400 | [diff] [blame] | 58 | #include <asm/vm86.h> |
Ricardo Neri | 6fc9dc8 | 2017-11-05 18:27:55 -0800 | [diff] [blame] | 59 | #include <asm/umip.h> |
Jann Horn | 59c1dcb | 2019-12-19 00:11:48 +0100 | [diff] [blame] | 60 | #include <asm/insn.h> |
| 61 | #include <asm/insn-eval.h> |
Alexander van Heukelum | c1d518c | 2008-10-03 23:17:11 +0200 | [diff] [blame] | 62 | |
Alexander van Heukelum | 081f75b | 2008-10-03 22:00:39 +0200 | [diff] [blame] | 63 | #ifdef CONFIG_X86_64 |
Thomas Gleixner | 428cf90 | 2009-08-20 10:35:46 +0200 | [diff] [blame] | 64 | #include <asm/x86_init.h> |
Alexander van Heukelum | 081f75b | 2008-10-03 22:00:39 +0200 | [diff] [blame] | 65 | #include <asm/pgalloc.h> |
| 66 | #include <asm/proto.h> |
Alexander van Heukelum | 081f75b | 2008-10-03 22:00:39 +0200 | [diff] [blame] | 67 | #else |
Alexander van Heukelum | c1d518c | 2008-10-03 23:17:11 +0200 | [diff] [blame] | 68 | #include <asm/processor-flags.h> |
Ingo Molnar | 8e6dafd | 2009-02-23 00:34:39 +0100 | [diff] [blame] | 69 | #include <asm/setup.h> |
Ingo Molnar | b2502b4 | 2015-06-08 08:42:03 +0200 | [diff] [blame] | 70 | #include <asm/proto.h> |
Alexander van Heukelum | 081f75b | 2008-10-03 22:00:39 +0200 | [diff] [blame] | 71 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
Thomas Gleixner | 7854f82 | 2017-09-13 23:29:26 +0200 | [diff] [blame] | 73 | DECLARE_BITMAP(system_vectors, NR_VECTORS); |
Yinghai Lu | b77b881 | 2008-12-19 15:23:44 -0800 | [diff] [blame] | 74 | |
Alexander Kuleshov | d99e1bd | 2016-01-25 20:41:46 +0100 | [diff] [blame] | 75 | static inline void cond_local_irq_enable(struct pt_regs *regs) |
Alexander van Heukelum | 762db43 | 2008-09-09 21:55:55 +0200 | [diff] [blame] | 76 | { |
| 77 | if (regs->flags & X86_EFLAGS_IF) |
| 78 | local_irq_enable(); |
| 79 | } |
| 80 | |
Alexander Kuleshov | d99e1bd | 2016-01-25 20:41:46 +0100 | [diff] [blame] | 81 | static inline void cond_local_irq_disable(struct pt_regs *regs) |
Thomas Gleixner | be71661 | 2009-01-13 23:36:34 +0100 | [diff] [blame] | 82 | { |
| 83 | if (regs->flags & X86_EFLAGS_IF) |
| 84 | local_irq_disable(); |
| 85 | } |
| 86 | |
Peter Zijlstra | 9a93848 | 2017-02-02 14:43:51 +0100 | [diff] [blame] | 87 | int is_valid_bugaddr(unsigned long addr) |
| 88 | { |
| 89 | unsigned short ud; |
| 90 | |
| 91 | if (addr < TASK_SIZE_MAX) |
| 92 | return 0; |
| 93 | |
| 94 | if (probe_kernel_address((unsigned short *)addr, ud)) |
| 95 | return 0; |
| 96 | |
| 97 | return ud == INSN_UD0 || ud == INSN_UD2; |
| 98 | } |
| 99 | |
Peter Zijlstra | 8a524f8 | 2017-06-12 13:52:46 +0200 | [diff] [blame] | 100 | int fixup_bug(struct pt_regs *regs, int trapnr) |
Peter Zijlstra | 9a93848 | 2017-02-02 14:43:51 +0100 | [diff] [blame] | 101 | { |
| 102 | if (trapnr != X86_TRAP_UD) |
| 103 | return 0; |
| 104 | |
| 105 | switch (report_bug(regs->ip, regs)) { |
| 106 | case BUG_TRAP_TYPE_NONE: |
| 107 | case BUG_TRAP_TYPE_BUG: |
| 108 | break; |
| 109 | |
| 110 | case BUG_TRAP_TYPE_WARN: |
Peter Zijlstra | 3b3a371 | 2018-02-09 13:16:59 +0100 | [diff] [blame] | 111 | regs->ip += LEN_UD2; |
Peter Zijlstra | 9a93848 | 2017-02-02 14:43:51 +0100 | [diff] [blame] | 112 | return 1; |
| 113 | } |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 118 | static nokprobe_inline int |
Eric W. Biederman | 79e21d6 | 2017-08-04 14:01:50 -0500 | [diff] [blame] | 119 | do_trap_no_signal(struct task_struct *tsk, int trapnr, const char *str, |
Frederic Weisbecker | c416ddf | 2012-09-25 14:51:19 +0200 | [diff] [blame] | 120 | struct pt_regs *regs, long error_code) |
| 121 | { |
Andy Lutomirski | d74ef11 | 2015-03-18 18:33:35 -0700 | [diff] [blame] | 122 | if (v8086_mode(regs)) { |
Frederic Weisbecker | c416ddf | 2012-09-25 14:51:19 +0200 | [diff] [blame] | 123 | /* |
| 124 | * Traps 0, 1, 3, 4, and 5 should be forwarded to vm86. |
| 125 | * On nmi (interrupt 2), do_trap should not be called. |
| 126 | */ |
| 127 | if (trapnr < X86_TRAP_UD) { |
| 128 | if (!handle_vm86_trap((struct kernel_vm86_regs *) regs, |
| 129 | error_code, trapnr)) |
| 130 | return 0; |
| 131 | } |
Eric W. Biederman | 79e21d6 | 2017-08-04 14:01:50 -0500 | [diff] [blame] | 132 | } else if (!user_mode(regs)) { |
Jann Horn | 81fd9c1 | 2018-08-28 22:14:19 +0200 | [diff] [blame] | 133 | if (fixup_exception(regs, trapnr, error_code, 0)) |
Peter Zijlstra | 9a93848 | 2017-02-02 14:43:51 +0100 | [diff] [blame] | 134 | return 0; |
| 135 | |
Peter Zijlstra | 9a93848 | 2017-02-02 14:43:51 +0100 | [diff] [blame] | 136 | tsk->thread.error_code = error_code; |
| 137 | tsk->thread.trap_nr = trapnr; |
| 138 | die(str, regs, error_code); |
Frederic Weisbecker | c416ddf | 2012-09-25 14:51:19 +0200 | [diff] [blame] | 139 | } |
| 140 | |
Ingo Molnar | b596440 | 2008-02-26 11:15:50 +0100 | [diff] [blame] | 141 | /* |
Srikar Dronamraju | 51e7dc7 | 2012-03-12 14:55:55 +0530 | [diff] [blame] | 142 | * We want error_code and trap_nr set for userspace faults and |
Ingo Molnar | b596440 | 2008-02-26 11:15:50 +0100 | [diff] [blame] | 143 | * kernelspace faults which result in die(), but not |
| 144 | * kernelspace faults which are fixed up. die() gives the |
| 145 | * process no chance to handle the signal and notice the |
| 146 | * kernel fault information, so that won't result in polluting |
| 147 | * the information about previously queued, but not yet |
Thomas Gleixner | be4c11a | 2020-02-25 23:16:25 +0100 | [diff] [blame] | 148 | * delivered, faults. See also exc_general_protection below. |
Ingo Molnar | b596440 | 2008-02-26 11:15:50 +0100 | [diff] [blame] | 149 | */ |
| 150 | tsk->thread.error_code = error_code; |
Srikar Dronamraju | 51e7dc7 | 2012-03-12 14:55:55 +0530 | [diff] [blame] | 151 | tsk->thread.trap_nr = trapnr; |
Ingo Molnar | b596440 | 2008-02-26 11:15:50 +0100 | [diff] [blame] | 152 | |
Jan Beulich | 20c0d2d | 2006-03-26 01:37:01 -0800 | [diff] [blame] | 153 | return -1; |
Alexander Nyberg | 6e274d1 | 2005-06-25 14:58:26 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Eric W. Biederman | 6ace109 | 2017-08-03 14:34:24 -0500 | [diff] [blame] | 156 | static void show_signal(struct task_struct *tsk, int signr, |
| 157 | const char *type, const char *desc, |
| 158 | struct pt_regs *regs, long error_code) |
| 159 | { |
Alexander van Heukelum | 081f75b | 2008-10-03 22:00:39 +0200 | [diff] [blame] | 160 | if (show_unhandled_signals && unhandled_signal(tsk, signr) && |
| 161 | printk_ratelimit()) { |
Eric W. Biederman | 6ace109 | 2017-08-03 14:34:24 -0500 | [diff] [blame] | 162 | pr_info("%s[%d] %s%s ip:%lx sp:%lx error:%lx", |
| 163 | tsk->comm, task_pid_nr(tsk), type, desc, |
Joe Perches | c767a54 | 2012-05-21 19:50:07 -0700 | [diff] [blame] | 164 | regs->ip, regs->sp, error_code); |
Markus Trippelsdorf | 1c99a68 | 2017-04-07 14:09:04 +0200 | [diff] [blame] | 165 | print_vma_addr(KERN_CONT " in ", regs->ip); |
Joe Perches | c767a54 | 2012-05-21 19:50:07 -0700 | [diff] [blame] | 166 | pr_cont("\n"); |
Alexander van Heukelum | 081f75b | 2008-10-03 22:00:39 +0200 | [diff] [blame] | 167 | } |
Eric W. Biederman | 6ace109 | 2017-08-03 14:34:24 -0500 | [diff] [blame] | 168 | } |
Alexander van Heukelum | 081f75b | 2008-10-03 22:00:39 +0200 | [diff] [blame] | 169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | static void |
| 171 | do_trap(int trapnr, int signr, char *str, struct pt_regs *regs, |
Eric W. Biederman | 164881b | 2018-04-16 14:29:39 -0500 | [diff] [blame] | 172 | long error_code, int sicode, void __user *addr) |
Prasanna S Panchamukhi | 3d97ae5 | 2005-09-06 15:19:27 -0700 | [diff] [blame] | 173 | { |
| 174 | struct task_struct *tsk = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
Alexander Nyberg | 4f339ec | 2005-06-25 14:58:27 -0700 | [diff] [blame] | 176 | if (!do_trap_no_signal(tsk, trapnr, str, regs, error_code)) |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 177 | return; |
Ingo Molnar | b596440 | 2008-02-26 11:15:50 +0100 | [diff] [blame] | 178 | |
Eric W. Biederman | 6ace109 | 2017-08-03 14:34:24 -0500 | [diff] [blame] | 179 | show_signal(tsk, signr, "trap ", str, regs, error_code); |
Ingo Molnar | b596440 | 2008-02-26 11:15:50 +0100 | [diff] [blame] | 180 | |
Eric W. Biederman | 164881b | 2018-04-16 14:29:39 -0500 | [diff] [blame] | 181 | if (!sicode) |
Eric W. Biederman | 3cf5d07 | 2019-05-23 10:17:27 -0500 | [diff] [blame] | 182 | force_sig(signr); |
Eric W. Biederman | 164881b | 2018-04-16 14:29:39 -0500 | [diff] [blame] | 183 | else |
Eric W. Biederman | 2e1661d2 | 2019-05-23 11:04:24 -0500 | [diff] [blame] | 184 | force_sig_fault(signr, sicode, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 186 | NOKPROBE_SYMBOL(do_trap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
Oleg Nesterov | dff0796 | 2014-05-07 17:21:34 +0200 | [diff] [blame] | 188 | static void do_error_trap(struct pt_regs *regs, long error_code, char *str, |
Eric W. Biederman | 164881b | 2018-04-16 14:29:39 -0500 | [diff] [blame] | 189 | unsigned long trapnr, int signr, int sicode, void __user *addr) |
Oleg Nesterov | dff0796 | 2014-05-07 17:21:34 +0200 | [diff] [blame] | 190 | { |
Linus Torvalds | 5778077 | 2015-09-01 08:40:25 -0700 | [diff] [blame] | 191 | RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU"); |
Andy Lutomirski | 02fdcd5 | 2015-07-03 12:44:24 -0700 | [diff] [blame] | 192 | |
Alexander Shishkin | b8347c21 | 2017-07-24 13:04:28 +0300 | [diff] [blame] | 193 | /* |
| 194 | * WARN*()s end up here; fix them up before we call the |
| 195 | * notifier chain. |
| 196 | */ |
| 197 | if (!user_mode(regs) && fixup_bug(regs, trapnr)) |
| 198 | return; |
| 199 | |
Oleg Nesterov | dff0796 | 2014-05-07 17:21:34 +0200 | [diff] [blame] | 200 | if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) != |
| 201 | NOTIFY_STOP) { |
Alexander Kuleshov | d99e1bd | 2016-01-25 20:41:46 +0100 | [diff] [blame] | 202 | cond_local_irq_enable(regs); |
Eric W. Biederman | 164881b | 2018-04-16 14:29:39 -0500 | [diff] [blame] | 203 | do_trap(trapnr, signr, str, regs, error_code, sicode, addr); |
Thomas Gleixner | ca4c6a9 | 2019-10-23 14:27:10 +0200 | [diff] [blame] | 204 | cond_local_irq_disable(regs); |
Oleg Nesterov | dff0796 | 2014-05-07 17:21:34 +0200 | [diff] [blame] | 205 | } |
Oleg Nesterov | dff0796 | 2014-05-07 17:21:34 +0200 | [diff] [blame] | 206 | } |
| 207 | |
Thomas Gleixner | 218e31b | 2020-02-25 23:16:13 +0100 | [diff] [blame] | 208 | /* |
| 209 | * Posix requires to provide the address of the faulting instruction for |
| 210 | * SIGILL (#UD) and SIGFPE (#DE) in the si_addr member of siginfo_t. |
| 211 | * |
| 212 | * This address is usually regs->ip, but when an uprobe moved the code out |
| 213 | * of line then regs->ip points to the XOL code which would confuse |
| 214 | * anything which analyzes the fault address vs. the unmodified binary. If |
| 215 | * a trap happened in XOL code then uprobe maps regs->ip back to the |
| 216 | * original instruction address. |
| 217 | */ |
| 218 | static __always_inline void __user *error_get_trap_addr(struct pt_regs *regs) |
| 219 | { |
| 220 | return (void __user *)uprobe_get_trap_addr(regs); |
| 221 | } |
| 222 | |
Thomas Gleixner | 9d06c40 | 2020-02-25 23:16:14 +0100 | [diff] [blame] | 223 | DEFINE_IDTENTRY(exc_divide_error) |
| 224 | { |
| 225 | do_error_trap(regs, 0, "divide_error", X86_TRAP_DE, SIGFPE, |
| 226 | FPE_INTDIV, error_get_trap_addr(regs)); |
| 227 | } |
| 228 | |
Thomas Gleixner | 4b6b911 | 2020-02-25 23:16:15 +0100 | [diff] [blame] | 229 | DEFINE_IDTENTRY(exc_overflow) |
| 230 | { |
| 231 | do_error_trap(regs, 0, "overflow", X86_TRAP_OF, SIGSEGV, 0, NULL); |
| 232 | } |
| 233 | |
Thomas Gleixner | 49893c5 | 2020-02-25 23:16:18 +0100 | [diff] [blame] | 234 | #ifdef CONFIG_X86_F00F_BUG |
| 235 | void handle_invalid_op(struct pt_regs *regs) |
| 236 | #else |
| 237 | static inline void handle_invalid_op(struct pt_regs *regs) |
| 238 | #endif |
| 239 | { |
| 240 | do_error_trap(regs, 0, "invalid opcode", X86_TRAP_UD, SIGILL, |
| 241 | ILL_ILLOPN, error_get_trap_addr(regs)); |
| 242 | } |
| 243 | |
| 244 | DEFINE_IDTENTRY(exc_invalid_op) |
| 245 | { |
| 246 | handle_invalid_op(regs); |
| 247 | } |
| 248 | |
Thomas Gleixner | f95658f | 2020-02-25 23:16:20 +0100 | [diff] [blame] | 249 | DEFINE_IDTENTRY(exc_coproc_segment_overrun) |
| 250 | { |
| 251 | do_error_trap(regs, 0, "coprocessor segment overrun", |
| 252 | X86_TRAP_OLD_MF, SIGFPE, 0, NULL); |
| 253 | } |
| 254 | |
Thomas Gleixner | 97b3d29 | 2020-02-25 23:16:22 +0100 | [diff] [blame] | 255 | DEFINE_IDTENTRY_ERRORCODE(exc_invalid_tss) |
| 256 | { |
| 257 | do_error_trap(regs, error_code, "invalid TSS", X86_TRAP_TS, SIGSEGV, |
| 258 | 0, NULL); |
| 259 | } |
| 260 | |
Thomas Gleixner | 99a3fb8 | 2020-02-25 23:16:23 +0100 | [diff] [blame] | 261 | DEFINE_IDTENTRY_ERRORCODE(exc_segment_not_present) |
| 262 | { |
| 263 | do_error_trap(regs, error_code, "segment not present", X86_TRAP_NP, |
| 264 | SIGBUS, 0, NULL); |
| 265 | } |
| 266 | |
Thomas Gleixner | fd9689b | 2020-02-25 23:16:24 +0100 | [diff] [blame] | 267 | DEFINE_IDTENTRY_ERRORCODE(exc_stack_segment) |
| 268 | { |
| 269 | do_error_trap(regs, error_code, "stack segment", X86_TRAP_SS, SIGBUS, |
| 270 | 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Thomas Gleixner | 436608b | 2020-02-25 23:16:28 +0100 | [diff] [blame] | 273 | DEFINE_IDTENTRY_ERRORCODE(exc_alignment_check) |
Peter Zijlstra (Intel) | 6650cdd | 2020-01-26 12:05:35 -0800 | [diff] [blame] | 274 | { |
| 275 | char *str = "alignment check"; |
| 276 | |
Peter Zijlstra (Intel) | 6650cdd | 2020-01-26 12:05:35 -0800 | [diff] [blame] | 277 | if (notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_AC, SIGBUS) == NOTIFY_STOP) |
| 278 | return; |
| 279 | |
| 280 | if (!user_mode(regs)) |
| 281 | die("Split lock detected\n", regs, error_code); |
| 282 | |
| 283 | local_irq_enable(); |
| 284 | |
| 285 | if (handle_user_split_lock(regs, error_code)) |
| 286 | return; |
| 287 | |
| 288 | do_trap(X86_TRAP_AC, SIGBUS, "alignment check", regs, |
| 289 | error_code, BUS_ADRALN, NULL); |
| 290 | } |
| 291 | |
Andy Lutomirski | e37e43a | 2016-08-11 02:35:23 -0700 | [diff] [blame] | 292 | #ifdef CONFIG_VMAP_STACK |
Andy Lutomirski | 6271cfd | 2016-08-30 17:27:57 -0700 | [diff] [blame] | 293 | __visible void __noreturn handle_stack_overflow(const char *message, |
| 294 | struct pt_regs *regs, |
| 295 | unsigned long fault_address) |
Andy Lutomirski | e37e43a | 2016-08-11 02:35:23 -0700 | [diff] [blame] | 296 | { |
| 297 | printk(KERN_EMERG "BUG: stack guard page was hit at %p (stack is %p..%p)\n", |
| 298 | (void *)fault_address, current->stack, |
| 299 | (char *)current->stack + THREAD_SIZE - 1); |
| 300 | die(message, regs, 0); |
| 301 | |
| 302 | /* Be absolutely certain we don't return. */ |
Rasmus Villemoes | 2022cce | 2018-10-27 00:20:04 +0200 | [diff] [blame] | 303 | panic("%s", message); |
Andy Lutomirski | e37e43a | 2016-08-11 02:35:23 -0700 | [diff] [blame] | 304 | } |
| 305 | #endif |
| 306 | |
Andy Lutomirski | 7d8d8cf | 2019-11-20 23:06:41 -0800 | [diff] [blame] | 307 | /* |
| 308 | * Runs on an IST stack for x86_64 and on a special task stack for x86_32. |
| 309 | * |
| 310 | * On x86_64, this is more or less a normal kernel entry. Notwithstanding the |
| 311 | * SDM's warnings about double faults being unrecoverable, returning works as |
| 312 | * expected. Presumably what the SDM actually means is that the CPU may get |
| 313 | * the register state wrong on entry, so returning could be a bad idea. |
| 314 | * |
| 315 | * Various CPU engineers have promised that double faults due to an IRET fault |
| 316 | * while the stack is read-only are, in fact, recoverable. |
| 317 | * |
| 318 | * On x86_32, this is entered through a task gate, and regs are synthesized |
| 319 | * from the TSS. Returning is, in principle, okay, but changes to regs will |
| 320 | * be lost. If, for some reason, we need to return to a context with modified |
| 321 | * regs, the shim code could be adjusted to synchronize the registers. |
Thomas Gleixner | c29c775 | 2020-02-25 23:33:31 +0100 | [diff] [blame] | 322 | * |
| 323 | * The 32bit #DF shim provides CR2 already as an argument. On 64bit it needs |
| 324 | * to be read before doing anything else. |
Andy Lutomirski | 7d8d8cf | 2019-11-20 23:06:41 -0800 | [diff] [blame] | 325 | */ |
Thomas Gleixner | c29c775 | 2020-02-25 23:33:31 +0100 | [diff] [blame] | 326 | DEFINE_IDTENTRY_DF(exc_double_fault) |
Alexander van Heukelum | 081f75b | 2008-10-03 22:00:39 +0200 | [diff] [blame] | 327 | { |
| 328 | static const char str[] = "double fault"; |
| 329 | struct task_struct *tsk = current; |
| 330 | |
Ingo Molnar | 7102cb0 | 2020-05-25 09:42:41 +0200 | [diff] [blame] | 331 | #ifdef CONFIG_VMAP_STACK |
Thomas Gleixner | c29c775 | 2020-02-25 23:33:31 +0100 | [diff] [blame] | 332 | unsigned long address = read_cr2(); |
| 333 | #endif |
| 334 | |
Andy Lutomirski | af726f2 | 2014-11-22 18:00:31 -0800 | [diff] [blame] | 335 | #ifdef CONFIG_X86_ESPFIX64 |
| 336 | extern unsigned char native_irq_return_iret[]; |
| 337 | |
| 338 | /* |
| 339 | * If IRET takes a non-IST fault on the espfix64 stack, then we |
Andy Lutomirski | 6d9256f | 2017-12-04 15:07:22 +0100 | [diff] [blame] | 340 | * end up promoting it to a doublefault. In that case, take |
| 341 | * advantage of the fact that we're not using the normal (TSS.sp0) |
| 342 | * stack right now. We can write a fake #GP(0) frame at TSS.sp0 |
| 343 | * and then modify our own IRET frame so that, when we return, |
| 344 | * we land directly at the #GP(0) vector with the stack already |
| 345 | * set up according to its expectations. |
| 346 | * |
| 347 | * The net result is that our #GP handler will think that we |
| 348 | * entered from usermode with the bad user context. |
Andy Lutomirski | 9592747 | 2014-11-19 17:41:09 -0800 | [diff] [blame] | 349 | * |
Peter Zijlstra | 0d00449 | 2020-02-19 09:46:43 +0100 | [diff] [blame] | 350 | * No need for nmi_enter() here because we don't use RCU. |
Andy Lutomirski | af726f2 | 2014-11-22 18:00:31 -0800 | [diff] [blame] | 351 | */ |
Andy Lutomirski | c739f93 | 2017-12-12 07:56:36 -0800 | [diff] [blame] | 352 | if (((long)regs->sp >> P4D_SHIFT) == ESPFIX_PGD_ENTRY && |
Andy Lutomirski | af726f2 | 2014-11-22 18:00:31 -0800 | [diff] [blame] | 353 | regs->cs == __KERNEL_CS && |
| 354 | regs->ip == (unsigned long)native_irq_return_iret) |
| 355 | { |
Andy Lutomirski | c482fee | 2017-12-04 15:07:29 +0100 | [diff] [blame] | 356 | struct pt_regs *gpregs = (struct pt_regs *)this_cpu_read(cpu_tss_rw.x86_tss.sp0) - 1; |
Peter Zijlstra | e966039 | 2020-02-20 13:17:27 +0100 | [diff] [blame] | 357 | unsigned long *p = (unsigned long *)regs->sp; |
Andy Lutomirski | af726f2 | 2014-11-22 18:00:31 -0800 | [diff] [blame] | 358 | |
Andy Lutomirski | 6d9256f | 2017-12-04 15:07:22 +0100 | [diff] [blame] | 359 | /* |
| 360 | * regs->sp points to the failing IRET frame on the |
| 361 | * ESPFIX64 stack. Copy it to the entry stack. This fills |
| 362 | * in gpregs->ss through gpregs->ip. |
| 363 | * |
| 364 | */ |
Peter Zijlstra | e966039 | 2020-02-20 13:17:27 +0100 | [diff] [blame] | 365 | gpregs->ip = p[0]; |
| 366 | gpregs->cs = p[1]; |
| 367 | gpregs->flags = p[2]; |
| 368 | gpregs->sp = p[3]; |
| 369 | gpregs->ss = p[4]; |
Andy Lutomirski | 6d9256f | 2017-12-04 15:07:22 +0100 | [diff] [blame] | 370 | gpregs->orig_ax = 0; /* Missing (lost) #GP error code */ |
| 371 | |
| 372 | /* |
| 373 | * Adjust our frame so that we return straight to the #GP |
| 374 | * vector with the expected RSP value. This is safe because |
| 375 | * we won't enable interupts or schedule before we invoke |
| 376 | * general_protection, so nothing will clobber the stack |
| 377 | * frame we just set up. |
Andy Lutomirski | bd7b1f7 | 2018-09-03 15:59:42 -0700 | [diff] [blame] | 378 | * |
| 379 | * We will enter general_protection with kernel GSBASE, |
| 380 | * which is what the stub expects, given that the faulting |
| 381 | * RIP will be the IRET instruction. |
Andy Lutomirski | 6d9256f | 2017-12-04 15:07:22 +0100 | [diff] [blame] | 382 | */ |
Thomas Gleixner | be4c11a | 2020-02-25 23:16:25 +0100 | [diff] [blame] | 383 | regs->ip = (unsigned long)asm_exc_general_protection; |
Andy Lutomirski | 6d9256f | 2017-12-04 15:07:22 +0100 | [diff] [blame] | 384 | regs->sp = (unsigned long)&gpregs->orig_ax; |
Andy Lutomirski | 9592747 | 2014-11-19 17:41:09 -0800 | [diff] [blame] | 385 | |
Andy Lutomirski | af726f2 | 2014-11-22 18:00:31 -0800 | [diff] [blame] | 386 | return; |
| 387 | } |
| 388 | #endif |
| 389 | |
Peter Zijlstra | 0d00449 | 2020-02-19 09:46:43 +0100 | [diff] [blame] | 390 | nmi_enter(); |
Thomas Gleixner | c29c775 | 2020-02-25 23:33:31 +0100 | [diff] [blame] | 391 | instrumentation_begin(); |
Kees Cook | c940826 | 2012-03-09 16:07:10 -0800 | [diff] [blame] | 392 | notify_die(DIE_TRAP, str, regs, error_code, X86_TRAP_DF, SIGSEGV); |
Alexander van Heukelum | 081f75b | 2008-10-03 22:00:39 +0200 | [diff] [blame] | 393 | |
| 394 | tsk->thread.error_code = error_code; |
Srikar Dronamraju | 51e7dc7 | 2012-03-12 14:55:55 +0530 | [diff] [blame] | 395 | tsk->thread.trap_nr = X86_TRAP_DF; |
Alexander van Heukelum | 081f75b | 2008-10-03 22:00:39 +0200 | [diff] [blame] | 396 | |
Andy Lutomirski | e37e43a | 2016-08-11 02:35:23 -0700 | [diff] [blame] | 397 | #ifdef CONFIG_VMAP_STACK |
| 398 | /* |
| 399 | * If we overflow the stack into a guard page, the CPU will fail |
| 400 | * to deliver #PF and will send #DF instead. Similarly, if we |
| 401 | * take any non-IST exception while too close to the bottom of |
| 402 | * the stack, the processor will get a page fault while |
| 403 | * delivering the exception and will generate a double fault. |
| 404 | * |
| 405 | * According to the SDM (footnote in 6.15 under "Interrupt 14 - |
| 406 | * Page-Fault Exception (#PF): |
| 407 | * |
| 408 | * Processors update CR2 whenever a page fault is detected. If a |
| 409 | * second page fault occurs while an earlier page fault is being |
Andy Lutomirski | 6d9256f | 2017-12-04 15:07:22 +0100 | [diff] [blame] | 410 | * delivered, the faulting linear address of the second fault will |
Andy Lutomirski | e37e43a | 2016-08-11 02:35:23 -0700 | [diff] [blame] | 411 | * overwrite the contents of CR2 (replacing the previous |
| 412 | * address). These updates to CR2 occur even if the page fault |
| 413 | * results in a double fault or occurs during the delivery of a |
| 414 | * double fault. |
| 415 | * |
| 416 | * The logic below has a small possibility of incorrectly diagnosing |
| 417 | * some errors as stack overflows. For example, if the IDT or GDT |
| 418 | * gets corrupted such that #GP delivery fails due to a bad descriptor |
| 419 | * causing #GP and we hit this condition while CR2 coincidentally |
| 420 | * points to the stack guard page, we'll think we overflowed the |
| 421 | * stack. Given that we're going to panic one way or another |
| 422 | * if this happens, this isn't necessarily worth fixing. |
| 423 | * |
| 424 | * If necessary, we could improve the test by only diagnosing |
| 425 | * a stack overflow if the saved RSP points within 47 bytes of |
| 426 | * the bottom of the stack: if RSP == tsk_stack + 48 and we |
| 427 | * take an exception, the stack is already aligned and there |
| 428 | * will be enough room SS, RSP, RFLAGS, CS, RIP, and a |
| 429 | * possible error code, so a stack overflow would *not* double |
| 430 | * fault. With any less space left, exception delivery could |
| 431 | * fail, and, as a practical matter, we've overflowed the |
| 432 | * stack even if the actual trigger for the double fault was |
| 433 | * something else. |
| 434 | */ |
Thomas Gleixner | c29c775 | 2020-02-25 23:33:31 +0100 | [diff] [blame] | 435 | if ((unsigned long)task_stack_page(tsk) - 1 - address < PAGE_SIZE) { |
| 436 | handle_stack_overflow("kernel stack overflow (double-fault)", |
| 437 | regs, address); |
| 438 | } |
Andy Lutomirski | e37e43a | 2016-08-11 02:35:23 -0700 | [diff] [blame] | 439 | #endif |
| 440 | |
Andy Lutomirski | 93efbde | 2019-11-20 22:12:38 -0800 | [diff] [blame] | 441 | pr_emerg("PANIC: double fault, error_code: 0x%lx\n", error_code); |
Andy Lutomirski | 0337b7e | 2019-11-25 22:37:44 -0800 | [diff] [blame] | 442 | die("double fault", regs, error_code); |
Andy Lutomirski | 93efbde | 2019-11-20 22:12:38 -0800 | [diff] [blame] | 443 | panic("Machine halted."); |
Thomas Gleixner | c29c775 | 2020-02-25 23:33:31 +0100 | [diff] [blame] | 444 | instrumentation_end(); |
Alexander van Heukelum | 081f75b | 2008-10-03 22:00:39 +0200 | [diff] [blame] | 445 | } |
Alexander van Heukelum | 081f75b | 2008-10-03 22:00:39 +0200 | [diff] [blame] | 446 | |
Thomas Gleixner | 58d9c81 | 2020-02-25 23:16:17 +0100 | [diff] [blame] | 447 | DEFINE_IDTENTRY(exc_bounds) |
Dave Hansen | fe3d197 | 2014-11-14 07:18:29 -0800 | [diff] [blame] | 448 | { |
Thomas Gleixner | 58d9c81 | 2020-02-25 23:16:17 +0100 | [diff] [blame] | 449 | if (notify_die(DIE_TRAP, "bounds", regs, 0, |
Dave Hansen | fe3d197 | 2014-11-14 07:18:29 -0800 | [diff] [blame] | 450 | X86_TRAP_BR, SIGSEGV) == NOTIFY_STOP) |
Andy Lutomirski | 8c84014 | 2015-07-03 12:44:32 -0700 | [diff] [blame] | 451 | return; |
Alexander Kuleshov | d99e1bd | 2016-01-25 20:41:46 +0100 | [diff] [blame] | 452 | cond_local_irq_enable(regs); |
Dave Hansen | fe3d197 | 2014-11-14 07:18:29 -0800 | [diff] [blame] | 453 | |
Andy Lutomirski | f39b6f0 | 2015-03-18 18:33:33 -0700 | [diff] [blame] | 454 | if (!user_mode(regs)) |
Thomas Gleixner | 58d9c81 | 2020-02-25 23:16:17 +0100 | [diff] [blame] | 455 | die("bounds", regs, 0); |
Dave Hansen | fe3d197 | 2014-11-14 07:18:29 -0800 | [diff] [blame] | 456 | |
Thomas Gleixner | 58d9c81 | 2020-02-25 23:16:17 +0100 | [diff] [blame] | 457 | do_trap(X86_TRAP_BR, SIGSEGV, "bounds", regs, 0, 0, NULL); |
Thomas Gleixner | ca4c6a9 | 2019-10-23 14:27:10 +0200 | [diff] [blame] | 458 | |
| 459 | cond_local_irq_disable(regs); |
Dave Hansen | fe3d197 | 2014-11-14 07:18:29 -0800 | [diff] [blame] | 460 | } |
| 461 | |
Jann Horn | 59c1dcb | 2019-12-19 00:11:48 +0100 | [diff] [blame] | 462 | enum kernel_gp_hint { |
| 463 | GP_NO_HINT, |
| 464 | GP_NON_CANONICAL, |
| 465 | GP_CANONICAL |
| 466 | }; |
| 467 | |
| 468 | /* |
| 469 | * When an uncaught #GP occurs, try to determine the memory address accessed by |
| 470 | * the instruction and return that address to the caller. Also, try to figure |
| 471 | * out whether any part of the access to that address was non-canonical. |
| 472 | */ |
| 473 | static enum kernel_gp_hint get_kernel_gp_address(struct pt_regs *regs, |
| 474 | unsigned long *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | { |
Jann Horn | 59c1dcb | 2019-12-19 00:11:48 +0100 | [diff] [blame] | 476 | u8 insn_buf[MAX_INSN_SIZE]; |
| 477 | struct insn insn; |
| 478 | |
| 479 | if (probe_kernel_read(insn_buf, (void *)regs->ip, MAX_INSN_SIZE)) |
| 480 | return GP_NO_HINT; |
| 481 | |
| 482 | kernel_insn_init(&insn, insn_buf, MAX_INSN_SIZE); |
| 483 | insn_get_modrm(&insn); |
| 484 | insn_get_sib(&insn); |
| 485 | |
| 486 | *addr = (unsigned long)insn_get_addr_ref(&insn, regs); |
| 487 | if (*addr == -1UL) |
| 488 | return GP_NO_HINT; |
| 489 | |
| 490 | #ifdef CONFIG_X86_64 |
| 491 | /* |
| 492 | * Check that: |
| 493 | * - the operand is not in the kernel half |
| 494 | * - the last byte of the operand is not in the user canonical half |
| 495 | */ |
| 496 | if (*addr < ~__VIRTUAL_MASK && |
| 497 | *addr + insn.opnd_bytes - 1 > __VIRTUAL_MASK) |
| 498 | return GP_NON_CANONICAL; |
| 499 | #endif |
| 500 | |
| 501 | return GP_CANONICAL; |
| 502 | } |
| 503 | |
| 504 | #define GPFSTR "general protection fault" |
| 505 | |
Thomas Gleixner | be4c11a | 2020-02-25 23:16:25 +0100 | [diff] [blame] | 506 | DEFINE_IDTENTRY_ERRORCODE(exc_general_protection) |
Jann Horn | 59c1dcb | 2019-12-19 00:11:48 +0100 | [diff] [blame] | 507 | { |
| 508 | char desc[sizeof(GPFSTR) + 50 + 2*sizeof(unsigned long) + 1] = GPFSTR; |
Borislav Petkov | 3620976 | 2019-12-31 17:15:35 +0100 | [diff] [blame] | 509 | enum kernel_gp_hint hint = GP_NO_HINT; |
Alexander van Heukelum | 13485ab | 2008-07-02 01:32:04 +0200 | [diff] [blame] | 510 | struct task_struct *tsk; |
Borislav Petkov | 3620976 | 2019-12-31 17:15:35 +0100 | [diff] [blame] | 511 | unsigned long gp_addr; |
| 512 | int ret; |
Ingo Molnar | b596440 | 2008-02-26 11:15:50 +0100 | [diff] [blame] | 513 | |
Alexander Kuleshov | d99e1bd | 2016-01-25 20:41:46 +0100 | [diff] [blame] | 514 | cond_local_irq_enable(regs); |
Alexander van Heukelum | c6df0d7 | 2008-09-09 21:56:07 +0200 | [diff] [blame] | 515 | |
Ricardo Neri | 6fc9dc8 | 2017-11-05 18:27:55 -0800 | [diff] [blame] | 516 | if (static_cpu_has(X86_FEATURE_UMIP)) { |
| 517 | if (user_mode(regs) && fixup_umip_exception(regs)) |
Thomas Gleixner | ca4c6a9 | 2019-10-23 14:27:10 +0200 | [diff] [blame] | 518 | goto exit; |
Ricardo Neri | 6fc9dc8 | 2017-11-05 18:27:55 -0800 | [diff] [blame] | 519 | } |
| 520 | |
Andy Lutomirski | d74ef11 | 2015-03-18 18:33:35 -0700 | [diff] [blame] | 521 | if (v8086_mode(regs)) { |
Frederic Weisbecker | ef3f6288 | 2012-09-24 21:05:52 +0200 | [diff] [blame] | 522 | local_irq_enable(); |
| 523 | handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code); |
Thomas Gleixner | ca4c6a9 | 2019-10-23 14:27:10 +0200 | [diff] [blame] | 524 | local_irq_disable(); |
Andy Lutomirski | 8c84014 | 2015-07-03 12:44:32 -0700 | [diff] [blame] | 525 | return; |
Frederic Weisbecker | ef3f6288 | 2012-09-24 21:05:52 +0200 | [diff] [blame] | 526 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | |
Alexander van Heukelum | 13485ab | 2008-07-02 01:32:04 +0200 | [diff] [blame] | 528 | tsk = current; |
Frederic Weisbecker | ef3f6288 | 2012-09-24 21:05:52 +0200 | [diff] [blame] | 529 | |
Borislav Petkov | 3620976 | 2019-12-31 17:15:35 +0100 | [diff] [blame] | 530 | if (user_mode(regs)) { |
Frederic Weisbecker | ef3f6288 | 2012-09-24 21:05:52 +0200 | [diff] [blame] | 531 | tsk->thread.error_code = error_code; |
| 532 | tsk->thread.trap_nr = X86_TRAP_GP; |
Jann Horn | 76dee4a | 2018-08-28 22:14:16 +0200 | [diff] [blame] | 533 | |
Borislav Petkov | 3620976 | 2019-12-31 17:15:35 +0100 | [diff] [blame] | 534 | show_signal(tsk, SIGSEGV, "", desc, regs, error_code); |
| 535 | force_sig(SIGSEGV); |
Thomas Gleixner | ca4c6a9 | 2019-10-23 14:27:10 +0200 | [diff] [blame] | 536 | goto exit; |
Frederic Weisbecker | ef3f6288 | 2012-09-24 21:05:52 +0200 | [diff] [blame] | 537 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
Borislav Petkov | 3620976 | 2019-12-31 17:15:35 +0100 | [diff] [blame] | 539 | if (fixup_exception(regs, X86_TRAP_GP, error_code, 0)) |
Thomas Gleixner | ca4c6a9 | 2019-10-23 14:27:10 +0200 | [diff] [blame] | 540 | goto exit; |
Borislav Petkov | 3620976 | 2019-12-31 17:15:35 +0100 | [diff] [blame] | 541 | |
Alexander van Heukelum | 13485ab | 2008-07-02 01:32:04 +0200 | [diff] [blame] | 542 | tsk->thread.error_code = error_code; |
Srikar Dronamraju | 51e7dc7 | 2012-03-12 14:55:55 +0530 | [diff] [blame] | 543 | tsk->thread.trap_nr = X86_TRAP_GP; |
Ingo Molnar | b596440 | 2008-02-26 11:15:50 +0100 | [diff] [blame] | 544 | |
Borislav Petkov | 3620976 | 2019-12-31 17:15:35 +0100 | [diff] [blame] | 545 | /* |
| 546 | * To be potentially processing a kprobe fault and to trust the result |
| 547 | * from kprobe_running(), we have to be non-preemptible. |
| 548 | */ |
| 549 | if (!preemptible() && |
| 550 | kprobe_running() && |
| 551 | kprobe_fault_handler(regs, X86_TRAP_GP)) |
Thomas Gleixner | ca4c6a9 | 2019-10-23 14:27:10 +0200 | [diff] [blame] | 552 | goto exit; |
Masoud Asgharifard Sharbiani | abd4f75 | 2007-07-22 11:12:28 +0200 | [diff] [blame] | 553 | |
Borislav Petkov | 3620976 | 2019-12-31 17:15:35 +0100 | [diff] [blame] | 554 | ret = notify_die(DIE_GPF, desc, regs, error_code, X86_TRAP_GP, SIGSEGV); |
| 555 | if (ret == NOTIFY_STOP) |
Thomas Gleixner | ca4c6a9 | 2019-10-23 14:27:10 +0200 | [diff] [blame] | 556 | goto exit; |
Borislav Petkov | 3620976 | 2019-12-31 17:15:35 +0100 | [diff] [blame] | 557 | |
| 558 | if (error_code) |
| 559 | snprintf(desc, sizeof(desc), "segment-related " GPFSTR); |
| 560 | else |
| 561 | hint = get_kernel_gp_address(regs, &gp_addr); |
| 562 | |
| 563 | if (hint != GP_NO_HINT) |
| 564 | snprintf(desc, sizeof(desc), GPFSTR ", %s 0x%lx", |
| 565 | (hint == GP_NON_CANONICAL) ? "probably for non-canonical address" |
| 566 | : "maybe for address", |
| 567 | gp_addr); |
| 568 | |
| 569 | /* |
| 570 | * KASAN is interested only in the non-canonical case, clear it |
| 571 | * otherwise. |
| 572 | */ |
| 573 | if (hint != GP_NON_CANONICAL) |
| 574 | gp_addr = 0; |
| 575 | |
| 576 | die_addr(desc, regs, error_code, gp_addr); |
| 577 | |
Thomas Gleixner | ca4c6a9 | 2019-10-23 14:27:10 +0200 | [diff] [blame] | 578 | exit: |
| 579 | cond_local_irq_disable(regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Peter Zijlstra | 21e2829 | 2020-03-05 16:09:52 +0100 | [diff] [blame] | 582 | static bool do_int3(struct pt_regs *regs) |
| 583 | { |
| 584 | int res; |
| 585 | |
| 586 | #ifdef CONFIG_KGDB_LOW_LEVEL_TRAP |
| 587 | if (kgdb_ll_trap(DIE_INT3, "int3", regs, 0, X86_TRAP_BP, |
| 588 | SIGTRAP) == NOTIFY_STOP) |
| 589 | return true; |
| 590 | #endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */ |
| 591 | |
| 592 | #ifdef CONFIG_KPROBES |
| 593 | if (kprobe_int3_handler(regs)) |
| 594 | return true; |
| 595 | #endif |
| 596 | res = notify_die(DIE_INT3, "int3", regs, 0, X86_TRAP_BP, SIGTRAP); |
| 597 | |
| 598 | return res == NOTIFY_STOP; |
| 599 | } |
| 600 | |
| 601 | static void do_int3_user(struct pt_regs *regs) |
| 602 | { |
| 603 | if (do_int3(regs)) |
| 604 | return; |
| 605 | |
| 606 | cond_local_irq_enable(regs); |
| 607 | do_trap(X86_TRAP_BP, SIGTRAP, "int3", regs, 0, 0, NULL); |
| 608 | cond_local_irq_disable(regs); |
| 609 | } |
| 610 | |
Thomas Gleixner | 8edd7e3 | 2020-02-25 23:16:16 +0100 | [diff] [blame] | 611 | DEFINE_IDTENTRY_RAW(exc_int3) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | { |
Peter Zijlstra | f64366e | 2020-02-20 13:28:06 +0100 | [diff] [blame] | 613 | /* |
| 614 | * poke_int3_handler() is completely self contained code; it does (and |
| 615 | * must) *NOT* call out to anything, lest it hits upon yet another |
| 616 | * INT3. |
| 617 | */ |
Jiri Kosina | 17f4157 | 2013-07-23 10:09:28 +0200 | [diff] [blame] | 618 | if (poke_int3_handler(regs)) |
| 619 | return; |
| 620 | |
Andy Lutomirski | d8ba61b | 2015-07-23 15:37:48 -0700 | [diff] [blame] | 621 | /* |
Thomas Gleixner | fa95d7d | 2020-05-21 22:05:19 +0200 | [diff] [blame] | 622 | * idtentry_enter_user() uses static_branch_{,un}likely() and therefore |
Thomas Gleixner | 8edd7e3 | 2020-02-25 23:16:16 +0100 | [diff] [blame] | 623 | * can trigger INT3, hence poke_int3_handler() must be done |
| 624 | * before. If the entry came from kernel mode, then use nmi_enter() |
| 625 | * because the INT3 could have been hit in any context including |
| 626 | * NMI. |
Andy Lutomirski | d8ba61b | 2015-07-23 15:37:48 -0700 | [diff] [blame] | 627 | */ |
Peter Zijlstra | 21e2829 | 2020-03-05 16:09:52 +0100 | [diff] [blame] | 628 | if (user_mode(regs)) { |
Thomas Gleixner | fa95d7d | 2020-05-21 22:05:19 +0200 | [diff] [blame] | 629 | idtentry_enter_user(regs); |
Peter Zijlstra | 21e2829 | 2020-03-05 16:09:52 +0100 | [diff] [blame] | 630 | instrumentation_begin(); |
| 631 | do_int3_user(regs); |
| 632 | instrumentation_end(); |
Thomas Gleixner | fa95d7d | 2020-05-21 22:05:19 +0200 | [diff] [blame] | 633 | idtentry_exit_user(regs); |
Peter Zijlstra | 21e2829 | 2020-03-05 16:09:52 +0100 | [diff] [blame] | 634 | } else { |
| 635 | nmi_enter(); |
| 636 | instrumentation_begin(); |
Peter Zijlstra | bf2b300 | 2020-05-29 23:27:40 +0200 | [diff] [blame] | 637 | trace_hardirqs_off_finish(); |
Peter Zijlstra | 21e2829 | 2020-03-05 16:09:52 +0100 | [diff] [blame] | 638 | if (!do_int3(regs)) |
| 639 | die("int3", regs, 0); |
Thomas Gleixner | 3ffdfdc | 2020-05-21 22:05:51 +0200 | [diff] [blame] | 640 | if (regs->flags & X86_EFLAGS_IF) |
| 641 | trace_hardirqs_on_prepare(); |
Peter Zijlstra | 21e2829 | 2020-03-05 16:09:52 +0100 | [diff] [blame] | 642 | instrumentation_end(); |
Peter Zijlstra | 0d00449 | 2020-02-19 09:46:43 +0100 | [diff] [blame] | 643 | nmi_exit(); |
Peter Zijlstra | 21e2829 | 2020-03-05 16:09:52 +0100 | [diff] [blame] | 644 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | |
Alexander van Heukelum | 081f75b | 2008-10-03 22:00:39 +0200 | [diff] [blame] | 647 | #ifdef CONFIG_X86_64 |
Ingo Molnar | bd8b96d | 2008-12-26 09:20:22 +0100 | [diff] [blame] | 648 | /* |
Andy Lutomirski | 7f2590a | 2017-12-04 15:07:23 +0100 | [diff] [blame] | 649 | * Help handler running on a per-cpu (IST or entry trampoline) stack |
| 650 | * to switch to the normal thread stack if the interrupted code was in |
| 651 | * user mode. The actual stack switch is done in entry_64.S |
Ingo Molnar | bd8b96d | 2008-12-26 09:20:22 +0100 | [diff] [blame] | 652 | */ |
Thomas Gleixner | daf7a69 | 2020-03-25 23:47:51 +0100 | [diff] [blame] | 653 | asmlinkage __visible noinstr struct pt_regs *sync_regs(struct pt_regs *eregs) |
Alexander van Heukelum | 081f75b | 2008-10-03 22:00:39 +0200 | [diff] [blame] | 654 | { |
Andy Lutomirski | 7f2590a | 2017-12-04 15:07:23 +0100 | [diff] [blame] | 655 | struct pt_regs *regs = (struct pt_regs *)this_cpu_read(cpu_current_top_of_stack) - 1; |
| 656 | if (regs != eregs) |
| 657 | *regs = *eregs; |
Alexander van Heukelum | 081f75b | 2008-10-03 22:00:39 +0200 | [diff] [blame] | 658 | return regs; |
| 659 | } |
Andy Lutomirski | b645af2 | 2014-11-22 18:00:33 -0800 | [diff] [blame] | 660 | |
| 661 | struct bad_iret_stack { |
| 662 | void *error_entry_ret; |
| 663 | struct pt_regs regs; |
| 664 | }; |
| 665 | |
Thomas Gleixner | d73a332 | 2020-03-25 19:53:38 +0100 | [diff] [blame] | 666 | asmlinkage __visible noinstr |
Andy Lutomirski | b645af2 | 2014-11-22 18:00:33 -0800 | [diff] [blame] | 667 | struct bad_iret_stack *fixup_bad_iret(struct bad_iret_stack *s) |
| 668 | { |
| 669 | /* |
| 670 | * This is called from entry_64.S early in handling a fault |
| 671 | * caused by a bad iret to user mode. To handle the fault |
Andy Lutomirski | 7f2590a | 2017-12-04 15:07:23 +0100 | [diff] [blame] | 672 | * correctly, we want to move our stack frame to where it would |
| 673 | * be had we entered directly on the entry stack (rather than |
| 674 | * just below the IRET frame) and we want to pretend that the |
| 675 | * exception came from the IRET target. |
Andy Lutomirski | b645af2 | 2014-11-22 18:00:33 -0800 | [diff] [blame] | 676 | */ |
Thomas Gleixner | d73a332 | 2020-03-25 19:53:38 +0100 | [diff] [blame] | 677 | struct bad_iret_stack tmp, *new_stack = |
| 678 | (struct bad_iret_stack *)__this_cpu_read(cpu_tss_rw.x86_tss.sp0) - 1; |
Andy Lutomirski | b645af2 | 2014-11-22 18:00:33 -0800 | [diff] [blame] | 679 | |
Thomas Gleixner | d73a332 | 2020-03-25 19:53:38 +0100 | [diff] [blame] | 680 | /* Copy the IRET target to the temporary storage. */ |
| 681 | memcpy(&tmp.regs.ip, (void *)s->regs.sp, 5*8); |
Andy Lutomirski | b645af2 | 2014-11-22 18:00:33 -0800 | [diff] [blame] | 682 | |
| 683 | /* Copy the remainder of the stack from the current stack. */ |
Thomas Gleixner | d73a332 | 2020-03-25 19:53:38 +0100 | [diff] [blame] | 684 | memcpy(&tmp, s, offsetof(struct bad_iret_stack, regs.ip)); |
| 685 | |
| 686 | /* Update the entry stack */ |
| 687 | memcpy(new_stack, &tmp, sizeof(tmp)); |
Andy Lutomirski | b645af2 | 2014-11-22 18:00:33 -0800 | [diff] [blame] | 688 | |
Andy Lutomirski | f39b6f0 | 2015-03-18 18:33:33 -0700 | [diff] [blame] | 689 | BUG_ON(!user_mode(&new_stack->regs)); |
Andy Lutomirski | b645af2 | 2014-11-22 18:00:33 -0800 | [diff] [blame] | 690 | return new_stack; |
| 691 | } |
Alexander van Heukelum | 081f75b | 2008-10-03 22:00:39 +0200 | [diff] [blame] | 692 | #endif |
| 693 | |
Andy Lutomirski | f2b3757 | 2016-03-09 19:00:30 -0800 | [diff] [blame] | 694 | static bool is_sysenter_singlestep(struct pt_regs *regs) |
| 695 | { |
| 696 | /* |
| 697 | * We don't try for precision here. If we're anywhere in the region of |
| 698 | * code that can be single-stepped in the SYSENTER entry path, then |
| 699 | * assume that this is a useless single-step trap due to SYSENTER |
| 700 | * being invoked with TF set. (We don't know in advance exactly |
| 701 | * which instructions will be hit because BTF could plausibly |
| 702 | * be set.) |
| 703 | */ |
| 704 | #ifdef CONFIG_X86_32 |
| 705 | return (regs->ip - (unsigned long)__begin_SYSENTER_singlestep_region) < |
| 706 | (unsigned long)__end_SYSENTER_singlestep_region - |
| 707 | (unsigned long)__begin_SYSENTER_singlestep_region; |
| 708 | #elif defined(CONFIG_IA32_EMULATION) |
| 709 | return (regs->ip - (unsigned long)entry_SYSENTER_compat) < |
| 710 | (unsigned long)__end_entry_SYSENTER_compat - |
| 711 | (unsigned long)entry_SYSENTER_compat; |
| 712 | #else |
| 713 | return false; |
| 714 | #endif |
| 715 | } |
| 716 | |
Peter Zijlstra | 9f58fdde | 2020-04-06 21:02:56 +0200 | [diff] [blame] | 717 | static __always_inline void debug_enter(unsigned long *dr6, unsigned long *dr7) |
| 718 | { |
| 719 | /* |
| 720 | * Disable breakpoints during exception handling; recursive exceptions |
| 721 | * are exceedingly 'fun'. |
| 722 | * |
| 723 | * Since this function is NOKPROBE, and that also applies to |
| 724 | * HW_BREAKPOINT_X, we can't hit a breakpoint before this (XXX except a |
| 725 | * HW_BREAKPOINT_W on our stack) |
| 726 | * |
| 727 | * Entry text is excluded for HW_BP_X and cpu_entry_area, which |
| 728 | * includes the entry stack is excluded for everything. |
| 729 | */ |
Peter Zijlstra | e1de11d | 2020-05-29 23:27:33 +0200 | [diff] [blame] | 730 | *dr7 = local_db_save(); |
Peter Zijlstra | 9f58fdde | 2020-04-06 21:02:56 +0200 | [diff] [blame] | 731 | |
| 732 | /* |
| 733 | * The Intel SDM says: |
| 734 | * |
| 735 | * Certain debug exceptions may clear bits 0-3. The remaining |
| 736 | * contents of the DR6 register are never cleared by the |
| 737 | * processor. To avoid confusion in identifying debug |
| 738 | * exceptions, debug handlers should clear the register before |
| 739 | * returning to the interrupted task. |
| 740 | * |
| 741 | * Keep it simple: clear DR6 immediately. |
| 742 | */ |
| 743 | get_debugreg(*dr6, 6); |
| 744 | set_debugreg(0, 6); |
| 745 | /* Filter out all the reserved bits which are preset to 1 */ |
| 746 | *dr6 &= ~DR6_RESERVED; |
| 747 | } |
| 748 | |
| 749 | static __always_inline void debug_exit(unsigned long dr7) |
| 750 | { |
Peter Zijlstra | e1de11d | 2020-05-29 23:27:33 +0200 | [diff] [blame] | 751 | local_db_restore(dr7); |
Peter Zijlstra | 9f58fdde | 2020-04-06 21:02:56 +0200 | [diff] [blame] | 752 | } |
| 753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | /* |
| 755 | * Our handling of the processor debug registers is non-trivial. |
| 756 | * We do not clear them on entry and exit from the kernel. Therefore |
| 757 | * it is possible to get a watchpoint trap here from inside the kernel. |
| 758 | * However, the code in ./ptrace.c has ensured that the user can |
| 759 | * only set watchpoints on userspace addresses. Therefore the in-kernel |
| 760 | * watchpoint trap can only occur in code which is reading/writing |
| 761 | * from user space. Such code must not hold kernel locks (since it |
| 762 | * can equally take a page fault), therefore it is safe to call |
| 763 | * force_sig_info even though that claims and releases locks. |
Ingo Molnar | b596440 | 2008-02-26 11:15:50 +0100 | [diff] [blame] | 764 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | * Code in ./signal.c ensures that the debug control register |
| 766 | * is restored before we deliver any signal, and therefore that |
| 767 | * user code runs with the correct debug control register even though |
| 768 | * we clear it here. |
| 769 | * |
| 770 | * Being careful here means that we don't have to be as careful in a |
| 771 | * lot of more complicated places (task switching can be a bit lazy |
| 772 | * about restoring all the debug state, and ptrace doesn't have to |
| 773 | * find every occurrence of the TF bit that could be saved away even |
| 774 | * by user code) |
Alexander van Heukelum | c1d518c | 2008-10-03 23:17:11 +0200 | [diff] [blame] | 775 | * |
| 776 | * May run on IST stack. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | */ |
Thomas Gleixner | 9347f41 | 2020-05-04 19:56:26 +0200 | [diff] [blame] | 778 | static void noinstr handle_debug(struct pt_regs *regs, unsigned long dr6, |
| 779 | bool user_icebp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | struct task_struct *tsk = current; |
Srinivasa Ds | da654b7 | 2008-09-23 15:23:52 +0530 | [diff] [blame] | 782 | int si_code; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | |
K.Prasad | 08d6832 | 2009-06-01 23:44:08 +0530 | [diff] [blame] | 784 | /* Store the virtualized DR6 value */ |
| 785 | tsk->thread.debugreg6 = dr6; |
| 786 | |
Thomas Gleixner | 75347bb2 | 2020-04-30 11:07:20 +0200 | [diff] [blame] | 787 | instrumentation_begin(); |
Masami Hiramatsu | 6f6343f | 2014-04-17 17:17:33 +0900 | [diff] [blame] | 788 | #ifdef CONFIG_KPROBES |
Thomas Gleixner | 75347bb2 | 2020-04-30 11:07:20 +0200 | [diff] [blame] | 789 | if (kprobe_debug_handler(regs)) { |
| 790 | instrumentation_end(); |
Thomas Gleixner | 4c0dcd8 | 2020-02-25 23:33:29 +0100 | [diff] [blame] | 791 | return; |
Thomas Gleixner | 75347bb2 | 2020-04-30 11:07:20 +0200 | [diff] [blame] | 792 | } |
Masami Hiramatsu | 6f6343f | 2014-04-17 17:17:33 +0900 | [diff] [blame] | 793 | #endif |
| 794 | |
Thomas Gleixner | 2bbc68f | 2020-02-25 23:33:26 +0100 | [diff] [blame] | 795 | if (notify_die(DIE_DEBUG, "debug", regs, (long)&dr6, 0, |
Thomas Gleixner | 75347bb2 | 2020-04-30 11:07:20 +0200 | [diff] [blame] | 796 | SIGTRAP) == NOTIFY_STOP) { |
| 797 | instrumentation_end(); |
Thomas Gleixner | 4c0dcd8 | 2020-02-25 23:33:29 +0100 | [diff] [blame] | 798 | return; |
Thomas Gleixner | 75347bb2 | 2020-04-30 11:07:20 +0200 | [diff] [blame] | 799 | } |
Alexander van Heukelum | 3d2a71a | 2008-09-30 18:41:37 +0200 | [diff] [blame] | 800 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | /* It's safe to allow irq's after DR6 has been saved */ |
Alexander Kuleshov | d99e1bd | 2016-01-25 20:41:46 +0100 | [diff] [blame] | 802 | cond_local_irq_enable(regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | |
Andy Lutomirski | d74ef11 | 2015-03-18 18:33:35 -0700 | [diff] [blame] | 804 | if (v8086_mode(regs)) { |
Thomas Gleixner | 2bbc68f | 2020-02-25 23:33:26 +0100 | [diff] [blame] | 805 | handle_vm86_trap((struct kernel_vm86_regs *) regs, 0, |
| 806 | X86_TRAP_DB); |
Thomas Gleixner | 9347f41 | 2020-05-04 19:56:26 +0200 | [diff] [blame] | 807 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Andy Lutomirski | f2b3757 | 2016-03-09 19:00:30 -0800 | [diff] [blame] | 810 | if (WARN_ON_ONCE((dr6 & DR_STEP) && !user_mode(regs))) { |
| 811 | /* |
| 812 | * Historical junk that used to handle SYSENTER single-stepping. |
| 813 | * This should be unreachable now. If we survive for a while |
| 814 | * without anyone hitting this warning, we'll turn this into |
| 815 | * an oops. |
| 816 | */ |
K.Prasad | 08d6832 | 2009-06-01 23:44:08 +0530 | [diff] [blame] | 817 | tsk->thread.debugreg6 &= ~DR_STEP; |
| 818 | set_tsk_thread_flag(tsk, TIF_SINGLESTEP); |
| 819 | regs->flags &= ~X86_EFLAGS_TF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | } |
Thomas Gleixner | 9347f41 | 2020-05-04 19:56:26 +0200 | [diff] [blame] | 821 | |
K.Prasad | 08d6832 | 2009-06-01 23:44:08 +0530 | [diff] [blame] | 822 | si_code = get_si_code(tsk->thread.debugreg6); |
Frederic Weisbecker | a1e80fa | 2010-06-30 15:09:06 +0200 | [diff] [blame] | 823 | if (tsk->thread.debugreg6 & (DR_STEP | DR_TRAP_BITS) || user_icebp) |
Thomas Gleixner | 2bbc68f | 2020-02-25 23:33:26 +0100 | [diff] [blame] | 824 | send_sigtrap(regs, 0, si_code); |
Thomas Gleixner | 9347f41 | 2020-05-04 19:56:26 +0200 | [diff] [blame] | 825 | |
| 826 | out: |
Alexander Kuleshov | d99e1bd | 2016-01-25 20:41:46 +0100 | [diff] [blame] | 827 | cond_local_irq_disable(regs); |
Thomas Gleixner | 75347bb2 | 2020-04-30 11:07:20 +0200 | [diff] [blame] | 828 | instrumentation_end(); |
Thomas Gleixner | 4c0dcd8 | 2020-02-25 23:33:29 +0100 | [diff] [blame] | 829 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | |
Thomas Gleixner | 4c0dcd8 | 2020-02-25 23:33:29 +0100 | [diff] [blame] | 831 | static __always_inline void exc_debug_kernel(struct pt_regs *regs, |
| 832 | unsigned long dr6) |
| 833 | { |
| 834 | nmi_enter(); |
Thomas Gleixner | 3ffdfdc | 2020-05-21 22:05:51 +0200 | [diff] [blame] | 835 | instrumentation_begin(); |
Peter Zijlstra | bf2b300 | 2020-05-29 23:27:40 +0200 | [diff] [blame] | 836 | trace_hardirqs_off_finish(); |
Thomas Gleixner | 3ffdfdc | 2020-05-21 22:05:51 +0200 | [diff] [blame] | 837 | instrumentation_end(); |
| 838 | |
Thomas Gleixner | 9347f41 | 2020-05-04 19:56:26 +0200 | [diff] [blame] | 839 | /* |
| 840 | * The SDM says "The processor clears the BTF flag when it |
| 841 | * generates a debug exception." Clear TIF_BLOCKSTEP to keep |
| 842 | * TIF_BLOCKSTEP in sync with the hardware BTF flag. |
| 843 | */ |
| 844 | clear_thread_flag(TIF_BLOCKSTEP); |
| 845 | |
| 846 | /* |
| 847 | * Catch SYSENTER with TF set and clear DR_STEP. If this hit a |
| 848 | * watchpoint at the same time then that will still be handled. |
| 849 | */ |
| 850 | if ((dr6 & DR_STEP) && is_sysenter_singlestep(regs)) |
| 851 | dr6 &= ~DR_STEP; |
| 852 | |
| 853 | /* |
| 854 | * If DR6 is zero, no point in trying to handle it. The kernel is |
| 855 | * not using INT1. |
| 856 | */ |
| 857 | if (dr6) |
| 858 | handle_debug(regs, dr6, false); |
| 859 | |
Thomas Gleixner | 3ffdfdc | 2020-05-21 22:05:51 +0200 | [diff] [blame] | 860 | instrumentation_begin(); |
| 861 | if (regs->flags & X86_EFLAGS_IF) |
| 862 | trace_hardirqs_on_prepare(); |
| 863 | instrumentation_end(); |
Thomas Gleixner | 4c0dcd8 | 2020-02-25 23:33:29 +0100 | [diff] [blame] | 864 | nmi_exit(); |
| 865 | } |
| 866 | |
| 867 | static __always_inline void exc_debug_user(struct pt_regs *regs, |
| 868 | unsigned long dr6) |
| 869 | { |
Thomas Gleixner | fa95d7d | 2020-05-21 22:05:19 +0200 | [diff] [blame] | 870 | idtentry_enter_user(regs); |
Thomas Gleixner | 9347f41 | 2020-05-04 19:56:26 +0200 | [diff] [blame] | 871 | clear_thread_flag(TIF_BLOCKSTEP); |
| 872 | |
| 873 | /* |
| 874 | * If dr6 has no reason to give us about the origin of this trap, |
| 875 | * then it's very likely the result of an icebp/int01 trap. |
| 876 | * User wants a sigtrap for that. |
| 877 | */ |
| 878 | handle_debug(regs, dr6, !dr6); |
Thomas Gleixner | fa95d7d | 2020-05-21 22:05:19 +0200 | [diff] [blame] | 879 | idtentry_exit_user(regs); |
Thomas Gleixner | 4c0dcd8 | 2020-02-25 23:33:29 +0100 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | #ifdef CONFIG_X86_64 |
| 883 | /* IST stack entry */ |
| 884 | DEFINE_IDTENTRY_DEBUG(exc_debug) |
| 885 | { |
| 886 | unsigned long dr6, dr7; |
| 887 | |
| 888 | debug_enter(&dr6, &dr7); |
| 889 | exc_debug_kernel(regs, dr6); |
Peter Zijlstra | 9f58fdde | 2020-04-06 21:02:56 +0200 | [diff] [blame] | 890 | debug_exit(dr7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | } |
| 892 | |
Thomas Gleixner | 4c0dcd8 | 2020-02-25 23:33:29 +0100 | [diff] [blame] | 893 | /* User entry, runs on regular task stack */ |
| 894 | DEFINE_IDTENTRY_DEBUG_USER(exc_debug) |
| 895 | { |
| 896 | unsigned long dr6, dr7; |
| 897 | |
| 898 | debug_enter(&dr6, &dr7); |
| 899 | exc_debug_user(regs, dr6); |
| 900 | debug_exit(dr7); |
| 901 | } |
| 902 | #else |
| 903 | /* 32 bit does not have separate entry points. */ |
| 904 | DEFINE_IDTENTRY_DEBUG(exc_debug) |
| 905 | { |
| 906 | unsigned long dr6, dr7; |
| 907 | |
| 908 | debug_enter(&dr6, &dr7); |
| 909 | |
| 910 | if (user_mode(regs)) |
| 911 | exc_debug_user(regs, dr6); |
| 912 | else |
| 913 | exc_debug_kernel(regs, dr6); |
| 914 | |
| 915 | debug_exit(dr7); |
| 916 | } |
| 917 | #endif |
| 918 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | /* |
| 920 | * Note that we play around with the 'TS' bit in an attempt to get |
| 921 | * the correct behaviour even in the presence of the asynchronous |
| 922 | * IRQ13 behaviour |
| 923 | */ |
Thomas Gleixner | 48227e2 | 2020-02-25 23:16:29 +0100 | [diff] [blame] | 924 | static void math_error(struct pt_regs *regs, int trapnr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | { |
Brian Gerst | e2e75c91 | 2010-03-21 09:00:45 -0400 | [diff] [blame] | 926 | struct task_struct *task = current; |
Ingo Molnar | e1cebad | 2015-04-30 09:29:38 +0200 | [diff] [blame] | 927 | struct fpu *fpu = &task->thread.fpu; |
Eric W. Biederman | 0a996c1 | 2018-09-18 01:16:39 +0200 | [diff] [blame] | 928 | int si_code; |
Kees Cook | c940826 | 2012-03-09 16:07:10 -0800 | [diff] [blame] | 929 | char *str = (trapnr == X86_TRAP_MF) ? "fpu exception" : |
| 930 | "simd exception"; |
Brian Gerst | e2e75c91 | 2010-03-21 09:00:45 -0400 | [diff] [blame] | 931 | |
Alexander Kuleshov | d99e1bd | 2016-01-25 20:41:46 +0100 | [diff] [blame] | 932 | cond_local_irq_enable(regs); |
Brian Gerst | e2e75c91 | 2010-03-21 09:00:45 -0400 | [diff] [blame] | 933 | |
Ingo Molnar | e1cebad | 2015-04-30 09:29:38 +0200 | [diff] [blame] | 934 | if (!user_mode(regs)) { |
Thomas Gleixner | 48227e2 | 2020-02-25 23:16:29 +0100 | [diff] [blame] | 935 | if (fixup_exception(regs, trapnr, 0, 0)) |
Thomas Gleixner | ca4c6a9 | 2019-10-23 14:27:10 +0200 | [diff] [blame] | 936 | goto exit; |
Siarhei Liakh | 3ae6295 | 2018-06-14 19:36:07 +0000 | [diff] [blame] | 937 | |
Thomas Gleixner | 48227e2 | 2020-02-25 23:16:29 +0100 | [diff] [blame] | 938 | task->thread.error_code = 0; |
Siarhei Liakh | 3ae6295 | 2018-06-14 19:36:07 +0000 | [diff] [blame] | 939 | task->thread.trap_nr = trapnr; |
| 940 | |
Thomas Gleixner | 48227e2 | 2020-02-25 23:16:29 +0100 | [diff] [blame] | 941 | if (notify_die(DIE_TRAP, str, regs, 0, trapnr, |
| 942 | SIGFPE) != NOTIFY_STOP) |
| 943 | die(str, regs, 0); |
Thomas Gleixner | ca4c6a9 | 2019-10-23 14:27:10 +0200 | [diff] [blame] | 944 | goto exit; |
Brian Gerst | e2e75c91 | 2010-03-21 09:00:45 -0400 | [diff] [blame] | 945 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | |
| 947 | /* |
| 948 | * Save the info for the exception handler and clear the error. |
| 949 | */ |
Ingo Molnar | e1cebad | 2015-04-30 09:29:38 +0200 | [diff] [blame] | 950 | fpu__save(fpu); |
| 951 | |
| 952 | task->thread.trap_nr = trapnr; |
Thomas Gleixner | 48227e2 | 2020-02-25 23:16:29 +0100 | [diff] [blame] | 953 | task->thread.error_code = 0; |
H. Peter Anvin | adf77ba | 2008-12-22 17:56:05 -0800 | [diff] [blame] | 954 | |
Eric W. Biederman | 0a996c1 | 2018-09-18 01:16:39 +0200 | [diff] [blame] | 955 | si_code = fpu__exception_code(fpu, trapnr); |
Ingo Molnar | e1cebad | 2015-04-30 09:29:38 +0200 | [diff] [blame] | 956 | /* Retry when we get spurious exceptions: */ |
Eric W. Biederman | 0a996c1 | 2018-09-18 01:16:39 +0200 | [diff] [blame] | 957 | if (!si_code) |
Thomas Gleixner | ca4c6a9 | 2019-10-23 14:27:10 +0200 | [diff] [blame] | 958 | goto exit; |
Ingo Molnar | e1cebad | 2015-04-30 09:29:38 +0200 | [diff] [blame] | 959 | |
Eric W. Biederman | 0a996c1 | 2018-09-18 01:16:39 +0200 | [diff] [blame] | 960 | force_sig_fault(SIGFPE, si_code, |
Eric W. Biederman | 2e1661d2 | 2019-05-23 11:04:24 -0500 | [diff] [blame] | 961 | (void __user *)uprobe_get_trap_addr(regs)); |
Thomas Gleixner | ca4c6a9 | 2019-10-23 14:27:10 +0200 | [diff] [blame] | 962 | exit: |
| 963 | cond_local_irq_disable(regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | } |
| 965 | |
Thomas Gleixner | 14a8bd2 | 2020-02-25 23:16:27 +0100 | [diff] [blame] | 966 | DEFINE_IDTENTRY(exc_coprocessor_error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | { |
Thomas Gleixner | 48227e2 | 2020-02-25 23:16:29 +0100 | [diff] [blame] | 968 | math_error(regs, X86_TRAP_MF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | } |
| 970 | |
Thomas Gleixner | 48227e2 | 2020-02-25 23:16:29 +0100 | [diff] [blame] | 971 | DEFINE_IDTENTRY(exc_simd_coprocessor_error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | { |
Thomas Gleixner | 48227e2 | 2020-02-25 23:16:29 +0100 | [diff] [blame] | 973 | if (IS_ENABLED(CONFIG_X86_INVD_BUG)) { |
| 974 | /* AMD 486 bug: INVD in CPL 0 raises #XF instead of #GP */ |
| 975 | if (!static_cpu_has(X86_FEATURE_XMM)) { |
| 976 | __exc_general_protection(regs, 0); |
| 977 | return; |
| 978 | } |
| 979 | } |
| 980 | math_error(regs, X86_TRAP_XF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | } |
| 982 | |
Thomas Gleixner | dad7106 | 2020-02-25 23:16:26 +0100 | [diff] [blame] | 983 | DEFINE_IDTENTRY(exc_spurious_interrupt_bug) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | { |
Thomas Gleixner | d244d0e | 2020-02-25 22:36:41 +0100 | [diff] [blame] | 985 | /* |
| 986 | * This addresses a Pentium Pro Erratum: |
| 987 | * |
| 988 | * PROBLEM: If the APIC subsystem is configured in mixed mode with |
| 989 | * Virtual Wire mode implemented through the local APIC, an |
| 990 | * interrupt vector of 0Fh (Intel reserved encoding) may be |
| 991 | * generated by the local APIC (Int 15). This vector may be |
| 992 | * generated upon receipt of a spurious interrupt (an interrupt |
| 993 | * which is removed before the system receives the INTA sequence) |
| 994 | * instead of the programmed 8259 spurious interrupt vector. |
| 995 | * |
| 996 | * IMPLICATION: The spurious interrupt vector programmed in the |
| 997 | * 8259 is normally handled by an operating system's spurious |
| 998 | * interrupt handler. However, a vector of 0Fh is unknown to some |
| 999 | * operating systems, which would crash if this erratum occurred. |
| 1000 | * |
| 1001 | * In theory this could be limited to 32bit, but the handler is not |
| 1002 | * hurting and who knows which other CPUs suffer from this. |
| 1003 | */ |
Alexander van Heukelum | 081f75b | 2008-10-03 22:00:39 +0200 | [diff] [blame] | 1004 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | |
Thomas Gleixner | 866ae2c | 2020-02-25 23:16:19 +0100 | [diff] [blame] | 1006 | DEFINE_IDTENTRY(exc_device_not_available) |
Alexander van Heukelum | 7643e9b | 2008-09-09 21:56:02 +0200 | [diff] [blame] | 1007 | { |
Borislav Petkov | ee35b9b | 2019-01-17 13:02:05 +0100 | [diff] [blame] | 1008 | unsigned long cr0 = read_cr0(); |
Andy Lutomirski | bef8b6d | 2016-10-31 15:18:47 -0700 | [diff] [blame] | 1009 | |
Brian Gerst | a334fe4 | 2010-09-03 21:17:15 -0400 | [diff] [blame] | 1010 | #ifdef CONFIG_MATH_EMULATION |
Borislav Petkov | ee35b9b | 2019-01-17 13:02:05 +0100 | [diff] [blame] | 1011 | if (!boot_cpu_has(X86_FEATURE_FPU) && (cr0 & X86_CR0_EM)) { |
Tejun Heo | d315760 | 2009-02-09 22:17:39 +0900 | [diff] [blame] | 1012 | struct math_emu_info info = { }; |
| 1013 | |
Alexander Kuleshov | d99e1bd | 2016-01-25 20:41:46 +0100 | [diff] [blame] | 1014 | cond_local_irq_enable(regs); |
Tejun Heo | d315760 | 2009-02-09 22:17:39 +0900 | [diff] [blame] | 1015 | |
Brian Gerst | aa78bcf | 2009-02-10 09:51:45 -0500 | [diff] [blame] | 1016 | info.regs = regs; |
Tejun Heo | d315760 | 2009-02-09 22:17:39 +0900 | [diff] [blame] | 1017 | math_emulate(&info); |
Thomas Gleixner | ca4c6a9 | 2019-10-23 14:27:10 +0200 | [diff] [blame] | 1018 | |
| 1019 | cond_local_irq_disable(regs); |
Brian Gerst | a334fe4 | 2010-09-03 21:17:15 -0400 | [diff] [blame] | 1020 | return; |
Alexander van Heukelum | 7643e9b | 2008-09-09 21:56:02 +0200 | [diff] [blame] | 1021 | } |
Brian Gerst | a334fe4 | 2010-09-03 21:17:15 -0400 | [diff] [blame] | 1022 | #endif |
Andy Lutomirski | bef8b6d | 2016-10-31 15:18:47 -0700 | [diff] [blame] | 1023 | |
| 1024 | /* This should not happen. */ |
Andy Lutomirski | bef8b6d | 2016-10-31 15:18:47 -0700 | [diff] [blame] | 1025 | if (WARN(cr0 & X86_CR0_TS, "CR0.TS was set")) { |
| 1026 | /* Try to fix it up and carry on. */ |
| 1027 | write_cr0(cr0 & ~X86_CR0_TS); |
| 1028 | } else { |
| 1029 | /* |
| 1030 | * Something terrible happened, and we're better off trying |
| 1031 | * to kill the task than getting stuck in a never-ending |
| 1032 | * loop of #NM faults. |
| 1033 | */ |
Thomas Gleixner | 866ae2c | 2020-02-25 23:16:19 +0100 | [diff] [blame] | 1034 | die("unexpected #NM exception", regs, 0); |
Andy Lutomirski | bef8b6d | 2016-10-31 15:18:47 -0700 | [diff] [blame] | 1035 | } |
Alexander van Heukelum | 7643e9b | 2008-09-09 21:56:02 +0200 | [diff] [blame] | 1036 | } |
| 1037 | |
Alexander van Heukelum | 081f75b | 2008-10-03 22:00:39 +0200 | [diff] [blame] | 1038 | #ifdef CONFIG_X86_32 |
Thomas Gleixner | d772905 | 2020-02-25 23:16:30 +0100 | [diff] [blame] | 1039 | DEFINE_IDTENTRY_SW(iret_error) |
Alexander van Heukelum | f8e0870 | 2008-09-09 21:56:13 +0200 | [diff] [blame] | 1040 | { |
Alexander van Heukelum | f8e0870 | 2008-09-09 21:56:13 +0200 | [diff] [blame] | 1041 | local_irq_enable(); |
Thomas Gleixner | d772905 | 2020-02-25 23:16:30 +0100 | [diff] [blame] | 1042 | if (notify_die(DIE_TRAP, "iret exception", regs, 0, |
Frederic Weisbecker | 6ba3c97 | 2012-07-11 20:26:35 +0200 | [diff] [blame] | 1043 | X86_TRAP_IRET, SIGILL) != NOTIFY_STOP) { |
Thomas Gleixner | d772905 | 2020-02-25 23:16:30 +0100 | [diff] [blame] | 1044 | do_trap(X86_TRAP_IRET, SIGILL, "iret exception", regs, 0, |
Eric W. Biederman | 164881b | 2018-04-16 14:29:39 -0500 | [diff] [blame] | 1045 | ILL_BADSTK, (void __user *)NULL); |
Frederic Weisbecker | 6ba3c97 | 2012-07-11 20:26:35 +0200 | [diff] [blame] | 1046 | } |
Thomas Gleixner | ca4c6a9 | 2019-10-23 14:27:10 +0200 | [diff] [blame] | 1047 | local_irq_disable(); |
Alexander van Heukelum | f8e0870 | 2008-09-09 21:56:13 +0200 | [diff] [blame] | 1048 | } |
Alexander van Heukelum | 081f75b | 2008-10-03 22:00:39 +0200 | [diff] [blame] | 1049 | #endif |
Alexander van Heukelum | f8e0870 | 2008-09-09 21:56:13 +0200 | [diff] [blame] | 1050 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | void __init trap_init(void) |
| 1052 | { |
Andy Lutomirski | 40e7f94 | 2017-12-04 15:07:26 +0100 | [diff] [blame] | 1053 | /* Init cpu_entry_area before IST entries are set up */ |
| 1054 | setup_cpu_entry_areas(); |
| 1055 | |
Thomas Gleixner | b70543a | 2017-08-28 08:47:53 +0200 | [diff] [blame] | 1056 | idt_setup_traps(); |
Yinghai Lu | bb3f0b5 | 2009-01-25 02:38:09 -0800 | [diff] [blame] | 1057 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | /* |
Kees Cook | 4eefbe7 | 2013-04-10 12:24:22 -0700 | [diff] [blame] | 1059 | * Set the IDT descriptor to a fixed read-only location, so that the |
| 1060 | * "sidt" instruction will not leak the location of the kernel, and |
| 1061 | * to defend the IDT against arbitrary memory write vulnerabilities. |
| 1062 | * It will be reloaded in cpu_init() */ |
Thomas Gleixner | 92a0f81 | 2017-12-20 18:51:31 +0100 | [diff] [blame] | 1063 | cea_set_pte(CPU_ENTRY_AREA_RO_IDT_VADDR, __pa_symbol(idt_table), |
| 1064 | PAGE_KERNEL_RO); |
| 1065 | idt_descr.address = CPU_ENTRY_AREA_RO_IDT; |
Kees Cook | 4eefbe7 | 2013-04-10 12:24:22 -0700 | [diff] [blame] | 1066 | |
| 1067 | /* |
Ingo Molnar | b596440 | 2008-02-26 11:15:50 +0100 | [diff] [blame] | 1068 | * Should be a barrier for any external CPU state: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | */ |
| 1070 | cpu_init(); |
| 1071 | |
Thomas Gleixner | 90f6225 | 2017-08-28 08:47:52 +0200 | [diff] [blame] | 1072 | idt_setup_ist_traps(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | } |