Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp4765941iob; Mon, 9 May 2022 00:42:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwdp3Nd0Q5ShzdsPm3ghXs4n8oJ62wrcjLvUl2ZO2eZX5uj3i+dCwnrvO/kSJfsgFwJG4YT X-Received: by 2002:a65:404c:0:b0:3c6:4018:ffbf with SMTP id h12-20020a65404c000000b003c64018ffbfmr12498911pgp.408.1652082161724; Mon, 09 May 2022 00:42:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652082161; cv=none; d=google.com; s=arc-20160816; b=C4K1fDccM6nJRC7VCSMtj6cnHyLOw7jHM2T3J64fzsVuoU+wlhlPcUJ0JkwbIVDZLR OGh4z70egU7x+9ltMFLm9z+L41iv9m0m41dtiUqYU2K5ilGycVf+LI3FERywISeLiob9 iKfIegsHnZJZaceauw4J+mULbmkV2VvQmtb93OU/IYuu7lIu+PfQiapv9kMRxBwUx8Z9 Jc5Ox8QhtYK/UdIVKyzk2m7Q1fI0dHrQihryCkraCXKLyjaMcsR63ZvMFAqgxiXC+3nY 0E0NMuPUDSKSOBAAY7QYzW/xMYU8IMxEwVkW7WP2RxD8uOWiUJzIObV8bSaFuVPw+gii ARSQ== 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=yIz4sH3PrOefTbRb5yft0M7xju3kcQUHzuozGXc6HGA=; b=nBHUK6SR2cTfTNbgDw9sOscR5C1GKDCrfVlY/9OGSjbGy1GFAbMxwal5O7e9MwksIa bL1gVJpVk2iYHAqAKGJ4EsZW2eTG5rJSkZ2ZZcYlbFjD26njbOpmX4lHhaLUNdJt/DXv A3vik6tdzaReY0TjEVoxyXF0FEcEF52HpRTcpdqVGX+Qw4VWNUL/YkfViiOLOoC8ZaL5 chS9U0y00U+KP3/D1Yaoe9waja+owzAAbXJ8g7iBq01Q/FFXZyP57206Q8ARLOjZbXiJ ghMwOgJBpHeKKnPj17JRTMjm9FTXP+Olj5qn7c33iv/8CZoDx5Y0/SxbA2LGoJ4hfHj/ 0VHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=eeGXkpLO; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id l185-20020a6388c2000000b0039da5aa4ed6si15258097pgd.710.2022.05.09.00.42.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 May 2022 00:42:41 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=eeGXkpLO; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D3C63AAE2C; Mon, 9 May 2022 00:36:11 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240965AbiEES0b (ORCPT + 99 others); Thu, 5 May 2022 14:26:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36324 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383229AbiEESTm (ORCPT ); Thu, 5 May 2022 14:19:42 -0400 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C5453562CD; Thu, 5 May 2022 11:15: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=1651774554; x=1683310554; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=r2/5ldw5pVUVlMl5ZifuzWjDJnhnIJbR7f7gWYsK9LU=; b=eeGXkpLOfpsQKkghs9OP1ULd2s3qnyTwynBe6Pgx2tfsxvKsq8UzzwZL q5WzZvfPZbpZ9k6DSu3zOir5UAsdj/RkKTacnsBrTKfVDqhRjnsddR8Rz vBmzigo1NoD87vfoVn1GfNcb+IcLmADUdg9c2jhjVb3u/JrsHxp7FMjmB r1i5oO3D4/z/x0/jlMgUESgPV4J28sADrhozoBNEGgNqDGIMOxgydj3Ez xwqaPcs0+6ekva0RSfl0hNRvHXVttukzageUtZU4dx9p4zcInoMNxD5UU 2KRPMD7auPv70FrGKxV1Iknf0fFtWpUrFq21ItoFsAHpzOdPWj36oGa40 A==; X-IronPort-AV: E=McAfee;i="6400,9594,10338"; a="268354849" X-IronPort-AV: E=Sophos;i="5.91,202,1647327600"; d="scan'208";a="268354849" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 May 2022 11:15:49 -0700 X-IronPort-AV: E=Sophos;i="5.91,202,1647327600"; d="scan'208";a="665083358" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 May 2022 11:15:49 -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 Subject: [RFC PATCH v6 059/104] KVM: x86/mmu: Introduce kvm_mmu_map_tdp_page() for use by TDX Date: Thu, 5 May 2022 11:14:53 -0700 Message-Id: <8ef4dd02cdbdbee00064c35c22b753ef32a20c90.1651774250.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=-2.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h index beff084d6cd3..6606f790ae0b 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -254,6 +254,9 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, return vcpu->arch.mmu->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); + /* * 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 497e2b9e58cc..643b33c75ae9 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4276,6 +4276,45 @@ 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(), + .is_private = kvm_is_private_gpa(vcpu->kvm, gpa), + }; + + 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