Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp1122366rdb; Tue, 30 Jan 2024 08:30:04 -0800 (PST) X-Google-Smtp-Source: AGHT+IG4asYCm7moMXMwqsXJ+Vn61fC7nfY+huIzBY+FAX0p1jAxk0OntwLSn03rHmK/iYcXLazK X-Received: by 2002:ac2:52a6:0:b0:50e:9c59:5627 with SMTP id r6-20020ac252a6000000b0050e9c595627mr6706164lfm.45.1706632204498; Tue, 30 Jan 2024 08:30:04 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706632204; cv=pass; d=google.com; s=arc-20160816; b=uGSIeAYQ9MhsR7EtcZ8WRlli1nGBqUBjC99tzEPZGysW4PjeNeFjbwve/fN7T+BYRU YU/8tBoUDyzI1GJJ2+6XdAfH4buO5Z9+uz5UPTq840mj77anA7uCasc2YqpizaQJje3f x8T3qbWsCX9KJ7mUyw6CJAhHXOr8+T3WCPbWL3p3aAbLZ9o1FB2SLX4pMC3v3nJ0fED0 Udomp21KAxK5QHve7uebL55TdGNqfnoRgQb3ChhZqq3QoZlwckq7+1aNCqVWn0KR7JXb Xxv4566VPoucSts370qWpqfZZPwQhg1ecdeOoXlPy7mJ9UNIkKxGYsVpUmpE5KiLNYv9 28qA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:precedence:robot-unsubscribe:robot-id :message-id:mime-version:list-unsubscribe:list-subscribe:list-id :precedence:references:in-reply-to:cc:subject:to:reply-to:sender :from:dkim-signature:dkim-signature:date; bh=SCTo04sk1wsU4KOT3MJcifNoYqtBHsWzx0mOZ/gkacU=; fh=xLrUVAfyO5vFwy0TPmHDBT+CR4ip3D6kNuLpn5b1TSE=; b=tUbTbFzOK82d/+Mvr35NMJoDfWIy7Qc9K+CU+mehGdQEJVJ2El5Q3VyBBguf6DxP9F egPhAK0SMhFyEBy+Bm+EMhhy6X+dH0+9Q3fPGjUVMj9T2aLKh8EH1GVSBKVQxlwH22d3 q2S44BUITKsTMDSC0ny458xnhkzMBfa/A9E25n1cDS40sh3EynZAqXfhk73qni155Gm+ b/qkuK1DbybPpUnR/P9WytM0WGptI3x+5XnbIOE2GN1UE/v/BTJoJ3VUGvf930CA7Hac UdkFjMZchuhYaax5NRivUBKvuuML6ahy+chGUD1qVstaRZu4AXaNg2yBeS5CE0UxEfyE BUTw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=A1cSkPbf; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-44937-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-44937-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id c15-20020a509f8f000000b0055f3f79386asi780108edf.117.2024.01.30.08.30.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 08:30:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-44937-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=@linutronix.de header.s=2020 header.b=A1cSkPbf; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-44937-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-44937-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de 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 D66241F27926 for ; Tue, 30 Jan 2024 16:30:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1536912FF71; Tue, 30 Jan 2024 16:26:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="A1cSkPbf"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="avqzJu1O" Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 669B512CDB0; Tue, 30 Jan 2024 16:26:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706631973; cv=none; b=hGiu1GZv10fY8dRJUflnyHkAzfzmeoYORDWUncIpTYAQVrS+MhBC0N7gAKs6Afnff3rE3xbhXHcIuuxdXhbFWiUMbM1EydyYns7Q79XFlNOHBawwSq3PMSg5kZbGTyb4D2LmnWHKYoIjV9fm64PIep5H27Jkl2yPWMugAQpAZJc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706631973; c=relaxed/simple; bh=vFe5JVUm77KdHRpyYdzV+lNo2hjIlqeiOjaKMoo88y0=; h=Date:From:To:Subject:Cc:In-Reply-To:References:MIME-Version: Message-ID:Content-Type; b=Q9+jvnOD9dkp2L+aJO2B5o0b+e/lFSmZaRGX3dskDkYLZimXDLWvZL3PYVkmmeMEo2wZAwdqMqrXqH/SfUyc6oOuWnhsEu9rNMsloJzg382k+7rU+mUePrEHDs3RF/3IfJ9kwxFNjSGydFOw9H3g1QfYG0DEZJL3a7SqJM40qyI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=A1cSkPbf; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=avqzJu1O; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Date: Tue, 30 Jan 2024 16:26:08 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1706631969; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SCTo04sk1wsU4KOT3MJcifNoYqtBHsWzx0mOZ/gkacU=; b=A1cSkPbfkdR0OhXTbVRSLxok6wvLYleR9+ODWlfpsYlaFqMxYNUus4NrGeFyUcrnQJg38o jr4QbttB5CrLU2OsqkgFbL9aeUqmMXCHdA0NjHF9z2sCvUwdGoJZ0V0f7Yu98W/q064ojs f3k02skywzcbCuCaZMwxzspMi6dI3PwS/8CVEnKABVdrayFt/9YWwtqszCrCMP+kJ/C1k0 NYN39dbyyZjbrX1Xp2n9h6FPpBz0RSnpbjPKl4oMmylckn75DqvHBJEswBBJunWeA0PfR1 O//nTmczadOg3c0HFzqmfDfZtU4AEDuccpgYz63td/rWamFICAxSMzfPPm4GSQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1706631969; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SCTo04sk1wsU4KOT3MJcifNoYqtBHsWzx0mOZ/gkacU=; b=avqzJu1OUDXpfksXv/gI0qjoRMDbXDvfvij+APc8ngYMDfmmvvar/g2Yg8Zf1SzchfbDZa RwXIYNXNLm0RuKDA== From: "tip-bot2 for Brijesh Singh" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/sev] x86/fault: Add helper for dumping RMP entries Cc: Brijesh Singh , Ashish Kalra , Michael Roth , "Borislav Petkov (AMD)" , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20240126041126.1927228-8-michael.roth@amd.com> References: <20240126041126.1927228-8-michael.roth@amd.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-ID: <170663196883.398.18077552190870723071.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit The following commit has been merged into the x86/sev branch of tip: Commit-ID: 1f568d36361b4891696280b719ca4b142db872ba Gitweb: https://git.kernel.org/tip/1f568d36361b4891696280b719ca4b142db872ba Author: Brijesh Singh AuthorDate: Thu, 25 Jan 2024 22:11:07 -06:00 Committer: Borislav Petkov (AMD) CommitterDate: Mon, 29 Jan 2024 17:26:30 +01:00 x86/fault: Add helper for dumping RMP entries This information will be useful for debugging things like page faults due to RMP access violations and RMPUPDATE failures. [ mdr: move helper to standalone patch, rework dump logic as suggested by Boris. ] Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth Signed-off-by: Borislav Petkov (AMD) Link: https://lore.kernel.org/r/20240126041126.1927228-8-michael.roth@amd.com --- arch/x86/include/asm/sev.h | 2 +- arch/x86/virt/svm/sev.c | 99 ++++++++++++++++++++++++++++++++---- 2 files changed, 91 insertions(+), 10 deletions(-) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 01ce61b..2c53e3d 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -247,9 +247,11 @@ static inline u64 sev_get_status(void) { return 0; } #ifdef CONFIG_KVM_AMD_SEV bool snp_probe_rmptable_info(void); int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level); +void snp_dump_hva_rmpentry(unsigned long address); #else static inline bool snp_probe_rmptable_info(void) { return false; } static inline int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level) { return -ENODEV; } +static inline void snp_dump_hva_rmpentry(unsigned long address) {} #endif #endif diff --git a/arch/x86/virt/svm/sev.c b/arch/x86/virt/svm/sev.c index 7669b2f..c74266e 100644 --- a/arch/x86/virt/svm/sev.c +++ b/arch/x86/virt/svm/sev.c @@ -35,16 +35,21 @@ * Family 19h Model 01h, Rev B1 processor. */ struct rmpentry { - u64 assigned : 1, - pagesize : 1, - immutable : 1, - rsvd1 : 9, - gpa : 39, - asid : 10, - vmsa : 1, - validated : 1, - rsvd2 : 1; - u64 rsvd3; + union { + struct { + u64 assigned : 1, + pagesize : 1, + immutable : 1, + rsvd1 : 9, + gpa : 39, + asid : 10, + vmsa : 1, + validated : 1, + rsvd2 : 1; + }; + u64 lo; + }; + u64 hi; } __packed; /* @@ -263,3 +268,77 @@ int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level) return 0; } EXPORT_SYMBOL_GPL(snp_lookup_rmpentry); + +/* + * Dump the raw RMP entry for a particular PFN. These bits are documented in the + * PPR for a particular CPU model and provide useful information about how a + * particular PFN is being utilized by the kernel/firmware at the time certain + * unexpected events occur, such as RMP faults. + */ +static void dump_rmpentry(u64 pfn) +{ + u64 pfn_i, pfn_end; + struct rmpentry *e; + int level; + + e = __snp_lookup_rmpentry(pfn, &level); + if (IS_ERR(e)) { + pr_err("Failed to read RMP entry for PFN 0x%llx, error %ld\n", + pfn, PTR_ERR(e)); + return; + } + + if (e->assigned) { + pr_info("PFN 0x%llx, RMP entry: [0x%016llx - 0x%016llx]\n", + pfn, e->lo, e->hi); + return; + } + + /* + * If the RMP entry for a particular PFN is not in an assigned state, + * then it is sometimes useful to get an idea of whether or not any RMP + * entries for other PFNs within the same 2MB region are assigned, since + * those too can affect the ability to access a particular PFN in + * certain situations, such as when the PFN is being accessed via a 2MB + * mapping in the host page table. + */ + pfn_i = ALIGN_DOWN(pfn, PTRS_PER_PMD); + pfn_end = pfn_i + PTRS_PER_PMD; + + pr_info("PFN 0x%llx unassigned, dumping non-zero entries in 2M PFN region: [0x%llx - 0x%llx]\n", + pfn, pfn_i, pfn_end); + + while (pfn_i < pfn_end) { + e = __snp_lookup_rmpentry(pfn_i, &level); + if (IS_ERR(e)) { + pr_err("Error %ld reading RMP entry for PFN 0x%llx\n", + PTR_ERR(e), pfn_i); + pfn_i++; + continue; + } + + if (e->lo || e->hi) + pr_info("PFN: 0x%llx, [0x%016llx - 0x%016llx]\n", pfn_i, e->lo, e->hi); + pfn_i++; + } +} + +void snp_dump_hva_rmpentry(unsigned long hva) +{ + unsigned long paddr; + unsigned int level; + pgd_t *pgd; + pte_t *pte; + + pgd = __va(read_cr3_pa()); + pgd += pgd_index(hva); + pte = lookup_address_in_pgd(pgd, hva, &level); + + if (!pte) { + pr_err("Can't dump RMP entry for HVA %lx: no PTE/PFN found\n", hva); + return; + } + + paddr = PFN_PHYS(pte_pfn(*pte)) | (hva & ~page_level_mask(level)); + dump_rmpentry(PHYS_PFN(paddr)); +}