KVM TDX basic feature support
Subsystems (8)
RFC PATCH v5 75/104 KVM: x86: Check for pending APICv interrupt in kvm_vcpu_has_events()
In reply to [email protected]From: Sean Christopherson <[email protected]>
Return true for kvm_vcpu_has_events() if the vCPU has a pending APICv interrupt to support TDX's usage of APICv. Unlike VMX, TDX doesn't have access to vmcs.GUEST_INTR_STATUS and so can't emulate posted interrupts, i.e. needs to generate a posted interrupt and more importantly can't manually move requested interrupts into the vIRR (which it also doesn't have access to).
Because pi_has_pending_interrupt() is heavy operation which uses two atomic test bit operations and one atomic 256 bit bitmap check, introduce new callback for this check instead of reusing dy_apicv_has_pending_interrupt() callback to avoid affecting the exiting code.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/vmx/main.c | 9 +++++++++ arch/x86/kvm/x86.c | 5 ++++- 3 files changed, 14 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1491,6 +1491,7 @@ struct kvm_x86_ops {void (*start_assignment)(struct kvm *kvm);void (*apicv_post_state_restore)(struct kvm_vcpu *vcpu);bool (*dy_apicv_has_pending_interrupt)(struct kvm_vcpu *vcpu); +bool (*apicv_has_pending_interrupt)(struct kvm_vcpu *vcpu);int (*set_hv_timer)(struct kvm_vcpu *vcpu,u64 guest_deadline_tsc,bool *expired);
arch/x86/kvm/vmx/main.c +9 -0
@@ -148,6 +148,14 @@ static void vt_vcpu_load(struct kvm_vcpu *vcpu, int cpu)return vmx_vcpu_load (vcpu, cpu); } +static bool vt_apicv_has_pending_interrupt (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return pi_has_pending_interrupt (vcpu); + +return false; +} +static void vt_flush_tlb_all (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))
@@ -297,6 +305,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .sync_pir_to_irr = vmx_sync_pir_to_irr, .deliver_interrupt = vmx_deliver_interrupt, .dy_apicv_has_pending_interrupt = pi_has_pending_interrupt, + .apicv_has_pending_interrupt = vt_apicv_has_pending_interrupt, .set_tss_addr = vmx_set_tss_addr, .set_identity_map_addr = vmx_set_identity_map_addr,
arch/x86/kvm/x86.c +4 -1
@@ -12111,7 +12111,10 @@ static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)if (kvm_arch_interrupt_allowed (vcpu) && (kvm_cpu_has_interrupt (vcpu) || -kvm_guest_apic_has_interrupt (vcpu))) +kvm_guest_apic_has_interrupt (vcpu) || + (vcpu->arch.apicv_active && + kvm_x86_ops.apicv_has_pending_interrupt && + kvm_x86_ops.apicv_has_pending_interrupt (vcpu))))return true;if (kvm_hv_has_stimer_pending (vcpu))
Signature · 1 line
2.25.1
RFC PATCH v5 79/104 KVM: TDX: Implements vcpu request_immediate_exit
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Now we are able to inject interrupts into TDX vcpu, it's ready to block TDX vcpu. Wire up kvm x86 methods for blocking/unblocking vcpu for TDX. To unblock on pending events, request immediate exit methods is also needed.
--- arch/x86/kvm/vmx/main.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/main.c +9 -1
@@ -280,6 +280,14 @@ static void vt_enable_irq_window(struct kvm_vcpu *vcpu)vmx_enable_irq_window (vcpu); } +static void vt_request_immediate_exit (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return __kvm_request_immediate_exit (vcpu); + +vmx_request_immediate_exit (vcpu); +} +static int vt_mem_enc_op (struct kvm *kvm,void __user *argp) {if (!is_td (kvm))
@@ -402,7 +410,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .check_intercept = vmx_check_intercept, .handle_exit_irqoff = vmx_handle_exit_irqoff, - .request_immediate_exit = vmx_request_immediate_exit, + .request_immediate_exit = vt_request_immediate_exit, .sched_in = vt_sched_in,
Signature · 1 line
2.25.1
RFC PATCH v5 82/104 KVM: VMX: Move NMI/exception handler to common helper
In reply to [email protected]From: Sean Christopherson <[email protected]>
TDX mostly handles NMI/exception exit mostly the same to VMX case. The difference is how to retrieve exit qualification. To share the code with TDX, move NMI/exception to a common header
--- arch/x86/kvm/vmx/common.h | 50 ++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 60 ++++++--------------------------------- 2 files changed, 59 insertions(+), 51 deletions(-)
arch/x86/kvm/vmx/common.h +50 -0
@@ -4,8 +4,58 @@#include <linux/kvm_host.h> +#include <asm/traps.h> +#include "posted_intr.h" #include "mmu.h" +#include "vmcs.h" +#include "x86.h" + +extern unsigned long vmx_host_idt_base; +void vmx_handle_nm_fault_irqoff (struct kvm_vcpu *vcpu); +void vmx_do_interrupt_nmi_irqoff (unsigned long entry); + +static inline void vmx_handle_interrupt_nmi_irqoff (struct kvm_vcpu *vcpu, +unsigned long entry) +{ +bool is_nmi = entry == (unsigned long )asm_exc_nmi_noist; + +kvm_before_interrupt (vcpu, is_nmi ?KVM_HANDLING_NMI :KVM_HANDLING_IRQ ); +vmx_do_interrupt_nmi_irqoff (entry); +kvm_after_interrupt (vcpu); +} + +static inline void vmx_handle_exception_nmi_irqoff (struct kvm_vcpu *vcpu, +u32 intr_info) +{ +const unsigned long nmi_entry = (unsigned long )asm_exc_nmi_noist; + +/* if exit due to PF check for async PF */ +if (is_page_fault (intr_info)) + vcpu->arch.apf.host_apf_flags =kvm_read_and_reset_apf_flags (); +/* if exit due to NM, handle before interrupts are enabled */ +else if (is_nm_fault (intr_info)) +vmx_handle_nm_fault_irqoff (vcpu); +/* Handle machine checks before interrupts are enabled */ +else if (is_machine_check (intr_info)) +kvm_machine_check (); +/* We need to handle NMIs before interrupts are enabled */ +else if (is_nmi (intr_info)) +vmx_handle_interrupt_nmi_irqoff (vcpu, nmi_entry); +} + +static inline void vmx_handle_external_interrupt_irqoff (struct kvm_vcpu *vcpu, +u32 intr_info) +{ +unsigned int vector = intr_info &INTR_INFO_VECTOR_MASK ; +gate_desc *desc = (gate_desc *)vmx_host_idt_base + vector; + +if (KVM_BUG (!is_external_intr (intr_info), vcpu->kvm, +"KVM: unexpected VM-Exit interrupt info: 0x%x" , intr_info)) +return ; + +vmx_handle_interrupt_nmi_irqoff (vcpu,gate_offset (desc)); +}static inline int __vmx_handle_ept_violation (struct kvm_vcpu *vcpu,gpa_t gpa,unsigned long exit_qualification)
arch/x86/kvm/vmx/vmx.c +9 -51
105 lines not on this page · raw message
Signature · 1 line
2.25.1
RFC PATCH v5 76/104 KVM: x86: Add option to force LAPIC expiration wait
In reply to [email protected]From: Sean Christopherson <[email protected]>
Add an option to skip the IRR check-in kvm_wait_lapic_expire(). This will be used by TDX to wait if there is an outstanding notification for a TD, i.e. a virtual interrupt is being triggered via posted interrupt processing. KVM TDX doesn't emulate PI processing, i.e. there will never be a bit set in IRR/ISR, so the default behavior for APICv of querying the IRR doesn't work as intended.
--- arch/x86/kvm/lapic.c | 4 ++-- arch/x86/kvm/lapic.h | 2 +- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/vmx/vmx.c | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-)
arch/x86/kvm/lapic.c +2 -2
@@ -1620,12 +1620,12 @@ static void __kvm_wait_lapic_expire(struct kvm_vcpu *vcpu)__wait_lapic_expire (vcpu, tsc_deadline - guest_tsc); } -void kvm_wait_lapic_expire (struct kvm_vcpu *vcpu) +void kvm_wait_lapic_expire (struct kvm_vcpu *vcpu,bool force_wait) {if (lapic_in_kernel (vcpu) && vcpu->arch.apic->lapic_timer.expired_tscdeadline && vcpu->arch.apic->lapic_timer.timer_advance_ns && -lapic_timer_int_injected (vcpu)) + (force_wait ||lapic_timer_int_injected (vcpu)))__kvm_wait_lapic_expire (vcpu); }EXPORT_SYMBOL_GPL (kvm_wait_lapic_expire);
arch/x86/kvm/lapic.h +1 -1
@@ -233,7 +233,7 @@ static inline int kvm_lapic_latched_init(struct kvm_vcpu *vcpu)bool kvm_apic_pending_eoi (struct kvm_vcpu *vcpu,int vector); -void kvm_wait_lapic_expire (struct kvm_vcpu *vcpu); +void kvm_wait_lapic_expire (struct kvm_vcpu *vcpu,bool force_wait);void kvm_bitmap_or_dest_vcpus (struct kvm *kvm,struct kvm_lapic_irq *irq,unsigned long *vcpu_bitmap);
arch/x86/kvm/svm/svm.c +1 -1
@@ -3766,7 +3766,7 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)clgi ();kvm_load_guest_xsave_state (vcpu); -kvm_wait_lapic_expire (vcpu); +kvm_wait_lapic_expire (vcpu, false);/* * If this vCPU has touched SPEC_CTRL, restore the guest's value if
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -6838,7 +6838,7 @@ fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu)if (enable_preemption_timer)vmx_update_hv_timer (vcpu); -kvm_wait_lapic_expire (vcpu); +kvm_wait_lapic_expire (vcpu, false);/* * If this vCPU has touched SPEC_CTRL, restore the guest's value if
Signature · 1 line
2.25.1
RFC PATCH v5 78/104 KVM: TDX: Implement interrupt injection
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDX supports interrupt inject into vcpu with posted interrupt. Wire up the corresponding kvm x86 operations to posted interrupt. Move kvm_vcpu_trigger_posted_interrupt() from vmx.c to common.h to share the code.
VMX can inject interrupt by setting interrupt information field, VM_ENTRY_INTR_INFO_FIELD, of VMCS. TDX supports interrupt injection only by posted interrupt. Ignore the execution path to access VM_ENTRY_INTR_INFO_FIELD.
As cpu state is protected and apicv is enabled for the TDX guest, VMM can inject interrupt by updating posted interrupt descriptor. Treat interrupt can be injected always.
--- arch/x86/kvm/vmx/common.h | 70 +++++++++++++++++++++++++ arch/x86/kvm/vmx/main.c | 93 ++++++++++++++++++++++++++++++---- arch/x86/kvm/vmx/posted_intr.c | 6 +++ arch/x86/kvm/vmx/tdx.c | 33 ++++++++++++ arch/x86/kvm/vmx/tdx.h | 3 ++ arch/x86/kvm/vmx/vmx.c | 57 +-------------------- arch/x86/kvm/vmx/x86_ops.h | 7 ++- 7 files changed, 203 insertions(+), 66 deletions(-)
arch/x86/kvm/vmx/common.h +70 -0
@@ -4,6 +4,7 @@#include <linux/kvm_host.h> +#include "posted_intr.h" #include "mmu.h" static inline int __vmx_handle_ept_violation(struct kvm_vcpu *vcpu, gpa_t gpa,
@@ -32,4 +33,73 @@ static inline int __vmx_handle_ept_violation(struct kvm_vcpu *vcpu, gpa_t gpa, return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); } +static inline void kvm_vcpu_trigger_posted_interrupt (struct kvm_vcpu *vcpu, +int pi_vec) +{ +#ifdef CONFIG_SMP +if (vcpu->mode ==IN_GUEST_MODE ) { +/* +* The vector of interrupt to be delivered to vcpu had +* been set in PIR before this function. +* +* Following cases will be reached in this block, and +* we always send a notification event in all cases as +* explained below. +* +* Case 1: vcpu keeps in non-root mode. Sending a +* notification event posts the interrupt to vcpu. +* +* Case 2: vcpu exits to root mode and is still +* runnable. PIR will be synced to vIRR before the +* next vcpu entry. Sending a notification event in +* this case has no effect, as vcpu is not in root +* mode. +* +* Case 3: vcpu exits to root mode and is blocked. +* vcpu_block() has already synced PIR to vIRR and +* never blocks vcpu if vIRR is not cleared. Therefore, +* a blocked vcpu here does not wait for any requested +* interrupts in PIR, and sending a notification event +* which has no effect is safe here. +*/ + + apic->send_IPI_mask (get_cpu_mask (vcpu->cpu), pi_vec); +return ; + } +#endif +/* +* The vCPU isn't in the guest; wake the vCPU in case it is blocking, +* otherwise do nothing as KVM will grab the highest priority pending +* IRQ via ->sync_pir_to_irr() in vcpu_enter_guest(). +*/ +kvm_vcpu_wake_up (vcpu); +} + +/* +* Send interrupt to vcpu via posted interrupt way. +* 1. If target vcpu is running(non-root mode), send posted interrupt +* notification to vcpu and hardware will sync PIR to vIRR atomically. +* 2. If target vcpu isn't running(root mode), kick it to pick up the +* interrupt from PIR in next vmentry. +*/ +static inline void __vmx_deliver_posted_interrupt ( +struct kvm_vcpu *vcpu,struct pi_desc *pi_desc,int vector) +{ +if (pi_test_and_set_pir (vector, pi_desc)) +return ; + +/* If a previous notification has sent the IPI, nothing to do. */ +if (pi_test_and_set_on (pi_desc)) +return ; + +/* +* The implied barrier in pi_test_and_set_on() pairs with the smp_mb_*() +* after setting vcpu->mode in vcpu_enter_guest(), thus the vCPU is +* guaranteed to see PID.ON=1 and sync the PIR to IRR if triggering a +* posted interrupt "fails" because vcpu->mode != IN_GUEST_MODE. +*/ +kvm_vcpu_trigger_posted_interrupt (vcpu,POSTED_INTR_VECTOR ); +} + +#endif /* __KVM_X86_VMX_COMMON_H */
arch/x86/kvm/vmx/main.c +84 -9
127 lines not on this page · raw message
arch/x86/kvm/vmx/posted_intr.c +6 -0
@@ -7,6 +7,7 @@#include "lapic.h" #include "irq.h" #include "posted_intr.h" +#include "tdx.h" #include "trace.h" #include "vmx.h"
@@ -31,6 +32,11 @@ static DEFINE_PER_CPU(raw_spinlock_t, wakeup_vcpus_on_cpu_lock);static inline struct pi_desc *vcpu_to_pi_desc (struct kvm_vcpu *vcpu) { +#ifdef CONFIG_INTEL_TDX_HOST +if (is_td_vcpu (vcpu)) +return &(to_tdx (vcpu)->pi_desc); +#endif +return &(to_vmx (vcpu)->pi_desc); }
arch/x86/kvm/vmx/tdx.c +33 -0
@@ -7,6 +7,7 @@#include "capabilities.h" #include "x86_ops.h" +#include "common.h" #include "mmu.h" #include "tdx.h" #include "vmx.h"
@@ -494,6 +495,9 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu) vcpu->arch.guest_state_protected = !(to_kvm_tdx (vcpu->kvm)->attributes &TDX_TD_ATTRIBUTE_DEBUG ); + tdx->pi_desc.nv =POSTED_INTR_VECTOR ; + tdx->pi_desc.sn =1 ; + tdx->host_state_need_save = true; tdx->host_state_need_restore = false;
@@ -514,6 +518,7 @@ void tdx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) {struct vcpu_tdx *tdx =to_tdx (vcpu); +vmx_vcpu_pi_load (vcpu, cpu);if (vcpu->cpu == cpu)return ;
@@ -735,6 +740,12 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)trace_kvm_entry (vcpu); +if (pi_test_on (&tdx->pi_desc)) { + apic->send_IPI_self (POSTED_INTR_VECTOR ); + +kvm_wait_lapic_expire (vcpu, true); + } +tdx_vcpu_enter_exit (vcpu, tdx);tdx_user_return_update_cache ();
@@ -1008,6 +1019,24 @@ static void tdx_handle_changed_private_spte( } } +void tdx_apicv_post_state_restore (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +pi_clear_on (&tdx->pi_desc); +memset (tdx->pi_desc.pir,0 ,sizeof (tdx->pi_desc.pir)); +} + +void tdx_deliver_interrupt (struct kvm_lapic *apic,int delivery_mode, +int trig_mode,int vector) +{ +struct kvm_vcpu *vcpu = apic->vcpu; +struct vcpu_tdx *tdx =to_tdx (vcpu); + +/* TDX supports only posted interrupt. No lapic emulation. */ +__vmx_deliver_posted_interrupt (vcpu, &tdx->pi_desc, vector); +} +static int tdx_capabilities (struct kvm *kvm,struct kvm_tdx_cmd *cmd) {struct kvm_tdx_capabilities __user *user_caps;
@@ -1425,6 +1454,10 @@ int tdx_vcpu_ioctl(struct kvm_vcpu *vcpu, void __user *argp)return -EIO ; } +td_vmcs_write16 (tdx,POSTED_INTR_NV ,POSTED_INTR_VECTOR ); +td_vmcs_write64 (tdx,POSTED_INTR_DESC_ADDR ,__pa (&tdx->pi_desc)); +td_vmcs_setbit32 (tdx,PIN_BASED_VM_EXEC_CONTROL ,PIN_BASED_POSTED_INTR ); + tdx->initialized = true;return 0 ; }
arch/x86/kvm/vmx/tdx.h +3 -0
@@ -83,6 +83,9 @@ struct vcpu_tdx {struct list_head cpu_list; +/* Posted interrupt descriptor */ +struct pi_desc pi_desc; +union tdx_exit_reason exit_reason;bool initialized;
arch/x86/kvm/vmx/vmx.c +1 -56
69 lines not on this page · raw message
arch/x86/kvm/vmx/x86_ops.h +6 -1
@@ -148,7 +148,8 @@ void tdx_vcpu_put(struct kvm_vcpu *vcpu);void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu);void tdx_apicv_post_state_restore (struct kvm_vcpu *vcpu); -int tdx_deliver_posted_interrupt (struct kvm_vcpu *vcpu,int vector); +void tdx_deliver_interrupt (struct kvm_lapic *apic,int delivery_mode, +int trig_mode,int vector);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -176,6 +177,10 @@ static inline void tdx_prepare_switch_to_guest(struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_put (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu) {} +static inline void tdx_apicv_post_state_restore (struct kvm_vcpu *vcpu) {} +static inline void tdx_deliver_interrupt ( +struct kvm_lapic *apic,int delivery_mode,int trig_mode,int vector) {} +static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
Signature · 1 line
2.25.1
RFC PATCH v5 92/104 KVM: TDX: Handle TDX PV HLT hypercall
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Wire up TDX PV HLT hypercall to the KVM backend function.
When the guest issues HLT, the hypercall instruction can be the right after CLI instruction. Atomically unmask virtual interrupt and issue HLT hypercall. The virtual interrupts can arrive right after CLI instruction before switching back to VMM. In such a case, the VMM should return to the guest without losing the interrupt. Check if interrupts arrived before the TDX module switching to VMM. And return to the guest in such cases.
--- arch/x86/kvm/vmx/tdx.c | 45 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/tdx.c +44 -1
@@ -917,6 +917,48 @@ static int tdx_emulate_cpuid(struct kvm_vcpu *vcpu)return 1 ; } +static int tdx_emulate_hlt (struct kvm_vcpu *vcpu) +{ +bool interrupt_disabled =tdvmcall_p1_read (vcpu); +union tdx_vcpu_state_details details; + +tdvmcall_set_return_code (vcpu,TDG_VP_VMCALL_SUCCESS ); + +if (!interrupt_disabled) { +/* +* Virtual interrupt can arrive after TDG.VM.VMCALL<HLT> during +* the TDX module executing. On the other hand, KVM doesn't +* know if vcpu was executing in the guest TD or the TDX module. +* +* CPU mode transition: +* TDG.VP.VMCALL<HLT> (SEAM VMX non-root mode) -> +* the TDX module (SEAM VMX root mode) -> +* KVM (Legacy VMX root mode) +* +* If virtual interrupt arrives to this vcpu +* - In the guest TD executing: +* KVM can handle it in the same way to the VMX case. +* - During the TDX module executing: +* The TDX modules switches to KVM with TDG.VM.VMCALL<HLT> +* exit reason. KVM thinks the guest was running. So KVM +* vcpu wake up logic doesn't kick in. Check if virtual +* interrupt is pending and resume vcpu without blocking vcpu. +* - KVM executing: +* The existing logic wakes up the target vcpu on injecting +* virtual interrupt in the same way to the VMX case. +* +* Check if the interrupt is already pending. If yes, resume +* vcpu from guest HLT without emulating hlt instruction. +*/ + details.full =td_state_non_arch_read64 ( +to_tdx (vcpu),TD_VCPU_STATE_DETAILS_NON_ARCH ); +if (details.vmxip) +return 1 ; + } + +return kvm_emulate_halt_noskip (vcpu); +} +static int handle_tdvmcall (struct kvm_vcpu *vcpu) {struct vcpu_tdx *tdx =to_tdx (vcpu);
@@ -930,7 +972,8 @@ static int handle_tdvmcall(struct kvm_vcpu *vcpu)switch (tdvmcall_exit_reason (vcpu)) {case EXIT_REASON_CPUID :return tdx_emulate_cpuid (vcpu); - +case EXIT_REASON_HLT : +return tdx_emulate_hlt (vcpu);default :break ; }
Signature · 1 line
2.25.1
RFC PATCH v5 86/104 KVM: TDX: handle ept violation/misconfig exit
In reply to [email protected]From: Isaku Yamahata <[email protected]>
On EPT violation, call a common function, __vmx_handle_ept_violation() to trigger x86 MMU code. On EPT misconfiguration, exit to ring 3 with KVM_EXIT_UNKNOWN. because EPT misconfiguration can't happen as MMIO is trigged by TDG.VP.VMCALL. No point to set a misconfiguration value for the fast path.
--- arch/x86/kvm/vmx/tdx.c | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/tdx.c +38 -2
@@ -1081,6 +1081,40 @@ void tdx_deliver_interrupt(struct kvm_lapic *apic, int delivery_mode,__vmx_deliver_posted_interrupt (vcpu, &tdx->pi_desc, vector); } +#define TDX_SEPT_PFERR (PFERR_WRITE_MASK | PFERR_USER_MASK) + +static int tdx_handle_ept_violation (struct kvm_vcpu *vcpu) +{ +unsigned long exit_qual; + +if (kvm_is_private_gpa (vcpu->kvm,tdexit_gpa (vcpu))) + exit_qual =TDX_SEPT_PFERR ; +else { + exit_qual =tdexit_exit_qual (vcpu); +if (exit_qual &EPT_VIOLATION_ACC_INSTR ) { +pr_warn ("kvm: TDX instr fetch to shared GPA = 0x%lx @ RIP = 0x%lx\n" , +tdexit_gpa (vcpu),kvm_rip_read (vcpu)); + vcpu->run->exit_reason =KVM_EXIT_EXCEPTION ; + vcpu->run->ex.exception =PF_VECTOR ; + vcpu->run->ex.error_code = exit_qual; +return 0 ; + } + } + +trace_kvm_page_fault (tdexit_gpa (vcpu), exit_qual); +return __vmx_handle_ept_violation (vcpu,tdexit_gpa (vcpu), exit_qual); +} + +static int tdx_handle_ept_misconfig (struct kvm_vcpu *vcpu) +{ +WARN_ON (1 ); + + vcpu->run->exit_reason =KVM_EXIT_UNKNOWN ; + vcpu->run->hw.hardware_exit_reason =EXIT_REASON_EPT_MISCONFIG ; + +return 0 ; +} +int tdx_handle_exit (struct kvm_vcpu *vcpu,fastpath_t fastpath) {union tdx_exit_reason exit_reason =to_tdx (vcpu)->exit_reason;
@@ -1097,6 +1131,10 @@ int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath)WARN_ON_ONCE (fastpath !=EXIT_FASTPATH_NONE );switch (exit_reason.basic) { +case EXIT_REASON_EPT_VIOLATION : +return tdx_handle_ept_violation (vcpu); +case EXIT_REASON_EPT_MISCONFIG : +return tdx_handle_ept_misconfig (vcpu);case EXIT_REASON_OTHER_SMI :/* * If reach here, it's not a MSMI.
@@ -1378,8 +1416,6 @@ void tdx_flush_tlb(struct kvm_vcpu *vcpu)cpu_relax (); } -#define TDX_SEPT_PFERR (PFERR_WRITE_MASK | PFERR_USER_MASK) -static int tdx_init_mem_region (struct kvm *kvm,struct kvm_tdx_cmd *cmd) {struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm);
Signature · 1 line
2.25.1
RFC PATCH v5 87/104 KVM: TDX: handle EXCEPTION_NMI and EXTERNAL_INTERRUPT
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Because guest TD state is protected, exceptions in guest TDs can't be intercepted. TDX VMM doesn't need to handle exceptions. tdx_handle_exit_irqoff() handles NMI and machine check. Ignore NMI and machine check and continue guest TD execution.
For external interrupt, increment stats same to the VMX case.
--- arch/x86/kvm/vmx/tdx.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
arch/x86/kvm/vmx/tdx.c +21 -0
@@ -800,6 +800,23 @@ void tdx_handle_exit_irqoff(struct kvm_vcpu *vcpu) tdexit_intr_info(vcpu)); } +static int tdx_handle_exception (struct kvm_vcpu *vcpu) +{ +u32 intr_info =tdexit_intr_info (vcpu); + +if (is_nmi (intr_info) ||is_machine_check (intr_info)) +return 1 ; + +kvm_pr_unimpl ("unexpected exception 0x%x\n" , intr_info); +return -EFAULT ; +} + +static int tdx_handle_external_interrupt (struct kvm_vcpu *vcpu) +{ + ++vcpu->stat.irq_exits; +return 1 ; +} +static int tdx_handle_triple_fault (struct kvm_vcpu *vcpu) { vcpu->run->exit_reason =KVM_EXIT_SHUTDOWN ;
@@ -1131,6 +1148,10 @@ int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath)WARN_ON_ONCE (fastpath !=EXIT_FASTPATH_NONE );switch (exit_reason.basic) { +case EXIT_REASON_EXCEPTION_NMI : +return tdx_handle_exception (vcpu); +case EXIT_REASON_EXTERNAL_INTERRUPT : +return tdx_handle_external_interrupt (vcpu);case EXIT_REASON_EPT_VIOLATION :return tdx_handle_ept_violation (vcpu);case EXIT_REASON_EPT_MISCONFIG :
Signature · 1 line
2.25.1
RFC PATCH v5 97/104 KVM: TDX: Handle TDX PV wrmsr hypercall
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Wire up TDX PV wrmsr hypercall to the KVM backend function.
--- arch/x86/kvm/vmx/tdx.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
arch/x86/kvm/vmx/tdx.c +18 -0
@@ -1141,6 +1141,22 @@ static int tdx_emulate_rdmsr(struct kvm_vcpu *vcpu)return 1 ; } +static int tdx_emulate_wrmsr (struct kvm_vcpu *vcpu) +{ +u32 index =tdvmcall_p1_read (vcpu); +u64 data =tdvmcall_p2_read (vcpu); + +if (kvm_set_msr (vcpu, index, data)) { +trace_kvm_msr_write_ex (index, data); +tdvmcall_set_return_code (vcpu,TDG_VP_VMCALL_INVALID_OPERAND ); +return 1 ; + } + +trace_kvm_msr_write (index, data); +tdvmcall_set_return_code (vcpu,TDG_VP_VMCALL_SUCCESS ); +return 1 ; +} +static int handle_tdvmcall (struct kvm_vcpu *vcpu) {struct vcpu_tdx *tdx =to_tdx (vcpu);
@@ -1162,6 +1178,8 @@ static int handle_tdvmcall(struct kvm_vcpu *vcpu)return tdx_emulate_mmio (vcpu);case EXIT_REASON_MSR_READ :return tdx_emulate_rdmsr (vcpu); +case EXIT_REASON_MSR_WRITE : +return tdx_emulate_wrmsr (vcpu);default :break ; }
Signature · 1 line
2.25.1
RFC PATCH v5 99/104 KVM: TDX: Handle TDX PV map_gpa hypercall
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Wire up TDX PV map_gpa hypercall to the kvm/mmu backend.
--- arch/x86/kvm/vmx/tdx.c | 59 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+)
arch/x86/kvm/vmx/tdx.c +59 -0
@@ -1166,6 +1166,63 @@ static int tdx_report_fatal_error(struct kvm_vcpu *vcpu)return 0 ; } +static int tdx_map_gpa (struct kvm_vcpu *vcpu) +{ +struct kvm *kvm = vcpu->kvm; +gpa_t gpa =tdvmcall_p1_read (vcpu); +gpa_t size =tdvmcall_p2_read (vcpu); +gpa_t end = gpa + size; + +tdvmcall_set_return_code (vcpu,TDG_VP_VMCALL_INVALID_OPERAND ); +if (!IS_ALIGNED (gpa,4096 ) || !IS_ALIGNED (size,4096 ) || + end < gpa || + end >kvm_gfn_stolen_mask (kvm) << (PAGE_SHIFT +1 ) || +kvm_is_private_gpa (kvm, gpa) !=kvm_is_private_gpa (kvm, end)) +return 1 ; + +tdvmcall_set_return_code (vcpu,TDG_VP_VMCALL_SUCCESS ); + +#define TDX_MAP_GPA_SIZE_MAX (16 * 1024 * 1024) +while (gpa < end) { +gfn_t s =gpa_to_gfn (gpa); +gfn_t e =gpa_to_gfn ( +min (roundup (gpa +1 ,TDX_MAP_GPA_SIZE_MAX ), end)); +int ret =kvm_mmu_map_gpa (vcpu, &s, e); + +if (ret == -EAGAIN ) + e = s; +else if (ret) { +tdvmcall_set_return_code (vcpu, +TDG_VP_VMCALL_INVALID_OPERAND ); +break ; + } + + gpa =gfn_to_gpa (e); + +/* +* TODO: +* Interrupt this hypercall invocation to return remaining +* region to the guest and let the guest to resume the +* hypercall. +* +* The TDX Guest-Hypervisor Communication Interface(GHCI) +* specification and guest implementation need to be updated. +* +* if (gpa < end && need_resched()) { +* size = end - gpa; +* kvm_r12_write(vcpu, gpa); +* kvm_r13_write(vcpu, size); +* tdvmcall_set_return_code(vcpu, TDG_VP_VMCALL_INTERRUPTED_RESUME); +* break; +* } +*/ +if (gpa < end &&need_resched ()) +cond_resched (); + } + +return 1 ; +} +static int handle_tdvmcall (struct kvm_vcpu *vcpu) {struct vcpu_tdx *tdx =to_tdx (vcpu);
@@ -1191,6 +1248,8 @@ static int handle_tdvmcall(struct kvm_vcpu *vcpu)return tdx_emulate_wrmsr (vcpu);case TDG_VP_VMCALL_REPORT_FATAL_ERROR :return tdx_report_fatal_error (vcpu); +case TDG_VP_VMCALL_MAP_GPA : +return tdx_map_gpa (vcpu);default :break ; }
Signature · 1 line
2.25.1
RFC PATCH v5 83/104 KVM: x86: Split core of hypercall emulation to helper function
In reply to [email protected]From: Sean Christopherson <[email protected]>
By necessity, TDX will use a different register ABI for hypercalls. Break out the core functionality so that it may be reused for TDX.
--- arch/x86/include/asm/kvm_host.h | 4 +++ arch/x86/kvm/x86.c | 54 ++++++++++++++++++++------------- 2 files changed, 37 insertions(+), 21 deletions(-)
arch/x86/include/asm/kvm_host.h +4 -0
@@ -1818,6 +1818,10 @@ void kvm_request_apicv_update(struct kvm *kvm, bool activate,void __kvm_request_apicv_update (struct kvm *kvm,bool activate,unsigned long bit); +unsigned long __kvm_emulate_hypercall (struct kvm_vcpu *vcpu,unsigned long nr, +unsigned long a0,unsigned long a1, +unsigned long a2,unsigned long a3, +int op_64_bit);int kvm_emulate_hypercall (struct kvm_vcpu *vcpu); int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code,
arch/x86/kvm/x86.c +33 -21
76 lines not on this page · raw message
Signature · 1 line
2.25.1
RFC PATCH v5 94/104 KVM: TDX: Handle TDX PV MMIO hypercall
In reply to [email protected]From: Sean Christopherson <[email protected]>
Export kvm_io_bus_read and kvm_mmio tracepoint and wire up TDX PV MMIO hypercall to the KVM backend functions.
kvm_io_bus_read/write() searches KVM device emulated in kernel of the given MMIO address and emulates the MMIO. As TDX PV MMIO also needs it, export kvm_io_bus_read(). kvm_io_bus_write() is already exported. TDX PV MMIO emulates some of MMIO itself. To add trace point consistently with x86 kvm, export kvm_mmio tracepoint.
--- arch/x86/kvm/vmx/tdx.c | 114 +++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 1 + virt/kvm/kvm_main.c | 2 + 3 files changed, 117 insertions(+)
arch/x86/kvm/vmx/tdx.c +114 -0
@@ -1012,6 +1012,118 @@ static int tdx_emulate_io(struct kvm_vcpu *vcpu)return ret; } +static int tdx_complete_mmio (struct kvm_vcpu *vcpu) +{ +unsigned long val =0 ; +gpa_t gpa; +int size; + +WARN_ON (vcpu->mmio_needed !=1 ); + vcpu->mmio_needed =0 ; + +if (!vcpu->mmio_is_write) { + gpa = vcpu->mmio_fragments[0 ].gpa; + size = vcpu->mmio_fragments[0 ].len; + +memcpy (&val, vcpu->run->mmio.data, size); +tdvmcall_set_return_val (vcpu, val); +trace_kvm_mmio (KVM_TRACE_MMIO_READ , size, gpa, &val); + } +return 1 ; +} + +static inline int tdx_mmio_write (struct kvm_vcpu *vcpu,gpa_t gpa,int size, +unsigned long val) +{ +if (kvm_iodevice_write (vcpu, &vcpu->arch.apic->dev, gpa, size, &val) && +kvm_io_bus_write (vcpu,KVM_MMIO_BUS , gpa, size, &val)) +return -EOPNOTSUPP ; + +trace_kvm_mmio (KVM_TRACE_MMIO_WRITE , size, gpa, &val); +return 0 ; +} + +static inline int tdx_mmio_read (struct kvm_vcpu *vcpu,gpa_t gpa,int size) +{ +unsigned long val; + +if (kvm_iodevice_read (vcpu, &vcpu->arch.apic->dev, gpa, size, &val) && +kvm_io_bus_read (vcpu,KVM_MMIO_BUS , gpa, size, &val)) +return -EOPNOTSUPP ; + +tdvmcall_set_return_val (vcpu, val); +trace_kvm_mmio (KVM_TRACE_MMIO_READ , size, gpa, &val); +return 0 ; +} + +static int tdx_emulate_mmio (struct kvm_vcpu *vcpu) +{ +struct kvm_memory_slot *slot; +int size, write, r; +unsigned long val; +gpa_t gpa; + +WARN_ON (vcpu->mmio_needed); + + size =tdvmcall_p1_read (vcpu); + write =tdvmcall_p2_read (vcpu); + gpa =tdvmcall_p3_read (vcpu); + val = write ?tdvmcall_p4_read (vcpu) :0 ; + +if (size !=1 && size !=2 && size !=4 && size !=8 ) + goto error; +if (write !=0 && write !=1 ) + goto error; + +/* Strip the shared bit, allow MMIO with and without it set. */ + gpa =kvm_gpa_unalias (vcpu->kvm, gpa); + +if (size >8u || ((gpa + size -1 ) ^ gpa) &PAGE_MASK ) + goto error; + + slot =kvm_vcpu_gfn_to_memslot (vcpu,gpa_to_gfn (gpa)); +if (slot && !(slot->flags &KVM_MEMSLOT_INVALID )) + goto error; + +if (!kvm_io_bus_write (vcpu,KVM_FAST_MMIO_BUS , gpa,0 ,NULL )) { +trace_kvm_fast_mmio (gpa); +return 1 ; + } + +if (write) + r =tdx_mmio_write (vcpu, gpa, size, val); +else + r =tdx_mmio_read (vcpu, gpa, size); +if (!r) { +/* Kernel completed device emulation. */ +tdvmcall_set_return_code (vcpu,TDG_VP_VMCALL_SUCCESS ); +return 1 ; + } + +/* Request the device emulation to userspace device model. */ + vcpu->mmio_needed =1 ; + vcpu->mmio_is_write = write; + vcpu->arch.complete_userspace_io = tdx_complete_mmio; + + vcpu->run->mmio.phys_addr = gpa; + vcpu->run->mmio.len = size; + vcpu->run->mmio.is_write = write; + vcpu->run->exit_reason =KVM_EXIT_MMIO ; + +if (write) { +memcpy (vcpu->run->mmio.data, &val, size); + }else { + vcpu->mmio_fragments[0 ].gpa = gpa; + vcpu->mmio_fragments[0 ].len = size; +trace_kvm_mmio (KVM_TRACE_MMIO_READ_UNSATISFIED , size, gpa,NULL ); + } +return 0 ; + +error: +tdvmcall_set_return_code (vcpu,TDG_VP_VMCALL_SUCCESS ); +return 1 ; +} +static int handle_tdvmcall (struct kvm_vcpu *vcpu) {struct vcpu_tdx *tdx =to_tdx (vcpu);
@@ -1029,6 +1141,8 @@ static int handle_tdvmcall(struct kvm_vcpu *vcpu)return tdx_emulate_hlt (vcpu);case EXIT_REASON_IO_INSTRUCTION :return tdx_emulate_io (vcpu); +case EXIT_REASON_EPT_VIOLATION : +return tdx_emulate_mmio (vcpu);default :break ; }
arch/x86/kvm/x86.c +1 -0
@@ -12915,6 +12915,7 @@ bool kvm_arch_dirty_log_supported(struct kvm *kvm)EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_entry);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_exit); +EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_mmio);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_fast_mmio);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_inj_virq);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_page_fault);
virt/kvm/kvm_main.c +2 -0
@@ -2259,6 +2259,7 @@ struct kvm_memory_slot *kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfnreturn NULL ; } +EXPORT_SYMBOL_GPL (kvm_vcpu_gfn_to_memslot);bool kvm_is_visible_gfn (struct kvm *kvm,gfn_t gfn) {
@@ -5126,6 +5127,7 @@ int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr, r =__kvm_io_bus_read (vcpu, bus, &range, val);return r <0 ? r :0 ; } +EXPORT_SYMBOL_GPL (kvm_io_bus_read);/* Caller must hold slots_lock. */ int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
Signature · 1 line
2.25.1
RFC PATCH v5 85/104 KVM: TDX: handle EXIT_REASON_OTHER_SMI
In reply to [email protected]From: Isaku Yamahata <[email protected]>
If the control reaches EXIT_REASON_OTHER_SMI, #SMI is delivered and handled right after returning from the TDX module to KVM nothing needs to be done in KVM. Continue TDX vcpu execution.
--- arch/x86/include/uapi/asm/vmx.h | 1 + arch/x86/kvm/vmx/tdx.c | 7 +++++++ 2 files changed, 8 insertions(+)
arch/x86/include/uapi/asm/vmx.h +1 -0
@@ -34,6 +34,7 @@#define EXIT_REASON_TRIPLE_FAULT 2#define EXIT_REASON_INIT_SIGNAL 3#define EXIT_REASON_SIPI_SIGNAL 4 +#define EXIT_REASON_OTHER_SMI 6#define EXIT_REASON_INTERRUPT_WINDOW 7#define EXIT_REASON_NMI_WINDOW 8
arch/x86/kvm/vmx/tdx.c +7 -0
@@ -1097,6 +1097,13 @@ int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath)WARN_ON_ONCE (fastpath !=EXIT_FASTPATH_NONE );switch (exit_reason.basic) { +case EXIT_REASON_OTHER_SMI : +/* +* If reach here, it's not a MSMI. +* #SMI is delivered and handled right after SEAMRET, nothing +* needs to be done in KVM. +*/ +return 1 ;default :break ; }
Signature · 1 line
2.25.1
RFC PATCH v5 93/104 KVM: TDX: Handle TDX PV port io hypercall
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Wire up TDX PV port IO hypercall to the KVM backend function.
--- arch/x86/kvm/vmx/tdx.c | 55 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+)
arch/x86/kvm/vmx/tdx.c +55 -0
@@ -959,6 +959,59 @@ static int tdx_emulate_hlt(struct kvm_vcpu *vcpu)return kvm_emulate_halt_noskip (vcpu); } +static int tdx_complete_pio_in (struct kvm_vcpu *vcpu) +{ +struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; +unsigned long val =0 ; +int ret; + +WARN_ON (vcpu->arch.pio.count !=1 ); + + ret = ctxt->ops->pio_in_emulated (ctxt, vcpu->arch.pio.size, + vcpu->arch.pio.port, &val,1 ); +WARN_ON (!ret); + +tdvmcall_set_return_code (vcpu,TDG_VP_VMCALL_SUCCESS ); +tdvmcall_set_return_val (vcpu, val); + +return 1 ; +} + +static int tdx_emulate_io (struct kvm_vcpu *vcpu) +{ +struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; +unsigned long val =0 ; +unsigned int port; +int size, ret; + + ++vcpu->stat.io_exits; + + size =tdvmcall_p1_read (vcpu); + port =tdvmcall_p3_read (vcpu); + +if (size !=1 && size !=2 && size !=4 ) { +tdvmcall_set_return_code (vcpu,TDG_VP_VMCALL_INVALID_OPERAND ); +return 1 ; + } + +if (!tdvmcall_p2_read (vcpu)) { + ret = ctxt->ops->pio_in_emulated (ctxt, size, port, &val,1 ); +if (!ret) + vcpu->arch.complete_userspace_io = tdx_complete_pio_in; +else +tdvmcall_set_return_val (vcpu, val); + }else { + val =tdvmcall_p4_read (vcpu); + ret = ctxt->ops->pio_out_emulated (ctxt, size, port, &val,1 ); + +/* No need for a complete_userspace_io callback. */ + vcpu->arch.pio.count =0 ; + } +if (ret) +tdvmcall_set_return_code (vcpu,TDG_VP_VMCALL_SUCCESS ); +return ret; +} +static int handle_tdvmcall (struct kvm_vcpu *vcpu) {struct vcpu_tdx *tdx =to_tdx (vcpu);
@@ -974,6 +1027,8 @@ static int handle_tdvmcall(struct kvm_vcpu *vcpu)return tdx_emulate_cpuid (vcpu);case EXIT_REASON_HLT :return tdx_emulate_hlt (vcpu); +case EXIT_REASON_IO_INSTRUCTION : +return tdx_emulate_io (vcpu);default :break ; }
Signature · 1 line
2.25.1
RFC PATCH v5 91/104 KVM: TDX: Handle TDX PV CPUID hypercall
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Wire up TDX PV CPUID hypercall to the KVM backend function.
--- arch/x86/kvm/vmx/tdx.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)
arch/x86/kvm/vmx/tdx.c +27 -0
@@ -893,6 +893,30 @@ static int tdx_emulate_vmcall(struct kvm_vcpu *vcpu)return 1 ; } +static int tdx_emulate_cpuid (struct kvm_vcpu *vcpu) +{ +u32 eax, ebx, ecx, edx; + +/* EAX and ECX for cpuid is stored in R12 and R13. */ + eax =tdvmcall_p1_read (vcpu); + ecx =tdvmcall_p2_read (vcpu); + +kvm_cpuid (vcpu, &eax, &ebx, &ecx, &edx, true); + +/* +* The returned value for CPUID (EAX, EBX, ECX, and EDX) is stored into +* R12, R13, R14, and R15. +*/ +tdvmcall_p1_write (vcpu, eax); +tdvmcall_p2_write (vcpu, ebx); +tdvmcall_p3_write (vcpu, ecx); +tdvmcall_p4_write (vcpu, edx); + +tdvmcall_set_return_code (vcpu,TDG_VP_VMCALL_SUCCESS ); + +return 1 ; +} +static int handle_tdvmcall (struct kvm_vcpu *vcpu) {struct vcpu_tdx *tdx =to_tdx (vcpu);
@@ -904,6 +928,9 @@ static int handle_tdvmcall(struct kvm_vcpu *vcpu)return tdx_emulate_vmcall (vcpu);switch (tdvmcall_exit_reason (vcpu)) { +case EXIT_REASON_CPUID : +return tdx_emulate_cpuid (vcpu); +default :break ; }
Signature · 1 line
2.25.1
RFC PATCH v5 90/104 KVM: TDX: handle KVM hypercall with TDG.VP.VMCALL
In reply to [email protected]From: Isaku Yamahata <[email protected]>
The TDX Guest-Host communication interface (GHCI) specification defines
the ABI for the guest TD to issue hypercall. It reserves vendor specific
arguments for VMM specific use. Use it as KVM hypercall and handle it.
--- arch/x86/kvm/vmx/tdx.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)
arch/x86/kvm/vmx/tdx.c +31 -0
@@ -865,6 +865,34 @@ static int tdx_handle_triple_fault(struct kvm_vcpu *vcpu)return 0 ; } +static int tdx_emulate_vmcall (struct kvm_vcpu *vcpu) +{ +unsigned long nr, a0, a1, a2, a3, ret; + +/* +* ABI for KVM tdvmcall argument: +* In Guest-Hypervisor Communication Interface(GHCI) specification, +* Non-zero leaf number (R10 != 0) is defined to indicate +* vendor-specific. KVM uses this for KVM hypercall. NOTE: KVM +* hypercall number starts from one. Zero isn't used for KVM hypercall +* number. +* +* R10: KVM h ypercall number +* arguments: R11, R12, R13, R14. +*/ + nr =kvm_r10_read (vcpu); + a0 =kvm_r11_read (vcpu); + a1 =kvm_r12_read (vcpu); + a2 =kvm_r13_read (vcpu); + a3 =kvm_r14_read (vcpu); + + ret =__kvm_emulate_hypercall (vcpu, nr, a0, a1, a2, a3, true); + +tdvmcall_set_return_code (vcpu, ret); + +return 1 ; +} +static int handle_tdvmcall (struct kvm_vcpu *vcpu) {struct vcpu_tdx *tdx =to_tdx (vcpu);
@@ -872,6 +900,9 @@ static int handle_tdvmcall(struct kvm_vcpu *vcpu)if (unlikely (tdx->tdvmcall.xmm_mask)) goto unsupported; +if (tdvmcall_exit_type (vcpu)) +return tdx_emulate_vmcall (vcpu); +switch (tdvmcall_exit_reason (vcpu)) {default :break ;
Signature · 1 line
2.25.1
RFC PATCH v5 81/104 KVM: VMX: Modify NMI and INTR handlers to take intr_info as function argument
In reply to [email protected]From: Sean Christopherson <[email protected]>
TDX uses different ABI to get information about VM exit. Pass intr_info to the NMI and INTR handlers instead of pulling it from vcpu_vmx in preparation for sharing the bulk of the handlers with TDX.
When the guest TD exits to VMM, RAX holds status and exit reason, RCX holds exit qualification etc rather than the VMCS fields because VMM doesn't have access to the VMCS. The eventual code will be
VMX:
- get exit reason, intr_info, exit_qualification, and etc from VMCS
- call NMI/INTR handlers (common code)
TDX:
- get exit reason, intr_info, exit_qualification, and etc from guest registers
- call NMI/INTR handlers (common code)
--- arch/x86/kvm/vmx/vmx.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-)
arch/x86/kvm/vmx/vmx.c +8 -9
@@ -6442,28 +6442,27 @@ static void handle_nm_fault_irqoff(struct kvm_vcpu *vcpu)rdmsrl (MSR_IA32_XFD_ERR , vcpu->arch.guest_fpu.xfd_err); } -static void handle_exception_nmi_irqoff (struct vcpu_vmx *vmx) +static void handle_exception_nmi_irqoff (struct kvm_vcpu *vcpu,u32 intr_info) {const unsigned long nmi_entry = (unsigned long )asm_exc_nmi_noist; -u32 intr_info =vmx_get_intr_info (&vmx->vcpu);/* if exit due to PF check for async PF */ if (is_page_fault (intr_info)) - vmx->vcpu.arch.apf.host_apf_flags =kvm_read_and_reset_apf_flags (); + vcpu->arch.apf.host_apf_flags =kvm_read_and_reset_apf_flags ();/* if exit due to NM, handle before interrupts are enabled */ else if (is_nm_fault (intr_info)) -handle_nm_fault_irqoff (&vmx->vcpu); +handle_nm_fault_irqoff (vcpu);/* Handle machine checks before interrupts are enabled */ else if (is_machine_check (intr_info))kvm_machine_check ();/* We need to handle NMIs before interrupts are enabled */ else if (is_nmi (intr_info)) -handle_interrupt_nmi_irqoff (&vmx->vcpu, nmi_entry); +handle_interrupt_nmi_irqoff (vcpu, nmi_entry); } -static void handle_external_interrupt_irqoff (struct kvm_vcpu *vcpu) +static void handle_external_interrupt_irqoff (struct kvm_vcpu *vcpu, +u32 intr_info) { -u32 intr_info =vmx_get_intr_info (vcpu);unsigned int vector = intr_info &INTR_INFO_VECTOR_MASK ;gate_desc *desc = (gate_desc *)host_idt_base + vector;
@@ -6482,9 +6481,9 @@ void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu)return ;if (vmx->exit_reason.basic ==EXIT_REASON_EXTERNAL_INTERRUPT ) -handle_external_interrupt_irqoff (vcpu); +handle_external_interrupt_irqoff (vcpu,vmx_get_intr_info (vcpu));else if (vmx->exit_reason.basic ==EXIT_REASON_EXCEPTION_NMI ) -handle_exception_nmi_irqoff (vmx); +handle_exception_nmi_irqoff (vcpu,vmx_get_intr_info (vcpu)); }/*
Signature · 1 line
2.25.1
RFC PATCH v5 104/104 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/tdx-tdp-mmu.rst | 466 +++++++++++++++++++++++++ 1 file changed, 466 insertions(+) create mode 100644 Documentation/virt/kvm/tdx-tdp-mmu.rst
Documentation/virt/kvm/tdx-tdp-mmu.rst new file +466 -0
@@ -0,0 +1,466 @@ +.. SPDX-License-Identifier: GPL-2.0 + +Design of TDP MMU for TDX support +================================= +This document describes a (high level) design for TDX support of KVM TDP MMU of +x86 KVM. + +In this document, we use "TD" or "guest TD" to differentiate it from the current +"VM" (Virtual Machine), which is supported by KVM today. + + +Background of TDX +================= +TD private memory is designed to hold TD private content, encrypted by the CPU +using the TD ephemeral key. An encryption engine holds a table of encryption +keys, and an encryption key is selected for each memory transaction based on a +Host Key Identifier (HKID). By design, the host VMM does not have access to the +encryption keys. + +In the first generation of MKTME, HKID is "stolen" from the physical address by +allocating a configurable number of bits from the top of the physical address. +The HKID space is partitioned into shared HKIDs for legacy MKTME accesses and +private HKIDs for SEAM-mode-only accesses. We use 0 for the shared HKID on the +host so that MKTME can be opaque or bypassed on the host. + +During TDX non-root operation (i.e. guest TD), memory accesses can be qualified +as either shared or private, based on the value of a new SHARED bit in the Guest +Physical Address (GPA). The CPU translates shared GPAs using the usual VMX EPT +(Extended Page Table) or "Shared EPT" (in this document), which resides in the +host VMM memory. The Shared EPT is directly managed by the host VMM - the same +as with the current VMX. Since guest TDs usually require I/O, and the data +exchange needs to be done via shared memory, thus KVM needs to use the current +EPT functionality even for TDs. + +The CPU translates private GPAs using a separate Secure EPT. The Secure EPT +pages are encrypted and integrity-protected with the TD's ephemeral private key. +Secure EPT can be managed _indirectly_ by the host VMM, using the TDX interface +functions (SEAMCALLs), and thus conceptually Secure EPT is a subset of EPT +because not all functionalities are available. + +Since the execution of such interface functions takes much longer time than +accessing memory directly, in KVM we use the existing TDP code to mirror the +Secure EPT for the TD. And we think there are at least two options today in +terms of the timing for executing such SEAMCALLs: + +1. synchronous, i.e. while walking the TDP page tables, or +2. post-walk, i.e. record what needs to be done to the real Secure EPT during + the walk, and execute SEAMCALLs later. + +The option 1 seems to be more intuitive and simpler, but the Secure EPT +concurrency rules are different from the ones of the TDP or EPT. For example, +MEM.SEPT.RD acquire shared access to the whole Secure EPT tree of the target + +Secure EPT(SEPT) operations +--------------------------- +Secure EPT is an Extended Page Table for GPA-to-HPA translation of TD private +HPA. A Secure EPT is designed to be encrypted with the TD's ephemeral private +key. SEPT pages are allocated by the host VMM via Intel TDX functions, but their +content is intended to be hidden and is not architectural. + +Unlike the conventional EPT, the CPU can't directly read/write its entry. +Instead, TDX SEAMCALL API is used. Several SEAMCALLs correspond to operation on +the EPT entry. + +* TDH.MEM.SEPT.ADD(): + Add a secure EPT page from the secure EPT tree. This corresponds to updating + the non-leaf EPT entry with present bit set + +* TDH.MEM.SEPT.REMOVE(): + Remove the secure page from the secure EPT tree. There is no corresponding + to the EPT operation. + +* TDH.MEM.SEPT.RD(): + Read the secure EPT entry. This corresponds to reading the EPT entry as + memory. Please note that this is much slower than direct memory reading. + +* TDH.MEM.PAGE.ADD() and TDH.MEM.PAGE.AUG(): + Add a private page to the secure EPT tree. This corresponds to updating the + leaf EPT entry with present bit set. + +* THD.MEM.PAGE.REMOVE(): + Remove a private page from the secure EPT tree. There is no corresponding + to the EPT operation. + +* TDH.MEM.RANGE.BLOCK(): + This (mostly) corresponds to clearing the present bit of the leaf EPT entry. + Note that the private page is still linked in the secure EPT. To remove it + from the secure EPT, TDH.MEM.SEPT.REMOVE() and TDH.MEM.PAGE.REMOVE() needs to + be called. + +* TDH.MEM.TRACK(): + Increment the TLB epoch counter. This (mostly) corresponds to EPT TLB flush. + Note that the private page is still linked in the secure EPT. To remove it + from the secure EPT, tdh_mem_page_remove() needs to be called. + + +Adding private page +------------------- +The procedure of populating the private page looks as follows. + +1. TDH.MEM.SEPT.ADD(512G level) +2. TDH.MEM.SEPT.ADD(1G level) +3. TDH.MEM.SEPT.ADD(2M level) +4. TDH.MEM.PAGE.AUG(4K level) + +Those operations correspond to updating the EPT entries. + +Dropping private page and TLB shootdown +--------------------------------------- +The procedure of dropping the private page looks as follows. + +1. TDH.MEM.RANGE.BLOCK(4K level) + This mostly corresponds to clear the present bit in the EPT entry. This + prevents (or blocks) TLB entry from creating in the future. Note that the + private page is still linked in the secure EPT tree and the existing cache + entry in the TLB isn't flushed. +2. TDH.MEM.TRACK(range) and TLB shootdown + This mostly corresponds to the EPT TLB shootdown. Because all vcpus share + the same Secure EPT, all vcpus need to flush TLB. + * TDH.MEM.TRACK(range) by one vcpu. It increments the global internal TLB + epoch counter. + * send IPI to remote vcpus + * Other vcpu exits to VMM from guest TD and then re-enter. TDH.VP.ENTER(). + * TDH.VP.ENTER() checks the TLB epoch counter and If its TLB is old, flush + TLB. + Note that only single vcpu issues tdh_mem_track(). + Note that the private page is still linked in the secure EPT tree, unlike the + conventional EPT. +3. TDH.MEM.PAGE.PROMOTE, TDH.MEM.PAGEDEMOTE(), TDH.MEM.PAGE.RELOCATE(), or + TDH.MEM.PAGE.REMOVE() + There is no corresponding operation to the conventional EPT. + * When changing page size (e.g. 4K <-> 2M) TDH.MEM.PAGE.PROMOTE() or + TDH.MEM.PAGE.DEMOTE() is used. During those operation, the guest page is + kept referenced in the Secure EPT. + * When migrating page, TDH.MEM.PAGE.RELOCATE(). This requires both source + page and destination page. + * when destroying TD, TDH.MEM.PAGE.REMOVE() removes the private page from the + secure EPT tree. In this case TLB shootdown is not needed because vcpus + don't run any more. + +The basic idea for TDX support +============================== +Because shared EPT is the same as the existing EPT, use the existing logic for +shared EPT. On the other hand, secure EPT requires additional operations +instead of directly reading/writing of the EPT entry. + +On EPT violation, The KVM mmu walks down the EPT tree from the root, determines +the EPT entry to operate, and updates the entry. If necessary, a TLB shootdown +is done. Because it's very slow to directly walk secure EPT by TDX SEAMCALL, +TDH.MEM.SEPT.RD(), the mirror of secure EPT is created and maintained. Add +hooks to KVM MMU to reuse the existing code. + +EPT violation on shared GPA +--------------------------- +(1) EPT violation on shared GPA or zapping shared GPA + walk down shared EPT tree (the existing code) + | + | + V +shared EPT tree (CPU refers.) +(2) update the EPT entry. (the existing code) + TLB shootdown in the case of zapping. + + +EPT violation on private GPA +---------------------------- +(1) EPT violation on private GPA or zapping private GPA + walk down the mirror of secure EPT tree (mostly same as the existing code) + | + | + V +mirror of secure EPT tree (KVM MMU software only. reuse of the existing code) +(2) update the (mirrored) EPT entry. (mostly same as the existing code) +(3) call the hooks with what EPT entry is changed + | + NEW: hooks in KVM MMU + | + V +secure EPT root(CPU refers) +(4) the TDX backend calls necessary TDX SEAMCALLs to update real secure EPT. + +The major modification is to add hooks for the TDX backend for additional +operations and to pass down which EPT, shared EPT, or private EPT is used, and +twist the behavior if we're operating on private EPT. + +The following depicts the relationship. +:: + + KVM | TDX module + | | | + -------------+---------- | | + | | | | + V V | | + shared GPA private GPA | | + CPU shared EPT pointer KVM private EPT pointer | CPU secure EPT pointer + | | | | + | | | | + V V | V + shared EPT private EPT<-------mirror----->Secure EPT + | | | | + | \--------------------+------\ | + | | | | + V | V V + shared guest page | private guest page + | + | + non-encrypted memory | encrypted memory + | + +shared EPT: CPU and KVM walk with shared GPA + Maintained by the existing code +private EPT: KVM walks with private GPA + Maintained by the twisted existing code +secure EPT: CPU walks with private GPA. + Maintained by TDX module with TDX SEAMCALLs via hooks + + +Tracking private EPT page +========================= +Shared EPT pages are managed by struct kvm_mmu_page. They are linked in a list +structure. When necessary, the list is traversed to operate on. Private EPT +pages have different characteristics. For example, private pages can't be +swapped out. When shrinking memory, we'd like to traverse only shared EPT pages +and skip private EPT pages. Likewise, page migration isn't supported for +private pages (yet). Introduce an additional list to track shared EPT pages and +track private EPT pages independently. + +At the beginning of EPT violation, the fault handler knows fault GPA, thus it +knows which EPT to operate on, private or shared. If it's private EPT, +an additional task is done. Something like "if (private) { callback a hook }". +Since the fault handler has deep function calls, it's cumbersome to hold the +information of which EPT is operating. Options to mitigate it are + +1. Pass the information as an argument for the function call. +2. Record the information in struct kvm_mmu_page somehow. +3. Record the information in vcpu structure. + +Option 2 was chosen. Because option 1 requires modifying all the functions. It +would affect badly to the normal case. Option 3 doesn't work well because in +some cases, we need to walk both private and shared EPT. + +The role of the EPT page can be utilized and one bit can be curved out from +unused bits in struct kvm_mmu_page_role. When allocating the EPT page, +initialize the information. Mostly struct kvm_mmu_page is available because +we're operating on EPT pages. + + +The conversion of private GPA and shared GPA +============================================ +A page of a given GPA can be assigned to only private GPA xor shared GPA at one +time. The GPA can't be accessed simultaneously via both private GPA and shared +GPA. On guest startup, all the GPAs are assigned as private. Guest converts +the range of GPA to shared (or private) from private (or shared) by MapGPA +hypercall. MapGPA hypercall takes the start GPA and the size of the region. If +the given start GPA is shared, VMM converts the region into shared (if it's +already shared, nop). If the start GPA is private, VMM converts the region into +private. It implies the guest won't access the unmapped region. private(or +shared) region after converting to shared(or private). + +If the guest TD triggers an EPT violation on the already converted region, the +access won't be allowed (loop in EPT violation) until other vcpu converts back +the region. + +KVM MMU records which GPA is allowed to access, private or shared. It steals +software usable bit from MMU present mask. SEPT_PRIVATE_PROHIBIT. The bit is +recorded in both shared EPT and the mirror of secure EPT. + +* If SEPT_PRIVATE_PROHIBIT cleared in the shared EPT and the mirror of secure EPT: + Private GPA is allowed. Shared GPA is not allowed. + +* SEPT_PRIVATE_PROHIBIT set in the shared EPT and the mirror of secure EPT: + Private GPA is not allowed. Shared GPA is allowed. + +The default is that SEPT_PRIVATE_PROHIBIT is cleared so that the existing KVM +MMU code (mostly) works. + +The reason why the bit is recorded in both shared and private EPT is to optimize +for EPT violation path by penalizing MapGPA hypercall. + +The state machine of EPT entry +------------------------------ +(private EPT entry, shared EPT entry) = + (non-present, non-present): private mapping is allowed + (present, non-present): private mapping is mapped + (non-present | PRIVATE_PROHIBIT, non-present | PRIVATE_PROHIBIT): + shared mapping is allowed + (non-present | PRIVATE_PROHIBIT, present | PRIVATE_PROHIBIT): + shared mapping is mapped + (present | PRIVATE_PROHIBIT, any) invalid combination + +* map_gpa(private GPA): Mark the region that private GPA is allowed(NEW) + private EPT entry: clear PRIVATE_PROHIBIT + present: nop + non-present: nop + non-present | PRIVATE_PROHIBIT -> non-present (clear PRIVATE_PROHIBIT) + + shared EPT entry: zap the entry, clear PRIVATE_PROHIBIT + present: invalid + non-present -> non-present: nop + present | PRIVATE_PROHIBIT -> non-present + non-present | PRIVATE_PROHIBIT -> non-present + +* map_gpa(shared GPA): Mark the region that shared GPA is allowed(NEW) + private EPT entry: zap and set PRIVATE_PROHIBIT + present -> non-present | PRIVATE_PROHIBIT + non-present -> non-present | PRIVATE_PROHIBIT + non-present | PRIVATE_PROHIBIT: nop + + shared EPT entry: set PRIVATE_PROHIBIT + present: invalid + non-present -> non-present | PRIVATE_PROHIBIT + present | PRIVATE_PROHIBIT -> present | PRIVATE_PROHIBIT: nop + non-present | PRIVATE_PROHIBIT -> non-present | PRIVATE_PROHIBIT: nop + +* map(private GPA) + private EPT entry + present: nop + non-present -> present + non-present | PRIVATE_PROHIBIT: nop. looping on EPT violation(NEW) + + shared EPT entry: nop + +* map(shared GPA) + private EPT entry: nop + + shared EPT entry + present: invalid + present | PRIVATE_PROHIBIT: nop + non-present | PRIVATE_PROHIBIT -> present | PRIVATE_PROHIBIT + non-present: nop. looping on EPT violation(NEW) + +* zap(private GPA) + private EPT entry: zap the entry with keeping PRIVATE_PROHIBIT + present -> non-present + present | PRIVATE_PROHIBIT: invalid + non-present: nop as is_shadow_present_pte() is checked + non-present | PRIVATE_PROHIBIT: nop as is_shadow_present_pte() is + checked + + shared EPT entry: nop + +* zap(shared GPA) + private EPT entry: nop + + shared EPT entry: zap + any -> non-present + present: invalid + present | PRIVATE_PROHIBIT -> non-present | PRIVATE_PROHIBIT + non-present: nop as is_shadow_present_pte() is checked + non-present | PRIVATE_PROHIBIT: nop as is_shadow_present_pte() is + checked + + +The original TDP MMU and race condition +======================================= +Because vcpus share the EPT, once the EPT entry is zapped, we need to shootdown +TLB. Send IPI to remote vcpus. Remote vcpus flush their down TLBs. Until TLB +shootdown is done, vcpus may reference the zapped guest page. + +TDP MMU uses read lock of mmu_lock to mitigate vcpu contention. When read lock +is obtained, it depends on the atomic update of the EPT entry. (On the other +hand legacy MMU uses write lock.) When vcpu is populating/zapping the EPT entry +with a read lock held, other vcpu may be populating or zapping the same EPT +entry at the same time. + +To avoid the race condition, the entry is frozen. It means the EPT entry is set +to the special value, REMOVED_SPTE which clears the present bit. And then after +TLB shootdown, update the EPT entry to the final value. + +Concurrent zapping +------------------ +1. read lock +2. freeze the EPT entry (atomically set the value to REMOVED_SPTE) + If other vcpu froze the entry, restart page fault. +3. TLB shootdown + * send IPI to remote vcpus + * TLB flush (local and remote) + For each entry update, TLB shootdown is needed because of the + concurrency. +4. atomically set the EPT entry to the final value +5. read unlock + +Concurrent populating +--------------------- +In the case of populating the non-present EPT entry, atomically update the EPT +entry. +1. read lock +2. atomically update the EPT entry + If other vcpu frozen the entry or updated the entry, restart page fault. +3. read unlock + +In the case of updating the present EPT entry (e.g. page migration), the +operation is split into two. Zapping the entry and populating the entry. +1. read lock +2. zap the EPT entry. follow the concurrent zapping case. +3. populate the non-present EPT entry. +4. read unlock + +Non-concurrent batched zapping +------------------------------ +In some cases, zapping the ranges is done exclusively with a write lock held. +In this case, the TLB shootdown is batched into one. + +1. write lock +2. zap the EPT entries by traversing them +3. TLB shootdown +4. write unlock + + +For Secure EPT, TDX SEAMCALLs are needed in addition to updating the mirrored +EPT entry. + +TDX concurrent zapping +---------------------- +Add a hook for TDX SEAMCALLs at the step of the TLB shootdown. + +1. read lock +2. freeze the EPT entry(set the value to REMOVED_SPTE) +3. TLB shootdown via a hook + * TLB.MEM.RANGE.BLOCK() + * TLB.MEM.TRACK() + * send IPI to remote vcpus +4. set the EPT entry to the final value +5. read unlock + +TDX concurrent populating +------------------------- +TDX SEAMCALLs are required in addition to operating the mirrored EPT entry. The +frozen entry is utilized by following the zapping case to avoid the race +condition. A hook can be added. + +1. read lock +2. freeze the EPT entry +3. hook + * TDH_MEM_SEPT_ADD() for non-leaf or TDH_MEM_PAGE_AUG() for leaf. +4. set the EPT entry to the final value +5. read unlock + +Without freezing the entry, the following race can happen. Suppose two vcpus +are faulting on the same GPA and the 2M and 4K level entries aren't populated +yet. + +* vcpu 1: update 2M level EPT entry +* vcpu 2: update 4K level EPT entry +* vcpu 2: TDX SEAMCALL to update 4K secure EPT entry => error +* vcpu 1: TDX SEAMCALL to update 2M secure EPT entry + + +TDX non-concurrent batched zapping +---------------------------------- +For simplicity, the procedure of concurrent populating is utilized. The +procedure can be optimized later. + + +Co-existing with unmapping guest private memory +=============================================== +TODO. This needs to be addressed. + + +Restrictions or future work +=========================== +The following features aren't supported yet at the moment. + +* optimizing non-concurrent zap +* Large page +* Page migration
Signature · 1 line
2.25.1
RFC PATCH v5 102/104 KVM: TDX: Add methods to ignore accesses to CPU state
In reply to [email protected]From: Sean Christopherson <[email protected]>
TDX protects TDX guest state from VMM. Implements to access methods for TDX guest state to ignore them or return zero.
--- arch/x86/kvm/vmx/main.c | 465 +++++++++++++++++++++++++++++++++---- arch/x86/kvm/vmx/tdx.c | 44 ++++ arch/x86/kvm/vmx/x86_ops.h | 17 ++ 3 files changed, 483 insertions(+), 43 deletions(-)
arch/x86/kvm/vmx/main.c +422 -43
@@ -228,6 +228,46 @@ static void vt_enable_smi_window(struct kvm_vcpu *vcpu)vmx_enable_smi_window (vcpu); } +static bool vt_can_emulate_instruction (struct kvm_vcpu *vcpu,int emul_type, +void *insn,int insn_len) +{ +if (is_td_vcpu (vcpu)) +return false; + +return vmx_can_emulate_instruction (vcpu, emul_type, insn, insn_len); +} + +static int vt_check_intercept (struct kvm_vcpu *vcpu, +struct x86_instruction_info *info, +enum x86_intercept_stage stage, +struct x86_exception *exception) +{ +/* +* This call back is triggered by the x86 instruction emulator. TDX +* doesn't allow guest memory inspection. +*/ +if (KVM_BUG_ON (is_td_vcpu (vcpu), vcpu->kvm)) +return X86EMUL_UNHANDLEABLE ; + +return vmx_check_intercept (vcpu, info, stage, exception); +} + +static bool vt_apic_init_signal_blocked (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return true; + +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_post_state_restore (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))
@@ -236,6 +276,31 @@ static void vt_apicv_post_state_restore(struct kvm_vcpu *vcpu)return vmx_apicv_post_state_restore (vcpu); } +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 (struct kvm_vcpu *vcpu,int max_isr) +{ +if (is_td_vcpu (vcpu)) +return ; + +return vmx_hwapic_isr_update (vcpu, 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))
@@ -272,6 +337,179 @@ static void vt_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)kvm_vcpu_deliver_sipi_vector (vcpu, vector); } +static void vt_vcpu_after_set_cpuid (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return ; + +return vmx_vcpu_after_set_cpuid (vcpu); +} + +static void vt_update_exception_bitmap (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return ; + +vmx_update_exception_bitmap (vcpu); +} + +static u64 vt_get_segment_base (struct kvm_vcpu *vcpu,int seg) +{ +if (is_td_vcpu (vcpu)) +return tdx_get_segment_base (vcpu, seg); + +return vmx_get_segment_base (vcpu, seg); +} + +static void vt_get_segment (struct kvm_vcpu *vcpu,struct kvm_segment *var, +int seg) +{ +if (is_td_vcpu (vcpu)) +return tdx_get_segment (vcpu, var, seg); + +vmx_get_segment (vcpu, var, seg); +} + +static void vt_set_segment (struct kvm_vcpu *vcpu,struct kvm_segment *var, +int seg) +{ +if (is_td_vcpu (vcpu)) +return ; + +vmx_set_segment (vcpu, var, seg); +} + +static int vt_get_cpl (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_get_cpl (vcpu); + +return vmx_get_cpl (vcpu); +} + +static void vt_get_cs_db_l_bits (struct kvm_vcpu *vcpu,int *db,int *l) +{ +if (is_td_vcpu (vcpu)) +return ; + +vmx_get_cs_db_l_bits (vcpu, db, l); +} + +static void vt_set_cr0 (struct kvm_vcpu *vcpu,unsigned long cr0) +{ +if (is_td_vcpu (vcpu)) +return ; + +vmx_set_cr0 (vcpu, cr0); +} + +static void vt_set_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4) +{ +if (is_td_vcpu (vcpu)) +return ; + +vmx_set_cr4 (vcpu, cr4); +} + +static int vt_set_efer (struct kvm_vcpu *vcpu,u64 efer) +{ +if (is_td_vcpu (vcpu)) +return 0 ; + +return vmx_set_efer (vcpu, efer); +} + +static void vt_get_idt (struct kvm_vcpu *vcpu,struct desc_ptr *dt) +{ +if (is_td_vcpu (vcpu)) { +memset (dt,0 ,sizeof (*dt)); +return ; + } + +vmx_get_idt (vcpu, dt); +} + +static void vt_set_idt (struct kvm_vcpu *vcpu,struct desc_ptr *dt) +{ +if (is_td_vcpu (vcpu)) +return ; + +vmx_set_idt (vcpu, dt); +} + +static void vt_get_gdt (struct kvm_vcpu *vcpu,struct desc_ptr *dt) +{ +if (is_td_vcpu (vcpu)) { +memset (dt,0 ,sizeof (*dt)); +return ; + } + +vmx_get_gdt (vcpu, dt); +} + +static void vt_set_gdt (struct kvm_vcpu *vcpu,struct desc_ptr *dt) +{ +if (is_td_vcpu (vcpu)) +return ; + +vmx_set_gdt (vcpu, dt); +} + +static void vt_set_dr7 (struct kvm_vcpu *vcpu,unsigned long val) +{ +if (is_td_vcpu (vcpu)) +return ; + +vmx_set_dr7 (vcpu, val); +} + +static void vt_sync_dirty_debug_regs (struct kvm_vcpu *vcpu) +{ +/* +* MOV-DR exiting is always cleared for TD guest, even in debug mode. +* Thus KVM_DEBUGREG_WONT_EXIT can never be set and it should never +* reach here for TD vcpu. +*/ +if (KVM_BUG_ON (is_td_vcpu (vcpu), vcpu->kvm)) +return ; + +vmx_sync_dirty_debug_regs (vcpu); +} + +static void vt_cache_reg (struct kvm_vcpu *vcpu,enum kvm_reg reg) +{ +if (is_td_vcpu (vcpu)) { +tdx_cache_reg (vcpu, reg); +return ; + } + +vmx_cache_reg (vcpu, reg); +} + +static unsigned long vt_get_rflags (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_get_rflags (vcpu); + +return vmx_get_rflags (vcpu); +} + +static void vt_set_rflags (struct kvm_vcpu *vcpu,unsigned long rflags) +{ +if (is_td_vcpu (vcpu)) +return ; + +vmx_set_rflags (vcpu, rflags); +} + +static bool vt_get_if_flag (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return false; + +return vmx_get_if_flag (vcpu); +} +static void vt_flush_tlb_all (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))
@@ -388,6 +626,15 @@ 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) +{ +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) {if (is_td_vcpu (vcpu))
@@ -396,6 +643,14 @@ static void vt_inject_irq(struct kvm_vcpu *vcpu)vmx_inject_irq (vcpu); } +static void vt_queue_exception (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return ; + +vmx_queue_exception (vcpu); +} +static void vt_cancel_injection (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))
@@ -428,6 +683,130 @@ static void vt_request_immediate_exit(struct kvm_vcpu *vcpu)vmx_request_immediate_exit (vcpu); } +static void vt_update_cr8_intercept (struct kvm_vcpu *vcpu,int tpr,int irr) +{ +if (is_td_vcpu (vcpu)) +return ; + +vmx_update_cr8_intercept (vcpu, tpr, irr); +} + +static void vt_set_apic_access_page_addr (struct kvm_vcpu *vcpu) +{ +if (WARN_ON_ONCE (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)) +return ; + +vmx_load_eoi_exitmap (vcpu, eoi_exit_bitmap); +} + +static int vt_set_tss_addr (struct kvm *kvm,unsigned int addr) +{ +if (is_td (kvm)) +return 0 ; + +return vmx_set_tss_addr (kvm, addr); +} + +static int vt_set_identity_map_addr (struct kvm *kvm,u64 ident_addr) +{ +if (is_td (kvm)) +return 0 ; + +return vmx_set_identity_map_addr (kvm, ident_addr); +} + +static u64 vt_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio) +{ +if (is_td_vcpu (vcpu)) { +if (is_mmio) +return MTRR_TYPE_UNCACHABLE <<VMX_EPT_MT_EPTE_SHIFT ; +return MTRR_TYPE_WRBACK <<VMX_EPT_MT_EPTE_SHIFT ; + } + +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,u64 offset) +{ +/* In TDX, tsc offset can't be changed. */ +if (is_td_vcpu (vcpu)) +return ; + +vmx_write_tsc_offset (vcpu, offset); +} + +static void vt_write_tsc_multiplier (struct kvm_vcpu *vcpu,u64 multiplier) +{ +/* In TDX, tsc multiplier can't be changed. */ +if (is_td_vcpu (vcpu)) +return ; + +vmx_write_tsc_multiplier (vcpu, multiplier); +} + +static void vt_update_cpu_dirty_logging (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return ; + +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. Set vt_set_hv_timer(). */ +if (KVM_BUG_ON (is_td_vcpu (vcpu), vcpu->kvm)) +return ; + +vmx_cancel_hv_timer (vcpu); +} +#endif +static void vt_get_exit_info (struct kvm_vcpu *vcpu,u32 *reason,u64 *info1,u64 *info2,u32 *intr_info,u32 *error_code) {
@@ -480,29 +859,29 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_load = vt_vcpu_load, .vcpu_put = vt_vcpu_put, - .update_exception_bitmap = vmx_update_exception_bitmap, + .update_exception_bitmap = vt_update_exception_bitmap, .get_msr_feature = vmx_get_msr_feature, .get_msr = vt_get_msr, .set_msr = vt_set_msr, - .get_segment_base = vmx_get_segment_base, - .get_segment = vmx_get_segment, - .set_segment = vmx_set_segment, - .get_cpl = vmx_get_cpl, - .get_cs_db_l_bits = vmx_get_cs_db_l_bits, - .set_cr0 = vmx_set_cr0, + .get_segment_base = vt_get_segment_base, + .get_segment = vt_get_segment, + .set_segment = vt_set_segment, + .get_cpl = vt_get_cpl, + .get_cs_db_l_bits = vt_get_cs_db_l_bits, + .set_cr0 = vt_set_cr0, .is_valid_cr4 = vmx_is_valid_cr4, - .set_cr4 = vmx_set_cr4, - .set_efer = vmx_set_efer, - .get_idt = vmx_get_idt, - .set_idt = vmx_set_idt, - .get_gdt = vmx_get_gdt, - .set_gdt = vmx_set_gdt, - .set_dr7 = vmx_set_dr7, - .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs, - .cache_reg = vmx_cache_reg, - .get_rflags = vmx_get_rflags, - .set_rflags = vmx_set_rflags, - .get_if_flag = vmx_get_if_flag, + .set_cr4 = vt_set_cr4, + .set_efer = vt_set_efer, + .get_idt = vt_get_idt, + .set_idt = vt_set_idt, + .get_gdt = vt_get_gdt, + .set_gdt = vt_set_gdt, + .set_dr7 = vt_set_dr7, + .sync_dirty_debug_regs = vt_sync_dirty_debug_regs, + .cache_reg = vt_cache_reg, + .get_rflags = vt_get_rflags, + .set_rflags = vt_set_rflags, + .get_if_flag = vt_get_if_flag, .tlb_flush_all = vt_flush_tlb_all, .tlb_flush_current = vt_flush_tlb_current,
@@ -516,10 +895,10 @@ 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, .set_irq = vt_inject_irq, .set_nmi = vt_inject_nmi, - .queue_exception = vmx_queue_exception, + .queue_exception = vt_queue_exception, .cancel_injection = vt_cancel_injection, .interrupt_allowed = vt_interrupt_allowed, .nmi_allowed = vt_nmi_allowed,
@@ -527,39 +906,39 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .set_nmi_mask = vt_set_nmi_mask, .enable_nmi_window = vt_enable_nmi_window, .enable_irq_window = vt_enable_irq_window, - .update_cr8_intercept = vmx_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, - .load_eoi_exitmap = vmx_load_eoi_exitmap, + .update_cr8_intercept = vt_update_cr8_intercept, + .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_post_state_restore = vt_apicv_post_state_restore, .check_apicv_inhibit_reasons = vmx_check_apicv_inhibit_reasons, - .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, .apicv_has_pending_interrupt = vt_apicv_has_pending_interrupt, - .set_tss_addr = vmx_set_tss_addr, - .set_identity_map_addr = vmx_set_identity_map_addr, - .get_mt_mask = vmx_get_mt_mask, + .set_tss_addr = vt_set_tss_addr, + .set_identity_map_addr = vt_set_identity_map_addr, + .get_mt_mask = vt_get_mt_mask, .get_exit_info = vt_get_exit_info, - .vcpu_after_set_cpuid = vmx_vcpu_after_set_cpuid, + .vcpu_after_set_cpuid = vt_vcpu_after_set_cpuid, .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, - .check_intercept = vmx_check_intercept, + .check_intercept = vt_check_intercept, .handle_exit_irqoff = vt_handle_exit_irqoff, .request_immediate_exit = vt_request_immediate_exit,
@@ -567,7 +946,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .sched_in = vt_sched_in, .cpu_dirty_log_size = PML_ENTITY_NUM, - .update_cpu_dirty_logging = vmx_update_cpu_dirty_logging, + .update_cpu_dirty_logging = vt_update_cpu_dirty_logging, .pmu_ops = &intel_pmu_ops, .nested_ops = &vmx_nested_ops,
@@ -576,8 +955,8 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .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,
@@ -587,8 +966,8 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .leave_smm = vt_leave_smm, .enable_smi_window = vt_enable_smi_window, - .can_emulate_instruction = vmx_can_emulate_instruction, - .apic_init_signal_blocked = vmx_apic_init_signal_blocked, + .can_emulate_instruction = vt_can_emulate_instruction, + .apic_init_signal_blocked = vt_apic_init_signal_blocked, .migrate_timers = vmx_migrate_timers, .msr_filter_changed = vmx_msr_filter_changed,
arch/x86/kvm/vmx/tdx.c +44 -0
56 lines not on this page · raw message
arch/x86/kvm/vmx/x86_ops.h +17 -0
@@ -163,6 +163,14 @@ int tdx_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection);int tdx_enter_smm (struct kvm_vcpu *vcpu,char *smstate);int tdx_leave_smm (struct kvm_vcpu *vcpu,const char *smstate);void tdx_enable_smi_window (struct kvm_vcpu *vcpu); +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); +unsigned long tdx_get_rflags (struct kvm_vcpu *vcpu); +bool tdx_is_emulated_msr (u32 index,bool write); +u64 tdx_get_segment_base (struct kvm_vcpu *vcpu,int seg); +void tdx_get_segment (struct kvm_vcpu *vcpu,struct kvm_segment *var,int seg);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -203,10 +211,19 @@ static inline void tdx_get_exit_info(static inline bool tdx_is_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 int tdx_smi_allowed (struct kvm_vcpu *vcpu,bool for_injection) {return false; }static inline int tdx_enter_smm (struct kvm_vcpu *vcpu,char *smstate) {return 0 ; }static inline int tdx_leave_smm (struct kvm_vcpu *vcpu,const char *smstate) {return 0 ; }static inline void tdx_enable_smi_window (struct kvm_vcpu *vcpu) {} +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 ; } +static inline u64 tdx_get_segment_base (struct kvm_vcpu *vcpu,int seg) {return 0 ;} +static inline void tdx_get_segment ( +struct kvm_vcpu *vcpu,struct kvm_segment *var,int seg) {}static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
Signature · 1 line
2.25.1
RFC PATCH v5 101/104 KVM: TDX: Silently ignore INIT/SIPI
In reply to [email protected]From: Isaku Yamahata <[email protected]>
The TDX module API doesn't provide API for VMM to inject INIT IPI and SIPI. Instead it defines the different protocols to boot application processors. Ignore INIT and SIPI events for the TDX guest.
There are two options. 1) (silently) ignore INIT/SIPI request or 2) return error to guest TDs somehow. Given that TDX guest is paravirtualized to boot AP, the option 1 is chosen for simplicity.
--- arch/x86/kvm/lapic.c | 21 +++++++++++++++++---- arch/x86/kvm/vmx/main.c | 10 +++++++++- arch/x86/kvm/x86.h | 5 +++++ 3 files changed, 31 insertions(+), 5 deletions(-)
arch/x86/kvm/lapic.c +17 -4
@@ -2921,11 +2921,20 @@ int kvm_apic_accept_events(struct kvm_vcpu *vcpu)if (test_bit (KVM_APIC_INIT , &pe)) {clear_bit (KVM_APIC_INIT , &apic->pending_events); -kvm_vcpu_reset (vcpu, true); -if (kvm_vcpu_is_bsp (apic->vcpu)) +if (kvm_init_sipi_unsupported (vcpu->kvm)) +/* +* TDX doesn't support INIT. Ignore INIT event. In the +* case of SIPI, the callback of +* vcpu_deliver_sipi_vector ignores it. +*/ vcpu->arch.mp_state =KVM_MP_STATE_RUNNABLE ; -else - vcpu->arch.mp_state =KVM_MP_STATE_INIT_RECEIVED ; +else { +kvm_vcpu_reset (vcpu, true); +if (kvm_vcpu_is_bsp (apic->vcpu)) + vcpu->arch.mp_state =KVM_MP_STATE_RUNNABLE ; +else + vcpu->arch.mp_state =KVM_MP_STATE_INIT_RECEIVED ; + } }if (test_bit (KVM_APIC_SIPI , &pe)) {clear_bit (KVM_APIC_SIPI , &apic->pending_events);
@@ -2933,6 +2942,10 @@ int kvm_apic_accept_events(struct kvm_vcpu *vcpu)/* evaluate pending_events before reading the vector */ smp_rmb (); sipi_vector = apic->sipi_vector; +/* +* If SINIT isn't supported, the callback ignores SIPI +* request. +*/ kvm_x86_ops.vcpu_deliver_sipi_vector (vcpu, sipi_vector); vcpu->arch.mp_state =KVM_MP_STATE_RUNNABLE ; }
arch/x86/kvm/vmx/main.c +9 -1
@@ -264,6 +264,14 @@ static bool vt_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu)return false; } +static void vt_vcpu_deliver_sipi_vector (struct kvm_vcpu *vcpu,u8 vector) +{ +if (is_td_vcpu (vcpu)) +return ; + +kvm_vcpu_deliver_sipi_vector (vcpu, vector); +} +static void vt_flush_tlb_all (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))
@@ -586,7 +594,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .msr_filter_changed = vmx_msr_filter_changed, .complete_emulated_msr = kvm_complete_insn_gp, - .vcpu_deliver_sipi_vector = kvm_vcpu_deliver_sipi_vector, + .vcpu_deliver_sipi_vector = vt_vcpu_deliver_sipi_vector, .mem_enc_op = vt_mem_enc_op, .mem_enc_op_vcpu = vt_mem_enc_op_vcpu,
arch/x86/kvm/x86.h +5 -0
@@ -405,6 +405,11 @@ static inline void kvm_machine_check(void)#endif } +static __always_inline bool kvm_init_sipi_unsupported(struct kvm *kvm) +{ +return kvm->arch.vm_type ==KVM_X86_TDX_VM ; +} +void kvm_load_guest_xsave_state (struct kvm_vcpu *vcpu);void kvm_load_host_xsave_state (struct kvm_vcpu *vcpu);int kvm_spec_ctrl_test_value (u64 value);
Signature · 1 line
2.25.1
RFC PATCH v5 103/104 Documentation/virtual/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/intel-tdx.rst | 360 +++++++++++++++++++++++++++ 2 files changed, 368 insertions(+), 1 deletion(-) create mode 100644 Documentation/virt/kvm/intel-tdx.rst
Documentation/virt/kvm/api.rst +8 -1
@@ -1384,6 +1384,9 @@ It is recommended to use this API instead of the KVM_SET_MEMORY_REGION ioctl. The KVM_SET_MEMORY_REGION does not allow fine grained control over memory allocation and is deprecated. +For TDX guest, deleting/moving memory region loses guest memory contents. +Read only region isn't supported. Only as-id 0 is supported. + 4.36 KVM_SET_TSS_ADDR ---------------------
@@ -4539,7 +4542,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
@@ -4551,6 +4554,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/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/intel-tdx.rst. + 4.111 KVM_MEMORY_ENCRYPT_REG_REGION -----------------------------------
Documentation/virt/kvm/intel-tdx.rst new file +360 -0
360 lines not on this page · raw message
Signature · 1 line
2.25.1
RFC PATCH v5 100/104 KVM: TDX: Silently discard SMI request
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDX doesn't support system-management mode (SMM) and system-management interrupt (SMI) in guest TDs. Because guest state (vcpu state, memory state) is protected, it must go through the TDX module APIs to change guest state, injecting SMI and changing vcpu mode into SMM. The TDX module doesn't provide a way for VMM to inject SMI into guest TD and a way for VMM to switch guest vcpu mode into SMM.
We have two options in KVM when handling SMM or SMI in the guest TD or the device model (e.g. QEMU): 1) silently ignore the request or 2) return a meaningful error.
For simplicity, we implemented the option 1).
--- arch/x86/kvm/vmx/main.c | 43 ++++++++++++++++++++++++++++++++++---- arch/x86/kvm/vmx/tdx.c | 25 ++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 8 +++++++ 3 files changed, 72 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx/main.c +39 -4
@@ -193,6 +193,41 @@ static int vt_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)return vmx_get_msr (vcpu, msr_info); } +static int vt_smi_allowed (struct kvm_vcpu *vcpu,bool for_injection) +{ +if (is_td_vcpu (vcpu)) +return tdx_smi_allowed (vcpu, for_injection); + +return vmx_smi_allowed (vcpu, for_injection); +} + +static int vt_enter_smm (struct kvm_vcpu *vcpu,char *smstate) +{ +if (unlikely (is_td_vcpu (vcpu))) +return tdx_enter_smm (vcpu, smstate); + +return vmx_enter_smm (vcpu, smstate); +} + +static int vt_leave_smm (struct kvm_vcpu *vcpu,const char *smstate) +{ +if (unlikely (is_td_vcpu (vcpu))) +return tdx_leave_smm (vcpu, smstate); + +return vmx_leave_smm (vcpu, smstate); +} + +static void vt_enable_smi_window (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) { +tdx_enable_smi_window (vcpu); +return ; + } + +/* RSM will cause a vmexit anyway. */ +vmx_enable_smi_window (vcpu); +} +static void vt_apicv_post_state_restore (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))
@@ -539,10 +574,10 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .setup_mce = vmx_setup_mce, - .smi_allowed = vmx_smi_allowed, - .enter_smm = vmx_enter_smm, - .leave_smm = vmx_leave_smm, - .enable_smi_window = vmx_enable_smi_window, + .smi_allowed = vt_smi_allowed, + .enter_smm = vt_enter_smm, + .leave_smm = vt_leave_smm, + .enable_smi_window = vt_enable_smi_window, .can_emulate_instruction = vmx_can_emulate_instruction, .apic_init_signal_blocked = vmx_apic_init_signal_blocked,
arch/x86/kvm/vmx/tdx.c +25 -0
@@ -1692,6 +1692,31 @@ int tdx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)return 1 ; } +int tdx_smi_allowed (struct kvm_vcpu *vcpu,bool for_injection) +{ +/* SMI isn't supported for TDX. */ +return false; +} + +int tdx_enter_smm (struct kvm_vcpu *vcpu,char *smstate) +{ +/* smi_allowed() is always false for TDX as above. */ +WARN_ON_ONCE (1 ); +return 0 ; +} + +int tdx_leave_smm (struct kvm_vcpu *vcpu,const char *smstate) +{ +WARN_ON_ONCE (1 ); +return 0 ; +} + +void tdx_enable_smi_window (struct kvm_vcpu *vcpu) +{ +/* SMI isn't supported for TDX. Silently discard SMI request. */ + vcpu->arch.smi_pending = false; +} +static int tdx_capabilities (struct kvm *kvm,struct kvm_tdx_cmd *cmd) {struct kvm_tdx_capabilities __user *user_caps;
arch/x86/kvm/vmx/x86_ops.h +8 -0
@@ -159,6 +159,10 @@ void tdx_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason,bool tdx_is_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); +int tdx_smi_allowed (struct kvm_vcpu *vcpu,bool for_injection); +int tdx_enter_smm (struct kvm_vcpu *vcpu,char *smstate); +int tdx_leave_smm (struct kvm_vcpu *vcpu,const char *smstate); +void tdx_enable_smi_window (struct kvm_vcpu *vcpu);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -199,6 +203,10 @@ static inline void tdx_get_exit_info(static inline bool tdx_is_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 int tdx_smi_allowed (struct kvm_vcpu *vcpu,bool for_injection) {return false; } +static inline int tdx_enter_smm (struct kvm_vcpu *vcpu,char *smstate) {return 0 ; } +static inline int tdx_leave_smm (struct kvm_vcpu *vcpu,const char *smstate) {return 0 ; } +static inline void tdx_enable_smi_window (struct kvm_vcpu *vcpu) {}static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
Signature · 1 line
2.25.1
RFC PATCH v5 98/104 KVM: TDX: Handle TDX PV report fatal error hypercall
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Wire up TDX PV report fatal error hypercall to KVM_SYSTEM_EVENT_CRASH KVM exit event.
--- arch/x86/kvm/vmx/tdx.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
arch/x86/kvm/vmx/tdx.c +11 -0
@@ -1157,6 +1157,15 @@ static int tdx_emulate_wrmsr(struct kvm_vcpu *vcpu)return 1 ; } +static int tdx_report_fatal_error (struct kvm_vcpu *vcpu) +{ +/* Exit to userspace device model for teardown. */ + vcpu->run->exit_reason =KVM_EXIT_SYSTEM_EVENT ; + vcpu->run->system_event.type =KVM_SYSTEM_EVENT_CRASH ; + vcpu->run->system_event.flags =tdvmcall_p1_read (vcpu); +return 0 ; +} +static int handle_tdvmcall (struct kvm_vcpu *vcpu) {struct vcpu_tdx *tdx =to_tdx (vcpu);
@@ -1180,6 +1189,8 @@ static int handle_tdvmcall(struct kvm_vcpu *vcpu)return tdx_emulate_rdmsr (vcpu);case EXIT_REASON_MSR_WRITE :return tdx_emulate_wrmsr (vcpu); +case TDG_VP_VMCALL_REPORT_FATAL_ERROR : +return tdx_report_fatal_error (vcpu);default :break ; }
Signature · 1 line
2.25.1
RFC PATCH v5 96/104 KVM: TDX: Handle TDX PV rdmsr hypercall
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Wire up TDX PV rdmsr hypercall to the KVM backend function.
--- arch/x86/kvm/vmx/tdx.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
arch/x86/kvm/vmx/tdx.c +19 -0
@@ -1124,6 +1124,23 @@ static int tdx_emulate_mmio(struct kvm_vcpu *vcpu)return 1 ; } +static int tdx_emulate_rdmsr (struct kvm_vcpu *vcpu) +{ +u32 index =tdvmcall_p1_read (vcpu); +u64 data; + +if (kvm_get_msr (vcpu, index, &data)) { +trace_kvm_msr_read_ex (index); +tdvmcall_set_return_code (vcpu,TDG_VP_VMCALL_INVALID_OPERAND ); +return 1 ; + } +trace_kvm_msr_read (index, data); + +tdvmcall_set_return_code (vcpu,TDG_VP_VMCALL_SUCCESS ); +tdvmcall_set_return_val (vcpu, data); +return 1 ; +} +static int handle_tdvmcall (struct kvm_vcpu *vcpu) {struct vcpu_tdx *tdx =to_tdx (vcpu);
@@ -1143,6 +1160,8 @@ static int handle_tdvmcall(struct kvm_vcpu *vcpu)return tdx_emulate_io (vcpu);case EXIT_REASON_EPT_VIOLATION :return tdx_emulate_mmio (vcpu); +case EXIT_REASON_MSR_READ : +return tdx_emulate_rdmsr (vcpu);default :break ; }
Signature · 1 line
2.25.1
RFC PATCH v5 95/104 KVM: TDX: Implement callbacks for MSR operations for TDX
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Implements set_msr/get_msr/has_emulated_msr methods for TDX to handle hypercall from guest TD for paravirtualized rdmsr and wrmsr. The TDX module virtualizes MSRs. For some MSRs, it injects #VE to the guest TD upon RDMSR or WRMSR. The exact list of such MSRs are defined in the spec.
Upon #VE, the guest TD may execute hypercalls, TDG.VP.VMCALL<INSTRUCTION.RDMSR> and TDG.VP.VMCALL<INSTRUCTION.WRMSR>, which are defined in GHCI (Guest-Host Communication Interface) so that the host VMM (e.g. KVM) can virtualizes the MSRs.
--- arch/x86/kvm/vmx/main.c | 34 +++++++++++++++++-- arch/x86/kvm/vmx/tdx.c | 68 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 6 ++++ 3 files changed, 105 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/main.c +31 -3
@@ -165,6 +165,34 @@ static void vt_handle_exit_irqoff(struct kvm_vcpu *vcpu)vmx_handle_exit_irqoff (vcpu); } +static int vt_set_msr (struct kvm_vcpu *vcpu,struct msr_data *msr_info) +{ +if (unlikely (is_td_vcpu (vcpu))) +return tdx_set_msr (vcpu, msr_info); + +return vmx_set_msr (vcpu, msr_info); +} + +/* +* The kvm parameter can be NULL (module initialization, or invocation before +* VM creation). Be sure to check the kvm parameter before using it. +*/ +static bool vt_has_emulated_msr (struct kvm *kvm,u32 index) +{ +if (kvm &&is_td (kvm)) +return tdx_is_emulated_msr (index, true); + +return vmx_has_emulated_msr (kvm, index); +} + +static int vt_get_msr (struct kvm_vcpu *vcpu,struct msr_data *msr_info) +{ +if (unlikely (is_td_vcpu (vcpu))) +return tdx_get_msr (vcpu, msr_info); + +return vmx_get_msr (vcpu, msr_info); +} +static void vt_apicv_post_state_restore (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))
@@ -393,7 +421,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .hardware_enable = vt_hardware_enable, .hardware_disable = vt_hardware_disable, .cpu_has_accelerated_tpr = report_flexpriority, - .has_emulated_msr = vmx_has_emulated_msr, + .has_emulated_msr = vt_has_emulated_msr, .is_vm_type_supported = vt_is_vm_type_supported, .vm_size = sizeof(struct kvm_vmx),
@@ -411,8 +439,8 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .update_exception_bitmap = vmx_update_exception_bitmap, .get_msr_feature = vmx_get_msr_feature, - .get_msr = vmx_get_msr, - .set_msr = vmx_set_msr, + .get_msr = vt_get_msr, + .set_msr = vt_set_msr, .get_segment_base = vmx_get_segment_base, .get_segment = vmx_get_segment, .set_segment = vmx_set_segment,
arch/x86/kvm/vmx/tdx.c +68 -0
74 lines not on this page · raw message
arch/x86/kvm/vmx/x86_ops.h +6 -0
@@ -156,6 +156,9 @@ void tdx_deliver_interrupt(struct kvm_lapic *apic, int delivery_mode,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); +bool tdx_is_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);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -193,6 +196,9 @@ 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) {} +static inline bool tdx_is_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 int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
Signature · 1 line
2.25.1
RFC PATCH v5 80/104 KVM: TDX: Implement methods to inject NMI
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDX vcpu control structure defines one bit for pending NMI for VMM to inject NMI by setting the bit without knowing TDX vcpu NMI states. Because the vcpu state is protected, VMM can't know about NMI states of TDX vcpu. The TDX module handles actual injection and NMI states transition.
Add methods for NMI and treat NMI can be injected always.
--- arch/x86/kvm/vmx/main.c | 62 +++++++++++++++++++++++++++++++++++--- arch/x86/kvm/vmx/tdx.c | 5 +++ arch/x86/kvm/vmx/x86_ops.h | 2 ++ 3 files changed, 64 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx/main.c +57 -5
@@ -216,6 +216,58 @@ static void vt_flush_tlb_guest(struct kvm_vcpu *vcpu)vmx_flush_tlb_guest (vcpu); } +static void vt_inject_nmi (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_inject_nmi (vcpu); + +vmx_inject_nmi (vcpu); +} + +static int vt_nmi_allowed (struct kvm_vcpu *vcpu,bool for_injection) +{ +/* +* The TDX module manages NMI windows and NMI reinjection, and hides NMI +* blocking, all KVM can do is throw an NMI over the wall. +*/ +if (is_td_vcpu (vcpu)) +return true; + +return vmx_nmi_allowed (vcpu, for_injection); +} + +static bool vt_get_nmi_mask (struct kvm_vcpu *vcpu) +{ +/* +* Assume NMIs are always unmasked. KVM could query PEND_NMI and treat +* NMIs as masked if a previous NMI is still pending, but SEAMCALLs are +* expensive and the end result is unchanged as the only relevant usage +* of get_nmi_mask() is to limit the number of pending NMIs, i.e. it +* only changes whether KVM or the TDX module drops an NMI. +*/ +if (is_td_vcpu (vcpu)) +return false; + +return vmx_get_nmi_mask (vcpu); +} + +static void vt_set_nmi_mask (struct kvm_vcpu *vcpu,bool masked) +{ +if (is_td_vcpu (vcpu)) +return ; + +vmx_set_nmi_mask (vcpu, masked); +} + +static void vt_enable_nmi_window (struct kvm_vcpu *vcpu) +{ +/* Refer the comment in vt_get_nmi_mask(). */ +if (is_td_vcpu (vcpu)) +return ; + +vmx_enable_nmi_window (vcpu); +} +static void vt_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int pgd_level) {
@@ -366,14 +418,14 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .get_interrupt_shadow = vt_get_interrupt_shadow, .patch_hypercall = vmx_patch_hypercall, .set_irq = vt_inject_irq, - .set_nmi = vmx_inject_nmi, + .set_nmi = vt_inject_nmi, .queue_exception = vmx_queue_exception, .cancel_injection = vt_cancel_injection, .interrupt_allowed = vt_interrupt_allowed, - .nmi_allowed = vmx_nmi_allowed, - .get_nmi_mask = vmx_get_nmi_mask, - .set_nmi_mask = vmx_set_nmi_mask, - .enable_nmi_window = vmx_enable_nmi_window, + .nmi_allowed = vt_nmi_allowed, + .get_nmi_mask = vt_get_nmi_mask, + .set_nmi_mask = vt_set_nmi_mask, + .enable_nmi_window = vt_enable_nmi_window, .enable_irq_window = vt_enable_irq_window, .update_cr8_intercept = vmx_update_cr8_intercept, .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
arch/x86/kvm/vmx/tdx.c +5 -0
@@ -763,6 +763,11 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)return EXIT_FASTPATH_NONE ; } +void tdx_inject_nmi (struct kvm_vcpu *vcpu) +{ +td_management_write8 (to_tdx (vcpu),TD_VCPU_PEND_NMI ,1 ); +} +void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int pgd_level) {td_vmcs_write64 (to_tdx (vcpu),SHARED_EPT_POINTER , root_hpa &PAGE_MASK );
arch/x86/kvm/vmx/x86_ops.h +2 -0
@@ -150,6 +150,7 @@ void tdx_vcpu_load(struct kvm_vcpu *vcpu, int cpu);void tdx_apicv_post_state_restore (struct kvm_vcpu *vcpu);void tdx_deliver_interrupt (struct kvm_lapic *apic,int delivery_mode,int trig_mode,int vector); +void tdx_inject_nmi (struct kvm_vcpu *vcpu);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -180,6 +181,7 @@ static inline void tdx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) {}static inline void tdx_apicv_post_state_restore (struct kvm_vcpu *vcpu) {}static inline void tdx_deliver_interrupt (struct kvm_lapic *apic,int delivery_mode,int trig_mode,int vector) {} +static inline void tdx_inject_nmi (struct kvm_vcpu *vcpu) {}static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
Signature · 1 line
2.25.1
RFC PATCH v5 89/104 KVM: TDX: Add a placeholder for handler of TDX hypercalls (TDG.VP.VMCALL)
In reply to [email protected]From: Sean Christopherson <[email protected]>
The TDX module specification defines TDG.VP.VMCALL API (TDVMCALL for short) for the guest TD to call hypercall to VMM. When the guest TD issues TDG.VP.VMCALL, the guest TD exits to VMM with a new exit reason of TDVMCALL. The arguments from the guest TD and returned values from the VMM are passed in the guest registers. The guest RCX registers indicates which registers are used.
Define the TDVMCALL exit reason, which is carved out from the VMX exit reason namespace as the TDVMCALL exit from TDX guest to TDX-SEAM is really just a VM-Exit. Add a place holder to handle TDVMCALL exit.
--- arch/x86/include/uapi/asm/vmx.h | 4 +++- arch/x86/kvm/vmx/tdx.c | 27 ++++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 13 +++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-)
arch/x86/include/uapi/asm/vmx.h +3 -1
@@ -92,6 +92,7 @@#define EXIT_REASON_UMWAIT 67#define EXIT_REASON_TPAUSE 68#define EXIT_REASON_BUS_LOCK 74 +#define EXIT_REASON_TDCALL 77#define VMX_EXIT_REASONS \ { EXIT_REASON_EXCEPTION_NMI, "EXCEPTION_NMI" }, \
@@ -154,7 +155,8 @@ { EXIT_REASON_XRSTORS, "XRSTORS" }, \ { EXIT_REASON_UMWAIT, "UMWAIT" }, \ { EXIT_REASON_TPAUSE, "TPAUSE" }, \ - { EXIT_REASON_BUS_LOCK, "BUS_LOCK" } + { EXIT_REASON_BUS_LOCK, "BUS_LOCK" }, \ + { EXIT_REASON_TDCALL, "TDCALL" } #define VMX_EXIT_REASON_FLAGS \ { VMX_EXIT_REASONS_FAILED_VMENTRY, "FAILED_VMENTRY" }
arch/x86/kvm/vmx/tdx.c +26 -1
@@ -780,7 +780,8 @@ static noinstr void tdx_vcpu_enter_exit(struct kvm_vcpu *vcpu, struct vcpu_tdx *tdx) {guest_enter_irqoff (); - tdx->exit_reason.full =__tdx_vcpu_run (tdx->tdvpr.pa, vcpu->arch.regs,0 ); + tdx->exit_reason.full =__tdx_vcpu_run (tdx->tdvpr.pa, vcpu->arch.regs, + tdx->tdvmcall.regs_mask);guest_exit_irqoff (); }
@@ -815,6 +816,11 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)if (tdx->exit_reason.error || tdx->exit_reason.non_recoverable)return EXIT_FASTPATH_NONE ; + +if (tdx->exit_reason.basic ==EXIT_REASON_TDCALL ) + tdx->tdvmcall.rcx = vcpu->arch.regs[VCPU_REGS_RCX ]; +else + tdx->tdvmcall.rcx =0 ;return EXIT_FASTPATH_NONE ; }
@@ -859,6 +865,23 @@ static int tdx_handle_triple_fault(struct kvm_vcpu *vcpu)return 0 ; } +static int handle_tdvmcall (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (unlikely (tdx->tdvmcall.xmm_mask)) + goto unsupported; + +switch (tdvmcall_exit_reason (vcpu)) { +default : +break ; + } + +unsupported: +tdvmcall_set_return_code (vcpu,TDG_VP_VMCALL_INVALID_OPERAND ); +return 1 ; +} +void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int pgd_level) {td_vmcs_write64 (to_tdx (vcpu),SHARED_EPT_POINTER , root_hpa &PAGE_MASK );
@@ -1187,6 +1210,8 @@ int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath)return tdx_handle_exception (vcpu);case EXIT_REASON_EXTERNAL_INTERRUPT :return tdx_handle_external_interrupt (vcpu); +case EXIT_REASON_TDCALL : +return handle_tdvmcall (vcpu);case EXIT_REASON_EPT_VIOLATION :return tdx_handle_ept_violation (vcpu);case EXIT_REASON_EPT_MISCONFIG :
arch/x86/kvm/vmx/tdx.h +13 -0
@@ -86,6 +86,19 @@ struct vcpu_tdx {/* Posted interrupt descriptor */ struct pi_desc pi_desc; +union { +struct { +union { +struct { +u16 gpr_mask; +u16 xmm_mask; + }; +u32 regs_mask; + }; +u32 reserved; + }; +u64 rcx; + } tdvmcall;union tdx_exit_reason exit_reason;bool initialized;
Signature · 1 line
2.25.1
RFC PATCH v5 88/104 KVM: TDX: Add TDG.VP.VMCALL accessors to access guest vcpu registers
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDX defines ABI for the TDX guest to call hypercall with TDG.VP.VMCALL API. To get hypercall arguments and to set return values, add accessors to guest vcpu registers.
--- arch/x86/kvm/vmx/tdx.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+)
arch/x86/kvm/vmx/tdx.c +35 -0
@@ -88,6 +88,41 @@ static __always_inline unsigned long tdexit_intr_info(struct kvm_vcpu *vcpu)return kvm_r9_read (vcpu); } +#define BUILD_TDVMCALL_ACCESSORS (param, gpr) \ +static __always_inline \ +unsigned long tdvmcall_##param##_read(struct kvm_vcpu *vcpu) \ +{ \ + return kvm_##gpr##_read(vcpu); \ +} \ +static __always_inline void tdvmcall_##param##_write(struct kvm_vcpu *vcpu, \ + unsigned long val) \ +{ \ + kvm_##gpr##_write(vcpu, val); \ +} +BUILD_TDVMCALL_ACCESSORS (p1, r12); +BUILD_TDVMCALL_ACCESSORS (p2, r13); +BUILD_TDVMCALL_ACCESSORS (p3, r14); +BUILD_TDVMCALL_ACCESSORS (p4, r15); + +static __always_inline unsigned long tdvmcall_exit_type (struct kvm_vcpu *vcpu) +{ +return kvm_r10_read (vcpu); +} +static __always_inline unsigned long tdvmcall_exit_reason (struct kvm_vcpu *vcpu) +{ +return kvm_r11_read (vcpu); +} +static __always_inline void tdvmcall_set_return_code(struct kvm_vcpu *vcpu, +long val) +{ +kvm_r10_write (vcpu, val); +} +static __always_inline void tdvmcall_set_return_val(struct kvm_vcpu *vcpu, +unsigned long val) +{ +kvm_r11_write (vcpu, val); +} +static inline bool is_td_vcpu_created (struct vcpu_tdx *tdx) {return tdx->tdvpr.added;
Signature · 1 line
2.25.1
RFC PATCH v5 77/104 KVM: TDX: Use vcpu_to_pi_desc() uniformly in posted_intr.c
In reply to [email protected]From: Yuan Yao <[email protected]>
The helper function, vcpu_to_pi_desc(), is defined to get the posted interrupt descriptor from vcpu. There is one place that doesn't use it, but direct reference to vmx_vcpu->pi_desc. It's inconsistent.
For TDX, TDX vcpu structure will be defined and the helper function, vcpu_to_pi_desc(), will return tdx_vcpu->pi_desc for TDX case instead of vmx_vcpu->pi_desc. The direct reference to vmx_vcpu->pi_desc doesn't work for TDX.
Replace vmx_vcpu->pi_desc with the helper function, vcpu_pi_desc() for consistency and TDX.
--- arch/x86/kvm/vmx/posted_intr.c | 2 +- arch/x86/kvm/vmx/x86_ops.h | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/posted_intr.c +1 -1
@@ -311,7 +311,7 @@ int pi_update_irte(struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq,continue ; } - vcpu_info.pi_desc_addr =__pa (&to_vmx (vcpu)->pi_desc); + vcpu_info.pi_desc_addr =__pa (vcpu_to_pi_desc (vcpu)); vcpu_info.vector = irq.vector; trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
arch/x86/kvm/vmx/x86_ops.h +3 -0
@@ -147,6 +147,9 @@ void tdx_prepare_switch_to_guest(struct kvm_vcpu *vcpu);void tdx_vcpu_put (struct kvm_vcpu *vcpu);void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu); +void tdx_apicv_post_state_restore (struct kvm_vcpu *vcpu); +int tdx_deliver_posted_interrupt (struct kvm_vcpu *vcpu,int vector); +int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
Signature · 1 line
2.25.1
RFC PATCH v5 84/104 KVM: TDX: Add a place holder to handle TDX VM exit
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Wire up handle_exit and handle_exit_irqoff methods and add a place holder to handle VM exit. Add helper functions to get exit info, exit qualification, etc.
--- arch/x86/kvm/vmx/main.c | 35 +++++++++++++++-- arch/x86/kvm/vmx/tdx.c | 79 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 11 ++++++ 3 files changed, 122 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/main.c +32 -3
@@ -148,6 +148,23 @@ static void vt_vcpu_load(struct kvm_vcpu *vcpu, int cpu)return vmx_vcpu_load (vcpu, cpu); } +static int vt_handle_exit (struct kvm_vcpu *vcpu, +enum exit_fastpath_completion fastpath) +{ +if (is_td_vcpu (vcpu)) +return tdx_handle_exit (vcpu, fastpath); + +return vmx_handle_exit (vcpu, fastpath); +} + +static void vt_handle_exit_irqoff (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_handle_exit_irqoff (vcpu); + +vmx_handle_exit_irqoff (vcpu); +} +static void vt_apicv_post_state_restore (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))
@@ -340,6 +357,18 @@ static void vt_request_immediate_exit(struct kvm_vcpu *vcpu)vmx_request_immediate_exit (vcpu); } +static void vt_get_exit_info (struct kvm_vcpu *vcpu,u32 *reason, +u64 *info1,u64 *info2,u32 *intr_info,u32 *error_code) +{ +if (is_td_vcpu (vcpu)) { +tdx_get_exit_info (vcpu, reason, info1, info2, intr_info, + error_code); +return ; + } + +vmx_get_exit_info (vcpu, reason, info1, info2, intr_info, error_code); +} +static int vt_mem_enc_op (struct kvm *kvm,void __user *argp) {if (!is_td (kvm))
@@ -411,7 +440,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_pre_run = vmx_vcpu_pre_run, .run = vt_vcpu_run, - .handle_exit = vmx_handle_exit, + .handle_exit = vt_handle_exit, .skip_emulated_instruction = vmx_skip_emulated_instruction, .update_emulated_instruction = vmx_update_emulated_instruction, .set_interrupt_shadow = vt_set_interrupt_shadow,
@@ -446,7 +475,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .set_identity_map_addr = vmx_set_identity_map_addr, .get_mt_mask = vmx_get_mt_mask, - .get_exit_info = vmx_get_exit_info, + .get_exit_info = vt_get_exit_info, .vcpu_after_set_cpuid = vmx_vcpu_after_set_cpuid,
@@ -460,7 +489,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .load_mmu_pgd = vt_load_mmu_pgd, .check_intercept = vmx_check_intercept, - .handle_exit_irqoff = vmx_handle_exit_irqoff, + .handle_exit_irqoff = vt_handle_exit_irqoff, .request_immediate_exit = vt_request_immediate_exit,
arch/x86/kvm/vmx/tdx.c +79 -0
97 lines not on this page · raw message
arch/x86/kvm/vmx/x86_ops.h +11 -0
@@ -146,11 +146,16 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu);void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu);void tdx_vcpu_put (struct kvm_vcpu *vcpu);void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu); +void tdx_handle_exit_irqoff (struct kvm_vcpu *vcpu); +int tdx_handle_exit (struct kvm_vcpu *vcpu, +enum exit_fastpath_completion fastpath);void tdx_apicv_post_state_restore (struct kvm_vcpu *vcpu);void tdx_deliver_interrupt (struct kvm_lapic *apic,int delivery_mode,int trig_mode,int vector);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);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -177,11 +182,17 @@ static inline fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) { return EXIT_FASTPstatic inline void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_put (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu) {} +static inline void tdx_handle_exit_irqoff (struct kvm_vcpu *vcpu) {} +static inline int tdx_handle_exit (struct kvm_vcpu *vcpu, +enum exit_fastpath_completion fastpath) {return 0 ; }static inline void tdx_apicv_post_state_restore (struct kvm_vcpu *vcpu) {}static inline void tdx_deliver_interrupt (struct kvm_lapic *apic,int delivery_mode,int trig_mode,int vector) {}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) {}static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
Signature · 1 line
2.25.1
RFC PATCH v5 74/104 KVM: x86: Add a switch_db_regs flag to handle TDX's auto-switched behavior
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Add a flag, KVM_DEBUGREG_AUTO_SWITCHED_GUEST, to skip saving/restoring DRs irrespective of any other flags. TDX-SEAM unconditionally saves and restores guest DRs and reset to architectural INIT state on TD exit. So, KVM needs to save host DRs before TD enter without restoring guest DRs and restore host DRs after TD exit.
Opportunistically convert the KVM_DEBUGREG_* definitions to use BIT().
--- arch/x86/include/asm/kvm_host.h | 10 ++++++++-- arch/x86/kvm/vmx/tdx.c | 1 + arch/x86/kvm/x86.c | 3 ++- 3 files changed, 11 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm_host.h +8 -2
@@ -535,8 +535,14 @@ struct kvm_pmu {struct kvm_pmu_ops ;enum { -KVM_DEBUGREG_BP_ENABLED =1 , -KVM_DEBUGREG_WONT_EXIT =2 , +KVM_DEBUGREG_BP_ENABLED =BIT (0 ), +KVM_DEBUGREG_WONT_EXIT =BIT (1 ), +KVM_DEBUGREG_RELOAD =BIT (2 ), +/* +* Guest debug registers are saved/restored by hardware on exit from +* or enter guest. KVM needn't switch them. +*/ +KVM_DEBUGREG_AUTO_SWITCH =BIT (3 ), };struct kvm_mtrr_range {
arch/x86/kvm/vmx/tdx.c +1 -0
@@ -485,6 +485,7 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu) vcpu->arch.efer =EFER_SCE |EFER_LME |EFER_LMA |EFER_NX ; + vcpu->arch.switch_db_regs =KVM_DEBUGREG_AUTO_SWITCH ; vcpu->arch.cr0_guest_owned_bits =-1ul ; vcpu->arch.cr4_guest_owned_bits =-1ul ;
arch/x86/kvm/x86.c +2 -1
@@ -10084,7 +10084,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)if (vcpu->arch.guest_fpu.xfd_err)wrmsrl (MSR_IA32_XFD_ERR , vcpu->arch.guest_fpu.xfd_err); -if (unlikely (vcpu->arch.switch_db_regs)) { +if (unlikely (vcpu->arch.switch_db_regs & ~KVM_DEBUGREG_AUTO_SWITCH )) {set_debugreg (0 ,7 );set_debugreg (vcpu->arch.eff_db[0 ],0 );set_debugreg (vcpu->arch.eff_db[1 ],1 );
@@ -10126,6 +10126,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)*/ if (unlikely (vcpu->arch.switch_db_regs &KVM_DEBUGREG_WONT_EXIT )) {WARN_ON (vcpu->guest_debug &KVM_GUESTDBG_USE_HW_BP ); +WARN_ON (vcpu->arch.switch_db_regs &KVM_DEBUGREG_AUTO_SWITCH );static_call (kvm_x86_sync_dirty_debug_regs)(vcpu);kvm_update_dr0123 (vcpu);kvm_update_dr7 (vcpu);
Signature · 1 line
2.25.1
RFC PATCH v5 1/104 KVM: VMX: Move out vmx_x86_ops to 'main.c' to wrap VMX and TDX
In reply to [email protected]From: Sean Christopherson <[email protected]>
KVM accesses Virtual Machine Control Structure (VMCS) with VMX instructions to operate on VM. TDX defines its data structure and TDX SEAMCALL APIs for VMM to operate on Trust Domain (TD) instead.
Trust Domain Virtual Processor State (TDVPS) is the root control structure of a TD VCPU. It helps the TDX module control the operation of the VCPU, and holds the VCPU state while the VCPU is not running. TDVPS is opaque to software and DMA access, accessible only by using the TDX module interface functions (such as TDH.VP.RD, TDH.VP.WR ,..). TDVPS includes TD VMCS, and TD VMCS auxiliary structures, such as virtual APIC page, virtualization exception information, etc. TDVPS is composed of Trust Domain Virtual Processor Root (TDVPR) which is the root page of TDVPS and Trust Domain Virtual Processor eXtension (TDVPX) pages which extend TDVPR to help provide enough physical space for the logical TDVPS structure.
Also, we have a new structure, Trust Domain Control Structure (TDCS) is the main control structure of a guest TD, and encrypted (using the guest TD's ephemeral private key). At a high level, TDCS holds information for controlling TD operation as a whole, execution, EPTP, MSR bitmaps, etc. KVM needs to set it up. Note that MSR bitmaps are held as part of TDCS (unlike VMX) because they are meant to have the same value for all VCPUs of the same TD. TDCS is a multi-page logical structure composed of multiple Trust Domain Control Extension (TDCX) physical pages. Trust Domain Root (TDR) is the root control structure of a guest TD and is encrypted using the TDX global private key. It holds a minimal set of state variables that enable guest TD control even during times when the TD's private key is not known, or when the TD's key management state does not permit access to memory encrypted using the TD's private key.
The following shows the relationship between those structures.
TDR--> TDCS per-TD
| \--> TDCX
\
\--> TDVPS per-TD VCPU
\--> TDVPR and TDVPXThe existing global struct kvm_x86_ops already defines an interface which fits with TDX. But kvm_x86_ops is system-wide, not per-VM structure. To allow VMX to coexist with TDs, the kvm_x86_ops callbacks will have wrappers "if (tdx) tdx_op() else vmx_op()" to switch VMX or TDX at run time.
To split the runtime switch, the VMX implementation, and the TDX implementation, add main.c, and move out the vmx_x86_ops hooks in preparation for adding TDX, which can coexist with VMX, i.e. KVM can run both VMs and TDs. Use 'vt' for the naming scheme as a nod to VT-x and as a concatenation of VmxTdx.
The current code looks as follows.
In vmx.c
static vmx_op() { ... }
static struct kvm_x86_ops vmx_x86_ops = {
.op = vmx_op,initialization code
The eventually converted code will look like In vmx.c, keep the VMX operations.
vmx_op() { ... }
VMX initialization
In tdx.c, define the TDX operations.
tdx_op() { ... }
TDX initialization
In x86_ops.h, declare the VMX and TDX operations.
vmx_op();
tdx_op();
In main.c, define common wrappers for VMX and VMX.
static vt_ops() { if (tdx) tdx_ops() else vmx_ops() }
static struct kvm_x86_ops vt_x86_ops = {
.op = vt_op,initialization to call VMX and TDX initialization
Opportunistically, fix the name inconsistency from vmx_create_vcpu() and vmx_free_vcpu() to vmx_vcpu_create() and vxm_vcpu_free().
--- arch/x86/kvm/Makefile | 2 +- arch/x86/kvm/vmx/main.c | 154 ++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 360 +++++++++++-------------------------- arch/x86/kvm/vmx/x86_ops.h | 126 +++++++++++++ 4 files changed, 385 insertions(+), 257 deletions(-) create mode 100644 arch/x86/kvm/vmx/main.c create mode 100644 arch/x86/kvm/vmx/x86_ops.h
arch/x86/kvm/Makefile +1 -1
@@ -22,7 +22,7 @@ kvm-$(CONFIG_X86_64) += mmu/tdp_iter.o mmu/tdp_mmu.o kvm-$(CONFIG_KVM_XEN) += xen.o kvm-intel-y += vmx/vmx.o vmx/vmenter.o vmx/pmu_intel.o vmx/vmcs12.o \ - vmx/evmcs.o vmx/nested.o vmx/posted_intr.o + vmx/evmcs.o vmx/nested.o vmx/posted_intr.o vmx/main.o kvm-intel-$(CONFIG_X86_SGX_KVM) += vmx/sgx.o kvm-amd-y += svm/svm.o svm/vmenter.o svm/pmu.o svm/nested.o svm/avic.o svm/sev.o
arch/x86/kvm/vmx/main.c new file +154 -0
154 lines not on this page · raw message
arch/x86/kvm/vmx/vmx.c +104 -256
905 lines not on this page · raw message
arch/x86/kvm/vmx/x86_ops.h new file +126 -0
126 lines not on this page · raw message
Signature · 1 line
2.25.1
RFC PATCH v5 3/104 KVM: TDX: Detect CPU feature on kernel module initialization
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDX requires several initialization steps for KVM to create guest TDs. Detect CPU feature, enable VMX (TDX is based on VMX), detect TDX module availability, and initialize TDX module. This patch implements the first step to detect CPU feature. Because VMX isn't enabled yet by VMXON instruction on KVM kernel module initialization, defer further initialization step until VMX is enabled by hardware_enable callback.
Introduce a module parameter, enable_tdx, to explicitly enable TDX KVM support. It's off by default to keep same behavior for those who don't use TDX. Implement CPU feature detection at KVM kernel module initialization as hardware_setup callback to check if CPU feature is available and get some CPU parameters.
--- arch/x86/kvm/Makefile | 1 + arch/x86/kvm/vmx/main.c | 15 ++++++++++- arch/x86/kvm/vmx/tdx.c | 53 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 6 +++++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 arch/x86/kvm/vmx/tdx.c
arch/x86/kvm/Makefile +1 -0
@@ -24,6 +24,7 @@ kvm-$(CONFIG_KVM_XEN) += xen.o kvm-intel-y += vmx/vmx.o vmx/vmenter.o vmx/pmu_intel.o vmx/vmcs12.o \ vmx/evmcs.o vmx/nested.o vmx/posted_intr.o vmx/main.o kvm-intel-$(CONFIG_X86_SGX_KVM) += vmx/sgx.o +kvm-intel-$(CONFIG_INTEL_TDX_HOST) += vmx/tdx.o kvm-amd-y += svm/svm.o svm/vmenter.o svm/pmu.o svm/nested.o svm/avic.o svm/sev.o
arch/x86/kvm/vmx/main.c +14 -1
@@ -6,6 +6,19 @@#include "nested.h" #include "pmu.h" +static __init int vt_hardware_setup (void ) +{ +int ret; + + ret =vmx_hardware_setup (); +if (ret) +return ret; + +tdx_hardware_setup (&vt_x86_ops); + +return 0 ; +} +struct kvm_x86_ops vt_x86_ops__initdata = { .name ="kvm_intel" ,
@@ -147,7 +160,7 @@ struct kvm_x86_init_ops vt_init_ops __initdata = { .cpu_has_kvm_support = vmx_cpu_has_kvm_support, .disabled_by_bios = vmx_disabled_by_bios, .check_processor_compatibility = vmx_check_processor_compat, - .hardware_setup = vmx_hardware_setup, + .hardware_setup = vt_hardware_setup, .handle_intel_pt_intr = NULL, .runtime_ops = &vt_x86_ops,
arch/x86/kvm/vmx/tdx.c new file +53 -0
53 lines not on this page · raw message
arch/x86/kvm/vmx/x86_ops.h +6 -0
@@ -123,4 +123,10 @@ void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu);#endif void vmx_setup_mce (struct kvm_vcpu *vcpu); +#ifdef CONFIG_INTEL_TDX_HOST +void __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops); +#else +static inline void tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {} +#endif +#endif /* __KVM_X86_VMX_X86_OPS_H */
Signature · 1 line
2.25.1
RFC PATCH v5 2/104 x86/virt/tdx: export platform_has_tdx
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDX KVM uses platform_has_tdx() via hardware_setup().
--- arch/x86/virt/vmx/tdx.c | 1 + 1 file changed, 1 insertion(+)
arch/x86/virt/vmx/tdx.c +1 -0
@@ -1630,3 +1630,4 @@ bool platform_has_tdx(void) {return seamrr_enabled () &&tdx_keyid_sufficient (); } +EXPORT_SYMBOL_GPL (platform_has_tdx);
Signature · 1 line
2.25.1
RFC PATCH v5 5/104 KVM: x86: Refactor KVM VMX module init/exit functions
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Currently, KVM VMX module initialization/exit functions are a single function each. Refactor KVM VMX module initialization functions into KVM common part and VMX part so that TDX specific part can be added cleanly. Opportunistically refactor module exit function as well.
The current module initialization flow is, 1.) calculate the sizes of VMX kvm structure and VMX vcpu structure, 2.) report those sizes to the KVM common layer and KVM common initialization, and 3.) VMX specific system-wide initialization.
Refactor the KVM VMX module initialization function into functions with a wrapper function to separate VMX logic in vmx.c from a file, main.c, common among VMX and TDX. We have a wrapper function,
"vt_init() {vmx_pre_kvm_init(); kvm_init(); vmx_init(); }" in main.c, and vmx_pre_kvm_init() and vmx_init() in vmx.c. vmx_pre_kvm_init() calculates the sizes of VMX kvm structure and KVM vcpu structure, kvm_init() does system-wide initialization of the KVM common layer, and vmx_init() does system-wide VMX initialization.
The KVM architecture common layer allocates struct kvm with reported size for architecture-specific code. The KVM VMX module defines its structure as struct vmx_kvm { struct kvm; VMX specific members;} and uses it as struct vmx kvm. Similar for vcpu structure. TDX KVM patches will define TDX specific kvm and vcpu structures, add tdx_pre_kvm_init() to report the sizes of them to the KVM common layer.
The current module exit function is also a single function, a combination of VMX specific logic and common KVM logic. Refactor it into VMX specific logic and KVM common logic. This is just refactoring to keep the VMX specific logic in vmx.c from main.c.
--- arch/x86/kvm/vmx/main.c | 33 +++++++++++++ arch/x86/kvm/vmx/vmx.c | 97 +++++++++++++++++++------------------- arch/x86/kvm/vmx/x86_ops.h | 5 +- 3 files changed, 86 insertions(+), 49 deletions(-)
arch/x86/kvm/vmx/main.c +33 -0
@@ -165,3 +165,36 @@ struct kvm_x86_init_ops vt_init_ops __initdata = { .runtime_ops = &vt_x86_ops, }; + +static int __init vt_init (void ) +{ +unsigned int vcpu_size =0 , vcpu_align =0 ; +int r; + +vmx_pre_kvm_init (&vcpu_size, &vcpu_align); + + r =kvm_init (&vt_init_ops, vcpu_size, vcpu_align,THIS_MODULE ); +if (r) + goto err_vmx_post_exit; + + r =vmx_init (); +if (r) + goto err_kvm_exit; + +return 0 ; + +err_kvm_exit: +kvm_exit (); +err_vmx_post_exit: +vmx_post_kvm_exit (); +return r; +} +module_init (vt_init); + +static void vt_exit (void ) +{ +vmx_exit (); +kvm_exit (); +vmx_post_kvm_exit (); +} +module_exit (vt_exit);
arch/x86/kvm/vmx/vmx.c +49 -48
121 lines not on this page · raw message
arch/x86/kvm/vmx/x86_ops.h +4 -1
@@ -8,7 +8,10 @@#include "x86.h" -extern struct kvm_x86_init_ops vt_init_ops__initdata ; +void __init vmx_pre_kvm_init (unsigned int *vcpu_size,unsigned int *vcpu_align); +int __init vmx_init (void ); +void vmx_exit (void ); +void vmx_post_kvm_exit (void );__init int vmx_cpu_has_kvm_support (void );__init int vmx_disabled_by_bios (void );
Signature · 1 line
2.25.1
RFC PATCH v5 4/104 KVM: Enable hardware before doing arch VM initialization
In reply to [email protected]From: Sean Christopherson <[email protected]>
Swap the order of hardware_enable_all() and kvm_arch_init_vm() to accommodate Intel's TDX, which needs VMX to be enabled during VM init in order to make SEAMCALLs.
This also provides consistent ordering between kvm_create_vm() and kvm_destroy_vm() with respect to calling kvm_arch_destroy_vm() and hardware_disable_all().
--- virt/kvm/kvm_main.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
virt/kvm/kvm_main.c +7 -7
@@ -1105,19 +1105,19 @@ static struct kvm *kvm_create_vm(unsigned long type)rcu_assign_pointer (kvm->buses[i],kzalloc (sizeof (struct kvm_io_bus ),GFP_KERNEL_ACCOUNT ));if (!kvm->buses[i]) - goto out_err_no_arch_destroy_vm; + goto out_err_no_disable; } kvm->max_halt_poll_ns = halt_poll_ns; - r =kvm_arch_init_vm (kvm, type); -if (r) - goto out_err_no_arch_destroy_vm; - r =hardware_enable_all ();if (r) goto out_err_no_disable; + r =kvm_arch_init_vm (kvm, type); +if (r) + goto out_err_no_arch_destroy_vm; +#ifdef CONFIG_HAVE_KVM_IRQFD INIT_HLIST_HEAD (&kvm->irq_ack_notifier_list);#endif
@@ -1145,10 +1145,10 @@ static struct kvm *kvm_create_vm(unsigned long type)mmu_notifier_unregister (&kvm->mmu_notifier, current->mm);#endif out_err_no_mmu_notifier: -hardware_disable_all (); -out_err_no_disable:kvm_arch_destroy_vm (kvm); out_err_no_arch_destroy_vm: +hardware_disable_all (); +out_err_no_disable:WARN_ON_ONCE (!refcount_dec_and_test (&kvm->users_count));for (i =0 ; i <KVM_NR_BUSES ; i++)kfree (kvm_get_bus (kvm, i));
Signature · 1 line
2.25.1
RFC PATCH v5 6/104 KVM: TDX: Add placeholders for TDX VM/vcpu structure
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Add placeholders TDX VM/vcpu structure that overlays with VMX VM/vcpu structures. Initialize VM structure size and vcpu size/align so that x86 KVM common code knows those size irrespective of VMX or TDX. Those structures will be populated as guest creation logic develops.
Add helper functions to check if the VM is guest TD and add conversion functions between KVM VM/VCPU and TDX VM/VCPU.
--- arch/x86/kvm/vmx/main.c | 3 +++ arch/x86/kvm/vmx/tdx.c | 11 +++++++++ arch/x86/kvm/vmx/tdx.h | 47 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 4 ++++ 4 files changed, 65 insertions(+) create mode 100644 arch/x86/kvm/vmx/tdx.h
arch/x86/kvm/vmx/main.c +3 -0
@@ -171,6 +171,9 @@ static int __init vt_init(void)unsigned int vcpu_size =0 , vcpu_align =0 ;int r; +/* tdx_pre_kvm_init must be called before vmx_pre_kvm_init(). */ +tdx_pre_kvm_init (&vcpu_size, &vcpu_align, &vt_x86_ops.vm_size); +vmx_pre_kvm_init (&vcpu_size, &vcpu_align); r =kvm_init (&vt_init_ops, vcpu_size, vcpu_align,THIS_MODULE );
arch/x86/kvm/vmx/tdx.c +11 -0
@@ -5,6 +5,7 @@#include "capabilities.h" #include "x86_ops.h" +#include "tdx.h" #undef pr_fmt#define pr_fmt (fmt) "tdx: " fmt
@@ -51,3 +52,13 @@ void __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops)if (__tdx_hardware_setup (&vt_x86_ops)) enable_tdx = false; } + +void __init tdx_pre_kvm_init (unsigned int *vcpu_size, +unsigned int *vcpu_align,unsigned int *vm_size) +{ + *vcpu_size =sizeof (struct vcpu_tdx ); + *vcpu_align = __alignof__(struct vcpu_tdx ); + +if (sizeof (struct kvm_tdx ) > *vm_size) + *vm_size =sizeof (struct kvm_tdx ); +}
arch/x86/kvm/vmx/tdx.h new file +47 -0
47 lines not on this page · raw message
arch/x86/kvm/vmx/x86_ops.h +4 -0
@@ -127,8 +127,12 @@ void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu);void vmx_setup_mce (struct kvm_vcpu *vcpu);#ifdef CONFIG_INTEL_TDX_HOST +void __init tdx_pre_kvm_init (unsigned int *vcpu_size, +unsigned int *vcpu_align,unsigned int *vm_size);void __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops);#else +static inline void tdx_pre_kvm_init ( +unsigned int *vcpu_size,unsigned int *vcpu_align,unsigned int *vm_size) {}static inline void tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {}#endif
Signature · 1 line
2.25.1
RFC PATCH v5 7/104 x86/virt/tdx: Add a helper function to return system wide info about TDX module
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDX KVM needs system-wide information about the TDX module, struct tdsysinfo_struct. Add a helper function tdx_get_sysinfo() to return it instead of KVM getting it with various error checks. Move out the struct definition about it to common place tdx_host.h.
--- arch/x86/include/asm/tdx.h | 55 ++++++++++++++++++++++++++++++++++++++ arch/x86/virt/vmx/tdx.c | 16 +++++++++-- arch/x86/virt/vmx/tdx.h | 52 ----------------------------------- 3 files changed, 69 insertions(+), 54 deletions(-)
arch/x86/include/asm/tdx.h +55 -0
@@ -82,15 +82,70 @@ static inline long tdx_kvm_hypercall(unsigned int nr, unsigned long p1,#endif /* CONFIG_INTEL_TDX_GUEST && CONFIG_KVM_GUEST */ #ifdef CONFIG_INTEL_TDX_HOST +struct tdx_cpuid_config { +u32 leaf; +u32 sub_leaf; +u32 eax; +u32 ebx; +u32 ecx; +u32 edx; +} __packed; + +#define TDSYSINFO_STRUCT_SIZE 1024 +#define TDSYSINFO_STRUCT_ALIGNMENT 1024 + +struct tdsysinfo_struct { +/* TDX-SEAM Module Info */ +u32 attributes; +u32 vendor_id; +u32 build_date; +u16 build_num; +u16 minor_version; +u16 major_version; +u8 reserved0[14 ]; +/* Memory Info */ +u16 max_tdmrs; +u16 max_reserved_per_tdmr; +u16 pamt_entry_size; +u8 reserved1[10 ]; +/* Control Struct Info */ +u16 tdcs_base_size; +u8 reserved2[2 ]; +u16 tdvps_base_size; +u8 tdvps_xfam_dependent_size; +u8 reserved3[9 ]; +/* TD Capabilities */ +u64 attributes_fixed0; +u64 attributes_fixed1; +u64 xfam_fixed0; +u64 xfam_fixed1; +u8 reserved4[32 ]; +u32 num_cpuid_config; +/* +* The actual number of CPUID_CONFIG depends on above +* 'num_cpuid_config'. The size of 'struct tdsysinfo_struct' +* is 1024B defined by TDX architecture. Use a union with +* specific padding to make 'sizeof(struct tdsysinfo_struct)' +* equal to 1024. +*/ +union { +struct tdx_cpuid_config cpuid_configs[0 ]; +u8 reserved5[892 ]; + }; +} __packed__aligned(TDSYSINFO_STRUCT_ALIGNMENT) ; +void tdx_detect_cpu (struct cpuinfo_x86 *c);int tdx_detect (void );int tdx_init (void );bool platform_has_tdx (void ); +const struct tdsysinfo_struct *tdx_get_sysinfo (void );#else static inline void tdx_detect_cpu (struct cpuinfo_x86 *c) { }static inline int tdx_detect (void ) {return -ENODEV ; }static inline int tdx_init (void ) {return -ENODEV ; }static inline bool platform_has_tdx (void ) {return false; } +struct tdsysinfo_struct ; +static inline const struct tdsysinfo_struct *tdx_get_sysinfo (void ) {return NULL ; }#endif /* CONFIG_INTEL_TDX_HOST */ #endif /* !__ASSEMBLY__ */
arch/x86/virt/vmx/tdx.c +14 -2
@@ -641,7 +641,7 @@ static int sanitize_cmrs(struct cmr_info *cmr_array, int cmr_num)return 0 ; } -static int tdx_get_sysinfo (void ) +static int __tdx_get_sysinfo (void ) {struct tdx_module_output out;u64 tdsysinfo_sz, cmr_num;
@@ -676,6 +676,18 @@ static int tdx_get_sysinfo(void)return sanitize_cmrs (tdx_cmr_array, cmr_num); } +const struct tdsysinfo_struct *tdx_get_sysinfo (void ) +{ +const struct tdsysinfo_struct *r =NULL ; + +mutex_lock (&tdx_module_lock); +if (tdx_module_status ==TDX_MODULE_INITIALIZED ) + r = &tdx_sysinfo; +mutex_unlock (&tdx_module_lock); +return r; +} +EXPORT_SYMBOL_GPL (tdx_get_sysinfo); +/* * Only E820_TYPE_RAM and E820_TYPE_PRAM are considered as candidate for * TDX usable memory. The latter is treated as RAM because it is created
@@ -1383,7 +1395,7 @@ static int init_tdx_module(void) goto out;/* Get TDX module information and CMRs */ - ret =tdx_get_sysinfo (); + ret =__tdx_get_sysinfo ();if (ret) goto out;
arch/x86/virt/vmx/tdx.h +0 -52
58 lines not on this page · raw message
Signature · 1 line
2.25.1
RFC PATCH v5 9/104 KVM: x86: Introduce vm_type to differentiate default VMs from confidential VMs
In reply to [email protected]From: Sean Christopherson <[email protected]>
Unlike default VMs, confidential VMs (Intel TDX and AMD SEV-ES) don't allow some operations (e.g., memory read/write, register state access, etc).
Introduce vm_type to track the type of the VM to x86 KVM. Other arch KVMs already use vm_type, KVM_INIT_VM accepts vm_type, and x86 KVM callback vm_init accepts vm_type. So follow them. Further, a different policy can be made based on vm_type. Define KVM_X86_DEFAULT_VM for default VM as default and define KVM_X86_TDX_VM for Intel TDX VM. The wrapper function will be defined as "bool is_td(kvm) { return vm_type == VM_TYPE_TDX; }"
Add a capability KVM_CAP_VM_TYPES to effectively allow device model, e.g. qemu, to query what VM types are supported by KVM. This (introduce a new capability and add vm_type) is chosen to align with other arch KVMs that have VM types already. Other arch KVMs uses different name to query supported vm types and there is no common name for it, so new name was chosen.
--- Documentation/virt/kvm/api.rst | 15 +++++++++++++++ arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/include/uapi/asm/kvm.h | 3 +++ arch/x86/kvm/svm/svm.c | 6 ++++++ arch/x86/kvm/vmx/main.c | 1 + arch/x86/kvm/vmx/tdx.h | 6 +----- arch/x86/kvm/vmx/vmx.c | 5 +++++ arch/x86/kvm/vmx/x86_ops.h | 1 + arch/x86/kvm/x86.c | 9 ++++++++- include/uapi/linux/kvm.h | 1 + tools/arch/x86/include/uapi/asm/kvm.h | 3 +++ tools/include/uapi/linux/kvm.h | 1 + 13 files changed, 48 insertions(+), 6 deletions(-)
Documentation/virt/kvm/api.rst +15 -0
@@ -147,15 +147,30 @@ described as 'basic' will be available. The new VM has no virtual cpus and no memory. You probably want to use 0 as machine type. +X86: +^^^^ + +Supported vm type can be queried from KVM_CAP_VM_TYPES, which returns the +bitmap of supported vm types. The 1-setting of bit @n means vm type with +value @n is supported. + +S390: +^^^^^ + In order to create user controlled virtual machines on S390, check KVM_CAP_S390_UCONTROL and use the flag KVM_VM_S390_UCONTROL as privileged user (CAP_SYS_ADMIN). +MIPS: +^^^^^ + To use hardware assisted virtualization on MIPS (VZ ASE) rather than the default trap & emulate implementation (which changes the virtual memory layout to fit in user mode), check KVM_CAP_MIPS_VZ and use the flag KVM_VM_MIPS_VZ. +ARM64: +^^^^^^ On arm64, the physical address size for a VM (IPA Size limit) is limited to 40bits by default. The limit can be configured if the host supports the
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -18,6 +18,7 @@ KVM_X86_OP_NULL(hardware_unsetup) KVM_X86_OP_NULL(cpu_has_accelerated_tpr) KVM_X86_OP(has_emulated_msr) KVM_X86_OP(vcpu_after_set_cpuid) +KVM_X86_OP(is_vm_type_supported) KVM_X86_OP(vm_init) KVM_X86_OP_NULL(vm_destroy) KVM_X86_OP(vcpu_create)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1048,6 +1048,7 @@ struct kvm_x86_msr_filter {#define APICV_INHIBIT_REASON_ABSENT 7struct kvm_arch { +unsigned long vm_type;unsigned long n_used_mmu_pages;unsigned long n_requested_mmu_pages;unsigned long n_max_mmu_pages;
@@ -1322,6 +1323,7 @@ struct kvm_x86_ops {bool (*has_emulated_msr)(struct kvm *kvm,u32 index);void (*vcpu_after_set_cpuid)(struct kvm_vcpu *vcpu); +bool (*is_vm_type_supported)(unsigned long vm_type);unsigned int vm_size;int (*vm_init)(struct kvm *kvm);void (*vm_destroy)(struct kvm *kvm);
arch/x86/include/uapi/asm/kvm.h +3 -0
@@ -525,4 +525,7 @@ struct kvm_pmu_event_filter {#define KVM_VCPU_TSC_CTRL 0/* control group for the timestamp counter (TSC) */ #define KVM_VCPU_TSC_OFFSET 0/* attribute for the TSC offset */ +#define KVM_X86_DEFAULT_VM 0 +#define KVM_X86_TDX_VM 1 +#endif /* _ASM_X86_KVM_H */
arch/x86/kvm/svm/svm.c +6 -0
@@ -4512,6 +4512,11 @@ static void svm_vm_destroy(struct kvm *kvm)sev_vm_destroy (kvm); } +static bool svm_is_vm_type_supported (unsigned long type) +{ +return type ==KVM_X86_DEFAULT_VM ; +} +static int svm_vm_init (struct kvm *kvm) {if (!pause_filter_count || !pause_filter_thresh)
@@ -4539,6 +4544,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .vcpu_free = svm_free_vcpu, .vcpu_reset = svm_vcpu_reset, + .is_vm_type_supported = svm_is_vm_type_supported, .vm_size = sizeof(struct kvm_svm), .vm_init = svm_vm_init, .vm_destroy = svm_vm_destroy,
arch/x86/kvm/vmx/main.c +1 -0
@@ -29,6 +29,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .cpu_has_accelerated_tpr = report_flexpriority, .has_emulated_msr = vmx_has_emulated_msr, + .is_vm_type_supported = vmx_is_vm_type_supported, .vm_size = sizeof(struct kvm_vmx), .vm_init = vmx_vm_init,
arch/x86/kvm/vmx/tdx.h +1 -5
@@ -15,11 +15,7 @@ struct vcpu_tdx {static inline bool is_td (struct kvm *kvm) { -/* -* TDX VM type isn't defined yet. -* return kvm->arch.vm_type == KVM_X86_TDX_VM; -*/ -return false; +return kvm->arch.vm_type ==KVM_X86_TDX_VM ; }static inline bool is_td_vcpu (struct kvm_vcpu *vcpu)
arch/x86/kvm/vmx/vmx.c +5 -0
@@ -7079,6 +7079,11 @@ int vmx_vcpu_create(struct kvm_vcpu *vcpu)return err; } +bool vmx_is_vm_type_supported (unsigned long type) +{ +return type ==KVM_X86_DEFAULT_VM ; +} +#define L1TF_MSG_SMT "L1TF CPU bug present and SMT on, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n"#define L1TF_MSG_L1D "L1TF CPU bug present and virtualization mitigation disabled, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n"
arch/x86/kvm/vmx/x86_ops.h +1 -0
@@ -25,6 +25,7 @@ void vmx_hardware_unsetup(void);int vmx_hardware_enable (void );void vmx_hardware_disable (void );bool report_flexpriority (void ); +bool vmx_is_vm_type_supported (unsigned long type);int vmx_vm_init (struct kvm *kvm);int vmx_vcpu_create (struct kvm_vcpu *vcpu);int vmx_vcpu_pre_run (struct kvm_vcpu *vcpu);
arch/x86/kvm/x86.c +8 -1
@@ -4344,6 +4344,11 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) r =sizeof (struct kvm_xsave );break ; } +case KVM_CAP_VM_TYPES : + r =BIT (KVM_X86_DEFAULT_VM ); +if (static_call (kvm_x86_is_vm_type_supported)(KVM_X86_TDX_VM )) + r |=BIT (KVM_X86_TDX_VM ); +break ;default :break ; }
@@ -11583,9 +11588,11 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)int ret;unsigned long flags; -if (type) +if (!static_call (kvm_x86_is_vm_type_supported)(type))return -EINVAL ; + kvm->arch.vm_type = type; + ret =kvm_page_track_init (kvm);if (ret)return ret;
include/uapi/linux/kvm.h +1 -0
@@ -1135,6 +1135,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_XSAVE2 208#define KVM_CAP_SYS_ATTRIBUTES 209#define KVM_CAP_PPC_AIL_MODE_3 210 +#define KVM_CAP_VM_TYPES 21#ifdef KVM_CAP_IRQ_ROUTING
tools/arch/x86/include/uapi/asm/kvm.h +3 -0
@@ -525,4 +525,7 @@ struct kvm_pmu_event_filter {#define KVM_VCPU_TSC_CTRL 0/* control group for the timestamp counter (TSC) */ #define KVM_VCPU_TSC_OFFSET 0/* attribute for the TSC offset */ +#define KVM_X86_DEFAULT_VM 0 +#define KVM_X86_TDX_VM 1 +#endif /* _ASM_X86_KVM_H */
tools/include/uapi/linux/kvm.h +1 -0
@@ -1135,6 +1135,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_XSAVE2 208#define KVM_CAP_SYS_ATTRIBUTES 209#define KVM_CAP_PPC_AIL_MODE_3 210 +#define KVM_CAP_VM_TYPES 211#ifdef KVM_CAP_IRQ_ROUTING
Signature · 1 line
2.25.1
RFC PATCH v5 8/104 KVM: TDX: Add a function to initialize TDX module
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Memory used for TDX is encrypted with an encryption key. An encryption key is assigned to guest TD, and TDX memory is encrypted. VMM calculates Trust Domain Memory Range (TDMR), a range of memory pages that can hold TDX memory encrypted with an encryption key. VMM allocates memory regions for Physical Address Metadata Table (PAMT) which the TDX module uses to track page states. Used for TDX memory, assigned to which guest TD, etc. VMM gives PAMT regions to the TDX module and initializes it which is also encrypted.
TDX requires more initialization steps in addition to VMX. As a preparation step, check if the CPU feature is available and enable VMX because the TDX module API requires VMX to be enabled to be functional. The next step is basic platform initialization. Check if TDX module API is available, call system-wide initialization API (TDH.SYS.INIT), and call LP initialization API (TDH.SYS.LP.INIT). Lastly, get system-wide parameters (TDH.SYS.INFO), allocate PAMT for TDX module to track page states (TDH.SYS.CONFIG), configure encryption key (TDH.SYS.KEY.CONFIG), and initialize PAMT (TDH.SYS.TDMR.INIT).
A TDX host patch series implements those details and it provides APIs, seamrr_enabled() to check if CPU feature is available, init_tdx() to initialize the TDX module, tdx_get_tdsysinfo() to get TDX system parameters.
Add a wrapper function to initialize the TDX module and get system-wide parameters via those APIs. Because TDX requires VMX enabled, It will be called on-demand when the first guest TD is created via x86 KVM init_vm callback.
--- arch/x86/kvm/vmx/tdx.c | 89 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 4 ++ 2 files changed, 93 insertions(+)
arch/x86/kvm/vmx/tdx.c +89 -0
@@ -13,9 +13,98 @@static bool __read_mostly enable_tdx = true;module_param_named (tdx, enable_tdx, bool,0644 ); +#define TDX_MAX_NR_CPUID_CONFIGS \ + ((sizeof(struct tdsysinfo_struct) - \ + offsetof(struct tdsysinfo_struct, cpuid_configs)) \ + / sizeof(struct tdx_cpuid_config)) + +struct tdx_capabilities { +u8 tdcs_nr_pages; +u8 tdvpx_nr_pages; + +u64 attrs_fixed0; +u64 attrs_fixed1; +u64 xfam_fixed0; +u64 xfam_fixed1; + +u32 nr_cpuid_configs; +struct tdx_cpuid_config cpuid_configs[TDX_MAX_NR_CPUID_CONFIGS ]; +}; + +/* Capabilities of KVM + the TDX module. */ +struct tdx_capabilities tdx_caps; +static u64 hkid_mask__ro_after_init ;static u8 hkid_start_pos__ro_after_init ; +static int __tdx_module_setup (void ) +{ +const struct tdsysinfo_struct *tdsysinfo; +int ret =0 ; + +BUILD_BUG_ON (sizeof (*tdsysinfo) !=1024 ); +BUILD_BUG_ON (TDX_MAX_NR_CPUID_CONFIGS !=37 ); + + ret =tdx_detect (); +if (ret) { +pr_info ("Failed to detect TDX module.\n" ); +return ret; + } + + ret =tdx_init (); +if (ret) { +pr_info ("Failed to initialize TDX module.\n" ); +return ret; + } + + tdsysinfo =tdx_get_sysinfo (); +if (tdx_caps.nr_cpuid_configs >TDX_MAX_NR_CPUID_CONFIGS ) +return -EIO ; + + tdx_caps = (struct tdx_capabilities ) { + .tdcs_nr_pages = tdsysinfo->tdcs_base_size /PAGE_SIZE , +/* +* TDVPS = TDVPR(4K page) + TDVPX(multiple 4K pages). +* -1 for TDVPR. +*/ + .tdvpx_nr_pages = tdsysinfo->tdvps_base_size /PAGE_SIZE -1 , + .attrs_fixed0 = tdsysinfo->attributes_fixed0, + .attrs_fixed1 = tdsysinfo->attributes_fixed1, + .xfam_fixed0 = tdsysinfo->xfam_fixed0, + .xfam_fixed1 = tdsysinfo->xfam_fixed1, + .nr_cpuid_configs = tdsysinfo->num_cpuid_config, + }; +if (!memcpy (tdx_caps.cpuid_configs, tdsysinfo->cpuid_configs, + tdsysinfo->num_cpuid_config * +sizeof (struct tdx_cpuid_config ))) +return -EIO ; + +return 0 ; +} + +int tdx_module_setup (void ) +{ +static DEFINE_MUTEX (tdx_init_lock); +static bool __read_mostly tdx_module_initialized; +int ret =0 ; + +mutex_lock (&tdx_init_lock); + +if (!tdx_module_initialized) { +if (enable_tdx) { + ret =__tdx_module_setup (); +if (ret) + enable_tdx = false; +else + tdx_module_initialized = true; + }else + ret = -EOPNOTSUPP ; + } + +mutex_unlock (&tdx_init_lock); +return ret; +} +static int __init __tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {u32 max_pa;
arch/x86/kvm/vmx/tdx.h +4 -0
@@ -3,6 +3,8 @@#define __KVM_X86_TDX_H#ifdef CONFIG_INTEL_TDX_HOST +int tdx_module_setup (void ); +struct kvm_tdx {struct kvm kvm; };
@@ -35,6 +37,8 @@ static inline struct vcpu_tdx *to_tdx(struct kvm_vcpu *vcpu) return container_of(vcpu, struct vcpu_tdx, vcpu); }#else +static inline int tdx_module_setup (void ) {return -ENODEV ; }; +struct kvm_tdx ;struct vcpu_tdx ;
Signature · 1 line
2.25.1
RFC PATCH v5 10/104 KVM: TDX: Make TDX VM type supported
In reply to [email protected]From: Isaku Yamahata <[email protected]>
As first step TDX VM support, return that TDX VM type supported to device model, e.g. qemu. The callback to create guest TD is vm_init callback for KVM_CREATE_VM. Add a place holder function and call a function to initialize TDX module on demand because in that callback VMX is enabled by hardware_enable callback (vmx_hardware_enable).
--- arch/x86/kvm/vmx/main.c | 24 ++++++++++++++++++++++-- arch/x86/kvm/vmx/tdx.c | 5 +++++ arch/x86/kvm/vmx/vmx.c | 5 ----- arch/x86/kvm/vmx/x86_ops.h | 3 ++- 4 files changed, 29 insertions(+), 8 deletions(-)
arch/x86/kvm/vmx/main.c +22 -2
@@ -5,6 +5,12 @@#include "vmx.h" #include "nested.h" #include "pmu.h" +#include "tdx.h" + +static bool vt_is_vm_type_supported (unsigned long type) +{ +return type ==KVM_X86_DEFAULT_VM ||tdx_is_vm_type_supported (type); +}static __init int vt_hardware_setup (void ) {
@@ -19,6 +25,20 @@ static __init int vt_hardware_setup(void)return 0 ; } +static int vt_vm_init (struct kvm *kvm) +{ +int ret; + +if (is_td (kvm)) { + ret =tdx_module_setup (); +if (ret) +return ret; +return -EOPNOTSUPP ;/* Not ready to create guest TD yet. */ + } + +return vmx_vm_init (kvm); +} +struct kvm_x86_ops vt_x86_ops__initdata = { .name ="kvm_intel" ,
@@ -29,9 +49,9 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .cpu_has_accelerated_tpr = report_flexpriority, .has_emulated_msr = vmx_has_emulated_msr, - .is_vm_type_supported = vmx_is_vm_type_supported, + .is_vm_type_supported = vt_is_vm_type_supported, .vm_size = sizeof(struct kvm_vmx), - .vm_init = vmx_vm_init, + .vm_init = vt_vm_init, .vcpu_create = vmx_vcpu_create, .vcpu_free = vmx_vcpu_free,
arch/x86/kvm/vmx/tdx.c +5 -0
@@ -105,6 +105,11 @@ int tdx_module_setup(void)return ret; } +bool tdx_is_vm_type_supported (unsigned long type) +{ +return type ==KVM_X86_TDX_VM &&READ_ONCE (enable_tdx); +} +static int __init __tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {u32 max_pa;
arch/x86/kvm/vmx/vmx.c +0 -5
@@ -7079,11 +7079,6 @@ int vmx_vcpu_create(struct kvm_vcpu *vcpu)return err; } -bool vmx_is_vm_type_supported (unsigned long type) -{ -return type ==KVM_X86_DEFAULT_VM ; -} -#define L1TF_MSG_SMT "L1TF CPU bug present and SMT on, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n"#define L1TF_MSG_L1D "L1TF CPU bug present and virtualization mitigation disabled, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n"
arch/x86/kvm/vmx/x86_ops.h +2 -1
@@ -25,7 +25,6 @@ void vmx_hardware_unsetup(void);int vmx_hardware_enable (void );void vmx_hardware_disable (void );bool report_flexpriority (void ); -bool vmx_is_vm_type_supported (unsigned long type);int vmx_vm_init (struct kvm *kvm);int vmx_vcpu_create (struct kvm_vcpu *vcpu);int vmx_vcpu_pre_run (struct kvm_vcpu *vcpu);
@@ -130,10 +129,12 @@ void vmx_setup_mce(struct kvm_vcpu *vcpu);#ifdef CONFIG_INTEL_TDX_HOST void __init tdx_pre_kvm_init (unsigned int *vcpu_size,unsigned int *vcpu_align,unsigned int *vm_size); +bool tdx_is_vm_type_supported (unsigned long type);void __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops);#else static inline void tdx_pre_kvm_init (unsigned int *vcpu_size,unsigned int *vcpu_align,unsigned int *vm_size) {} +static inline bool tdx_is_vm_type_supported (unsigned long type) {return false; }static inline void tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {}#endif
Signature · 1 line
2.25.1
RFC PATCH v5 11/104 The start of TDX KVM patch series: TDX architectural definitions
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of TDX architectural definitions.
--- .../virt/kvm/intel-tdx-layer-status.rst | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Documentation/virt/kvm/intel-tdx-layer-status.rst
Documentation/virt/kvm/intel-tdx-layer-status.rst new file +29 -0
@@ -0,0 +1,29 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=================================== +Intel Trust Dodmain Extensions(TDX) +=================================== + +Layer status +============ +What qemu can do +---------------- +- TDX VM TYPE is exposed to Qemu. +- Qemu can try to create VM of TDX VM type and then fails. + +Patch Layer status +------------------ + Patch layer Status +* TDX, VMX coexistence: Applied +* TDX architectural definitions: Applying +* TD VM creation/destruction: Not yet +* TD vcpu creation/destruction: Not yet +* TDX EPT violation: Not yet +* TD finalization: Not yet +* TD vcpu enter/exit: Not yet +* TD vcpu interrupts/exit/hypercall: Not yet + +* KVM MMU GPA stolen bits: Not yet +* KVM TDP refactoring for TDX: Not yet +* KVM TDP MMU hooks: Not yet +* KVM TDP MMU MapGPA: Not yet
Signature · 1 line
2.25.1
RFC PATCH v5 29/104 KVM: TDX: allocate/free TDX vcpu structure
In reply to [email protected]From: Isaku Yamahata <[email protected]>
The next step of TDX guest creation is to create vcpu. Allocate TDX vcpu structures, initialize it. Allocate pages of TDX vcpu for the TDX module.
In the case of the conventional case, cpuid is empty at the initialization. and cpuid is configured after the vcpu initialization. Because TDX supports only X2APIC mode, cpuid is forcibly initialized to support X2APIC on the vcpu initialization.
--- arch/x86/kvm/vmx/main.c | 30 +++++++- arch/x86/kvm/vmx/tdx.c | 142 +++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx_ops.h | 2 + arch/x86/kvm/vmx/x86_ops.h | 8 +++ arch/x86/kvm/x86.c | 3 +- arch/x86/kvm/x86.h | 1 + 6 files changed, 182 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx/main.c +27 -3
@@ -65,6 +65,30 @@ static int vt_mem_enc_op(struct kvm *kvm, void __user *argp)return tdx_vm_ioctl (kvm, argp); } +static int vt_vcpu_create (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_vcpu_create (vcpu); + +return vmx_vcpu_create (vcpu); +} + +static void vt_vcpu_free (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_vcpu_free (vcpu); + +return vmx_vcpu_free (vcpu); +} + +static void vt_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) +{ +if (is_td_vcpu (vcpu)) +return tdx_vcpu_reset (vcpu, init_event); + +return vmx_vcpu_reset (vcpu, init_event); +} +struct kvm_x86_ops vt_x86_ops__initdata = { .name ="kvm_intel" ,
@@ -81,9 +105,9 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .mmu_prezap = vt_mmu_prezap, .vm_free = vt_vm_free, - .vcpu_create = vmx_vcpu_create, - .vcpu_free = vmx_vcpu_free, - .vcpu_reset = vmx_vcpu_reset, + .vcpu_create = vt_vcpu_create, + .vcpu_free = vt_vcpu_free, + .vcpu_reset = vt_vcpu_reset, .prepare_guest_switch = vmx_prepare_switch_to_guest, .vcpu_load = vmx_vcpu_load,
arch/x86/kvm/vmx/tdx.c +142 -0
160 lines not on this page · raw message
arch/x86/kvm/vmx/tdx_ops.h +2 -0
@@ -41,6 +41,7 @@ static inline u64 tdh_mem_sept_add(hpa_t tdr, gpa_t gpa, int level, hpa_t page,static inline u64 tdh_vp_addcx (hpa_t tdvpr,hpa_t addr) { +tdx_clflush_page (addr);return kvm_seamcall (TDH_VP_ADDCX , addr, tdvpr,0 ,0 ,0 ,NULL ); }
@@ -69,6 +70,7 @@ static inline u64 tdh_mng_create(hpa_t tdr, int hkid)static inline u64 tdh_vp_create (hpa_t tdr,hpa_t tdvpr) { +tdx_clflush_page (tdvpr);return kvm_seamcall (TDH_VP_CREATE , tdvpr, tdr,0 ,0 ,0 ,NULL ); }
arch/x86/kvm/vmx/x86_ops.h +8 -0
@@ -137,6 +137,10 @@ int tdx_vm_init(struct kvm *kvm);void tdx_mmu_prezap (struct kvm *kvm);void tdx_vm_free (struct kvm *kvm); +int tdx_vcpu_create (struct kvm_vcpu *vcpu); +void tdx_vcpu_free (struct kvm_vcpu *vcpu); +void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event); +int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);#else static inline void tdx_pre_kvm_init(
@@ -149,6 +153,10 @@ static inline int tdx_vm_init(struct kvm *kvm) { return -EOPNOTSUPP; }static inline void tdx_mmu_prezap (struct kvm *kvm) {}static inline void tdx_vm_free (struct kvm *kvm) {} +static inline int tdx_vcpu_create (struct kvm_vcpu *vcpu) {return -EOPNOTSUPP ; } +static inline void tdx_vcpu_free (struct kvm_vcpu *vcpu) {} +static inline void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) {} +static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }#endif
arch/x86/kvm/x86.c +2 -1
@@ -2322,7 +2322,7 @@ static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)return 0 ; } -static int kvm_set_tsc_khz (struct kvm_vcpu *vcpu,u32 user_tsc_khz) +int kvm_set_tsc_khz (struct kvm_vcpu *vcpu,u32 user_tsc_khz) {u32 thresh_lo, thresh_hi;int use_scaling =0 ;
@@ -2354,6 +2354,7 @@ static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz) }return set_tsc_khz (vcpu, user_tsc_khz, use_scaling); } +EXPORT_SYMBOL_GPL (kvm_set_tsc_khz);static u64 compute_guest_tsc (struct kvm_vcpu *vcpu,s64 kernel_ns) {
arch/x86/kvm/x86.h +1 -0
@@ -303,6 +303,7 @@ extern int pi_inject_timer;extern bool report_ignored_msrs;extern unsigned long max_tsc_khz; +int kvm_set_tsc_khz (struct kvm_vcpu *vcpu,u32 user_tsc_khz);static inline u64 nsec_to_cycles (struct kvm_vcpu *vcpu,u64 nsec) {
Signature · 1 line
2.25.1
RFC PATCH v5 24/104 KVM: TDX: create/destroy VM structure
In reply to [email protected]From: Sean Christopherson <[email protected]>
As the first step to create TDX guest, create/destroy VM struct. Assign Host Key ID (HKID) to the TDX guest for memory encryption and allocate extra pages for the TDX guest. On destruction, free allocated pages, and HKID.
Add a second kvm_x86_ops hook in kvm_arch_vm_destroy() to support TDX's destruction path, which needs to first put the VM into a teardown state, then free per-vCPU resources, and finally free per-VM resources.
--- arch/x86/kvm/vmx/main.c | 16 +- arch/x86/kvm/vmx/tdx.c | 312 +++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 2 + arch/x86/kvm/vmx/tdx_errno.h | 2 +- arch/x86/kvm/vmx/tdx_ops.h | 8 + arch/x86/kvm/vmx/x86_ops.h | 8 + 6 files changed, 346 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/main.c +15 -1
@@ -39,12 +39,24 @@ static int vt_vm_init(struct kvm *kvm) ret =tdx_module_setup ();if (ret)return ret; -return -EOPNOTSUPP ;/* Not ready to create guest TD yet. */ +return tdx_vm_init (kvm); }return vmx_vm_init (kvm); } +static void vt_mmu_prezap (struct kvm *kvm) +{ +if (is_td (kvm)) +return tdx_mmu_prezap (kvm); +} + +static void vt_vm_free (struct kvm *kvm) +{ +if (is_td (kvm)) +return tdx_vm_free (kvm); +} +struct kvm_x86_ops vt_x86_ops__initdata = { .name ="kvm_intel" ,
@@ -58,6 +70,8 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .is_vm_type_supported = vt_is_vm_type_supported, .vm_size = sizeof(struct kvm_vmx), .vm_init = vt_vm_init, + .mmu_prezap = vt_mmu_prezap, + .vm_free = vt_vm_free, .vcpu_create = vmx_vcpu_create, .vcpu_free = vmx_vcpu_free,
arch/x86/kvm/vmx/tdx.c +312 -0
332 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.h +2 -0
@@ -19,6 +19,8 @@ struct kvm_tdx {struct tdx_td_page tdr;struct tdx_td_page *tdcs; + +int hkid; };struct vcpu_tdx {
arch/x86/kvm/vmx/tdx_errno.h +1 -1
@@ -12,11 +12,11 @@#define TDX_SUCCESS 0x0000000000000000ULL#define TDX_NON_RECOVERABLE_VCPU 0x4000000100000000ULL#define TDX_INTERRUPTED_RESUMABLE 0x8000000300000000ULL -#define TDX_LIFECYCLE_STATE_INCORRECT 0xC000060700000000ULL#define TDX_VCPU_NOT_ASSOCIATED 0x8000070200000000ULL#define TDX_KEY_GENERATION_FAILED 0x8000080000000000ULL#define TDX_KEY_STATE_INCORRECT 0xC000081100000000ULL#define TDX_KEY_CONFIGURED 0x0000081500000000ULL +#define TDX_NO_HKID_READY_TO_WBCACHE 0x0000082100000000ULL#define TDX_EPT_WALK_FAILED 0xC0000B0000000000ULL/*
arch/x86/kvm/vmx/tdx_ops.h +8 -0
@@ -6,6 +6,7 @@#include <linux/compiler.h> +#include <asm/cacheflush.h> #include <asm/asm.h> #include <asm/kvm_host.h>
@@ -15,8 +16,14 @@#ifdef CONFIG_INTEL_TDX_HOST +static inline void tdx_clflush_page (hpa_t addr) +{ +clflush_cache_range (__va (addr),PAGE_SIZE ); +} +static inline u64 tdh_mng_addcx (hpa_t tdr,hpa_t addr) { +tdx_clflush_page (addr);return kvm_seamcall (TDH_MNG_ADDCX , addr, tdr,0 ,0 ,0 ,NULL ); }
@@ -56,6 +63,7 @@ static inline u64 tdh_mng_key_config(hpa_t tdr)static inline u64 tdh_mng_create (hpa_t tdr,int hkid) { +tdx_clflush_page (tdr);return kvm_seamcall (TDH_MNG_CREATE , tdr, hkid,0 ,0 ,0 ,NULL ); }
arch/x86/kvm/vmx/x86_ops.h +8 -0
@@ -132,12 +132,20 @@ void __init tdx_pre_kvm_init(unsigned int *vcpu_size,bool tdx_is_vm_type_supported (unsigned long type);void __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops);void tdx_hardware_unsetup (void ); + +int tdx_vm_init (struct kvm *kvm); +void tdx_mmu_prezap (struct kvm *kvm); +void tdx_vm_free (struct kvm *kvm);#else static inline void tdx_pre_kvm_init (unsigned int *vcpu_size,unsigned int *vcpu_align,unsigned int *vm_size) {}static inline bool tdx_is_vm_type_supported (unsigned long type) {return false; }static inline void tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {}static inline void tdx_hardware_unsetup (void ) {} + +static inline int tdx_vm_init (struct kvm *kvm) {return -EOPNOTSUPP ; } +static inline void tdx_mmu_prezap (struct kvm *kvm) {} +static inline void tdx_vm_free (struct kvm *kvm) {}#endif #endif /* __KVM_X86_VMX_X86_OPS_H */
Signature · 1 line
2.25.1
RFC PATCH v5 23/104 x86/cpu: Add helper functions to allocate/free MKTME keyid
In reply to [email protected]From: Isaku Yamahata <[email protected]>
MKTME keyid is assigned to guest TD. The memory controller encrypts guest TD memory with key id. Add helper functions to allocate/free MKTME keyid so that TDX KVM assign keyid.
Also export MKTME global keyid that is used to encrypt TDX module and its memory.
--- arch/x86/include/asm/tdx.h | 6 ++++++ arch/x86/virt/vmx/tdx.c | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-)
arch/x86/include/asm/tdx.h +6 -0
@@ -139,6 +139,9 @@ int tdx_detect(void);int tdx_init (void );bool platform_has_tdx (void );const struct tdsysinfo_struct *tdx_get_sysinfo (void ); +u32 tdx_get_global_keyid (void ); +int tdx_keyid_alloc (void ); +void tdx_keyid_free (int keyid);#else static inline void tdx_detect_cpu (struct cpuinfo_x86 *c) { }static inline int tdx_detect (void ) {return -ENODEV ; }
@@ -146,6 +149,9 @@ static inline int tdx_init(void) { return -ENODEV; }static inline bool platform_has_tdx (void ) {return false; }struct tdsysinfo_struct ;static inline const struct tdsysinfo_struct *tdx_get_sysinfo (void ) {return NULL ; } +static inline u32 tdx_get_global_keyid (void ) {return 0 ; }; +static inline int tdx_keyid_alloc (void ) {return -EOPNOTSUPP ; } +static inline void tdx_keyid_free (int keyid) { }#endif /* CONFIG_INTEL_TDX_HOST */ #endif /* !__ASSEMBLY__ */
arch/x86/virt/vmx/tdx.c +32 -1
@@ -113,7 +113,13 @@ static int tdx_cmr_num;static struct tdsysinfo_struct tdx_sysinfo;/* TDX global KeyID to protect TDX metadata */ -static u32 tdx_global_keyid; +static u32 __read_mostly tdx_global_keyid; + +u32 tdx_get_global_keyid (void ) +{ +return tdx_global_keyid; +} +EXPORT_SYMBOL_GPL (tdx_get_global_keyid);static bool enable_tdx_host;
@@ -189,6 +195,31 @@ static void detect_seam(struct cpuinfo_x86 *c)detect_seam_ap (c); } +/* TDX KeyID pool */ +static DEFINE_IDA (tdx_keyid_pool); + +int tdx_keyid_alloc (void ) +{ +if (WARN_ON_ONCE (!tdx_keyid_start || !tdx_keyid_num)) +return -EINVAL ; + +/* The first keyID is reserved for the global key. */ +return ida_alloc_range (&tdx_keyid_pool, tdx_keyid_start +1 , + tdx_keyid_start + tdx_keyid_num -1 , +GFP_KERNEL ); +} +EXPORT_SYMBOL_GPL (tdx_keyid_alloc); + +void tdx_keyid_free (int keyid) +{ +/* keyid = 0 is reserved. */ +if (!keyid || keyid <=0 ) +return ; + +ida_free (&tdx_keyid_pool, keyid); +} +EXPORT_SYMBOL_GPL (tdx_keyid_free); +static void detect_tdx_keyids_bsp (struct cpuinfo_x86 *c) {u64 keyid_part;
Signature · 1 line
2.25.1
RFC PATCH v5 21/104 KVM: x86: Introduce hooks to free VM callback prezap and vm_free
In reply to [email protected]From: Kai Huang <[email protected]>
Before tearing down private page tables, TDX requires some resources of the guest TD to be destroyed (i.e. keyID must have been reclaimed, etc). Add prezap callback before tearing down private page tables for it.
TDX needs to free some resources after other resources (i.e. vcpu related resources). Add vm_free callback at the end of kvm_arch_destroy_vm().
--- arch/x86/include/asm/kvm-x86-ops.h | 2 ++ arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/x86.c | 8 ++++++++ 3 files changed, 12 insertions(+)
arch/x86/include/asm/kvm-x86-ops.h +2 -0
@@ -20,7 +20,9 @@ KVM_X86_OP(has_emulated_msr) KVM_X86_OP(vcpu_after_set_cpuid) KVM_X86_OP(is_vm_type_supported) KVM_X86_OP(vm_init) +KVM_X86_OP_NULL(mmu_prezap) KVM_X86_OP_NULL(vm_destroy) +KVM_X86_OP_NULL(vm_free) KVM_X86_OP(vcpu_create) KVM_X86_OP(vcpu_free) KVM_X86_OP(vcpu_reset)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1326,7 +1326,9 @@ struct kvm_x86_ops {bool (*is_vm_type_supported)(unsigned long vm_type);unsigned int vm_size;int (*vm_init)(struct kvm *kvm); +void (*mmu_prezap)(struct kvm *kvm);void (*vm_destroy)(struct kvm *kvm); +void (*vm_free)(struct kvm *kvm);/* Create, but do not attach this VCPU */ int (*vcpu_create)(struct kvm_vcpu *vcpu);
arch/x86/kvm/x86.c +8 -0
@@ -11779,6 +11779,7 @@ void kvm_arch_destroy_vm(struct kvm *kvm)kvm_page_track_cleanup (kvm);kvm_xen_destroy_vm (kvm);kvm_hv_destroy_vm (kvm); +static_call_cond (kvm_x86_vm_free)(kvm); }static void memslot_rmap_free (struct kvm_memory_slot *slot)
@@ -12036,6 +12037,13 @@ void kvm_arch_commit_memory_region(struct kvm *kvm,void kvm_arch_flush_shadow_all (struct kvm *kvm) { +/* +* kvm_mmu_zap_all() zaps both private and shared page tables. Before +* tearing down private page tables, TDX requires some TD resources to +* be destroyed (i.e. keyID must have been reclaimed, etc). Invoke +* kvm_x86_mmu_prezap() for this. +*/ +static_call_cond (kvm_x86_mmu_prezap)(kvm);kvm_mmu_zap_all (kvm); }
Signature · 1 line
2.25.1
RFC PATCH v5 12/104 KVM: TDX: Define TDX architectural definitions
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Define architectural definitions for KVM to issue the TDX SEAMCALLs.
Structures and values that are architecturally defined in the TDX module specifications the chapter of ABI Reference.
--- arch/x86/kvm/vmx/tdx_arch.h | 158 ++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 arch/x86/kvm/vmx/tdx_arch.h
arch/x86/kvm/vmx/tdx_arch.h new file +158 -0
@@ -0,0 +1,158 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* architectural constants/data definitions for TDX SEAMCALLs */ + +#ifndef __KVM_X86_TDX_ARCH_H +#define __KVM_X86_TDX_ARCH_H + +#include <linux/types.h> + +/* +* TDX SEAMCALL API function leaves +*/ +#define TDH_VP_ENTER 0 +#define TDH_MNG_ADDCX 1 +#define TDH_MEM_PAGE_ADD 2 +#define TDH_MEM_SEPT_ADD 3 +#define TDH_VP_ADDCX 4 +#define TDH_MEM_PAGE_AUG 6 +#define TDH_MEM_RANGE_BLOCK 7 +#define TDH_MNG_KEY_CONFIG 8 +#define TDH_MNG_CREATE 9 +#define TDH_VP_CREATE 10 +#define TDH_MNG_RD 11 +#define TDH_MR_EXTEND 16 +#define TDH_MR_FINALIZE 17 +#define TDH_VP_FLUSH 18 +#define TDH_MNG_VPFLUSHDONE 19 +#define TDH_MNG_KEY_FREEID 20 +#define TDH_MNG_INIT 21 +#define TDH_VP_INIT 22 +#define TDH_VP_RD 26 +#define TDH_MNG_KEY_RECLAIMID 27 +#define TDH_PHYMEM_PAGE_RECLAIM 28 +#define TDH_MEM_PAGE_REMOVE 29 +#define TDH_MEM_TRACK 38 +#define TDH_MEM_RANGE_UNBLOCK 39 +#define TDH_PHYMEM_CACHE_WB 40 +#define TDH_PHYMEM_PAGE_WBINVD 41 +#define TDH_VP_WR 43 +#define TDH_SYS_LP_SHUTDOWN 44 + +#define TDG_VP_VMCALL_GET_TD_VM_CALL_INFO 0x10000 +#define TDG_VP_VMCALL_MAP_GPA 0x10001 +#define TDG_VP_VMCALL_GET_QUOTE 0x10002 +#define TDG_VP_VMCALL_REPORT_FATAL_ERROR 0x10003 +#define TDG_VP_VMCALL_SETUP_EVENT_NOTIFY_INTERRUPT 0x10004 + +/* TDX control structure (TDR/TDCS/TDVPS) field access codes */ +#define TDX_NON_ARCH BIT_ULL(63) +#define TDX_CLASS_SHIFT 56 +#define TDX_FIELD_MASK GENMASK_ULL(31, 0) + +#define __BUILD_TDX_FIELD (non_arch, class, field) \ + (((non_arch) ? TDX_NON_ARCH : 0) | \ + ((u64)(class) << TDX_CLASS_SHIFT) | \ + ((u64)(field) & TDX_FIELD_MASK)) + +#define BUILD_TDX_FIELD (class, field) \ + __BUILD_TDX_FIELD(false, (class), (field)) + +#define BUILD_TDX_FIELD_NON_ARCH (class, field) \ + __BUILD_TDX_FIELD(true, (class), (field)) + + +/* @field is the VMCS field encoding */ +#define TDVPS_VMCS (field) BUILD_TDX_FIELD(0, (field)) + +enum tdx_guest_other_state { +TD_VCPU_STATE_DETAILS_NON_ARCH =0x100 , +}; + +union tdx_vcpu_state_details { +struct { +u64 vmxip :1 ; +u64 reserved :63 ; + }; +u64 full; +}; + +/* @field is any of enum tdx_guest_other_state */ +#define TDVPS_STATE (field) BUILD_TDX_FIELD(17, (field)) +#define TDVPS_STATE_NON_ARCH (field) BUILD_TDX_FIELD_NON_ARCH(17, (field)) + +/* Management class fields */ +enum tdx_guest_management { +TD_VCPU_PEND_NMI =11 , +}; + +/* @field is any of enum tdx_guest_management */ +#define TDVPS_MANAGEMENT (field) BUILD_TDX_FIELD(32, (field)) + +enum tdx_tdcs_execution_control { +TD_TDCS_EXEC_TSC_OFFSET =10 , +}; + +/* @field is any of enum tdx_tdcs_execution_control */ +#define TDCS_EXEC (field) BUILD_TDX_FIELD(17, (field)) + +#define TDX_EXTENDMR_CHUNKSIZE 256 + +struct tdx_cpuid_value { +u32 eax; +u32 ebx; +u32 ecx; +u32 edx; +} __packed; + +#define TDX_TD_ATTRIBUTE_DEBUG BIT_ULL(0) +#define TDX_TD_ATTRIBUTE_PKS BIT_ULL(30) +#define TDX_TD_ATTRIBUTE_KL BIT_ULL(31) +#define TDX_TD_ATTRIBUTE_PERFMON BIT_ULL(63) + +#define TDX_TD_XFAM_LBR BIT_ULL(15) +#define TDX_TD_XFAM_AMX (BIT_ULL(17) | BIT_ULL(18)) + +/* +* TD_PARAMS is provided as an input to TDH_MNG_INIT, the size of which is 1024B. +*/ +struct td_params { +u64 attributes; +u64 xfam; +u32 max_vcpus; +u32 reserved0; + +u64 eptp_controls; +u64 exec_controls; +u16 tsc_frequency; +u8 reserved1[38 ]; + +u64 mrconfigid[6 ]; +u64 mrowner[6 ]; +u64 mrownerconfig[6 ]; +u64 reserved2[4 ]; + +union { +struct tdx_cpuid_value cpuid_values[0 ]; +u8 reserved3[768 ]; + }; +} __packed__aligned(1024) ; + +/* +* Guest uses MAX_PA for GPAW when set. +* 0: GPA.SHARED bit is GPA[47] +* 1: GPA.SHARED bit is GPA[51] +*/ +#define TDX_EXEC_CONTROL_MAX_GPAW BIT_ULL(0) + +/* +* TDX requires the frequency to be defined in units of 25MHz, which is the +* frequency of the core crystal clock on TDX-capable platforms, i.e. the TDX +* module can only program frequencies that are multiples of 25MHz. The +* frequency must be between 100mhz and 10ghz (inclusive). +*/ +#define TDX_TSC_KHZ_TO_25MHZ (tsc_in_khz) ((tsc_in_khz) / (25 * 1000)) +#define TDX_TSC_25MHZ_TO_KHZ (tsc_in_25mhz) ((tsc_in_25mhz) * (25 * 1000)) +#define TDX_MIN_TSC_FREQUENCY_KHZ (100 * 1000) +#define TDX_MAX_TSC_FREQUENCY_KHZ (10 * 1000 * 1000) + +#endif /* __KVM_X86_TDX_ARCH_H */
Signature · 1 line
2.25.1
RFC PATCH v5 13/104 KVM: TDX: Add TDX "architectural" error codes
In reply to [email protected]From: Sean Christopherson <[email protected]>
Add error codes for the TDX SEAMCALLs both for TDX VMM side for TDH SEAMCALL and TDX guest side for TDG.VP.VMCALL. KVM issues the TDX SEAMCALLs and checks its error code. KVM handles hypercall from the TDX guest and may return an error. So error code for the TDX guest is also needed.
TDX SEAMCALL uses bits 31:0 to return more information, so these error codes will only exactly match RAX[63:32]. Error codes for TDG.VP.VMCALL is defined by TDX Guest-Host-Communication interface spec.
--- arch/x86/kvm/vmx/tdx_errno.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 arch/x86/kvm/vmx/tdx_errno.h
arch/x86/kvm/vmx/tdx_errno.h new file +29 -0
@@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* architectural status code for SEAMCALL */ + +#ifndef __KVM_X86_TDX_ERRNO_H +#define __KVM_X86_TDX_ERRNO_H + +#define TDX_SEAMCALL_STATUS_MASK 0xFFFFFFFF00000000ULL + +/* +* TDX SEAMCALL Status Codes (returned in RAX) +*/ +#define TDX_SUCCESS 0x0000000000000000ULL +#define TDX_NON_RECOVERABLE_VCPU 0x4000000100000000ULL +#define TDX_INTERRUPTED_RESUMABLE 0x8000000300000000ULL +#define TDX_LIFECYCLE_STATE_INCORRECT 0xC000060700000000ULL +#define TDX_VCPU_NOT_ASSOCIATED 0x8000070200000000ULL +#define TDX_KEY_GENERATION_FAILED 0x8000080000000000ULL +#define TDX_KEY_STATE_INCORRECT 0xC000081100000000ULL +#define TDX_KEY_CONFIGURED 0x0000081500000000ULL +#define TDX_EPT_WALK_FAILED 0xC0000B0000000000ULL + +/* +* TDG.VP.VMCALL Status Codes (returned in R10) +*/ +#define TDG_VP_VMCALL_SUCCESS 0x0000000000000000ULL +#define TDG_VP_VMCALL_INVALID_OPERAND 0x8000000000000000ULL +#define TDG_VP_VMCALL_TDREPORT_FAILED 0x8000000000000001ULL + +#endif /* __KVM_X86_TDX_ERRNO_H */
Signature · 1 line
2.25.1
RFC PATCH v5 15/104 KVM: TDX: add a helper function for KVM to issue SEAMCALL
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TODO: Consolidate seamcall helper function with TDX host/guest patch series. For now, this is kept to make this patch series compile/work.
A VMM interacts with the TDX module using a new instruction (SEAMCALL). A TDX VMM uses SEAMCALLs where a VMX VMM would have directly interacted with VMX instructions. For instance, a TDX VMM does not have full access to the VM control structure corresponding to VMX VMCS. Instead, a VMM induces the TDX module to act on behalf via SEAMCALLs.
Add a helper function for KVM C code to execute SEAMCALL instruction to hide its SEAMCALL ABI details. Although the x86 TDX host patch series defines a similar wrapper, the KVM TDX patch series defines its own because KVM TDX case is performance-critical, unlike the x86 TDX one that does one-time initialization. The difference is that the KVM TDX one is defined as a static inline function without an error check that is known to not happen so that compiler can optimize it better. The wrapper fiction in the x86 TDX host patch is defined as a function written in assembly code with error check so that it can detect errors that can occur only during the initialization.
--- arch/x86/kvm/vmx/seamcall.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 arch/x86/kvm/vmx/seamcall.h
arch/x86/kvm/vmx/seamcall.h new file +23 -0
@@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __KVM_VMX_SEAMCALL_H +#define __KVM_VMX_SEAMCALL_H + +#ifdef CONFIG_INTEL_TDX_HOST + +#ifdef __ASSEMBLY__ + +.macro seamcall + .byte 0x66, 0x0f, 0x01, 0xcf +.endm + +#else + +struct tdx_module_output ; +u64 kvm_seamcall (u64 op,u64 rcx,u64 rdx,u64 r8,u64 r9,u64 r10, +struct tdx_module_output *out); + +#endif /* !__ASSEMBLY__ */ + +#endif /* CONFIG_INTEL_TDX_HOST */ + +#endif /* __KVM_VMX_SEAMCALL_H */
Signature · 1 line
2.25.1
RFC PATCH v5 14/104 KVM: TDX: Add a function for KVM to invoke SEAMCALL
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Add an assembly function for KVM to call the TDX module because __seamcall defined in arch/x86/virt/vmx/seamcall.S doesn't fit for the KVM use case.
TDX module API returns extended error information in registers, rcx, rdx, r8, r9, r10, and r11 in addition to success case. KVM uses those extended error information in addition to the status code returned in RAX. Update the assembly code to optionally return those outputs even in the error case and define the specific version for KVM to call the TDX module.
SEAMCALL to the SEAM module (P-SEAMLDR or TDX module) can result in the error of VmFailInvalid indicated by CF=1 when VMX isn't enabled by VMXON instruction. Because KVM guarantees that VMX is enabled, VmFailInvalid error won't happen. Don't check the error for KVM.
--- arch/x86/kvm/Makefile | 2 +- arch/x86/kvm/vmx/seamcall.S | 55 +++++++++++++++++++++++++++++++++++++ arch/x86/virt/tdxcall.S | 8 ++++-- 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 arch/x86/kvm/vmx/seamcall.S
arch/x86/kvm/Makefile +1 -1
@@ -24,7 +24,7 @@ kvm-$(CONFIG_KVM_XEN) += xen.o kvm-intel-y += vmx/vmx.o vmx/vmenter.o vmx/pmu_intel.o vmx/vmcs12.o \ vmx/evmcs.o vmx/nested.o vmx/posted_intr.o vmx/main.o kvm-intel-$(CONFIG_X86_SGX_KVM) += vmx/sgx.o -kvm-intel-$(CONFIG_INTEL_TDX_HOST) += vmx/tdx.o +kvm-intel-$(CONFIG_INTEL_TDX_HOST) += vmx/tdx.o vmx/seamcall.o kvm-amd-y += svm/svm.o svm/vmenter.o svm/pmu.o svm/nested.o svm/avic.o svm/sev.o
arch/x86/kvm/vmx/seamcall.S new file +55 -0
55 lines not on this page · raw message
arch/x86/virt/tdxcall.S +6 -2
@@ -13,7 +13,7 @@ #define tdcall .byte 0x66,0x0f,0x01,0xcc #define seamcall .byte 0x66,0x0f,0x01,0xcf -.macro TDX_MODULE_CALL host:req +.macro TDX_MODULE_CALL host:req error_check=1 /* * R12 will be used as temporary storage for struct tdx_module_output * pointer. Since R12-R15 registers are not used by TDCALL/SEAMCALL
@@ -51,9 +51,11 @@ * * Set %rax to TDX_SEAMCALL_VMFAILINVALID for VMfailInvalid. * This value will never be used as actual SEAMCALL error code. - */ + */ + .if \error_check jnc .Lno_vmfailinvalid mov $TDX_SEAMCALL_VMFAILINVALID, %rax + .endif .Lno_vmfailinvalid: .else tdcall
@@ -66,8 +68,10 @@ pop %r12 /* Check for success: 0 - Successful, otherwise failed */ + .if \error_check test %rax, %rax jnz .Lno_output_struct + .endif /* * Since this function can be initiated without an output pointer,
Signature · 1 line
2.25.1
RFC PATCH v5 16/104 KVM: TDX: Add C wrapper functions for SEAMCALLs to the TDX module
In reply to [email protected]From: Sean Christopherson <[email protected]>
TDX SEAMCALL interface is defined in the TDX module specification. Define C wrapper functions for SEAMCALLs for readability.
--- arch/x86/kvm/vmx/tdx_ops.h | 161 +++++++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 arch/x86/kvm/vmx/tdx_ops.h
arch/x86/kvm/vmx/tdx_ops.h new file +161 -0
@@ -0,0 +1,161 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* constants/data definitions for TDX SEAMCALLs */ + +#ifndef __KVM_X86_TDX_OPS_H +#define __KVM_X86_TDX_OPS_H + +#include <linux/compiler.h> + +#include <asm/asm.h> +#include <asm/kvm_host.h> + +#include "tdx_errno.h" +#include "tdx_arch.h" +#include "seamcall.h" + +#ifdef CONFIG_INTEL_TDX_HOST + +static inline u64 tdh_mng_addcx (hpa_t tdr,hpa_t addr) +{ +return kvm_seamcall (TDH_MNG_ADDCX , addr, tdr,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_mem_page_add (hpa_t tdr,gpa_t gpa,hpa_t hpa,hpa_t source, +struct tdx_module_output *out) +{ +return kvm_seamcall (TDH_MEM_PAGE_ADD , gpa, tdr, hpa, source,0 , out); +} + +static inline u64 tdh_mem_sept_add (hpa_t tdr,gpa_t gpa,int level,hpa_t page, +struct tdx_module_output *out) +{ +return kvm_seamcall (TDH_MEM_SEPT_ADD , gpa | level, tdr, page,0 ,0 , out); +} + +static inline u64 tdh_vp_addcx (hpa_t tdvpr,hpa_t addr) +{ +return kvm_seamcall (TDH_VP_ADDCX , addr, tdvpr,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_mem_page_aug (hpa_t tdr,gpa_t gpa,hpa_t hpa, +struct tdx_module_output *out) +{ +return kvm_seamcall (TDH_MEM_PAGE_AUG , gpa, tdr, hpa,0 ,0 , out); +} + +static inline u64 tdh_mem_range_block (hpa_t tdr,gpa_t gpa,int level, +struct tdx_module_output *out) +{ +return kvm_seamcall (TDH_MEM_RANGE_BLOCK , gpa | level, tdr,0 ,0 ,0 , out); +} + +static inline u64 tdh_mng_key_config (hpa_t tdr) +{ +return kvm_seamcall (TDH_MNG_KEY_CONFIG , tdr,0 ,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_mng_create (hpa_t tdr,int hkid) +{ +return kvm_seamcall (TDH_MNG_CREATE , tdr, hkid,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_vp_create (hpa_t tdr,hpa_t tdvpr) +{ +return kvm_seamcall (TDH_VP_CREATE , tdvpr, tdr,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_mng_rd (hpa_t tdr,u64 field,struct tdx_module_output *out) +{ +return kvm_seamcall (TDH_MNG_RD , tdr, field,0 ,0 ,0 , out); +} + +static inline u64 tdh_mr_extend (hpa_t tdr,gpa_t gpa,struct tdx_module_output *out) +{ +return kvm_seamcall (TDH_MR_EXTEND , gpa, tdr,0 ,0 ,0 , out); +} + +static inline u64 tdh_mr_finalize (hpa_t tdr) +{ +return kvm_seamcall (TDH_MR_FINALIZE , tdr,0 ,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_vp_flush (hpa_t tdvpr) +{ +return kvm_seamcall (TDH_VP_FLUSH , tdvpr,0 ,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_mng_vpflushdone (hpa_t tdr) +{ +return kvm_seamcall (TDH_MNG_VPFLUSHDONE , tdr,0 ,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_mng_key_freeid (hpa_t tdr) +{ +return kvm_seamcall (TDH_MNG_KEY_FREEID , tdr,0 ,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_mng_init (hpa_t tdr,hpa_t td_params,struct tdx_module_output *out) +{ +return kvm_seamcall (TDH_MNG_INIT , tdr, td_params,0 ,0 ,0 , out); +} + +static inline u64 tdh_vp_init (hpa_t tdvpr,u64 rcx) +{ +return kvm_seamcall (TDH_VP_INIT , tdvpr, rcx,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_vp_rd (hpa_t tdvpr,u64 field,struct tdx_module_output *out) +{ +return kvm_seamcall (TDH_VP_RD , tdvpr, field,0 ,0 ,0 , out); +} + +static inline u64 tdh_mng_key_reclaimid (hpa_t tdr) +{ +return kvm_seamcall (TDH_MNG_KEY_RECLAIMID , tdr,0 ,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_phymem_page_reclaim (hpa_t page,struct tdx_module_output *out) +{ +return kvm_seamcall (TDH_PHYMEM_PAGE_RECLAIM , page,0 ,0 ,0 ,0 , out); +} + +static inline u64 tdh_mem_page_remove (hpa_t tdr,gpa_t gpa,int level, +struct tdx_module_output *out) +{ +return kvm_seamcall (TDH_MEM_PAGE_REMOVE , gpa | level, tdr,0 ,0 ,0 , out); +} + +static inline u64 tdh_sys_lp_shutdown (void ) +{ +return kvm_seamcall (TDH_SYS_LP_SHUTDOWN ,0 ,0 ,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_mem_track (hpa_t tdr) +{ +return kvm_seamcall (TDH_MEM_TRACK , tdr,0 ,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_mem_range_unblock (hpa_t tdr,gpa_t gpa,int level, +struct tdx_module_output *out) +{ +return kvm_seamcall (TDH_MEM_RANGE_UNBLOCK , gpa | level, tdr,0 ,0 ,0 , out); +} + +static inline u64 tdh_phymem_cache_wb (bool resume) +{ +return kvm_seamcall (TDH_PHYMEM_CACHE_WB , resume ?1 :0 ,0 ,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_phymem_page_wbinvd (hpa_t page) +{ +return kvm_seamcall (TDH_PHYMEM_PAGE_WBINVD , page,0 ,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_vp_wr (hpa_t tdvpr,u64 field,u64 val,u64 mask, +struct tdx_module_output *out) +{ +return kvm_seamcall (TDH_VP_WR , tdvpr, field, val, mask,0 , out); +} +#endif /* CONFIG_INTEL_TDX_HOST */ + +#endif /* __KVM_X86_TDX_OPS_H */
Signature · 1 line
2.25.1
RFC PATCH v5 17/104 KVM: TDX: Add helper functions to print TDX SEAMCALL error
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Add helper functions to print out errors from the TDX module in a uniform manner.
--- arch/x86/kvm/Makefile | 2 +- arch/x86/kvm/vmx/seamcall.h | 2 ++ arch/x86/kvm/vmx/tdx_error.c | 22 ++++++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 arch/x86/kvm/vmx/tdx_error.c
arch/x86/kvm/Makefile +1 -1
@@ -24,7 +24,7 @@ kvm-$(CONFIG_KVM_XEN) += xen.o kvm-intel-y += vmx/vmx.o vmx/vmenter.o vmx/pmu_intel.o vmx/vmcs12.o \ vmx/evmcs.o vmx/nested.o vmx/posted_intr.o vmx/main.o kvm-intel-$(CONFIG_X86_SGX_KVM) += vmx/sgx.o -kvm-intel-$(CONFIG_INTEL_TDX_HOST) += vmx/tdx.o vmx/seamcall.o +kvm-intel-$(CONFIG_INTEL_TDX_HOST) += vmx/tdx.o vmx/seamcall.o vmx/tdx_error.o kvm-amd-y += svm/svm.o svm/vmenter.o svm/pmu.o svm/nested.o svm/avic.o svm/sev.o
arch/x86/kvm/vmx/seamcall.h +2 -0
@@ -16,6 +16,8 @@ struct tdx_module_output;u64 kvm_seamcall (u64 op,u64 rcx,u64 rdx,u64 r8,u64 r9,u64 r10,struct tdx_module_output *out); +void pr_tdx_error (u64 op,u64 error_code,const struct tdx_module_output *out); +#endif /* !__ASSEMBLY__ */ #endif /* CONFIG_INTEL_TDX_HOST */
arch/x86/kvm/vmx/tdx_error.c new file +22 -0
@@ -0,0 +1,22 @@ +// SPDX-License-Identifier: GPL-2.0 +/* functions to record TDX SEAMCALL error */ + +#include <linux/kernel.h> +#include <linux/bug.h> + +#include "tdx_ops.h" + +void pr_tdx_error (u64 op,u64 error_code,const struct tdx_module_output *out) +{ +if (!out) { +pr_err_ratelimited ("SEAMCALL[%lld] failed: 0x%llx\n" , + op, error_code); +return ; + } + +pr_err_ratelimited ( +"SEAMCALL[%lld] failed: 0x%llx " +"RCX 0x%llx, RDX 0x%llx, R8 0x%llx, R9 0x%llx, R10 0x%llx, R11 0x%llx\n" , + op, error_code, + out->rcx, out->rdx, out->r8, out->r9, out->r10, out->r11); +}
Signature · 1 line
2.25.1
RFC PATCH v5 18/104 The start of TDX KVM patch series: TD VM creation/destruction
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of TD VM creation/destruction.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +2 -2
@@ -15,8 +15,8 @@ Patch Layer status ------------------ Patch layer Status * TDX, VMX coexistence: Applied -* TDX architectural definitions: Applying -* TD VM creation/destruction: Not yet +* TDX architectural definitions: Applied +* TD VM creation/destruction: Applying * TD vcpu creation/destruction: Not yet * TDX EPT violation: Not yet * TD finalization: Not yet
Signature · 1 line
2.25.1
RFC PATCH v5 20/104 KVM: TDX: allocate per-package mutex
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Several TDX SEAMCALLs are per-package scope (concretely per memory controller) and they need to be serialized per-package. Allocate mutex for it.
--- arch/x86/kvm/vmx/main.c | 8 +++++++- arch/x86/kvm/vmx/tdx.c | 18 ++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 2 ++ 3 files changed, 27 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/main.c +7 -1
@@ -25,6 +25,12 @@ static __init int vt_hardware_setup(void)return 0 ; } +static void vt_hardware_unsetup (void ) +{ +tdx_hardware_unsetup (); +vmx_hardware_unsetup (); +} +static int vt_vm_init (struct kvm *kvm) {int ret;
@@ -42,7 +48,7 @@ static int vt_vm_init(struct kvm *kvm)struct kvm_x86_ops vt_x86_ops__initdata = { .name ="kvm_intel" , - .hardware_unsetup = vmx_hardware_unsetup, + .hardware_unsetup = vt_hardware_unsetup, .hardware_enable = vmx_hardware_enable, .hardware_disable = vmx_hardware_disable,
arch/x86/kvm/vmx/tdx.c +18 -0
@@ -34,6 +34,8 @@ struct tdx_capabilities {/* Capabilities of KVM + the TDX module. */ struct tdx_capabilities tdx_caps; +static struct mutex *tdx_mng_key_config_lock; +static u64 hkid_mask__ro_after_init ;static u8 hkid_start_pos__ro_after_init ;
@@ -112,7 +114,9 @@ bool tdx_is_vm_type_supported(unsigned long type)static int __init __tdx_hardware_setup (struct kvm_x86_ops *x86_ops) { +int max_pkgs;u32 max_pa; +int i;if (!enable_ept) {pr_warn ("Cannot enable TDX with EPT disabled\n" );
@@ -127,6 +131,14 @@ static int __init __tdx_hardware_setup(struct kvm_x86_ops *x86_ops)if (WARN_ON_ONCE (x86_ops->tlb_remote_flush))return -EIO ; + max_pkgs =topology_max_packages (); + tdx_mng_key_config_lock =kcalloc (max_pkgs,sizeof (*tdx_mng_key_config_lock), +GFP_KERNEL ); +if (!tdx_mng_key_config_lock) +return -ENOMEM ; +for (i =0 ; i < max_pkgs; i++) +mutex_init (&tdx_mng_key_config_lock[i]); + max_pa =cpuid_eax (0x80000008 ) &0xff ; hkid_start_pos = boot_cpu_data.x86_phys_bits; hkid_mask =GENMASK_ULL (max_pa -1 , hkid_start_pos);
@@ -147,6 +159,12 @@ void __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops) enable_tdx = false; } +void tdx_hardware_unsetup (void ) +{ +/* kfree accepts NULL. */ +kfree (tdx_mng_key_config_lock); +} +void __init tdx_pre_kvm_init (unsigned int *vcpu_size,unsigned int *vcpu_align,unsigned int *vm_size) {
arch/x86/kvm/vmx/x86_ops.h +2 -0
@@ -131,11 +131,13 @@ void __init tdx_pre_kvm_init(unsigned int *vcpu_size, unsigned int *vcpu_align, unsigned int *vm_size);bool tdx_is_vm_type_supported (unsigned long type);void __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops); +void tdx_hardware_unsetup (void );#else static inline void tdx_pre_kvm_init (unsigned int *vcpu_size,unsigned int *vcpu_align,unsigned int *vm_size) {}static inline bool tdx_is_vm_type_supported (unsigned long type) {return false; }static inline void tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {} +static inline void tdx_hardware_unsetup (void ) {}#endif #endif /* __KVM_X86_VMX_X86_OPS_H */
Signature · 1 line
2.25.1
RFC PATCH v5 26/104 KVM: TDX: x86: Add vm ioctl to get TDX systemwide parameters
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Implement a VM-scoped subcomment to get system-wide parameters. Although this is system-wide parameters not per-VM, this subcomand is VM-scoped because
- Device model needs TDX system-wide parameters after creating KVM VM.
- This subcommands requires to initialize TDX module. For lazy initialization of the TDX module, vm-scope ioctl is better.
--- arch/x86/include/uapi/asm/kvm.h | 22 ++++++++++++++ arch/x86/kvm/vmx/tdx.c | 41 +++++++++++++++++++++++++++ tools/arch/x86/include/uapi/asm/kvm.h | 22 ++++++++++++++ 3 files changed, 85 insertions(+)
arch/x86/include/uapi/asm/kvm.h +22 -0
@@ -530,6 +530,8 @@ struct kvm_pmu_event_filter {/* Trust Domain eXtension sub-ioctl() commands. */ enum kvm_tdx_cmd_id { +KVM_TDX_CAPABILITIES =0 , +KVM_TDX_CMD_NR_MAX , };
@@ -539,4 +541,24 @@ struct kvm_tdx_cmd { __u64 data; }; +struct kvm_tdx_cpuid_config { +__u32 leaf; +__u32 sub_leaf; +__u32 eax; +__u32 ebx; +__u32 ecx; +__u32 edx; +}; + +struct kvm_tdx_capabilities { +__u64 attrs_fixed0; +__u64 attrs_fixed1; +__u64 xfam_fixed0; +__u64 xfam_fixed1; + +__u32 nr_cpuid_configs; +__u32 padding; +struct kvm_tdx_cpuid_config cpuid_configs[0 ]; +}; +#endif /* _ASM_X86_KVM_H */
arch/x86/kvm/vmx/tdx.c +41 -0
53 lines not on this page · raw message
tools/arch/x86/include/uapi/asm/kvm.h +22 -0
@@ -530,6 +530,8 @@ struct kvm_pmu_event_filter {/* Trust Domain eXtension sub-ioctl() commands. */ enum kvm_tdx_cmd_id { +KVM_TDX_CAPABILITIES =0 , +KVM_TDX_CMD_NR_MAX , };
@@ -539,4 +541,24 @@ struct kvm_tdx_cmd { __u64 data; }; +struct kvm_tdx_cpuid_config { +__u32 leaf; +__u32 sub_leaf; +__u32 eax; +__u32 ebx; +__u32 ecx; +__u32 edx; +}; + +struct kvm_tdx_capabilities { +__u64 attrs_fixed0; +__u64 attrs_fixed1; +__u64 xfam_fixed0; +__u64 xfam_fixed1; + +__u32 nr_cpuid_configs; +__u32 padding; +struct kvm_tdx_cpuid_config cpuid_configs[0 ]; +}; +#endif /* _ASM_X86_KVM_H */
Signature · 1 line
2.25.1
RFC PATCH v5 27/104 KVM: TDX: initialize VM with TDX specific parameters
In reply to [email protected]From: Xiaoyao Li <[email protected]>
TDX requires additional parameters for TDX VM for confidential execution to protect its confidentiality of its memory contents and its CPU state from any other software, including VMM. When creating guest TD VM before creating vcpu, the number of vcpu, TSC frequency (that is same among vcpus. and it can't be changed.) CPUIDs which is emulated by the TDX module. It means guest can trust those CPUIDs. and sha384 values for measurement.
Add new subcommand, KVM_TDX_INIT_VM, to pass parameters for TDX guest. It assigns encryption key to the TDX guest for memory encryption. TDX encrypts memory per-guest bases. It assigns Device model passes per-VM parameters for the TDX guest. The maximum number of vcpus, tsc frequency (TDX guest has fised VM-wide TSC frequency. not per-vcpu. The TDX guest can not change it.), attributes (production or debug), available extended features (which is reflected into guest XCR0, IA32_XSS MSR), cpuids, sha384 measurements, and etc.
--- arch/x86/include/asm/kvm_host.h | 2 + arch/x86/include/uapi/asm/kvm.h | 12 ++ arch/x86/kvm/vmx/tdx.c | 200 ++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 26 ++++ arch/x86/kvm/x86.c | 3 +- arch/x86/kvm/x86.h | 2 + tools/arch/x86/include/uapi/asm/kvm.h | 12 ++ 7 files changed, 256 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1234,6 +1234,8 @@ struct kvm_arch {hpa_t hv_root_tdp;spinlock_t hv_root_tdp_lock;#endif + +gfn_t gfn_shared_mask; };struct kvm_vm_stat {
arch/x86/include/uapi/asm/kvm.h +12 -0
@@ -531,6 +531,7 @@ struct kvm_pmu_event_filter {/* Trust Domain eXtension sub-ioctl() commands. */ enum kvm_tdx_cmd_id {KVM_TDX_CAPABILITIES =0 , +KVM_TDX_INIT_VM ,KVM_TDX_CMD_NR_MAX , };
@@ -561,4 +562,15 @@ struct kvm_tdx_capabilities { struct kvm_tdx_cpuid_config cpuid_configs[0]; }; +struct kvm_tdx_init_vm { +__u32 max_vcpus; +__u32 tsc_khz; +__u64 attributes; +__u64 cpuid; +__u64 mrconfigid[6 ];/* sha384 digest */ +__u64 mrowner[6 ];/* sha384 digest */ +__u64 mrownerconfig[6 ];/* sha348 digest */ +__u64 reserved[43 ];/* must be zero for future extensibility */ +}; +#endif /* _ASM_X86_KVM_H */
arch/x86/kvm/vmx/tdx.c +200 -0
212 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.h +26 -0
@@ -20,7 +20,15 @@ struct kvm_tdx {struct tdx_td_page tdr;struct tdx_td_page *tdcs; +u64 attributes; +u64 xfam;int hkid; + +int cpuid_nent; +struct kvm_cpuid_entry2 cpuid_entries[KVM_MAX_CPUID_ENTRIES ]; + +u64 tsc_offset; +unsigned long tsc_khz; };struct vcpu_tdx {
@@ -50,6 +58,11 @@ static inline struct vcpu_tdx *to_tdx(struct kvm_vcpu *vcpu) return container_of(vcpu, struct vcpu_tdx, vcpu); } +static inline bool is_td_initialized (struct kvm *kvm) +{ +return !!kvm->max_vcpus; +} + static __always_inline void tdvps_vmcs_check(u32 field, u8 bits) { BUILD_BUG_ON_MSG(__builtin_constant_p(field) && (field) & 0x1,
@@ -135,6 +148,19 @@ TDX_BUILD_TDVPS_ACCESSORS(64, VMCS, vmcs);TDX_BUILD_TDVPS_ACCESSORS (64 ,STATE_NON_ARCH , state_non_arch);TDX_BUILD_TDVPS_ACCESSORS (8 ,MANAGEMENT , management); +static __always_inline u64 td_tdcs_exec_read64(struct kvm_tdx *kvm_tdx, u32 field) +{ +struct tdx_module_output out; +u64 err; + + err =tdh_mng_rd (kvm_tdx->tdr.pa,TDCS_EXEC (field), &out); +if (unlikely (err)) { +pr_err ("TDH_MNG_RD[EXEC.0x%x] failed: 0x%llx\n" , field, err); +return 0 ; + } +return out.r8; +} +#else static inline int tdx_module_setup (void ) {return -ENODEV ; };
arch/x86/kvm/x86.c +2 -1
@@ -2274,7 +2274,8 @@ static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0);#endif static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz); -static unsigned long max_tsc_khz; +unsigned long max_tsc_khz; +EXPORT_SYMBOL_GPL (max_tsc_khz);static u32 adjust_tsc_khz (u32 khz,s32 ppm) {
arch/x86/kvm/x86.h +2 -0
@@ -302,6 +302,8 @@ extern int pi_inject_timer;extern bool report_ignored_msrs; +extern unsigned long max_tsc_khz; +static inline u64 nsec_to_cycles (struct kvm_vcpu *vcpu,u64 nsec) { return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult,
tools/arch/x86/include/uapi/asm/kvm.h +12 -0
@@ -531,6 +531,7 @@ struct kvm_pmu_event_filter {/* Trust Domain eXtension sub-ioctl() commands. */ enum kvm_tdx_cmd_id {KVM_TDX_CAPABILITIES =0 , +KVM_TDX_INIT_VM ,KVM_TDX_CMD_NR_MAX , };
@@ -561,4 +562,15 @@ struct kvm_tdx_capabilities { struct kvm_tdx_cpuid_config cpuid_configs[0]; }; +struct kvm_tdx_init_vm { +__u32 max_vcpus; +__u32 tsc_khz; +__u64 attributes; +__u64 cpuid; +__u64 mrconfigid[6 ];/* sha384 digest */ +__u64 mrowner[6 ];/* sha384 digest */ +__u64 mrownerconfig[6 ];/* sha348 digest */ +__u64 reserved[43 ];/* must be zero for future extensibility */ +}; +#endif /* _ASM_X86_KVM_H */
Signature · 1 line
2.25.1
RFC PATCH v5 41/104 KVM: VMX: Move setting of EPT MMU masks to common VT-x code
In reply to [email protected]From: Sean Christopherson <[email protected]>
EPT MMU masks are used commonly for VMX and TDX. The value needs to be initialized in common code before both VMX/TDX-specific initialization code.
--- arch/x86/kvm/vmx/main.c | 5 +++++ arch/x86/kvm/vmx/vmx.c | 4 ---- 2 files changed, 5 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx/main.c +5 -0
@@ -4,6 +4,7 @@#include "x86_ops.h" #include "vmx.h" #include "nested.h" +#include "mmu.h" #include "pmu.h" #include "tdx.h"
@@ -22,6 +23,10 @@ static __init int vt_hardware_setup(void)tdx_hardware_setup (&vt_x86_ops); +if (enable_ept) +kvm_mmu_set_ept_masks (enable_ept_ad_bits, +cpu_has_vmx_ept_execute_only ()); +return 0 ; }
arch/x86/kvm/vmx/vmx.c +0 -4
@@ -7817,10 +7817,6 @@ __init int vmx_hardware_setup(void)set_bit (0 , vmx_vpid_bitmap);/* 0 is reserved for host */ -if (enable_ept) -kvm_mmu_set_ept_masks (enable_ept_ad_bits, -cpu_has_vmx_ept_execute_only ()); -kvm_configure_mmu (enable_ept,0 ,vmx_get_max_tdp_level (),ept_caps_to_lpage_level (vmx_capability.ept));
Signature · 1 line
2.25.1
RFC PATCH v5 28/104 The start of TDX KVM patch series: TD vcpu creation/destruction
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of TD vcpu creation/destruction.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +3 -3
@@ -9,15 +9,15 @@ Layer status What qemu can do ---------------- - TDX VM TYPE is exposed to Qemu. -- Qemu can try to create VM of TDX VM type and then fails. +- Qemu can create/destroy guest of TDX vm type. Patch Layer status ------------------ Patch layer Status * TDX, VMX coexistence: Applied * TDX architectural definitions: Applied -* TD VM creation/destruction: Applying -* TD vcpu creation/destruction: Not yet +* TD VM creation/destruction: Applied +* TD vcpu creation/destruction: Applying * TDX EPT violation: Not yet * TD finalization: Not yet * TD vcpu enter/exit: Not yet
Signature · 1 line
2.25.1
RFC PATCH v5 30/104 KVM: TDX: Do TDX specific vcpu initialization
In reply to [email protected]From: Sean Christopherson <[email protected]>
TD guest vcpu need to be configured before ready to run which requests addtional information from Device model (e.g. qemu), one 64bit value is passed to vcpu's RCX as an initial value. Repurpose KVM_MEMORY_ENCRYPT_OP to vcpu-scope and add new sub-commands KVM_TDX_INIT_VCPU under it for such additional vcpu configuration.
Add callback for kvm vCPU-scoped operations of KVM_MEMORY_ENCRYPT_OP and add a new subcommand, KVM_TDX_INIT_VCPU, for further vcpu initialization.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/vmx/main.c | 25 +++++++++++++------- arch/x86/kvm/vmx/tdx.c | 34 +++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 4 ++++ arch/x86/kvm/vmx/x86_ops.h | 2 ++ arch/x86/kvm/x86.c | 6 +++++ tools/arch/x86/include/uapi/asm/kvm.h | 1 + 8 files changed, 66 insertions(+), 8 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1483,6 +1483,7 @@ struct kvm_x86_ops {void (*enable_smi_window)(struct kvm_vcpu *vcpu);int (*mem_enc_op)(struct kvm *kvm,void __user *argp); +int (*mem_enc_op_vcpu)(struct kvm_vcpu *vcpu,void __user *argp);int (*mem_enc_reg_region)(struct kvm *kvm,struct kvm_enc_region *argp);int (*mem_enc_unreg_region)(struct kvm *kvm,struct kvm_enc_region *argp);int (*vm_copy_enc_context_from)(struct kvm *kvm,unsigned int source_fd);
arch/x86/include/uapi/asm/kvm.h +1 -0
@@ -532,6 +532,7 @@ struct kvm_pmu_event_filter {enum kvm_tdx_cmd_id {KVM_TDX_CAPABILITIES =0 ,KVM_TDX_INIT_VM , +KVM_TDX_INIT_VCPU ,KVM_TDX_CMD_NR_MAX , };
arch/x86/kvm/vmx/main.c +17 -8
@@ -57,14 +57,6 @@ static void vt_vm_free(struct kvm *kvm)return tdx_vm_free (kvm); } -static int vt_mem_enc_op (struct kvm *kvm,void __user *argp) -{ -if (!is_td (kvm)) -return -ENOTTY ; - -return tdx_vm_ioctl (kvm, argp); -} -static int vt_vcpu_create (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))
@@ -89,6 +81,22 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)return vmx_vcpu_reset (vcpu, init_event); } +static int vt_mem_enc_op (struct kvm *kvm,void __user *argp) +{ +if (!is_td (kvm)) +return -ENOTTY ; + +return tdx_vm_ioctl (kvm, argp); +} + +static int vt_mem_enc_op_vcpu (struct kvm_vcpu *vcpu,void __user *argp) +{ +if (!is_td_vcpu (vcpu)) +return -EINVAL ; + +return tdx_vcpu_ioctl (vcpu, argp); +} +struct kvm_x86_ops vt_x86_ops__initdata = { .name ="kvm_intel" ,
@@ -229,6 +237,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_deliver_sipi_vector = kvm_vcpu_deliver_sipi_vector, .mem_enc_op = vt_mem_enc_op, + .mem_enc_op_vcpu = vt_mem_enc_op_vcpu, };struct kvm_x86_init_ops vt_init_ops__initdata = {
arch/x86/kvm/vmx/tdx.c +34 -0
@@ -73,6 +73,11 @@ static inline bool is_hkid_assigned(struct kvm_tdx *kvm_tdx)return kvm_tdx->hkid >0 ; } +static inline bool is_td_finalized (struct kvm_tdx *kvm_tdx) +{ +return kvm_tdx->finalized; +} +static void tdx_clear_page (unsigned long page) {const void *zero_page = (const void *)__va (page_to_phys (ZERO_PAGE (0 )));
@@ -756,6 +761,35 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp)return r; } +int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (vcpu->kvm); +struct vcpu_tdx *tdx =to_tdx (vcpu); +struct kvm_tdx_cmd cmd; +u64 err; + +if (tdx->initialized) +return -EINVAL ; + +if (!is_td_initialized (vcpu->kvm) ||is_td_finalized (kvm_tdx)) +return -EINVAL ; + +if (copy_from_user (&cmd, argp,sizeof (cmd))) +return -EFAULT ; + +if (cmd.metadata || cmd.id !=KVM_TDX_INIT_VCPU ) +return -EINVAL ; + + err =tdh_vp_init (tdx->tdvpr.pa, cmd.data); +if (WARN_ON_ONCE (err)) { +pr_tdx_error (TDH_VP_INIT , err,NULL ); +return -EIO ; + } + + tdx->initialized = true; +return 0 ; +} +static int __tdx_module_setup (void ) {const struct tdsysinfo_struct *tdsysinfo;
arch/x86/kvm/vmx/tdx.h +4 -0
@@ -27,6 +27,8 @@ struct kvm_tdx {int cpuid_nent;struct kvm_cpuid_entry2 cpuid_entries[KVM_MAX_CPUID_ENTRIES ]; +bool finalized; +u64 tsc_offset;unsigned long tsc_khz; };
@@ -36,6 +38,8 @@ struct vcpu_tdx {struct tdx_td_page tdvpr;struct tdx_td_page *tdvpx; + +bool initialized; };static inline bool is_td (struct kvm *kvm)
arch/x86/kvm/vmx/x86_ops.h +2 -0
@@ -142,6 +142,7 @@ void tdx_vcpu_free(struct kvm_vcpu *vcpu);void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp); +int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);#else static inline void tdx_pre_kvm_init (unsigned int *vcpu_size,unsigned int *vcpu_align,unsigned int *vm_size) {}
@@ -158,6 +159,7 @@ static inline void tdx_vcpu_free(struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) {}static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; } +static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }#endif #endif /* __KVM_X86_VMX_X86_OPS_H */
arch/x86/kvm/x86.c +6 -0
@@ -5680,6 +5680,12 @@ long kvm_arch_vcpu_ioctl(struct file *filp,case KVM_SET_DEVICE_ATTR : r =kvm_vcpu_ioctl_device_attr (vcpu, ioctl, argp);break ; +case KVM_MEMORY_ENCRYPT_OP : + r = -EINVAL ; +if (!kvm_x86_ops.mem_enc_op_vcpu) + goto out; + r = kvm_x86_ops.mem_enc_op_vcpu (vcpu, argp); +break ;default : r = -EINVAL ; }
tools/arch/x86/include/uapi/asm/kvm.h +1 -0
@@ -532,6 +532,7 @@ struct kvm_pmu_event_filter {enum kvm_tdx_cmd_id {KVM_TDX_CAPABILITIES =0 ,KVM_TDX_INIT_VM , +KVM_TDX_INIT_VCPU ,KVM_TDX_CMD_NR_MAX , };
Signature · 1 line
2.25.1
RFC PATCH v5 31/104 The start of TDX KVM patch series: KVM MMU GPA stolen bits
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of KVM MMU GPA stolen bits.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +3 -2
@@ -10,6 +10,7 @@ What qemu can do ---------------- - TDX VM TYPE is exposed to Qemu. - Qemu can create/destroy guest of TDX vm type. +- Qemu can create/destroy vcpu of TDX vm type. Patch Layer status ------------------
@@ -17,13 +18,13 @@ Patch Layer status * TDX, VMX coexistence: Applied * TDX architectural definitions: Applied * TD VM creation/destruction: Applied -* TD vcpu creation/destruction: Applying +* TD vcpu creation/destruction: Applied * TDX EPT violation: Not yet * TD finalization: Not yet * TD vcpu enter/exit: Not yet * TD vcpu interrupts/exit/hypercall: Not yet -* KVM MMU GPA stolen bits: Not yet +* KVM MMU GPA stolen bits: Applying * KVM TDP refactoring for TDX: Not yet * KVM TDP MMU hooks: Not yet * KVM TDP MMU MapGPA: Not yet
Signature · 1 line
2.25.1
RFC PATCH v5 25/104 KVM: TDX: Add place holder for TDX VM specific mem_enc_op ioctl
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Add a place holder function for TDX specific VM-scoped ioctl as mem_enc_op. TDX specific sub-commands will be added to retrieve/pass TDX specific parameters.
KVM_MEMORY_ENCRYPT_OP was introduced for VM-scoped operations specific for guest state-protected VM. It defined subcommands for technology-specific operations under KVM_MEMORY_ENCRYPT_OP. Despite its name, the subcommands are not limited to memory encryption, but various technology-specific operations are defined. It's natural to repurpose KVM_MEMORY_ENCRYPT_OP for TDX specific operations and define subcommands.
TDX requires VM-scoped, and VCPU-scoped TDX-specific operations for device model, for example, qemu. Getting system-wide parameters, TDX-specific VM initialization, and TDX-specific vCPU initialization. Which requires KVM vCPU-scoped operations in addition to the existing VM-scoped operations.
--- arch/x86/include/uapi/asm/kvm.h | 11 +++++++++++ arch/x86/kvm/vmx/main.c | 10 ++++++++++ arch/x86/kvm/vmx/tdx.c | 24 ++++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 4 ++++ tools/arch/x86/include/uapi/asm/kvm.h | 11 +++++++++++ 5 files changed, 60 insertions(+)
arch/x86/include/uapi/asm/kvm.h +11 -0
@@ -528,4 +528,15 @@ struct kvm_pmu_event_filter {#define KVM_X86_DEFAULT_VM 0#define KVM_X86_TDX_VM 1 +/* Trust Domain eXtension sub-ioctl() commands. */ +enum kvm_tdx_cmd_id { +KVM_TDX_CMD_NR_MAX , +}; + +struct kvm_tdx_cmd { +__u32 id; +__u32 metadata; +__u64 data; +}; +#endif /* _ASM_X86_KVM_H */
arch/x86/kvm/vmx/main.c +10 -0
@@ -57,6 +57,14 @@ static void vt_vm_free(struct kvm *kvm)return tdx_vm_free (kvm); } +static int vt_mem_enc_op (struct kvm *kvm,void __user *argp) +{ +if (!is_td (kvm)) +return -ENOTTY ; + +return tdx_vm_ioctl (kvm, argp); +} +struct kvm_x86_ops vt_x86_ops__initdata = { .name ="kvm_intel" ,
@@ -195,6 +203,8 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .complete_emulated_msr = kvm_complete_insn_gp, .vcpu_deliver_sipi_vector = kvm_vcpu_deliver_sipi_vector, + + .mem_enc_op = vt_mem_enc_op, };struct kvm_x86_init_ops vt_init_ops__initdata = {
arch/x86/kvm/vmx/tdx.c +24 -0
@@ -349,6 +349,30 @@ int tdx_vm_init(struct kvm *kvm)return ret; } +int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) +{ +struct kvm_tdx_cmd tdx_cmd; +int r; + +if (copy_from_user (&tdx_cmd, argp,sizeof (struct kvm_tdx_cmd ))) +return -EFAULT ; + +mutex_lock (&kvm->lock); + +switch (tdx_cmd.id) { +default : + r = -EINVAL ; + goto out; + } + +if (copy_to_user (argp, &tdx_cmd,sizeof (struct kvm_tdx_cmd ))) + r = -EFAULT ; + +out: +mutex_unlock (&kvm->lock); +return r; +} +static int __tdx_module_setup (void ) {const struct tdsysinfo_struct *tdsysinfo;
arch/x86/kvm/vmx/x86_ops.h +4 -0
@@ -136,6 +136,8 @@ void tdx_hardware_unsetup(void);int tdx_vm_init (struct kvm *kvm);void tdx_mmu_prezap (struct kvm *kvm);void tdx_vm_free (struct kvm *kvm); + +int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);#else static inline void tdx_pre_kvm_init (unsigned int *vcpu_size,unsigned int *vcpu_align,unsigned int *vm_size) {}
@@ -146,6 +148,8 @@ static inline void tdx_hardware_unsetup(void) {}static inline int tdx_vm_init (struct kvm *kvm) {return -EOPNOTSUPP ; }static inline void tdx_mmu_prezap (struct kvm *kvm) {}static inline void tdx_vm_free (struct kvm *kvm) {} + +static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }#endif #endif /* __KVM_X86_VMX_X86_OPS_H */
tools/arch/x86/include/uapi/asm/kvm.h +11 -0
@@ -528,4 +528,15 @@ struct kvm_pmu_event_filter {#define KVM_X86_DEFAULT_VM 0#define KVM_X86_TDX_VM 1 +/* Trust Domain eXtension sub-ioctl() commands. */ +enum kvm_tdx_cmd_id { +KVM_TDX_CMD_NR_MAX , +}; + +struct kvm_tdx_cmd { +__u32 id; +__u32 metadata; +__u64 data; +}; +#endif /* _ASM_X86_KVM_H */
Signature · 1 line
2.25.1
RFC PATCH v5 22/104 KVM: Add max_vcpus field in common 'struct kvm'
In reply to [email protected]From: Sean Christopherson <[email protected]>
For TDX guests, the maximum number of vcpus needs to be specified when the TDX guest VM is initialized (creating the TDX data corresponding to TDX guest) before creating vcpu. It needs to record the maximum number of vcpus on VM creation (KVM_CREATE_VM) and return error if the number of vcpus exceeds it
Because there is already max_vcpu member in arm64 struct kvm_arch, move it to common struct kvm and initialize it to KVM_MAX_VCPUS before kvm_arch_init_vm() instead of adding it to x86 struct kvm_arch.
--- arch/arm64/include/asm/kvm_host.h | 3 --- arch/arm64/kvm/arm.c | 6 +++--- arch/arm64/kvm/vgic/vgic-init.c | 6 +++--- include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 3 ++- 5 files changed, 9 insertions(+), 10 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -3
@@ -107,9 +107,6 @@ struct kvm_arch {/* VTCR_EL2 value for this VM */ u64 vtcr; -/* The maximum number of vCPUs depends on the used GIC model */ -int max_vcpus; -/* Interrupt controller */ struct vgic_dist vgic;
arch/arm64/kvm/arm.c +3 -3
@@ -153,7 +153,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)kvm_vgic_early_init (kvm);/* The maximum number of VCPUs is limited by the host's GIC model */ - kvm->arch.max_vcpus =kvm_arm_default_max_vcpus (); + kvm->max_vcpus =kvm_arm_default_max_vcpus ();set_default_spectre (kvm);
@@ -229,7 +229,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_MAX_VCPUS :case KVM_CAP_MAX_VCPU_ID :if (kvm) - r = kvm->arch.max_vcpus; + r = kvm->max_vcpus;else r =kvm_arm_default_max_vcpus ();break ;
@@ -305,7 +305,7 @@ int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)if (irqchip_in_kernel (kvm) &&vgic_initialized (kvm))return -EBUSY ; -if (id >= kvm->arch.max_vcpus) +if (id >= kvm->max_vcpus)return -EINVAL ;return 0 ;
arch/arm64/kvm/vgic/vgic-init.c +3 -3
@@ -98,11 +98,11 @@ int kvm_vgic_create(struct kvm *kvm, u32 type) ret =0 ;if (type ==KVM_DEV_TYPE_ARM_VGIC_V2 ) - kvm->arch.max_vcpus =VGIC_V2_MAX_CPUS ; + kvm->max_vcpus =VGIC_V2_MAX_CPUS ;else - kvm->arch.max_vcpus =VGIC_V3_MAX_CPUS ; + kvm->max_vcpus =VGIC_V3_MAX_CPUS ; -if (atomic_read (&kvm->online_vcpus) > kvm->arch.max_vcpus) { +if (atomic_read (&kvm->online_vcpus) > kvm->max_vcpus) { ret = -E2BIG ; goto out_unlock; }
include/linux/kvm_host.h +1 -0
@@ -715,6 +715,7 @@ struct kvm {* and is accessed atomically. */ atomic_t online_vcpus; +int max_vcpus;int created_vcpus;int last_boosted_vcpu;struct list_head vm_list;
virt/kvm/kvm_main.c +2 -1
@@ -1075,6 +1075,7 @@ static struct kvm *kvm_create_vm(unsigned long type)spin_lock_init (&kvm->gpc_lock);INIT_LIST_HEAD (&kvm->devices); + kvm->max_vcpus =KVM_MAX_VCPUS ;BUILD_BUG_ON (KVM_MEM_SLOTS_NUM >SHRT_MAX );
@@ -3718,7 +3719,7 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)return -EINVAL ;mutex_lock (&kvm->lock); -if (kvm->created_vcpus ==KVM_MAX_VCPUS ) { +if (kvm->created_vcpus >= kvm->max_vcpus) {mutex_unlock (&kvm->lock);return -EINVAL ; }
Signature · 1 line
2.25.1
RFC PATCH v5 32/104 KVM: x86/mmu: introduce config for PRIVATE KVM MMU
In reply to [email protected]From: Isaku Yamahata <[email protected]>
To Keep the case of non TDX intact, introduce a new config option for private KVM MMU support. At the moment, this is synonym for CONFIG_INTEL_TDX_HOST && CONFIG_KVM_INTEL. The new flag make it clear that the config is only for x86 KVM MMU.
--- arch/x86/kvm/Kconfig | 4 ++++ 1 file changed, 4 insertions(+)
arch/x86/kvm/Kconfig +4 -0
@@ -136,4 +136,8 @@ config KVM_MMU_AUDIT config KVM_EXTERNAL_WRITE_TRACKING bool +config KVM_MMU_PRIVATE + def_bool y + depends on INTEL_TDX_HOST && KVM_INTEL + endif # VIRTUALIZATION
Signature · 1 line
2.25.1
RFC PATCH v5 33/104 KVM: x86: Add infrastructure for stolen GPA bits
In reply to [email protected]From: Rick Edgecombe <[email protected]>
Add support in KVM's MMU for aliasing multiple GPAs (from a hardware perspective) to a single GPA (from a memslot perspective). GPA aliasing will be used to repurpose GPA bits as attribute bits, e.g. to expose an execute-only permission bit to the guest. To keep the implementation simple (relatively speaking), GPA aliasing is only supported via TDP.
Today KVM assumes two things that are broken by GPA aliasing.
1. GPAs coming from hardware can be simply shifted to get the GFNs.
2. GPA bits 51:MAXPHYADDR are reserved to zero.
With GPA aliasing, translating a GPA to GFN requires masking off the repurposed bit, and a repurposed bit may reside in 51:MAXPHYADDR.
To support GPA aliasing, introduce the concept of per-VM GPA stolen bits, that is, bits stolen from the GPA to act as new virtualized attribute bits. A bit in the mask will cause the MMU code to create aliases of the GPA. It can also be used to find the GFN out of a GPA coming from a tdp fault.
To handle case (1) from above, retain any stolen bits when passing a GPA in KVM's MMU code, but strip them when converting to a GFN so that the GFN contains only the "real" GFN, i.e. never has repurposed bits set.
GFNs (without stolen bits) continue to be used to:
- Specify physical memory by userspace via memslots
- Map GPAs to TDP PTEs via RMAP
- Specify dirty tracking and write protection
- Look up MTRR types
- Inject async page faults
Since there are now multiple aliases for the same aliased GPA, when userspace memory backing the memslots is paged out, both aliases need to be modified. Fortunately, this happens automatically. Since rmap supports multiple mappings for the same GFN for PTE shadowing based paging, by adding/removing each alias PTE with its GFN, kvm_handle_hva() based operations will be applied to both aliases.
In the case of the rmap being removed in the future, the needed information could be recovered by iterating over the stolen bits and walking the TDP page tables.
For TLB flushes that are address based, make sure to flush both aliases in the case of stolen bits.
Only support stolen bits in 64 bit guest paging modes (long, PAE). Features that use this infrastructure should restrict the stolen bits to exclude the other paging modes. Don't support stolen bits for shadow EPT.
--- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu.h | 51 +++++++++++++++++++++++++++++++++ arch/x86/kvm/mmu/mmu.c | 19 ++++++++++-- arch/x86/kvm/mmu/paging_tmpl.h | 25 +++++++++------- 4 files changed, 84 insertions(+), 13 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1235,7 +1235,9 @@ struct kvm_arch {spinlock_t hv_root_tdp_lock;#endif +#ifdef CONFIG_KVM_MMU_PRIVATE gfn_t gfn_shared_mask; +#endif };struct kvm_vm_stat {
arch/x86/kvm/mmu.h +51 -0
55 lines not on this page · raw message
arch/x86/kvm/mmu/mmu.c +16 -3
@@ -276,11 +276,24 @@ static inline bool kvm_available_flush_tlb_with_range(void)static void kvm_flush_remote_tlbs_with_range (struct kvm *kvm,struct kvm_tlb_range *range) { -int ret = -ENOTSUPP ; +int ret = -EOPNOTSUPP ; +u64 gfn_stolen_mask; -if (range && kvm_x86_ops.tlb_remote_flush_with_range) +/* +* Fall back to the big hammer flush if there is more than one +* GPA alias that needs to be flushed. +*/ + gfn_stolen_mask =kvm_gfn_stolen_mask (kvm); +if (hweight64 (gfn_stolen_mask) >1 ) + goto generic_flush; + +if (range &&kvm_available_flush_tlb_with_range ()) { +/* Callback should flush both private GFN and shared GFN. */ + range->start_gfn =kvm_gfn_unalias (kvm, range->start_gfn); ret =static_call (kvm_x86_tlb_remote_flush_with_range)(kvm, range); + } +generic_flush:if (ret)kvm_flush_remote_tlbs (kvm); }
@@ -4010,7 +4023,7 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *faultunsigned long mmu_seq;int r; - fault->gfn = fault->addr >>PAGE_SHIFT ; + fault->gfn =kvm_gfn_unalias (vcpu->kvm,gpa_to_gfn (fault->addr)); fault->slot =kvm_vcpu_gfn_to_memslot (vcpu, fault->gfn);if (page_fault_handle_page_track (vcpu, fault))
arch/x86/kvm/mmu/paging_tmpl.h +15 -10
@@ -25,7 +25,8 @@#define guest_walker guest_walker64#define FNAME (name) paging##64_##name#define PT_BASE_ADDR_MASK GUEST_PT64_BASE_ADDR_MASK -#define PT_LVL_ADDR_MASK (lvl) PT64_LVL_ADDR_MASK(lvl) +#define PT_LVL_ADDR_MASK (vcpu, lvl) (~kvm_gpa_stolen_mask(vcpu->kvm) & \ + PT64_LVL_ADDR_MASK(lvl))#define PT_LVL_OFFSET_MASK (lvl) PT64_LVL_OFFSET_MASK(lvl)#define PT_INDEX (addr, level) PT64_INDEX(addr, level)#define PT_LEVEL_BITS PT64_LEVEL_BITS
@@ -44,7 +45,7 @@#define guest_walker guest_walker32#define FNAME (name) paging##32_##name#define PT_BASE_ADDR_MASK PT32_BASE_ADDR_MASK -#define PT_LVL_ADDR_MASK (lvl) PT32_LVL_ADDR_MASK(lvl) +#define PT_LVL_ADDR_MASK (vcpu, lvl) PT32_LVL_ADDR_MASK(lvl)#define PT_LVL_OFFSET_MASK (lvl) PT32_LVL_OFFSET_MASK(lvl)#define PT_INDEX (addr, level) PT32_INDEX(addr, level)#define PT_LEVEL_BITS PT32_LEVEL_BITS
@@ -58,7 +59,7 @@#define guest_walker guest_walkerEPT#define FNAME (name) ept_##name#define PT_BASE_ADDR_MASK GUEST_PT64_BASE_ADDR_MASK -#define PT_LVL_ADDR_MASK (lvl) PT64_LVL_ADDR_MASK(lvl) +#define PT_LVL_ADDR_MASK (vcpu, lvl) PT64_LVL_ADDR_MASK(lvl)#define PT_LVL_OFFSET_MASK (lvl) PT64_LVL_OFFSET_MASK(lvl)#define PT_INDEX (addr, level) PT64_INDEX(addr, level)#define PT_LEVEL_BITS PT64_LEVEL_BITS
@@ -75,7 +76,7 @@#define PT_GUEST_ACCESSED_MASK (1 << PT_GUEST_ACCESSED_SHIFT)#define gpte_to_gfn_lvl FNAME(gpte_to_gfn_lvl) -#define gpte_to_gfn (pte) gpte_to_gfn_lvl((pte), PG_LEVEL_4K) +#define gpte_to_gfn (vcpu, pte) gpte_to_gfn_lvl(vcpu, pte, PG_LEVEL_4K)/* * The guest_walker structure emulates the behavior of the hardware page
@@ -96,9 +97,9 @@ struct guest_walker {struct x86_exception fault; }; -static gfn_t gpte_to_gfn_lvl (pt_element_t gpte,int lvl) +static gfn_t gpte_to_gfn_lvl (struct kvm_vcpu *vcpu,pt_element_t gpte,int lvl) { -return (gpte &PT_LVL_ADDR_MASK (lvl)) >>PAGE_SHIFT ; +return (gpte &PT_LVL_ADDR_MASK (vcpu, lvl)) >>PAGE_SHIFT ; } static inline void FNAME(protect_clean_gpte)(struct kvm_mmu *mmu, unsigned *access,
@@ -395,7 +396,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker, --walker->level; index =PT_INDEX (addr, walker->level); - table_gfn =gpte_to_gfn (pte); + table_gfn =gpte_to_gfn (vcpu, pte); offset = index *sizeof (pt_element_t); pte_gpa =gfn_to_gpa (table_gfn) + offset;
@@ -460,7 +461,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,if (unlikely (errcode)) goto error; - gfn =gpte_to_gfn_lvl (pte, walker->level); + gfn =gpte_to_gfn_lvl (vcpu, pte, walker->level); gfn += (addr &PT_LVL_OFFSET_MASK (walker->level)) >>PAGE_SHIFT ;if (PTTYPE ==32 && walker->level >PG_LEVEL_4K &&is_cpuid_PSE36 ())
@@ -555,12 +556,14 @@ FNAME(prefetch_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,gfn_t gfn;kvm_pfn_t pfn; +WARN_ON (gpte &kvm_gpa_stolen_mask (vcpu->kvm)); +if (FNAME (prefetch_invalid_gpte)(vcpu, sp, spte, gpte))return false;pgprintk ("%s: gpte %llx spte %p\n" , __func__, (u64 )gpte, spte); - gfn =gpte_to_gfn (gpte); + gfn =gpte_to_gfn (vcpu, gpte); pte_access = sp->role.access &FNAME (gpte_access)(gpte);FNAME (protect_clean_gpte)(vcpu->arch.mmu, &pte_access, gpte);
@@ -656,6 +659,8 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault,WARN_ON_ONCE (gw->gfn != base_gfn); direct_access = gw->pte_access; +WARN_ON (fault->addr &kvm_gpa_stolen_mask (vcpu->kvm)); + top_level = vcpu->arch.mmu->root_level;if (top_level ==PT32E_ROOT_LEVEL ) top_level =PT32_ROOT_LEVEL ;
@@ -1080,7 +1085,7 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)continue ; } - gfn =gpte_to_gfn (gpte); + gfn =gpte_to_gfn (vcpu, gpte); pte_access = sp->role.access; pte_access &=FNAME (gpte_access)(gpte);FNAME (protect_clean_gpte)(vcpu->arch.mmu, &pte_access, gpte);
Signature · 1 line
2.25.1
RFC PATCH v5 34/104 The start of TDX KVM patch series: KVM TDP refactoring for TDX
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of KVM TDP refactoring for TDX.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +2 -2
@@ -24,7 +24,7 @@ Patch Layer status * TD vcpu enter/exit: Not yet * TD vcpu interrupts/exit/hypercall: Not yet -* KVM MMU GPA stolen bits: Applying -* KVM TDP refactoring for TDX: Not yet +* KVM MMU GPA stolen bits: Applied +* KVM TDP refactoring for TDX: Applying * KVM TDP MMU hooks: Not yet * KVM TDP MMU MapGPA: Not yet
Signature · 1 line
2.25.1
RFC PATCH v5 38/104 KVM: x86/mmu: Allow per-VM override of the TDP max page level
In reply to [email protected]From: Sean Christopherson <[email protected]>
In the existing x86 KVM MMU code, there is already max_level member in struct kvm_page_fault with KVM_MAX_HUGEPAGE_LEVEL initial value. The KVM page fault handler denies page size larger than max_level.
Add per-VM member to indicate the allowed maximum page size with KVM_MAX_HUGEPAGE_LEVEL as default value and initialize max_level in struct kvm_page_fault with it.
For the guest TD, the set per-VM value for allows maximum page size to 4K page size. Then only allowed page size is 4K. It means large page is disabled.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu.h | 2 +- arch/x86/kvm/mmu/mmu.c | 2 ++ 3 files changed, 4 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1053,6 +1053,7 @@ struct kvm_arch {unsigned long n_requested_mmu_pages;unsigned long n_max_mmu_pages;unsigned int indirect_shadow_pages; +int tdp_max_page_level;u8 mmu_valid_gen;struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES ];struct list_head active_mmu_pages;
arch/x86/kvm/mmu.h +1 -1
@@ -192,7 +192,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .is_tdp = likely(vcpu->arch.mmu->page_fault == kvm_tdp_page_fault), .nx_huge_page_workaround_enabled = is_nx_huge_page_enabled(), - .max_level = KVM_MAX_HUGEPAGE_LEVEL, + .max_level = vcpu->kvm->arch.tdp_max_page_level, .req_level = PG_LEVEL_4K, .goal_level = PG_LEVEL_4K, };
arch/x86/kvm/mmu/mmu.c +2 -0
@@ -5782,6 +5782,8 @@ void kvm_mmu_init_vm(struct kvm *kvm) node->track_write = kvm_mmu_pte_write; node->track_flush_slot = kvm_mmu_invalidate_zap_pages_in_memslot;kvm_page_track_register_notifier (kvm, node); + + kvm->arch.tdp_max_page_level =KVM_MAX_HUGEPAGE_LEVEL ; }void kvm_mmu_uninit_vm (struct kvm *kvm)
Signature · 1 line
2.25.1
RFC PATCH v5 39/104 KVM: x86/mmu: Disallow fast page fault on private GPA
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDX requires TDX SEAMCALL to operate Secure EPT instead of direct memory access and TDX SEAMCALL is heavy operation. Fast page fault on private GPA doesn't make sense. Disallow fast page fault on private GPA.
--- arch/x86/kvm/mmu/mmu.c | 7 +++++++ 1 file changed, 7 insertions(+)
arch/x86/kvm/mmu/mmu.c +7 -0
@@ -3185,6 +3185,13 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)u64 *sptep =NULL ;uint retry_count =0 ; +/* +* TDX private mapping doesn't support fast page fault because the EPT +* entry needs TDX SEAMCALL. not direct memory access. +*/ +if (kvm_is_private_gpa (vcpu->kvm, fault->addr)) +return ret; +if (!page_fault_can_be_fast (fault))return ret;
Signature · 1 line
2.25.1
RFC PATCH v5 42/104 KVM: x86/mmu: Track shadow MMIO value/mask on a per-VM basis
In reply to [email protected]From: Sean Christopherson <[email protected]>
Define the EPT Violation #VE control bit, #VE info VMCS fields, and the suppress #VE bit for EPT entries.
TDX will use a different shadow PTE entry value for MMIO from VMX. Add members to kvm_arch and track value for MMIO per-VM. By using per-VM EPT entry value for MMIO, the existing VMX logic is kept working.
In the case of VMX VM case, the EPT entry for MMIO is non-present PTE (present bit cleared) without backing guest physical address (on EPT violation, KVM seaching backing guest memory and it finds there is no backing guest page.) or the value to trigger EPT misconfiguration. For fast path. Once MMIO is triggered on the EPT entry, the EPT entry is updated for the future MMIO. It allows KVM to understand the memory access is for MMIO without searching backing guest pages.). And then KVM parses guest instruction to figure out address/value/width for MMIO.
In the case of the guest TD, the guest memory is protected so that VMM can't parse guest instruction to trigger EPT violation. Instead VMM sets up (Shared) EPT to trigger #VE. When the guest TD issues MMIO, #VE is injected. guest VE handler converts MMIO access into MMIO hypercall to pass address/value/width for MMIO to VMM. (or directly paravirtualize MMIO into hypercall.) Then VMM can handle the MMIO hypercall without parsing guest instruction.
When the guest accesses GPA if "the EPT Violation #VE" control bit is set and EPT SUPPRESS VE bit in EPT entry is cleared, #VE, virtualization exception, is injected into the guest. Because the TDX guest vCPU state and memory are protected, a VMM can't emulate MMIO by the TDX guest on EPT violation by snooping vCPU state and parsing instruction to figure out MMIO address and value. Instead, PV MMIO (MMIO hypercall) is adapted. On EPT violation, CPU injects #VE to guest and the guest converts MMIO instruction into PV MMIO. Or guest directly issues MMIO hypercall.
The existing VMX code uses zero as an initial value for EPT entry. TDX will enable EPT-violation #VE VM-execution control and requires suppress VE bit cleared in shared EPT entry to inject #VE into the TDX guest. To keep the same behavior for VMX, suppress VE bit needs to be set. Allow to specify an initial value for EPT entry and if TDX is enabled, set initial EPT entry value to suppress VE bit set. EPT-violation #VE VM-execution control will be enabled, and For TDX shared EPT suppress VE bit will be cleared for TDX shared EPT entry.
--- arch/x86/include/asm/kvm_host.h | 3 +++ arch/x86/include/asm/vmx.h | 1 + arch/x86/kvm/mmu.h | 6 ++++-- arch/x86/kvm/mmu/mmu.c | 19 +++++++++++------ arch/x86/kvm/mmu/spte.c | 38 ++++++++++++++++----------------- arch/x86/kvm/mmu/spte.h | 9 ++++---- arch/x86/kvm/mmu/tdp_mmu.c | 6 +++--- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/vmx/main.c | 7 ++++-- arch/x86/kvm/vmx/tdx.c | 2 +- arch/x86/kvm/vmx/tdx.h | 2 ++ arch/x86/kvm/vmx/vmx.c | 8 +++++++ 12 files changed, 63 insertions(+), 40 deletions(-)
arch/x86/include/asm/kvm_host.h +3 -0
@@ -1069,6 +1069,9 @@ struct kvm_arch {*/ spinlock_t mmu_unsync_pages_lock; +u64 shadow_mmio_value; +u64 shadow_mmio_mask; +struct list_head assigned_dev_head;struct iommu_domain *iommu_domain;bool iommu_noncoherent;
arch/x86/include/asm/vmx.h +1 -0
@@ -498,6 +498,7 @@ enum vmcs_field {#define VMX_EPT_IPAT_BIT (1ull << 6)#define VMX_EPT_ACCESS_BIT (1ull << 8)#define VMX_EPT_DIRTY_BIT (1ull << 9) +#define VMX_EPT_SUPPRESS_VE_BIT (1ull << 63)#define VMX_EPT_RWX_MASK (VMX_EPT_READABLE_MASK | \ VMX_EPT_WRITABLE_MASK | \ VMX_EPT_EXECUTABLE_MASK)
arch/x86/kvm/mmu.h +4 -2
@@ -64,8 +64,10 @@ static __always_inline u64 rsvd_bits(int s, int e)return ((2ULL << (e - s)) -1 ) << s; } -void kvm_mmu_set_mmio_spte_mask (u64 mmio_value,u64 mmio_mask,u64 access_mask); -void kvm_mmu_set_ept_masks (bool has_ad_bits,bool has_exec_only); +void kvm_mmu_set_mmio_spte_mask (struct kvm *kvm,u64 mmio_value,u64 mmio_mask, +u64 access_mask); +void kvm_mmu_set_default_mmio_spte_mask (u64 mask); +void kvm_mmu_set_ept_masks (bool has_ad_bits,bool has_exec_only,u64 init_value);void kvm_mmu_set_spte_init_value (u64 init_value);void kvm_init_mmu (struct kvm_vcpu *vcpu);
arch/x86/kvm/mmu/mmu.c +13 -6
@@ -2336,7 +2336,7 @@ static int mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,return kvm_mmu_prepare_zap_page (kvm, child, invalid_list); } - }else if (is_mmio_spte (pte)) { + }else if (is_mmio_spte (kvm, pte)) {mmu_spte_clear_no_track (spte); }return 0 ;
@@ -3069,9 +3069,12 @@ static bool handle_abnormal_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fa/* * If MMIO caching is disabled, emulate immediately without * touching the shadow page tables as attempting to install an -* MMIO SPTE will just be an expensive nop. +* MMIO SPTE will just be an expensive nop, but excludes the +* INTEL TD guest due to it also uses shadow_mmio_value = 0 +* to emulating MMIO access. */ -if (unlikely (!shadow_mmio_value)) { +if (unlikely (!vcpu->kvm->arch.shadow_mmio_value) + && !kvm_gfn_stolen_mask (vcpu->kvm)) { *ret_val =RET_PF_EMULATE ;return true; }
@@ -3209,7 +3212,8 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)break ; sp =sptep_to_sp (sptep); -if (!is_last_spte (spte, sp->role.level) ||is_mmio_spte (spte)) +if (!is_last_spte (spte, sp->role.level) || +is_mmio_spte (vcpu->kvm, spte))break ;/*
@@ -3892,7 +3896,7 @@ static int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr, bool direct)if (WARN_ON (reserved))return -EINVAL ; -if (is_mmio_spte (spte)) { +if (is_mmio_spte (vcpu->kvm, spte)) {gfn_t gfn =get_mmio_spte_gfn (spte);unsigned int access =get_mmio_spte_access (spte);
@@ -4294,7 +4298,7 @@ static unsigned long get_cr3(struct kvm_vcpu *vcpu)static bool sync_mmio_spte (struct kvm_vcpu *vcpu,u64 *sptep,gfn_t gfn,unsigned int access) { -if (unlikely (is_mmio_spte (*sptep))) { +if (unlikely (is_mmio_spte (vcpu->kvm, *sptep))) {if (gfn !=get_mmio_spte_gfn (*sptep)) {mmu_spte_clear_no_track (sptep);return true;
@@ -5791,6 +5795,9 @@ void kvm_mmu_init_vm(struct kvm *kvm)kvm_page_track_register_notifier (kvm, node); kvm->arch.tdp_max_page_level =KVM_MAX_HUGEPAGE_LEVEL ; +kvm_mmu_set_mmio_spte_mask (kvm, shadow_default_mmio_mask, + shadow_default_mmio_mask, +ACC_WRITE_MASK |ACC_USER_MASK ); }void kvm_mmu_uninit_vm (struct kvm *kvm)
arch/x86/kvm/mmu/spte.c +18 -20
@@ -29,8 +29,7 @@ u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */u64 __read_mostly shadow_user_mask;u64 __read_mostly shadow_accessed_mask;u64 __read_mostly shadow_dirty_mask; -u64 __read_mostly shadow_mmio_value; -u64 __read_mostly shadow_mmio_mask; +u64 __read_mostly shadow_default_mmio_mask;u64 __read_mostly shadow_mmio_access_mask;u64 __read_mostly shadow_present_mask;u64 __read_mostly shadow_me_mask;
@@ -59,10 +58,11 @@ u64 make_mmio_spte(struct kvm_vcpu *vcpu, u64 gfn, unsigned int access)u64 spte =generation_mmio_spte_mask (gen);u64 gpa = gfn <<PAGE_SHIFT ; -WARN_ON_ONCE (!shadow_mmio_value); +WARN_ON_ONCE (!vcpu->kvm->arch.shadow_mmio_value && + !kvm_gfn_stolen_mask (vcpu->kvm)); access &= shadow_mmio_access_mask; - spte |= shadow_mmio_value | access; + spte |= vcpu->kvm->arch.shadow_mmio_value | access; spte |= gpa | shadow_nonpresent_or_rsvd_mask; spte |= (gpa & shadow_nonpresent_or_rsvd_mask) <<SHADOW_NONPRESENT_OR_RSVD_MASK_LEN ;
@@ -279,7 +279,8 @@ u64 mark_spte_for_access_track(u64 spte)return spte; } -void kvm_mmu_set_mmio_spte_mask (u64 mmio_value,u64 mmio_mask,u64 access_mask) +void kvm_mmu_set_mmio_spte_mask (struct kvm *kvm,u64 mmio_value,u64 mmio_mask, +u64 access_mask) {BUG_ON ((u64 )(unsigned )access_mask != access_mask);WARN_ON (mmio_value & shadow_nonpresent_or_rsvd_lower_gfn_mask);
@@ -308,39 +309,32 @@ void kvm_mmu_set_mmio_spte_mask(u64 mmio_value, u64 mmio_mask, u64 access_mask) WARN_ON(mmio_value && (REMOVED_SPTE & mmio_mask) == mmio_value)) mmio_value =0 ; - shadow_mmio_value = mmio_value; - shadow_mmio_mask = mmio_mask; + kvm->arch.shadow_mmio_value = mmio_value; + kvm->arch.shadow_mmio_mask = mmio_mask; shadow_mmio_access_mask = access_mask; }EXPORT_SYMBOL_GPL (kvm_mmu_set_mmio_spte_mask); -void kvm_mmu_set_ept_masks (bool has_ad_bits,bool has_exec_only) +void kvm_mmu_set_ept_masks (bool has_ad_bits,bool has_exec_only,u64 init_value) { shadow_user_mask =VMX_EPT_READABLE_MASK ; shadow_accessed_mask = has_ad_bits ?VMX_EPT_ACCESS_BIT :0ull ; shadow_dirty_mask = has_ad_bits ?VMX_EPT_DIRTY_BIT :0ull ; shadow_nx_mask =0ull ; shadow_x_mask =VMX_EPT_EXECUTABLE_MASK ; - shadow_present_mask = has_exec_only ?0ull :VMX_EPT_READABLE_MASK ; + shadow_present_mask = + (has_exec_only ?0ull :VMX_EPT_READABLE_MASK ) | init_value; shadow_acc_track_mask =VMX_EPT_RWX_MASK ; shadow_me_mask =0ull ; shadow_host_writable_mask =EPT_SPTE_HOST_WRITABLE ; shadow_mmu_writable_mask =EPT_SPTE_MMU_WRITABLE ; - -/* -* EPT Misconfigurations are generated if the value of bits 2:0 -* of an EPT paging-structure entry is 110b (write/execute). -*/ -kvm_mmu_set_mmio_spte_mask (VMX_EPT_MISCONFIG_WX_VALUE , -VMX_EPT_RWX_MASK ,0 ); }EXPORT_SYMBOL_GPL (kvm_mmu_set_ept_masks);void kvm_mmu_reset_all_pte_masks (void ) {u8 low_phys_bits; -u64 mask; shadow_phys_bits =kvm_get_shadow_phys_bits ();
@@ -389,9 +383,13 @@ void kvm_mmu_reset_all_pte_masks(void)* PTEs and so the reserved PA approach must be disabled. */ if (shadow_phys_bits <52 ) - mask =BIT_ULL (51 ) |PT_PRESENT_MASK ; + shadow_default_mmio_mask =BIT_ULL (51 ) |PT_PRESENT_MASK ;else - mask =0 ; + shadow_default_mmio_mask =0 ; +} -kvm_mmu_set_mmio_spte_mask (mask, mask,ACC_WRITE_MASK |ACC_USER_MASK ); +void kvm_mmu_set_default_mmio_spte_mask (u64 mask) +{ + shadow_default_mmio_mask = mask; } +EXPORT_SYMBOL_GPL (kvm_mmu_set_default_mmio_spte_mask);
arch/x86/kvm/mmu/spte.h +4 -5
@@ -165,8 +165,7 @@ extern u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */extern u64 __read_mostly shadow_user_mask;extern u64 __read_mostly shadow_accessed_mask;extern u64 __read_mostly shadow_dirty_mask; -extern u64 __read_mostly shadow_mmio_value; -extern u64 __read_mostly shadow_mmio_mask; +extern u64 __read_mostly shadow_default_mmio_mask;extern u64 __read_mostly shadow_mmio_access_mask;extern u64 __read_mostly shadow_present_mask;extern u64 __read_mostly shadow_me_mask;
@@ -229,10 +228,10 @@ extern u64 __read_mostly shadow_nonpresent_or_rsvd_lower_gfn_mask;*/ extern u8 __read_mostly shadow_phys_bits; -static inline bool is_mmio_spte (u64 spte) +static inline bool is_mmio_spte (struct kvm *kvm,u64 spte) { -return (spte & shadow_mmio_mask) == shadow_mmio_value && -likely (shadow_mmio_value); +return (spte & kvm->arch.shadow_mmio_mask) == kvm->arch.shadow_mmio_value && +likely (kvm->arch.shadow_mmio_value ||kvm_gfn_stolen_mask (kvm)); }static inline bool is_shadow_present_pte (u64 pte)
arch/x86/kvm/mmu/tdp_mmu.c +3 -3
@@ -447,8 +447,8 @@ static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,* impact the guest since both the former and current SPTEs * are nonpresent. */ -if (WARN_ON (!is_mmio_spte (old_spte) && - !is_mmio_spte (new_spte) && +if (WARN_ON (!is_mmio_spte (kvm, old_spte) && + !is_mmio_spte (kvm, new_spte) && !is_removed_spte (new_spte)))pr_err ("Unexpected SPTE change! Nonpresent SPTEs\n" "should not be replaced with another,\n"
@@ -927,7 +927,7 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, }/* If a MMIO SPTE is installed, the MMIO will need to be emulated. */ -if (unlikely (is_mmio_spte (new_spte))) { +if (unlikely (is_mmio_spte (vcpu->kvm, new_spte))) {trace_mark_mmio_spte (rcu_dereference (iter->sptep), iter->gfn, new_spte); ret =RET_PF_EMULATE ;
arch/x86/kvm/svm/svm.c +1 -1
@@ -4704,7 +4704,7 @@ static __init void svm_adjust_mmio_mask(void)*/ mask = (mask_bit <52 ) ?rsvd_bits (mask_bit,51 ) |PT_PRESENT_MASK :0 ; -kvm_mmu_set_mmio_spte_mask (mask, mask,PT_WRITABLE_MASK |PT_USER_MASK ); +kvm_mmu_set_default_mmio_spte_mask (mask); }static __init void svm_set_cpu_caps (void )
arch/x86/kvm/vmx/main.c +5 -2
@@ -23,9 +23,12 @@ static __init int vt_hardware_setup(void)tdx_hardware_setup (&vt_x86_ops); -if (enable_ept) +if (enable_ept) { +const u64 init_value = enable_tdx ?VMX_EPT_SUPPRESS_VE_BIT :0ull ;kvm_mmu_set_ept_masks (enable_ept_ad_bits, -cpu_has_vmx_ept_execute_only ()); +cpu_has_vmx_ept_execute_only (), init_value); +kvm_mmu_set_spte_init_value (init_value); + }return 0 ; }
arch/x86/kvm/vmx/tdx.c +1 -1
@@ -11,7 +11,7 @@#undef pr_fmt#define pr_fmt (fmt) "tdx: " fmt -static bool __read_mostly enable_tdx = true; +bool __read_mostly enable_tdx = true;module_param_named (tdx, enable_tdx, bool,0644 );#define TDX_MAX_NR_CPUID_CONFIGS \
arch/x86/kvm/vmx/tdx.h +2 -0
@@ -6,6 +6,7 @@#include "tdx_ops.h" +extern bool __read_mostly enable_tdx;int tdx_module_setup (void );struct tdx_td_page {
@@ -166,6 +167,7 @@ static __always_inline u64 td_tdcs_exec_read64(struct kvm_tdx *kvm_tdx, u32 fiel }#else +#define enable_tdx falsestatic inline int tdx_module_setup (void ) {return -ENODEV ; };struct kvm_tdx ;
arch/x86/kvm/vmx/vmx.c +8 -0
@@ -7065,6 +7065,14 @@ int vmx_vm_init(struct kvm *kvm)if (!ple_gap) kvm->arch.pause_in_guest = true; +/* +* EPT Misconfigurations can be generated if the value of bits 2:0 +* of an EPT paging-structure entry is 110b (write/execute). +*/ +if (enable_ept) +kvm_mmu_set_mmio_spte_mask (kvm,VMX_EPT_MISCONFIG_WX_VALUE , +VMX_EPT_MISCONFIG_WX_VALUE ,0 ); +if (boot_cpu_has (X86_BUG_L1TF ) && enable_ept) {switch (l1tf_mitigation) {case L1TF_MITIGATION_OFF :
Signature · 1 line
2.25.1
RFC PATCH v5 45/104 KVM: x86/tdp_mmu: make REMOVED_SPTE include shadow_initial value
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDP MMU uses REMOVED_SPTE = 0x5a0ULL as special constant to indicate the intermediate value to indicate one thread is operating on it and the value should be semi-arbitrary value. For TDX (more correctly to use #VE), the value should include suppress #VE value which is shadow_init_value.
Define SHADOW_REMOVED_SPTE as shadow_init_value | REMOVED_SPTE, and replace REMOVED_SPTE with SHADOW_REMOVED_SPTE to use suppress #VE bit properly for TDX.
--- arch/x86/kvm/mmu/spte.h | 14 ++++++++++++-- arch/x86/kvm/mmu/tdp_mmu.c | 23 ++++++++++++++++------- 2 files changed, 28 insertions(+), 9 deletions(-)
arch/x86/kvm/mmu/spte.h +12 -2
@@ -194,7 +194,9 @@ extern u64 __read_mostly shadow_nonpresent_or_rsvd_mask;* If a thread running without exclusive control of the MMU lock must perform a * multi-part operation on an SPTE, it can set the SPTE to REMOVED_SPTE as a * non-present intermediate value. Other threads which encounter this value -* should not modify the SPTE. +* should not modify the SPTE. When TDX is enabled, shadow_init_value, which +* is "suppress #VE" bit set, is also set to removed SPTE, because TDX module +* always enables "EPT violation #VE". * * Use a semi-arbitrary value that doesn't set RWX bits, i.e. is not-present on * bot AMD and Intel CPUs, and doesn't set PFN bits, i.e. doesn't create a L1TF
@@ -207,9 +209,17 @@ extern u64 __read_mostly shadow_nonpresent_or_rsvd_mask;/* Removed SPTEs must not be misconstrued as shadow present PTEs. */ static_assert (!(REMOVED_SPTE &SPTE_MMU_PRESENT_MASK )); +/* +* See above comment around REMOVED_SPTE. SHADOW_REMOVED_SPTE is the actual +* intermediate value set to the removed SPET. When TDX is enabled, it sets +* the "suppress #VE" bit, otherwise it's REMOVED_SPTE. +*/ +extern u64 __read_mostly shadow_init_value; +#define SHADOW_REMOVED_SPTE (shadow_init_value | REMOVED_SPTE) +static inline bool is_removed_spte (u64 spte) { -return spte ==REMOVED_SPTE ; +return spte ==SHADOW_REMOVED_SPTE ; }/*
arch/x86/kvm/mmu/tdp_mmu.c +16 -7
@@ -338,7 +338,7 @@ static void handle_removed_tdp_mmu_page(struct kvm *kvm, tdp_ptep_t pt,* value to the removed SPTE value. */ for (;;) { - old_child_spte =xchg (sptep,REMOVED_SPTE ); + old_child_spte =xchg (sptep,SHADOW_REMOVED_SPTE );if (!is_removed_spte (old_child_spte))break ;cpu_relax ();
@@ -365,10 +365,10 @@ static void handle_removed_tdp_mmu_page(struct kvm *kvm, tdp_ptep_t pt,* the two branches consistent and simplifies * the function. */ -WRITE_ONCE (*sptep,REMOVED_SPTE ); +WRITE_ONCE (*sptep,SHADOW_REMOVED_SPTE ); }handle_changed_spte (kvm,kvm_mmu_page_as_id (sp), gfn, - old_child_spte,REMOVED_SPTE , level, + old_child_spte,SHADOW_REMOVED_SPTE , level, shared); }
@@ -537,7 +537,7 @@ static inline bool tdp_mmu_zap_spte_atomic(struct kvm *kvm,* immediately installing a present entry in its place * before the TLBs are flushed. */ -if (!tdp_mmu_set_spte_atomic (kvm, iter,REMOVED_SPTE )) +if (!tdp_mmu_set_spte_atomic (kvm, iter,SHADOW_REMOVED_SPTE ))return false; kvm_flush_remote_tlbs_with_address(kvm, iter->gfn,
@@ -550,8 +550,16 @@ static inline bool tdp_mmu_zap_spte_atomic(struct kvm *kvm,* special removed SPTE value. No bookkeeping is needed * here since the SPTE is going from non-present * to non-present. +* +* Set non-present value to shadow_init_value, rather than 0. +* It is because when TDX is enabled, TDX module always +* enables "EPT-violation #VE", so KVM needs to set +* "suppress #VE" bit in EPT table entries, in order to get +* real EPT violation, rather than TDVMCALL. KVM sets +* shadow_init_value (which sets "suppress #VE" bit) so it +* can be set when EPT table entries are zapped. */ -WRITE_ONCE (*rcu_dereference (iter->sptep),0 ); +WRITE_ONCE (*rcu_dereference (iter->sptep), shadow_init_value);return true; }
@@ -748,7 +756,8 @@ static bool zap_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,continue ;if (!shared) { -tdp_mmu_set_spte (kvm, &iter,0 ); +/* see comments in tdp_mmu_zap_spte_atomic() */ +tdp_mmu_set_spte (kvm, &iter, shadow_init_value); flush = true; }else if (!tdp_mmu_zap_spte_atomic (kvm, &iter)) {/*
@@ -1135,7 +1144,7 @@ static bool set_spte_gfn(struct kvm *kvm, struct tdp_iter *iter,* invariant that the PFN of a present * leaf SPTE can never change. * See __handle_changed_spte(). */ -tdp_mmu_set_spte (kvm, iter,0 ); +tdp_mmu_set_spte (kvm, iter, shadow_init_value);if (!pte_write (range->pte)) { new_spte = kvm_mmu_changed_pte_notifier_make_spte(iter->old_spte,
Signature · 1 line
2.25.1
RFC PATCH v5 44/104 The start of TDX KVM patch series: KVM TDP MMU hooks
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of KVM TDP MMU hooks.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +2 -2
@@ -25,6 +25,6 @@ Patch Layer status * TD vcpu interrupts/exit/hypercall: Not yet * KVM MMU GPA stolen bits: Applied -* KVM TDP refactoring for TDX: Applying -* KVM TDP MMU hooks: Not yet +* KVM TDP refactoring for TDX: Applied +* KVM TDP MMU hooks: Applying * KVM TDP MMU MapGPA: Not yet
Signature · 1 line
2.25.1
RFC PATCH v5 46/104 KVM: x86/tdp_mmu: refactor kvm_tdp_mmu_map()
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Factor out non-leaf SPTE population logic from kvm_tdp_mmu_map(). MapGPA hypercall needs to populate non-leaf SPTE to record which GPA, private or shared, is allowed in the leaf EPT entry.
--- arch/x86/kvm/mmu/tdp_mmu.c | 48 ++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 18 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +30 -18
@@ -955,6 +955,31 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,return ret; } +static bool tdp_mmu_populate_nonleaf ( +struct kvm_vcpu *vcpu,struct tdp_iter *iter,bool account_nx) +{ +struct kvm_mmu_page *sp; +u64 *child_pt; +u64 new_spte; + +WARN_ON (is_shadow_present_pte (iter->old_spte)); +WARN_ON (is_removed_spte (iter->old_spte)); + + sp =alloc_tdp_mmu_page (vcpu, iter->gfn, iter->level -1 ); + child_pt = sp->spt; + + new_spte =make_nonleaf_spte (child_pt, !shadow_accessed_mask); + +if (!tdp_mmu_set_spte_atomic (vcpu->kvm, iter, new_spte)) { +tdp_mmu_free_sp (sp); +return false; + } + +tdp_mmu_link_page (vcpu->kvm, sp, account_nx); +trace_kvm_mmu_get_page (sp, true); +return true; +} +/* * Handle a TDP page fault (NPT/EPT violation/misconfiguration) by installing * page tables and SPTEs to translate the faulting guest physical address.
@@ -963,9 +988,6 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) {struct kvm_mmu *mmu = vcpu->arch.mmu;struct tdp_iter iter; -struct kvm_mmu_page *sp; -u64 *child_pt; -u64 new_spte;int ret;kvm_mmu_hugepage_adjust (vcpu, fault);
@@ -1000,6 +1022,8 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) }if (!is_shadow_present_pte (iter.old_spte)) { +bool account_nx; +/* * If SPTE has been frozen by another thread, just * give up and retry, avoiding unnecessary page table
@@ -1008,22 +1032,10 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)if (is_removed_spte (iter.old_spte))break ; - sp =alloc_tdp_mmu_page (vcpu, iter.gfn, iter.level -1 ); - child_pt = sp->spt; - - new_spte =make_nonleaf_spte (child_pt, - !shadow_accessed_mask); - -if (tdp_mmu_set_spte_atomic (vcpu->kvm, &iter, new_spte)) { -tdp_mmu_link_page (vcpu->kvm, sp, - fault->huge_page_disallowed && - fault->req_level >= iter.level); - -trace_kvm_mmu_get_page (sp, true); - }else { -tdp_mmu_free_sp (sp); + account_nx = fault->huge_page_disallowed && + fault->req_level >= iter.level; +if (!tdp_mmu_populate_nonleaf (vcpu, &iter, account_nx))break ; - } } }
Signature · 1 line
2.25.1
RFC PATCH v5 50/104 The start of TDX KVM patch series: TDX EPT violation
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of TDX EPT violation.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +2 -2
@@ -19,12 +19,12 @@ Patch Layer status * TDX architectural definitions: Applied * TD VM creation/destruction: Applied * TD vcpu creation/destruction: Applied -* TDX EPT violation: Not yet +* TDX EPT violation: Applying * TD finalization: Not yet * TD vcpu enter/exit: Not yet * TD vcpu interrupts/exit/hypercall: Not yet * KVM MMU GPA stolen bits: Applied * KVM TDP refactoring for TDX: Applied -* KVM TDP MMU hooks: Applying +* KVM TDP MMU hooks: Applied * KVM TDP MMU MapGPA: Not yet
Signature · 1 line
2.25.1
RFC PATCH v5 37/104 KVM: x86/mmu: Allow non-zero init value for shadow PTE
In reply to [email protected]From: Sean Christopherson <[email protected]>
TDX will run with EPT violation #VEs enabled for shared EPT, which means KVM needs to set the "suppress #VE" bit in unused PTEs to avoid unintentionally reflecting not-present EPT violations into the guest.
Because guest memory is protected with TDX, VMM can't parse instructions in the guest memory. Instead, MMIO hypercall is used to pass necessary information to VMM.
To make unmodified device driver work, guest TD expects #VE on accessing shared GPA. The #VE handler converts MMIO access into MMIO hypercall with the EPT entry of enabled "#VE" by clearing "suppress #VE" bit. Before VMM enabling #VE, it needs to figure out the given GPA is for MMIO by EPT violation. So the execution flow looks like
- allocate unused shared EPT entry with suppress #VE bit set.
- EPT violation on that GPA.
- VMM figures out the faulted GPA is for MMIO.
- VMM clears the suppress #VE bit.
- Guest TD gets #VE, and converts MMIO access into MMIO hypercall.
--- arch/x86/kvm/mmu.h | 1 + arch/x86/kvm/mmu/mmu.c | 50 +++++++++++++++++++++++++++++++++++------ arch/x86/kvm/mmu/spte.c | 10 +++++++++ arch/x86/kvm/mmu/spte.h | 2 ++ 4 files changed, 56 insertions(+), 7 deletions(-)
arch/x86/kvm/mmu.h +1 -0
@@ -66,6 +66,7 @@ static __always_inline u64 rsvd_bits(int s, int e)void kvm_mmu_set_mmio_spte_mask (u64 mmio_value,u64 mmio_mask,u64 access_mask);void kvm_mmu_set_ept_masks (bool has_ad_bits,bool has_exec_only); +void kvm_mmu_set_spte_init_value (u64 init_value);void kvm_init_mmu (struct kvm_vcpu *vcpu); void kvm_init_shadow_npt_mmu(struct kvm_vcpu *vcpu, unsigned long cr0,
arch/x86/kvm/mmu/mmu.c +43 -7
87 lines not on this page · raw message
arch/x86/kvm/mmu/spte.c +10 -0
@@ -35,6 +35,7 @@ u64 __read_mostly shadow_mmio_access_mask;u64 __read_mostly shadow_present_mask;u64 __read_mostly shadow_me_mask;u64 __read_mostly shadow_acc_track_mask; +u64 __read_mostly shadow_init_value;u64 __read_mostly shadow_nonpresent_or_rsvd_mask;u64 __read_mostly shadow_nonpresent_or_rsvd_lower_gfn_mask;
@@ -223,6 +224,14 @@ u64 kvm_mmu_changed_pte_notifier_make_spte(u64 old_spte, kvm_pfn_t new_pfn)return new_spte; } +void kvm_mmu_set_spte_init_value (u64 init_value) +{ +if (WARN_ON (!IS_ENABLED (CONFIG_X86_64 ) && init_value)) + init_value =0 ; + shadow_init_value = init_value; +} +EXPORT_SYMBOL_GPL (kvm_mmu_set_spte_init_value); +static u8 kvm_get_shadow_phys_bits (void ) {/*
@@ -367,6 +376,7 @@ void kvm_mmu_reset_all_pte_masks(void) shadow_present_mask =PT_PRESENT_MASK ; shadow_acc_track_mask =0 ; shadow_me_mask = sme_me_mask; + shadow_init_value =0 ; shadow_host_writable_mask =DEFAULT_SPTE_HOST_WRITEABLE ; shadow_mmu_writable_mask =DEFAULT_SPTE_MMU_WRITEABLE ;
arch/x86/kvm/mmu/spte.h +2 -0
@@ -171,6 +171,8 @@ extern u64 __read_mostly shadow_mmio_access_mask;extern u64 __read_mostly shadow_present_mask;extern u64 __read_mostly shadow_me_mask; +extern u64 __read_mostly shadow_init_value; +/* * SPTEs in MMUs without A/D bits are marked with SPTE_TDP_AD_DISABLED_MASK; * shadow_acc_track_mask is the set of bits to be cleared in non-accessed
Signature · 1 line
2.25.1
RFC PATCH v5 43/104 KVM: TDX: Add load_mmu_pgd method for TDX
In reply to [email protected]From: Sean Christopherson <[email protected]>
For virtual IO, the guest TD shares guest pages with VMM without encryption. Shared EPT is used to map guest pages in unprotected way.
Add the VMCS field encoding for the shared EPTP, which will be used by TDX to have separate EPT walks for private GPAs (existing EPTP) versus shared GPAs (new shared EPTP).
Set shared EPT pointer value for the TDX guest to initialize TDX MMU.
--- arch/x86/include/asm/vmx.h | 1 + arch/x86/kvm/vmx/main.c | 11 ++++++++++- arch/x86/kvm/vmx/tdx.c | 5 +++++ arch/x86/kvm/vmx/x86_ops.h | 4 ++++ 4 files changed, 20 insertions(+), 1 deletion(-)
arch/x86/include/asm/vmx.h +1 -0
@@ -221,6 +221,7 @@ enum vmcs_field {ENCLS_EXITING_BITMAP_HIGH =0x0000202F ,TSC_MULTIPLIER =0x00002032 ,TSC_MULTIPLIER_HIGH =0x00002033 , +SHARED_EPT_POINTER =0x0000203C ,GUEST_PHYSICAL_ADDRESS =0x00002400 ,GUEST_PHYSICAL_ADDRESS_HIGH =0x00002401 , VMCS_LINK_POINTER = 0x00002800,
arch/x86/kvm/vmx/main.c +10 -1
@@ -89,6 +89,15 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)return vmx_vcpu_reset (vcpu, init_event); } +static void vt_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa, +int pgd_level) +{ +if (is_td_vcpu (vcpu)) +return tdx_load_mmu_pgd (vcpu, root_hpa, pgd_level); + +vmx_load_mmu_pgd (vcpu, root_hpa, pgd_level); +} +static int vt_mem_enc_op (struct kvm *kvm,void __user *argp) {if (!is_td (kvm))
@@ -205,7 +214,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .write_tsc_offset = vmx_write_tsc_offset, .write_tsc_multiplier = vmx_write_tsc_multiplier, - .load_mmu_pgd = vmx_load_mmu_pgd, + .load_mmu_pgd = vt_load_mmu_pgd, .check_intercept = vmx_check_intercept, .handle_exit_irqoff = vmx_handle_exit_irqoff,
arch/x86/kvm/vmx/tdx.c +5 -0
@@ -496,6 +496,11 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->kvm->vm_bugged = true; } +void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int pgd_level) +{ +td_vmcs_write64 (to_tdx (vcpu),SHARED_EPT_POINTER , root_hpa &PAGE_MASK ); +} +static int tdx_capabilities (struct kvm *kvm,struct kvm_tdx_cmd *cmd) {struct kvm_tdx_capabilities __user *user_caps;
arch/x86/kvm/vmx/x86_ops.h +4 -0
@@ -143,6 +143,8 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp); + +void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);#else static inline void tdx_pre_kvm_init (unsigned int *vcpu_size,unsigned int *vcpu_align,unsigned int *vm_size) {}
@@ -160,6 +162,8 @@ static inline void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) {}static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; } + +static inline void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level) {}#endif #endif /* __KVM_X86_VMX_X86_OPS_H */
Signature · 1 line
2.25.1
RFC PATCH v5 49/104 KVM: x86/tdp_mmu: Ignore unsupported mmu operation on private GFNs
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Some KVM MMU operations (dirty page logging, page migration, aging page) aren't supported for private GFNs (yet). Silently return on unsupported TDX KVM MMU operations.
For dirty logging, aging page, the GFN should be recorded with un-aliased GFN. Insert GFN un-aliasing where necessary.
--- arch/x86/kvm/mmu/tdp_mmu.c | 100 +++++++++++++++++++++++++++++++++++-- 1 file changed, 96 insertions(+), 4 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +96 -4
@@ -257,11 +257,20 @@ static void handle_changed_spte_acc_track(u64 old_spte, u64 new_spte, int level) }static void handle_changed_spte_dirty_log (struct kvm *kvm,int as_id,gfn_t gfn, +bool private_spte,u64 old_spte,u64 new_spte,int level) {bool pfn_changed;struct kvm_memory_slot *slot; +/* +* TDX doesn't support live migration. Never mark private page as +* dirty in log-dirty bitmap, since it's not possible for userspace +* hypervisor to live migrate private page anyway. +*/ +if (private_spte) +return ; +if (level >PG_LEVEL_4K )return ;
@@ -269,6 +278,8 @@ static void handle_changed_spte_dirty_log(struct kvm *kvm, int as_id, gfn_t gfn,if ((!is_writable_pte (old_spte) || pfn_changed) &&is_writable_pte (new_spte)) { +/* For memory slot operations, use GFN without aliasing */ + gfn =kvm_gfn_unalias (kvm, gfn); slot =__gfn_to_memslot (__kvm_memslots (kvm, as_id), gfn);mark_page_dirty_in_slot (kvm, slot, gfn); }
@@ -547,7 +558,7 @@ static void handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,__handle_changed_spte (kvm, as_id, gfn, private_spte, old_spte, new_spte, level, shared);handle_changed_spte_acc_track (old_spte, new_spte, level); -handle_changed_spte_dirty_log (kvm, as_id, gfn, old_spte, +handle_changed_spte_dirty_log (kvm, as_id, gfn, private_spte, old_spte, new_spte, level); }
@@ -678,6 +689,7 @@ static inline void __tdp_mmu_set_spte(struct kvm *kvm, struct tdp_iter *iter, iter->level);if (record_dirty_log)handle_changed_spte_dirty_log (kvm, iter->as_id, iter->gfn, +is_private_spte (iter->sptep), iter->old_spte, new_spte, iter->level); }
@@ -1215,7 +1227,23 @@ static __always_inline bool kvm_tdp_mmu_handle_gfn(struct kvm *kvm,* into this helper allow blocking; it'd be dead, wasteful code. */ for_each_tdp_mmu_root (kvm, root, range->slot->as_id) { -tdp_root_for_each_leaf_pte (iter, root, range->start, range->end) +/* +* For TDX shared mapping, set GFN shared bit to the range, +* so the handler() doesn't need to set it, to avoid duplicated +* code in multiple handler()s. +*/ +gfn_t start; +gfn_t end; + +if (is_private_sp (root)) { + start =kvm_gfn_private (kvm, range->start); + end =kvm_gfn_private (kvm, range->end); + }else { + start =kvm_gfn_shared (kvm, range->start); + end =kvm_gfn_shared (kvm, range->end); + } + +tdp_root_for_each_leaf_pte (iter, root, start, end) ret |=handler (kvm, &iter, range); }
@@ -1237,6 +1265,15 @@ static bool age_gfn_range(struct kvm *kvm, struct tdp_iter *iter,if (!is_accessed_spte (iter->old_spte))return false; +/* +* First TDX generation doesn't support clearing A bit for private +* mapping, since there's no secure EPT API to support it. However +* it's a legitimate request for TDX guest, so just return w/o a +* WARN(). +*/ +if (is_private_spte (iter->sptep)) +return false; + new_spte = iter->old_spte;if (spte_ad_enabled (new_spte)) {
@@ -1281,6 +1318,13 @@ static bool set_spte_gfn(struct kvm *kvm, struct tdp_iter *iter,/* Huge pages aren't expected to be modified without first being zapped. */ WARN_ON (pte_huge (range->pte) || range->start +1 != range->end); +/* +* .change_pte() callback should not happen for private page, because +* for now TDX private pages are pinned during VM's life time. +*/ +if (WARN_ON (is_private_spte (iter->sptep))) +return false; +if (iter->level !=PG_LEVEL_4K || !is_shadow_present_pte (iter->old_spte))return false;
@@ -1332,6 +1376,16 @@ static bool wrprot_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,u64 new_spte;bool spte_set = false; +/* +* First TDX generation doesn't support write protecting private +* mappings, since there's no secure EPT API to support it. It +* is a bug to reach here for TDX guest. +*/ +if (WARN_ON (is_private_sp (root))) +return spte_set; + start =kvm_gfn_shared (kvm, start); + end =kvm_gfn_shared (kvm, end); +rcu_read_lock ();BUG_ON (min_level >KVM_MAX_HUGEPAGE_LEVEL );
@@ -1398,6 +1452,16 @@ static bool clear_dirty_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,u64 new_spte;bool spte_set = false; +/* +* First TDX generation doesn't support clearing dirty bit, +* since there's no secure EPT API to support it. It is a +* bug to reach here for TDX guest. +*/ +if (WARN_ON (is_private_sp (root))) +return spte_set; + start =kvm_gfn_shared (kvm, start); + end =kvm_gfn_shared (kvm, end); +rcu_read_lock ();tdp_root_for_each_leaf_pte (iter, root, start, end) {
@@ -1467,6 +1531,15 @@ static void clear_dirty_pt_masked(struct kvm *kvm, struct kvm_mmu_page *root,struct tdp_iter iter;u64 new_spte; +/* +* First TDX generation doesn't support clearing dirty bit, +* since there's no secure EPT API to support it. It is a +* bug to reach here for TDX guest. +*/ +if (WARN_ON (is_private_sp (root))) +return ; + gfn =kvm_gfn_shared (kvm, gfn); +rcu_read_lock (); tdp_root_for_each_leaf_pte(iter, root, gfn + __ffs(mask),
@@ -1530,6 +1603,16 @@ static void zap_collapsible_spte_range(struct kvm *kvm,struct tdp_iter iter;kvm_pfn_t pfn; +/* +* This should only be reachable in case of log-dirty, which TD +* private mapping doesn't support so far. Give a WARN() if it +* hits private mapping. +*/ +if (WARN_ON (is_private_sp (root))) +return ; + start =kvm_gfn_shared (kvm, start); + end =kvm_gfn_shared (kvm, end); +rcu_read_lock ();tdp_root_for_each_pte (iter, root, start, end) {
@@ -1543,8 +1626,9 @@ static void zap_collapsible_spte_range(struct kvm *kvm, pfn =spte_to_pfn (iter.old_spte);if (kvm_is_reserved_pfn (pfn) || - iter.level >=kvm_mmu_max_mapping_level (kvm, slot, iter.gfn, - pfn,PG_LEVEL_NUM )) + iter.level >=kvm_mmu_max_mapping_level (kvm, slot, +tdp_iter_gfn_unalias (kvm, &iter), pfn, +PG_LEVEL_NUM ))continue ;/* Note, a successful atomic zap also does a remote TLB flush. */
@@ -1590,6 +1674,14 @@ static bool write_protect_gfn(struct kvm *kvm, struct kvm_mmu_page *root,BUG_ON (min_level >KVM_MAX_HUGEPAGE_LEVEL ); +/* +* First TDX generation doesn't support write protecting private +* mappings, since there's no secure EPT API to support it. It +* is a bug to reach here for TDX guest. +*/ +if (WARN_ON (is_private_sp (root))) +return spte_set; +rcu_read_lock (); for_each_tdp_pte_min_level(iter, root->spt, root->role.level,
Signature · 1 line
2.25.1
RFC PATCH v5 47/104 KVM: x86/mmu: add a private pointer to struct kvm_mmu_page
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Add a private pointer to kvm_mmu_page for private EPT.
To resolve KVM page fault on private GPA, it will allocate additional page for Secure EPT in addition to private EPT. Add memory allocator for it and topup its memory allocator before resolving KVM page fault similar to shared EPT page. Allocation of those memory will be done for TDP MMU by alloc_tdp_mmu_page(). Freeing those memory will be done for TDP MMU on behalf of kvm_tdp_mmu_zap_all() called by kvm_mmu_zap_all(). Private EPT page needs to carry one more page used for Secure EPT in addition to the private EPT page. Add private pointer to struct kvm_mmu_page for that purpose and Add helper functions to allocate/free a page for Secure EPT. Also add helper functions to check if a given kvm_mmu_page is private.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 9 ++++ arch/x86/kvm/mmu/mmu_internal.h | 84 +++++++++++++++++++++++++++++++++ arch/x86/kvm/mmu/tdp_mmu.c | 3 ++ 4 files changed, 97 insertions(+)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -689,6 +689,7 @@ struct kvm_vcpu_arch {struct kvm_mmu_memory_cache mmu_shadow_page_cache;struct kvm_mmu_memory_cache mmu_gfn_array_cache;struct kvm_mmu_memory_cache mmu_page_header_cache; +struct kvm_mmu_memory_cache mmu_private_sp_cache;/* * QEMU userspace and the guest each have their own FPU state.
arch/x86/kvm/mmu/mmu.c +9 -0
@@ -758,6 +758,13 @@ static int mmu_topup_shadow_page_cache(struct kvm_vcpu *vcpu)struct kvm_mmu_memory_cache *mc = &vcpu->arch.mmu_shadow_page_cache;int start, end, i, r; +if (kvm_gfn_stolen_mask (vcpu->kvm)) { + r =kvm_mmu_topup_memory_cache (&vcpu->arch.mmu_private_sp_cache, +PT64_ROOT_MAX_LEVEL ); +if (r) +return r; + } +if (shadow_init_value) start =kvm_mmu_memory_cache_nr_free_objects (mc);
@@ -799,6 +806,7 @@ static void mmu_free_memory_caches(struct kvm_vcpu *vcpu) {kvm_mmu_free_memory_cache (&vcpu->arch.mmu_pte_list_desc_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_shadow_page_cache); +kvm_mmu_free_memory_cache (&vcpu->arch.mmu_private_sp_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_gfn_array_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_page_header_cache); }
@@ -1791,6 +1799,7 @@ static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, int directif (!direct) sp->gfns =kvm_mmu_memory_cache_alloc (&vcpu->arch.mmu_gfn_array_cache);set_page_private (virt_to_page (sp->spt), (unsigned long )sp); +kvm_mmu_init_private_sp (sp);/* * active_mmu_pages must be a FIFO list, as kvm_zap_obsolete_pages()
arch/x86/kvm/mmu/mmu_internal.h +84 -0
96 lines not on this page · raw message
arch/x86/kvm/mmu/tdp_mmu.c +3 -0
@@ -59,6 +59,8 @@ static bool zap_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,static void tdp_mmu_free_sp (struct kvm_mmu_page *sp) { +if (is_private_sp (sp)) +kvm_mmu_free_private_sp (sp);free_page ((unsigned long )sp->spt);kmem_cache_free (mmu_page_header_cache, sp); }
@@ -184,6 +186,7 @@ static struct kvm_mmu_page *alloc_tdp_mmu_page(struct kvm_vcpu *vcpu, gfn_t gfn, sp->role.word =page_role_for_level (vcpu, level).word; sp->gfn = gfn; sp->tdp_mmu_page = true; +kvm_mmu_init_private_sp (sp);trace_kvm_mmu_get_page (sp, true);
Signature · 1 line
2.25.1
RFC PATCH v5 48/104 KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Use private EPT to mirror Secure EPT, and On the change of the private EPT entry, invoke kvm_x86_ops hook in __handle_changed_spte() to propagate the change to Secure EPT.
On EPT violation, determine which EPT to use, private or shared, based on faulted GPA. When allocating an EPT page, record it (private or shared) in the page role. The private is passed down to the function as an argument as necessary. When the private EPT entry is changed, call the hook.
When zapping EPT, the EPT entry is frozen with the special EPT value that clears the present bit. After the TLB shootdown, the entry is set to the eventual value. On populating the EPT entry, atomically set the entry.
For TDX, TDX SEAMCALL to update Secure EPT in addition to direct access to the private EPT entry. For the zapping case, freeing the EPT entry works. It can call TDX SEAMCALL in addition to TLB shootdown. For populating the private EPT entry, there can be a race condition without further protection
vcpu 1: populating 2M private EPT entry
vcpu 2: populating 4K private EPT entry
vcpu 2: TDX SEAMCALL to update 4K secure EPT => error vcpu 1: TDX SEAMCALL to update 4M secure EPT
To avoid the race, the frozen EPT entry is utilized. Instead of atomic update of the private EPT entry, freeze the entry, call the hook that invokes TDX SEAMCALL, set the entry to the final value (unfreeze).
Support 4K page only at this stage. 2M page support can be done in future patches.
--- arch/x86/include/asm/kvm-x86-ops.h | 2 + arch/x86/include/asm/kvm_host.h | 8 + arch/x86/kvm/mmu/mmu.c | 31 +++- arch/x86/kvm/mmu/tdp_iter.h | 2 +- arch/x86/kvm/mmu/tdp_mmu.c | 226 +++++++++++++++++++++++------ arch/x86/kvm/mmu/tdp_mmu.h | 13 +- virt/kvm/kvm_main.c | 1 + 7 files changed, 232 insertions(+), 51 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +2 -0
@@ -91,6 +91,8 @@ KVM_X86_OP(set_tss_addr) KVM_X86_OP(set_identity_map_addr) KVM_X86_OP(get_mt_mask) KVM_X86_OP(load_mmu_pgd) +KVM_X86_OP(free_private_sp) +KVM_X86_OP(handle_changed_private_spte) KVM_X86_OP_NULL(has_wbinvd_exit) KVM_X86_OP(get_l2_tsc_offset) KVM_X86_OP(get_l2_tsc_multiplier)
arch/x86/include/asm/kvm_host.h +8 -0
@@ -433,6 +433,7 @@ struct kvm_mmu { struct kvm_mmu_page *sp);void (*invlpg)(struct kvm_vcpu *vcpu,gva_t gva,hpa_t root_hpa);hpa_t root_hpa; +hpa_t private_root_hpa;gpa_t root_pgd;union kvm_mmu_role mmu_role;u8 root_level;
@@ -1433,6 +1434,13 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level); +int (*free_private_sp)(struct kvm *kvm,gfn_t gfn,enum pg_level level, +void *private_sp); +void (*handle_changed_private_spte)( +struct kvm *kvm,gfn_t gfn,enum pg_level level, +kvm_pfn_t old_pfn,bool was_present,bool was_leaf, +kvm_pfn_t new_pfn,bool is_present,bool is_leaf,void *sept_page); +bool (*has_wbinvd_exit)(void );u64 (*get_l2_tsc_offset)(struct kvm_vcpu *vcpu);
arch/x86/kvm/mmu/mmu.c +29 -2
@@ -3422,6 +3422,7 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) {struct kvm_mmu *mmu = vcpu->arch.mmu;u8 shadow_root_level = mmu->shadow_root_level; +gfn_t gfn_stolen =kvm_gfn_stolen_mask (vcpu->kvm);hpa_t root;unsigned i;int r;
@@ -3432,7 +3433,11 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) goto out_unlock;if (is_tdp_mmu_enabled (vcpu->kvm)) { - root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu); +if (gfn_stolen && !VALID_PAGE (mmu->private_root_hpa)) { + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, true); + mmu->private_root_hpa = root; + } + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, false); mmu->root_hpa = root; }else if (shadow_root_level >=PT64_ROOT_4LEVEL ) { root =mmu_alloc_root (vcpu,0 ,0 , shadow_root_level, true);
@@ -5596,6 +5601,7 @@ static int __kvm_mmu_create(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu)int i; mmu->root_hpa =INVALID_PAGE ; + mmu->private_root_hpa =INVALID_PAGE ; mmu->root_pgd =0 ;for (i =0 ; i <KVM_MMU_NUM_PREV_ROOTS ; i++) mmu->prev_roots[i] =KVM_MMU_ROOT_INFO_INVALID ;
@@ -5772,6 +5778,10 @@ static void kvm_mmu_zap_all_fast(struct kvm *kvm)write_unlock (&kvm->mmu_lock); +/* +* For now private root is never invalidate during VM is running, +* so this can only happen for shared roots. +*/ if (is_tdp_mmu_enabled (kvm)) {read_lock (&kvm->mmu_lock);kvm_tdp_mmu_zap_invalidated_roots (kvm);
@@ -5871,7 +5881,8 @@ void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end)if (is_tdp_mmu_enabled (kvm)) {for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) flush =kvm_tdp_mmu_zap_gfn_range (kvm, i, gfn_start, - gfn_end, flush); + gfn_end, flush, + false); }if (flush)
@@ -5904,6 +5915,11 @@ void kvm_mmu_slot_remove_write_access(struct kvm *kvm,write_unlock (&kvm->mmu_lock); } +/* +* For now this can only happen for non-TD VM, because TD private +* mapping doesn't support write protection. kvm_tdp_mmu_wrprot_slot() +* will give a WARN() if it hits for TD. +*/ if (is_tdp_mmu_enabled (kvm)) {read_lock (&kvm->mmu_lock); flush |=kvm_tdp_mmu_wrprot_slot (kvm, memslot, start_level);
@@ -5952,6 +5968,9 @@ static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm, sp =sptep_to_sp (sptep); pfn =spte_to_pfn (*sptep); +/* Private page dirty logging is not supported. */ +KVM_BUG_ON (is_private_spte (sptep), kvm); +/* * We cannot do huge page mapping for indirect shadow pages, * which are found on the last rmap (level = 1) when not using
@@ -5992,6 +6011,11 @@ void kvm_mmu_zap_collapsible_sptes(struct kvm *kvm,write_unlock (&kvm->mmu_lock); } +/* +* This should only be reachable in case of log-dirty, wihch TD private +* mapping doesn't support so far. kvm_tdp_mmu_zap_collapsible_sptes() +* internally gives a WARN() when it hits. +*/ if (is_tdp_mmu_enabled (kvm)) {read_lock (&kvm->mmu_lock);kvm_tdp_mmu_zap_collapsible_sptes (kvm, slot);
@@ -6266,6 +6290,9 @@ int kvm_mmu_module_init(void)void kvm_mmu_destroy (struct kvm_vcpu *vcpu) {kvm_mmu_unload (vcpu); +if (is_tdp_mmu_enabled (vcpu->kvm)) +mmu_free_root_page (vcpu->kvm, &vcpu->arch.mmu->private_root_hpa, +NULL );free_mmu_pages (&vcpu->arch.root_mmu);free_mmu_pages (&vcpu->arch.guest_mmu);mmu_free_memory_caches (vcpu);
arch/x86/kvm/mmu/tdp_iter.h +1 -1
@@ -28,7 +28,7 @@ struct tdp_iter {tdp_ptep_t pt_path[PT64_ROOT_MAX_LEVEL ];/* A pointer to the current SPTE */ tdp_ptep_t sptep; -/* The lowest GFN mapped by the current SPTE */ +/* The lowest GFN (stolen bits included) mapped by the current SPTE */ gfn_t gfn;/* The level of the root page given to the iterator */ int root_level;
arch/x86/kvm/mmu/tdp_mmu.c +183 -43
467 lines not on this page · raw message
arch/x86/kvm/mmu/tdp_mmu.h +8 -5
@@ -5,7 +5,7 @@#include <linux/kvm_host.h> -hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu); +hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu,bool private);__must_check static inline bool kvm_tdp_mmu_get_root (struct kvm *kvm,struct kvm_mmu_page *root)
@@ -20,11 +20,14 @@ void kvm_tdp_mmu_put_root(struct kvm *kvm, struct kvm_mmu_page *root, bool shared);bool __kvm_tdp_mmu_zap_gfn_range (struct kvm *kvm,int as_id,gfn_t start, -gfn_t end,bool can_yield,bool flush); +gfn_t end,bool can_yield,bool flush, +bool zap_private);static inline bool kvm_tdp_mmu_zap_gfn_range (struct kvm *kvm,int as_id, -gfn_t start,gfn_t end,bool flush) +gfn_t start,gfn_t end,bool flush, +bool zap_private) { -return __kvm_tdp_mmu_zap_gfn_range (kvm, as_id, start, end, true, flush); +return __kvm_tdp_mmu_zap_gfn_range (kvm, as_id, start, end, true, flush, + zap_private); }static inline bool kvm_tdp_mmu_zap_sp (struct kvm *kvm,struct kvm_mmu_page *sp) {
@@ -41,7 +44,7 @@ static inline bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp)*/ lockdep_assert_held_write (&kvm->mmu_lock);return __kvm_tdp_mmu_zap_gfn_range (kvm,kvm_mmu_page_as_id (sp), - sp->gfn, end, false, false); + sp->gfn, end, false, false, false); }void kvm_tdp_mmu_zap_all (struct kvm *kvm);
virt/kvm/kvm_main.c +1 -0
@@ -190,6 +190,7 @@ bool kvm_is_reserved_pfn(kvm_pfn_t pfn)return true; } +EXPORT_SYMBOL_GPL (kvm_is_reserved_pfn);/* * Switches to specified vcpu, until a matching vcpu_put()
Signature · 1 line
2.25.1
RFC PATCH v5 40/104 KVM: VMX: Split out guts of EPT violation to common/exposed function
In reply to [email protected]From: Sean Christopherson <[email protected]>
The difference of TDX EPT violation is how to retrieve information, GPA, and exit qualification. To share the code to handle EPT violation, split out the guts of EPT violation handler so that VMX/TDX exit handler can call it after retrieving GPA and exit qualification.
--- arch/x86/kvm/vmx/common.h | 35 +++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 34 ++++++---------------------------- 2 files changed, 41 insertions(+), 28 deletions(-) create mode 100644 arch/x86/kvm/vmx/common.h
arch/x86/kvm/vmx/common.h new file +35 -0
@@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_X86_VMX_COMMON_H +#define __KVM_X86_VMX_COMMON_H + +#include <linux/kvm_host.h> + +#include "mmu.h" + +static inline int __vmx_handle_ept_violation (struct kvm_vcpu *vcpu,gpa_t gpa, +unsigned long exit_qualification) +{ +u64 error_code; + +/* Is it a read fault? */ + error_code = (exit_qualification &EPT_VIOLATION_ACC_READ ) + ?PFERR_USER_MASK :0 ; +/* Is it a write fault? */ + error_code |= (exit_qualification &EPT_VIOLATION_ACC_WRITE ) + ?PFERR_WRITE_MASK :0 ; +/* Is it a fetch fault? */ + error_code |= (exit_qualification &EPT_VIOLATION_ACC_INSTR ) + ?PFERR_FETCH_MASK :0 ; +/* ept page table entry is present? */ + error_code |= (exit_qualification & + (EPT_VIOLATION_READABLE |EPT_VIOLATION_WRITABLE | +EPT_VIOLATION_EXECUTABLE )) + ?PFERR_PRESENT_MASK :0 ; + + error_code |= (exit_qualification &EPT_VIOLATION_GVA_TRANSLATED ) !=0 ? +PFERR_GUEST_FINAL_MASK :PFERR_GUEST_PAGE_MASK ; + +return kvm_mmu_page_fault (vcpu, gpa, error_code,NULL ,0 ); +} + +#endif /* __KVM_X86_VMX_COMMON_H */
arch/x86/kvm/vmx/vmx.c +6 -28
@@ -50,6 +50,7 @@#include <asm/vmx.h> #include "capabilities.h" +#include "common.h" #include "cpuid.h" #include "evmcs.h" #include "hyperv.h"
@@ -5381,11 +5382,10 @@ static int handle_task_switch(struct kvm_vcpu *vcpu)static int handle_ept_violation (struct kvm_vcpu *vcpu) { -unsigned long exit_qualification; -gpa_t gpa; -u64 error_code; +unsigned long exit_qualification =vmx_get_exit_qual (vcpu); +gpa_t gpa =vmcs_read64 (GUEST_PHYSICAL_ADDRESS ); - exit_qualification =vmx_get_exit_qual (vcpu); +trace_kvm_page_fault (gpa, exit_qualification);/* * EPT violation happened while executing iret from NMI,
@@ -5394,31 +5394,9 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu)* AAK134, BY25. */ if (!(to_vmx (vcpu)->idt_vectoring_info &VECTORING_INFO_VALID_MASK ) && - enable_vnmi && - (exit_qualification &INTR_INFO_UNBLOCK_NMI )) + enable_vnmi && (exit_qualification &INTR_INFO_UNBLOCK_NMI ))vmcs_set_bits (GUEST_INTERRUPTIBILITY_INFO ,GUEST_INTR_STATE_NMI ); - gpa =vmcs_read64 (GUEST_PHYSICAL_ADDRESS ); -trace_kvm_page_fault (gpa, exit_qualification); - -/* Is it a read fault? */ - error_code = (exit_qualification &EPT_VIOLATION_ACC_READ ) - ?PFERR_USER_MASK :0 ; -/* Is it a write fault? */ - error_code |= (exit_qualification &EPT_VIOLATION_ACC_WRITE ) - ?PFERR_WRITE_MASK :0 ; -/* Is it a fetch fault? */ - error_code |= (exit_qualification &EPT_VIOLATION_ACC_INSTR ) - ?PFERR_FETCH_MASK :0 ; -/* ept page table entry is present? */ - error_code |= (exit_qualification & - (EPT_VIOLATION_READABLE |EPT_VIOLATION_WRITABLE | -EPT_VIOLATION_EXECUTABLE )) - ?PFERR_PRESENT_MASK :0 ; - - error_code |= (exit_qualification &EPT_VIOLATION_GVA_TRANSLATED ) !=0 ? -PFERR_GUEST_FINAL_MASK :PFERR_GUEST_PAGE_MASK ; - vcpu->arch.exit_qualification = exit_qualification;/*
@@ -5432,7 +5410,7 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu)if (unlikely (allow_smaller_maxphyaddr &&kvm_vcpu_is_illegal_gpa (vcpu, gpa)))return kvm_emulate_instruction (vcpu,0 ); -return kvm_mmu_page_fault (vcpu, gpa, error_code,NULL ,0 ); +return __vmx_handle_ept_violation (vcpu, gpa, exit_qualification); }static int handle_ept_misconfig (struct kvm_vcpu *vcpu)
Signature · 1 line
2.25.1
RFC PATCH v5 36/104 KVM: x86/mmu: Explicitly check for MMIO spte in fast page fault
In reply to [email protected]From: Sean Christopherson <[email protected]>
Explicitly check for an MMIO spte in the fast page fault flow. TDX will use a not-present entry for MMIO sptes, which can be mistaken for an access-tracked spte since both have SPTE_SPECIAL_MASK set.
The fast page fault handles the case of changing access bits without obtaining mmu_lock. For example, clear write protect bit for dirty page tracking. MMIO emulation is handled in a slow path. So it doesn't affect the default VM case.
--- arch/x86/kvm/mmu/mmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/mmu/mmu.c +1 -1
@@ -3167,7 +3167,7 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)break ; sp =sptep_to_sp (sptep); -if (!is_last_spte (spte, sp->role.level)) +if (!is_last_spte (spte, sp->role.level) ||is_mmio_spte (spte))break ;/*
Signature · 1 line
2.25.1
RFC PATCH v5 35/104 KVM: x86/mmu: Disallow dirty logging for x86 TDX
In reply to [email protected]From: Sean Christopherson <[email protected]>
TDX doesn't support dirty logging. Report dirty logging isn't supported so that device model, for example qemu, can properly handle it.
--- arch/x86/kvm/x86.c | 5 +++++ include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 15 ++++++++++++--- 3 files changed, 18 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +5 -0
@@ -12876,6 +12876,11 @@ int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size, }EXPORT_SYMBOL_GPL (kvm_sev_es_string_io); +bool kvm_arch_dirty_log_supported (struct kvm *kvm) +{ +return kvm->arch.vm_type !=KVM_X86_TDX_VM ; +} +EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_entry);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_exit);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_fast_mmio);
include/linux/kvm_host.h +1 -0
@@ -1423,6 +1423,7 @@ bool kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu);int kvm_arch_post_init_vm (struct kvm *kvm);void kvm_arch_pre_destroy_vm (struct kvm *kvm);int kvm_arch_create_vm_debugfs (struct kvm *kvm); +bool kvm_arch_dirty_log_supported (struct kvm *kvm);#ifndef __KVM_HAVE_ARCH_VM_ALLOC/*
virt/kvm/kvm_main.c +12 -3
@@ -1423,9 +1423,18 @@ static void kvm_replace_memslot(struct kvm *kvm, } } -static int check_memory_region_flags (const struct kvm_userspace_memory_region *mem) +bool __weak kvm_arch_dirty_log_supported (struct kvm *kvm) { -u32 valid_flags =KVM_MEM_LOG_DIRTY_PAGES ; +return true; +} + +static int check_memory_region_flags (struct kvm *kvm, +const struct kvm_userspace_memory_region *mem) +{ +u32 valid_flags =0 ; + +if (kvm_arch_dirty_log_supported (kvm)) + valid_flags |=KVM_MEM_LOG_DIRTY_PAGES ;#ifdef __KVM_HAVE_READONLY_MEM valid_flags |=KVM_MEM_READONLY ;
@@ -1826,7 +1835,7 @@ int __kvm_set_memory_region(struct kvm *kvm,int as_id, id;int r; - r =check_memory_region_flags (mem); + r =check_memory_region_flags (kvm, mem);if (r)return r;
Signature · 1 line
2.25.1
RFC PATCH v5 19/104 KVM: TDX: Stub in tdx.h with structs, accessors, and VMCS helpers
In reply to [email protected]From: Sean Christopherson <[email protected]>
Stub in kvm_tdx, vcpu_tdx, and their various accessors. TDX defines SEAMCALL APIs to access TDX control structures corresponding to the VMX VMCS. Introduce helper accessors to hide its SEAMCALL ABI details.
--- arch/x86/kvm/vmx/tdx.h | 101 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+)
arch/x86/kvm/vmx/tdx.h +101 -0
@@ -3,14 +3,29 @@#define __KVM_X86_TDX_H#ifdef CONFIG_INTEL_TDX_HOST + +#include "tdx_ops.h" +int tdx_module_setup (void ); +struct tdx_td_page { +unsigned long va; +hpa_t pa; +bool added; +}; +struct kvm_tdx {struct kvm kvm; + +struct tdx_td_page tdr; +struct tdx_td_page *tdcs; };struct vcpu_tdx {struct kvm_vcpu vcpu; + +struct tdx_td_page tdvpr; +struct tdx_td_page *tdvpx; };static inline bool is_td (struct kvm *kvm)
@@ -32,6 +47,92 @@ static inline struct vcpu_tdx *to_tdx(struct kvm_vcpu *vcpu) { return container_of(vcpu, struct vcpu_tdx, vcpu); } + +static __always_inline void tdvps_vmcs_check(u32 field, u8 bits) +{ +BUILD_BUG_ON_MSG (__builtin_constant_p (field) && (field) &0x1 , +"Read/Write to TD VMCS *_HIGH fields not supported" ); + +BUILD_BUG_ON (bits !=16 && bits !=32 && bits !=64 ); + +BUILD_BUG_ON_MSG (bits !=64 &&__builtin_constant_p (field) && + (((field) &0x6000 ) ==0x2000 || + ((field) &0x6000 ) ==0x6000 ), +"Invalid TD VMCS access for 64-bit field" ); +BUILD_BUG_ON_MSG (bits !=32 &&__builtin_constant_p (field) && + ((field) &0x6000 ) ==0x4000 , +"Invalid TD VMCS access for 32-bit field" ); +BUILD_BUG_ON_MSG (bits !=16 &&__builtin_constant_p (field) && + ((field) &0x6000 ) ==0x0000 , +"Invalid TD VMCS access for 16-bit field" ); +} + +static __always_inline void tdvps_state_non_arch_check(u64 field, u8 bits) {} +static __always_inline void tdvps_management_check(u64 field, u8 bits) {} + +#define TDX_BUILD_TDVPS_ACCESSORS (bits, uclass, lclass) \ +static __always_inline u##bits td_##lclass##_read##bits(struct vcpu_tdx *tdx, \ + u32 field) \ +{ \ + struct tdx_module_output out; \ + u64 err; \ + \ + tdvps_##lclass##_check(field, bits); \ + err = tdh_vp_rd(tdx->tdvpr.pa, TDVPS_##uclass(field), &out); \ + if (unlikely(err)) { \ + pr_err("TDH_VP_RD["#uclass".0x%x] failed: 0x%llx\n", \ + field, err); \ + return 0; \ + } \ + return (u##bits)out.r8; \ +} \ +static __always_inline void td_##lclass##_write##bits(struct vcpu_tdx *tdx, \ + u32 field, u##bits val) \ +{ \ + struct tdx_module_output out; \ + u64 err; \ + \ + tdvps_##lclass##_check(field, bits); \ + err = tdh_vp_wr(tdx->tdvpr.pa, TDVPS_##uclass(field), val, \ + GENMASK_ULL(bits - 1, 0), &out); \ + if (unlikely(err)) \ + pr_err("TDH_VP_WR["#uclass".0x%x] = 0x%llx failed: 0x%llx\n", \ + field, (u64)val, err); \ +} \ +static __always_inline void td_##lclass##_setbit##bits(struct vcpu_tdx *tdx, \ + u32 field, u64 bit) \ +{ \ + struct tdx_module_output out; \ + u64 err; \ + \ + tdvps_##lclass##_check(field, bits); \ + err = tdh_vp_wr(tdx->tdvpr.pa, TDVPS_##uclass(field), bit, bit, \ + &out); \ + if (unlikely(err)) \ + pr_err("TDH_VP_WR["#uclass".0x%x] |= 0x%llx failed: 0x%llx\n", \ + field, bit, err); \ +} \ +static __always_inline void td_##lclass##_clearbit##bits(struct vcpu_tdx *tdx, \ + u32 field, u64 bit) \ +{ \ + struct tdx_module_output out; \ + u64 err; \ + \ + tdvps_##lclass##_check(field, bits); \ + err = tdh_vp_wr(tdx->tdvpr.pa, TDVPS_##uclass(field), 0, bit, \ + &out); \ + if (unlikely(err)) \ + pr_err("TDH_VP_WR["#uclass".0x%x] &= ~0x%llx failed: 0x%llx\n", \ + field, bit, err); \ +} + +TDX_BUILD_TDVPS_ACCESSORS (16 ,VMCS , vmcs); +TDX_BUILD_TDVPS_ACCESSORS (32 ,VMCS , vmcs); +TDX_BUILD_TDVPS_ACCESSORS (64 ,VMCS , vmcs); + +TDX_BUILD_TDVPS_ACCESSORS (64 ,STATE_NON_ARCH , state_non_arch); +TDX_BUILD_TDVPS_ACCESSORS (8 ,MANAGEMENT , management); +#else static inline int tdx_module_setup (void ) {return -ENODEV ; };
Signature · 1 line
2.25.1
v5 cover KVM TDX basic feature support
From: Isaku Yamahata <[email protected]>
Hi. Now TDX host kernel patch series was posted, I've rebased this patch series to it and make it work.
https://lore.kernel.org/lkml/[email protected]/
Changes from v4:
- rebased to TDX host kernel patch series.
- include all the patches to make this patch series working.
- add [MARKER] patches to mark the patch layer clear.
Thanks,
* What's TDX?
TDX stands for Trust Domain Extensions, which extends Intel Virtual Machines Extensions (VMX) to introduce a kind of virtual machine guest called a Trust Domain (TD) for confidential computing.
A TD runs in a CPU mode that is designed to protect the confidentiality of its memory contents and its CPU state from any other software, including the hosting Virtual Machine Monitor (VMM), unless explicitly shared by the TD itself.
We have more detailed explanations below (***). We have the high-level design of TDX KVM below (****).
In this patch series, we use "TD" or "guest TD" to differentiate it from the current "VM" (Virtual Machine), which is supported by KVM today.
* The organization of this patch series
This patch series is on top of the patches series "TDX host kernel support":
https://lore.kernel.org/lkml/[email protected]/
this patch series is available at
https://github.com/intel/tdx/releases/tag/kvm-upstream
The corresponding patches to qemu are available at
https://github.com/intel/qemu-tdx/commits/tdx-upstream
The relations of the layers are depicted as follows.
The arrows below show the order of patch reviews we would like to have.
The below layers are chosen so that the device model, for example, qemu can exercise each layering step by step. Check if TDX is supported, create TD VM, create TD vcpu, allow vcpu running, populate TD guest private memory, and handle vcpu exits/hypercalls/interrupts to run TD fully.
TDX vcpu
interrupt/exits/hypercall<------------\
^ |
| |
TD finalization |
^ |
| |
TDX EPT violation<------------\ |
^ | |
| | |
TD vcpu enter/exit | |
^ | |
| | |
TD vcpu creation/destruction | \-------KVM TDP MMU MapGPA
^ | ^
| | |
TD VM creation/destruction \---------------KVM TDP MMU hooks ^ ^
| |
TDX architectural definitions KVM TDP refactoring for TDX
^ ^
| |
TDX, VMX <--------TDX host kernel KVM MMU GPA stolen bits
coexistence support
The followings are explanations of each layer. Each layer has a dummy commit that starts with [MARKER] in subject. It is intended to help to identify where each layer starts.
TDX host kernel support:
https://lore.kernel.org/lkml/[email protected]/
The guts of system-wide initialization of TDX module. There is an independent patch series for host x86. TDX KVM patches call functions this patch series provides to initialize the TDX module.
TDX, VMX coexistence:
Infrastructure to allow TDX to coexist with VMX and trigger the initialization of the TDX module.
This layer starts with
"KVM: VMX: Move out vmx_x86_ops to 'main.c' to wrap VMX and TDX"
TDX architectural definitions:
Add TDX architectural definitions and helper functions
This layer starts with
"[MARKER] The start of TDX KVM patch series: TDX architectural definitions".
TD VM creation/destruction:
Guest TD creation/destroy allocation and releasing of TDX specific vm and vcpu structure. Create an initial guest memory image with TDX measurement.
This layer starts with
"[MARKER] The start of TDX KVM patch series: TD VM creation/destruction".
TD vcpu creation/destruction:
guest TD creation/destroy Allocation and releasing of TDX specific vm and vcpu structure. Create an initial guest memory image with TDX measurement.
This layer starts with
"[MARKER] The start of TDX KVM patch series: TD vcpu creation/destruction"
TDX EPT violation:
Create an initial guest memory image with TDX measurement. Handle secure EPT violations to populate guest pages with TDX SEAMCALLs. This layer starts with
"[MARKER] The start of TDX KVM patch series: TDX EPT violation"
TD vcpu enter/exit:
Allow TDX vcpu to enter into TD and exit from TD. Save CPU state before entering into TD. Restore CPU state after exiting from TD.
This layer starts with
"[MARKER] The start of TDX KVM patch series: TD vcpu enter/exit"
TD vcpu interrupts/exit/hypercall:
Handle various exits/hypercalls and allow interrupts to be injected so that TD vcpu can continue running.
This layer starts with
"[MARKER] The start of TDX KVM patch series: TD vcpu exits/interrupts/hypercalls"
KVM MMU GPA stolen bits:
Introduce framework to handle stolen repurposed bit of GPA TDX repurposed a bit of GPA to indicate shared or private. If it's shared, it's the same as the conventional VMX EPT case. VMM can access shared guest pages. If it's private, it's handled by Secure-EPT and the guest page is encrypted.
This layer starts with
"[MARKER] The start of TDX KVM patch series: KVM MMU GPA stolen bits"
KVM TDP refactoring for TDX:
TDX Secure EPT requires different constants. e.g. initial value EPT entry value etc. Various refactoring for those differences. This layer starts with
"[MARKER] The start of TDX KVM patch series: KVM TDP refactoring for TDX"
KVM TDP MMU hooks:
Introduce framework to TDP MMU to add hooks in addition to direct EPT access TDX added Secure EPT which is an enhancement to VMX EPT. Unlike conventional VMX EPT, CPU can't directly read/write Secure EPT. Instead, use TDX SEAMCALLs to operate on Secure EPT.
This layer starts with
"[MARKER] The start of TDX KVM patch series: KVM TDP MMU hooks"
KVM TDP MMU MapGPA:
Introduce framework to handle switching guest pages from private/shared to shared/private. For a given GPA, a guest page can be assigned to a private GPA or a shared GPA exclusively. With TDX MapGPA hypercall, guest TD converts GPA assignments from private (or shared) to shared (or private).
This layer starts with
"[MARKER] The start of TDX KVM patch series: KVM TDP MMU MapGPA "
KVM guest private memory: (not shown in the above diagram)
[PATCH v4 00/12] KVM: mm: fd-based approach for supporting KVM guest private
memory: https://lkml.org/lkml/2022/1/18/395
Guest private memory requires different memory management in KVM. The patch proposes a way for it. Integration with TDX KVM.
(***)
* TDX module
A CPU-attested software module called the "TDX module" is designed to implement the TDX architecture, and it is loaded by the UEFI firmware today. It can be loaded by the kernel or driver at runtime, but in this patch series we assume that the TDX module is already loaded and initialized.
The TDX module provides two main new logical modes of operation built upon the new SEAM (Secure Arbitration Mode) root and non-root CPU modes added to the VMX architecture. TDX root mode is mostly identical to the VMX root operation mode, and the TDX functions (described later) are triggered by the new SEAMCALL instruction with the desired interface function selected by an input operand (leaf number, in RAX). TDX non-root mode is used for TD guest operation. TDX non-root operation (i.e. "guest TD" mode) is similar to the VMX non-root operation (i.e. guest VM), with changes and restrictions to better assure that no other software or hardware has direct visibility of the TD memory and state.
TDX transitions between TDX root operation and TDX non-root operation include TD Entries, from TDX root to TDX non-root mode, and TD Exits from TDX non-root to TDX root mode. A TD Exit might be asynchronous, triggered by some external event (e.g., external interrupt or SMI) or an exception, or it might be synchronous, triggered by a TDCALL (TDG.VP.VMCALL) function.
TD VCPUs can be entered using SEAMCALL(TDH.VP.ENTER) by KVM. TDH.VP.ENTER is one of the TDX interface functions as mentioned above, and "TDH" stands for Trust Domain Host. Those host-side TDX interface functions are categorized into various areas just for better organization, such as SYS (TDX module management), MNG (TD management), VP (VCPU), PHYSMEM (physical memory), MEM (private memory), etc. For example, SEAMCALL(TDH.SYS.INFO) returns the TDX module information.
TDCS (Trust Domain Control Structure) is the main control structure of a guest TD, and encrypted (using the guest TD's ephemeral private key). At a high level, TDCS holds information for controlling TD operation as a whole, execution, EPTP, MSR bitmaps, etc that KVM needs to set it up. Note that MSR bitmaps are held as part of TDCS (unlike VMX) because they are meant to have the same value for all VCPUs of the same TD.
Trust Domain Virtual Processor State (TDVPS) is the root control structure of a TD VCPU. It helps the TDX module control the operation of the VCPU, and holds the VCPU state while the VCPU is not running. TDVPS is opaque to software and DMA access, accessible only by using the TDX module interface functions (such as TDH.VP.RD, TDH.VP.WR). TDVPS includes TD VMCS, and TD VMCS auxiliary structures, such as virtual APIC page, virtualization exception information, etc.
Several VMX control structures (such as Shared EPT and Posted interrupt descriptor) are directly managed and accessed by the host VMM. These control structures are pointed to by fields in the TD VMCS.
The above means that 1) KVM needs to allocate different data structures for TDs,
2) KVM can reuse the existing code for TDs for some operations, 3) it needs to define TD-specific handling for others. 3) Redirect operations to . 3) Redirect operations to the TDX specific callbacks, like "if (is_td_vcpu(vcpu)) tdx_callback() else vmx_callback();".
*TD Private Memory
TD private memory is designed to hold TD private content, encrypted by the CPU using the TD ephemeral key. An encryption engine holds a table of encryption keys, and an encryption key is selected for each memory transaction based on a Host Key Identifier (HKID). By design, the host VMM does not have access to the encryption keys.
In the first generation of MKTME, HKID is "stolen" from the physical address by allocating a configurable number of bits from the top of the physical address. The HKID space is partitioned into shared HKIDs for legacy MKTME accesses and private HKIDs for SEAM-mode-only accesses. We use 0 for the shared HKID on the host so that MKTME can be opaque or bypassed on the host.
During TDX non-root operation (i.e. guest TD), memory accesses can be qualified as either shared or private, based on the value of a new SHARED bit in the Guest Physical Address (GPA). The CPU translates shared GPAs using the usual VMX EPT (Extended Page Table) or "Shared EPT" (in this document), which resides in host VMM memory. The Shared EPT is directly managed by the host VMM - the same as with the current VMX. Since guest TDs usually require I/O, and the data exchange needs to be done via shared memory, thus KVM needs to use the current EPT functionality even for TDs.
* Secure EPT and Minoring using the TDP code
The CPU translates private GPAs using a separate Secure EPT. The Secure EPT pages are encrypted and integrity-protected with the TD's ephemeral private key. Secure EPT can be managed _indirectly_ by the host VMM, using the TDX interface functions, and thus conceptually Secure EPT is a subset of EPT (why "subset"). Since execution of such interface functions takes much longer time than accessing memory directly, in KVM we use the existing TDP code to minor the Secure EPT for the TD.
This way, we can effectively walk Secure EPT without using the TDX interface functions.
* VM life cycle and TDX specific operations
The userspace VMM, such as QEMU, needs to build and treat TDs differently. For example, a TD needs to boot in private memory, and the host software cannot copy the initial image to private memory.
* TSC Virtualization
The TDX module helps TDs maintain reliable TSC (Time Stamp Counter) values (e.g. consistent among the TD VCPUs) and the virtual TSC frequency is determined by TD configuration, i.e. when the TD is created, not per VCPU. The current KVM owns TSC virtualization for VMs, but the TDX module does for TDs.
* MCE support for TDs
The TDX module doesn't allow VMM to inject MCE. Instead PV way is needed for TD to communicate with VMM. For now, KVM silently ignores MCE request by VMM. MSRs related to MCE (e.g, MCE bank registers) can be naturally emulated by paravirtualizing MSR access.
[1] For details, the specifications, [2], [3], [4], [5], [6], [7], are available.
* Restrictions or future work
Some features are not included to reduce patch size. Those features are addressed as future independent patch series.
- large page (2M, 1G)
- qemu gdb stub
- guest PMU
- and more
* Prerequisites
It's required to load the TDX module and initialize it. It's out of the scope of this patch series. Another independent patch for the common x86 code is planned. It defines CONFIG_INTEL_TDX_HOST and this patch series uses CONFIG_INTEL_TDX_HOST. It's assumed that With CONFIG_INTEL_TDX_HOST=y, the TDX module is initialized and ready for KVM to use the TDX module APIs for TDX guest life cycle like tdh.mng.init are ready to use.
Concretely Global initialization, LP (Logical Processor) initialization, global configuration, the key configuration, and TDMR and PAMT initialization are done. The state of the TDX module is SYS_READY. Please refer to the TDX module specification, the chapter Intel TDX Module Lifecycle State Machine
** Detecting the TDX module readiness.
TDX host patch series implements the detection of the TDX module availability and its initialization so that KVM can use it. Also it manages Host KeyID (HKID) assigned to guest TD.
The assumed APIs the TDX host patch series provides are
- int seamrr_enabled()
Check if required cpu feature (SEAM mode) is available. This only check CPU feature availability. At this point, the TDX module may not be ready for KVM to use.
- int init_tdx(void);
Initialization of TDX module so that the TDX module is ready for KVM to use.
- const struct tdsysinfo_struct *tdx_get_sysinfo(void);
Return the system wide information about the TDX module. NULL if the TDX isn't initialized.
- u32 tdx_get_global_keyid(void);
Return global key id that is used for the TDX module itself.
- int tdx_keyid_alloc(void);
Allocate HKID for guest TD.
- void tdx_keyid_free(int keyid);
Free HKID for guest TD.
(****)
* TDX KVM high-level design
- Host key ID management
Host Key ID (HKID) needs to be assigned to each TDX guest for memory encryption. It is assumed The TDX host patch series implements necessary functions, u32 tdx_get_global_keyid(void), int tdx_keyid_alloc(void) and,
void tdx_keyid_free(int keyid).
- Data structures and VM type
Because TDX is different from VMX, define its own VM/VCPU structures, struct kvm_tdx and struct vcpu_tdx instead of struct kvm_vmx and struct vcpu_vmx. To identify the VM, introduce VM-type to specify which VM type, VMX (default) or TDX, is used.
- VM life cycle and TDX specific operations
Re-purpose the existing KVM_MEMORY_ENCRYPT_OP to add TDX specific operations. New commands are used to get the TDX system parameters, set TDX specific VM/VCPU parameters, set initial guest memory and measurement.
The creation of TDX VM requires five additional operations in addition to the conventional VM creation.
- Get KVM system capability to check if TDX VM type is supported
- VM creation (KVM_CREATE_VM)
- New: Get the TDX specific system parameters. KVM_TDX_GET_CAPABILITY.
- New: Set TDX specific VM parameters. KVM_TDX_INIT_VM.
- VCPU creation (KVM_CREATE_VCPU)
- New: Set TDX specific VCPU parameters. KVM_TDX_INIT_VCPU.
- New: Initialize guest memory as boot state and extend the measurement with the memory. KVM_TDX_INIT_MEM_REGION.
- New: Finalize VM. KVM_TDX_FINALIZE. Complete measurement of the initial TDX VM contents.
- VCPU RUN (KVM_VCPU_RUN)
- Protected guest state
Because the guest state (CPU state and guest memory) is protected, the KVM VMM can't operate on them. For example, accessing CPU registers, injecting exceptions, and accessing guest memory. Those operations are handled as silently ignored, returning zero or initial reset value when it's requested via KVM API ioctls.
VM/VCPU state and callbacks for TDX specific operations. Define tdx specific VM state and VCPU state instead of VMX ones. Redirect operations to TDX specific callbacks. "if (tdx) tdx_op() else vmx_op()".
Operations on the CPU state
silently ignore operations on the guest state. For example, the write to CPU registers is ignored and the read from CPU registers returns 0.
. ignore access to CPU registers except for allowed ones.
. TSC: add a check if tsc is immutable and return an error. Because the KVM
implementation updates the internal tsc state and it's difficult to back out those changes. Instead, skip the logic.
. dirty logging: add check if dirty logging is supported.
. exceptions/SMI/MCE/SIPI/INIT: silently ignore
Note: virtual external interrupt and NMI can be injected into TDX guests.
- KVM MMU integration
One bit of the guest physical address (bit 51 or 47) is repurposed to indicate if the guest physical address is private (the bit is cleared) or shared (the bit is set). The bits are called stolen bits.
- Stolen bits framework
systematically tracks which guest physical address, shared or private, is used.
- Shared EPT and secure EPT
There are two EPTs. Shared EPT (the conventional one) and Secure EPT(the new one). Shared EPT is handled the same for the stolen bit set. Secure EPT points to private guest pages. To resolve EPT violation, KVM walks one of two EPTs based on faulted GPA. Because it's costly to access secure EPT during walking EPTs with SEAMCALLs for the private guest physical address, another private EPT is used as a shadow of Secure-EPT with the existing logic at the cost of extra memory.
The following depicts the relationship.
KVM | TDX module
| | |
-------------+---------- | |
| | | |
V V | |
shared GPA private GPA | |
CPU shared EPT pointer KVM private EPT pointer | CPU secure EPT pointer
| | | |
| | | |
V V | V
shared EPT private EPT<-------mirror----->Secure EPT
| | | |
| \--------------------+------\ |
| | | |
V | V V
shared guest page | private guest page
|
|
non-encrypted memory | encrypted memory
| - Operating on Secure EPT
Use the TDX module APIs to operate on Secure EPT. To call the TDX API during resolving EPT violation, add hooks to additional operation and wiring it to TDX backend.
* References
[1] TDX specification
https://software.intel.com/content/www/us/en/develop/articles/intel-trust-domain-extensions.html
[2] Intel Trust Domain Extensions (Intel TDX)
https://software.intel.com/content/dam/develop/external/us/en/documents/tdx-whitepaper-final9-17.pdf
[3] Intel CPU Architectural Extensions Specification
https://software.intel.com/content/dam/develop/external/us/en/documents-tps/intel-tdx-cpu-architectural-specification.pdf
[4] Intel TDX Module 1.0 EAS
https://software.intel.com/content/dam/develop/external/us/en/documents/tdx-module-1eas-v0.85.039.pdf
[5] Intel TDX Loader Interface Specification
https://software.intel.com/content/dam/develop/external/us/en/documents-tps/intel-tdx-seamldr-interface-specification.pdf
[6] Intel TDX Guest-Hypervisor Communication Interface
https://software.intel.com/content/dam/develop/external/us/en/documents/intel-tdx-guest-hypervisor-communication-interface.pdf
[7] Intel TDX Virtual Firmware Design Guide
https://software.intel.com/content/dam/develop/external/us/en/documents/tdx-virtual-firmware-design-guide-rev-1.pdf
[8] intel public github
kvm TDX branch: https://github.com/intel/tdx/tree/kvm
TDX guest branch: https://github.com/intel/tdx/tree/guest
qemu TDX https://github.com/intel/qemu-tdx
[9] TDVF
https://github.com/tianocore/edk2-staging/tree/TDVF
Chao Gao (1):
KVM: x86: Allow to update cached values in kvm_user_return_msrs w/o
wrmsr
Isaku Yamahata (73):
x86/virt/tdx: export platform_has_tdx
KVM: TDX: Detect CPU feature on kernel module initialization
KVM: x86: Refactor KVM VMX module init/exit functions
KVM: TDX: Add placeholders for TDX VM/vcpu structure x86/virt/tdx: Add a helper function to return system wide info about
TDX module
KVM: TDX: Add a function to initialize TDX module
KVM: TDX: Make TDX VM type supported
[MARKER] The start of TDX KVM patch series: TDX architectural
definitions
KVM: TDX: Define TDX architectural definitions
KVM: TDX: Add a function for KVM to invoke SEAMCALL
KVM: TDX: add a helper function for KVM to issue SEAMCALL
KVM: TDX: Add helper functions to print TDX SEAMCALL error [MARKER] The start of TDX KVM patch series: TD VM creation/destruction
KVM: TDX: allocate per-package mutex
x86/cpu: Add helper functions to allocate/free MKTME keyid
KVM: TDX: Add place holder for TDX VM specific mem_enc_op ioctl
KVM: TDX: x86: Add vm ioctl to get TDX systemwide parameters [MARKER] The start of TDX KVM patch series: TD vcpu
creation/destruction
KVM: TDX: allocate/free TDX vcpu structure
[MARKER] The start of TDX KVM patch series: KVM MMU GPA stolen bits
KVM: x86/mmu: introduce config for PRIVATE KVM MMU [MARKER] The start of TDX KVM patch series: KVM TDP refactoring for
TDX
KVM: x86/mmu: Disallow fast page fault on private GPA [MARKER] The start of TDX KVM patch series: KVM TDP MMU hooks
KVM: x86/tdp_mmu: make REMOVED_SPTE include shadow_initial value
KVM: x86/tdp_mmu: refactor kvm_tdp_mmu_map()
KVM: x86/mmu: add a private pointer to struct kvm_mmu_page
KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
KVM: x86/tdp_mmu: Ignore unsupported mmu operation on private GFNs [MARKER] The start of TDX KVM patch series: TDX EPT violation
KVM: TDX: TDP MMU TDX support
[MARKER] The start of TDX KVM patch series: KVM TDP MMU MapGPA
KVM: x86/mmu: steal software usable bit for EPT to represent shared
page
KVM: x86/tdp_mmu: Keep PRIVATE_PROHIBIT bit when zapping
KVM: x86/tdp_mmu: prevent private/shared map based on PRIVATE_PROHIBIT
KVM: x86/tdp_mmu: implement MapGPA hypercall for TDX
KVM: x86/mmu: Focibly use TDP MMU for TDX
[MARKER] The start of TDX KVM patch series: TD finalization
KVM: TDX: Create initial guest memory
KVM: TDX: Finalize VM initialization
[MARKER] The start of TDX KVM patch series: TD vcpu enter/exit
KVM: TDX: Add helper assembly function to TDX vcpu
KVM: TDX: Implement TDX vcpu enter/exit path
KVM: TDX: vcpu_run: save/restore host state(host kernel gs)
KVM: TDX: restore host xsave state when exit from the guest TD
KVM: TDX: restore user ret MSRs
[MARKER] The start of TDX KVM patch series: TD vcpu
exits/interrupts/hypercalls
KVM: TDX: complete interrupts after tdexit
KVM: TDX: restore debug store when TD exit
KVM: TDX: handle vcpu migration over logical processor
KVM: TDX: track LP tdx vcpu run and teardown vcpus on descroing the
guest TD
KVM: x86: Add a switch_db_regs flag to handle TDX's auto-switched
behavior
KVM: TDX: Implement interrupt injection
KVM: TDX: Implements vcpu request_immediate_exit
KVM: TDX: Implement methods to inject NMI
KVM: TDX: Add a place holder to handle TDX VM exit
KVM: TDX: handle EXIT_REASON_OTHER_SMI
KVM: TDX: handle ept violation/misconfig exit
KVM: TDX: handle EXCEPTION_NMI and EXTERNAL_INTERRUPT
KVM: TDX: Add TDG.VP.VMCALL accessors to access guest vcpu registers
KVM: TDX: handle KVM hypercall with TDG.VP.VMCALL
KVM: TDX: Handle TDX PV CPUID hypercall
KVM: TDX: Handle TDX PV HLT hypercall
KVM: TDX: Handle TDX PV port io hypercall
KVM: TDX: Implement callbacks for MSR operations for TDX
KVM: TDX: Handle TDX PV rdmsr hypercall
KVM: TDX: Handle TDX PV wrmsr hypercall
KVM: TDX: Handle TDX PV report fatal error hypercall
KVM: TDX: Handle TDX PV map_gpa hypercall
KVM: TDX: Silently discard SMI request
KVM: TDX: Silently ignore INIT/SIPI Documentation/virtual/kvm: Document on Trust Domain Extensions(TDX)
KVM: x86: design documentation on TDX support of x86 KVM TDP MMU
Kai Huang (1):
KVM: x86: Introduce hooks to free VM callback prezap and vm_free
Rick Edgecombe (1):
KVM: x86: Add infrastructure for stolen GPA bits
Sean Christopherson (26):
KVM: VMX: Move out vmx_x86_ops to 'main.c' to wrap VMX and TDX
KVM: Enable hardware before doing arch VM initialization
KVM: x86: Introduce vm_type to differentiate default VMs from
confidential VMs
KVM: TDX: Add TDX "architectural" error codes
KVM: TDX: Add C wrapper functions for SEAMCALLs to the TDX module
KVM: TDX: Stub in tdx.h with structs, accessors, and VMCS helpers
KVM: Add max_vcpus field in common 'struct kvm'
KVM: TDX: create/destroy VM structure
KVM: TDX: Do TDX specific vcpu initialization
KVM: x86/mmu: Disallow dirty logging for x86 TDX
KVM: x86/mmu: Explicitly check for MMIO spte in fast page fault
KVM: x86/mmu: Allow non-zero init value for shadow PTE
KVM: x86/mmu: Allow per-VM override of the TDP max page level
KVM: VMX: Split out guts of EPT violation to common/exposed function
KVM: VMX: Move setting of EPT MMU masks to common VT-x code
KVM: x86/mmu: Track shadow MMIO value/mask on a per-VM basis
KVM: TDX: Add load_mmu_pgd method for TDX
KVM: x86/mmu: Introduce kvm_mmu_map_tdp_page() for use by TDX
KVM: x86: Check for pending APICv interrupt in kvm_vcpu_has_events()
KVM: x86: Add option to force LAPIC expiration wait
KVM: VMX: Modify NMI and INTR handlers to take intr_info as function
argument
KVM: VMX: Move NMI/exception handler to common helper
KVM: x86: Split core of hypercall emulation to helper function
KVM: TDX: Add a placeholder for handler of TDX hypercalls
(TDG.VP.VMCALL)
KVM: TDX: Handle TDX PV MMIO hypercall
KVM: TDX: Add methods to ignore accesses to CPU state
Xiaoyao Li (1):
KVM: TDX: initialize VM with TDX specific parameters
Yuan Yao (1):
KVM: TDX: Use vcpu_to_pi_desc() uniformly in posted_intr.c
Documentation/virt/kvm/api.rst | 24 +- .../virt/kvm/intel-tdx-layer-status.rst | 33 + Documentation/virt/kvm/intel-tdx.rst | 360 +++ Documentation/virt/kvm/tdx-tdp-mmu.rst | 466 ++++ arch/arm64/include/asm/kvm_host.h | 3 - arch/arm64/kvm/arm.c | 6 +- arch/arm64/kvm/vgic/vgic-init.c | 6 +- arch/x86/events/intel/ds.c | 1 + arch/x86/include/asm/kvm-x86-ops.h | 5 + arch/x86/include/asm/kvm_host.h | 38 +- arch/x86/include/asm/tdx.h | 61 + arch/x86/include/asm/vmx.h | 2 + arch/x86/include/uapi/asm/kvm.h | 59 + arch/x86/include/uapi/asm/vmx.h | 5 +- arch/x86/kvm/Kconfig | 4 + arch/x86/kvm/Makefile | 3 +- arch/x86/kvm/lapic.c | 25 +- arch/x86/kvm/lapic.h | 2 +- arch/x86/kvm/mmu.h | 65 +- arch/x86/kvm/mmu/mmu.c | 232 +- arch/x86/kvm/mmu/mmu_internal.h | 84 + arch/x86/kvm/mmu/paging_tmpl.h | 25 +- arch/x86/kvm/mmu/spte.c | 48 +- arch/x86/kvm/mmu/spte.h | 40 +- arch/x86/kvm/mmu/tdp_iter.h | 2 +- arch/x86/kvm/mmu/tdp_mmu.c | 642 ++++- arch/x86/kvm/mmu/tdp_mmu.h | 16 +- arch/x86/kvm/svm/svm.c | 10 +- arch/x86/kvm/vmx/common.h | 155 ++ arch/x86/kvm/vmx/main.c | 1026 ++++++++ arch/x86/kvm/vmx/posted_intr.c | 8 +- arch/x86/kvm/vmx/seamcall.S | 55 + arch/x86/kvm/vmx/seamcall.h | 25 + arch/x86/kvm/vmx/tdx.c | 2337 +++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 253 ++ arch/x86/kvm/vmx/tdx_arch.h | 158 ++ arch/x86/kvm/vmx/tdx_errno.h | 29 + arch/x86/kvm/vmx/tdx_error.c | 22 + arch/x86/kvm/vmx/tdx_ops.h | 174 ++ arch/x86/kvm/vmx/vmenter.S | 146 + arch/x86/kvm/vmx/vmx.c | 619 ++--- arch/x86/kvm/vmx/x86_ops.h | 235 ++ arch/x86/kvm/x86.c | 123 +- arch/x86/kvm/x86.h | 8 + arch/x86/virt/tdxcall.S | 8 +- arch/x86/virt/vmx/tdx.c | 50 +- arch/x86/virt/vmx/tdx.h | 52 - include/linux/kvm_host.h | 2 + include/uapi/linux/kvm.h | 1 + tools/arch/x86/include/uapi/asm/kvm.h | 59 + tools/include/uapi/linux/kvm.h | 1 + virt/kvm/kvm_main.c | 35 +- 52 files changed, 7142 insertions(+), 706 deletions(-) create mode 100644 Documentation/virt/kvm/intel-tdx-layer-status.rst create mode 100644 Documentation/virt/kvm/intel-tdx.rst create mode 100644 Documentation/virt/kvm/tdx-tdp-mmu.rst create mode 100644 arch/x86/kvm/vmx/common.h create mode 100644 arch/x86/kvm/vmx/main.c create mode 100644 arch/x86/kvm/vmx/seamcall.S create mode 100644 arch/x86/kvm/vmx/seamcall.h create mode 100644 arch/x86/kvm/vmx/tdx.c create mode 100644 arch/x86/kvm/vmx/tdx.h create mode 100644 arch/x86/kvm/vmx/tdx_arch.h create mode 100644 arch/x86/kvm/vmx/tdx_errno.h create mode 100644 arch/x86/kvm/vmx/tdx_error.c create mode 100644 arch/x86/kvm/vmx/tdx_ops.h create mode 100644 arch/x86/kvm/vmx/x86_ops.h
Signature · 1 line
2.25.1
RFC PATCH v5 51/104 KVM: TDX: TDP MMU TDX support
From: Isaku Yamahata <[email protected]>
Implement hooks of TDP MMU for TDX backend. TLB flush, TLB shootdown, propagating the change private EPT entry to Secure EPT and freeing Secure EPT page.
TLB flush handles both shared EPT and private EPT. It flushes shared EPT same as VMX. It also waits for the TDX TLB shootdown.
For the hook to free Secure EPT page, unlinks the Secure EPT page from the Secure EPT so that the page can be freed to OS.
Propagating the entry change to Secure EPT. The possible entry changes are present -> non-present(zapping) and non-present -> present(population). On population just link the Secure EPT page or the private guest page to the Secure EPT by TDX SEAMCALL.
Because TDP MMU allows concurrent zapping/population, zapping requires synchronous TLB shootdown with the frozen EPT entry. It zaps the secure entry, increments TLB counter, sends IPI to remote vcpus to trigger TLB flush, and then unlinks the private guest page from the Secure EPT.
For simplicity, batched zapping with exclude lock is handled as concurrent zapping. Although it's inefficient, it can be optimized in the future.
--- arch/x86/kvm/vmx/main.c | 40 +++++- arch/x86/kvm/vmx/tdx.c | 246 +++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 14 +++ arch/x86/kvm/vmx/tdx_ops.h | 3 + arch/x86/kvm/vmx/x86_ops.h | 2 + 5 files changed, 301 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx/main.c +36 -4
@@ -89,6 +89,38 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)return vmx_vcpu_reset (vcpu, init_event); } +static void vt_flush_tlb_all (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_flush_tlb (vcpu); + +vmx_flush_tlb_all (vcpu); +} + +static void vt_flush_tlb_current (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_flush_tlb (vcpu); + +vmx_flush_tlb_current (vcpu); +} + +static void vt_flush_tlb_gva (struct kvm_vcpu *vcpu,gva_t addr) +{ +if (KVM_BUG_ON (is_td_vcpu (vcpu), vcpu->kvm)) +return ; + +vmx_flush_tlb_gva (vcpu, addr); +} + +static void vt_flush_tlb_guest (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return ; + +vmx_flush_tlb_guest (vcpu); +} +static void vt_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int pgd_level) {
@@ -162,10 +194,10 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .set_rflags = vmx_set_rflags, .get_if_flag = vmx_get_if_flag, - .tlb_flush_all = vmx_flush_tlb_all, - .tlb_flush_current = vmx_flush_tlb_current, - .tlb_flush_gva = vmx_flush_tlb_gva, - .tlb_flush_guest = vmx_flush_tlb_guest, + .tlb_flush_all = vt_flush_tlb_all, + .tlb_flush_current = vt_flush_tlb_current, + .tlb_flush_gva = vt_flush_tlb_gva, + .tlb_flush_guest = vt_flush_tlb_guest, .vcpu_pre_run = vmx_vcpu_pre_run, .run = vmx_vcpu_run,
arch/x86/kvm/vmx/tdx.c +246 -0
283 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.h +14 -0
@@ -29,9 +29,17 @@ struct kvm_tdx {struct kvm_cpuid_entry2 cpuid_entries[KVM_MAX_CPUID_ENTRIES ];bool finalized; +bool tdh_mem_track;u64 tsc_offset;unsigned long tsc_khz; + +/* +* Lock to prevent seamcalls from running concurrently +* when TDP MMU is enabled, because TDP fault handler +* runs concurrently. +*/ +spinlock_t seamcall_lock; };struct vcpu_tdx {
@@ -166,6 +174,12 @@ static __always_inline u64 td_tdcs_exec_read64(struct kvm_tdx *kvm_tdx, u32 fielreturn out.r8; } +static __always_inline int pg_level_to_tdx_sept_level(enum pg_level level) +{ +WARN_ON (level ==PG_LEVEL_NONE ); +return level -1 ; +} +#else #define enable_tdx falsestatic inline int tdx_module_setup (void ) {return -ENODEV ; };
arch/x86/kvm/vmx/tdx_ops.h +3 -0
@@ -30,12 +30,14 @@ static inline u64 tdh_mng_addcx(hpa_t tdr, hpa_t addr)static inline u64 tdh_mem_page_add (hpa_t tdr,gpa_t gpa,hpa_t hpa,hpa_t source,struct tdx_module_output *out) { +tdx_clflush_page (hpa);return kvm_seamcall (TDH_MEM_PAGE_ADD , gpa, tdr, hpa, source,0 , out); }static inline u64 tdh_mem_sept_add (hpa_t tdr,gpa_t gpa,int level,hpa_t page,struct tdx_module_output *out) { +tdx_clflush_page (page);return kvm_seamcall (TDH_MEM_SEPT_ADD , gpa | level, tdr, page,0 ,0 , out); }
@@ -48,6 +50,7 @@ static inline u64 tdh_vp_addcx(hpa_t tdvpr, hpa_t addr)static inline u64 tdh_mem_page_aug (hpa_t tdr,gpa_t gpa,hpa_t hpa,struct tdx_module_output *out) { +tdx_clflush_page (hpa);return kvm_seamcall (TDH_MEM_PAGE_AUG , gpa, tdr, hpa,0 ,0 , out); }
arch/x86/kvm/vmx/x86_ops.h +2 -0
@@ -144,6 +144,7 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp); +void tdx_flush_tlb (struct kvm_vcpu *vcpu);void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);#else static inline void tdx_pre_kvm_init(
@@ -163,6 +164,7 @@ static inline void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) {}static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; } +static inline void tdx_flush_tlb (struct kvm_vcpu *vcpu) {}static inline void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level) {}#endif
Signature · 1 line
2.25.1
RFC PATCH v5 62/104 The start of TDX KVM patch series: TD vcpu enter/exit
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of TD vcpu enter/exit.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +3 -2
@@ -12,6 +12,7 @@ What qemu can do - Qemu can create/destroy guest of TDX vm type. - Qemu can create/destroy vcpu of TDX vm type. - Qemu can populate initial guest memory image. +- Qemu can finalize guest TD. Patch Layer status ------------------
@@ -21,8 +22,8 @@ Patch Layer status * TD VM creation/destruction: Applied * TD vcpu creation/destruction: Applied * TDX EPT violation: Applied -* TD finalization: Applying -* TD vcpu enter/exit: Not yet +* TD finalization: Applied +* TD vcpu enter/exit: Applying * TD vcpu interrupts/exit/hypercall: Not yet * KVM MMU GPA stolen bits: Applied
Signature · 1 line
2.25.1
RFC PATCH v5 65/104 KVM: TDX: vcpu_run: save/restore host state(host kernel gs)
In reply to [email protected]From: Isaku Yamahata <[email protected]>
On entering/exiting TDX vcpu, Preserved or clobbered CPU state is different from VMX case. Add TDX hooks to save/restore host/guest CPU state. Save/restore kernel GS base MSR.
--- arch/x86/kvm/vmx/main.c | 28 +++++++++++++++++++++++++-- arch/x86/kvm/vmx/tdx.c | 39 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 4 ++++ arch/x86/kvm/vmx/x86_ops.h | 4 ++++ 4 files changed, 73 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/main.c +26 -2
@@ -89,6 +89,30 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)return vmx_vcpu_reset (vcpu, init_event); } +static void vt_prepare_switch_to_guest (struct kvm_vcpu *vcpu) +{ +/* +* All host state is saved/restored across SEAMCALL/SEAMRET, and the +* guest state of a TD is obviously off limits. Deferring MSRs and DRs +* is pointless because the TDX module needs to load *something* so as +* not to expose guest state. +*/ +if (is_td_vcpu (vcpu)) { +tdx_prepare_switch_to_guest (vcpu); +return ; + } + +vmx_prepare_switch_to_guest (vcpu); +} + +static void vt_vcpu_put (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_vcpu_put (vcpu); + +return vmx_vcpu_put (vcpu); +} +static fastpath_t vt_vcpu_run (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))
@@ -174,9 +198,9 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_free = vt_vcpu_free, .vcpu_reset = vt_vcpu_reset, - .prepare_guest_switch = vmx_prepare_switch_to_guest, + .prepare_guest_switch = vt_prepare_switch_to_guest, .vcpu_load = vmx_vcpu_load, - .vcpu_put = vmx_vcpu_put, + .vcpu_put = vt_vcpu_put, .update_exception_bitmap = vmx_update_exception_bitmap, .get_msr_feature = vmx_get_msr_feature,
arch/x86/kvm/vmx/tdx.c +39 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <linux/cpu.h> +#include <linux/mmu_context.h> #include <asm/tdx.h>
@@ -407,6 +408,9 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu) vcpu->arch.guest_state_protected = !(to_kvm_tdx (vcpu->kvm)->attributes &TDX_TD_ATTRIBUTE_DEBUG ); + tdx->host_state_need_save = true; + tdx->host_state_need_restore = false; +return 0 ; free_tdvpx:
@@ -420,6 +424,39 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu)return ret; } +void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (!tdx->host_state_need_save) +return ; + +if (likely (is_64bit_mm (current->mm))) + tdx->msr_host_kernel_gs_base = current->thread.gsbase; +else + tdx->msr_host_kernel_gs_base =read_msr (MSR_KERNEL_GS_BASE ); + + tdx->host_state_need_save = false; +} + +static void tdx_prepare_switch_to_host (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + + tdx->host_state_need_save = true; +if (!tdx->host_state_need_restore) +return ; + +wrmsrl (MSR_KERNEL_GS_BASE , tdx->msr_host_kernel_gs_base); + tdx->host_state_need_restore = false; +} + +void tdx_vcpu_put (struct kvm_vcpu *vcpu) +{ +vmx_vcpu_pi_put (vcpu); +tdx_prepare_switch_to_host (vcpu); +} +void tdx_vcpu_free (struct kvm_vcpu *vcpu) {struct vcpu_tdx *tdx =to_tdx (vcpu);
@@ -535,6 +572,8 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (vcpu, tdx); + tdx->host_state_need_restore = true; + vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ;trace_kvm_exit (vcpu,KVM_ISA_VMX );
arch/x86/kvm/vmx/tdx.h +4 -0
@@ -84,6 +84,10 @@ struct vcpu_tdx {union tdx_exit_reason exit_reason;bool initialized; + +bool host_state_need_save; +bool host_state_need_restore; +u64 msr_host_kernel_gs_base; };static inline bool is_td (struct kvm *kvm)
arch/x86/kvm/vmx/x86_ops.h +4 -0
@@ -141,6 +141,8 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu);void tdx_vcpu_free (struct kvm_vcpu *vcpu);void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event);fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu); +void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu); +void tdx_vcpu_put (struct kvm_vcpu *vcpu);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -162,6 +164,8 @@ static inline int tdx_vcpu_create(struct kvm_vcpu *vcpu) { return -EOPNOTSUPP; }static inline void tdx_vcpu_free (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) {}static inline fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) {return EXIT_FASTPATH_NONE ; } +static inline void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {} +static inline void tdx_vcpu_put (struct kvm_vcpu *vcpu) {}static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
Signature · 1 line
2.25.1
RFC PATCH v5 64/104 KVM: TDX: Implement TDX vcpu enter/exit path
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This patch implements running TDX vcpu. Once vcpu runs on the logical processor (LP), the TDX vcpu is associated with it. When the TDX vcpu moves to another LP, the TDX vcpu needs to flush its status on the LP. When destroying TDX vcpu, it needs to complete flush and flush cpu memory cache. Track which LP the TDX vcpu run and flush it as necessary.
Do nothing on sched_in event as TDX doesn't support pause loop.
TDX vcpu execution requires restoring PMU debug store after returning back to KVM because the TDX module unconditionally resets the value. To reuse the existing code, export perf_restore_debug_store.
--- arch/x86/kvm/vmx/main.c | 10 +++++++++- arch/x86/kvm/vmx/tdx.c | 34 ++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 33 +++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 2 ++ arch/x86/kvm/x86.c | 1 + 5 files changed, 79 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/main.c +9 -1
@@ -89,6 +89,14 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)return vmx_vcpu_reset (vcpu, init_event); } +static fastpath_t vt_vcpu_run (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_vcpu_run (vcpu); + +return vmx_vcpu_run (vcpu); +} +static void vt_flush_tlb_all (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))
@@ -200,7 +208,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .tlb_flush_guest = vt_flush_tlb_guest, .vcpu_pre_run = vmx_vcpu_pre_run, - .run = vmx_vcpu_run, + .run = vt_vcpu_run, .handle_exit = vmx_handle_exit, .skip_emulated_instruction = vmx_skip_emulated_instruction, .update_emulated_instruction = vmx_update_emulated_instruction,
arch/x86/kvm/vmx/tdx.c +34 -0
@@ -10,6 +10,9 @@#include "vmx.h" #include "x86.h" +#include <trace/events/kvm.h> +#include "trace.h" +#undef pr_fmt#define pr_fmt (fmt) "tdx: " fmt
@@ -509,6 +512,37 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->kvm->vm_bugged = true; } +u64 __tdx_vcpu_run (hpa_t tdvpr,void *regs,u32 regs_mask); + +static noinstr void tdx_vcpu_enter_exit(struct kvm_vcpu *vcpu, +struct vcpu_tdx *tdx) +{ +guest_enter_irqoff (); + tdx->exit_reason.full =__tdx_vcpu_run (tdx->tdvpr.pa, vcpu->arch.regs,0 ); +guest_exit_irqoff (); +} + +fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (unlikely (vcpu->kvm->vm_bugged)) { + tdx->exit_reason.full =TDX_NON_RECOVERABLE_VCPU ; +return EXIT_FASTPATH_NONE ; + } + +trace_kvm_entry (vcpu); + +tdx_vcpu_enter_exit (vcpu, tdx); + + vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ; +trace_kvm_exit (vcpu,KVM_ISA_VMX ); + +if (tdx->exit_reason.error || tdx->exit_reason.non_recoverable) +return EXIT_FASTPATH_NONE ; +return EXIT_FASTPATH_NONE ; +} +void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int pgd_level) {td_vmcs_write64 (to_tdx (vcpu),SHARED_EPT_POINTER , root_hpa &PAGE_MASK );
arch/x86/kvm/vmx/tdx.h +33 -0
@@ -44,12 +44,45 @@ struct kvm_tdx {spinlock_t seamcall_lock; }; +union tdx_exit_reason { +struct { +/* 31:0 mirror the VMX Exit Reason format */ +u64 basic :16 ; +u64 reserved16 :1 ; +u64 reserved17 :1 ; +u64 reserved18 :1 ; +u64 reserved19 :1 ; +u64 reserved20 :1 ; +u64 reserved21 :1 ; +u64 reserved22 :1 ; +u64 reserved23 :1 ; +u64 reserved24 :1 ; +u64 reserved25 :1 ; +u64 bus_lock_detected :1 ; +u64 enclave_mode :1 ; +u64 smi_pending_mtf :1 ; +u64 smi_from_vmx_root :1 ; +u64 reserved30 :1 ; +u64 failed_vmentry :1 ; + +/* 63:32 are TDX specific */ +u64 details_l1 :8 ; +u64 class :8 ; +u64 reserved61_48 :14 ; +u64 non_recoverable :1 ; +u64 error :1 ; + }; +u64 full; +}; +struct vcpu_tdx {struct kvm_vcpu vcpu;struct tdx_td_page tdvpr;struct tdx_td_page *tdvpx; +union tdx_exit_reason exit_reason; +bool initialized; };
arch/x86/kvm/vmx/x86_ops.h +2 -0
@@ -140,6 +140,7 @@ void tdx_vm_free(struct kvm *kvm);int tdx_vcpu_create (struct kvm_vcpu *vcpu);void tdx_vcpu_free (struct kvm_vcpu *vcpu);void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event); +fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -160,6 +161,7 @@ static inline void tdx_vm_free(struct kvm *kvm) {}static inline int tdx_vcpu_create (struct kvm_vcpu *vcpu) {return -EOPNOTSUPP ; }static inline void tdx_vcpu_free (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) {} +static inline fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) {return EXIT_FASTPATH_NONE ; }static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
arch/x86/kvm/x86.c +1 -0
@@ -300,6 +300,7 @@ const struct kvm_stats_header kvm_vcpu_stats_header = { };u64 __read_mostly host_xcr0; +EXPORT_SYMBOL_GPL (host_xcr0);u64 __read_mostly supported_xcr0;EXPORT_SYMBOL_GPL (supported_xcr0);
Signature · 1 line
2.25.1
RFC PATCH v5 67/104 KVM: x86: Allow to update cached values in kvm_user_return_msrs w/o wrmsr
In reply to [email protected]From: Chao Gao <[email protected]>
Several MSRs are constant and only used in userspace(ring 3). But VMs may have different values. KVM uses kvm_set_user_return_msr() to switch to guest's values and leverages user return notifier to restore them when the kernel is to return to userspace. To eliminate unnecessary wrmsr, KVM also caches the value it wrote to an MSR last time.
TDX module unconditionally resets some of these MSRs to architectural INIT state on TD exit. It makes the cached values in kvm_user_return_msrs are inconsistent with values in hardware. This inconsistency needs to be fixed. Otherwise, it may mislead kvm_on_user_return() to skip restoring some MSRs to the host's values. kvm_set_user_return_msr() can help correct this case, but it is not optimal as it always does a wrmsr. So, introduce a variation of kvm_set_user_return_msr() to update cached values and skip that wrmsr.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1894,6 +1894,7 @@ int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low,int kvm_add_user_return_msr (u32 msr);int kvm_find_user_return_msr (u32 msr);int kvm_set_user_return_msr (unsigned index,u64 val,u64 mask); +void kvm_user_return_update_cache (unsigned int index,u64 val);static inline bool kvm_is_supported_user_return_msr (u32 msr) {
arch/x86/kvm/x86.c +20 -5
@@ -427,6 +427,15 @@ static void kvm_user_return_msr_cpu_online(void) } } +static void kvm_user_return_register_notifier (struct kvm_user_return_msrs *msrs) +{ +if (!msrs->registered) { + msrs->urn.on_user_return = kvm_on_user_return; +user_return_notifier_register (&msrs->urn); + msrs->registered = true; + } +} +int kvm_set_user_return_msr (unsigned slot,u64 value,u64 mask) {unsigned int cpu =smp_processor_id ();
@@ -441,15 +450,21 @@ int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask)return 1 ; msrs->values[slot].curr = value; -if (!msrs->registered) { - msrs->urn.on_user_return = kvm_on_user_return; -user_return_notifier_register (&msrs->urn); - msrs->registered = true; - } +kvm_user_return_register_notifier (msrs);return 0 ; }EXPORT_SYMBOL_GPL (kvm_set_user_return_msr); +/* Update the cache, "curr", and register the notifier */ +void kvm_user_return_update_cache (unsigned int slot,u64 value) +{ +struct kvm_user_return_msrs *msrs =this_cpu_ptr (user_return_msrs); + + msrs->values[slot].curr = value; +kvm_user_return_register_notifier (msrs); +} +EXPORT_SYMBOL_GPL (kvm_user_return_update_cache); +static void drop_user_return_notifiers (void ) {unsigned int cpu =smp_processor_id ();
Signature · 1 line
2.25.1
RFC PATCH v5 68/104 KVM: TDX: restore user ret MSRs
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Several user ret MSRs are clobbered on TD exit. Restore those values on TD exit and before returning to ring 3.
--- arch/x86/kvm/vmx/tdx.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
arch/x86/kvm/vmx/tdx.c +33 -0
@@ -550,6 +550,28 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->kvm->vm_bugged = true; } +struct tdx_uret_msr { +u32 msr; +unsigned int slot; +u64 defval; +}; + +static struct tdx_uret_msr tdx_uret_msrs[] = { + {.msr =MSR_SYSCALL_MASK ,}, + {.msr =MSR_STAR ,}, + {.msr =MSR_LSTAR ,}, + {.msr =MSR_TSC_AUX ,}, +}; + +static void tdx_user_return_update_cache (void ) +{ +int i; + +for (i =0 ; i <ARRAY_SIZE (tdx_uret_msrs); i++) +kvm_user_return_update_cache (tdx_uret_msrs[i].slot, + tdx_uret_msrs[i].defval); +} +static void tdx_restore_host_xsave_state (struct kvm_vcpu *vcpu) {struct kvm_tdx *kvm_tdx =to_kvm_tdx (vcpu->kvm);
@@ -589,6 +611,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (vcpu, tdx); +tdx_user_return_update_cache ();tdx_restore_host_xsave_state (vcpu); tdx->host_state_need_restore = true;
@@ -1371,6 +1394,16 @@ static int __init __tdx_hardware_setup(struct kvm_x86_ops *x86_ops)if (WARN_ON_ONCE (x86_ops->tlb_remote_flush))return -EIO ; +for (i =0 ; i <ARRAY_SIZE (tdx_uret_msrs); i++) { + tdx_uret_msrs[i].slot =kvm_find_user_return_msr (tdx_uret_msrs[i].msr); +if (tdx_uret_msrs[i].slot ==-1 ) { +/* If any MSR isn't supported, it is a KVM bug */ +pr_err ("MSR %x isn't included by kvm_find_user_return_msr\n" , + tdx_uret_msrs[i].msr); +return -EIO ; + } + } + max_pkgs =topology_max_packages (); tdx_mng_key_config_lock =kcalloc (max_pkgs,sizeof (*tdx_mng_key_config_lock),GFP_KERNEL );
Signature · 1 line
2.25.1
RFC PATCH v5 69/104 The start of TDX KVM patch series: TD vcpu exits/interrupts/hypercalls
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of TD vcpu exits, interrupts, and hypercalls.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +2 -1
@@ -13,6 +13,7 @@ What qemu can do - Qemu can create/destroy vcpu of TDX vm type. - Qemu can populate initial guest memory image. - Qemu can finalize guest TD. +- Qemu can start to run vcpu. But vcpu can not make progress yet. Patch Layer status ------------------
@@ -23,7 +24,7 @@ Patch Layer status * TD vcpu creation/destruction: Applied * TDX EPT violation: Applied * TD finalization: Applied -* TD vcpu enter/exit: Applying +* TD vcpu enter/exit: Applied * TD vcpu interrupts/exit/hypercall: Not yet * KVM MMU GPA stolen bits: Applied
Signature · 1 line
2.25.1
RFC PATCH v5 70/104 KVM: TDX: complete interrupts after tdexit
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This corresponds to VMX __vmx_complete_interrupts(). Because TDX virtualize vAPIC, KVM only needs to care NMI injection.
--- arch/x86/kvm/vmx/tdx.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
arch/x86/kvm/vmx/tdx.c +10 -0
@@ -550,6 +550,14 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->kvm->vm_bugged = true; } +static void tdx_complete_interrupts (struct kvm_vcpu *vcpu) +{ +/* Avoid costly SEAMCALL if no nmi was injected */ +if (vcpu->arch.nmi_injected) + vcpu->arch.nmi_injected =td_management_read8 (to_tdx (vcpu), +TD_VCPU_PEND_NMI ); +} +struct tdx_uret_msr {u32 msr;unsigned int slot;
@@ -618,6 +626,8 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ;trace_kvm_exit (vcpu,KVM_ISA_VMX ); +tdx_complete_interrupts (vcpu); +if (tdx->exit_reason.error || tdx->exit_reason.non_recoverable)return EXIT_FASTPATH_NONE ;return EXIT_FASTPATH_NONE ;
Signature · 1 line
2.25.1
RFC PATCH v5 63/104 KVM: TDX: Add helper assembly function to TDX vcpu
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDX defines an API to run TDX vcpu with its own ABI. Define an assembly helper function to run TDX vcpu to hide the special ABI so that C code can call it with function call ABI.
--- arch/x86/kvm/vmx/vmenter.S | 146 +++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+)
arch/x86/kvm/vmx/vmenter.S +146 -0
@@ -2,6 +2,7 @@ #include <linux/linkage.h> #include <asm/asm.h> #include <asm/bitsperlong.h> +#include <asm/errno.h> #include <asm/kvm_vcpu_regs.h> #include <asm/nospec-branch.h> #include <asm/segment.h>
@@ -28,6 +29,13 @@ #define VCPU_R15 __VCPU_REGS_R15 * WORD_SIZE #endif +#ifdef CONFIG_INTEL_TDX_HOST +#define TDENTER 0 +#define EXIT_REASON_TDCALL 77 +#define TDENTER_ERROR_BIT 63 +#include "seamcall.h" +#endif + .section .noinstr.text, "ax" /**
@@ -328,3 +336,141 @@ SYM_FUNC_START(vmx_do_interrupt_nmi_irqoff) pop %_ASM_BP RET SYM_FUNC_END(vmx_do_interrupt_nmi_irqoff) + +#ifdef CONFIG_INTEL_TDX_HOST + +.pushsection .noinstr.text, "ax" + +/** + * __tdx_vcpu_run - Call SEAMCALL(TDENTER) to run a TD vcpu + * @tdvpr: physical address of TDVPR + * @regs: void * (to registers of TDVCPU) + * @gpr_mask: non-zero if guest registers need to be loaded prior to TDENTER + * + * Returns: + * TD-Exit Reason + * + * Note: KVM doesn't support using XMM in its hypercalls, it's the HyperV + * code's responsibility to save/restore XMM registers on TDVMCALL. + */ +SYM_FUNC_START(__tdx_vcpu_run) + push %rbp + mov %rsp, %rbp + + push %r15 + push %r14 + push %r13 + push %r12 + push %rbx + + /* Save @regs, which is needed after TDENTER to capture output. */ + push %rsi + + /* Load @tdvpr to RCX */ + mov %rdi, %rcx + + /* No need to load guest GPRs if the last exit wasn't a TDVMCALL. */ + test %dx, %dx + je 1f + + /* Load @regs to RAX, which will be clobbered with $TDENTER anyways. */ + mov %rsi, %rax + + mov VCPU_RBX(%rax), %rbx + mov VCPU_RDX(%rax), %rdx + mov VCPU_RBP(%rax), %rbp + mov VCPU_RSI(%rax), %rsi + mov VCPU_RDI(%rax), %rdi + + mov VCPU_R8 (%rax), %r8 + mov VCPU_R9 (%rax), %r9 + mov VCPU_R10(%rax), %r10 + mov VCPU_R11(%rax), %r11 + mov VCPU_R12(%rax), %r12 + mov VCPU_R13(%rax), %r13 + mov VCPU_R14(%rax), %r14 + mov VCPU_R15(%rax), %r15 + + /* Load TDENTER to RAX. This kills the @regs pointer! */ +1: mov $TDENTER, %rax + +2: seamcall + + /* Skip to the exit path if TDENTER failed. */ + bt $TDENTER_ERROR_BIT, %rax + jc 4f + + /* Temporarily save the TD-Exit reason. */ + push %rax + + /* check if TD-exit due to TDVMCALL */ + cmp $EXIT_REASON_TDCALL, %ax + + /* Reload @regs to RAX. */ + mov 8(%rsp), %rax + + /* Jump on non-TDVMCALL */ + jne 3f + + /* Save all output from SEAMCALL(TDENTER) */ + mov %rbx, VCPU_RBX(%rax) + mov %rbp, VCPU_RBP(%rax) + mov %rsi, VCPU_RSI(%rax) + mov %rdi, VCPU_RDI(%rax) + mov %r10, VCPU_R10(%rax) + mov %r11, VCPU_R11(%rax) + mov %r12, VCPU_R12(%rax) + mov %r13, VCPU_R13(%rax) + mov %r14, VCPU_R14(%rax) + mov %r15, VCPU_R15(%rax) + +3: mov %rcx, VCPU_RCX(%rax) + mov %rdx, VCPU_RDX(%rax) + mov %r8, VCPU_R8 (%rax) + mov %r9, VCPU_R9 (%rax) + + /* + * Clear all general purpose registers except RSP and RAX to prevent + * speculative use of the guest's values. + */ + xor %rbx, %rbx + xor %rcx, %rcx + xor %rdx, %rdx + xor %rsi, %rsi + xor %rdi, %rdi + xor %rbp, %rbp + xor %r8, %r8 + xor %r9, %r9 + xor %r10, %r10 + xor %r11, %r11 + xor %r12, %r12 + xor %r13, %r13 + xor %r14, %r14 + xor %r15, %r15 + + /* Restore the TD-Exit reason to RAX for return. */ + pop %rax + + /* "POP" @regs. */ +4: add $8, %rsp + pop %rbx + pop %r12 + pop %r13 + pop %r14 + pop %r15 + + pop %rbp + ret + +5: cmpb $0, kvm_rebooting + je 6f + mov $-EFAULT, %rax + jmp 4b +6: ud2 + _ASM_EXTABLE(2b, 5b) + +SYM_FUNC_END(__tdx_vcpu_run) + +.popsection + +#endif
Signature · 1 line
2.25.1
RFC PATCH v5 71/104 KVM: TDX: restore debug store when TD exit
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Because debug store is clobbered, restore it on TD exit.
--- arch/x86/events/intel/ds.c | 1 + arch/x86/kvm/vmx/tdx.c | 1 + 2 files changed, 2 insertions(+)
arch/x86/events/intel/ds.c +1 -0
@@ -2256,3 +2256,4 @@ void perf_restore_debug_store(void)wrmsrl (MSR_IA32_DS_AREA , (unsigned long )ds); } +EXPORT_SYMBOL_GPL (perf_restore_debug_store);
arch/x86/kvm/vmx/tdx.c +1 -0
@@ -620,6 +620,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (vcpu, tdx);tdx_user_return_update_cache (); +perf_restore_debug_store ();tdx_restore_host_xsave_state (vcpu); tdx->host_state_need_restore = true;
Signature · 1 line
2.25.1
RFC PATCH v5 57/104 KVM: x86/mmu: Introduce kvm_mmu_map_tdp_page() for use by TDX
In reply to [email protected]From: Sean Christopherson <[email protected]>
Introduce a helper to directly (pun intended) fault-in a TDP page without having to go through the full page fault path. This allows TDX to get the resulting pfn and also allows the RET_PF_* enums to stay in mmu.c where they belong.
--- arch/x86/kvm/mmu.h | 3 +++ arch/x86/kvm/mmu/mmu.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+)
arch/x86/kvm/mmu.h +3 -0
@@ -205,6 +205,9 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,return vcpu->arch.mmu->page_fault (vcpu, &fault); } +kvm_pfn_t kvm_mmu_map_tdp_page (struct kvm_vcpu *vcpu,gpa_t gpa, +u32 error_code,int max_level); +/* * Currently, we have two sorts of write-protection, a) the first one * write-protects guest page to sync the guest modification, b) another one is
arch/x86/kvm/mmu/mmu.c +38 -0
@@ -4192,6 +4192,44 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)return direct_page_fault (vcpu, fault); } +kvm_pfn_t kvm_mmu_map_tdp_page (struct kvm_vcpu *vcpu,gpa_t gpa, +u32 error_code,int max_level) +{ +int r; +struct kvm_page_fault fault = (struct kvm_page_fault ) { + .addr = gpa, + .error_code = error_code, + .exec = error_code &PFERR_FETCH_MASK , + .write = error_code &PFERR_WRITE_MASK , + .present = error_code &PFERR_PRESENT_MASK , + .rsvd = error_code &PFERR_RSVD_MASK , + .user = error_code &PFERR_USER_MASK , + .prefetch = false, + .is_tdp = true, + .nx_huge_page_workaround_enabled =is_nx_huge_page_enabled (), + }; + +if (mmu_topup_memory_caches (vcpu, false)) +return KVM_PFN_ERR_FAULT ; + +/* +* Loop on the page fault path to handle the case where an mmu_notifier +* invalidation triggers RET_PF_RETRY. In the normal page fault path, +* KVM needs to resume the guest in case the invalidation changed any +* of the page fault properties, i.e. the gpa or error code. For this +* path, the gpa and error code are fixed by the caller, and the caller +* expects failure if and only if the page fault can't be fixed. +*/ +do { + fault.max_level = max_level; + fault.req_level =PG_LEVEL_4K ; + fault.goal_level =PG_LEVEL_4K ; + r =direct_page_fault (vcpu, &fault); + }while (r ==RET_PF_RETRY && !is_error_noslot_pfn (fault.pfn)); +return fault.pfn; +} +EXPORT_SYMBOL_GPL (kvm_mmu_map_tdp_page); +static void nonpaging_init_context (struct kvm_mmu *context) { context->page_fault = nonpaging_page_fault;
Signature · 1 line
2.25.1
RFC PATCH v5 72/104 KVM: TDX: handle vcpu migration over logical processor
In reply to [email protected]From: Isaku Yamahata <[email protected]>
For vcpu migration, in the case of VMX, VCMS is flushed on the source pcpu, and load it on the target pcpu. There are corresponding TDX SEAMCALL APIs, call them on vcpu migration. The logic is mostly same as VMX except the TDX SEAMCALLs are used.
--- arch/x86/kvm/vmx/main.c | 20 +++++++++++++-- arch/x86/kvm/vmx/tdx.c | 51 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 2 ++ 3 files changed, 71 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/main.c +18 -2
@@ -121,6 +121,14 @@ static fastpath_t vt_vcpu_run(struct kvm_vcpu *vcpu)return vmx_vcpu_run (vcpu); } +static void vt_vcpu_load (struct kvm_vcpu *vcpu,int cpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_vcpu_load (vcpu, cpu); + +return vmx_vcpu_load (vcpu, cpu); +} +static void vt_flush_tlb_all (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))
@@ -162,6 +170,14 @@ static void vt_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa,vmx_load_mmu_pgd (vcpu, root_hpa, pgd_level); } +static void vt_sched_in (struct kvm_vcpu *vcpu,int cpu) +{ +if (is_td_vcpu (vcpu)) +return ; + +vmx_sched_in (vcpu, cpu); +} +static int vt_mem_enc_op (struct kvm *kvm,void __user *argp) {if (!is_td (kvm))
@@ -199,7 +215,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_reset = vt_vcpu_reset, .prepare_guest_switch = vt_prepare_switch_to_guest, - .vcpu_load = vmx_vcpu_load, + .vcpu_load = vt_vcpu_load, .vcpu_put = vt_vcpu_put, .update_exception_bitmap = vmx_update_exception_bitmap,
@@ -285,7 +301,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .request_immediate_exit = vmx_request_immediate_exit, - .sched_in = vmx_sched_in, + .sched_in = vt_sched_in, .cpu_dirty_log_size = PML_ENTITY_NUM, .update_cpu_dirty_logging = vmx_update_cpu_dirty_logging,
arch/x86/kvm/vmx/tdx.c +51 -0
69 lines not on this page · raw message
arch/x86/kvm/vmx/x86_ops.h +2 -0
@@ -143,6 +143,7 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event);fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu);void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu);void tdx_vcpu_put (struct kvm_vcpu *vcpu); +void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -166,6 +167,7 @@ static inline void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) {}static inline fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) {return EXIT_FASTPATH_NONE ; }static inline void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_put (struct kvm_vcpu *vcpu) {} +static inline void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu) {}static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
Signature · 1 line
2.25.1
RFC PATCH v5 73/104 KVM: TDX: track LP tdx vcpu run and teardown vcpus on descroing the guest TD
In reply to [email protected]From: Isaku Yamahata <[email protected]>
When shutting down the machine, (VMX or TDX) vcpus needs to be shutdown on each pcpu. Do the similar for TDX with TDX SEAMCALL APIs.
--- arch/x86/kvm/vmx/main.c | 23 +++++++++++-- arch/x86/kvm/vmx/tdx.c | 70 ++++++++++++++++++++++++++++++++++++-- arch/x86/kvm/vmx/tdx.h | 2 ++ arch/x86/kvm/vmx/x86_ops.h | 4 +++ 4 files changed, 95 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx/main.c +21 -2
@@ -13,6 +13,25 @@ static bool vt_is_vm_type_supported(unsigned long type)return type ==KVM_X86_DEFAULT_VM ||tdx_is_vm_type_supported (type); } +static int vt_hardware_enable (void ) +{ +int ret; + + ret =vmx_hardware_enable (); +if (ret) +return ret; + +tdx_hardware_enable (); +return 0 ; +} + +static void vt_hardware_disable (void ) +{ +/* Note, TDX *and* VMX need to be disabled if TDX is enabled. */ +tdx_hardware_disable (); +vmx_hardware_disable (); +} +static __init int vt_hardware_setup (void ) {int ret;
@@ -199,8 +218,8 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .hardware_unsetup = vt_hardware_unsetup, - .hardware_enable = vmx_hardware_enable, - .hardware_disable = vmx_hardware_disable, + .hardware_enable = vt_hardware_enable, + .hardware_disable = vt_hardware_disable, .cpu_has_accelerated_tpr = report_flexpriority, .has_emulated_msr = vmx_has_emulated_msr,
arch/x86/kvm/vmx/tdx.c +68 -2
115 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.h +2 -0
@@ -81,6 +81,8 @@ struct vcpu_tdx {struct tdx_td_page tdvpr;struct tdx_td_page *tdvpx; +struct list_head cpu_list; +union tdx_exit_reason exit_reason;bool initialized;
arch/x86/kvm/vmx/x86_ops.h +4 -0
@@ -132,6 +132,8 @@ void __init tdx_pre_kvm_init(unsigned int *vcpu_size,bool tdx_is_vm_type_supported (unsigned long type);void __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops);void tdx_hardware_unsetup (void ); +void tdx_hardware_enable (void ); +void tdx_hardware_disable (void );int tdx_vm_init (struct kvm *kvm);void tdx_mmu_prezap (struct kvm *kvm);
@@ -156,6 +158,8 @@ static inline void tdx_pre_kvm_init(static inline bool tdx_is_vm_type_supported (unsigned long type) {return false; }static inline void tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {}static inline void tdx_hardware_unsetup (void ) {} +static inline void tdx_hardware_enable (void ) {} +static inline void tdx_hardware_disable (void ) {}static inline int tdx_vm_init (struct kvm *kvm) {return -EOPNOTSUPP ; }static inline void tdx_mmu_prezap (struct kvm *kvm) {}
Signature · 1 line
2.25.1
RFC PATCH v5 61/104 KVM: TDX: Finalize VM initialization
In reply to [email protected]From: Isaku Yamahata <[email protected]>
To protect the initial contents of the guest TD, the TDX module measures the guest TD during the build process as SHA-384 measurement. The measurement of the guest TD contents needs to be completed to make the guest TD ready to run.
Add a new subcommand, KVM_TDX_FINALIZE_VM, for VM-scoped KVM_MEMORY_ENCRYPT_OP to finalize the measurement and mark the TDX VM ready to run.
--- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/vmx/tdx.c | 21 +++++++++++++++++++++ tools/arch/x86/include/uapi/asm/kvm.h | 1 + 3 files changed, 23 insertions(+)
arch/x86/include/uapi/asm/kvm.h +1 -0
@@ -534,6 +534,7 @@ enum kvm_tdx_cmd_id {KVM_TDX_INIT_VM ,KVM_TDX_INIT_VCPU ,KVM_TDX_INIT_MEM_REGION , +KVM_TDX_FINALIZE_VM , KVM_TDX_CMD_NR_MAX, };
arch/x86/kvm/vmx/tdx.c +21 -0
@@ -1103,6 +1103,24 @@ static int tdx_init_mem_region(struct kvm *kvm, struct kvm_tdx_cmd *cmd)return ret; } +static int tdx_td_finalizemr (struct kvm *kvm) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +u64 err; + +if (!is_td_initialized (kvm) ||is_td_finalized (kvm_tdx)) +return -EINVAL ; + + err =tdh_mr_finalize (kvm_tdx->tdr.pa); +if (WARN_ON_ONCE (err)) { +pr_tdx_error (TDH_MR_FINALIZE , err,NULL ); +return -EIO ; + } + + kvm_tdx->finalized = true; +return 0 ; +} +int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {struct kvm_tdx_cmd tdx_cmd;
@@ -1123,6 +1141,9 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp)case KVM_TDX_INIT_MEM_REGION : r =tdx_init_mem_region (kvm, &tdx_cmd);break ; +case KVM_TDX_FINALIZE_VM : + r =tdx_td_finalizemr (kvm); +break ;default : r = -EINVAL ; goto out;
tools/arch/x86/include/uapi/asm/kvm.h +1 -0
@@ -534,6 +534,7 @@ enum kvm_tdx_cmd_id {KVM_TDX_INIT_VM ,KVM_TDX_INIT_VCPU ,KVM_TDX_INIT_MEM_REGION , +KVM_TDX_FINALIZE_VM , KVM_TDX_CMD_NR_MAX, };
Signature · 1 line
2.25.1
RFC PATCH v5 54/104 KVM: x86/tdp_mmu: Keep PRIVATE_PROHIBIT bit when zapping
In reply to [email protected]From: Isaku Yamahata <[email protected]>
SPTE_PRIVATE_PROHIBIT specifies the share or private GPA is allowed or not. It needs to be kept over zapping the EPT entry. Currently the EPT entry is initialized shadow_init_value unconditionally to clear SPTE_PRIVATE_PROHIBIT bit. To carry SPTE_PRIVATE_PROHIBIT bit, introduce a helper function to get initial value for zapped entry with SPTE_PRIVATE_PROHIBIT bit. Replace shadow_init_value with it.
--- arch/x86/kvm/mmu/tdp_mmu.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +15 -4
@@ -610,6 +610,12 @@ static inline bool tdp_mmu_set_spte_atomic(struct kvm *kvm,return true; } +static u64 shadow_init_spte (u64 old_spte) +{ +return shadow_init_value | + (is_private_prohibit_spte (old_spte) ?SPTE_PRIVATE_PROHIBIT :0 ); +} +static inline bool tdp_mmu_zap_spte_atomic (struct kvm *kvm,struct tdp_iter *iter) {
@@ -641,7 +647,8 @@ static inline bool tdp_mmu_zap_spte_atomic(struct kvm *kvm,* shadow_init_value (which sets "suppress #VE" bit) so it * can be set when EPT table entries are zapped. */ -WRITE_ONCE (*rcu_dereference (iter->sptep), shadow_init_value); +WRITE_ONCE (*rcu_dereference (iter->sptep), +shadow_init_spte (iter->old_spte));return true; }
@@ -853,7 +860,8 @@ static bool zap_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,if (!shared) {/* see comments in tdp_mmu_zap_spte_atomic() */ -tdp_mmu_set_spte (kvm, &iter, shadow_init_value); +tdp_mmu_set_spte (kvm, &iter, +shadow_init_spte (iter.old_spte)); flush = true; }else if (!tdp_mmu_zap_spte_atomic (kvm, &iter)) {/*
@@ -1038,11 +1046,14 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, new_spte =make_mmio_spte (vcpu,tdp_iter_gfn_unalias (vcpu->kvm, iter), pte_access); - else + else { wrprot =make_spte (vcpu, sp, fault->slot, pte_access,tdp_iter_gfn_unalias (vcpu->kvm, iter), fault->pfn, iter->old_spte, fault->prefetch, true, fault->map_writable, &new_spte); +if (is_private_prohibit_spte (iter->old_spte)) + new_spte |=SPTE_PRIVATE_PROHIBIT ; + }if (new_spte == iter->old_spte) ret =RET_PF_SPURIOUS ;
@@ -1335,7 +1346,7 @@ static bool set_spte_gfn(struct kvm *kvm, struct tdp_iter *iter,* invariant that the PFN of a present * leaf SPTE can never change. * See __handle_changed_spte(). */ -tdp_mmu_set_spte (kvm, iter, shadow_init_value); +tdp_mmu_set_spte (kvm, iter,shadow_init_spte (iter->old_spte));if (!pte_write (range->pte)) { new_spte = kvm_mmu_changed_pte_notifier_make_spte(iter->old_spte,
Signature · 1 line
2.25.1
RFC PATCH v5 56/104 KVM: x86/tdp_mmu: implement MapGPA hypercall for TDX
In reply to [email protected]From: Isaku Yamahata <[email protected]>
The TDX Guest-Hypervisor communication interface(GHCI) specification defines MapGPA hypercall for guest TD to request the host VMM to map given GPA range as private or shared.
It means the guest TD uses the GPA as shared (or private). The GPA won't be used as private (or shared). VMM should enforce GPA usage. VMM doesn't have to map the GPA on the hypercall request.
- Zap the aliased region.
If shared (or private) GPA is requested, zap private (or shared) GPA (modulo shared bit).
- Record the request GPA is shared (or private) by SPTE_PRIVATE_PROHIBIT in SPTE in both shared and private EPT tables.
- With SPTE_PRIVATE_PROHIBIT set, a shared GPA is allowed.
- With SPTE_PRIVATE_PROHIBIT cleared, a private GPA is allowed.
The reason to record SPTE_PRIVATE_PROHIBIT in both shared and private EPT is to optimize EPT violation path for normal guest TD execution path and penalize map_gpa hypercall.
If the guest TD faults on not-allowed GPA (modulo shared bit), the KVM doesn't resolve EPT violation and let vcpu retry. vcpu will keep faulting until other vcpu maps the region with MapGPA hypercall. With the initial value of spte(initial shadow_init_value), SPTE_PRIVATE_PROHIBIT is cleared. So the default behavior doesn't change.
- don't map GPA.
The GPA is mapped on the next EPT violation.
--- arch/x86/kvm/mmu.h | 2 + arch/x86/kvm/mmu/mmu.c | 56 ++++++++++ arch/x86/kvm/mmu/tdp_mmu.c | 208 +++++++++++++++++++++++++++++++++++++ arch/x86/kvm/mmu/tdp_mmu.h | 3 + 4 files changed, 269 insertions(+)
arch/x86/kvm/mmu.h +2 -0
@@ -305,6 +305,8 @@ void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end);int kvm_arch_write_log_dirty (struct kvm_vcpu *vcpu); +int kvm_mmu_map_gpa (struct kvm_vcpu *vcpu,gfn_t *startp,gfn_t end); +int kvm_mmu_post_init_vm (struct kvm *kvm);void kvm_mmu_pre_destroy_vm (struct kvm *kvm);
arch/x86/kvm/mmu/mmu.c +56 -0
62 lines not on this page · raw message
arch/x86/kvm/mmu/tdp_mmu.c +208 -0
220 lines not on this page · raw message
arch/x86/kvm/mmu/tdp_mmu.h +3 -0
@@ -74,6 +74,9 @@ bool kvm_tdp_mmu_write_protect_gfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn, int min_level); +int kvm_tdp_mmu_map_gpa (struct kvm_vcpu *vcpu, +gfn_t *startp,gfn_t end,bool is_private); +static inline void kvm_tdp_mmu_walk_lockless_begin (void ) {rcu_read_lock ();
Signature · 1 line
2.25.1
RFC PATCH v5 66/104 KVM: TDX: restore host xsave state when exit from the guest TD
In reply to [email protected]From: Isaku Yamahata <[email protected]>
On exiting from the guest TD, xsave state is clobbered. Restore xsave state on TD exit.
--- arch/x86/kvm/vmx/tdx.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
arch/x86/kvm/vmx/tdx.c +18 -0
@@ -2,6 +2,7 @@#include <linux/cpu.h> #include <linux/mmu_context.h> +#include <asm/fpu/xcr.h> #include <asm/tdx.h> #include "capabilities.h"
@@ -549,6 +550,22 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->kvm->vm_bugged = true; } +static void tdx_restore_host_xsave_state (struct kvm_vcpu *vcpu) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (vcpu->kvm); + +if (static_cpu_has (X86_FEATURE_XSAVE ) && + host_xcr0 != (kvm_tdx->xfam & supported_xcr0)) +xsetbv (XCR_XFEATURE_ENABLED_MASK , host_xcr0); +if (static_cpu_has (X86_FEATURE_XSAVES ) && +/* PT can be exposed to TD guest regardless of KVM's XSS support */ + host_xss != (kvm_tdx->xfam & (supported_xss |XFEATURE_MASK_PT ))) +wrmsrl (MSR_IA32_XSS , host_xss); +if (static_cpu_has (X86_FEATURE_PKU ) && + (kvm_tdx->xfam &XFEATURE_MASK_PKRU )) +write_pkru (vcpu->arch.host_pkru); +} +u64 __tdx_vcpu_run (hpa_t tdvpr,void *regs,u32 regs_mask); static noinstr void tdx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
@@ -572,6 +589,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (vcpu, tdx); +tdx_restore_host_xsave_state (vcpu); tdx->host_state_need_restore = true; vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ;
Signature · 1 line
2.25.1
RFC PATCH v5 53/104 KVM: x86/mmu: steal software usable bit for EPT to represent shared page
In reply to [email protected]From: Isaku Yamahata <[email protected]>
With TDX, all GFNs are private at guest boot time. At run time guest TD can explicitly change it to shared from private or vice-versa by MapGPA hypercall. If it's specified, the given GFN can't be used as otherwise. That's is, if a guest tells KVM that the GFN is shared, it can't be used as private. or vice-versa.
KVM needs to record it. Steal software usable bit for it from MMIO counter.
--- arch/x86/kvm/mmu/spte.h | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
arch/x86/kvm/mmu/spte.h +12 -3
@@ -14,6 +14,9 @@*/ #define SPTE_MMU_PRESENT_MASK BIT_ULL(11) +/* Masks that used to track for shared GPA **/ +#define SPTE_PRIVATE_PROHIBIT BIT_ULL(62) +/* * TDP SPTES (more specifically, EPT SPTEs) may not have A/D bits, and may also * be restricted to using write-protection (for L2 when CPU dirty logging, i.e.
@@ -124,7 +127,7 @@ static_assert(!(EPT_SPTE_MMU_WRITABLE & SHADOW_ACC_TRACK_SAVED_MASK));* the memslots generation and is derived as follows: * * Bits 0-7 of the MMIO generation are propagated to spte bits 3-10 -* Bits 8-18 of the MMIO generation are propagated to spte bits 52-62 +* Bits 8-18 of the MMIO generation are propagated to spte bits 52-61 * * The KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS flag is intentionally not included in * the MMIO generation number, as doing so would require stealing a bit from
@@ -138,7 +141,7 @@ static_assert(!(EPT_SPTE_MMU_WRITABLE & SHADOW_ACC_TRACK_SAVED_MASK));#define MMIO_SPTE_GEN_LOW_END 10#define MMIO_SPTE_GEN_HIGH_START 52 -#define MMIO_SPTE_GEN_HIGH_END 62 +#define MMIO_SPTE_GEN_HIGH_END 61#define MMIO_SPTE_GEN_LOW_MASK GENMASK_ULL(MMIO_SPTE_GEN_LOW_END, \ MMIO_SPTE_GEN_LOW_START)
@@ -151,7 +154,7 @@ static_assert(!(SPTE_MMU_PRESENT_MASK &#define MMIO_SPTE_GEN_HIGH_BITS (MMIO_SPTE_GEN_HIGH_END - MMIO_SPTE_GEN_HIGH_START + 1)/* remember to adjust the comment above as well if you change these */ -static_assert (MMIO_SPTE_GEN_LOW_BITS ==8 &&MMIO_SPTE_GEN_HIGH_BITS ==11 ); +static_assert (MMIO_SPTE_GEN_LOW_BITS ==8 &&MMIO_SPTE_GEN_HIGH_BITS ==10 );#define MMIO_SPTE_GEN_LOW_SHIFT (MMIO_SPTE_GEN_LOW_START - 0)#define MMIO_SPTE_GEN_HIGH_SHIFT (MMIO_SPTE_GEN_HIGH_START - MMIO_SPTE_GEN_LOW_BITS)
@@ -208,6 +211,7 @@ extern u64 __read_mostly shadow_nonpresent_or_rsvd_mask;/* Removed SPTEs must not be misconstrued as shadow present PTEs. */ static_assert (!(REMOVED_SPTE &SPTE_MMU_PRESENT_MASK )); +static_assert (!(REMOVED_SPTE &SPTE_PRIVATE_PROHIBIT ));/* * See above comment around REMOVED_SPTE. SHADOW_REMOVED_SPTE is the actual
@@ -222,6 +226,11 @@ static inline bool is_removed_spte(u64 spte)return spte ==SHADOW_REMOVED_SPTE ; } +static inline bool is_private_prohibit_spte (u64 spte) +{ +return !!(spte &SPTE_PRIVATE_PROHIBIT ); +} +/* * In some cases, we need to preserve the GFN of a non-present or reserved * SPTE when we usurp the upper five bits of the physical address space to
Signature · 1 line
2.25.1
RFC PATCH v5 55/104 KVM: x86/tdp_mmu: prevent private/shared map based on PRIVATE_PROHIBIT
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Use the bit SPTE_PRIVATE_PROHIBIT in shared and private EPT to determine which mapping, shared or private, is allowed. If requested mapping isn't allowed, return RET_PF_RETRY to wait for other vcpu to change it.
--- arch/x86/kvm/mmu/spte.h | 2 +- arch/x86/kvm/mmu/tdp_mmu.c | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-)
arch/x86/kvm/mmu/spte.h +1 -1
@@ -223,7 +223,7 @@ extern u64 __read_mostly shadow_init_value;static inline bool is_removed_spte (u64 spte) { -return spte ==SHADOW_REMOVED_SPTE ; +return (spte & ~SPTE_PRIVATE_PROHIBIT ) ==SHADOW_REMOVED_SPTE ; }static inline bool is_private_prohibit_spte (u64 spte)
arch/x86/kvm/mmu/tdp_mmu.c +19 -3
@@ -1038,9 +1038,25 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,WARN_ON (sp->role.level != fault->goal_level); -/* TDX shared GPAs are no executable, enforce this for the SDV. */ -if (!kvm_is_private_gfn (vcpu->kvm, iter->gfn)) - pte_access &= ~ACC_EXEC_MASK ; +if (kvm_gfn_stolen_mask (vcpu->kvm)) { +if (is_private_spte (iter->sptep)) { +/* +* This GPA is not allowed to map as private. Let +* vcpu loop in page fault until other vcpu change it +* by MapGPA hypercall. +*/ +if (fault->slot && +is_private_prohibit_spte (iter->old_spte)) +return RET_PF_RETRY ; + }else { +/* This GPA is not allowed to map as shared. */ +if (fault->slot && + !is_private_prohibit_spte (iter->old_spte)) +return RET_PF_RETRY ; +/* TDX shared GPAs are no executable, enforce this. */ + pte_access &= ~ACC_EXEC_MASK ; + } + }if (unlikely (!fault->slot)) new_spte = make_mmio_spte(vcpu,
Signature · 1 line
2.25.1
RFC PATCH v5 58/104 KVM: x86/mmu: Focibly use TDP MMU for TDX
In reply to [email protected]From: Isaku Yamahata <[email protected]>
At this point, TDX supports TDP MMU and doesn't support legacy MMU. Forcibly use TDP MMU for TDX irrelevant of kernel parameter to disable TDP MMU.
--- arch/x86/kvm/mmu/tdp_mmu.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
arch/x86/kvm/mmu/tdp_mmu.c +6 -1
@@ -16,7 +16,12 @@ module_param_named(tdp_mmu, tdp_mmu_enabled, bool, 0644);/* Initializes the TDP MMU for the VM, if enabled. */ bool kvm_mmu_init_tdp_mmu (struct kvm *kvm) { -if (!tdp_enabled || !READ_ONCE (tdp_mmu_enabled)) +/* +* Because TDX supports only TDP MMU, forcibly use TDP MMU in the case +* of TDX. +*/ +if (kvm->arch.vm_type !=KVM_X86_TDX_VM && + (!tdp_enabled || !READ_ONCE (tdp_mmu_enabled)))return false;/* This should not be changed for the lifetime of the VM. */
Signature · 1 line
2.25.1
RFC PATCH v5 59/104 The start of TDX KVM patch series: TD finalization
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of TD finalization.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +2 -2
@@ -21,11 +21,11 @@ Patch Layer status * TD VM creation/destruction: Applied * TD vcpu creation/destruction: Applied * TDX EPT violation: Applied -* TD finalization: Not yet +* TD finalization: Applying * TD vcpu enter/exit: Not yet * TD vcpu interrupts/exit/hypercall: Not yet * KVM MMU GPA stolen bits: Applied * KVM TDP refactoring for TDX: Applied * KVM TDP MMU hooks: Applied -* KVM TDP MMU MapGPA: Not yet +* KVM TDP MMU MapGPA: Applied
Signature · 1 line
2.25.1
RFC PATCH v5 52/104 The start of TDX KVM patch series: KVM TDP MMU MapGPA
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of KVM TDP MMU MapGPA.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +2 -1
@@ -11,6 +11,7 @@ What qemu can do - TDX VM TYPE is exposed to Qemu. - Qemu can create/destroy guest of TDX vm type. - Qemu can create/destroy vcpu of TDX vm type. +- Qemu can populate initial guest memory image. Patch Layer status ------------------
@@ -19,7 +20,7 @@ Patch Layer status * TDX architectural definitions: Applied * TD VM creation/destruction: Applied * TD vcpu creation/destruction: Applied -* TDX EPT violation: Applying +* TDX EPT violation: Applied * TD finalization: Not yet * TD vcpu enter/exit: Not yet * TD vcpu interrupts/exit/hypercall: Not yet
Signature · 1 line
2.25.1
RFC PATCH v5 60/104 KVM: TDX: Create initial guest memory
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Because the guest memory is protected in TDX, the creation of the initial guest memory requires a dedicated TDX module API, tdh_mem_page_add, instead of directly copying the memory contents into the guest memory in the case of the default VM type. KVM MMU page fault handler callback, private_page_add, handles it.
Define new subcommand, KVM_TDX_INIT_MEM_REGION, of VM-scoped KVM_MEMORY_ENCRYPT_OP. It assigns the guest page, copies the initial memory contents into the guest memory, encrypts the guest memory. At the same time, optionally it extends memory measurement of the TDX guest. It calls the KVM MMU page fault(EPT-violation) handler to trigger the callbacks for it.
--- arch/x86/include/uapi/asm/kvm.h | 9 ++ arch/x86/kvm/mmu/mmu.c | 1 + arch/x86/kvm/vmx/tdx.c | 128 ++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 2 + tools/arch/x86/include/uapi/asm/kvm.h | 9 ++ 5 files changed, 149 insertions(+)
arch/x86/include/uapi/asm/kvm.h +9 -0
@@ -533,6 +533,7 @@ enum kvm_tdx_cmd_id {KVM_TDX_CAPABILITIES =0 ,KVM_TDX_INIT_VM ,KVM_TDX_INIT_VCPU , +KVM_TDX_INIT_MEM_REGION , KVM_TDX_CMD_NR_MAX, };
@@ -574,4 +575,12 @@ struct kvm_tdx_init_vm { __u64 reserved[43]; /* must be zero for future extensibility */ }; +#define KVM_TDX_MEASURE_MEMORY_REGION (1UL << 0) + +struct kvm_tdx_init_mem_region { +__u64 source_addr; +__u64 gpa; +__u64 nr_pages; +}; +#endif /* _ASM_X86_KVM_H */
arch/x86/kvm/mmu/mmu.c +1 -0
@@ -5226,6 +5226,7 @@ int kvm_mmu_load(struct kvm_vcpu *vcpu) out:return r; } +EXPORT_SYMBOL (kvm_mmu_load);void kvm_mmu_unload (struct kvm_vcpu *vcpu) {
arch/x86/kvm/vmx/tdx.c +128 -0
164 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.h +2 -0
@@ -28,6 +28,8 @@ struct kvm_tdx {int cpuid_nent;struct kvm_cpuid_entry2 cpuid_entries[KVM_MAX_CPUID_ENTRIES ]; +hpa_t source_pa; +bool finalized;bool tdh_mem_track;
tools/arch/x86/include/uapi/asm/kvm.h +9 -0
@@ -533,6 +533,7 @@ enum kvm_tdx_cmd_id {KVM_TDX_CAPABILITIES =0 ,KVM_TDX_INIT_VM ,KVM_TDX_INIT_VCPU , +KVM_TDX_INIT_MEM_REGION , KVM_TDX_CMD_NR_MAX, };
@@ -574,4 +575,12 @@ struct kvm_tdx_init_vm { __u64 reserved[43]; /* must be zero for future extensibility */ }; +#define KVM_TDX_MEASURE_MEMORY_REGION (1UL << 0) + +struct kvm_tdx_init_mem_region { +__u64 source_addr; +__u64 gpa; +__u64 nr_pages; +}; +#endif /* _ASM_X86_KVM_H */
Signature · 1 line
2.25.1
A series of 104 patches is completely unreviewably, please split it into reasonable chunks.