Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2689329rwd; Sun, 28 May 2023 22:16:28 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6fMf2V3P/3CLA6Js116fBJbdNDZ0fqzJa6D8hi2p1J5zWMLKOWVSAdj/jj2ytrRvcSCc6t X-Received: by 2002:a17:903:258b:b0:1b0:2e1e:ba12 with SMTP id jb11-20020a170903258b00b001b02e1eba12mr4829554plb.54.1685337388428; Sun, 28 May 2023 22:16:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685337388; cv=none; d=google.com; s=arc-20160816; b=uXvzfJm1j6muI8NG7ShQKSoZI8q0CUqY4Xp+yriwN1AgXnn6s8uULs/wQjDTQMKZo2 +UzkJsJZifpEgOOGbUqsVdCCZPDppZYiQiejTGTKz/CgAgzkE8UE5xV08lCWPonH3vnG Jv/17x8PbUS77sxA2cFRYsN0LU/eJ/5vNBD++AEP9NcPsRMLqLCy+RXXBefTfcV0iLUk PM5ul0i2H4FzdPIjOO/hlfsMYP5elxz737tEyEQAoe8zEDORUb6cJaQH/JbGL20Px7vo yPlCoiWGbEfdQdjlvJhlKqqErtvGmMjxIgmiKr7koGh4eUCAEu/3IlRBuDuLXc+Ub9MQ 07fw== 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=mZq72JeJpYP6MimkcBj2KjCJtjMuAWNlBGo7ckF4PpU=; b=LYuXB6dCfFbFy1SX2QwSRL5zw2labjCW+HogA29s1nexb/AqWJKiXxlV8/kxq1iuy4 DunoZuR/cWkyE8c1F5iDZkXEVurg27nwQjd8eLIYn7fJHoyvV6ZPIk90bJhqXOYVp8+/ LlcNqJ4La3uxw8oqtQ0Wwi2DRgXBlD1kMc2YWcGPOurOiE21dExxUrRaYf3d0VagpPKb Wo3UUJmSs7i618BKl7P9+K8XRmy3sgCHrYe6tb3yx80vCasKR4O6Khg/y/rKQonwlhh8 5c6Az4crTifjGVB6Xh/QjMO4fTVaZT8QSNbdQF+Ihf6sku8Ps161moJ8Yg50jkyptTz7 Ru/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ixCfhvw+; 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 g9-20020a170902868900b001ab29e16b3asi8413659plo.286.2023.05.28.22.16.16; Sun, 28 May 2023 22:16:28 -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=ixCfhvw+; 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 S231639AbjE2EXx (ORCPT + 99 others); Mon, 29 May 2023 00:23:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44660 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231753AbjE2EXX (ORCPT ); Mon, 29 May 2023 00:23:23 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 08CCFB1; Sun, 28 May 2023 21:21:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685334082; x=1716870082; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ztVERYVmfJhaWj8p6J4zmQ1B97tsm5NVmQxpQPxMn4M=; b=ixCfhvw++//sHFNizZpyQhxUtEJfLg1q0+n4zS6B4jmZrV8Rzt8cSm6d jUULmfiZQanv8JuaPDt80RFRfhy3AcwNLp2qD5TVRt5M8AKGa1YSBv0re J/ADi6oiPu7+eA0RTw8FiEm6B2I4GoP/7Xu+CG1KJClhXbuuN+3FJAVoS fzt2UK2GPN7yLKzwaLO/glm6+UnJ9XTEM7Xq1O5tZhCKapJFtTn+BKL9n LZ3Yk3RU2L9t6QJ+pbaVF/NJMk86EzfnWR52KZ+rF8bC6u5cBNhKplA/R vicAeVtwYloh+6eerVQF8SxRQOzU8ch+rjAPQQ7RLRHeCf1K2inOrSsYP w==; X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="418094392" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="418094392" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2023 21:21:02 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="683419379" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="683419379" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2023 21:21:01 -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, Rick Edgecombe Subject: [PATCH v14 028/113] KVM: x86/mmu: Add address conversion functions for TDX shared bit of GPA Date: Sun, 28 May 2023 21:19:10 -0700 Message-Id: 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=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,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 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 e13794608dcc..487381bdda23 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1445,6 +1445,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 92d5a1924fc1..cf9c112aec8e 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -302,4 +302,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 3118b4f52911..59b5eb8b41b4 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -820,6 +820,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