Skip to content

Commit 8f34c5b

Browse files
KAGA-KOKOsuryasaimadhu
authored andcommitted
x86/exceptions: Make IST index zero based
The defines for the exception stack (IST) array in the TSS are using the SDM convention IST1 - IST7. That causes all sorts of code to subtract 1 for array indices related to IST. That's confusing at best and does not provide any value. Make the indices zero based and fixup the usage sites. The only code which needs to adjust the 0 based index is the interrupt descriptor setup which needs to add 1 now. Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Borislav Petkov <bp@suse.de> Reviewed-by: Sean Christopherson <sean.j.christopherson@intel.com> Cc: Andy Lutomirski <luto@kernel.org> Cc: Baoquan He <bhe@redhat.com> Cc: "Chang S. Bae" <chang.seok.bae@intel.com> Cc: Dave Hansen <dave.hansen@linux.intel.com> Cc: Dominik Brodowski <linux@dominikbrodowski.net> Cc: Dou Liyang <douly.fnst@cn.fujitsu.com> Cc: "H. Peter Anvin" <hpa@zytor.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: Jonathan Corbet <corbet@lwn.net> Cc: Josh Poimboeuf <jpoimboe@redhat.com> Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com> Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com> Cc: linux-doc@vger.kernel.org Cc: Nicolai Stange <nstange@suse.de> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Qian Cai <cai@lca.pw> Cc: x86-ml <x86@kernel.org> Link: https://lkml.kernel.org/r/20190414160144.331772825@linutronix.de
1 parent 3084221 commit 8f34c5b

8 files changed

Lines changed: 34 additions & 28 deletions

File tree

‎Documentation/x86/kernel-stacks‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ If that assumption is ever broken then the stacks will become corrupt.
5959

6060
The currently assigned IST stacks are :-
6161

62-
* DOUBLEFAULT_STACK. EXCEPTION_STKSZ (PAGE_SIZE).
62+
* ESTACK_DF. EXCEPTION_STKSZ (PAGE_SIZE).
6363

6464
Used for interrupt 8 - Double Fault Exception (#DF).
6565

@@ -68,15 +68,15 @@ The currently assigned IST stacks are :-
6868
Using a separate stack allows the kernel to recover from it well enough
6969
in many cases to still output an oops.
7070

71-
* NMI_STACK. EXCEPTION_STKSZ (PAGE_SIZE).
71+
* ESTACK_NMI. EXCEPTION_STKSZ (PAGE_SIZE).
7272

7373
Used for non-maskable interrupts (NMI).
7474

7575
NMI can be delivered at any time, including when the kernel is in the
7676
middle of switching stacks. Using IST for NMI events avoids making
7777
assumptions about the previous state of the kernel stack.
7878

79-
* DEBUG_STACK. DEBUG_STKSZ
79+
* ESTACK_DB. DEBUG_STKSZ
8080

8181
Used for hardware debug interrupts (interrupt 1) and for software
8282
debug interrupts (INT3).
@@ -86,7 +86,7 @@ The currently assigned IST stacks are :-
8686
avoids making assumptions about the previous state of the kernel
8787
stack.
8888

89-
* MCE_STACK. EXCEPTION_STKSZ (PAGE_SIZE).
89+
* ESTACK_MCE. EXCEPTION_STKSZ (PAGE_SIZE).
9090

9191
Used for interrupt 18 - Machine Check Exception (#MC).
9292

‎arch/x86/entry/entry_64.S‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ apicinterrupt IRQ_WORK_VECTOR irq_work_interrupt smp_irq_work_interrupt
841841
/*
842842
* Exception entry points.
843843
*/
844-
#define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss_rw) + (TSS_ist + ((x) - 1) * 8)
844+
#define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss_rw) + (TSS_ist + (x) * 8)
845845

846846
/**
847847
* idtentry - Generate an IDT entry stub
@@ -1129,7 +1129,7 @@ apicinterrupt3 HYPERV_STIMER0_VECTOR \
11291129
hv_stimer0_callback_vector hv_stimer0_vector_handler
11301130
#endif /* CONFIG_HYPERV */
11311131

1132-
idtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
1132+
idtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=ESTACK_DB
11331133
idtentry int3 do_int3 has_error_code=0
11341134
idtentry stack_segment do_stack_segment has_error_code=1
11351135

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@
2424
#define IRQ_STACK_ORDER (2 + KASAN_STACK_ORDER)
2525
#define IRQ_STACK_SIZE (PAGE_SIZE << IRQ_STACK_ORDER)
2626

27-
#define DOUBLEFAULT_STACK 1
28-
#define NMI_STACK 2
29-
#define DEBUG_STACK 3
30-
#define MCE_STACK 4
31-
#define N_EXCEPTION_STACKS 4 /* hw limit: 7 */
27+
/*
28+
* The index for the tss.ist[] array. The hardware limit is 7 entries.
29+
*/
30+
#define ESTACK_DF 0
31+
#define ESTACK_NMI 1
32+
#define ESTACK_DB 2
33+
#define ESTACK_MCE 3
34+
#define N_EXCEPTION_STACKS 4
3235

3336
/*
3437
* Set __PAGE_OFFSET to the most negative possible address +

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ DEFINE_PER_CPU(struct cpu_entry_area *, cpu_entry_area);
516516
*/
517517
static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = {
518518
[0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ,
519-
[DEBUG_STACK - 1] = DEBUG_STKSZ
519+
[ESTACK_DB] = DEBUG_STKSZ
520520
};
521521
#endif
522522

@@ -1760,7 +1760,7 @@ void cpu_init(void)
17601760
estacks += exception_stack_sizes[v];
17611761
oist->ist[v] = t->x86_tss.ist[v] =
17621762
(unsigned long)estacks;
1763-
if (v == DEBUG_STACK-1)
1763+
if (v == ESTACK_DB)
17641764
per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks;
17651765
}
17661766
}

‎arch/x86/kernel/dumpstack_64.c‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818

1919
#include <asm/stacktrace.h>
2020

21-
static char *exception_stack_names[N_EXCEPTION_STACKS] = {
22-
[ DOUBLEFAULT_STACK-1 ] = "#DF",
23-
[ NMI_STACK-1 ] = "NMI",
24-
[ DEBUG_STACK-1 ] = "#DB",
25-
[ MCE_STACK-1 ] = "#MC",
21+
static const char *exception_stack_names[N_EXCEPTION_STACKS] = {
22+
[ ESTACK_DF ] = "#DF",
23+
[ ESTACK_NMI ] = "NMI",
24+
[ ESTACK_DB ] = "#DB",
25+
[ ESTACK_MCE ] = "#MC",
2626
};
2727

28-
static unsigned long exception_stack_sizes[N_EXCEPTION_STACKS] = {
28+
static const unsigned long exception_stack_sizes[N_EXCEPTION_STACKS] = {
2929
[0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ,
30-
[DEBUG_STACK - 1] = DEBUG_STKSZ
30+
[ESTACK_DB] = DEBUG_STKSZ
3131
};
3232

3333
const char *stack_type_name(enum stack_type type)

‎arch/x86/kernel/idt.c‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ struct idt_data {
4141
#define SYSG(_vector, _addr) \
4242
G(_vector, _addr, DEFAULT_STACK, GATE_INTERRUPT, DPL3, __KERNEL_CS)
4343

44-
/* Interrupt gate with interrupt stack */
44+
/*
45+
* Interrupt gate with interrupt stack. The _ist index is the index in
46+
* the tss.ist[] array, but for the descriptor it needs to start at 1.
47+
*/
4548
#define ISTG(_vector, _addr, _ist) \
46-
G(_vector, _addr, _ist, GATE_INTERRUPT, DPL0, __KERNEL_CS)
49+
G(_vector, _addr, _ist + 1, GATE_INTERRUPT, DPL0, __KERNEL_CS)
4750

4851
/* Task gate */
4952
#define TSKG(_vector, _gdt) \
@@ -180,11 +183,11 @@ gate_desc debug_idt_table[IDT_ENTRIES] __page_aligned_bss;
180183
* cpu_init() when the TSS has been initialized.
181184
*/
182185
static const __initconst struct idt_data ist_idts[] = {
183-
ISTG(X86_TRAP_DB, debug, DEBUG_STACK),
184-
ISTG(X86_TRAP_NMI, nmi, NMI_STACK),
185-
ISTG(X86_TRAP_DF, double_fault, DOUBLEFAULT_STACK),
186+
ISTG(X86_TRAP_DB, debug, ESTACK_DB),
187+
ISTG(X86_TRAP_NMI, nmi, ESTACK_NMI),
188+
ISTG(X86_TRAP_DF, double_fault, ESTACK_DF),
186189
#ifdef CONFIG_X86_MCE
187-
ISTG(X86_TRAP_MC, &machine_check, MCE_STACK),
190+
ISTG(X86_TRAP_MC, &machine_check, ESTACK_MCE),
188191
#endif
189192
};
190193

‎arch/x86/kernel/irq_64.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ static inline void stack_overflow_check(struct pt_regs *regs)
6161
return;
6262

6363
oist = this_cpu_ptr(&orig_ist);
64-
estack_top = (u64)oist->ist[DEBUG_STACK];
64+
estack_top = (u64)oist->ist[ESTACK_DB];
6565
estack_bottom = estack_top - DEBUG_STKSZ + STACK_MARGIN;
6666
if (regs->sp >= estack_bottom && regs->sp <= estack_top)
6767
return;

‎arch/x86/mm/fault.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ no_context(struct pt_regs *regs, unsigned long error_code,
793793
if (is_vmalloc_addr((void *)address) &&
794794
(((unsigned long)tsk->stack - 1 - address < PAGE_SIZE) ||
795795
address - ((unsigned long)tsk->stack + THREAD_SIZE) < PAGE_SIZE)) {
796-
unsigned long stack = this_cpu_read(orig_ist.ist[DOUBLEFAULT_STACK]) - sizeof(void *);
796+
unsigned long stack = this_cpu_read(orig_ist.ist[ESTACK_DF]) - sizeof(void *);
797797
/*
798798
* We're likely to be running with very little stack space
799799
* left. It's plausible that we'd hit this condition but

0 commit comments

Comments
 (0)