Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp1172928rbb; Mon, 26 Feb 2024 00:33:26 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUY5KkFkeShK/0hCF49EVarDFsy6aYRPBY4obl0H129Hqj5V1/i1CtYFtUUjQ38PTbxKSj4fNQNt24ZjX5ilM3d/54L+MVxVN4D4ayfDA== X-Google-Smtp-Source: AGHT+IFQvQbKZ7Hg/kYupWkD3MT/B0ZApqMQ2fb2R5dk3BTXk1I1N+AkpJk0TDuuC5OXtZY/PVz5 X-Received: by 2002:a17:906:261b:b0:a42:f53b:5a49 with SMTP id h27-20020a170906261b00b00a42f53b5a49mr3320218ejc.19.1708936406030; Mon, 26 Feb 2024 00:33:26 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708936406; cv=pass; d=google.com; s=arc-20160816; b=TuPmO3FOMBUyZPx5u37g/AhP2uavW0Nw+AJIt9Jkj5eQHJw0AGDQzMEdt/GqU1XoJ8 +X5XuRezYnzNeJ+x7Nu9+ovQjBzo0cTy7u+q/9qy8LV0DRJvZ91YSjlIWlMqvhqtwa06 zx5n30St81wwhxdJ4d8IerJrOr7+zkp8j53KQ+/EbqEvJYkddIlLrdkCjpyNEgozCZGG fAmvNO6fiyuPFukD2FpMnrcEhQli+0zAdHyt7UE5RGEhyIyoUv/y15EddO+wxCOeYR/5 z5KS/gMAz7UZyIsT2PKppPh8dZreIDpt8TXLa/Pv5Amlbi+hKllIQxPXhuCmbAFN04m9 cf7w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=gcPjTyXRs1Z0nStCUb2QjLYisrqJLz5OqDG/8cEOe2A=; fh=ka1BjeomumTXa1Vhgkzg2yK2f13tB8VguFI2e9zPJ3c=; b=m864vcwDvktqqctCe5R0uRact70v04xzBPaMEJUTipCMNpLSM5Xc8PcOzy6XV4kPq8 fwQnleiyDofDES1pPNBGw4uVNYUB4n6GLlIPS/WqMjnJKzVIRRan4P1L0dPJ3nwKq303 OKVQ3dYABm/58M16VaM/z8kvZuCpeSqLonI798MHPiN8+E8UkUtZXrMAFaDOn85UQTAc Ksuu8p92S0dwq5ONihn7mSQpaceQPfvhrUDb26PM2tJwjqqzLXZ9HbBDBj8YFGe+b0Qd G58T8y+Juhtnj0F9l3rpgPwxTXIFAgmXgWJd63EBNpBRHCymou8LRQ3xU3stjVdykBPU Mbyw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=hffJbi5x; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-80777-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80777-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id p16-20020a170906229000b00a3f89681212si1880096eja.101.2024.02.26.00.33.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 00:33:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80777-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=hffJbi5x; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-80777-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80777-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id C47ED1F21A70 for ; Mon, 26 Feb 2024 08:33:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DBDFD537ED; Mon, 26 Feb 2024 08:27:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="hffJbi5x" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9AB6E50A68; Mon, 26 Feb 2024 08:27:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936069; cv=none; b=mqaolWkBKHTkWGBS+VHTcwPlXd+AckwWLGYaMSq7GynlsmAzwthbQkyeQ8zMoheXfNdBycCNoq6hhAy3YNS+NDSghzNXFqKpBBI4gwEEbVJyQSUCQXDmOQ3g6YE04EWdc4kkfR2BXoR5MRuEcb5/Fr3au1CxQl9ETk3yZOexXr4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936069; c=relaxed/simple; bh=GqHoLR8nZGd0nBBjdIPWykKQiMpqYqCouXVMjthiwa8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JFePOq7ChUDEcekOi3iewSjsOsB5n3RBaGvhXt/N1z2ECwFvAS0SAQ8qKbqaCmrpOn3cjE6O1zPClBJ5xCpk4MGD1/yz0FHDsE72+oGGgv5gCX+LRUeGuoEpTHQGy992blD8dv5vDkiic5S/ZecaMYgDg43iFXuZD+8wWA8INOA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=hffJbi5x; arc=none smtp.client-ip=192.198.163.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708936068; x=1740472068; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=GqHoLR8nZGd0nBBjdIPWykKQiMpqYqCouXVMjthiwa8=; b=hffJbi5x2K/2siNEXQ1FVjo2ysr0bPi9K2Df481tpULsmHv/EWg0J7S6 tuRzhg0Eo8pz2mc5ql5oAA+cBb5fYGVbnNH9gTINHMM/Ze/DYURmmC9vT /LtPuP/HhaNLfORLkycSUunGQDFGpaM60hQNTccnGaCPT8zPgyCoDbn7I j2pZqcgAx3eatwrrPrMNTVcIuodzzOgBVG50Z1ZGzter9ZuOpCKjs3NjR mR2v33sblytVbMxOmvc1WsW5jY20vMS36sHnDxWTkLdU2LVN16I8Cas9G WsyakamYJ1yPI3XqY+tzT33WhsIwRarQYj/POhsZpubc2286ZiXc4vD8j w==; X-IronPort-AV: E=McAfee;i="6600,9927,10995"; a="28631493" X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="28631493" Received: from fmviesa007.fm.intel.com ([10.60.135.147]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 00:27:43 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="6474345" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmviesa007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 00:27:43 -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 , erdemaktas@google.com, Sean Christopherson , Sagi Shahar , Kai Huang , chen.bo@intel.com, hang.yuan@intel.com, tina.zhang@intel.com, Sean Christopherson Subject: [PATCH v19 016/130] KVM: x86/mmu: Introduce kvm_mmu_map_tdp_page() for use by TDX Date: Mon, 26 Feb 2024 00:25:18 -0800 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- v19: - Move up for KVM_MEMORY_MAPPING. - Add goal_level for the caller to know how many pages are mapped. 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. Signed-off-by: Isaku Yamahata --- 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 60f21bb4c27b..d96c93a25b3b 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -183,6 +183,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, + u8 max_level, u8 *goal_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 61674d6b17aa..ca0c91f14063 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4615,6 +4615,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, + u8 max_level, u8 *goal_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.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: + if (goal_level) + *goal_level = fault.goal_level; + 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