KVM TDX basic feature support
Subsystems (8)
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 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
Re: [RFC PATCH v5 083/104] KVM: x86: Split core of hypercall emulation to helper function
Quoted 91 lines · On Fri, Mar 4, 2022 at 12:00 PM <[email protected]> wrote:
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.
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- 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 +16
-21@@ -9090,26 +9090,15 @@ static int complete_hypercall_exit(struct kvm_vcpu *vcpu)return kvm_skip_emulated_instruction (vcpu); } -int kvm_emulate_hypercall (struct kvm_vcpu *vcpu) +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) { -unsigned long nr, a0, a1, a2, a3, ret; -int op_64_bit; - -if (kvm_xen_hypercall_enabled (vcpu->kvm)) -return kvm_xen_hypercall (vcpu); - -if (kvm_hv_hypercall_enabled (vcpu)) -return kvm_hv_hypercall (vcpu); - - nr =kvm_rax_read (vcpu); - a0 =kvm_rbx_read (vcpu); - a1 =kvm_rcx_read (vcpu); - a2 =kvm_rdx_read (vcpu); - a3 =kvm_rsi_read (vcpu); +unsigned long ret;trace_kvm_hypercall (nr, a0, a1, a2, a3); - op_64_bit =is_64_bit_hypercall (vcpu);if (!op_64_bit) { nr &=0xFFFFFFFF ; a0 &=0xFFFFFFFF ;@@ -9118,11 +9107,6 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) a3 &=0xFFFFFFFF ; } -if (static_call (kvm_x86_get_cpl)(vcpu) !=0 ) { - ret = -KVM_EPERM ; - goto out; - } - ret = -KVM_ENOSYS ;switch (nr) {@@ -9181,6 +9165,34 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) ret = -KVM_ENOSYS ;break ; } +return ret; +} +EXPORT_SYMBOL_GPL (__kvm_emulate_hypercall); + +int kvm_emulate_hypercall (struct kvm_vcpu *vcpu) +{ +unsigned long nr, a0, a1, a2, a3, ret; +int op_64_bit; + +if (kvm_xen_hypercall_enabled (vcpu->kvm)) +return kvm_xen_hypercall (vcpu);
arch/x86/kvm/x86.c int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) · line 9188
Quoted 10 lines
arch/x86/kvm/x86.c +10
-0+ +if (kvm_hv_hypercall_enabled (vcpu)) +return kvm_hv_hypercall (vcpu); + + nr =kvm_rax_read (vcpu); + a0 =kvm_rbx_read (vcpu); + a1 =kvm_rcx_read (vcpu); + a2 =kvm_rdx_read (vcpu); + a3 =kvm_rsi_read (vcpu); + op_64_bit =is_64_bit_mode (vcpu);
I think this should be "op_64_bit = is_64_bit_hypercall(vcpu);" is_64_bit_mode was replaced with is_64_bit_hypercall to support protected guests here:
https://lore.kernel.org/all/[email protected]/T/
Without it, op_64_bit will be set to 0 for TD VMs which will cause the upper 32 bit of the registers to be cleared in __kvm_emulate_hypercall
Quoted 11 lines
arch/x86/kvm/x86.c +7
-1+ +if (static_call (kvm_x86_get_cpl)(vcpu) !=0 ) { + ret = -KVM_EPERM ; + goto out; + } + + ret =__kvm_emulate_hypercall (vcpu, nr, a0, a1, a2, a3, op_64_bit); out:if (!op_64_bit) ret = (u32 )ret; --
Quoted 2 lines
2.25.1
Sagi
Re: [RFC PATCH v5 083/104] KVM: x86: Split core of hypercall emulation to helper function
Quoted 79 lines · Sagi Shahar
Quoted 63 lines
arch/x86/kvm/x86.c +16
-21@@ -9090,26 +9090,15 @@ static int complete_hypercall_exit(struct kvm_vcpu *vcpu)return kvm_skip_emulated_instruction (vcpu); } -int kvm_emulate_hypercall (struct kvm_vcpu *vcpu) +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) { -unsigned long nr, a0, a1, a2, a3, ret; -int op_64_bit; - -if (kvm_xen_hypercall_enabled (vcpu->kvm)) -return kvm_xen_hypercall (vcpu); - -if (kvm_hv_hypercall_enabled (vcpu)) -return kvm_hv_hypercall (vcpu); - - nr =kvm_rax_read (vcpu); - a0 =kvm_rbx_read (vcpu); - a1 =kvm_rcx_read (vcpu); - a2 =kvm_rdx_read (vcpu); - a3 =kvm_rsi_read (vcpu); +unsigned long ret;trace_kvm_hypercall (nr, a0, a1, a2, a3); - op_64_bit =is_64_bit_hypercall (vcpu);if (!op_64_bit) { nr &=0xFFFFFFFF ; a0 &=0xFFFFFFFF ;@@ -9118,11 +9107,6 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) a3 &=0xFFFFFFFF ; } -if (static_call (kvm_x86_get_cpl)(vcpu) !=0 ) { - ret = -KVM_EPERM ; - goto out; - } - ret = -KVM_ENOSYS ;switch (nr) {@@ -9181,6 +9165,34 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) ret = -KVM_ENOSYS ;break ; } +return ret; +} +EXPORT_SYMBOL_GPL (__kvm_emulate_hypercall); + +int kvm_emulate_hypercall (struct kvm_vcpu *vcpu) +{ +unsigned long nr, a0, a1, a2, a3, ret; +int op_64_bit; + +if (kvm_xen_hypercall_enabled (vcpu->kvm)) +return kvm_xen_hypercall (vcpu);arch/x86/kvm/x86.c int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) · line 9188
Quoted 10 lines
arch/x86/kvm/x86.c +10
-0+ +if (kvm_hv_hypercall_enabled (vcpu)) +return kvm_hv_hypercall (vcpu); + + nr =kvm_rax_read (vcpu); + a0 =kvm_rbx_read (vcpu); + a1 =kvm_rcx_read (vcpu); + a2 =kvm_rdx_read (vcpu); + a3 =kvm_rsi_read (vcpu); + op_64_bit =is_64_bit_mode (vcpu);I think this should be "
op_64_bit=is_64_bit_hypercall(vcpu);"is_64_bit_modewas replaced withis_64_bit_hypercallto support protected guests here:
https://lore.kernel.org/all/[email protected]/T/
Quoted 2 lines
Without it,
op_64_bitwill be set to 0 for TD VMs which will cause the upper 32 bit of the registers to be cleared in__kvm_emulate_hypercall
Oops, thanks for pointing it out. I'll fix it up with next respin.
Signature · 1 line
Isaku Yamahata <[email protected]>
Paolo Bonzini
maintainerRe: [RFC PATCH v5 083/104] KVM: x86: Split core of hypercall emulation to helper function
In reply to Sagi ShaharQuoted 37 lines · Sagi Shahar
Quoted 36 lines · On Fri, Mar 4, 2022 at 12:00 PM <[email protected]> wrote:
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.
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- 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 +0
-0@@ -9090,26 +9090,15 @@ static int complete_hypercall_exit(struct kvm_vcpu *vcpu)return kvm_skip_emulated_instruction (vcpu); }
Quoted 14 lines
Quoted 4 lines
arch/x86/kvm/x86.c +3
-1-int kvm_emulate_hypercall (struct kvm_vcpu *vcpu) +unsigned long __kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr, + unsigned long a0, unsigned long a1, + unsigned long a2, unsigned long a3,arch/x86/kvm/x86.c static int complete_hypercall_exit(struct kvm_vcpu *vcpu) · line 9102
Quoted 10 lines
arch/x86/kvm/x86.c +1
-8+ int op_64_bit) { -unsigned long nr, a0, a1, a2, a3, ret; -int op_64_bit; - -if (kvm_xen_hypercall_enabled (vcpu->kvm)) -return kvm_xen_hypercall (vcpu); - -if (kvm_hv_hypercall_enabled (vcpu)) -return kvm_hv_hypercall (vcpu);
Please keep Xen and Hyper-V hypercalls to kvm_emulate_hypercall (more on this in the reply to patch 89). __kvm_emulate_hypercall should only handle KVM hypercalls.
Quoted 4 lines
arch/x86/kvm/x86.c int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) · line 9193
Quoted 4 lines
arch/x86/kvm/x86.c +4
-0+if (static_call (kvm_x86_get_cpl)(vcpu) !=0 ) { + ret = -KVM_EPERM ; + goto out; + }
Is this guaranteed by TDG.VP.VMCALL?
Paolo
Quoted 10 lines
Quoted 7 lines
arch/x86/kvm/x86.c +1
-1+ ret =__kvm_emulate_hypercall (vcpu, nr, a0, a1, a2, a3, op_64_bit); out:if (!op_64_bit) ret = (u32 )ret; --2.25.1
Sagi
Re: [RFC PATCH v5 083/104] KVM: x86: Split core of hypercall emulation to helper function
Quoted 6 lines · Paolo Bonzini
Quoted 4 lines
arch/x86/kvm/x86.c int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) · line 9193
Quoted 4 lines
arch/x86/kvm/x86.c +4
-0+if (static_call (kvm_x86_get_cpl)(vcpu) !=0 ) { + ret = -KVM_EPERM ; + goto out; + }Is this guaranteed by TDG.VP.VMCALL?
Yes. TDCALL instruction in TD results in #GP(0) if CPL > 0. It's documented in trust domain CPU architectural extensions spec.
https://www.intel.com/content/dam/develop/external/us/en/documents-tps/intel-tdx-cpu-architectural-specification.pdf
Anyway VMM can't know TD guest CPL (or other CPU state).
Signature · 1 line
Isaku Yamahata <[email protected]>
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
Re: [RFC PATCH v5 084/104] KVM: TDX: Add a place holder to handle TDX VM exit
Quoted 6 lines · On 3/4/22 20:49, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Wire up
handle_exitandhandle_exit_irqoffmethods and add a place holder to handle VM exit. Add helper functions to get exit info, exit qualification, etc.
Quoted 6 lines
Signed-off-by Isaku Yamahata <[email protected]>--- 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(-)
Quoted 217 lines
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@@ -68,6 +68,26 @@ static __always_inline hpa_t set_hkid_to_hpa(hpa_t pa, u16 hkid)return pa; } +static __always_inline unsigned long tdexit_exit_qual (struct kvm_vcpu *vcpu) +{ +return kvm_rcx_read (vcpu); +} + +static __always_inline unsigned long tdexit_ext_exit_qual (struct kvm_vcpu *vcpu) +{ +return kvm_rdx_read (vcpu); +} + +static __always_inline unsigned long tdexit_gpa (struct kvm_vcpu *vcpu) +{ +return kvm_r8_read (vcpu); +} + +static __always_inline unsigned long tdexit_intr_info (struct kvm_vcpu *vcpu) +{ +return kvm_r9_read (vcpu); +} +static inline bool is_td_vcpu_created (struct vcpu_tdx *tdx) {return tdx->tdvpr.added;@@ -768,6 +788,25 @@ void tdx_inject_nmi(struct kvm_vcpu *vcpu)td_management_write8 (to_tdx (vcpu),TD_VCPU_PEND_NMI ,1 ); } +void tdx_handle_exit_irqoff (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); +u16 exit_reason = tdx->exit_reason.basic; + +if (exit_reason ==EXIT_REASON_EXCEPTION_NMI ) +vmx_handle_exception_nmi_irqoff (vcpu,tdexit_intr_info (vcpu)); +else if (exit_reason ==EXIT_REASON_EXTERNAL_INTERRUPT ) +vmx_handle_external_interrupt_irqoff (vcpu, +tdexit_intr_info (vcpu)); +} + +static int tdx_handle_triple_fault (struct kvm_vcpu *vcpu) +{ + vcpu->run->exit_reason =KVM_EXIT_SHUTDOWN ; + vcpu->mmio_needed =0 ; +return 0 ; +} +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 );@@ -1042,6 +1081,46 @@ void tdx_deliver_interrupt(struct kvm_lapic *apic, int delivery_mode,__vmx_deliver_posted_interrupt (vcpu, &tdx->pi_desc, vector); } +int tdx_handle_exit (struct kvm_vcpu *vcpu,fastpath_t fastpath) +{ +union tdx_exit_reason exit_reason =to_tdx (vcpu)->exit_reason; + +if (unlikely (exit_reason.non_recoverable || exit_reason.error)) { +if (exit_reason.basic ==EXIT_REASON_TRIPLE_FAULT ) +return tdx_handle_triple_fault (vcpu); + +kvm_pr_unimpl ("TD exit 0x%llx, %d\n" , + exit_reason.full, exit_reason.basic); + goto unhandled_exit; + } + +WARN_ON_ONCE (fastpath !=EXIT_FASTPATH_NONE ); + +switch (exit_reason.basic) { +default : +break ; + } + +unhandled_exit: + vcpu->run->exit_reason =KVM_EXIT_UNKNOWN ; + vcpu->run->hw.hardware_exit_reason = exit_reason.full; +return 0 ; +} + +void tdx_get_exit_info (struct kvm_vcpu *vcpu,u32 *reason, +u64 *info1,u64 *info2,u32 *intr_info,u32 *error_code) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + + *reason = tdx->exit_reason.full; + + *info1 =tdexit_exit_qual (vcpu); + *info2 =tdexit_ext_exit_qual (vcpu); + + *intr_info =tdexit_intr_info (vcpu); + *error_code =0 ; +} +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 +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 ; }
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
Re: [RFC PATCH v5 085/104] KVM: TDX: handle EXIT_REASON_OTHER_SMI
Quoted 29 lines · On 3/4/22 20:49, [email protected] wrote:
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.Signed-off-by Isaku Yamahata <[email protected]>--- 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 8arch/x86/kvm/vmx/tdx.c +0
-0
arch/x86/kvm/vmx/tdx.c int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath) · line 1102
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c +3
-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.
Please expand MSMI. Otherwise,
Paolo
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c +4
-0+* #SMI is delivered and handled right after SEAMRET, nothing +* needs to be done in KVM. +*/ +return 1 ;default :break ; }
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
Re: [RFC PATCH v5 086/104] KVM: TDX: handle ept violation/misconfig exit
Quoted 19 lines · On Fri, Mar 4, 2022 at 12:23 PM <[email protected]> wrote:
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 withKVM_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.Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-)arch/x86/kvm/vmx/tdx.c +0
-0
arch/x86/kvm/vmx/tdx.c void tdx_deliver_interrupt(struct kvm_lapic *apic, int delivery_mode, · line 1084
Quoted 4 lines
arch/x86/kvm/vmx/tdx.c +1
-0@@ -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)
TDX_SEPT_PFERR is defined using PFERR_.* bitmask but __vmx_handle_ept_violation is accepting an EPT_VIOLATION_.* bitmask. so (PFERR_WRITE_MASK | PFERR_USER_MASK) will get interpreted as (EPT_VIOLATION_ACC_WRITE | EPT_VIOLATION_ACC_INSTR) which will get translated to (PFERR_WRITE_MASK | PFERR_FETCH_MASK). Was that the intention of this code?
Quoted 57 lines
arch/x86/kvm/vmx/tdx.c +37
-3+ +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); --
Quoted 2 lines
2.25.1
Sagi
Re: [RFC PATCH v5 086/104] KVM: TDX: handle ept violation/misconfig exit
Quoted 23 lines · Sagi Shahar
Quoted 22 lines · On Fri, Mar 4, 2022 at 12:23 PM <[email protected]> wrote:
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 withKVM_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.Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-)arch/x86/kvm/vmx/tdx.c +0
-0@@ -1081,6 +1081,40 @@ void tdx_deliver_interrupt(struct kvm_lapic *apic, int delivery_mode,__vmx_deliver_posted_interrupt (vcpu, &tdx->pi_desc, vector); }
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c void tdx_deliver_interrupt(struct kvm_lapic *apic, int delivery_mode, · line 1084
Quoted 1 line
arch/x86/kvm/vmx/tdx.c +1
-0+#define TDX_SEPT_PFERR (PFERR_WRITE_MASK | PFERR_USER_MASK)
TDX_SEPT_PFERRis defined using PFERR_.* bitmask but__vmx_handle_ept_violationis accepting an EPT_VIOLATION_.* bitmask. so (PFERR_WRITE_MASK|PFERR_USER_MASK) will get interpreted as (EPT_VIOLATION_ACC_WRITE|EPT_VIOLATION_ACC_INSTR) which will get translated to (PFERR_WRITE_MASK|PFERR_FETCH_MASK). Was that the intention of this code?
No. It's a mistake. We have corrected internally you can find corrected code in github repo or see it in next version.
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
Re: [RFC PATCH v5 087/104] KVM: TDX: handle EXCEPTION_NMI and EXTERNAL_INTERRUPT
Quoted 43 lines · On 3/4/22 20:49, [email protected] wrote:
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.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)arch/x86/kvm/vmx/tdx.c +17
-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 ;
arch/x86/kvm/vmx/tdx.c int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath) · line 1157
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +4
-0@@ -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 :
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
Re: [RFC PATCH v5 088/104] KVM: TDX: Add TDG.VP.VMCALL accessors to access guest vcpu registers
Quoted 24 lines · On Fri, 2022-03-04 at 11:49 -0800, [email protected] wrote:
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.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+)arch/x86/kvm/vmx/tdx.c +5
-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); \
arch/x86/kvm/vmx/tdx.c static __always_inline unsigned long tdexit_intr_info(struct kvm_vcpu *vcpu) · line 105
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+} \ +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);
Are they needed? Do those helpers provide more information than just using kvm_{reg}_read/write()?
Quoted 23 lines
arch/x86/kvm/vmx/tdx.c +20
-0+ +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;
Re: [RFC PATCH v5 088/104] KVM: TDX: Add TDG.VP.VMCALL accessors to access guest vcpu registers
In reply to [email protected]Quoted 10 lines · On 3/4/22 20:49, [email protected] wrote:
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.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+)
Quoted 46 lines
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;
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
Re: [RFC PATCH v5 089/104] KVM: TDX: Add a placeholder for handler of TDX hypercalls (TDG.VP.VMCALL)
Quoted 72 lines · On Fri, 2022-03-04 at 11:49 -0800, [email protected] wrote:
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.
Co-developed-by Xiaoyao Li <[email protected]>Signed-off-by Xiaoyao Li <[email protected]>Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- 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 +7
-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 ; }
arch/x86/kvm/vmx/tdx.c static int tdx_handle_triple_fault(struct kvm_vcpu *vcpu) · line 873
Quoted 9 lines
arch/x86/kvm/vmx/tdx.c +6
-0@@ -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;
Put a comment explaining this logic?
arch/x86/kvm/vmx/tdx.c static int tdx_handle_triple_fault(struct kvm_vcpu *vcpu) · line 875
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+ +switch (tdvmcall_exit_reason (vcpu)) {
Could we rename tdxvmcall_exit_reason() to something like tdvmcall_leaf()?
Btw, why couldn't we merge previous patch to this one, so we don't have to look back and forth to figure out exactly what do those functions do?
Quoted 45 lines
arch/x86/kvm/vmx/tdx.c +11
-0+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;
Paolo Bonzini
maintainerRe: [RFC PATCH v5 089/104] KVM: TDX: Add a placeholder for handler of TDX hypercalls (TDG.VP.VMCALL)
Quoted 9 lines · Kai Huang
arch/x86/kvm/vmx/tdx.c static int tdx_handle_triple_fault(struct kvm_vcpu *vcpu) · line 873
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c +6
-0+static int handle_tdvmcall (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (unlikely (tdx->tdvmcall.xmm_mask)) + goto unsupported;Put a comment explaining this logic?
This only seems to be necessary for Hyper-V hypercalls, which however are not supported by this series in TDX guests (because the kvm_hv_hypercall still calls kvm_*_read, likewise for Xen).
So for now this conditional can be dropped.
Since
Sean Christopherson
maintainerRe: [RFC PATCH v5 089/104] KVM: TDX: Add a placeholder for handler of TDX hypercalls (TDG.VP.VMCALL)
Quoted 10 lines · Paolo Bonzini
Quoted 8 lines · Kai Huang
arch/x86/kvm/vmx/tdx.c static int tdx_handle_triple_fault(struct kvm_vcpu *vcpu) · line 873
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c +6
-0+static int handle_tdvmcall (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (unlikely (tdx->tdvmcall.xmm_mask)) + goto unsupported;Put a comment explaining this logic?
Quoted 5 lines
This only seems to be necessary for Hyper-V hypercalls, which however are not supported by this series in TDX guests (because the
kvm_hv_hypercallstill calls kvm_*_read, likewise for Xen).So for now this conditional can be dropped.
I'd prefer to keep the sanity check, it's a cheap and easy way to detect a clear cut guest bug. E.g. KVM would be within its rights to write garbage the XMM registers in this case. Even though KVM isn't to be trusted, KVM can still be nice to the guest.
Paolo Bonzini
maintainerRe: [RFC PATCH v5 089/104] KVM: TDX: Add a placeholder for handler of TDX hypercalls (TDG.VP.VMCALL)
Quoted 11 lines · Sean Christopherson
Quoted 10 lines · Paolo Bonzini
Quoted 8 lines · Kai Huang
arch/x86/kvm/vmx/tdx.c static int tdx_handle_triple_fault(struct kvm_vcpu *vcpu) · line 873
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c +6
-0+static int handle_tdvmcall (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (unlikely (tdx->tdvmcall.xmm_mask)) + goto unsupported;Put a comment explaining this logic?
Quoted 8 lines
Quoted 5 lines
This only seems to be necessary for Hyper-V hypercalls, which however are not supported by this series in TDX guests (because the
kvm_hv_hypercallstill calls kvm_*_read, likewise for Xen).So for now this conditional can be dropped.
I'd prefer to keep the sanity check, it's a cheap and easy way to detect a clear cut guest bug.
I don't think it's necessarily a guest bug, just silly but valid behavior.
Paolo
E.g. KVM would be within its rights to write garbage the XMM
Quoted 2 lines
registers in this case. Even though KVM isn't to be trusted, KVM can still be nice to the guest.
Sean Christopherson
maintainerRe: [RFC PATCH v5 089/104] KVM: TDX: Add a placeholder for handler of TDX hypercalls (TDG.VP.VMCALL)
Quoted 16 lines · Paolo Bonzini
Quoted 15 lines · Sean Christopherson
Quoted 14 lines · Paolo Bonzini
Quoted 8 lines · Kai Huang
arch/x86/kvm/vmx/tdx.c static int tdx_handle_triple_fault(struct kvm_vcpu *vcpu) · line 873
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c +6
-0+static int handle_tdvmcall (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (unlikely (tdx->tdvmcall.xmm_mask)) + goto unsupported;Put a comment explaining this logic?
This only seems to be necessary for Hyper-V hypercalls, which however are not supported by this series in TDX guests (because the
kvm_hv_hypercallstill calls kvm_*_read, likewise for Xen).
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
So for now this conditional can be dropped.
I'd prefer to keep the sanity check, it's a cheap and easy way to detect a clear cut guest bug.
I don't think it's necessarily a guest bug, just silly but valid behavior.
It's a bug from a security perspective given that letting the host unnecessarily manipulate register state is an exploit waiting to happen.
Though for KVM to reject the TDVMCALLs, the GHCI should really be updated to state that exposing more state than is required _may_ be considered invalid ("may" so that KVM isn't required to check the mask on every exit, which IMO is beyond tedious).
Re: [RFC PATCH v5 089/104] KVM: TDX: Add a placeholder for handler of TDX hypercalls (TDG.VP.VMCALL)
Quoted 19 lines · Sean Christopherson
Quoted 18 lines · Paolo Bonzini
Quoted 17 lines · Sean Christopherson
Quoted 15 lines · Paolo Bonzini
Quoted 8 lines · Kai Huang
arch/x86/kvm/vmx/tdx.c static int tdx_handle_triple_fault(struct kvm_vcpu *vcpu) · line 873
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c +6
-0+static int handle_tdvmcall (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (unlikely (tdx->tdvmcall.xmm_mask)) + goto unsupported;Put a comment explaining this logic?
This only seems to be necessary for Hyper-V hypercalls, which however are not supported by this series in TDX guests (because the
kvm_hv_hypercallstill calls kvm_*_read, likewise for Xen).So for now this conditional can be dropped.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
I'd prefer to keep the sanity check, it's a cheap and easy way to detect a clear cut guest bug.
I don't think it's necessarily a guest bug, just silly but valid behavior.
It's a bug from a security perspective given that letting the host unnecessarily manipulate register state is an exploit waiting to happen.
Security perspective from guest's or host's respective? It's guest's responsibility to make sure itself doesn't expose unnecessarily security holes. In this particular case, if guest does, then it's guest's business, but I don't see how it can compromise host's security, or other VM's security?
As Paolo said it's a valid operation from guest, perhaps an alternative is KVM can unconditionally clear XMM registers, instead of rejecting this VMCALL.
Quoted 4 lines
Though for KVM to reject the TDVMCALLs, the GHCI should really be updated to state that exposing more state than is required _may_ be considered invalid ("may" so that KVM isn't required to check the mask on every exit, which IMO is beyond tedious).
Independent from this issue, I guess it's good for GHCI to have this anyway.
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 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
Re: [RFC PATCH v5 091/104] KVM: TDX: Handle TDX PV CPUID hypercall
Quoted 45 lines · On 3/4/22 20:49, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Wire up TDX PV CPUID hypercall to the KVM backend function.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)arch/x86/kvm/vmx/tdx.c +24
-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);
arch/x86/kvm/vmx/tdx.c static int handle_tdvmcall(struct kvm_vcpu *vcpu) · line 936
Quoted 9 lines
arch/x86/kvm/vmx/tdx.c +3
-0@@ -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 ; }
but I don't think tdvmcall_*_{read,write} add much.
Paolo
Sean Christopherson
maintainerRe: [RFC PATCH v5 091/104] KVM: TDX: Handle TDX PV CPUID hypercall
Quoted 42 lines · Paolo Bonzini
Quoted 41 lines · On 3/4/22 20:49, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Wire up TDX PV CPUID hypercall to the KVM backend function.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)arch/x86/kvm/vmx/tdx.c +24
-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)
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c static int handle_tdvmcall(struct kvm_vcpu *vcpu) · line 932
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c +3
-0{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); +
Spurious whitespace that gets deleted by the HLT patch.
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c static int handle_tdvmcall(struct kvm_vcpu *vcpu) · line 936
Quoted 3 lines
arch/x86/kvm/vmx/tdx.c +0
-0default :break ; }Reviewed-by Paolo Bonzini <[email protected]>but I don't think tdvmcall_*_{read,write} add much.
They provided a lot more value when the ABI was still in flux, but I still like having them. That said, either the comments about R12..R15 need to go, or the wrappers need to go. Having both is confusing.
Paolo Bonzini
maintainerRe: [RFC PATCH v5 091/104] KVM: TDX: Handle TDX PV CPUID hypercall
Quoted 7 lines · Sean Christopherson
Quoted 3 lines
Reviewed-by Paolo Bonzini<[email protected]>but I don't think tdvmcall_*_{read,write} add much.
They provided a lot more value when the ABI was still in flux, but I still like having them. That said, either the comments about R12..R15 need to go, or the wrappers need to go. Having both is confusing.
Fair enough, let's keep them but rename them a0..a3 for consistency with kvm_emulate_hypercall.
Paolo
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
Paolo Bonzini
maintainerRe: [RFC PATCH v5 092/104] KVM: TDX: Handle TDX PV HLT hypercall
arch/x86/kvm/vmx/tdx.c static int tdx_emulate_cpuid(struct kvm_vcpu *vcpu) · line 922
Quoted 1 line · On 3/4/22 20:49, [email protected] wrote:
arch/x86/kvm/vmx/tdx.c +1
-0+bool interrupt_disabled =tdvmcall_p1_read (vcpu);
Where is R12 documented for TDG.VP.VMCALL<Instruction.HLT>?
arch/x86/kvm/vmx/tdx.c static int tdx_emulate_cpuid(struct kvm_vcpu *vcpu) · line 931
Quoted 3 lines
arch/x86/kvm/vmx/tdx.c +3
-0+* 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.
I don't understand this; why isn't it enough to check PI.ON or something like that as part of HLT emulation?
arch/x86/kvm/vmx/tdx.c static int tdx_emulate_cpuid(struct kvm_vcpu *vcpu) · line 954
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+ details.full =td_state_non_arch_read64 ( +to_tdx (vcpu),TD_VCPU_STATE_DETAILS_NON_ARCH );
TDX documentation says "the meaning of the field may change with Intel TDX module version", where is this field documented? I cannot find any "other guest state" fields in the TDX documentation.
Paolo
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +5
-0+if (details.vmxip) +return 1 ; + } + +return kvm_emulate_halt_noskip (vcpu);
Sean Christopherson
maintainerRe: [RFC PATCH v5 092/104] KVM: TDX: Handle TDX PV HLT hypercall
Quoted 11 lines · Paolo Bonzini
arch/x86/kvm/vmx/tdx.c static int tdx_emulate_cpuid(struct kvm_vcpu *vcpu) · line 922
Quoted 1 line · On 3/4/22 20:49, [email protected] wrote:
arch/x86/kvm/vmx/tdx.c +1
-0+bool interrupt_disabled =tdvmcall_p1_read (vcpu);Where is R12 documented for TDG.VP.VMCALL<Instruction.HLT>?
arch/x86/kvm/vmx/tdx.c static int tdx_emulate_cpuid(struct kvm_vcpu *vcpu) · line 931
Quoted 3 lines
arch/x86/kvm/vmx/tdx.c +3
-0+* 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. I don't understand this; why isn't it enough to check PI.ON or something like that as part of HLT emulation?
Ooh, I think I remember what this is. This is for the case where the virtual interrupt is recognized, i.e. set in vmcs.RVI, between the STI and "HLT". KVM doesn't have access to RVI and the interrupt is no longer in the PID (because it was "recognized". It doesn't get delivered in the guest because the TDCALL completes before interrupts are enabled.
I lobbied to get this fixed in the TDX module by immediately resuming the guest in this case, but obviously that was unsuccessful.
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c static int tdx_emulate_cpuid(struct kvm_vcpu *vcpu) · line 954
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+ details.full =td_state_non_arch_read64 ( +to_tdx (vcpu),TD_VCPU_STATE_DETAILS_NON_ARCH );TDX documentation says "the meaning of the field may change with Intel TDX module version", where is this field documented? I cannot find any "other guest state" fields in the TDX documentation.
IMO we should put a stake in the ground and refuse to accept code that consumes "non-architectural" state. It's all software, having non-architectural APIs is completely ridiculous.
Paolo Bonzini
maintainerRe: [RFC PATCH v5 092/104] KVM: TDX: Handle TDX PV HLT hypercall
Quoted 13 lines · Sean Christopherson
Quoted 11 lines · Paolo Bonzini
arch/x86/kvm/vmx/tdx.c static int tdx_emulate_cpuid(struct kvm_vcpu *vcpu) · line 922
Quoted 1 line · On 3/4/22 20:49, [email protected] wrote:
arch/x86/kvm/vmx/tdx.c +1
-0+bool interrupt_disabled =tdvmcall_p1_read (vcpu);Where is R12 documented for TDG.VP.VMCALL<Instruction.HLT>?
arch/x86/kvm/vmx/tdx.c static int tdx_emulate_cpuid(struct kvm_vcpu *vcpu) · line 931
Quoted 3 lines
arch/x86/kvm/vmx/tdx.c +3
-0+* 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. I don't understand this; why isn't it enough to check PI.ON or something like that as part of HLT emulation?
Quoted 8 lines
Ooh, I think I remember what this is. This is for the case where the virtual interrupt is recognized, i.e. set in vmcs.RVI, between the STI and "HLT". KVM doesn't have access to RVI and the interrupt is no longer in the PID (because it was "recognized". It doesn't get delivered in the guest because the TDCALL completes before interrupts are enabled.
I lobbied to get this fixed in the TDX module by immediately resuming the guest in this case, but obviously that was unsuccessful.
So the TDX module sets RVI while in an STI interrupt shadow. So far so good. Then:
- it receives the HLT TDCALL from the guest. The interrupt shadow at this point is gone.
- it knows that there is an interrupt that can be delivered (RVI > PPR && EFLAGS.IF=1, the other conditions of 29.2.2 don't matter)
- it forwards the HLT TDCALL nevertheless, to a clueless hypervisor that has no way to glean either RVI or PPR?
It's absurd that this be treated as anything but a bug.
Until that is fixed, KVM needs to do something like:
- every time a bit is set in PID.PIR, set tdx->buggy_hlt_workaround = 1
- every time TDG.VP.VMCALL<HLT> is received, xchg(&tdx->buggy_hlt_workaround, 0) and return immediately to the guest if it is 1.
Basically an internal version of PID.ON.
Quoted 10 lines
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c static int tdx_emulate_cpuid(struct kvm_vcpu *vcpu) · line 954
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+ details.full =td_state_non_arch_read64 ( +to_tdx (vcpu),TD_VCPU_STATE_DETAILS_NON_ARCH );TDX documentation says "the meaning of the field may change with Intel TDX module version", where is this field documented? I cannot find any "other guest state" fields in the TDX documentation.
IMO we should put a stake in the ground and refuse to accept code that consumes "non-architectural" state. It's all software, having non-architectural APIs is completely ridiculous.
Having them is fine, *using* them to work around undocumented bugs is the ridiculous part.
You didn't answer the other question, which is "Where is R12 documented for TDG.VP.VMCALL<Instruction.HLT>?" though... Should I be worried? :)
Paolo
Sean Christopherson
maintainerRe: [RFC PATCH v5 092/104] KVM: TDX: Handle TDX PV HLT hypercall
Quoted 29 lines · Paolo Bonzini
Quoted 13 lines · Sean Christopherson
Quoted 11 lines · Paolo Bonzini
arch/x86/kvm/vmx/tdx.c static int tdx_emulate_cpuid(struct kvm_vcpu *vcpu) · line 922
Quoted 1 line · On 3/4/22 20:49, [email protected] wrote:
arch/x86/kvm/vmx/tdx.c +1
-0+bool interrupt_disabled =tdvmcall_p1_read (vcpu);Where is R12 documented for TDG.VP.VMCALL<Instruction.HLT>?
arch/x86/kvm/vmx/tdx.c static int tdx_emulate_cpuid(struct kvm_vcpu *vcpu) · line 931
Quoted 3 lines
arch/x86/kvm/vmx/tdx.c +3
-0+* 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. I don't understand this; why isn't it enough to check PI.ON or something like that as part of HLT emulation?
Quoted 8 lines
Ooh, I think I remember what this is. This is for the case where the virtual interrupt is recognized, i.e. set in vmcs.RVI, between the STI and "HLT". KVM doesn't have access to RVI and the interrupt is no longer in the PID (because it was "recognized". It doesn't get delivered in the guest because the TDCALL completes before interrupts are enabled.
I lobbied to get this fixed in the TDX module by immediately resuming the guest in this case, but obviously that was unsuccessful.
So the TDX module sets RVI while in an STI interrupt shadow. So far so good. Then:
- it receives the HLT TDCALL from the guest. The interrupt shadow at this point is gone.
Quoted 7 lines
- it knows that there is an interrupt that can be delivered (RVI > PPR && EFLAGS.IF=1, the other conditions of 29.2.2 don't matter)
- it forwards the HLT TDCALL nevertheless, to a clueless hypervisor that has no way to glean either RVI or PPR?
It's absurd that this be treated as anything but a bug.
That's what I said! :-)
Re: [RFC PATCH v5 092/104] KVM: TDX: Handle TDX PV HLT hypercall
In reply to Paolo BonziniQuoted 2 lines · Paolo Bonzini
You didn't answer the other question, which is "Where is R12 documented for TDG.VP.VMCALL<Instruction.HLT>?" though... Should I be worried? :)
It's publicly documented.
Guest-Host-Communication Interface(GHCI) spec, 344426-003US Feburary 2022. 3.8 TDG.VP.VMCALL<Instruction.HLT>
R12 Interrupt Blocked Flag.
The TD is expected to clear this flag iff RFLAGS.IF == 1 or the TDCALL instruction (that invoked TDG.VP.TDVMCALL(Instruction.HLT)) immediately follows an STI instruction, otherwise this flag should be set.
Signature · 1 line
Isaku Yamahata <[email protected]>
Paolo Bonzini
maintainerRe: [RFC PATCH v5 092/104] KVM: TDX: Handle TDX PV HLT hypercall
Quoted 6 lines · Isaku Yamahata
Quoted 2 lines · Paolo Bonzini
You didn't answer the other question, which is "Where is R12 documented for TDG.VP.VMCALL<Instruction.HLT>?" though... Should I be worried? :)
Quoted 8 lines
It's publicly documented.
Guest-Host-Communication Interface(GHCI) spec, 344426-003US Feburary 2022. 3.8 TDG.VP.VMCALL<Instruction.HLT>
R12 Interrupt Blocked Flag.
The TD is expected to clear this flag iff RFLAGS.IF == 1 or the TDCALL instruction (that invokedTDG.VP.TDVMCALL(Instruction.HLT)) immediately follows an STI instruction, otherwise this flag should be set.
Oh, Google doesn't know about this version of the spec... It can be downloaded from https://www.intel.com/content/www/us/en/developer/articles/technical/intel-trust-domain-extensions.html though.
I also found VCPU_STATE_DETAILS in https://www.intel.com/content/dam/develop/external/us/en/documents/tdx-module-1.0-public-spec-v0.931.pdf:
Bit 0: VMXIP, indicates that a virtual interrupt is pending delivery, i.e. VMCS.RVI[7:4] > TDVPS.VAPIC.VPPR[7:4]
It also documents how it has to be used. So this looks more or less okay, just rename "vmxip" to "interrupt_pending_delivery".
The VCPU_STATE_DETAILS being "non-architectural" is still worrisome.
Paolo
Sean Christopherson
maintainerRe: [RFC PATCH v5 092/104] KVM: TDX: Handle TDX PV HLT hypercall
Quoted 20 lines · Paolo Bonzini
Quoted 6 lines · Isaku Yamahata
Quoted 2 lines · Paolo Bonzini
You didn't answer the other question, which is "Where is R12 documented for TDG.VP.VMCALL<Instruction.HLT>?" though... Should I be worried? :)
Quoted 8 lines
It's publicly documented.
Guest-Host-Communication Interface(GHCI) spec, 344426-003US Feburary 2022. 3.8 TDG.VP.VMCALL<Instruction.HLT>
R12 Interrupt Blocked Flag.
The TD is expected to clear this flag iff RFLAGS.IF == 1 or the TDCALL instruction (that invokedTDG.VP.TDVMCALL(Instruction.HLT)) immediately follows an STI instruction, otherwise this flag should be set.Oh, Google doesn't know about this version of the spec... It can be
downloaded from https://www.intel.com/content/www/us/en/developer/articles/technical/intel-trust-domain-extensions.html
though.
Quoted 7 lines
I also found
VCPU_STATE_DETAILSin https://www.intel.com/content/dam/develop/external/us/en/documents/tdx-module-1.0-public-spec-v0.931.pdf:Bit 0: VMXIP, indicates that a virtual interrupt is pending delivery, i.e. VMCS.RVI[7:4] > TDVPS.VAPIC.VPPR[7:4]
It also documents how it has to be used. So this looks more or less okay, just rename "vmxip" to "
interrupt_pending_delivery".
If we're keeping the call back into SEAM, then this belongs in the path of apic_has_interrupt_for_ppr(), not in the HLT-exit path. To avoid multiple SEAMCALLS in a single exit, VCPU_EXREG_RVI can be added.
Paolo Bonzini
maintainerRe: [RFC PATCH v5 092/104] KVM: TDX: Handle TDX PV HLT hypercall
Quoted 6 lines · Sean Christopherson
Quoted 2 lines
It also documents how it has to be used. So this looks more or less okay, just rename "vmxip" to "
interrupt_pending_delivery".If we're keeping the call back into SEAM, then this belongs in the path of
apic_has_interrupt_for_ppr(), not in the HLT-exit path. To avoid multiple SEAMCALLS in a single exit,VCPU_EXREG_RVIcan be added.
But apic_has_interrupt_for_ppr takes a PPR argument and that is not available.
So I suppose you mean kvm_apic_has_interrupt? You would change that to a callback, like
if (!kvm_apic_present(vcpu))
return -1;
return static_call(kvm_x86_apic_has_interrupt)(vcpu);
}and the default version would also be inlined in kvm_get_apic_interrupt, like
- int vector = kvm_apic_has_interrupt(vcpu);
struct kvm_lapic *apic = vcpu->arch.apic;
u32 ppr;- if (vector == -1)
+ if (!kvm_apic_present(vcpu))
return -1;
+ __apic_update_ppr(apic, &ppr);
+ vector = apic_has_interrupt_for_ppr(apic, ppr);
Checking the SEAM state (which would likewise not be VCPU_EXREG_RVI, but more like VCPU_EXREG_INTR_PENDING) would be done in the tdx case of kvm_x86_apic_has_interrupt.
Paolo
Sean Christopherson
maintainerRe: [RFC PATCH v5 092/104] KVM: TDX: Handle TDX PV HLT hypercall
Quoted 4 lines · Paolo Bonzini
Quoted 3 lines · Sean Christopherson
Quoted 2 lines
It also documents how it has to be used. So this looks more or less okay, just rename "vmxip" to "
interrupt_pending_delivery".
Quoted 8 lines
Quoted 3 lines
If we're keeping the call back into SEAM, then this belongs in the path of
apic_has_interrupt_for_ppr(), not in the HLT-exit path. To avoid multiple SEAMCALLS in a single exit,VCPU_EXREG_RVIcan be added.But
apic_has_interrupt_for_pprtakes a PPR argument and that is not available.So I suppose you mean
kvm_apic_has_interrupt?
Yeah, I realized that when I actually tried to implement my idea in code :-)
My hopefully-fully-thought-out idea for handling this:
https://lore.kernel.org/all/[email protected]
https://lore.kernel.org/all/[email protected]
Sean Christopherson
maintainerRe: [RFC PATCH v5 092/104] KVM: TDX: Handle TDX PV HLT hypercall
In reply to [email protected]Quoted 10 lines · On Fri, Mar 04, 2022, [email protected] wrote:
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.
Pretty sure you mean STI, not CLI.
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
Re: [RFC PATCH v5 093/104] KVM: TDX: Handle TDX PV port io hypercall
Quoted 74 lines · On 3/4/22 20:49, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Wire up TDX PV port IO hypercall to the KVM backend function.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 55 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+)arch/x86/kvm/vmx/tdx.c +53
-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);
arch/x86/kvm/vmx/tdx.c static int handle_tdvmcall(struct kvm_vcpu *vcpu) · line 1034
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c +2
-0@@ -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 ; }
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
Re: [RFC PATCH v5 094/104] KVM: TDX: Handle TDX PV MMIO hypercall
Quoted 176 lines · On 3/4/22 20:49, [email protected] wrote:
From: Sean Christopherson <[email protected]>
Export
kvm_io_bus_readandkvm_mmiotracepoint 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, exportkvm_mmiotracepoint.Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- 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 +1
-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) {
virt/kvm/kvm_main.c int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr, · line 5133
Quoted 7 lines
virt/kvm/kvm_main.c +1
-0@@ -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,
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
Re: [RFC PATCH v5 095/104] KVM: TDX: Implement callbacks for MSR operations for TDX
Quoted 172 lines · On 3/4/22 20:49, [email protected] wrote:
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.
Signed-off-by Isaku Yamahata <[email protected]>--- 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@@ -1517,6 +1517,74 @@ void tdx_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason, *error_code =0 ; } +bool tdx_is_emulated_msr (u32 index,bool write) +{ +switch (index) { +case MSR_IA32_UCODE_REV : +case MSR_IA32_ARCH_CAPABILITIES : +case MSR_IA32_POWER_CTL : +case MSR_MTRRcap: + case 0x200 ... 0x26f: +/* IA32_MTRR_PHYS{BASE, MASK}, IA32_MTRR_FIX*_* */ +case MSR_IA32_CR_PAT : +case MSR_MTRRdefType: +case MSR_IA32_TSC_DEADLINE : +case MSR_IA32_MISC_ENABLE : +case MSR_KVM_STEAL_TIME : +case MSR_KVM_POLL_CONTROL : +case MSR_PLATFORM_INFO : +case MSR_MISC_FEATURES_ENABLES : +case MSR_IA32_MCG_CAP : +case MSR_IA32_MCG_STATUS : +case MSR_IA32_MCG_CTL : +case MSR_IA32_MCG_EXT_CTL : + case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_MISC(28) - 1: +/* MSR_IA32_MCx_{CTL, STATUS, ADDR, MISC} */ +return true; + case APIC_BASE_MSR ... APIC_BASE_MSR + 0xff: +/* +* x2APIC registers that are virtualized by the CPU can't be +* emulated, KVM doesn't have access to the virtual APIC page. +*/ +switch (index) { +case X2APIC_MSR (APIC_TASKPRI ): +case X2APIC_MSR (APIC_PROCPRI ): +case X2APIC_MSR (APIC_EOI ): + case X2APIC_MSR(APIC_ISR) ... X2APIC_MSR(APIC_ISR + APIC_ISR_NR): + case X2APIC_MSR(APIC_TMR) ... X2APIC_MSR(APIC_TMR + APIC_ISR_NR): + case X2APIC_MSR(APIC_IRR) ... X2APIC_MSR(APIC_IRR + APIC_ISR_NR): +return false; +default : +return true; + } +case MSR_IA32_APICBASE : +case MSR_EFER : +return !write; + case MSR_IA32_MCx_CTL2(0) ... MSR_IA32_MCx_CTL2(31): +/* +* 0x280 - 0x29f: The x86 common code doesn't emulate MCx_CTL2. +* Refer to kvm_{get,set}_msr_common(), +* kvm_mtrr_{get, set}_msr(), and msr_mtrr_valid(). +*/ +default : +return false; + } +} + +int tdx_get_msr (struct kvm_vcpu *vcpu,struct msr_data *msr) +{ +if (tdx_is_emulated_msr (msr->index, false)) +return kvm_get_msr_common (vcpu, msr); +return 1 ; +} + +int tdx_set_msr (struct kvm_vcpu *vcpu,struct msr_data *msr) +{ +if (tdx_is_emulated_msr (msr->index, true)) +return kvm_set_msr_common (vcpu, msr); +return 1 ; +} +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 +3
-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);
arch/x86/kvm/vmx/x86_ops.h static inline void tdx_inject_nmi(struct kvm_vcpu *vcpu) {} · line 204
Quoted 9 lines
arch/x86/kvm/vmx/x86_ops.h +3
-0@@ -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 ; }
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
Re: [RFC PATCH v5 096/104] KVM: TDX: Handle TDX PV rdmsr hypercall
Quoted 38 lines · On 3/4/22 20:49, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Wire up TDX PV rdmsr hypercall to the KVM backend function.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)arch/x86/kvm/vmx/tdx.c +17
-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);
arch/x86/kvm/vmx/tdx.c static int handle_tdvmcall(struct kvm_vcpu *vcpu) · line 1167
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c +2
-0@@ -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 ; }
and feel free to squash with the wrmsr one.
Paolo
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 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
Paolo Bonzini
maintainerRe: [RFC PATCH v5 098/104] KVM: TDX: Handle TDX PV report fatal error hypercall
Quoted 16 lines · On 3/4/22 20:49, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Wire up TDX PV report fatal error hypercall to
KVM_SYSTEM_EVENT_CRASHKVM exit event.Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 11 +++++++++++ 1 file changed, 11 insertions(+)arch/x86/kvm/vmx/tdx.c +0
-0@@ -1157,6 +1157,15 @@ static int tdx_emulate_wrmsr(struct kvm_vcpu *vcpu)return 1 ;
arch/x86/kvm/vmx/tdx.c static int tdx_emulate_wrmsr(struct kvm_vcpu *vcpu) · line 1167
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +8
-0} +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 ; +}
With the latest SEV changes we have a data[] member. Please set type instead to KVM_SYSTEM_EVENT_CRASH|KVM_SYSTEM_EVENT_NDATA_VALID and ndata to 1, so that the value of p1 (which will be a0 in the next series) can go in data[0].
Paolo
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 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
Paolo Bonzini
maintainerRe: [RFC PATCH v5 100/104] KVM: TDX: Silently discard SMI request
Quoted 9 lines · On 3/4/22 20:49, [email protected] wrote:
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.
Quoted 5 lines
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).
Please also:
1) return zero from vmx_has_emulated_msr(MSR_IA32_SMBASE) for TDX virtual machines.
2) do a check for static_call(kvm_x86_has_emulated_msr)(kvm, MSR_IA32_SMBASE) in kvm_vcpu_ioctl_smi and __apic_accept_irq.
3) WARN_ON_ONCE in tdx_smi_allowed and tdx_enable_smi_window.
Paolo
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
Paolo Bonzini
maintainerRe: [RFC PATCH v5 101/104] KVM: TDX: Silently ignore INIT/SIPI
Quoted 6 lines · On 3/4/22 20:49, [email protected] wrote:
arch/x86/kvm/lapic.c +6
-0+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. +*/
arch/x86/kvm/lapic.c int kvm_apic_accept_events(struct kvm_vcpu *vcpu) · line 2937
Quoted 10 lines
arch/x86/kvm/lapic.c +7
-2vcpu->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 ; + }
Should you check vcpu->arch.guest_state_protected instead of special-casing TDX? KVM_APIC_INIT is not valid for SEV-ES either, if I remember correctly.
Paolo
Re: [RFC PATCH v5 101/104] KVM: TDX: Silently ignore INIT/SIPI
Quoted 18 lines · Paolo Bonzini
Quoted 8 lines · On 3/4/22 20:49, [email protected] wrote:
+ 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_vectorignores it.
+ */
vcpu->arch.mp_state=KVM_MP_STATE_RUNNABLE; - elseQuoted 8 lines
-
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; + }
Quoted 1 line
Should you check
vcpu->arch.guest_state_protectedinstead of special-casing TDX?KVM_APIC_INITis not valid for SEV-ES either, if I remember correctly.
While the INIT doesn't update any actual state that is in the encrypted VMSA, SEV-ES still calls kvm_vcpu_reset() to allow KVM to set any internal tracking state, etc. I haven't ever tested SEV-ES where that is bypassed.
Thanks,
Tom
Quoted 2 lines
Paolo
Re: [RFC PATCH v5 101/104] KVM: TDX: Silently ignore INIT/SIPI
In reply to Paolo BonziniQuoted 18 lines · Paolo Bonzini
Quoted 8 lines · On 3/4/22 20:49, [email protected] wrote:
+ 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_vectorignores it.
+ */
vcpu->arch.mp_state=KVM_MP_STATE_RUNNABLE; - elseQuoted 8 lines
-
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; + }
Quoted 1 line
Should you check
vcpu->arch.guest_state_protectedinstead of special-casing TDX?
We cannot use vcpu->arch.guest_state_protected because TDX supports debug TD, of which the states are not protected.
At least we need another flag, I think.
Quoted 3 lines
KVM_APIC_INITis not valid for SEV-ES either, if I remember correctly.Paolo
Paolo Bonzini
maintainerRe: [RFC PATCH v5 101/104] KVM: TDX: Silently ignore INIT/SIPI
Quoted 19 lines · Xiaoyao Li
Quoted 18 lines · Paolo Bonzini
Quoted 16 lines · On 3/4/22 20:49, [email protected] wrote:
+ 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_vectorignores 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; + }
Quoted 5 lines
Quoted 1 line
Should you check
vcpu->arch.guest_state_protectedinstead of special-casing TDX?We cannot use
vcpu->arch.guest_state_protectedbecause TDX supports debug TD, of which the states are not protected.At least we need another flag, I think.
Let's add .deliver_init to the kvm_x86_ops then.
Paolo
Re: [RFC PATCH v5 101/104] KVM: TDX: Silently ignore INIT/SIPI
Quoted 23 lines · Paolo Bonzini
Quoted 22 lines · Xiaoyao Li
Quoted 20 lines · Paolo Bonzini
Quoted 8 lines · On 3/4/22 20:49, [email protected] wrote:
+ 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_vectorignores it.
+ */
vcpu->arch.mp_state=KVM_MP_STATE_RUNNABLE; - elseQuoted 8 lines
-
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; + }Should you check
vcpu->arch.guest_state_protectedinstead of special-casing TDX?
Quoted 6 lines
Quoted 4 lines
We cannot use
vcpu->arch.guest_state_protectedbecause TDX supports debug TD, of which the states are not protected.At least we need another flag, I think.
Let's add .deliver_init to the
kvm_x86_opsthen.
Will do.
Signature · 1 line
Isaku Yamahata <[email protected]>
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
Paolo Bonzini
maintainerRe: [RFC PATCH v5 102/104] KVM: TDX: Add methods to ignore accesses to CPU state
Quoted 7 lines · On 3/4/22 20:49, [email protected] wrote:
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.
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>
For most of these, it would be interesting to see which paths actually can be hit. For SEV, it's all cut out by
if (vcpu->arch.guest_state_protected)
return 0;in functions such as __set_sregs_common. Together with the fact that TDX does not get to e.g. handle_set_cr0, this should prevent most such calls from happening. So most of these should be KVM_BUG_ON or WARN_ON, not just returns.
Thanks,
Paolo
Quoted 682 lines
--- 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@@ -3,6 +3,7 @@#include <linux/mmu_context.h> #include <asm/fpu/xcr.h> +#include <asm/virtext.h> #include <asm/tdx.h> #include "capabilities.h" @@ -1717,6 +1718,49 @@ void tdx_enable_smi_window(struct kvm_vcpu *vcpu) vcpu->arch.smi_pending = false; } +void tdx_set_virtual_apic_mode (struct kvm_vcpu *vcpu) +{ +/* Only x2APIC mode is supported for TD. */ +WARN_ON_ONCE (kvm_get_apic_mode (vcpu) !=LAPIC_MODE_X2APIC ); +} + +int tdx_get_cpl (struct kvm_vcpu *vcpu) +{ +return 0 ; +} + +void tdx_cache_reg (struct kvm_vcpu *vcpu,enum kvm_reg reg) +{ +kvm_register_mark_available (vcpu, reg); +switch (reg) { +case VCPU_REGS_RSP : +case VCPU_REGS_RIP : +case VCPU_EXREG_PDPTR : +case VCPU_EXREG_CR0 : +case VCPU_EXREG_CR3 : +case VCPU_EXREG_CR4 : +break ; +default : +KVM_BUG_ON (1 , vcpu->kvm); +break ; + } +} + +unsigned long tdx_get_rflags (struct kvm_vcpu *vcpu) +{ +return 0 ; +} + +u64 tdx_get_segment_base (struct kvm_vcpu *vcpu,int seg) +{ +return 0 ; +} + +void tdx_get_segment (struct kvm_vcpu *vcpu,struct kvm_segment *var,int seg) +{ +memset (var,0 ,sizeof (*var)); +} +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 +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 ; }
Re: [RFC PATCH v5 102/104] KVM: TDX: Add methods to ignore accesses to CPU state
Quoted 12 lines · Paolo Bonzini
Quoted 7 lines · On 3/4/22 20:49, [email protected] wrote:
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.
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>For most of these, it would be interesting to see which paths actually can be hit. For SEV, it's all cut out by
Quoted 7 lines
if (vcpu->arch.guest_state_protected) return 0;in functions such as
__set_sregs_common. Together with the fact that TDX does not get to e.g.handle_set_cr0, this should prevent most such calls from happening. So most of these should beKVM_BUG_ONorWARN_ON, not just returns.
If debug mode is enabled, guest state isn't protected. memory/cpu state can be read/written via SEAMCALLs. So guest_state_protected isn't set to true.
Anyway for now with this patch series, debug mode isn't supported well, I will go with adding KVM_BUG_ON/WARN_ON.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [RFC PATCH v5 102/104] KVM: TDX: Add methods to ignore accesses to CPU state
In reply to [email protected]Quoted 5 lines · On 3/5/2022 3:49 AM, [email protected] wrote:
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.
...
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c +6
-0+void tdx_cache_reg (struct kvm_vcpu *vcpu,enum kvm_reg reg) +{ +kvm_register_mark_available (vcpu, reg); +switch (reg) { +case VCPU_REGS_RSP : +case VCPU_REGS_RIP :
arch/x86/kvm/vmx/tdx.c void tdx_enable_smi_window(struct kvm_vcpu *vcpu) · line 1747
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+case VCPU_EXREG_PDPTR : +case VCPU_EXREG_CR0 : +case VCPU_EXREG_CR3 : +case VCPU_EXREG_CR4 : +break ; +default : +KVM_BUG_ON (1 , vcpu->kvm); +break ; + } +}
Isaku,
We missed one case that some GPRs are accessible by KVM/userspace for TDVMCALL exit.
Paolo Bonzini
maintainerRe: [RFC PATCH v5 102/104] KVM: TDX: Add methods to ignore accesses to CPU state
Quoted 20 lines · Xiaoyao Li
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c +6
-0+void tdx_cache_reg (struct kvm_vcpu *vcpu,enum kvm_reg reg) +{ + kvm_register_mark_available(vcpu, reg); + switch (reg) { + case VCPU_REGS_RSP: + case VCPU_REGS_RIP:arch/x86/kvm/vmx/tdx.c void tdx_enable_smi_window(struct kvm_vcpu *vcpu) · line 1747
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+ case VCPU_EXREG_PDPTR: + case VCPU_EXREG_CR0: + case VCPU_EXREG_CR3: + case VCPU_EXREG_CR4: + break; + default: + KVM_BUG_ON(1, vcpu->kvm); + break; + } +}Isaku,
Quoted 1 line
We missed one case that some GPRs are accessible by KVM/userspace for TDVMCALL exit.
If a register is not in the VMX_REGS_LAZY_LOAD_SET it will never be passed to tdx_cache_reg. As far as I understand those TDVMCALL registers do not include either RSP or RIP.
Paolo
Re: [RFC PATCH v5 102/104] KVM: TDX: Add methods to ignore accesses to CPU state
Quoted 21 lines · Paolo Bonzini
Quoted 20 lines · Xiaoyao Li
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c +6
-0+void tdx_cache_reg (struct kvm_vcpu *vcpu,enum kvm_reg reg) +{ + kvm_register_mark_available(vcpu, reg); + switch (reg) { + case VCPU_REGS_RSP: + case VCPU_REGS_RIP:arch/x86/kvm/vmx/tdx.c void tdx_enable_smi_window(struct kvm_vcpu *vcpu) · line 1747
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+ case VCPU_EXREG_PDPTR: + case VCPU_EXREG_CR0: + case VCPU_EXREG_CR3: + case VCPU_EXREG_CR4: + break; + default: + KVM_BUG_ON(1, vcpu->kvm); + break; + } +}Isaku,
Quoted 3 lines
Quoted 1 line
We missed one case that some GPRs are accessible by KVM/userspace for TDVMCALL exit.
If a register is not in the
VMX_REGS_LAZY_LOAD_SETit will never be passed to tdx_cache_reg. As far as I understand those TDVMCALL registers do not include either RSP or RIP.
Sorry, I should not keep the code snippet of tdx_cache_reg() as reference to mislead you and other people.
I just want to remind that in the certain case of TDVMCALL, GPRs might be accessible.
Quoted 2 lines
Paolo
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 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
A series of 104 patches is completely unreviewably, please split it into reasonable chunks.
Paolo Bonzini
maintainerQuoted 2 lines · Christoph Hellwig
A series of 104 patches is completely unreviewably, please split it into reasonable chunks.
It is split into 5-15 patch chunks, and I'm going to review it mostly according to the separation. It's just posted together because it doesn't really accomplish anything until all the chunks are merged together.
From the cover letter:
Quoted 57 lines
Quoted 57 lines
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 outvmx_x86_opsto '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"
Quoted 8 lines
Quoted 8 lines
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 "
Paolo
Paolo Bonzini
maintainerQuoted 11 lines · On 3/4/22 20:48, [email protected] wrote:
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.
I think I have reviewed everything except the TDP MMU parts (48, 54-57).
I will do those next week, but in the meanwhile feel free to send v6
if you have it ready. A lot of the requests have been cosmetic.
If you would like to use something like Trello to track all the changes, and submit before you have done all of them, that's fine by me.
Paolo
Quoted 620 lines
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-upstreamThe 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 supportThe 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 outvmx_x86_opsto '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()elsevmx_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 definesCONFIG_INTEL_TDX_HOSTand this patch series usesCONFIG_INTEL_TDX_HOST. It's assumed that WithCONFIG_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
- intseamrr_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.
- intinit_tdx(void);
Initialization of TDX module so that the TDX module is ready for KVM to use.
- const structtdsysinfo_struct*tdx_get_sysinfo(void);
Return the system wide information about the TDX module. NULL if the TDX isn't initialized.
- u32tdx_get_global_keyid(void);
Return global key id that is used for the TDX module itself.
- inttdx_keyid_alloc(void);
Allocate HKID for guest TD.
- voidtdx_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, u32tdx_get_global_keyid(void), inttdx_keyid_alloc(void)and,
voidtdx_keyid_free(int keyid).- Data structures and VM type
Because TDX is different from VMX, define its own VM/VCPU structures, structkvm_tdxand structvcpu_tdxinstead of structkvm_vmxand structvcpu_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 existingKVM_MEMORY_ENCRYPT_OPto 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()elsevmx_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 ignoreNote: 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 EAShttps://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/TDVFChao Gao (1):
KVM: x86: Allow to update cached values inkvm_user_return_msrsw/o
wrmsrIsaku Yamahata (73):
x86/virt/tdx: exportplatform_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 specificmem_enc_opioctl
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: makeREMOVED_SPTEincludeshadow_initialvalue
KVM: x86/tdp_mmu: refactorkvm_tdp_mmu_map()
KVM: x86/mmu: add a private pointer to structkvm_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: KeepPRIVATE_PROHIBITbit when zapping
KVM: x86/tdp_mmu: prevent private/shared map based onPRIVATE_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 aswitch_db_regsflag to handle TDX's auto-switched
behavior
KVM: TDX: Implement interrupt injection
KVM: TDX: Implements vcpurequest_immediate_exit
KVM: TDX: Implement methods to inject NMI
KVM: TDX: Add a place holder to handle TDX VM exit
KVM: TDX: handleEXIT_REASON_OTHER_SMI
KVM: TDX: handle ept violation/misconfig exit
KVM: TDX: handleEXCEPTION_NMIandEXTERNAL_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 PVmap_gpahypercall
KVM: TDX: Silently discard SMI request
KVM: TDX: Silently ignore INIT/SIPIDocumentation/virtual/kvm: Document on Trust Domain Extensions(TDX)
KVM: x86: design documentation on TDX support of x86 KVM TDP MMUKai Huang (1):
KVM: x86: Introduce hooks to free VM callback prezap andvm_freeRick Edgecombe (1):
KVM: x86: Add infrastructure for stolen GPA bitsSean Christopherson (26):
KVM: VMX: Move outvmx_x86_opsto 'main.c' to wrap VMX and TDX
KVM: Enable hardware before doing arch VM initialization
KVM: x86: Introducevm_typeto 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 intdx.hwith structs, accessors, and VMCS helpers
KVM: Addmax_vcpusfield 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: Addload_mmu_pgdmethod for TDX
KVM: x86/mmu: Introducekvm_mmu_map_tdp_page()for use by TDX
KVM: x86: Check for pending APICv interrupt inkvm_vcpu_has_events()
KVM: x86: Add option to force LAPIC expiration wait
KVM: VMX: Modify NMI and INTR handlers to takeintr_infoas 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 stateXiaoyao Li (1):
KVM: TDX: initialize VM with TDX specific parametersYuan Yao (1):
KVM: TDX: Usevcpu_to_pi_desc()uniformly inposted_intr.cDocumentation/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.rstcreate mode 100644Documentation/virt/kvm/intel-tdx.rstcreate mode 100644Documentation/virt/kvm/tdx-tdp-mmu.rstcreate mode 100644arch/x86/kvm/vmx/common.h
create mode 100644arch/x86/kvm/vmx/main.c
create mode 100644arch/x86/kvm/vmx/seamcall.S
create mode 100644arch/x86/kvm/vmx/seamcall.h
create mode 100644arch/x86/kvm/vmx/tdx.c
create mode 100644arch/x86/kvm/vmx/tdx.h
create mode 100644arch/x86/kvm/vmx/tdx_arch.h
create mode 100644arch/x86/kvm/vmx/tdx_errno.h
create mode 100644arch/x86/kvm/vmx/tdx_error.c
create mode 100644arch/x86/kvm/vmx/tdx_ops.h
create mode 100644arch/x86/kvm/vmx/x86_ops.h
Paolo Bonzini
maintainerQuoted 13 lines · Paolo Bonzini
Quoted 11 lines · On 3/4/22 20:48, [email protected] wrote:
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.
Quoted 5 lines
I think I have reviewed everything except the TDP MMU parts (48, 54-57).
I will do those next week, but in the meanwhile feel free to send v6
if you have it ready. A lot of the requests have been cosmetic.If you would like to use something like Trello to track all the changes, and submit before you have done all of them, that's fine by me.
Also, I have now pushed what (I think) should be all that's needed to run TDX guests at branch kvm-tdx-5.17 of https://git.kernel.org/pub/scm/virt/kvm/kvm.git. It's only compile-tested for now, but if I missed something please report so that it can be used by people doing other work (including QEMU, TDVF and guest).
Thanks,
Paolo
Quoted 638 lines
Paolo
Quoted 635 lines
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-upstreamThe 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 supportThe 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 outvmx_x86_opsto '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()elsevmx_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 definesCONFIG_INTEL_TDX_HOSTand this patch series uses CONFIG_INTEL_TDX_HOST. It's assumed that WithCONFIG_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
- intseamrr_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.
- intinit_tdx(void);
Initialization of TDX module so that the TDX module is ready for KVM to use.
- const structtdsysinfo_struct*tdx_get_sysinfo(void);
Return the system wide information about the TDX module. NULL if the TDX isn't initialized.
- u32tdx_get_global_keyid(void);
Return global key id that is used for the TDX module itself.
- inttdx_keyid_alloc(void);
Allocate HKID for guest TD.
- voidtdx_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, u32tdx_get_global_keyid(void), inttdx_keyid_alloc(void)and,
voidtdx_keyid_free(int keyid).- Data structures and VM type
Because TDX is different from VMX, define its own VM/VCPU structures, structkvm_tdxand structvcpu_tdxinstead of structkvm_vmxand 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 existingKVM_MEMORY_ENCRYPT_OPto 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()elsevmx_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 ignoreNote: 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
[4] Intel TDX Module 1.0 EAS
[5] Intel TDX Loader Interface Specification
[6] Intel TDX Guest-Hypervisor Communication Interface
[7] Intel TDX Virtual Firmware Design Guide
[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/TDVFChao Gao (1):
KVM: x86: Allow to update cached values inkvm_user_return_msrsw/o wrmsrIsaku Yamahata (73):
x86/virt/tdx: exportplatform_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 specificmem_enc_opioctl
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: makeREMOVED_SPTEincludeshadow_initialvalue
KVM: x86/tdp_mmu: refactorkvm_tdp_mmu_map()
KVM: x86/mmu: add a private pointer to structkvm_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: KeepPRIVATE_PROHIBITbit when zapping
KVM: x86/tdp_mmu: prevent private/shared map based onPRIVATE_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 aswitch_db_regsflag to handle TDX's auto-switched behavior
KVM: TDX: Implement interrupt injection
KVM: TDX: Implements vcpurequest_immediate_exit
KVM: TDX: Implement methods to inject NMI
KVM: TDX: Add a place holder to handle TDX VM exit
KVM: TDX: handleEXIT_REASON_OTHER_SMI
KVM: TDX: handle ept violation/misconfig exit
KVM: TDX: handleEXCEPTION_NMIandEXTERNAL_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 PVmap_gpahypercall
KVM: TDX: Silently discard SMI request
KVM: TDX: Silently ignore INIT/SIPIDocumentation/virtual/kvm: Document on Trust Domain Extensions(TDX)
KVM: x86: design documentation on TDX support of x86 KVM TDP MMUKai Huang (1):
KVM: x86: Introduce hooks to free VM callback prezap andvm_freeRick Edgecombe (1):
KVM: x86: Add infrastructure for stolen GPA bitsSean Christopherson (26):
KVM: VMX: Move outvmx_x86_opsto 'main.c' to wrap VMX and TDX
KVM: Enable hardware before doing arch VM initialization
KVM: x86: Introducevm_typeto 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 intdx.hwith structs, accessors, and VMCS helpers
KVM: Addmax_vcpusfield 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: Addload_mmu_pgdmethod for TDX
KVM: x86/mmu: Introducekvm_mmu_map_tdp_page()for use by TDX
KVM: x86: Check for pending APICv interrupt inkvm_vcpu_has_events()
KVM: x86: Add option to force LAPIC expiration wait
KVM: VMX: Modify NMI and INTR handlers to takeintr_infoas 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 stateXiaoyao Li (1):
KVM: TDX: initialize VM with TDX specific parametersYuan Yao (1):
KVM: TDX: Usevcpu_to_pi_desc()uniformly inposted_intr.cDocumentation/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.rstcreate mode 100644Documentation/virt/kvm/intel-tdx.rstcreate mode 100644Documentation/virt/kvm/tdx-tdp-mmu.rstcreate mode 100644arch/x86/kvm/vmx/common.h
create mode 100644arch/x86/kvm/vmx/main.c
create mode 100644arch/x86/kvm/vmx/seamcall.S
create mode 100644arch/x86/kvm/vmx/seamcall.h
create mode 100644arch/x86/kvm/vmx/tdx.c
create mode 100644arch/x86/kvm/vmx/tdx.h
create mode 100644arch/x86/kvm/vmx/tdx_arch.h
create mode 100644arch/x86/kvm/vmx/tdx_errno.h
create mode 100644arch/x86/kvm/vmx/tdx_error.c
create mode 100644arch/x86/kvm/vmx/tdx_ops.h
create mode 100644arch/x86/kvm/vmx/x86_ops.h
Quoted 8 lines · Paolo Bonzini
Quoted 7 lines · On 3/4/22 20:48, [email protected] wrote:
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]/
Quoted 8 lines
Quoted 4 lines
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.I think I have reviewed everything except the TDP MMU parts (48, 54-57). I will do those next week, but in the meanwhile feel free to send v6 if you have it ready. A lot of the requests have been cosmetic.
Thank you so much. I'm updating patches now.
Quoted 2 lines
If you would like to use something like Trello to track all the changes, and submit before you have done all of them, that's fine by me.
Sure. I've created public trello board.
If you want to edit it, please let me know. I'll add you to the project member.
https://trello.com/kvmtdxreview
thanks,
Signature · 1 line
Isaku Yamahata <[email protected]>