2024-02-22 13:12:18 +03:00
|
|
|
From bd997a1e8e262706b5a65cb1ff48f939c698b835 Mon Sep 17 00:00:00 2001
|
2023-03-15 17:53:05 +03:00
|
|
|
From: Alexey Sheplyakov <asheplyakov@basealt.ru>
|
|
|
|
Date: Mon, 23 May 2022 19:28:24 +0400
|
|
|
|
Subject: [PATCH 607/631] arm64-stub: fixed secondary cores boot on Baikal-M
|
|
|
|
SoC
|
|
|
|
|
|
|
|
Old versions of Baikal-M firmware (ARM-TF) deny execution attempts
|
|
|
|
outside of the (physical) address ranges
|
|
|
|
[0x80000000, 0x8FFFFFFF] and [0xA0000000, 0xBFFFFFFF]
|
|
|
|
Thus PSCI calls to boot secondary cores fail unless the kernel image
|
|
|
|
resides in one of these address ranges. However UEFI PE/COFF loader
|
|
|
|
puts the kernel image into the forbidden range. Since the alignment
|
|
|
|
is good enough EFI stub does not try to relocate the kernel.
|
|
|
|
As a result secondary CPUs fail to boot.
|
|
|
|
|
|
|
|
Relocation to a random address is not going to work either.
|
|
|
|
Therefore automatically disable kaslr on "known bad" systems (for
|
|
|
|
now only Baikal-M) and forcibly relocate the kernel to a low(er)
|
|
|
|
address.
|
|
|
|
|
|
|
|
This patch is necessary only for old firmware (pre SDK-M 5.1) and
|
|
|
|
prevents kalsr from working on Baikal-M systems.
|
|
|
|
|
|
|
|
Signed-off-by: Alexey Sheplyakov <asheplyakov@basealt.ru>
|
|
|
|
X-DONTUPSTREAM
|
|
|
|
X-legacy
|
|
|
|
X-feature-Baikal-M
|
2024-02-22 13:12:18 +03:00
|
|
|
|
|
|
|
[ Adapted for kernel 6.6 ]
|
|
|
|
Signed-off-by: Mikhail Novosyolov <m.novosyolov@rosalinux.ru>
|
2023-03-15 17:53:05 +03:00
|
|
|
---
|
2024-02-22 13:12:18 +03:00
|
|
|
drivers/firmware/efi/libstub/kaslr.c | 61 +++++++++++++++++++++++++++-
|
|
|
|
1 file changed, 60 insertions(+), 1 deletion(-)
|
2023-03-15 17:53:05 +03:00
|
|
|
|
2024-02-22 13:12:18 +03:00
|
|
|
diff --git a/drivers/firmware/efi/libstub/kaslr.c b/drivers/firmware/efi/libstub/kaslr.c
|
|
|
|
index 1a9808012abd..bbbf496853cd 100644
|
|
|
|
--- a/drivers/firmware/efi/libstub/kaslr.c
|
|
|
|
+++ b/drivers/firmware/efi/libstub/kaslr.c
|
|
|
|
@@ -4,9 +4,47 @@
|
|
|
|
* architectures to deal with physical address space randomization.
|
|
|
|
*/
|
|
|
|
#include <linux/efi.h>
|
2023-03-15 17:53:05 +03:00
|
|
|
+#include <linux/libfdt.h>
|
|
|
|
|
|
|
|
#include "efistub.h"
|
|
|
|
|
2024-02-22 13:12:18 +03:00
|
|
|
+static inline efi_status_t efi_low_alloc(unsigned long size, unsigned long align,
|
|
|
|
+ unsigned long *addr)
|
|
|
|
+{
|
|
|
|
+ /*
|
|
|
|
+ * Don't allocate at 0x0. It will confuse code that
|
|
|
|
+ * checks pointers against NULL. Skip the first 8
|
|
|
|
+ * bytes so we start at a nice even number.
|
|
|
|
+ */
|
|
|
|
+ return efi_low_alloc_above(size, align, addr, 0x8);
|
|
|
|
+}
|
|
|
|
+
|
2023-03-15 17:53:05 +03:00
|
|
|
+static const char* machines_need_low_alloc[] = {
|
|
|
|
+ "baikal,baikal-m",
|
|
|
|
+ "baikal,bm1000",
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static bool need_low_alloc(void) {
|
|
|
|
+ size_t i;
|
|
|
|
+ const void *fdt;
|
|
|
|
+ const char *match;
|
|
|
|
+
|
|
|
|
+ fdt = get_efi_config_table(DEVICE_TREE_GUID);
|
|
|
|
+ if (!fdt) {
|
|
|
|
+ efi_info("failed to retrive FDT from EFI\n");
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (i = 0; i < ARRAY_SIZE(machines_need_low_alloc); i++) {
|
|
|
|
+ match = machines_need_low_alloc[i];
|
|
|
|
+ if (fdt_node_check_compatible(fdt, 0, match) == 0) {
|
|
|
|
+ efi_info("machine %s: forcing kernel relocation to low address\n", match);
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+}
|
|
|
|
+
|
2024-02-22 13:12:18 +03:00
|
|
|
/**
|
|
|
|
* efi_kaslr_get_phys_seed() - Get random seed for physical kernel KASLR
|
|
|
|
* @image_handle: Handle to the image
|
|
|
|
@@ -26,8 +64,17 @@ u32 efi_kaslr_get_phys_seed(efi_handle_t image_handle)
|
|
|
|
if (!IS_ENABLED(CONFIG_RANDOMIZE_BASE))
|
|
|
|
return 0;
|
2023-03-15 17:53:05 +03:00
|
|
|
|
|
|
|
+ bool force_low_reloc = need_low_alloc();
|
|
|
|
+ if (force_low_reloc) {
|
|
|
|
+ if (!efi_nokaslr) {
|
|
|
|
+ efi_info("booting on a broken firmware, KASLR will be disabled\n");
|
|
|
|
+ efi_nokaslr = true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
2024-02-22 13:12:18 +03:00
|
|
|
if (efi_nokaslr) {
|
|
|
|
- efi_info("KASLR disabled on kernel command line\n");
|
|
|
|
+ if (!force_low_reloc)
|
|
|
|
+ efi_info("KASLR disabled on kernel command line\n");
|
|
|
|
} else if (efi_bs_call(handle_protocol, image_handle,
|
|
|
|
&li_fixed_proto, &p) == EFI_SUCCESS) {
|
|
|
|
efi_info("Image placement fixed by loader\n");
|
|
|
|
@@ -126,6 +173,15 @@ efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr,
|
2023-03-15 17:53:05 +03:00
|
|
|
status = EFI_OUT_OF_RESOURCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ if (force_low_reloc) {
|
|
|
|
+ status = efi_low_alloc(*reserve_size,
|
|
|
|
+ min_kimg_align,
|
|
|
|
+ reserve_addr);
|
|
|
|
+ if (status != EFI_SUCCESS) {
|
|
|
|
+ efi_err("Failed to relocate kernel, expect secondary CPUs boot failure\n");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
if (status != EFI_SUCCESS) {
|
2024-02-22 13:12:18 +03:00
|
|
|
if (!check_image_region(*image_addr, kernel_memsize)) {
|
2023-03-15 17:53:05 +03:00
|
|
|
efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n");
|
2024-02-22 13:12:18 +03:00
|
|
|
@@ -152,6 +208,9 @@ efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr,
|
2023-03-15 17:53:05 +03:00
|
|
|
|
2024-02-22 13:12:18 +03:00
|
|
|
memcpy((void *)*reserve_addr, (void *)*image_addr, kernel_size);
|
2023-03-15 17:53:05 +03:00
|
|
|
*image_addr = *reserve_addr;
|
|
|
|
+ if (efi_nokaslr) {
|
|
|
|
+ efi_info("relocating kernel to 0x%lx\n", *image_addr);
|
|
|
|
+ }
|
2024-02-22 13:12:18 +03:00
|
|
|
efi_icache_sync(*image_addr, *image_addr + kernel_codesize);
|
|
|
|
efi_remap_image(*image_addr, *reserve_size, kernel_codesize);
|
2023-03-15 17:53:05 +03:00
|
|
|
|
|
|
|
--
|
2024-02-22 13:12:18 +03:00
|
|
|
2.40.1
|
2023-03-15 17:53:05 +03:00
|
|
|
|