From patchwork Tue Feb 20 15:10:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 203638 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp465913dyc; Tue, 20 Feb 2024 07:13:30 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUXGFIHyW9fb++jcHnv3GeWLSWwWtSliymx7uxquQTqM8JDVlbP3cs1AmMQrz6IQTfiurrjcnw38wURzBMcEeoYW1XvZg== X-Google-Smtp-Source: AGHT+IGnqTYzkgoJuY9Y4XQDdO+ay6m06kEzGirmh7TZTGdzmTmylvDe/IextfBqW9e1NkTiKrv1 X-Received: by 2002:a05:6402:17cc:b0:564:9019:f865 with SMTP id s12-20020a05640217cc00b005649019f865mr5064544edy.12.1708442009917; Tue, 20 Feb 2024 07:13:29 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708442009; cv=pass; d=google.com; s=arc-20160816; b=Mfus6Z0YiVWyCdhPV0mZzdEwONxG+LMPsvVlXnM5Dfr9dHjXPJWfRyECMVyROKbvnQ y771Fzid7fLH1BZ5yaMIcMkonLBu9d88ZMMn5EC204JiORclYEyHqXxg2YQb80/xGAr8 yOlgHwO7zkv5bOvexYUB0lrEk4T4XXvEF0Zp4MjnI05X4lZ9BUtPnTOhx4VhGt6fLBqG eOmxXaPRuGwOrWZqFhku/fe3bCDx8KKc0PjcqAEw5m79KmS3WKRsSduE9OD++dh94hER w48oFGX6utdpxvLEVn2myMtib2VRuiQo5dL2SoCEaw6wiXMGO6SKoALBufzzgx6s9K2a Nkmg== 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=ZHfwmvLyQ0+qgqD3PE0n64rmEHSiFk4davoaQ8asvC8=; fh=Ffa9zuGqDmXz14IVUH9fIb0VBC6NurzcFeLwtQTm8TE=; b=vOauhQLiOR9TciLBBIZq9nZWQZg2a23hhCCbdmImymRl408ySAsRNXmYDeF22qP5s4 BLIDM5pjUVJmzTbnYHZY6urQLtzRWH8NaJwlLqHIYr/IFkGu48ci9HJaHXezxIzjFOCR U6I8ztlOuJW3JX3maxs+WxHnl1tc6POrtkDIDMU0+J0AzC5rof9hybAqrB1ma84/ReCJ 8lFdhMoyebooZbTfoI1GxpJGwa8Swimtm/WnfRSVeEdIUP5LHduvqiZ9XdDQ7ftfDI2+ IpxkaA+2ffo6xNpw9OBd8KEMT2jCLWCkavP5Y3Fap1zyMtN/bYa37qLs4SP13Ox3JciR tI5g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Iqc5rFB4; 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-73226-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-73226-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id n20-20020a5099d4000000b00564566672b6si2335323edb.303.2024.02.20.07.13.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 07:13:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-73226-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Iqc5rFB4; 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-73226-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-73226-ouuuleilei=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 am.mirrors.kernel.org (Postfix) with ESMTPS id 3431A1F22485 for ; Tue, 20 Feb 2024 15:13:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 548D074E0F; Tue, 20 Feb 2024 15:11:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Iqc5rFB4" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.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 23251763FD for ; Tue, 20 Feb 2024 15:11:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708441906; cv=none; b=IjbMFHcUsNFxKLQDDRpDYbOghCKkBDBHrTVgkCu9TeJ2fEx08d7w3lKbD1ljdQqeQLWvdJLn5RqkWhg/3tMYyJH6RszpzAvsIMLiy2zEOFTP1SpK1PpP3Zx+uyvMdiAEQtHb0vKtp8jZvPm5LmpSejYo0tRmEHUtOXwYkNwHzJg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708441906; c=relaxed/simple; bh=osQOTfWyBrFUVH8hZunBwsrwKsnogQjJ3uD+EhyAPM4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=G2iozxCoPsO+WcguAJTD5P8HRZC0vEj6ImOckFmtmhiZMCtvXhssgk5TK2mcmQjOdj+s+X4WVgdqYTZeOIp4siUbSMYHpFgcH/j62BEVKbiEFGGbZyTkP3RBxu9zxpw1QQzxPYYUH9/W8BR4IA9JY7J3Hx8kW1B2pmqRoKfAmdU= 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=Iqc5rFB4; arc=none smtp.client-ip=209.85.219.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-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc6ceade361so10386963276.0 for ; Tue, 20 Feb 2024 07:11:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708441904; x=1709046704; 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=ZHfwmvLyQ0+qgqD3PE0n64rmEHSiFk4davoaQ8asvC8=; b=Iqc5rFB4b6Ws5GRhffS4rw1vcOaJR8md+JEvWtXfqcpDh6OezG1stx2JUbsadkdU+O GZGzJWvtvFdpVLb2kyZGdJe7pz0UQjZaHoWMlEXr3W4tX1CPPHj11t4fT+x2000zyKvw u/H0dTDmzryxxMnPKttXvoASbgpponrp7U5WcKBeQJmMrA1uyvx3fyRsQG5DtF450rfF tdQLWZaC7aapxDTrheJQ6zXwgTcCW0w/spro6ktm07oP20G4amR7U/i51cfUSBsP52St xw8chp2dZW9O16a958TURA/SFFbrOOPvihBoVribsexTntxfzR9UWn/Sbz7vzDrz2Ldd RmLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708441904; x=1709046704; 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=ZHfwmvLyQ0+qgqD3PE0n64rmEHSiFk4davoaQ8asvC8=; b=gynp/c24y60MF4trvx2wVzIImjffwDEAHYGtdIbe6u6Dd2k5ORsIBUu9iUuXuS1EOn HNbsZkahJUloozd6lUId6uwAskrpIMYhWSJueuZqgpB4PR+GvzEGKE/bocLDtPrxhyXr cRSm4h8CbovzIf+zwFzTCxx/WNJP5H48+gszlQWkEIDBEmNGvuTQY95mzlvB38OEXXLZ zwKxhfWVpG/YVjlqTcE+8oahTcN4Eavoe3wrnLqV7yArXe3C4zfmdMir4LZGLgw8EZ/3 RBR3A+Z3aIfNkCQ/X2It+8RWKFD4fNAds+kmdPdfKJTG4uWy5ffh0qGB4N+S4IA2KGd0 YKlQ== X-Forwarded-Encrypted: i=1; AJvYcCXHxbFpx340cq9v2aEL1+Ywky2joJTs4HGy472cDYClo/KAfb+vRKOa1YX00zwxQx4ob2IiUtdMtc9AYJvFtUti0msPJaZJw0lBF2Rr X-Gm-Message-State: AOJu0YxG97k3J2ZAWKJAXyGHyPqaoHag1utuR5bvLjvlR0PHZ53Aum3h YHjb7lY4pUpY9KmYB326PSBb6cIrEfcPms2O4JnqTAFYoEEDm1Fnob2SBGoC7Utnu+zgR0XkCqR +VUgZGg5REtPX70OtReBDV4X6tQ== X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a25:c78d:0:b0:dcd:ad52:6932 with SMTP id w135-20020a25c78d000000b00dcdad526932mr3864927ybe.5.1708441904161; Tue, 20 Feb 2024 07:11:44 -0800 (PST) Date: Tue, 20 Feb 2024 15:10:34 +0000 In-Reply-To: <20240220151035.327199-1-sebastianene@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240220151035.327199-1-sebastianene@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Message-ID: <20240220151035.327199-6-sebastianene@google.com> Subject: [PATCH v6 5/6] 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-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791431288676758760 X-GMAIL-MSGID: 1791431288676758760 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 | 146 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 139 insertions(+), 7 deletions(-) diff --git a/arch/arm64/kvm/ptdump.c b/arch/arm64/kvm/ptdump.c index 9b04c24bb9be..2c4e0c122d23 100644 --- a/arch/arm64/kvm/ptdump.c +++ b/arch/arm64/kvm/ptdump.c @@ -14,6 +14,61 @@ #include +#define MARKERS_LEN (2) +#define KVM_PGTABLE_LEVELS (KVM_PGTABLE_LAST_LEVEL + 1) + +struct kvm_ptdump_guest_state { + struct kvm *kvm; + struct pg_state parser_state; + struct addr_marker ipa_marker[MARKERS_LEN]; + struct pg_level level[KVM_PGTABLE_LEVELS]; + struct ptdump_range range[MARKERS_LEN]; +}; + +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", + }, +}; + static int kvm_ptdump_visitor(const struct kvm_pgtable_visit_ctx *ctx, enum kvm_pgtable_walk_flags visit) { @@ -37,15 +92,78 @@ static int kvm_ptdump_show_common(struct seq_file *m, return kvm_pgtable_walk(pgtable, 0, BIT(pgtable->ia_bits), &walker); } +static int 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 = 0; + + if (start_lvl > 2) { + pr_err("invalid start_lvl %u\n", start_lvl); + return -EINVAL; + } + + for (i = 0; i < ARRAY_SIZE(stage2_pte_bits); i++) + mask |= 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; + } + + if (start_lvl > 0) + level[start_lvl].name = level_names[0]; + + return 0; +} + +static struct kvm_ptdump_guest_state +*kvm_ptdump_parser_init(struct kvm *kvm) +{ + struct kvm_ptdump_guest_state *st; + struct kvm_s2_mmu *mmu = &kvm->arch.mmu; + struct kvm_pgtable *pgtable = mmu->pgt; + int ret; + + st = kzalloc(sizeof(struct kvm_ptdump_guest_state), GFP_KERNEL_ACCOUNT); + if (!st) + return NULL; + + ret = kvm_ptdump_build_levels(&st->level[0], pgtable->start_level); + if (ret) + goto free_with_state; + + st->ipa_marker[0].name = "Guest IPA"; + st->ipa_marker[1].start_address = BIT(pgtable->ia_bits); + st->range[0].end = BIT(pgtable->ia_bits); + + st->kvm = kvm; + st->parser_state = (struct pg_state) { + .marker = &st->ipa_marker[0], + .level = -1, + .pg_level = &st->level[0], + .ptdump.range = &st->range[0], + }; + + return st; +free_with_state: + kfree(st); + return NULL; +} + static int kvm_ptdump_guest_show(struct seq_file *m, void *) { - struct kvm *kvm = m->private; + struct kvm_ptdump_guest_state *st = m->private; + struct kvm *kvm = st->kvm; struct kvm_s2_mmu *mmu = &kvm->arch.mmu; - struct pg_state parser_state = {0}; int ret; + st->parser_state.seq = m; + write_lock(&kvm->mmu_lock); - ret = kvm_ptdump_show_common(m, mmu->pgt, &parser_state); + ret = kvm_ptdump_show_common(m, mmu->pgt, &st->parser_state); write_unlock(&kvm->mmu_lock); return ret; @@ -54,22 +172,36 @@ static int kvm_ptdump_guest_show(struct seq_file *m, void *) static int kvm_ptdump_guest_open(struct inode *m, struct file *file) { struct kvm *kvm = m->i_private; + struct kvm_ptdump_guest_state *st; int ret; - if (!kvm_get_kvm_safe(kvm)) - return -ENOENT; + st = kvm_ptdump_parser_init(kvm); + if (!st) + return -ENOMEM; - ret = single_open(file, kvm_ptdump_guest_show, m->i_private); + if (!kvm_get_kvm_safe(kvm)) { + ret = -ENOENT; + goto free_with_state; + } + + ret = single_open(file, kvm_ptdump_guest_show, st); if (ret < 0) - kvm_put_kvm(kvm); + goto free_with_kvm_ref; return ret; +free_with_kvm_ref: + kvm_put_kvm(kvm); +free_with_state: + kfree(st); + return ret; } static int kvm_ptdump_guest_close(struct inode *m, struct file *file) { struct kvm *kvm = m->i_private; + void *st = ((struct seq_file *)file->private_data)->private; + kfree(st); kvm_put_kvm(kvm); return single_release(m, file); }