From patchwork Mon Aug 9 15:24:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Perret X-Patchwork-Id: 12426795 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.5 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4C350C4338F for ; Mon, 9 Aug 2021 15:27:41 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id 16D6B6101D for ; Mon, 9 Aug 2021 15:27:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 16D6B6101D Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org 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:References: Mime-Version:Message-Id: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=+M5HZBCN3/6JDBu71w4tktAsrGkfS2/6fC0TPjLPD/8=; b=ObOExywhjpoeg+nQ/alpzHhX8A tLN5fwog5xXVm4w+avHC3K0rs4Wu9ImuZV7173J5QXJhJxQ18Dpv0OFFHnwDP2iOAVpltFW0ka4YV EG7gYsf3cdfbewQSpt2lBLLLIYbA6EcF/Y1ZAeA4k+/Wqv++pZG8ZTUQJyPubb4wrSukny+jKtuX6 i0QaDjyw75MJ16kK1WMhH4gcZ3y7Rxmk+epFOMOPOPtYlBN4B2Tyt0OFsIfm4Ahvc9rW9RFJ6/1Ry b/PSppiRHpasPiaQe9PxiYlddaTYHGCrzDvjz/tCEpER65v4oNL+9+Sy5Op3sZLf3TQnlKlEN4tQo sI+BDEkw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mD7A2-0018CA-I8; Mon, 09 Aug 2021 15:25:54 +0000 Received: from mail-ed1-x549.google.com ([2a00:1450:4864:20::549]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mD79C-0017ro-Er for linux-arm-kernel@lists.infradead.org; Mon, 09 Aug 2021 15:25:03 +0000 Received: by mail-ed1-x549.google.com with SMTP id j15-20020aa7c40f0000b02903be5fbe68a9so2435269edq.2 for ; Mon, 09 Aug 2021 08:25:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=GMZNfzuPpqQplgJREqrbVT8SGjVRH/ICHDZ3APociKw=; b=UHGjFJ88t++uDbi+ho+q/XdFtIa+W/7vj7rOtKa5ztsftv96V3hJF8oNqfWIZeOQaL aUP+VWMsxv/Ww6yCE/11VKrtsrFRo4r1jE9zZCcHCxeW4QTXs3Hf0eg59+DEpi0AsG9+ pQpwB2Zfd62iUZFWqRr7WkBLq6/W/YQO57q79BUO2PTtMUj5uiQIu4UTW0GFsdzup9UE 9v9sMuWxhogJDOCknqN7ch+GClEeK3D78t6XMjW2bjoowOIRZuE8PbSwxbSu3F3AkfXj J5a/V4E7m6hSw9dq9XYFJeT/AzeYnLZo6tEmPEs60tS9JdK639KAKwpr/ZkQ/mj98px6 3hRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=GMZNfzuPpqQplgJREqrbVT8SGjVRH/ICHDZ3APociKw=; b=oe2F1p4zzLulDHQdrjDOW/62jKnR6dVLDUuq2M8/yF4w0Ui0t6tpJpHa95KI1+sQPE Utr4xlPNQ3ygKgerZctojt5kQt3lfOpEe0ZTdDZ/SpNfO1LtiFfasBNv3cl4ZpJrERNc ERz0W2fJ8ouQpN/jye/psFtWKCNaUBkhMbTtngcgW+RACWjb/4ruX5uVLUotzvWnoujl dpUBxcpo93afHnGe7xyD0+Dbq/KFnW3VlbTISH3JLSW2Ycn5YN+dd7fmQeyT9scmQ7xI x5qNTW3IfTCCf76/zhzK7PQ9VIIv7bMNUHehFkCvDjMpt9SnUA9Jep36VoMK7zqI6zMR Dx4g== X-Gm-Message-State: AOAM530vzfqXG7lbP93LAf6GT16QRRGSnBz43HUTHTiW70CYWwyjmxcW eHsmqm/xmtX8UFacI1ofkir2V7Vo91wJ X-Google-Smtp-Source: ABdhPJyqzY0L1dCaL3AwOH4Fm7LMkYjK+OM+7knyOMrkv2ibGaXiqJGzChD13pqf4rXPj/gIvGULi/3nex3K X-Received: from luke.lon.corp.google.com ([2a00:79e0:d:210:b0e8:d460:758b:a0ae]) (user=qperret job=sendgmr) by 2002:aa7:d899:: with SMTP id u25mr30548023edq.151.1628522699812; Mon, 09 Aug 2021 08:24:59 -0700 (PDT) Date: Mon, 9 Aug 2021 16:24:31 +0100 In-Reply-To: <20210809152448.1810400-1-qperret@google.com> Message-Id: <20210809152448.1810400-5-qperret@google.com> Mime-Version: 1.0 References: <20210809152448.1810400-1-qperret@google.com> X-Mailer: git-send-email 2.32.0.605.g8dce9f2422-goog Subject: [PATCH v4 04/21] KVM: arm64: Introduce helper to retrieve a PTE and its level From: Quentin Perret To: maz@kernel.org, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, will@kernel.org Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, ardb@kernel.org, qwandor@google.com, tabba@google.com, dbrazdil@google.com, kernel-team@android.com, qperret@google.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210809_082502_560116_50F9312F X-CRM114-Status: GOOD ( 13.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 From: Marc Zyngier It is becoming a common need to fetch the PTE for a given address together with its level. Add such a helper. Signed-off-by: Marc Zyngier Signed-off-by: Quentin Perret Reviewed-by: Andrew Walbran --- arch/arm64/include/asm/kvm_pgtable.h | 19 ++++++++++++++ arch/arm64/kvm/hyp/pgtable.c | 39 ++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h index f004c0115d89..082b9d65f40b 100644 --- a/arch/arm64/include/asm/kvm_pgtable.h +++ b/arch/arm64/include/asm/kvm_pgtable.h @@ -432,6 +432,25 @@ int kvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size); int kvm_pgtable_walk(struct kvm_pgtable *pgt, u64 addr, u64 size, struct kvm_pgtable_walker *walker); +/** + * kvm_pgtable_get_leaf() - Walk a page-table and retrieve the leaf entry + * with its level. + * @pgt: Page-table structure initialised by kvm_pgtable_*_init(). + * @addr: Input address for the start of the walk. + * @ptep: Pointer to storage for the retrieved PTE. + * @level: Pointer to storage for the level of the retrieved PTE. + * + * The offset of @addr within a page is ignored. + * + * The walker will walk the page-table entries corresponding to the input + * address specified, retrieving the leaf corresponding to this address. + * Invalid entries are treated as leaf entries. + * + * Return: 0 on success, negative error code on failure. + */ +int kvm_pgtable_get_leaf(struct kvm_pgtable *pgt, u64 addr, + kvm_pte_t *ptep, u32 *level); + /** * kvm_pgtable_stage2_find_range() - Find a range of Intermediate Physical * Addresses with compatible permission diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c index 05321f4165e3..78f36bd5df6c 100644 --- a/arch/arm64/kvm/hyp/pgtable.c +++ b/arch/arm64/kvm/hyp/pgtable.c @@ -326,6 +326,45 @@ int kvm_pgtable_walk(struct kvm_pgtable *pgt, u64 addr, u64 size, return _kvm_pgtable_walk(&walk_data); } +struct leaf_walk_data { + kvm_pte_t pte; + u32 level; +}; + +static int leaf_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, + enum kvm_pgtable_walk_flags flag, void * const arg) +{ + struct leaf_walk_data *data = arg; + + data->pte = *ptep; + data->level = level; + + return 0; +} + +int kvm_pgtable_get_leaf(struct kvm_pgtable *pgt, u64 addr, + kvm_pte_t *ptep, u32 *level) +{ + struct leaf_walk_data data; + struct kvm_pgtable_walker walker = { + .cb = leaf_walker, + .flags = KVM_PGTABLE_WALK_LEAF, + .arg = &data, + }; + int ret; + + ret = kvm_pgtable_walk(pgt, ALIGN_DOWN(addr, PAGE_SIZE), + PAGE_SIZE, &walker); + if (!ret) { + if (ptep) + *ptep = data.pte; + if (level) + *level = data.level; + } + + return ret; +} + struct hyp_map_data { u64 phys; kvm_pte_t attr;