From patchwork Mon Jul 26 09:28:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Perret X-Patchwork-Id: 12398941 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 89F62C4338F for ; Mon, 26 Jul 2021 09:35:09 +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 5193C60238 for ; Mon, 26 Jul 2021 09:35:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 5193C60238 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=bjYgdbmAPL4MRSHjmn+85k6sv3L+Ko+g9xk9t3bc/U8=; b=vJIUcdu65FcQ11QIuRRLHkElOi damMFcETP+SdG+8JafXVEBuOCdaBA0a5AA5A463LYlOq66pjwqfV9OxUD4QZE8q59gEetheXLVaCa CqziJpgUHP0HGn7Nc3n037oNBQAitn4PbHgJivrRNe9zfHmHfI4lTv7NB0p1lP9Mkm6cogDdrAYbU fbCOhTFxXsjmJGtsobxSRSus15/26cVKuXqlM/3ypoQsE5FyyD8FeFwJ3UpPywvEJjpwvbVZRV8b4 FG7nI2ZjPIa/Nl2KrP3ofJvMuiNJ5zdOfZxLsfAJwR4dZqy3VjySUCf9lcJyxPFTt8H555bgh1+jj E+VE5CfQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1m7wz0-00AT9i-Rc; Mon, 26 Jul 2021 09:33:11 +0000 Received: from mail-qk1-x749.google.com ([2607:f8b0:4864:20::749]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1m7wvU-00ARZv-Ht for linux-arm-kernel@lists.infradead.org; Mon, 26 Jul 2021 09:29:34 +0000 Received: by mail-qk1-x749.google.com with SMTP id h5-20020a05620a0525b02903b861bec838so8483263qkh.7 for ; Mon, 26 Jul 2021 02:29:31 -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=hjv3q9oqO8KJkTui97+bkuRVMGWdHhFVwbAOefAmuts=; b=K4Np44DCnp9BZz24AKPA6E28uJ8LOJp91dy0Wm/EHbQJI7pAQDa2a8mi+EA/DZXSlk GoG2gmduJ7YZYfatlUidQ9d0VAniN3/JmTbPm71T6b4P77OPCFz9BNUKp8ONmEowp3w/ +EHlEkCKgDkbLFk9y1zDY6RfRxq2WiIl0Og9u8g5HrFr4JTxKUxlrLo4+zEdUifNs6W8 JmhIv0lqvT/uAvDr5GgydXVCEY8FGiCe07r7MYrwAi0db/RDEYrNT00/DpYdaeNPmkQ8 ZLKO/aUnD0MVomB9CV6RjJbp1KN1Bzw9DuQJdFYBlMJBVi2xfvWSfOA8vbkSqq6lKfjK aBFQ== 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=hjv3q9oqO8KJkTui97+bkuRVMGWdHhFVwbAOefAmuts=; b=p3YsyM2nOzcMGLaz8TnywjbIBmUw31CbhRfCq9aZOFaOEwrZj1OEcZrml0GPTGH/9Y u/K3xq+TIwfXn4PYf5TrvjnGnC+KY5bs3UWFh6uZA2BW/KedYX6d4XsZvO/hAKeZF9ce 4q/9lZv8KoQdbNskX2teQsZUElxncHdtty+gIoAm4THbKMzm11WyXWIPjbixJNlmhid6 TcQ7Eaeue2/Eps7l6/CHhx1VZ+0UBqI/K9RG+/UrW8+HnfVI7LVtt/tB6JNRS3xQKUqU eRvteqiyQrZtTleESaUgceQWAJ6blv3YLasF5vBoOC7HFTCOc17ZjuGXbVAIjnRHoSMF U4JQ== X-Gm-Message-State: AOAM530cD4id28BhaLoJ1YgkoX9ZJSShNuAsDCi8kfdVOuFLvOV2VZsp B+wSJw4x5mfp9bKgf3F9IMctXOgyH1Vb X-Google-Smtp-Source: ABdhPJzI3c0tpPwx43KTm4Da4gOx40eS0i8088IhwtbH4MD7mLJsbFdIcU3LsaNGXrE1RIGFD4BUqlkCiWJN X-Received: from luke.lon.corp.google.com ([2a00:79e0:d:210:23a0:2f14:433:e6cb]) (user=qperret job=sendgmr) by 2002:a05:6214:ca5:: with SMTP id s5mr16960791qvs.58.1627291771064; Mon, 26 Jul 2021 02:29:31 -0700 (PDT) Date: Mon, 26 Jul 2021 10:28:58 +0100 In-Reply-To: <20210726092905.2198501-1-qperret@google.com> Message-Id: <20210726092905.2198501-10-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 09/16] KVM: arm64: Allow populating software bits 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_022932_638504_DFC5F23C X-CRM114-Status: GOOD ( 16.26 ) 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 infrastructure allowing to manipulate software bits in stage-1 and stage-2 page-tables using additional entries in the kvm_pgtable_prot enum. This is heavily inspired by Marc's implementation of a similar feature in the NV patch series, but adapted to allow stage-1 changes as well: https://lore.kernel.org/kvmarm/20210510165920.1913477-56-maz@kernel.org/ Suggested-by: Marc Zyngier Signed-off-by: Quentin Perret --- arch/arm64/include/asm/kvm_pgtable.h | 12 +++++++++++- arch/arm64/kvm/hyp/pgtable.c | 5 +++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h index 9246a27e2839..0be9f83974ad 100644 --- a/arch/arm64/include/asm/kvm_pgtable.h +++ b/arch/arm64/include/asm/kvm_pgtable.h @@ -108,6 +108,10 @@ enum kvm_pgtable_stage2_flags { * @KVM_PGTABLE_PROT_W: Write permission. * @KVM_PGTABLE_PROT_R: Read permission. * @KVM_PGTABLE_PROT_DEVICE: Device attributes. + * @KVM_PGTABLE_PROT_SW0: Software bit 0. + * @KVM_PGTABLE_PROT_SW1: Software bit 1. + * @KVM_PGTABLE_PROT_SW2: Software bit 2. + * @KVM_PGTABLE_PROT_SW3: Software bit 3. */ enum kvm_pgtable_prot { KVM_PGTABLE_PROT_X = BIT(0), @@ -115,6 +119,11 @@ enum kvm_pgtable_prot { KVM_PGTABLE_PROT_R = BIT(2), KVM_PGTABLE_PROT_DEVICE = BIT(3), + + KVM_PGTABLE_PROT_SW0 = BIT(55), + KVM_PGTABLE_PROT_SW1 = BIT(56), + KVM_PGTABLE_PROT_SW2 = BIT(57), + KVM_PGTABLE_PROT_SW3 = BIT(58), }; #define KVM_PGTABLE_PROT_RW (KVM_PGTABLE_PROT_R | KVM_PGTABLE_PROT_W) @@ -406,7 +415,8 @@ kvm_pte_t kvm_pgtable_stage2_mkold(struct kvm_pgtable *pgt, u64 addr); * If there is a valid, leaf page-table entry used to translate @addr, then * relax the permissions in that entry according to the read, write and * execute permissions specified by @prot. No permissions are removed, and - * TLB invalidation is performed after updating the entry. + * TLB invalidation is performed after updating the entry. Software bits cannot + * be set or cleared using kvm_pgtable_stage2_relax_perms(). * * Return: 0 on success, negative error code on failure. */ diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c index e0cd748e4af6..bd409d524dea 100644 --- a/arch/arm64/kvm/hyp/pgtable.c +++ b/arch/arm64/kvm/hyp/pgtable.c @@ -370,6 +370,7 @@ static int hyp_set_prot_attr(enum kvm_pgtable_prot prot, kvm_pte_t *ptep) attr |= FIELD_PREP(KVM_PTE_LEAF_ATTR_LO_S1_AP, ap); attr |= FIELD_PREP(KVM_PTE_LEAF_ATTR_LO_S1_SH, sh); attr |= KVM_PTE_LEAF_ATTR_LO_S1_AF; + attr |= prot & KVM_PTE_LEAF_ATTR_HI_SW; *ptep = attr; return 0; @@ -571,6 +572,7 @@ static int stage2_set_prot_attr(struct kvm_pgtable *pgt, enum kvm_pgtable_prot p attr |= FIELD_PREP(KVM_PTE_LEAF_ATTR_LO_S2_SH, sh); attr |= KVM_PTE_LEAF_ATTR_LO_S2_AF; + attr |= prot & KVM_PTE_LEAF_ATTR_HI_SW; *ptep = attr; return 0; @@ -1038,6 +1040,9 @@ int kvm_pgtable_stage2_relax_perms(struct kvm_pgtable *pgt, u64 addr, u32 level; kvm_pte_t set = 0, clr = 0; + if (prot & KVM_PTE_LEAF_ATTR_HI_SW) + return -EINVAL; + if (prot & KVM_PGTABLE_PROT_R) set |= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R;