Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp3021643lqo; Tue, 14 May 2024 18:02:36 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW4BAcRhzgZGpjplNM4SgUn6ciqMy2e1Kb7pfbL6Xew3dFQJe472sRJIbxSWJo35uC6wpQ5YboZWwLnql+j82rwh02ezV0xb0ZPheWG8A== X-Google-Smtp-Source: AGHT+IGA5ei5KrAFi7qBpjwDzIMFZMSNseUb7KfltntTtFtoKKperc0X6KEd177HnPeg3LExpV23 X-Received: by 2002:a05:6358:4709:b0:17f:565c:8db2 with SMTP id e5c5f4694b2df-193bb612ba7mr1499415155d.12.1715734955986; Tue, 14 May 2024 18:02:35 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715734955; cv=pass; d=google.com; s=arc-20160816; b=yR/OG644rNR99PXG8mxXpWyjgRB9ofTbkb0TkbJIVPRU7UflWPHkwgHPAt3bxLP6P6 gMaiLx2+A3Kyq5rvczbtugX5wogmcpYEoEgGKMisnl0i+9tFkb/ibGFgPlARk32KIGJV 70Ap4SAq+nNKg47u5feZ8D+ItFZF2vW383Tpn41Uwnjh+9wxcbFpTr9eI72o9jqAPDUg Vn6GCccm8Yy8I1BjhuBWPCKjZjZvYWj4PROTO/joEVatDSCovjquZxbi03tv2Bpjqzah krw5N3CD5WPXTp9YLCr88k83Cxduxp9xkMLXLsw2LVcL/3bBNyxgVyYHgVCsRk42NVy+ Ednw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=MUEChDe8r8qcoQrL3jViX1am8U3Y1OdVypmFPe5LZeE=; fh=2qyTsUcbFUT0xMqvy+8kysdtakguE/Gq+nkNuNfVOtU=; b=PcI7uMY1V2TrRMR8qw3eotS8pBmeFprLN2+Rd36Bz1pTo7tAWIfu+KUXAeMBQjtV4u rC1F1p2eQUWxozmHF8ZVcQK13wvBa85VMOcpl1jQ1vZdJCJ71KVHfukdzGMqiEqOYNiC ycnSlJ227AB6ufjPu3fTk5LXoxL7OOQUZSu2GKN+5fH8oFtqg26XI7QwX2zwv+SlgYdp K+37UpEI6PLDOZI9notRzt9lbo9Jy6Zd4vhc+b2FgvzmLEd8G4dKGZdCN5oSVA3MXIu3 iCQzZq/RGOtRrN1tPNNigsVpPF76w3LSv3KdAAfkZaINGJDOZJTUt0MMs/qKh0emrsne h4LA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ELClGgbz; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-179293-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-179293-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id 41be03b00d2f7-63f14c541b1si6466039a12.289.2024.05.14.18.02.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 May 2024 18:02:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-179293-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ELClGgbz; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-179293-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-179293-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 3EA28B21AE0 for ; Wed, 15 May 2024 01:01:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8F36424A08; Wed, 15 May 2024 01:00:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="ELClGgbz" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.19]) (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 4EBEB79C8; Wed, 15 May 2024 01:00:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.19 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715734808; cv=none; b=DchnOauOSx5Oj/40XJ8HH6pC1+BECVDdfk9evILnk2WG91qIfJuTA+SOeaOP20iWsedf9YmwwwRLaHzeM1XSktdZZOSKH2tMffclelXsBLfBqnQLvdmTjsqG1UAUIBepmyynb1oHfHs2vfjaSUbveElaBYzrVYMyPt38R8AYSZA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715734808; c=relaxed/simple; bh=EiPyaz12Hs/yvu2vb2xAWA/RYdi5EWxAwUrWQYyN7dw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=omXYjLZAJZNkVKgFuc27xWr/qpePJh5RuQC+A5VCpKBUiyTLGGRf0aBAWJoPmkXx9JJ10zEJ7V6TZ8sI6AJ0Hz3mSnbg1rwsedW+zT1UcUMN6CVBh0kzfo0CgyzkAdcS8s1Ae1mDg4JP04fr8NeNEWxTMviU3tpEed3kwtOa+Jg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=ELClGgbz; arc=none smtp.client-ip=192.198.163.19 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1715734805; x=1747270805; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=EiPyaz12Hs/yvu2vb2xAWA/RYdi5EWxAwUrWQYyN7dw=; b=ELClGgbzEWPPrOQH8LYy5ogKNpc3jx+tSlMjye+JiqFExsvgHzzG5Ct9 qitUmcVoDVhkpetFmKXVQPz5vbo3K958kRmRMhGVT03kWxl2yOMXpRV1l 8LbUHuVOAbukSalUjQQG+pKGdz0qOk53LdC849VmgH4RwWTxnPM+c11ou m9S6XowJ3sIy4Kexg/CBE4bx12e/9BNqlro6U4IbhtA6j4mgHkZFGQ+PL wHHZ5P2WBZXY+5wXa8IAPV48xnOhAOvnlndsOA4s65ZWz6dVJMGh0TysD dVgIKg510yGYw1RLTnUyDBprb/sYn7JLzwlRuHSPAPF4+93uxjxdsxfBv A==; X-CSE-ConnectionGUID: JscFULhURNOu5LsnGHxd2A== X-CSE-MsgGUID: KLCznybqRSyZBVQ8OZh0XA== X-IronPort-AV: E=McAfee;i="6600,9927,11073"; a="11613944" X-IronPort-AV: E=Sophos;i="6.08,160,1712646000"; d="scan'208";a="11613944" Received: from fmviesa006.fm.intel.com ([10.60.135.146]) by fmvoesa113.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 May 2024 18:00:04 -0700 X-CSE-ConnectionGUID: 59cLFGsYRK+7uA0a878tEw== X-CSE-MsgGUID: G3nbzKu1SHaBw238yzeYqA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,160,1712646000"; d="scan'208";a="30942724" Received: from oyildiz-mobl1.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.209.51.34]) by fmviesa006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 May 2024 18:00:02 -0700 From: Rick Edgecombe To: pbonzini@redhat.com, seanjc@google.com, kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, isaku.yamahata@gmail.com, erdemaktas@google.com, sagis@google.com, yan.y.zhao@intel.com, dmatlack@google.com, rick.p.edgecombe@intel.com Subject: [PATCH 04/16] KVM: x86/mmu: Add address conversion functions for TDX shared bit of GPA Date: Tue, 14 May 2024 17:59:40 -0700 Message-Id: <20240515005952.3410568-5-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240515005952.3410568-1-rick.p.edgecombe@intel.com> References: <20240515005952.3410568-1-rick.p.edgecombe@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Isaku Yamahata Introduce a "gfn_shared_mask" field in the kvm_arch structure to record GPA shared bit and provide address conversion helpers for TDX shared bit of GPA. TDX designates a specific GPA bit as the shared bit, which can be either bit 51 or bit 47 based on configuration. This GPA shared bit indicates whether the corresponding physical page is shared (if shared bit set) or private (if shared bit cleared). - GPAs with shared bit set will be mapped by VMM into conventional EPT, which is pointed by shared EPTP in TDVMCS, resides in host VMM memory and is managed by VMM. - GPAs with shared bit cleared will be mapped by VMM firstly into a mirrored EPT, which resides in host VMM memory. Changes of the mirrored EPT are then propagated into a private EPT, which resides outside of host VMM memory and is managed by TDX module. Add the "gfn_shared_mask" field to the kvm_arch structure for each VM with a default value of 0. It will be set to the position of the GPA shared bit in GFN through TD specific initialization code. Provide helpers to utilize the gfn_shared_mask to determine whether a GPA is shared or private, retrieve the GPA shared bit value, and insert/strip shared bit to/from a GPA. Signed-off-by: Isaku Yamahata Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Reviewed-by: Binbin Wu --- TDX MMU Part 1: - Update commit log (Yan) - Fix documentation on kvm_is_private_gpa() (Binbin) v19: - Add comment on default vm case. - Added behavior table in the commit message - drop CONFIG_KVM_MMU_PRIVATE v18: - Added Reviewed-by Binbin --- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu.h | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index aabf1648a56a..d2f924f1d579 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1519,6 +1519,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 3c7a88400cbb..dac13a2d944f 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -321,4 +321,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 true if GPA has S bit clear + * 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 -- 2.34.1