Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2684732rwd; Sun, 28 May 2023 22:10:06 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7zjBr+lmn5NRqm7CM93hhS9JfDqjI8QYzv1WBHnH7Pez+AsNL5cuA16gpqwMC6N27UoVTK X-Received: by 2002:a05:6a00:1797:b0:63d:3981:313d with SMTP id s23-20020a056a00179700b0063d3981313dmr14290629pfg.10.1685337006079; Sun, 28 May 2023 22:10:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685337006; cv=none; d=google.com; s=arc-20160816; b=QXZqx/ehiveLullG13bYvDwp4pNBjLQbb7obxvxRwSNGOLqTAwSNS4qJQHtTMsDZfL 7GwG6016QHpFdD3WLiMhDXifrgseS89kTPWr6JKzoWY4LY/GCaSMg9MTotC5F5mr2jvk 4b8O+gL2JHgtjP2/kpN1/SFsOKQfK8anF/dn9qhPr6dljcqH4Z2NiaFu2iQ3EjKu0s5F A8PUJlfRPf396N8XCpBikKRrnbP0gFsuzHKCIS2NKDLIkstC+Rfp4bnqO1zI+21RKo5c +2KG1ltCZhda9kAlyUUgGKXcaQpT2R+3WfxNHTepZppg3UcIAo9/QriIaP9oO4KX7f8I d8DQ== 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=NbIMvmhObyLxGJqLTKP7Ya3ZoRjjo8fBP3hmbYIqkFA=; b=iNWG8E+4yb4cMMKgezeoCe0othyPcmiGhbwkHq/xsnrZmLBXno959nco5TVSrPl40g NdKz6RtPY90ght8FgQVRsPEKzm6zxhQ9Pl+qM7m/DVQt6wflgd/kLWVpNq9t61hwEZG7 9bI7znOR3pCCo2YgsJbSdUWKElfGklwKCXy9TCiDi5cdqWApHnarUClSxhzmJiPaEzr2 IgSWgR6glI8muJ09xB3papsZittzFVFkOdsgoOub+gAUkNULgblORP35FkReolTGsiyS W+cQ+2M/xOffM5RmbT4UImDD6NziJIKIGGnjyGbnZgQ2ZoeCqB3rsQLdZ8HfqhpVAGOm 5h0A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=IiSNUFST; 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 c17-20020aa79531000000b0064f4865a125si8948277pfp.8.2023.05.28.22.09.54; Sun, 28 May 2023 22:10:06 -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=IiSNUFST; 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 S231838AbjE2E1k (ORCPT + 99 others); Mon, 29 May 2023 00:27:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48424 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231901AbjE2E0g (ORCPT ); Mon, 29 May 2023 00:26:36 -0400 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09F9510C4; Sun, 28 May 2023 21:24:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685334249; x=1716870249; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=4fQMAwBkhBOPcZHrfNwNvf0vZJYCSBt6EDjp3nltWEw=; b=IiSNUFSTH0XLpjAQVuHqO6owXndUbg9ms8K8PPFyiC1YIRBKgOvSM4WD Rl8ZQFptolVeHQilOGylAPCzgyryglN3ixhWVqN/bmWequfgYD18HW8s7 KaENlhutP4/DXfupXR8QXQTcm/fRAKZRbLE7xWBu4/rwgKjxKmWsTI4qq pjvLmHNWsXg0d9i8d3mGxclTDzgjDK11EFwCwU8OpektqdYGUtdxxQSUV n08IpnLXTCZe0D1SngSQd9GYX+FYddWJh1Xu+FC3GhHUPt8nJCM3JiLT5 r8g5Ougax98bGxwkF2XqAQuPZoRgs1SJ5jfHMH5unpbqjXm8HVjkb0Zi7 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="334966075" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="334966075" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2023 21:21:21 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="775784369" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="775784369" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2023 21:21:21 -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 , David Matlack , Kai Huang , Zhi Wang , chen.bo@intel.com, Sean Christopherson Subject: [PATCH v14 058/113] KVM: x86/mmu: Introduce kvm_mmu_map_tdp_page() for use by TDX Date: Sun, 28 May 2023 21:19:40 -0700 Message-Id: <20d6b4926f3bcc31f6acac8882a5b0986f71000b.1685333728.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.6 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,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 | 49 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h index c1781e0d29e0..a613ef581059 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -174,6 +174,9 @@ static inline void kvm_mmu_refresh_passthrough_bits(struct kvm_vcpu *vcpu, __kvm_mmu_refresh_passthrough_bits(vcpu, mmu); } +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 c61b77981403..d6e900fb3927 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4666,6 +4666,55 @@ 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; + +#ifdef CONFIG_X86_64 + if (tdp_mmu_enabled) + r = kvm_tdp_mmu_page_fault(vcpu, &fault); + else +#endif + 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