From patchwork Wed Nov 15 17:16:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 13457139 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 24B70C2BB3F for ; Wed, 15 Nov 2023 17:17:48 +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=K1LTAITZ1jATOBDnjj20qIPjPeTn1G4sigYS9fa1AWI=; b=NpsixC/OeDxKfiaYsYyZSK9fNz Y8qK4GUIhIMwtAiYxBrbyJ4g5h4oenUws8Tem1vmJgZ4GjKLvDV7MOyke3PrkpG83uV72EWu128/b ejf0Ta6rREd5U0jh4Z4pUQphFQAnoMmjK/dBskVCID8r5kn10MbgYuhMo/lRWuhJP5xHqkDvFt7IG 1xcMYK3JGN0RlyKNoN0X3vuysbdKN6Arh/ebf/V4wJ5P/LOVaDI2H3l7AAqnYbO6pujdoAPYVUTc5 gbma+jjRIONikM+ZrmRwHlSIgw+/3ByT5QUH3xVFAnV03v8718tNYzoTDGEwf+DvQhc/xQswNn3fe IUUhaDrg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r3JVy-001Pdl-28; Wed, 15 Nov 2023 17:17:22 +0000 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1r3JVp-001PWc-1K for linux-arm-kernel@lists.infradead.org; Wed, 15 Nov 2023 17:17:15 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5bc3be6a91bso8956347b3.1 for ; Wed, 15 Nov 2023 09:17:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1700068630; x=1700673430; 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=zCmrW40F76i5JztVQV1SbSI81TwYB1s24Z+PnC9WnV4=; b=QLso3DkwanHJZRn6XDoSmcC76rxP7wrRooIvChCAGt6mqXJhAfjyuiuIkSyI+DbflG bxFZY/uVKHi/MRVrW6hZLEFRK7XzUmMv0QaneQkcYMXYTSZ5+HG/55tPBfnnWZZXSSMg dYYBZXbQOJR9SezrpjNJN+/Uvuzu7HOPVw90HdJYqlySr76HOzxDNFW1t4NPfW0U1XXA QiQ79JPaUkbdJgLMNAxuU5UWJCv3BtmrZXzoNJ1wdlBojYgiDjloIe7tN/GKIAqxcvVv xPLzoh9cJjZJ9CBbefRHJcgGcrOvTmhh224//F+E27Ui/2qwS2IoacFvzdVeWPVgso6Y edxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700068630; x=1700673430; 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=zCmrW40F76i5JztVQV1SbSI81TwYB1s24Z+PnC9WnV4=; b=neWxi7KKHDcx/PKjlKb8jrj2iHube5AKWXE3N49rbtUvWftZc3R2jBecbL/YgVrDIF 1PkCwHQKnGBIcE6P5sqHFLf0weYROcjhffJXfN63ocD0Un1Ww6Fh+tjRaXfd3xk68d1C uPUVxDqPLXsW27J33cyLZLiXdj8enk6bqVyqlcWpHUXUQ3FaR4noKXkVo4pBdT32/qaw JM0SuNlHgpeI5XrDuF8QfQ0HjFAttpc10tjqqYccBnAAZrCFrlV5P9wnTWygERCEQEp5 qregOwvDCurOzT90skOcn971rW910cJBRwQgJSJYoUUJ7NpLSTZN8CVD6lJOahq8wuaa SWkw== X-Gm-Message-State: AOJu0YzoN71BMDMVRiqVvx1T9pmCy0+m+gUy1ebbPHTxXW+kiQ0NEddc 7WGeEnf17Y1P7F+NbSyuoTRRs5dkysYhQ5EC7iU= X-Google-Smtp-Source: AGHT+IELfUQoq3/g4kbIGiBRU7VsMlUAs35EiaE420fomxFsRN5Mm1Zx4hWdYOb+MaGSa4SgHhjdvgBzYWcJ8schaZk= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a0d:c046:0:b0:5be:9742:cc3e with SMTP id b67-20020a0dc046000000b005be9742cc3emr200344ywd.2.1700068630474; Wed, 15 Nov 2023 09:17:10 -0800 (PST) Date: Wed, 15 Nov 2023 17:16:37 +0000 In-Reply-To: <20231115171639.2852644-2-sebastianene@google.com> Mime-Version: 1.0 References: <20231115171639.2852644-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.rc0.421.g78406f8d94-goog Message-ID: <20231115171639.2852644-9-sebastianene@google.com> Subject: [PATCH v3 07/10] arm64: ptdump: Parse the host stage-2 page-tables from the snapshot From: Sebastian Ene To: will@kernel.org, Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231115_091713_454016_9D2B239F X-CRM114-Status: GOOD ( 18.51 ) 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 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 --- 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,