Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp3448285rwb; Fri, 30 Sep 2022 03:53:00 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6lcO+jE528GrqGKQ+bmUwG9TprdPZqfifgK6coFhQw+uhmNOitWS+98+S7Tb8rlzjGpdQu X-Received: by 2002:a17:90b:4c43:b0:203:1ba4:f29a with SMTP id np3-20020a17090b4c4300b002031ba4f29amr9159799pjb.76.1664535179720; Fri, 30 Sep 2022 03:52:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664535179; cv=none; d=google.com; s=arc-20160816; b=ck/7wXnzcjLWQnowWzyaHpFB75FSfGs3+uCBgp89ZpctYdVAps+nq926qUB7FmA1Z5 eEi+VGa4opeg1pWVGBD7Mselwea0XzPPvN7+SctinhbpQkTB+sVysy8kIppDDGkfkQWQ +/WiLdNENJ0IW2yurCV2MBZuk9bqrE0bMcs8KtE/0WvEOIIj5wV63xrTk0QkATsqOvUl gjf6fhgi4Hvc3Cmfrk3H79fjUjb50pgL9PN8LGbV5Eb9buRQLmLu83dNBQRVoNq+q7Np Nd3Yi4dKgxkni1fWrqU+E1kpqhWAv0RVe0cR/XUOWuS1LR4LAKhrPSkOIhIYd+wTXVYO RgRQ== 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=MMsGruq3VRmjA6LX8iC84xYw0uuDsgBxJbxY5tvrw2s=; b=YW531fUh0ThXZdvMq/XgvAGCjMeA5wIbjLjLmAlLOB0nNOgyOdLbSptnYyibK/Uy7x hSY/5zjGZMlRYm5sr1whj6v7pTX6r24h6BWmvhr58+YNi2EIBB6dIYXohfxHEiyP9Swj F/B/dKig/KJ3YmmNXRhj1ar7nSC/+NInin209IKWfpn0dlAwxqTtRH3EMKlRpglgkm28 u5EjQE0GB9nxv+mfnUDoDOvMOOyMnHfApCWNcXrsJx+OborMnVn7bWJ/xZNX7Nb1mt// B8euGi2N52AL+cW8iQItyLk+k9idIpp0ITa8aD9cWhr0j0aByto3ukVZ2WyKjL1y5LsZ 5Vjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Czatodoi; 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 z18-20020a170903019200b00176939b5cd9si2972145plg.578.2022.09.30.03.52.48; Fri, 30 Sep 2022 03:52:59 -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=Czatodoi; 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 S232300AbiI3K0j (ORCPT + 99 others); Fri, 30 Sep 2022 06:26:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231981AbiI3KVS (ORCPT ); Fri, 30 Sep 2022 06:21:18 -0400 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 199B715AB49; Fri, 30 Sep 2022 03:19:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1664533160; x=1696069160; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=51xqS3whQ2fAWcwxbOtIISmE29u7kwNJM4kAauqE20E=; b=Czatodoi3Mc03LtYH3f7ddbnikmtEbIumsR04T58lUoP1wjRQ2r6+L7m QvyPaAHiArbrr/4hi6brcMyX6h4XIpJJ9TQCyoluc+h/JKV5juUxWZVpk 0c8kypsv17VVAzhanscyFy5fWSYj/oBSHjN+Mj/yY+t5zQz/0zKA2O6o9 397nfbz6I6kP4nkt3jtJ/dkM5H55N1tFwOgXp2RCB9NIQFv3lHNi+m23g N/8jfVfhH5FNVkkTamV9ouNftyT1nTYPJAZj3J1lqj8ixJu4LJP4/2dyJ MIFjjPkQ5EcjLmRbwpBAluiCLvXiUIfZL4dZ1VJWAErgCUedKgpsEQVvl w==; X-IronPort-AV: E=McAfee;i="6500,9779,10485"; a="281870122" X-IronPort-AV: E=Sophos;i="5.93,358,1654585200"; d="scan'208";a="281870122" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Sep 2022 03:19:01 -0700 X-IronPort-AV: E=McAfee;i="6500,9779,10485"; a="726807693" X-IronPort-AV: E=Sophos;i="5.93,358,1654585200"; d="scan'208";a="726807693" 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; 30 Sep 2022 03:19: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 , Sean Christopherson Subject: [PATCH v9 058/105] KVM: x86/mmu: Introduce kvm_mmu_map_tdp_page() for use by TDX Date: Fri, 30 Sep 2022 03:17:52 -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.5 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_PASS,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 | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h index f86fb04fb7d7..3cd969bf5b69 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 6bbfaa24d06c..d463bc13c094 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4451,6 +4451,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(vcpu->kvm), + .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