Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp558952rwb; Thu, 12 Jan 2023 09:18:19 -0800 (PST) X-Google-Smtp-Source: AMrXdXvhojoITDQg2QfKOU9QD+3mzhSQnsW4FO3ignabT9/2YUBk+LTHS3XsKQXYprQT4Dbm1VCY X-Received: by 2002:a17:906:6d0e:b0:7c1:8f78:9562 with SMTP id m14-20020a1709066d0e00b007c18f789562mr153918ejr.50.1673543899197; Thu, 12 Jan 2023 09:18:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673543899; cv=none; d=google.com; s=arc-20160816; b=b1qy2v2fKMwm6ElISHUy0+4vA6bc0VWovNAsUmY4eM1VgSzs/QT0mpiq+PCiVjkIom X44WKxFB8uUGBCSBSs2YRut+eAzhVCW6pTv2vrmliS+pdc5WES5Dul713nxOPsCv0VHE DfMcUETfPOC8HyVuon+SB+YfeYOWjwNoqd268GuYSERQsmui8t8sftYWCYYaZD9BpCBc CNFdhSzSlet06eBAPPZjDOCCmavv6q+WIGkuqwN2lvsPvPm7CDzdenYmgiuXXEKn6bd9 nqEQ5G1L91pTEStJKCe3sXYSKncnzHYMeZBjicIQS0kTUxKk2pTKN1a9Tj+c4fPiteyd zcMw== 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=s4XH6LlBkHcb26rAgeLaj0peiNjxcACb8qxCMtmN/Kw=; b=DhK8hv/g2ujY/PS1tHbhFtFeFcmgU1ld7/gqqQln1SnnCVDffCw7F+Q5+Iw1GP5H8i NHmiw6bo2TsXEScLpuhF8dhd7HbWHtsGM6IYZV1aZBILdcA42NohUNv3XfJ7lP4yE0mI Ay2lvrbHWjv5ZpC9fEKiFpy7FX2NTKZoZQ7WxtKB+vh2GTiPPg9pOJwmoRjKcknJlwQk 8M1uHhQYkLDlg8Bbi08VIpcSr3I6UO61VPVeSTdeBcNntRU50JwhM64I5+UoTDXFqu+7 r083l3FXivRTf2sEWsZJU94GaPoZAgZLi20ATVzay8mjIJfsK4482NT8Jl/vdx6i1FD7 PyIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=fbyfyPdU; 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 sa18-20020a1709076d1200b007ae4717bf08si21425790ejc.359.2023.01.12.09.18.02; Thu, 12 Jan 2023 09:18:19 -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=fbyfyPdU; 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 S240664AbjALQrr (ORCPT + 50 others); Thu, 12 Jan 2023 11:47:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240290AbjALQip (ORCPT ); Thu, 12 Jan 2023 11:38:45 -0500 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE2B51CB06; Thu, 12 Jan 2023 08:34:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673541246; x=1705077246; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=62mQ8fLHlhIAeSorZdWTNSoO6Sps+++OvegPqPcNnAo=; b=fbyfyPdUBuSGcew12cFmyjBYhyttWUjz/jtuyltIWovN6xuPRrFt3rnW YXLNd08Qz6tvoR9ioOK2O0OLc91YMNT7UfpTXcER/1xP0HYQnNr2x1iEm uZ/OVCyFhQqftkS3v86ZQPQ9LYBMk2E70U2wQOXCTsHavsWFRhg+JY4vj ohqKQlQsQYyvH2wU65K0T+JNaIQePA6fxckYycvvBfDz+HzvZmNzQMQ/6 4XiQ5qn7rdguet6nLZCCVHYYy7Kel055uiSIAQFKs6PdjFCph2epWhsqj 8CpO0kte2H3MzDrdj2ZbzZQB5lBaBtyU+DhiAfRSOOvgfyvg7kSOywyko A==; X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="323811946" X-IronPort-AV: E=Sophos;i="5.97,211,1669104000"; d="scan'208";a="323811946" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2023 08:33:30 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="721151843" X-IronPort-AV: E=Sophos;i="5.97,211,1669104000"; d="scan'208";a="721151843" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2023 08:33:30 -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 , Sean Christopherson Subject: [PATCH v11 060/113] KVM: x86/mmu: Introduce kvm_mmu_map_tdp_page() for use by TDX Date: Thu, 12 Jan 2023 08:32:08 -0800 Message-Id: <88705015ec8c4fbdc20d4a4518db8f8fb63ab109.1673539699.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: Sean Christopherson Introduce a helper to directly (pun intended) fault-in a TDP page without having to go through the full page fault path. This allows TDX to get the resulting pfn and also allows the RET_PF_* enums to stay in mmu.c where they belong. Signed-off-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- arch/x86/kvm/mmu.h | 3 +++ arch/x86/kvm/mmu/mmu.c | 44 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h index 50d240d52697..e2a0dfbee56d 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -154,6 +154,9 @@ static inline void kvm_mmu_load_pgd(struct kvm_vcpu *vcpu) vcpu->arch.mmu->root_role.level); } +kvm_pfn_t kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, + u32 error_code, int max_level); + /* * Check if a given access (described through the I/D, W/R and U/S bits of a * page fault error code pfec) causes a permission fault with the given PTE diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 4f3f4cdc67ed..4058545a4851 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4524,6 +4524,50 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) return direct_page_fault(vcpu, fault); } +kvm_pfn_t kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, + u32 error_code, int max_level) +{ + int r; + struct kvm_page_fault fault = (struct kvm_page_fault) { + .addr = gpa, + .error_code = error_code, + .exec = error_code & PFERR_FETCH_MASK, + .write = error_code & PFERR_WRITE_MASK, + .present = error_code & PFERR_PRESENT_MASK, + .rsvd = error_code & PFERR_RSVD_MASK, + .user = error_code & PFERR_USER_MASK, + .prefetch = false, + .is_tdp = true, + .nx_huge_page_workaround_enabled = is_nx_huge_page_enabled(vcpu->kvm), + .is_private = kvm_is_private_gpa(vcpu->kvm, gpa), + }; + + WARN_ON_ONCE(!vcpu->arch.mmu->root_role.direct); + fault.gfn = gpa_to_gfn(fault.addr) & ~kvm_gfn_shared_mask(vcpu->kvm); + fault.slot = kvm_vcpu_gfn_to_memslot(vcpu, fault.gfn); + + if (mmu_topup_memory_caches(vcpu, false)) + return KVM_PFN_ERR_FAULT; + + /* + * Loop on the page fault path to handle the case where an mmu_notifier + * invalidation triggers RET_PF_RETRY. In the normal page fault path, + * KVM needs to resume the guest in case the invalidation changed any + * of the page fault properties, i.e. the gpa or error code. For this + * path, the gpa and error code are fixed by the caller, and the caller + * expects failure if and only if the page fault can't be fixed. + */ + do { + fault.max_level = max_level; + fault.req_level = PG_LEVEL_4K; + fault.goal_level = PG_LEVEL_4K; + + r = direct_page_fault(vcpu, &fault); + } while (r == RET_PF_RETRY && !is_error_noslot_pfn(fault.pfn)); + return fault.pfn; +} +EXPORT_SYMBOL_GPL(kvm_mmu_map_tdp_page); + static void nonpaging_init_context(struct kvm_mmu *context) { context->page_fault = nonpaging_page_fault; -- 2.25.1