blob: 121921b2927cb8ba90fbcda748004dec06e301ca [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Paul Gortmaker744c1932016-09-19 17:04:18 -04002#include <linux/extable.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -08003#include <linux/uaccess.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +01004#include <linux/sched/debug.h>
Juergen Gross42b3a4c2017-11-24 09:42:21 +01005#include <xen/xen.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +01006
Eric Biggersd5c80282017-09-23 15:00:09 +02007#include <asm/fpu/internal.h>
Brijesh Singhe7599592021-04-27 06:16:34 -05008#include <asm/sev.h>
Andy Lutomirski0d0efc02016-04-02 07:01:33 -07009#include <asm/traps.h>
Borislav Petkov81c29492016-07-05 00:31:27 +020010#include <asm/kdebug.h>
Harvey Harrison6d485832008-01-30 13:31:41 +010011
Tony Luck548acf12016-02-17 10:20:12 -080012typedef bool (*ex_handler_t)(const struct exception_table_entry *,
Jann Horn81fd9c12018-08-28 22:14:19 +020013 struct pt_regs *, int, unsigned long,
14 unsigned long);
Tony Luck548acf12016-02-17 10:20:12 -080015
H. Peter Anvin70627652012-04-20 17:12:48 -070016static inline unsigned long
H. Peter Anvin70627652012-04-20 17:12:48 -070017ex_fixup_addr(const struct exception_table_entry *x)
18{
19 return (unsigned long)&x->fixup + x->fixup;
20}
Tony Luck548acf12016-02-17 10:20:12 -080021static inline ex_handler_t
22ex_fixup_handler(const struct exception_table_entry *x)
Harvey Harrison6d485832008-01-30 13:31:41 +010023{
Tony Luck548acf12016-02-17 10:20:12 -080024 return (ex_handler_t)((unsigned long)&x->handler + x->handler);
25}
26
Andi Kleen80a3e392017-12-21 16:18:20 -080027__visible bool ex_handler_default(const struct exception_table_entry *fixup,
Jann Horn81fd9c12018-08-28 22:14:19 +020028 struct pt_regs *regs, int trapnr,
29 unsigned long error_code,
30 unsigned long fault_addr)
Tony Luck548acf12016-02-17 10:20:12 -080031{
32 regs->ip = ex_fixup_addr(fixup);
33 return true;
34}
35EXPORT_SYMBOL(ex_handler_default);
36
Andi Kleen80a3e392017-12-21 16:18:20 -080037__visible bool ex_handler_fault(const struct exception_table_entry *fixup,
Jann Horn81fd9c12018-08-28 22:14:19 +020038 struct pt_regs *regs, int trapnr,
39 unsigned long error_code,
40 unsigned long fault_addr)
Tony Luck548acf12016-02-17 10:20:12 -080041{
42 regs->ip = ex_fixup_addr(fixup);
43 regs->ax = trapnr;
44 return true;
45}
46EXPORT_SYMBOL_GPL(ex_handler_fault);
47
Kees Cook7a46ec02017-08-15 09:19:24 -070048/*
Eric Biggersd5c80282017-09-23 15:00:09 +020049 * Handler for when we fail to restore a task's FPU state. We should never get
50 * here because the FPU state of a task using the FPU (task->thread.fpu.state)
51 * should always be valid. However, past bugs have allowed userspace to set
52 * reserved bits in the XSAVE area using PTRACE_SETREGSET or sys_rt_sigreturn().
53 * These caused XRSTOR to fail when switching to the task, leaking the FPU
54 * registers of the task previously executing on the CPU. Mitigate this class
55 * of vulnerability by restoring from the initial state (essentially, zeroing
56 * out all the FPU registers) if we can't restore from the task's FPU state.
57 */
Andi Kleen80a3e392017-12-21 16:18:20 -080058__visible bool ex_handler_fprestore(const struct exception_table_entry *fixup,
Jann Horn81fd9c12018-08-28 22:14:19 +020059 struct pt_regs *regs, int trapnr,
60 unsigned long error_code,
61 unsigned long fault_addr)
Eric Biggersd5c80282017-09-23 15:00:09 +020062{
63 regs->ip = ex_fixup_addr(fixup);
64
65 WARN_ONCE(1, "Bad FPU state detected at %pB, reinitializing FPU registers.",
66 (void *)instruction_pointer(regs));
67
68 __copy_kernel_to_fpregs(&init_fpstate, -1);
69 return true;
70}
71EXPORT_SYMBOL_GPL(ex_handler_fprestore);
72
Jann Horn75045f72018-08-28 22:14:18 +020073__visible bool ex_handler_uaccess(const struct exception_table_entry *fixup,
Jann Horn81fd9c12018-08-28 22:14:19 +020074 struct pt_regs *regs, int trapnr,
75 unsigned long error_code,
76 unsigned long fault_addr)
Jann Horn75045f72018-08-28 22:14:18 +020077{
Linus Torvalds00c42372019-02-26 09:16:04 -080078 WARN_ONCE(trapnr == X86_TRAP_GP, "General protection fault in user access. Non-canonical address?");
Jann Horn75045f72018-08-28 22:14:18 +020079 regs->ip = ex_fixup_addr(fixup);
80 return true;
81}
82EXPORT_SYMBOL(ex_handler_uaccess);
83
Youquan Song278b9172020-10-06 14:09:07 -070084__visible bool ex_handler_copy(const struct exception_table_entry *fixup,
85 struct pt_regs *regs, int trapnr,
86 unsigned long error_code,
87 unsigned long fault_addr)
88{
89 WARN_ONCE(trapnr == X86_TRAP_GP, "General protection fault in user access. Non-canonical address?");
90 regs->ip = ex_fixup_addr(fixup);
91 regs->ax = trapnr;
92 return true;
93}
94EXPORT_SYMBOL(ex_handler_copy);
95
Andi Kleen80a3e392017-12-21 16:18:20 -080096__visible bool ex_handler_rdmsr_unsafe(const struct exception_table_entry *fixup,
Jann Horn81fd9c12018-08-28 22:14:19 +020097 struct pt_regs *regs, int trapnr,
98 unsigned long error_code,
99 unsigned long fault_addr)
Andy Lutomirskifbd70432016-04-02 07:01:37 -0700100{
Sakari Ailusd75f7732019-03-25 21:32:28 +0200101 if (pr_warn_once("unchecked MSR access error: RDMSR from 0x%x at rIP: 0x%lx (%pS)\n",
Borislav Petkov81c29492016-07-05 00:31:27 +0200102 (unsigned int)regs->cx, regs->ip, (void *)regs->ip))
103 show_stack_regs(regs);
Andy Lutomirskifbd70432016-04-02 07:01:37 -0700104
105 /* Pretend that the read succeeded and returned 0. */
106 regs->ip = ex_fixup_addr(fixup);
107 regs->ax = 0;
108 regs->dx = 0;
109 return true;
110}
111EXPORT_SYMBOL(ex_handler_rdmsr_unsafe);
112
Andi Kleen80a3e392017-12-21 16:18:20 -0800113__visible bool ex_handler_wrmsr_unsafe(const struct exception_table_entry *fixup,
Jann Horn81fd9c12018-08-28 22:14:19 +0200114 struct pt_regs *regs, int trapnr,
115 unsigned long error_code,
116 unsigned long fault_addr)
Andy Lutomirskifbd70432016-04-02 07:01:37 -0700117{
Sakari Ailusd75f7732019-03-25 21:32:28 +0200118 if (pr_warn_once("unchecked MSR access error: WRMSR to 0x%x (tried to write 0x%08x%08x) at rIP: 0x%lx (%pS)\n",
Borislav Petkov81c29492016-07-05 00:31:27 +0200119 (unsigned int)regs->cx, (unsigned int)regs->dx,
120 (unsigned int)regs->ax, regs->ip, (void *)regs->ip))
121 show_stack_regs(regs);
Andy Lutomirskifbd70432016-04-02 07:01:37 -0700122
123 /* Pretend that the write succeeded. */
124 regs->ip = ex_fixup_addr(fixup);
125 return true;
126}
127EXPORT_SYMBOL(ex_handler_wrmsr_unsafe);
128
Andi Kleen80a3e392017-12-21 16:18:20 -0800129__visible bool ex_handler_clear_fs(const struct exception_table_entry *fixup,
Jann Horn81fd9c12018-08-28 22:14:19 +0200130 struct pt_regs *regs, int trapnr,
131 unsigned long error_code,
132 unsigned long fault_addr)
Andy Lutomirski45e876f2016-04-26 12:23:26 -0700133{
134 if (static_cpu_has(X86_BUG_NULL_SEG))
135 asm volatile ("mov %0, %%fs" : : "rm" (__USER_DS));
136 asm volatile ("mov %0, %%fs" : : "rm" (0));
Jann Horn81fd9c12018-08-28 22:14:19 +0200137 return ex_handler_default(fixup, regs, trapnr, error_code, fault_addr);
Andy Lutomirski45e876f2016-04-26 12:23:26 -0700138}
139EXPORT_SYMBOL(ex_handler_clear_fs);
140
Tony Lucka05d54c2020-10-06 14:09:06 -0700141enum handler_type ex_get_fault_handler_type(unsigned long ip)
Tony Luck548acf12016-02-17 10:20:12 -0800142{
143 const struct exception_table_entry *e;
144 ex_handler_t handler;
145
146 e = search_exception_tables(ip);
147 if (!e)
Tony Lucka05d54c2020-10-06 14:09:06 -0700148 return EX_HANDLER_NONE;
Tony Luck548acf12016-02-17 10:20:12 -0800149 handler = ex_fixup_handler(e);
Tony Lucka05d54c2020-10-06 14:09:06 -0700150 if (handler == ex_handler_fault)
151 return EX_HANDLER_FAULT;
Youquan Song278b9172020-10-06 14:09:07 -0700152 else if (handler == ex_handler_uaccess || handler == ex_handler_copy)
Tony Lucka05d54c2020-10-06 14:09:06 -0700153 return EX_HANDLER_UACCESS;
154 else
155 return EX_HANDLER_OTHER;
Tony Luck548acf12016-02-17 10:20:12 -0800156}
157
Jann Horn81fd9c12018-08-28 22:14:19 +0200158int fixup_exception(struct pt_regs *regs, int trapnr, unsigned long error_code,
159 unsigned long fault_addr)
Tony Luck548acf12016-02-17 10:20:12 -0800160{
161 const struct exception_table_entry *e;
162 ex_handler_t handler;
Harvey Harrison6d485832008-01-30 13:31:41 +0100163
164#ifdef CONFIG_PNPBIOS
165 if (unlikely(SEGMENT_IS_PNP_CODE(regs->cs))) {
166 extern u32 pnp_bios_fault_eip, pnp_bios_fault_esp;
167 extern u32 pnp_bios_is_utter_crap;
168 pnp_bios_is_utter_crap = 1;
169 printk(KERN_CRIT "PNPBIOS fault.. attempting recovery.\n");
170 __asm__ volatile(
171 "movl %0, %%esp\n\t"
172 "jmp *%1\n\t"
173 : : "g" (pnp_bios_fault_esp), "g" (pnp_bios_fault_eip));
174 panic("do_trap: can't hit this");
175 }
176#endif
177
Tony Luck548acf12016-02-17 10:20:12 -0800178 e = search_exception_tables(regs->ip);
179 if (!e)
180 return 0;
H. Peter Anvin70627652012-04-20 17:12:48 -0700181
Tony Luck548acf12016-02-17 10:20:12 -0800182 handler = ex_fixup_handler(e);
Jann Horn81fd9c12018-08-28 22:14:19 +0200183 return handler(e, regs, trapnr, error_code, fault_addr);
Harvey Harrison6d485832008-01-30 13:31:41 +0100184}
H. Peter Anvin6a1ea272012-04-19 15:24:20 -0700185
Andy Lutomirski0e861fb2016-04-02 07:01:34 -0700186extern unsigned int early_recursion_flag;
187
H. Peter Anvin6a1ea272012-04-19 15:24:20 -0700188/* Restricted version used during very early boot */
Andy Lutomirski0e861fb2016-04-02 07:01:34 -0700189void __init early_fixup_exception(struct pt_regs *regs, int trapnr)
H. Peter Anvin6a1ea272012-04-19 15:24:20 -0700190{
Andy Lutomirski0d0efc02016-04-02 07:01:33 -0700191 /* Ignore early NMIs. */
192 if (trapnr == X86_TRAP_NMI)
Andy Lutomirski0e861fb2016-04-02 07:01:34 -0700193 return;
194
195 if (early_recursion_flag > 2)
196 goto halt_loop;
197
Andy Lutomirskifc0e81b2016-11-19 18:42:40 -0800198 /*
199 * Old CPUs leave the high bits of CS on the stack
200 * undefined. I'm not sure which CPUs do this, but at least
201 * the 486 DX works this way.
Juergen Gross42b3a4c2017-11-24 09:42:21 +0100202 * Xen pv domains are not using the default __KERNEL_CS.
Andy Lutomirskifc0e81b2016-11-19 18:42:40 -0800203 */
Juergen Gross42b3a4c2017-11-24 09:42:21 +0100204 if (!xen_pv_domain() && regs->cs != __KERNEL_CS)
Andy Lutomirski0e861fb2016-04-02 07:01:34 -0700205 goto fail;
Andy Lutomirski0d0efc02016-04-02 07:01:33 -0700206
Andy Lutomirski60a0e202016-04-04 08:46:22 -0700207 /*
208 * The full exception fixup machinery is available as soon as
209 * the early IDT is loaded. This means that it is the
210 * responsibility of extable users to either function correctly
211 * when handlers are invoked early or to simply avoid causing
212 * exceptions before they're ready to handle them.
213 *
214 * This is better than filtering which handlers can be used,
215 * because refusing to call a handler here is guaranteed to
216 * result in a hard-to-debug panic.
217 *
218 * Keep in mind that not all vectors actually get here. Early
Jann Horn81fd9c12018-08-28 22:14:19 +0200219 * page faults, for example, are special.
Andy Lutomirski60a0e202016-04-04 08:46:22 -0700220 */
Jann Horn81fd9c12018-08-28 22:14:19 +0200221 if (fixup_exception(regs, trapnr, regs->orig_ax, 0))
Andy Lutomirskiae7ef452016-04-02 07:01:35 -0700222 return;
Andy Lutomirski0e861fb2016-04-02 07:01:34 -0700223
Andy Lutomirski15a416e2020-06-11 20:26:38 -0700224 if (trapnr == X86_TRAP_UD) {
225 if (report_bug(regs->ip, regs) == BUG_TRAP_TYPE_WARN) {
226 /* Skip the ud2. */
227 regs->ip += LEN_UD2;
228 return;
229 }
230
231 /*
232 * If this was a BUG and report_bug returns or if this
233 * was just a normal #UD, we want to continue onward and
234 * crash.
235 */
236 }
Peter Zijlstra8a524f82017-06-12 13:52:46 +0200237
Andy Lutomirski0e861fb2016-04-02 07:01:34 -0700238fail:
239 early_printk("PANIC: early exception 0x%02x IP %lx:%lx error %lx cr2 0x%lx\n",
240 (unsigned)trapnr, (unsigned long)regs->cs, regs->ip,
241 regs->orig_ax, read_cr2());
242
243 show_regs(regs);
244
245halt_loop:
246 while (true)
247 halt();
H. Peter Anvin6a1ea272012-04-19 15:24:20 -0700248}