From patchwork Thu Jul 29 13:28:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Perret X-Patchwork-Id: 12408769 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 DDF1EC4338F for ; Thu, 29 Jul 2021 13:43:12 +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 AB9236052B for ; Thu, 29 Jul 2021 13:43:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org AB9236052B 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=fivnS/gx4ueDX42AviIl5pw5tDZ7BH+O4Gxf5yhaxtc=; b=mDR0vuK2CDOcHJi9v+SG36QMP2 aNcmnlO6iG75Rsy8knWy5jnXb/CKxrTGAy7qWyvJwOFjXYDQW4dnJCO3dgDvXIwS0E2R5f8F4t/kw nYfvO6SKTxNwAhC6ZqiUniWIGGfLsTY3QKj1nCQuYbY1fW8XL+LouWYX3T7em0ha1Kzieayt6gHoT PTKUjlM9Mynj4habUx/W/EVoMqyO6FV0v3u7Flfsq89byIS5vaD2lLbZsgKFOzsxkRlKpfod1YStF qj9z+j40fivmeUnRhv4ZfPlROjcuQiBb4r1D+Gzp4eghOBTotBd3jeLefry+YYqLZVplApZS3EC1c vIO7MjSA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1m96HB-004JGD-Au; Thu, 29 Jul 2021 13:40:41 +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 1m965t-004Ekg-IV for linux-arm-kernel@lists.infradead.org; Thu, 29 Jul 2021 13:29:03 +0000 Received: by mail-ed1-x549.google.com with SMTP id ay20-20020a0564022034b02903bc515b673dso2952863edb.3 for ; Thu, 29 Jul 2021 06:29:00 -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=NSGLd5rP0jEwEBedGkOnIxS9bTOxpLrW9yksOtiI2rw=; b=V/JC2o+aGhrHJS4UXvEuNCHDiEoLg0Y9XUZ8DbpCYMVAaV4Qp4pzYEUbN0+dCmIvz7 ug5TgqNXz91s47rFXRUdqwwXHw4JQuDC4YIRvTaES4qlC6IViW7MlohARYj5vvT0HcXy /5bS8SkpCABb135E8AaU/tE8brFIIdv5fpfYyBW71CPKxptU9BgZs+MYtCqgHg+dBstw duJUKwMMAPYhVeA9CtbAiKMEryWEmXgjNczXNbmQgmcV5olnAQbG+pcpqX2jyZgCv6/G 2PpFBHLl2j4lLRIWTBQgzXUfNbOCBlsqYaTYIFoxKrvw8JAyP3M415pQ3MBK0SqcQDRG qZow== 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=NSGLd5rP0jEwEBedGkOnIxS9bTOxpLrW9yksOtiI2rw=; b=kEh2wewuP6PgY25czs+LjccHivM50JV29V36l1C3+y9DceQgfDAqNfgbKY9m1dhY3W h0AUQUaKlNjvsV78akzGGpdEknMPIqBaq2bMyuAxhueJPyvkHTO6kc+a7oinBAYeMzp4 cKvzqR/zYP7JKd6pouO7IH9nD5uemeAPQ3jgdpz3ZvSGG4HufgrpdZplje/BIXQyIq+A pE9d8EhPRFP/Wl4trML6Nd9v3qNeQFvWaIR4B5xXeatB5MSzFFejsJtvPpMpeN3DAvUc opwvcn1aR6wFtjjRiV/SfbFOVlaHt3X/MuU44X3FoJZNRcKrIsG5lUWJjphXQLZwjJSU HmCw== X-Gm-Message-State: AOAM532g1onbNV7IKPSN5MzYsat+a85FU/34cRSYj87zwA02EWOk48OK y3Inzv7A3XCcDDT2NQKDLPtkcE9lSDuR X-Google-Smtp-Source: ABdhPJzB+o9pIY4nGwgIcOv53lmv+Ol4R2FzrR+UHxpEjNw7CJIBQCY1IdszSXZqu7HKk8gJv4G9waEPi+1Y X-Received: from luke.lon.corp.google.com ([2a00:79e0:d:210:293a:bc89:7514:5218]) (user=qperret job=sendgmr) by 2002:a17:906:1701:: with SMTP id c1mr4576882eje.425.1627565339524; Thu, 29 Jul 2021 06:28:59 -0700 (PDT) Date: Thu, 29 Jul 2021 14:28:13 +0100 In-Reply-To: <20210729132818.4091769-1-qperret@google.com> Message-Id: <20210729132818.4091769-17-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 16/21] 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-20210729_062901_660028_59C0C3AF X-CRM114-Status: GOOD ( 12.70 ) 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 | 37 ++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h index d5ca9b6ce241..7ff9f52239ba 100644 --- a/arch/arm64/include/asm/kvm_pgtable.h +++ b/arch/arm64/include/asm/kvm_pgtable.h @@ -505,4 +505,24 @@ int kvm_pgtable_walk(struct kvm_pgtable *pgt, u64 addr, u64 size, */ int kvm_pgtable_get_leaf(struct kvm_pgtable *pgt, u64 addr, kvm_pte_t *ptep, u32 *level); + +/** + * 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 1915489bb127..a6eda8f23cb6 100644 --- a/arch/arm64/kvm/hyp/pgtable.c +++ b/arch/arm64/kvm/hyp/pgtable.c @@ -363,6 +363,26 @@ 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 = pte & KVM_PTE_LEAF_ATTR_HI_SW; + u32 ap; + + if (!kvm_pte_valid(pte)) + return prot; + + 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; + + return prot; +} + static bool hyp_pte_needs_update(kvm_pte_t old, kvm_pte_t new) { /* @@ -565,6 +585,23 @@ 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 = pte & KVM_PTE_LEAF_ATTR_HI_SW; + + if (!kvm_pte_valid(pte)) + return prot; + + 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; + + 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))