From: Sebastian Ene <sebastianene@google.com>
To: will@kernel.org, Oliver Upton <oliver.upton@linux.dev>,
James Morse <james.morse@arm.com>,
Suzuki K Poulose <suzuki.poulose@arm.com>,
Zenghui Yu <yuzenghui@huawei.com>,
catalin.marinas@arm.com, mark.rutland@arm.com,
akpm@linux-foundation.org, maz@kernel.org
Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, kernel-team@android.com,
vdonnefort@google.com, qperret@google.com, smostafa@google.com,
Sebastian Ene <sebastianene@google.com>
Subject: [PATCH v3 07/10] arm64: ptdump: Parse the host stage-2 page-tables from the snapshot
Date: Wed, 15 Nov 2023 17:16:37 +0000 [thread overview]
Message-ID: <20231115171639.2852644-9-sebastianene@google.com> (raw)
In-Reply-To: <20231115171639.2852644-2-sebastianene@google.com>
Add a walker function which configures ptdump to parse the page-tables
from the snapshot. Define the attributes used by the stage-2 parser and
build a description of an array which holds the name of the level.
Walk the entire address space configured in the pagetable and parse the
attribute descriptors.
Signed-off-by: Sebastian Ene <sebastianene@google.com>
---
arch/arm64/mm/ptdump.c | 157 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 157 insertions(+)
diff --git a/arch/arm64/mm/ptdump.c b/arch/arm64/mm/ptdump.c
index 0b4cb54e43ff..9f88542d5312 100644
--- a/arch/arm64/mm/ptdump.c
+++ b/arch/arm64/mm/ptdump.c
@@ -86,6 +86,7 @@ struct pg_state {
bool check_wx;
unsigned long wx_pages;
unsigned long uxn_pages;
+ struct ptdump_info_file_priv *f_priv;
};
struct prot_bits {
@@ -174,6 +175,66 @@ static const struct prot_bits pte_bits[] = {
}
};
+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,
+ .val = KVM_PTE_LEAF_ATTR_HI_S2_XN,
+ .set = "XN",
+ .clear = " ",
+ }, {
+ .mask = KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R,
+ .val = KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R,
+ .set = "R",
+ .clear = " ",
+ }, {
+ .mask = KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W,
+ .val = KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W,
+ .set = "W",
+ .clear = " ",
+ }, {
+ .mask = KVM_PTE_LEAF_ATTR_LO_S2_AF,
+ .val = KVM_PTE_LEAF_ATTR_LO_S2_AF,
+ .set = "AF",
+ .clear = " ",
+ }, {
+ .mask = PTE_NG,
+ .val = PTE_NG,
+ .set = "FnXS",
+ .clear = " ",
+ }, {
+ .mask = PTE_CONT,
+ .val = PTE_CONT,
+ .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",
+ },
+};
+
struct pg_level {
const struct prot_bits *bits;
const char *name;
@@ -286,6 +347,7 @@ static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level,
delta >>= 10;
unit++;
}
+
pt_dump_seq_printf(st->seq, "%9lu%c %s", delta, *unit,
pg_info[st->level].name);
if (st->current_prot && pg_info[st->level].bits)
@@ -394,6 +456,11 @@ static void *ptdump_host_va(phys_addr_t phys)
return __va(phys);
}
+static struct kvm_pgtable_mm_ops host_mmops = {
+ .phys_to_virt = ptdump_host_va,
+ .virt_to_phys = ptdump_host_pa,
+};
+
static size_t stage2_get_pgd_len(void)
{
u64 mmfr0, mmfr1, vtcr;
@@ -528,6 +595,95 @@ static void stage2_ptdump_end_walk(void *file_priv)
f_priv->file_priv = NULL;
}
+static int stage2_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;
+
+ pt_st->note_page(pt_st, ctx->addr, ctx->level, ctx->old);
+
+ return 0;
+}
+
+static void stage2_ptdump_build_levels(struct pg_level *level,
+ size_t num_levels,
+ unsigned int start_level)
+{
+ static const char * const lvl_names[] = {"PGD", "PUD", "PMD", "PTE"};
+ int i, j, name_index;
+
+ if (num_levels > KVM_PGTABLE_MAX_LEVELS && start_level > 2) {
+ pr_warn("invalid configuration %lu levels start_lvl %u\n",
+ num_levels, start_level);
+ return;
+ }
+
+ for (i = start_level; i < num_levels; i++) {
+ name_index = i - start_level;
+ name_index = name_index * start_level + name_index;
+
+ level[i].name = lvl_names[name_index];
+ level[i].num = ARRAY_SIZE(stage2_pte_bits);
+ level[i].bits = stage2_pte_bits;
+
+ for (j = 0; j < level[i].num; j++)
+ level[i].mask |= level[i].bits[j].mask;
+ }
+}
+
+static void stage2_ptdump_walk(struct seq_file *s, struct ptdump_info *info)
+{
+ struct ptdump_info_file_priv *f_priv =
+ container_of(info, struct ptdump_info_file_priv, info);
+ struct kvm_pgtable_snapshot *snapshot = f_priv->file_priv;
+ struct pg_state st;
+ struct kvm_pgtable *pgtable;
+ u64 start_ipa = 0, end_ipa;
+ struct addr_marker ipa_address_markers[3];
+ struct pg_level stage2_pg_level[KVM_PGTABLE_MAX_LEVELS] = {0};
+ struct kvm_pgtable_walker walker = (struct kvm_pgtable_walker) {
+ .cb = stage2_ptdump_visitor,
+ .arg = &st,
+ .flags = KVM_PGTABLE_WALK_LEAF,
+ };
+
+ if (snapshot == NULL || !snapshot->pgtable.pgd)
+ return;
+
+ pgtable = &snapshot->pgtable;
+ pgtable->mm_ops = &host_mmops;
+ end_ipa = BIT(pgtable->ia_bits) - 1;
+
+ memset(&ipa_address_markers[0], 0, sizeof(ipa_address_markers));
+
+ ipa_address_markers[0].start_address = start_ipa;
+ ipa_address_markers[0].name = "IPA start";
+
+ ipa_address_markers[1].start_address = end_ipa;
+ ipa_address_markers[1].name = "IPA end";
+
+ stage2_ptdump_build_levels(stage2_pg_level, KVM_PGTABLE_MAX_LEVELS,
+ pgtable->start_level);
+
+ st = (struct pg_state) {
+ .seq = s,
+ .marker = &ipa_address_markers[0],
+ .level = -1,
+ .pg_level = &stage2_pg_level[0],
+ .f_priv = f_priv,
+ .ptdump = {
+ .note_page = note_page,
+ .range = (struct ptdump_range[]) {
+ {start_ipa, end_ipa},
+ {0, 0},
+ },
+ },
+ };
+
+ kvm_pgtable_walk(pgtable, start_ipa, end_ipa, &walker);
+}
+
void ptdump_register_host_stage2(void)
{
if (!is_protected_kvm_enabled())
@@ -537,6 +693,7 @@ void ptdump_register_host_stage2(void)
.mc_len = host_s2_pgtable_pages(),
.ptdump_prepare_walk = stage2_ptdump_prepare_walk,
.ptdump_end_walk = stage2_ptdump_end_walk,
+ .ptdump_walk = stage2_ptdump_walk,
};
ptdump_debugfs_kvm_register(&stage2_kernel_ptdump_info,
--
2.43.0.rc0.421.g78406f8d94-goog
next prev parent reply other threads:[~2023-11-15 17:17 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-15 17:16 [PATCH v3 00/10] arm64: ptdump: View the second stage page-tables Sebastian Ene
2023-11-15 17:16 ` [PATCH v3 01/10] KVM: arm64: Add snap shooting the host stage-2 pagetables Sebastian Ene
2023-11-21 14:12 ` Vincent Donnefort
2023-11-23 14:40 ` Sebastian Ene
2023-11-15 17:16 ` [PATCH v3 02/10] arm64: ptdump: Use the mask from the state structure Sebastian Ene
2023-11-15 17:16 ` [PATCH v3 03/10] arm64: ptdump: Add the walker function to the ptdump info structure Sebastian Ene
2023-11-15 17:16 ` [PATCH v3 04/10] KVM: arm64: Move pagetable definitions to common header Sebastian Ene
2023-11-15 17:16 ` [PATCH v3 05/10] arm64: ptdump: Add hooks on debugfs file operations Sebastian Ene
2023-11-22 14:36 ` Vincent Donnefort
2023-11-15 17:16 ` [PATCH v3 06/10] arm64: ptdump: Register a debugfs entry for the host stage-2 tables Sebastian Ene
2023-11-21 17:13 ` Vincent Donnefort
2023-11-23 14:48 ` Sebastian Ene
2023-11-15 17:16 ` Sebastian Ene [this message]
2023-11-15 21:57 ` [PATCH v3 07/10] arm64: ptdump: Parse the host stage-2 page-tables from the snapshot kernel test robot
2023-11-18 22:39 ` kernel test robot
2023-11-15 17:16 ` [PATCH v3 08/10] arm64: ptdump: Interpret memory attributes based on runtime configuration Sebastian Ene
2023-11-15 17:16 ` [PATCH v3 09/10] arm64: ptdump: Interpret pKVM ownership annotations Sebastian Ene
2023-11-15 17:16 ` [PATCH v3 10/10] arm64: ptdump: Add support for guest stage-2 pagetables dumping Sebastian Ene
2023-11-22 23:35 ` Oliver Upton
2023-11-23 10:58 ` Sebastian Ene
2023-11-22 23:18 ` [PATCH v3 00/10] arm64: ptdump: View the second stage page-tables Oliver Upton
2023-11-23 9:49 ` Sebastian Ene
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231115171639.2852644-9-sebastianene@google.com \
--to=sebastianene@google.com \
--cc=akpm@linux-foundation.org \
--cc=catalin.marinas@arm.com \
--cc=james.morse@arm.com \
--cc=kernel-team@android.com \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=maz@kernel.org \
--cc=oliver.upton@linux.dev \
--cc=qperret@google.com \
--cc=smostafa@google.com \
--cc=suzuki.poulose@arm.com \
--cc=vdonnefort@google.com \
--cc=will@kernel.org \
--cc=yuzenghui@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®