Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp3105465rwa; Mon, 22 Aug 2022 21:42:56 -0700 (PDT) X-Google-Smtp-Source: AA6agR4mi1soKQwbOJxckP4+5vLFvO+gCoX+3kZX11f4P5oHhbrlOcAAQCcOysWIcN+28qMaPUO9 X-Received: by 2002:a17:906:99c5:b0:73d:70c5:1a4f with SMTP id s5-20020a17090699c500b0073d70c51a4fmr7079923ejn.302.1661229775937; Mon, 22 Aug 2022 21:42:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661229775; cv=none; d=google.com; s=arc-20160816; b=m+WtcGso2EM6clRAnUnbA6OaQzFiTfX1uNwb7pkZd2BRQjeK8xK12OZWSY0+3XjKpM WvIuqp5+Xb1A8XXxZ3goMOcrtxWX9tViHPJHuQTA071zyhZ4dSVrt0xDxEA+vxXB7p7u JwoULbY1N6BQEUfvD0a+YozSW+whrU+I8jzKtFyAPk9lZSL4sODUBt36KywWIDBHKxiu IUhUCsCfHmpGAoeG/1yVd9LGGXV3fRisLAqgkxufCYGXCTMdcp8lhDaJOK0exHHlEtXY ycukezrmr4DBn8FuJvNelQIdHGmrMHkblBsTwkWu8yD8wmWzgAR4HJ0o1k2Po4H0auVk 1pPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:subject:user-agent:mime-version:date:message-id :dkim-signature; bh=L4cbao9/DDHCmJQOBQKZseDgAXA+zTqd9NFuQvGZ8ec=; b=1HyMWl5q/Beww88Eji+itsKgdxTl0b+GzFjQ8KlCHIfZ2arBAGeqzf6LpB91xS5Ai9 WChnjakIOan9K3yzJaQr5Uj5ZKZnBYhYleu5MuaL6cz840V7CzcwIeAb1+scLTKV/ETB NIUSQEKCGDmT3pgwj5XDwu81WxYqcsDouU4zdQU8nkD1Mcgwf0L9P3GUJuBg19sfbfIL ExIB7pNxmTVFd3CXCC+zOuDycfd/8E+jghTfZwVvmVXcRmdKSHc8N9Tpm3rkw1ofo3Px s4uksJ/y5BxZ652/6jyMTibHk8C7I3CX2EAi/4hEXYBkQ/+PFVbmFe3cAyEp7Ri2dUDZ lGNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=CFZXvAMI; 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=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 28-20020a508e1c000000b0043e64d81518si1081343edw.619.2022.08.22.21.42.27; Mon, 22 Aug 2022 21:42:55 -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=@intel.com header.s=Intel header.b=CFZXvAMI; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240139AbiHWDkA (ORCPT + 99 others); Mon, 22 Aug 2022 23:40:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234003AbiHWDj5 (ORCPT ); Mon, 22 Aug 2022 23:39:57 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 589E85AC72; Mon, 22 Aug 2022 20:39:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661225996; x=1692761996; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=yvzYmlV0h8A3b8MaJVDQI7eQJR88zPIb6BOXH99svto=; b=CFZXvAMIxMtgBVmyz4vss59MjqheiJWRQUDmQqYeC0/KdVjqfDFMim8a 22+e/BhgRsx88XQ0oguJbOw6wHQUbfy0osjfWZdNn50aCd32u8MUsY0HE +4wMIR2oil1iowLYzLtUWQEgda3EtDfoQ8PeYi84Fjz0tGaPG81Q+YtmB W8rwd073yBMyA7CpJpsJYjr3hYQmJLlxq8Xz+xXEnibbplO54X0AW47Ii 4Kf6qkJiuzdkVREUK4CONtZVQ/w+cm/FbsUyw2HkUEYAEuu1JgiOUcaMw 8XhfGFJnqyQE+CxxJh/kdrCxoGjdeKVo+XQNcOaoNa003y18At+ynsCPe A==; X-IronPort-AV: E=McAfee;i="6500,9779,10447"; a="280554364" X-IronPort-AV: E=Sophos;i="5.93,256,1654585200"; d="scan'208";a="280554364" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Aug 2022 20:39:55 -0700 X-IronPort-AV: E=Sophos;i="5.93,256,1654585200"; d="scan'208";a="669839016" Received: from binbinwu-mobl.ccr.corp.intel.com (HELO [10.238.0.236]) ([10.238.0.236]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Aug 2022 20:39:53 -0700 Message-ID: <651c33a5-4b9b-927f-cb04-ec20b8c3d730@linux.intel.com> Date: Tue, 23 Aug 2022 11:39:51 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.1.2 Subject: Re: [PATCH v8 018/103] KVM: TDX: Stub in tdx.h with structs, accessors, and VMCS helpers To: isaku.yamahata@intel.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@gmail.com, Paolo Bonzini , erdemaktas@google.com, Sean Christopherson , Sagi Shahar References: From: Binbin Wu In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE 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 2022/8/8 6:01, isaku.yamahata@intel.com wrote: > From: Sean Christopherson > > Stub in kvm_tdx, vcpu_tdx, and their various accessors. TDX defines > SEAMCALL APIs to access TDX control structures corresponding to the VMX > VMCS. Introduce helper accessors to hide its SEAMCALL ABI details. > > Signed-off-by: Sean Christopherson > Signed-off-by: Isaku Yamahata > --- > arch/x86/kvm/vmx/tdx.h | 103 ++++++++++++++++++++++++++++++++++++++++- > 1 file changed, 101 insertions(+), 2 deletions(-) > > diff --git a/arch/x86/kvm/vmx/tdx.h b/arch/x86/kvm/vmx/tdx.h > index 2f43db5bbefb..f50d37f3fc9c 100644 > --- a/arch/x86/kvm/vmx/tdx.h > +++ b/arch/x86/kvm/vmx/tdx.h > @@ -3,16 +3,29 @@ > #define __KVM_X86_TDX_H > > #ifdef CONFIG_INTEL_TDX_HOST > + > +#include "tdx_ops.h" > + > int tdx_module_setup(void); > > +struct tdx_td_page { > + unsigned long va; > + hpa_t pa; > + bool added; > +}; > + > struct kvm_tdx { > struct kvm kvm; > - /* TDX specific members follow. */ > + > + struct tdx_td_page tdr; > + struct tdx_td_page *tdcs; > }; > > struct vcpu_tdx { > struct kvm_vcpu vcpu; > - /* TDX specific members follow. */ > + > + struct tdx_td_page tdvpr; > + struct tdx_td_page *tdvpx; > }; > > static inline bool is_td(struct kvm *kvm) > @@ -34,6 +47,92 @@ static inline struct vcpu_tdx *to_tdx(struct kvm_vcpu *vcpu) > { > return container_of(vcpu, struct vcpu_tdx, vcpu); > } > + > +static __always_inline void tdvps_vmcs_check(u32 field, u8 bits) > +{ > + BUILD_BUG_ON_MSG(__builtin_constant_p(field) && (field) & 0x1, > + "Read/Write to TD VMCS *_HIGH fields not supported"); > + > + BUILD_BUG_ON(bits != 16 && bits != 32 && bits != 64); > + > + BUILD_BUG_ON_MSG(bits != 64 && __builtin_constant_p(field) && > + (((field) & 0x6000) == 0x2000 || > + ((field) & 0x6000) == 0x6000), > + "Invalid TD VMCS access for 64-bit field"); if bits is 64 here, "bits != 64" is false, how could this check for "Invalid TD VMCS access for 64-bit field"? > + BUILD_BUG_ON_MSG(bits != 32 && __builtin_constant_p(field) && > + ((field) & 0x6000) == 0x4000, > + "Invalid TD VMCS access for 32-bit field"); ditto > + BUILD_BUG_ON_MSG(bits != 16 && __builtin_constant_p(field) && > + ((field) & 0x6000) == 0x0000, > + "Invalid TD VMCS access for 16-bit field"); ditto > +} > + > +static __always_inline void tdvps_state_non_arch_check(u64 field, u8 bits) {} > +static __always_inline void tdvps_management_check(u64 field, u8 bits) {} > + > +#define TDX_BUILD_TDVPS_ACCESSORS(bits, uclass, lclass) \ > +static __always_inline u##bits td_##lclass##_read##bits(struct vcpu_tdx *tdx, \ > + u32 field) \ > +{ \ > + struct tdx_module_output out; \ > + u64 err; \ > + \ > + tdvps_##lclass##_check(field, bits); \ > + err = tdh_vp_rd(tdx->tdvpr.pa, TDVPS_##uclass(field), &out); \ > + if (unlikely(err)) { \ > + pr_err("TDH_VP_RD["#uclass".0x%x] failed: 0x%llx\n", \ > + field, err); \ > + return 0; \ > + } \ > + return (u##bits)out.r8; \ > +} \ > +static __always_inline void td_##lclass##_write##bits(struct vcpu_tdx *tdx, \ > + u32 field, u##bits val) \ > +{ \ > + struct tdx_module_output out; \ > + u64 err; \ > + \ > + tdvps_##lclass##_check(field, bits); \ > + err = tdh_vp_wr(tdx->tdvpr.pa, TDVPS_##uclass(field), val, \ > + GENMASK_ULL(bits - 1, 0), &out); \ > + if (unlikely(err)) \ > + pr_err("TDH_VP_WR["#uclass".0x%x] = 0x%llx failed: 0x%llx\n", \ > + field, (u64)val, err); \ > +} \ > +static __always_inline void td_##lclass##_setbit##bits(struct vcpu_tdx *tdx, \ > + u32 field, u64 bit) \ > +{ \ > + struct tdx_module_output out; \ > + u64 err; \ > + \ > + tdvps_##lclass##_check(field, bits); \ > + err = tdh_vp_wr(tdx->tdvpr.pa, TDVPS_##uclass(field), bit, bit, \ > + &out); \ > + if (unlikely(err)) \ > + pr_err("TDH_VP_WR["#uclass".0x%x] |= 0x%llx failed: 0x%llx\n", \ > + field, bit, err); \ > +} \ > +static __always_inline void td_##lclass##_clearbit##bits(struct vcpu_tdx *tdx, \ > + u32 field, u64 bit) \ > +{ \ > + struct tdx_module_output out; \ > + u64 err; \ > + \ > + tdvps_##lclass##_check(field, bits); \ > + err = tdh_vp_wr(tdx->tdvpr.pa, TDVPS_##uclass(field), 0, bit, \ > + &out); \ > + if (unlikely(err)) \ > + pr_err("TDH_VP_WR["#uclass".0x%x] &= ~0x%llx failed: 0x%llx\n", \ > + field, bit, err); \ > +} > + > +TDX_BUILD_TDVPS_ACCESSORS(16, VMCS, vmcs); > +TDX_BUILD_TDVPS_ACCESSORS(32, VMCS, vmcs); > +TDX_BUILD_TDVPS_ACCESSORS(64, VMCS, vmcs); > + > +TDX_BUILD_TDVPS_ACCESSORS(64, STATE_NON_ARCH, state_non_arch); > +TDX_BUILD_TDVPS_ACCESSORS(8, MANAGEMENT, management); > + > #else > static inline int tdx_module_setup(void) { return -ENODEV; }; >