Received: by 2002:ab2:6991:0:b0:1f2:fff1:ace7 with SMTP id v17csp98496lqo; Wed, 27 Mar 2024 07:58:30 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXVkCKBbQnXhR8zqGJiTAaS3avMd8zjmNNE4vKEJKGiotZ4gA1QUoMD3cHb2y642hrjNfxjfBi7s6NblznfpARW3zC+/lyJF0ktwI4pfg== X-Google-Smtp-Source: AGHT+IE6D0dtQvH64OZ7jhDyBSb9/2VMoXzJiACVyWjJLm4Cwr3CfKqa4hwrTxQTkPVtIm2xf8A5 X-Received: by 2002:a05:6830:204b:b0:6e6:b2c4:3b9d with SMTP id f11-20020a056830204b00b006e6b2c43b9dmr175881otp.30.1711551510540; Wed, 27 Mar 2024 07:58:30 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711551510; cv=pass; d=google.com; s=arc-20160816; b=J30UPuvoke257xcvBO6RGqPpmx0KqvtyJDYzN3NYo2qMTTjwIbiegHJM7Yr2gSGeIs qaXcnsjhowhpb0+yinivM3YF7hCX/pF85selYJ4bEQfHJ0GXR4iDOBHRJrwdQsMV6Wdl Nu2nhpxoMElgtJizz45OwiBPNqQ5k5SCpKxlW+c4ryFo5LF19XiUDTamWAY0POJ3Wq/k hUhk5eS/FZFKejHy6S3dvcPf1JG2qQpsWo3oguoNFzzyCnKc8gZCsJCNCXNK+R9npReZ gzQG3hykuIueY1jVxLCDUSyAWpP2iTnb9bgcizEneZS1qrRQD+Ln7CoWJb0HdghxykQD 3z3A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to:subject :user-agent:mime-version:list-unsubscribe:list-subscribe:list-id :precedence:date:message-id:dkim-signature; bh=PYkamczjPY6qM1VDrasKMTrBKWkjavJzfx8H/SrwiQY=; fh=ror2ncHT9i4eu+m6N+NcHsVGHw1+DzHfnD1A+qpataU=; b=yrtmWXx1n+cLRJio3d3sY1QZ3Cmk0K0iMxKyramYJ773e44ZVhbOcpUl9/YGwdjEec Al4uKekA83z2kd0/B+pjA+nQDPH8qBEihEDn6m1KCHx1WPsoQd36D9Cn9Xqh+zpolHOb 4rv42k6GdQ61H3tllZTMC4FEOqnNh1+Wozee+QOnTNnL7kvxAiwc/DgLlZF7hb3+BFFs FZ+zgR179gh1xl1o2DKQseXkZN38pzeahxBNTAYRM1/I/pm/e5EOk7xhw9wPeirzTyEE HWr0yp8UETMAb/n//PBex822ohXGsRbt8WmdGy85uENmth9m4DOTNz8/vsVd2RqgAiQb XPBA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=WW8NhqAS; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-121402-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-121402-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id xz19-20020a05620a5e1300b0078a531ed264si7517099qkn.23.2024.03.27.07.58.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 07:58:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-121402-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=WW8NhqAS; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-121402-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-121402-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 347811C29A90 for ; Wed, 27 Mar 2024 14:58:30 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 97CBE131192; Wed, 27 Mar 2024 14:09:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="WW8NhqAS" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DED586FC2; Wed, 27 Mar 2024 14:09:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.8 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711548570; cv=none; b=t5wDrh6Mu2YsKairp3alk2BZRcKSfDCktuPG2/BFoxr2HEwNg8QqcOyk6AgbDYAFKP0ePZ5MwCrtoHG/6Yxfn2C2cyDTjk0J3a8DGnLY/ylD78QfAxwEfVFeCixPJ4XnmhJhbDMgt3Fos9pNoliOGRIHixvoMFbmco+n4v/ZgGw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711548570; c=relaxed/simple; bh=lFo6CayJr3bn7um+nsNwcJD9J6nuJeNUotXXO++8fAE=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=RDHh3gxgMPND8fuuhr0ImtAhFv/U0Erjocg8KLqzezNfp0upLnM2Vu2nVM0Xti9ch257qp9kXQLLuQG3YmANw8dwHphkTaQZzMdWndlDFbBVbU0nsADn45tBggYwYF2emxGHpKVyzjPGMW/wrWBGYizg2itrVxD4wOKjcyi1drk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=WW8NhqAS; arc=none smtp.client-ip=192.198.163.8 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711548569; x=1743084569; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=lFo6CayJr3bn7um+nsNwcJD9J6nuJeNUotXXO++8fAE=; b=WW8NhqAS9AwEHFLsidgVcuWjX9cX6Un3jtgSY1kUvY5DYdyYL+4VAbFS iB7zXljbsmB1MrLBEe82ZJNS9Krs/WBEpEmoGXSWirBIp91Po8s7d/Spg ByETN4veWyovOso40f2GriWWY4lVP9xLh1F+rJ06I+nBgvAZSbbtuzeYO Euz40tUxYO+lCuQwKAe4Lu/VBP7siY8mTZ5w8PmvqXSvwbiHj4tPOhimn 1lnl1YskNRCBH4+ra7budpWLQ1DPz6mI9irhKTsuWOoCmmuRgox83T2cu cGncSWoz47gsDuUR1tS29YL5TztDlkEU+2WYRJ66ewm57NkLTQlljXQLM g==; X-CSE-ConnectionGUID: cmU7qda0RN6+97kSML4HWA== X-CSE-MsgGUID: QaIjldYZS1aJEpFLLtW9rg== X-IronPort-AV: E=McAfee;i="6600,9927,11025"; a="24141735" X-IronPort-AV: E=Sophos;i="6.07,159,1708416000"; d="scan'208";a="24141735" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Mar 2024 07:09:28 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,159,1708416000"; d="scan'208";a="20949910" Received: from binbinwu-mobl.ccr.corp.intel.com (HELO [10.124.242.47]) ([10.124.242.47]) by ORVIESA003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Mar 2024 07:09:24 -0700 Message-ID: <6052c6cd-635e-483a-90e2-d66beb1bb91b@linux.intel.com> Date: Wed, 27 Mar 2024 22:09:21 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v19 046/130] KVM: x86/mmu: Add address conversion functions for TDX shared bit of GPA To: Chenyi Qiang , 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 , Kai Huang , chen.bo@intel.com, hang.yuan@intel.com, tina.zhang@intel.com, Rick Edgecombe References: <973a3e06111fe84f2b1e971636cbaa3facf7b120.1708933498.git.isaku.yamahata@intel.com> From: Binbin Wu In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 3/27/2024 11:08 AM, Chenyi Qiang wrote: > > On 2/26/2024 4:25 PM, 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. >> >> default or SEV-SNP TDX: S = (47 or 51) - 12 >> gfn_shared_mask 0 S bit >> kvm_is_private_gpa() always false true if GFN has S bit set > TDX: true if GFN has S bit clear? > >> kvm_gfn_to_shared() nop set S bit >> kvm_gfn_to_private() nop clear S bit >> >> fault.is_private means that host page should be gotten from guest_memfd >> is_private_gpa() means that KVM MMU should invoke private MMU hooks. >> >> Co-developed-by: Rick Edgecombe >> Signed-off-by: Rick Edgecombe >> Signed-off-by: Isaku Yamahata >> Reviewed-by: Binbin Wu >> --- >> v19: >> - Add comment on default vm case. >> - Added behavior table in the commit message >> - drop CONFIG_KVM_MMU_PRIVATE >> >> v18: >> - Added Reviewed-by Binbin >> >> Signed-off-by: Isaku Yamahata >> --- >> arch/x86/include/asm/kvm_host.h | 2 ++ >> arch/x86/kvm/mmu.h | 33 +++++++++++++++++++++++++++++++++ >> arch/x86/kvm/vmx/tdx.c | 5 +++++ >> 3 files changed, 40 insertions(+) >> >> diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h >> index 5da3c211955d..de6dd42d226f 100644 >> --- a/arch/x86/include/asm/kvm_host.h >> +++ b/arch/x86/include/asm/kvm_host.h >> @@ -1505,6 +1505,8 @@ struct kvm_arch { >> */ >> #define SPLIT_DESC_CACHE_MIN_NR_OBJECTS (SPTE_ENT_PER_PAGE + 1) >> struct kvm_mmu_memory_cache split_desc_cache; >> + >> + gfn_t gfn_shared_mask; >> }; >> >> struct kvm_vm_stat { >> diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h >> index d96c93a25b3b..395b55684cb9 100644 >> --- a/arch/x86/kvm/mmu.h >> +++ b/arch/x86/kvm/mmu.h >> @@ -322,4 +322,37 @@ static inline gpa_t kvm_translate_gpa(struct kvm_vcpu *vcpu, >> return gpa; >> return translate_nested_gpa(vcpu, gpa, access, exception); >> } >> + >> +/* >> + * default or SEV-SNP TDX: where S = (47 or 51) - 12 >> + * gfn_shared_mask 0 S bit >> + * is_private_gpa() always false if GPA has S bit set Also here, TDX: true if GFN has S bit cleared >> + * gfn_to_shared() nop set S bit >> + * gfn_to_private() nop clear S bit >> + * >> + * fault.is_private means that host page should be gotten from guest_memfd >> + * is_private_gpa() means that KVM MMU should invoke private MMU hooks. >> + */ >> +static inline gfn_t kvm_gfn_shared_mask(const struct kvm *kvm) >> +{ >> + return kvm->arch.gfn_shared_mask; >> +} >> + >> +static inline gfn_t kvm_gfn_to_shared(const struct kvm *kvm, gfn_t gfn) >> +{ >> + return gfn | kvm_gfn_shared_mask(kvm); >> +} >> + >> +static inline gfn_t kvm_gfn_to_private(const struct kvm *kvm, gfn_t gfn) >> +{ >> + return gfn & ~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 aa1da51b8af7..54e0d4efa2bd 100644 >> --- a/arch/x86/kvm/vmx/tdx.c >> +++ b/arch/x86/kvm/vmx/tdx.c >> @@ -906,6 +906,11 @@ static int tdx_td_init(struct kvm *kvm, struct kvm_tdx_cmd *cmd) >> kvm_tdx->attributes = td_params->attributes; >> kvm_tdx->xfam = td_params->xfam; >> >> + 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);