From patchwork Wed Feb 7 14:48:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 13548616 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9436BC4828F for ; Wed, 7 Feb 2024 14:49:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=/thY6XdGVSxezgcpr1XQ0Ch+YHonljaTNHDn5s4Ik8I=; b=Ucv9iy+6Mhmx0F3aDcqPVpxNZ7 oicjsxAoHmznymWfp0eO3NwmMIW9ksmdsD6r5V1VdlO+c7BVRhz6fULHZUlwa5gFg2sK2v5+dMkM1 KQKcccVf+XGsbYH3CYoE2GGu68G+TuXWptcpVm3pIbo0AQL4JfYaRUd3Bkn75ksDS5OKUPzl8eew4 fd4Lzlcr+Nby4/k4jfMsFN2Jn/bAVlEN0KFFwrcrWpgXZpAoaShjl4tKgd7tVzixVZj5J78VvmJXi 2Z97xAP8B2isKgQiGlWZQq3ECS5WmlX4lj1k60DCHtIls1A3YgWtL9bSpIAhL0css5p4LjMJ0ojCO wmpe/qhw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rXjF6-0000000AxvE-3uKn; Wed, 07 Feb 2024 14:49:40 +0000 Received: from mail-wm1-x34a.google.com ([2a00:1450:4864:20::34a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rXjEv-0000000AxlN-29Ef for linux-arm-kernel@lists.infradead.org; Wed, 07 Feb 2024 14:49:30 +0000 Received: by mail-wm1-x34a.google.com with SMTP id 5b1f17b1804b1-40efad468a5so4258515e9.2 for ; Wed, 07 Feb 2024 06:49:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707317366; x=1707922166; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=hjt2/pLXLGVmY8in0C2dCgK4OmxemqDeWqGIHoWM86A=; b=gzmSa5eahs0IpjDLf8Re2JzrnAfIrNdgxd5BN5I2aTyOI1QTRS+tXpoGacjYg5GUP3 h/agyd9ib9xhynBid+xgELngY078HyFQ76V/XywRGUkB1UzCQHr5SOer3lXrMFqHuabb RjA1VW8yZUP7SfBmfaZe/pY/CJRc0CO2dzwFWC1YbDdS3SHZQ1z3Sal9rZcDR4XPlJ3w /gZvWQtIos9Ue+JGxXgCC7q3s9LG15TfTKk4ydumEZzxJmQt8f2d+Ox43H8n2Lt5P18L c03vUj9VB0RfGwJ4AumaQ1O/XZeEiXuzRNovAUA9MYMYhZCR76DWU0RvCPqrHWazCmat 7WLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707317366; x=1707922166; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=hjt2/pLXLGVmY8in0C2dCgK4OmxemqDeWqGIHoWM86A=; b=TO4PB4bC8uGy7JyJA9Y6qoufYOymR0iSUHlv5/dNBDudrN4JPmH+fMxIYqWwKZU6Q3 OULcWx8FAa6cP5arCYv8qWfv8QlUTTdnj6mRCFrVqHsKwWGXski1Zkav6bA4gHpmIKC2 88iMhA1QyBM51zwghUknQ/cl5eqk8LEnQZL7lepW+AJAUCvg1XeaNpnw9XrOIm6Wg89P Kh6p5u6sPsD5NvrKOo9pgz4NStqnhNahwFPC8rR9xvSoyvlT1SY515KJ1FR/JwIIelFG K9jmLPO+ZDfKzz4bvjLReYS4FIY2QTq4WyZvN3Q7ilaF2e7237W9F/wCD2exErUI1R0w q7+w== X-Gm-Message-State: AOJu0YwfOqRWUOcw70sYdSvW5Sz9ASsV38WWEJHu2agtwabqBa7YdhNL GjFSOLvYOVZ1yTQ/qp9+aFHksHrvULctjkHT8cqn3gwdMcFa16pqAgBB0LK22ErL633uwxTvPB4 KenGW3vvbwwpse6sspgom1HV87Q== X-Google-Smtp-Source: AGHT+IGS0S84nWWPeysL+mFOFap3NjMcgT1vbRmpG1ZnvwANgcD7I9rDqiPadoprZ3xgO/+lEX4pEIHtW1Yj6kYgmrQ= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a05:600c:5644:b0:40f:df1e:ff56 with SMTP id js4-20020a05600c564400b0040fdf1eff56mr25678wmb.6.1707317366436; Wed, 07 Feb 2024 06:49:26 -0800 (PST) Date: Wed, 7 Feb 2024 14:48:33 +0000 In-Reply-To: <20240207144832.1017815-2-sebastianene@google.com> Mime-Version: 1.0 References: <20240207144832.1017815-2-sebastianene@google.com> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Message-ID: <20240207144832.1017815-6-sebastianene@google.com> Subject: [PATCH v5 4/4] KVM: arm64: Initialize the ptdump parser with stage-2 attributes From: Sebastian Ene To: catalin.marinas@arm.com, gshan@redhat.com, james.morse@arm.com, mark.rutland@arm.com, maz@kernel.org, oliver.upton@linux.dev, rananta@google.com, ricarkol@google.com, ryan.roberts@arm.com, shahuang@redhat.com, suzuki.poulose@arm.com, will@kernel.org, yuzenghui@huawei.com Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-team@android.com, vdonnefort@google.com, Sebastian Ene X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240207_064929_584382_9A417F21 X-CRM114-Status: GOOD ( 17.50 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Define a set of attributes used by the ptdump parser to display the properties of a guest memory region covered by a pagetable descriptor. Build a description of the pagetable levels and initialize the parser with this configuration. Signed-off-by: Sebastian Ene --- arch/arm64/kvm/ptdump.c | 156 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) diff --git a/arch/arm64/kvm/ptdump.c b/arch/arm64/kvm/ptdump.c index a4e984da8aa7..60725d46f17b 100644 --- a/arch/arm64/kvm/ptdump.c +++ b/arch/arm64/kvm/ptdump.c @@ -14,6 +14,69 @@ #include +#define ADDR_MARKER_LEN (2) +#define MARKER_MSG_LEN (32) + +static const struct prot_bits stage2_pte_bits[] = { + { + .mask = PTE_VALID, + .val = PTE_VALID, + .set = " ", + .clear = "F", + }, { + .mask = KVM_PTE_LEAF_ATTR_HI_S2_XN | PTE_VALID, + .val = KVM_PTE_LEAF_ATTR_HI_S2_XN | PTE_VALID, + .set = "XN", + .clear = " ", + }, { + .mask = KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | PTE_VALID, + .val = KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | PTE_VALID, + .set = "R", + .clear = " ", + }, { + .mask = KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | PTE_VALID, + .val = KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | PTE_VALID, + .set = "W", + .clear = " ", + }, { + .mask = KVM_PTE_LEAF_ATTR_LO_S2_AF | PTE_VALID, + .val = KVM_PTE_LEAF_ATTR_LO_S2_AF | PTE_VALID, + .set = "AF", + .clear = " ", + }, { + .mask = PTE_NG, + .val = PTE_NG, + .set = "FnXS", + .clear = " ", + }, { + .mask = PTE_CONT | PTE_VALID, + .val = PTE_CONT | PTE_VALID, + .set = "CON", + .clear = " ", + }, { + .mask = PTE_TABLE_BIT, + .val = PTE_TABLE_BIT, + .set = " ", + .clear = "BLK", + }, { + .mask = KVM_PGTABLE_PROT_SW0, + .val = KVM_PGTABLE_PROT_SW0, + .set = "SW0", /* PKVM_PAGE_SHARED_OWNED */ + }, { + .mask = KVM_PGTABLE_PROT_SW1, + .val = KVM_PGTABLE_PROT_SW1, + .set = "SW1", /* PKVM_PAGE_SHARED_BORROWED */ + }, { + .mask = KVM_PGTABLE_PROT_SW2, + .val = KVM_PGTABLE_PROT_SW2, + .set = "SW2", + }, { + .mask = KVM_PGTABLE_PROT_SW3, + .val = KVM_PGTABLE_PROT_SW3, + .set = "SW3", + }, +}; + static int kvm_ptdump_guest_open(struct inode *inode, struct file *file); static int kvm_ptdump_guest_show(struct seq_file *m, void *); @@ -52,6 +115,94 @@ static int kvm_ptdump_show_common(struct seq_file *m, return kvm_pgtable_walk(pgtable, 0, BIT(pgtable->ia_bits), &walker); } +static void kvm_ptdump_build_levels(struct pg_level *level, u32 start_lvl) +{ + static const char * const level_names[] = {"PGD", "PUD", "PMD", "PTE"}; + u32 i = 0; + u64 mask_lvl = 0; + + if (start_lvl > 2) { + pr_err("invalid start_lvl %u\n", start_lvl); + return; + } + + for (i = 0; i < ARRAY_SIZE(stage2_pte_bits); i++) + mask_lvl |= stage2_pte_bits[i].mask; + + for (i = start_lvl; i <= KVM_PGTABLE_LAST_LEVEL; i++) { + level[i].name = level_names[i]; + level[i].num = ARRAY_SIZE(stage2_pte_bits); + level[i].bits = stage2_pte_bits; + level[i].mask = mask_lvl; + } + + if (start_lvl > 0) + level[start_lvl].name = level_names[0]; +} + +static int kvm_ptdump_parser_init(struct pg_state *st, + struct kvm_pgtable *pgtable, + struct seq_file *m) +{ + struct addr_marker *ipa_addr_marker; + char *marker_msg; + struct pg_level *level_descr; + struct ptdump_range *range; + + ipa_addr_marker = kzalloc(sizeof(struct addr_marker) * ADDR_MARKER_LEN, + GFP_KERNEL_ACCOUNT); + if (!ipa_addr_marker) + return -ENOMEM; + + marker_msg = kzalloc(MARKER_MSG_LEN, GFP_KERNEL_ACCOUNT); + if (!marker_msg) + goto free_with_marker; + + level_descr = kzalloc(sizeof(struct pg_level) * (KVM_PGTABLE_LAST_LEVEL + 1), + GFP_KERNEL_ACCOUNT); + if (!level_descr) + goto free_with_msg; + + range = kzalloc(sizeof(struct ptdump_range) * ADDR_MARKER_LEN, + GFP_KERNEL_ACCOUNT); + if (!range) + goto free_with_level; + + kvm_ptdump_build_levels(level_descr, pgtable->start_level); + + snprintf(marker_msg, MARKER_MSG_LEN, "IPA bits %2u start lvl %1d", + pgtable->ia_bits, pgtable->start_level); + + ipa_addr_marker[0].name = marker_msg; + ipa_addr_marker[1].start_address = BIT(pgtable->ia_bits); + range[0].end = BIT(pgtable->ia_bits); + + st->seq = m; + st->marker = ipa_addr_marker; + st->level = -1, + st->pg_level = level_descr, + st->ptdump.range = range; + return 0; + +free_with_level: + kfree(level_descr); +free_with_msg: + kfree(marker_msg); +free_with_marker: + kfree(ipa_addr_marker); + return -ENOMEM; +} + +static void kvm_ptdump_parser_teardown(struct pg_state *st) +{ + const struct addr_marker *ipa_addr_marker = st->marker; + + kfree(ipa_addr_marker[0].name); + kfree(ipa_addr_marker); + kfree(st->pg_level); + kfree(st->ptdump.range); +} + static int kvm_ptdump_guest_show(struct seq_file *m, void *) { struct kvm *guest_kvm = m->private; @@ -59,10 +210,15 @@ static int kvm_ptdump_guest_show(struct seq_file *m, void *) struct pg_state parser_state = {0}; int ret; + ret = kvm_ptdump_parser_init(&parser_state, mmu->pgt, m); + if (ret) + return ret; + write_lock(&guest_kvm->mmu_lock); ret = kvm_ptdump_show_common(m, mmu->pgt, &parser_state); write_unlock(&guest_kvm->mmu_lock); + kvm_ptdump_parser_teardown(&parser_state); return ret; }