From patchwork Mon Jul 19 10:47: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: 12385399 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=-18.2 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, 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 3940CC07E9B for ; Mon, 19 Jul 2021 10:56:48 +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 E613F6101E for ; Mon, 19 Jul 2021 10:56:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E613F6101E Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@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=RPi7AFx4ZTCB9g6F71rOPxZD56Er7y9qKcFvkcu/I+M=; b=pwfdu7dKjlh5yuNqKUh9eWkYwr saoUZGMABl8KLpQauWaTLkNp3alun7tCZTHvvIfsWfidWJiwiXcRh+TWdneIoW/G26WU73aQFVYdK 52UNfx9iPyA+wwZobtQPfqn9D53vtEq8KLdKLsxMcLj5UG21qeWZzFGIyZm3Jpff4r2mgfNIot6PW jjZWDAsUtxNxbBGSV+q5az/4o2xgntclFNKaP9EyIXin7QeGlle85qHW3reJ28gEu3VUMGth6GkFX zsHgATH3SelfJnKeK6DOwcpVe/XkQdpSYiC6nbVZu8y/DjVdl3Dgh/2Enbs7krn9DGhtA8EnykalB XXB9Yvig==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1m5QuE-009N1w-JW; Mon, 19 Jul 2021 10:53:51 +0000 Received: from mail-wm1-x34a.google.com ([2a00:1450:4864:20::34a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1m5Qog-009Kaq-4s for linux-arm-kernel@lists.infradead.org; Mon, 19 Jul 2021 10:48:07 +0000 Received: by mail-wm1-x34a.google.com with SMTP id l18-20020a05600c1d12b02902278758ab90so3803538wms.9 for ; Mon, 19 Jul 2021 03:48:05 -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=CdbaQ8ErF1YpwqlK/5v4bHdri/rt0mg1VAsqwWTr/Tw=; b=aS8jkLkK2wa4INhA28hZzPjEUZaTZKmwenqCsFfJQTdAz0UFabdJsQo6ri3VfTWEFw RGYXatDhYnjA27xu0fDx7wkypb2FYIlxvf85tFl8oYsJq2rHaR4IgkQpa+q4+FwBsWna y/5f6F8ckCQplGSacvD6+H8TPidKSuTLdk1aFl3aPp9BvtHNW1S7YV6XGANf94XI+K92 wG0tzBduwioFtmG8ODCJFZhpzoP2jF27+9hOe1r+E4fg0ujsayMVKVG7rMKfXMgeAsAF CGBfReNZE1y1Up6lw+wxGzVUgnz28Z7y6A7GjkAKvbDoo8/B1qcvnhtx5JDqVDbCH60+ RlAg== 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=CdbaQ8ErF1YpwqlK/5v4bHdri/rt0mg1VAsqwWTr/Tw=; b=ZtS12NuBKepvbdHQT/S/urJZAjO0DBNPZNFDlIed/opZHp3+RdN8zDKAR21Eb2mGFD ceJNZPCTuD8+O9b0qgoEt7fJMvgGSeBsKhRxb/+b4LCjT90EYnP7D2ScIUKPhmELMBH7 QYVg1PACMtL65/lZ/cDaddL/R0tKnxwspIzSBivKSjWbUpZMks6Vt6jKRidd1xFGrHde IpSpHSrzlVgmY3wLmt05Yo6j7TjEZ2T9s+u1d4U+DIr1O4iRirm6FKcDimsafINPhkJW eJSrI3nQnMLNOaSVXQXKI33vpG1g7vbm93wNcmpcVI11gZj6SVAzDek1nHWiDpHE8x3K gavg== X-Gm-Message-State: AOAM531ayPQuKQwTS7ELI9OJx7c4x17V/UIrAGMvMl8O43Ft1w1Cnbla EZsBguH3q82hxl/rHV8H7e4R/+dakaan X-Google-Smtp-Source: ABdhPJznScsyUNWBAsIAI2Jzd/ytZ2sHt95anHYybQcNiWFSnjuNiVNyg44CFTl1PSMEfVjtqavG8Cwof0vY X-Received: from luke.lon.corp.google.com ([2a00:79e0:d:210:1a96:a43f:6c2e:bb5a]) (user=qperret job=sendgmr) by 2002:a7b:ca50:: with SMTP id m16mr31242327wml.140.1626691683897; Mon, 19 Jul 2021 03:48:03 -0700 (PDT) Date: Mon, 19 Jul 2021 11:47:31 +0100 In-Reply-To: <20210719104735.3681732-1-qperret@google.com> Message-Id: <20210719104735.3681732-11-qperret@google.com> Mime-Version: 1.0 References: <20210719104735.3681732-1-qperret@google.com> X-Mailer: git-send-email 2.32.0.402.g57bb445576-goog Subject: [PATCH 10/14] KVM: arm64: Enable retrieving protections attributes of PTEs 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-20210719_034806_263554_595B6920 X-CRM114-Status: GOOD ( 12.75 ) 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 Introduce helper functions in the KVM stage-2 and stage-1 page-table manipulation library allowing to retrieve the enum kvm_pgtable_prot of a PTE. This will be useful to implement custom walkers outside of pgtable.c. Signed-off-by: Quentin Perret --- arch/arm64/include/asm/kvm_pgtable.h | 20 ++++++++++++ arch/arm64/kvm/hyp/pgtable.c | 49 ++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h index f6d3d5c8910d..1aa49d6aabb7 100644 --- a/arch/arm64/include/asm/kvm_pgtable.h +++ b/arch/arm64/include/asm/kvm_pgtable.h @@ -467,4 +467,24 @@ int kvm_pgtable_walk(struct kvm_pgtable *pgt, u64 addr, u64 size, */ int kvm_pgtable_stage2_find_range(struct kvm_pgtable *pgt, u64 addr, int owner, struct kvm_mem_range *range); + +/** + * kvm_pgtable_stage2_pte_prot() - Retrieve the protection attributes of a + * stage-2 Page-Table Entry. + * @pte: Page-table entry + * + * Return: protection attributes of the page-table entry in the enum + * kvm_pgtable_prot format. + */ +enum kvm_pgtable_prot kvm_pgtable_stage2_pte_prot(kvm_pte_t pte); + +/** + * kvm_pgtable_hyp_pte_prot() - Retrieve the protection attributes of a stage-1 + * Page-Table Entry. + * @pte: Page-table entry + * + * Return: protection attributes of the page-table entry in the enum + * kvm_pgtable_prot format. + */ +enum kvm_pgtable_prot kvm_pgtable_hyp_pte_prot(kvm_pte_t pte); #endif /* __ARM64_KVM_PGTABLE_H__ */ diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c index 51598b79dafc..c7120797404a 100644 --- a/arch/arm64/kvm/hyp/pgtable.c +++ b/arch/arm64/kvm/hyp/pgtable.c @@ -234,6 +234,20 @@ static kvm_pte_t pte_ignored_bit_prot(enum kvm_pgtable_prot prot) return FIELD_PREP(KVM_PTE_LEAF_ATTR_IGNORED, ignored_bits); } +static enum kvm_pgtable_prot pte_read_ignored_bits_prot(kvm_pte_t pte) +{ + enum kvm_pgtable_prot prot = 0; + kvm_pte_t ignored_bits = 0; + + ignored_bits = FIELD_GET(KVM_PTE_LEAF_ATTR_IGNORED, pte); + if (ignored_bits & BIT(1)) + prot |= KVM_PGTABLE_STATE_BORROWED | KVM_PGTABLE_STATE_SHARED; + if (ignored_bits & BIT(0)) + prot |= KVM_PGTABLE_STATE_SHARED; + + return prot; +} + static int kvm_pgtable_visitor_cb(struct kvm_pgtable_walk_data *data, u64 addr, u32 level, kvm_pte_t *ptep, enum kvm_pgtable_walk_flags flag) @@ -386,6 +400,25 @@ static int hyp_set_prot_attr(enum kvm_pgtable_prot prot, kvm_pte_t *ptep) return 0; } +enum kvm_pgtable_prot kvm_pgtable_hyp_pte_prot(kvm_pte_t pte) +{ + enum kvm_pgtable_prot prot = 0; + u32 ap; + + if (!(pte & KVM_PTE_LEAF_ATTR_HI_S1_XN)) + prot |= KVM_PGTABLE_PROT_X; + + ap = FIELD_GET(KVM_PTE_LEAF_ATTR_LO_S1_AP, pte); + if (ap == KVM_PTE_LEAF_ATTR_LO_S1_AP_RO) + prot |= KVM_PGTABLE_PROT_R; + else if (ap == KVM_PTE_LEAF_ATTR_LO_S1_AP_RW) + prot |= KVM_PGTABLE_PROT_R | KVM_PGTABLE_PROT_W; + + prot |= pte_read_ignored_bits_prot(pte); + + return prot; +} + static bool hyp_pte_needs_update(kvm_pte_t old, kvm_pte_t new) { if (old == new) @@ -588,6 +621,22 @@ static int stage2_set_prot_attr(struct kvm_pgtable *pgt, enum kvm_pgtable_prot p return 0; } +enum kvm_pgtable_prot kvm_pgtable_stage2_pte_prot(kvm_pte_t pte) +{ + enum kvm_pgtable_prot prot = 0; + + if (pte & KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R) + prot |= KVM_PGTABLE_PROT_R; + if (pte & KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W) + prot |= KVM_PGTABLE_PROT_W; + if (!(pte & KVM_PTE_LEAF_ATTR_HI_S2_XN)) + prot |= KVM_PGTABLE_PROT_X; + + prot |= pte_read_ignored_bits_prot(pte); + + return prot; +} + static bool stage2_pte_needs_update(kvm_pte_t old, kvm_pte_t new) { if (!kvm_pte_valid(old) || !kvm_pte_valid(new))