Skip to contentSkip to conversation

Misc

RFC PATCH New Jason Chen CJ 1 participant 5 messages Mar 12, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover Misc

This patch set is part-4 of this RFC patches. It contains patches for misc functionalities including supporting of VPID, debug and NMI handling.

Enable VPID for host VM in pKVM if the platform supports it, which improve VMX transition overhead.

Add tmp debug method for pKVM by allowing access Linux printk.

Add nmi support in pKVM, which will record nmi happened in root mode then inject to host VM when vmenter. This ensure physical nmi will not be lost.

Chuanxiao Dong (1):
pkvm: x86: Handle pending nmi in pKVM runtime

Jason Chen CJ (3):
pkvm: x86: Enable VPID for host VM
pkvm: x86: Add pKVM debug support
pkvm: x86: Support get_pcpu_id

 arch/x86/include/asm/pkvm_image.h         |  2 +-
 arch/x86/include/asm/pkvm_image_vars.h    |  4 ++
 arch/x86/kvm/Kconfig                      |  8 +++
 arch/x86/kvm/vmx/pkvm/hyp/Makefile        | 10 ++-
 arch/x86/kvm/vmx/pkvm/hyp/cpu.h           | 30 ++++++++
 arch/x86/kvm/vmx/pkvm/hyp/debug.h         |  7 ++
 arch/x86/kvm/vmx/pkvm/hyp/idt.S           | 67 ++++++++++++++++++
 arch/x86/kvm/vmx/pkvm/hyp/init_finalise.c | 28 +++++++-
 arch/x86/kvm/vmx/pkvm/hyp/irq.c           | 60 ++++++++++++++++
 arch/x86/kvm/vmx/pkvm/hyp/mmu.c           | 21 ++++++
 arch/x86/kvm/vmx/pkvm/hyp/mmu.h           |  6 ++
 arch/x86/kvm/vmx/pkvm/hyp/vmexit.c        | 26 +++++++
 arch/x86/kvm/vmx/pkvm/hyp/vmx.h           |  5 ++
 arch/x86/kvm/vmx/pkvm/include/pkvm.h      |  6 ++
 arch/x86/kvm/vmx/pkvm/pkvm_host.c         | 83 ++++++++++++++++++++---
 virt/kvm/pkvm/pkvm.c                      |  6 +-
 16 files changed, 352 insertions(+), 17 deletions(-)
 create mode 100644 arch/x86/kvm/vmx/pkvm/hyp/cpu.h
 create mode 100644 arch/x86/kvm/vmx/pkvm/hyp/idt.S
 create mode 100644 arch/x86/kvm/vmx/pkvm/hyp/irq.c
Signature · 1 line
2.25.1
#1 lore raw

RFC PATCH 1/4 pkvm: x86: Enable VPID for host VM

If VPID is disabled on a guest, upon VM entries, TLB entries associated with VPID 0000H will be invalidated. Enable VPID for host VM to improve VMX transition overhead.

---
 arch/x86/kvm/vmx/pkvm/hyp/init_finalise.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
arch/x86/kvm/vmx/pkvm/hyp/init_finalise.c +15 -0
index ae10d550448d..e0c74d5ac2fa 100644
@@ -289,6 +289,21 @@ int __pkvm_init_finalise(struct kvm_vcpu *vcpu, struct pkvm_section sections[],
 	secondary_exec_controls_setbit(&pkvm_host_vcpu->vmx, SECONDARY_EXEC_ENABLE_EPT);
 	vmcs_write64(EPT_POINTER, eptp);
 
+	/* enable vpid */
+	if (pkvm_hyp->vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_VPID) {
+		static u16 pkvm_host_vpid = VMX_NR_VPIDS - 1;
+
+		/*
+		 * Fixed VPIDs for the host vCPUs, which implies that it could conflict
+		 * with VPIDs from nested guests.
+		 *
+		 * It's safe because cached mappings used in non-root mode are associated
+		 * with EP4TA, which is managed by pKVM and unique for every guest.
+		 */
+		vmcs_write16(VIRTUAL_PROCESSOR_ID, pkvm_host_vpid--);
+		secondary_exec_controls_setbit(&pkvm_host_vcpu->vmx, SECONDARY_EXEC_ENABLE_VPID);
+	}
+
 	ept_sync_global();
 
 out:
Signature · 1 line
2.25.1
#2 lore raw

RFC PATCH 2/4 pkvm: x86: Add pKVM debug support

In reply to Jason Chen CJ

Add debug option CONFIG_PKVM_INTEL_DEBUG to allow use printk in pKVM for debugging purpose.

Enable this option will do:
- keep using host GDT/IDT/segment etc.
- clone host CR3 entries from __page_base_offset
- no __pkvm prefix for pkvm runtime symbols, so to avoid same variable name definition for hyp_memory, rename hyp_memory in virt/kvm/pkvm/pkvm.c to _hyp_memory

NOTE: this is a tmp solution for debugging before we design a new solution.

---
 arch/x86/include/asm/pkvm_image.h         |  2 +-
 arch/x86/include/asm/pkvm_image_vars.h    |  4 +++
 arch/x86/kvm/Kconfig                      |  8 +++++
 arch/x86/kvm/vmx/pkvm/hyp/Makefile        |  8 ++++-
 arch/x86/kvm/vmx/pkvm/hyp/debug.h         |  7 ++++
 arch/x86/kvm/vmx/pkvm/hyp/init_finalise.c | 13 ++++++-
 arch/x86/kvm/vmx/pkvm/hyp/mmu.c           | 21 ++++++++++++
 arch/x86/kvm/vmx/pkvm/hyp/mmu.h           |  6 ++++
 arch/x86/kvm/vmx/pkvm/pkvm_host.c         | 42 +++++++++++++++++++++++
 virt/kvm/pkvm/pkvm.c                      |  6 ++--
 10 files changed, 111 insertions(+), 6 deletions(-)
arch/x86/include/asm/pkvm_image.h +1 -1
index ed026b740a78..5ae6a53177eb 100644
@@ -5,7 +5,7 @@
 #ifndef __X86_INTEL_PKVM_IMAGE_H
 #define __X86_INTEL_PKVM_IMAGE_H
 
-#ifdef __PKVM_HYP__
+#if defined(CONFIG_PKVM_INTEL_DEBUG) || defined(__PKVM_HYP__)
 /* No prefix will be added */
 #define PKVM_DECLARE(type, f)	type f
 #define pkvm_sym(sym)		sym
arch/x86/include/asm/pkvm_image_vars.h +4 -0
index a7823dc9b981..598c60302bac 100644
@@ -5,6 +5,8 @@
 #ifndef __ASM_x86_PKVM_IMAGE_VARS_H
 #define __ASM_x86_PKVM_IMAGE_VARS_H
 
+#ifndef CONFIG_PKVM_INTEL_DEBUG
+
 #ifdef CONFIG_DYNAMIC_PHYSICAL_MASK
 PKVM_ALIAS(physical_mask);
 #endif
@@ -16,3 +18,5 @@ PKVM_ALIAS(sme_me_mask);
 PKVM_ALIAS(__default_kernel_pte_mask);
 
 #endif
+
+#endif
arch/x86/kvm/Kconfig +8 -0
index 5a8ae5f80849..c2f66d3eef37 100644
@@ -100,6 +100,14 @@ config PKVM_INTEL
 
 	  If unsure, say N.
 
+config PKVM_INTEL_DEBUG
+        bool "Debug pKVM"
+        depends on PKVM_INTEL
+        help
+          Provides debug support for pKVM.
+
+          If unsure, say N.
+
 config X86_SGX_KVM
 	bool "Software Guard eXtensions (SGX) Virtualization"
 	depends on X86_SGX && KVM_INTEL
arch/x86/kvm/vmx/pkvm/hyp/Makefile +7 -1
index 9a1cb483a55e..a7546e1d0b80 100644
@@ -9,15 +9,17 @@ ccflags-y += -fno-stack-protector
 ccflags-y += -D__DISABLE_EXPORTS
 ccflags-y += -D__PKVM_HYP__
 
-lib-dir		:= lib
 virt-dir	:= ../../../../../../$(KVM_PKVM)
 
 pkvm-hyp-y	:= vmx_asm.o vmexit.o memory.o early_alloc.o pgtable.o mmu.o pkvm.o \
 		   init_finalise.o ept.o
 
+ifndef CONFIG_PKVM_INTEL_DEBUG
+lib-dir		:= lib
 pkvm-hyp-y	+= $(lib-dir)/memset_64.o
 pkvm-hyp-$(CONFIG_RETPOLINE)	+= $(lib-dir)/retpoline.o
 pkvm-hyp-$(CONFIG_DEBUG_LIST)	+= $(lib-dir)/list_debug.o
+endif
 pkvm-hyp-y	+= $(virt-dir)/page_alloc.o
 
 pkvm-obj 	:= $(patsubst %.o,%.pkvm.o,$(pkvm-hyp-y))
@@ -40,7 +42,11 @@ $(obj)/pkvm.o: $(obj)/pkvm.tmp.o FORCE
 	$(call if_changed,pkvmcopy)
 
 quiet_cmd_pkvmcopy = PKVMPCOPY $@
+ifdef CONFIG_PKVM_INTEL_DEBUG
+      cmd_pkvmcopy = $(OBJCOPY) --prefix-symbols= $< $@
+else
       cmd_pkvmcopy = $(OBJCOPY) --prefix-symbols=__pkvm_ --remove-section=.retpoline_sites --remove-section=.return_sites $< $@
+endif
 
 # Remove ftrace, Shadow Call Stack, and CFI CFLAGS.
 # This is equivalent to the 'notrace', '__noscs', and '__nocfi' annotations.
arch/x86/kvm/vmx/pkvm/hyp/debug.h +7 -0
index 97f633ca0bac..f8f7d00dfb88 100644
@@ -6,8 +6,15 @@
 #ifndef _PKVM_DEBUG_H_
 #define _PKVM_DEBUG_H_
 
+#ifdef CONFIG_PKVM_INTEL_DEBUG
+#include <linux/printk.h>
+#define pkvm_dbg(f, x...) pr_debug(f, ## x)
+#define pkvm_info(f, x...) pr_info(f, ## x)
+#define pkvm_err(f, x...) pr_err(f, ## x)
+#else
 #define pkvm_dbg(x...)
 #define pkvm_info(x...)
 #define pkvm_err(x...)
+#endif
 
 #endif
arch/x86/kvm/vmx/pkvm/hyp/init_finalise.c +12 -1
index e0c74d5ac2fa..8c585a73237a 100644
@@ -100,14 +100,24 @@ static int create_mmu_mapping(const struct pkvm_section sections[],
 				 int section_sz)
 {
 	unsigned long nr_pages = pkvm_mmu_pgtable_pages();
+	int ret;
+#ifndef CONFIG_PKVM_INTEL_DEBUG
 	struct memblock_region *reg;
-	int ret, i;
+	int i;
+#endif
 
 	ret = pkvm_early_mmu_init(&pkvm_hyp->mmu_cap,
 			pkvm_mmu_pgt_base, nr_pages);
 	if (ret)
 		return ret;
 
+#ifdef CONFIG_PKVM_INTEL_DEBUG
+	/*
+	 * clone host CR3 page mapping from __page_base_offset, it covers both
+	 * direct mapping and symbol mapping for pkvm (same mapping as kernel)
+	 */
+	pkvm_mmu_clone_host(pkvm_hyp->mmu_cap.level, __page_base_offset);
+#else
 	/*
 	 * Create mapping for the memory in memblocks.
 	 * This will include all the memory host kernel can see, as well
@@ -135,6 +145,7 @@ static int create_mmu_mapping(const struct pkvm_section sections[],
 		if (ret)
 			return ret;
 	}
+#endif
 
 	ret = pkvm_back_vmemmap(__pkvm_pa(pkvm_vmemmap_base));
 	if (ret)
arch/x86/kvm/vmx/pkvm/hyp/mmu.c +21 -0
index 7684d16dd2c9..b32ca706fa4b 100644
@@ -169,7 +169,15 @@ static int fix_pgtable_refcnt(void)
 	 * Calculate the max address space, then walk the [0, size) address
 	 * range to fixup refcount of every used page.
 	 */
+#ifdef CONFIG_PKVM_INTEL_DEBUG
+	/*
+	 * only fix vmmemap range for debug mode, now for 64T memory,
+	 * could be extended if physical memory is bigger than 64T
+	 */
+	size = (SZ_64T / PAGE_SIZE) * sizeof(struct hyp_page);
+#else
 	size = pgt_ops->pgt_level_to_size(hyp_mmu.level + 1);
+#endif
 
 	return pgtable_walk(&hyp_mmu, 0, size, &walker);
 }
@@ -228,3 +236,16 @@ int pkvm_later_mmu_init(void *mmu_pool_base, unsigned long mmu_pool_pages)
 	 */
 	return fix_pgtable_refcnt();
 }
+
+#ifdef CONFIG_PKVM_INTEL_DEBUG
+void pkvm_mmu_clone_host(int level, unsigned long start_vaddr)
+{
+	int i = mmu_entry_to_index(start_vaddr, level);
+	u64 *ptep = __va(hyp_mmu.root_pa);
+	u64 *host_cr3 = __va(__read_cr3() & PAGE_MASK);
+
+	for (; i < PTRS_PER_PTE; i++)
+		ptep[i] = host_cr3[i];
+
+}
+#endif
arch/x86/kvm/vmx/pkvm/hyp/mmu.h +6 -0
index 6b678ae94b31..218e3d4ef92c 100644
@@ -16,4 +16,10 @@ int pkvm_early_mmu_init(struct pkvm_pgtable_cap *cap,
 
 int pkvm_later_mmu_init(void *mmu_pool_base, unsigned long mmu_pool_pages);
 
+#ifdef CONFIG_PKVM_INTEL_DEBUG
+void pkvm_mmu_clone_host(int level, unsigned long start_vaddr);
+#else
+static inline void pkvm_mmu_clone_host(int level, unsigned long start_vaddr) {}
+#endif
+
 #endif
arch/x86/kvm/vmx/pkvm/pkvm_host.c +42 -0

59 lines not on this page · raw message

virt/kvm/pkvm/pkvm.c +3 -3
index 8a20a662b917..cdc3a8a2fee9 100644
@@ -10,7 +10,7 @@
 
 #include <asm/kvm_pkvm.h>
 
-static struct memblock_region *hyp_memory = pkvm_sym(hyp_memory);
+static struct memblock_region *_hyp_memory = pkvm_sym(hyp_memory);
 static unsigned int *hyp_memblock_nr_ptr = &pkvm_sym(hyp_memblock_nr);
 
 phys_addr_t hyp_mem_base;
@@ -26,7 +26,7 @@ static int cmp_hyp_memblock(const void *p1, const void *p2)
 
 static void __init sort_memblock_regions(void)
 {
-	sort(hyp_memory,
+	sort(_hyp_memory,
 	     *hyp_memblock_nr_ptr,
 	     sizeof(struct memblock_region),
 	     cmp_hyp_memblock,
@@ -41,7 +41,7 @@ static int __init register_memblock_regions(void)
 		if (*hyp_memblock_nr_ptr >= HYP_MEMBLOCK_REGIONS)
 			return -ENOMEM;
 
-		hyp_memory[*hyp_memblock_nr_ptr] = *reg;
+		_hyp_memory[*hyp_memblock_nr_ptr] = *reg;
 		(*hyp_memblock_nr_ptr)++;
 	}
 	sort_memblock_regions();
Signature · 1 line
2.25.1
#3 lore raw

RFC PATCH 3/4 pkvm: x86: Support get_pcpu_id

In reply to Jason Chen CJ

There is use case to get pcpu id during pKVM runtime, especially within exception/irq context when it cannot get such information from current host vcpu.

Use MSR_GS_BASE to record pcpu_id, and enable pKVM to get pcpu_id during its runtime.

It is prepared for nmi handler, as when CPU is running in pKVM hypervisor, nmi is not able to block, so pKVM need to appropriately inject it back to host vcpu to avoid nmi lost. When nmi happen, its handler doesn't have context which host vcpu it shall do the following injection. get_pcpu_id() here can help the nmi handler get corresponding host vcpu.

---
 arch/x86/kvm/vmx/pkvm/hyp/cpu.h   | 30 ++++++++++++++++++++++++++++++
 arch/x86/kvm/vmx/pkvm/pkvm_host.c | 12 +++++++-----
 2 files changed, 37 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx/pkvm/hyp/cpu.h new file +30 -0
new file mode 100644
index 000000000000..c49074292f7c
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2022 Intel Corporation
+ */
+#ifndef _PKVM_CPU_H_
+#define _PKVM_CPU_H_
+
+static inline u64 pkvm_msr_read(u32 reg)
+{
+	u32 msrl, msrh;
+
+	asm volatile (" rdmsr ":"=a"(msrl), "=d"(msrh) : "c" (reg));
+	return (((u64)msrh << 32U) | msrl);
+}
+
+#ifdef CONFIG_PKVM_INTEL_DEBUG
+#include <linux/smp.h>
+static inline u64 get_pcpu_id(void)
+{
+	return raw_smp_processor_id();
+}
+#else
+/* this function shall only be used during pkvm runtime */
+static inline u64 get_pcpu_id(void)
+{
+	return pkvm_msr_read(MSR_GS_BASE);
+}
+#endif
+
+#endif
arch/x86/kvm/vmx/pkvm/pkvm_host.c +7 -5
index c7768ee22e57..77cd7b654168 100644
@@ -240,14 +240,13 @@ static __init void init_guest_state_area(struct pkvm_host_vcpu *vcpu, int cpu)
 	vmcs_write64(VMCS_LINK_POINTER, -1ull);
 }
 
-static __init void _init_host_state_area(struct pkvm_pcpu *pcpu)
+static __init void _init_host_state_area(struct pkvm_pcpu *pcpu, int cpu)
 {
 	unsigned long a;
 #ifdef CONFIG_PKVM_INTEL_DEBUG
 	u32 high, low;
 	struct desc_ptr dt;
 	u16 selector;
-	int cpu = raw_smp_processor_id();
 #endif
 
 	vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS);
@@ -302,6 +301,9 @@ static __init void _init_host_state_area(struct pkvm_pcpu *pcpu)
 	vmcs_writel(HOST_TR_BASE, (unsigned long)&pcpu->tss);
 	vmcs_writel(HOST_GDTR_BASE, (unsigned long)(&pcpu->gdt_page));
 	vmcs_writel(HOST_IDTR_BASE, (unsigned long)(&pcpu->idt_page));
+
+	vmcs_write16(HOST_GS_SELECTOR, __KERNEL_DS);
+	vmcs_writel(HOST_GS_BASE, cpu);
 #endif
 
 	/* MSR area */
@@ -312,11 +314,11 @@ static __init void _init_host_state_area(struct pkvm_pcpu *pcpu)
 	vmcs_write64(HOST_IA32_PAT, a);
 }
 
-static __init void init_host_state_area(struct pkvm_host_vcpu *vcpu)
+static __init void init_host_state_area(struct pkvm_host_vcpu *vcpu, int cpu)
 {
 	struct pkvm_pcpu *pcpu = vcpu->pcpu;
 
-	_init_host_state_area(pcpu);
+	_init_host_state_area(pcpu, cpu);
 
 	/*host RIP*/
 	vmcs_writel(HOST_RIP, (unsigned long)pkvm_sym(__pkvm_vmx_vmexit));
@@ -403,7 +405,7 @@ static __init int pkvm_host_init_vmx(struct pkvm_host_vcpu *vcpu, int cpu)
 	vmcs_load(vmx->loaded_vmcs->vmcs);
 
 	init_guest_state_area(vcpu, cpu);
-	init_host_state_area(vcpu);
+	init_host_state_area(vcpu, cpu);
 	init_execution_control(vmx, &pkvm->vmcs_config, &pkvm->vmx_cap);
 	init_vmexit_control(vmx, &pkvm->vmcs_config);
 	init_vmentry_control(vmx, &pkvm->vmcs_config);
Signature · 1 line
2.25.1
#4 lore raw

RFC PATCH 4/4 pkvm: x86: Handle pending nmi in pKVM runtime

In reply to Jason Chen CJ

From: Chuanxiao Dong <[email protected]>

There is still chance that nmi will happen under root mode during pKVM runtime, ignore it will cause nmi lost in the host VM. Need ensure such nmi could be injected into host VM.

Add a simple handling for this kind of pending nmi by taking use of irq window.

---
 arch/x86/kvm/vmx/pkvm/hyp/Makefile   |  2 +-
 arch/x86/kvm/vmx/pkvm/hyp/idt.S      | 67 ++++++++++++++++++++++++++++
 arch/x86/kvm/vmx/pkvm/hyp/irq.c      | 60 +++++++++++++++++++++++++
 arch/x86/kvm/vmx/pkvm/hyp/vmexit.c   | 26 +++++++++++
 arch/x86/kvm/vmx/pkvm/hyp/vmx.h      |  5 +++
 arch/x86/kvm/vmx/pkvm/include/pkvm.h |  6 +++
 arch/x86/kvm/vmx/pkvm/pkvm_host.c    | 35 +++++++++++----
 7 files changed, 192 insertions(+), 9 deletions(-)
arch/x86/kvm/vmx/pkvm/hyp/Makefile +1 -1
index a7546e1d0b80..fe852bd43a7e 100644
@@ -12,7 +12,7 @@ ccflags-y += -D__PKVM_HYP__
 virt-dir	:= ../../../../../../$(KVM_PKVM)
 
 pkvm-hyp-y	:= vmx_asm.o vmexit.o memory.o early_alloc.o pgtable.o mmu.o pkvm.o \
-		   init_finalise.o ept.o
+		   init_finalise.o ept.o idt.o irq.o
 
 ifndef CONFIG_PKVM_INTEL_DEBUG
 lib-dir		:= lib
arch/x86/kvm/vmx/pkvm/hyp/idt.S new file +67 -0

67 lines not on this page · raw message

arch/x86/kvm/vmx/pkvm/hyp/irq.c new file +60 -0

60 lines not on this page · raw message

arch/x86/kvm/vmx/pkvm/hyp/vmexit.c +26 -0
index 88cbd276caf8..e8015a6830b0 100644
@@ -117,6 +117,27 @@ static void handle_xsetbv(struct kvm_vcpu *vcpu)
 			: : "a" (eax), "d" (edx), "c" (ecx));
 }
 
+static void handle_irq_window(struct kvm_vcpu *vcpu)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	u32 cpu_based_exec_ctrl = exec_controls_get(vmx);
+
+	exec_controls_set(vmx, cpu_based_exec_ctrl & ~CPU_BASED_INTR_WINDOW_EXITING);
+	pkvm_dbg("%s: CPU%d clear irq_window_exiting\n", __func__, vcpu->cpu);
+}
+
+static void handle_pending_events(struct kvm_vcpu *vcpu)
+{
+	struct pkvm_host_vcpu *pkvm_host_vcpu = to_pkvm_hvcpu(vcpu);
+
+	if (pkvm_host_vcpu->pending_nmi) {
+		/* Inject if NMI is not blocked */
+		vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
+			     INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
+		pkvm_host_vcpu->pending_nmi = false;
+	}
+}
+
 /* we take use of kvm_vcpu structure, but not used all the fields */
 int pkvm_main(struct kvm_vcpu *vcpu)
 {
@@ -171,6 +192,9 @@ int pkvm_main(struct kvm_vcpu *vcpu)
 			if (handle_host_ept_violation(vmcs_read64(GUEST_PHYSICAL_ADDRESS)))
 				skip_instruction = true;
 			break;
+		case EXIT_REASON_INTERRUPT_WINDOW:
+			handle_irq_window(vcpu);
+			break;
 		default:
 			pkvm_dbg("CPU%d: Unsupported vmexit reason 0x%x.\n", vcpu->cpu, vmx->exit_reason.full);
 			skip_instruction = true;
@@ -183,6 +207,8 @@ int pkvm_main(struct kvm_vcpu *vcpu)
 		if (skip_instruction)
 			skip_emulated_instruction();
 
+		handle_pending_events(vcpu);
+
 		native_write_cr2(vcpu->arch.cr2);
 	} while (1);
 
arch/x86/kvm/vmx/pkvm/hyp/vmx.h +5 -0
index c0a42cc56764..178139d1b61f 100644
@@ -45,4 +45,9 @@ static inline u64 pkvm_construct_eptp(unsigned long root_hpa, int level)
 	return eptp;
 }
 
+static inline void vmx_enable_irq_window(struct vcpu_vmx *vmx)
+{
+	exec_controls_setbit(vmx, CPU_BASED_INTR_WINDOW_EXITING);
+}
+
 #endif
arch/x86/kvm/vmx/pkvm/include/pkvm.h +6 -0
index d0a7283b0e19..292d48d8ee44 100644
@@ -33,6 +33,9 @@ struct pkvm_host_vcpu {
 	struct vcpu_vmx vmx;
 	struct pkvm_pcpu *pcpu;
 	struct vmcs *vmxarea;
+	struct vmcs *current_vmcs;
+
+	bool pending_nmi;
 };
 
 struct pkvm_host_vm {
@@ -100,4 +103,7 @@ PKVM_DECLARE(void *, pkvm_early_alloc_contig(unsigned int nr_pages));
 PKVM_DECLARE(void *, pkvm_early_alloc_page(void));
 PKVM_DECLARE(void, pkvm_early_alloc_init(void *virt, unsigned long size));
 
+PKVM_DECLARE(void, noop_handler(void));
+PKVM_DECLARE(void, nmi_handler(void));
+
 #endif
arch/x86/kvm/vmx/pkvm/pkvm_host.c +27 -8
index 77cd7b654168..8ea2d64236d0 100644
@@ -276,8 +276,7 @@ static __init void _init_host_state_area(struct pkvm_pcpu *pcpu, int cpu)
 
 	native_store_gdt(&dt);
 	vmcs_writel(HOST_GDTR_BASE, dt.address);
-	store_idt(&dt);
-	vmcs_writel(HOST_IDTR_BASE, dt.address);
+	vmcs_writel(HOST_IDTR_BASE, (unsigned long)(&pcpu->idt_page));
 
 	rdmsr(MSR_IA32_SYSENTER_CS, low, high);
 	vmcs_write32(HOST_IA32_SYSENTER_CS, low);
@@ -403,6 +402,7 @@ static __init int pkvm_host_init_vmx(struct pkvm_host_vcpu *vcpu, int cpu)
 
 	vmx->loaded_vmcs = &vmx->vmcs01;
 	vmcs_load(vmx->loaded_vmcs->vmcs);
+	vcpu->current_vmcs = vmx->loaded_vmcs->vmcs;
 
 	init_guest_state_area(vcpu, cpu);
 	init_host_state_area(vcpu, cpu);
@@ -514,11 +514,6 @@ static __init void init_gdt(struct pkvm_pcpu *pcpu)
 	pcpu->gdt_page = pkvm_gdt_page;
 }
 
-void noop_handler(void)
-{
-	/* To be added */
-}
-
 static __init void init_idt(struct pkvm_pcpu *pcpu)
 {
 	gate_desc *idt = pcpu->idt_page.idt;
@@ -533,13 +528,37 @@ static __init void init_idt(struct pkvm_pcpu *pcpu)
 	gate_desc desc;
 	int i;
 
+#ifdef CONFIG_PKVM_INTEL_DEBUG
+	gate_desc *host_idt;
+	struct desc_ptr dt;
+
+	store_idt(&dt);
+	host_idt = (gate_desc *)dt.address;
+
+	/* reuse other exception handler but control nmi handler */
+	for (i = 0; i <= X86_TRAP_IRET; i++) {
+		if (i == X86_TRAP_NMI) {
+			d.vector = i;
+			d.bits.ist = 0;
+			d.addr = (const void *)pkvm_sym(nmi_handler);
+			idt_init_desc(&desc, &d);
+			write_idt_entry(idt, i, &desc);
+		} else {
+			memcpy(&idt[i], &host_idt[i], sizeof(gate_desc));
+		}
+	}
+#else
 	for (i = 0; i <= X86_TRAP_IRET; i++) {
 		d.vector = i;
 		d.bits.ist = 0;
-		d.addr = (const void *)noop_handler;
+		if (i == X86_TRAP_NMI)
+			d.addr = (const void *)pkvm_sym(nmi_handler);
+		else
+			d.addr = (const void *)pkvm_sym(noop_handler);
 		idt_init_desc(&desc, &d);
 		write_idt_entry(idt, i, &desc);
 	}
+#endif
 }
 
 static __init void init_tss(struct pkvm_pcpu *pcpu)
Signature · 1 line
2.25.1