Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp181064rwe; Wed, 31 Aug 2022 00:20:49 -0700 (PDT) X-Google-Smtp-Source: AA6agR69rAJ3jySjVVTkWR87sOKZR5D1bVYLfsh267qojUHwIRCNu05IIeRf7Etec/yAOHZ4a5Xl X-Received: by 2002:a17:906:7307:b0:73c:c9ee:8b5c with SMTP id di7-20020a170906730700b0073cc9ee8b5cmr19083555ejc.310.1661930448941; Wed, 31 Aug 2022 00:20:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661930448; cv=none; d=google.com; s=arc-20160816; b=seMpO0MbzbtM7SQjCGFyfOiZyU7cY2Ub4nj3NwolxBywB6MenjJAbaguFGECrRSyIk waJ0jWMGsk7Ron2sOgv5seQI9hzfo/MEqWQZvRZJTxQFb1+tAqHPT66kMkAEFeBkj/f/ rsO6h5JslgepS95AtRcwzv7iY4V0PLjqP6iM3hBQ9ZOdDL40XDoBmMJpASNbAgDUXKGa 1IjHZpmxoaF1A2DT1omS6UBoc4NVYGbat0vAXVrp8nR3T+ICISZuw1Vm1/Ghn9KePLHz NCJezy9+kiZ9GJ5vEhD8nB0//6CR0hKF0ms1NF9T6LRCTh6CQiRaGS829c5qjIdYPog2 l29w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=Mi9ZaY32GF9XdHYVbkEYCGnRC1c5SnmAPxJMbTGL2hE=; b=rqi7ZVaF0XiPgHSV82ewo9sqFSi0uXwIv3U/uCkli+VadtupcVSnHPRPw/R9BnuR+e KkqP6OP0DGZUpyI9YBq8KZh41HAu51cmPespnMoxZwg/c+A6WZ52S10Ect3gNCrG25H0 E9Xn8soVVa05Lv9XLWGOpWc7ihR1u5v5nQjSJEA3vik3yYtge/2ZZbmyBo832Q/BWj3V Od1NajT7dbVc95jxEQi+lPlmYITyuhP43OzwhrsOtDv4cZOXyZ2zsd+TYBDiuiTVv3fc 2D+GJKaJMTcKGyikPFRgdCFd8ZI1Przjq3MpVhY+fU0pzNFOQn7K8oe2Esyb6ipGYwLE VxdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Zbbz03rN; 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 e7-20020a50d4c7000000b00445d6ec34d2si9861012edj.303.2022.08.31.00.20.23; Wed, 31 Aug 2022 00:20:48 -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=Zbbz03rN; 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 S230402AbiHaHHR (ORCPT + 99 others); Wed, 31 Aug 2022 03:07:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48732 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229810AbiHaHHP (ORCPT ); Wed, 31 Aug 2022 03:07:15 -0400 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BFE695E33D; Wed, 31 Aug 2022 00:07:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661929634; x=1693465634; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=kVV9HRdEeMuPwKG8bSE257Bhd6inSuO7fo2Ew7TH9RU=; b=Zbbz03rNP1dOzVxAMyr3MejPH9D2wose/Hv1qBjz9CSNuHlGLYTLBvi5 YOvE6y7VCf6h/irhHs8kw+ZXQwqQSbMWoJ8YuDPNdmuZpgcjxccDT99am IuxKxDeT1Gcqaa1EAEdMbwZnnJgeQ54EpZjjmk0gCJ7/OAoPL4B5ita4U F6pqqbfFq85p7ahWOELgDOjClmgyHdV2cJi2ePXRzmwTe+Rbob6Q3ujgB OF93vs2tAEZlJL4e7/jufiVOtj/4KrdbaT/OU9VGli0pjbM/No6JxYgz3 D30rChVrGJfYcaEBDDaI1a3GxuUxbirp3EUbii5Z9jxG+VWLzneqnhiyL g==; X-IronPort-AV: E=McAfee;i="6500,9779,10455"; a="294129498" X-IronPort-AV: E=Sophos;i="5.93,277,1654585200"; d="scan'208";a="294129498" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Aug 2022 00:07:14 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,277,1654585200"; d="scan'208";a="641739280" Received: from yy-desk-7060.sh.intel.com (HELO localhost) ([10.239.159.76]) by orsmga008.jf.intel.com with ESMTP; 31 Aug 2022 00:07:12 -0700 Date: Wed, 31 Aug 2022 15:07:11 +0800 From: Yuan Yao To: isaku.yamahata@intel.com Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, isaku.yamahata@gmail.com, Paolo Bonzini , erdemaktas@google.com, Sean Christopherson , Sagi Shahar Subject: Re: [PATCH v8 030/103] KVM: x86/mmu: Add address conversion functions for TDX shared bit of GPA Message-ID: <20220831070711.yli6s6yk7euyvgqu@yy-desk-7060> References: <97e6f89f0460ac0b29392528e848cca2458b54c9.1659854790.git.isaku.yamahata@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <97e6f89f0460ac0b29392528e848cca2458b54c9.1659854790.git.isaku.yamahata@intel.com> User-Agent: NeoMutt/20171215 X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,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 Sun, Aug 07, 2022 at 03:01:15PM -0700, isaku.yamahata@intel.com wrote: > From: Isaku Yamahata > > TDX repurposes one GPA bit (51 bit or 47 bit based on configuration) to > indicate the GPA is private(if cleared) or shared (if set) with VMM. If > GPA.shared is set, GPA is covered by the existing conventional EPT pointed > by EPTP. If GPA.shared bit is cleared, GPA is covered by TDX module. > VMM has to issue SEAMCALLs to operate. > > Add a member to remember GPA shared bit for each guest TDs, add address > conversion functions between private GPA and shared GPA and test if GPA > is private. > > Because struct kvm_arch (or struct kvm which includes struct kvm_arch. See > kvm_arch_alloc_vm() that passes __GPF_ZERO) is zero-cleared when allocated, > the new member to remember GPA shared bit is guaranteed to be zero with > this patch unless it's initialized explicitly. > > Co-developed-by: Rick Edgecombe > Signed-off-by: Rick Edgecombe > Signed-off-by: Isaku Yamahata Reviewed-by: Yuan Yao > --- > arch/x86/include/asm/kvm_host.h | 4 ++++ > arch/x86/kvm/mmu.h | 32 ++++++++++++++++++++++++++++++++ > arch/x86/kvm/vmx/tdx.c | 5 +++++ > 3 files changed, 41 insertions(+) > > diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h > index e856abbe80ab..6787d5214fd8 100644 > --- a/arch/x86/include/asm/kvm_host.h > +++ b/arch/x86/include/asm/kvm_host.h > @@ -1358,6 +1358,10 @@ struct kvm_arch { > */ > #define SPLIT_DESC_CACHE_MIN_NR_OBJECTS (SPTE_ENT_PER_PAGE + 1) > struct kvm_mmu_memory_cache split_desc_cache; > + > +#ifdef CONFIG_KVM_MMU_PRIVATE > + gfn_t gfn_shared_mask; > +#endif > }; > > struct kvm_vm_stat { > diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h > index a99acec925eb..df9f79ee07d4 100644 > --- a/arch/x86/kvm/mmu.h > +++ b/arch/x86/kvm/mmu.h > @@ -276,4 +276,36 @@ static inline gpa_t kvm_translate_gpa(struct kvm_vcpu *vcpu, > return gpa; > return translate_nested_gpa(vcpu, gpa, access, exception); > } > + > +static inline gfn_t kvm_gfn_shared_mask(const struct kvm *kvm) > +{ > +#ifdef CONFIG_KVM_MMU_PRIVATE > + return kvm->arch.gfn_shared_mask; > +#else > + return 0; > +#endif > +} > + > +static inline gfn_t kvm_gfn_shared(const struct kvm *kvm, gfn_t gfn) > +{ > + return gfn | kvm_gfn_shared_mask(kvm); > +} > + > +static inline gfn_t kvm_gfn_private(const struct kvm *kvm, gfn_t gfn) > +{ > + return gfn & ~kvm_gfn_shared_mask(kvm); > +} > + > +static inline gpa_t kvm_gpa_private(const struct kvm *kvm, gpa_t gpa) > +{ > + return gpa & ~gfn_to_gpa(kvm_gfn_shared_mask(kvm)); > +} > + > +static inline bool kvm_is_private_gpa(const struct kvm *kvm, gpa_t gpa) > +{ > + gfn_t mask = kvm_gfn_shared_mask(kvm); > + > + return mask && !(gpa_to_gfn(gpa) & mask); > +} > + > #endif > diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c > index 37272fe1e69f..36d2127cb7b7 100644 > --- a/arch/x86/kvm/vmx/tdx.c > +++ b/arch/x86/kvm/vmx/tdx.c > @@ -753,6 +753,11 @@ static int tdx_td_init(struct kvm *kvm, struct kvm_tdx_cmd *cmd) > kvm_tdx->xfam = td_params->xfam; > kvm->max_vcpus = td_params->max_vcpus; > > + if (td_params->exec_controls & TDX_EXEC_CONTROL_MAX_GPAW) > + kvm->arch.gfn_shared_mask = gpa_to_gfn(BIT_ULL(51)); > + else > + kvm->arch.gfn_shared_mask = gpa_to_gfn(BIT_ULL(47)); > + > out: > /* kfree() accepts NULL. */ > kfree(init_vm); > -- > 2.25.1 >