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 */
24static gate_desc bringup_idt_table[NUM_EXCEPTION_VECTORS] __page_aligned_data;
25
26/* This may run while still in the direct mapping */
27void startup_64_load_idt(void *vc_handler)
28{
29 struct desc_ptr desc = {
30 .address = (unsigned long)rip_rel_ptr(p: 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: &data, X86_TRAP_VC, addr: vc_handler);
39 idt_init_desc(gate: &idt_desc, d: &data);
40 native_write_idt_entry(idt: (gate_desc *)desc.address, X86_TRAP_VC, gate: &idt_desc);
41 }
42
43 native_load_idt(dtr: &desc);
44}
45
46/*
47 * Setup boot CPU state needed before kernel switches to virtual addresses.
48 */
49void __init startup_64_setup_gdt_idt(void)
50{
51 struct gdt_page *gp = rip_rel_ptr(p: (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(dtr: &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(p: vc_no_ghcb);
69
70 startup_64_load_idt(vc_handler: handler);
71}
72

source code of linux/arch/x86/boot/startup/gdt_idt.c