[PATCH v3 3/4] LoongArch: Fix entry point in image header

Jiaxun Yang posted 4 patches 1 year, 8 months ago
[PATCH v3 3/4] LoongArch: Fix entry point in image header
Posted by Jiaxun Yang 1 year, 8 months ago
Currently kernel entry in head.S is in DMW address range,
firmware is instructed to jump to this address after loading
the image.

However kernel should not make any assumption on firmware's
DMW setting, thus the entry point should be a physical address
falls into direct translation region.

Fix by converting entry address to physical and amend entry
calculation logic in libstub accordingly.

Cc: stable@vger.kernel.org
Signed-off-by: Jiaxun Yang <jiaxun.yang@flygoat.com>
---
v2: Fix efistub
v3: Move calculation to linker script
---
 arch/loongarch/kernel/head.S             | 2 +-
 arch/loongarch/kernel/vmlinux.lds.S      | 2 ++
 drivers/firmware/efi/libstub/loongarch.c | 2 +-
 3 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/arch/loongarch/kernel/head.S b/arch/loongarch/kernel/head.S
index c4f7de2e2805..2cdc1ea808d9 100644
--- a/arch/loongarch/kernel/head.S
+++ b/arch/loongarch/kernel/head.S
@@ -22,7 +22,7 @@
 _head:
 	.word	MZ_MAGIC		/* "MZ", MS-DOS header */
 	.org	0x8
-	.dword	kernel_entry		/* Kernel entry point */
+	.dword	_kernel_entry_phys	/* Kernel entry point (physical address) */
 	.dword	_kernel_asize		/* Kernel image effective size */
 	.quad	PHYS_LINK_KADDR		/* Kernel image load offset from start of RAM */
 	.org	0x38			/* 0x20 ~ 0x37 reserved */
diff --git a/arch/loongarch/kernel/vmlinux.lds.S b/arch/loongarch/kernel/vmlinux.lds.S
index e8e97dbf9ca4..c6f89e51257a 100644
--- a/arch/loongarch/kernel/vmlinux.lds.S
+++ b/arch/loongarch/kernel/vmlinux.lds.S
@@ -6,6 +6,7 @@
 
 #define PAGE_SIZE _PAGE_SIZE
 #define RO_EXCEPTION_TABLE_ALIGN	4
+#define TO_PHYS_MASK			0x000fffffffffffff /* 48-bit */
 
 /*
  * Put .bss..swapper_pg_dir as the first thing in .bss. This will
@@ -142,6 +143,7 @@ SECTIONS
 
 #ifdef CONFIG_EFI_STUB
 	/* header symbols */
+	_kernel_entry_phys = kernel_entry & TO_PHYS_MASK;
 	_kernel_asize = _end - _text;
 	_kernel_fsize = _edata - _text;
 	_kernel_vsize = _end - __initdata_begin;
diff --git a/drivers/firmware/efi/libstub/loongarch.c b/drivers/firmware/efi/libstub/loongarch.c
index 684c9354637c..60c145121393 100644
--- a/drivers/firmware/efi/libstub/loongarch.c
+++ b/drivers/firmware/efi/libstub/loongarch.c
@@ -41,7 +41,7 @@ static efi_status_t exit_boot_func(struct efi_boot_memmap *map, void *priv)
 unsigned long __weak kernel_entry_address(unsigned long kernel_addr,
 		efi_loaded_image_t *image)
 {
-	return *(unsigned long *)(kernel_addr + 8) - VMLINUX_LOAD_ADDRESS + kernel_addr;
+	return *(unsigned long *)(kernel_addr + 8) - TO_PHYS(VMLINUX_LOAD_ADDRESS) + kernel_addr;
 }
 
 efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,

-- 
2.43.0
Re: [PATCH v3 3/4] LoongArch: Fix entry point in image header
Posted by WANG Rui 1 year, 8 months ago
On Thu, May 23, 2024 at 6:03 AM Jiaxun Yang <jiaxun.yang@flygoat.com> wrote:
>
> Currently kernel entry in head.S is in DMW address range,
> firmware is instructed to jump to this address after loading
> the image.
>
> However kernel should not make any assumption on firmware's
> DMW setting, thus the entry point should be a physical address
> falls into direct translation region.
>
> Fix by converting entry address to physical and amend entry
> calculation logic in libstub accordingly.
>
> Cc: stable@vger.kernel.org
> Signed-off-by: Jiaxun Yang <jiaxun.yang@flygoat.com>
> ---
> v2: Fix efistub
> v3: Move calculation to linker script
> ---
>  arch/loongarch/kernel/head.S             | 2 +-
>  arch/loongarch/kernel/vmlinux.lds.S      | 2 ++
>  drivers/firmware/efi/libstub/loongarch.c | 2 +-
>  3 files changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/arch/loongarch/kernel/head.S b/arch/loongarch/kernel/head.S
> index c4f7de2e2805..2cdc1ea808d9 100644
> --- a/arch/loongarch/kernel/head.S
> +++ b/arch/loongarch/kernel/head.S
> @@ -22,7 +22,7 @@
>  _head:
>         .word   MZ_MAGIC                /* "MZ", MS-DOS header */
>         .org    0x8
> -       .dword  kernel_entry            /* Kernel entry point */
> +       .dword  _kernel_entry_phys      /* Kernel entry point (physical address) */
>         .dword  _kernel_asize           /* Kernel image effective size */
>         .quad   PHYS_LINK_KADDR         /* Kernel image load offset from start of RAM */
>         .org    0x38                    /* 0x20 ~ 0x37 reserved */
> diff --git a/arch/loongarch/kernel/vmlinux.lds.S b/arch/loongarch/kernel/vmlinux.lds.S
> index e8e97dbf9ca4..c6f89e51257a 100644
> --- a/arch/loongarch/kernel/vmlinux.lds.S
> +++ b/arch/loongarch/kernel/vmlinux.lds.S
> @@ -6,6 +6,7 @@
>
>  #define PAGE_SIZE _PAGE_SIZE
>  #define RO_EXCEPTION_TABLE_ALIGN       4
> +#define TO_PHYS_MASK                   0x000fffffffffffff /* 48-bit */
>
>  /*
>   * Put .bss..swapper_pg_dir as the first thing in .bss. This will
> @@ -142,6 +143,7 @@ SECTIONS
>
>  #ifdef CONFIG_EFI_STUB
>         /* header symbols */
> +       _kernel_entry_phys = kernel_entry & TO_PHYS_MASK;

 -       _kernel_entry_phys = kernel_entry & TO_PHYS_MASK;
 +       _kernel_entry_phys = ABSOLUTE(kernel_entry & TO_PHYS_MASK);

>         _kernel_asize = _end - _text;
>         _kernel_fsize = _edata - _text;
>         _kernel_vsize = _end - __initdata_begin;
> diff --git a/drivers/firmware/efi/libstub/loongarch.c b/drivers/firmware/efi/libstub/loongarch.c
> index 684c9354637c..60c145121393 100644
> --- a/drivers/firmware/efi/libstub/loongarch.c
> +++ b/drivers/firmware/efi/libstub/loongarch.c
> @@ -41,7 +41,7 @@ static efi_status_t exit_boot_func(struct efi_boot_memmap *map, void *priv)
>  unsigned long __weak kernel_entry_address(unsigned long kernel_addr,
>                 efi_loaded_image_t *image)
>  {
> -       return *(unsigned long *)(kernel_addr + 8) - VMLINUX_LOAD_ADDRESS + kernel_addr;
> +       return *(unsigned long *)(kernel_addr + 8) - TO_PHYS(VMLINUX_LOAD_ADDRESS) + kernel_addr;
>  }
>
>  efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
>
> --
> 2.43.0
>
>

- Rui
Re: [PATCH v3 3/4] LoongArch: Fix entry point in image header
Posted by Nathan Chancellor 1 year, 8 months ago
Hi Jiaxun,

On Wed, May 22, 2024 at 11:02:19PM +0100, Jiaxun Yang wrote:
> Currently kernel entry in head.S is in DMW address range,
> firmware is instructed to jump to this address after loading
> the image.
> 
> However kernel should not make any assumption on firmware's
> DMW setting, thus the entry point should be a physical address
> falls into direct translation region.
> 
> Fix by converting entry address to physical and amend entry
> calculation logic in libstub accordingly.
> 
> Cc: stable@vger.kernel.org
> Signed-off-by: Jiaxun Yang <jiaxun.yang@flygoat.com>
> ---
> v2: Fix efistub
> v3: Move calculation to linker script
> ---
>  arch/loongarch/kernel/head.S             | 2 +-
>  arch/loongarch/kernel/vmlinux.lds.S      | 2 ++
>  drivers/firmware/efi/libstub/loongarch.c | 2 +-
>  3 files changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/loongarch/kernel/head.S b/arch/loongarch/kernel/head.S
> index c4f7de2e2805..2cdc1ea808d9 100644
> --- a/arch/loongarch/kernel/head.S
> +++ b/arch/loongarch/kernel/head.S
> @@ -22,7 +22,7 @@
>  _head:
>  	.word	MZ_MAGIC		/* "MZ", MS-DOS header */
>  	.org	0x8
> -	.dword	kernel_entry		/* Kernel entry point */
> +	.dword	_kernel_entry_phys	/* Kernel entry point (physical address) */
>  	.dword	_kernel_asize		/* Kernel image effective size */
>  	.quad	PHYS_LINK_KADDR		/* Kernel image load offset from start of RAM */
>  	.org	0x38			/* 0x20 ~ 0x37 reserved */
> diff --git a/arch/loongarch/kernel/vmlinux.lds.S b/arch/loongarch/kernel/vmlinux.lds.S
> index e8e97dbf9ca4..c6f89e51257a 100644
> --- a/arch/loongarch/kernel/vmlinux.lds.S
> +++ b/arch/loongarch/kernel/vmlinux.lds.S
> @@ -6,6 +6,7 @@
>  
>  #define PAGE_SIZE _PAGE_SIZE
>  #define RO_EXCEPTION_TABLE_ALIGN	4
> +#define TO_PHYS_MASK			0x000fffffffffffff /* 48-bit */
>  
>  /*
>   * Put .bss..swapper_pg_dir as the first thing in .bss. This will
> @@ -142,6 +143,7 @@ SECTIONS
>  
>  #ifdef CONFIG_EFI_STUB
>  	/* header symbols */
> +	_kernel_entry_phys = kernel_entry & TO_PHYS_MASK;
>  	_kernel_asize = _end - _text;
>  	_kernel_fsize = _edata - _text;
>  	_kernel_vsize = _end - __initdata_begin;
> diff --git a/drivers/firmware/efi/libstub/loongarch.c b/drivers/firmware/efi/libstub/loongarch.c
> index 684c9354637c..60c145121393 100644
> --- a/drivers/firmware/efi/libstub/loongarch.c
> +++ b/drivers/firmware/efi/libstub/loongarch.c
> @@ -41,7 +41,7 @@ static efi_status_t exit_boot_func(struct efi_boot_memmap *map, void *priv)
>  unsigned long __weak kernel_entry_address(unsigned long kernel_addr,
>  		efi_loaded_image_t *image)
>  {
> -	return *(unsigned long *)(kernel_addr + 8) - VMLINUX_LOAD_ADDRESS + kernel_addr;
> +	return *(unsigned long *)(kernel_addr + 8) - TO_PHYS(VMLINUX_LOAD_ADDRESS) + kernel_addr;
>  }
>  
>  efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
> 
> -- 
> 2.43.0
> 

This patch is now in -next as commit 75461304ee4e ("LoongArch: Fix entry
point in kernel image header"). I just bisected a build failure that I
see when building with LLVM (either 18 or 19) to this change.

$ make -skj"$(nproc)" ARCH=loongarch LLVM=1 defconfig vmlinux
...
kallsyms failure: relative symbol value 0x9000000000200000 out of range in relative mode
make[4]: *** [scripts/Makefile.vmlinux:34: vmlinux] Error 1
...

Does kallsyms need some adjustment for this?

Cheers,
Nathan

# bad: [9d99040b1bc8dbf385a8aa535e9efcdf94466e19] Add linux-next specific files for 20240529
# good: [e0cce98fe279b64f4a7d81b7f5c3a23d80b92fbc] Merge tag 'tpmdd-next-6.10-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd
git bisect start '9d99040b1bc8dbf385a8aa535e9efcdf94466e19' 'e0cce98fe279b64f4a7d81b7f5c3a23d80b92fbc'
# bad: [270c6bb9d5e8448b74950f23ff2a192faaf10428] Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/wireless/wireless-next.git
git bisect bad 270c6bb9d5e8448b74950f23ff2a192faaf10428
# good: [c38b067bf2ab58d93590a50cbc06c992fe00447e] Merge branch 'ti-next' of git://git.kernel.org/pub/scm/linux/kernel/git/ti/linux.git
git bisect good c38b067bf2ab58d93590a50cbc06c992fe00447e
# bad: [6bcfb2dcf8b00c0b4cef68ac026c71dae3c25070] Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/printk/linux.git
git bisect bad 6bcfb2dcf8b00c0b4cef68ac026c71dae3c25070
# good: [2ca0cd490407a728a9aa57b9538f3ca8b287a089] Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/gfs2/linux-gfs2.git
git bisect good 2ca0cd490407a728a9aa57b9538f3ca8b287a089
# good: [f3760c80d06a838495185c0fe341c043e6495142] Merge branch 'rework/write-atomic' into for-next
git bisect good f3760c80d06a838495185c0fe341c043e6495142
# good: [b9fc9904efcaea8470f0d4cd0691f1295add9381] Merge branch 'vfs.module.description' into vfs.all
git bisect good b9fc9904efcaea8470f0d4cd0691f1295add9381
# good: [88dbb5f3c068ba8944e97235ccfdc5fbd6c7d577] Merge branch '9p-next' of git://github.com/martinetd/linux
git bisect good 88dbb5f3c068ba8944e97235ccfdc5fbd6c7d577
# bad: [b4660cd50cb1e5821532e34dbc7f47cb155ba57b] Merge branch 'next' of git://git.monstr.eu/linux-2.6-microblaze.git
git bisect bad b4660cd50cb1e5821532e34dbc7f47cb155ba57b
# bad: [c768fc96978cd0f74dd297d58720cb984a7f6341] LoongArch: Override higher address bits in JUMP_VIRT_ADDR
git bisect bad c768fc96978cd0f74dd297d58720cb984a7f6341
# good: [2624e739c2e9abe5f6cc9acc37f9752f0055fb5f] LoongArch: Add all CPUs enabled by fdt to NUMA node 0
git bisect good 2624e739c2e9abe5f6cc9acc37f9752f0055fb5f
# bad: [75461304ee4e7e2cb282265a6a89c35b81282d19] LoongArch: Fix entry point in kernel image header
git bisect bad 75461304ee4e7e2cb282265a6a89c35b81282d19
# first bad commit: [75461304ee4e7e2cb282265a6a89c35b81282d19] LoongArch: Fix entry point in kernel image header