1e99b6f46SAndy Lutomirski // SPDX-License-Identifier: GPL-2.0
2e99b6f46SAndy Lutomirski #include <linux/mm.h>
3e99b6f46SAndy Lutomirski #include <linux/sched.h>
4e99b6f46SAndy Lutomirski #include <linux/sched/debug.h>
5e99b6f46SAndy Lutomirski #include <linux/init_task.h>
6e99b6f46SAndy Lutomirski #include <linux/fs.h>
7e99b6f46SAndy Lutomirski
8e99b6f46SAndy Lutomirski #include <linux/uaccess.h>
9e99b6f46SAndy Lutomirski #include <asm/processor.h>
10e99b6f46SAndy Lutomirski #include <asm/desc.h>
117d8d8cfdSAndy Lutomirski #include <asm/traps.h>
12*c9664839SArnd Bergmann #include <asm/doublefault.h>
13e99b6f46SAndy Lutomirski
14e99b6f46SAndy Lutomirski #define ptr_ok(x) ((x) > PAGE_OFFSET && (x) < PAGE_OFFSET + MAXMEM)
15e99b6f46SAndy Lutomirski
167d8d8cfdSAndy Lutomirski #define TSS(x) this_cpu_read(cpu_tss_rw.x86_tss.x)
177d8d8cfdSAndy Lutomirski
187d8d8cfdSAndy Lutomirski static void set_df_gdt_entry(unsigned int cpu);
197d8d8cfdSAndy Lutomirski
207d8d8cfdSAndy Lutomirski /*
217d8d8cfdSAndy Lutomirski * Called by double_fault with CR0.TS and EFLAGS.NT cleared. The CPU thinks
227d8d8cfdSAndy Lutomirski * we're running the doublefault task. Cannot return.
237d8d8cfdSAndy Lutomirski */
doublefault_shim(void)24c29c775aSThomas Gleixner asmlinkage noinstr void __noreturn doublefault_shim(void)
25e99b6f46SAndy Lutomirski {
267d8d8cfdSAndy Lutomirski unsigned long cr2;
277d8d8cfdSAndy Lutomirski struct pt_regs regs;
28e99b6f46SAndy Lutomirski
29dc4e0021SAndy Lutomirski BUILD_BUG_ON(sizeof(struct doublefault_stack) != PAGE_SIZE);
30dc4e0021SAndy Lutomirski
317d8d8cfdSAndy Lutomirski cr2 = native_read_cr2();
32e99b6f46SAndy Lutomirski
337d8d8cfdSAndy Lutomirski /* Reset back to the normal kernel task. */
347d8d8cfdSAndy Lutomirski force_reload_TR();
357d8d8cfdSAndy Lutomirski set_df_gdt_entry(smp_processor_id());
36e99b6f46SAndy Lutomirski
377d8d8cfdSAndy Lutomirski trace_hardirqs_off();
38e99b6f46SAndy Lutomirski
397d8d8cfdSAndy Lutomirski /*
407d8d8cfdSAndy Lutomirski * Fill in pt_regs. A downside of doing this in C is that the unwinder
417d8d8cfdSAndy Lutomirski * won't see it (no ENCODE_FRAME_POINTER), so a nested stack dump
427d8d8cfdSAndy Lutomirski * won't successfully unwind to the source of the double fault.
43c29c775aSThomas Gleixner * The main dump from exc_double_fault() is fine, though, since it
447d8d8cfdSAndy Lutomirski * uses these regs directly.
457d8d8cfdSAndy Lutomirski *
467d8d8cfdSAndy Lutomirski * If anyone ever cares, this could be moved to asm.
477d8d8cfdSAndy Lutomirski */
487d8d8cfdSAndy Lutomirski regs.ss = TSS(ss);
497d8d8cfdSAndy Lutomirski regs.__ssh = 0;
507d8d8cfdSAndy Lutomirski regs.sp = TSS(sp);
517d8d8cfdSAndy Lutomirski regs.flags = TSS(flags);
527d8d8cfdSAndy Lutomirski regs.cs = TSS(cs);
537d8d8cfdSAndy Lutomirski /* We won't go through the entry asm, so we can leave __csh as 0. */
547d8d8cfdSAndy Lutomirski regs.__csh = 0;
557d8d8cfdSAndy Lutomirski regs.ip = TSS(ip);
567d8d8cfdSAndy Lutomirski regs.orig_ax = 0;
577d8d8cfdSAndy Lutomirski regs.gs = TSS(gs);
587d8d8cfdSAndy Lutomirski regs.__gsh = 0;
597d8d8cfdSAndy Lutomirski regs.fs = TSS(fs);
607d8d8cfdSAndy Lutomirski regs.__fsh = 0;
617d8d8cfdSAndy Lutomirski regs.es = TSS(es);
627d8d8cfdSAndy Lutomirski regs.__esh = 0;
637d8d8cfdSAndy Lutomirski regs.ds = TSS(ds);
647d8d8cfdSAndy Lutomirski regs.__dsh = 0;
657d8d8cfdSAndy Lutomirski regs.ax = TSS(ax);
667d8d8cfdSAndy Lutomirski regs.bp = TSS(bp);
677d8d8cfdSAndy Lutomirski regs.di = TSS(di);
687d8d8cfdSAndy Lutomirski regs.si = TSS(si);
697d8d8cfdSAndy Lutomirski regs.dx = TSS(dx);
707d8d8cfdSAndy Lutomirski regs.cx = TSS(cx);
717d8d8cfdSAndy Lutomirski regs.bx = TSS(bx);
72e99b6f46SAndy Lutomirski
73c29c775aSThomas Gleixner exc_double_fault(®s, 0, cr2);
74e99b6f46SAndy Lutomirski
757d8d8cfdSAndy Lutomirski /*
767d8d8cfdSAndy Lutomirski * x86_32 does not save the original CR3 anywhere on a task switch.
777d8d8cfdSAndy Lutomirski * This means that, even if we wanted to return, we would need to find
787d8d8cfdSAndy Lutomirski * some way to reconstruct CR3. We could make a credible guess based
797d8d8cfdSAndy Lutomirski * on cpu_tlbstate, but that would be racy and would not account for
807d8d8cfdSAndy Lutomirski * PTI.
817d8d8cfdSAndy Lutomirski */
827d8d8cfdSAndy Lutomirski panic("cannot return from double fault\n");
83e99b6f46SAndy Lutomirski }
84e99b6f46SAndy Lutomirski
85dc4e0021SAndy Lutomirski DEFINE_PER_CPU_PAGE_ALIGNED(struct doublefault_stack, doublefault_stack) = {
86dc4e0021SAndy Lutomirski .tss = {
87dc4e0021SAndy Lutomirski /*
88dc4e0021SAndy Lutomirski * No sp0 or ss0 -- we never run CPL != 0 with this TSS
89dc4e0021SAndy Lutomirski * active. sp is filled in later.
90dc4e0021SAndy Lutomirski */
91e99b6f46SAndy Lutomirski .ldt = 0,
92e99b6f46SAndy Lutomirski .io_bitmap_base = IO_BITMAP_OFFSET_INVALID,
93e99b6f46SAndy Lutomirski
94c29c775aSThomas Gleixner .ip = (unsigned long) asm_exc_double_fault,
957d8d8cfdSAndy Lutomirski .flags = X86_EFLAGS_FIXED,
96e99b6f46SAndy Lutomirski .es = __USER_DS,
97e99b6f46SAndy Lutomirski .cs = __KERNEL_CS,
98e99b6f46SAndy Lutomirski .ss = __KERNEL_DS,
99e99b6f46SAndy Lutomirski .ds = __USER_DS,
100e99b6f46SAndy Lutomirski .fs = __KERNEL_PERCPU,
1013fb0fdb3SAndy Lutomirski .gs = 0,
102e99b6f46SAndy Lutomirski
103e99b6f46SAndy Lutomirski .__cr3 = __pa_nodebug(swapper_pg_dir),
104dc4e0021SAndy Lutomirski },
105e99b6f46SAndy Lutomirski };
106dc4e0021SAndy Lutomirski
set_df_gdt_entry(unsigned int cpu)1077d8d8cfdSAndy Lutomirski static void set_df_gdt_entry(unsigned int cpu)
1087d8d8cfdSAndy Lutomirski {
1097d8d8cfdSAndy Lutomirski /* Set up doublefault TSS pointer in the GDT */
1107d8d8cfdSAndy Lutomirski __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS,
1117d8d8cfdSAndy Lutomirski &get_cpu_entry_area(cpu)->doublefault_stack.tss);
1127d8d8cfdSAndy Lutomirski
1137d8d8cfdSAndy Lutomirski }
1147d8d8cfdSAndy Lutomirski
doublefault_init_cpu_tss(void)115dc4e0021SAndy Lutomirski void doublefault_init_cpu_tss(void)
116dc4e0021SAndy Lutomirski {
117dc4e0021SAndy Lutomirski unsigned int cpu = smp_processor_id();
118dc4e0021SAndy Lutomirski struct cpu_entry_area *cea = get_cpu_entry_area(cpu);
119dc4e0021SAndy Lutomirski
120dc4e0021SAndy Lutomirski /*
121dc4e0021SAndy Lutomirski * The linker isn't smart enough to initialize percpu variables that
122dc4e0021SAndy Lutomirski * point to other places in percpu space.
123dc4e0021SAndy Lutomirski */
124dc4e0021SAndy Lutomirski this_cpu_write(doublefault_stack.tss.sp,
125dc4e0021SAndy Lutomirski (unsigned long)&cea->doublefault_stack.stack +
126dc4e0021SAndy Lutomirski sizeof(doublefault_stack.stack));
127dc4e0021SAndy Lutomirski
1287d8d8cfdSAndy Lutomirski set_df_gdt_entry(cpu);
129dc4e0021SAndy Lutomirski }
130