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: 13548612 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BF9CAC4828F for ; Wed, 7 Feb 2024 14:49:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=p7BSuk+nTHRaA7j7T2+3Kf/XS9+VohDWHPhlYMqGXxc=; b=4HuHutjs19OMC0/192hPUEcrAN U5Hm8YEwCeewBryxmcqR2JV+tZF4rjDTzXdPZjB2KwF3n3pk6ZS78SA2GTleIeahjj8icGcem2mjL DEF7sTwl2jqvQrkL8tmyzElTEztIWlipzh/pYsWPdiLFNYuyJ9Xf3skVmCTue7Yi7OmTGbIkfOlz5 Sq7JrolNuD/dx2XO/xUSSKXclcAjkwSSw4yWekS+/qHnTbh4/TCGoLPavOdqR84tfxpJOp3qXgII2 Q9Vn3hxVtgPNHsa4e0ToY8Q6YLrfc8TgJTyVgjGhPX5y1YtQash2mHC1iGRB4P0TKj80/Th4cmCLm 1YpBDAxA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rXjEp-0000000AxjU-1268; Wed, 07 Feb 2024 14:49:23 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rXjEn-0000000Axim-0RkB for linux-arm-kernel@lists.infradead.org; Wed, 07 Feb 2024 14:49:22 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d9a541b720aso1174028276.0 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=lists.infradead.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=IyfBaGRpyw3d+2e3hQiZ338qKzCebR7pGBfaGl5Ae3s1qdcnTtWP/orOwtK0B2UVX6 QrszturJKcbkEWaHQInVt8qmjy4aE3aWV94Ta95LUPCD6BS/XCrDjR2Dt8kqcbvDHk4w 47O3XDEyQ/pHW3pXSSShaenaU4UbhdN1QckMeSd+e7WUNT/tOimtJOFOot9a63GsZjKd vXpkzHgNafGVPP4OMRtyjfOPwDv716NhCcUHK4fXweP9p35TiU+OnDpUtZ7RbqNtdAj+ vYINbHdf/J/h/6WgnMAtMbT4sEWZyeRLo33/7BhUhNQDIdD2YVZFbk6nIIgllupEhRLI szoA== 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=gbKhRho4cJJyYHPNw8VZBpaUhu6Oa1QuIvRdEbbuakbF6Ty28+AT545JyhmBbt/Ev0 ZG8uIlvzM3sQqk3nf3pyEkc2Ad+RFfoYu0kg3qgCL2UysJjq9z/JZXHP2Z9GXpr3iP1B eLOfdky9NDWoSvfBlt3Q0XmcA8bS+NKHDnJNjmvGDxF30I478ZF9x4PBRC4wyGD7Z430 CT/jq8eaKESGejZttPW8vU0lnhO9yTwQGTLv8Uaugh6GLS6WSUVDEMsTjagu9fpwzXyZ A6LsgMoTBcuKg9i/90aAW5ennLo46DnD19wBrMacderXYs7IZLi56iD4izEWTCKFL+Ze QdWg== X-Forwarded-Encrypted: i=1; AJvYcCXEmvYpPNI4m/Z0RX2F2uX61FroT9llNeJk00z6OjK+a1pR51hBOl0pOPVN47a3tfTeq7r45Fl4XETVNSOqGEIFgsJlWeIIvdNaJQ38kQouIIm/Vmw= X-Gm-Message-State: AOJu0YxC1ky/M1nluxNag8XPSz0Mz5YApND1TNI5yr0YUEFE5GCKVcjT Xj0gPO/1LY/q8ol8bjtc/6OpExPUggg0gWZPNuY8nQkYi+BK6hGdEVZx3XYyBU6HWNCnNI7UBjk xlUtiGCVUcPbkImcP+W38sNY4+A== X-Google-Smtp-Source: AGHT+IFtCuGfAeqFdgjl+E/TRR4RFQ5mbVlens49Mh0ShuT1nuemqiHaz2jdZNbdKjru2453mCpm44BAccO7UGz0AYU= 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> 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240207_064921_170185_5E54295D X-CRM114-Status: GOOD ( 18.33 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 13548614 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 91C59C4828F for ; Wed, 7 Feb 2024 14:49:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=ntBv9f+yRSRF1rt8EDB5XBI+6yoMnggt/P7qgfIv9X4=; b=xkChdcgetBf2HPGs4YBDXxSIsi +HHLwjVwxnM68xBxoL2eSFyTPxw1XmeMdgswm142qkLMHZLkfpxIJ2HfJ/pHF7ZrUMOFvxKFbZnBz es1YLc5dghh1jyzjcwO0+su+KXDlqGiFgqAn3WpoScH+IXuAmYDdrp/lh6NMC1HmhSZASIsIhkRsD t3y4xsom8Vyru6LGAiyhURHZsr5EVTrb+dHmtDno984BVDAz35iWRan6Q/1VH+dM1sCfotInYhQDs at7mEVb94nNuSgu7oAoofEPljS5V4G4zT84SfQJtn89Uc27KEVqjhYijFAv3CuNvQeDRVI4kOtw/Z XOf0uhjg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rXjEw-0000000AxnO-4AwI; Wed, 07 Feb 2024 14:49:31 +0000 Received: from mail-wm1-x349.google.com ([2a00:1450:4864:20::349]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rXjEq-0000000AxjL-2iqo for linux-arm-kernel@lists.infradead.org; Wed, 07 Feb 2024 14:49:25 +0000 Received: by mail-wm1-x349.google.com with SMTP id 5b1f17b1804b1-40fb1690f94so3405055e9.0 for ; Wed, 07 Feb 2024 06:49:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707317362; x=1707922162; darn=lists.infradead.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=8tIUh8e7+u6fe/L9d2IoiyaWpJieVgDcIoakNbd8hrU=; b=ZcijRlmEPjelLzklaCQlgZ2jbfRr7kOI4P/tGf9alt/bDN0/juMIdmOTSdVhQADXsF ZXpCNMvPYLlaipNQb7+y5lUPcrKPc5fAIU8E7IRXx+6odFC2Zj5qCCSumWlhr20aCri8 63ObaL22jhgYeSszPrW2Y4EyUqUbf9F6+lFYShwW+CLwcuaEI9w3Mfuzp+btzbMCOVsN 1H5eRvKfRYfrifr5WujTc3RCJWndsqvGiediSiGNmqGqzDFn2q4nK/ypj0aJmAdPKE8X eiLW2yw0FgTX55Eno7yr+MB/sCUONFWNcCOmuvuvsedFjOsL3WtQGaDilBIFcAvnjWMo O20A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707317362; x=1707922162; 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=8tIUh8e7+u6fe/L9d2IoiyaWpJieVgDcIoakNbd8hrU=; b=qnmik4suQpLNVeJDkcbEvTspSIfmPPxMyDQQS+8NeQbOUuBzWwPK8WxoLFx26B8KGF MhFNsq0Iknw2ZiCdcXv1L2xkBYy6SQacDDoHfc+woZ2pEiDW5vyQHSd5aVQT1zZObd9l 4MtvqdN4uicOhLEZku2YksZzIaZ+/DvELet2LqrFcc00KOqKTI2AnSoUE1flRXgihrv3 u3l2O8J+AaGJWYYGvDZ3yWWEzGqpg0+yfiZC3m7ySy5FTkcSpYooidWi/iJ50Fn8LAC9 9x0m/Vfatf0y2XR9QoHw5V1qi2VOSAURarQPZTYrsqP/88gpV2phdNSsC2htR6GzTnlB 9WIw== X-Gm-Message-State: AOJu0YxTEyM5bs3/j5Ej9XEWZs0sxaiH0EhGO89e3/UQClzxjhUaxODv jmcs6UBzfDFhH0rG48RX5EIksmcCpkK5IU0gOLydzl4zfnOTJ4m/cZpf++OcLhWJGn41K4gnR7g AcCcJQQh3D97c9IzqA9+cBq//Jg== X-Google-Smtp-Source: AGHT+IHZWozJw6iEdHYJvbi8rYI97x/8VSLY+PehA27Gu1vNkYuY53+7wudnkk76X2VvuGrFo0tvrbQt62XM8gjTJX0= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a05:600c:518b:b0:40f:e205:9868 with SMTP id fa11-20020a05600c518b00b0040fe2059868mr158210wmb.0.1707317361885; Wed, 07 Feb 2024 06:49:21 -0800 (PST) Date: Wed, 7 Feb 2024 14:48:31 +0000 In-Reply-To: <20240207144832.1017815-2-sebastianene@google.com> 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-4-sebastianene@google.com> Subject: [PATCH v5 2/4] arm64: ptdump: Use the mask from the state structure 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240207_064924_740813_CAC53E2C X-CRM114-Status: GOOD ( 18.55 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Printing the descriptor attributes requires accessing a mask which has a different set of attributes for stage-2. In preparation for adding support for the stage-2 pagetables dumping, use the mask from the local context and not from the globally defined pg_level array. Store a pointer to the pg_level array in the ptdump state structure. This will allow us to extract the mask which is wrapped in the pg_level array and use it for descriptor parsing in the note_page. Signed-off-by: Sebastian Ene --- arch/arm64/include/asm/ptdump.h | 1 + arch/arm64/mm/ptdump.c | 13 ++++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/arch/arm64/include/asm/ptdump.h b/arch/arm64/include/asm/ptdump.h index 23510be35084..4e728d2a1f2c 100644 --- a/arch/arm64/include/asm/ptdump.h +++ b/arch/arm64/include/asm/ptdump.h @@ -45,6 +45,7 @@ struct pg_level { */ struct pg_state { struct ptdump_state ptdump; + struct pg_level *pg_level; struct seq_file *seq; const struct addr_marker *marker; unsigned long start_address; diff --git a/arch/arm64/mm/ptdump.c b/arch/arm64/mm/ptdump.c index 64127c70b109..015ed65d3e9b 100644 --- a/arch/arm64/mm/ptdump.c +++ b/arch/arm64/mm/ptdump.c @@ -220,11 +220,12 @@ 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); + struct pg_level *pg_info = st->pg_level; static const char units[] = "KMGTPE"; u64 prot = 0; if (level >= 0) - prot = val & pg_level[level].mask; + prot = val & pg_info[level].mask; if (st->level == -1) { st->level = level; @@ -250,10 +251,10 @@ void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, unit++; } pt_dump_seq_printf(st->seq, "%9lu%c %s", delta, *unit, - pg_level[st->level].name); - if (st->current_prot && pg_level[st->level].bits) - dump_prot(st, pg_level[st->level].bits, - pg_level[st->level].num); + pg_info[st->level].name); + if (st->current_prot && pg_info[st->level].bits) + dump_prot(st, pg_info[st->level].bits, + pg_info[st->level].num); pt_dump_seq_puts(st->seq, "\n"); if (addr >= st->marker[1].start_address) { @@ -284,6 +285,7 @@ void ptdump_walk(struct seq_file *s, struct ptdump_info *info) st = (struct pg_state){ .seq = s, .marker = info->markers, + .pg_level = &pg_level[0], .level = -1, .ptdump = { .note_page = note_page, @@ -321,6 +323,7 @@ void ptdump_check_wx(void) { 0, NULL}, { -1, NULL}, }, + .pg_level = &pg_level[0], .level = -1, .check_wx = true, .ptdump = { 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: 13548615 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B2740C4828D for ; Wed, 7 Feb 2024 14:49:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=qWaRq/UCEkMIQv9zQmUbHyYRJzduoUtEpSK7EsQ+1gI=; b=v+cdMUhg4BMs2ytT0kcUohWc8T ndWpAYwu41o9b1vg92X2AnfYMWNjjGSgE6R2tLgW2l49r63wea3Y1jnAa5QL2nPff9toa9Nm/X5nf FVN2kr370cgntqPv0MoDDm1D0beTwkg3s/+tNrwhsEbykufUGtATfgmZ2rzxsPizwnLjvu/vYuFvd 7K1QbTE9gJiba7rYAW5XMD0gdKuezKm28DHAiGOA8WUcfbnmPK3W+0lD9iGWa0GeWR2yivhlwsTPZ iJ2vIPFv6agah2W9zxgxNYvf5JjxyIlNY6nGCzqV/yRgL8MmeTOJRK5FMz/FlK66K0LWiLvZeY/I+ ZfsF2pPA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rXjEy-0000000Axow-37sX; Wed, 07 Feb 2024 14:49:32 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rXjEs-0000000Axjq-2eSZ for linux-arm-kernel@lists.infradead.org; Wed, 07 Feb 2024 14:49:28 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-dc64f63d768so1226723276.2 for ; Wed, 07 Feb 2024 06:49:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707317364; x=1707922164; darn=lists.infradead.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=wCYWiJCH1QR60h6Rc7c1nLAcVqF1u/E3rnQoA+/vd4VlSB3BmCXUMTQrDwWG9u59pS Z8clMBkc0DvctL/8gKWAm1CLv27biB3+0E/0DvDzfgRPfVznoKyim4k1fyW8rOCyRgg5 ui55F7kqHEw1in2ht0XdkesmFHdsJka1zqeNWQf0DWZY+ypAmcOqzy79js7R/16iEhM7 GPs4QnMKnHa9XdWl58fia/4PAKWoEQzVX7dADJaffAh3btdbMR7zHh79iq2y/VeHLh3d jokUvSGiHlA0U8VEF44dB3BW15LqWqGkjyV84b0BEdHg+SyRf9usmaSU+/VbwzQck1E8 4/uw== 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=Yzuo3jY6h2U8ktjr6vUfLLmiusEwgNMA7kXG/LR82zQ6LBRpaQ/nwI4V/0AmBKSKG8 Foah8ze4n4wA6I3uDk7I779AKZ2UAWHwFDEpShydCyaK+Y3TiKWSbTgyipjh4BrvpFed sDh7zD074S4yscQjvzZn0mRVQkOEjRITxp7yl4cHMrsJeT8CfREx7Ox5w+goVR5u+qqU yoVLkvYL3ul/JkcElrzr5f/mwp8GEsCGnp3Jv3e8aqwdNZR96MuvrNXENPILEhucp/5N iy561AbxkoU6plo2AiCgI13fdoCmSrAirrUEBUJIxpZ8eeGxWiueLNxz/ZYOH+ocehkG KDGw== X-Gm-Message-State: AOJu0Yxub7WlQRovvOSiUWKTQ9PM7frFhSBmOksWsLGTb7NLRqtNLUsW dYGhHBz7rRHGTcEZWvoSVlTMY6BjghtIl78Cdu/hlA9sYpcoVuFgY/RW5pEmcAV/1lmvzKbg2Sm pLqFsmLr+a/J5Wdw5nWVaA7M6Nw== X-Google-Smtp-Source: AGHT+IEC9mYi5t2mpH+RrbaTrZi6L4zjhcBIBP8lvg21KvfSYfviuojbe9O+9+vO64zGP+pdG6Z5ql7eX/H03dtdRo4= 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> 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240207_064926_716498_42D5F42A X-CRM114-Status: GOOD ( 23.05 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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: 13548616 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9436BC4828F for ; Wed, 7 Feb 2024 14:49:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=/thY6XdGVSxezgcpr1XQ0Ch+YHonljaTNHDn5s4Ik8I=; b=Ucv9iy+6Mhmx0F3aDcqPVpxNZ7 oicjsxAoHmznymWfp0eO3NwmMIW9ksmdsD6r5V1VdlO+c7BVRhz6fULHZUlwa5gFg2sK2v5+dMkM1 KQKcccVf+XGsbYH3CYoE2GGu68G+TuXWptcpVm3pIbo0AQL4JfYaRUd3Bkn75ksDS5OKUPzl8eew4 fd4Lzlcr+Nby4/k4jfMsFN2Jn/bAVlEN0KFFwrcrWpgXZpAoaShjl4tKgd7tVzixVZj5J78VvmJXi 2Z97xAP8B2isKgQiGlWZQq3ECS5WmlX4lj1k60DCHtIls1A3YgWtL9bSpIAhL0css5p4LjMJ0ojCO wmpe/qhw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rXjF6-0000000AxvE-3uKn; Wed, 07 Feb 2024 14:49:40 +0000 Received: from mail-wm1-x34a.google.com ([2a00:1450:4864:20::34a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rXjEv-0000000AxlN-29Ef for linux-arm-kernel@lists.infradead.org; Wed, 07 Feb 2024 14:49:30 +0000 Received: by mail-wm1-x34a.google.com with SMTP id 5b1f17b1804b1-40efad468a5so4258515e9.2 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=lists.infradead.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=gzmSa5eahs0IpjDLf8Re2JzrnAfIrNdgxd5BN5I2aTyOI1QTRS+tXpoGacjYg5GUP3 h/agyd9ib9xhynBid+xgELngY078HyFQ76V/XywRGUkB1UzCQHr5SOer3lXrMFqHuabb RjA1VW8yZUP7SfBmfaZe/pY/CJRc0CO2dzwFWC1YbDdS3SHZQ1z3Sal9rZcDR4XPlJ3w /gZvWQtIos9Ue+JGxXgCC7q3s9LG15TfTKk4ydumEZzxJmQt8f2d+Ox43H8n2Lt5P18L c03vUj9VB0RfGwJ4AumaQ1O/XZeEiXuzRNovAUA9MYMYhZCR76DWU0RvCPqrHWazCmat 7WLw== 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=TO4PB4bC8uGy7JyJA9Y6qoufYOymR0iSUHlv5/dNBDudrN4JPmH+fMxIYqWwKZU6Q3 OULcWx8FAa6cP5arCYv8qWfv8QlUTTdnj6mRCFrVqHsKwWGXski1Zkav6bA4gHpmIKC2 88iMhA1QyBM51zwghUknQ/cl5eqk8LEnQZL7lepW+AJAUCvg1XeaNpnw9XrOIm6Wg89P Kh6p5u6sPsD5NvrKOo9pgz4NStqnhNahwFPC8rR9xvSoyvlT1SY515KJ1FR/JwIIelFG K9jmLPO+ZDfKzz4bvjLReYS4FIY2QTq4WyZvN3Q7ilaF2e7237W9F/wCD2exErUI1R0w q7+w== X-Gm-Message-State: AOJu0YwfOqRWUOcw70sYdSvW5Sz9ASsV38WWEJHu2agtwabqBa7YdhNL GjFSOLvYOVZ1yTQ/qp9+aFHksHrvULctjkHT8cqn3gwdMcFa16pqAgBB0LK22ErL633uwxTvPB4 KenGW3vvbwwpse6sspgom1HV87Q== X-Google-Smtp-Source: AGHT+IGS0S84nWWPeysL+mFOFap3NjMcgT1vbRmpG1ZnvwANgcD7I9rDqiPadoprZ3xgO/+lEX4pEIHtW1Yj6kYgmrQ= 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> 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240207_064929_584382_9A417F21 X-CRM114-Status: GOOD ( 17.50 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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; }