From patchwork Mon Nov 20 13:10:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marc Zyngier X-Patchwork-Id: 13461278 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id B3F54C54FB9 for ; Mon, 20 Nov 2023 13:16:47 +0000 (UTC) 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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=oToGsjll/kaysI36JQ7h5GtoNumiiOjeFhi5rbBnU5w=; b=dM0TdG4iyXCG2W INIGIsneuMow9o3J6T2zCjRjHkKM4gsA/hAWdaOKeJ3mTu4KR4OiDT0O+nrPUISgLyfIb5ypsBKDa 1wV6fqWwvmOFmSqQ9wCrDfQcUeQ9g34rEHJPPXQd+seXI+23VNKwARIF9j7IbZEMMU0Vauaekdk8k +Nm2nC6WuoNOAYjosH3YCFnILTol4JvrxLG5TRS1wZ+Q+nUq1wkRs9f+8c/3mnjaY6lSgn2fyi1xM nHpm6VvaY+8EPMS2Bkp5H3HlHWnEyzhirMoMVtG4UrjqF3irt/NLiDkgy5aNDFgP4hxOnxYKmrtG8 6mvbdUgfbM/CZgQEkruA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r547Y-00C6ZX-2E; Mon, 20 Nov 2023 13:16:03 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1r543Y-00C4n1-0k for linux-arm-kernel@bombadil.infradead.org; Mon, 20 Nov 2023 13:11:16 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=Wj2gNvJRxHGp+r9rdjbHdLgsPSMEWVjJLiXEIEGeJFI=; b=Q3EtyzMX1nPBQz3GlcJ5EyhD0a GKQF+uCzKNEY8zdvog9eNNSzTcB4akoTTELs4Q/I03ony+n9hT8qd6aKIiTLPHxLlppc6JGqXKoLY HdhpDDV60/qDxquiZ9bpQ/xz7W9KivutAL7WuTPtdm3MLqEmZjeSh+25jP0P1DhKd1ExfpuiJ5uVC nPHYsjVXp0nhwX3gEI9Ht4E9/0cZeLhPA8BpoF8d5GMRQlhpkVyhC1gxPX9Ac9KGzuzEgacvr6tG9 4FfEM9MOw7ZM2FqPZ0lfm9fA7Nim0kYWztPcpqaNqa/U1imqvSWv85BMTzdOXiOZziWLMzhCfX1GG X4ysrjmw==; Received: from ams.source.kernel.org ([145.40.68.75]) by casper.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1r543T-004Y6V-UN for linux-arm-kernel@lists.infradead.org; Mon, 20 Nov 2023 13:11:14 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id 7F9BBB81739; Mon, 20 Nov 2023 13:11:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 85B31C433C9; Mon, 20 Nov 2023 13:11:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700485860; bh=arLCzUodwRs9zyrNECW0rOWp80VBJlsnV+5EDDsiMWA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HiI4BtQynh6J7ezwWBB58nPAlPegaJDyp5vAxSNE36aIvamWvGhV60tp9i67DHFaK 5pEuUs4SIyQASLWDsvBl1AY13ByVi7OrsF7pO8npiYilcNSVYjcb8I/7sbXmkL8zNN OG0bcz31Lk+NJrxv15Td9EGdzb2c2mXGtyJnBzihXh5eMP1bGRhI2N7ZbraS/mfNqz G9xb6NWmqer5oF2dXx52YvYKn7hhZewU0XB6d8WtgPXld+/RLsZO2BM0Bpo2pqfoVv sGKayOCd4sVU5MEdfSnjeOIaYE/4XLlmzQcSmPikROje/i3FZp0U6VnUOjqZ/ZPgs0 6ufoHlHo8RlOA== Received: from sofa.misterjones.org ([185.219.108.64] helo=valley-girl.lan) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1r543G-00EjnU-MV; Mon, 20 Nov 2023 13:10:58 +0000 From: Marc Zyngier To: kvmarm@lists.linux.dev, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org Cc: Alexandru Elisei , Andre Przywara , Chase Conklin , Christoffer Dall , Ganapatrao Kulkarni , Darren Hart , Jintack Lim , Russell King , Miguel Luis , James Morse , Suzuki K Poulose , Oliver Upton , Zenghui Yu Subject: [PATCH v11 37/43] KVM: arm64: nv: Tag shadow S2 entries with nested level Date: Mon, 20 Nov 2023 13:10:21 +0000 Message-Id: <20231120131027.854038-38-maz@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231120131027.854038-1-maz@kernel.org> References: <20231120131027.854038-1-maz@kernel.org> MIME-Version: 1.0 X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: kvmarm@lists.linux.dev, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, alexandru.elisei@arm.com, andre.przywara@arm.com, chase.conklin@arm.com, christoffer.dall@arm.com, gankulkarni@os.amperecomputing.com, darren@os.amperecomputing.com, jintack@cs.columbia.edu, rmk+kernel@armlinux.org.uk, miguel.luis@oracle.com, james.morse@arm.com, suzuki.poulose@arm.com, oliver.upton@linux.dev, yuzenghui@huawei.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231120_131112_084317_D2548280 X-CRM114-Status: GOOD ( 15.50 ) 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 Populate bits [56:55] of the leaf entry with the level provided by the guest's S2 translation. Signed-off-by: Marc Zyngier --- arch/arm64/include/asm/kvm_nested.h | 6 ++++++ arch/arm64/kvm/mmu.c | 16 ++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/arch/arm64/include/asm/kvm_nested.h b/arch/arm64/include/asm/kvm_nested.h index 128c1d8281af..da7ebd2f6e24 100644 --- a/arch/arm64/include/asm/kvm_nested.h +++ b/arch/arm64/include/asm/kvm_nested.h @@ -5,6 +5,7 @@ #include #include #include +#include static inline bool vcpu_has_nv(const struct kvm_vcpu *vcpu) { @@ -134,4 +135,9 @@ int kvm_init_nv_sysregs(struct kvm *kvm); #define KVM_NV_GUEST_MAP_SZ (KVM_PGTABLE_PROT_SW1 | KVM_PGTABLE_PROT_SW0) +static inline u64 kvm_encode_nested_level(struct kvm_s2_trans *trans) +{ + return FIELD_PREP(KVM_NV_GUEST_MAP_SZ, trans->level); +} + #endif /* __ARM64_KVM_NESTED_H */ diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 8c77547f5582..61bdd8798f83 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1618,11 +1618,17 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, * Potentially reduce shadow S2 permissions to match the guest's own * S2. For exec faults, we'd only reach this point if the guest * actually allowed it (see kvm_s2_handle_perm_fault). + * + * Also encode the level of the nested translation in the SW bits of + * the PTE/PMD/PUD. This will be retrived on TLB invalidation from + * the guest. */ if (nested) { writable &= kvm_s2_trans_writable(nested); if (!kvm_s2_trans_readable(nested)) prot &= ~KVM_PGTABLE_PROT_R; + + prot |= kvm_encode_nested_level(nested); } read_lock(&kvm->mmu_lock); @@ -1676,14 +1682,20 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, * permissions only if vma_pagesize equals fault_granule. Otherwise, * kvm_pgtable_stage2_map() should be called to change block size. */ - if (fault_status == ESR_ELx_FSC_PERM && vma_pagesize == fault_granule) + if (fault_status == ESR_ELx_FSC_PERM && vma_pagesize == fault_granule) { + /* + * Drop the SW bits in favour of those stored in the + * PTE, which will be preserved. + */ + prot &= ~KVM_NV_GUEST_MAP_SZ; ret = kvm_pgtable_stage2_relax_perms(pgt, fault_ipa, prot); - else + } else { ret = kvm_pgtable_stage2_map(pgt, fault_ipa, vma_pagesize, __pfn_to_phys(pfn), prot, memcache, KVM_PGTABLE_WALK_HANDLE_FAULT | KVM_PGTABLE_WALK_SHARED); + } /* Mark the page dirty only if the fault is handled successfully */ if (writable && !ret) {