Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp2624388rdg; Mon, 16 Oct 2023 09:42:29 -0700 (PDT) X-Google-Smtp-Source: AGHT+IECvxVv7APD743nw0FM8TYbPkzigiRNl0f+L/sKKq7TT3mcA2M2MBAVip5GaGCKAxMdloCh X-Received: by 2002:a05:6808:1406:b0:3ae:108c:57b3 with SMTP id w6-20020a056808140600b003ae108c57b3mr50168543oiv.39.1697474549135; Mon, 16 Oct 2023 09:42:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697474549; cv=none; d=google.com; s=arc-20160816; b=hLdyUOkQXs9OJSo/wznK3AvWO1R0KR4dqnj4I187VCgr5TAefF5dggloyOxFIBtLEP J1u54AJW/a0LehKOi2iHExf3dseBVyJvUqutkEuhPCuyk11plX/g6pxMPoOS8BLaCrFT KSn+s8H1+01Ic7BUllkzc2z5uFuu7kEbASLHJZkMBtKi61l1en2cqiqunGlFOmSzWUeC VR74a8Lb+UmeJIZPmOD7P21ysxk+5t1eFeXnu9iJvpMHGdbldM7FsdRPgNE4/gP8dPLI eRhWLxWUoMPYG5hLKLgx74ygEpOvFxgvVG5zOYmp2rHlRX3F7G/C2m/VbY7XGwUSMf+p gITg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=+CYA1Qr5yIa4a+639SEjrtShAyJ89a3mUA7kwjRVqnw=; fh=dY1uMhBhDhUa2lk5QaA8DGqqes+/EvB0wwcNJ3QzMYw=; b=KRTw19/2efY2uG8Lp1otXqVu/Kdme9vmoVGbr2/saaSHg+SMevYVHc1XyDh1gAeuqq Tq3MD3qYidXwmb0vQH6IA4FH/wV+bG14qEmUMsxG2R9lBmDAJ7RT6l31IMxWoC+gtueF hs1InO1zmTwTQmuR0XLYwOHNoocdpy76wnIztuWEIWql5tAM/GQ8lb3Xz8zqZ5Pmw2mq YdXqPqSbmqXMSJBfiqEybDXR1d4g6N1HQ9VWidDeTSwFkNCISVDkjIhdIpZfyvW/Y7bP J4MiFjXSuAFSRWzLopPkI7Mj7+vgQn/xhBaSG0zdIJ6d7YjY/+3JuGDh6goIYVJ1oKfc 6EAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=YB9RM6D1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id cb6-20020a056a02070600b005b4600b5738si4879652pgb.261.2023.10.16.09.42.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Oct 2023 09:42:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=YB9RM6D1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id D6F78807CF46; Mon, 16 Oct 2023 09:42:13 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234445AbjJPQkq (ORCPT + 99 others); Mon, 16 Oct 2023 12:40:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234265AbjJPQj2 (ORCPT ); Mon, 16 Oct 2023 12:39:28 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F391F769A; Mon, 16 Oct 2023 09:22:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1697473374; x=1729009374; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=yjhldKEQtesjhUjBNJ+oTjkD2MK7pTp/AR1/NzqWdyI=; b=YB9RM6D1RvWYUGJU/3jpXpZF5jPYE8LHGykxuSm9gn+m+W5GKW4AC1Qk 4d7As7Bq5S8GfzJrWqJqzxNyqLCrxadmwHHvNFzx704nP8NFiAFV+3FMF Ig8A62Q6IKLhZ4Xh4HuUbX+K618ywq3ll5Zg8DZdoB6dK5Vs2mSikFA/j 6YAvhzYzVqf7jcriIm5U7TICXuZxZDSVmG69V29KLZxW8LWoRe9iMP2hl wErV375DY2syEqFPSCzC3miQ0GKtVtbs2uTTGSEH963geyt/xA3bln3sW zwSBkRvvdD+X5181GAGlzmnJ73ssReg4hGxneQyTAQzaYHA2od3KnIlwl w==; X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="365826019" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="365826019" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2023 09:15:36 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="1087126092" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="1087126092" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2023 09:15:34 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , erdemaktas@google.com, Sean Christopherson , Sagi Shahar , David Matlack , Kai Huang , Zhi Wang , chen.bo@intel.com, hang.yuan@intel.com, tina.zhang@intel.com, Rick Edgecombe Subject: [PATCH v16 029/116] KVM: x86/mmu: Add address conversion functions for TDX shared bit of GPA Date: Mon, 16 Oct 2023 09:13:41 -0700 Message-Id: <007c6dfc17bb785ec874b94afa0db1aa4410b96b.1697471314.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Mon, 16 Oct 2023 09:42:14 -0700 (PDT) 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 --- arch/x86/include/asm/kvm_host.h | 4 ++++ arch/x86/kvm/mmu.h | 27 +++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.c | 5 +++++ 3 files changed, 36 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index a21c060ffc68..742e97f23573 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1467,6 +1467,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 253fb2093d5d..f5ba6cf589aa 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -304,4 +304,31 @@ 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_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 c1a8560981a3..fe793425d393 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -878,6 +878,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); -- 2.25.1