Received: by 2002:a05:6358:e9c4:b0:b2:91dc:71ab with SMTP id hc4csp4118195rwb; Sun, 7 Aug 2022 15:34:10 -0700 (PDT) X-Google-Smtp-Source: AA6agR4910drZBTwQ+3vGULeHx/5hFXxN+SoBL8KQR2M/TiRUyznDBoVqV8k9IufJGZfJqJpo/bS X-Received: by 2002:a17:90b:390:b0:1f3:ee2:62a8 with SMTP id ga16-20020a17090b039000b001f30ee262a8mr25883460pjb.148.1659911649920; Sun, 07 Aug 2022 15:34:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659911649; cv=none; d=google.com; s=arc-20160816; b=WFOC3wp7Xt7e35IxjV5b0JBPi+xRzFmw9l1m3ZtKjDdvVDqGCOYKbjOgr/pJ0G/mzm i6Lewz5ueLHN7nXXg2i9tFh0iL6T0SeMuPxOmXmzoVRdRZoVZZ4HV31xroxSaHOziUeX fiwnavBwkqIaLpiKu6lrS3DXUx3aI6qgNS/1bDcoyn/RL1L2x76ZQGKbYl/LR9UiCjGU V4+8ALjJAWGAHTilHm0b+i7tSs0/BtVC8k8isuwHDLk7YmT22OtVNGNPlLtipWXNqytJ RjKnQAWyEfMQjN+st86b+GnvWb2cfLty/5OaazgJ7kW8TFO1vAFRcpnYv2ClDwXIcZjw qizQ== 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=4i/0E8EieOfa2H31XV57EToaDztKgAN3I7qXTzOTnZ0=; b=I//wSCW7wQ5ztodAQPQROnyKq+5zEFa/PbMUQMh9UE0RQw7n1ZY9XFBjRx/kDXu9zG dI9yyZk7aRPsGKCwU5Qkh+FYbbq/XxdgDq8t0c8FXWI8g+6XSY2isZ3aq/xHwnrjKNko T1FalCW8l2Hu89zu4MjlzUk4XZVNwD7LWY4hFzWUAlvBGr7zusRRucIm1oUxhBAfA5on Ofm8Hg7Ai+wZjWdTXNvc90PS4xcfW6SqCGhfkfVrkxlv+v+oiYgEnEGBzhJvF/etZo/C 8M14j78r/Kmk3AbvCw1B2w93BVqwwXdGBOC4aaMU8Yi5M2uvLGk+XBDQZUKX65xxuBJ5 wI9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Gkxd5NBt; 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 h12-20020a170902f70c00b0016d8b0dfd3fsi5479731plo.566.2022.08.07.15.33.56; Sun, 07 Aug 2022 15:34:09 -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=Gkxd5NBt; 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 S237160AbiHGWHb (ORCPT + 99 others); Sun, 7 Aug 2022 18:07:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235324AbiHGWDK (ORCPT ); Sun, 7 Aug 2022 18:03:10 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B522B64F6; Sun, 7 Aug 2022 15:02:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1659909764; x=1691445764; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ZB5jllOGvlju3MAuaJoUn61WKZ/LX+iD2tVH/KhjmM0=; b=Gkxd5NBtbPAzOSXfsSYx+f+UG0dCEy2JUGPKpTPvMwWsTq2Z+V2ElAQ4 G4cotVS6Wlup471R0Ib1TiJH6hZRdqqOb54+SijcB3ETc8xWxq4Ypdw59 7Yk7T0ZIk6yXxXMpdjs2LwIdTbj7SbkzVA+2Z1sjMVd81LKbwqiSShZ+T 883hEezCQgj9rFKrI+FHkYRqvoyv9bi9os/CYh9cRaOeZIj3fLMZ1G73G 4dl8crNn0sE6oTIxjiRYIhCjcHmuMHRuyHhW+sZ6SWW/27jKuSIBxwqcG SOrsqIayFjQc9n65sihrkHzPUaTRedL6Y9hxVfWraT8tAyHfuAP4WjDgv g==; X-IronPort-AV: E=McAfee;i="6400,9594,10432"; a="291695698" X-IronPort-AV: E=Sophos;i="5.93,220,1654585200"; d="scan'208";a="291695698" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Aug 2022 15:02:38 -0700 X-IronPort-AV: E=Sophos;i="5.93,220,1654585200"; d="scan'208";a="663682631" 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; 07 Aug 2022 15:02:37 -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: [PATCH v8 057/103] KVM: x86/mmu: Introduce kvm_mmu_map_tdp_page() for use by TDX Date: Sun, 7 Aug 2022 15:01:42 -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=-5.0 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 | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h index dea9f2ed0177..14f795f6db3a 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -152,6 +152,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 c9c27945ed44..00f797c83300 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4416,6 +4416,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