Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp1354277pxk; Fri, 4 Sep 2020 07:26:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyTrzTVmhBsRS1EFYRpnTj6jVsZ7SCcNna0/FbxzosGdtuC6FNryNxk1JGsESnAOhaPXMfd X-Received: by 2002:a17:906:a201:: with SMTP id r1mr7535261ejy.432.1599229594305; Fri, 04 Sep 2020 07:26:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599229594; cv=none; d=google.com; s=arc-20160816; b=Vn7lhzo9CwHcG+UoCCKjBo0VIohnJQVdLnG3lK9p22a9VAC8yf6Q9QpRhjTa4jS9EU Z2gef/IxKWZ+Wa+B9QjezqgS9/oZOKt1z8gRZWc63iC3HW9OMoKYlJ8R/6miCvKLj5nq zmNjTzn7+JoMBmamAkNzxqVxvnrNbLM0388AUb7U+Pqqe6hsJqEU1b7uIb20bro2fFOh 5efMZ3WWGUTwVjFVFTdMMCU2iR9U2t9irtPSPW2jRWnlQxeEowt1umm+Oirf5OELsSmt TigU+z2G3/si02wd7Ry+B4T2I2ehiu62LxpqDeF6Pk2KXqpHmhODQ5yI3r/D4EWC87ei VYwg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=r1oka55bbGWmAtCdL9dVq1UzV4t5Z98qeo79AHy18II=; b=j63ITqFufNNgwjtq/Gc276ELvSGwLneH+8VEgZssSNy6eFE/bjVB4TK+R1ynX6CMPA ol31yjAswzI8h/NMah7aV0QWjgVcOiMrY5VH11cIzzPM5M73Pblj+zjQI0N+tCKn0IEu gKn1agxyMs4TS1JxUEgonnfffvPVL+04IfRa7+QV3TEX0whhFpkqAnc1X3mv+kjMJ8xu 9M7SpInUXlNfLOuKPDjoGalmRj41dWmkKIBpq7ohSA6Yv27n33TH8hebMws6A8LGtAOC dY807AuLRRdgRZZm95qOkc96xAmquF8ei/BlIM7YBqG7phC0oq0ZvG3YGanC4fpS/bXC OwLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=zPn+u4B9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id mb10si4154917ejb.35.2020.09.04.07.26.11; Fri, 04 Sep 2020 07:26:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=zPn+u4B9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730611AbgIDOV4 (ORCPT + 99 others); Fri, 4 Sep 2020 10:21:56 -0400 Received: from mail.kernel.org ([198.145.29.99]:38338 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730476AbgIDNeX (ORCPT ); Fri, 4 Sep 2020 09:34:23 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 3432B2166E; Fri, 4 Sep 2020 13:31:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1599226269; bh=6KTkxpgzQHDR1wFViFDOTk7r1crsAnUhrlDc4NUnbmE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=zPn+u4B9ePNx0o2SSf9VKW00FGM4iak4fYTwhWyQlISiP00VOZwKADM5bGqOFNlnv QjD25xugUtaQMoycDoSqkz8TZ1Axjytkmfgrt+ZK/SVpqPGmZqHjaAlGgrFu7Nk6WP Xyofay/lkks4ouVV9Wvc1ftKf2w3Db13SmodVKtU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, James Morse , Marc Zyngier , Catalin Marinas , Andre Przywara Subject: [PATCH 5.8 09/17] KVM: arm64: Survive synchronous exceptions caused by AT instructions Date: Fri, 4 Sep 2020 15:30:08 +0200 Message-Id: <20200904120258.449102679@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200904120257.983551609@linuxfoundation.org> References: <20200904120257.983551609@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: James Morse commit 88a84ccccb3966bcc3f309cdb76092a9892c0260 upstream. KVM doesn't expect any synchronous exceptions when executing, any such exception leads to a panic(). AT instructions access the guest page tables, and can cause a synchronous external abort to be taken. The arm-arm is unclear on what should happen if the guest has configured the hardware update of the access-flag, and a memory type in TCR_EL1 that does not support atomic operations. B2.2.6 "Possible implementation restrictions on using atomic instructions" from DDI0487F.a lists synchronous external abort as a possible behaviour of atomic instructions that target memory that isn't writeback cacheable, but the page table walker may behave differently. Make KVM robust to synchronous exceptions caused by AT instructions. Add a get_user() style helper for AT instructions that returns -EFAULT if an exception was generated. While KVM's version of the exception table mixes synchronous and asynchronous exceptions, only one of these can occur at each location. Re-enter the guest when the AT instructions take an exception on the assumption the guest will take the same exception. This isn't guaranteed to make forward progress, as the AT instructions may always walk the page tables, but guest execution may use the translation cached in the TLB. This isn't a problem, as since commit 5dcd0fdbb492 ("KVM: arm64: Defer guest entry when an asynchronous exception is pending"), KVM will return to the host to process IRQs allowing the rest of the system to keep running. Cc: stable@vger.kernel.org # Reviewed-by: Marc Zyngier Signed-off-by: Catalin Marinas Signed-off-by: Andre Przywara Signed-off-by: Greg Kroah-Hartman --- arch/arm64/include/asm/kvm_asm.h | 28 ++++++++++++++++++++++++++++ arch/arm64/kvm/hyp/hyp-entry.S | 14 ++++++++++---- arch/arm64/kvm/hyp/switch.c | 8 ++++---- 3 files changed, 42 insertions(+), 8 deletions(-) --- a/arch/arm64/include/asm/kvm_asm.h +++ b/arch/arm64/include/asm/kvm_asm.h @@ -121,6 +121,34 @@ extern char __smccc_workaround_1_smc[__S *__hyp_this_cpu_ptr(sym); \ }) +#define __KVM_EXTABLE(from, to) \ + " .pushsection __kvm_ex_table, \"a\"\n" \ + " .align 3\n" \ + " .long (" #from " - .), (" #to " - .)\n" \ + " .popsection\n" + + +#define __kvm_at(at_op, addr) \ +( { \ + int __kvm_at_err = 0; \ + u64 spsr, elr; \ + asm volatile( \ + " mrs %1, spsr_el2\n" \ + " mrs %2, elr_el2\n" \ + "1: at "at_op", %3\n" \ + " isb\n" \ + " b 9f\n" \ + "2: msr spsr_el2, %1\n" \ + " msr elr_el2, %2\n" \ + " mov %w0, %4\n" \ + "9:\n" \ + __KVM_EXTABLE(1b, 2b) \ + : "+r" (__kvm_at_err), "=&r" (spsr), "=&r" (elr) \ + : "r" (addr), "i" (-EFAULT)); \ + __kvm_at_err; \ +} ) + + #else /* __ASSEMBLY__ */ .macro hyp_adr_this_cpu reg, sym, tmp --- a/arch/arm64/kvm/hyp/hyp-entry.S +++ b/arch/arm64/kvm/hyp/hyp-entry.S @@ -166,13 +166,19 @@ el1_error: b __guest_exit el2_sync: - /* Check for illegal exception return, otherwise panic */ + /* Check for illegal exception return */ mrs x0, spsr_el2 + tbnz x0, #20, 1f - /* if this was something else, then panic! */ - tst x0, #PSR_IL_BIT - b.eq __hyp_panic + save_caller_saved_regs_vect + stp x29, x30, [sp, #-16]! + bl kvm_unexpected_el2_exception + ldp x29, x30, [sp], #16 + restore_caller_saved_regs_vect + eret + +1: /* Let's attempt a recovery from the illegal exception return */ get_vcpu_ptr x1, x0 mov x0, #ARM_EXCEPTION_IL --- a/arch/arm64/kvm/hyp/switch.c +++ b/arch/arm64/kvm/hyp/switch.c @@ -303,10 +303,10 @@ static bool __hyp_text __translate_far_t * saved the guest context yet, and we may return early... */ par = read_sysreg(par_el1); - asm volatile("at s1e1r, %0" : : "r" (far)); - isb(); - - tmp = read_sysreg(par_el1); + if (!__kvm_at("s1e1r", far)) + tmp = read_sysreg(par_el1); + else + tmp = SYS_PAR_EL1_F; /* back to the guest */ write_sysreg(par, par_el1); if (unlikely(tmp & SYS_PAR_EL1_F))