riscv: cleanup XIP_FIXUP macro
authorNam Cao <namcao@linutronix.de>
Fri, 7 Jun 2024 20:22:06 +0000 (22:22 +0200)
committerPalmer Dabbelt <palmer@rivosinc.com>
Thu, 12 Sep 2024 14:22:55 +0000 (07:22 -0700)
The XIP_FIXUP macro is used to fix addresses early during boot before MMU:
generated code "thinks" the data section is in ROM while it is actually in
RAM. So this macro corrects the addresses in the data section.

This macro determines if the address needs to be fixed by checking if it is
within the range starting from ROM address up to the size of (2 *
XIP_OFFSET).

This means if the kernel size is bigger than (2 * XIP_OFFSET), some
addresses would not be fixed up.

XIP kernel can still work if the above scenario does not happen. But this
macro is obviously incorrect.

Rewrite this macro to only fix up addresses within the data section.

Signed-off-by: Nam Cao <namcao@linutronix.de>
Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Link: https://lore.kernel.org/r/95f50a4ec8204ec4fcbf2a80c9addea0e0609e3b.1717789719.git.namcao@linutronix.de
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
arch/riscv/include/asm/pgtable.h

index 089f3c9f56a389249382a698ae46936c95d970f1..6e16ee77619f15bc25bbf84a5d6d830615db2d12 100644 (file)
 
 #ifdef CONFIG_XIP_KERNEL
 #define XIP_FIXUP(addr) ({                                                     \
+       extern char _sdata[], _start[], _end[];                                 \
+       uintptr_t __rom_start_data = CONFIG_XIP_PHYS_ADDR                       \
+                               + (uintptr_t)&_sdata - (uintptr_t)&_start;      \
+       uintptr_t __rom_end_data = CONFIG_XIP_PHYS_ADDR                         \
+                               + (uintptr_t)&_end - (uintptr_t)&_start;        \
        uintptr_t __a = (uintptr_t)(addr);                                      \
-       (__a >= CONFIG_XIP_PHYS_ADDR && \
-        __a < CONFIG_XIP_PHYS_ADDR + XIP_OFFSET * 2) ? \
-               __a - CONFIG_XIP_PHYS_ADDR + CONFIG_PHYS_RAM_BASE - XIP_OFFSET :\
-               __a;                                                            \
+       (__a >= __rom_start_data && __a < __rom_end_data) ?                     \
+               __a - __rom_start_data + CONFIG_PHYS_RAM_BASE : __a;            \
        })
 #else
 #define XIP_FIXUP(addr)                (addr)