Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp401151rwb; Tue, 25 Jul 2023 18:11:18 -0700 (PDT) X-Google-Smtp-Source: APBJJlE1oKtANFBZcWXIYSnAZSmO7+FlApkRIR9EVAqF41WAatklmVxNcXH2d2zNfwqIrpOzlRmw X-Received: by 2002:a05:6e02:214f:b0:345:af1c:29c4 with SMTP id d15-20020a056e02214f00b00345af1c29c4mr629992ilv.25.1690333878338; Tue, 25 Jul 2023 18:11:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690333878; cv=none; d=google.com; s=arc-20160816; b=fZbz5j6iSpox7eUgM8xRP/RJhXbPPJYoKpxmkoEZZaxy4k7rFtdFXQWp4PRwp88kVt yu4JvVIHlah2vQIm0C15i7q5yNR0Mnp7+H5GU4payfQJkIEt8GzniB2uzJR4OsU47KBF lGQWlFF85NxlPosyUg9C9LgcGfqFUX/B8wzc6yK/S+lBnXdcD+Rod6oi7BSMTw0dU1y7 QhtruOu1Nr+gbPvATjx+SmMHMbfW8QMuRWpEqS1LbtZ83HZFhvrtPyC0GfTInmp8g5CW jdtl3e0H4m5mPtFn35n6vNwKG4rtXcAmvag/Dk9jtrXl9vOoNCZ3sATWx9qLxstkAsKE +d2w== 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=fsBycagJXgJcmqPwI4No3cEI7nGr86pnJdWbBnQ2T/Y=; fh=aHvdLX1Pa61KvQFmeZODM982ht845x7CZ22gIB9kxIo=; b=aCVOCcQ9jmWM/1ykcund6hfIu6HwF9gqwnH5BWYoZz1SU7503PqMJKUYjh8VplZhPn ovrk3GpJt+ifomSGjAZIVPQMBakaYqRoDedxSYIbJUbG4phJ5shrX9UAbTAGTtv+k8JY Gt47E009o2FCuY+AH8ZPyhYRI/y3is5GAY28ub7sEjQdqJMBHw/5XctB/jFZS+xtOQnJ CSjVNUD3GraTs5IHGM65cLlpjDvVdP2Nbv8sqHIEus7j9EAMD9URj5ibocjN909Yrsgo GRdSL89yMRSvIc/oXlMlnaMWy37S5Li7dBNWCYcgDqT1boyh25repLKbQKG22P/oWiMG 1OHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Qyx0cowZ; 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 x32-20020a17090a6c2300b0025c0d113469si288482pjj.58.2023.07.25.18.11.05; Tue, 25 Jul 2023 18:11:18 -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=Qyx0cowZ; 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 S232151AbjGYWRk (ORCPT + 99 others); Tue, 25 Jul 2023 18:17:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232235AbjGYWQO (ORCPT ); Tue, 25 Jul 2023 18:16:14 -0400 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB43430EA; Tue, 25 Jul 2023 15:15:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690323351; x=1721859351; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Wvj0sNH9JtePwrau64elH2rCIjd14dw+JkGIDo/IbFM=; b=Qyx0cowZ3xtSX3QO0zhDTKnQotr9aki1CNjId3JEKoUUMArM0RO65jf+ YXI3LG6HarqQDw8YE6P0cBOdPzSGDhyW/XM9whv88l2ydLtNF4e7bHHxi tgWeikVh9QwStZDpWt88uuUMjKKZcfofRBGiEMmVoO+a4OvqbaSyHF78v D2VW0p6QfYbpomBQjU7uZ4Ie6kmHsC0W5eEiYtepse7ID48B0nBC+Tp+P ZpCevIqyt3MDYsf6beMikhHnCl9mCqTtlkbXlN9xTuChK4CoOBpXvQ10/ gbYwWfJxgBQeSPp1z3BmFgvxeItknJ9rrQJoxOuB73SExwPCLKj5eNmQr g==; X-IronPort-AV: E=McAfee;i="6600,9927,10782"; a="367882511" X-IronPort-AV: E=Sophos;i="6.01,231,1684825200"; d="scan'208";a="367882511" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jul 2023 15:15:49 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10782"; a="840001771" X-IronPort-AV: E=Sophos;i="6.01,231,1684825200"; d="scan'208";a="840001771" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jul 2023 15:15:49 -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 v15 058/115] KVM: x86/mmu: Introduce kvm_mmu_map_tdp_page() for use by TDX Date: Tue, 25 Jul 2023 15:14:09 -0700 Message-Id: <6a4c029af70d41b63bcee3d6a1f0c2377f6eb4bd.1690322424.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.4 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,URIBL_BLOCKED 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 --- 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 | 58 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h index 801e3d6b572d..1bca16217da3 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); } +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 4e9343e759f6..7ef66d8a785b 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4673,6 +4673,64 @@ 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: + case RET_PF_USER: + 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