Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp1792861rdb; Tue, 20 Feb 2024 07:13:09 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUAUiy/T9GZxPs/fGWY2qSk/nWolsqOmU510YtYSQCbQMqCzd4dMKW7l80RorLHWZUG3JB425uueLdewrYJ7wUI5vM/qlk7e0ecnOIdkg== X-Google-Smtp-Source: AGHT+IFUvfXnHt5rweR8CYDGxJ0b5bIThNfhOBIEYUCs3T9abj/lJ6GwIlsDTKtRp5/lnCSwv/xy X-Received: by 2002:a05:6102:5088:b0:470:54f7:1c6a with SMTP id bl8-20020a056102508800b0047054f71c6amr5439667vsb.5.1708441989168; Tue, 20 Feb 2024 07:13:09 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708441989; cv=pass; d=google.com; s=arc-20160816; b=M3jD8BT42ZgOMA+QXgFyroBs2i71zqFMOqENKJfLGQKwsZZZSbpe9Nmf1s2JtCL9z0 PdMII1j0DWNeDIIawCkMYAGqDGlhy75qQPAdIeKjjbm8c1wAMMp4vbTP9BGSrAVPqc7y 2HcDWushVCj7szfi1zcpKHK7mMEXIDN6QHpdpbq7FblnxRPzc0T1n6J1GPnW39zWYKlm snutTrwJA7rzmcI4JLgkAcJ7HcO4n8GaJukbS2nDgdViT+R28ZLZsGKJml9wSyHrxVD8 ZgqcFEd+z1NrM1WbJIv7U0MZviFPynVOD8NVCdIhAfTz1erEbwuLoDDUaRAwaTr5f04Y 30JQ== 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-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-73226-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id dc42-20020a05610255aa00b004707228fa1asi186123vsb.642.2024.02.20.07.13.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 07:13:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-73226-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; 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-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-73226-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id D3E1F1C20EC7 for ; Tue, 20 Feb 2024 15:13:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C511E76412; Tue, 20 Feb 2024 15:11:47 +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 Content-Type: text/plain; charset="UTF-8" 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); } -- 2.44.0.rc0.258.g7320e95886-goog