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=-14.9 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,NICE_REPLY_A,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 74ADEC432BE for ; Fri, 6 Aug 2021 11:12:18 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (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 2738F601FD for ; Fri, 6 Aug 2021 11:12:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 2738F601FD Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=xen.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.xenproject.org Received: from list by lists.xenproject.org with outflank-mailman.164718.301059 (Exim 4.92) (envelope-from ) id 1mBxln-0007JH-18; Fri, 06 Aug 2021 11:12:07 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 164718.301059; Fri, 06 Aug 2021 11:12:06 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mBxlm-0007JA-Sb; Fri, 06 Aug 2021 11:12:06 +0000 Received: by outflank-mailman (input) for mailman id 164718; Fri, 06 Aug 2021 11:12:06 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mBxll-0007J4-Vw for xen-devel@lists.xenproject.org; Fri, 06 Aug 2021 11:12:05 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mBxlk-0007ra-4U; Fri, 06 Aug 2021 11:12:04 +0000 Received: from [54.239.6.186] (helo=a483e7b01a66.ant.amazon.com) by xenbits.xenproject.org with esmtpsa (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mBxlj-0005L4-UJ; Fri, 06 Aug 2021 11:12:04 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xen.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type:In-Reply-To: MIME-Version:Date:Message-ID:From:References:Cc:To:Subject; bh=3SadOEWW7n5k6UfiP1oknJUfUOwrHSTRDku0rwodCcM=; b=dz+Y0x1cgojsy9m0nC2K1rsLYu zgfnlDzDwgRWmwwmQi9/jRD/XdDHu701l/EalpDG8Rf5XzZRYeaXPole2rpNWF7cM1c+/wsFna5/I jrl39cLOa7y08eeDjnCoDI0lwogYO/U3JsRJ+WmH3u5iGeORn+wnK8GS2Zolb7o3BOxI=; Subject: Re: [PATCH v2] xen/arm64: Remove vreg_emulate_sysreg32 To: Michal Orzel , xen-devel@lists.xenproject.org Cc: Stefano Stabellini , Volodymyr Babchuk , bertrand.marquis@arm.com, Ash Wilding , Ian Jackson References: <20210729104258.6320-1-michal.orzel@arm.com> <4a5ac822-3078-de0e-8f84-6881d47ad3eb@xen.org> From: Julien Grall Message-ID: Date: Fri, 6 Aug 2021 12:12:01 +0100 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.12.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 8bit On 29/07/2021 12:47, Michal Orzel wrote: > Hi Julien, Hi Michal, > On 29.07.2021 13:20, Julien Grall wrote: >> Hi Michal, >> >> On 29/07/2021 11:42, Michal Orzel wrote: >>> According to ARMv8A architecture, AArch64 registers >>> are 64bit wide even though in many cases the upper >>> 32bit is reserved. Therefore there is no need for >>> function vreg_emulate_sysreg32 on arm64. This means >>> that we can have just one function vreg_emulate_sysreg >>> using new function pointer: >>> typedef bool (*vreg_reg_fn_t)(struct cpu_user_regs *regs, >>>                                register_t *r, bool read); >>> >>> Modify vreg_emulate_cp32 to use the new function pointer >>> as well. >>> >>> This change allows to properly use 64bit registers in AArch64 >>> state and in case of AArch32 the upper 32 bits of AArch64 >>> registers are inaccessible and are ignored(D1.20.1 ARM DDI 0487A.j). >> >> What you wrote only says that the bits are ignored. It doesn't say whether the bits will be 0. >> >> They are probably, but as I wrote yesterday, I couldn't confirm it. >> > Should I then remove this part of the commit or write below?: > "We can assume that those bits will be 0 but the architecture > reference manual does not clarify this." There was some back and forth on security@xen.org about this. I will summarizing the discussion here as we considered this was a just a bug. I wasn't looking at the correct section in the Arm Arm. There is a paragraph clearly describing the expected behavior in a different section (thanks Ash for the pointer!). Per section D1.19.2 in DDI 0487F.c: " If the general-purpose register was accessible from AArch32 state The upper 32 bits either become zero, or hold the value that the same architectural register held before any AArch32 execution. The choice between these two options is IMPLEMENTATION DEFINED, and might vary dynamically within an implementation. Correspondingly, software must regard the value as being a CONSTRAINED UNPREDICTABLE choice between these two values. This behavior applies regardless of whether any execution occurred at the Exception level that was using AArch32. That is, this behavior applies even if AArch32 state was entered by an exception return from AArch64 state, and another exception was immediately taken to AArch64 state without any instruction execution in AArch32 state. " So we can't assume the top 32-bits are zeroed unless the hypervisor ensured they were. Today, we don't have that guarantee in Xen. This needs to be fixed. The two approachs we discussed are: 1) Update set_user_reg() to zero the top 32-bit. We have a couple of places using directly the fields xN. So we would need to switch them to use set_user_reg() 2) Only saving/restoring the bottom 32-bit when entering/leaving the hypervisor. At the moment, my preference goes towards the latter because we don't risk to introduce new place where set_user_reg() is not used. I have quickly hack the entry path. This would look like: diff --git a/xen/arch/arm/arm64/entry.S b/xen/arch/arm/arm64/entry.S index fc3811ad0ad5..65e24c88b059 100644 --- a/xen/arch/arm/arm64/entry.S +++ b/xen/arch/arm/arm64/entry.S @@ -111,6 +111,11 @@ */ .macro entry, hyp, compat, save_x0_x1=1 sub sp, sp, #(UREGS_SPSR_el1 - UREGS_LR) /* CPSR, PC, SP, LR */ + .if \compat == 1 /* AArch32 mode */ + /* Clobber the top 32-bit of the registers */ + mov w0, w0 + mov w1, w1 + .endif push x28, x29 push x26, x27 push x24, x25 I haven't looked whether this can be optimized or the exit path would be easier to modify. Anyway, this is not a new bug so I would be fine to get this patch merged first. Although, I think this wants to be fixed for xen 4.16 (CCing Ian to track it). I will try to find sometimes in the next couple of weeks to fix it and have another review of this patch. Cheers, -- Julien Grall