From patchwork Thu Jul 29 13:28:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Perret X-Patchwork-Id: 12408717 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 509D0C4338F for ; Thu, 29 Jul 2021 13:31:07 +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 10729601FC for ; Thu, 29 Jul 2021 13:31:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 10729601FC 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=KlsHUByL4CBUCJ6a3rHJaUWrdM +9ZPWLVm2r4cVvz/NQuKBfEqNOJj9KKCj7PGGj5va0YRpHZehe9fXOCpFEiqyIPecOYQq+BVLzG2c uDH3LO1YSiUGW7/q7wLkx++sOMUn2JrsV4JHW19NZNq7EePLVF77uMCG1MrqIlODgFBjPSyD9C2Ld 33Hq3K8hgd2q0F3TNZJFUUCA1k941VWAdkzUTvBX5jh9g0CUR9COyARPTNWwmFF7T2v0H2AeAP6pg g6z89kxzvxMDmWgJ/gNQx8cMnKYSFEaNRSimbOLDBWYjLf5WN7yhwAouZMBsADeZK6jVeJK8QbX+C uTI5obtA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1m966I-004Et5-4m; Thu, 29 Jul 2021 13:29:26 +0000 Received: from mail-wr1-x44a.google.com ([2a00:1450:4864:20::44a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1m965S-004Eb9-B3 for linux-arm-kernel@lists.infradead.org; Thu, 29 Jul 2021 13:28:35 +0000 Received: by mail-wr1-x44a.google.com with SMTP id s16-20020adfdb100000b0290140a25efc6dso2251051wri.5 for ; Thu, 29 Jul 2021 06:28:33 -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=liAJ/B9k752zaUtEr2dNmyI6PNIq95D2uxyTofIPP2Y9x50NOXw4/JT8oVxsLvIkmc P7ulUWGLvyLx+t+fWD8rdAsH4RvIVZHUhm9AOhec5RvozMBkX+zZj7nLaIAXqPqIyTGm qMSgBtOd1z4GkgAg8Aq2iTh9+h3RRsOREBBjibJ8VnR4P30ZOtFv7y65TudPffgn0bRg RznpmwquKro9JhqASPV8OQxONr9moaG80DsU8VJsjXcHn5Yyqn3qHvtX9DT4MJ6bUy36 iuW7lgbeDizByaPiTm3DcjyGsmrR4gr7L+ezKHNRzsImGGj1FE1SjSaaY2EcjvFz2eYA lr2g== 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=PapmxnVbAg5RR40l7oTNAu/MxhlaRnAN5yPY1U2nFmIRUi2CR/YOXBn5L7AsFfpkPr wrDW9k+3S1H+4AMp1D/IqELOwmLoIK6Fm0XrYYAYxNz3uEUGu95iYit8+iROJ3qZCsM6 66h1Ijc1cKbtKs4I8gDZC9UlRDe1reGh7n5J0BGLWZ6deP/O9qGK/9dHrxYH83ml2tPo 3auODPLqyTltJRQRMgRumYrnj/t3FBpzCppZMvxRL5Swyk3UA/9NmE3l8Tl8cS+6dg4+ pRCk8dJLEfs1qFdyXYfT0HvicZb4VCgx0NJ7B//zvK8/l+8GGnyKGoUxhPREucA88FVc nMnA== X-Gm-Message-State: AOAM532fjEjE8p2NDunqreQh76rlMs/74p5jPapvRNKhzf3fxca5F5iB ZHSX72Fn1tZzOCa8SsqlMmGOXByUl9Si X-Google-Smtp-Source: ABdhPJyC7oJeo9hm9rZKLPbAlJuErb6lfSMGOtMtJPYFP3+ud7lcAhFVhw7sxxXnSC8c2pWtkaZZ9KzshhQh X-Received: from luke.lon.corp.google.com ([2a00:79e0:d:210:293a:bc89:7514:5218]) (user=qperret job=sendgmr) by 2002:a7b:c147:: with SMTP id z7mr4842725wmi.10.1627565311894; Thu, 29 Jul 2021 06:28:31 -0700 (PDT) Date: Thu, 29 Jul 2021 14:28:01 +0100 In-Reply-To: <20210729132818.4091769-1-qperret@google.com> Message-Id: <20210729132818.4091769-5-qperret@google.com> Mime-Version: 1.0 References: <20210729132818.4091769-1-qperret@google.com> X-Mailer: git-send-email 2.32.0.432.gabb21c7263-goog Subject: [PATCH v3 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, Quentin Perret X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210729_062834_440297_A37A64C8 X-CRM114-Status: GOOD ( 13.20 ) 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;