/* SPDX-License-Identifier: GPL-2.0-only */1/*2* X86 specific Hyper-V kdump/crash related code.3*4* Copyright (C) 2025, Microsoft, Inc.5*6*/7#include <linux/linkage.h>8#include <asm/alternative.h>9#include <asm/msr.h>10#include <asm/processor-flags.h>11#include <asm/nospec-branch.h>1213/*14* void noreturn hv_crash_asm32(arg1)15* arg1 == edi == 32bit PA of struct hv_crash_tramp_data16*17* The hypervisor jumps here upon devirtualization in protected mode. This18* code gets copied to a page in the low 4G ie, 32bit space so it can run19* in the protected mode. Hence we cannot use any compile/link time offsets or20* addresses. It restores long mode via temporary gdt and page tables and21* eventually jumps to kernel code entry at HV_CRASHDATA_OFFS_C_entry.22*23* PreCondition (ie, Hypervisor call back ABI):24* o CR0 is set to 0x0021: PE(prot mode) and NE are set, paging is disabled25* o CR4 is set to 0x026* o IA32_EFER is set to 0x901 (SCE and NXE are set)27* o EDI is set to the Arg passed to HVCALL_DISABLE_HYP_EX.28* o CS, DS, ES, FS, GS are all initialized with a base of 0 and limit 0xFFFF29* o IDTR, TR and GDTR are initialized with a base of 0 and limit of 0xFFFF30* o LDTR is initialized as invalid (limit of 0)31* o MSR PAT is power on default.32* o Other state/registers are cleared. All TLBs flushed.33*/3435#define HV_CRASHDATA_OFFS_TRAMPCR3 0x0 /* 0 */36#define HV_CRASHDATA_OFFS_KERNCR3 0x8 /* 8 */37#define HV_CRASHDATA_OFFS_GDTRLIMIT 0x12 /* 18 */38#define HV_CRASHDATA_OFFS_CS_JMPTGT 0x28 /* 40 */39#define HV_CRASHDATA_OFFS_C_entry 0x30 /* 48 */4041.text42.code324344SYM_CODE_START(hv_crash_asm32)45UNWIND_HINT_UNDEFINED46ENDBR47movl $X86_CR4_PAE, %ecx48movl %ecx, %cr44950movl %edi, %ebx51add $HV_CRASHDATA_OFFS_TRAMPCR3, %ebx52movl %cs:(%ebx), %eax53movl %eax, %cr35455/* Setup EFER for long mode now */56movl $MSR_EFER, %ecx57rdmsr58btsl $_EFER_LME, %eax59wrmsr6061/* Turn paging on using the temp 32bit trampoline page table */62movl %cr0, %eax63orl $(X86_CR0_PG), %eax64movl %eax, %cr06566/* since kernel cr3 could be above 4G, we need to be in the long mode67* before we can load 64bits of the kernel cr3. We use a temp gdt for68* that with CS.L=1 and CS.D=0 */69mov %edi, %eax70add $HV_CRASHDATA_OFFS_GDTRLIMIT, %eax71lgdtl %cs:(%eax)7273/* not done yet, restore CS now to switch to CS.L=1 */74mov %edi, %eax75add $HV_CRASHDATA_OFFS_CS_JMPTGT, %eax76ljmp %cs:*(%eax)77SYM_CODE_END(hv_crash_asm32)7879/* we now run in full 64bit IA32-e long mode, CS.L=1 and CS.D=0 */80.code6481.balign 882SYM_CODE_START(hv_crash_asm64)83UNWIND_HINT_UNDEFINED84ENDBR85/* restore kernel page tables so we can jump to kernel code */86mov %edi, %eax87add $HV_CRASHDATA_OFFS_KERNCR3, %eax88movq %cs:(%eax), %rbx89movq %rbx, %cr39091mov %edi, %eax92add $HV_CRASHDATA_OFFS_C_entry, %eax93movq %cs:(%eax), %rbx94ANNOTATE_RETPOLINE_SAFE95jmp *%rbx9697int $39899SYM_INNER_LABEL(hv_crash_asm_end, SYM_L_GLOBAL)100SYM_CODE_END(hv_crash_asm64)101102103