Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp2261047pxm; Fri, 4 Mar 2022 12:33:51 -0800 (PST) X-Google-Smtp-Source: ABdhPJwKHjS8p90C7ye1RVLmx/gbbP36kfzng+BCI+tih5bl5gsk92vD0x+Mj55FgaJtrbPhFoQx X-Received: by 2002:a63:1a54:0:b0:375:82eb:7449 with SMTP id a20-20020a631a54000000b0037582eb7449mr138906pgm.341.1646426030937; Fri, 04 Mar 2022 12:33:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646426030; cv=none; d=google.com; s=arc-20160816; b=Bcm2tevUixD3kCPxd+iGiPA1NKST1LjjHFJKj7N5T5qYL/eU57lBD5jah/u+x9X93V 3dGW65EcHPcB7su/RCxvWmJ7yyF9iAuY75K/p6k6xAOe8v5qC1tpLc04SIFD9AUXVfy6 k37W6IT0MBy2Fakgq2ElnSI+chpIDaaQj+hVXjTMDyHhKTU+8m0vc8pFjJShnpqsdaNC AFTvj+QP+2YwEu5fPKXsnMm3NZgYIqUYCKCJqwhvvhz+3nTl8iPhGhc9pwm+diGs+gQr jnwGLewRQNxUEcd54hBEXJXrrKXmVzV7XuNt5Qemn9MXX+UP+RVNWf5YzOqCDhh53Q6o N5Jw== 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=13FG5onepweC+KnMxIK/M96p5eKqpqOvYU9LwtmZktc=; b=wu7PKNt1Yg05+sPSHoeuXspI4M3ZSNDknOSDrLoDDGYfb1dkn8Ph66q2Zljx8Z57FG 5hfCaO0FeSv0LKMIupSep04gxFMrZPmhAoBZOl6GA+mLTDbLVYImBFlx7C2bxbrW4p0o DT67WwQkxebx3N0bP/a/NUFU8+bMsuxaBHMrMQOizYuPT8g9pqt640Zs2ikRMlIo3cyY 5OdHGqMrJ5JnIi1HB3nlstNmFzzb75GqgBvuIJZyGYb7Y+TBzrbFKDUQJTllN6yTtrod fDPja6gWT6U9mCf6PXTt4Xl5UFIF4bJi/LB3wPs6cIROOBiOmjW5d/ghUrTN5bBwM44R V9qw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=AAxVrYiQ; 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 o16-20020a17090ab89000b001bef6ac9572si410278pjr.166.2022.03.04.12.33.35; Fri, 04 Mar 2022 12:33:50 -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=AAxVrYiQ; 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 S230357AbiCDUdp (ORCPT + 99 others); Fri, 4 Mar 2022 15:33:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230025AbiCDUcV (ORCPT ); Fri, 4 Mar 2022 15:32:21 -0500 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CD5C51E6EAF; Fri, 4 Mar 2022 12:31: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=1646425888; x=1677961888; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=R8wGscALK0YrqTLN5xYNgcB9idFZVhdTH83i80jdSd8=; b=AAxVrYiQA7mHpbryeO9P/+P3P8AFjrFAlO3s+mRBm1rJ2FynmvOhonCT ERHMMiQ+bhcE2hmRy+aGG3RxEGzyvMrxnpc2gPk8goFDp/7TibtTIf6XZ m9CulwUdYYoBj4+V9xyhOpyml61oyjaJiELlCvqIqfLXVGAqMdZ0ml2wK CJgTEOhInQ8hwLCHXG76TmT+TvOGhavqZ+9gP5ig9MXNJGqw6Q4vDyHDo atB8O51aACcdgzy3/C/blnFCFAZqxxCUO6TP/CtDv9VYEhDnRxUc8bMXl 3y5OSh8YHKZ/n2LVJjVnw/4nHGfW23nZxBckxORoTlDdP+jfJIp1GnXWf g==; X-IronPort-AV: E=McAfee;i="6200,9189,10276"; a="251624228" X-IronPort-AV: E=Sophos;i="5.90,156,1643702400"; d="scan'208";a="251624228" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Mar 2022 11:50:28 -0800 X-IronPort-AV: E=Sophos;i="5.90,156,1643702400"; d="scan'208";a="552344413" 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; 04 Mar 2022 11:50:28 -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 , Jim Mattson , erdemaktas@google.com, Connor Kuehl , Sean Christopherson Subject: [RFC PATCH v5 057/104] KVM: x86/mmu: Introduce kvm_mmu_map_tdp_page() for use by TDX Date: Fri, 4 Mar 2022 11:49:13 -0800 Message-Id: <9d51edef710ce19ab1358394e05e9d7b7ee55f4e.1646422845.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=-7.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE 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 | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h index ac4540aa694d..bd93e7235812 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -205,6 +205,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); + /* * Currently, we have two sorts of write-protection, a) the first one * write-protects guest page to sync the guest modification, b) another one is diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index e2d4a7d546e1..72d8f200c819 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4192,6 +4192,44 @@ 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(), + }; + + 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