From patchwork Wed Feb 7 14:48:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 197956 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp2279509dyb; Wed, 7 Feb 2024 06:49:55 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXu9fCjPtLuQnhujHmy1YWfa/PWwPzsiSvV4gcx/XqfiJGQFS9F3Bre2AZRtXD6V9yTjILDkJkvM1xJGfKGlAU9E5R1zA== X-Google-Smtp-Source: AGHT+IFcE7TExEXyVXY3MBBu1+oW9JFdBFpNyLfZrlKH7MdkuLpOFCR63I6Xmqa3wExPMw8TF2Ux X-Received: by 2002:a2e:9992:0:b0:2d0:9076:e9cc with SMTP id w18-20020a2e9992000000b002d09076e9ccmr4665383lji.29.1707317394904; Wed, 07 Feb 2024 06:49:54 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707317394; cv=pass; d=google.com; s=arc-20160816; b=KyNMvnVePSWeay0opj7DiW/o2nFn2iMegQNq071J7Bk+jviWz1IT8CVGvdoZakHBxk Pom/k8jVi0fAMVcoHtkGiEF2s+YQ1215h28rTbhlADGsOI3esw96GC7qc0uT7GxqZgMZ TyqCRhmGTvNEoYxdpEC+W2B5cEQtC8pJe0VcX7VJ8M+ABfxDhp546ypJHn/2QuGXFqzr MjrxtwOeqFqKHgLQ7K0wHWYc7eYNr9i8ffoQr0UACTRzSZ96tbkdYhKnuYbZLlQ12Mfg WbqmORe0hLBAkfQifuacbHYeFFz37nNWr/ev/ztnTq8d0HPA+oIZsJ7IxluT+jEgiSpQ TMUA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=KuSad3U75XWRyrG0hVFC7e1TsgCe+6P5oSqEz6h0Hdo=; fh=eYaC3yeqFx4xPXrC+hayLw7zNuqgT6vKYFTWYGF/G7U=; b=yKXDI0EGK+PHs+EdFm9NPZGTjHhIsGP5PkTHgReTxPy8KbXWJCOouzdsr5BsUeQ4Ek jOPXBzM+8Oqu4cMRJHHby9dtZilsqz8mI3u7LiCjVnLLzg8FfYsouV1cS1Hrnr7FaIKN i3VhZA7FE+4kiA9w35+yInNKI7PELJubDKLJbfzNLTcGdhbO4AIjkBV4p+R/7C61S5mq lH0F/3Fh7kJFsbJp3KNdwJ77Dafp7aHNUeurGuePU6rNVpBRY7jJZXMFT7Iy1ap8CMr9 qAjpOmAnY3+ilRLGz7+oVIEeWx1wzNU1qxUlv/BNP35J3ULZ0zi8mGjGrUZ+jaxwWJ+v j/sA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=V+6LXN51; arc=pass (i=1 spf=pass spfdomain=flex--sebastianene.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-56651-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56651-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCVd6NsdqixM6mZFz1Ktjp6tdj7dlg+uccrANG1cB2iW7POld0xqoZJ+rDvzN1BlGDEFjy89rhaJknPIAxrSuR6HO6iJBg== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a1-20020aa7d741000000b00560afd05a4bsi893606eds.88.2024.02.07.06.49.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 06:49:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-56651-ouuuleilei=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=@google.com header.s=20230601 header.b=V+6LXN51; arc=pass (i=1 spf=pass spfdomain=flex--sebastianene.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-56651-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56651-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 52E2E1F27327 for ; Wed, 7 Feb 2024 14:49:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7A4A27F7EB; Wed, 7 Feb 2024 14:49:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="V+6LXN51" Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 87B647E788 for ; Wed, 7 Feb 2024 14:49:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707317362; cv=none; b=P9EOzuqlwA3MRLGvPR5Mws35BvtcscH87H8UdZyIZTv9pSbwmHutCoCDDShmSKJS0KaMGURTsBtrMNPwN/Z8suZgFX6AdOwLq6CbaiFAsQR5MdPeBWjPy+/5GGoSnTc4i3PG7jcwBXAWT20LeoAVQ9PhH875tBR/DAw+J4eeeRU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707317362; c=relaxed/simple; bh=6vzgHsVxuihU3g4TMAduWqr1ZW75RviyAlTg5U5fA5M=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=sOfVXlgGCM9GhDtJKkwmpaKUMwkKMlt8sctJZOguQ1bCCrXvyEhiHWmxM2qDxI9DqzkxSw+Ex5mBKBGCpaJFdeAEpILKtTKih1+Ira8po7cf8CvKvFdfjh7K74KBjVfeVZFjtk4sRnElZSVPM4w+PbsMxC+Ki59eaboNRiYJae4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=V+6LXN51; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dc64b659a9cso1170664276.3 for ; Wed, 07 Feb 2024 06:49:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707317359; x=1707922159; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=KuSad3U75XWRyrG0hVFC7e1TsgCe+6P5oSqEz6h0Hdo=; b=V+6LXN51egocelhYpPCGC0FSCX3O9qOcgbKQeueTaPfPCCUdXooa8sH6hTR4s8ZqdH sf3qWUxWDiMJ/3NNf7b4pKoMv95h77K1HqzL/5FPVSLh1eKOqnz3WCzq1mh5I2h44MDz 7vDGFIeZC0Th9ngqu75h6GDro6aDxL9zBENFMnsp6jeiozra+94th9FpWRBCPe51TSLA 7dKgZ+Pfs6OjwpTNYJ3v9mAlSNfn8denk0ik0uMYJjGic05ugTcM/qZVr5h2l0KbIYcH 4L0+3tHfXh5Dto/M3X5PENBJXSm1Gww8KNg4hMr9w4W2MgPWUsMJCKtvm4mU/IIUvFHu Y/Yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707317359; x=1707922159; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=KuSad3U75XWRyrG0hVFC7e1TsgCe+6P5oSqEz6h0Hdo=; b=XOT7T/3CaDMFK/ezKHGftWt/6BZl18bXWYqd5R/xRTqv2T9CDyDerM2L6ijklorVXv TBJaKS5I+b5LhJ74dOnax9lMqm2ukaV9BBC4MVn0DA7qLGdGll2O9HDMvjisZBaZrqWM ZJapE4pmkaVNAN/QcHFlumieezpVm6XmjHMSKuQlScpAXGGvtgPERmMv6+x2zY0SDIgz xwRnK7fBvb6FpdSYTIJcw61Fqg7is5GVJZfkByKxPAiiJQHak8VLDopIcT5NyvO5/5z2 r//6RBhesLFTULdqZtLr/orsU11qPCVr3RmrVPz0nG/Vw9BV2tVOCALubDn6Cx2VLfF4 xKjw== X-Forwarded-Encrypted: i=1; AJvYcCUQ9T2IhfyJHjM/oGVIpWVufPTNA4RWG+AKWDRR7wEwxr0L6tjlIvFN0mSAWFCECRUANQQBQ/pxl06I1IoF6mdMZNQ2GvxaYhOQ6zHQ X-Gm-Message-State: AOJu0YwmTtaZsoLpT5sC7KZRqylAd4FrSM+cRHDEIuLonIYd5iWDXj0W asKEnbcfdYh2+dG7QkY3FUZsIssRw1uOk6m15V2pgMZwy+Mjc65AmWi5Luxi55BxcAFtAlObphK dobnzHE60mrF5lq2iuocflGCKmQ== X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a05:6902:200c:b0:dc6:53c3:bcbd with SMTP id dh12-20020a056902200c00b00dc653c3bcbdmr1272474ybb.7.1707317359551; Wed, 07 Feb 2024 06:49:19 -0800 (PST) Date: Wed, 7 Feb 2024 14:48:30 +0000 In-Reply-To: <20240207144832.1017815-2-sebastianene@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240207144832.1017815-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Message-ID: <20240207144832.1017815-3-sebastianene@google.com> Subject: [PATCH v5 1/4] arm64: ptdump: Expose the attribute parsing functionality From: Sebastian Ene To: catalin.marinas@arm.com, gshan@redhat.com, james.morse@arm.com, mark.rutland@arm.com, maz@kernel.org, oliver.upton@linux.dev, rananta@google.com, ricarkol@google.com, ryan.roberts@arm.com, shahuang@redhat.com, suzuki.poulose@arm.com, will@kernel.org, yuzenghui@huawei.com Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-team@android.com, vdonnefort@google.com, Sebastian Ene X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790252044581169498 X-GMAIL-MSGID: 1790252044581169498 To keep the same output format as the arch specific ptdump and for the sake of reusability, move the parser's state tracking code out of the arch specific. Signed-off-by: Sebastian Ene --- arch/arm64/include/asm/ptdump.h | 41 ++++++++++++++++++++++++++++++++- arch/arm64/mm/ptdump.c | 36 ++--------------------------- 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/arch/arm64/include/asm/ptdump.h b/arch/arm64/include/asm/ptdump.h index 581caac525b0..23510be35084 100644 --- a/arch/arm64/include/asm/ptdump.h +++ b/arch/arm64/include/asm/ptdump.h @@ -9,6 +9,8 @@ #include #include +#include + struct addr_marker { unsigned long start_address; @@ -21,15 +23,52 @@ struct ptdump_info { unsigned long base_addr; }; +struct prot_bits { + u64 mask; + u64 val; + const char *set; + const char *clear; +}; + +struct pg_level { + const struct prot_bits *bits; + const char *name; + size_t num; + u64 mask; +}; + +/* + * The page dumper groups page table entries of the same type into a single + * description. It uses pg_state to track the range information while + * iterating over the pte entries. When the continuity is broken it then + * dumps out a description of the range. + */ +struct pg_state { + struct ptdump_state ptdump; + struct seq_file *seq; + const struct addr_marker *marker; + unsigned long start_address; + int level; + u64 current_prot; + bool check_wx; + unsigned long wx_pages; + unsigned long uxn_pages; +}; + void ptdump_walk(struct seq_file *s, struct ptdump_info *info); +void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, + u64 val); #ifdef CONFIG_PTDUMP_DEBUGFS #define EFI_RUNTIME_MAP_END DEFAULT_MAP_WINDOW_64 void __init ptdump_debugfs_register(struct ptdump_info *info, const char *name); #else static inline void ptdump_debugfs_register(struct ptdump_info *info, const char *name) { } -#endif +#endif /* CONFIG_PTDUMP_DEBUGFS */ void ptdump_check_wx(void); +#else +static inline void note_page(void *pt_st, unsigned long addr, + int level, u64 val) { } #endif /* CONFIG_PTDUMP_CORE */ #ifdef CONFIG_DEBUG_WX diff --git a/arch/arm64/mm/ptdump.c b/arch/arm64/mm/ptdump.c index e305b6593c4e..64127c70b109 100644 --- a/arch/arm64/mm/ptdump.c +++ b/arch/arm64/mm/ptdump.c @@ -66,31 +66,6 @@ static struct addr_marker address_markers[] = { seq_printf(m, fmt); \ }) -/* - * The page dumper groups page table entries of the same type into a single - * description. It uses pg_state to track the range information while - * iterating over the pte entries. When the continuity is broken it then - * dumps out a description of the range. - */ -struct pg_state { - struct ptdump_state ptdump; - struct seq_file *seq; - const struct addr_marker *marker; - unsigned long start_address; - int level; - u64 current_prot; - bool check_wx; - unsigned long wx_pages; - unsigned long uxn_pages; -}; - -struct prot_bits { - u64 mask; - u64 val; - const char *set; - const char *clear; -}; - static const struct prot_bits pte_bits[] = { { .mask = PTE_VALID, @@ -170,13 +145,6 @@ static const struct prot_bits pte_bits[] = { } }; -struct pg_level { - const struct prot_bits *bits; - const char *name; - size_t num; - u64 mask; -}; - static struct pg_level pg_level[] = { { /* pgd */ .name = "PGD", @@ -248,8 +216,8 @@ static void note_prot_wx(struct pg_state *st, unsigned long addr) st->wx_pages += (addr - st->start_address) / PAGE_SIZE; } -static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, - u64 val) +void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, + u64 val) { struct pg_state *st = container_of(pt_st, struct pg_state, ptdump); static const char units[] = "KMGTPE"; From patchwork Wed Feb 7 14:48:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 197958 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp2279841dyb; Wed, 7 Feb 2024 06:50:27 -0800 (PST) X-Google-Smtp-Source: AGHT+IHg6KiGkhdi+umeNBVrW4AJhUxDB3QFOS1n2Oo4v8UYp/KPut+T8pIURvyi/towuolJij/k X-Received: by 2002:a25:361f:0:b0:dc2:2799:981a with SMTP id d31-20020a25361f000000b00dc22799981amr5012711yba.18.1707317427575; Wed, 07 Feb 2024 06:50:27 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707317427; cv=pass; d=google.com; s=arc-20160816; b=ttvdJ+bt0f27qU0ime+RLXH6eqa6jT7eee/lVDn+wMOg5hFb7DcQ/mqJMkTfSd4qma dESnYR53EIeQTar5bemDrtejqk9aA+X4pilfMh2jYT+4NSwFzSId8brzEbLZH4Y9Zwjc HVwtcLS7g6/npH07Bh8mWgZfy/mx7otRYQ74UEhKyqNiJ19RXU6kiUOW2m92NXc9ce2q TEnHaaeCDx+jHlHVhHYsi0HJlZeAEvC03k0xQvTj8kgLUzeVHPIt6P0tzS7kGMDyHHYC mG5BiPBEPmkmXnBGE2vIymBvCmwgL3wvWjD2xpk+4PyTd/JPKkEJELVoybAPGYA9yWxq cMjQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=J1F3sgOZBAQMU7R2Rlac0l+wJat72mlIsX+wYVgtE5c=; fh=SvCmgZovs3mGa+N9+o6udi4hmqRZ3fFxl+Mt6Ywoiss=; b=zTSErMQ4AHs1cosC33PPFTk/+RXBiINAuADhb4QjwBO9W0euaJ6KnjH7lc+dC//1kf X5ZAFcURmv2AGs/6Hdku78nH1V57WM3PLoMlsdtzoFukjnsqpDKAteesUvr9cHFIN8nM Pc5U8cmGgFbZAvxGWo+IhADDJN/wzqQ7WC1rcakZCCYOhhlVRhK9L4Ai0MWknVh5S734 FHyj/E14no4j9Ay5lqeiHj2zHy5LhYKHFdjEeirzKBh66SQULqOukTqELMSHpNKXzRp5 VnK4n/v5ijSfHG4XxM0Y/rdIyGrCpVEhaV9pb/vH05+4j8rJLA1uQMNleWJoWuzlwjTs tD+Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=mT7hu7CU; arc=pass (i=1 spf=pass spfdomain=flex--sebastianene.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-56653-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56653-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCUONMmRkEUiG8/btDRWUGgOY4/ls6Tu05UWmT1A9iSarylresK6aRtgv4XTpd+ZHJWOtEHXo3OJlGDvbhhsEUy78hOlAQ== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id fq12-20020a056214258c00b0068c67a0fc45si1298545qvb.572.2024.02.07.06.50.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 06:50:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-56653-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=mT7hu7CU; arc=pass (i=1 spf=pass spfdomain=flex--sebastianene.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-56653-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56653-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 4E0F71C20A10 for ; Wed, 7 Feb 2024 14:50:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 755E27FBD5; Wed, 7 Feb 2024 14:49:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="mT7hu7CU" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 364CE7F7CB for ; Wed, 7 Feb 2024 14:49:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707317366; cv=none; b=NmKdxB23+vheXpX/znlkzYogfLYxYeHgoZCmlIyzMMGvP7sYYN/1Vo2nKNUaa/IWQc25MjKZKjcqdY+4oKPFf/dWiGkykoP4M81Qv53bNHIYU+CYMDry1lJLLRXKLWc6vz+pyG1NL4IJLbuLMCLWOe7Jim2ztOu2tvG6C0mfXrs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707317366; c=relaxed/simple; bh=rNGGZUY2dBsJq36eaQaTKYVIz8wf7Lh+KRhuF6gF5W8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=uhogigeS7Z02GU5TAarZXSBQ4WjKuW93vl1qFnXgESzuHNWDzFjDHuZ/dxSPlddUdwN0ysYamkogJSrF1MxqhFSOfZvfCFH70hJus8PUoNsBK7ERRW9nn63P3wb4n/YS7nP1QtEFa44nlD8v8HzlnNHkJ75QQpwnpePUErXjpGQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=mT7hu7CU; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-60484a61250so13945727b3.1 for ; Wed, 07 Feb 2024 06:49:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707317364; x=1707922164; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=J1F3sgOZBAQMU7R2Rlac0l+wJat72mlIsX+wYVgtE5c=; b=mT7hu7CUGFOIS+XLPewRqWmTVYeEXNsJCow9FtMkZvi4rOgj5qMvlSo2CPDrEJHc3A T1J7oTJWracl5M8TZxDssHBdqeahrPGQfMAqwDctQRsnOEup2inS0VUxWApvwGpEYpsq qAn+aBEmza8uX/stXyWMuCB+t4zjqDhKqojNLNif57sBKFoY5ek6YMMFDdvAT/yi4X2x vWZCJq17Auf9wH4d+YmtqRcrpBCCdeW1zg5+YKuZIMNNtfr2svs7KaVsIXDboXZJl8rZ aZmMF4L8iDs/SmzUTgheCZKErzuvzSS6AUhBGCvNnQxRK9khFTdkw4IqdIJ2A/QsgdEO Pn8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707317364; x=1707922164; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=J1F3sgOZBAQMU7R2Rlac0l+wJat72mlIsX+wYVgtE5c=; b=cqCod8HM7TMCNxXvRjPNGZePm9nAgq8DildUYknDIoJ84eOH26Jm3TrbMZV8pa0iCR ymUlfFM9bqDcIsIXnQ+jwoaUH44OXNOLyYjN2fpLdlL1MPUgMofPiblriHtSQDodfaix QHw8xYqJSc8P2cuPqUGwUDVggHTJ7FvHG6jwxUNmVE/teJmluGSYnwlB6yJ2nWH1e5+W aebgjbIM6K806r+AW5EgpMl5s1aSOMzDFnY7o5E4VeHCWstg9rQBkJeU4A7NE6KkABra BR2FNwH6jDjIaS6DfW1V+uBA0geOfT0qVe5ZvCPlgq5X/DdPj7PSpjtWPHpoT5dzP09s k1XA== X-Gm-Message-State: AOJu0YzOuyQuVqGUo2od9oJl2MgTA2LPgUdD0+1oFym0H5XWb1hfjE6m DGjFAIDmodq8+EqPIPVd0deq47zvrH/2Ama/+bB5FUjoehzbVNxTcgwt0r1s2P8UwpkrvxbDoPJ +uIyMlRt0g52rlmOc53aHONpkEA== X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a05:6902:2187:b0:dc2:1cd6:346e with SMTP id dl7-20020a056902218700b00dc21cd6346emr1222350ybb.8.1707317364293; Wed, 07 Feb 2024 06:49:24 -0800 (PST) Date: Wed, 7 Feb 2024 14:48:32 +0000 In-Reply-To: <20240207144832.1017815-2-sebastianene@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240207144832.1017815-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Message-ID: <20240207144832.1017815-5-sebastianene@google.com> Subject: [PATCH v5 3/4] KVM: arm64: Register ptdump with debugfs on guest creation From: Sebastian Ene To: catalin.marinas@arm.com, gshan@redhat.com, james.morse@arm.com, mark.rutland@arm.com, maz@kernel.org, oliver.upton@linux.dev, rananta@google.com, ricarkol@google.com, ryan.roberts@arm.com, shahuang@redhat.com, suzuki.poulose@arm.com, will@kernel.org, yuzenghui@huawei.com Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-team@android.com, vdonnefort@google.com, Sebastian Ene X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790252078665693786 X-GMAIL-MSGID: 1790252078665693786 While arch/*/mem/ptdump handles the kernel pagetable dumping code, introduce KVM/ptdump which shows the guest stage-2 pagetables. The separation is necessary because most of the definitions from the stage-2 pagetable reside in the KVM path and we will be invoking functionality **specific** to KVM. When a guest is created, register a new file entry under the guest debugfs dir which allows userspace to show the contents of the guest stage-2 pagetables when accessed. Signed-off-by: Sebastian Ene --- arch/arm64/kvm/Kconfig | 13 ++++++ arch/arm64/kvm/Makefile | 1 + arch/arm64/kvm/debug.c | 7 ++++ arch/arm64/kvm/kvm_ptdump.h | 20 ++++++++++ arch/arm64/kvm/ptdump.c | 79 +++++++++++++++++++++++++++++++++++++ 5 files changed, 120 insertions(+) create mode 100644 arch/arm64/kvm/kvm_ptdump.h create mode 100644 arch/arm64/kvm/ptdump.c diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig index 6c3c8ca73e7f..28097dd72174 100644 --- a/arch/arm64/kvm/Kconfig +++ b/arch/arm64/kvm/Kconfig @@ -68,4 +68,17 @@ config PROTECTED_NVHE_STACKTRACE If unsure, or not using protected nVHE (pKVM), say N. +config PTDUMP_STAGE2_DEBUGFS + bool "Present the stage-2 pagetables to debugfs" + depends on PTDUMP_DEBUGFS && KVM + default n + help + Say Y here if you want to show the stage-2 kernel pagetables + layout in a debugfs file. This information is only useful for kernel developers + who are working in architecture specific areas of the kernel. + It is probably not a good idea to enable this feature in a production + kernel. + + If in doubt, say N. + endif # VIRTUALIZATION diff --git a/arch/arm64/kvm/Makefile b/arch/arm64/kvm/Makefile index c0c050e53157..190eac17538c 100644 --- a/arch/arm64/kvm/Makefile +++ b/arch/arm64/kvm/Makefile @@ -23,6 +23,7 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ vgic/vgic-its.o vgic/vgic-debug.o kvm-$(CONFIG_HW_PERF_EVENTS) += pmu-emul.o pmu.o +kvm-$(CONFIG_PTDUMP_STAGE2_DEBUGFS) += ptdump.o always-y := hyp_constants.h hyp-constants.s diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c index 8725291cb00a..aef52836cd90 100644 --- a/arch/arm64/kvm/debug.c +++ b/arch/arm64/kvm/debug.c @@ -14,6 +14,8 @@ #include #include +#include + #include "trace.h" /* These are the bits of MDSCR_EL1 we may manipulate */ @@ -342,3 +344,8 @@ void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu) vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_SPE); vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_TRBE); } + +int kvm_arch_create_vm_debugfs(struct kvm *kvm) +{ + return kvm_ptdump_guest_register(kvm); +} diff --git a/arch/arm64/kvm/kvm_ptdump.h b/arch/arm64/kvm/kvm_ptdump.h new file mode 100644 index 000000000000..a7c00a28481b --- /dev/null +++ b/arch/arm64/kvm/kvm_ptdump.h @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) Google, 2023 + * Author: Sebastian Ene + */ + +#ifndef __KVM_PTDUMP_H +#define __KVM_PTDUMP_H + +#include +#include + + +#ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS +int kvm_ptdump_guest_register(struct kvm *kvm); +#else +static inline int kvm_ptdump_guest_register(struct kvm *kvm) { return 0; } +#endif /* CONFIG_PTDUMP_STAGE2_DEBUGFS */ + +#endif /* __KVM_PTDUMP_H */ diff --git a/arch/arm64/kvm/ptdump.c b/arch/arm64/kvm/ptdump.c new file mode 100644 index 000000000000..a4e984da8aa7 --- /dev/null +++ b/arch/arm64/kvm/ptdump.c @@ -0,0 +1,79 @@ +// SPDX-License-Identifier: GPL-2.0-only +// +// Debug helper used to dump the stage-2 pagetables of the system and their +// associated permissions. +// +// Copyright (C) Google, 2023 +// Author: Sebastian Ene + +#include +#include +#include + +#include +#include + + +static int kvm_ptdump_guest_open(struct inode *inode, struct file *file); +static int kvm_ptdump_guest_show(struct seq_file *m, void *); + +static const struct file_operations kvm_ptdump_guest_fops = { + .open = kvm_ptdump_guest_open, + .read = seq_read, + .llseek = seq_lseek, + .release = single_release, +}; + +static int kvm_ptdump_guest_open(struct inode *inode, struct file *file) +{ + return single_open(file, kvm_ptdump_guest_show, inode->i_private); +} + +static int kvm_ptdump_visitor(const struct kvm_pgtable_visit_ctx *ctx, + enum kvm_pgtable_walk_flags visit) +{ + struct pg_state *st = ctx->arg; + struct ptdump_state *pt_st = &st->ptdump; + + note_page(pt_st, ctx->addr, ctx->level, ctx->old); + return 0; +} + +static int kvm_ptdump_show_common(struct seq_file *m, + struct kvm_pgtable *pgtable, + struct pg_state *parser_state) +{ + struct kvm_pgtable_walker walker = (struct kvm_pgtable_walker) { + .cb = kvm_ptdump_visitor, + .arg = parser_state, + .flags = KVM_PGTABLE_WALK_LEAF, + }; + + return kvm_pgtable_walk(pgtable, 0, BIT(pgtable->ia_bits), &walker); +} + +static int kvm_ptdump_guest_show(struct seq_file *m, void *) +{ + struct kvm *guest_kvm = m->private; + struct kvm_s2_mmu *mmu = &guest_kvm->arch.mmu; + struct pg_state parser_state = {0}; + int ret; + + write_lock(&guest_kvm->mmu_lock); + ret = kvm_ptdump_show_common(m, mmu->pgt, &parser_state); + write_unlock(&guest_kvm->mmu_lock); + + return ret; +} + +int kvm_ptdump_guest_register(struct kvm *kvm) +{ + struct dentry *parent; + + parent = debugfs_create_file("stage2_page_tables", 0400, + kvm->debugfs_dentry, kvm, + &kvm_ptdump_guest_fops); + if (IS_ERR(parent)) + return PTR_ERR(parent); + return 0; +} From patchwork Wed Feb 7 14:48:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 197965 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp2283122dyb; Wed, 7 Feb 2024 06:56:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IGs6DwvMXcusd46/LWKtuOCBdfoGLtXqu78sbZ0HtBWOGYzQPa9NBhG+mgF8Uy93R5LlN09 X-Received: by 2002:ac2:5ec4:0:b0:511:4f19:f45b with SMTP id d4-20020ac25ec4000000b005114f19f45bmr4146009lfq.41.1707317782191; Wed, 07 Feb 2024 06:56:22 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707317782; cv=pass; d=google.com; s=arc-20160816; b=oW5SB4oIqgDqhY6tMPaDH6ktXONpyeq+GD447vl98lTX3Aj6od30iS9Oc3dlpoEyKZ mCBODBewMnrvI/NZWsDx5/9gBnNffw3iIq8jhxsIUwqjAEaeVYad78koMqdkBuDRX6PZ FZMR29F628YmP7x7wyzlpTT2BJjd2Ym9+OwvzOzBs38b4az/YJ4vtDZWCJGmKKilSJ+E 3C5Z5OS9J9Z6tosC84ickRt8n417RjWMlGgKaFB9iUFYhWWebqgEOKsIk/HnbgvPOGkd LcWKd+jdQo3pYBSekWPpYHlLnJYm3cyD/qQ0gYYnO6fpFYYD4qPBZrMJf7S6Z06UtzCY 6hxw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=hjt2/pLXLGVmY8in0C2dCgK4OmxemqDeWqGIHoWM86A=; fh=bkz7DXUc6q74QiDWq7VMZrkwo9szRvbRUpTXs5G7u3I=; b=xfxInRiAza6lq6BmunJVxEHsPRxJU+VlFHL8m1U5RYnR+vOH0Nc1jPiDq/tmsY+aUW VILpZqSv7R236iQQCDXPbPH87qqCUMDv46mZvvWbASAtlJSRWX5J3RCLwq/RNQECLMdx XAYUuQmVCXjnfKZCVCaAmGy+v4k9GNPT70/7Xr45w/smgLQ9NtFdj+JyTxCE0HjoOY9k wH0yLaEwJ68iLHlVzZmmBYk3hbw9Bx+x8vuUCQrSpmnt1ueOHMirjYlkoIqrR9CzYolv 0DaKEU6rbzAjLqMncLKWRO6idIzLC4t2eDUjxlcxlefRMbKBNkjSBUK5UXdyCYbD3G0I BnWQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=1XFynHfn; arc=pass (i=1 spf=pass spfdomain=flex--sebastianene.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-56654-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56654-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCVBIOoJnssRIeoKJppkWnvQtXiJ4VXPpDD8RcNt9iySnfpBeOJly+AA4WNzKpUnkoUGBzv3qdvCWWWJeL9Sl4rUU5VKOg== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id e14-20020a056402190e00b00560ef62a0ffsi194252edz.84.2024.02.07.06.56.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 06:56:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-56654-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=1XFynHfn; arc=pass (i=1 spf=pass spfdomain=flex--sebastianene.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-56654-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56654-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 BEE371F27E11 for ; Wed, 7 Feb 2024 14:50:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 464D980023; Wed, 7 Feb 2024 14:49:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="1XFynHfn" Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2FF427F7F0 for ; Wed, 7 Feb 2024 14:49:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707317369; cv=none; b=NMbN0VMMjdFIuyt+rZGgiPZ5Lth6Hab8zI3nGFeGyYvD9Ek57OkzPcyzQxXUJyvOmu+O49OPIkcykxCedgSgKfDQp0Vjx6sRoCE5jg5vOYlyYeQVPZsko2hW9WQ9K/YXVn373tbEVcVIHZCvb55Q3Z397cB5QKf180H+AQf36dU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707317369; c=relaxed/simple; bh=WW5SKAuytyWeorL93MuaOawok6QGUPzmoldTAJow+n4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ZLxF/Ws+RrBG2BsAACsFhxb70OXcVln13TSDJ6bhn5pJcu3FORsFS7IQHqtY2To3LR4uu7HLHNZ2rxU8S/3r8EmdCkDXFDI+Pbl0mvbdB6uoCPhMdNZlF6I6BhNzj9ZFXq0eOZvP7Chb5j/roOLePlah7DJQArzY8ygdrSSAqIQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=1XFynHfn; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-41024549a9fso725305e9.0 for ; Wed, 07 Feb 2024 06:49:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707317366; x=1707922166; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=hjt2/pLXLGVmY8in0C2dCgK4OmxemqDeWqGIHoWM86A=; b=1XFynHfn3hb7oLspH/59RWg6bFRAZK5eOySp/R6iCuGWxuQ4kEEIE1E0wl24aQmvYz ljAtYwaUQmLg8udyxzpTKpMx3hlFQ0lG9zb3MGuCPWlN5zLkrhHL9t+/tCfuzYg1by31 4vTwj2nwPtO6zZ8XHR0S1PT9RyjTq4mQbZ3JO+s1mqFXEYnUgaLGpn14JxdEFODTRHjf WbjrEr7MBYxrzgRPv7sLWRLHW85am8QgzJnOuaT98fF+cw6IMGhAUdPj+Xb5JJlRklQ1 FHImrrZ400LAnUI7xVPqQ2x+f4dD8aJUwYGSYCdKYyMq70tsiFrbbGH8V9b8CwNBioHc scyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707317366; x=1707922166; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=hjt2/pLXLGVmY8in0C2dCgK4OmxemqDeWqGIHoWM86A=; b=Irm/L1HgNNKsGCsn2kKPXL4x/JeqXg7AuGOVOO1QWfWk6e+/Cd7iyNg4FtM6yGRwuo SWjM+YFICnKX3MPDa1YfcQ+FQjytkNJCW/7PXNp2Y7nsxPFlJ2vrtwndRm5ToNU5WFPf CSubjFd1A25A8aGr5dYZpc49oYP2qeKILm+lHneIFhrSMCYeDGhvQgeeoE60gevFUEZy /GIz4Q+okEKbcYUcV98cJBRFq5dY+9SMUHUF9883gpieIRNNihcaqfQ3PA5Rx/67DSjw onIr38gipbplbgnnN+Z/Mkn1WzLyTfTn6LpQPRVQ06lB2saqcjEL8hf5m06mVqlGnAwW Ksfg== X-Gm-Message-State: AOJu0Ywm1LSehkG6AYkut2J7tMByOhVkVtrV0fRo41fqBJ9LTi87d6LE gbESsK8fzVsBqMkPpUQjWXh9qBi5eUi0Ign4C6KsvaZmgD0MNt9Ddx9OdL6tVIDj+ORbffNunns qzPdH235blHZr55IsVBEVlzmgbw== X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a05:600c:5644:b0:40f:df1e:ff56 with SMTP id js4-20020a05600c564400b0040fdf1eff56mr25678wmb.6.1707317366436; Wed, 07 Feb 2024 06:49:26 -0800 (PST) Date: Wed, 7 Feb 2024 14:48:33 +0000 In-Reply-To: <20240207144832.1017815-2-sebastianene@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240207144832.1017815-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Message-ID: <20240207144832.1017815-6-sebastianene@google.com> Subject: [PATCH v5 4/4] KVM: arm64: Initialize the ptdump parser with stage-2 attributes From: Sebastian Ene To: catalin.marinas@arm.com, gshan@redhat.com, james.morse@arm.com, mark.rutland@arm.com, maz@kernel.org, oliver.upton@linux.dev, rananta@google.com, ricarkol@google.com, ryan.roberts@arm.com, shahuang@redhat.com, suzuki.poulose@arm.com, will@kernel.org, yuzenghui@huawei.com Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-team@android.com, vdonnefort@google.com, Sebastian Ene X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790252450622906109 X-GMAIL-MSGID: 1790252450622906109 Define a set of attributes used by the ptdump parser to display the properties of a guest memory region covered by a pagetable descriptor. Build a description of the pagetable levels and initialize the parser with this configuration. Signed-off-by: Sebastian Ene --- arch/arm64/kvm/ptdump.c | 156 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) diff --git a/arch/arm64/kvm/ptdump.c b/arch/arm64/kvm/ptdump.c index a4e984da8aa7..60725d46f17b 100644 --- a/arch/arm64/kvm/ptdump.c +++ b/arch/arm64/kvm/ptdump.c @@ -14,6 +14,69 @@ #include +#define ADDR_MARKER_LEN (2) +#define MARKER_MSG_LEN (32) + +static const struct prot_bits stage2_pte_bits[] = { + { + .mask = PTE_VALID, + .val = PTE_VALID, + .set = " ", + .clear = "F", + }, { + .mask = KVM_PTE_LEAF_ATTR_HI_S2_XN | PTE_VALID, + .val = KVM_PTE_LEAF_ATTR_HI_S2_XN | PTE_VALID, + .set = "XN", + .clear = " ", + }, { + .mask = KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | PTE_VALID, + .val = KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | PTE_VALID, + .set = "R", + .clear = " ", + }, { + .mask = KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | PTE_VALID, + .val = KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | PTE_VALID, + .set = "W", + .clear = " ", + }, { + .mask = KVM_PTE_LEAF_ATTR_LO_S2_AF | PTE_VALID, + .val = KVM_PTE_LEAF_ATTR_LO_S2_AF | PTE_VALID, + .set = "AF", + .clear = " ", + }, { + .mask = PTE_NG, + .val = PTE_NG, + .set = "FnXS", + .clear = " ", + }, { + .mask = PTE_CONT | PTE_VALID, + .val = PTE_CONT | PTE_VALID, + .set = "CON", + .clear = " ", + }, { + .mask = PTE_TABLE_BIT, + .val = PTE_TABLE_BIT, + .set = " ", + .clear = "BLK", + }, { + .mask = KVM_PGTABLE_PROT_SW0, + .val = KVM_PGTABLE_PROT_SW0, + .set = "SW0", /* PKVM_PAGE_SHARED_OWNED */ + }, { + .mask = KVM_PGTABLE_PROT_SW1, + .val = KVM_PGTABLE_PROT_SW1, + .set = "SW1", /* PKVM_PAGE_SHARED_BORROWED */ + }, { + .mask = KVM_PGTABLE_PROT_SW2, + .val = KVM_PGTABLE_PROT_SW2, + .set = "SW2", + }, { + .mask = KVM_PGTABLE_PROT_SW3, + .val = KVM_PGTABLE_PROT_SW3, + .set = "SW3", + }, +}; + static int kvm_ptdump_guest_open(struct inode *inode, struct file *file); static int kvm_ptdump_guest_show(struct seq_file *m, void *); @@ -52,6 +115,94 @@ static int kvm_ptdump_show_common(struct seq_file *m, return kvm_pgtable_walk(pgtable, 0, BIT(pgtable->ia_bits), &walker); } +static void kvm_ptdump_build_levels(struct pg_level *level, u32 start_lvl) +{ + static const char * const level_names[] = {"PGD", "PUD", "PMD", "PTE"}; + u32 i = 0; + u64 mask_lvl = 0; + + if (start_lvl > 2) { + pr_err("invalid start_lvl %u\n", start_lvl); + return; + } + + for (i = 0; i < ARRAY_SIZE(stage2_pte_bits); i++) + mask_lvl |= stage2_pte_bits[i].mask; + + for (i = start_lvl; i <= KVM_PGTABLE_LAST_LEVEL; i++) { + level[i].name = level_names[i]; + level[i].num = ARRAY_SIZE(stage2_pte_bits); + level[i].bits = stage2_pte_bits; + level[i].mask = mask_lvl; + } + + if (start_lvl > 0) + level[start_lvl].name = level_names[0]; +} + +static int kvm_ptdump_parser_init(struct pg_state *st, + struct kvm_pgtable *pgtable, + struct seq_file *m) +{ + struct addr_marker *ipa_addr_marker; + char *marker_msg; + struct pg_level *level_descr; + struct ptdump_range *range; + + ipa_addr_marker = kzalloc(sizeof(struct addr_marker) * ADDR_MARKER_LEN, + GFP_KERNEL_ACCOUNT); + if (!ipa_addr_marker) + return -ENOMEM; + + marker_msg = kzalloc(MARKER_MSG_LEN, GFP_KERNEL_ACCOUNT); + if (!marker_msg) + goto free_with_marker; + + level_descr = kzalloc(sizeof(struct pg_level) * (KVM_PGTABLE_LAST_LEVEL + 1), + GFP_KERNEL_ACCOUNT); + if (!level_descr) + goto free_with_msg; + + range = kzalloc(sizeof(struct ptdump_range) * ADDR_MARKER_LEN, + GFP_KERNEL_ACCOUNT); + if (!range) + goto free_with_level; + + kvm_ptdump_build_levels(level_descr, pgtable->start_level); + + snprintf(marker_msg, MARKER_MSG_LEN, "IPA bits %2u start lvl %1d", + pgtable->ia_bits, pgtable->start_level); + + ipa_addr_marker[0].name = marker_msg; + ipa_addr_marker[1].start_address = BIT(pgtable->ia_bits); + range[0].end = BIT(pgtable->ia_bits); + + st->seq = m; + st->marker = ipa_addr_marker; + st->level = -1, + st->pg_level = level_descr, + st->ptdump.range = range; + return 0; + +free_with_level: + kfree(level_descr); +free_with_msg: + kfree(marker_msg); +free_with_marker: + kfree(ipa_addr_marker); + return -ENOMEM; +} + +static void kvm_ptdump_parser_teardown(struct pg_state *st) +{ + const struct addr_marker *ipa_addr_marker = st->marker; + + kfree(ipa_addr_marker[0].name); + kfree(ipa_addr_marker); + kfree(st->pg_level); + kfree(st->ptdump.range); +} + static int kvm_ptdump_guest_show(struct seq_file *m, void *) { struct kvm *guest_kvm = m->private; @@ -59,10 +210,15 @@ static int kvm_ptdump_guest_show(struct seq_file *m, void *) struct pg_state parser_state = {0}; int ret; + ret = kvm_ptdump_parser_init(&parser_state, mmu->pgt, m); + if (ret) + return ret; + write_lock(&guest_kvm->mmu_lock); ret = kvm_ptdump_show_common(m, mmu->pgt, &parser_state); write_unlock(&guest_kvm->mmu_lock); + kvm_ptdump_parser_teardown(&parser_state); return ret; }