Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp2610716rdg; Mon, 16 Oct 2023 09:19:29 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG7J3696Zdk9f07W35MQiqhnnsnnHBytIBC1Qfw2p9U3pndeW7/P6C8t9UZy6VPYQmf+A5l X-Received: by 2002:a17:903:2307:b0:1c6:21b4:30bb with SMTP id d7-20020a170903230700b001c621b430bbmr38368639plh.15.1697473168659; Mon, 16 Oct 2023 09:19:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697473168; cv=none; d=google.com; s=arc-20160816; b=aP1KG1o3nLlQTaYAQjaOzrF/yYdhC8Xm1FU3UhWpHkVNxIUgtu83Gltb9WwDEm1qWa UK0rltE4pBcxQtPdkp7H+D7qTErc9WBUALKEkaVVwgptXep3sWLT0k1X40qU7dVGaPGU 3/w+NaY9me9jewQKdwDtam7JEpnh4bnp+dNHPR9OLLWN8rJI32OGCJIxVpD6CUrb67c6 Ap63pGMMCEf/RO9+dOypiM4E/BWjieo8t4SrwlDJNdWBcKmfG6rReTm9ZF6LH6p9hhfd IEFhqjPnQgbykW3heTVx/pNMsI3jv7eI8REicX3m81b07xfdbLJKl9/LABFw6H2TsDVA 6ITg== 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=mFTeLYhST8vwhUfHnRBPcqaHuFQTuH9q1JyrC9nrhRU=; fh=4OEEkzWp/q+dELKxtVy/jK+pEs8VYY1pIiC6FIGYmPg=; b=iE1s1TLeoxIjXdDs4MgzBvt7+YQch088iL9OaFMMsme7l95SSoidi5K2bX8IBFVcG5 JKLa750gQNJOjiITvX7F/fbq6ta4HKSdilb8bzva1bUq2V7/AyB0NGCqMdMRuD1unFd5 xRRST1Q0nw3ozSBx0vp+GbMMFbD2E3zN1qHQdsB6rHHpUefXs4sekbSGCZ8pcIW8HH5U fuiBT2egkndMGb4lDRRjtxRMbM4msiWzU5jY5v2yCdjoCrYDnRg3FZGgef/S/BNdYVzH ZSovqOajvRYvDHRlOYtLS7rvf7b0li88j6etvKJmGzXDa9Lw3ZZrApXYsZ/aVP1gfIk4 jSLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=kzBA+Q7G; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id x8-20020a170902ec8800b001bb9533b968si11609926plg.408.2023.10.16.09.19.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Oct 2023 09:19:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=kzBA+Q7G; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 2DC998023A9B; Mon, 16 Oct 2023 09:19:12 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234084AbjJPQSI (ORCPT + 99 others); Mon, 16 Oct 2023 12:18:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50238 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233904AbjJPQRp (ORCPT ); Mon, 16 Oct 2023 12:17:45 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BFBA110D9; Mon, 16 Oct 2023 09:17:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1697473031; x=1729009031; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=91Iz9TU2xkLNnVsUDWP1cpIEj043q/wVYLPObuZcTGM=; b=kzBA+Q7GW50x7Belc7lx4ungthu4syFOmAhQuZH3pxH5GnlruCvN26Nk SNhSOjEnz4am1dW35Mr4K66whqScFI0wOrx5a+Twi8qaDAtMfaOPZXbzm lDvwp0q/ZuKDAUMPGyy3p7CC+FcWm8d884D4iOFNgHBrhrn7mFUDOQmaZ WWeGfb9se5cSs2fADGT4o8hBhO/sTb5gu33j4xkpvQMkp4PhT6I5Y6J9C cmi0VVW6DX9mJU8D15Ara4T5l6yjhddJQtJWa7EMa47R8ir11rphKqhRQ HiaD5K0xUYYojg7wrHn1qKpMkAc9zTpjQYiEE2JaH8KkXHNtO+KpDg1WB A==; X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="364921837" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="364921837" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2023 09:15:49 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="846448166" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="846448166" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2023 09:15:48 -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, hang.yuan@intel.com, tina.zhang@intel.com, Sean Christopherson Subject: [PATCH v16 058/116] KVM: x86/mmu: Introduce kvm_mmu_map_tdp_page() for use by TDX Date: Mon, 16 Oct 2023 09:14:10 -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=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Mon, 16 Oct 2023 09:19:12 -0700 (PDT) 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 --- v14 -> v15: - Remove loop in kvm_mmu_map_tdp_page() and return error code based on RET_FP_xxx value to avoid potential infinite loop. The caller should loop on -EAGAIN instead now. --- arch/x86/kvm/mmu.h | 3 +++ arch/x86/kvm/mmu/mmu.c | 57 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h index c30fefa39bb4..cb332d90c57f 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -176,6 +176,9 @@ static inline void kvm_mmu_refresh_passthrough_bits(struct kvm_vcpu *vcpu, __kvm_mmu_refresh_passthrough_bits(vcpu, mmu); } +int kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, u64 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 91250b2a7081..107cf27505fe 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4624,6 +4624,63 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) return direct_page_fault(vcpu, fault); } +int kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, u64 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, + .is_private = error_code & PFERR_GUEST_ENC_MASK, + .nx_huge_page_workaround_enabled = is_nx_huge_page_enabled(vcpu->kvm), + }; + + 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); + + r = mmu_topup_memory_caches(vcpu, false); + if (r) + return r; + + 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); + + if (is_error_noslot_pfn(fault.pfn) || vcpu->kvm->vm_bugged) + return -EFAULT; + + switch (r) { + case RET_PF_RETRY: + return -EAGAIN; + + case RET_PF_FIXED: + case RET_PF_SPURIOUS: + return 0; + + case RET_PF_CONTINUE: + case RET_PF_EMULATE: + case RET_PF_INVALID: + default: + return -EIO; + } +} +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