From patchwork Mon Oct 14 16:33:09 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 176290 Delivered-To: patch@linaro.org Received: by 2002:a92:7e96:0:0:0:0:0 with SMTP id q22csp4736972ill; Mon, 14 Oct 2019 09:33:25 -0700 (PDT) X-Google-Smtp-Source: APXvYqyC10feEfRtqiWU8JlxX4nPNKD3AsgVFEtL10v8/foz8EwezpSU0RWJs4c6ceoRZI/xa7/h X-Received: by 2002:a50:fc86:: with SMTP id f6mr29317524edq.233.1571070805115; Mon, 14 Oct 2019 09:33:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571070805; cv=none; d=google.com; s=arc-20160816; b=JYBoZEWaPKhAzejH6orbI1yf9Epqs9kIbEPgAQMeMmkBoP7rtRroumQ6TzP+03aYAk svn/T0RLmlHTocaoXhTzUvet3vMsRf/lhTRpLxwKsXnogNLZtPig500ngWyvyb0Z5j09 KpRdBGYxrNyn9K9faE/iiL2+B3M2IhrXUHugSyfvyR38fx/8w+AewJ/c5raNBvacV5aA V5XCIUcmvNrV/Yr/pMpldMKKvSz5a2VmO451vC6jpC1CAJxihpOGyCsM/ZdUHt6UW1z4 6GsBG1TDWiOFtikQNdtk2qs4MB/UuEux4OX6AiKmK/Y4be8rLIyJsuGWDha/0w4JgfxZ xGLg== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=yuEdphuMWOUGDahWSE97oqgmOlObKgpyvQgzFU8TRQU=; b=LXS9kKQ3iNISzQfPPjYcV2hK/wH1lK0WeLNR/8yBKkEL9K5W9eAt4zgRrO686F9EA0 dnPQFVcaNH/6c1T+cbnlfd/uXcmTB3E/3gcnsPTWU2jxF5uy0stGb6xBz2qlHptzg81s 0EjHMVoFZoBC4PNHzmkGAjYnwQ4lKOtZwokOcXqMJrZgAJcu8+QX7lWBAEYyI9xcu0q1 6KsrgTuPI6EV+tm7xq6izsG8Pw6VEKfLfmiCLpYs6qUoOhoUjHav2S9cERljDe4aN93H T9kHLDk2rMTL6Pu5HFy085kEQLkPhPMa1Lwh814excI0xliNZC9ath0MIAcT6KyFS9/b FbZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="THTDXG/3"; spf=pass (google.com: best guess record for domain of linux-efi-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-efi-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g30si13199576eda.2.2019.10.14.09.33.24; Mon, 14 Oct 2019 09:33:25 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-efi-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 header.s=google header.b="THTDXG/3"; spf=pass (google.com: best guess record for domain of linux-efi-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-efi-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 S1726637AbfJNQdY (ORCPT + 3 others); Mon, 14 Oct 2019 12:33:24 -0400 Received: from mail-wm1-f68.google.com ([209.85.128.68]:33659 "EHLO mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388127AbfJNQdY (ORCPT ); Mon, 14 Oct 2019 12:33:24 -0400 Received: by mail-wm1-f68.google.com with SMTP id r17so393522wme.0 for ; Mon, 14 Oct 2019 09:33:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=yuEdphuMWOUGDahWSE97oqgmOlObKgpyvQgzFU8TRQU=; b=THTDXG/3mOeZtb6dOK6qrH1eQvV7z52+jijnE+ULOhMbPReKZiIPTLCR3+W7HnWIkL RFmtL80kGqOr/FynSqFtzLiwv4TaClqwXnVtULNnpITi/cHhy+AnvWVfQCDZ1THjSHSh pduFTV40hbsfQR4K3wm6K4LGLOnaAkS193JT28eOdR7oOcCjbZSKoQKTYCquWrAWTeIo oQAUnFNuy4gdBy+94VmzR0AdgNrDXTc5KPDYLaOTSBxZUJQ8+7pDFhxOxm06B6gNgPEp fzYcPcRV2R9hc6PtL8jxMKRNQ3PcJnas8KU2DsmaK/FJZxgtzXs97TaHydGKq33TYgst Mpow== 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:mime-version :content-transfer-encoding; bh=yuEdphuMWOUGDahWSE97oqgmOlObKgpyvQgzFU8TRQU=; b=MprdklcMxjePDrX6KYVZlFm2cdoBxtFFemqg4Dx/0yTkBsIy7a+SK3QI0ZuQbVNCym aGMjbkL29h1QzA1hTPLSg9fGcQl8CP2fu91KL/kkEC086bQ/D+Zkg8/IE+jNYIZ5PDqM 1qW0CRezfAlcRmRaIncBtTZi2lO59krZiQ0T45gCE81u2UOdPEzL6XdmO9DWCMxIVCKh zf6bVInxNEHNuQJO/KvVMzpqlXu60tieU6lgGjUr9ptE7WFangU3yn4zFKS/9kAEhF0H deZfb3h8NmE0Rt1BP5aPw2zlszc11F4MsA/SxLqXRq12POkBGuvSnyqpZNLUHgp7lQTZ IwOw== X-Gm-Message-State: APjAAAWmp6s9Z5yKvH9RUwzi4lw/O0CzAEE+LtJUC1/N85aEXeDNlGhQ qBUDQhjmy/rpmW1oGgryWb9DoqB8i92L6A== X-Received: by 2002:a1c:a516:: with SMTP id o22mr16781400wme.116.1571070801893; Mon, 14 Oct 2019 09:33:21 -0700 (PDT) Received: from localhost.localdomain (laubervilliers-657-1-83-120.w92-154.abo.wanadoo.fr. [92.154.90.120]) by smtp.gmail.com with ESMTPSA id o70sm25785240wme.29.2019.10.14.09.33.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Oct 2019 09:33:21 -0700 (PDT) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Cc: linux-efi@vger.kernel.org, Ard Biesheuvel , Guillaume Gardet , Chester Lin Subject: [PATCH] efi: libstub/arm: account for firmware reserved memory at the base of RAM Date: Mon, 14 Oct 2019 18:33:09 +0200 Message-Id: <20191014163309.2860-1-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Sender: linux-efi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org The EFI stubloader for ARM starts out by allocating a 32 MB window at the base of RAM, in order to ensure that the decompressor (which blindly copies the uncompressed kernel into that window) does not overwrite other allocations that are made while running in the context of the EFI firmware. In some cases, (e.g., U-Boot running on the Raspberry Pi 2), this is causing boot failures because this initial allocation conflicts with a page of reserved memory at the base of RAM that contains the SMP spin tables and other pieces of firmware data and which was put there by the bootloader under the assumption that the TEXT_OFFSET window right below the kernel is only used partially during early boot, and will be left alone once the memory reservations are processed and taken into account. So let's permit reserved memory regions to exist in the region starting at the base of RAM, and ending at TEXT_OFFSET - 5 * PAGE_SIZE, which is the window below the kernel that is not touched by the early boot code. Cc: Guillaume Gardet Cc: Chester Lin Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/libstub/Makefile | 1 + drivers/firmware/efi/libstub/arm32-stub.c | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) -- 2.20.1 Acked-by: Chester Lin Tested-by: Guillaume Gardet diff --git a/drivers/firmware/efi/libstub/Makefile b/drivers/firmware/efi/libstub/Makefile index 0460c7581220..ee0661ddb25b 100644 --- a/drivers/firmware/efi/libstub/Makefile +++ b/drivers/firmware/efi/libstub/Makefile @@ -52,6 +52,7 @@ lib-$(CONFIG_EFI_ARMSTUB) += arm-stub.o fdt.o string.o random.o \ lib-$(CONFIG_ARM) += arm32-stub.o lib-$(CONFIG_ARM64) += arm64-stub.o +CFLAGS_arm32-stub.o := -DTEXT_OFFSET=$(TEXT_OFFSET) CFLAGS_arm64-stub.o := -DTEXT_OFFSET=$(TEXT_OFFSET) # diff --git a/drivers/firmware/efi/libstub/arm32-stub.c b/drivers/firmware/efi/libstub/arm32-stub.c index e8f7aefb6813..47aafeff3e01 100644 --- a/drivers/firmware/efi/libstub/arm32-stub.c +++ b/drivers/firmware/efi/libstub/arm32-stub.c @@ -195,6 +195,7 @@ efi_status_t handle_kernel_image(efi_system_table_t *sys_table, unsigned long dram_base, efi_loaded_image_t *image) { + unsigned long kernel_base; efi_status_t status; /* @@ -204,9 +205,18 @@ efi_status_t handle_kernel_image(efi_system_table_t *sys_table, * loaded. These assumptions are made by the decompressor, * before any memory map is available. */ - dram_base = round_up(dram_base, SZ_128M); + kernel_base = round_up(dram_base, SZ_128M); - status = reserve_kernel_base(sys_table, dram_base, reserve_addr, + /* + * Note that some platforms (notably, the Raspberry Pi 2) put + * spin-tables and other pieces of firmware at the base of RAM, + * abusing the fact that the window of TEXT_OFFSET bytes at the + * base of the kernel image is only partially used at the moment. + * (Up to 5 pages are used for the swapper page table) + */ + kernel_base += TEXT_OFFSET - 5 * PAGE_SIZE; + + status = reserve_kernel_base(sys_table, kernel_base, reserve_addr, reserve_size); if (status != EFI_SUCCESS) { pr_efi_err(sys_table, "Unable to allocate memory for uncompressed kernel.\n"); @@ -220,7 +230,7 @@ efi_status_t handle_kernel_image(efi_system_table_t *sys_table, *image_size = image->image_size; status = efi_relocate_kernel(sys_table, image_addr, *image_size, *image_size, - dram_base + MAX_UNCOMP_KERNEL_SIZE, 0); + kernel_base + MAX_UNCOMP_KERNEL_SIZE, 0); if (status != EFI_SUCCESS) { pr_efi_err(sys_table, "Failed to relocate kernel.\n"); efi_free(sys_table, *reserve_size, *reserve_addr);