// SPDX-License-Identifier: GPL-2.012#include <linux/linkage.h>3#include <linux/types.h>45#include <asm/desc.h>6#include <asm/init.h>7#include <asm/setup.h>8#include <asm/sev.h>9#include <asm/trapnr.h>1011/*12* Data structures and code used for IDT setup in head_64.S. The bringup-IDT is13* used until the idt_table takes over. On the boot CPU this happens in14* x86_64_start_kernel(), on secondary CPUs in start_secondary(). In both cases15* this happens in the functions called from head_64.S.16*17* The idt_table can't be used that early because all the code modifying it is18* in idt.c and can be instrumented by tracing or KASAN, which both don't work19* during early CPU bringup. Also the idt_table has the runtime vectors20* configured which require certain CPU state to be setup already (like TSS),21* which also hasn't happened yet in early CPU bringup.22*/23static gate_desc bringup_idt_table[NUM_EXCEPTION_VECTORS] __page_aligned_data;2425/* This may run while still in the direct mapping */26void __head startup_64_load_idt(void *vc_handler)27{28struct desc_ptr desc = {29.address = (unsigned long)rip_rel_ptr(bringup_idt_table),30.size = sizeof(bringup_idt_table) - 1,31};32struct idt_data data;33gate_desc idt_desc;3435/* @vc_handler is set only for a VMM Communication Exception */36if (vc_handler) {37init_idt_data(&data, X86_TRAP_VC, vc_handler);38idt_init_desc(&idt_desc, &data);39native_write_idt_entry((gate_desc *)desc.address, X86_TRAP_VC, &idt_desc);40}4142native_load_idt(&desc);43}4445/*46* Setup boot CPU state needed before kernel switches to virtual addresses.47*/48void __head startup_64_setup_gdt_idt(void)49{50struct gdt_page *gp = rip_rel_ptr((void *)(__force unsigned long)&gdt_page);51void *handler = NULL;5253struct desc_ptr startup_gdt_descr = {54.address = (unsigned long)gp->gdt,55.size = GDT_SIZE - 1,56};5758/* Load GDT */59native_load_gdt(&startup_gdt_descr);6061/* New GDT is live - reload data segment registers */62asm volatile("movl %%eax, %%ds\n"63"movl %%eax, %%ss\n"64"movl %%eax, %%es\n" : : "a"(__KERNEL_DS) : "memory");6566if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT))67handler = rip_rel_ptr(vc_no_ghcb);6869startup_64_load_idt(handler);70}717273