From patchwork Mon Jul 26 09:28:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Perret X-Patchwork-Id: 12398927 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 23E02C432BE for ; Mon, 26 Jul 2021 09:31: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 E7E8460238 for ; Mon, 26 Jul 2021 09:31:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org E7E8460238 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=J9yLLqnAZ3DXDtXSOobwdtuXGHkQCNNcKeF4soEIxEk=; b=siwplf6E0ZbpNznweyljRmUbyW qlbaptfGsXRPW7aWreQG+kWOweX7FN0UTcQg5O7bnsujSjqVe0F0ya8TMzmt5q00j/nfbOj0YFUdt yz8VpOemAJB9gi71lUbrY0InQorMrXHLGk3EX8cwkXgXtrqImiwlBA6j2ZCXK5+38TDlPkgAekIyy TtBi+jf/vC2YFrDb7794H1n8TuRoV59sUbDqrLCdy+jRGlXnfjPayWjQ+ygJsPEiDEO/OtB8xjuxy 8uwajTdJKa0wVFBp21v5DRIvpqC+J+dWLzbJ2xz1ZrRI6KRY20fVPWd7hcgTBcYsnlFvS4iexv+wO 0wVwxcBg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1m7wvP-00ARYb-VR; Mon, 26 Jul 2021 09:29:28 +0000 Received: from mail-qt1-x84a.google.com ([2607:f8b0:4864:20::84a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1m7wvD-00ARVP-N7 for linux-arm-kernel@lists.infradead.org; Mon, 26 Jul 2021 09:29:17 +0000 Received: by mail-qt1-x84a.google.com with SMTP id e19-20020ac84b530000b0290251f0b91196so4017780qts.14 for ; Mon, 26 Jul 2021 02:29:14 -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=Gjf1jLwbr60oU/lDUo56+HNoAVcvWGbxZryQe2MxgVI=; b=DUXlt4CIx1Jj1CbDy7cdGv8bTdi62I2RYHsrT/s+KJOvTfJ/fwZsR6eGvk7It8rJcN bBoWcqIo5xKqnQFtfIFlQBaEBPm+SOq7Xuyw31L0WnzSfdn2t6kbbMQIZzXB0MUUPX8Q wxW+8/zX7bGrdM87rM65vxKXgcwv/bXY77icyCnicQZ+mAEy26wCuFtcvS96nYsEpGkg 0z8vJbr77sREiu6d0IOmXfqa+VIbetUENBqdqiS0fCh4TnmOc6+k33nLzzQDzu0GsDiL pmQlhJkSyvFpULMaSDUptBv+CZ5WsBH34jLy6q57bE4W63AjjHdbQf7QAxbCShWBSI8V MdwQ== 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=Gjf1jLwbr60oU/lDUo56+HNoAVcvWGbxZryQe2MxgVI=; b=MzEcmOFVcpow1OczlBAhQeHWiVbGPF5hgE6kwxc/Z7+9TuR00NbIlBTHOPRnACIk3M noVSJjeZ85Bg6zPxtS+qbKaPYUCIt8WjUNL8vf9AVpV/+Lmkf5ZOrFFtRP/uOV2a/YH9 reQ9Qpd0N8MA4cEefSo8C5rQMi5lpH7TnpMx/gtJfpM/ZphS3h7TKmmcCUU/+qWaqYCq AVazp8n1w77MeQF7GfE2BZ3OXIM47eQW97G3PFHiaaf0yD/sTlhlPgEKGpTek2THBfrF 7PaWJRmRykT+rR+JIQFs/iHTvVWTljeVaPv1aGCUIxo2qQQrVHtUhv1R7IWqduQ+Gp99 ZWuA== X-Gm-Message-State: AOAM530w5WOX4BD0EXODmxiQwyerofo76fUb0k+hCAfs1e063Zw54iLF +WxZBNewE8zEySJLwovE6v+2VSpHSX+R X-Google-Smtp-Source: ABdhPJwsdWxD5Vpv+7zklcHUJYq1XgMklZukeoIEPoWdSvMcON5nO372Kxs1TISOnicfY2UxNWOoh5v1fVJj X-Received: from luke.lon.corp.google.com ([2a00:79e0:d:210:23a0:2f14:433:e6cb]) (user=qperret job=sendgmr) by 2002:a05:6214:dce:: with SMTP id 14mr16478055qvt.40.1627291753422; Mon, 26 Jul 2021 02:29:13 -0700 (PDT) Date: Mon, 26 Jul 2021 10:28:50 +0100 In-Reply-To: <20210726092905.2198501-1-qperret@google.com> Message-Id: <20210726092905.2198501-2-qperret@google.com> Mime-Version: 1.0 References: <20210726092905.2198501-1-qperret@google.com> X-Mailer: git-send-email 2.32.0.432.gabb21c7263-goog Subject: [PATCH v2 01/16] 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, Quentin Perret X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210726_022915_798482_4187791D X-CRM114-Status: GOOD ( 13.60 ) 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 --- 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;