From patchwork Wed Feb 1 17:45:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 93080 Delivered-To: patch@linaro.org Received: by 10.140.20.99 with SMTP id 90csp2530200qgi; Wed, 1 Feb 2017 09:45:31 -0800 (PST) X-Received: by 10.98.159.80 with SMTP id g77mr5084216pfe.34.1485971131430; Wed, 01 Feb 2017 09:45:31 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q2si14999253pga.211.2017.02.01.09.45.31; Wed, 01 Feb 2017 09:45:31 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754160AbdBARpR (ORCPT + 25 others); Wed, 1 Feb 2017 12:45:17 -0500 Received: from mail-wm0-f46.google.com ([74.125.82.46]:36564 "EHLO mail-wm0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754134AbdBARpP (ORCPT ); Wed, 1 Feb 2017 12:45:15 -0500 Received: by mail-wm0-f46.google.com with SMTP id c85so49854654wmi.1 for ; Wed, 01 Feb 2017 09:45:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=OxwlZr5tINv6Xc6wXSUoWMk0N3aa+XfaS6qHiYohACk=; b=Qh/8MFGhVlboNxQ42g3eKLPLxuR1HT35gaE58bxF+4veX2nDRBbKlgPxz1RPsYw6DX ikTpZt9l3w3+J5Tdyih2VtPcsUP95Pqf6dDGo8mLS3BABUza9IjVbYkUW2dT4rTPAux9 LwNRyHpcUB6FplUbv2YNgtL4O2bZFKqzyQ2Qs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=OxwlZr5tINv6Xc6wXSUoWMk0N3aa+XfaS6qHiYohACk=; b=gpNf1WUgGyIGDGDP7eF63ucWRm8nhHUEbzRk6bPGs1PwMDarXewfV/Rs7wgbAvMi2I cpFcLaSAWXd1R1HjoaHvex7n9zg7zDFI2qbiZpOMrEEbsH+fwZ9tBc9zwKruReXtBCyz L3ykfBRRxK8qMrhwkrkEyB+jb/ahhsRB053qU+mwLfD1EwxSCd/5Xq5thpgs51I99tp4 gJBR3gAtYnXQgoVoak1HGnV2jk+y2oMLULK3IozVvhTPJStn0SSSY6bKGKmhh+xT7tAf HwID/7jjYuN3DSK6yYobemrPj5n5MjV+JhdxUnm6E7BhesxdYEATZ+UvVpcDfyk9CMhf Djqw== X-Gm-Message-State: AIkVDXJ1diy844QsfzhRO9o0nFKfqFeSUN+jT+SeQ6FBqldW8Zvr5VQbqK4khLF0QYCEDe2X X-Received: by 10.28.181.145 with SMTP id e139mr23108407wmf.127.1485971113683; Wed, 01 Feb 2017 09:45:13 -0800 (PST) Received: from localhost.localdomain ([105.130.17.13]) by smtp.gmail.com with ESMTPSA id y30sm35312020wrc.23.2017.02.01.09.45.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 01 Feb 2017 09:45:12 -0800 (PST) From: Ard Biesheuvel To: leif.lindholm@linaro.org, mark.rutland@arm.com, Ingo Molnar , Thomas Gleixner , "H . Peter Anvin" Cc: linux-arm-kernel@lists.infradead.org, riku.voipio@linaro.org, matt@codeblueprint.co.uk, Ard Biesheuvel , linux-kernel@vger.kernel.org, linux-efi@vger.kernel.org Subject: [PATCH] efi: fdt: avoid FDT manipulation after ExitBootServices() Date: Wed, 1 Feb 2017 17:45:02 +0000 Message-Id: <1485971102-23330-2-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1485971102-23330-1-git-send-email-ard.biesheuvel@linaro.org> References: <1485971102-23330-1-git-send-email-ard.biesheuvel@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Some AArch64 UEFI implementations disable the MMU in ExitBootServices(), after which unaligned accesses to RAM are no longer supported. Commit abfb7b686a3e ("efi/libstub/arm*: Pass latest memory map to the kernel") fixed an issue in the memory map handling of the stub FDT code, but inadvertently created an issue with such firmwares, by moving some of the FDT manipulation to after the invocation of ExitBootServices(). Given that the stub's libfdt implementation uses the ordinary, accelerated string functions, which rely on hardware handling of unaligned accesses, manipulating the FDT with the MMU off may result in alignment faults. So fix the situation by moving the update_fdt_memmap() call into the callback function invoked by efi_exit_boot_services() right before it calls the ExitBootServices() UEFI service (which is arguably a better place for it anyway) Note that disabling the MMU in ExitBootServices() is not compliant with the UEFI spec, and carries great risk due to the fact that switching from cached to uncached memory accesses halfway through compiler generated code (i.e., involving a stack) can never be done in a way that is architecturally safe. Cc: Fixes: abfb7b686a3e ("efi/libstub/arm*: Pass latest memory map to the kernel") Tested-by: Riku Voipio Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/libstub/fdt.c | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) -- 2.7.4 Acked-by: Leif Lindholm diff --git a/drivers/firmware/efi/libstub/fdt.c b/drivers/firmware/efi/libstub/fdt.c index 921dfa047202..260c4b4b492e 100644 --- a/drivers/firmware/efi/libstub/fdt.c +++ b/drivers/firmware/efi/libstub/fdt.c @@ -187,6 +187,7 @@ static efi_status_t update_fdt_memmap(void *fdt, struct efi_boot_memmap *map) struct exit_boot_struct { efi_memory_desc_t *runtime_map; int *runtime_entry_count; + void *new_fdt_addr; }; static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg, @@ -202,7 +203,7 @@ static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg, efi_get_virtmap(*map->map, *map->map_size, *map->desc_size, p->runtime_map, p->runtime_entry_count); - return EFI_SUCCESS; + return update_fdt_memmap(p->new_fdt_addr, map); } /* @@ -300,22 +301,13 @@ efi_status_t allocate_new_fdt_and_exit_boot(efi_system_table_t *sys_table, priv.runtime_map = runtime_map; priv.runtime_entry_count = &runtime_entry_count; + priv.new_fdt_addr = (void *)*new_fdt_addr; status = efi_exit_boot_services(sys_table, handle, &map, &priv, exit_boot_func); if (status == EFI_SUCCESS) { efi_set_virtual_address_map_t *svam; - status = update_fdt_memmap((void *)*new_fdt_addr, &map); - if (status != EFI_SUCCESS) { - /* - * The kernel won't get far without the memory map, but - * may still be able to print something meaningful so - * return success here. - */ - return EFI_SUCCESS; - } - /* Install the new virtual address map */ svam = sys_table->runtime->set_virtual_address_map; status = svam(runtime_entry_count * desc_size, desc_size,