Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1820501rwb; Thu, 8 Dec 2022 16:02:56 -0800 (PST) X-Google-Smtp-Source: AA0mqf7tB80++OIhbEOVeryY6qud2dsCRLNrupgxOV0C668ieUWf4HYJqImk59//ccOPnSYzLUH+ X-Received: by 2002:a17:907:b60c:b0:7c0:54f2:af9d with SMTP id vl12-20020a170907b60c00b007c054f2af9dmr3640280ejc.37.1670544176435; Thu, 08 Dec 2022 16:02:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670544176; cv=none; d=google.com; s=arc-20160816; b=uiZH5hXIXAw7qtFQt2MbK6fqhaROScITIfwj7QbfSVsMrxIFCDmraqQzxfwDiqrSeQ mlesqkvdJ4h8ybo4mNMadhIcIyP2yMs1f47AxUHc5b92OI/1p1quDl6z14WHKqJfo0mW uysyDQCgoofIY22xNDpkPI9z5y4rSwVUfVXzijc2mI0iHrjoPugQLOZ6/fWgqBY/gsYE pSXjLzzIoDxZWts2oBXeFR3yOXgEV0DUgnP4cV9TjqRojfooaPoxXFudeSq4Wuo/VTDN RVu8uUfUs5hXtjaxLi34tavkK7hVyBb0FQYcCLEa0KHUmGxddvB1jWg8FQ8q8tB0AruJ 2Zmg== 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=wt/ERXBTYwlnonKM8PzD1fVmwMYGl+1O6fIBuS+zYYk=; b=oZXW3d6qx1wqKDuwD/g0a4nN/TQYtlgWpKqCggrCFCWIajZGcTZc9HtwIptfiKwAW4 I81reqB0QVmiFqDmQ/c/UKBFW5vI4qtFkXb07W4+U0veHtIzqhNc2FFkNdvqtixSZTao XYQ79Ji9COTmBRdT+gQzlQKYC11Jz2uqS1jHGPmxW4cGgl1ZcYjOV9AbJ+uClzOq8iiU +9a4CtReReVHMWqQMRQlPJDEpxgHA8jvz4W4FAc0XxK0Lu/fVokF3Uep+MhCPa4/Igj+ WFNptrwj+Q73d5UY0qdehVaFxZgndUGngscl2xtS14URmK9pcMkwoHpHhkdLFdQMGXiX yyCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="LyZ1e3/b"; 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 d2-20020a170906370200b007ae4c686a4dsi15647872ejc.649.2022.12.08.16.02.37; Thu, 08 Dec 2022 16:02:56 -0800 (PST) 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="LyZ1e3/b"; 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 S229723AbiLHXgs (ORCPT + 73 others); Thu, 8 Dec 2022 18:36:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230080AbiLHXgI (ORCPT ); Thu, 8 Dec 2022 18:36:08 -0500 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9677E6FF0F; Thu, 8 Dec 2022 15:36:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1670542562; x=1702078562; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=zWuGHJKvb2rhePR8KFWGDOd9HnNPDNaoiycd/Rf+gtg=; b=LyZ1e3/bN0YMeDdzoaIemiu61PXZQAQ+UECywFsJgp+ckjz1Oy1Fwhaa UUoZ6ixKHQKTnp4ngMk0Jpkrgo/+O54HKi8LI9ZFSdgazY5eZ/lm3SLy2 aCGLMWTzmOj7LdQBSQrweusLIb4oOvI+1icapYyuavoROnx5yJX4f+8Ly fn2n8v1Iszk8iYZtjTwOp5WXlzQ/y5lcvN++aAHz4m6Yja0WaMSamcIwF vq9ditkIlAQ50Nziwgne0Zjn4UGHMRzv9zKWP4ri9jrxhYeCdFUqL57QH JoRq1Pr38SmjkDhSGwhWiB+QyOGNZanx8wwaQo1H+ZJ8k5Rm1q3p+CDo4 Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10555"; a="403586561" X-IronPort-AV: E=Sophos;i="5.96,228,1665471600"; d="scan'208";a="403586561" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Dec 2022 15:36:00 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10555"; a="677951029" X-IronPort-AV: E=Sophos;i="5.96,228,1665471600"; d="scan'208";a="677951029" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Dec 2022 15:36:00 -0800 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 , Sean Christopherson , David Matlack Subject: [RFC PATCH v2 13/15] KVM: x86/tdp_mmu: TDX: Implement merge pages into a large page Date: Thu, 8 Dec 2022 15:35:48 -0800 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.4 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 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 Implement merge_private_stp callback. Signed-off-by: Isaku Yamahata --- arch/x86/kvm/vmx/tdx.c | 70 ++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx_arch.h | 1 + arch/x86/kvm/vmx/tdx_errno.h | 2 ++ arch/x86/kvm/vmx/tdx_ops.h | 6 ++++ 4 files changed, 79 insertions(+) diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index ce7026136334..f20e931cf983 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -1487,6 +1487,47 @@ static int tdx_sept_split_private_spt(struct kvm *kvm, gfn_t gfn, return 0; } +static int tdx_sept_merge_private_spt(struct kvm *kvm, gfn_t gfn, + enum pg_level level, void *private_spt) +{ + int tdx_level = pg_level_to_tdx_sept_level(level); + struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm); + struct tdx_module_output out; + gpa_t gpa = gfn_to_gpa(gfn); + u64 err; + + /* See comment in tdx_sept_set_private_spte() */ + err = tdh_mem_page_promote(kvm_tdx->tdr.pa, gpa, tdx_level, &out); + if (err == TDX_ERROR_SEPT_BUSY) + return -EAGAIN; + if (err == TDX_EPT_INVALID_PROMOTE_CONDITIONS) + /* + * Some pages are accepted, some pending. Need to wait for TD + * to accept all pages. Tell it the caller. + */ + return -EAGAIN; + if (KVM_BUG_ON(err, kvm)) { + pr_tdx_error(TDH_MEM_PAGE_PROMOTE, err, &out); + return -EIO; + } + WARN_ON_ONCE(out.rcx != __pa(private_spt)); + + /* + * TDH.MEM.PAGE.PROMOTE frees the Secure-EPT page for the lower level. + * Flush cache for reuse. + */ + do { + err = tdh_phymem_page_wbinvd(set_hkid_to_hpa(__pa(private_spt), + to_kvm_tdx(kvm)->hkid)); + } while (err == (TDX_OPERAND_BUSY | TDX_OPERAND_ID_RCX)); + if (WARN_ON_ONCE(err)) { + pr_tdx_error(TDH_PHYMEM_PAGE_WBINVD, err, NULL); + return -EIO; + } + + return 0; +} + static int tdx_sept_zap_private_spte(struct kvm *kvm, gfn_t gfn, enum pg_level level) { @@ -1556,6 +1597,33 @@ static void tdx_track(struct kvm_tdx *kvm_tdx) } +static int tdx_sept_unzap_private_spte(struct kvm *kvm, gfn_t gfn, + enum pg_level level) +{ + int tdx_level = pg_level_to_tdx_sept_level(level); + struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm); + gpa_t gpa = gfn_to_gpa(gfn); + struct tdx_module_output out; + u64 err; + + do { + err = tdh_mem_range_unblock(kvm_tdx->tdr.pa, gpa, tdx_level, &out); + + /* + * tdh_mem_range_block() is accompanied with tdx_track() via kvm + * remote tlb flush. Wait for the caller of + * tdh_mem_range_block() to complete TDX track. + */ + } while (err == (TDX_TLB_TRACKING_NOT_DONE | TDX_OPERAND_ID_SEPT)); + if (err == TDX_ERROR_SEPT_BUSY) + return -EAGAIN; + if (KVM_BUG_ON(err, kvm)) { + pr_tdx_error(TDH_MEM_RANGE_UNBLOCK, err, &out); + return -EIO; + } + return 0; +} + static int tdx_sept_free_private_spt(struct kvm *kvm, gfn_t gfn, enum pg_level level, void *private_spt) { @@ -2681,9 +2749,11 @@ int __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops) x86_ops->link_private_spt = tdx_sept_link_private_spt; x86_ops->free_private_spt = tdx_sept_free_private_spt; x86_ops->split_private_spt = tdx_sept_split_private_spt; + x86_ops->merge_private_spt = tdx_sept_merge_private_spt; x86_ops->set_private_spte = tdx_sept_set_private_spte; x86_ops->remove_private_spte = tdx_sept_remove_private_spte; x86_ops->zap_private_spte = tdx_sept_zap_private_spte; + x86_ops->unzap_private_spte = tdx_sept_unzap_private_spte; return 0; } diff --git a/arch/x86/kvm/vmx/tdx_arch.h b/arch/x86/kvm/vmx/tdx_arch.h index 508d9a1139ce..3a3c9c608bf0 100644 --- a/arch/x86/kvm/vmx/tdx_arch.h +++ b/arch/x86/kvm/vmx/tdx_arch.h @@ -29,6 +29,7 @@ #define TDH_MNG_KEY_FREEID 20 #define TDH_MNG_INIT 21 #define TDH_VP_INIT 22 +#define TDH_MEM_PAGE_PROMOTE 23 #define TDH_VP_RD 26 #define TDH_MNG_KEY_RECLAIMID 27 #define TDH_PHYMEM_PAGE_RECLAIM 28 diff --git a/arch/x86/kvm/vmx/tdx_errno.h b/arch/x86/kvm/vmx/tdx_errno.h index 389b1b53da25..74a5777c05f1 100644 --- a/arch/x86/kvm/vmx/tdx_errno.h +++ b/arch/x86/kvm/vmx/tdx_errno.h @@ -19,6 +19,8 @@ #define TDX_KEY_CONFIGURED 0x0000081500000000ULL #define TDX_NO_HKID_READY_TO_WBCACHE 0x0000082100000000ULL #define TDX_EPT_WALK_FAILED 0xC0000B0000000000ULL +#define TDX_TLB_TRACKING_NOT_DONE 0xC0000B0800000000ULL +#define TDX_EPT_INVALID_PROMOTE_CONDITIONS 0xC0000B0900000000ULL /* * TDG.VP.VMCALL Status Codes (returned in R10) diff --git a/arch/x86/kvm/vmx/tdx_ops.h b/arch/x86/kvm/vmx/tdx_ops.h index 60cbc7f94b18..5d2d0b1eed28 100644 --- a/arch/x86/kvm/vmx/tdx_ops.h +++ b/arch/x86/kvm/vmx/tdx_ops.h @@ -140,6 +140,12 @@ static inline u64 tdh_mem_page_demote(hpa_t tdr, gpa_t gpa, int level, hpa_t pag return seamcall_sept(TDH_MEM_PAGE_DEMOTE, gpa | level, tdr, page, 0, out); } +static inline u64 tdh_mem_page_promote(hpa_t tdr, gpa_t gpa, int level, + struct tdx_module_output *out) +{ + return seamcall_sept(TDH_MEM_PAGE_PROMOTE, gpa | level, tdr, 0, 0, out); +} + static inline u64 tdh_mr_extend(hpa_t tdr, gpa_t gpa, struct tdx_module_output *out) { -- 2.25.1