xref: /linux/arch/x86/boot/startup/gdt_idt.c (revision ab93e0dd72c37d378dd936f031ffb83ff2bd87ce)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/linkage.h>
4 #include <linux/types.h>
5 
6 #include <asm/desc.h>
7 #include <asm/init.h>
8 #include <asm/setup.h>
9 #include <asm/sev.h>
10 #include <asm/trapnr.h>
11 
12 /*
13  * Data structures and code used for IDT setup in head_64.S. The bringup-IDT is
14  * used until the idt_table takes over. On the boot CPU this happens in
15  * x86_64_start_kernel(), on secondary CPUs in start_secondary(). In both cases
16  * this happens in the functions called from head_64.S.
17  *
18  * The idt_table can't be used that early because all the code modifying it is
19  * in idt.c and can be instrumented by tracing or KASAN, which both don't work
20  * during early CPU bringup. Also the idt_table has the runtime vectors
21  * configured which require certain CPU state to be setup already (like TSS),
22  * which also hasn't happened yet in early CPU bringup.
23  */
24 static gate_desc bringup_idt_table[NUM_EXCEPTION_VECTORS] __page_aligned_data;
25 
26 /* This may run while still in the direct mapping */
startup_64_load_idt(void * vc_handler)27 void __head startup_64_load_idt(void *vc_handler)
28 {
29 	struct desc_ptr desc = {
30 		.address = (unsigned long)rip_rel_ptr(bringup_idt_table),
31 		.size    = sizeof(bringup_idt_table) - 1,
32 	};
33 	struct idt_data data;
34 	gate_desc idt_desc;
35 
36 	/* @vc_handler is set only for a VMM Communication Exception */
37 	if (vc_handler) {
38 		init_idt_data(&data, X86_TRAP_VC, vc_handler);
39 		idt_init_desc(&idt_desc, &data);
40 		native_write_idt_entry((gate_desc *)desc.address, X86_TRAP_VC, &idt_desc);
41 	}
42 
43 	native_load_idt(&desc);
44 }
45 
46 /*
47  * Setup boot CPU state needed before kernel switches to virtual addresses.
48  */
startup_64_setup_gdt_idt(void)49 void __head startup_64_setup_gdt_idt(void)
50 {
51 	struct gdt_page *gp = rip_rel_ptr((void *)(__force unsigned long)&gdt_page);
52 	void *handler = NULL;
53 
54 	struct desc_ptr startup_gdt_descr = {
55 		.address = (unsigned long)gp->gdt,
56 		.size    = GDT_SIZE - 1,
57 	};
58 
59 	/* Load GDT */
60 	native_load_gdt(&startup_gdt_descr);
61 
62 	/* New GDT is live - reload data segment registers */
63 	asm volatile("movl %%eax, %%ds\n"
64 		     "movl %%eax, %%ss\n"
65 		     "movl %%eax, %%es\n" : : "a"(__KERNEL_DS) : "memory");
66 
67 	if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT))
68 		handler = rip_rel_ptr(vc_no_ghcb);
69 
70 	startup_64_load_idt(handler);
71 }
72