From mboxrd@z Thu Jan 1 00:00:00 1970 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=-8.2 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=ham 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 D18E5CA9EA0 for ; Mon, 4 Nov 2019 11:51:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A738721D7F for ; Mon, 4 Nov 2019 11:51:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727771AbfKDLvo (ORCPT ); Mon, 4 Nov 2019 06:51:44 -0500 Received: from foss.arm.com ([217.140.110.172]:40822 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726633AbfKDLvo (ORCPT ); Mon, 4 Nov 2019 06:51:44 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 6535A1FB; Mon, 4 Nov 2019 03:51:43 -0800 (PST) Received: from lakrids.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 29D123F6C4; Mon, 4 Nov 2019 03:51:41 -0800 (PST) Date: Mon, 4 Nov 2019 11:51:39 +0000 From: Mark Rutland To: Sami Tolvanen Cc: Will Deacon , Catalin Marinas , Steven Rostedt , Masami Hiramatsu , Ard Biesheuvel , Dave Martin , Kees Cook , Laura Abbott , Marc Zyngier , Nick Desaulniers , Jann Horn , Miguel Ojeda , Masahiro Yamada , clang-built-linux@googlegroups.com, kernel-hardening@lists.openwall.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v4 03/17] arm64: kvm: stop treating register x18 as caller save Message-ID: <20191104115138.GB45140@lakrids.cambridge.arm.com> References: <20191018161033.261971-1-samitolvanen@google.com> <20191101221150.116536-1-samitolvanen@google.com> <20191101221150.116536-4-samitolvanen@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20191101221150.116536-4-samitolvanen@google.com> User-Agent: Mutt/1.11.1+11 (2f07cb52) (2018-12-01) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Nov 01, 2019 at 03:11:36PM -0700, Sami Tolvanen wrote: > From: Ard Biesheuvel > > In preparation of reserving x18, stop treating it as caller save in > the KVM guest entry/exit code. Currently, the code assumes there is > no need to preserve it for the host, given that it would have been > assumed clobbered anyway by the function call to __guest_enter(). > Instead, preserve its value and restore it upon return. > > Link: https://patchwork.kernel.org/patch/9836891/ > Signed-off-by: Ard Biesheuvel > [Sami: updated commit message, switched from x18 to x29 for the guest context] > Signed-off-by: Sami Tolvanen > Reviewed-by: Kees Cook > --- > arch/arm64/kvm/hyp/entry.S | 41 +++++++++++++++++++------------------- > 1 file changed, 20 insertions(+), 21 deletions(-) > > diff --git a/arch/arm64/kvm/hyp/entry.S b/arch/arm64/kvm/hyp/entry.S > index e5cc8d66bf53..c3c2d842c609 100644 > --- a/arch/arm64/kvm/hyp/entry.S > +++ b/arch/arm64/kvm/hyp/entry.S > @@ -23,6 +23,7 @@ > .pushsection .hyp.text, "ax" > Could we please add a note here, e.g. /* * We treat x18 as callee-saved as the host may use it as a platform * register (e.g. for shadow call stack). */ ... as that will avoid anyone trying to optimize this away in future after reading the AAPCS. > .macro save_callee_saved_regs ctxt > + str x18, [\ctxt, #CPU_XREG_OFFSET(18)] > stp x19, x20, [\ctxt, #CPU_XREG_OFFSET(19)] > stp x21, x22, [\ctxt, #CPU_XREG_OFFSET(21)] > stp x23, x24, [\ctxt, #CPU_XREG_OFFSET(23)] > @@ -32,6 +33,8 @@ > .endm > > .macro restore_callee_saved_regs ctxt > + // We assume \ctxt is not x18-x28 Probably worth s/assume/require/ here. Otherwise, this looks godo to me: Reviewed-by: Mark Rutland Mark. > + ldr x18, [\ctxt, #CPU_XREG_OFFSET(18)] > ldp x19, x20, [\ctxt, #CPU_XREG_OFFSET(19)] > ldp x21, x22, [\ctxt, #CPU_XREG_OFFSET(21)] > ldp x23, x24, [\ctxt, #CPU_XREG_OFFSET(23)] > @@ -48,7 +51,7 @@ ENTRY(__guest_enter) > // x0: vcpu > // x1: host context > // x2-x17: clobbered by macros > - // x18: guest context > + // x29: guest context > > // Store the host regs > save_callee_saved_regs x1 > @@ -67,31 +70,28 @@ alternative_else_nop_endif > ret > > 1: > - add x18, x0, #VCPU_CONTEXT > + add x29, x0, #VCPU_CONTEXT > > // Macro ptrauth_switch_to_guest format: > // ptrauth_switch_to_guest(guest cxt, tmp1, tmp2, tmp3) > // The below macro to restore guest keys is not implemented in C code > // as it may cause Pointer Authentication key signing mismatch errors > // when this feature is enabled for kernel code. > - ptrauth_switch_to_guest x18, x0, x1, x2 > + ptrauth_switch_to_guest x29, x0, x1, x2 > > // Restore guest regs x0-x17 > - ldp x0, x1, [x18, #CPU_XREG_OFFSET(0)] > - ldp x2, x3, [x18, #CPU_XREG_OFFSET(2)] > - ldp x4, x5, [x18, #CPU_XREG_OFFSET(4)] > - ldp x6, x7, [x18, #CPU_XREG_OFFSET(6)] > - ldp x8, x9, [x18, #CPU_XREG_OFFSET(8)] > - ldp x10, x11, [x18, #CPU_XREG_OFFSET(10)] > - ldp x12, x13, [x18, #CPU_XREG_OFFSET(12)] > - ldp x14, x15, [x18, #CPU_XREG_OFFSET(14)] > - ldp x16, x17, [x18, #CPU_XREG_OFFSET(16)] > - > - // Restore guest regs x19-x29, lr > - restore_callee_saved_regs x18 > - > - // Restore guest reg x18 > - ldr x18, [x18, #CPU_XREG_OFFSET(18)] > + ldp x0, x1, [x29, #CPU_XREG_OFFSET(0)] > + ldp x2, x3, [x29, #CPU_XREG_OFFSET(2)] > + ldp x4, x5, [x29, #CPU_XREG_OFFSET(4)] > + ldp x6, x7, [x29, #CPU_XREG_OFFSET(6)] > + ldp x8, x9, [x29, #CPU_XREG_OFFSET(8)] > + ldp x10, x11, [x29, #CPU_XREG_OFFSET(10)] > + ldp x12, x13, [x29, #CPU_XREG_OFFSET(12)] > + ldp x14, x15, [x29, #CPU_XREG_OFFSET(14)] > + ldp x16, x17, [x29, #CPU_XREG_OFFSET(16)] > + > + // Restore guest regs x18-x29, lr > + restore_callee_saved_regs x29 > > // Do not touch any register after this! > eret > @@ -114,7 +114,7 @@ ENTRY(__guest_exit) > // Retrieve the guest regs x0-x1 from the stack > ldp x2, x3, [sp], #16 // x0, x1 > > - // Store the guest regs x0-x1 and x4-x18 > + // Store the guest regs x0-x1 and x4-x17 > stp x2, x3, [x1, #CPU_XREG_OFFSET(0)] > stp x4, x5, [x1, #CPU_XREG_OFFSET(4)] > stp x6, x7, [x1, #CPU_XREG_OFFSET(6)] > @@ -123,9 +123,8 @@ ENTRY(__guest_exit) > stp x12, x13, [x1, #CPU_XREG_OFFSET(12)] > stp x14, x15, [x1, #CPU_XREG_OFFSET(14)] > stp x16, x17, [x1, #CPU_XREG_OFFSET(16)] > - str x18, [x1, #CPU_XREG_OFFSET(18)] > > - // Store the guest regs x19-x29, lr > + // Store the guest regs x18-x29, lr > save_callee_saved_regs x1 > > get_host_ctxt x2, x3 > -- > 2.24.0.rc1.363.gb1bccd3e3d-goog > From mboxrd@z Thu Jan 1 00:00:00 1970 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=-8.2 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 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 F3281CA9EA0 for ; Mon, 4 Nov 2019 11:51:49 +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 CABD221D7F for ; Mon, 4 Nov 2019 11:51:49 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="EmXQhGg0" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org CABD221D7F Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=arm.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+infradead-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.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=xGEHPk8UwlMCR+508sDhwGdkFmrS/1QSlf2OIM3tD6Y=; b=EmXQhGg0bjdcjx H7dYZ/j15t/nwQYm22Cob/uO7i9IO4qvwYZENUvcu6gm02C/DHdv0XQ97Bw448eq0ZsQoZJ/gwKuJ qdwbqKpZyP3SSNXqza5S5aWjZ82Kx/t6xvkVHVgAQCLKo6nWUIGv/Ns/Vtp/CUBlx5Woo/cSXU++Z rVKPDJ2Ef3Me5VSWc2sV5lf6nGkPnuLFkY12ZAdAOTYazZsOT0IU/9L8MAcri+OhCK07+3dHDtm76 yy7nHJht4Iq1kjIRIEk7N6yEdbnsRMoZtyiI+BEPpqIDuyc92MlvhVq+ihbYe57mRVKk59DnI6s5z UBWOHvAnYEpym+QSWRKA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1iRath-00069N-9X; Mon, 04 Nov 2019 11:51:49 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1iRate-00068b-8t for linux-arm-kernel@lists.infradead.org; Mon, 04 Nov 2019 11:51:48 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 6535A1FB; Mon, 4 Nov 2019 03:51:43 -0800 (PST) Received: from lakrids.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 29D123F6C4; Mon, 4 Nov 2019 03:51:41 -0800 (PST) Date: Mon, 4 Nov 2019 11:51:39 +0000 From: Mark Rutland To: Sami Tolvanen Subject: Re: [PATCH v4 03/17] arm64: kvm: stop treating register x18 as caller save Message-ID: <20191104115138.GB45140@lakrids.cambridge.arm.com> References: <20191018161033.261971-1-samitolvanen@google.com> <20191101221150.116536-1-samitolvanen@google.com> <20191101221150.116536-4-samitolvanen@google.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20191101221150.116536-4-samitolvanen@google.com> User-Agent: Mutt/1.11.1+11 (2f07cb52) (2018-12-01) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20191104_035146_404102_8BAEB1C2 X-CRM114-Status: GOOD ( 19.44 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kees Cook , Ard Biesheuvel , Masahiro Yamada , Catalin Marinas , Jann Horn , Nick Desaulniers , linux-kernel@vger.kernel.org, Steven Rostedt , Miguel Ojeda , clang-built-linux@googlegroups.com, Masami Hiramatsu , Marc Zyngier , kernel-hardening@lists.openwall.com, Laura Abbott , Will Deacon , Dave Martin , linux-arm-kernel@lists.infradead.org Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Fri, Nov 01, 2019 at 03:11:36PM -0700, Sami Tolvanen wrote: > From: Ard Biesheuvel > > In preparation of reserving x18, stop treating it as caller save in > the KVM guest entry/exit code. Currently, the code assumes there is > no need to preserve it for the host, given that it would have been > assumed clobbered anyway by the function call to __guest_enter(). > Instead, preserve its value and restore it upon return. > > Link: https://patchwork.kernel.org/patch/9836891/ > Signed-off-by: Ard Biesheuvel > [Sami: updated commit message, switched from x18 to x29 for the guest context] > Signed-off-by: Sami Tolvanen > Reviewed-by: Kees Cook > --- > arch/arm64/kvm/hyp/entry.S | 41 +++++++++++++++++++------------------- > 1 file changed, 20 insertions(+), 21 deletions(-) > > diff --git a/arch/arm64/kvm/hyp/entry.S b/arch/arm64/kvm/hyp/entry.S > index e5cc8d66bf53..c3c2d842c609 100644 > --- a/arch/arm64/kvm/hyp/entry.S > +++ b/arch/arm64/kvm/hyp/entry.S > @@ -23,6 +23,7 @@ > .pushsection .hyp.text, "ax" > Could we please add a note here, e.g. /* * We treat x18 as callee-saved as the host may use it as a platform * register (e.g. for shadow call stack). */ ... as that will avoid anyone trying to optimize this away in future after reading the AAPCS. > .macro save_callee_saved_regs ctxt > + str x18, [\ctxt, #CPU_XREG_OFFSET(18)] > stp x19, x20, [\ctxt, #CPU_XREG_OFFSET(19)] > stp x21, x22, [\ctxt, #CPU_XREG_OFFSET(21)] > stp x23, x24, [\ctxt, #CPU_XREG_OFFSET(23)] > @@ -32,6 +33,8 @@ > .endm > > .macro restore_callee_saved_regs ctxt > + // We assume \ctxt is not x18-x28 Probably worth s/assume/require/ here. Otherwise, this looks godo to me: Reviewed-by: Mark Rutland Mark. > + ldr x18, [\ctxt, #CPU_XREG_OFFSET(18)] > ldp x19, x20, [\ctxt, #CPU_XREG_OFFSET(19)] > ldp x21, x22, [\ctxt, #CPU_XREG_OFFSET(21)] > ldp x23, x24, [\ctxt, #CPU_XREG_OFFSET(23)] > @@ -48,7 +51,7 @@ ENTRY(__guest_enter) > // x0: vcpu > // x1: host context > // x2-x17: clobbered by macros > - // x18: guest context > + // x29: guest context > > // Store the host regs > save_callee_saved_regs x1 > @@ -67,31 +70,28 @@ alternative_else_nop_endif > ret > > 1: > - add x18, x0, #VCPU_CONTEXT > + add x29, x0, #VCPU_CONTEXT > > // Macro ptrauth_switch_to_guest format: > // ptrauth_switch_to_guest(guest cxt, tmp1, tmp2, tmp3) > // The below macro to restore guest keys is not implemented in C code > // as it may cause Pointer Authentication key signing mismatch errors > // when this feature is enabled for kernel code. > - ptrauth_switch_to_guest x18, x0, x1, x2 > + ptrauth_switch_to_guest x29, x0, x1, x2 > > // Restore guest regs x0-x17 > - ldp x0, x1, [x18, #CPU_XREG_OFFSET(0)] > - ldp x2, x3, [x18, #CPU_XREG_OFFSET(2)] > - ldp x4, x5, [x18, #CPU_XREG_OFFSET(4)] > - ldp x6, x7, [x18, #CPU_XREG_OFFSET(6)] > - ldp x8, x9, [x18, #CPU_XREG_OFFSET(8)] > - ldp x10, x11, [x18, #CPU_XREG_OFFSET(10)] > - ldp x12, x13, [x18, #CPU_XREG_OFFSET(12)] > - ldp x14, x15, [x18, #CPU_XREG_OFFSET(14)] > - ldp x16, x17, [x18, #CPU_XREG_OFFSET(16)] > - > - // Restore guest regs x19-x29, lr > - restore_callee_saved_regs x18 > - > - // Restore guest reg x18 > - ldr x18, [x18, #CPU_XREG_OFFSET(18)] > + ldp x0, x1, [x29, #CPU_XREG_OFFSET(0)] > + ldp x2, x3, [x29, #CPU_XREG_OFFSET(2)] > + ldp x4, x5, [x29, #CPU_XREG_OFFSET(4)] > + ldp x6, x7, [x29, #CPU_XREG_OFFSET(6)] > + ldp x8, x9, [x29, #CPU_XREG_OFFSET(8)] > + ldp x10, x11, [x29, #CPU_XREG_OFFSET(10)] > + ldp x12, x13, [x29, #CPU_XREG_OFFSET(12)] > + ldp x14, x15, [x29, #CPU_XREG_OFFSET(14)] > + ldp x16, x17, [x29, #CPU_XREG_OFFSET(16)] > + > + // Restore guest regs x18-x29, lr > + restore_callee_saved_regs x29 > > // Do not touch any register after this! > eret > @@ -114,7 +114,7 @@ ENTRY(__guest_exit) > // Retrieve the guest regs x0-x1 from the stack > ldp x2, x3, [sp], #16 // x0, x1 > > - // Store the guest regs x0-x1 and x4-x18 > + // Store the guest regs x0-x1 and x4-x17 > stp x2, x3, [x1, #CPU_XREG_OFFSET(0)] > stp x4, x5, [x1, #CPU_XREG_OFFSET(4)] > stp x6, x7, [x1, #CPU_XREG_OFFSET(6)] > @@ -123,9 +123,8 @@ ENTRY(__guest_exit) > stp x12, x13, [x1, #CPU_XREG_OFFSET(12)] > stp x14, x15, [x1, #CPU_XREG_OFFSET(14)] > stp x16, x17, [x1, #CPU_XREG_OFFSET(16)] > - str x18, [x1, #CPU_XREG_OFFSET(18)] > > - // Store the guest regs x19-x29, lr > + // Store the guest regs x18-x29, lr > save_callee_saved_regs x1 > > get_host_ctxt x2, x3 > -- > 2.24.0.rc1.363.gb1bccd3e3d-goog > _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel