Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp486204pxb; Thu, 24 Mar 2022 00:32:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwl6lQucB6eTPLVA4OP+sE7X8wzuLvlUFPXnt6IQTmXUadSar1+ieF6987TwXmL2WVa0W7a X-Received: by 2002:a17:903:41c7:b0:154:25bf:7d20 with SMTP id u7-20020a17090341c700b0015425bf7d20mr4267362ple.113.1648107136813; Thu, 24 Mar 2022 00:32:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648107136; cv=none; d=google.com; s=arc-20160816; b=wE1xmzQO+wJR/7bSC90wniAeNj5mHIDaYDegBWl2ZEHrlmddiJHJc1yJ9OfqpGjbEc hC8Gi0tsGFJ0D6LLj+55BkwcxYJMDBMMnB0/vL0q9j5vPlHY1/SNkIE7qmS4YEbc20AP /h8joeLu7iUYFOIcbTU16uvrRKCu0BT6Kd2fP4XAIgP2BbmXnbjM6EORHmsQHyZv+uat ESQAyNT3AbP3+R8DEPgx5WuiqSFyVN8NCfs/ap39UC1dOOjJLaMXTf+FzPrvVdgYRoff 6vY5fRgkYNPbvpHQqBK6cvTYL7oZDnK9XF0drIN5aKaTk5GNAv8+4iAgpIdYHeBbLV/7 xHzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=ngLIg9b4BlGvztL4m59Z6YNmmHxfTM8WHA3l9qn5cQw=; b=XQ6lSHklQxeNrHE/zbBo4wg8z1oFD3cuW61dde3SFUN4OzIF6QFk3CIYZirHqjcJb/ TxkI4SxS3OgMONecV/WPA0KreXul4C1PQKrI+8wsxP/5hTc4+GUWo4Zp/EJaJrkDsiHo EcytveD4rn9gw+o/appUgze+7sygt+thclZo5heHhUV3JZdByuSwSNh4GMt50v+4796/ E9XTu0nZaXfLkxC9B8J1oraRENQUCDJRfIXK40YOcD11lfm5bgd+Uqnv1sp+Ko1G6xRT 9hOQ1pLsgYfVvOf5PA5nDeCaYfiR0mEQDgd9qpuUoMchPGgDDeoz6Hbcyj/lVCYpEj8z O1mA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=p4kXZ57B; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h8-20020a656388000000b003816043eee3si21045176pgv.216.2022.03.24.00.32.00; Thu, 24 Mar 2022 00:32:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=p4kXZ57B; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239766AbiCVRag (ORCPT + 99 others); Tue, 22 Mar 2022 13:30:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35682 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239754AbiCVRad (ORCPT ); Tue, 22 Mar 2022 13:30:33 -0400 Received: from mail-yw1-x112f.google.com (mail-yw1-x112f.google.com [IPv6:2607:f8b0:4864:20::112f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4BB4FDFE2 for ; Tue, 22 Mar 2022 10:28:54 -0700 (PDT) Received: by mail-yw1-x112f.google.com with SMTP id 00721157ae682-2e6650cde1bso46919497b3.12 for ; Tue, 22 Mar 2022 10:28:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=ngLIg9b4BlGvztL4m59Z6YNmmHxfTM8WHA3l9qn5cQw=; b=p4kXZ57BGxTHMyT63eQqh+aVMHPNJP6siHAhD8Qz8wWtqDCYeluc9hvog8YHro/8nF Jto/x2Die2vbaHlQq8eUojlc7umSrjRAPBZEBhA7aUAlJ68M4L+HiWvlp24eF/oPv8c8 GrC1WdAIzQELqr8iVdZcLM8ECe/EiCJYlggXM/YXIdOh8abmnJD50NfLJCA/CloGeq5r Jme7fTDpENccLa6rMCqBBKoldhhifXKiZhb/Hfm6YlThLJzD9WtyF71tJKvXTHcShF3F vDHHPHJRUjq6OrEHzIJpJ38KhVqMBrFjSXLSPhHN+Svtqjm+ani6vVEnWtAlAabK2ZGA gxSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=ngLIg9b4BlGvztL4m59Z6YNmmHxfTM8WHA3l9qn5cQw=; b=ZZG6w6cwk5PxP2n0UQyqkVDBOEea/V7FgGGevXNL1f8q2GhaZOClzqmFjCGEVFW9VQ M/ZE6NyjLarKulLpfp8AaGyhqDXwf6aLwz+cmURbe8AMlcP0r77r6//acrziEsNs2ik0 6YBpo0XsAsI5nUmO/7W7YPvri7PMg6DrjDHwD7Fle5g+ghYkKMDpXHAXdcCZo3aKL4pl 5paOC1CQ7B30TwB4TyK9L7KTaJib8ARNFKySO3i4LncLEigG5kRZAzYt56mlUSGkcdtX azJ0LiaKLFKsXyDYy4QApcw4Q0KZA8ftj50/42Bt5zl9q4oz3Q1GbR7a1M0Vqrvkdfau 33OQ== X-Gm-Message-State: AOAM531zfkbsY+64gPpwJDru5Mf5z247dwF1oVr6c7I25i62n96qZQ8y dfSiyOb8GjYHvmpOPJgj3OZ5zyjPCk50BvmxuUWAcw== X-Received: by 2002:a0d:f485:0:b0:2e6:8c95:d874 with SMTP id d127-20020a0df485000000b002e68c95d874mr1924073ywf.23.1647970133194; Tue, 22 Mar 2022 10:28:53 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Erdem Aktas Date: Tue, 22 Mar 2022 10:28:42 -0700 Message-ID: Subject: Re: [RFC PATCH v5 064/104] KVM: TDX: Implement TDX vcpu enter/exit path To: "Yamahata, Isaku" Cc: "open list:KERNEL VIRTUAL MACHINE (KVM)" , open list , isaku.yamahata@gmail.com, Paolo Bonzini , Jim Mattson , Connor Kuehl , Sean Christopherson Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Mar 4, 2022 at 11:50 AM wrote: > > From: Isaku Yamahata > > This patch implements running TDX vcpu. Once vcpu runs on the logical > processor (LP), the TDX vcpu is associated with it. When the TDX vcpu > moves to another LP, the TDX vcpu needs to flush its status on the LP. > When destroying TDX vcpu, it needs to complete flush and flush cpu memory > cache. Track which LP the TDX vcpu run and flush it as necessary. > > Do nothing on sched_in event as TDX doesn't support pause loop. > > TDX vcpu execution requires restoring PMU debug store after returning back > to KVM because the TDX module unconditionally resets the value. To reuse > the existing code, export perf_restore_debug_store. > > Signed-off-by: Isaku Yamahata > --- > arch/x86/kvm/vmx/main.c | 10 +++++++++- > arch/x86/kvm/vmx/tdx.c | 34 ++++++++++++++++++++++++++++++++++ > arch/x86/kvm/vmx/tdx.h | 33 +++++++++++++++++++++++++++++++++ > arch/x86/kvm/vmx/x86_ops.h | 2 ++ > arch/x86/kvm/x86.c | 1 + > 5 files changed, 79 insertions(+), 1 deletion(-) > > diff --git a/arch/x86/kvm/vmx/main.c b/arch/x86/kvm/vmx/main.c > index f571b07c2aae..2e5a7a72d560 100644 > --- a/arch/x86/kvm/vmx/main.c > +++ b/arch/x86/kvm/vmx/main.c > @@ -89,6 +89,14 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) > return vmx_vcpu_reset(vcpu, init_event); > } > > +static fastpath_t vt_vcpu_run(struct kvm_vcpu *vcpu) > +{ > + if (is_td_vcpu(vcpu)) > + return tdx_vcpu_run(vcpu); > + > + return vmx_vcpu_run(vcpu); > +} > + > static void vt_flush_tlb_all(struct kvm_vcpu *vcpu) > { > if (is_td_vcpu(vcpu)) > @@ -200,7 +208,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { > .tlb_flush_guest = vt_flush_tlb_guest, > > .vcpu_pre_run = vmx_vcpu_pre_run, > - .run = vmx_vcpu_run, > + .run = vt_vcpu_run, > .handle_exit = vmx_handle_exit, > .skip_emulated_instruction = vmx_skip_emulated_instruction, > .update_emulated_instruction = vmx_update_emulated_instruction, > diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c > index 85d5f961d97e..ebe4f9bf19e7 100644 > --- a/arch/x86/kvm/vmx/tdx.c > +++ b/arch/x86/kvm/vmx/tdx.c > @@ -10,6 +10,9 @@ > #include "vmx.h" > #include "x86.h" > > +#include > +#include "trace.h" > + > #undef pr_fmt > #define pr_fmt(fmt) "tdx: " fmt > > @@ -509,6 +512,37 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) > vcpu->kvm->vm_bugged = true; > } > > +u64 __tdx_vcpu_run(hpa_t tdvpr, void *regs, u32 regs_mask); > + > +static noinstr void tdx_vcpu_enter_exit(struct kvm_vcpu *vcpu, > + struct vcpu_tdx *tdx) > +{ > + guest_enter_irqoff(); > + tdx->exit_reason.full = __tdx_vcpu_run(tdx->tdvpr.pa, vcpu->arch.regs, 0); > + guest_exit_irqoff(); > +} > + > +fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) > +{ > + struct vcpu_tdx *tdx = to_tdx(vcpu); > + > + if (unlikely(vcpu->kvm->vm_bugged)) { > + tdx->exit_reason.full = TDX_NON_RECOVERABLE_VCPU; > + return EXIT_FASTPATH_NONE; > + } > + > + trace_kvm_entry(vcpu); > + > + tdx_vcpu_enter_exit(vcpu, tdx); > + > + vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET; > + trace_kvm_exit(vcpu, KVM_ISA_VMX); > + > + if (tdx->exit_reason.error || tdx->exit_reason.non_recoverable) > + return EXIT_FASTPATH_NONE; Looks like the above if statement has no effect. Just checking if this is intentional. > + return EXIT_FASTPATH_NONE; > +} > + > void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) > { > td_vmcs_write64(to_tdx(vcpu), SHARED_EPT_POINTER, root_hpa & PAGE_MASK); > diff --git a/arch/x86/kvm/vmx/tdx.h b/arch/x86/kvm/vmx/tdx.h > index bf9865a88991..e950404ce5de 100644 > --- a/arch/x86/kvm/vmx/tdx.h > +++ b/arch/x86/kvm/vmx/tdx.h > @@ -44,12 +44,45 @@ struct kvm_tdx { > spinlock_t seamcall_lock; > }; > > +union tdx_exit_reason { > + struct { > + /* 31:0 mirror the VMX Exit Reason format */ > + u64 basic : 16; > + u64 reserved16 : 1; > + u64 reserved17 : 1; > + u64 reserved18 : 1; > + u64 reserved19 : 1; > + u64 reserved20 : 1; > + u64 reserved21 : 1; > + u64 reserved22 : 1; > + u64 reserved23 : 1; > + u64 reserved24 : 1; > + u64 reserved25 : 1; > + u64 bus_lock_detected : 1; > + u64 enclave_mode : 1; > + u64 smi_pending_mtf : 1; > + u64 smi_from_vmx_root : 1; > + u64 reserved30 : 1; > + u64 failed_vmentry : 1; > + > + /* 63:32 are TDX specific */ > + u64 details_l1 : 8; > + u64 class : 8; > + u64 reserved61_48 : 14; > + u64 non_recoverable : 1; > + u64 error : 1; > + }; > + u64 full; > +}; > + > struct vcpu_tdx { > struct kvm_vcpu vcpu; > > struct tdx_td_page tdvpr; > struct tdx_td_page *tdvpx; > > + union tdx_exit_reason exit_reason; > + > bool initialized; > }; > > diff --git a/arch/x86/kvm/vmx/x86_ops.h b/arch/x86/kvm/vmx/x86_ops.h > index 922a3799336e..44404dd25737 100644 > --- a/arch/x86/kvm/vmx/x86_ops.h > +++ b/arch/x86/kvm/vmx/x86_ops.h > @@ -140,6 +140,7 @@ void tdx_vm_free(struct kvm *kvm); > int tdx_vcpu_create(struct kvm_vcpu *vcpu); > void tdx_vcpu_free(struct kvm_vcpu *vcpu); > void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event); > +fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu); > > int tdx_vm_ioctl(struct kvm *kvm, void __user *argp); > int tdx_vcpu_ioctl(struct kvm_vcpu *vcpu, void __user *argp); > @@ -160,6 +161,7 @@ static inline void tdx_vm_free(struct kvm *kvm) {} > static inline int tdx_vcpu_create(struct kvm_vcpu *vcpu) { return -EOPNOTSUPP; } > static inline void tdx_vcpu_free(struct kvm_vcpu *vcpu) {} > static inline void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) {} > +static inline fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) { return EXIT_FASTPATH_NONE; } > > static inline int tdx_vm_ioctl(struct kvm *kvm, void __user *argp) { return -EOPNOTSUPP; } > static inline int tdx_vcpu_ioctl(struct kvm_vcpu *vcpu, void __user *argp) { return -EOPNOTSUPP; } > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index da411bcd8cbc..66400810d54f 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -300,6 +300,7 @@ const struct kvm_stats_header kvm_vcpu_stats_header = { > }; > > u64 __read_mostly host_xcr0; > +EXPORT_SYMBOL_GPL(host_xcr0); > u64 __read_mostly supported_xcr0; > EXPORT_SYMBOL_GPL(supported_xcr0); > > -- > 2.25.1 >