Skip to content

Commit e5b3fc1

Browse files
committed
Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull x86 fixes from Ingo Molnar: "Various fixes: - Fix the PAT performance regression that downgraded write-combining device memory regions to uncached. - There's been a number of bugs in 32-bit double fault handling - hopefully all fixed now. - Fix an LDT crash - Fix an FPU over-optimization that broke with GCC9 code optimizations. - Misc cleanups" * 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: x86/mm/pat: Fix off-by-one bugs in interval tree search x86/ioperm: Save an indentation level in tss_update_io_bitmap() x86/fpu: Don't cache access to fpu_fpregs_owner_ctx x86/entry/32: Remove unused 'restore_all_notrace' local label x86/ptrace: Document FSBASE and GSBASE ABI oddities x86/ptrace: Remove set_segment_reg() implementations for current x86/traps: die() instead of panicking on a double fault x86/doublefault/32: Rewrite the x86_32 #DF handler and unify with 64-bit x86/doublefault/32: Move #DF stack and TSS to cpu_entry_area x86/doublefault/32: Rename doublefault.c to doublefault_32.c x86/traps: Disentangle the 32-bit and 64-bit doublefault code lkdtm: Add a DOUBLE_FAULT crash type on x86 selftests/x86/single_step_syscall: Check SYSENTER directly x86/mm/32: Sync only to VMALLOC_END in vmalloc_sync_all()
2 parents b7fcf31 + 91298f1 commit e5b3fc1

23 files changed

Lines changed: 467 additions & 171 deletions

‎arch/x86/Kconfig.debug‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ config DEBUG_WX
117117

118118
config DOUBLEFAULT
119119
default y
120-
bool "Enable doublefault exception handler" if EXPERT
120+
bool "Enable doublefault exception handler" if EXPERT && X86_32
121121
---help---
122122
This option allows trapping of rare doublefault exceptions that
123123
would otherwise cause a system to silently reboot. Disabling this

‎arch/x86/entry/entry_32.S‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,6 @@ SYM_FUNC_START(entry_INT80_32)
10901090
restore_all:
10911091
TRACE_IRQS_IRET
10921092
SWITCH_TO_ENTRY_STACK
1093-
.Lrestore_all_notrace:
10941093
CHECK_AND_APPLY_ESPFIX
10951094
.Lrestore_nocheck:
10961095
/* Switch back to user CR3 */
@@ -1537,6 +1536,48 @@ SYM_CODE_START(debug)
15371536
jmp common_exception
15381537
SYM_CODE_END(debug)
15391538

1539+
#ifdef CONFIG_DOUBLEFAULT
1540+
SYM_CODE_START(double_fault)
1541+
1:
1542+
/*
1543+
* This is a task gate handler, not an interrupt gate handler.
1544+
* The error code is on the stack, but the stack is otherwise
1545+
* empty. Interrupts are off. Our state is sane with the following
1546+
* exceptions:
1547+
*
1548+
* - CR0.TS is set. "TS" literally means "task switched".
1549+
* - EFLAGS.NT is set because we're a "nested task".
1550+
* - The doublefault TSS has back_link set and has been marked busy.
1551+
* - TR points to the doublefault TSS and the normal TSS is busy.
1552+
* - CR3 is the normal kernel PGD. This would be delightful, except
1553+
* that the CPU didn't bother to save the old CR3 anywhere. This
1554+
* would make it very awkward to return back to the context we came
1555+
* from.
1556+
*
1557+
* The rest of EFLAGS is sanitized for us, so we don't need to
1558+
* worry about AC or DF.
1559+
*
1560+
* Don't even bother popping the error code. It's always zero,
1561+
* and ignoring it makes us a bit more robust against buggy
1562+
* hypervisor task gate implementations.
1563+
*
1564+
* We will manually undo the task switch instead of doing a
1565+
* task-switching IRET.
1566+
*/
1567+
1568+
clts /* clear CR0.TS */
1569+
pushl $X86_EFLAGS_FIXED
1570+
popfl /* clear EFLAGS.NT */
1571+
1572+
call doublefault_shim
1573+
1574+
/* We don't support returning, so we have no IRET here. */
1575+
1:
1576+
hlt
1577+
jmp 1b
1578+
SYM_CODE_END(double_fault)
1579+
#endif
1580+
15401581
/*
15411582
* NMI is doubly nasty. It can happen on the first instruction of
15421583
* entry_SYSENTER_32 (just like #DB), but it can also interrupt the beginning

‎arch/x86/include/asm/cpu_entry_area.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ enum exception_stack_ordering {
6565

6666
#endif
6767

68+
#ifdef CONFIG_X86_32
69+
struct doublefault_stack {
70+
unsigned long stack[(PAGE_SIZE - sizeof(struct x86_hw_tss)) / sizeof(unsigned long)];
71+
struct x86_hw_tss tss;
72+
} __aligned(PAGE_SIZE);
73+
#endif
74+
6875
/*
6976
* cpu_entry_area is a percpu region that contains things needed by the CPU
7077
* and early entry/exit code. Real types aren't used for all fields here
@@ -86,6 +93,11 @@ struct cpu_entry_area {
8693
#endif
8794
struct entry_stack_page entry_stack_page;
8895

96+
#ifdef CONFIG_X86_32
97+
char guard_doublefault_stack[PAGE_SIZE];
98+
struct doublefault_stack doublefault_stack;
99+
#endif
100+
89101
/*
90102
* On x86_64, the TSS is mapped RO. On x86_32, it's mapped RW because
91103
* we need task switches to work, and task switches write to the TSS.

‎arch/x86/include/asm/doublefault.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _ASM_X86_DOUBLEFAULT_H
3+
#define _ASM_X86_DOUBLEFAULT_H
4+
5+
#if defined(CONFIG_X86_32) && defined(CONFIG_DOUBLEFAULT)
6+
extern void doublefault_init_cpu_tss(void);
7+
#else
8+
static inline void doublefault_init_cpu_tss(void)
9+
{
10+
}
11+
#endif
12+
13+
#endif /* _ASM_X86_DOUBLEFAULT_H */

‎arch/x86/include/asm/fpu/internal.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ static inline void __fpu_invalidate_fpregs_state(struct fpu *fpu)
509509

510510
static inline int fpregs_state_valid(struct fpu *fpu, unsigned int cpu)
511511
{
512-
return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu;
512+
return fpu == this_cpu_read(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu;
513513
}
514514

515515
/*

‎arch/x86/include/asm/pgtable_32_types.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ extern bool __vmalloc_start_set; /* set once high_memory is set */
4141
#endif
4242

4343
/*
44-
* Define this here and validate with BUILD_BUG_ON() in pgtable_32.c
45-
* to avoid include recursion hell
44+
* This is an upper bound on sizeof(struct cpu_entry_area) / PAGE_SIZE.
45+
* Define this here and validate with BUILD_BUG_ON() in cpu_entry_area.c
46+
* to avoid include recursion hell.
4647
*/
47-
#define CPU_ENTRY_AREA_PAGES (NR_CPUS * 41)
48+
#define CPU_ENTRY_AREA_PAGES (NR_CPUS * 43)
4849

4950
/* The +1 is for the readonly IDT page: */
5051
#define CPU_ENTRY_AREA_BASE \

‎arch/x86/include/asm/processor.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ enum cpuid_regs_idx {
166166
extern struct cpuinfo_x86 boot_cpu_data;
167167
extern struct cpuinfo_x86 new_cpu_data;
168168

169-
extern struct x86_hw_tss doublefault_tss;
170169
extern __u32 cpu_caps_cleared[NCAPINTS + NBUGINTS];
171170
extern __u32 cpu_caps_set[NCAPINTS + NBUGINTS];
172171

@@ -997,7 +996,6 @@ bool xen_set_default_idle(void);
997996
#endif
998997

999998
void stop_this_cpu(void *dummy);
1000-
void df_debug(struct pt_regs *regs, long error_code);
1001999
void microcode_check(void);
10021000

10031001
enum l1tf_mitigations {

‎arch/x86/include/asm/traps.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ dotraplinkage void do_overflow(struct pt_regs *regs, long error_code);
6969
dotraplinkage void do_bounds(struct pt_regs *regs, long error_code);
7070
dotraplinkage void do_invalid_op(struct pt_regs *regs, long error_code);
7171
dotraplinkage void do_device_not_available(struct pt_regs *regs, long error_code);
72+
#if defined(CONFIG_X86_64) || defined(CONFIG_DOUBLEFAULT)
73+
dotraplinkage void do_double_fault(struct pt_regs *regs, long error_code, unsigned long cr2);
74+
#endif
7275
dotraplinkage void do_coprocessor_segment_overrun(struct pt_regs *regs, long error_code);
7376
dotraplinkage void do_invalid_TSS(struct pt_regs *regs, long error_code);
7477
dotraplinkage void do_segment_not_present(struct pt_regs *regs, long error_code);

‎arch/x86/kernel/Makefile‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ obj-$(CONFIG_KEXEC_FILE) += kexec-bzimage64.o
100100
obj-$(CONFIG_CRASH_DUMP) += crash_dump_$(BITS).o
101101
obj-y += kprobes/
102102
obj-$(CONFIG_MODULES) += module.o
103-
obj-$(CONFIG_DOUBLEFAULT) += doublefault.o
103+
ifeq ($(CONFIG_X86_32),y)
104+
obj-$(CONFIG_DOUBLEFAULT) += doublefault_32.o
105+
endif
104106
obj-$(CONFIG_KGDB) += kgdb.o
105107
obj-$(CONFIG_VM86) += vm86_32.o
106108
obj-$(CONFIG_EARLY_PRINTK) += early_printk.o

‎arch/x86/kernel/cpu/common.c‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <asm/stackprotector.h>
2525
#include <asm/perf_event.h>
2626
#include <asm/mmu_context.h>
27+
#include <asm/doublefault.h>
2728
#include <asm/archrandom.h>
2829
#include <asm/hypervisor.h>
2930
#include <asm/processor.h>
@@ -1814,8 +1815,6 @@ static inline void tss_setup_ist(struct tss_struct *tss)
18141815
tss->x86_tss.ist[IST_INDEX_MCE] = __this_cpu_ist_top_va(MCE);
18151816
}
18161817

1817-
static inline void gdt_setup_doublefault_tss(int cpu) { }
1818-
18191818
#else /* CONFIG_X86_64 */
18201819

18211820
static inline void setup_getcpu(int cpu) { }
@@ -1827,13 +1826,6 @@ static inline void ucode_cpu_init(int cpu)
18271826

18281827
static inline void tss_setup_ist(struct tss_struct *tss) { }
18291828

1830-
static inline void gdt_setup_doublefault_tss(int cpu)
1831-
{
1832-
#ifdef CONFIG_DOUBLEFAULT
1833-
/* Set up the doublefault TSS pointer in the GDT */
1834-
__set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
1835-
#endif
1836-
}
18371829
#endif /* !CONFIG_X86_64 */
18381830

18391831
static inline void tss_setup_io_bitmap(struct tss_struct *tss)
@@ -1923,7 +1915,7 @@ void cpu_init(void)
19231915
clear_all_debug_regs();
19241916
dbg_restore_debug_regs();
19251917

1926-
gdt_setup_doublefault_tss(cpu);
1918+
doublefault_init_cpu_tss();
19271919

19281920
fpu__init_cpu();
19291921

0 commit comments

Comments
 (0)