Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp564719rwb; Thu, 12 Jan 2023 09:22:23 -0800 (PST) X-Google-Smtp-Source: AMrXdXv5EripDT8WirWZ0cjfBsOQqcImAgfSsyfIpEG+nyC50PzvpMR41SsV0IccXPyXyBAZuzdu X-Received: by 2002:a17:907:7da4:b0:78d:f455:b5dc with SMTP id oz36-20020a1709077da400b0078df455b5dcmr81989029ejc.28.1673544143009; Thu, 12 Jan 2023 09:22:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673544142; cv=none; d=google.com; s=arc-20160816; b=Bh46xGVcj+CDe64sEsGOOR/ZSw2F89i8q0u531+5RpFX3cjUwWYTwcjJHG3qEEWgiT HbNnV00CHXqfko8XG2XNA2u0hR3dpO7bYqXOpeq+pa9HQ3DBjkP+b2Ipv53wqmyMxzyi 6iiPuASg2huEHLVjtn/LGk34o/WqxXwaOqms5lU2893AT0lzRXbWY3KGk0YH16+P5aMf 2ujbXViheXFfmxiZ088WI9P30SpiU80MEgXi0SZlH/IOPObr3uOhogWNhx5BNAQmOVO/ LEzqLirpRLg8tyVFGdURaZ9dHLp0HP/+Bcte2uD26xyh86KhZFjrVhX/WZER3wsneIig kSLA== 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=pZGs3wzJ4n1Lf5ic7Dsu70JT0l4TbGJPDQhBc56Lnr0=; b=0iPi/epwEwA8dGijq6F9e2x1oQCmHGH17gn+tCFRp0rRwtrgQHULkU97hF5p8TC0t9 GDjprE7Lh3t5883DT2S/HgY91W6AGmnX5qd7Jf4NHGM56X4ZIREPJ4aWfx6wZphxs85l pestRhgnCCIhDfrD2/j121uPTQYnVMQ+6v19hjUc+xLOmP2qnRCcTNG1+OtSX60a2q+N oga8HaH/J6C1MbpBF89lHtE/bsbGXX2snf7KVK0upEX1+6SpVcbPK+ZikeYAIjHBbkKV BDH32olnUwJhj4KTWkoRqMLXE1Iu/TsO7VYe3Ga2oZxk4LOB48F1FPSEu2gmJ7QZVLU0 65dg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=b7MVAN0f; 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 gb12-20020a170907960c00b007c1633cea0csi19921464ejc.513.2023.01.12.09.22.10; Thu, 12 Jan 2023 09:22:22 -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=b7MVAN0f; 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 S240335AbjALRP5 (ORCPT + 50 others); Thu, 12 Jan 2023 12:15:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42014 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240631AbjALRO5 (ORCPT ); Thu, 12 Jan 2023 12:14:57 -0500 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 48736809A5; Thu, 12 Jan 2023 08:49:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673542168; x=1705078168; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=k4iD1TNlKoUYX44DYqXUqzhco4y+uWUH8JhSCeeuIUE=; b=b7MVAN0fyOZWWAx9jc0LMRNXBhvw4qx6iUTosbxS4aMR6CDGbPwEXI1U hZZT6Cb8IK4xabXgyhpHOAqx5fCWm/3IMJS8rbLL9rfGW70fo8dt2wxJB aBmAiO2gTO9PtAxYI6SeoOe9cJcV1zpj2C3GG0fuFqJ/c9NpftXZ1gbOX 6L5GliQC/licRJYJclsms8LNvPR1I1IPilq0OvcueF592PqcfSgpHDdqW lzeE5nFaT0D1yBimM/V8l33gDyd6mzLQ2jClR3byvkKqI48Q1DlhM00zT dtPwTf+lc2lbkH4u55IlM52pSf+JXuLm/sRyljadHvv0GQB1uwc9RfFaB Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="323816340" X-IronPort-AV: E=Sophos;i="5.97,211,1669104000"; d="scan'208";a="323816340" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2023 08:44:18 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="986658364" X-IronPort-AV: E=Sophos;i="5.97,211,1669104000"; d="scan'208";a="986658364" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2023 08:44:18 -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 , erdemaktas@google.com, Sean Christopherson , Sagi Shahar , David Matlack Subject: [RFC PATCH v3 14/16] KVM: x86/tdp_mmu: TDX: Implement merge pages into a large page Date: Thu, 12 Jan 2023 08:44:06 -0800 Message-Id: <149d3cb8ff72b1a87c37b0356a5729ec4900b9dd.1673541292.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=-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 3fb7eb0df3aa..4ed76ef46b0d 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -1515,6 +1515,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) { @@ -1584,6 +1625,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) { @@ -2746,9 +2814,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