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

Jiaxun Yang posted 4 patches 1 year, 8 months ago
There is a newer version of this series
[PATCH v2 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 applying a calculation to the entry and amend entry
calculation logic in libstub accordingly.

Note that due to relocation restriction TO_PHYS can't be used
in assembly, we can only do plus and minus here.

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

diff --git a/arch/loongarch/kernel/head.S b/arch/loongarch/kernel/head.S
index c4f7de2e2805..1a83564023e1 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	PHYS_LINK_KADDR + (kernel_entry	- _head)	/* Kernel entry point */
 	.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/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 v2 3/4] LoongArch: Fix entry point in image header
Posted by Huacai Chen 1 year, 8 months ago
Hi, Jiaxun,

On Wed, May 22, 2024 at 2:30 PM 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 applying a calculation to the entry and amend entry
> calculation logic in libstub accordingly.
>
> Note that due to relocation restriction TO_PHYS can't be used
> in assembly, we can only do plus and minus here.
>
> Cc: stable@vger.kernel.org
> Signed-off-by: Jiaxun Yang <jiaxun.yang@flygoat.com>
> ---
> v2: Fix efistub
> ---
>  arch/loongarch/kernel/head.S             | 2 +-
>  drivers/firmware/efi/libstub/loongarch.c | 2 +-
>  2 files changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/arch/loongarch/kernel/head.S b/arch/loongarch/kernel/head.S
> index c4f7de2e2805..1a83564023e1 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  PHYS_LINK_KADDR + (kernel_entry - _head)        /* Kernel entry point */
It could be better to calculate it in the link script, just as _kernel_asize.

Huacai

>         .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/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
>
>