Skip to contentSkip to conversation

efi/libstub: Ensure instruction cache coherency after kernel relocation

PATCH Superseded WANG Rui 2 participants 5 messages Apr 27

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

WANG Rui

author
#0 lore raw

cover efi/libstub: Ensure instruction cache coherency after kernel relocation

The EFI stub relocates the kernel image by copying it to a new memory location using memcpy(). On architectures where the instruction and data caches are not automatically coherent, the copied instructions may not be visible to the instruction fetch unit without explicit cache maintenance.

This series adds the missing call to efi_cache_sync_image() in the generic relocation path, and provides a LoongArch implementation using the ibar instruction to ensure instruction fetches observe the updated contents.

WANG Rui (2):
efi/loongarch: Implement efi_cache_sync_image efi/libstub: Synchronize instruction cache after kernel relocation

 drivers/firmware/efi/libstub/loongarch.c | 5 +++++
 drivers/firmware/efi/libstub/relocate.c  | 1 +
 2 files changed, 6 insertions(+)
Signature · 1 line
2.54.0
author
#1 lore raw

PATCH 1/2 efi/loongarch: Implement efi_cache_sync_image

Provide a LoongArch implementation of efi_cache_sync_image() to ensure instruction cache coherency after the kernel image is relocated.

---
 drivers/firmware/efi/libstub/loongarch.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/firmware/efi/libstub/loongarch.c +5 -0
index 9825f5218137..26495747f69f 100644
@@ -82,3 +82,8 @@ efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
 	real_kernel_entry(true, (unsigned long)cmdline_ptr,
 			  (unsigned long)efi_system_table);
 }
+
+void efi_cache_sync_image(unsigned long image_base, unsigned long alloc_size)
+{
+	asm volatile ("ibar 0" ::: "memory");
+}
Signature · 1 line
2.54.0
maintainer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] efi/loongarch: Implement efi_cache_sync_image

Hi, Rui,

Quoted 3 lines · WANG Rui

Provide a LoongArch implementation of efi_cache_sync_image() to ensure instruction cache coherency after the kernel image is relocated.

Please also add "()" after function name in the subject.

Quoted 11 lines
---
 drivers/firmware/efi/libstub/loongarch.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/firmware/efi/libstub/loongarch.c +0 -0
index 9825f5218137..26495747f69f 100644

drivers/firmware/efi/libstub/loongarch.c efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image, · line 89

Quoted 8 lines
drivers/firmware/efi/libstub/loongarch.c +5 -0
@@ -82,3 +82,8 @@ efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
        real_kernel_entry(true, (unsigned long)cmdline_ptr,
                          (unsigned long)efi_system_table);
 }
+
+void efi_cache_sync_image(unsigned long image_base, unsigned long alloc_size)
+{
+       asm volatile ("ibar 0" ::: "memory");
+}

Please move it after check_platform_features(), which is the same as arm64.

Others look good to me.

Quoted 4 lines

--
2.54.0

author
#3 lore raw

PATCH 2/2 efi/libstub: Synchronize instruction cache after kernel relocation

In reply to WANG Rui

The relocated kernel image is copied to its new location using memcpy(). On architectures with separate instruction and data caches, the copied instructions may remain stale in the instruction cache, leading to the execution of outdated contents.

Call efi_cache_sync_image() after the relocation copy to ensure the instruction cache is synchronized with the updated memory contents before control is transferred to the relocated kernel.

---
 drivers/firmware/efi/libstub/relocate.c | 1 +
 1 file changed, 1 insertion(+)
drivers/firmware/efi/libstub/relocate.c +1 -0
index d4264bfb6dc1..913b425d089d 100644
@@ -158,6 +158,7 @@ efi_status_t efi_relocate_kernel(unsigned long *image_addr,
 	 * have been allocated by UEFI, so we can safely use memcpy.
 	 */
 	memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
+	efi_cache_sync_image(new_addr, image_size);
 
 	/* Return the new address of the relocated image. */
 	*image_addr = new_addr;
Signature · 1 line
2.54.0
maintainer Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] efi/libstub: Synchronize instruction cache after kernel relocation

Quoted 11 lines · WANG Rui

The relocated kernel image is copied to its new location using memcpy(). On architectures with separate instruction and data caches, the copied instructions may remain stale in the instruction cache, leading to the execution of outdated contents.

Call efi_cache_sync_image() after the relocation copy to ensure the instruction cache is synchronized with the updated memory contents before control is transferred to the relocated kernel.

Quoted 19 lines
---
 drivers/firmware/efi/libstub/relocate.c | 1 +
 1 file changed, 1 insertion(+)
drivers/firmware/efi/libstub/relocate.c +1 -1
index d4264bfb6dc1..913b425d089d 100644
@@ -158,6 +158,7 @@ efi_status_t efi_relocate_kernel(unsigned long *image_addr,
         * have been allocated by UEFI, so we can safely use memcpy.
         */
        memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
+       efi_cache_sync_image(new_addr, image_size);
 
        /* Return the new address of the relocated image. */
        *image_addr = new_addr;
--

2.54.0