Skip to contentSkip to conversation

KVM TDX basic feature support

PATCH In review [email protected] 21 participants 695 messages Feb 26, 2024 – Jul 18, 2024 Page 6 of 6

Subsystems (8)
Documentation x86 KVM Kselftest +4 more subsystems
lore
Share & tools
lore
#670 lore raw

Re: [PATCH v19 120/130] KVM: TDX: Add a method to ignore dirty logging

In reply to Edgecombe, Rick P
Quoted 10 lines · Rick P
Quoted 7 lines · Isaku Yamahata

I categorize as follows. Unless otherwise, I'll update this series.

- dirty log check
  As we will drop this ptach, we'll have no call site.

- KVM_BUG_ON() in main.c
  We should drop them because their logic isn't complex.

What about "KVM: TDX: Add methods to ignore guest instruction emulation"? Is it cleanly blocked somehow?

KVM fault handler, kvm_mmu_page_fault(), is the caller into the emulation, It should skip the emulation.

As the second guard, x86_emulate_instruction(), calls check_emulate_instruction() callback to check if the emulation can/should be done. TDX callback can return it as X86EMUL_UNHANDLEABLE. Then, the flow goes to user space as error. I'll update the vt_check_emulate_instruction().

Quoted 7 lines
Quoted 7 lines

- KVM_BUG_ON() in tdx.c
  - The error check of the return value from SEAMCALL
    We should keep it as it's unexpected error from TDX module. When we hit
    this, we should mark the guest bugged and prevent further operation.  It's
    hard to deduce the reason.  TDX mdoule might be broken.

Quoted 7 lines

Yes. Makes sense.

Quoted 3 lines

  - Other check
    We should drop them.

Offhand, I'm not sure what is in this category.

- Checking error code on TD enter/exit
I'll revise how to check error from TD enter/exit. We'll have new code. I will update wrapper function to take struct kvm_tdx or struct tdx_vcpu, and revise to remove random check. Cleanups related to kvm_rebooting, TDX_SW_ERROR, kvm_spurious_fault()

- Remaining random check for debug.
The examples are as follows. They were added for debug.

@@ -797,18 +788,14 @@ void tdx_vcpu_free(struct kvm_vcpu *vcpu)

         * list_{del,add}() on associated_tdvcpus list later.
         */
        tdx_disassociate_vp_on_cpu(vcpu);

- WARN_ON_ONCE(vcpu->cpu != -1);

/*
* This methods can be called when vcpu allocation/initialization
* failed. So it's possible that hkid, tdvpx and tdvpr are not assigned
* yet.
*/
- if (is_hkid_assigned(to_kvm_tdx(vcpu->kvm))) {
- WARN_ON_ONCE(tdx->tdvpx_pa);
- WARN_ON_ONCE(tdx->tdvpr_pa);
+ if (is_hkid_assigned(to_kvm_tdx(vcpu->kvm)))

                return;

- }

        if (tdx->tdvpx_pa) {
                for (i = 0; i < tdx_info->nr_tdvpx_pages; i++) {

@@ -831,9 +818,9 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
{

        /* vcpu_deliver_init method silently discards INIT event. */
-       if (KVM_BUG_ON(init_event, vcpu->kvm))
+       if (init_event)
                return;
-       if (KVM_BUG_ON(is_td_vcpu_created(to_tdx(vcpu)), vcpu->kvm))
+       if (is_td_vcpu_created(to_tdx(vcpu)))
                return;
 
        /*

@@ -831,9 +818,9 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
{

        /* vcpu_deliver_init method silently discards INIT event. */
-       if (KVM_BUG_ON(init_event, vcpu->kvm))
+       if (init_event)
                return;
-       if (KVM_BUG_ON(is_td_vcpu_created(to_tdx(vcpu)), vcpu->kvm))
+       if (is_td_vcpu_created(to_tdx(vcpu)))
                return;
 
        /*

@@ -831,9 +818,9 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
{

        /* vcpu_deliver_init method silently discards INIT event. */
-       if (KVM_BUG_ON(init_event, vcpu->kvm))
+       if (init_event)
                return;
-       if (KVM_BUG_ON(is_td_vcpu_created(to_tdx(vcpu)), vcpu->kvm))
+       if (is_td_vcpu_created(to_tdx(vcpu)))
                return;
 
        /*
Signature · 1 line
Isaku Yamahata <[email protected]>
#671 lore raw

Re: [PATCH v19 120/130] KVM: TDX: Add a method to ignore dirty logging

Quoted 14 lines · Isaku Yamahata
Quoted 10 lines · Rick P
Quoted 7 lines · Isaku Yamahata

I categorize as follows. Unless otherwise, I'll update this series.

- dirty log check
  As we will drop this ptach, we'll have no call site.

- KVM_BUG_ON() in main.c
  We should drop them because their logic isn't complex.

What about "KVM: TDX: Add methods to ignore guest instruction emulation"? Is it cleanly blocked somehow?

Quoted 7 lines

KVM fault handler, kvm_mmu_page_fault(), is the caller into the emulation, It should skip the emulation.

As the second guard, x86_emulate_instruction(), calls check_emulate_instruction() callback to check if the emulation can/should be done. TDX callback can return it as X86EMUL_UNHANDLEABLE. Then, the flow goes to user space as error. I'll update the vt_check_emulate_instruction().

Oops. It was wrong. It should be X86EMUL_RETRY_INSTR. RETRY_INSTR means, let vcpu execute the intrusion again, UNHANDLEABLE means, emulator can't emulate, inject exception or give up with KVM_EXIT_INTERNAL_ERROR.

For TDX, we'd like to inject #VE to the guest so that the guest #VE handler can issue TDG.VP.VMCALL<MMIO>. The default non-present sept value has #VE suppress bit set. As first step, EPT violation occurs. then KVM sets up mmio_spte with #VE suppress bit cleared. Then X86EMUL_RETRY_INSTR tells kvm to resume vcpu to inject #VE.

Signature · 1 line
Isaku Yamahata <[email protected]>
#672 lore raw

Re: [PATCH v19 120/130] KVM: TDX: Add a method to ignore dirty logging

Quoted 18 lines · Isaku Yamahata
Quoted 11 lines

KVM fault handler, kvm_mmu_page_fault(), is the caller into the emulation,
It should skip the emulation.

As the second guard, x86_emulate_instruction(), calls check_emulate_instruction() callback to check if the emulation can/should be
done.  TDX callback can return it as X86EMUL_UNHANDLEABLE.  Then, the flow goes
to user space as error.  I'll update the vt_check_emulate_instruction().

Oops. It was wrong. It should be X86EMUL_RETRY_INSTR.  RETRY_INSTR means, let
vcpu execute the intrusion again, UNHANDLEABLE means, emulator can't emulate,
inject exception or give up with KVM_EXIT_INTERNAL_ERROR.

Quoted 9 lines

For TDX, we'd like to inject #VE to the guest so that the guest #VE handler
can issue TDG.VP.VMCALL<MMIO>.  The default non-present sept value has
#VE suppress bit set.  As first step, EPT violation occurs. then KVM sets
up mmio_spte with #VE suppress bit cleared. Then X86EMUL_RETRY_INSTR tells
kvm to resume vcpu to inject #VE.

Ah, so in a normal VM it would:
- get ept violation for no memslot
- setup MMIO PTE for later
- go ahead and head to the emulator anyway instead of waiting to
refault

In TDX, it could skip the last step and head right to the guest, but instead if heads to the emulator and gets told to retry there. It's a good solution in that there is already an way to cleanly insert logic at check_emulate_instruction(). Otherwise we would need to add another check somewhere to know in TDX to consider the fault resolved.

I was initially thinking if it gets anywhere near the emulator things have gone wrong and we are missing something. The downside is that we can't find those issues. If something tries to use the emulator it will just fault in a loop instead of exiting to userspace or bugging the VM. Hmm.

#673 lore raw

PATCH v19 121/130 KVM: TDX: Add methods to ignore VMX preemption timer

In reply to [email protected]

From: Isaku Yamahata <[email protected]>

TDX doesn't support VMX preemption timer. Implement access methods for VMM to ignore VMX preemption timer.

---
 arch/x86/kvm/vmx/main.c | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/main.c +23 -2
index 991bfdecaed2..ec5c0fda92e9 100644
@@ -842,6 +842,27 @@ static void vt_update_cpu_dirty_logging(struct kvm_vcpu *vcpu)
 	vmx_update_cpu_dirty_logging(vcpu);
 }
 
+#ifdef CONFIG_X86_64
+static int vt_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc,
+			      bool *expired)
+{
+	/* VMX-preemption timer isn't available for TDX. */
+	if (is_td_vcpu(vcpu))
+		return -EINVAL;
+
+	return vmx_set_hv_timer(vcpu, guest_deadline_tsc, expired);
+}
+
+static void vt_cancel_hv_timer(struct kvm_vcpu *vcpu)
+{
+	/* VMX-preemption timer can't be set.  See vt_set_hv_timer(). */
+	if (KVM_BUG_ON(is_td_vcpu(vcpu), vcpu->kvm))
+		return;
+
+	vmx_cancel_hv_timer(vcpu);
+}
+#endif
+
 static int vt_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
 {
 	if (!is_td(kvm))
@@ -1024,8 +1045,8 @@ struct kvm_x86_ops vt_x86_ops __initdata = {
 	.pi_start_assignment = vmx_pi_start_assignment,
 
 #ifdef CONFIG_X86_64
-	.set_hv_timer = vmx_set_hv_timer,
-	.cancel_hv_timer = vmx_cancel_hv_timer,
+	.set_hv_timer = vt_set_hv_timer,
+	.cancel_hv_timer = vt_cancel_hv_timer,
 #endif
 
 	.setup_mce = vmx_setup_mce,
Signature · 1 line
2.25.1
#674 lore raw

PATCH v19 122/130 KVM: TDX: Add methods to ignore accesses to TSC

In reply to [email protected]

From: Isaku Yamahata <[email protected]>

TDX protects TDX guest TSC state from VMM. Implement access methods to ignore guest TSC.

---
 arch/x86/kvm/vmx/main.c | 44 +++++++++++++++++++++++++++++++++++++----
 1 file changed, 40 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx/main.c +40 -4
index ec5c0fda92e9..9fcd71999bba 100644
@@ -834,6 +834,42 @@ static u8 vt_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
 	return vmx_get_mt_mask(vcpu, gfn, is_mmio);
 }
 
+static u64 vt_get_l2_tsc_offset(struct kvm_vcpu *vcpu)
+{
+	/* TDX doesn't support L2 guest at the moment. */
+	if (KVM_BUG_ON(is_td_vcpu(vcpu), vcpu->kvm))
+		return 0;
+
+	return vmx_get_l2_tsc_offset(vcpu);
+}
+
+static u64 vt_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu)
+{
+	/* TDX doesn't support L2 guest at the moment. */
+	if (KVM_BUG_ON(is_td_vcpu(vcpu), vcpu->kvm))
+		return 0;
+
+	return vmx_get_l2_tsc_multiplier(vcpu);
+}
+
+static void vt_write_tsc_offset(struct kvm_vcpu *vcpu)
+{
+	/* In TDX, tsc offset can't be changed. */
+	if (is_td_vcpu(vcpu))
+		return;
+
+	vmx_write_tsc_offset(vcpu);
+}
+
+static void vt_write_tsc_multiplier(struct kvm_vcpu *vcpu)
+{
+	/* In TDX, tsc multiplier can't be changed. */
+	if (is_td_vcpu(vcpu))
+		return;
+
+	vmx_write_tsc_multiplier(vcpu);
+}
+
 static void vt_update_cpu_dirty_logging(struct kvm_vcpu *vcpu)
 {
 	if (KVM_BUG_ON(is_td_vcpu(vcpu), vcpu->kvm))
@@ -1022,10 +1058,10 @@ struct kvm_x86_ops vt_x86_ops __initdata = {
 
 	.has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
 
-	.get_l2_tsc_offset = vmx_get_l2_tsc_offset,
-	.get_l2_tsc_multiplier = vmx_get_l2_tsc_multiplier,
-	.write_tsc_offset = vmx_write_tsc_offset,
-	.write_tsc_multiplier = vmx_write_tsc_multiplier,
+	.get_l2_tsc_offset = vt_get_l2_tsc_offset,
+	.get_l2_tsc_multiplier = vt_get_l2_tsc_multiplier,
+	.write_tsc_offset = vt_write_tsc_offset,
+	.write_tsc_multiplier = vt_write_tsc_multiplier,
 
 	.load_mmu_pgd = vt_load_mmu_pgd,
 
Signature · 1 line
2.25.1
#675 lore raw

PATCH v19 123/130 KVM: TDX: Ignore setting up mce

In reply to [email protected]

From: Isaku Yamahata <[email protected]>

Because vmx_set_mce function is VMX specific and it cannot be used for TDX. Add vt stub to ignore setting up mce for TDX.

---
 arch/x86/kvm/vmx/main.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/main.c +9 -1
index 9fcd71999bba..7c47b02d88d8 100644
@@ -899,6 +899,14 @@ static void vt_cancel_hv_timer(struct kvm_vcpu *vcpu)
 }
 #endif
 
+static void vt_setup_mce(struct kvm_vcpu *vcpu)
+{
+	if (is_td_vcpu(vcpu))
+		return;
+
+	vmx_setup_mce(vcpu);
+}
+
 static int vt_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
 {
 	if (!is_td(kvm))
@@ -1085,7 +1093,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = {
 	.cancel_hv_timer = vt_cancel_hv_timer,
 #endif
 
-	.setup_mce = vmx_setup_mce,
+	.setup_mce = vt_setup_mce,
 
 #ifdef CONFIG_KVM_SMM
 	.smi_allowed = vt_smi_allowed,
Signature · 1 line
2.25.1
#676 lore raw

PATCH v19 124/130 KVM: TDX: Add a method to ignore for TDX to ignore hypercall patch

In reply to [email protected]

From: Isaku Yamahata <[email protected]>

Because guest TD memory is protected, VMM patching guest binary for hypercall instruction isn't possible. Add a method to ignore hypercall patching with a warning. Note: guest TD kernel needs to be modified to use TDG.VP.VMCALL for hypercall.

---
 arch/x86/kvm/vmx/main.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/main.c +14 -1
index 7c47b02d88d8..fae5a3668361 100644
@@ -731,6 +731,19 @@ static u32 vt_get_interrupt_shadow(struct kvm_vcpu *vcpu)
 	return vmx_get_interrupt_shadow(vcpu);
 }
 
+static void vt_patch_hypercall(struct kvm_vcpu *vcpu,
+				  unsigned char *hypercall)
+{
+	/*
+	 * Because guest memory is protected, guest can't be patched. TD kernel
+	 * is modified to use TDG.VP.VMCAL for hypercall.
+	 */
+	if (KVM_BUG_ON(is_td_vcpu(vcpu), vcpu->kvm))
+		return;
+
+	vmx_patch_hypercall(vcpu, hypercall);
+}
+
 static void vt_inject_irq(struct kvm_vcpu *vcpu, bool reinjected)
 {
 	if (is_td_vcpu(vcpu))
@@ -1030,7 +1043,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = {
 	.update_emulated_instruction = vmx_update_emulated_instruction,
 	.set_interrupt_shadow = vt_set_interrupt_shadow,
 	.get_interrupt_shadow = vt_get_interrupt_shadow,
-	.patch_hypercall = vmx_patch_hypercall,
+	.patch_hypercall = vt_patch_hypercall,
 	.inject_irq = vt_inject_irq,
 	.inject_nmi = vt_inject_nmi,
 	.inject_exception = vt_inject_exception,
Signature · 1 line
2.25.1
#677 lore raw

PATCH v19 125/130 KVM: TDX: Add methods to ignore virtual apic related operation

In reply to [email protected]

From: Isaku Yamahata <[email protected]>

TDX protects TDX guest APIC state from VMM. Implement access methods of TDX guest vAPIC state to ignore them or return zero.

---
 arch/x86/kvm/vmx/main.c    | 61 ++++++++++++++++++++++++++++++++++----
 arch/x86/kvm/vmx/tdx.c     |  6 ++++
 arch/x86/kvm/vmx/x86_ops.h |  3 ++
 3 files changed, 64 insertions(+), 6 deletions(-)
arch/x86/kvm/vmx/main.c +55 -6
index fae5a3668361..c46c860be0f2 100644
@@ -352,6 +352,14 @@ static bool vt_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
 	return vmx_apic_init_signal_blocked(vcpu);
 }
 
+static void vt_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
+{
+	if (is_td_vcpu(vcpu))
+		return tdx_set_virtual_apic_mode(vcpu);
+
+	return vmx_set_virtual_apic_mode(vcpu);
+}
+
 static void vt_apicv_pre_state_restore(struct kvm_vcpu *vcpu)
 {
 	struct pi_desc *pi = vcpu_to_pi_desc(vcpu);
@@ -360,6 +368,31 @@ static void vt_apicv_pre_state_restore(struct kvm_vcpu *vcpu)
 	memset(pi->pir, 0, sizeof(pi->pir));
 }
 
+static void vt_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
+{
+	if (is_td_vcpu(vcpu))
+		return;
+
+	return vmx_hwapic_irr_update(vcpu, max_irr);
+}
+
+static void vt_hwapic_isr_update(int max_isr)
+{
+	if (is_td_vcpu(kvm_get_running_vcpu()))
+		return;
+
+	return vmx_hwapic_isr_update(max_isr);
+}
+
+static bool vt_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
+{
+	/* TDX doesn't support L2 at the moment. */
+	if (WARN_ON_ONCE(is_td_vcpu(vcpu)))
+		return false;
+
+	return vmx_guest_apic_has_interrupt(vcpu);
+}
+
 static int vt_sync_pir_to_irr(struct kvm_vcpu *vcpu)
 {
 	if (is_td_vcpu(vcpu))
@@ -815,6 +848,22 @@ static void vt_update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
 	vmx_update_cr8_intercept(vcpu, tpr, irr);
 }
 
+static void vt_set_apic_access_page_addr(struct kvm_vcpu *vcpu)
+{
+	if (is_td_vcpu(vcpu))
+		return;
+
+	vmx_set_apic_access_page_addr(vcpu);
+}
+
+static void vt_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
+{
+	if (WARN_ON_ONCE(is_td_vcpu(vcpu)))
+		return;
+
+	vmx_refresh_apicv_exec_ctrl(vcpu);
+}
+
 static void vt_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
 {
 	if (is_td_vcpu(vcpu))
@@ -1055,15 +1104,15 @@ struct kvm_x86_ops vt_x86_ops __initdata = {
 	.enable_nmi_window = vt_enable_nmi_window,
 	.enable_irq_window = vt_enable_irq_window,
 	.update_cr8_intercept = vt_update_cr8_intercept,
-	.set_virtual_apic_mode = vmx_set_virtual_apic_mode,
-	.set_apic_access_page_addr = vmx_set_apic_access_page_addr,
-	.refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
+	.set_virtual_apic_mode = vt_set_virtual_apic_mode,
+	.set_apic_access_page_addr = vt_set_apic_access_page_addr,
+	.refresh_apicv_exec_ctrl = vt_refresh_apicv_exec_ctrl,
 	.load_eoi_exitmap = vt_load_eoi_exitmap,
 	.apicv_pre_state_restore = vt_apicv_pre_state_restore,
 	.required_apicv_inhibits = VMX_REQUIRED_APICV_INHIBITS,
-	.hwapic_irr_update = vmx_hwapic_irr_update,
-	.hwapic_isr_update = vmx_hwapic_isr_update,
-	.guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
+	.hwapic_irr_update = vt_hwapic_irr_update,
+	.hwapic_isr_update = vt_hwapic_isr_update,
+	.guest_apic_has_interrupt = vt_guest_apic_has_interrupt,
 	.sync_pir_to_irr = vt_sync_pir_to_irr,
 	.deliver_interrupt = vt_deliver_interrupt,
 	.dy_apicv_has_pending_interrupt = pi_has_pending_interrupt,
arch/x86/kvm/vmx/tdx.c +6 -0
index fc78e9ca61c1..f706c346eea4 100644
@@ -2080,6 +2080,12 @@ void tdx_enable_smi_window(struct kvm_vcpu *vcpu)
 }
 #endif
 
+void tdx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
+{
+	/* Only x2APIC mode is supported for TD. */
+	WARN_ON_ONCE(kvm_get_apic_mode(vcpu) != LAPIC_MODE_X2APIC);
+}
+
 int tdx_get_cpl(struct kvm_vcpu *vcpu)
 {
 	return 0;
arch/x86/kvm/vmx/x86_ops.h +3 -0
index 727c4d418601..c507f1513dac 100644
@@ -168,6 +168,7 @@ void tdx_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason,
 bool tdx_has_emulated_msr(u32 index, bool write);
 int tdx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr);
 int tdx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr);
+void tdx_set_virtual_apic_mode(struct kvm_vcpu *vcpu);
 
 int tdx_get_cpl(struct kvm_vcpu *vcpu);
 void tdx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg);
@@ -227,6 +228,8 @@ static inline bool tdx_has_emulated_msr(u32 index, bool write) { return false; }
 static inline int tdx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) { return 1; }
 static inline int tdx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) { return 1; }
 
+static inline void tdx_set_virtual_apic_mode(struct kvm_vcpu *vcpu) {}
+
 static inline int tdx_get_cpl(struct kvm_vcpu *vcpu) { return 0; }
 static inline void tdx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg) {}
 static inline unsigned long tdx_get_rflags(struct kvm_vcpu *vcpu) { return 0; }
Signature · 1 line
2.25.1
#678 lore raw

Re: [PATCH v19 125/130] KVM: TDX: Add methods to ignore virtual apic related operation

Quoted 17 lines · On 2/26/2024 4:27 PM, [email protected] wrote:

From: Isaku Yamahata <[email protected]>

TDX protects TDX guest APIC state from VMM. Implement access methods of TDX guest vAPIC state to ignore them or return zero.

---
  arch/x86/kvm/vmx/main.c    | 61 ++++++++++++++++++++++++++++++++++----
  arch/x86/kvm/vmx/tdx.c     |  6 ++++
  arch/x86/kvm/vmx/x86_ops.h |  3 ++
  3 files changed, 64 insertions(+), 6 deletions(-)
arch/x86/kvm/vmx/main.c +0 -0
index fae5a3668361..c46c860be0f2 100644

arch/x86/kvm/vmx/main.c static bool vt_apic_init_signal_blocked(struct kvm_vcpu *vcpu) · line 358

Quoted 7 lines
arch/x86/kvm/vmx/main.c +4 -0
@@ -352,6 +352,14 @@ static bool vt_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
  	return vmx_apic_init_signal_blocked(vcpu);
  }
 
+static void vt_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
+{
+	if (is_td_vcpu(vcpu))
+		return tdx_set_virtual_apic_mode(vcpu);

Can open code this function...

Quoted 4 lines
arch/x86/kvm/vmx/main.c +4 -0
+
+	return vmx_set_virtual_apic_mode(vcpu);
+}
+

[...]

Quoted 7 lines
arch/x86/kvm/vmx/main.c +6 -0
+void tdx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
+{
+	/* Only x2APIC mode is supported for TD. */
+	WARN_ON_ONCE(kvm_get_apic_mode(vcpu) != LAPIC_MODE_X2APIC);
+}
+
#679 lore raw

Re: [PATCH v19 125/130] KVM: TDX: Add methods to ignore virtual apic related operation

Quoted 15 lines · Binbin Wu
Quoted 12 lines · On 2/26/2024 4:27 PM, [email protected] wrote:

From: Isaku Yamahata <[email protected]>

TDX protects TDX guest APIC state from VMM. Implement access methods of TDX guest vAPIC state to ignore them or return zero.

---
  arch/x86/kvm/vmx/main.c    | 61 ++++++++++++++++++++++++++++++++++----
  arch/x86/kvm/vmx/tdx.c     |  6 ++++
  arch/x86/kvm/vmx/x86_ops.h |  3 ++
  3 files changed, 64 insertions(+), 6 deletions(-)
Quoted 12 lines

arch/x86/kvm/vmx/main.c static bool vt_apic_init_signal_blocked(struct kvm_vcpu *vcpu) · line 357

Quoted 11 lines
arch/x86/kvm/vmx/main.c +4 -0
index fae5a3668361..c46c860be0f2 100644
@@ -352,6 +352,14 @@ static bool vt_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
  	return vmx_apic_init_signal_blocked(vcpu);
  }
+static void vt_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
+{
+	if (is_td_vcpu(vcpu))
+		return tdx_set_virtual_apic_mode(vcpu);

Can open code this function...

Yes, the function is empty currently.

Signature · 1 line
Isaku Yamahata <[email protected]>
#680 lore raw

PATCH v19 126/130 KVM: TDX: Inhibit APICv for TDX guest

In reply to [email protected]

From: Isaku Yamahata <[email protected]>

TDX doesn't support APICV, inhibit APICv for TDX guest. Follow how SEV does it. Define a new inhibit reason for TDX, set it on TD initialization, and add the flag to kvm_x86_ops.required_apicv_inhibits.

---
 arch/x86/include/asm/kvm_host.h | 9 +++++++++
 arch/x86/kvm/vmx/main.c         | 3 ++-
 arch/x86/kvm/vmx/tdx.c          | 4 ++++
 3 files changed, 15 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +9 -0
index 2686c080820b..920fb771246b 100644
@@ -1300,6 +1300,15 @@ enum kvm_apicv_inhibit {
 	 * mapping between logical ID and vCPU.
 	 */
 	APICV_INHIBIT_REASON_LOGICAL_ID_ALIASED,
+
+	/*********************************************************/
+	/* INHIBITs that are relevant only to the Intel's APICv. */
+	/*********************************************************/
+
+	/*
+	 * APICv is disabled because TDX doesn't support it.
+	 */
+	APICV_INHIBIT_REASON_TDX,
 };
 
 struct kvm_arch {
arch/x86/kvm/vmx/main.c +2 -1
index c46c860be0f2..2cd404fd7176 100644
@@ -1022,7 +1022,8 @@ static void vt_post_memory_mapping(struct kvm_vcpu *vcpu,
 	 BIT(APICV_INHIBIT_REASON_BLOCKIRQ) |			\
 	 BIT(APICV_INHIBIT_REASON_PHYSICAL_ID_ALIASED) |	\
 	 BIT(APICV_INHIBIT_REASON_APIC_ID_MODIFIED) |		\
-	 BIT(APICV_INHIBIT_REASON_APIC_BASE_MODIFIED))
+	 BIT(APICV_INHIBIT_REASON_APIC_BASE_MODIFIED) |		\
+	 BIT(APICV_INHIBIT_REASON_TDX))
 
 struct kvm_x86_ops vt_x86_ops __initdata = {
 	.name = KBUILD_MODNAME,
arch/x86/kvm/vmx/tdx.c +4 -0
index f706c346eea4..7be1be161dc2 100644
@@ -2488,6 +2488,8 @@ static int __tdx_td_init(struct kvm *kvm, struct td_params *td_params,
 		goto teardown;
 	}
 
+	kvm_set_apicv_inhibit(kvm, APICV_INHIBIT_REASON_TDX);
+
 	return 0;
 
 	/*
@@ -2821,6 +2823,8 @@ static int tdx_td_vcpu_init(struct kvm_vcpu *vcpu, u64 vcpu_rcx)
 		return -EIO;
 	}
 
+	WARN_ON_ONCE(kvm_apicv_activated(vcpu->kvm));
+	vcpu->arch.apic->apicv_active = false;
 	vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
 	tdx->td_vcpu_created = true;
 	return 0;
Signature · 1 line
2.25.1
#681 lore raw

PATCH v19 127/130 Documentation/virt/kvm: Document on Trust Domain Extensions(TDX)

In reply to [email protected]

From: Isaku Yamahata <[email protected]>

Add documentation to Intel Trusted Domain Extensions(TDX) support.

---
 Documentation/virt/kvm/api.rst           |   9 +-
 Documentation/virt/kvm/x86/index.rst     |   1 +
 Documentation/virt/kvm/x86/intel-tdx.rst | 362 +++++++++++++++++++++++
 3 files changed, 371 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/virt/kvm/x86/intel-tdx.rst
Documentation/virt/kvm/api.rst +8 -1
index 667dc58f7d2f..4b70d2b43532 100644
@@ -1394,6 +1394,9 @@ the memory region are automatically reflected into the guest.  For example, an
 mmap() that affects the region will be made visible immediately.  Another
 example is madvise(MADV_DROP).
 
+For TDX guest, deleting/moving memory region loses guest memory contents.
+Read only region isn't supported.  Only as-id 0 is supported.
+
 Note: On arm64, a write generated by the page-table walker (to update
 the Access and Dirty flags, for example) never results in a
 KVM_EXIT_MMIO exit when the slot has the KVM_MEM_READONLY flag. This
@@ -4734,7 +4737,7 @@ H_GET_CPU_CHARACTERISTICS hypercall.
 
 :Capability: basic
 :Architectures: x86
-:Type: vm
+:Type: vm ioctl, vcpu ioctl
 :Parameters: an opaque platform specific structure (in/out)
 :Returns: 0 on success; -1 on error
 
@@ -4746,6 +4749,10 @@ Currently, this ioctl is used for issuing Secure Encrypted Virtualization
 (SEV) commands on AMD Processors. The SEV commands are defined in
 Documentation/virt/kvm/x86/amd-memory-encryption.rst.
 
+Currently, this ioctl is used for issuing Trusted Domain Extensions
+(TDX) commands on Intel Processors. The TDX commands are defined in
+Documentation/virt/kvm/x86/intel-tdx.rst.
+
 4.111 KVM_MEMORY_ENCRYPT_REG_REGION
 -----------------------------------
 
Documentation/virt/kvm/x86/index.rst +1 -0
index 9ece6b8dc817..851e99174762 100644
@@ -11,6 +11,7 @@ KVM for x86 systems
    cpuid
    errata
    hypercalls
+   intel-tdx
    mmu
    msr
    nested-vmx
Documentation/virt/kvm/x86/intel-tdx.rst new file +362 -0

362 lines not on this page · raw message

Signature · 1 line
2.25.1
#682 lore raw

PATCH v19 128/130 KVM: x86: design documentation on TDX support of x86 KVM TDP MMU

In reply to [email protected]

From: Isaku Yamahata <[email protected]>

Add a high level design document on TDX changes to TDP MMU.

---
 Documentation/virt/kvm/x86/index.rst       |   1 +
 Documentation/virt/kvm/x86/tdx-tdp-mmu.rst | 443 +++++++++++++++++++++
 2 files changed, 444 insertions(+)
 create mode 100644 Documentation/virt/kvm/x86/tdx-tdp-mmu.rst
Documentation/virt/kvm/x86/index.rst +1 -0
index 851e99174762..63a78bd41b16 100644
@@ -16,4 +16,5 @@ KVM for x86 systems
    msr
    nested-vmx
    running-nested-guests
+   tdx-tdp-mmu
    timekeeping
Documentation/virt/kvm/x86/tdx-tdp-mmu.rst new file +443 -0

443 lines not on this page · raw message

Signature · 1 line
2.25.1
#683 lore raw

PATCH v19 129/130 RFC: KVM: x86: Add x86 callback to check cpuid

In reply to [email protected]

From: Isaku Yamahata <[email protected]>

The x86 backend should check the consistency of KVM_SET_CPUID2 because it has its constraint. Add a callback for it. The backend code will come as another patch.

---
 arch/x86/include/asm/kvm-x86-ops.h |  2 ++
 arch/x86/include/asm/kvm_host.h    |  1 +
 arch/x86/kvm/cpuid.c               | 20 ++++++++++++--------
 3 files changed, 15 insertions(+), 8 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +2 -0
index 85c04aad6ab3..3a7140129855 100644
@@ -20,6 +20,8 @@ KVM_X86_OP(hardware_disable)
 KVM_X86_OP(hardware_unsetup)
 KVM_X86_OP_OPTIONAL_RET0(offline_cpu)
 KVM_X86_OP(has_emulated_msr)
+/* TODO: Once all backend implemented this op, remove _OPTIONAL_RET0. */
+KVM_X86_OP_OPTIONAL_RET0(vcpu_check_cpuid)
 KVM_X86_OP(vcpu_after_set_cpuid)
 KVM_X86_OP(is_vm_type_supported)
 KVM_X86_OP_OPTIONAL(max_vcpus);
arch/x86/include/asm/kvm_host.h +1 -0
index 920fb771246b..e4d40e31fc31 100644
@@ -1638,6 +1638,7 @@ struct kvm_x86_ops {
 	void (*hardware_unsetup)(void);
 	int (*offline_cpu)(void);
 	bool (*has_emulated_msr)(struct kvm *kvm, u32 index);
+	int (*vcpu_check_cpuid)(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2, int nent);
 	void (*vcpu_after_set_cpuid)(struct kvm_vcpu *vcpu);
 
 	bool (*is_vm_type_supported)(unsigned long vm_type);
arch/x86/kvm/cpuid.c +12 -8
index 8cdcd6f406aa..b57006943247 100644
@@ -136,6 +136,7 @@ static int kvm_check_cpuid(struct kvm_vcpu *vcpu,
 {
 	struct kvm_cpuid_entry2 *best;
 	u64 xfeatures;
+	int r;
 
 	/*
 	 * The existing code assumes virtual address is 48-bit or 57-bit in the
@@ -155,15 +156,18 @@ static int kvm_check_cpuid(struct kvm_vcpu *vcpu,
 	 * enabling in the FPU, e.g. to expand the guest XSAVE state size.
 	 */
 	best = cpuid_entry2_find(entries, nent, 0xd, 0);
-	if (!best)
-		return 0;
-
-	xfeatures = best->eax | ((u64)best->edx << 32);
-	xfeatures &= XFEATURE_MASK_USER_DYNAMIC;
-	if (!xfeatures)
-		return 0;
+	if (best) {
+		xfeatures = best->eax | ((u64)best->edx << 32);
+		xfeatures &= XFEATURE_MASK_USER_DYNAMIC;
+		if (xfeatures) {
+			r = fpu_enable_guest_xfd_features(&vcpu->arch.guest_fpu,
+							  xfeatures);
+			if (r)
+				return r;
+		}
+	}
 
-	return fpu_enable_guest_xfd_features(&vcpu->arch.guest_fpu, xfeatures);
+	return static_call(kvm_x86_vcpu_check_cpuid)(vcpu, entries, nent);
 }
 
 /* Check whether the supplied CPUID data is equal to what is already set for the vCPU. */
Signature · 1 line
2.25.1
#684 lore raw

PATCH v19 130/130 RFC: KVM: x86, TDX: Add check for KVM_SET_CPUID2

In reply to [email protected]

From: Isaku Yamahata <[email protected]>

Implement a hook of KVM_SET_CPUID2 for additional consistency check.

Intel TDX or AMD SEV has a restriction on the value of cpuid. For example, some values must be the same between all vcpus. Check if the new values are consistent with the old values. The check is light because the cpuid consistency is very model specific and complicated. The user space VMM should set cpuid and MSRs consistently.

---
v18:
- Use TDH.SYS.RD() instead of struct tdsysinfo_struct

---
 arch/x86/kvm/vmx/main.c    | 10 ++++++
 arch/x86/kvm/vmx/tdx.c     | 66 ++++++++++++++++++++++++++++++++++----
 arch/x86/kvm/vmx/tdx.h     |  7 ++++
 arch/x86/kvm/vmx/x86_ops.h |  4 +++
 4 files changed, 81 insertions(+), 6 deletions(-)
arch/x86/kvm/vmx/main.c +10 -0
index 2cd404fd7176..1a979ec644d0 100644
@@ -432,6 +432,15 @@ static void vt_vcpu_deliver_init(struct kvm_vcpu *vcpu)
 	kvm_vcpu_deliver_init(vcpu);
 }
 
+static int vt_vcpu_check_cpuid(struct kvm_vcpu *vcpu,
+			       struct kvm_cpuid_entry2 *e2, int nent)
+{
+	if (is_td_vcpu(vcpu))
+		return tdx_vcpu_check_cpuid(vcpu, e2, nent);
+
+	return 0;
+}
+
 static void vt_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
 {
 	if (is_td_vcpu(vcpu))
@@ -1125,6 +1134,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = {
 
 	.get_exit_info = vt_get_exit_info,
 
+	.vcpu_check_cpuid = vt_vcpu_check_cpuid,
 	.vcpu_after_set_cpuid = vt_vcpu_after_set_cpuid,
 
 	.has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
arch/x86/kvm/vmx/tdx.c +60 -6

117 lines not on this page · raw message

arch/x86/kvm/vmx/tdx.h +7 -0
index 11c74c34555f..af3a2b8afee8 100644
@@ -31,6 +31,13 @@ struct kvm_tdx {
 
 	u64 tsc_offset;
 
+	/*
+	 * For KVM_SET_CPUID to check consistency. Remember the one passed to
+	 * TDH.MNG_INIT
+	 */
+	int cpuid_nent;
+	struct kvm_cpuid_entry2 *cpuid;
+
 	/* For KVM_MEMORY_MAPPING */
 	struct mutex source_lock;
 	struct page *source_page;
arch/x86/kvm/vmx/x86_ops.h +4 -0
index c507f1513dac..6b067842a67f 100644
@@ -162,6 +162,8 @@ u8 tdx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio);
 
 void tdx_deliver_interrupt(struct kvm_lapic *apic, int delivery_mode,
 			   int trig_mode, int vector);
+int tdx_vcpu_check_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2,
+			 int nent);
 void tdx_inject_nmi(struct kvm_vcpu *vcpu);
 void tdx_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason,
 		u64 *info1, u64 *info2, u32 *intr_info, u32 *error_code);
@@ -221,6 +223,8 @@ static inline u8 tdx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
 
 static inline void tdx_deliver_interrupt(struct kvm_lapic *apic, int delivery_mode,
 					 int trig_mode, int vector) {}
+static inline int tdx_vcpu_check_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2,
+				       int nent) { return -EOPNOTSUPP; }
 static inline void tdx_inject_nmi(struct kvm_vcpu *vcpu) {}
 static inline void tdx_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason, u64 *info1,
 				     u64 *info2, u32 *intr_info, u32 *error_code) {}
Signature · 1 line
2.25.1
#685 lore raw

Re: [PATCH v19 130/130] RFC: KVM: x86, TDX: Add check for KVM_SET_CPUID2

Quoted 11 lines · On Mon, 2024-02-26 at 00:27 -0800, [email protected] wrote:

Implement a hook of KVM_SET_CPUID2 for additional consistency check.

Intel TDX or AMD SEV has a restriction on the value of cpuid.  For example,
some values must be the same between all vcpus.  Check if the new values
are consistent with the old values.  The check is light because the cpuid
consistency is very model specific and complicated.  The user space VMM
should set cpuid and MSRs consistently.

I see that this was suggested by Sean, but can you explain the problem that this is working around? From the linked thread, it seems like the problem is what to do when userspace also calls SET_CPUID after already configuring CPUID to the TDX module in the special way. The choices discussed included:
1. Reject the call
2. Check the consistency between the first CPUID configuration and the second one.

1 is a lot simpler, but the reasoning for 2 is because "some KVM code paths rely on guest CPUID configuration" it seems. Is this a hypothetical or real issue? Which code paths are problematic for TDX/SNP?

Just trying to assess what we should do with these two patches.

#686 lore raw

Re: [PATCH v19 130/130] RFC: KVM: x86, TDX: Add check for KVM_SET_CPUID2

Quoted 22 lines · Rick P
Quoted 11 lines · On Mon, 2024-02-26 at 00:27 -0800, [email protected] wrote:

Implement a hook of KVM_SET_CPUID2 for additional consistency check.

Intel TDX or AMD SEV has a restriction on the value of cpuid.  For example,
some values must be the same between all vcpus.  Check if the new values
are consistent with the old values.  The check is light because the cpuid
consistency is very model specific and complicated.  The user space VMM
should set cpuid and MSRs consistently.

I see that this was suggested by Sean, but can you explain the problem that this is working around? From the linked thread, it seems like the problem is what to do when userspace also calls SET_CPUID after already configuring CPUID to the TDX module in the special way. The choices discussed included:
1. Reject the call
2. Check the consistency between the first CPUID configuration and the second one.

Quoted 4 lines

1 is a lot simpler, but the reasoning for 2 is because "some KVM code paths rely on guest CPUID configuration" it seems. Is this a hypothetical or real issue? Which code paths are problematic for TDX/SNP?

There might be use case that TDX guest wants to use some CPUID which isn't handled by the TDX module but purely by KVM. These (PV) CPUIDs need to be provided via KVM_SET_CPUID2.

Btw, Isaku, I don't understand why you tag the last two patches as RFC and put them at last. I think I've expressed this before. Per the discussion with Sean, my understanding is this isn't something optional but the right thing we should do?

https://lore.kernel.org/lkml/[email protected]/

#687 lore raw

Re: [PATCH v19 130/130] RFC: KVM: x86, TDX: Add check for KVM_SET_CPUID2

Quoted 19 lines · Kai
Quoted 13 lines

I see that this was suggested by Sean, but can you explain the problem
that this is working around? From the linked thread, it seems like the
problem is what to do when userspace also calls SET_CPUID after already
configuring CPUID to the TDX module in the special way. The choices discussed included:
1. Reject the call
2. Check the consistency between the first CPUID configuration and the
second one.

Quoted 5 lines

1 is a lot simpler, but the reasoning for 2 is because "some KVM code
paths rely on guest CPUID configuration" it seems. Is this a hypothetical or real issue? Which code paths are problematic for TDX/SNP?

Quoted 4 lines

There might be use case that TDX guest wants to use some CPUID which isn't handled by the TDX module but purely by KVM.  These (PV) CPUIDs need to be
provided via KVM_SET_CPUID2.

Right, but are there any needed today? I read that Sean's point was that KVM_SET_CPUID2 can't accept anything today what we would want to block later, otherwise it would introduce a regression. This was the major constraint IIUC, and means the base series requires *something* here.

If we want to support only the most basic support first, we don't need to support PV CPUIDs on day 1, right?

So I'm wondering, if we could shrink the base series by going with option 1 to start, and then expanding it with this solution later to enable more features. Do you see a problem or conflict with Sean's comments?

#688 lore raw

Re: [PATCH v19 130/130] RFC: KVM: x86, TDX: Add check for KVM_SET_CPUID2

Quoted 20 lines · Rick P
Quoted 19 lines · Kai
Quoted 18 lines

I see that this was suggested by Sean, but can you explain the problem
that this is working around? From the linked thread, it seems like the
problem is what to do when userspace also calls SET_CPUID after already
configuring CPUID to the TDX module in the special way. The choices discussed included:
1. Reject the call
2. Check the consistency between the first CPUID configuration and the
second one.

1 is a lot simpler, but the reasoning for 2 is because "some KVM code
paths rely on guest CPUID configuration" it seems. Is this a hypothetical or real issue? Which code paths are problematic for TDX/SNP?

Quoted 7 lines
Quoted 4 lines

There might be use case that TDX guest wants to use some CPUID which isn't handled by the TDX module but purely by KVM.  These (PV) CPUIDs need to be
provided via KVM_SET_CPUID2.

Right, but are there any needed today?

I am not sure. Isaku may know better?

Quoted 6 lines

I read that Sean's point was
that KVM_SET_CPUID2 can't accept anything today what we would want to block later, otherwise it would introduce a regression. This was the major constraint IIUC, and means the base series requires *something* here.

Quoted 9 lines

If we want to support only the most basic support first, we don't need to support PV CPUIDs on day 1, right?

So I'm wondering, if we could shrink the base series by going with option 1 to start, and then expanding it with this solution later to enable more features. Do you see a problem or conflict with Sean's comments?

To confirm, I mean you want to simply make KVM_SET_CPUID2 return error for TDX guest?

It is acceptable to me, and I don't see any conflict with Sean's comments.

But I don't know Sean's perference. As he said, I think the consistency checking is quite straight-forward:

"
It's not complicated at all. Walk through the leafs defined during TDH.MNG.INIT, reject KVM_SET_CPUID if a leaf isn't present or doesn't match exactly.
"

So to me it's not a big deal.

Either way, we need a patch to handle SET_CPUID2:

1) if we go option 1) -- that is reject SET_CPUID2 completely -- we need to make vcpu's CPUID point to KVM's saved CPUID during TDH.MNG.INIT.

2) if we do consistency check, we do a for loop and reject when in-consistency found.

I'll leave to you to judge :-)

#689 lore raw

Re: [PATCH v19 130/130] RFC: KVM: x86, TDX: Add check for KVM_SET_CPUID2

Quoted 5 lines · Kai

To confirm, I mean you want to simply make KVM_SET_CPUID2 return error for TDX guest?

It is acceptable to me, and I don't see any conflict with Sean's comments.

Quoted 9 lines

But I don't know Sean's perference.  As he said, I think  the consistency checking is quite straight-forward:

"
It's not complicated at all.  Walk through the leafs defined during TDH.MNG.INIT, reject KVM_SET_CPUID if a leaf isn't present or doesn't match exactly.
"

Yea, I'm just thinking if we could take two patches down to one small one it might be a way to essentially break off this work to another series without affecting the ability to boot a TD. It *seems* to be the way things are going.

Quoted 6 lines

So to me it's not a big deal.

Either way, we need a patch to handle SET_CPUID2:

1) if we go option 1) -- that is reject SET_CPUID2 completely -- we need to make vcpu's CPUID point to KVM's saved CPUID during TDH.MNG.INIT.

Ah, I missed this part. Can you elaborate? By dropping these two patches it doesn't prevent a TD boot. If we then reject SET_CPUID, this will break things unless we make other changes? And they are not small?

#690 lore raw

Re: [PATCH v19 130/130] RFC: KVM: x86, TDX: Add check for KVM_SET_CPUID2

Quoted 21 lines · Rick P
Quoted 5 lines · Kai

To confirm, I mean you want to simply make KVM_SET_CPUID2 return error for TDX guest?

It is acceptable to me, and I don't see any conflict with Sean's comments.

Quoted 9 lines

But I don't know Sean's perference.  As he said, I think  the consistency checking is quite straight-forward:

"
It's not complicated at all.  Walk through the leafs defined during TDH.MNG.INIT, reject KVM_SET_CPUID if a leaf isn't present or doesn't match exactly.
"

Yea, I'm just thinking if we could take two patches down to one small one it might be a way to essentially break off this work to another series without affecting the ability to boot a TD. It *seems* to be the way things are going.

Quoted 2 lines

So to me it's not a big deal.

Quoted 8 lines
Quoted 4 lines

Either way, we need a patch to handle SET_CPUID2:

1) if we go option 1) -- that is reject SET_CPUID2 completely -- we need to make vcpu's CPUID point to KVM's saved CPUID during TDH.MNG.INIT.

Ah, I missed this part. Can you elaborate? By dropping these two patches it doesn't prevent a TD boot. If we then reject SET_CPUID, this will break things unless we make other changes? And they are not small?

If we go forthis, the extended topology enumeration (cpuid[0xb or 0x1f]) would need special handling because it's per-vcpu. not TD wide.

Signature · 1 line
Isaku Yamahata <[email protected]>
#691 lore raw

RE: [PATCH v19 130/130] RFC: KVM: x86, TDX: Add check for KVM_SET_CPUID2

In reply to Edgecombe, Rick P
Quoted 24 lines · Kai
Quoted 5 lines

To confirm, I mean you want to simply make KVM_SET_CPUID2 return error for TDX guest?

It is acceptable to me, and I don't see any conflict with Sean's comments.

Quoted 9 lines

But I don't know Sean's perference.  As he said, I think  the consistency checking is quite straight-forward:

"
It's not complicated at all.  Walk through the leafs defined during TDH.MNG.INIT, reject KVM_SET_CPUID if a leaf isn't present or doesn't match exactly.
"

Yea, I'm just thinking if we could take two patches down to one small one it might be a way to essentially break off this work to another series without affecting the ability to boot a TD. It
*seems* to be the way things are going.

Quoted 4 lines

So to me it's not a big deal.

Either way, we need a patch to handle SET_CPUID2:

Quoted 8 lines
Quoted 2 lines

1) if we go option 1) -- that is reject SET_CPUID2 completely -- we need to make vcpu's CPUID point to KVM's saved CPUID during

TDH.MNG.INIT.

Ah, I missed this part. Can you elaborate? By dropping these two patches it doesn't prevent a TD boot. If we then reject SET_CPUID, this will break things unless we make other changes? And they are not small?

(sorry replying from outlook due to some issue to my linux box environment)

It booted because Qemu does sane thing, i.e., it always passes the correct CPUIDs in KVM_SET_CPUID2.

Per-Sean's comments, KVM should guarantee the consistency between CPUIDs done in TDH.MNG.INIT and KVM_SET_CPUID2, otherwise if Qemu passes in-consistent CPUIDs KVM can easily fail to work with TD.

To guarantee the consistency, KVM could do two options as we discussed:

1) reject KVM_SET_CPUID2 completely.
2) Still allow KVM_SET_CPUID2 but manually check the CPUID consistency between the one done in TDH.MNG.INIT and the one passed in KVM_SET_CPUID2.

1) can obviously guarantee consistency. But KVM maintains CPUIDs in 'vcpu', so to make the existing KVM code continue to work, we need to manually set 'vcpu->cpuid' to the one that is done in TDH.MNG.INIT.

2) you need to check the consistency and reject KVM_SET_CPUID2 if in-consistency found. But other than that, KVM doesn't need to anything more because if we allow KVM_SET_CPUID2, the 'vcpu' will have its own CPUIDs populated anyway.

#692 lore raw

Re: [PATCH v19 130/130] RFC: KVM: x86, TDX: Add check for KVM_SET_CPUID2

Quoted 14 lines · Kai

(sorry replying from outlook due to some issue to my linux box environment)

It booted because Qemu does sane thing, i.e., it always passes the correct CPUIDs in KVM_SET_CPUID2.

Per-Sean's comments, KVM should guarantee the consistency between CPUIDs done in TDH.MNG.INIT and KVM_SET_CPUID2, otherwise if Qemu passes in-consistent CPUIDs KVM can easily fail to work with TD.

To guarantee the consistency, KVM could do two options as we discussed:

1) reject KVM_SET_CPUID2 completely.
2) Still allow KVM_SET_CPUID2 but manually check the CPUID consistency between the one done in TDH.MNG.INIT and the one passed in KVM_SET_CPUID2.

Quoted 7 lines

1) can obviously guarantee consistency.  But KVM maintains CPUIDs in 'vcpu', so to make the existing KVM code continue to work, we need to manually set 'vcpu->cpuid' to the one that is done in TDH.MNG.INIT.

2) you need to check the consistency and reject KVM_SET_CPUID2 if in-consistency found.  But other than that, KVM doesn't need to anything more because if we allow KVM_SET_CPUID2, the 'vcpu' will have its own CPUIDs populated anyway.

Ah, thanks for explaining. So 1 is not that simple, it is a maybe slightly smaller separate solution. Now I see why the discussion was to just do the consistency checking up front.

#693 lore raw

Re: [PATCH v19 130/130] RFC: KVM: x86, TDX: Add check for KVM_SET_CPUID2

In reply to Huang, Kai
Quoted 26 lines · Kai
Quoted 25 lines · Rick P
Quoted 23 lines · Kai
Quoted 13 lines

I see that this was suggested by Sean, but can you explain the problem
that this is working around? From the linked thread, it seems like the
problem is what to do when userspace also calls SET_CPUID after already
configuring CPUID to the TDX module in the special way. The choices discussed included:
1. Reject the call
2. Check the consistency between the first CPUID configuration and the
second one.

Quoted 5 lines

1 is a lot simpler, but the reasoning for 2 is because "some KVM code
paths rely on guest CPUID configuration" it seems. Is this a hypothetical or real issue? Which code paths are problematic for TDX/SNP?

There might be use case that TDX guest wants to use some CPUID which isn't handled by the TDX module but purely by KVM.  These (PV) CPUIDs need to be
provided via KVM_SET_CPUID2.

Quoted 4 lines
Quoted 2 lines

Right, but are there any needed today?

I am not sure. Isaku may know better?

It's not needed to boot TD. The check is safe guard. The multiple of source of cpuids can be inconsistent.

Signature · 1 line
Isaku Yamahata <[email protected]>
#694 lore raw

Re: [PATCH v19 130/130] RFC: KVM: x86, TDX: Add check for KVM_SET_CPUID2

In reply to Huang, Kai
Quoted 33 lines · Kai
Quoted 22 lines · Rick P
Quoted 11 lines · On Mon, 2024-02-26 at 00:27 -0800, [email protected] wrote:

Implement a hook of KVM_SET_CPUID2 for additional consistency check.

Intel TDX or AMD SEV has a restriction on the value of cpuid.  For example,
some values must be the same between all vcpus.  Check if the new values
are consistent with the old values.  The check is light because the cpuid
consistency is very model specific and complicated.  The user space VMM
should set cpuid and MSRs consistently.

I see that this was suggested by Sean, but can you explain the problem that this is working around? From the linked thread, it seems like the problem is what to do when userspace also calls SET_CPUID after already configuring CPUID to the TDX module in the special way. The choices discussed included:
1. Reject the call
2. Check the consistency between the first CPUID configuration and the second one.

Quoted 4 lines

1 is a lot simpler, but the reasoning for 2 is because "some KVM code paths rely on guest CPUID configuration" it seems. Is this a hypothetical or real issue? Which code paths are problematic for TDX/SNP?

There might be use case that TDX guest wants to use some CPUID which isn't handled by the TDX module but purely by KVM. These (PV) CPUIDs need to be provided via KVM_SET_CPUID2.

Quoted 6 lines

Btw, Isaku, I don't understand why you tag the last two patches as RFC and put them at last. I think I've expressed this before. Per the discussion with Sean, my understanding is this isn't something optional but the right thing we should do?

https://lore.kernel.org/lkml/[email protected]/

Ok, let's remove RFC and reorder this patches. Do you see any issue of the cpuid check logic itself?

Signature · 1 line
Isaku Yamahata <[email protected]>