Skip to content

Commit 3e77abd

Browse files
committed
x86/idt: Consolidate idt functionality
- Move load_current_idt() out of line and replace the hideous comment with a lockdep assert. This allows to make idt_table and idt_descr static. - Mark idt_table read only after the IDT initialization is complete. - Shuffle code around to consolidate the #ifdef sections into one. - Adapt the F00F bug code. Signed-off-by: Thomas Gleixner <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 00229a5 commit 3e77abd

File tree

3 files changed

+44
-52
lines changed

3 files changed

+44
-52
lines changed

arch/x86/include/asm/desc.h

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ static inline void fill_ldt(struct desc_struct *desc, const struct user_desc *in
4040
desc->l = 0;
4141
}
4242

43-
extern struct desc_ptr idt_descr;
44-
extern gate_desc idt_table[];
45-
4643
struct gdt_page {
4744
struct desc_struct gdt[GDT_ENTRIES];
4845
} __attribute__((aligned(PAGE_SIZE)));
@@ -388,22 +385,12 @@ void alloc_intr_gate(unsigned int n, const void *addr);
388385

389386
extern unsigned long system_vectors[];
390387

391-
/*
392-
* The load_current_idt() must be called with interrupts disabled
393-
* to avoid races. That way the IDT will always be set back to the expected
394-
* descriptor. It's also called when a CPU is being initialized, and
395-
* that doesn't need to disable interrupts, as nothing should be
396-
* bothering the CPU then.
397-
*/
398-
static __always_inline void load_current_idt(void)
399-
{
400-
load_idt((const struct desc_ptr *)&idt_descr);
401-
}
402-
388+
extern void load_current_idt(void);
403389
extern void idt_setup_early_handler(void);
404390
extern void idt_setup_early_traps(void);
405391
extern void idt_setup_traps(void);
406392
extern void idt_setup_apic_and_irq_gates(void);
393+
extern bool idt_is_f00f_address(unsigned long address);
407394

408395
#ifdef CONFIG_X86_64
409396
extern void idt_setup_early_pf(void);

arch/x86/kernel/idt.c

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <linux/interrupt.h>
66

77
#include <asm/cpu_entry_area.h>
8+
#include <asm/set_memory.h>
89
#include <asm/traps.h>
910
#include <asm/proto.h>
1011
#include <asm/desc.h>
@@ -156,37 +157,25 @@ static const __initconst struct idt_data apic_idts[] = {
156157
#endif
157158
};
158159

159-
#ifdef CONFIG_X86_64
160-
/*
161-
* Early traps running on the DEFAULT_STACK because the other interrupt
162-
* stacks work only after cpu_init().
163-
*/
164-
static const __initconst struct idt_data early_pf_idts[] = {
165-
INTG(X86_TRAP_PF, asm_exc_page_fault),
166-
};
167-
#endif
168-
169-
/* Must be page-aligned because the real IDT is used in a fixmap. */
170-
gate_desc idt_table[IDT_ENTRIES] __page_aligned_bss;
160+
/* Must be page-aligned because the real IDT is used in the cpu entry area */
161+
static gate_desc idt_table[IDT_ENTRIES] __page_aligned_bss;
171162

172163
struct desc_ptr idt_descr __ro_after_init = {
173164
.size = IDT_TABLE_SIZE - 1,
174165
.address = (unsigned long) idt_table,
175166
};
176167

177-
#ifdef CONFIG_X86_64
178-
/*
179-
* The exceptions which use Interrupt stacks. They are setup after
180-
* cpu_init() when the TSS has been initialized.
181-
*/
182-
static const __initconst struct idt_data ist_idts[] = {
183-
ISTG(X86_TRAP_DB, asm_exc_debug, IST_INDEX_DB),
184-
ISTG(X86_TRAP_NMI, asm_exc_nmi, IST_INDEX_NMI),
185-
ISTG(X86_TRAP_DF, asm_exc_double_fault, IST_INDEX_DF),
186-
#ifdef CONFIG_X86_MCE
187-
ISTG(X86_TRAP_MC, asm_exc_machine_check, IST_INDEX_MCE),
188-
#endif
189-
};
168+
void load_current_idt(void)
169+
{
170+
lockdep_assert_irqs_disabled();
171+
load_idt(&idt_descr);
172+
}
173+
174+
#ifdef CONFIG_X86_F00F_BUG
175+
bool idt_is_f00f_address(unsigned long address)
176+
{
177+
return ((address - idt_descr.address) >> 3) == 6;
178+
}
190179
#endif
191180

192181
static inline void idt_init_desc(gate_desc *gate, const struct idt_data *d)
@@ -255,6 +244,27 @@ void __init idt_setup_traps(void)
255244
}
256245

257246
#ifdef CONFIG_X86_64
247+
/*
248+
* Early traps running on the DEFAULT_STACK because the other interrupt
249+
* stacks work only after cpu_init().
250+
*/
251+
static const __initconst struct idt_data early_pf_idts[] = {
252+
INTG(X86_TRAP_PF, asm_exc_page_fault),
253+
};
254+
255+
/*
256+
* The exceptions which use Interrupt stacks. They are setup after
257+
* cpu_init() when the TSS has been initialized.
258+
*/
259+
static const __initconst struct idt_data ist_idts[] = {
260+
ISTG(X86_TRAP_DB, asm_exc_debug, IST_INDEX_DB),
261+
ISTG(X86_TRAP_NMI, asm_exc_nmi, IST_INDEX_NMI),
262+
ISTG(X86_TRAP_DF, asm_exc_double_fault, IST_INDEX_DF),
263+
#ifdef CONFIG_X86_MCE
264+
ISTG(X86_TRAP_MC, asm_exc_machine_check, IST_INDEX_MCE),
265+
#endif
266+
};
267+
258268
/**
259269
* idt_setup_early_pf - Initialize the idt table with early pagefault handler
260270
*
@@ -325,6 +335,9 @@ void __init idt_setup_apic_and_irq_gates(void)
325335
idt_map_in_cea();
326336
load_idt(&idt_descr);
327337

338+
/* Make the IDT table read only */
339+
set_memory_ro((unsigned long)&idt_table, 1);
340+
328341
idt_setup_done = true;
329342
}
330343

arch/x86/mm/fault.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -414,21 +414,13 @@ static int is_errata100(struct pt_regs *regs, unsigned long address)
414414
return 0;
415415
}
416416

417+
/* Pentium F0 0F C7 C8 bug workaround: */
417418
static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
418419
{
419420
#ifdef CONFIG_X86_F00F_BUG
420-
unsigned long nr;
421-
422-
/*
423-
* Pentium F0 0F C7 C8 bug workaround:
424-
*/
425-
if (boot_cpu_has_bug(X86_BUG_F00F)) {
426-
nr = (address - idt_descr.address) >> 3;
427-
428-
if (nr == 6) {
429-
handle_invalid_op(regs);
430-
return 1;
431-
}
421+
if (boot_cpu_has_bug(X86_BUG_F00F) && idt_is_f00f_address(address)) {
422+
handle_invalid_op(regs);
423+
return 1;
432424
}
433425
#endif
434426
return 0;

0 commit comments

Comments
 (0)