Received: by 2002:a05:7412:2a8a:b0:fc:a2b0:25d7 with SMTP id u10csp354680rdh; Wed, 7 Feb 2024 06:50:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IFkaQ7BBPFJ8G2e3AgcP9pwgdudRutzvR6vjdH9+vwzCdkQGA4rOoqnZBFdRajAJZxHWu8U X-Received: by 2002:a05:6a20:9c8e:b0:19e:9cc8:bf27 with SMTP id mj14-20020a056a209c8e00b0019e9cc8bf27mr4757024pzb.18.1707317407608; Wed, 07 Feb 2024 06:50:07 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707317407; cv=pass; d=google.com; s=arc-20160816; b=VjhwjeIoEbmHF8g9r7xgT3itps2xKUe8HmuC6mTMfqBcNFt+hf23N1hJtWOw0vDGrG tvU6iSO4K4eviUGtbIXHQ1MvnMXT+Lj6ucwPGPDpqfB1eNVKu22QEVCNAT+5KdUyki2m hQkiyLyvjyeG9P/hwdLRd0m+h1SnG29spIonOezO727kCN/KXgjZ9i7tAvX5liX/JwOU Panx7TJ/xFbohlXtMZW6AsyEE/s60o+rpMdOS6rRh5/ehVOBviX0sUi5KF8uVLzCc6VO Ct1Oh882911olht9N7j/egJA7f0EisYxgoSCOm6jjX/UoZd0Nf+lgHJcLKV70PnJQZM2 rFhg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=J1F3sgOZBAQMU7R2Rlac0l+wJat72mlIsX+wYVgtE5c=; fh=ORRNr33tgwxi/0DsJvTiw2vbY5xTYjr3z76QC4Dd41s=; b=f9RRtiOo2q6kcPxoZ2kPXXRzIATzq+ycEgqQL1qJ0VWEGKD4PBCs7/6Cgr6Vor6LvY s10baYZPBT/sDYNW43zJIAA5S7Ff7qoZlxwxFOh2Suv9HEFxRD78J3GLK9c1sV7UMyIS Bso0HqS29mb0WB34KxdYbFGO3iTdv/MbdjhG1Zmz8AvdnjdWiEIurdPOiqw5u9mOW9VE s+yi9X35p/o6mThoNRSkyQYGP/FjhM8cqHi8lRkt1NO9T02zcNQz/u9e90gSTF2bq6Ph /btaUxpNInIn00BnSI6buSKYQL5lCTqhNH59PyO1wRLN/VnshO4vbY7zT5kRwuhMy0im CH6A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=mT7hu7CU; arc=pass (i=1 spf=pass spfdomain=flex--sebastianene.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-56653-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56653-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCVDBKkNM9C/sKMcaF8QhIF3G2H9zsK+zDizv3vpt1Dn5lTQF6ME0zo+mWrqcYQ7mPZP2PxRX4vN1V6eFkso55z1azDKmgqmGDzsEQGAcg== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id it15-20020a056a00458f00b006e02da3a0d8si1628212pfb.276.2024.02.07.06.50.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 06:50:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-56653-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=mT7hu7CU; arc=pass (i=1 spf=pass spfdomain=flex--sebastianene.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-56653-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56653-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 3455C281893 for ; Wed, 7 Feb 2024 14:50:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E73C17FBAC; Wed, 7 Feb 2024 14:49:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="mT7hu7CU" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 364CE7F7CB for ; Wed, 7 Feb 2024 14:49:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707317366; cv=none; b=NmKdxB23+vheXpX/znlkzYogfLYxYeHgoZCmlIyzMMGvP7sYYN/1Vo2nKNUaa/IWQc25MjKZKjcqdY+4oKPFf/dWiGkykoP4M81Qv53bNHIYU+CYMDry1lJLLRXKLWc6vz+pyG1NL4IJLbuLMCLWOe7Jim2ztOu2tvG6C0mfXrs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707317366; c=relaxed/simple; bh=rNGGZUY2dBsJq36eaQaTKYVIz8wf7Lh+KRhuF6gF5W8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=uhogigeS7Z02GU5TAarZXSBQ4WjKuW93vl1qFnXgESzuHNWDzFjDHuZ/dxSPlddUdwN0ysYamkogJSrF1MxqhFSOfZvfCFH70hJus8PUoNsBK7ERRW9nn63P3wb4n/YS7nP1QtEFa44nlD8v8HzlnNHkJ75QQpwnpePUErXjpGQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=mT7hu7CU; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-60484a61250so13945727b3.1 for ; Wed, 07 Feb 2024 06:49:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707317364; x=1707922164; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=J1F3sgOZBAQMU7R2Rlac0l+wJat72mlIsX+wYVgtE5c=; b=mT7hu7CUGFOIS+XLPewRqWmTVYeEXNsJCow9FtMkZvi4rOgj5qMvlSo2CPDrEJHc3A T1J7oTJWracl5M8TZxDssHBdqeahrPGQfMAqwDctQRsnOEup2inS0VUxWApvwGpEYpsq qAn+aBEmza8uX/stXyWMuCB+t4zjqDhKqojNLNif57sBKFoY5ek6YMMFDdvAT/yi4X2x vWZCJq17Auf9wH4d+YmtqRcrpBCCdeW1zg5+YKuZIMNNtfr2svs7KaVsIXDboXZJl8rZ aZmMF4L8iDs/SmzUTgheCZKErzuvzSS6AUhBGCvNnQxRK9khFTdkw4IqdIJ2A/QsgdEO Pn8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707317364; x=1707922164; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=J1F3sgOZBAQMU7R2Rlac0l+wJat72mlIsX+wYVgtE5c=; b=cqCod8HM7TMCNxXvRjPNGZePm9nAgq8DildUYknDIoJ84eOH26Jm3TrbMZV8pa0iCR ymUlfFM9bqDcIsIXnQ+jwoaUH44OXNOLyYjN2fpLdlL1MPUgMofPiblriHtSQDodfaix QHw8xYqJSc8P2cuPqUGwUDVggHTJ7FvHG6jwxUNmVE/teJmluGSYnwlB6yJ2nWH1e5+W aebgjbIM6K806r+AW5EgpMl5s1aSOMzDFnY7o5E4VeHCWstg9rQBkJeU4A7NE6KkABra BR2FNwH6jDjIaS6DfW1V+uBA0geOfT0qVe5ZvCPlgq5X/DdPj7PSpjtWPHpoT5dzP09s k1XA== X-Gm-Message-State: AOJu0YzOuyQuVqGUo2od9oJl2MgTA2LPgUdD0+1oFym0H5XWb1hfjE6m DGjFAIDmodq8+EqPIPVd0deq47zvrH/2Ama/+bB5FUjoehzbVNxTcgwt0r1s2P8UwpkrvxbDoPJ +uIyMlRt0g52rlmOc53aHONpkEA== X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a05:6902:2187:b0:dc2:1cd6:346e with SMTP id dl7-20020a056902218700b00dc21cd6346emr1222350ybb.8.1707317364293; Wed, 07 Feb 2024 06:49:24 -0800 (PST) Date: Wed, 7 Feb 2024 14:48:32 +0000 In-Reply-To: <20240207144832.1017815-2-sebastianene@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240207144832.1017815-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Message-ID: <20240207144832.1017815-5-sebastianene@google.com> Subject: [PATCH v5 3/4] KVM: arm64: Register ptdump with debugfs on guest creation From: Sebastian Ene To: catalin.marinas@arm.com, gshan@redhat.com, james.morse@arm.com, mark.rutland@arm.com, maz@kernel.org, oliver.upton@linux.dev, rananta@google.com, ricarkol@google.com, ryan.roberts@arm.com, shahuang@redhat.com, suzuki.poulose@arm.com, will@kernel.org, yuzenghui@huawei.com Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-team@android.com, vdonnefort@google.com, Sebastian Ene Content-Type: text/plain; charset="UTF-8" While arch/*/mem/ptdump handles the kernel pagetable dumping code, introduce KVM/ptdump which shows the guest stage-2 pagetables. The separation is necessary because most of the definitions from the stage-2 pagetable reside in the KVM path and we will be invoking functionality **specific** to KVM. When a guest is created, register a new file entry under the guest debugfs dir which allows userspace to show the contents of the guest stage-2 pagetables when accessed. Signed-off-by: Sebastian Ene --- arch/arm64/kvm/Kconfig | 13 ++++++ arch/arm64/kvm/Makefile | 1 + arch/arm64/kvm/debug.c | 7 ++++ arch/arm64/kvm/kvm_ptdump.h | 20 ++++++++++ arch/arm64/kvm/ptdump.c | 79 +++++++++++++++++++++++++++++++++++++ 5 files changed, 120 insertions(+) create mode 100644 arch/arm64/kvm/kvm_ptdump.h create mode 100644 arch/arm64/kvm/ptdump.c diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig index 6c3c8ca73e7f..28097dd72174 100644 --- a/arch/arm64/kvm/Kconfig +++ b/arch/arm64/kvm/Kconfig @@ -68,4 +68,17 @@ config PROTECTED_NVHE_STACKTRACE If unsure, or not using protected nVHE (pKVM), say N. +config PTDUMP_STAGE2_DEBUGFS + bool "Present the stage-2 pagetables to debugfs" + depends on PTDUMP_DEBUGFS && KVM + default n + help + Say Y here if you want to show the stage-2 kernel pagetables + layout in a debugfs file. This information is only useful for kernel developers + who are working in architecture specific areas of the kernel. + It is probably not a good idea to enable this feature in a production + kernel. + + If in doubt, say N. + endif # VIRTUALIZATION diff --git a/arch/arm64/kvm/Makefile b/arch/arm64/kvm/Makefile index c0c050e53157..190eac17538c 100644 --- a/arch/arm64/kvm/Makefile +++ b/arch/arm64/kvm/Makefile @@ -23,6 +23,7 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ vgic/vgic-its.o vgic/vgic-debug.o kvm-$(CONFIG_HW_PERF_EVENTS) += pmu-emul.o pmu.o +kvm-$(CONFIG_PTDUMP_STAGE2_DEBUGFS) += ptdump.o always-y := hyp_constants.h hyp-constants.s diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c index 8725291cb00a..aef52836cd90 100644 --- a/arch/arm64/kvm/debug.c +++ b/arch/arm64/kvm/debug.c @@ -14,6 +14,8 @@ #include #include +#include + #include "trace.h" /* These are the bits of MDSCR_EL1 we may manipulate */ @@ -342,3 +344,8 @@ void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu) vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_SPE); vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_TRBE); } + +int kvm_arch_create_vm_debugfs(struct kvm *kvm) +{ + return kvm_ptdump_guest_register(kvm); +} diff --git a/arch/arm64/kvm/kvm_ptdump.h b/arch/arm64/kvm/kvm_ptdump.h new file mode 100644 index 000000000000..a7c00a28481b --- /dev/null +++ b/arch/arm64/kvm/kvm_ptdump.h @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) Google, 2023 + * Author: Sebastian Ene + */ + +#ifndef __KVM_PTDUMP_H +#define __KVM_PTDUMP_H + +#include +#include + + +#ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS +int kvm_ptdump_guest_register(struct kvm *kvm); +#else +static inline int kvm_ptdump_guest_register(struct kvm *kvm) { return 0; } +#endif /* CONFIG_PTDUMP_STAGE2_DEBUGFS */ + +#endif /* __KVM_PTDUMP_H */ diff --git a/arch/arm64/kvm/ptdump.c b/arch/arm64/kvm/ptdump.c new file mode 100644 index 000000000000..a4e984da8aa7 --- /dev/null +++ b/arch/arm64/kvm/ptdump.c @@ -0,0 +1,79 @@ +// SPDX-License-Identifier: GPL-2.0-only +// +// Debug helper used to dump the stage-2 pagetables of the system and their +// associated permissions. +// +// Copyright (C) Google, 2023 +// Author: Sebastian Ene + +#include +#include +#include + +#include +#include + + +static int kvm_ptdump_guest_open(struct inode *inode, struct file *file); +static int kvm_ptdump_guest_show(struct seq_file *m, void *); + +static const struct file_operations kvm_ptdump_guest_fops = { + .open = kvm_ptdump_guest_open, + .read = seq_read, + .llseek = seq_lseek, + .release = single_release, +}; + +static int kvm_ptdump_guest_open(struct inode *inode, struct file *file) +{ + return single_open(file, kvm_ptdump_guest_show, inode->i_private); +} + +static int kvm_ptdump_visitor(const struct kvm_pgtable_visit_ctx *ctx, + enum kvm_pgtable_walk_flags visit) +{ + struct pg_state *st = ctx->arg; + struct ptdump_state *pt_st = &st->ptdump; + + note_page(pt_st, ctx->addr, ctx->level, ctx->old); + return 0; +} + +static int kvm_ptdump_show_common(struct seq_file *m, + struct kvm_pgtable *pgtable, + struct pg_state *parser_state) +{ + struct kvm_pgtable_walker walker = (struct kvm_pgtable_walker) { + .cb = kvm_ptdump_visitor, + .arg = parser_state, + .flags = KVM_PGTABLE_WALK_LEAF, + }; + + return kvm_pgtable_walk(pgtable, 0, BIT(pgtable->ia_bits), &walker); +} + +static int kvm_ptdump_guest_show(struct seq_file *m, void *) +{ + struct kvm *guest_kvm = m->private; + struct kvm_s2_mmu *mmu = &guest_kvm->arch.mmu; + struct pg_state parser_state = {0}; + int ret; + + write_lock(&guest_kvm->mmu_lock); + ret = kvm_ptdump_show_common(m, mmu->pgt, &parser_state); + write_unlock(&guest_kvm->mmu_lock); + + return ret; +} + +int kvm_ptdump_guest_register(struct kvm *kvm) +{ + struct dentry *parent; + + parent = debugfs_create_file("stage2_page_tables", 0400, + kvm->debugfs_dentry, kvm, + &kvm_ptdump_guest_fops); + if (IS_ERR(parent)) + return PTR_ERR(parent); + return 0; +} -- 2.43.0.594.gd9cf4e227d-goog