From patchwork Mon Dec 18 13:58:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 13497028 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 6C9AEC35274 for ; Mon, 18 Dec 2023 14:00: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=eHc2+1y4fG4HJ/I9SL7gv8LJOoeqvvyWx2RAlOWmqcc=; b=Rxn/fwY2ImIEiQY8urUFMPcDh3 h0v2iVoNJTCVNKW4podI33yuAqaX5RsnXZvHos9BGSP3xWDy627ZD1sCCpVAFWf4J1R8Guwf3A7ET M44TCSjLarWbw2ex9XpkvHqWG8zxhQOn3k5OL6uUr3ffQXNwaVY92/fQqQK4XCZXL+OtwcYV99gGq YlhqmfSl4rl5WExZ/bPuQi69CMlIaiyerdnmAAQPZ9Gl5B5NkTPApmp7fUKdecXfDiv1L6FW6RBh7 7il1L8aWEVHADwBznJOMBfFp8MAHP3bFIudvtne3DL+qagtj9gTRNzOk+xnaxrfq9cOUJ624tCsYw kXxCzWyw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rFEA4-00AweQ-0W; Mon, 18 Dec 2023 14:00:00 +0000 Received: from mail-wm1-x34a.google.com ([2a00:1450:4864:20::34a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rFE9u-00AwZq-1t for linux-arm-kernel@lists.infradead.org; Mon, 18 Dec 2023 13:59:52 +0000 Received: by mail-wm1-x34a.google.com with SMTP id 5b1f17b1804b1-40c35d1d776so28146845e9.3 for ; Mon, 18 Dec 2023 05:59:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702907989; x=1703512789; 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=6k7cehEzikJK7Gdbm4Te2RqGLAqH3crGDKksVzZAStI=; b=RuVq0HPukbn4BIF/QwYetuDV8KsnSVfXA+ipb9+WyqF43ZqegIOAOqNvgYT9yGea1w gj6W5Rypy92ImCGrYDRc/M7DyN8F5InfIlF+y6isXB11gxfq5heDBt70fwSuYw/uBL62 eEsX4YnARnTYl0TxMgKcBS5r1FN3nJuOyJPlxZna8AdQj8sfQvEqlgksxmypDWUMlds9 xodAiGe30CysT7Gzal/mgmO8VuWRklMO+PuJXamLtgjO9PArlz58XWhTfIOiPj91rgU3 p4R3NSOCQBhjEqmnwXaEwHqrsG678AFmdNhakUIfAyhdA6DtwUPJylrEOqpePRrgxjEh JPqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702907989; x=1703512789; 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=6k7cehEzikJK7Gdbm4Te2RqGLAqH3crGDKksVzZAStI=; b=gUmSBQW5XtVkOnWEII2oBVZ1UXouSFYTVZXt7HFWzb4Vu3yI5lJM1ksYWefKyp03XJ zT3HxVacgORzsj986zPIuXvC1zuHl4RLgxmHYRzdyhJlgf+dEvFuXUYQv7D24zeb9zDS J9e1AholW3vjwLBnMqTF3QUnpJJ9iIwQFYwDYBlqF7E627jdO4jKMzDZK/qcDodEdLdU zceSwTbXGJaN8zBhTc1CpNBe4uqckOYDzwlLAB29c8CH4vGP0VCj3kcv6quqqEJN/p02 ByeVM1pF9bUa75E3ujCrTuDK6ePGfcXGR0NYWeFHTdILYXgMzsHahHdJEyo9lswPu4UB Bf0g== X-Gm-Message-State: AOJu0YzRIPjFyVXE77KupVbTaVkTKwES2HkHEQSo4GD9O2WRv3JgSa30 P1PbDMFt0rn5m/oobOY/iOu/WW4CRBsKI9j4i3Y= X-Google-Smtp-Source: AGHT+IEFl7UeJkAWNGeuMCf/woxCmNRLzb6+Y/KXy9tkOKGuWrtl+ZOKH9Agd0lSddYxe/N6Z/QJtu8jdu20gfmQMmA= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a05:600c:1c07:b0:40b:2c9e:e144 with SMTP id j7-20020a05600c1c0700b0040b2c9ee144mr333270wms.1.1702907989006; Mon, 18 Dec 2023 05:59:49 -0800 (PST) Date: Mon, 18 Dec 2023 13:58:51 +0000 In-Reply-To: <20231218135859.2513568-2-sebastianene@google.com> Mime-Version: 1.0 References: <20231218135859.2513568-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231218135859.2513568-3-sebastianene@google.com> Subject: [PATCH v4 01/10] KVM: arm64: Add snapshot interface for the host stage-2 pagetable 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-20231218_055950_625135_DCAF0524 X-CRM114-Status: GOOD ( 28.00 ) 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 Introduce a new hvc that allows the pKVM hypervisor to snapshot the host stage-2 pagetables. The caller is expected to allocate & free the memory for the pagetable snapshot. The below diagram shows the sequence of events. [--- HOST ---] (1) allocate memory for the snapshot (2) invoke the __pkvm_copy_host_stage2(snapshot) interface |____ [--- HYP ---] (3) donate the snapshot from HOST -> HYP (4) save the host stage-2 pagetables in the snapshot (5) donate the snapshot from HYP -> HOST [--- return from HYP ---] [--- HOST ---] (6) free the memory for the snapshot When the host kernel invokes this interface, the memory is donated from the host to the hypervisor. By doing this the memory is unmapped from the host's translation regime to prevent the host from modifying the buffer while the hypervisor uses it. The hypervisor walks the host stage-2 pagetable copy while re-creating the original mappings. When the copying is done, the memory is donated from the hypervisor back to the host. The pages that have been used from the memcache are donated back to the host from a temporary array. The hvc is executing synchronously and the preemption is disabled for the current CPU while running inside the hypervisor so calling this interface blocks the current CPU until the snapshoting is done. Signed-off-by: Sebastian Ene --- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/include/asm/kvm_pgtable.h | 43 ++++ arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 1 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 12 ++ arch/arm64/kvm/hyp/nvhe/mem_protect.c | 186 ++++++++++++++++++ arch/arm64/kvm/hyp/pgtable.c | 43 ++++ 6 files changed, 286 insertions(+) diff --git a/arch/arm64/include/asm/kvm_asm.h b/arch/arm64/include/asm/kvm_asm.h index 24b5e6b23..9df3367d8 100644 --- a/arch/arm64/include/asm/kvm_asm.h +++ b/arch/arm64/include/asm/kvm_asm.h @@ -81,6 +81,7 @@ enum __kvm_host_smccc_func { __KVM_HOST_SMCCC_FUNC___pkvm_init_vm, __KVM_HOST_SMCCC_FUNC___pkvm_init_vcpu, __KVM_HOST_SMCCC_FUNC___pkvm_teardown_vm, + __KVM_HOST_SMCCC_FUNC___pkvm_host_stage2_snapshot, }; #define DECLARE_KVM_VHE_SYM(sym) extern char sym[] diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h index d3e354bb8..f73efd8a8 100644 --- a/arch/arm64/include/asm/kvm_pgtable.h +++ b/arch/arm64/include/asm/kvm_pgtable.h @@ -10,6 +10,7 @@ #include #include #include +#include #define KVM_PGTABLE_MAX_LEVELS 4U @@ -351,6 +352,28 @@ struct kvm_pgtable { kvm_pgtable_force_pte_cb_t force_pte_cb; }; +/** + * struct kvm_pgtable_snapshot - Snapshot page-table wrapper. + * @pgtable: The page-table configuration. + * @mc: Memcache used for pagetable pages allocation. + * @pgd_hva: Host virtual address of a physically contiguous buffer + * used for storing the PGD. + * @pgd_pages: The size of the phyisically contiguous buffer in pages. + * @used_pages_hva: Host virtual address of a physically contiguous buffer + * used for storing the consumed pages from the memcache. + * @num_used_pages The size of the used buffer in pages. + * @used_pages_indx The current index of the used pages array. + */ +struct kvm_pgtable_snapshot { + struct kvm_pgtable pgtable; + struct kvm_hyp_memcache mc; + void *pgd_hva; + size_t pgd_pages; + phys_addr_t *used_pages_hva; + size_t num_used_pages; + size_t used_pages_indx; +}; + /** * kvm_pgtable_hyp_init() - Initialise a hypervisor stage-1 page-table. * @pgt: Uninitialised page-table structure to initialise. @@ -756,4 +779,24 @@ enum kvm_pgtable_prot kvm_pgtable_hyp_pte_prot(kvm_pte_t pte); */ void kvm_tlb_flush_vmid_range(struct kvm_s2_mmu *mmu, phys_addr_t addr, size_t size); + +#ifdef CONFIG_NVHE_EL2_DEBUG +/** + * kvm_pgtable_stage2_snapshot() - Given a memcache and a destination + * pagetable where we have the original PGD + * copied, build a snapshot table with page table + * pages from the given memcache. + * + * @to_pgt: Destination pagetable + * @mc: The memcache where we allocate the destination pagetables from + */ +int kvm_pgtable_stage2_snapshot(struct kvm_pgtable *to_pgt, + void *mc); +#else +static inline int kvm_pgtable_stage2_snapshot(struct kvm_pgtable *to_pgt, + void *mc) +{ + return -EPERM; +} +#endif /* CONFIG_NVHE_EL2_DEBUG */ #endif /* __ARM64_KVM_PGTABLE_H__ */ diff --git a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h index 0972faccc..ca8f76915 100644 --- a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +++ b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h @@ -69,6 +69,7 @@ int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages); int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages); int __pkvm_host_share_ffa(u64 pfn, u64 nr_pages); int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages); +int __pkvm_host_stage2_snapshot(struct kvm_pgtable_snapshot *snapshot); bool addr_is_memory(phys_addr_t phys); int host_stage2_idmap_locked(phys_addr_t addr, u64 size, enum kvm_pgtable_prot prot); diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c index 2385fd03e..7b215245f 100644 --- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c +++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c @@ -314,6 +314,17 @@ static void handle___pkvm_teardown_vm(struct kvm_cpu_context *host_ctxt) cpu_reg(host_ctxt, 1) = __pkvm_teardown_vm(handle); } +static void handle___pkvm_host_stage2_snapshot(struct kvm_cpu_context *host_ctxt) +{ +#ifdef CONFIG_NVHE_EL2_DEBUG + DECLARE_REG(struct kvm_pgtable_snapshot *, snapshot_hva, host_ctxt, 1); + + cpu_reg(host_ctxt, 1) = __pkvm_host_stage2_snapshot(snapshot_hva); +#else + cpu_reg(host_ctxt, 0) = SMCCC_RET_NOT_SUPPORTED; +#endif +} + typedef void (*hcall_t)(struct kvm_cpu_context *); #define HANDLE_FUNC(x) [__KVM_HOST_SMCCC_FUNC_##x] = (hcall_t)handle_##x @@ -348,6 +359,7 @@ static const hcall_t host_hcall[] = { HANDLE_FUNC(__pkvm_init_vm), HANDLE_FUNC(__pkvm_init_vcpu), HANDLE_FUNC(__pkvm_teardown_vm), + HANDLE_FUNC(__pkvm_host_stage2_snapshot), }; static void handle_host_hcall(struct kvm_cpu_context *host_ctxt) diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c index 8d0a5834e..aaf07f9e1 100644 --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c @@ -266,6 +266,192 @@ int kvm_guest_prepare_stage2(struct pkvm_hyp_vm *vm, void *pgd) return 0; } +#ifdef CONFIG_NVHE_EL2_DEBUG +static void *snap_zalloc_page(void *mc) +{ + struct hyp_page *p; + void *addr; + struct kvm_pgtable_snapshot *snap; + phys_addr_t *used_pg; + + snap = container_of(mc, struct kvm_pgtable_snapshot, mc); + used_pg = kern_hyp_va(snap->used_pages_hva); + + /* Check if we have space to track the used page */ + if (snap->used_pages_indx * sizeof(phys_addr_t) > snap->num_used_pages * PAGE_SIZE) + return NULL; + + addr = pop_hyp_memcache(mc, hyp_phys_to_virt); + if (!addr) + return addr; + used_pg[snap->used_pages_indx++] = hyp_virt_to_phys(addr); + + memset(addr, 0, PAGE_SIZE); + p = hyp_virt_to_page(addr); + memset(p, 0, sizeof(*p)); + + return addr; +} + +static void snap_free_pages_exact(void *addr, unsigned long size) +{ + u8 order = get_order(size); + unsigned int i; + struct hyp_page *p; + + for (i = 0; i < (1 << order); i++) { + p = hyp_virt_to_page(addr + (i * PAGE_SIZE)); + hyp_page_ref_dec(p); + } +} + +static void *pkvm_setup_snapshot(struct kvm_pgtable_snapshot *snap_hva) +{ + unsigned long i; + void *pgd, *used_pg; + phys_addr_t mc_page, next_mc_page; + struct kvm_pgtable_snapshot *snap; + + snap = (void *)kern_hyp_va(snap_hva); + if (!PAGE_ALIGNED(snap)) + return NULL; + + if (__pkvm_host_donate_hyp(hyp_virt_to_pfn(snap), 1)) + return NULL; + + pgd = kern_hyp_va(snap->pgd_hva); + if (!PAGE_ALIGNED(pgd)) + goto error_with_snapshot; + + if (__pkvm_host_donate_hyp(hyp_virt_to_pfn(pgd), snap->pgd_pages)) + goto error_with_snapshot; + + mc_page = snap->mc.head; + for (i = 0; i < snap->mc.nr_pages; i++) { + if (!PAGE_ALIGNED(mc_page)) + goto error_with_memcache; + + if (__pkvm_host_donate_hyp(hyp_phys_to_pfn(mc_page), 1)) + goto error_with_memcache; + + mc_page = *((phys_addr_t *)hyp_phys_to_virt(mc_page)); + } + + used_pg = kern_hyp_va(snap->used_pages_hva); + if (!PAGE_ALIGNED(used_pg)) + goto error_with_memcache; + + if (__pkvm_host_donate_hyp(hyp_virt_to_pfn(used_pg), snap->num_used_pages)) + goto error_with_memcache; + + return snap; +error_with_memcache: + mc_page = snap->mc.head; + for (; i >= 0; i--) { + next_mc_page = *((phys_addr_t *)hyp_phys_to_virt(mc_page)); + WARN_ON(__pkvm_hyp_donate_host(hyp_phys_to_pfn(mc_page), 1)); + mc_page = next_mc_page; + } + + WARN_ON(__pkvm_hyp_donate_host(hyp_virt_to_pfn(pgd), snap->pgd_pages)); +error_with_snapshot: + WARN_ON(__pkvm_hyp_donate_host(hyp_virt_to_pfn(snap), 1)); + return NULL; +} + +static void pkvm_teardown_snapshot(struct kvm_pgtable_snapshot *snap) +{ + size_t i; + phys_addr_t mc_page, next_mc_page; + u64 *used_pg = kern_hyp_va(snap->used_pages_hva); + void *pgd = kern_hyp_va(snap->pgd_hva); + + for (i = 0; i < snap->used_pages_indx; i++) { + mc_page = used_pg[i]; + WARN_ON(__pkvm_hyp_donate_host(hyp_phys_to_pfn(mc_page), 1)); + } + + WARN_ON(__pkvm_hyp_donate_host(hyp_virt_to_pfn(used_pg), + snap->num_used_pages)); + + mc_page = snap->mc.head; + for (i = 0; i < snap->mc.nr_pages; i++) { + next_mc_page = *((phys_addr_t *)hyp_phys_to_virt(mc_page)); + WARN_ON(__pkvm_hyp_donate_host(hyp_phys_to_pfn(mc_page), 1)); + mc_page = next_mc_page; + } + + snap->pgtable.mm_ops = NULL; + WARN_ON(__pkvm_hyp_donate_host(hyp_virt_to_pfn(pgd), snap->pgd_pages)); + WARN_ON(__pkvm_hyp_donate_host(hyp_virt_to_pfn(snap), 1)); +} + +static int pkvm_host_stage2_snapshot(struct kvm_pgtable_snapshot *snap) +{ + int ret; + void *pgd; + size_t required_pgd_len; + struct kvm_pgtable_mm_ops mm_ops = {0}; + struct kvm_s2_mmu *mmu; + struct kvm_pgtable *to_pgt, *from_pgt; + + if (snap->used_pages_indx != 0) + return -EINVAL; + + from_pgt = &host_mmu.pgt; + mmu = &host_mmu.arch.mmu; + required_pgd_len = kvm_pgtable_stage2_pgd_size(mmu->vtcr); + if (snap->pgd_pages < (required_pgd_len >> PAGE_SHIFT)) + return -ENOMEM; + + if (snap->mc.nr_pages < host_s2_pgtable_pages()) + return -ENOMEM; + + to_pgt = &snap->pgtable; + pgd = kern_hyp_va(snap->pgd_hva); + + mm_ops.zalloc_page = snap_zalloc_page; + mm_ops.free_pages_exact = snap_free_pages_exact; + mm_ops.phys_to_virt = hyp_phys_to_virt; + mm_ops.virt_to_phys = hyp_virt_to_phys; + mm_ops.page_count = hyp_page_count; + + to_pgt->ia_bits = from_pgt->ia_bits; + to_pgt->start_level = from_pgt->start_level; + to_pgt->flags = from_pgt->flags; + to_pgt->mm_ops = &mm_ops; + + host_lock_component(); + + to_pgt->pgd = pgd; + memcpy(to_pgt->pgd, from_pgt->pgd, required_pgd_len); + ret = kvm_pgtable_stage2_snapshot(to_pgt, &snap->mc); + + host_unlock_component(); + + return ret; +} + +int __pkvm_host_stage2_snapshot(struct kvm_pgtable_snapshot *snap_hva) +{ + int ret; + struct kvm_pgtable_snapshot *snap; + kvm_pteref_t pgd; + + snap = pkvm_setup_snapshot(snap_hva); + if (!snap) + return -EPERM; + + ret = pkvm_host_stage2_snapshot(snap); + if (!ret) { + pgd = snap->pgtable.pgd; + snap->pgtable.pgd = (kvm_pteref_t)__hyp_pa(pgd); + } + pkvm_teardown_snapshot(snap); + return ret; +} +#endif /* CONFIG_NVHE_EL2_DEBUG */ + void reclaim_guest_pages(struct pkvm_hyp_vm *vm, struct kvm_hyp_memcache *mc) { void *addr; diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c index 1966fdee7..82fef9620 100644 --- a/arch/arm64/kvm/hyp/pgtable.c +++ b/arch/arm64/kvm/hyp/pgtable.c @@ -1598,3 +1598,46 @@ void kvm_pgtable_stage2_free_unlinked(struct kvm_pgtable_mm_ops *mm_ops, void *p WARN_ON(mm_ops->page_count(pgtable) != 1); mm_ops->put_page(pgtable); } + +#ifdef CONFIG_NVHE_EL2_DEBUG +static int snapshot_walker(const struct kvm_pgtable_visit_ctx *ctx, + enum kvm_pgtable_walk_flags visit) +{ + struct kvm_pgtable_mm_ops *mm_ops = ctx->mm_ops; + void *copy_table, *original_addr; + kvm_pte_t new = ctx->old; + + if (!stage2_pte_is_counted(ctx->old)) + return 0; + + if (kvm_pte_table(ctx->old, ctx->level)) { + copy_table = mm_ops->zalloc_page(ctx->arg); + if (!copy_table) + return -ENOMEM; + + original_addr = kvm_pte_follow(ctx->old, mm_ops); + + memcpy(copy_table, original_addr, PAGE_SIZE); + new = kvm_init_table_pte(copy_table, mm_ops); + } + + *ctx->ptep = new; + + return 0; +} + +int kvm_pgtable_stage2_snapshot(struct kvm_pgtable *to_pgt, void *mc) +{ + struct kvm_pgtable_walker walker = { + .cb = snapshot_walker, + .flags = KVM_PGTABLE_WALK_LEAF | + KVM_PGTABLE_WALK_TABLE_PRE, + .arg = mc + }; + + if (!to_pgt->pgd) + return -ENOMEM; + + return kvm_pgtable_walk(to_pgt, 0, BIT(to_pgt->ia_bits), &walker); +} +#endif /* CONFIG_NVHE_EL2_DEBUG */ From patchwork Mon Dec 18 13:58:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 13497030 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 32E59C35274 for ; Mon, 18 Dec 2023 14:00:34 +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=ZpD5azldRq88kNp6wUx7EF98bW1YKv8XW7zhBAKwk2M=; b=azibDWdljF95Pib3AlY5TaYQXi aLU27eNuXPMIO8j3491cGyNhKZYm1TifKTXEsSvzS489skK6xbf/wewtHb7D7RX/ae0uAswBgPrpB MaTJmD32rrMrsSRHME9c2aBqVPzhzQ42ggT7aShb/iMA/79bUbwbb7o4y6GBbCitA8VLq3D8l5Fz7 hNs7cESXffgGPD6UYHekhCiemeJi7ZqH+97s9f1b3FBE0SznioBfvcFCKaLKYtUlHkyL+OPFovJ/o 05MDPz+H37imnSttjbLELBmpBkx8FPRuEo0nILQjqL341ZbDou1ozK4w8/2PKOE+mv0xym6SjdkwP LHgezuAQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rFEA5-00Awf0-0D; Mon, 18 Dec 2023 14:00:01 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rFE9w-00Awae-2u for linux-arm-kernel@lists.infradead.org; Mon, 18 Dec 2023 13:59:54 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5ca26c07848so38556517b3.0 for ; Mon, 18 Dec 2023 05:59:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702907991; x=1703512791; 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=gY15YxDdKSZ0D8Iy0CW6yGr0u1mIBXiISHgw6mfVrxM=; b=J578pr2t9hK9RSde3iPYeMDYnvg825mBn7hRjSI4Gwrpa3A7TyEUazH6xA+HfKTm74 46JwPza+9MZ6qMmMuQl6uWxSJl8o2C9jgtdjJASNfHilOm9q0F7yBA4MNpd2lOGikfBO rIIVuynB3+YyulbnFu0u5+S6Yk1tLaVyALSUBVnASnj8Y8xfyh3eJju6uL8filwThX/3 X+QJPSdD2QqCI4fJnhFJ9e9XuSSMEvDhWGYFnSICedyfti4dwJNOjBTJzJdCe8/rj7F/ M86DFQDPtrSV9lOLXip+I9lLRd14c9/Pb9BdSdBYzXrcZs72vaFCs7VqMMJRebk/JVUZ cXDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702907991; x=1703512791; 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=gY15YxDdKSZ0D8Iy0CW6yGr0u1mIBXiISHgw6mfVrxM=; b=GHbig57eaWLUWYIu7b0zslXir1FvhfetAdyBc10jxisLG5DmH+gGzAbEVx+QN7EdYl DP/W0lhgGeNRVUg8XmDNqx/ms/UVscjoOFRK6oeXu1DOKyooedzK5OKVBI9BjLuY9R52 4qgBcoS5hvdTmIjnn5lH/QaKCr8dpGMXQVIx8paQnhlp5lLiSSfEeoWkGSrOQB1WvmpL kmlJezl3ww9gzNfajy4NRHkMHFqEqjO6jW8X5O5NjnpReLUpFM7F/gOmFVBANI1tn4QC VaXcJ2U0UJJddkpoLg2EkaXqo1/kSoSj0o+0kc3KauGerYhK6Z1aKGE2iOSXQT5Vs951 9xXQ== X-Gm-Message-State: AOJu0YyC8NKNP3jMP5RXRfUiuQ3mKLOvaFcD2qDEwiP6wo9k49U2zq1a 6peIfbX47ZWXLu4lljebu92AneCCSrkBoynTVBo= X-Google-Smtp-Source: AGHT+IHDTm4uPycg4NnOmmmHO8eB8Hxe6SvZg93QYIJIsVaMcke5f0Zbhd+84fF7mn/7zDbMIdfM8WX0GvJvBINN+B4= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a05:690c:385:b0:5d3:a348:b0c0 with SMTP id bh5-20020a05690c038500b005d3a348b0c0mr2399920ywb.5.1702907991281; Mon, 18 Dec 2023 05:59:51 -0800 (PST) Date: Mon, 18 Dec 2023 13:58:52 +0000 In-Reply-To: <20231218135859.2513568-2-sebastianene@google.com> Mime-Version: 1.0 References: <20231218135859.2513568-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231218135859.2513568-4-sebastianene@google.com> Subject: [PATCH v4 02/10] KVM: arm64: Add ptdump registration with debugfs for the stage-2 pagetables 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-20231218_055952_973183_19820F0B X-CRM114-Status: GOOD ( 23.80 ) 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 deals with the 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. This registers a wrapper on top of debugfs_create_file which allows us to hook callbacks on the debugfs open/show/close. The callbacks are used to prepare the display portion of the pagetable dumping code. Guard this functionality under the newly introduced PTDUMP_STAGE2_DEBUGFS. Signed-off-by: Sebastian Ene --- arch/arm64/kvm/Kconfig | 13 +++++ arch/arm64/kvm/Makefile | 1 + arch/arm64/kvm/arm.c | 2 + arch/arm64/kvm/kvm_ptdump.h | 18 +++++++ arch/arm64/kvm/ptdump.c | 96 +++++++++++++++++++++++++++++++++++++ 5 files changed, 130 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 83c1e09be..0014e55e2 100644 --- a/arch/arm64/kvm/Kconfig +++ b/arch/arm64/kvm/Kconfig @@ -71,4 +71,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 c0c050e53..190eac175 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/arm.c b/arch/arm64/kvm/arm.c index e5f75f1f1..ee8d7cb67 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -40,6 +40,7 @@ #include #include #include +#include #include #include @@ -2592,6 +2593,7 @@ static __init int kvm_arm_init(void) if (err) goto out_subs; + kvm_ptdump_register_host(); kvm_arm_initialised = true; return 0; diff --git a/arch/arm64/kvm/kvm_ptdump.h b/arch/arm64/kvm/kvm_ptdump.h new file mode 100644 index 000000000..98b595ce8 --- /dev/null +++ b/arch/arm64/kvm/kvm_ptdump.h @@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +// +// Copyright (C) Google, 2023 +// Author: Sebastian Ene + +#ifndef __KVM_PTDUMP_H +#define __KVM_PTDUMP_H + +#include + + +#ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS +void kvm_ptdump_register_host(void); +#else +static inline void kvm_ptdump_register_host(void) { } +#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 000000000..5816fc632 --- /dev/null +++ b/arch/arm64/kvm/ptdump.c @@ -0,0 +1,96 @@ +// 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 + + +struct kvm_ptdump_register { + void *(*get_ptdump_info)(struct kvm_ptdump_register *reg); + void (*put_ptdump_info)(void *priv); + int (*show_ptdump_info)(struct seq_file *m, void *v); + void *priv; +}; + +static int kvm_ptdump_open(struct inode *inode, struct file *file); +static int kvm_ptdump_release(struct inode *inode, struct file *file); +static int kvm_ptdump_show(struct seq_file *m, void *); + +static const struct file_operations kvm_ptdump_fops = { + .open = kvm_ptdump_open, + .read = seq_read, + .llseek = seq_lseek, + .release = kvm_ptdump_release, +}; + +static int kvm_ptdump_open(struct inode *inode, struct file *file) +{ + struct kvm_ptdump_register *reg = inode->i_private; + void *info = NULL; + int ret; + + if (reg->get_ptdump_info) { + info = reg->get_ptdump_info(reg); + if (!info) + return -ENOMEM; + } + + if (!reg->show_ptdump_info) + reg->show_ptdump_info = kvm_ptdump_show; + + ret = single_open(file, reg->show_ptdump_info, info); + if (ret && reg->put_ptdump_info) + reg->put_ptdump_info(info); + + return ret; +} + +static int kvm_ptdump_release(struct inode *inode, struct file *file) +{ + struct kvm_ptdump_register *reg = inode->i_private; + struct seq_file *seq_file = file->private_data; + + if (reg->put_ptdump_info) + reg->put_ptdump_info(seq_file->private); + + return 0; +} + +static int kvm_ptdump_show(struct seq_file *m, void *) +{ + return -EINVAL; +} + +static void kvm_ptdump_debugfs_register(struct kvm_ptdump_register *reg, + const char *name, struct dentry *parent) +{ + debugfs_create_file(name, 0400, parent, reg, &kvm_ptdump_fops); +} + +static struct kvm_ptdump_register host_reg; + +void kvm_ptdump_register_host(void) +{ + if (!is_protected_kvm_enabled()) + return; + + kvm_ptdump_debugfs_register(&host_reg, "host_page_tables", + kvm_debugfs_dir); +} + +static int __init kvm_host_ptdump_init(void) +{ + host_reg.priv = (void *)host_s2_pgtable_pages(); + return 0; +} + +device_initcall(kvm_host_ptdump_init); From patchwork Mon Dec 18 13:58:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 13497029 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 1DC75C46CD3 for ; Mon, 18 Dec 2023 14:00:33 +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=4B5WVKDph1Caqawiayo6ditu1jXnZeRZAubA4XKzRGc=; b=A1ovIzJfPjcIfsczttQOIFx8WY cdBMePOmmi+OQiwXzw/UHP4g8CezpDsAg93uRwNlLVc8QPqU7valXEqg0L1jlSaT/yNZvaqd49tO0 WMsuF+OG3TKGjjI3cR+7KGovqN8938mNXw/2KBfmBt5ttL9j1NZJw/C/+ryFipOhnQ3pOqUEpbbBW /RqIbQ2mh9puwAR9rRmwkP54ukfbkYDzXdM0FBoRHM+qUmprChRD+LrVtBM3bjHnHu+DceUNvDOe/ 97ErmUdlyihL6ftCz6R1LRt2s92DzwDQqehFUgZLfs4EcZQvkBcquNO7eMQKA1LehoKZsaTZqekCn B2I/XmBA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rFEA6-00AwgG-0g; Mon, 18 Dec 2023 14:00:02 +0000 Received: from mail-wr1-x449.google.com ([2a00:1450:4864:20::449]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rFE9z-00Awbr-32 for linux-arm-kernel@lists.infradead.org; Mon, 18 Dec 2023 13:59:57 +0000 Received: by mail-wr1-x449.google.com with SMTP id ffacd0b85a97d-3364fa774ccso2208409f8f.2 for ; Mon, 18 Dec 2023 05:59:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702907993; x=1703512793; 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=LD/nzFY+bvR1rFs9NYLk8iA9qjOdN0RioRORlh5RIvY=; b=PLx5kOhZXpNB8sOihpueYroK+egNJnA24FkAV/OhmGofq1zDK6xi8+IbGCHcB2ucE3 0YgX0P2x5ubbh43LjfHguUI8Vgv9yBZWc8AJyQgxK8tT5dUHm3Bp8gFJfNToIqri6SUn 7p0S2d7rHjq9KcjDTScJPzZXiLkEbfueX0QQniezvMbn53Y87VJSwxQLYOlYd7mcxY/l 5oKANIAzdO+0bCLkFBcUSYUsiDI/gq3DFEKiDVPPqnvGpTMpt9wlc5xLJxufc6acRDrz qHpKKuKuON4i0saXXD2bILUVv/p0QQeSefKiWp8BH/51JtIuuVT/um00zRzqU81eD5oH 7WLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702907993; x=1703512793; 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=LD/nzFY+bvR1rFs9NYLk8iA9qjOdN0RioRORlh5RIvY=; b=DgGChqEhFMLMgdl9viA5EjPDLMP8anI3JQm7v/s+DQRPzzJERSkYjHv/6rAPvkFJ2e s2vENDW0UjD3rUehEyVxuXfBU23+buERbvf23xng4zvRRMKTfjhMOzl+lFXHo9xtBNFX 7JeoHYDZGWYry2L2isKrhX/4BvG6Xu93pIXKFRgHx4w2IpFvOD9FWse42E+eeI5chou8 ODlVGcW1PijBQgRB9xmyRXPv7BKLiSFjj7EoAGghhp1+zGlFDbj096TFCbcN3JY5fa8d 4xNwwU4LC6WOEk89QWTYGktJWBO6EqKjLRXkWVDpR734w+5iAd7ihH3zYKzJZxn2JdQx y2fQ== X-Gm-Message-State: AOJu0YzZ0KWVrKX3rtpPv3vzx32CmqdUTMyi/EF4bP8BzDSfpyzZqaOn fMJzqxSEfcemJfXg7U8CKJX9zlAvxj03pELckX0= X-Google-Smtp-Source: AGHT+IEcnSKTTSrh78/RC8KORdrGIpZs/n1ecYZzGosf52yOA02mXkz/mRucWOXm1wghUliteBLHAeukkuvzSEjzuuI= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a05:6000:1ac9:b0:336:4b35:ba52 with SMTP id i9-20020a0560001ac900b003364b35ba52mr32388wry.9.1702907993532; Mon, 18 Dec 2023 05:59:53 -0800 (PST) Date: Mon, 18 Dec 2023 13:58:53 +0000 In-Reply-To: <20231218135859.2513568-2-sebastianene@google.com> Mime-Version: 1.0 References: <20231218135859.2513568-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231218135859.2513568-5-sebastianene@google.com> Subject: [PATCH v4 03/10] KVM: arm64: Invoke the snapshot interface for the host stage-2 pagetable 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-20231218_055955_981663_B47FC382 X-CRM114-Status: GOOD ( 17.71 ) 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 Allocate memory for the snapshot by creating a memory cache with empty pages that will be used by the hypervisor during the page table copy. Get the required size of the PGD and allocate physically contiguous memory for it. Allocate contiguous memory for an array that is used to keep track of the pages used from the memcache. Call the snapshot interface and release the memory for the snapshot. Signed-off-by: Sebastian Ene --- arch/arm64/kvm/ptdump.c | 107 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/arch/arm64/kvm/ptdump.c b/arch/arm64/kvm/ptdump.c index 5816fc632..e99bab427 100644 --- a/arch/arm64/kvm/ptdump.c +++ b/arch/arm64/kvm/ptdump.c @@ -25,6 +25,9 @@ static int kvm_ptdump_open(struct inode *inode, struct file *file); static int kvm_ptdump_release(struct inode *inode, struct file *file); static int kvm_ptdump_show(struct seq_file *m, void *); +static phys_addr_t get_host_pa(void *addr); +static void *get_host_va(phys_addr_t pa); + static const struct file_operations kvm_ptdump_fops = { .open = kvm_ptdump_open, .read = seq_read, @@ -32,6 +35,11 @@ static const struct file_operations kvm_ptdump_fops = { .release = kvm_ptdump_release, }; +static struct kvm_pgtable_mm_ops ptdump_host_mmops = { + .phys_to_virt = get_host_va, + .virt_to_phys = get_host_pa, +}; + static int kvm_ptdump_open(struct inode *inode, struct file *file) { struct kvm_ptdump_register *reg = inode->i_private; @@ -78,11 +86,110 @@ static void kvm_ptdump_debugfs_register(struct kvm_ptdump_register *reg, static struct kvm_ptdump_register host_reg; +static size_t host_stage2_get_pgd_len(void) +{ + u32 phys_shift = get_kvm_ipa_limit(); + u64 vtcr = kvm_get_vtcr(read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1), + read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1), + phys_shift); + return (kvm_pgtable_stage2_pgd_size(vtcr) >> PAGE_SHIFT); +} + +static phys_addr_t get_host_pa(void *addr) +{ + return __pa(addr); +} + +static void *get_host_va(phys_addr_t pa) +{ + return __va(pa); +} + +static void kvm_host_put_ptdump_info(void *snap) +{ + void *mc_page; + size_t i; + struct kvm_pgtable_snapshot *snapshot; + + if (!snap) + return; + + snapshot = snap; + while ((mc_page = pop_hyp_memcache(&snapshot->mc, get_host_va)) != NULL) + free_page((unsigned long)mc_page); + + if (snapshot->pgd_hva) + free_pages_exact(snapshot->pgd_hva, snapshot->pgd_pages); + + if (snapshot->used_pages_hva) { + for (i = 0; i < snapshot->used_pages_indx; i++) { + mc_page = get_host_va(snapshot->used_pages_hva[i]); + free_page((unsigned long)mc_page); + } + + free_pages_exact(snapshot->used_pages_hva, snapshot->num_used_pages); + } + + free_page((unsigned long)snapshot); +} + +static void *kvm_host_get_ptdump_info(struct kvm_ptdump_register *reg) +{ + int i, ret; + void *mc_page; + struct kvm_pgtable_snapshot *snapshot; + size_t memcache_len; + + snapshot = (void *)__get_free_page(GFP_KERNEL_ACCOUNT); + if (!snapshot) + return NULL; + + memset(snapshot, 0, sizeof(struct kvm_pgtable_snapshot)); + + snapshot->pgd_pages = host_stage2_get_pgd_len(); + snapshot->pgd_hva = alloc_pages_exact(snapshot->pgd_pages, GFP_KERNEL_ACCOUNT); + if (!snapshot->pgd_hva) + goto err; + + memcache_len = (size_t)reg->priv; + for (i = 0; i < memcache_len; i++) { + mc_page = (void *)__get_free_page(GFP_KERNEL_ACCOUNT); + if (!mc_page) + goto err; + + push_hyp_memcache(&snapshot->mc, mc_page, get_host_pa); + } + + snapshot->num_used_pages = DIV_ROUND_UP(sizeof(phys_addr_t) * memcache_len, + PAGE_SIZE); + snapshot->used_pages_hva = alloc_pages_exact(snapshot->num_used_pages, + GFP_KERNEL_ACCOUNT); + if (!snapshot->used_pages_hva) + goto err; + + ret = kvm_call_hyp_nvhe(__pkvm_host_stage2_snapshot, snapshot); + if (ret) { + pr_err("ERROR %d snapshot host pagetables\n", ret); + goto err; + } + + snapshot->pgtable.pgd = get_host_va((phys_addr_t)snapshot->pgtable.pgd); + snapshot->pgtable.mm_ops = &ptdump_host_mmops; + + return snapshot; +err: + kvm_host_put_ptdump_info(snapshot); + return NULL; +} + void kvm_ptdump_register_host(void) { if (!is_protected_kvm_enabled()) return; + host_reg.get_ptdump_info = kvm_host_get_ptdump_info; + host_reg.put_ptdump_info = kvm_host_put_ptdump_info; + kvm_ptdump_debugfs_register(&host_reg, "host_page_tables", kvm_debugfs_dir); } From patchwork Mon Dec 18 13:58:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 13497031 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 716E9C46CCD for ; Mon, 18 Dec 2023 14:00:46 +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=A5l3NogVPV0vuPjLn34Pkm1nGWAjcmTi3Skn6PiII5A=; b=U3rLeqv6gtTogqCzLmdN4rGnc2 5Q/zlfTsRzGfq5fVPuR5e1BB6d9DIURZTiRzieEiMspnFOOMdUvKuQjvcanmYvGOtyeA4A7ejncb9 0Sn7F8pViCpCS4g8QpqTUn/LRy5afstst2zoDzS1Vc2K42Tc0f8RMgcfmUTjr1ZqdMGBymy1rLUvM uzy9XhHkxyXGnGa4/d3H3Te1fJJGrVx+zhmL7FuWd57Iaof5slZCINIpp1He1EXu6uU6R0JklfkTg JYM1EfC5VDRhYuAf5PdWeCaa93yWTH4aahiA75UoUXdPiGNnumHFlMNMU2VIOlgQk8igNqENp+cO7 0pfy5CCw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rFEAL-00Awnk-2C; Mon, 18 Dec 2023 14:00:17 +0000 Received: from mail-wr1-x44a.google.com ([2a00:1450:4864:20::44a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rFEA2-00AwcY-12 for linux-arm-kernel@lists.infradead.org; Mon, 18 Dec 2023 13:59:59 +0000 Received: by mail-wr1-x44a.google.com with SMTP id ffacd0b85a97d-336688e89dbso485298f8f.0 for ; Mon, 18 Dec 2023 05:59:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702907995; x=1703512795; 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=/snQWpLs/9aDAfFwDwLoi7Q0iJhDc3rGT80KePC/RNs=; b=CMPzeN6NxcD/x3RyVJXBVVy4VTusi61bBXRsd+H1/UVmfSWe3pU0QC0jGbqXrMFc+r AlbyssV7mkgZfhpYI4joi6FzSj4IABqhaQBLYqA1NrBxk6uosHiCqRHwDEzBS77X+7g+ lIsCLaxdVU1oFTHOLd/sLYAEPGvYyBkbmNbur4Mc0NnqkwORjiOcvSn30XmLTxpBlIG3 70Z/i5blhFNHkZ3rM6+vmx7zOTpXXUjruE0J4l5V1f1idnrUxVgRQf02pNe9MdSEBR0z rwcGLSE1xSdq96YLP6c9vtatbUfwBQkYf8VSlaW2tPSN7Yuztq9SXS3e2paez+OUSGmv 7d7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702907995; x=1703512795; 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=/snQWpLs/9aDAfFwDwLoi7Q0iJhDc3rGT80KePC/RNs=; b=PVdxZlfGimCsG9s759kKTPLHmYFMz4zWgvXfVJ9avS/8C98WzscpFtHR523MR7j6kM DrIlGaj7JDusxqLDHhkCnCbIdjLvMolQ4LhoI9hZJUugnsuLDd24ySYLtPzQkCPI5MQe vwblEtSsRantYnKw1nzd6dqlULU5CR1U9mFLiGpvQ8gymUx6BnGQ7YoCQv9qtpd93v2P bP9j36+bzGKhHn4Epq5USADU1lYawIcT5LszFGzYyhYPFTSYsulolDQr/slC99KHxPrD zBi0alXoDLlghokIYUxkAeB3iobGxVMtNhB8JST+KciHKwJ/LKtuSo3wM6PmIhUdJ2Qu ILJw== X-Gm-Message-State: AOJu0YxzfFTMv1emt8CD88uGtXbApdJknLEGFJX9JyDrfF01lHrrMBUZ cvgkGo/R9tVI6Zi87LVPkNKLBL21b0oY3uxGwCA= X-Google-Smtp-Source: AGHT+IGcU8IkdlfTmgAO7aC/oFs5X2llBNjhnb3I0wUbMfWOUAWu8CT+O1oF7roOu+Kzu88bosaE4t8ma3t0wuku/bU= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:adf:f548:0:b0:336:68ff:e929 with SMTP id j8-20020adff548000000b0033668ffe929mr7158wrp.5.1702907995808; Mon, 18 Dec 2023 05:59:55 -0800 (PST) Date: Mon, 18 Dec 2023 13:58:54 +0000 In-Reply-To: <20231218135859.2513568-2-sebastianene@google.com> Mime-Version: 1.0 References: <20231218135859.2513568-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231218135859.2513568-6-sebastianene@google.com> Subject: [PATCH v4 04/10] arm64: ptdump: Expose the attribute parsing functionality 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-20231218_055958_362553_412FA99A X-CRM114-Status: GOOD ( 17.90 ) 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 581caac52..23510be35 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 e305b6593..64127c70b 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 Mon Dec 18 13:58:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 13497034 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 D62BFC35274 for ; Mon, 18 Dec 2023 14:00:51 +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=dYZ1j9099a5qOIKQsltFV0e30ukwyNHeqv+hArmVbL0=; b=2wfRWl+y3DI2x7VyB/4jXpPsub M66nNqu3ZbbZEglj0fqKp+aB7cC8l9MnC5Bwac2ke8TCHR57VMHPY2o9mcH524Emdo5y+4Lax3r8B 7IpgjD+M87S0yV2ZY/Lg8dTsKXcGHWIoeX/pTgrP1AsAWAXgDhMEKF+dDQNAh3xVQGuUvK2tY0Lxq eo78WOC/AEWFKEoaIRKQUSGBOtaGZx1kGDdhJK72iYMXZ0MEt56Hj7GL725bIuFkF2m3SZqR3/7Jd efML1L0/BaGNFrIlZQEKdZzcLNTho1UsmAn9LB5wPZZQXjnAzgazyH2n5UILDiZ7e9/1ePqWJcIsD BXyGV4cA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rFEAM-00AwoL-2k; Mon, 18 Dec 2023 14:00:18 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rFEA3-00Awd7-2U for linux-arm-kernel@lists.infradead.org; Mon, 18 Dec 2023 14:00:01 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-dbd4b49bac7so20145276.0 for ; Mon, 18 Dec 2023 05:59:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702907998; x=1703512798; 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=mFw1xMXqNfgjkaBSBUL3X2hMTwwdH9z1Qrb9oU+YYCg=; b=CNtUWP6z1jyD+Au8HX3xKyuis79R+QSPJSIrKt33IPH0bmbNnXTFS2TlqXdkv2JfVW QJcIyaScZ78Ya35IfoXS1iKhks3ZjlWQRJ5H81XOAtHfNNmhL7Qa5buognsBI83IFRtZ /MSDcpNFA/2U/RP6L4vBYb4c5l/I4N8jbjgSRnrR/UQoTCnZ/Y2eiQdf8++ywX5Phd4/ Xh3afw6ZY3lEOx1CIs+MoATuZO3Rr7J3T+xI/Ql/e9er2QBF1k7ft3Ek06iCnU73GinZ UeNaVRXu5DtK9x9K4cqcEwdnCzsN35rxgAn7y38hYQtNsdE81th1iHm4ivtiTPMrN925 qnbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702907998; x=1703512798; 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=mFw1xMXqNfgjkaBSBUL3X2hMTwwdH9z1Qrb9oU+YYCg=; b=njEwpCMvztNX4BY0O1qNKm8y3VxYtl7iSGtbsvn7Sup07buF1lw3KSr/u9TpgYyEp+ SNhgvlg3VYHMdUaNBd7oppe/Wng7LlntGVlDknYAdjWwP+sHFzsHxkLNn/TML5xm1bJ4 Yzbkq+XjFEdwBZaQCSnLY6DxQIGopTzy3xVO24SmdSnkja2QTXASVSWb4odg5f6lWQDE FU3InJwdZ5nQKPILsclTyDkmb65W5l3DNDMeBz17zmXvCfUKh2jR9loyzsAh5ArpoJ0l AU11K94sGwWpR4WOzEbHhMFP63E0groHF9NxNuoDkGVoo+XGGniDnGzdiBZCMJXZkd78 1XJQ== X-Gm-Message-State: AOJu0YwvBLmNgKNEs7pg5UWcz4+rJHHtcFqTS3hSAt+tkcVVDTjqO0/F QX7vsn+msGQw9FslPvepdZH6KnONTkvP6d65IWk= X-Google-Smtp-Source: AGHT+IEFAVylPAkwhhH2AZEDt0+C92z4jGsfWuRL60FJ8EEzto9BtF/sl4dbwHDk1YbX1tmslSZZ2vCCzauBKs4CL5A= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a25:8a09:0:b0:db5:4d59:3b30 with SMTP id g9-20020a258a09000000b00db54d593b30mr1613891ybl.8.1702907998064; Mon, 18 Dec 2023 05:59:58 -0800 (PST) Date: Mon, 18 Dec 2023 13:58:55 +0000 In-Reply-To: <20231218135859.2513568-2-sebastianene@google.com> Mime-Version: 1.0 References: <20231218135859.2513568-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231218135859.2513568-7-sebastianene@google.com> Subject: [PATCH v4 05/10] arm64: ptdump: Use the mask from the state structure 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-20231218_055959_823594_8F9CCFFE X-CRM114-Status: GOOD ( 18.13 ) 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 23510be35..4e728d2a1 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 64127c70b..015ed65d3 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 Mon Dec 18 13:58:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 13497033 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 C9B73C46CD3 for ; Mon, 18 Dec 2023 14:00:49 +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=Ok6wHaHL+Y+BXK2ZH8oXL0+WgfG2f78fLXoSUuR3nPo=; b=xOrBWXNMIp9wdWZmmFmf6hub2p eigp+KHNIELesrJfRp597rbDtTVKDN2r2aiqOdGxJf8HeNBxEh6VK4vQBsk/RJWLd/Rz+13IGot54 bQb2rxt8MJnKr0OlA8aB0l7vcfPdQ+mIUb4kLpxbAg2peqbG++l+yct7ytotq1EMTM/kMWuEg7BDB Kjx5EimUzLcI8+2asS91YEAixMsasSsPrZiCp29sPaVmnD3EGHRm/RjrMacq0yaoVldBSFVbr71rK WhmPXdmEtPmpC+D880ez9h4yqJBxxp5+xpMaNd52GnBr3G+MY2s5dnMrBpYnAFHDnrdDWM63Thvpy gTVKPNtQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rFEAN-00Awpa-2f; Mon, 18 Dec 2023 14:00:19 +0000 Received: from mail-wr1-x449.google.com ([2a00:1450:4864:20::449]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rFEA5-00Awev-2b for linux-arm-kernel@lists.infradead.org; Mon, 18 Dec 2023 14:00:03 +0000 Received: by mail-wr1-x449.google.com with SMTP id ffacd0b85a97d-332ee20a3f0so2752948f8f.0 for ; Mon, 18 Dec 2023 06:00:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702908000; x=1703512800; 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=rOYwKOEiNUEli62sB45rLYKU5arboivvkJa3j/TVfbA=; b=qKJ7RsULnrcOGlMA80TqBEWMVhn2jCmpvpgxC70F/HzVhGrkg0FVXCZ7G94N5/Ckt1 mhEtEsu/1AVEs56kVzzM4T3TCl+qwHH515n4n7MxKb9gNkAVyxliJHac9e9VsO8g4Z0F qVsqKIIhKm8TOf1TiTna00ZZgmKGCewXx3nL4Lmuj8yeUOJjE4ditcvyVDM6f0bz7S5i YejAp1YNqmrutIIgpco2R7nAjP+duJ8XO070JY/drz7OO+53rBaZAGg67ySfQRBD8Ugw 7VETvbnRifzNAYizKCsgfn1HZnXvurGRa8uEiuBRwZzVtret+vxTPp1j+IlCOJCbpc9N Gpyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702908000; x=1703512800; 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=rOYwKOEiNUEli62sB45rLYKU5arboivvkJa3j/TVfbA=; b=Q8VENc8FeJNPW1aGpKGDa0fgByzZY7qMyN+TFU3XdmWikiUuESS5Wi+B6P8CODpFuP FS6Z8WH5MIb7PhxDcK65yuh5kLx7ByFkWe8racEPujR3fPdnkKL9ZIIej9eLxe9Z8TLf l/N0Qjj8W8pYnH9kzGZnwFONWM9PnfuOek7r2DqIbb5Iw3eu5EfNrDKV7cgj9nxp+IJT YN0uS8p2zUU0Vg61wsZP0FcrHYPVdqruONg6+oo7C7vJ5aJanVS1Rofg+HHLrTe69hux 6KNP6iP7+cURvYGfsBFDTm6lblJ9fbOAhpqISg5PX9ENzBgbfmVIFMZX7QieVio/oR2h 9N+w== X-Gm-Message-State: AOJu0Yw8LkBBDdH3y5NZcDWBRxiULCjMsdaHMyw9mExNyq4aTEfsrquh dawwjPPY0oSAMK4K+YtO0an0bKQZqxqQV0891F0= X-Google-Smtp-Source: AGHT+IGXjL4T5r7Jt2z8+UD516Qhh+E409pWPWPbcvwZsNr9Ka8qw+CBcfH+yjDL6U97mYGpDrYSGheDFXUPbCIOwCo= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a05:6000:144d:b0:336:6ccb:c32b with SMTP id v13-20020a056000144d00b003366ccbc32bmr3131wrx.9.1702908000205; Mon, 18 Dec 2023 06:00:00 -0800 (PST) Date: Mon, 18 Dec 2023 13:58:56 +0000 In-Reply-To: <20231218135859.2513568-2-sebastianene@google.com> Mime-Version: 1.0 References: <20231218135859.2513568-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231218135859.2513568-8-sebastianene@google.com> Subject: [PATCH v4 06/10] KVM: arm64: Move pagetable definitions to common header 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-20231218_060001_963755_D9E798EF X-CRM114-Status: GOOD ( 11.18 ) 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 In preparation for using the stage-2 definitions in ptdump, move some of these macros in the common header. Signed-off-by: Sebastian Ene --- arch/arm64/include/asm/kvm_pgtable.h | 42 ++++++++++++++++++++++++++++ arch/arm64/kvm/hyp/pgtable.c | 42 ---------------------------- 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h index f73efd8a8..37f2a8532 100644 --- a/arch/arm64/include/asm/kvm_pgtable.h +++ b/arch/arm64/include/asm/kvm_pgtable.h @@ -45,6 +45,48 @@ typedef u64 kvm_pte_t; #define KVM_PHYS_INVALID (-1ULL) +#define KVM_PTE_LEAF_ATTR_LO GENMASK(11, 2) + +#define KVM_PTE_LEAF_ATTR_LO_S1_ATTRIDX GENMASK(4, 2) +#define KVM_PTE_LEAF_ATTR_LO_S1_AP GENMASK(7, 6) +#define KVM_PTE_LEAF_ATTR_LO_S1_AP_RO \ + ({ cpus_have_final_cap(ARM64_KVM_HVHE) ? 2 : 3; }) +#define KVM_PTE_LEAF_ATTR_LO_S1_AP_RW \ + ({ cpus_have_final_cap(ARM64_KVM_HVHE) ? 0 : 1; }) +#define KVM_PTE_LEAF_ATTR_LO_S1_SH GENMASK(9, 8) +#define KVM_PTE_LEAF_ATTR_LO_S1_SH_IS 3 +#define KVM_PTE_LEAF_ATTR_LO_S1_AF BIT(10) + +#define KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR GENMASK(5, 2) +#define KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R BIT(6) +#define KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W BIT(7) +#define KVM_PTE_LEAF_ATTR_LO_S2_SH GENMASK(9, 8) +#define KVM_PTE_LEAF_ATTR_LO_S2_SH_IS 3 +#define KVM_PTE_LEAF_ATTR_LO_S2_AF BIT(10) + +#define KVM_PTE_LEAF_ATTR_HI GENMASK(63, 50) + +#define KVM_PTE_LEAF_ATTR_HI_SW GENMASK(58, 55) + +#define KVM_PTE_LEAF_ATTR_HI_S1_XN BIT(54) + +#define KVM_PTE_LEAF_ATTR_HI_S2_XN BIT(54) + +#define KVM_PTE_LEAF_ATTR_HI_S1_GP BIT(50) + +#define KVM_PTE_LEAF_ATTR_S2_PERMS (KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | \ + KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | \ + KVM_PTE_LEAF_ATTR_HI_S2_XN) + +#define KVM_INVALID_PTE_OWNER_MASK GENMASK(9, 2) +#define KVM_MAX_OWNER_ID 1 + +/* + * Used to indicate a pte for which a 'break-before-make' sequence is in + * progress. + */ +#define KVM_INVALID_PTE_LOCKED BIT(10) + static inline bool kvm_pte_valid(kvm_pte_t pte) { return pte & KVM_PTE_VALID; diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c index 82fef9620..ccd46bf56 100644 --- a/arch/arm64/kvm/hyp/pgtable.c +++ b/arch/arm64/kvm/hyp/pgtable.c @@ -17,48 +17,6 @@ #define KVM_PTE_TYPE_PAGE 1 #define KVM_PTE_TYPE_TABLE 1 -#define KVM_PTE_LEAF_ATTR_LO GENMASK(11, 2) - -#define KVM_PTE_LEAF_ATTR_LO_S1_ATTRIDX GENMASK(4, 2) -#define KVM_PTE_LEAF_ATTR_LO_S1_AP GENMASK(7, 6) -#define KVM_PTE_LEAF_ATTR_LO_S1_AP_RO \ - ({ cpus_have_final_cap(ARM64_KVM_HVHE) ? 2 : 3; }) -#define KVM_PTE_LEAF_ATTR_LO_S1_AP_RW \ - ({ cpus_have_final_cap(ARM64_KVM_HVHE) ? 0 : 1; }) -#define KVM_PTE_LEAF_ATTR_LO_S1_SH GENMASK(9, 8) -#define KVM_PTE_LEAF_ATTR_LO_S1_SH_IS 3 -#define KVM_PTE_LEAF_ATTR_LO_S1_AF BIT(10) - -#define KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR GENMASK(5, 2) -#define KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R BIT(6) -#define KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W BIT(7) -#define KVM_PTE_LEAF_ATTR_LO_S2_SH GENMASK(9, 8) -#define KVM_PTE_LEAF_ATTR_LO_S2_SH_IS 3 -#define KVM_PTE_LEAF_ATTR_LO_S2_AF BIT(10) - -#define KVM_PTE_LEAF_ATTR_HI GENMASK(63, 50) - -#define KVM_PTE_LEAF_ATTR_HI_SW GENMASK(58, 55) - -#define KVM_PTE_LEAF_ATTR_HI_S1_XN BIT(54) - -#define KVM_PTE_LEAF_ATTR_HI_S2_XN BIT(54) - -#define KVM_PTE_LEAF_ATTR_HI_S1_GP BIT(50) - -#define KVM_PTE_LEAF_ATTR_S2_PERMS (KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | \ - KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | \ - KVM_PTE_LEAF_ATTR_HI_S2_XN) - -#define KVM_INVALID_PTE_OWNER_MASK GENMASK(9, 2) -#define KVM_MAX_OWNER_ID 1 - -/* - * Used to indicate a pte for which a 'break-before-make' sequence is in - * progress. - */ -#define KVM_INVALID_PTE_LOCKED BIT(10) - struct kvm_pgtable_walk_data { struct kvm_pgtable_walker *walker; From patchwork Mon Dec 18 13:58:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 13497035 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 0E2D1C35274 for ; Mon, 18 Dec 2023 14:00:55 +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=1K4JxbLBGVvy7RaVjiaYk9vqYIoKzJv5ZpYNKNUyb+o=; b=tZEDU4LN4nZX9Z8aFaRo0sfwAE 2cfFxbQuEZHgny0nxiIxWuwSVcYZWU24fq4eRt+ro/E55LK9BFQRHEVT6ArNR5sgEseXwr9OaNNPT TKAwSi+0mi6UKtaN5P3+Qpxe7Ltvl4L6TKK4PxLOK98oh58sBodnIfamR9uabOQHfllNQc52Y9LeZ 2ANkfPXxUEkRJlost38PTQZRgnI21dm2j04ohH0/iRrGjFvBx0IWHo3EGvAgqRCPqQ3giXEzEX+yc iFlAvE9PJt7qzbUCu2auxoMIwtD2v216dDFgvJX29zDTkBzNk5BFzHRcKa4rt9O6YVgC4bNX8BEFw pNG23Ybg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rFEAP-00Awqs-00; Mon, 18 Dec 2023 14:00:21 +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 1rFEAA-00Awgw-0I for linux-arm-kernel@lists.infradead.org; Mon, 18 Dec 2023 14:00:07 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5ca26c07848so38559487b3.0 for ; Mon, 18 Dec 2023 06:00:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702908002; x=1703512802; 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=EaxQbDI0z91PmFyzqzSN6rlfHYYOk4E9JSBlc3pa6ug=; b=PDAUAC9DoQIRInd8pnleFSL8EEqTAEs3iXVV2DUvMYeNtDGUz+zL5gKY3yaKMY7HOd YnOUOvWRfQWLxbOIgN2w9Pngsw5xZy60srano+0DSi9wlYoivVrxtgTkCCS8ctdTKjLk 2ciX7dv4072AUAkqvMMBbip6lFfC1H+mAOpD9MGT0s5U6kEamWJmFEYURw7Va6sLbMDu uTnUfWIciknvIsPJ3nwosEfkQoDrXRvan3pnpINjZve3wBkcNiddENkcSKjeCrPq9piY wVa//hFW6db3MCtJuugrZpvoCP320wrsNKhS7jgTIUhbAA5KpvQ5AqY1ItjUsHZElCz5 0ecw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702908002; x=1703512802; 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=EaxQbDI0z91PmFyzqzSN6rlfHYYOk4E9JSBlc3pa6ug=; b=TC2pL0qqkxwjr27ItsSnLL+ZcNWefTVEiYbdXjqWUfXEkRskPOYeSKQUyEnakmMmBW vlhFv60zsJgYnz7SU8cR5FHgS0VPsr26O4jodCGpvQQT1liIXHigvVMaY/+jFmh2AZ1N uuIqJnDizSUxb0WXdhtXej0ti/az97ggTb5DsaoDi7+kJHiRw7RDj5Qn23mlEHJ615p8 WL2+MzPqvdXgOq16kQByGEcnIRxhxXDw5Xz4sDXsaUam7tj4kUpts+6GcVu8AGknxXAH 0B3Mj2e02BNRgpAc2CVP2lYcGISDNvVqseuO8iHnPSzr4GzGNc4fUFRMdxJF1ZlEQIUH EuxQ== X-Gm-Message-State: AOJu0YxIvtNCOzAEYoySUIysT/S7SR0PgI43luzzXOmisZtUe6bapVrN FuRYT/56Q1koN9FEnuW4TVW7N+RnQaxVLnCd/cI= X-Google-Smtp-Source: AGHT+IFdxs9UOFdPERhZjY8OFaYu75oZ041FHbjTLNFGBMLYUBQ7MdtHx0C9eihZ77Ez9vOgv3CrKRgqSuQvLgOuiJs= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a05:690c:891:b0:5d3:40f3:56bf with SMTP id cd17-20020a05690c089100b005d340f356bfmr2377671ywb.1.1702908002759; Mon, 18 Dec 2023 06:00:02 -0800 (PST) Date: Mon, 18 Dec 2023 13:58:57 +0000 In-Reply-To: <20231218135859.2513568-2-sebastianene@google.com> Mime-Version: 1.0 References: <20231218135859.2513568-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231218135859.2513568-9-sebastianene@google.com> Subject: [PATCH v4 07/10] KVM: arm64: Walk the pagetable snapshot and parse the ptdump descriptors 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-20231218_060006_151553_C6CB770D X-CRM114-Status: GOOD ( 17.85 ) 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 the attributes for the stage-2 pagetables that will be used by the ptdump parser code to interepret and show the descriptors. Build the pagetable level description dynamically and use the KVM pagetable walker to visit the PTEs. Display the number of the bits used by the IPA address space and the start level. Signed-off-by: Sebastian Ene --- arch/arm64/kvm/ptdump.c | 135 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 134 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/ptdump.c b/arch/arm64/kvm/ptdump.c index e99bab427..80c338e03 100644 --- a/arch/arm64/kvm/ptdump.c +++ b/arch/arm64/kvm/ptdump.c @@ -40,6 +40,66 @@ static struct kvm_pgtable_mm_ops ptdump_host_mmops = { .virt_to_phys = get_host_pa, }; +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", + }, +}; + static int kvm_ptdump_open(struct inode *inode, struct file *file) { struct kvm_ptdump_register *reg = inode->i_private; @@ -73,9 +133,82 @@ static int kvm_ptdump_release(struct inode *inode, struct file *file) return 0; } +static int kvm_ptdump_build_levels(struct pg_level *level, unsigned int start_level) +{ + static const char * const level_names[] = {"PGD", "PUD", "PMD", "PTE"}; + int i, j, name_index; + + if (start_level > 2) { + pr_err("invalid start_level %u\n", start_level); + return -EINVAL; + } + + for (i = start_level; i < KVM_PGTABLE_MAX_LEVELS; i++) { + name_index = i - start_level; + name_index += name_index * start_level; + + level[i].name = level_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; + } + + return 0; +} + +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(struct seq_file *m, void *) { - return -EINVAL; + u64 ipa_size; + char ipa_description[32]; + struct pg_state st; + struct addr_marker ipa_addr_markers[3] = {0}; + struct pg_level pg_level_descr[KVM_PGTABLE_MAX_LEVELS] = {0}; + struct kvm_pgtable_snapshot *snapshot = m->private; + struct kvm_pgtable *pgtable = &snapshot->pgtable; + struct kvm_pgtable_walker walker = (struct kvm_pgtable_walker) { + .cb = kvm_ptdump_visitor, + .arg = &st, + .flags = KVM_PGTABLE_WALK_LEAF, + }; + + if (kvm_ptdump_build_levels(pg_level_descr, pgtable->start_level) < 0) + return -EINVAL; + + snprintf(ipa_description, sizeof(ipa_description), + "IPA bits %2u start lvl %1u", pgtable->ia_bits, + pgtable->start_level); + + ipa_size = BIT(pgtable->ia_bits); + ipa_addr_markers[0].name = ipa_description; + ipa_addr_markers[1].start_address = ipa_size; + + st = (struct pg_state) { + .seq = m, + .marker = &ipa_addr_markers[0], + .level = -1, + .pg_level = &pg_level_descr[0], + .ptdump = { + .note_page = note_page, + .range = (struct ptdump_range[]) { + {0, ipa_size}, + {0, 0}, + }, + }, + }; + + return kvm_pgtable_walk(pgtable, 0, ipa_size, &walker); } static void kvm_ptdump_debugfs_register(struct kvm_ptdump_register *reg, From patchwork Mon Dec 18 13:58:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 13497032 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 0B6B3C35274 for ; Mon, 18 Dec 2023 14:00:49 +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=S5Vq3H9yWxKz5MDD802NSu2bphMKGmaZ9khpQE/Oj3Q=; b=HSmQK9oY1XuPQtPEiFx/0IlJJa SDc5ZLTxxMbB8tibJOXyiofpSRwkWuW75h1XS/rYi8tbMZWYehMgxOlYwusKSZ7LkAeaU+2wuIJVg prox9P3av3pjhBMFE9HeCTxVyQZNS5Xhi7vx4D/I2cmpoJ+XZmyxaAoTKz0RRXF7SNWGeH9ADKDwJ T8w6ftjlZ51kz0Au/b/P/x+g5z5dQP2xz4R8tuhSA2zwpkN6fLIbitzPFMaM0kNQ4xdVcDnjta2kq ZqYyD4aMWhdwzF4pXp9JWvA13aNf+pOdfLths+37Rin49SNAsUJ6s+lMvsGvt8oNtPUmoiCiFKXY7 v/8D/MYg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rFEAQ-00Awrw-1P; Mon, 18 Dec 2023 14:00:22 +0000 Received: from mail-wr1-x449.google.com ([2a00:1450:4864:20::449]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rFEAB-00Awii-0K for linux-arm-kernel@lists.infradead.org; Mon, 18 Dec 2023 14:00:08 +0000 Received: by mail-wr1-x449.google.com with SMTP id ffacd0b85a97d-33652ced20fso2216964f8f.1 for ; Mon, 18 Dec 2023 06:00:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702908005; x=1703512805; 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=/DzJPItky+7MIXN0FJdMX89fRzHYJTFATAafmjRyPww=; b=Lz10crCuegwJSD61QfVg0rZUcALHS24IKCm+LaQRUS/dpjS+JaEH04lvmVJEGmqe6N hYVGGWrHepLK5z/hhdTT7RG0SPQLAjz4wioTZFHf/4aL/GdHbbq4hHUZgGzYwwD27CHA Sued2xjv0Nq3w8dHEdzJAAgNncZMPn5XwftawD5D/3oMdvUp9GSNu9Isb0ozkmgbw52v BgEuhBIqaLEvGufNWJAjNiBpnShYZ5iPW35ukpCN1Kfz0AU/LKk32/ZcCIlYAbltPMjC hKYtg1S7ePwW/LatpBuknFwtOpH8y35n7nprP3e9/8df14+5mABUu7alsPEcsNU5h5LX Om+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702908005; x=1703512805; 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=/DzJPItky+7MIXN0FJdMX89fRzHYJTFATAafmjRyPww=; b=V/h7HnAfX65/MtJgV8Ckmr4Wri/8tcrzrTF2gf12kDJoPoJLjnHb7yycdTOoVQ1f1A X9VHMIcF7FIyDeGvlPMfuILoHmLmZu41msGeiTz8Ab2HuHtHpEpGaD2LUKiV7NYJSWoF wYg9o1PYsR8Lb1GwvGpeXMaxUHyW7pU1mOYVBbzjJ1QJLg6mAYWVb+3Qe6aQixXB9BVF ZieiWYozrvfwWTNwAFdQA9EPX4qCmiuUY+JDkzrNURRCQBauGGK2ElO1qsRqLjD3f/Nh Rl12WKGHWKgqlU8ZMocLB6cnNSscKt4SlD4CRB6k44JrE1cSVwB/+/J14AhlVMrdSqco RqSQ== X-Gm-Message-State: AOJu0YyweLCYQs5WUPKAYsmvO19MskInYGesT7Y41UYtqF7uHCH81BGc v8WZycl92fVddxujFmVePhR621ve8byh6IoH8rU= X-Google-Smtp-Source: AGHT+IFva+9bduuuV31oR8xkc8BswjCQxwwMXxhfSUj0PJxjfOR04yw1jMSSd5Mf5dzNJ/J+K9OLoYJn4glXAxT7l0Q= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:adf:d1cf:0:b0:333:5232:d7f5 with SMTP id b15-20020adfd1cf000000b003335232d7f5mr91065wrd.8.1702908005046; Mon, 18 Dec 2023 06:00:05 -0800 (PST) Date: Mon, 18 Dec 2023 13:58:58 +0000 In-Reply-To: <20231218135859.2513568-2-sebastianene@google.com> Mime-Version: 1.0 References: <20231218135859.2513568-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231218135859.2513568-10-sebastianene@google.com> Subject: [PATCH v4 08/10] arm64: ptdump: Interpret memory attributes based on the runtime config 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-20231218_060007_138463_A8560120 X-CRM114-Status: GOOD ( 15.99 ) 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 Introduce two callbacks that verify the current runtime configuration before parsing the attribute fields. This is used to check when FWB is enabled which changes the interpretation of the descriptor bits. Signed-off-by: Sebastian Ene --- arch/arm64/include/asm/ptdump.h | 7 +++++++ arch/arm64/kvm/ptdump.c | 32 ++++++++++++++++++++++++++++++++ arch/arm64/mm/ptdump.c | 6 ++++++ 3 files changed, 45 insertions(+) diff --git a/arch/arm64/include/asm/ptdump.h b/arch/arm64/include/asm/ptdump.h index 4e728d2a1..e150fc21f 100644 --- a/arch/arm64/include/asm/ptdump.h +++ b/arch/arm64/include/asm/ptdump.h @@ -23,11 +23,18 @@ struct ptdump_info { unsigned long base_addr; }; +/* Forward declaration */ +struct pg_state; + struct prot_bits { u64 mask; u64 val; const char *set; const char *clear; + /* bit ignored if the callback returns false */ + bool (*feature_on)(const struct pg_state *ctxt); + /* bit ignored if the callback returns true */ + bool (*feature_off)(const struct pg_state *ctxt); }; struct pg_level { diff --git a/arch/arm64/kvm/ptdump.c b/arch/arm64/kvm/ptdump.c index 80c338e03..0ad7944e5 100644 --- a/arch/arm64/kvm/ptdump.c +++ b/arch/arm64/kvm/ptdump.c @@ -40,6 +40,18 @@ static struct kvm_pgtable_mm_ops ptdump_host_mmops = { .virt_to_phys = get_host_pa, }; +static bool is_fwb_enabled(const struct pg_state *m) +{ + struct kvm_pgtable_snapshot *snapshot = m->seq->private; + struct kvm_pgtable *pgtable = &snapshot->pgtable; + bool fwb_enabled = false; + + if (cpus_have_final_cap(ARM64_HAS_STAGE2_FWB)) + fwb_enabled = !(pgtable->flags & KVM_PGTABLE_S2_NOFWB); + + return fwb_enabled; +} + static const struct prot_bits stage2_pte_bits[] = { { .mask = PTE_VALID, @@ -81,6 +93,26 @@ static const struct prot_bits stage2_pte_bits[] = { .val = PTE_TABLE_BIT, .set = " ", .clear = "BLK", + }, { + .mask = KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR | PTE_VALID, + .val = PTE_S2_MEMATTR(MT_S2_DEVICE_nGnRE) | PTE_VALID, + .set = "DEVICE/nGnRE", + .feature_off = is_fwb_enabled, + }, { + .mask = KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR | PTE_VALID, + .val = PTE_S2_MEMATTR(MT_S2_FWB_DEVICE_nGnRE) | PTE_VALID, + .set = "DEVICE/nGnRE FWB", + .feature_on = is_fwb_enabled, + }, { + .mask = KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR | PTE_VALID, + .val = PTE_S2_MEMATTR(MT_S2_NORMAL) | PTE_VALID, + .set = "MEM/NORMAL", + .feature_off = is_fwb_enabled, + }, { + .mask = KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR | PTE_VALID, + .val = PTE_S2_MEMATTR(MT_S2_FWB_NORMAL) | PTE_VALID, + .set = "MEM/NORMAL FWB", + .feature_on = is_fwb_enabled, }, { .mask = KVM_PGTABLE_PROT_SW0, .val = KVM_PGTABLE_PROT_SW0, diff --git a/arch/arm64/mm/ptdump.c b/arch/arm64/mm/ptdump.c index 015ed65d3..6c7208f66 100644 --- a/arch/arm64/mm/ptdump.c +++ b/arch/arm64/mm/ptdump.c @@ -177,6 +177,12 @@ static void dump_prot(struct pg_state *st, const struct prot_bits *bits, for (i = 0; i < num; i++, bits++) { const char *s; + if (bits->feature_on && !bits->feature_on(st)) + continue; + + if (bits->feature_off && bits->feature_off(st)) + continue; + if ((st->current_prot & bits->mask) == bits->val) s = bits->set; else From patchwork Mon Dec 18 13:58:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 13497037 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 38FAAC35274 for ; Mon, 18 Dec 2023 14:00:58 +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=V+Lw89cmhRJXyVgmhza8M7Ae+chsFUUQ4u0t9D1Ow0c=; b=dlgH9T+FmxTV6dAJFC1jgax6rk ezyCPVRXk7KJGXEg5MWYDPqs/eGEJdFBPYc3CBxEvc/RKm8HLaj9tG1TiJ4XJ+5rnwNk2Cch1iv7n i16y46UXJUZ3hzRnjt8qwpkGfHWEoJpwUbLN+fpUXiaTWL7PeVUIXRiKIvf+1GEKVwMsG6Cr1hVfN f0gt6nHIsEcWXFPtsdrOutoIMiorfqNE/XuTBTeIJpYfo/8LBO5FrTDGomjkuFcm3nBdtGcDNpOA9 bBwnAaKYx5VXSLCbO/g726zOZVYbqdbIBdfZrjLzfW//LvKu7/b0cWWXyH7B3bT3HLG40+kznX+N1 G9F/5YYA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rFEAR-00Awsx-2W; Mon, 18 Dec 2023 14:00:23 +0000 Received: from mail-wr1-x449.google.com ([2a00:1450:4864:20::449]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rFEAC-00Awjd-2O for linux-arm-kernel@lists.infradead.org; Mon, 18 Dec 2023 14:00:10 +0000 Received: by mail-wr1-x449.google.com with SMTP id ffacd0b85a97d-3335df64539so2515320f8f.3 for ; Mon, 18 Dec 2023 06:00:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702908007; x=1703512807; 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=RVeSPgnrVagCch9Gfw7vmBqjH7Htt5Ae7divhOxYmU8=; b=P30c0lb4o8uVyBESZDaPGgKxbKQ96u3OmqAS6vRs0eR5obbfBCB7KNvaqo17Bh+62Z wnCNvLvRDgofUAVmqDOoV0zXIC7V/SLptF6ynGxdSQ+vecbF6O3uVcLj5CzemvLj5UPt olFwzc9+JgV3hNHzwAyzoNScnV4BnoEh14Z+v2k2S54n2kWxP7t6JlVknzq1Xn+9zkXt dPIY5DiXHxanBV7ekwHXPzYBsjmggB964xJ6JI8SLAkJqM7HZC1zmxvUd3HZAX61w4Km ZkV3NtHV8dgJcNyWQP5Lc3EoQZZ1hMhG9+8lhv4Nbk9S+yz4g9Kr8W4hDKg4r74Vkq3R TXZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702908007; x=1703512807; 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=RVeSPgnrVagCch9Gfw7vmBqjH7Htt5Ae7divhOxYmU8=; b=Iqjc0jrX7CXe51LPLurpuU/mB7WEtXcFel0DdSYzDVwyZlv3VBX3KtvC0hz/ERBzn8 iW26z87JN2hAPLBMGn9TbU7Fb/8/2Hj+TSuqQWIc5TYbi+DxBwgewjs8LtEW0jy9ReZL PE2cCheAp8IdR+QJrasYsDCb+urRPT4iOEtRxhZ3gY6kPAr4ZpBtu7afWOvUMPNGS/NI r08ArMIQXX5JFeF9OVZZdrdLfB2DHbXup8rI6MI+mddIVgd5Yls/iuLS7slscw99cTfe J0aB7/l5yTMBxc2K+Nbn9IuLS0cwO8G/qVxZE40Z3Fy25FrUFxgJ30TWVFd8gLsa29R0 i+9A== X-Gm-Message-State: AOJu0Yw2sufcyKmeE0D4m0Daiq5geeguhlPyvwBUGVBb31rCfCV0rijh m5YMfNJNzQG1RDt5mk8KUbHv0XFesFZkwoHkWr4= X-Google-Smtp-Source: AGHT+IEiB7+Bsh8hl0BeEi1gynzTqms9UzqwBVVufWuvsdpGz2NnU2Nuh5mMuTpa32OSR/46WklHFd6NPUWZmF2MJtg= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a05:6000:400f:b0:336:6611:7936 with SMTP id cp15-20020a056000400f00b0033666117936mr7753wrb.11.1702908007241; Mon, 18 Dec 2023 06:00:07 -0800 (PST) Date: Mon, 18 Dec 2023 13:58:59 +0000 In-Reply-To: <20231218135859.2513568-2-sebastianene@google.com> Mime-Version: 1.0 References: <20231218135859.2513568-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231218135859.2513568-11-sebastianene@google.com> Subject: [PATCH v4 09/10] arm64: ptdump: Interpret pKVM ownership annotations 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-20231218_060008_802881_724C9525 X-CRM114-Status: GOOD ( 17.45 ) 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 When pKVM is enabled the software bits are used to keep track of the page sharing state. Interepret these fields when pKVM is enabled and print the sharing state. Move the definitions to common pagetable header. Signed-off-by: Sebastian Ene --- arch/arm64/include/asm/kvm_pgtable.h | 26 ++++++++++ arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 26 ---------- arch/arm64/kvm/ptdump.c | 47 +++++++++++++++++-- 3 files changed, 69 insertions(+), 30 deletions(-) diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h index 37f2a8532..7f654d4aa 100644 --- a/arch/arm64/include/asm/kvm_pgtable.h +++ b/arch/arm64/include/asm/kvm_pgtable.h @@ -87,6 +87,13 @@ typedef u64 kvm_pte_t; */ #define KVM_INVALID_PTE_LOCKED BIT(10) +/* This corresponds to page-table locking order */ +enum pkvm_component_id { + PKVM_ID_HOST, + PKVM_ID_HYP, + PKVM_ID_FFA, +}; + static inline bool kvm_pte_valid(kvm_pte_t pte) { return pte & KVM_PTE_VALID; @@ -230,6 +237,25 @@ enum kvm_pgtable_prot { KVM_PGTABLE_PROT_SW3 = BIT(58), }; +/* + * SW bits 0-1 are reserved to track the memory ownership state of each page: + * 00: The page is owned exclusively by the page-table owner. + * 01: The page is owned by the page-table owner, but is shared + * with another entity. + * 10: The page is shared with, but not owned by the page-table owner. + * 11: Reserved for future use (lending). + */ +enum pkvm_page_state { + PKVM_PAGE_OWNED = 0ULL, + PKVM_PAGE_SHARED_OWNED = KVM_PGTABLE_PROT_SW0, + PKVM_PAGE_SHARED_BORROWED = KVM_PGTABLE_PROT_SW1, + __PKVM_PAGE_RESERVED = KVM_PGTABLE_PROT_SW0 | + KVM_PGTABLE_PROT_SW1, + + /* Meta-states which aren't encoded directly in the PTE's SW bits */ + PKVM_NOPAGE, +}; + #define KVM_PGTABLE_PROT_RW (KVM_PGTABLE_PROT_R | KVM_PGTABLE_PROT_W) #define KVM_PGTABLE_PROT_RWX (KVM_PGTABLE_PROT_RW | KVM_PGTABLE_PROT_X) diff --git a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h index ca8f76915..677686b86 100644 --- a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +++ b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h @@ -14,25 +14,6 @@ #include #include -/* - * SW bits 0-1 are reserved to track the memory ownership state of each page: - * 00: The page is owned exclusively by the page-table owner. - * 01: The page is owned by the page-table owner, but is shared - * with another entity. - * 10: The page is shared with, but not owned by the page-table owner. - * 11: Reserved for future use (lending). - */ -enum pkvm_page_state { - PKVM_PAGE_OWNED = 0ULL, - PKVM_PAGE_SHARED_OWNED = KVM_PGTABLE_PROT_SW0, - PKVM_PAGE_SHARED_BORROWED = KVM_PGTABLE_PROT_SW1, - __PKVM_PAGE_RESERVED = KVM_PGTABLE_PROT_SW0 | - KVM_PGTABLE_PROT_SW1, - - /* Meta-states which aren't encoded directly in the PTE's SW bits */ - PKVM_NOPAGE, -}; - #define PKVM_PAGE_STATE_PROT_MASK (KVM_PGTABLE_PROT_SW0 | KVM_PGTABLE_PROT_SW1) static inline enum kvm_pgtable_prot pkvm_mkstate(enum kvm_pgtable_prot prot, enum pkvm_page_state state) @@ -53,13 +34,6 @@ struct host_mmu { }; extern struct host_mmu host_mmu; -/* This corresponds to page-table locking order */ -enum pkvm_component_id { - PKVM_ID_HOST, - PKVM_ID_HYP, - PKVM_ID_FFA, -}; - extern unsigned long hyp_nr_cpus; int __pkvm_prot_finalize(void); diff --git a/arch/arm64/kvm/ptdump.c b/arch/arm64/kvm/ptdump.c index 0ad7944e5..4296e739f 100644 --- a/arch/arm64/kvm/ptdump.c +++ b/arch/arm64/kvm/ptdump.c @@ -52,6 +52,11 @@ static bool is_fwb_enabled(const struct pg_state *m) return fwb_enabled; } +static bool is_pkvm_enabled(const struct pg_state *m) +{ + return is_protected_kvm_enabled(); +} + static const struct prot_bits stage2_pte_bits[] = { { .mask = PTE_VALID, @@ -113,22 +118,56 @@ static const struct prot_bits stage2_pte_bits[] = { .val = PTE_S2_MEMATTR(MT_S2_FWB_NORMAL) | PTE_VALID, .set = "MEM/NORMAL FWB", .feature_on = is_fwb_enabled, + }, { + .mask = KVM_INVALID_PTE_OWNER_MASK | PTE_VALID, + .val = FIELD_PREP_CONST(KVM_INVALID_PTE_OWNER_MASK, + PKVM_ID_HYP), + .set = "HYP", + }, { + .mask = KVM_INVALID_PTE_OWNER_MASK | PTE_VALID, + .val = FIELD_PREP_CONST(KVM_INVALID_PTE_OWNER_MASK, + PKVM_ID_FFA), + .set = "FF-A", + }, { + .mask = __PKVM_PAGE_RESERVED | PTE_VALID, + .val = PKVM_PAGE_OWNED | PTE_VALID, + .set = "PKVM_PAGE_OWNED", + .feature_on = is_pkvm_enabled, + }, { + .mask = __PKVM_PAGE_RESERVED | PTE_VALID, + .val = PKVM_PAGE_SHARED_OWNED | PTE_VALID, + .set = "PKVM_PAGE_SHARED_OWNED", + .feature_on = is_pkvm_enabled, + }, { + .mask = __PKVM_PAGE_RESERVED | PTE_VALID, + .val = PKVM_PAGE_SHARED_BORROWED | PTE_VALID, + .set = "PKVM_PAGE_SHARED_BORROWED", + .feature_on = is_pkvm_enabled, + }, { + .mask = PKVM_NOPAGE | PTE_VALID, + .val = PKVM_NOPAGE, + .set = "PKVM_NOPAGE", + .feature_on = is_pkvm_enabled, }, { .mask = KVM_PGTABLE_PROT_SW0, .val = KVM_PGTABLE_PROT_SW0, - .set = "SW0", /* PKVM_PAGE_SHARED_OWNED */ + .set = "SW0", + .feature_off = is_pkvm_enabled, }, { - .mask = KVM_PGTABLE_PROT_SW1, + .mask = KVM_PGTABLE_PROT_SW1, .val = KVM_PGTABLE_PROT_SW1, - .set = "SW1", /* PKVM_PAGE_SHARED_BORROWED */ + .set = "SW1", + .feature_off = is_pkvm_enabled, }, { - .mask = KVM_PGTABLE_PROT_SW2, + .mask = KVM_PGTABLE_PROT_SW2, .val = KVM_PGTABLE_PROT_SW2, .set = "SW2", + .feature_off = is_pkvm_enabled, }, { .mask = KVM_PGTABLE_PROT_SW3, .val = KVM_PGTABLE_PROT_SW3, .set = "SW3", + .feature_off = is_pkvm_enabled, }, }; From patchwork Mon Dec 18 13:59:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 13497036 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 CB7A2C46CCD for ; Mon, 18 Dec 2023 14:00:55 +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=ToTk+wjW7EzZmd4T4znrfGj2ZWVsPuviBFJSTBuEE1o=; b=hT1gig1U207omIhR07KAYOX7cn bHikPwNKBg1CGLp+ymoz+/CTV6/zWMNDNheMoWNAYq4reOzpqiHE3freOxX782+NtjWwR48rfT8yx RuM5YoKNDI1yTrXfcH5vwL4xosbhJen4r/gM07VHk4uQtDY6JFH35iX+buql+5eKSgD24mNiBWNNN BIkFCXGkmI34ltakk42Hys0et7fRq/YOdYiUsCzUbFPA7JPLcvRrEH5KQdwIMHb5mmjPghpuEZdd5 v80NdJKrk4GmlvfnR8aigMy8TFer2fen+Byqw/THN+v1DxZJfAhl1K0KUhXq1H9ZymmJIR52EvG0l vf1IFL8w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rFEAT-00Awtp-0d; Mon, 18 Dec 2023 14:00:25 +0000 Received: from mail-wm1-x34a.google.com ([2a00:1450:4864:20::34a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rFEAF-00Awkr-0P for linux-arm-kernel@lists.infradead.org; Mon, 18 Dec 2023 14:00:12 +0000 Received: by mail-wm1-x34a.google.com with SMTP id 5b1f17b1804b1-40c2c6f0893so25072535e9.3 for ; Mon, 18 Dec 2023 06:00:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702908009; x=1703512809; 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=5rhLwUQ1ywrBkBuwEQIDGD2rpJ8EK3ecZScs+lwiHfY=; b=Lxi5W8Z3eTabP5GSUva6+ehW2bTJOlHpuMjuIX7U0kkjZ5dvBn/O9HKuXjumWe6jJw 0W+AIgVZjSRIhvGPDeyOAciB0oNmojaoIIiUCcoSo+H/5vo+eNQ0ejGhE6jddnfrFb3z z4gQkzXGG4yLCGMTn4QFoyuj8GQClktVP2he8aDhRltaj0ygrBAJ7utzQzgf+I5AQroa l3Yogbd0iGhElZXA+2oNT3lNHEAVZhblAiD9mR9npxtOKnQLLRUhh+FJB9p9xDBetqRY 4aZDUpE0wcM6Gwa4UX5Az82fNIqNqULM8Vun3m/hdkZBbt05NG288rxVArA5p2rgPylc NtoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702908009; x=1703512809; 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=5rhLwUQ1ywrBkBuwEQIDGD2rpJ8EK3ecZScs+lwiHfY=; b=fUxO2Elomt1NoTVmLVDdASSphqpJ6v6KhUXO6DhwKIlDKp7RRzG7d8S2QDAM6h3KUF hWDieCIw/bWZnO6IdLh1JtKkhktOPwPvq+7HC4IK+2V+exTYp/8YZoOCpX4TUtn6BxYk dnjel66KdNZsLPpnDipxDOy7OHbbkLrAQQCGhMgZBk0KbVL3Wj4ZbISVl+Dq19sVeOlG 2VmcqQseN1MFqCppM8z7lH3hzfhu8+XKIyNTwiahee2zppycHBhV9/KIpkVozgxA+SHJ OyGMW7+xSWLbdLW3C78RRoeEVJ/GDZX21WVN02L+YdVAzt6qo8xblF4kFrxq0L6WkXhN A/ww== X-Gm-Message-State: AOJu0YzZ6DXJLwncRNRtlG+I9EO9qca8azXCJk+n3MMD5gWA97m03s5o MlsC23cjI7ZsxqyTfIKJi2sc020l89CWeciZ9wM= X-Google-Smtp-Source: AGHT+IHEoseKyYRphf3NMbsIOuRZOeFVPDEEXSm6Ts7CS70/UVcEBiTuIn3NWzoz1j965+zBJY2oLLctkSjyPvjToeI= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a05:600c:3b95:b0:407:8ee2:998c with SMTP id n21-20020a05600c3b9500b004078ee2998cmr167649wms.3.1702908009341; Mon, 18 Dec 2023 06:00:09 -0800 (PST) Date: Mon, 18 Dec 2023 13:59:00 +0000 In-Reply-To: <20231218135859.2513568-2-sebastianene@google.com> Mime-Version: 1.0 References: <20231218135859.2513568-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231218135859.2513568-12-sebastianene@google.com> Subject: [PATCH v4 10/10] arm64: ptdump: Add guest stage-2 pagetables dumping 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-20231218_060011_165352_7B165781 X-CRM114-Status: GOOD ( 16.08 ) 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 Register a debugfs file on guest creation to be able to view their second translation tables with ptdump. This assumes that the host is in control of the guest stage-2 and has direct access to the pagetables. Signed-off-by: Sebastian Ene --- arch/arm64/kvm/debug.c | 6 ++++++ arch/arm64/kvm/kvm_ptdump.h | 3 +++ arch/arm64/kvm/ptdump.c | 35 ++++++++++++++++++++++++++++++++--- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c index 8725291cb..7c4c2902d 100644 --- a/arch/arm64/kvm/debug.c +++ b/arch/arm64/kvm/debug.c @@ -13,6 +13,7 @@ #include #include #include +#include #include "trace.h" @@ -342,3 +343,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_register_guest(kvm); +} diff --git a/arch/arm64/kvm/kvm_ptdump.h b/arch/arm64/kvm/kvm_ptdump.h index 98b595ce8..5f5a455d0 100644 --- a/arch/arm64/kvm/kvm_ptdump.h +++ b/arch/arm64/kvm/kvm_ptdump.h @@ -6,13 +6,16 @@ #ifndef __KVM_PTDUMP_H #define __KVM_PTDUMP_H +#include #include #ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS void kvm_ptdump_register_host(void); +int kvm_ptdump_register_guest(struct kvm *kvm); #else static inline void kvm_ptdump_register_host(void) { } +static inline int kvm_ptdump_register_guest(struct kvm *kvm) { return -1; } #endif /* CONFIG_PTDUMP_STAGE2_DEBUGFS */ #endif /* __KVM_PTDUMP_H */ diff --git a/arch/arm64/kvm/ptdump.c b/arch/arm64/kvm/ptdump.c index 4296e739f..62a753d6b 100644 --- a/arch/arm64/kvm/ptdump.c +++ b/arch/arm64/kvm/ptdump.c @@ -181,6 +181,8 @@ static int kvm_ptdump_open(struct inode *inode, struct file *file) info = reg->get_ptdump_info(reg); if (!info) return -ENOMEM; + } else { + info = inode->i_private; } if (!reg->show_ptdump_info) @@ -239,15 +241,14 @@ static int kvm_ptdump_visitor(const struct kvm_pgtable_visit_ctx *ctx, return 0; } -static int kvm_ptdump_show(struct seq_file *m, void *) +static int kvm_ptdump_show_common(struct seq_file *m, + struct kvm_pgtable *pgtable) { u64 ipa_size; char ipa_description[32]; struct pg_state st; struct addr_marker ipa_addr_markers[3] = {0}; struct pg_level pg_level_descr[KVM_PGTABLE_MAX_LEVELS] = {0}; - struct kvm_pgtable_snapshot *snapshot = m->private; - struct kvm_pgtable *pgtable = &snapshot->pgtable; struct kvm_pgtable_walker walker = (struct kvm_pgtable_walker) { .cb = kvm_ptdump_visitor, .arg = &st, @@ -282,6 +283,26 @@ static int kvm_ptdump_show(struct seq_file *m, void *) return kvm_pgtable_walk(pgtable, 0, ipa_size, &walker); } +static int kvm_host_ptdump_show(struct seq_file *m, void *) +{ + struct kvm_pgtable_snapshot *snapshot = m->private; + + return kvm_ptdump_show_common(m, &snapshot->pgtable); +} + +static int kvm_ptdump_show(struct seq_file *m, void *) +{ + struct kvm *guest_kvm = m->private; + struct kvm_s2_mmu *mmu = &guest_kvm->arch.mmu; + int ret; + + write_lock(&guest_kvm->mmu_lock); + ret = kvm_ptdump_show_common(m, mmu->pgt); + write_unlock(&guest_kvm->mmu_lock); + + return ret; +} + static void kvm_ptdump_debugfs_register(struct kvm_ptdump_register *reg, const char *name, struct dentry *parent) { @@ -393,11 +414,19 @@ void kvm_ptdump_register_host(void) host_reg.get_ptdump_info = kvm_host_get_ptdump_info; host_reg.put_ptdump_info = kvm_host_put_ptdump_info; + host_reg.show_ptdump_info = kvm_host_ptdump_show; kvm_ptdump_debugfs_register(&host_reg, "host_page_tables", kvm_debugfs_dir); } +int kvm_ptdump_register_guest(struct kvm *kvm) +{ + debugfs_create_file("stage2_page_tables", 0400, kvm->debugfs_dentry, + kvm, &kvm_ptdump_fops); + return 0; +} + static int __init kvm_host_ptdump_init(void) { host_reg.priv = (void *)host_s2_pgtable_pages();